cloud-fan commented on code in PR #59655:
URL: https://github.com/apache/spark/pull/59655#discussion_r4624217198


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -1869,11 +1872,14 @@ object CollapseProject extends Rule[LogicalPlan] with 
AliasHelper {
     // We can only collapse expressions if all input expressions meet the 
following criteria:
     // - The input is deterministic.
     // - The input is only consumed once OR the underlying input expression is 
cheap.
+    // References are counted by expression ID, since references to one 
attribute can differ in
+    // qualifier.
     consumers
       .filter(_.references.exists(producerMap.contains))
       .flatMap(collectReferences)
-      .groupBy(identity)
-      .view.mapValues(v => v.size)
+      .groupBy(_.exprId)

Review Comment:
   **Non-blocking (P2):** Counting by expression ID here also changes scan 
planning. `OperationHelper.collectProjectsAndFilters` (patterns.scala:77-78) 
uses this check to decide whether a Filter over a Project can be folded into 
`PhysicalOperation` or `ScanOperation`, and those patterns are where 
`PruneFileSourcePartitions`, `FileSourceStrategy` and the Hive scan rules get 
their partition filters.
   
   An example, traced by reading the code rather than run: with `part_tbl` 
partitioned by `dt` and a deterministic Scala UDF `myUdf`,
   
   ```scala
   val df2 = spark.table("part_tbl").select($"dt", myUdf($"dt").as("y"))
   df2.as("t").filter($"t.y" === 2024 || df2("y") === 2023)
   ```
   
   the condition references `y` once as `t.y` and once unqualified. 
`PushPredicateThroughNonJoin` keeps this Filter above the Project because `y` 
is expensive (`spark.sql.optimizer.avoidDoubleFilterEval` defaults to true). On 
the base branch the two references form two groups of one, so the Filter is 
folded into the scan as `myUdf(dt) = 2024 OR myUdf(dt) = 2023`, prunes 
partitions, and `FileSourceStrategy` drops it from the per-row filters. With 
this change they count as 2, `myUdf` is not cheap, the Filter is not folded, 
and no partition filter reaches the relation. The scan then reads every 
partition, the UDF runs on every row, and the relation keeps full-table 
statistics, which can also turn a broadcast join into a shuffle join.
   
   Only filters on an expensive deterministic alias computed from partition 
columns (such as a Scala UDF, an RLIKE match on a column, or `array_join`) that 
reference it under two qualifiers are affected, over v1 file-source or Hive 
tables. The same query written with one qualifier already loses pruning on the 
base branch, and filters of this shape on data columns gain from the change, 
because the expression is no longer evaluated again for each reference. The PR 
description does not mention this trade-off.
   
   **Recommended change:** Keep counting references by expression ID, and state 
the scan-planning trade-off where the reviewer and later readers can see it. In 
the PR description, explain that canCollapseExpressions is shared with the 
scan-planning patterns. As a result, a Filter that stays above a Project 
because it references an expensive alias of partition columns (for example a 
Scala UDF over the partition column) under two qualifiers is no longer pushed 
into the scan, and its partition-only predicates no longer prune partitions. 
The base branch got that pruning only because it counted the two qualifiers 
separately, and the same query written with one qualifier already loses it 
there. Name the alternative, which is to let the partition-pruning rules take a 
Filter's partition-only predicates through such an alias, and say why this PR 
leaves it out. Then add one sentence to the comment on the reference count in 
canCollapseExpressions (Optimizer.scala:1875-1876) saying that scan pla
 nning shares this count, so such a Filter stays above its Project.
   
   **Why this works:** Once the author states the regression together with the 
alternative it rejects and the reason, the lost pruning becomes a stated 
trade-off that the reviewer can weigh, not an unannounced change. The comment 
keeps the shared count's effect on scan planning visible to anyone who later 
reads or changes the counting code.
   
   **Compatibility:** Counting by expression ID in mergeProjectExpressions and 
canCollapseExpressions, and the plans that the new CollapseProject tests pin, 
stay unchanged.
   
   **Risks:** Queries whose Filter references an expensive alias of partition 
columns under two qualifiers keep reading every partition until partition 
pruning learns to look through such aliases. Setting 
spark.sql.optimizer.collapseProjectAlwaysInline to true restores the folding, 
but it also inlines expensive expressions everywhere. The reviewer may still 
prefer removing the regression, since its cost grows with the number of 
partitions read.
   
   **Constraints:** Counting by expression ID stays at both counting sites; 
documenting the trade-off must not narrow or revert it.
   
   **Exclusions:**
   - Teaching the partition-pruning rules and scan strategies to take 
partition-only predicates through an expensive alias, for both the 
two-qualifier and one-qualifier forms, is left to a follow-up.
   
   **Verification:**
   - **Inspection:** Confirm that the PR description and the comment on the 
reference count in canCollapseExpressions both describe the scan-planning 
effect: a Filter that references an expensive alias of partition columns under 
two qualifiers stays above its Project and does not reach partition pruning. 
Also confirm that the description names the rejected alternative and gives the 
reason, and that the counting code itself is unchanged.



-- 
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