Skip to content

Commit 6873c92

Browse files
committed
fix(compiler): Don't crash on WHERE x IN (... UNION ...)
fixes sqlc-dev#2139
1 parent b67215b commit 6873c92

File tree

7 files changed

+125
-1
lines changed

7 files changed

+125
-1
lines changed

internal/compiler/find_params.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
195195
if n.Sel == nil {
196196
p.parent = node
197197
} else {
198-
if sel, ok := n.Sel.(*ast.SelectStmt); ok && sel.FromClause != nil {
198+
if sel, ok := n.Sel.(*ast.SelectStmt); ok && sel.FromClause != nil && len(sel.FromClause.Items) > 0 {
199199
from := sel.FromClause
200200
if schema, ok := from.Items[0].(*ast.RangeVar); ok && schema != nil {
201201
p.rangeVar = &ast.RangeVar{

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

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/in_union/mysql/go/models.go

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/in_union/mysql/go/query.sql.go

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: GetAuthors :many
2+
SELECT * FROM authors
3+
WHERE author_id IN (SELECT author_id FROM book1 UNION SELECT author_id FROM book2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE authors (
2+
id int PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
CREATE TABLE book1 (
7+
author_id int PRIMARY KEY,
8+
name text,
9+
FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE
10+
);
11+
CREATE TABLE book2 (
12+
author_id int PRIMARY KEY,
13+
name text,
14+
FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE
15+
);
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": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)