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.
Project invariant arguments once before Expand. Preserve its grouping IDs, batch order and per-mask reservation checks so memory pressure cannot introduce a new overflow boundary. Cover array reuse and checked sums under pressure.
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
Consider a sales report with three grouping sets and
SUM(price * quantity). Comet executes Spark's Expand by producing one batch per set for each input batch. Computing the product in the partial aggregate repeats the multiplication three times. All three sets read the same price and quantity.What changes are included in this PR?
Comet projects eligible arguments once below Expand. Each grouping set reuses the resulting Arrow array. The aggregate still updates all three sets, using Spark's original keys, grouping IDs and batch order.
The batch boundaries matter under memory pressure. An earlier version moved the grouping masks into DataFusion's aggregate. That delayed the reservation check until after all masks, which could change where the aggregate emitted a partial sum. For example, a subtotal containing
-1, then the maximum representable value and+1, fits when accumulated in order. Emitting the first batch's-1separately makes the next partial overflow before the final merge. Keeping Expand preserves the original checks and addresses that regression. Existing sensitivity to partial boundaries in ordinary aggregation is outside this change.The optimization defaults on. Set
spark.comet.exec.aggregate.groupingSets.enabled=falseto disable it. It applies to unfiltered partial COUNT, SUM, AVG, MIN and MAX above compatible Expand projections. A computed argument must be repeatable, use numeric or Boolean expressions, and read the same input columns across projections. If any computed argument reads a key that one grouping set masks, Comet keeps the original plan. Grand totals retain their ordinary Expand behavior.Sharing keeps the computed arrays alive through the last grouping set. Queries that only aggregate plain columns, such as
SUM(price), keep the original plan and should see no speedup from this change. The projection's compute time is attributed to the aggregate. There is no change to the SQL, shuffle schema or number of rows processed by the partial aggregate.How are these changes tested?
I compared dbc5e67b with Apache main at ef771f2b, the common base used in the earlier reproduction. The tests used Spark 4.0.4 on Linux ARM64.
CometAggregateSuitepassed. They cover stored nulls, grand totals and checked sums with a small task memory budget. Filter bygrouping sets share partial aggregate inputsto run both.shares_only_unchanged_arguments_and_keeps_separate_batchespassed. It checks the column mapping, rejects a masked input and verifies that both output batches reference the very same computed Arrow array.Benchmark against Apache main
The table measures this implementation alone. Each case computes
SUM(price * quantity)andCOUNT(*). The regular and nullable random inputs use three grouping sets. The last case adds a grand total to the nullable input, so four grouping sets share the product. In all three cases the native plans show the projection below Expand.Both builds used Java 17, release native libraries with jemalloc, Spark 4.0.4 in
local[8], a 12 GiB driver heap, 4 GiB of Spark off-heap memory, native shuffle and AQE disabled. For each size, two concurrent jobs ran both builds in separate JVMs and reversed the order. Each time below is the mean of the two job medians from SQL planning through collecting all rows. Speedup divides the main time by the branch time. The last two columns give that ratio within each job.The small input used 16 partitions for generation and shuffle, one warmup and five timed collections per build, job and case. The large input used 64 partitions, two warmups and seven timed collections. Each also had an untimed plan check. All 144 timed collections and all 36 warmups and all 24 plan checks matched the schema and every result row from the same SQL with Comet disabled. Result digests also matched across builds and jobs. Input generation and result verification were outside the timer.
The branch was faster in both orders for every case. At 256 million rows it saved 0.287–0.469 seconds per collection. These are single-host measurements of queries with a repeated decimal product. The savings depend on the cost of the expression and the number of grouping sets that reuse it.
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 checks the Spark plan and prints the native plan for inspection.AI assistance
OpenAI Codex assisted with the implementation, tests, benchmarks and write-up.