Repository navigation
Conversation
Use DataFusion grouping masks in eligible partial aggregates and restore the Spark grouping ID before shuffle. Preserve Expand when copies differ in payload, duplicate a mask, include a grand total, or can use partial aggregation bypass.
DataFusion processes every mask before the next memory check. Keep Expand for variable-width keys or accumulator states to avoid overflowing Arrow offsets while collecting multiple copies. Let users turn sharing off independently when high cardinality makes the extra peak memory undesirable.
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?
No separate issue is linked yet. This draft tracks the proposal.
Rationale for this change
A report that groups sales by region and category, by region alone and by category alone has three grouping sets. Spark uses Expand to copy every row three times. A partial
sum(price * quantity)then multiplies the same inputs three times. DataFusion grouping sets evaluate aggregate arguments once per batch and reuse them across the three groups.What changes are included in this PR?
For unfiltered COUNT, SUM, MIN, MAX or AVG, recognize a leading detail projection and copies that differ only by typed nulls in grouping keys and the corresponding ID bits. Keep the detail projection and pass the masks to DataFusion's partial aggregate. Restore Spark's nullable long ID before shuffle. The ID distinguishes stored nulls from subtotal nulls. Every row still updates every requested grouping set.
The rewrite defaults on. Set
spark.comet.exec.aggregate.groupingSets.enabled=falseto disable it independently. Both the grouping keys and the retained aggregate state fields must be fixed width. For example, COUNT over strings retains a counter, while MIN over strings retains a string. Only the former is eligible. Keep the original plan for grand totals, duplicate masks, arguments that depend on masked columns and plans eligible for partial aggregation bypass.DataFusion checks its reservation after updating all masks for the batch. A variable-width key repeated across masks could exceed the Arrow offset limit before that check. Even with fixed-width state and the eight-mask cap, high-cardinality inputs can increase peak memory between checks.
Expose DataFusion's existing reduction factor denominator on grouped partial aggregates so the SQL UI shows how many rows the aggregate processed. The Spark plan still displays the original Expand.
How are these changes tested?
The focused Scala regression and all three native matcher tests passed. They cover mask validation, stored nulls, the independent switch, variable-width keys and state, and COUNT over strings.
I also ran eight masks over two partitions, each with 4,096 distinct 64 KiB keys. Retaining every key in every mask requires 2 GiB of string data for one partition. With 1,200 MiB of Spark off-heap memory, the previous revision failed with
offset overflow, buffer size > 2147483647. Apache main and this revision returned the same 32,768 groups as Spark. The new revision processed 65,536 partial input rows using Expand. With fixed-width keys the counter fell to 8,192 and disabling the switch restored 65,536. The standalone check below compares the schema, each key identifier and length, grouping ID, grouping columns and count.Benchmark against Apache main
I measured the branch at cc25f2ab0 independently. The baseline was Apache Comet main at ef771f2b. Both builds used stock Spark 4.0.4, Java 17.0.21.1, release native builds with jemalloc, native shuffle and AQE disabled. Each Spark application used
local[8]on Linux ARM64 with a 12 GiB driver heap and 4 GiB of Spark off-heap memory.For each input size, two jobs ran concurrently. Each ran both builds in separate JVMs, with the order reversed in the second job. Times include SQL planning through collecting all rows. Each time is the mean of the two job medians. Speedup divides the main time by the branch time. The last two columns show the ratios within each job. A negative saving means the branch took longer. These are single-host operator workloads. Shared host load and JVM variation can affect small differences.
For every measured execution the benchmark compared the schema and all result rows, including duplicates and nulls, with the same SQL on Spark with Comet disabled. All comparisons passed. Result digests also matched across builds and jobs. Verification, input preparation, Spark reference queries and warmups were outside the timer.
The smaller input used 16 data generation partitions and 16 shuffle partitions, one warmup and five measured collections per build, case and job. The larger input used 64 partitions for generation and shuffle, two warmups and seven measured collections. Spark chose the scan task count separately. Both sizes also had an untimed plan check. Spark generated the same deterministic Parquet values separately for each application.
At 256 million input rows the two eligible queries have three masks. Ordinary expansion feeds 768 million rows to the partial aggregate. The branch counter recorded 256 million. It evaluates the decimal product once per input batch and still updates all three grouping sets. The grand total control kept all four projections and fed 1.024 billion rows to the partial aggregate. Both measured key distributions have few groups. The wide-key regression above exercises the fallback separately.
The only subsequent change to the measured commit adjusts the Scala test to inspect both sort and hash aggregates for MIN over strings.
Reproduce the measurements
Use the two Spark 4.0 assembly jars with the matching native libraries packaged for your platform. Set
MAIN_JARandPATCH_JARto their absolute paths. Save the Python below asbench.py. Start each invocation with a clean Spark configuration. Repeat with the order reversed in a second job. The script sets the relevant SQL options and asserts the executed paths.Reproduce the separate correctness check
Use the same spark-submit options as above with
local[2]and replacebench.pywithstress.py. Pass only--arm mainor--arm patchto the script, matching the jar. Setspark.memory.offHeap.size=1200m.AI assistance
OpenAI Codex assisted with the implementation, tests, benchmarks and write-up.