Skip to content

Commit 53f2f69

Browse files
authored
Support With Clause in Delete Statement in MySQL. (#34817)
* Support With Segment in Delete Statement in MySQL. * Update Release Notes.
1 parent 645956a commit 53f2f69

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

Diff for: RELEASE-NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
1. SQL Parser: Support MySQL SELECT CAST AS YEAR statement parse - [#34638](https://github.com/apache/shardingsphere/pull/34638)
2424
1. SQL Parser: Support MySQL SELECT MAX(ALL expr) statement parse - [#34639](https://github.com/apache/shardingsphere/pull/34639)
2525
1. SQL Parser: Support MySQL INSERT with GEOMCOLLECTION function parse - [#34654](https://github.com/apache/shardingsphere/pull/34654)
26+
1. SQL Parser: Support MySQL DELETE with statement parse - [#34817](https://github.com/apache/shardingsphere/pull/34817)
2627

2728
### Bug Fixes
2829

Diff for: parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/DMLStatement.g4

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ blobValue
105105
;
106106

107107
delete
108-
: DELETE deleteSpecification (singleTableClause | multipleTablesClause) whereClause? orderByClause? limitClause? returningClause?
108+
: withClause? DELETE deleteSpecification (singleTableClause | multipleTablesClause) whereClause? orderByClause? limitClause? returningClause?
109109
;
110110

111111
deleteSpecification

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

+3
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,9 @@ public ASTNode visitDelete(final DeleteContext ctx) {
16491649
if (null != ctx.returningClause()) {
16501650
result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
16511651
}
1652+
if (null != ctx.withClause()) {
1653+
result.setWithSegment((WithSegment) visit(ctx.withClause()));
1654+
}
16521655
return result;
16531656
}
16541657

Diff for: parser/sql/statement/type/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dml/MySQLDeleteStatement.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
2222
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
2323
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
24+
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
2425
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
2526
import org.apache.shardingsphere.sql.parser.statement.mysql.MySQLStatement;
2627

@@ -38,6 +39,8 @@ public final class MySQLDeleteStatement extends DeleteStatement implements MySQL
3839

3940
private ReturningSegment returningSegment;
4041

42+
private WithSegment withSegment;
43+
4144
@Override
4245
public Optional<OrderBySegment> getOrderBy() {
4346
return Optional.ofNullable(orderBy);
@@ -52,4 +55,9 @@ public Optional<LimitSegment> getLimit() {
5255
public Optional<ReturningSegment> getReturningSegment() {
5356
return Optional.ofNullable(returningSegment);
5457
}
58+
59+
@Override
60+
public Optional<WithSegment> getWithSegment() {
61+
return Optional.ofNullable(withSegment);
62+
}
5563
}

Diff for: test/it/parser/src/main/resources/case/dml/delete.xml

+31
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,37 @@
393393
</where>
394394
</delete>
395395

396+
<delete sql-case-id="delete_using_with_clause">
397+
<with start-index="0" stop-index="63">
398+
<common-table-expression name = "cte" start-index="5" stop-index="63">
399+
<subquery-expression start-index="5" stop-index="63">
400+
<select>
401+
<from>
402+
<simple-table name="t_order" start-index="34" stop-index="40" />
403+
</from>
404+
<projections start-index="20" stop-index="27">
405+
<column-projection name="order_id" start-index="20" stop-index="27" />
406+
</projections>
407+
<where start-index="42" stop-index="62">
408+
<expr>
409+
<binary-operation-expression start-index="48" stop-index="62">
410+
<left>
411+
<column name="order_id" start-index="48" stop-index="55"/>
412+
</left>
413+
<operator>=</operator>
414+
<right>
415+
<literal-expression value="1000" start-index="59" stop-index="62"/>
416+
</right>
417+
</binary-operation-expression>
418+
</expr>
419+
</where>
420+
</select>
421+
</subquery-expression>
422+
</common-table-expression>
423+
</with>
424+
<table name="t_order" start-index="77" stop-index="83"/>
425+
</delete>
426+
396427
<delete sql-case-id="delete_without_columns_with_with_clause">
397428
<with start-index="0" stop-index="50">
398429
<common-table-expression name="cte" start-index="5" stop-index="50">

Diff for: test/it/parser/src/main/resources/sql/supported/dml/delete.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<sql-case id="delete_with_top" value="DELETE TOP(10) FROM t_order WHERE order_id = ?" db-types="SQLServer" />
3131
<sql-case id="delete_with_top_percent" value="DELETE TOP(10) PERCENT FROM t_order WHERE order_id = ?" db-types="SQLServer" />
3232
<sql-case id="delete_with_with_clause" value="WITH cte (order_id, user_id) AS (SELECT order_id, user_id FROM t_order) DELETE t_order FROM cte WHERE t_order.order_id = cte.order_id" db-types="SQLServer" />
33+
<sql-case id="delete_using_with_clause" value="WITH cte AS (SELECT order_id FROM t_order WHERE order_id = 1000) DELETE FROM t_order USING t_order JOIN cte ON t_order.order_id = cte.order_id" db-types="MySQL" />
3334
<sql-case id="delete_without_columns_with_with_clause" value="WITH cte AS (SELECT order_id, user_id FROM t_order) DELETE t_order FROM cte WHERE t_order.order_id = cte.order_id" db-types="SQLServer" />
3435
<sql-case id="delete_multi_tables" value="DELETE t_order, t_order_item from t_order, t_order_item where t_order.order_id = t_order_item.order_id and t_order.status = ?" db-types="MySQL" />
3536
<sql-case id="delete_multi_tables_with_using" value="DELETE from t_order, t_order_item using t_order left join t_order_item on t_order.order_id = t_order_item.order_id where t_order.status = ?" db-types="MySQL" />

0 commit comments

Comments
 (0)