Skip to content

Commit eb4b0b5

Browse files
authored
Support DELETE with JOIN on subquery (#4023)
1 parent 17ace55 commit eb4b0b5

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

internal/endtoend/testdata/delete_join/mysql/db/query.sql.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/delete_join/mysql/query.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ FROM
2626
RIGHT JOIN primary_table as pt ON jt.primary_table_id = pt.id
2727
WHERE
2828
jt.id = ?
29-
AND pt.user_id = ?;
29+
AND pt.user_id = ?;
30+
31+
-- name: DeleteJoinWithSubquery :exec
32+
DELETE pt
33+
FROM primary_table pt
34+
JOIN (SELECT 1 as id) jt ON pt.id = jt.id;

internal/engine/dolphin/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func convertToRangeVarList(list *ast.List, result *ast.List) {
5252
if !ok {
5353
if list, check := rel.Larg.(*ast.List); check {
5454
convertToRangeVarList(list, result)
55+
} else if subselect, check := rel.Larg.(*ast.RangeSubselect); check {
56+
// Handle subqueries in JOIN clauses
57+
result.Items = append(result.Items, subselect)
5558
} else {
5659
panic("expected range var")
5760
}
@@ -64,6 +67,9 @@ func convertToRangeVarList(list *ast.List, result *ast.List) {
6467
if !ok {
6568
if list, check := rel.Rarg.(*ast.List); check {
6669
convertToRangeVarList(list, result)
70+
} else if subselect, check := rel.Rarg.(*ast.RangeSubselect); check {
71+
// Handle subqueries in JOIN clauses
72+
result.Items = append(result.Items, subselect)
6773
} else {
6874
panic("expected range var")
6975
}
@@ -75,6 +81,9 @@ func convertToRangeVarList(list *ast.List, result *ast.List) {
7581
case *ast.RangeVar:
7682
result.Items = append(result.Items, rel)
7783

84+
case *ast.RangeSubselect:
85+
result.Items = append(result.Items, rel)
86+
7887
default:
7988
panic("expected range var")
8089
}

0 commit comments

Comments
 (0)