Repository navigation
Fix unsound scalar aggregation over empty input - #10
Closed
zvonimir-dd wants to merge 2 commits into
Closed
zvonimir-dd wants to merge 2 commits into
zvonimir-dd wants to merge 2 commits into
Conversation
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>
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9.
Two commits, so the soundness fix can be taken on its own:
relation.rs).A scalar aggregate (
GROUP BY ()) always has exactly one group, so theGrouplowering nolonger guards it with
‖∑ source‖whenkeysis empty. This only adds the empty-input row SQLrequires; it cannot create a new equivalence.
normal.rs, optional).When an aggregate's body normalizes to the semiring zero, evaluate it to
0forCOUNT/$SUM0andNULLotherwise, instead of an uninterpreted nullary op. This recoverstestPushAggregateThroughJoinOnEmptyLogicalValues, which commit 1 alone gives up because it wasonly proved through the same collapse.
Verification
Both reproductions from #9 are not provable after commit 1; the control (
WHERE a = 5vsWHERE 5 = a) stays provable.Over
tests/(1731 cases):31f4b6ccockroach/xform/404calcite/testExistsWithAtLeastOneRowSubQuerycalcite/testPushAggregateThroughJoinOnEmptyLogicalValuescalcite/testEmptyAggregateEmptyKeyWithAggregateValuesRuleCommit 1 can only affect the 60 cases containing a group with empty
keys, and changes exactly thefirst three rows above. Across all 1731 cases, every other case gets the same verdict at
31f4b6cand at commit 2; the same 30 panic and the same 24 hit a time limit in both.
cockroach/xform/404is the same bug in the existing suite: one side groups byv, the other isscalar, and they differ on empty input.
🤖 Generated with Claude Code