dongjoon-hyun commented on code in PR #60631:
URL: https://github.com/apache/spark/pull/60631#discussion_r4605947033


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2631,10 +2631,14 @@ object CombineFilters extends Rule[LogicalPlan] with 
PredicateHelper {
     // Combining a condition written after a non-deterministic or throwable 
one evaluates it ahead
     // of that one, so it must also be unable to fail. `throwable` does not 
report e.g. a division
     // by zero in ANSI mode, which the condition before it may have kept from 
being evaluated.
+    // Nor may it read a column that can fail (see `failableOutput`).
     case Filter(fc, nf @ Filter(nc, grandChild)) if nc.deterministic =>
       val (leading, following) =
         splitConjunctivePredicates(fc).span(p => p.deterministic && 
!p.throwable)
-      val (cannotFail, rest) = 
following.partition(ExprUtils.canEvaluateUnconditionally)
+      lazy val failable = failableOutput(grandChild)

Review Comment:
   - Operators such as `Limit` are a limitation of the shared `failableOutput` 
itself, as discussed on #60574. Extending it only here would make the two rules 
diverge, so I would rather extend the helper in a separate PR for both callers.
   - On partition pruning: agreed it matches the single-filter case. I would 
rather not add a legacy flag or a migration-guide entry, since SPARK-60980, 
which introduced this ordering, is not released yet, so this corrects behavior 
no release has shipped.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:
##########
@@ -185,6 +185,28 @@ class FilterPushdownSuite extends PlanTest {
     }
   }
 
+  test("SPARK-61886: a condition over a column that can fail is not combined 
ahead of a " +
+    "non-deterministic one") {
+    // `i` can fail as the cast does, and `j` as it reads `i`, also through 
the sample, the
+    // projection and the filter above them. A condition over either stays 
above, while one over
+    // the non-deterministic `r`, which is computed on every row, is combined.
+    val projected = testStringRelation
+      .select($"a", Cast($"e", IntegerType, evalMode = EvalMode.ANSI).as("i"), 
Rand(1).as("r"))
+      .sample(0.0, 0.5, withReplacement = false, 1L)
+      .select($"a", $"i", IsNull($"i").as("j"), $"r")
+    val originalQuery = projected
+      .where($"a" > 0)
+      .where($"a" > 1)
+      .where(Rand(0) > 0.1 && $"i" > 0 && $"j" && $"r" > 0.5)

Review Comment:
   Good point. I added `w` (`Cast(a, LongType)`) carried through the same chain 
and a `w > 0` conjunct after `Rand(0)`; the expected plan combines it 
(2ce79eaeb0a). The test now fails if every deterministic alias is treated as 
failable.



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