kazantsev-maksim commented on code in PR #4744:
URL: https://github.com/apache/datafusion-comet/pull/4744#discussion_r4048772189


##########
spark/src/main/scala/org/apache/comet/serde/CometHighOrderFunction.scala:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.comet.serde
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.spark.sql.catalyst.expressions.{Attribute, 
HigherOrderFunction, LambdaFunction => SparkLambdaFunction, NamedLambdaVariable 
=> SparkNamedLambdaVariable}
+
+import org.apache.comet.CometConf
+import org.apache.comet.serde.CometHighOrderFunction.{containsJvmDispatch, 
namedLambdaVariable2Proto}
+import org.apache.comet.serde.ExprOuterClass.{HigherOrderFunc, LambdaFunction, 
NamedLambdaVariable}
+import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, 
serializeDataType}
+
+/**
+ * Serializer that converts Spark higher-order functions (e.g. `filter`, 
`transform`, `exists`)
+ * into Comet's protobuf representation.
+ *
+ * Path selection happens in [[convert]] and has exactly two outcomes, chosen 
in order:
+ *
+ * Native HOF proto - when 
`spark.comet.exec.higherOrderFunction.native.enabled` is set and
+ * [[highOrderFunction2Proto]] serializes the whole expression natively. The 
method returns `None`
+ * (and the HOF falls to the next path) if the HOF is structurally invalid 
(lambda functions must
+ * be `LambdaFunction`, lambda arguments must be `NamedLambdaVariable`) or if 
a lambda body
+ * contains a dispatch-only subexpression (regex, JSON, ...). Such 
subexpressions are serialized
+ * as JVM codegen dispatch nodes, which cannot bind `NamedLambdaVariable`s, so 
the lambda cannot
+ * be executed natively. A dispatch-only expression among the *value* 
arguments is fine and stays
+ * native. JVM codegen dispatch - `CometScalaUDF.emitJvmCodegenDispatch` runs 
the whole HOF
+ * (lambda included) on the JVM; `NamedLambdaVariable`s never cross this 
boundary because the
+ * lambda is evaluated by Spark's own implementation. Returns `None` when the 
dispatcher cannot
+ * handle the expression, which falls back to Spark entirely.
+ *
+ * Whether a lambda body requires dispatch is decided from the already-built 
body proto by
+ * [[CometHighOrderFunction.containsJvmDispatch]] - the body is serialized 
exactly once, and the
+ * verdict comes from the same proto that native execution would use.
+ */
+case class CometHighOrderFunction[T <: HigherOrderFunction](name: String)
+    extends CometExpressionSerde[T] {
+
+  def convert(expr: T, inputs: Seq[Attribute], binding: Boolean): 
Option[ExprOuterClass.Expr] = {
+    if (!CometConf.COMET_EXEC_HIGHER_ORDER_FUNCTION_NATIVE_ENABLED.get()) {
+      return CometScalaUDF.emitJvmCodegenDispatch(expr, inputs, binding)
+    }
+    highOrderFunction2Proto(expr, inputs, binding)
+      .orElse {
+        CometScalaUDF.emitJvmCodegenDispatch(expr, inputs, binding)
+      }
+  }
+
+  private def highOrderFunction2Proto(
+      expr: T,
+      inputs: Seq[Attribute],
+      binding: Boolean): Option[ExprOuterClass.Expr] = {
+    val argumentsProto = expr.arguments.map(exprToProtoInternal(_, inputs, 
binding))
+    val functionsProto = expr.functions
+      .map {
+        case slf: SparkLambdaFunction =>
+          exprToProtoInternal(slf.function, inputs, binding)

Review Comment:
   Thanks for the thorough review and the spot-on reproduction case, @sunchao!
   
   I've addressed this by wrapping the speculative native HOF serialization in 
a `try-catch` block. When traversing the lambda body hits an expression that 
throws during planning (such as `CometCast.convert` eagerly evaluating 
`cast.eval()` in an unreachable/guarded branch under ANSI mode), we now catch 
`NonFatal` and cleanly return `None`. This allows `convert` to gracefully 
degrade to JVM codegen dispatch rather than letting the exception escape and 
abort query planning.
   
   I've also added an ANSI mode regression test covering this exact guarded 
branch scenario (`[-1, 0]` input with `filter(a, x -> CASE WHEN x > 0 THEN 
CAST('bad' AS INT) > 0 ELSE false END)`).



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