Skip to content

Commit fd3ed9d

Browse files
authored
fix: mysql delete join panic (#2197)
* fix:mysql delete join panic * fix: test
1 parent 3e55012 commit fd3ed9d

File tree

17 files changed

+325
-32
lines changed

17 files changed

+325
-32
lines changed

internal/compiler/output_columns.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
404404
var list *ast.List
405405
switch n := node.(type) {
406406
case *ast.DeleteStmt:
407-
list = &ast.List{
408-
Items: []ast.Node{n.Relation},
409-
}
407+
list = n.Relations
410408
case *ast.InsertStmt:
411409
list = &ast.List{
412410
Items: []ast.Node{n.Relation},

internal/endtoend/testdata/delete_inner_join/mysql/go/db.go

Lines changed: 31 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_inner_join/mysql/go/models.go

Lines changed: 22 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_inner_join/mysql/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CREATE TABLE author (
2+
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL
4+
);
5+
6+
CREATE TABLE book (
7+
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
8+
title VARCHAR(255) NOT NULL
9+
);
10+
11+
CREATE TABLE author_book (
12+
author_id INT UNSIGNED NOT NULL,
13+
book_id INT UNSIGNED NOT NULL,
14+
CONSTRAINT `pk-author_book` PRIMARY KEY (author_id, book_id),
15+
CONSTRAINT `fk-author_book-author-id` FOREIGN KEY (author_id) REFERENCES author (id),
16+
CONSTRAINT `fk-author_book-book-id` FOREIGN KEY (book_id) REFERENCES book (id)
17+
);
18+
19+
/* name: RemoveAllAuthorsFromTheGreatGatsby :exec */
20+
DELETE author_book
21+
FROM
22+
author_book
23+
INNER JOIN book ON book.id = author_book.book_id
24+
WHERE
25+
book.title = 'The Great Gatsby';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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

Lines changed: 31 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/db/models.go

Lines changed: 19 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/db/query.sql.go

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
CREATE TABLE primary_table (
2+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
3+
user_id bigint(20) unsigned NOT NULL,
4+
PRIMARY KEY (id)
5+
);
6+
7+
CREATE TABLE join_table (
8+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
9+
primary_table_id bigint(20) unsigned NOT NULL,
10+
other_table_id bigint(20) unsigned NOT NULL,
11+
is_active tinyint(1) NOT NULL DEFAULT '0',
12+
PRIMARY KEY (id)
13+
);
14+
15+
-- name: DeleteJoin :exec
16+
DELETE jt.*,
17+
pt.*
18+
FROM
19+
join_table as jt
20+
JOIN primary_table as pt ON jt.primary_table_id = pt.id
21+
WHERE
22+
jt.id = ?
23+
AND pt.user_id = ?;
24+
25+
-- name: DeleteLeftJoin :exec
26+
DELETE jt.*,
27+
pt.*
28+
FROM
29+
join_table as jt
30+
LEFT JOIN primary_table as pt ON jt.primary_table_id = pt.id
31+
WHERE
32+
jt.id = ?
33+
AND pt.user_id = ?;
34+
35+
-- name: DeleteRightJoin :exec
36+
DELETE jt.*,
37+
pt.*
38+
FROM
39+
join_table as jt
40+
RIGHT JOIN primary_table as pt ON jt.primary_table_id = pt.id
41+
WHERE
42+
jt.id = ?
43+
AND pt.user_id = ?;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "db",
6+
"engine": "mysql",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

internal/engine/dolphin/convert.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,11 @@ func (c *cc) convertDeleteStmt(n *pcast.DeleteStmt) *ast.DeleteStmt {
318318
if len(rels.Items) != 1 {
319319
panic("expected one range var")
320320
}
321-
rel := rels.Items[0]
322-
rangeVar, ok := rel.(*ast.RangeVar)
323-
if !ok {
324-
panic("expected range var")
325-
}
321+
relations := &ast.List{}
322+
convertToRangeVarList(rels, relations)
326323

327324
return &ast.DeleteStmt{
328-
Relation: rangeVar,
325+
Relations: relations,
329326
WhereClause: c.convert(n.Where),
330327
ReturningList: &ast.List{},
331328
WithClause: c.convertWithClause(n.With),
@@ -1140,22 +1137,24 @@ func (c *cc) convertSetOprType(n *pcast.SetOprType) (op ast.SetOperation, all bo
11401137
// into a tree. It is called for UNION, INTERSECT or EXCLUDE operation.
11411138
//
11421139
// Given an union with the following nodes:
1143-
// [Select{1}, Select{2}, Select{3}, Select{4}]
1140+
//
1141+
// [Select{1}, Select{2}, Select{3}, Select{4}]
11441142
//
11451143
// The function will return:
1146-
// Select{
1147-
// Larg: Select{
1148-
// Larg: Select{
1149-
// Larg: Select{1},
1150-
// Rarg: Select{2},
1151-
// Op: Union
1152-
// },
1153-
// Rarg: Select{3},
1154-
// Op: Union,
1155-
// },
1156-
// Rarg: Select{4},
1157-
// Op: Union,
1158-
// }
1144+
//
1145+
// Select{
1146+
// Larg: Select{
1147+
// Larg: Select{
1148+
// Larg: Select{1},
1149+
// Rarg: Select{2},
1150+
// Op: Union
1151+
// },
1152+
// Rarg: Select{3},
1153+
// Op: Union,
1154+
// },
1155+
// Rarg: Select{4},
1156+
// Op: Union,
1157+
// }
11591158
func (c *cc) convertSetOprSelectList(n *pcast.SetOprSelectList) ast.Node {
11601159
selectStmts := make([]*ast.SelectStmt, len(n.Selects))
11611160
for i, node := range n.Selects {

internal/engine/postgresql/convert.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,9 @@ func convertDeleteStmt(n *pg.DeleteStmt) *ast.DeleteStmt {
14351435
return nil
14361436
}
14371437
return &ast.DeleteStmt{
1438-
Relation: convertRangeVar(n.Relation),
1438+
Relations: &ast.List{
1439+
Items: []ast.Node{convertRangeVar(n.Relation)},
1440+
},
14391441
UsingClause: convertSlice(n.UsingClause),
14401442
WhereClause: convertNode(n.WhereClause),
14411443
ReturningList: convertSlice(n.ReturningList),

internal/engine/sqlite/convert.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,12 @@ func (c *cc) convertDelete_stmtContext(n *parser.Delete_stmtContext) ast.Node {
158158
relation.Alias = &ast.Alias{Aliasname: &alias}
159159
}
160160

161+
relations := &ast.List{}
162+
163+
relations.Items = append(relations.Items, relation)
164+
161165
delete := &ast.DeleteStmt{
162-
Relation: relation,
166+
Relations: relations,
163167
ReturningList: c.convertReturning_caluseContext(n.Returning_clause()),
164168
WithClause: nil,
165169
}

0 commit comments

Comments
 (0)