Skip to content

Commit

Permalink
Add hack to fix output type for coalesce (#1119)
Browse files Browse the repository at this point in the history
* Add hack to fix output type for coalesce

Add flag to skip table required check for joins when using COALESCE.
  • Loading branch information
kyleconroy authored Aug 12, 2021
1 parent b5d7f67 commit 0dfe728
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
for _, c := range columns {
found = true
c.NotNull = true
c.skipTableRequiredCheck = true
cols = append(cols, c)
}
}
Expand Down Expand Up @@ -245,7 +246,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {

if n, ok := node.(*ast.SelectStmt); ok {
for _, col := range cols {
if !col.NotNull || col.Table == nil {
if !col.NotNull || col.Table == nil || col.skipTableRequiredCheck {
continue
}
for _, f := range n.FromClause.Items {
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Column struct {
Scope string
Table *ast.TableName
Type *ast.TypeName

skipTableRequiredCheck bool
}

type Query struct {
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/coalesce_join/postgresql/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/coalesce_join/postgresql/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.

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/coalesce_join/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo(id bigserial primary key);
CREATE TABLE bar(id bigserial primary key);

-- name: GetBar :many
SELECT foo.*, COALESCE(bar.id, 0) AS bar_id
FROM foo
LEFT JOIN bar ON foo.id = bar.id;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/coalesce_join/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": "query.sql",
"queries": "query.sql"
}
]
}

0 comments on commit 0dfe728

Please sign in to comment.