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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala:
##########
@@ -412,6 +416,26 @@ case class CurrentUser()
   final override val nodePatterns: Seq[TreePattern] = Seq(CURRENT_LIKE)
 }
 
+private object AesEncryptDeterminism {
+  def apply(arguments: Seq[Expression]): Boolean = {
+    arguments.forall(_.deterministic) &&
+      (hasNonEmptyLiteral(arguments(4)) || isEcbLiteral(arguments(2)))

Review Comment:
   Fixed-IV classification now evaluates deterministic foldable expressions 
after ReplaceExpressions, including unhex, to_binary, encode and 
session-variable IVs. Analyzer/SQL tests cover GCM and CBC joins, DEFAULT 
insertion and generated-column writes; CI is running.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala:
##########
@@ -471,12 +495,15 @@ case class AesEncrypt(
     aad: Expression)
   extends RuntimeReplaceable with ImplicitCastInputTypes {
 
+  override lazy val deterministic: Boolean = AesEncryptDeterminism(children)

Review Comment:
   The collector now selects an enclosing expression when it remains 
nondeterministic after selected descendants are treated as deterministic. 
Random-IV AES is extracted once for GROUP BY, ORDER BY and repartitioning, 
including mixed AES/UUID inputs. Added plan-shape and execution coverage.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala:
##########
@@ -117,6 +120,36 @@ class MiscExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     assert(outputEval.contains(s"Result of $inputExpr is 1"))
   }
 
+  test("DynamicPruningExpression is not foldable") {
+    assert(!DynamicPruningExpression(Literal.TrueLiteral).foldable)
+  }
+
+  test("AesEncrypt determinism reflects whether it generates an IV") {
+    val randomIvExpression = new AesEncrypt(Literal("Spark"), 
Literal("0000111122223333"))
+    assert(!randomIvExpression.deterministic)
+    assert(!randomIvExpression.replacement.deterministic)
+    assert(!randomIvExpression.replacement.foldable)
+
+    val explicitIvExpression = new AesEncrypt(
+      Literal("Spark"),
+      Literal("0000111122223333"),
+      Literal("GCM"),
+      Literal("DEFAULT"),
+      Literal(Array.fill[Byte](12)(0)))

Review Comment:
   Expanded coverage beyond direct literals: analyzer-resolved fixed-IV 
wrappers and computed ECB mode, joins, defaults, generated columns and session 
variables; nondeterministic arguments in every AES argument position; and 
single extraction through Aggregate, Sort and repartitioning. Added execution 
checks for per-row IV generation, conditional guards and lambda scope.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/NondeterministicExpressionCollection.scala:
##########
@@ -33,8 +33,18 @@ object NondeterministicExpressionCollection {
           case udf: UserDefinedExpression if !udf.deterministic => udf
           case udf: ExternalUserDefinedFunction if !udf.deterministic => udf
         }
+        val expressionsToCollect = if (leafNondeterministic.nonEmpty) {

Review Comment:
   The bottom-up collector now handles legacy leaves and intrinsically 
nondeterministic wrappers together. For aes_encrypt(uuid(), key), it selects 
the enclosing AES computation rather than a separate UUID alias. Aggregate and 
Sort regressions assert one AES and one UUID with no remaining nondeterministic 
operator expression.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/RewriteWithExpressionSuite.scala:
##########
@@ -127,20 +127,6 @@ class RewriteWithExpressionSuite extends PlanTest {
     assert(rewritten == (Literal(1) + Literal(1)) * (Literal(1) + Literal(1)))
   }
 
-  test("applyForExpression rejects an impure foldable definition referenced 
more than once") {

Review Comment:
   Retained the purpose-built ImpureFoldable fixture and its multiply 
referenced definition test. Updated both stale AES examples in 
RewriteWithExpression to describe impure foldable invocations and side effects 
instead.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala:
##########
@@ -412,6 +418,38 @@ case class CurrentUser()
   final override val nodePatterns: Seq[TreePattern] = Seq(CURRENT_LIKE)
 }
 
+private object AesEncryptDeterminism {
+  def apply(arguments: Seq[Expression]): Boolean = {
+    arguments.forall(_.deterministic) &&
+      (hasNonEmptyFixedValue(arguments(4)) || isEcbFixedValue(arguments(2)))
+  }
+
+  private def fixedValue(expression: Expression): Option[Any] = {
+    if (expression.resolved && expression.foldable && expression.deterministic 
&&

Review Comment:
   Updated fixedValue to apply ReplaceExpressions before checking resolved, 
deterministic and foldable and evaluating the constant. This recognizes 
to_binary/encode IVs, session-variable IVs and computed ECB modes while 
retaining conservative handling of null, empty, per-row and unevaluable values. 
Added analyzer and SQL coverage for GCM/CBC joins and DEFAULT insertion, 
session-variable joins, and generated-column creation/writes. CI is running; 
local suite execution is blocked by Maven dependency-host DNS resolution.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/NondeterministicExpressionCollection.scala:
##########
@@ -22,19 +22,42 @@ import java.util.LinkedHashMap
 import org.apache.spark.sql.catalyst.expressions._
 
 object NondeterministicExpressionCollection {
+  /**
+   * 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.
+   */
+  private def collectNondeterministicFrontier(
+      expression: Expression): (Expression, Seq[Expression]) = {
+    if (expression.deterministic) {
+      (expression, Seq.empty)
+    } else {
+      val childResults = 
expression.children.map(collectNondeterministicFrontier)

Review Comment:
   Frontier selection now promotes a new unit in a conditionally evaluated 
input to its enclosing ConditionalExpression, or AND/OR for the right operand. 
This retains the AES guard when the unit is projected below the operator; 
legacy rand/UDF extraction stays unchanged unless another unit promotes the 
conditional. Added GROUP BY/ORDER BY/DISTRIBUTE BY plan tests and execution 
tests with invalid keys/IVs in skipped branches, covering CASE, IF, COALESCE, 
AND and OR.



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