Skip to content

fix(compiler): Don't crash on WHERE x IN (... UNION ...) #3652

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 1 commit into from
Nov 25, 2024
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
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"
}
]
}
Loading