Skip to content

Commit

Permalink
fixing the code to make the subselect nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu-ichiroh committed Aug 3, 2023
1 parent ce1b4c5 commit 093ea55
Show file tree
Hide file tree
Showing 30 changed files with 417 additions and 139 deletions.
26 changes: 10 additions & 16 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/catalog"

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
}

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

if hasWhereOrHavingClause(n) {
if !(first.IsFuncCall &&
(strings.EqualFold(first.FuncName, "count") ||
strings.EqualFold(first.FuncName, "total"))) {
first.NotNull = false
}

Expand Down Expand Up @@ -718,17 +726,3 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table, targetList *ast.List)

return nil
}

// hasWhereOrHavingClause returns true if the statement contains WHERE or HAVING clause
func hasWhereOrHavingClause(node ast.Node) bool {
stmt := node.(*ast.SelectStmt)

if _, isTODO := stmt.WhereClause.(*ast.TODO); stmt.WhereClause != nil && !isTODO {
return true
}
if _, isTODO := stmt.HavingClause.(*ast.TODO); stmt.HavingClause != nil && !isTODO {
return true
}

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

// XXX: Figure out what PostgreSQL calls `foo.id`
Scope string
Expand Down

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

71 changes: 53 additions & 18 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.

13 changes: 8 additions & 5 deletions internal/endtoend/testdata/nullable_subselect/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
CREATE TABLE foo (a int not null, b int);
-- name: FirstRowFromFooTable :many
SELECT a, (SELECT a FROM foo limit 1) as "first" FROM foo;

-- name: SubqueryWithWhereClause :many
SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo;
-- name: FirstRowFromEmptyTable :many
SELECT a, (SELECT a FROM empty limit 1) as "first" FROM foo;

-- name: SubqueryWithHavingClause :many
SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" 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);
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"schema": "schema.sql",
"queries": "query.sql"
}
]
Expand Down

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.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
CREATE TABLE foo (a int not null, b int);
-- name: FirstRowFromFooTable :many
SELECT a, (SELECT a FROM foo limit 1) as "first" FROM foo;

-- name: SubqueryWithWhereClause :many
SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo;
-- name: FirstRowFromEmptyTable :many
SELECT a, (SELECT a FROM empty limit 1) as "first" FROM foo;

-- name: SubqueryWithHavingClause :many
SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo;
-- In PostgreSQL, only count() returns 0 for empty table.
-- https://www.postgresql.org/docs/15/functions-aggregate.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);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"schema": "schema.sql",
"queries": "query.sql"
}
]
Expand Down

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

Loading

0 comments on commit 093ea55

Please sign in to comment.