Skip to content

Commit 5626602

Browse files
authored
fix(compiler): Mark nullable when casting NULL (#1233)
1 parent 5789c85 commit 5626602

File tree

11 files changed

+234
-1
lines changed

11 files changed

+234
-1
lines changed

internal/compiler/output_columns.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
106106
if res.Name != nil {
107107
name = *res.Name
108108
}
109-
// TODO: The TypeCase code has been copied from below. Instead, we need a recurse function to get the type of a node.
109+
// TODO: The TypeCase code has been copied from below. Instead, we
110+
// need a recurse function to get the type of a node.
110111
if tc, ok := n.Defresult.(*ast.TypeCast); ok {
111112
if tc.TypeName == nil {
112113
return nil, errors.New("no type name type cast")
@@ -236,6 +237,12 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
236237
// TODO Validate column names
237238
col := toColumn(n.TypeName)
238239
col.Name = name
240+
// TODO Add correct, real type inference
241+
if constant, ok := n.Arg.(*ast.A_Const); ok {
242+
if _, ok := constant.Val.(*ast.Null); ok {
243+
col.NotNull = false
244+
}
245+
}
239246
cols = append(cols, col)
240247

241248
default:

internal/endtoend/testdata/cast_null/pgx/go/db.go

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

internal/endtoend/testdata/cast_null/pgx/go/models.go

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

internal/endtoend/testdata/cast_null/pgx/go/query.sql.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE foo (bar text);
2+
3+
-- name: ListNullable :many
4+
SELECT
5+
NULL::text as a,
6+
NULL::integer as b,
7+
NULL::bigint as c,
8+
NULL::time as d
9+
FROM foo;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v4",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

internal/endtoend/testdata/cast_null/stdlib/go/db.go

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

internal/endtoend/testdata/cast_null/stdlib/go/models.go

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

internal/endtoend/testdata/cast_null/stdlib/go/query.sql.go

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE foo (bar text);
2+
3+
-- name: ListNullable :many
4+
SELECT
5+
NULL::text as a,
6+
NULL::integer as b,
7+
NULL::bigint as c,
8+
NULL::time as d
9+
FROM foo;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)