Repository navigation
fix(substrait): dedupe repeated GROUP BY keys in the producer - #27073
jonasHanhan wants to merge 3 commits into
Conversation
DataFusion's Aggregate schema keeps each grouping key once, but the Substrait producer was emitting a repeated expression_reference for GROUP BY k, k. The consumer then expanded those refs into extra columns, so projections built from the DataFusion plan indexed the wrong fields and a round trip swapped the grouping and measure columns. Skip duplicate grouping expressions within each grouping so the AggregateRel output layout matches the DataFusion schema. Closes apache#27058.
bvolpato
left a comment
There was a problem hiding this comment.
LGTM. The per-group deduplication matches the ordering used by Aggregate's schema, and the regression checks the unoptimized plan where the duplicate key is still present. I ran the Substrait crate tests at this head, including the round-trip regression.
Could you also enable Actions on your fork and push again? Rust (fork) currently reports no Rust workflow run for this commit, so CI still needs to run before merge. Thanks for fixing this.
|
Thanks for the review! I'll enable Actions on the fork and push again so Rust (fork) can run. |
kosiew
left a comment
There was a problem hiding this comment.
Thanks for the fix. Deduplicating grouping expressions before serialization keeps both Substrait encodings consistent with DataFusion's aggregate schema.
The regression coverage addresses the reported issue. Only minor test improvements remain.
| "producer should declare the repeated key once" | ||
| ); | ||
| assert_eq!( | ||
| aggregate.groupings[0].expression_references, |
There was a problem hiding this comment.
Could we also assert that aggregate.groupings[0].grouping_expressions.len() == 1? This would directly verify the deprecated inline encoding, which the consumer prioritizes when present.
| let expected = DataFrame::new(ctx.state(), plan.clone()).collect().await?; | ||
| let actual = DataFrame::new(ctx.state(), plan2).collect().await?; | ||
| assert_eq!( | ||
| format!("{expected:?}"), |
There was a problem hiding this comment.
Consider using datafusion::assert_batches_eq! with the expected rows (1, 2.00) and (3, 4.50) for clearer failure diagnostics. Retain a separate schema equality check to verify types and nullability.
alamb
left a comment
There was a problem hiding this comment.
Thanks @jonasHanhan and @bvolpato and @kosiew
| ) -> datafusion::common::Result<Grouping> { | ||
| let mut expression_references = Vec::with_capacity(exprs.len()); | ||
| let mut grouping_expressions = Vec::with_capacity(exprs.len()); | ||
| // DataFusion's Aggregate schema keeps each grouping key once |
There was a problem hiding this comment.
so basically the problem is that datafusion won't execute GROUP BY k, k (it will collapse the k)?
There was a problem hiding this comment.
Yeah, almost — it'll still execute GROUP BY k, k, it just collapses the extra k in the aggregate. The producer was still emitting both grouping expressions, so this just dedupes them first.
Also assert the deprecated inline grouping encoding, and check the expected batches and schema directly.
Which issue does this PR close?
GROUP BY k, kgives a plan whose projection reads the aggregate's columns shifted by one #27058.Rationale for this change
GROUP BY k, kis legal SQL and does not change the result, but producing Substrait from that plan and consuming it again swapped the grouping and measure columns.DataFusion's
Aggregateschema keeps each grouping key once (grouping_set_to_exprlist). The producer already shared identical expressions ingrouping_expressions, but it still wrote a repeatedexpression_referencefor each occurrence ([0, 0]). The consumer expands each reference into its own output column, so the AggregateRel effectively gained an extra column. Projections built from the DataFusion plan still indexed fields as if that column were not there, and a round trip returned(sum, k)under the names(k, s).What changes are included in this PR?
In
parse_flat_grouping_exprs, skip grouping expressions that are already present in the current grouping. The AggregateRel then declares and references the repeated key once, matching the DataFusion Aggregate schema that projections are built against.What is the testing strategy for this PR?
Added
aggregate_duplicate_group_by_key_roundtripinroundtrip_logical_plan.rs. It uses an unoptimized plan soeliminate_duplicated_exprdoes not hide the bug, checks that the producer emits a single grouping expression reference, and asserts that collecting the plan before and after a Substrait round trip returns the same batches. It also covers the optimized path where the duplicate key is already removed.Locally:
cargo test -p datafusion-substraitpasses, andcargo clippy -p datafusion-substrait --all-targets -- -D warningsandcargo fmtare clean.Are there any user-facing changes?
Substrait plans produced from logical plans that still contain repeated
GROUP BYkeys (for example unoptimized plans) now round-trip with the correct column order. No API changes.