Skip to content

Commit 97b9b48

Browse files
committed
fix:delete inner join panic
1 parent 3e55012 commit 97b9b48

File tree

14 files changed

+180
-31
lines changed

14 files changed

+180
-31
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ mysqlsh:
4646

4747
proto:
4848
buf generate
49+
test-internal:
50+
go build -o sqlc.exe .\cmd\sqlc\ && .\sqlc.exe generate -f .\internal\endtoend\testdata\delete_inner_join\sqlite\sqlc.json

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: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
(
13+
author_id INT UNSIGNED NOT NULL,
14+
book_id INT UNSIGNED NOT NULL,
15+
CONSTRAINT `pk-author_book` PRIMARY KEY (author_id, book_id),
16+
CONSTRAINT `fk-author_book-author-id` FOREIGN KEY (author_id) REFERENCES author (id),
17+
CONSTRAINT `fk-author_book-book-id` FOREIGN KEY (book_id) REFERENCES book (id)
18+
);
19+
20+
/* name: RemoveAllAuthorsFromTheGreatGatsby :exec */
21+
DELETE author_book
22+
FROM author_book
23+
INNER JOIN book ON book.id = author_book.book_id
24+
WHERE 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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
(
13+
author_id INT UNSIGNED NOT NULL,
14+
book_id INT UNSIGNED NOT NULL,
15+
CONSTRAINT `pk-author_book` PRIMARY KEY (author_id, book_id),
16+
CONSTRAINT `fk-author_book-author-id` FOREIGN KEY (author_id) REFERENCES author (id),
17+
CONSTRAINT `fk-author_book-book-id` FOREIGN KEY (book_id) REFERENCES book (id)
18+
);
19+
20+
-- name: RemoveAllAuthorsFromTheGreatGatsby
21+
DELETE author_book
22+
FROM author_book
23+
INNER JOIN book ON book.id = author_book.book_id
24+
WHERE 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+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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/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
}

internal/sql/ast/delete_stmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ast
22

33
type DeleteStmt struct {
4-
Relation *RangeVar
4+
Relations *List
55
UsingClause *List
66
WhereClause Node
77
ReturningList *List

internal/sql/astutils/rewrite.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ type ApplyFunc func(*Cursor) bool
3939
// Children are traversed in the order in which they appear in the
4040
// respective node's struct definition. A package's files are
4141
// traversed in the filenames' alphabetical order.
42-
//
4342
func Apply(root ast.Node, pre, post ApplyFunc) (result ast.Node) {
4443
parent := &struct{ ast.Node }{root}
4544
defer func() {
@@ -63,8 +62,8 @@ var abort = new(int) // singleton, to signal termination of Apply
6362
// c.Parent(), and f is the field identifier with name c.Name(),
6463
// the following invariants hold:
6564
//
66-
// p.f == c.Node() if c.Index() < 0
67-
// p.f[c.Index()] == c.Node() if c.Index() >= 0
65+
// p.f == c.Node() if c.Index() < 0
66+
// p.f[c.Index()] == c.Node() if c.Index() >= 0
6867
//
6968
// The methods Replace, Delete, InsertBefore, and InsertAfter
7069
// can be used to change the AST without disrupting Apply.
@@ -677,7 +676,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
677676
a.apply(n, "Definition", nil, n.Definition)
678677

679678
case *ast.DeleteStmt:
680-
a.apply(n, "Relation", nil, n.Relation)
679+
a.apply(n, "Relations", nil, n.Relations)
681680
a.apply(n, "UsingClause", nil, n.UsingClause)
682681
a.apply(n, "WhereClause", nil, n.WhereClause)
683682
a.apply(n, "ReturningList", nil, n.ReturningList)

internal/sql/astutils/walk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,8 @@ func Walk(f Visitor, node ast.Node) {
10541054
}
10551055

10561056
case *ast.DeleteStmt:
1057-
if n.Relation != nil {
1058-
Walk(f, n.Relation)
1057+
if n.Relations != nil {
1058+
Walk(f, n.Relations)
10591059
}
10601060
if n.UsingClause != nil {
10611061
Walk(f, n.UsingClause)

0 commit comments

Comments
 (0)