Skip to content

Commit

Permalink
add text for suquery segment (#28654)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuichenchuxin authored Oct 7, 2023
1 parent a584c52 commit 3cbb677
Show file tree
Hide file tree
Showing 23 changed files with 90 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static InsertStatement createInsertSelectStatement(final boolean contain
new IdentifierValue("status")));
projections.getProjections().add(new ColumnProjectionSegment(statusColumn));
selectStatement.setProjections(projections);
result.setInsertSelect(new SubquerySegment(0, 0, selectStatement));
result.setInsertSelect(new SubquerySegment(0, 0, selectStatement, ""));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private InsertStatement createInsertSelectStatement() {
InsertStatement result = createInsertStatement();
SelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setProjections(new ProjectionsSegment(0, 0));
result.setInsertSelect(new SubquerySegment(0, 0, selectStatement));
result.setInsertSelect(new SubquerySegment(0, 0, selectStatement, ""));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class SubquerySegmentBinder {
public static SubquerySegment bind(final SubquerySegment segment, final SQLStatementBinderContext statementBinderContext, final Map<String, TableSegmentBinderContext> outerTableBinderContexts) {
SelectStatement boundedSelectStatement = new SelectStatementBinder().bindCorrelateSubquery(segment.getSelect(), statementBinderContext.getMetaData(),
statementBinderContext.getDefaultDatabaseName(), outerTableBinderContexts, statementBinderContext.getExternalTableBinderContexts());
SubquerySegment result = new SubquerySegment(segment.getStartIndex(), segment.getStopIndex(), boundedSelectStatement);
SubquerySegment result = new SubquerySegment(segment.getStartIndex(), segment.getStopIndex(), boundedSelectStatement, segment.getText());
result.setSubqueryType(segment.getSubqueryType());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static SubqueryTableSegment bind(final SubqueryTableSegment segment, fina
fillPivotColumnNamesInBinderContext(segment, statementBinderContext);
SelectStatement boundedSelect = new SelectStatementBinder().bindCorrelateSubquery(segment.getSubquery().getSelect(), statementBinderContext.getMetaData(),
statementBinderContext.getDefaultDatabaseName(), outerTableBinderContexts, statementBinderContext.getExternalTableBinderContexts());
SubquerySegment boundedSubquerySegment = new SubquerySegment(segment.getSubquery().getStartIndex(), segment.getSubquery().getStopIndex(), boundedSelect);
SubquerySegment boundedSubquerySegment = new SubquerySegment(segment.getSubquery().getStartIndex(), segment.getSubquery().getStopIndex(), boundedSelect, segment.getSubquery().getText());
boundedSubquerySegment.setSubqueryType(segment.getSubquery().getSubqueryType());
SubqueryTableSegment result = new SubqueryTableSegment(boundedSubquerySegment);
segment.getAliasSegment().ifPresent(result::setAlias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void assertCreatePaginationContextWhenLimitSegmentAbsentAndTopSegmentPresent() {
subquerySelectStatement.getProjections().getProjections().add(new TopProjectionSegment(0, 10, null, "rowNumberAlias"));
SQLServerSelectStatement selectStatement = new SQLServerSelectStatement();
selectStatement.setProjections(new ProjectionsSegment(0, 0));
selectStatement.getProjections().getProjections().add(new SubqueryProjectionSegment(new SubquerySegment(0, 0, subquerySelectStatement), ""));
selectStatement.getProjections().getProjections().add(new SubqueryProjectionSegment(new SubquerySegment(0, 0, subquerySelectStatement, ""), ""));
PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(
selectStatement, mock(ProjectionsContext.class), Collections.emptyList(), Collections.emptyList());
assertFalse(paginationContext.getOffsetSegment().isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,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(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 @@ -445,7 +445,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(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 @@ -164,7 +164,7 @@ void assertInsertSelect() {
SelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.addParameterMarkerSegments(Collections.singleton(new ParameterMarkerExpressionSegment(0, 0, 0, ParameterMarkerType.QUESTION)));
selectStatement.setProjections(new ProjectionsSegment(0, 0));
SubquerySegment insertSelect = new SubquerySegment(0, 0, selectStatement);
SubquerySegment insertSelect = new SubquerySegment(0, 0, selectStatement, "");
insertStatement.setInsertSelect(insertSelect);
insertStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl"))));
InsertStatementContext actual = createInsertStatementContext(Collections.singletonList("param"), insertStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private void assertContainsSubquery(final SelectStatement selectStatement, final
subqueryProjections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("order_id"))));
subSelectStatement.setProjections(subqueryProjections);
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
SubquerySegment subquerySegment = new SubquerySegment(0, 0, subSelectStatement);
SubquerySegment subquerySegment = new SubquerySegment(0, 0, subSelectStatement, "");
SubqueryProjectionSegment subqueryProjectionSegment = new SubqueryProjectionSegment(subquerySegment, "");
projectionsSegment.getProjections().add(subqueryProjectionSegment);
selectStatement.setProjections(projectionsSegment);
Expand Down Expand Up @@ -483,11 +483,11 @@ private void assertContainsSubqueryWhereEmpty(final SelectStatement selectStatem
ProjectionsSegment subqueryProjections = new ProjectionsSegment(0, 0);
subqueryProjections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("order_id"))));
subSelectStatement.setProjections(subqueryProjections);
SubqueryExpressionSegment subqueryExpressionSegment = new SubqueryExpressionSegment(new SubquerySegment(0, 0, subSelectStatement));
SubqueryExpressionSegment subqueryExpressionSegment = new SubqueryExpressionSegment(new SubquerySegment(0, 0, subSelectStatement, ""));
SubqueryProjectionSegment projectionSegment = mock(SubqueryProjectionSegment.class);
WhereSegment whereSegment = new WhereSegment(0, 0, subqueryExpressionSegment);
selectStatement.setWhere(whereSegment);
SubquerySegment subquerySegment = new SubquerySegment(0, 0, subSelectStatement);
SubquerySegment subquerySegment = new SubquerySegment(0, 0, subSelectStatement, "");
when(projectionSegment.getSubquery()).thenReturn(subquerySegment);
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
projectionsSegment.getProjections().add(projectionSegment);
Expand Down 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(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 @@ -67,7 +67,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(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 @@ -101,7 +101,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(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 @@ -125,7 +125,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(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(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 @@ -102,7 +102,7 @@ void assertBindInsertSelectWithColumns() {
projections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("user_id"))));
projections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("status"))));
subSelectStatement.setProjections(projections);
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement));
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement, ""));
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList(new LiteralExpressionSegment(0, 0, 1),
new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, "OK"))));
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, createMetaData(), DefaultDatabase.LOGIC_NAME);
Expand All @@ -124,7 +124,7 @@ void assertBindInsertSelectWithoutColumns() {
projections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("user_id"))));
projections.getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("status"))));
subSelectStatement.setProjections(projections);
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement));
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement, ""));
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList(new LiteralExpressionSegment(0, 0, 1),
new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, "OK"))));
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, createMetaData(), DefaultDatabase.LOGIC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,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(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 @@ -491,7 +491,8 @@ private ASTNode createCompareSegment(final BooleanPrimaryContext ctx) {
if (null != ctx.predicate()) {
right = (ExpressionSegment) visit(ctx.predicate());
} else {
right = new SubqueryExpressionSegment(new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery())));
right = new SubqueryExpressionSegment(
new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()), getOriginalText(ctx.subquery())));
}
String operator = null == ctx.SAFE_EQ_() ? ctx.comparisonOperator().getText() : ctx.SAFE_EQ_().getText();
String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
Expand Down Expand Up @@ -523,7 +524,8 @@ private InExpression createInSegment(final PredicateContext ctx) {
ExpressionSegment left = (ExpressionSegment) visit(ctx.bitExpr(0));
ExpressionSegment right;
if (null != ctx.subquery()) {
right = new SubqueryExpressionSegment(new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery())));
right = new SubqueryExpressionSegment(
new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()), getOriginalText(ctx.subquery())));
} else {
right = new ListExpression(ctx.LP_().getSymbol().getStartIndex(), ctx.RP_().getSymbol().getStopIndex());
for (ExprContext each : ctx.expr()) {
Expand Down Expand Up @@ -593,7 +595,8 @@ public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
int startIndex = ctx.start.getStartIndex();
int stopIndex = ctx.stop.getStopIndex();
if (null != ctx.subquery()) {
SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().getStart().getStartIndex(), ctx.subquery().getStop().getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()));
SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().getStart().getStartIndex(), ctx.subquery().getStop().getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()),
getOriginalText(ctx.subquery()));
if (null != ctx.EXISTS()) {
subquerySegment.setSubqueryType(SubqueryType.EXISTS_SUBQUERY);
return new ExistsSubqueryExpression(startIndex, stopIndex, subquerySegment);
Expand Down Expand Up @@ -1377,7 +1380,7 @@ public ASTNode visitInsertSelectClause(final InsertSelectClauseContext ctx) {
private SubquerySegment createInsertSelectSegment(final InsertSelectClauseContext ctx) {
MySQLSelectStatement selectStatement = (MySQLSelectStatement) visit(ctx.select());
selectStatement.getParameterMarkerSegments().addAll(getParameterMarkerSegments());
return new SubquerySegment(ctx.select().start.getStartIndex(), ctx.select().stop.getStopIndex(), selectStatement);
return new SubquerySegment(ctx.select().start.getStartIndex(), ctx.select().stop.getStopIndex(), selectStatement, getOriginalText(ctx.select()));
}

@Override
Expand Down Expand Up @@ -1448,7 +1451,7 @@ public ASTNode visitReplaceSelectClause(final ReplaceSelectClauseContext ctx) {

private SubquerySegment createReplaceSelectSegment(final ReplaceSelectClauseContext ctx) {
MySQLSelectStatement selectStatement = (MySQLSelectStatement) visit(ctx.select());
return new SubquerySegment(ctx.select().start.getStartIndex(), ctx.select().stop.getStopIndex(), selectStatement);
return new SubquerySegment(ctx.select().start.getStartIndex(), ctx.select().stop.getStopIndex(), selectStatement, getOriginalText(ctx.select()));
}

@Override
Expand Down Expand Up @@ -1810,7 +1813,7 @@ public ASTNode visitTableReference(final TableReferenceContext ctx) {
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);
SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().start.getStartIndex(), ctx.subquery().stop.getStopIndex(), subquery, getOriginalText(ctx.subquery()));
SubqueryTableSegment result = new SubqueryTableSegment(subquerySegment);
if (null != ctx.alias()) {
result.setAlias((AliasSegment) visit(ctx.alias()));
Expand Down
Loading

0 comments on commit 3cbb677

Please sign in to comment.