Skip to content

Commit

Permalink
Adjust SubqueryTableSegment start index and stop index visit logic an…
Browse files Browse the repository at this point in the history
…d add test assert (apache#30999)
  • Loading branch information
strongduanmu authored Apr 24, 2024
1 parent b4b900d commit 1630792
Show file tree
Hide file tree
Showing 29 changed files with 101 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
Expand Down Expand Up @@ -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<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
Expand All @@ -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<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
SubqueryTableSegment actual = SubqueryTableSegmentBinder.bind(subqueryTableSegment, new SQLStatementBinderContext(metaData, DefaultDatabase.LOGIC_NAME, databaseType, Collections.emptySet()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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()));
}
Expand All @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
@Getter
public final class SubqueryTableSegment implements TableSegment {

private final int startIndex;

private final int stopIndex;

private final SubquerySegment subquery;

@Setter
Expand Down Expand Up @@ -63,12 +67,12 @@ public Optional<PivotSegment> getPivot() {

@Override
public int getStartIndex() {
return subquery.getStartIndex();
return startIndex;
}

@Override
public int getStopIndex() {
return subquery.getStopIndex();
return null == alias ? stopIndex : alias.getStopIndex();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubquerySegment> result = SubqueryExtractUtils.getSubquerySegments(selectStatement);
assertThat(result.size(), is(1));
Expand Down Expand Up @@ -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);
Expand All @@ -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<SubquerySegment> result = SubqueryExtractUtils.getSubquerySegments(selectStatement);
assertThat(result.size(), is(2));
}
Expand Down
Loading

0 comments on commit 1630792

Please sign in to comment.