This is an automated email from the ASF dual-hosted git repository. duanzhengqiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push: new 3e941857e08 Refactor SelectStatementContext. (#34845) 3e941857e08 is described below commit 3e941857e086e3712d7a9e2ca283c727c5e67d06 Author: Cong Hu <iamhuc...@gmail.com> AuthorDate: Tue Mar 4 09:45:53 2025 +0800 Refactor SelectStatementContext. (#34845) --- .../encrypt/merge/dql/EncryptMergedResult.java | 14 +++++----- .../encrypt/merge/dql/EncryptMergedResultTest.java | 31 +++++++++------------- .../statement/dml/SelectStatementContext.java | 11 ++++---- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java index 654378c557f..33d2a8f3326 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java @@ -21,13 +21,13 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.encrypt.exception.data.DecryptFailedException; import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier; import org.apache.shardingsphere.infra.merge.result.MergedResult; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo; import java.io.InputStream; import java.io.Reader; @@ -56,14 +56,14 @@ public final class EncryptMergedResult implements MergedResult { @Override public Object getValue(final int columnIndex, final Class<?> type) throws SQLException { - Optional<ColumnProjection> columnProjection = selectStatementContext.findColumnProjection(columnIndex); - if (!columnProjection.isPresent()) { + Optional<ColumnSegmentBoundInfo> columnSegmentBoundInfo = selectStatementContext.findColumnBoundInfo(columnIndex); + if (!columnSegmentBoundInfo.isPresent()) { return mergedResult.getValue(columnIndex, type); } - String originalTableName = columnProjection.get().getOriginalTable().getValue(); - String originalColumnName = columnProjection.get().getOriginalColumn().getValue(); - ShardingSphereDatabase database = metaData.containsDatabase(columnProjection.get().getColumnBoundInfo().getOriginalDatabase().getValue()) - ? metaData.getDatabase(columnProjection.get().getColumnBoundInfo().getOriginalDatabase().getValue()) + String originalTableName = columnSegmentBoundInfo.get().getOriginalTable().getValue(); + String originalColumnName = columnSegmentBoundInfo.get().getOriginalColumn().getValue(); + ShardingSphereDatabase database = metaData.containsDatabase(columnSegmentBoundInfo.get().getOriginalDatabase().getValue()) + ? metaData.getDatabase(columnSegmentBoundInfo.get().getOriginalDatabase().getValue()) : this.database; Optional<EncryptRule> rule = database.getRuleMetaData().findSingleRule(EncryptRule.class); if (!rule.isPresent() || !rule.get().findEncryptTable(originalTableName).map(optional -> optional.isEncryptColumn(originalColumnName)).orElse(false)) { diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java index 68303e921ba..898289d0944 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java @@ -24,7 +24,6 @@ import org.apache.shardingsphere.encrypt.rule.column.item.CipherColumnItem; import org.apache.shardingsphere.encrypt.rule.table.EncryptTable; import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.merge.result.MergedResult; @@ -81,7 +80,7 @@ class EncryptMergedResultTest { @Test void assertGetValueWithoutColumnProjection() throws SQLException { - when(selectStatementContext.findColumnProjection(1)).thenReturn(Optional.empty()); + when(selectStatementContext.findColumnBoundInfo(1)).thenReturn(Optional.empty()); when(mergedResult.getValue(1, String.class)).thenReturn("foo_value"); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); assertThat(new EncryptMergedResult(database, mock(), selectStatementContext, mergedResult).getValue(1, String.class), is("foo_value")); @@ -89,9 +88,8 @@ class EncryptMergedResultTest { @Test void assertGetValueWithoutEncryptTable() throws SQLException { - ColumnProjection columnProjection = new ColumnProjection(new IdentifierValue("bar_tbl"), new IdentifierValue("foo_col"), new IdentifierValue("foo_alias"), databaseType, - null, null, new ColumnSegmentBoundInfo(new IdentifierValue("foo_col"))); - when(selectStatementContext.findColumnProjection(1)).thenReturn(Optional.of(columnProjection)); + ColumnSegmentBoundInfo columnSegmentBoundInfo = new ColumnSegmentBoundInfo(new IdentifierValue("foo_col")); + when(selectStatementContext.findColumnBoundInfo(1)).thenReturn(Optional.of(columnSegmentBoundInfo)); EncryptRule rule = mockRule(mock(EncryptAlgorithm.class)); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", mock(), mock(), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); when(mergedResult.getValue(1, String.class)).thenReturn("foo_value"); @@ -100,9 +98,8 @@ class EncryptMergedResultTest { @Test void assertGetValueWithoutEncryptColumn() throws SQLException { - ColumnProjection columnProjection = new ColumnProjection(new IdentifierValue("foo_tbl"), new IdentifierValue("bar_col"), new IdentifierValue("bar_alias"), databaseType, - null, null, new ColumnSegmentBoundInfo(new IdentifierValue("bar_col"))); - when(selectStatementContext.findColumnProjection(1)).thenReturn(Optional.of(columnProjection)); + ColumnSegmentBoundInfo columnSegmentBoundInfo = new ColumnSegmentBoundInfo(new IdentifierValue("bar_col")); + when(selectStatementContext.findColumnBoundInfo(1)).thenReturn(Optional.of(columnSegmentBoundInfo)); EncryptRule rule = mockRule(mock(EncryptAlgorithm.class)); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", mock(), mock(), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); when(mergedResult.getValue(1, String.class)).thenReturn("foo_value"); @@ -111,11 +108,10 @@ class EncryptMergedResultTest { @Test void assertGetValueWithEncryptColumn() throws SQLException { - ColumnProjection columnProjection = - new ColumnProjection(new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), new IdentifierValue("foo_alias"), databaseType, null, null, new ColumnSegmentBoundInfo( - new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema")), new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), - TableSourceType.PHYSICAL_TABLE)); - when(selectStatementContext.findColumnProjection(1)).thenReturn(Optional.of(columnProjection)); + ColumnSegmentBoundInfo columnSegmentBoundInfo = new ColumnSegmentBoundInfo( + new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema")), new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), + TableSourceType.PHYSICAL_TABLE); + when(selectStatementContext.findColumnBoundInfo(1)).thenReturn(Optional.of(columnSegmentBoundInfo)); when(selectStatementContext.getTablesContext().getSchemaName()).thenReturn(Optional.of("foo_schema")); EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class); when(encryptAlgorithm.decrypt(eq("foo_value"), deepEq(new AlgorithmSQLContext("foo_db", "foo_schema", "foo_tbl", "foo_col")))).thenReturn("foo_decrypted_value"); @@ -128,11 +124,10 @@ class EncryptMergedResultTest { @Test void assertGetValueFailed() throws SQLException { - ColumnProjection columnProjection = - new ColumnProjection(new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), new IdentifierValue("foo_alias"), databaseType, null, null, new ColumnSegmentBoundInfo( - new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema")), new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), - TableSourceType.PHYSICAL_TABLE)); - when(selectStatementContext.findColumnProjection(1)).thenReturn(Optional.of(columnProjection)); + ColumnSegmentBoundInfo columnSegmentBoundInfo = new ColumnSegmentBoundInfo( + new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema")), new IdentifierValue("foo_tbl"), new IdentifierValue("foo_col"), + TableSourceType.PHYSICAL_TABLE); + when(selectStatementContext.findColumnBoundInfo(1)).thenReturn(Optional.of(columnSegmentBoundInfo)); when(selectStatementContext.getTablesContext().getSchemaName()).thenReturn(Optional.of("foo_schema")); EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class); when(encryptAlgorithm.decrypt(eq("foo_value"), deepEq(new AlgorithmSQLContext("foo_db", "foo_schema", "foo_tbl", "foo_col")))).thenThrow(new RuntimeException("Test failed")); diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContext.java index 45540103877..e64d6dbc6ac 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContext.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContext.java @@ -66,6 +66,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.ite import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.TextOrderByItemSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment; +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment; @@ -359,22 +360,22 @@ public final class SelectStatementContext extends CommonSQLStatementContext impl } /** - * Find column projection. + * Find column bound info. * * @param columnIndex column index - * @return find column projection + * @return column bound info */ - public Optional<ColumnProjection> findColumnProjection(final int columnIndex) { + public Optional<ColumnSegmentBoundInfo> findColumnBoundInfo(final int columnIndex) { List<Projection> expandProjections = projectionsContext.getExpandProjections(); if (expandProjections.size() < columnIndex) { return Optional.empty(); } Projection projection = expandProjections.get(columnIndex - 1); if (projection instanceof ColumnProjection) { - return Optional.of((ColumnProjection) projection); + return Optional.of(((ColumnProjection) projection).getColumnBoundInfo()); } if (projection instanceof SubqueryProjection && ((SubqueryProjection) projection).getProjection() instanceof ColumnProjection) { - return Optional.of((ColumnProjection) ((SubqueryProjection) projection).getProjection()); + return Optional.of(((ColumnProjection) ((SubqueryProjection) projection).getProjection()).getColumnBoundInfo()); } return Optional.empty(); }