Skip to content

Commit 540c5b9

Browse files
Bockitclaude
authored andcommitted
compiler: an aggregate inside an expression still yields one row
The previous commit keeps a scalar subquery's column never null only when the column is a bare aggregate call. `(SELECT count(*)::int FROM ...)` and `(SELECT count(*) + 1 FROM ...)` are aggregates with no GROUP BY too, and always yield one row, but were reported as nullable. Look for the aggregate anywhere in the column's expression, outside nested subqueries and window functions. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 0bb6a6e commit 540c5b9

7 files changed

Lines changed: 328 additions & 4 deletions

File tree

‎internal/compiler/output_columns.go‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,9 @@ var aggregates = map[string]bool{
826826
}
827827

828828
// yieldsOneRow reports whether a scalar subquery always yields exactly one row:
829-
// a SELECT with no FROM or WHERE, or one whose every column is an aggregate,
830-
// with no GROUP BY, HAVING, LIMIT, OFFSET or set operation.
829+
// a SELECT with no FROM or WHERE, or one whose every column holds an aggregate,
830+
// bare or inside an expression such as a cast, with no GROUP BY, HAVING, LIMIT,
831+
// OFFSET or set operation.
831832
func yieldsOneRow(node ast.Node) bool {
832833
sel, ok := node.(*ast.SelectStmt)
833834
if !ok || sel.Op != ast.None || given(sel.LimitCount) || given(sel.LimitOffset) || given(sel.HavingClause) {
@@ -847,14 +848,34 @@ func yieldsOneRow(node ast.Node) bool {
847848
if !ok {
848849
return false
849850
}
850-
call, ok := res.Val.(*ast.FuncCall)
851-
if !ok || call.Func == nil || call.Over != nil || !aggregates[strings.ToLower(call.Func.Name)] {
851+
var find aggregateFinder
852+
astutils.Walk(&find, res.Val)
853+
if !find.found {
852854
return false
853855
}
854856
}
855857
return true
856858
}
857859

860+
// aggregateFinder looks for an aggregate call in an expression. It doesn't look
861+
// inside subqueries, which are query levels of their own, and a window function
862+
// is not an aggregate.
863+
type aggregateFinder struct {
864+
found bool
865+
}
866+
867+
func (f *aggregateFinder) Visit(n ast.Node) astutils.Visitor {
868+
switch n := n.(type) {
869+
case *ast.SubLink, *ast.SelectStmt:
870+
return nil
871+
case *ast.FuncCall:
872+
if n.Func != nil && n.Over == nil && aggregates[strings.ToLower(n.Func.Name)] {
873+
f.found = true
874+
}
875+
}
876+
return f
877+
}
878+
858879
// given reports whether a clause was written: the parser leaves one that wasn't
859880
// as nil or as a placeholder
860881
func given(n ast.Node) bool {

‎internal/endtoend/testdata/nullable_scalar_subquery_aggregate/postgresql/pgx/go/db.go‎

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

‎internal/endtoend/testdata/nullable_scalar_subquery_aggregate/postgresql/pgx/go/models.go‎

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

‎internal/endtoend/testdata/nullable_scalar_subquery_aggregate/postgresql/pgx/go/query.sql.go‎

Lines changed: 192 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- name: AggregateUnderCast :many
2+
SELECT t.id, (SELECT count(*)::int FROM trackers k WHERE k.truck_id = t.id) AS trackers
3+
FROM trucks t;
4+
5+
-- name: AggregateInExpression :many
6+
SELECT t.id, (SELECT count(*)::int + 1 FROM trackers k WHERE k.truck_id = t.id) AS trackers
7+
FROM trucks t;
8+
9+
-- name: GroupedAggregate :many
10+
SELECT t.id, (SELECT count(*) FROM trackers k WHERE k.truck_id = t.id GROUP BY k.active LIMIT 1) AS trackers
11+
FROM trucks t;
12+
13+
-- name: PlainFunction :many
14+
SELECT t.id, (SELECT upper(d.name)::text FROM depots d WHERE d.id = t.depot_id) AS depot
15+
FROM trucks t;
16+
17+
-- name: AggregateInNestedSubquery :many
18+
SELECT t.id, (SELECT d.id::int + (SELECT count(*)::int FROM trackers) FROM depots d WHERE d.id = t.depot_id) AS n
19+
FROM trucks t;
20+
21+
-- name: WindowFunction :many
22+
SELECT t.id, (SELECT count(*) OVER () FROM depots d WHERE d.id = t.depot_id) AS n
23+
FROM trucks t;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE TABLE depots (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
note text
5+
);
6+
7+
CREATE TABLE trucks (
8+
id BIGSERIAL PRIMARY KEY,
9+
depot_id bigint NOT NULL,
10+
plate text NOT NULL
11+
);
12+
13+
CREATE TABLE trackers (
14+
id BIGSERIAL PRIMARY KEY,
15+
truck_id bigint NOT NULL,
16+
active boolean NOT NULL,
17+
serial text
18+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)