n-young commented on code in PR #58867:
URL: https://github.com/apache/spark/pull/58867#discussion_r4526002087


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/NondeterministicExpressionCollection.scala:
##########
@@ -46,4 +44,64 @@ object NondeterministicExpressionCollection {
 
     nonDeterministicToAttributes
   }
+
+  /** Expressions extracted by the original collector, including inside 
conditional branches. */
+  private def isLegacyNondeterministic(expression: Expression): Boolean = 
expression match {
+    case _: Nondeterministic | _: UserDefinedExpression | _: 
ExternalUserDefinedFunction => true
+    case _ => false
+  }
+
+  /**
+   * Returns a copy used only to classify the parent, plus the minimal 
nondeterministic
+   * evaluation frontier below `expression`.
+   *
+   * A selected expression is represented by a deterministic attribute while 
walking upward. If
+   * its parent is still nondeterministic after that substitution, the parent 
is the evaluation
+   * unit and replaces the selected descendants in the frontier.
+   *
+   * New units must retain their conditional evaluation and lambda scope. 
Predicate subqueries
+   * stay in their original operator so analysis can validate their placement.
+   */
+  private def collectNondeterministicFrontier(
+      expression: Expression): (Expression, Seq[Expression]) = {
+    if (expression.deterministic || expression.isInstanceOf[ListQuery] ||
+        expression.isInstanceOf[Exists]) {
+      (expression, Seq.empty)
+    } else {
+      val childResults = 
expression.children.map(collectNondeterministicFrontier)
+      val childrenWithResults = expression.children.zip(childResults)
+      val retainEvaluationContext = expression match {
+        case h: HigherOrderFunction =>
+          childrenWithResults.exists { case (child, (_, frontier)) =>
+            h.functions.contains(child) && frontier.nonEmpty
+          }
+        case c: ConditionalExpression =>
+          childrenWithResults.exists { case (child, (_, frontier)) =>
+            !c.alwaysEvaluatedInputs.contains(child) &&
+              frontier.exists(e => !isLegacyNondeterministic(e))
+          }
+        case _: And | _: Or =>
+          childResults(1)._2.exists(e => !isLegacyNondeterministic(e))
+        case _ => false
+      }
+      val expressionWithDeterministicChildren =
+        expression.withNewChildren(childResults.map(_._1))
+
+      // Subquery plans propagate pattern bits, but only predicates in this 
operator constrain
+      // extraction. A scalar subquery may contain valid IN/EXISTS predicates 
in its own plan.
+      val containsPredicateSubquery =
+        expression.containsAnyPattern(LIST_SUBQUERY, EXISTS_SUBQUERY) && 
expression.exists {
+          case _: ListQuery | _: Exists => true
+          case _ => false
+        }
+      if (!containsPredicateSubquery &&

Review Comment:
   Fixed in 1bfe3d95c31e: Aggregate can extract a wrapper or its retained 
conditional/lambda context together with deterministic IN/EXISTS. Sort and 
repartitioning retain the exclusion; ListQuery/Exists themselves are never 
units, and nondeterministic predicate-subquery plans retain their previous 
errors. Added grouping plan/execution tests for UDF/AES wrappers, 
CASE/IF/COALESCE, AND/OR and higher-order functions, plus unary placement 
checks. This also addresses the guarded GROUP BY finding in the review summary. 
All 224 focused tests pass: 
https://github.com/n-young/spark/actions/runs/42830637398.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/NondeterministicExpressionCollection.scala:
##########
@@ -46,4 +44,64 @@ object NondeterministicExpressionCollection {
 
     nonDeterministicToAttributes
   }
+
+  /** Expressions extracted by the original collector, including inside 
conditional branches. */
+  private def isLegacyNondeterministic(expression: Expression): Boolean = 
expression match {
+    case _: Nondeterministic | _: UserDefinedExpression | _: 
ExternalUserDefinedFunction => true
+    case _ => false
+  }
+
+  /**
+   * Returns a copy used only to classify the parent, plus the minimal 
nondeterministic
+   * evaluation frontier below `expression`.
+   *
+   * A selected expression is represented by a deterministic attribute while 
walking upward. If
+   * its parent is still nondeterministic after that substitution, the parent 
is the evaluation
+   * unit and replaces the selected descendants in the frontier.
+   *
+   * New units must retain their conditional evaluation and lambda scope. 
Predicate subqueries
+   * stay in their original operator so analysis can validate their placement.
+   */
+  private def collectNondeterministicFrontier(
+      expression: Expression): (Expression, Seq[Expression]) = {
+    if (expression.deterministic || expression.isInstanceOf[ListQuery] ||
+        expression.isInstanceOf[Exists]) {
+      (expression, Seq.empty)
+    } else {
+      val childResults = 
expression.children.map(collectNondeterministicFrontier)
+      val childrenWithResults = expression.children.zip(childResults)
+      val retainEvaluationContext = expression match {

Review Comment:
   Fixed in 1bfe3d95c31e: TryEval moves with a new evaluation unit, keeping AES 
inside the try block. Added GROUP BY/ORDER BY/DISTRIBUTE BY coverage for 
try_aes_decrypt and try_to_binary, including execution with whole-stage codegen 
enabled and disabled. Legacy rand() and nondeterministic UDF extraction remains 
unchanged and has plan checks. All 224 focused tests pass: 
https://github.com/n-young/spark/actions/runs/42830637398.



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