viirya opened a new pull request, #60475:
URL: https://github.com/apache/spark/pull/60475
### What changes were proposed in this pull request?
The code generated by `SortExec`, `SortMergeJoinExec` and
`SortMergeAsOfJoinExec` referenced the
plan node itself, via `ctx.addReferenceObj("plan", this)`: `SortExec` to
call `createSorter()`,
and the joins to call `cleanupResources()`, which releases the sorters of
the `SortExec`s below
them once the join is done.
This PR moves the sorter of `SortExec` into a new `SortExecSorterHolder`,
which holds only what
the sorter needs (the sort order, the output attributes, and the radix sort
and test spill
settings) and the sorter itself. `SortExec` keeps one holder, shared by the
node and its
generated code:
- The generated code of `SortExec` references the holder instead of the node.
- The generated code of the two joins references the holders of the
`SortExec`s below them in
the same stage (not beyond an exchange) and closes their sorters, which is
what
`cleanupResources()` did for them.
- `SortExec.createSorter()` and `SortExec.cleanupResources()` delegate to
the holder, so the
interpreted paths (e.g. the interpreted sort merge join, which calls
`cleanupResources()` on
its children) keep working.
The holder is serialized with the task like the node was, so within a task
the join, the
`SortExec` node and the generated code still see the same sorter, and each
task still gets its
own copy.
### 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 a plan node carries its whole child subtree,
through the exchange
below it, although only the sorter is used. This is the same issue as
SPARK-61661 for
`HashAggregateExec`.
Measured in local mode (AQE off, 200 shuffle partitions): size and
deserialization time of the
task binary of the affected stage, deserialized 1000 times on one thread:
| Stage | before | after |
|---|---|---|
| sort merge join + partial aggregate | 76.5 KB, 558 us | 61.9 KB, 359 us |
| sort merge join | 53.8 KB, 426 us | 42.9 KB, 263 us |
| global sort | 30.2 KB, 283 us | 24.2 KB, 186 us |
(The first row is measured with SPARK-61661 applied, otherwise the
aggregate's own plan
reference carries the join's subtree as well.)
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Added a test in `WholeStageCodegenSuite`. For every codegen stage that runs
a sort, a sort merge
join or a sort merge as-of join, it serializes the RDD the stage adds on top
of its input RDDs,
with and without `spark.sql.execution.usePartitionEvaluator`, and asserts
that no `SparkPlan` is
written. It covers a global sort, inner, outer, semi and anti sort merge
joins, and an as-of
join, and checks their answers against whole-stage codegen disabled. It
fails without this
change.
Also ran `JoinSuite` (including the eager cleanup tests of SPARK-21492),
`SortSuite`,
`SQLMetricsSuite`, the join and as-of join suites,
`KeyGroupedPartitioningSuite`,
`AdaptiveQueryExecSuite` and `FileFormatWriterSuite`.
### 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]