Skip to content

Fix unsound scalar aggregation over empty input - #10

Closed
zvonimir-dd wants to merge 2 commits into
qed-solver:mainfrom
zvonimir-dd:fix-empty-grouping-set
Closed

zvonimir-dd wants to merge 2 commits into
qed-solver:mainfrom
zvonimir-dd:fix-empty-grouping-set

Conversation

@zvonimir-dd

Copy link
Copy Markdown

Fixes #9.

Two commits, so the soundness fix can be taken on its own:

  1. Fix unsoundness: empty grouping set must yield one row over empty input (relation.rs).
    A scalar aggregate (GROUP BY ()) always has exactly one group, so the Group lowering no
    longer guards it with ‖∑ source‖ when keys is empty. This only adds the empty-input row SQL
    requires; it cannot create a new equivalence.
  2. Fold an aggregate over a provably-empty input to its SQL value (normal.rs, optional).
    When an aggregate's body normalizes to the semiring zero, evaluate it to 0 for
    COUNT/$SUM0 and NULL otherwise, instead of an uninterpreted nullary op. This recovers
    testPushAggregateThroughJoinOnEmptyLogicalValues, which commit 1 alone gives up because it was
    only proved through the same collapse.

Verification

Both reproductions from #9 are not provable after commit 1; the control (WHERE a = 5 vs
WHERE 5 = a) stays provable.

Over tests/ (1731 cases):

case 31f4b6c commit 1 commit 2
cockroach/xform/404 provable not provable not provable
calcite/testExistsWithAtLeastOneRowSubQuery not provable provable provable
calcite/testPushAggregateThroughJoinOnEmptyLogicalValues provable not provable provable
calcite/testEmptyAggregateEmptyKeyWithAggregateValuesRule not provable not provable provable

Commit 1 can only affect the 60 cases containing a group with empty keys, and changes exactly the
first three rows above. Across all 1731 cases, every other case gets the same verdict at 31f4b6c
and at commit 2; the same 30 panic and the same 24 hit a time limit in both.

cockroach/xform/404 is the same bug in the existing suite: one side groups by v, the other is
scalar, and they differ on empty input.

🤖 Generated with Claude Code

zvonimir-dd and others added 2 commits September 28, 2026 00:11
An empty grouping set (`GROUP BY ()`, i.e. scalar aggregation) always
produces exactly one group, even over an empty source. The Group lowering
guarded every group with ||sum source|| ("the group is non-empty"), which
zeroes the single group of an empty grouping set when the source is empty,
unsoundly equating `SELECT agg(x) FROM t WHERE p` with the grouped
`... GROUP BY k` (and even COUNT with SUM over empty input).

Use left = 1 when the grouping set is empty.

Refs qed-solver#9.

Environment: Datadog workspace

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
When an aggregate's body normalizes to the semiring zero, evaluate it to
its empty-input value (COUNT/$SUM0 -> 0, otherwise NULL) instead of an
uninterpreted nullary op. Recovers proofs that hinge on aggregate values
over empty input (e.g. AggregateValuesRule, PushAggregateThroughJoin on
empty inputs) without weakening soundness: the fold fires only when the
aggregated bag is provably empty.

Refs qed-solver#9.

Environment: Datadog workspace

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@wsx-ucb

wsx-ucb commented Oct 5, 2026

Copy link
Copy Markdown
Member

Hi, thanks for the report and the proposed fix. Can confirm I implemented the incorrect semantics for the empty grouping set according to the SQL standard, and I've fixed it in 9e9c262. I didn't include the second change you proposed, mostly because I'm not actively maintaining this project right now, and don't want to further complicate the normalization pipeline. Your changes do make sense to me though!

@wsx-ucb wsx-ucb closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsound: a scalar aggregate (empty GROUP BY) yields zero rows over empty input

2 participants