Skip to content

feat(compiler): Support LEFT JOIN on aliased table #2873

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
Oct 18, 2023
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
13 changes: 8 additions & 5 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
continue
}
for _, f := range n.FromClause.Items {
if res := isTableRequired(f, col, tableRequired); res != tableNotFound {
res := isTableRequired(f, col, tableRequired)
if res != tableNotFound {
col.NotNull = res == tableRequired
break
}
Expand All @@ -423,10 +424,12 @@ const (
func isTableRequired(n ast.Node, col *Column, prior int) int {
switch n := n.(type) {
case *ast.RangeVar:
if n.Alias == nil && *n.Relname == col.Table.Name {
return prior
tableMatch := *n.Relname == col.Table.Name
aliasMatch := true
if n.Alias != nil && col.TableAlias != "" {
aliasMatch = *n.Alias.Aliasname == col.TableAlias
}
if n.Alias != nil && *n.Alias.Aliasname == col.TableAlias && *n.Relname == col.Table.Name {
if aliasMatch && tableMatch {
return prior
}
Comment on lines +427 to 434
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the column doesn't use a table alias, don't check that the aliases match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct logically, although aliasMatch defaulting to true is potentially confusing to future readers. I might suggest just putting this comment you wrote in the actual source.

case *ast.JoinExpr:
Expand Down Expand Up @@ -673,13 +676,13 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
continue
}
for _, c := range t.Columns {

if c.Name == name {
found += 1
cname := c.Name
if res.Name != nil {
cname = *res.Name
}

cols = append(cols, &Column{
Name: cname,
Type: c.Type,
Expand Down
1 change: 1 addition & 0 deletions internal/endtoend/testdata/join_left_table_alias/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/1897

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- name: JoinBar :one
SELECT f.id, info
FROM foo f
LEFT JOIN bar b ON b.foo_id = f.id;

-- name: JoinBarAlias :one
SELECT f.id, b.info
FROM foo f
LEFT JOIN bar b ON b.foo_id = f.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE foo (
id BIGINT PRIMARY KEY
);

CREATE TABLE bar
(
foo_id BIGINT NOT NULL,
info TEXT NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"