diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index a01675645b..b34715b35d 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -148,9 +148,11 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) { if res.Name != nil { name = *res.Name } - var found bool + var firstColumn *Column + var shouldNotBeNull bool for _, arg := range n.Args.Items { - if found { + if _, ok := arg.(*ast.A_Const); ok { + shouldNotBeNull = true continue } if ref, ok := arg.(*ast.ColumnRef); ok { @@ -159,14 +161,18 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) { return nil, err } for _, c := range columns { - found = true - c.NotNull = true - c.skipTableRequiredCheck = true - cols = append(cols, c) + if firstColumn == nil { + firstColumn = c + } + shouldNotBeNull = shouldNotBeNull || c.NotNull } } } - if !found { + if firstColumn != nil { + firstColumn.NotNull = shouldNotBeNull + firstColumn.skipTableRequiredCheck = true + cols = append(cols, firstColumn) + } else { cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false}) } diff --git a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go index 68fb689859..9e99249537 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go +++ b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go @@ -73,7 +73,7 @@ FROM foo type CoalesceNumericNullRow struct { Baz sql.NullInt64 - Baz_2 int64 + Baz_2 sql.NullInt64 } func (q *Queries) CoalesceNumericNull(ctx context.Context) ([]CoalesceNumericNullRow, error) { @@ -159,7 +159,7 @@ FROM foo type CoalesceStringNullRow struct { Bar sql.NullString - Bar_2 string + Bar_2 sql.NullString } func (q *Queries) CoalesceStringNull(ctx context.Context) ([]CoalesceStringNullRow, error) { diff --git a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go index d0053363b1..5505f94d82 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go +++ b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go @@ -79,7 +79,7 @@ FROM foo type CoalesceNumericNullRow struct { Baz sql.NullInt64 - Baz_2 int64 + Baz_2 sql.NullInt64 } func (q *Queries) CoalesceNumericNull(ctx context.Context) ([]CoalesceNumericNullRow, error) { @@ -174,7 +174,7 @@ FROM foo type CoalesceStringNullRow struct { Bar sql.NullString - Bar_2 string + Bar_2 sql.NullString } func (q *Queries) CoalesceStringNull(ctx context.Context) ([]CoalesceStringNullRow, error) {