Skip to content

fix: mysql delete join panic #2197

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 2 commits into from
Apr 10, 2023
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
4 changes: 1 addition & 3 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
var list *ast.List
switch n := node.(type) {
case *ast.DeleteStmt:
list = &ast.List{
Items: []ast.Node{n.Relation},
}
list = n.Relations
case *ast.InsertStmt:
list = &ast.List{
Items: []ast.Node{n.Relation},
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/delete_inner_join/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/endtoend/testdata/delete_inner_join/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/endtoend/testdata/delete_inner_join/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/endtoend/testdata/delete_inner_join/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE author (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

CREATE TABLE book (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL
);

CREATE TABLE author_book (
author_id INT UNSIGNED NOT NULL,
book_id INT UNSIGNED NOT NULL,
CONSTRAINT `pk-author_book` PRIMARY KEY (author_id, book_id),
CONSTRAINT `fk-author_book-author-id` FOREIGN KEY (author_id) REFERENCES author (id),
CONSTRAINT `fk-author_book-book-id` FOREIGN KEY (book_id) REFERENCES book (id)
);

/* name: RemoveAllAuthorsFromTheGreatGatsby :exec */
DELETE author_book
FROM
author_book
INNER JOIN book ON book.id = author_book.book_id
WHERE
book.title = 'The Great Gatsby';
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/delete_inner_join/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "mysql",
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/delete_join/mysql/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/delete_join/mysql/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions internal/endtoend/testdata/delete_join/mysql/db/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions internal/endtoend/testdata/delete_join/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE primary_table (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
user_id bigint(20) unsigned NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE join_table (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
primary_table_id bigint(20) unsigned NOT NULL,
other_table_id bigint(20) unsigned NOT NULL,
is_active tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (id)
);

-- name: DeleteJoin :exec
DELETE jt.*,
pt.*
FROM
join_table as jt
JOIN primary_table as pt ON jt.primary_table_id = pt.id
WHERE
jt.id = ?
AND pt.user_id = ?;

-- name: DeleteLeftJoin :exec
DELETE jt.*,
pt.*
FROM
join_table as jt
LEFT JOIN primary_table as pt ON jt.primary_table_id = pt.id
WHERE
jt.id = ?
AND pt.user_id = ?;

-- name: DeleteRightJoin :exec
DELETE jt.*,
pt.*
FROM
join_table as jt
RIGHT JOIN primary_table as pt ON jt.primary_table_id = pt.id
WHERE
jt.id = ?
AND pt.user_id = ?;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/delete_join/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "db",
"engine": "mysql",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
39 changes: 19 additions & 20 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,11 @@ func (c *cc) convertDeleteStmt(n *pcast.DeleteStmt) *ast.DeleteStmt {
if len(rels.Items) != 1 {
panic("expected one range var")
}
rel := rels.Items[0]
rangeVar, ok := rel.(*ast.RangeVar)
if !ok {
panic("expected range var")
}
relations := &ast.List{}
convertToRangeVarList(rels, relations)

return &ast.DeleteStmt{
Relation: rangeVar,
Relations: relations,
WhereClause: c.convert(n.Where),
ReturningList: &ast.List{},
WithClause: c.convertWithClause(n.With),
Expand Down Expand Up @@ -1140,22 +1137,24 @@ func (c *cc) convertSetOprType(n *pcast.SetOprType) (op ast.SetOperation, all bo
// into a tree. It is called for UNION, INTERSECT or EXCLUDE operation.
//
// Given an union with the following nodes:
// [Select{1}, Select{2}, Select{3}, Select{4}]
//
// [Select{1}, Select{2}, Select{3}, Select{4}]
//
// The function will return:
// Select{
// Larg: Select{
// Larg: Select{
// Larg: Select{1},
// Rarg: Select{2},
// Op: Union
// },
// Rarg: Select{3},
// Op: Union,
// },
// Rarg: Select{4},
// Op: Union,
// }
//
// Select{
// Larg: Select{
// Larg: Select{
// Larg: Select{1},
// Rarg: Select{2},
// Op: Union
// },
// Rarg: Select{3},
// Op: Union,
// },
// Rarg: Select{4},
// Op: Union,
// }
func (c *cc) convertSetOprSelectList(n *pcast.SetOprSelectList) ast.Node {
selectStmts := make([]*ast.SelectStmt, len(n.Selects))
for i, node := range n.Selects {
Expand Down
4 changes: 3 additions & 1 deletion internal/engine/postgresql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,9 @@ func convertDeleteStmt(n *pg.DeleteStmt) *ast.DeleteStmt {
return nil
}
return &ast.DeleteStmt{
Relation: convertRangeVar(n.Relation),
Relations: &ast.List{
Items: []ast.Node{convertRangeVar(n.Relation)},
},
UsingClause: convertSlice(n.UsingClause),
WhereClause: convertNode(n.WhereClause),
ReturningList: convertSlice(n.ReturningList),
Expand Down
6 changes: 5 additions & 1 deletion internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ func (c *cc) convertDelete_stmtContext(n *parser.Delete_stmtContext) ast.Node {
relation.Alias = &ast.Alias{Aliasname: &alias}
}

relations := &ast.List{}

relations.Items = append(relations.Items, relation)

delete := &ast.DeleteStmt{
Relation: relation,
Relations: relations,
ReturningList: c.convertReturning_caluseContext(n.Returning_clause()),
WithClause: nil,
}
Expand Down
Loading