From 163079289ef12ea51bbfda6dc1b038861f657200 Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Wed, 24 Apr 2024 16:39:27 +0800 Subject: [PATCH] Adjust SubqueryTableSegment start index and stop index visit logic and add test assert (#30999) --- .../from/impl/SubqueryTableSegmentBinder.java | 2 +- .../engine/ProjectionsContextEngineTest.java | 4 +-- .../dml/SelectStatementContextTest.java | 2 +- .../impl/SubqueryTableSegmentBinderTest.java | 6 ++-- .../ShorthandProjectionSegmentBinderTest.java | 2 +- .../statement/MergeStatementBinderTest.java | 2 +- .../statement/MySQLStatementVisitor.java | 2 +- .../statement/OpenGaussStatementVisitor.java | 2 +- .../type/OracleDMLStatementVisitor.java | 18 +++++------ .../statement/PostgreSQLStatementVisitor.java | 2 +- .../type/SQL92DMLStatementVisitor.java | 2 +- .../statement/SQLServerStatementVisitor.java | 2 +- .../generic/table/SubqueryTableSegment.java | 8 +++-- .../common/util/SubqueryExtractUtilsTest.java | 8 ++--- .../common/util/WhereExtractUtilsTest.java | 2 +- .../asserts/segment/table/TableAssert.java | 1 + .../src/main/resources/case/dml/delete.xml | 4 +-- .../src/main/resources/case/dml/insert.xml | 4 +-- .../src/main/resources/case/dml/merge.xml | 12 +++---- .../resources/case/dml/select-expression.xml | 2 +- .../main/resources/case/dml/select-join.xml | 12 +++---- .../select-pagination-group-by-order-by.xml | 32 +++++++++---------- .../resources/case/dml/select-pagination.xml | 24 +++++++------- .../case/dml/select-special-function.xml | 2 +- .../case/dml/select-start-with-connect-by.xml | 2 +- .../resources/case/dml/select-sub-query.xml | 14 ++++---- .../src/main/resources/case/dml/select.xml | 18 +++++------ .../src/main/resources/case/dml/table.xml | 2 +- .../src/main/resources/case/dml/values.xml | 3 +- 29 files changed, 101 insertions(+), 95 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java index 156703a199251..86fa9d57aea63 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java @@ -74,7 +74,7 @@ public static SubqueryTableSegment bind(final SubqueryTableSegment segment, fina boundedSubquerySegment.setSubqueryType(segment.getSubquery().getSubqueryType()); IdentifierValue subqueryTableName = segment.getAliasSegment().map(AliasSegment::getIdentifier).orElseGet(() -> new IdentifierValue("")); bindParameterMarkerProjection(boundedSubquerySegment, subqueryTableName); - SubqueryTableSegment result = new SubqueryTableSegment(boundedSubquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(segment.getStartIndex(), segment.getStopIndex(), boundedSubquerySegment); segment.getAliasSegment().ifPresent(result::setAlias); tableBinderContexts.put(subqueryTableName.getValue().toLowerCase(), new SimpleTableSegmentBinderContext(createSubqueryProjections(boundedSelect.getProjections().getProjections(), subqueryTableName, statementBinderContext.getDatabaseType()))); diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java index f76a699c8c1f4..de8c931930e9c 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java @@ -389,7 +389,7 @@ private void assertCreateProjectionsContextWithTemporaryTable(final SelectStatem projectionsSegment.getProjections().add(projectionSegment); selectStatement.setProjections(projectionsSegment); subquerySelectStatement.setProjections(new ProjectionsSegment(0, 0)); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, subquerySelectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, subquerySelectStatement, "")); subqueryTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("d"))); selectStatement.setFrom(subqueryTableSegment); ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("name")); @@ -434,7 +434,7 @@ private void assertCreateProjectionsContextWhenTableNameOrAliasIgnoreCase(final projectionsSegment.getProjections().add(projectionSegment); selectStatement.setProjections(projectionsSegment); subquerySelectStatement.setProjections(new ProjectionsSegment(0, 0)); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, subquerySelectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, subquerySelectStatement, "")); subqueryTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("TABLE"))); selectStatement.setFrom(subqueryTableSegment); ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("name")); diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java index db56a65ea23c7..5b90551efcaba 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java @@ -632,7 +632,7 @@ void assertIsContainsEnhancedTable() { void assertContainsEnhancedTable() { SelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); - selectStatement.setFrom(new SubqueryTableSegment(new SubquerySegment(0, 0, createSubSelectStatement(), ""))); + selectStatement.setFrom(new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, createSubSelectStatement(), ""))); ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext actual = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME); diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinderTest.java index 02da20dbc3fca..3bf3d24233f18 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinderTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinderTest.java @@ -68,7 +68,7 @@ void assertBindWithSubqueryTableAlias() { ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); projectionsSegment.getProjections().add(new ShorthandProjectionSegment(0, 0)); when(selectStatement.getProjections()).thenReturn(projectionsSegment); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, selectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, selectStatement, "")); subqueryTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("temp"))); ShardingSphereMetaData metaData = createMetaData(); Map tableBinderContexts = new LinkedHashMap<>(); @@ -102,7 +102,7 @@ void assertBindWithSubqueryProjectionAlias() { columnProjectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("order_id_alias"))); projectionsSegment.getProjections().add(columnProjectionSegment); when(selectStatement.getProjections()).thenReturn(projectionsSegment); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, selectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, selectStatement, "")); subqueryTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("temp"))); ShardingSphereMetaData metaData = createMetaData(); Map tableBinderContexts = new LinkedHashMap<>(); @@ -126,7 +126,7 @@ void assertBindWithoutSubqueryTableAlias() { ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); projectionsSegment.getProjections().add(new ShorthandProjectionSegment(0, 0)); when(selectStatement.getProjections()).thenReturn(projectionsSegment); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, selectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, selectStatement, "")); ShardingSphereMetaData metaData = createMetaData(); Map tableBinderContexts = new LinkedHashMap<>(); SubqueryTableSegment actual = SubqueryTableSegmentBinder.bind(subqueryTableSegment, new SQLStatementBinderContext(metaData, DefaultDatabase.LOGIC_NAME, databaseType, Collections.emptySet()), diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/projection/impl/ShorthandProjectionSegmentBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/projection/impl/ShorthandProjectionSegmentBinderTest.java index 3807236a8b203..20d17357e1100 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/projection/impl/ShorthandProjectionSegmentBinderTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/projection/impl/ShorthandProjectionSegmentBinderTest.java @@ -83,7 +83,7 @@ void assertBindWithoutOwnerForSubqueryTableSegment() { ColumnProjectionSegment invisibleColumn = new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("status"))); invisibleColumn.setVisible(false); tableBinderContexts.put("o", new SimpleTableSegmentBinderContext(Arrays.asList(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("order_id"))), invisibleColumn))); - SubqueryTableSegment boundedTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, mock(MySQLSelectStatement.class), "")); + SubqueryTableSegment boundedTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, mock(MySQLSelectStatement.class), "")); boundedTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("o"))); ShorthandProjectionSegment actual = ShorthandProjectionSegmentBinder.bind(new ShorthandProjectionSegment(0, 0), boundedTableSegment, tableBinderContexts); assertThat(actual.getActualProjectionSegments().size(), is(1)); diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/MergeStatementBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/MergeStatementBinderTest.java index ad3c13e1978c2..2e5ff7e3e9a51 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/MergeStatementBinderTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/MergeStatementBinderTest.java @@ -129,7 +129,7 @@ void assertBindWithSubQuery() { OracleSelectStatement oracleSelectStatement = new OracleSelectStatement(); oracleSelectStatement.setProjections(projectionsSegment); oracleSelectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item")))); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(0, 0, oracleSelectStatement, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, oracleSelectStatement, "")); subqueryTableSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("b"))); mergeStatement.setSource(subqueryTableSegment); UpdateStatement updateStatement = new OracleUpdateStatement(); diff --git a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java index 218102adcc109..b5bd83fd87ba4 100644 --- a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java +++ b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java @@ -1830,7 +1830,7 @@ public ASTNode visitTableFactor(final TableFactorContext ctx) { if (null != ctx.subquery()) { MySQLSelectStatement subquery = (MySQLSelectStatement) visit(ctx.subquery()); SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquery, getOriginalText(ctx.subquery())); - SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); if (null != ctx.alias()) { result.setAlias((AliasSegment) visit(ctx.alias())); } diff --git a/parser/sql/dialect/opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/OpenGaussStatementVisitor.java b/parser/sql/dialect/opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/OpenGaussStatementVisitor.java index 9aabbd67c218f..deb6516788fa2 100644 --- a/parser/sql/dialect/opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/OpenGaussStatementVisitor.java +++ b/parser/sql/dialect/opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/OpenGaussStatementVisitor.java @@ -1207,7 +1207,7 @@ private SimpleTableSegment getSimpleTableSegment(final TableReferenceContext ctx private SubqueryTableSegment getSubqueryTableSegment(final TableReferenceContext ctx) { OpenGaussSelectStatement select = (OpenGaussSelectStatement) visit(ctx.selectWithParens()); SubquerySegment subquery = new SubquerySegment(ctx.selectWithParens().start.getStartIndex(), ctx.selectWithParens().stop.getStopIndex(), select, getOriginalText(ctx.selectWithParens())); - SubqueryTableSegment result = new SubqueryTableSegment(subquery); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquery); if (null != ctx.aliasClause()) { result.setAlias((AliasSegment) visit(ctx.aliasClause())); } diff --git a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java index 1211747b347ec..39e6921f3055f 100644 --- a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java +++ b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java @@ -226,10 +226,10 @@ public ASTNode visitUpdateSpecification(final UpdateSpecificationContext ctx) { } if (null != ctx.dmlTableExprClause().dmlSubqueryClause()) { SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.dmlTableExprClause().dmlSubqueryClause()); - return new SubqueryTableSegment(subquerySegment); + return new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); } SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.dmlTableExprClause().tableCollectionExpr()); - return new SubqueryTableSegment(subquerySegment); + return new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); } @Override @@ -421,10 +421,10 @@ public ASTNode visitDeleteSpecification(final DeleteSpecificationContext ctx) { } if (null != ctx.dmlTableExprClause().dmlSubqueryClause()) { SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.dmlTableExprClause().dmlSubqueryClause()); - return new SubqueryTableSegment(subquerySegment); + return new SubqueryTableSegment(ctx.dmlTableExprClause().dmlSubqueryClause().start.getStartIndex(), ctx.dmlTableExprClause().dmlSubqueryClause().stop.getStopIndex(), subquerySegment); } SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.dmlTableExprClause().tableCollectionExpr()); - return new SubqueryTableSegment(subquerySegment); + return new SubqueryTableSegment(ctx.dmlTableExprClause().tableCollectionExpr().start.getStartIndex(), ctx.dmlTableExprClause().tableCollectionExpr().stop.getStopIndex(), subquerySegment); } @Override @@ -1093,7 +1093,7 @@ public ASTNode visitCrossOuterApplyClause(final CrossOuterApplyClauseContext ctx result = (TableSegment) visit(ctx.selectTableReference()); } else { SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.collectionExpr()); - result = new SubqueryTableSegment(subquerySegment); + result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); } return result; } @@ -1163,11 +1163,11 @@ public ASTNode visitQueryTableExpr(final QueryTableExprContext ctx) { OracleSelectStatement subquery = (OracleSelectStatement) visit(ctx.lateralClause().selectSubquery()); SubquerySegment subquerySegment = new SubquerySegment(ctx.lateralClause().selectSubquery().start.getStartIndex(), ctx.lateralClause().selectSubquery().stop.getStopIndex(), subquery, getOriginalText(ctx.lateralClause().selectSubquery())); - result = new SubqueryTableSegment(subquerySegment); + result = new SubqueryTableSegment(ctx.lateralClause().LP_().getSymbol().getStartIndex(), ctx.lateralClause().RP_().getSymbol().getStopIndex(), subquerySegment); } else { if (null != ctx.tableCollectionExpr().collectionExpr().selectSubquery()) { SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.tableCollectionExpr()); - result = new SubqueryTableSegment(subquerySegment); + result = new SubqueryTableSegment(ctx.tableCollectionExpr().start.getStartIndex(), ctx.tableCollectionExpr().stop.getStopIndex(), subquerySegment); } else { result = new CollectionTableSegment((ExpressionSegment) visit(ctx.tableCollectionExpr())); } @@ -1376,7 +1376,7 @@ public ASTNode visitIntoClause(final IntoClauseContext ctx) { } OracleSelectStatement subquery = (OracleSelectStatement) visit(ctx.subquery()); SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquery, getOriginalText(ctx.subquery())); - SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquerySegment); if (null != ctx.alias()) { result.setAlias((AliasSegment) visit(ctx.alias())); } @@ -1402,7 +1402,7 @@ public ASTNode visitUsingClause(final UsingClauseContext ctx) { OracleSelectStatement subquery = (OracleSelectStatement) visit(ctx.subquery()); SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquery, getOriginalText(ctx.subquery())); subquerySegment.getSelect().getParameterMarkerSegments().addAll(popAllStatementParameterMarkerSegments()); - SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquerySegment); if (null != ctx.alias()) { result.setAlias((AliasSegment) visit(ctx.alias())); } diff --git a/parser/sql/dialect/postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/PostgreSQLStatementVisitor.java b/parser/sql/dialect/postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/PostgreSQLStatementVisitor.java index 9871be259bdb4..9ae6e809cc31c 100644 --- a/parser/sql/dialect/postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/PostgreSQLStatementVisitor.java +++ b/parser/sql/dialect/postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/PostgreSQLStatementVisitor.java @@ -1161,7 +1161,7 @@ public ASTNode visitTableReference(final TableReferenceContext ctx) { PostgreSQLSelectStatement select = (PostgreSQLSelectStatement) visit(ctx.selectWithParens()); SubquerySegment subquery = new SubquerySegment(ctx.selectWithParens().start.getStartIndex(), ctx.selectWithParens().stop.getStopIndex(), select, getOriginalText(ctx.selectWithParens())); AliasSegment alias = null == ctx.aliasClause() ? null : (AliasSegment) visit(ctx.aliasClause()); - SubqueryTableSegment result = new SubqueryTableSegment(subquery); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquery); result.setAlias(alias); return result; } diff --git a/parser/sql/dialect/sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/type/SQL92DMLStatementVisitor.java b/parser/sql/dialect/sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/type/SQL92DMLStatementVisitor.java index 3d5b1618e875c..1f27389058ef4 100644 --- a/parser/sql/dialect/sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/type/SQL92DMLStatementVisitor.java +++ b/parser/sql/dialect/sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/type/SQL92DMLStatementVisitor.java @@ -406,7 +406,7 @@ public ASTNode visitTableFactor(final TableFactorContext ctx) { if (null != ctx.subquery()) { SQL92SelectStatement subquery = (SQL92SelectStatement) visit(ctx.subquery()); SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquery, getOriginalText(ctx.subquery())); - SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); if (null != ctx.alias()) { result.setAlias((AliasSegment) visit(ctx.alias())); } diff --git a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java index 5648058907462..85442cf0ae095 100644 --- a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java +++ b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java @@ -1595,7 +1595,7 @@ public ASTNode visitTableFactor(final TableFactorContext ctx) { } else { subquerySegment.setSelect((SQLServerSelectStatement) visit(ctx.subquery())); } - SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment); + SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment); if (null != ctx.alias()) { result.setAlias((AliasSegment) visit(ctx.alias())); } diff --git a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/table/SubqueryTableSegment.java b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/table/SubqueryTableSegment.java index f562992bf1bf2..b420ee2ea471e 100644 --- a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/table/SubqueryTableSegment.java +++ b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/table/SubqueryTableSegment.java @@ -34,6 +34,10 @@ @Getter public final class SubqueryTableSegment implements TableSegment { + private final int startIndex; + + private final int stopIndex; + private final SubquerySegment subquery; @Setter @@ -63,12 +67,12 @@ public Optional getPivot() { @Override public int getStartIndex() { - return subquery.getStartIndex(); + return startIndex; } @Override public int getStopIndex() { - return subquery.getStopIndex(); + return null == alias ? stopIndex : alias.getStopIndex(); } /** diff --git a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilsTest.java b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilsTest.java index c0fe12beaffb1..b021f6176a354 100644 --- a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilsTest.java +++ b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilsTest.java @@ -102,7 +102,7 @@ void assertGetSubquerySegmentsInFrom1() { MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(7, 16)); selectStatement.getProjections().getProjections().add(new ColumnProjectionSegment(new ColumnSegment(7, 16, new IdentifierValue("order_id")))); - SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(new SubquerySegment(23, 71, subquery, "")); + SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(0, 0, new SubquerySegment(23, 71, subquery, "")); selectStatement.setFrom(subqueryTableSegment); Collection result = SubqueryExtractUtils.getSubquerySegments(selectStatement); assertThat(result.size(), is(1)); @@ -140,8 +140,8 @@ void assertGetSubquerySegmentsInFrom2() { ColumnSegment columnSegment2 = new ColumnSegment(203, 213, new IdentifierValue("order_id")); BinaryOperationExpression orderIdCondition = new BinaryOperationExpression(190, 213, columnSegment1, columnSegment2, "=", "o.order_id = oi.order_id"); from.setCondition(orderIdCondition); - SubqueryTableSegment leftSubquerySegment = new SubqueryTableSegment(new SubquerySegment(26, 92, subqueryLeftSelectStatement, "")); - SubqueryTableSegment rightSubquerySegment = new SubqueryTableSegment(new SubquerySegment(104, 175, subqueryRightSelectStatement, "")); + SubqueryTableSegment leftSubquerySegment = new SubqueryTableSegment(0, 0, new SubquerySegment(26, 92, subqueryLeftSelectStatement, "")); + SubqueryTableSegment rightSubquerySegment = new SubqueryTableSegment(0, 0, new SubquerySegment(104, 175, subqueryRightSelectStatement, "")); from.setLeft(leftSubquerySegment); from.setRight(rightSubquerySegment); selectStatement.setFrom(from); @@ -155,7 +155,7 @@ void assertGetSubquerySegmentsInFrom2() { @Test void assertGetSubquerySegmentsWithMultiNestedSubquery() { SelectStatement selectStatement = new MySQLSelectStatement(); - selectStatement.setFrom(new SubqueryTableSegment(createSubquerySegmentForFrom())); + selectStatement.setFrom(new SubqueryTableSegment(0, 0, createSubquerySegmentForFrom())); Collection result = SubqueryExtractUtils.getSubquerySegments(selectStatement); assertThat(result.size(), is(2)); } diff --git a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/WhereExtractUtilsTest.java b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/WhereExtractUtilsTest.java index 9766e40a6c443..ce09ee2c8154a 100644 --- a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/WhereExtractUtilsTest.java +++ b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/WhereExtractUtilsTest.java @@ -88,7 +88,7 @@ void assertGetWhereSegmentsFromSubQueryJoin() { MySQLSelectStatement subQuerySelectStatement = new MySQLSelectStatement(); subQuerySelectStatement.setFrom(joinTableSegment); MySQLSelectStatement selectStatement = new MySQLSelectStatement(); - selectStatement.setFrom(new SubqueryTableSegment(new SubquerySegment(20, 84, subQuerySelectStatement, ""))); + selectStatement.setFrom(new SubqueryTableSegment(0, 0, new SubquerySegment(20, 84, subQuerySelectStatement, ""))); Collection subqueryWhereSegments = WhereExtractUtils.getSubqueryWhereSegments(selectStatement); WhereSegment actual = subqueryWhereSegments.iterator().next(); assertThat(actual.getExpr(), is(joinTableSegment.getCondition())); diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java index 16f3f9bfc2cc1..089ac38fb050c 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java @@ -167,6 +167,7 @@ private static void assertIs(final SQLCaseAssertContext assertContext, final Ind * @param expected expected subquery expression */ public static void assertIs(final SQLCaseAssertContext assertContext, final SubqueryTableSegment actual, final ExpectedSubqueryTable expected) { + SQLSegmentAssert.assertIs(assertContext, actual, expected); if (null != actual.getSubquery().getSelect()) { SelectStatementAssert.assertIs(assertContext, actual.getSubquery().getSelect(), expected.getSubquery().getSelectTestCases()); } diff --git a/test/it/parser/src/main/resources/case/dml/delete.xml b/test/it/parser/src/main/resources/case/dml/delete.xml index 8a9beef69c3cc..e6f004f1a9a45 100644 --- a/test/it/parser/src/main/resources/case/dml/delete.xml +++ b/test/it/parser/src/main/resources/case/dml/delete.xml @@ -527,7 +527,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml b/test/it/parser/src/main/resources/case/dml/insert.xml index ba128255b5ad0..08615512c2f80 100644 --- a/test/it/parser/src/main/resources/case/dml/insert.xml +++ b/test/it/parser/src/main/resources/case/dml/insert.xml @@ -3959,11 +3959,11 @@ - + - + @@ -152,7 +152,7 @@ - + @@ -338,7 +338,7 @@ - + @@ -485,7 +485,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/select-join.xml b/test/it/parser/src/main/resources/case/dml/select-join.xml index a1a05944e21bd..5b36a96a053a4 100644 --- a/test/it/parser/src/main/resources/case/dml/select-join.xml +++ b/test/it/parser/src/main/resources/case/dml/select-join.xml @@ -318,7 +318,7 @@ - + @@ -2364,7 +2364,7 @@ - + @@ -2565,7 +2565,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/select-pagination-group-by-order-by.xml b/test/it/parser/src/main/resources/case/dml/select-pagination-group-by-order-by.xml index e5ba2832a5d26..6c7231e996b00 100644 --- a/test/it/parser/src/main/resources/case/dml/select-pagination-group-by-order-by.xml +++ b/test/it/parser/src/main/resources/case/dml/select-pagination-group-by-order-by.xml @@ -249,7 +249,7 @@ - + @@ -537,7 +537,7 @@ - + @@ -825,7 +825,7 @@ - + @@ -1113,7 +1113,7 @@ - + @@ -1401,7 +1401,7 @@ - + @@ -1572,7 +1572,7 @@ - + @@ -1743,7 +1743,7 @@ - + @@ -1914,7 +1914,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/select-pagination.xml b/test/it/parser/src/main/resources/case/dml/select-pagination.xml index 9ee682f8c278f..a3abd42e1afba 100644 --- a/test/it/parser/src/main/resources/case/dml/select-pagination.xml +++ b/test/it/parser/src/main/resources/case/dml/select-pagination.xml @@ -456,7 +456,7 @@ - + @@ -919,7 +919,7 @@ - + @@ -1197,7 +1197,7 @@ - + @@ -1475,7 +1475,7 @@ - + @@ -1625,7 +1625,7 @@ - + @@ -1791,7 +1791,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/select-special-function.xml b/test/it/parser/src/main/resources/case/dml/select-special-function.xml index 3b741bb1e754d..cc8b648cb1084 100644 --- a/test/it/parser/src/main/resources/case/dml/select-special-function.xml +++ b/test/it/parser/src/main/resources/case/dml/select-special-function.xml @@ -573,7 +573,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/select-sub-query.xml b/test/it/parser/src/main/resources/case/dml/select-sub-query.xml index be88402519b88..d3d4ffead23e9 100644 --- a/test/it/parser/src/main/resources/case/dml/select-sub-query.xml +++ b/test/it/parser/src/main/resources/case/dml/select-sub-query.xml @@ -26,7 +26,7 @@ - + @@ -106,7 +106,7 @@ - + @@ -716,7 +716,7 @@ - + @@ -832,7 +832,7 @@ - + @@ -380,7 +380,7 @@ - + @@ -4476,7 +4476,7 @@ - + @@ -4755,7 +4755,7 @@ @@ -5035,7 +5035,7 @@ @@ -6778,7 +6778,7 @@ - + diff --git a/test/it/parser/src/main/resources/case/dml/table.xml b/test/it/parser/src/main/resources/case/dml/table.xml index 2f4dfad87672c..41392c6e7dffa 100644 --- a/test/it/parser/src/main/resources/case/dml/table.xml +++ b/test/it/parser/src/main/resources/case/dml/table.xml @@ -74,7 +74,7 @@ - +