Skip to content

Commit

Permalink
fix(compiler): Don't crash on WHERE x IN (... UNION ...) (#3652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jille authored Nov 25, 2024
1 parent 999f389 commit 0b952b4
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/compiler/find_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
if n.Sel == nil {
p.parent = node
} else {
if sel, ok := n.Sel.(*ast.SelectStmt); ok && sel.FromClause != nil {
if sel, ok := n.Sel.(*ast.SelectStmt); ok && sel.FromClause != nil && len(sel.FromClause.Items) > 0 {
from := sel.FromClause
if schema, ok := from.Items[0].(*ast.RangeVar); ok && schema != nil {
p.rangeVar = &ast.RangeVar{
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/in_union/mysql/go/db.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/in_union/mysql/go/models.go

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

38 changes: 38 additions & 0 deletions internal/endtoend/testdata/in_union/mysql/go/query.sql.go

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

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/in_union/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: GetAuthors :many
SELECT * FROM authors
WHERE author_id IN (SELECT author_id FROM book1 UNION SELECT author_id FROM book2);
15 changes: 15 additions & 0 deletions internal/endtoend/testdata/in_union/mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
id int PRIMARY KEY,
name text NOT NULL,
bio text
);
CREATE TABLE book1 (
author_id int PRIMARY KEY,
name text,
FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE
);
CREATE TABLE book2 (
author_id int PRIMARY KEY,
name text,
FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/in_union/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": "schema.sql",
"queries": "query.sql"
}
]
}

0 comments on commit 0b952b4

Please sign in to comment.