Skip to content

fix(compiler): Support nullable fields in joins on same table #1270

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
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
30 changes: 17 additions & 13 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
continue
}
for _, f := range n.FromClause.Items {
if res := isTableRequired(f, col.Table.Name, tableRequired); res != tableNotFound {
if res := isTableRequired(f, col, tableRequired); res != tableNotFound {
col.NotNull = res == tableRequired
break
}
Expand All @@ -278,18 +278,21 @@ const (
tableOptional
)

func isTableRequired(n ast.Node, tableName string, prior int) int {
func isTableRequired(n ast.Node, col *Column, prior int) int {
switch n := n.(type) {
case *ast.RangeVar:
if *n.Relname == tableName {
if n.Alias == nil && *n.Relname == col.Table.Name {
return prior
}
if n.Alias != nil && *n.Alias.Aliasname == col.TableAlias && *n.Relname == col.Table.Name {
return prior
}
case *ast.JoinExpr:
helper := func(l, r int) int {
if res := isTableRequired(n.Larg, tableName, l); res != tableNotFound {
if res := isTableRequired(n.Larg, col, l); res != tableNotFound {
return res
}
if res := isTableRequired(n.Rarg, tableName, r); res != tableNotFound {
if res := isTableRequired(n.Rarg, col, r); res != tableNotFound {
return res
}
return tableNotFound
Expand All @@ -304,7 +307,7 @@ func isTableRequired(n ast.Node, tableName string, prior int) int {
}
case *ast.List:
for _, item := range n.Items {
if res := isTableRequired(item, tableName, prior); res != tableNotFound {
if res := isTableRequired(item, col, prior); res != tableNotFound {
return res
}
}
Expand Down Expand Up @@ -439,13 +442,14 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
cname = *res.Name
}
cols = append(cols, &Column{
Name: cname,
Type: c.Type,
Table: c.Table,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
Name: cname,
Type: c.Type,
Table: c.Table,
TableAlias: alias,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type Column struct {
Length *int

// XXX: Figure out what PostgreSQL calls `foo.id`
Scope string
Table *ast.TableName
Type *ast.TypeName
Scope string
Table *ast.TableName
TableAlias string
Type *ast.TypeName

skipTableRequiredCheck bool
}
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/mysql/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/mysql/go/models.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.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
id INT(10) NOT NULL,
name VARCHAR(255) NOT NULL,
parent_id INT(10),
PRIMARY KEY (id)
);

-- name: AllAuthors :many
SELECT a.id,
a.name,
p.id as alias_id,
p.name as alias_name
FROM authors a
LEFT JOIN authors p
ON (authors.parent_id = p.id);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/postgres/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.

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/postgres/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE authors (
id INT PRIMARY KEY,
name TEXT NOT NULL,
parent_id INT -- nullable
);

-- name: AllAuthors :many
SELECT a.id,
a.name,
p.id as alias_id,
p.name as alias_name
FROM authors a
LEFT JOIN authors p USING (parent_id);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_left_same_table/postgres/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": "query.sql",
"queries": "query.sql"
}
]
}