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


##########
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))
+        } => a
+      })
+    case _: Filter | _: Sample | _: LocalLimit | _: GlobalLimit | _: Offset | 
_: Sort |
+         _: Window | _: EventTimeWatermark =>

Review Comment:
   Thank you. I confirmed it with a probe. On the base branch, `a.k = b.k` 
written after `rand()` became the key of a broadcast hash join when `a` was an 
aggregation grouped by `CAST(s AS INT)`, a window, a repartition or a union 
over it, and with the previous commit it fell back to a broadcast nested loop 
join. A stream-stream join whose key is cast below `withWatermark` also failed 
to plan.
   
   I applied the recommended split. `failableOutput` now takes `atJoin`, which 
reports no column through a window, a watermark, a repartition, a rebalance or 
a grouping key, and `splitMovable` uses it for a conjunct over more than one of 
its inputs. Through those operators the equality goes into the join condition 
again. Through a union, which can pass the column on to the join as it is under 
whole-stage codegen, it stays above the join, as it already does through a 
projection directly below the join. I also added the TODO at the join case of 
`InferFiltersFromConstraints`, and the plan test now checks the equality for 
each side, including a streaming watermark.
   



##########
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))
+        } => a
+      })
+    case _: Filter | _: Sample | _: LocalLimit | _: GlobalLimit | _: Offset | 
_: Sort |
+         _: Window | _: EventTimeWatermark =>

Review Comment:
   Confirmed: the base branch put `ts >= tc` into the stream-stream join 
condition and inferred `isnotnull(ts)` above the watermark, and the previous 
commit did neither. The watermark case no longer reports its event time, and 
the scaladoc says why. The plan test checks that a condition over an event time 
that an ANSI cast computes is pushed into that side after `rand(0) > 0.1`.
   



##########
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))
+        } => a
+      })
+    case _: Filter | _: Sample | _: LocalLimit | _: GlobalLimit | _: Offset | 
_: Sort |
+         _: Window | _: EventTimeWatermark =>
       failableOutput(plan.children.head)
+    case u: UnaryNode if PushPredicateThroughNonJoin.canPushThrough(u) =>

Review Comment:
   Your as-of query fails with `CAST_OVERFLOW` on the base branch too, so the 
gap is not introduced here. My lateral variant, `LATERAL (SELECT a.ts + 1 AS 
v)`, returned no row both before and after this PR, and I did not probe the 
lookup join. These are other operators with their own pushdown paths, so I'll 
file a separate JIRA issue for them.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -255,12 +258,21 @@ trait PredicateHelper extends AliasHelper with Logging {
    *  - [[Join]], from both sides. Only the streamed side of a broadcast hash 
join passes them
    *    through; a build side, a shuffled hash join or a sort-merge join 
computes every column, so
    *    this is conservative for them.
+   *  - [[Union]], by position: an output column is one when the column of 
some child at its
+   *    position is.
+   *  - [[Window]], [[EventTimeWatermark]] and the operators of
+   *    `PushPredicateThroughNonJoin.canPushThrough`, e.g. `Repartition` and 
`Generate`.
+   * A union outside whole-stage codegen, these other operators and an 
aggregation, for a grouping
+   * key, may compute such a column on every row they get, but 
`PushPredicateThroughNonJoin`
+   * pushes a condition over it below them, and on through the projection that 
computes it with
+   * its expression substituted, e.g. into one side of a join there, where it 
is evaluated on the
+   * rows that the join drops.
    *
    * Other columns and operators do not count:
-   *  - A non-deterministic column, computed for every row the operator 
outputs, and the grouping
-   *    keys and aggregate functions of an aggregation, computed on every row 
it gets.
-   *  - The columns through any other operator, e.g. a `Union`, `Generate`, 
`Deduplicate`,
-   *    `Window` or `Repartition`, whose input rows get every column computed.
+   *  - A non-deterministic column, computed for every row the operator 
outputs, and the
+   *    aggregate functions of an aggregation, computed on every row it gets.
+   *  - The columns through any other operator, e.g. a `Deduplicate` or 
`CollectMetrics`, whose
+   *    input rows get every column computed and that no condition is pushed 
through.

Review Comment:
   Confirmed, and it fails on the base branch too. Seeing through a reference 
needs the CTE definitions passed to the helper, so I'll file a separate JIRA 
issue for it.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -255,12 +258,21 @@ trait PredicateHelper extends AliasHelper with Logging {
    *  - [[Join]], from both sides. Only the streamed side of a broadcast hash 
join passes them
    *    through; a build side, a shuffled hash join or a sort-merge join 
computes every column, so
    *    this is conservative for them.
+   *  - [[Union]], by position: an output column is one when the column of 
some child at its
+   *    position is.
+   *  - [[Window]], [[EventTimeWatermark]] and the operators of
+   *    `PushPredicateThroughNonJoin.canPushThrough`, e.g. `Repartition` and 
`Generate`.
+   * A union outside whole-stage codegen, these other operators and an 
aggregation, for a grouping
+   * key, may compute such a column on every row they get, but 
`PushPredicateThroughNonJoin`

Review Comment:
   Fixed. The scaladoc now names `PushDownPredicates` for the path into a join 
side, and says that `ColumnPruning` removes a window whose results are not 
read, which is why its other columns count too.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -255,12 +258,21 @@ trait PredicateHelper extends AliasHelper with Logging {
    *  - [[Join]], from both sides. Only the streamed side of a broadcast hash 
join passes them
    *    through; a build side, a shuffled hash join or a sort-merge join 
computes every column, so
    *    this is conservative for them.
+   *  - [[Union]], by position: an output column is one when the column of 
some child at its
+   *    position is.
+   *  - [[Window]], [[EventTimeWatermark]] and the operators of
+   *    `PushPredicateThroughNonJoin.canPushThrough`, e.g. `Repartition` and 
`Generate`.
+   * A union outside whole-stage codegen, these other operators and an 
aggregation, for a grouping
+   * key, may compute such a column on every row they get, but 
`PushPredicateThroughNonJoin`
+   * pushes a condition over it below them, and on through the projection that 
computes it with
+   * its expression substituted, e.g. into one side of a join there, where it 
is evaluated on the
+   * rows that the join drops.

Review Comment:
   The cases without a join in between are what SPARK-62144 (#60766) changes, 
and the PR description says so. I'd rather not add a TODO in the code, since it 
would be stale as soon as #60766 is merged; whichever of the two PRs lands 
later will be rebased onto the other.
   



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