Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate nullable value from subselect statements #2275

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"errors"
"fmt"
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
Expand Down Expand Up @@ -315,12 +316,14 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
DataType: dataType(fun.ReturnType),
NotNull: !fun.ReturnTypeNullable,
IsFuncCall: true,
FuncName: rel.Name,
})
} else {
cols = append(cols, &Column{
Name: name,
DataType: "any",
IsFuncCall: true,
FuncName: rel.Name,
})
}

Expand All @@ -341,6 +344,10 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if res.Name != nil {
first.Name = *res.Name
}
if !(first.IsFuncCall && strings.EqualFold(first.FuncName, "count")) {
first.NotNull = false
}
Comment on lines +347 to +349
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've researched about cases a subselect returns null.

A column of no rows returns null. but if count() return 0.

According to above documentation, total() also returns 0 in sqlite.
Other aggregate functions returns null.


cols = append(cols, first)
default:
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
Expand Down Expand Up @@ -377,6 +384,12 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if res.Name != nil {
first.Name = *res.Name
}
if !(first.IsFuncCall &&
(strings.EqualFold(first.FuncName, "count") ||
strings.EqualFold(first.FuncName, "total"))) {
first.NotNull = false
}

cols = append(cols, first)

default:
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Column struct {
Length *int
IsNamedParam bool
IsFuncCall bool
FuncName string

// XXX: Figure out what PostgreSQL calls `foo.id`
Scope string
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- name: FirstRowFromFooTable :many
SELECT a, (SELECT a FROM foo limit 1) as "first" FROM foo;

-- name: FirstRowFromEmptyTable :many
SELECT a, (SELECT a FROM empty limit 1) as "first" FROM foo;

-- In MySQL, only count() returns 0 for empty table.
-- https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html
-- name: CountRowsEmptyTable :many
SELECT a, (SELECT count(a) FROM empty) as "count" FROM foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo (a int not NULL, b int);

INSERT INTO foo VALUES (1, 2);
INSERT INTO foo VALUES (3, NULL);
INSERT INTO foo VALUES (4, 5);

CREATE TABLE empty (a int not NULL, b int);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}

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.

Loading
Loading