Skip to content

Commit 0dfe728

Browse files
authored
Add hack to fix output type for coalesce (#1119)
* Add hack to fix output type for coalesce Add flag to skip table required check for joins when using COALESCE.
1 parent b5d7f67 commit 0dfe728

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

internal/compiler/output_columns.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
140140
for _, c := range columns {
141141
found = true
142142
c.NotNull = true
143+
c.skipTableRequiredCheck = true
143144
cols = append(cols, c)
144145
}
145146
}
@@ -245,7 +246,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
245246

246247
if n, ok := node.(*ast.SelectStmt); ok {
247248
for _, col := range cols {
248-
if !col.NotNull || col.Table == nil {
249+
if !col.NotNull || col.Table == nil || col.skipTableRequiredCheck {
249250
continue
250251
}
251252
for _, f := range n.FromClause.Items {

internal/compiler/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Column struct {
2626
Scope string
2727
Table *ast.TableName
2828
Type *ast.TypeName
29+
30+
skipTableRequiredCheck bool
2931
}
3032

3133
type Query struct {

internal/endtoend/testdata/coalesce_join/postgresql/go/db.go

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

internal/endtoend/testdata/coalesce_join/postgresql/go/models.go

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

internal/endtoend/testdata/coalesce_join/postgresql/go/query.sql.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE foo(id bigserial primary key);
2+
CREATE TABLE bar(id bigserial primary key);
3+
4+
-- name: GetBar :many
5+
SELECT foo.*, COALESCE(bar.id, 0) AS bar_id
6+
FROM foo
7+
LEFT JOIN bar ON foo.id = bar.id;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)