fix: Set Substrait aggregation phase to INITIAL_TO_RESULT - #25146
Open
namanjain24-sudo wants to merge 2 commits into
Open
namanjain24-sudo wants to merge 2 commits into
namanjain24-sudo wants to merge 2 commits 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
A DataFusion round trip cannot see a field the consumer never reads, so nothing here catches a wrong AggregationPhase: the consumer rebuilds a complete aggregation whatever the plan says. substrait-spark does read it, and maps the default UNSPECIFIED to Spark's Final, which Spark then rejects because the inputs are raw rows rather than partial buffers. An ignored Rust test writes the plan for count/sum/avg, and a small Maven project converts it with substrait-java and runs it in Spark over t(i) = 1, 2, 3, checking both the aggregation mode and the rows. Two workarounds are applied on the Java side, each named after the issue it stands in for: apache#11545 for the extension URN, and apache#25049 for the unset output_type, which can go once that fix lands. The job runs only when the Substrait crate changes, because it pulls a Spark sized dependency set.
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:
The java interop test
mainand this branch both pass every test in the repository, because theDataFusion consumer never reads
phaseand rebuilds a complete aggregationeither way. #25100 asked for that gap to be covered against another
implementation, so this PR also adds one:
tests/cases/java_interop.rswrites the plan forSELECT count(i), sum(i), avg(i) FROM t. It is#[ignore]d, socargo testis unchanged for anyone without a JVM.
java-interop/converts that plan with substrait-java 0.103.0 and runs it inSpark 3.5.4 over
t(i) = 1, 2, 3, checking the aggregation mode and the rows.Run both halves with:
Against a plan from this branch it passes, with
Completeandcount 3, sum 6, avg 2.0. Against a plan frommainit fails withexpected: <[Complete, Complete, Complete]> but was: <[Final, Final, Final]>,which is the failure this change prevents.
Two workarounds are applied on the Java side, each named after the issue it
stands in for: #11545 for the extension URN, and #25049 for the unset
output_type, which can be removed once #25090 lands.The CI job runs only when
datafusion/substrait/**orrust.ymlchanges,because it pulls a Spark sized dependency set.
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.