Skip to content

Commit 02c9e3d

Browse files
authored
feat(compiler): Support LEFT JOIN on aliased table (#2873)
1 parent f514135 commit 02c9e3d

File tree

8 files changed

+133
-5
lines changed

8 files changed

+133
-5
lines changed

internal/compiler/output_columns.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
403403
continue
404404
}
405405
for _, f := range n.FromClause.Items {
406-
if res := isTableRequired(f, col, tableRequired); res != tableNotFound {
406+
res := isTableRequired(f, col, tableRequired)
407+
if res != tableNotFound {
407408
col.NotNull = res == tableRequired
408409
break
409410
}
@@ -423,10 +424,12 @@ const (
423424
func isTableRequired(n ast.Node, col *Column, prior int) int {
424425
switch n := n.(type) {
425426
case *ast.RangeVar:
426-
if n.Alias == nil && *n.Relname == col.Table.Name {
427-
return prior
427+
tableMatch := *n.Relname == col.Table.Name
428+
aliasMatch := true
429+
if n.Alias != nil && col.TableAlias != "" {
430+
aliasMatch = *n.Alias.Aliasname == col.TableAlias
428431
}
429-
if n.Alias != nil && *n.Alias.Aliasname == col.TableAlias && *n.Relname == col.Table.Name {
432+
if aliasMatch && tableMatch {
430433
return prior
431434
}
432435
case *ast.JoinExpr:
@@ -673,13 +676,13 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
673676
continue
674677
}
675678
for _, c := range t.Columns {
679+
676680
if c.Name == name {
677681
found += 1
678682
cname := c.Name
679683
if res.Name != nil {
680684
cname = *res.Name
681685
}
682-
683686
cols = append(cols, &Column{
684687
Name: cname,
685688
Type: c.Type,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/1897

internal/endtoend/testdata/join_left_table_alias/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/join_left_table_alias/postgresql/pgx/go/models.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/join_left_table_alias/postgresql/pgx/go/query.sql.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: JoinBar :one
2+
SELECT f.id, info
3+
FROM foo f
4+
LEFT JOIN bar b ON b.foo_id = f.id;
5+
6+
-- name: JoinBarAlias :one
7+
SELECT f.id, b.info
8+
FROM foo f
9+
LEFT JOIN bar b ON b.foo_id = f.id;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE foo (
2+
id BIGINT PRIMARY KEY
3+
);
4+
5+
CREATE TABLE bar
6+
(
7+
foo_id BIGINT NOT NULL,
8+
info TEXT NOT NULL
9+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)