rich7420 commented on code in PR #5972:
URL: https://github.com/apache/datafusion-comet/pull/5972#discussion_r4709554242


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1108,6 +1201,77 @@ object QueryPlanSerde extends Logging with CometExprShim 
with CometTypeShim {
       }
   }
 
+  /** Spark 4.2 changed this setting from String to an Enumeration.Value. */
+  private[comet] def usesInterpretedProjection(conf: SQLConf): Boolean =
+    conf.getConf(SQLConf.CODEGEN_FACTORY_MODE).toString == "NO_CODEGEN"
+
+  /** Inputs Spark evaluates whenever it consumes this expression's output. */
+  private[comet] def eagerlyEvaluatedChildren(expr: Expression): 
Seq[Expression] = expr match {
+    case conditional: ConditionalExpression => 
conditional.alwaysEvaluatedInputs
+    // Generated TryEval can catch failures from a deferred child's code.
+    case _: TryEval => Seq.empty
+    case order: SortOrder => Seq(order.child)
+    case unary: UnaryExpression => Seq(unary.child)
+    case _: DateAdd | _: DateSub | _: DateDiff | _: NextDay | _: 
BinaryComparison =>
+      expr.children.take(if (expr.children.head.nullable) 1 else 2)
+    case _: Concat | _: Greatest | _: Least | _: CreateArray => expr.children
+    case _ => Seq.empty
+  }
+
+  /** Reject ANSI NextDay below native parents that can evaluate children 
Spark skips. */
+  private def unsupportedNextDayEvaluation(expr: Expression): Option[String] = 
{
+    var stack: List[(Expression, Option[String])] = (expr, None) :: Nil
+    while (stack.nonEmpty) {
+      val (current, unsafeAncestor) = stack.head
+      stack = stack.tail
+      current match {
+        case nextDay: NextDay
+            if CometNextDay.evaluationMaskName(nextDay).isDefined && 
unsafeAncestor.isDefined =>
+          return Some(
+            s"next_day requires Spark evaluation under ${unsafeAncestor.get}: 
" +
+              "native evaluation may evaluate a masked child.")
+        case _ =>
+      }
+
+      def pushChildren(context: Option[String]): Unit = {
+        current.children.reverseIterator.foreach { child =>
+          stack = (child, context) :: stack
+        }
+      }
+
+      val wholeExpressionDispatch = exprSerdeMap.get(current.getClass).exists 
{ handler =>
+        (handler.isInstanceOf[CodegenDispatchFallback] &&
+          handler
+            .asInstanceOf[CometExpressionSerde[Expression]]
+            .getSupportLevel(current)
+            .isInstanceOf[Unsupported]) ||
+        (handler.isInstanceOf[CometCodegenDispatch[_]] &&
+          !handler.isInstanceOf[NativeOptInAvailable])
+      }
+      if (unsafeAncestor.isDefined) {
+        // An outer native parent can evaluate even a dispatched subtree too 
early.
+        pushChildren(unsafeAncestor)
+      } else if (!wholeExpressionDispatch) {
+        current match {
+          case _: If | _: CaseWhen | _: EqualNullSafe =>
+            pushChildren(None)
+          case coalesce: Coalesce if coalesce.deterministic =>
+            pushChildren(None)
+          case order: SortOrder =>

Review Comment:
   Could only the leading sort key count as eager? `SORT BY k, next_day(d, 
dow)` raises for me where Spark returns rows.



##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1108,6 +1201,77 @@ object QueryPlanSerde extends Logging with CometExprShim 
with CometTypeShim {
       }
   }
 
+  /** Spark 4.2 changed this setting from String to an Enumeration.Value. */
+  private[comet] def usesInterpretedProjection(conf: SQLConf): Boolean =
+    conf.getConf(SQLConf.CODEGEN_FACTORY_MODE).toString == "NO_CODEGEN"
+
+  /** Inputs Spark evaluates whenever it consumes this expression's output. */
+  private[comet] def eagerlyEvaluatedChildren(expr: Expression): 
Seq[Expression] = expr match {
+    case conditional: ConditionalExpression => 
conditional.alwaysEvaluatedInputs
+    // Generated TryEval can catch failures from a deferred child's code.
+    case _: TryEval => Seq.empty
+    case order: SortOrder => Seq(order.child)
+    case unary: UnaryExpression => Seq(unary.child)
+    case _: DateAdd | _: DateSub | _: DateDiff | _: NextDay | _: 
BinaryComparison =>
+      expr.children.take(if (expr.children.head.nullable) 1 else 2)
+    case _: Concat | _: Greatest | _: Least | _: CreateArray => expr.children

Review Comment:
   Could `Concat` follow the binary rule above? With a NULL `s`, `concat(s, 
CAST(next_day(d, dow) AS STRING))` raises `ILLEGAL_DAY_OF_WEEK` for me where 
Spark returns NULL.



##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1005,6 +1070,34 @@ object QueryPlanSerde extends Logging with CometExprShim 
with CometTypeShim {
       inputs: Seq[Attribute],
       binding: Boolean): Option[Expr] = {
 
+    // Inspect the full tree before an enclosing expression can dispatch it as 
a whole.
+    var remaining = expr :: Nil

Review Comment:
   Could these walks run once at the root? A 1600-term `Add` chain took 
`exprToProto` 104 ms for me, versus 1 ms on the merge base.



##########
spark/src/test/resources/sql-tests/expressions/string/levenshtein_threshold.sql:
##########
@@ -46,7 +46,7 @@ CREATE TABLE test_levenshtein_col(s1 string, s2 string, 
threshold int) USING par
 statement
 INSERT INTO test_levenshtein_col VALUES ('kitten', 'sitting', 2), ('frog', 
'fog', 5), ('abc', 'abc', 0), ('hello', 'world', 3)
 
-query
+query expect_fallback(levenshtein with a nullable threshold requires Spark 
evaluation)

Review Comment:
   Could one of these keep the Rust kernel's per-row threshold covered, using 
`coalesce(threshold, -1)` with `expect_native(levenshtein)`? Both fall back now.



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