From 8484a84117283165502cea0a9497d04c5d79669e Mon Sep 17 00:00:00 2001 From: mpyw Date: Tue, 7 Jun 2022 19:44:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Correctly=20switch=20?= =?UTF-8?q?`coalesce()`=20result=20`.NotNull`=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change follows the previous implementation that mutates and returns the first column, but switches only the `.NotNull` value depending on the situation. BREAKING CHANGE: 🧨 `coalesce()` may assign nullable results ✅ Closes: #1663 --- internal/compiler/output_columns.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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}) } From 2c7060432fc6c95e320c1dde50cfd7fa3abcb738 Mon Sep 17 00:00:00 2001 From: mpyw Date: Tue, 7 Jun 2022 19:56:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=F0=9F=92=8D=20Follow=20breaking=20?= =?UTF-8?q?changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go | 4 ++-- .../testdata/coalesce/postgresql/stdlib/go/query.sql.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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) {