viirya opened a new pull request, #60468:
URL: https://github.com/apache/spark/pull/60468
### What changes were proposed in this pull request?
The code generated by `HashAggregateExec` (grouping-key path) referenced the
plan node itself, via
`ctx.addReferenceObj("plan", this)`, to call a few runtime helpers
(`createHashMap`,
`getEmptyAggregationBuffer`, `getTaskContext`, `addFastHashMapCloseHook`,
`finishAggregate`).
This PR moves those helpers into a new `HashAggregateCodegenHelper`, which
holds only what they
need: the grouping key and buffer schemas, the initial values and merge
expressions of the
declarative aggregates, and the merge input attributes. The generated code
now references this
helper instead of the plan node. Metrics are not held by the helper; as
before, the generated code
passes the plan's registered `SQLMetric`s to `finishAggregate`. The unused
`HashAggregateExec.createUnsafeJoiner()` is removed.
### Why are the changes needed?
The references of the generated code are serialized into every task binary
and deserialized by
every task. A reference to the plan node carries its whole child subtree,
and for a final
aggregate the whole map-side plan through the exchange, although none of the
helpers reads the
child. On plans with many expressions below the aggregate this dominates
task deserialization.
Measured in local mode (local[8], 64 map + 200 reduce tasks, AQE off),
per-task task binary
deserialization:
| Query | before | after |
|---|---|---|
| `range` + group by `id % 100` + `sum` | 442 us, 41 KB | 298 us, 32 KB |
| 150 CASE columns + filter + 40 `sum`s | 1,809 us, 258 KB | 597 us, 152 KB |
| sort-merge join + aggregate | 696 us, 58 KB | 442 us, 50 KB |
For the wide query, end-to-end time over 60 alternating runs: median 542 ms
-> 495 ms (-8.7%).
Executors in cluster mode deserialize the same task binary per task, so the
saving applies there
as well.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Added a test in `WholeStageCodegenSuite` that serializes the code references
of both the partial
and the final aggregate stage, with the single-level map, the row-based and
vectorized fast hash
maps, and adaptive partial aggregation, and asserts that no `SparkPlan` is
written. It fails
without this change.
Ran the aggregate suites (`DataFrameAggregateSuite`, `SQLMetricsSuite`,
`WholeStageCodegenSuite`,
`AggregatingAccumulatorSuite`, `AdaptivePartialAggregationSuite`,
`HashAggregateExecSuite`, the
`*AggregateHashMapSuite`s, and others) and
`HashAggregationQueryWithControlledFallbackSuite`, which
covers the sort-based fallback in `finishAggregate`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 5.5)
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]