Repository navigation
fix: do not rewrite SUM(x + literal) for floating point arguments - #26241
Closed
RishavTiwari25 wants to merge 1 commit into
Closed
RishavTiwari25 wants to merge 1 commit into
RishavTiwari25 wants to merge 1 commit into
Conversation
With two or more `SUM(x + <literal>)` aggregates over the same `x`, the optimizer rewrites them into `SUM(x) + <literal> * COUNT(x)`. Floating point addition is not associative, so for `Float64` this changes the result: `sum(x + 1.0)` over `1e16, 1.0, -1e16` returns `2.0` alone but `4.0` next to `sum(x + 2.0)`. The rewrite now skips aggregates whose argument or literal is a floating point type, so they are computed as written. Integer aggregates are rewritten as before. Closes apache#26223 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Author
|
@alamb could you trigger CI on this one when you have a moment? It is my first PR here. Thanks! |
Contributor
|
Thank you @RishavTiwari25. However, I question is this is a bug, see #26223 (comment) |
alamb
requested changes
Oct 12, 2026
alamb
left a comment
Contributor
There was a problem hiding this comment.
As mentioned, I don't think this is a bug
Author
|
Closing per the discussion on #26223: this is floating point reassociation rather than a bug, and the rewritten form is actually the more accurate one in the reported example. |
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.
Which issue does this PR close?
SUM(x + c)toSUM(x) + c * COUNT(x)changes the result for floating pointx#26223.Rationale for this change
When a query has two or more
SUM(x + <literal>)aggregates over the samex, the optimizer rewrites them intoSUM(x) + <literal> * COUNT(x)so that only two aggregates are computed. For integers that is exact, but floating point addition is not associative, so forFloat64the rewritten query can return a different value than the same aggregate on its own:What changes are included in this PR?
rewrite_multiple_linear_aggregatesnow takes the aggregate's input schema and skips candidates whose argument or literal is a floating point type, so those aggregates are computed as written. Integer aggregates are rewritten as before.Sum::simplify_expr_op_literalbecause the inexactness comes from floating point arithmetic, not fromSUMitself, and because fix: plan and run repeated SUM(x + literal) on decimals and intervals #25966 is changing that function for decimals and intervals; keeping this change separate avoids conflicts, and I am happy to rebase on whichever lands first.What is the testing strategy for this PR?
New
sqllogictestcases inaggregates_simplify.sltcover aDOUBLEcolumn with float literals, aDOUBLEcolumn with integer literals, and an integer column with float literals, checking both the results and that theEXPLAINplan keeps the original aggregates. The existing integer cases in the same file still show the rewrite.Are there any user-facing changes?
Queries with several
SUM(<float> + <literal>)aggregates now return the same values as the individual aggregates. They no longer get the sharedSUM/COUNTrewrite, so they compute one aggregate per expression, as they did before the rewrite was introduced.I used an AI coding assistant while working on this change; I have read the diff and the tests and can walk through them.