This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new eac424581c67 [SPARK-61277][SQL] Avoid quadratic allocations in
`ColumnPruning` when pruning the children of a `Union`
eac424581c67 is described below
commit eac424581c671eecd5ae563798cc69244b1499b0
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Mon Nov 9 21:09:30 2026 -0800
[SPARK-61277][SQL] Avoid quadratic allocations in `ColumnPruning` when
pruning the children of a `Union`
### What changes were proposed in this pull request?
This PR makes the `Union` case of `ColumnPruning` compute the output of the
first child and the set of attributes to keep once, instead of once per column
of each child.
```scala
// Before
val newOutput = prunedChild(firstChild, p.references).output
// pruning the columns of all children based on the pruned first child.
val newChildren = u.children.map { p =>
val selected = p.output.zipWithIndex.filter { case (a, i) =>
newOutput.contains(firstChild.output(i))
}.map(_._1)
```
```scala
// After
val firstChildOutput = firstChild.output.toIndexedSeq
val newOutputSet = AttributeSet(prunedChild(firstChild,
p.references).output)
// pruning the columns of all children based on the pruned first child.
val newChildren = u.children.map { p =>
val selected = p.output.zipWithIndex.filter { case (a, i) =>
newOutputSet.contains(firstChildOutput(i))
}.map(_._1)
```
The pruned plans stay the same. `newOutput` is the part of the first
child's output whose expression IDs are in `p.references`. Since
`AttributeReference.equals` also compares expression IDs,
`newOutput.contains(firstChild.output(i))` is true exactly when the expression
ID of `firstChild.output(i)` is in `p.references`. `AttributeSet.contains`
checks the same condition.
This PR also regenerates the `WideSchemaBenchmark` results. In the new
results, `optimize large union` with 10,000 columns takes 250 ms instead of
11,052 ms on JDK 21 (44x faster) and 197 ms instead of 11,812 ms on JDK 25 (60x
faster).
### Why are the changes needed?
For a `Union` of `C` children with `N` columns each, this case evaluates
`firstChild.output` `C * N` times, once per column of each child.
- `output` is not cached for many operators. `Project.output` is
`projectList.map(_.toAttribute)`, and `Alias.toAttribute` creates a new
`AttributeReference` on every call. So when the first child is a `Project` with
aliases, every evaluation creates `N` attributes and an `N`-element list, `C *
N^2` of each in total. `Aggregate`, `Window` and outer `Join` also build their
output on every call.
- `firstChild.output(i)` takes linear time when the output is a `List`.
- `newOutput.contains` is a linear search over the kept columns.
This case runs when a parent of a `Union` needs only some of its columns.
`ColumnPruning` puts a `Project` between them and prunes the children of the
`Union` in the same pass. For example, the `optimize large union` case of
`WideSchemaBenchmark` (SPARK-61281) optimizes this query with `N` = 100, 1,000
and 10,000:
```sql
SELECT a_1, count(*)
FROM (SELECT id + 1 AS a_1, id + 2 AS a_2, ..., id + N AS a_N FROM range(1)
UNION ALL
SELECT id + 1 AS b_1, id + 2 AS b_2, ..., id + N AS b_N FROM range(1))
GROUP BY a_1
```
A deterministic `Project` right above a `Union`, as in `SELECT a_1 FROM
(... UNION ALL ...)`, is usually pushed into the children by
`PushProjectionThroughUnion` before `ColumnPruning` runs. A non-deterministic
one reaches this case too.
The Best Time of `optimize large union` in the `WideSchemaBenchmark`
results, before (`master`) and after this PR. All results were generated by
GitHub Actions on AMD EPYC 7763.
| Columns (N) | JDK 21 before | JDK 21 after | JDK 25 before | JDK 25 after
|
|---:|---:|---:|---:|---:|
| 100 | 5 ms | 4 ms | 5 ms | 4 ms |
| 1,000 | 118 ms | 21 ms | 112 ms | 19 ms |
| 10,000 | 11,052 ms | 250 ms | 11,812 ms | 197 ms |
`optimize large cube` is also faster in the new results (10,000 columns:
8,384 ms to 4,458 ms on JDK 21) because SPARK-61295 was merged after the
previous results were generated. This PR does not change that case. The other
cases show only run-to-run variance.
The time and allocations of one `ColumnPruning` application on `Project(<1
column>, Union(Project(<N aliases>, t1), t2))`:
| Columns (N) | Time before | Time after | Allocated before | Allocated
after |
|---:|---:|---:|---:|---:|
| 1,000 | 33 ms | 1.4 ms | 245 MiB | 1.3 MiB |
| 2,000 | 119 ms | 0.8 ms | 979 MiB | 2.5 MiB |
| 5,000 | 709 ms | 1.4 ms | 6.0 GiB | 6.2 MiB |
| 10,000 | 2.83 s | 2.3 ms | 23.9 GiB | 12.4 MiB |
The allocations are the bytes that the calling thread allocated during the
call, measured with `ThreadMXBean.getCurrentThreadAllocatedBytes`. They are a
total over the call, mostly short-lived objects, not the peak heap usage.
This is not a regression. The case has evaluated `firstChild.output` for
each column since Spark 2.0 (SPARK-13376).
### Does this PR introduce _any_ user-facing change?
No. The optimized plans are the same. The optimizer only spends less time
and memory on a wide `Union`.
### How was this patch tested?
Pass the CIs with the existing tests, such as `Column pruning on Union` in
`ColumnPruningSuite`. All suites under
`org.apache.spark.sql.catalyst.optimizer` also pass locally.
```
build/sbt 'catalyst/testOnly org.apache.spark.sql.catalyst.optimizer.*'
```
The `WideSchemaBenchmark` results were generated by GitHub Actions with JDK
21 and 25.
The single-application numbers come from a local harness, which is not part
of this PR. The same harness also compared the plans before and after this PR
for 15 plans, both after one `ColumnPruning` application and after the
`ColumnPruningSuite` optimizer. The plans cover a first child with aliases,
alias metadata, duplicate expression IDs, an `Aggregate`, an outer `Join` or a
nested `Union`, three children, no referenced column, and an `Aggregate` above
the `Union`. All of them were [...]
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60254 from dongjoon-hyun/SPARK-61277.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit e540d564a219d5e5f49ce383027fa52f7f206131)
Signed-off-by: Dongjoon Hyun <[email protected]>
skip-checks: true
---
.../scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
index dc209aa5dbcb..a661fa487f6c 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
@@ -1560,11 +1560,14 @@ object ColumnPruning extends Rule[LogicalPlan] {
case p @ Project(_, u: Union) =>
if (!u.outputSet.subsetOf(p.references)) {
val firstChild = u.children.head
- val newOutput = prunedChild(firstChild, p.references).output
+ // Compute these once, not per column of each child: `output` may
create new attributes
+ // on each call (e.g. a `Project` with aliases), and a `Seq` lookup is
linear.
+ val firstChildOutput = firstChild.output.toIndexedSeq
+ val newOutputSet = AttributeSet(prunedChild(firstChild,
p.references).output)
// pruning the columns of all children based on the pruned first child.
val newChildren = u.children.map { p =>
val selected = p.output.zipWithIndex.filter { case (a, i) =>
- newOutput.contains(firstChild.output(i))
+ newOutputSet.contains(firstChildOutput(i))
}.map(_._1)
Project(selected, p)
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]