Skip to content

Commit 6c92132

Browse files
authored
compiler: Return a single column from coalesce (#639)
Fixes #458
1 parent c91f7a1 commit 6c92132

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

internal/compiler/output_columns.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,26 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
9797
}
9898

9999
case *pg.CoalesceExpr:
100+
var found bool
100101
for _, arg := range n.Args.Items {
102+
if found {
103+
continue
104+
}
101105
if ref, ok := arg.(*pg.ColumnRef); ok {
102106
columns, err := outputColumnRefs(res, tables, ref)
103107
if err != nil {
104108
return nil, err
105109
}
106110
for _, c := range columns {
111+
found = true
107112
c.NotNull = true
108113
cols = append(cols, c)
109114
}
110115
}
111116
}
117+
if !found {
118+
cols = append(cols, &Column{Name: "coalesce", DataType: "any", NotNull: false})
119+
}
112120

113121
case *pg.ColumnRef:
114122
if hasStarRef(n) {

internal/endtoend/testdata/coalesce/go/models.go

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

internal/endtoend/testdata/coalesce/go/query.sql.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
CREATE TABLE foo (bar text);
1+
CREATE TABLE foo (bar text, bat text not null);
22

33
-- name: Coalesce :many
44
SELECT coalesce(bar, '') as login
55
FROM foo;
6+
7+
-- name: CoalesceColumns :many
8+
SELECT bar, bat, coalesce(bar, bat)
9+
FROM foo;

0 commit comments

Comments
 (0)