[SPARK-32750][SQL] Support whole-stage codegen for SortAggregate with grouping keys#57153
Open
ulysses-you wants to merge 2 commits into
Open
[SPARK-32750][SQL] Support whole-stage codegen for SortAggregate with grouping keys#57153ulysses-you wants to merge 2 commits into
ulysses-you wants to merge 2 commits into
Conversation
… grouping keys Add whole-stage code-gen support for SortAggregateExec when it has grouping keys. Previously only the no-grouping-keys path was code-gen'd; with keys it fell back to the interpreted SortBasedAggregationIterator. A new internal config spark.sql.codegen.aggregate.sortAggregate.withKeys.enabled (default true) gates this path and takes effect only when spark.sql.codegen.aggregate.sortAggregate.enabled is enabled. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
|
.cc @viirya @dongjoon-hyun @cloud-fan if you have time to take a look, thank you! |
Contributor
|
Long time no see. |
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.
What changes were proposed in this pull request?
This PR adds whole-stage code-gen support for
SortAggregateExecwhen it has groupingkeys. Previously only the no-grouping-keys path was code-gen'd; with keys it fell back to
the interpreted
SortBasedAggregationIterator.The implementation:
AggregateCodegenSupportis refactored to share the aggregation-buffer creation(
createAggBufVars) and buffer-update (generateAggBufferUpdateCode) logic between theno-keys path and the new sort-based with-keys path.
SortAggregateExecimplementsdoProduceWithKeys/doConsumeWithKeys. Since the inputis sorted by the grouping keys, a group's result is emitted as soon as the next group
starts (detected by comparing the binary representation of the grouping key). The produce
loop is resumable across
processNextinvocations viashouldStop(), so a completedgroup's output row is not overwritten by the reused output buffer.
supportCodegen), because group boundaries are detected viaUnsafeRow.equals.spark.sql.codegen.aggregate.sortAggregate.withKeys.enabled(default true) gates this path; it takes effect only when
spark.sql.codegen.aggregate.sortAggregate.enabledis enabled.Why are the changes needed?
Sort aggregate with grouping keys was the only remaining aggregate path without whole-stage
code-gen, forcing it through the slower interpreted iterator.
SortAggregateBenchmarkshowsa consistent 1.2~1.3x speedup for grouped aggregates on the code-gen path.
Does this PR introduce any user-facing change?
No. This is an internal code-gen optimization; results are unchanged. The new config is
internal().How was this patch tested?
WholeStageCodegenSuitecovering: multiple/numeric/string/decimal groupingkeys, null keys and values, single-row groups, a single all-rows group, downstream limit
(resumable
shouldStoppath), empty input, single partition, split aggregate functions,FILTER clauses, HAVING-style filters, and the config gate. Each test asserts a code-gen'd
SortAggregateExecin the plan and checks the result matches the interpreted (code-gendisabled) result.
SortAggregateBenchmarkwith results committed for JDK 17/21/25.The generated code of a simple query:
select id , count(*) from t1 group by id:Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code