fix: Set Substrait aggregation phase to INITIAL_TO_RESULT - #25146
Open
namanjain24-sudo wants to merge 1 commit into
Open
fix: Set Substrait aggregation phase to INITIAL_TO_RESULT#25146namanjain24-sudo wants to merge 1 commit into
namanjain24-sudo wants to merge 1 commit into
Conversation
namanjain24-sudo
force-pushed
the
fix-substrait-aggregation-phase
branch
2 times, most recently
from
September 11, 2026 12:38
26b02c3 to
d50b923
Compare
Author
Thank you, @Xuanwo |
namanjain24-sudo
force-pushed
the
fix-substrait-aggregation-phase
branch
from
September 11, 2026 16:02
d50b923 to
ae437fa
Compare
The producer left `phase` at its default on every aggregate and window function call it emits, so each one carried AGGREGATION_PHASE_UNSPECIFIED. That is not the same as omitting the field: the spec gives the value the meaning INTERMEDIATE_TO_RESULT, i.e. that the arguments are already intermediate aggregation state to be combined. A LogicalPlan::Aggregate is always a complete aggregation over its input rows. The partial/final split is a physical planning concern that the logical producer has no notion of. Both `AggregateFunction.phase` and `Expression.WindowFunction.phase` are documented as required, and as needing INITIAL_TO_RESULT for a complete invocation, so set that at both call sites. This stays invisible to a DataFusion-to-DataFusion round trip because the consumer never reads `phase`, but a consumer that honours the declaration reads a complete aggregation as one whose arguments are partial state.
namanjain24-sudo
force-pushed
the
fix-substrait-aggregation-phase
branch
from
September 11, 2026 17:22
ae437fa to
334f6f7
Compare
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?
Rationale for this change
The Substrait producer never sets
phaseon the aggregate and window functioncalls it emits, so every call carries
AGGREGATION_PHASE_UNSPECIFIED.That is not the same as leaving the field out. The spec gives the value a
meaning, and it is not the one these plans need:
Both
AggregateFunction.phaseandExpression.WindowFunction.phasearedocumented as
Required. Must be set to INITIAL_TO_RESULT for ... that are not decomposable.A
LogicalPlan::Aggregateis always a complete aggregation over its inputrows. The partial/final split is a physical planning concern, and the logical
producer has no notion of it, so
INITIAL_TO_RESULTis the phase these plansshould declare. What they declare instead carries the spec meaning
INTERMEDIATE_TO_RESULT: that the arguments are already intermediate state tobe combined.
This stays invisible to a DataFusion-to-DataFusion round trip because the
consumer never reads the field (#24967). A consumer that does honour the
declaration reads a complete aggregation as one whose arguments are already
partial state.
What changes are included in this PR?
Set
phasetoAGGREGATION_PHASE_INITIAL_TO_RESULTat the two producer callsites that emit it:
from_aggregate_functioninproducer/expr/aggregate_function.rsmake_substrait_window_functioninproducer/expr/window_function.rsNo other producer site emits the field, and the consumer does not read it, so
nothing else changes.
What is the testing strategy for this PR?
New test
aggregate_and_window_functions_declare_initial_to_resultindatafusion/substrait/tests/cases/serialize.rs. It produces plans for a bareaggregate, a grouped aggregate, and a window function, then walks the produced
protobuf directly and asserts the phase on every aggregate and window function
call, rather than round-tripping through the consumer (which ignores the
field, so a round trip could not catch this).
Verified the test fails without the producer change:
Are there any user-facing changes?
Plans produced by
to_substrait_plannow declareAGGREGATION_PHASE_INITIAL_TO_RESULTinstead ofAGGREGATION_PHASE_UNSPECIFIEDon aggregate and window function calls. Thisis a fix to the emitted protobuf rather than a Rust API change. Consumers that
read
phasewill now see the correct declaration; consumers that ignore it,including DataFusion's own, are unaffected.