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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -249,8 +269,20 @@ trait PredicateHelper extends AliasHelper with Logging {
               a.child.references.exists(below.contains)) => a.toAttribute
         case a: Attribute if below.contains(a) => a
       })
-    case Filter(_, child) => failableOutput(child)
-    case s: Sample => failableOutput(s.child)
+    case agg: Aggregate =>
+      AttributeSet(agg.aggregateExpressions.flatMap {
+        case a: Alias =>
+          val result = a.child.transform { case e: AggregateExpression => 
e.resultAttribute }
+          if (result.deterministic && 
!ExprUtils.canEvaluateUnconditionally(result)) {
+            Some(a.toAttribute)
+          } else {
+            None
+          }
+        case _ => None
+      })
+    case j: Join => failableOutput(j.left) ++ failableOutput(j.right)
+    case _: Filter | _: Sample | _: LocalLimit | _: GlobalLimit | _: Offset =>

Review Comment:
   Thanks, I confirmed it: at f61494887ab the query fails with `DIVIDE_BY_ZERO` 
and returns no row with `spark.sql.constraintPropagation.enabled=false`, and 
the executed plan has no sort. Its SQL form fails the same way on 3.5.9, 4.0.5 
and 4.3.0. I added `Sort` to the case, since, as for the limits, it is only 
conservative when the sort stays, and added the query to the `DataFrameSuite` 
test and to the description, and a sort to the plan test.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -231,14 +232,33 @@ trait PredicateHelper extends AliasHelper with Logging {
   }
 
   /**
-   * The output columns of `plan` that can fail to compute: the deterministic 
columns of a
-   * [[Project]] whose expression [[ExprUtils.canEvaluateUnconditionally]] 
rejects or reads such a
-   * column of its child, looking through [[Filter]] and [[Sample]]. A 
condition over such a column
-   * can fail as its expression does: under whole-stage codegen, the 
projection computes a
-   * deterministic column for a row only when a condition above first reads 
it, and
-   * `PushPredicateThroughNonJoin` pushes a condition below the projection 
with the expression
-   * substituted. A non-deterministic column is computed on every row the 
projection gets, so it
-   * does not count. Other operators are not looked through.
+   * The output columns of `plan` that can fail to compute. Under whole-stage 
codegen, a
+   * projection or an aggregation computes a deterministic column for a row 
only when a condition
+   * above first reads it, and `PushPredicateThroughNonJoin` pushes a 
condition below a projection
+   * with the expression substituted. So a condition over such a column can 
fail as its expression
+   * does. The columns are:
+   *  - A deterministic column of a [[Project]] whose expression
+   *    [[ExprUtils.canEvaluateUnconditionally]] rejects or reads such a 
column of its child.
+   *  - A result column of an [[Aggregate]] whose expression, with each 
aggregate function taken
+   *    as its value, is deterministic and rejected by it. Only an aggregation 
with grouping keys
+   *    under whole-stage codegen computes it lazily, so this is conservative 
for the others,
+   *    e.g. a global aggregation or an `ObjectHashAggregateExec`.
+   *
+   * These operators pass such columns of their children through:
+   *  - [[Filter]] and [[Sample]].
+   *  - [[LocalLimit]], [[GlobalLimit]] and [[Offset]]. A global limit or an 
offset passes them
+   *    through only when its input has a single partition; otherwise the 
exchange below it
+   *    computes every column, and this is conservative.
+   *  - [[Join]], from both sides. Only the streamed side of a hash join 
passes them through, so

Review Comment:
   Right. With `SHUFFLE_HASH`, the projection is in its own codegen stage below 
`ShuffledHashJoin`, and the query fails also with constraint propagation 
disabled. I used your wording here and in the description.
   



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/InferFiltersFromConstraintsSuite.scala:
##########
@@ -465,6 +465,27 @@ class InferFiltersFromConstraintsSuite extends PlanTest {
       projected.where(IsNotNull($"k") && $"k" > 1).where(IsNotNull($"r") && 
condition).analyze)
   }
 
+  test("SPARK-61921: a constraint over a column that can fail through a limit, 
an offset, a " +
+    "join or an aggregate is not inferred as a filter") {
+    // `i` can fail as the cast does, also through the limits, the offset and 
the join, and so can
+    // the aggregate result `c`. So no constraint over either, e.g. `i > 0` 
from `i = k AND k > 0`,
+    // goes ahead of the condition, while one over `m`, an aggregate function, 
or over `f`, a
+    // non-deterministic result computed for every group, does.
+    val left = LocalRelation($"s".string, $"k".int)
+      .select($"k", Cast($"s", IntegerType, evalMode = EvalMode.ANSI).as("i"))
+      .limit(5)
+      .offset(1)
+    val right = LocalRelation($"g".int, $"t".string)
+      .groupBy($"g")(max($"g").as("m"),

Review Comment:
   Thanks, done. The test outputs `g` now and checks that `isnotnull(g)` is 
inferred.
   



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