dongjoon-hyun opened a new pull request, #60255:
URL: https://github.com/apache/spark/pull/60255

   ### What changes were proposed in this pull request?
   
   This PR makes the `Expand` case of `ColumnPruning` look up the columns to 
keep in an `AttributeSet` instead of a `Seq`.
   
   ```scala
   // Before
   val newOutput = e.output.filter(a.references.contains(_))
   val newProjects = e.projections.map { proj =>
     proj.zip(e.output).filter { case (_, a) =>
       newOutput.contains(a)
     }.map(_._1)
   }
   ```
   
   ```scala
   // After
   val newOutput = e.output.filter(a.references.contains(_))
   val newOutputSet = AttributeSet(newOutput)
   val newProjects = e.projections.map { proj =>
     proj.zip(e.output).filter { case (_, a) =>
       newOutputSet.contains(a)
     }.map(_._1)
   }
   ```
   
   The pruned plans stay the same. `newOutput` is the part of `e.output` whose 
expression IDs the parent `Project` references. For an attribute of `e.output`, 
`newOutput.contains` is true exactly when its expression ID is referenced: 
`newOutput` then contains the attribute itself, and otherwise no attribute in 
`newOutput` can be equal to it, because `AttributeReference.equals` also 
compares expression IDs. `AttributeSet.contains` checks the same condition.
   
   ### Why are the changes needed?
   
   For an `Expand` with `P` projections and `N` output columns, of which the 
parent keeps `K`, this case does `P * N` lookups in the `Seq` of the `K` kept 
columns. That is about `P * K * (N - K / 2)` comparisons, quadratic in the 
number of columns.
   
   This case runs for most queries with `GROUPING SETS`, `CUBE` or `ROLLUP`. 
Their `Expand` outputs every column of the input of the `Aggregate` in addition 
to the grouping columns, and the `Aggregate` usually does not use all of them, 
such as the original grouping columns. `ColumnPruning` puts a `Project` between 
the `Aggregate` and the `Expand`, and prunes the `Expand` in the same pass. 
Sliding time windows, `UNPIVOT` and inline tables with non-foldable expressions 
also produce an `Expand` that reaches this case when some of its columns are 
not used.
   
   For example, with a table `t` that has the grouping columns `g0`, `g1` and 
`g2` and `N` integer columns `c0`, ..., `c{N-1}`:
   
   ```sql
   SELECT g0, g1, g2, sum(c0), sum(c1), ..., sum(c{N-1})
   FROM t
   GROUP BY CUBE(g0, g1, g2)
   ```
   
   Local measurements on an Apple M3 Max with JDK 21. The whole Catalyst 
optimizer (`SimpleTestOptimizer`, with the plan checks that only run in tests 
turned off) on the query above:
   
   | Columns (N) | Total before | Total after | `ColumnPruning` before | 
`ColumnPruning` after |
   |---:|---:|---:|---:|---:|
   | 1,000 | 40 ms | 24 ms | 20 ms | 3.3 ms |
   | 2,000 | 96 ms | 38 ms | 63 ms | 5.9 ms |
   | 5,000 | 463 ms | 93 ms | 383 ms | 16 ms |
   | 10,000 | 1.76 s | 186 ms | 1.59 s | 30 ms |
   
   With `CUBE` over five columns (32 grouping sets) and `N = 5,000`, the 
optimizer took 1.63 s before (1.44 s in `ColumnPruning`) and 212 ms after (39 
ms). `ROLLUP`, sliding time windows and `UNPIVOT` showed the same pattern.
   
   This is not a regression. The case has used a `Seq` lookup since Spark 2.0 
(SPARK-13351).
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The optimized plans are the same. The optimizer only spends less time on 
a wide `Expand`.
   
   ### How was this patch tested?
   
   Pass the CIs with the existing tests, such as `Column pruning for Expand` 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 22 plans, 
after one `ColumnPruning` application, after the `ColumnPruningSuite` optimizer 
and after the whole optimizer. The plans cover `CUBE`, `ROLLUP`, `GROUPING 
SETS` with duplicate sets, `count(DISTINCT ...)`, sliding time windows, 
`UNPIVOT`, inline tables with `rand()`, no referenced column, and an `Expand` 
output with the same expression ID under a different qualifier, nullability and 
metadata. 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]

Reply via email to