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 8ce5c3a8ff9b [SPARK-61278][SQL] Avoid quadratic time in 
`ColumnPruning` when pruning the columns of an `Expand`
8ce5c3a8ff9b is described below

commit 8ce5c3a8ff9bcba6ac933c67fb5a5795c3c61215
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Tue Nov 10 07:31:14 2026 -0800

    [SPARK-61278][SQL] Avoid quadratic time in `ColumnPruning` when pruning the 
columns of an `Expand`
    
    ### 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.
    
    This PR also regenerates the `WideSchemaBenchmark` results. In the new 
results, `optimize large cube` with 10,000 columns takes 1,280 ms instead of 
4,458 ms on JDK 21 (3.5x faster) and 1,057 ms instead of 4,017 ms on JDK 25 
(3.8x faster).
    
    ### 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 `Exp [...]
    
    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.
    
    The `optimize large cube` case of `WideSchemaBenchmark` (SPARK-61281) runs 
the same kind of query through the DataFrame API, `df.cube("g_1", "g_2", 
"g_3").agg(sum("c_1"), ..., sum("c_N"))`, with `N` = 100, 1,000 and 10,000. Its 
Best Time in the `WideSchemaBenchmark` results, before (`master`) and after 
this PR, all generated by GitHub Actions on AMD EPYC 7763:
    
    | Columns (N) | JDK 21 before | JDK 21 after | JDK 25 before | JDK 25 after 
|
    |---:|---:|---:|---:|---:|
    | 100 | 12 ms | 12 ms | 11 ms | 12 ms |
    | 1,000 | 124 ms | 98 ms | 118 ms | 93 ms |
    | 10,000 | 4,458 ms | 1,280 ms | 4,017 ms | 1,057 ms |
    
    Each iteration of this case also builds and analyzes the DataFrame, so its 
time includes more than the optimizer. No other case of `WideSchemaBenchmark` 
has an `Expand`, and they show only run-to-run variance.
    
    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 `WideSchemaBenchmark` results were generated by GitHub Actions with JDK 
21 and 25.
    
    The `SimpleTestOptimizer` 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 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 t [...]
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    Closes #60255 from dongjoon-hyun/SPARK-61278.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    (cherry picked from commit 110e1ddaeee3259675770a055b38c72215621e9b)
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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 a661fa487f6c..97cb0f2e97ef 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
@@ -1509,9 +1509,10 @@ object ColumnPruning extends Rule[LogicalPlan] {
         child = a.copy(aggregateExpressions = 
a.aggregateExpressions.filter(p.references.contains)))
     case a @ Project(_, e @ Expand(_, _, grandChild)) if 
!e.outputSet.subsetOf(a.references) =>
       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) =>
-          newOutput.contains(a)
+          newOutputSet.contains(a)
         }.map(_._1)
       }
       a.copy(child = Expand(newProjects, newOutput, grandChild))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to