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


##########
spark/src/main/spark-4.1+/org/apache/spark/sql/comet/CometArrowEvalPythonExec.scala:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.spark.sql.comet
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.spark.api.python.PythonEvalType
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, 
Expression, NamedArgumentExpression, NamedExpression, PythonUDF}
+import org.apache.spark.sql.execution.{PartitioningPreservingUnaryExecNode, 
SparkPlan}
+import org.apache.spark.sql.execution.python.ArrowEvalPythonExec
+import org.apache.spark.sql.types.{BinaryType, BooleanType, ByteType, 
DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, 
ShortType, StringType, TimestampNTZType}
+
+import com.google.common.base.Objects
+import com.google.protobuf.ByteString
+
+import org.apache.comet.{CometConf, ConfigEntry, NativeBase}
+import org.apache.comet.CometSparkSessionExtensions.withFallbackReason
+import org.apache.comet.serde.{CometOperatorSerde, Compatible, 
OperatorOuterClass, QueryPlanSerde, SupportLevel, Unsupported}
+import org.apache.comet.serde.OperatorOuterClass.Operator
+
+/** Native execution for Spark 4.1+ scalar `@arrow_udf` functions. */
+object CometArrowEvalPythonExec extends 
CometOperatorSerde[ArrowEvalPythonExec] {
+
+  // SparkContext adds this entry even when the user has not configured a 
Python
+  // environment. Keep other overrides on Spark's worker path.
+  private def hasUnsupportedEnvironment(env: java.util.Map[String, String]): 
Boolean =
+    env != null && env.asScala.exists { case (key, value) =>
+      key != "PYTHONHASHSEED" || value != "0"
+    }
+
+  private def hasCompatibleArrowSchema(dataType: DataType): Boolean = dataType 
match {
+    case _: BooleanType | _: ByteType | _: ShortType | _: IntegerType | _: 
LongType |
+        _: FloatType | _: DoubleType | _: BinaryType | _: DateType | _: 
DecimalType |
+        _: TimestampNTZType =>
+      true
+    // Spark's Arrow conversion accepts plain strings. Collated and 
constrained strings
+    // may carry semantics that are not represented by Comet's Utf8 Arrow type.
+    case s: StringType if s == StringType => true
+    case _ => false
+  }
+
+  override def enabledConfig: Option[ConfigEntry[Boolean]] =
+    Some(CometConf.COMET_NATIVE_ARROW_PYTHON_UDF_ENABLED)
+
+  override def getSupportLevel(op: ArrowEvalPythonExec): SupportLevel = {
+    if (!NativeBase.supportsPythonUdf()) {
+      return Unsupported(Some("Native library lacks the python-udf feature"))
+    }
+    if (op.evalType != PythonEvalType.SQL_SCALAR_ARROW_UDF) {
+      return Unsupported(Some("Only scalar @arrow_udf is supported"))
+    }
+    if (op.udfs.isEmpty || op.udfs.length != op.resultAttrs.length) {
+      return Unsupported(Some("Arrow UDF functions and result attributes do 
not match"))
+    }
+    if (op.conf.arrowUseLargeVarTypes) {
+      return Unsupported(Some("Arrow UDF large variable types are not 
supported in-process"))
+    }
+    if (op.conf.pythonUDFProfiler.nonEmpty) {
+      return Unsupported(Some("Arrow UDF profiling is not supported 
in-process"))
+    }
+    if (op.udfs.exists(_.children.exists(expr => 
!hasCompatibleArrowSchema(expr.dataType))) ||
+      op.resultAttrs.exists(attr => !hasCompatibleArrowSchema(attr.dataType))) 
{
+      return Unsupported(Some("Arrow UDF type is outside the verified native 
Arrow schema set"))
+    }
+    op.udfs.collectFirst {
+      case udf if udf.func.broadcastVars != null && 
!udf.func.broadcastVars.isEmpty =>
+        "Arrow UDF broadcast variables are not supported in-process"
+      case udf if udf.func.pythonIncludes != null && 
!udf.func.pythonIncludes.isEmpty =>
+        "Arrow UDF Python includes are not supported in-process"

Review Comment:
   `addPyFile` with a plain `.py` file and `addFile` both leave 
`pythonIncludes` empty, so they pass this check even though the embedded 
interpreter hasn't set up Spark's files.
   
   I tested both cases with Spark 4.1.3 and this head's native bridge. The UDFs 
succeed in Spark, but the added module fails to load in the bridge with 
`ModuleNotFoundError`, and reading the data file through `SparkFiles.get()` 
raises `AssertionError`. The native checks used a component harness. I haven't 
run the full Comet queries.
   
   Could we keep these cases on Spark's worker path until file setup is 
supported, and add regression tests for both?



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