Skip to content

Support With Clause in Delete Statement in MySQL. #34817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
1. SQL Parser: Support MySQL SELECT CAST AS YEAR statement parse - [#34638](https://github.com/apache/shardingsphere/pull/34638)
1. SQL Parser: Support MySQL SELECT MAX(ALL expr) statement parse - [#34639](https://github.com/apache/shardingsphere/pull/34639)
1. SQL Parser: Support MySQL INSERT with GEOMCOLLECTION function parse - [#34654](https://github.com/apache/shardingsphere/pull/34654)
1. SQL Parser: Support MySQL DELETE with statement parse - [#34817](https://github.com/apache/shardingsphere/pull/34817)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ blobValue
;

delete
: DELETE deleteSpecification (singleTableClause | multipleTablesClause) whereClause? orderByClause? limitClause? returningClause?
: withClause? DELETE deleteSpecification (singleTableClause | multipleTablesClause) whereClause? orderByClause? limitClause? returningClause?
;

deleteSpecification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,9 @@ public ASTNode visitDelete(final DeleteContext ctx) {
if (null != ctx.returningClause()) {
result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
}
if (null != ctx.withClause()) {
result.setWithSegment((WithSegment) visit(ctx.withClause()));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.mysql.MySQLStatement;

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

private ReturningSegment returningSegment;

private WithSegment withSegment;

@Override
public Optional<OrderBySegment> getOrderBy() {
return Optional.ofNullable(orderBy);
Expand All @@ -52,4 +55,9 @@ public Optional<LimitSegment> getLimit() {
public Optional<ReturningSegment> getReturningSegment() {
return Optional.ofNullable(returningSegment);
}

@Override
public Optional<WithSegment> getWithSegment() {
return Optional.ofNullable(withSegment);
}
}
31 changes: 31 additions & 0 deletions test/it/parser/src/main/resources/case/dml/delete.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,37 @@
</where>
</delete>

<delete sql-case-id="delete_using_with_clause">
<with start-index="0" stop-index="63">
<common-table-expression name = "cte" start-index="5" stop-index="63">
<subquery-expression start-index="5" stop-index="63">
<select>
<from>
<simple-table name="t_order" start-index="34" stop-index="40" />
</from>
<projections start-index="20" stop-index="27">
<column-projection name="order_id" start-index="20" stop-index="27" />
</projections>
<where start-index="42" stop-index="62">
<expr>
<binary-operation-expression start-index="48" stop-index="62">
<left>
<column name="order_id" start-index="48" stop-index="55"/>
</left>
<operator>=</operator>
<right>
<literal-expression value="1000" start-index="59" stop-index="62"/>
</right>
</binary-operation-expression>
</expr>
</where>
</select>
</subquery-expression>
</common-table-expression>
</with>
<table name="t_order" start-index="77" stop-index="83"/>
</delete>

<delete sql-case-id="delete_without_columns_with_with_clause">
<with start-index="0" stop-index="50">
<common-table-expression name="cte" start-index="5" stop-index="50">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<sql-case id="delete_with_top" value="DELETE TOP(10) FROM t_order WHERE order_id = ?" db-types="SQLServer" />
<sql-case id="delete_with_top_percent" value="DELETE TOP(10) PERCENT FROM t_order WHERE order_id = ?" db-types="SQLServer" />
<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" />
<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" />
<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" />
<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" />
<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" />
Expand Down
Loading