Skip to content
Open
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
7 changes: 7 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ func isTableRequired(n ast.Node, col *Column, prior int) int {
if aliasMatch && tableMatch {
return prior
}
case *ast.RangeSubselect:
// For subqueries, match only by alias since there's no table name
if n.Alias != nil && col.TableAlias != "" {
if *n.Alias.Aliasname == col.TableAlias {
return prior
}
}
case *ast.JoinExpr:
helper := func(l, r int) int {
if res := isTableRequired(n.Larg, col, l); res != tableNotFound {
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/join_left_subquery/postgresql/go/db.go

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

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

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/join_left_subquery/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- name: LeftJoinSubquery :many
SELECT * FROM a AS table_a
LEFT JOIN (SELECT * FROM b WHERE b.name IS NOT NULL) si ON si.a_id = table_a.id;

-- name: LeftJoinSubqueryExplicitColumns :many
SELECT
table_a.id,
table_a.name,
si.id,
si.a_id,
si.name
FROM a AS table_a
LEFT JOIN (SELECT id, a_id, name FROM b WHERE b.name IS NOT NULL) si ON si.a_id = table_a.id;

-- name: LeftJoinSubqueryNoAlias :many
SELECT * FROM a
LEFT JOIN (SELECT * FROM b) subquery ON subquery.a_id = a.id;

-- name: RightJoinSubquery :many
SELECT * FROM a AS table_a
RIGHT JOIN (SELECT * FROM b WHERE b.name IS NOT NULL) si ON si.a_id = table_a.id;

-- name: FullOuterJoinSubquery :many
SELECT * FROM a AS table_a
FULL OUTER JOIN (SELECT * FROM b WHERE b.name IS NOT NULL) si ON si.a_id = table_a.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- https://github.com/sqlc-dev/sqlc/issues/4117
CREATE TABLE a (
id uuid PRIMARY KEY,
name TEXT
);

CREATE TABLE b (
id uuid PRIMARY KEY,
a_id uuid NOT NULL REFERENCES a (id),
name TEXT
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_left_subquery/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
Loading