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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -274,19 +286,32 @@ trait PredicateHelper extends AliasHelper with Logging {
         case a: Attribute if below.contains(a) => a
       })
     case agg: Aggregate =>
+      val below = failableOutput(agg.child)
       AttributeSet(agg.aggregateExpressions.flatMap {
         case a: Alias =>
           val result = a.child.transform { case e: AggregateExpression => 
e.resultAttribute }
-          if (result.deterministic && 
!ExprUtils.canEvaluateUnconditionally(result)) {
+          if (result.deterministic && 
(!ExprUtils.canEvaluateUnconditionally(result) ||
+              result.references.exists(below.contains))) {
             Some(a.toAttribute)
           } else {
             None
           }
+        case a: Attribute if below.contains(a) => Some(a)
         case _ => None
       })
     case j: Join => failableOutput(j.left) ++ failableOutput(j.right)
-    case _: Filter | _: Sample | _: LocalLimit | _: GlobalLimit | _: Offset | 
_: Sort =>
+    case u: Union =>
+      val children = u.children.map(c => (c.output, failableOutput(c)))
+      AttributeSet(u.output.zipWithIndex.collect {
+        case (a, i) if children.exists { case (output, failable) =>
+          failable.contains(output(i))

Review Comment:
   Fixed. The Union case now collects the positions that each child reports in 
one pass over its output, without indexing it.
   



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:
##########
@@ -1306,6 +1306,37 @@ class FilterPushdownSuite extends PlanTest {
     }
   }
 
+  test("SPARK-62195: joins: do not push a condition over a column that can 
fail through a " +
+    "union, a window, a watermark, a pushdown operator or an aggregate key 
ahead of a " +
+    "non-deterministic one") {
+    // `q` can fail as the ANSI division does. These operators pass it on, the 
union from its
+    // second child by position, and so does an aggregation that groups by it, 
also as an alias.
+    // Below them, `PushPredicateThroughNonJoin` would push a condition over 
it on through the
+    // projection. So after `rand(0) > 0.1`, `q > 0` stays above the join, 
while `a > 0`, which
+    // can not fail, is pushed.
+    val relation = LocalRelation(attrA, attrB, $"t".timestamp)
+    def project(child: LogicalPlan): LogicalPlan =
+      child.select($"a", $"t", Divide($"a", $"b", 
NumericEvalContext(EvalMode.ANSI)).as("q"))
+    val projected = project(relation)
+    val winExpr = windowExpr(count($"a"), windowSpec($"q" :: Nil, Nil, 
UnspecifiedFrame))
+    val streaming = LocalRelation(Seq(attrA, attrB, $"t".timestamp), Nil, 
isStreaming = true)
+    Seq(
+      relation.select($"a", $"t", Cast($"b", 
DoubleType).as("q")).union(projected),
+      projected.window(winExpr.as("w") :: Nil, $"q" :: Nil, Nil),
+      EventTimeWatermark(UUID.randomUUID(), $"t", new CalendarInterval(0, 0, 
1000L),
+        project(streaming)),
+      projected.distribute($"q")(2),
+      projected.groupBy($"a", $"q")($"a", $"q"),

Review Comment:
   Added: an aggregation that also outputs `max(q)`. A condition over it after 
`rand(0) > 0.1` is still pushed below the join.
   



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