viirya commented on code in PR #60643:
URL: https://github.com/apache/spark/pull/60643#discussion_r4608248329


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -233,12 +234,17 @@ 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
+   * column of its child, and the result columns of an [[Aggregate]] whose 
expression, with each
+   * aggregate function taken as its value, is deterministic and rejected by 
it. A [[Filter]],
+   * [[Sample]], [[LocalLimit]], [[GlobalLimit]], [[Offset]] or [[Join]] 
passes such columns of
+   * its children through. A condition over such a column can fail as its 
expression does: under
+   * whole-stage codegen, the projection or aggregation computes a 
deterministic column for a row

Review Comment:
   This holds for a hash aggregation with grouping keys. Without grouping keys, 
`AggregateCodegenSupport` evaluates every result column eagerly 
(`evaluateVariables(resultVars)`), and so do `ObjectHashAggregateExec` and a 
`SortAggregateExec` that does not use codegen. Excluding those columns is only 
conservative, but the comment states it as a general rule. Could you either 
narrow the wording or match `agg.groupingExpressions.nonEmpty`?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -233,12 +234,17 @@ trait PredicateHelper extends AliasHelper with Logging {
   /**
    * The output columns of `plan` that can fail to compute: the deterministic 
columns of a

Review Comment:
   nit: This paragraph now mixes three things: which columns can fail, which 
operators pass them through, and why other operators are not looked through. A 
short list for each would be easier to follow. The "why not looked through" 
table in the PR description could go here in condensed form.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -249,8 +255,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:
   This only helps when there is no exchange below the limit. With more than 
one input partition, `GlobalLimitExec` requires `AllTuples`, so an `Exchange 
SinglePartition` writes `q` for every row that passes the local limit. Then 
`spark.range(0, 10, 1, 4)` with the same `LIMIT 8` query still fails with 
`DIVIDE_BY_ZERO`, whether or not constraint propagation is enabled. Keeping the 
case is fine since it costs one line, but please say in the comment that it 
applies only when the limit's input has a single partition. The new test uses 
`range(0, 10, 1, 1)`, which is the only case that benefits.



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