dongjoon-hyun opened a new pull request, #60254:
URL: https://github.com/apache/spark/pull/60254
### 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.
### 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, with an `Aggregate`:
```sql
SELECT c0, count(*)
FROM (SELECT id + 0 AS c0, id + 1 AS c1, ..., id + 9999 AS c9999 FROM
range(10)
UNION ALL
SELECT id + 0 AS d0, id + 1 AS d1, ..., id + 9999 AS d9999 FROM
range(10))
GROUP BY c0
```
A deterministic `Project` right above a `Union`, as in `SELECT c0 FROM (...
UNION ALL ...)`, is usually pushed into the children by
`PushProjectionThroughUnion` before `ColumnPruning` runs. A non-deterministic
one reaches this case too.
Local measurements on an Apple M3 Max with JDK 21. The whole Catalyst
optimizer (`SimpleTestOptimizer`) on the query above with `N` columns in each
child:
| Columns (N) | Total before | Total after | `ColumnPruning` before |
`ColumnPruning` after |
|---:|---:|---:|---:|---:|
| 1,000 | 57 ms | 17 ms | 40 ms | 0.8 ms |
| 2,000 | 193 ms | 22 ms | 172 ms | 1.3 ms |
| 5,000 | 1.38 s | 33 ms | 1.35 s | 1.8 ms |
| 10,000 | 3.88 s | 56 ms | 3.83 s | 2.8 ms |
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 |
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 numbers above come from a local benchmark, 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 the same, including expression IDs, nullability, qualifiers and
metadata.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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]