Skip to content

fix(compiler): Mark nullable when casting NULL #1233

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

Merged
merged 2 commits into from
Oct 9, 2021
Merged
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
9 changes: 8 additions & 1 deletion internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
if res.Name != nil {
name = *res.Name
}
// TODO: The TypeCase code has been copied from below. Instead, we need a recurse function to get the type of a node.
// TODO: The TypeCase code has been copied from below. Instead, we
// need a recurse function to get the type of a node.
if tc, ok := n.Defresult.(*ast.TypeCast); ok {
if tc.TypeName == nil {
return nil, errors.New("no type name type cast")
Expand Down Expand Up @@ -236,6 +237,12 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
// TODO Validate column names
col := toColumn(n.TypeName)
col.Name = name
// TODO Add correct, real type inference
if constant, ok := n.Arg.(*ast.A_Const); ok {
if _, ok := constant.Val.(*ast.Null); ok {
col.NotNull = false
}
}
cols = append(cols, col)

default:
Expand Down
30 changes: 30 additions & 0 deletions internal/endtoend/testdata/cast_null/pgx/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/cast_null/pgx/go/models.go

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

50 changes: 50 additions & 0 deletions internal/endtoend/testdata/cast_null/pgx/go/query.sql.go

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

9 changes: 9 additions & 0 deletions internal/endtoend/testdata/cast_null/pgx/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE foo (bar text);

-- name: ListNullable :many
SELECT
NULL::text as a,
NULL::integer as b,
NULL::bigint as c,
NULL::time as d
FROM foo;
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/cast_null/pgx/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/cast_null/stdlib/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/cast_null/stdlib/go/models.go

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

53 changes: 53 additions & 0 deletions internal/endtoend/testdata/cast_null/stdlib/go/query.sql.go

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

9 changes: 9 additions & 0 deletions internal/endtoend/testdata/cast_null/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE foo (bar text);

-- name: ListNullable :many
SELECT
NULL::text as a,
NULL::integer as b,
NULL::bigint as c,
NULL::time as d
FROM foo;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/cast_null/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}