Skip to content

Commit 7eb8516

Browse files
Bockitclaude
authored andcommitted
compiler: only count keeps an expression in a scalar subquery never null
The commit before last kept a scalar subquery's column never null when an aggregate sat anywhere in its expression. That is right for count, which is never NULL, and wrong for the rest: `(SELECT max(x)::int FROM ...)` yields one row, and that row is NULL when nothing matched, but a cast reports its column as never null. Keep the column never null only for an expression whose aggregates are all count. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 540c5b9 commit 7eb8516

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎internal/compiler/output_columns.go‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +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 holds an aggregate,
830-
// bare or inside an expression such as a cast, with no GROUP BY, HAVING, LIMIT,
831-
// OFFSET or set operation.
829+
// a SELECT with no FROM or WHERE, or one whose every column is an aggregate, or
830+
// an expression over count such as a cast of it, with no GROUP BY, HAVING,
831+
// LIMIT, OFFSET or set operation.
832832
func yieldsOneRow(node ast.Node) bool {
833833
sel, ok := node.(*ast.SelectStmt)
834834
if !ok || sel.Op != ast.None || given(sel.LimitCount) || given(sel.LimitOffset) || given(sel.HavingClause) {
@@ -848,29 +848,46 @@ func yieldsOneRow(node ast.Node) bool {
848848
if !ok {
849849
return false
850850
}
851+
if call, ok := res.Val.(*ast.FuncCall); ok {
852+
if call.Func == nil || call.Over != nil || !aggregates[strings.ToLower(call.Func.Name)] {
853+
return false
854+
}
855+
continue
856+
}
857+
// An expression over count, such as count(*)::int, is an aggregate
858+
// too and can't be NULL. One over any other aggregate yields a row
859+
// as well, but that row is NULL when nothing matched, and the
860+
// expression's own column doesn't say so.
851861
var find aggregateFinder
852862
astutils.Walk(&find, res.Val)
853-
if !find.found {
863+
if !find.count || find.other {
854864
return false
855865
}
856866
}
857867
return true
858868
}
859869

860-
// aggregateFinder looks for an aggregate call in an expression. It doesn't look
870+
// aggregateFinder looks for aggregate calls in an expression. It doesn't look
861871
// inside subqueries, which are query levels of their own, and a window function
862872
// is not an aggregate.
863873
type aggregateFinder struct {
864-
found bool
874+
count bool
875+
other bool
865876
}
866877

867878
func (f *aggregateFinder) Visit(n ast.Node) astutils.Visitor {
868879
switch n := n.(type) {
869880
case *ast.SubLink, *ast.SelectStmt:
870881
return nil
871882
case *ast.FuncCall:
872-
if n.Func != nil && n.Over == nil && aggregates[strings.ToLower(n.Func.Name)] {
873-
f.found = true
883+
if n.Func == nil || n.Over != nil {
884+
return f
885+
}
886+
name := strings.ToLower(n.Func.Name)
887+
if name == "count" {
888+
f.count = true
889+
} else if aggregates[name] {
890+
f.other = true
874891
}
875892
}
876893
return f

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

Lines changed: 60 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/query.sql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ FROM trucks t;
2121
-- name: WindowFunction :many
2222
SELECT t.id, (SELECT count(*) OVER () FROM depots d WHERE d.id = t.depot_id) AS n
2323
FROM trucks t;
24+
25+
-- name: OtherAggregateUnderCast :many
26+
SELECT t.id, (SELECT max(k.id)::int FROM trackers k WHERE k.truck_id = t.id) AS newest
27+
FROM trucks t;
28+
29+
-- name: CountWithAnotherAggregate :many
30+
SELECT t.id, (SELECT count(*)::int + max(k.id)::int FROM trackers k WHERE k.truck_id = t.id) AS n
31+
FROM trucks t;

0 commit comments

Comments
 (0)