Skip to content

Commit 8006e84

Browse files
authored
Fix mysql assignment column value parsing (#34975)
* Extract join where segment from update statement * Extract join where segment from update statement * Extract join where segment from update statement
1 parent b793ad2 commit 8006e84

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/dml/UpdateStatementContext.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ public final class UpdateStatementContext extends CommonSQLStatementContext impl
5454
public UpdateStatementContext(final UpdateStatement sqlStatement) {
5555
super(sqlStatement);
5656
tablesContext = new TablesContext(getAllSimpleTableSegments());
57-
getSqlStatement().getWhere().ifPresent(whereSegments::add);
57+
extractWhereSegments(whereSegments, sqlStatement);
5858
ColumnExtractor.extractColumnSegments(columnSegments, whereSegments);
5959
ExpressionExtractor.extractJoinConditions(joinConditions, whereSegments);
6060
}
6161

62+
private void extractWhereSegments(final Collection<WhereSegment> whereSegments, final UpdateStatement updateStatement) {
63+
updateStatement.getWhere().ifPresent(whereSegments::add);
64+
}
65+
6266
private Collection<SimpleTableSegment> getAllSimpleTableSegments() {
6367
TableExtractor tableExtractor = new TableExtractor();
6468
tableExtractor.extractTablesFromUpdate(getSqlStatement());

parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1613,12 +1613,7 @@ public ASTNode visitAssignment(final AssignmentContext ctx) {
16131613
public ASTNode visitAssignmentValue(final AssignmentValueContext ctx) {
16141614
ExprContext expr = ctx.expr();
16151615
if (null != expr) {
1616-
ASTNode result = visit(expr);
1617-
if (result instanceof ColumnSegment) {
1618-
return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
1619-
} else {
1620-
return result;
1621-
}
1616+
return visit(expr);
16221617
}
16231618
return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
16241619
}

test/it/parser/src/main/resources/case/dml/insert.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,17 @@
508508
<assignment start-index="65" stop-index="69">
509509
<column name="b" start-index="65" stop-index="65" />
510510
<assignment-value>
511-
<common-expression literal-text="n.a" start-index="67" stop-index="69" />
511+
<column name="a" start-index="67" stop-index="69">
512+
<owner name="n" start-index="67" stop-index="67" />
513+
</column>
512514
</assignment-value>
513515
</assignment>
514516
<assignment start-index="72" stop-index="76">
515517
<column name="a" start-index="72" stop-index="72" />
516518
<assignment-value>
517-
<common-expression literal-text="n.b" start-index="74" stop-index="76" />
519+
<column name="b" start-index="74" stop-index="76" >
520+
<owner name="n" start-index="74" stop-index="74" />
521+
</column>
518522
</assignment-value>
519523
</assignment>
520524
</on-duplicate-key-columns>

test/it/parser/src/main/resources/case/dml/update.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,14 @@
577577
<assignment start-index="19" stop-index="37" literal-stop-index="37">
578578
<column name="order_id" start-index="19" stop-index="26" />
579579
<assignment-value>
580-
<common-expression text="order_id" literal-text="order_id" start-index="30" stop-index="37" literal-start-index="30" literal-stop-index="37" />
580+
<column name="order_id" start-index="30" stop-index="37" literal-start-index="30" literal-stop-index="37" />
581581
</assignment-value>
582582
</assignment>
583583
<assignment start-index="40" stop-index="54" literal-stop-index="54">
584584
<column name="status" start-index="40" stop-index="45" />
585585
<assignment-value>
586586
<literal-expression value="init" start-index="49" stop-index="54" />
587-
<common-expression text="init" start-index="49" stop-index="54" literal-start-index="49" literal-stop-index="54" />
587+
<column name="init" start-index="49" stop-index="54" literal-start-index="49" literal-stop-index="54" />
588588
</assignment-value>
589589
</assignment>
590590
</set>

0 commit comments

Comments
 (0)