dongjoon-hyun commented on code in PR #60551:
URL: https://github.com/apache/spark/pull/60551#discussion_r4604205829


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/columnar/udf/EvalColumnarUDFsExec.scala:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.execution.columnar.udf
+
+import org.apache.spark.{SparkException, SparkRuntimeException, 
SparkUnsupportedOperationException, TaskContext}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, 
BindReferences, BoundReference, ColumnarUDF, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{EvalColumnarUDFs, 
LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.physical.Partitioning
+import org.apache.spark.sql.catalyst.util.TypeUtils.toSQLType
+import org.apache.spark.sql.columnar.ColumnarScalarFunction
+import org.apache.spark.sql.execution.{SparkPlan, SparkStrategy, UnaryExecNode}
+import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
+import org.apache.spark.sql.types.DataType
+import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
+
+/** Plans the evaluation of columnar user-defined functions. */
+object ColumnarUDFEvals extends SparkStrategy {
+  override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
+    case EvalColumnarUDFs(udfs, resultAttrs, child) =>
+      EvalColumnarUDFsExec(udfs, resultAttrs, planLater(child)) :: Nil
+    case _ => Nil
+  }
+}
+
+/**
+ * Evaluates calls of columnar user-defined functions, one batch at a time. 
The input and the
+ * output are columnar: an output batch is the columns of the input batch, as 
they are, followed
+ * by one column per call. The arguments of the calls are columns of the child.
+ *
+ * This node runs the implementation of each function that Spark can run, its
+ * [[ColumnarScalarFunction]]. An engine that replaces Spark's operators can 
replace this node
+ * with one that runs another implementation, from 
`ColumnarUDF.engineImplementations`. A
+ * function that Spark cannot run fails only when this node is executed, so 
that such an engine
+ * has replaced the node by then.
+ */
+case class EvalColumnarUDFsExec(
+    udfs: Seq[ColumnarUDF],
+    resultAttrs: Seq[Attribute],
+    child: SparkPlan) extends UnaryExecNode {
+
+  override def output: Seq[Attribute] = child.output ++ resultAttrs
+
+  override def producedAttributes: AttributeSet = AttributeSet(resultAttrs)
+
+  override def outputPartitioning: Partitioning = child.outputPartitioning
+
+  override def outputOrdering: Seq[SortOrder] = child.outputOrdering
+
+  override def supportsColumnar: Boolean = true
+
+  override lazy val metrics: Map[String, SQLMetric] = Map(
+    "numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output 
rows"),
+    "numOutputBatches" -> SQLMetrics.createMetric(sparkContext, "number of 
output batches"),
+    "evalTime" -> SQLMetrics.createNanoTimingMetric(sparkContext, "time in 
functions"))
+
+  override protected def doExecute(): RDD[InternalRow] = {
+    throw SparkException.internalError(s"$nodeName only supports columnar 
execution.")
+  }
+
+  override protected def doExecuteColumnar(): RDD[ColumnarBatch] = {
+    val functions: Array[ColumnarScalarFunction] = udfs.map { udf =>
+      udf.function.getOrElse {
+        throw new SparkUnsupportedOperationException(
+          errorClass = "COLUMNAR_UDF_NOT_EXECUTABLE",
+          messageParameters = Map(
+            "name" -> udf.name,
+            "kinds" -> udf.engineImplementations.map(_.kind).mkString("[", ", 
", "]")))
+      }
+    }.toArray
+    // The ordinals, in the input batch, of the arguments of each call.
+    val argOrdinals: Array[Array[Int]] = udfs.map { udf =>
+      udf.children.map { arg =>
+        BindReferences.bindReference(arg, child.output) match {
+          case b: BoundReference => b.ordinal
+          case other => throw SparkException.internalError(
+            s"The argument $other of ${udf.name} is not a column of the 
child.")
+        }
+      }.toArray
+    }.toArray
+    val names: Array[String] = udfs.map(_.name).toArray
+    val resultTypes: Array[DataType] = udfs.map(_.dataType).toArray
+    val numOutputRows = longMetric("numOutputRows")
+    val numOutputBatches = longMetric("numOutputBatches")
+    val evalTime = longMetric("evalTime")
+    val numInputColumns = child.output.length
+
+    child.executeColumnar().mapPartitionsInternal { batches =>
+      new Iterator[ColumnarBatch] {
+        // The results of the current batch, which this node owns.
+        private var results: Array[ColumnVector] = Array.empty
+
+        private def closeResults(): Unit = {
+          results.foreach(r => if (r != null) r.close())
+          results = Array.empty
+        }
+
+        Option(TaskContext.get()).foreach(_.addTaskCompletionListener[Unit](_ 
=> closeResults()))
+
+        private def isInputColumn(batch: ColumnarBatch, vector: ColumnVector): 
Boolean = {
+          var i = 0
+          while (i < numInputColumns) {
+            if (batch.column(i) eq vector) return true
+            i += 1
+          }
+          false
+        }
+
+        override def hasNext: Boolean = {
+          val more = batches.hasNext
+          if (!more) closeResults()
+          more
+        }
+
+        override def next(): ColumnarBatch = {
+          closeResults()
+          val batch = batches.next()
+          val numRows = batch.numRows()
+          val columns = new Array[ColumnVector](numInputColumns + 
functions.length)
+          var i = 0
+          while (i < numInputColumns) {
+            columns(i) = batch.column(i)
+            i += 1
+          }
+          val start = System.nanoTime()
+          val newResults = new Array[ColumnVector](functions.length)
+          results = newResults
+          i = 0
+          while (i < functions.length) {
+            val args = new ColumnarBatch(argOrdinals(i).map(batch.column), 
numRows)
+            val result = functions(i).eval(args)
+            // This node closes the results, and the child still owns its 
columns: it can use
+            // them again for its next batch. So a result that is a column of 
the child is not
+            // kept as a result, and is rejected.
+            if (result != null && isInputColumn(batch, result)) {
+              throw new SparkRuntimeException(
+                errorClass = "COLUMNAR_UDF_RESULT_IS_ARGUMENT",
+                messageParameters = Map("name" -> names(i)))
+            }
+            // Set before the check, so that a vector of the wrong type is 
closed as well.
+            newResults(i) = result
+            if (result == null ||
+                !DataType.equalsStructurally(result.dataType(), 
resultTypes(i), true)) {

Review Comment:
   **[25] A function whose result type contains a UDT always fails the result 
type check.** The `ColumnVector` constructor replaces every UDT in its type 
with the UDT's `sqlType`, recursively (ColumnVector.java L376-388), so 
`result.dataType()` never contains a UDT, while `resultTypes(i)` is the 
declared type (L98). `equalsStructurally` compares a UDT with `==`, so for a 
function declared with a UDT result type, e.g. `SQLDataTypes.VectorType`, the 
first batch fails with `COLUMNAR_UDF_RESULT_TYPE_MISMATCH` even when the 
function returns a vector of that type. `ARRAY<udt>` and a struct with a UDT 
field fail the same way. `registerColumnar` and the builder accept such a type, 
so this shows only at execution. The Python columnar path replaces UDTs in the 
expected types before it compares them 
(ColumnarArrowEvalPythonEvaluatorFactory.scala L240-243). Could this check do 
the same, with a test? If UDT results are out of scope for this PR, could they 
be rejected when the function is defined 
 instead?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/columnar/udf/EvalColumnarUDFsExec.scala:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.execution.columnar.udf
+
+import org.apache.spark.{SparkException, SparkRuntimeException, 
SparkUnsupportedOperationException, TaskContext}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, 
BindReferences, BoundReference, ColumnarUDF, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{EvalColumnarUDFs, 
LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.physical.Partitioning
+import org.apache.spark.sql.catalyst.util.TypeUtils.toSQLType
+import org.apache.spark.sql.columnar.ColumnarScalarFunction
+import org.apache.spark.sql.execution.{SparkPlan, SparkStrategy, UnaryExecNode}
+import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
+import org.apache.spark.sql.types.DataType
+import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
+
+/** Plans the evaluation of columnar user-defined functions. */
+object ColumnarUDFEvals extends SparkStrategy {
+  override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
+    case EvalColumnarUDFs(udfs, resultAttrs, child) =>
+      EvalColumnarUDFsExec(udfs, resultAttrs, planLater(child)) :: Nil
+    case _ => Nil
+  }
+}
+
+/**
+ * Evaluates calls of columnar user-defined functions, one batch at a time. 
The input and the
+ * output are columnar: an output batch is the columns of the input batch, as 
they are, followed
+ * by one column per call. The arguments of the calls are columns of the child.
+ *
+ * This node runs the implementation of each function that Spark can run, its
+ * [[ColumnarScalarFunction]]. An engine that replaces Spark's operators can 
replace this node
+ * with one that runs another implementation, from 
`ColumnarUDF.engineImplementations`. A
+ * function that Spark cannot run fails only when this node is executed, so 
that such an engine
+ * has replaced the node by then.
+ */
+case class EvalColumnarUDFsExec(
+    udfs: Seq[ColumnarUDF],
+    resultAttrs: Seq[Attribute],
+    child: SparkPlan) extends UnaryExecNode {
+
+  override def output: Seq[Attribute] = child.output ++ resultAttrs
+
+  override def producedAttributes: AttributeSet = AttributeSet(resultAttrs)
+
+  override def outputPartitioning: Partitioning = child.outputPartitioning
+
+  override def outputOrdering: Seq[SortOrder] = child.outputOrdering
+
+  override def supportsColumnar: Boolean = true
+
+  override lazy val metrics: Map[String, SQLMetric] = Map(
+    "numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output 
rows"),
+    "numOutputBatches" -> SQLMetrics.createMetric(sparkContext, "number of 
output batches"),
+    "evalTime" -> SQLMetrics.createNanoTimingMetric(sparkContext, "time in 
functions"))
+
+  override protected def doExecute(): RDD[InternalRow] = {
+    throw SparkException.internalError(s"$nodeName only supports columnar 
execution.")
+  }
+
+  override protected def doExecuteColumnar(): RDD[ColumnarBatch] = {
+    val functions: Array[ColumnarScalarFunction] = udfs.map { udf =>
+      udf.function.getOrElse {
+        throw new SparkUnsupportedOperationException(
+          errorClass = "COLUMNAR_UDF_NOT_EXECUTABLE",
+          messageParameters = Map(
+            "name" -> udf.name,
+            "kinds" -> udf.engineImplementations.map(_.kind).mkString("[", ", 
", "]")))
+      }
+    }.toArray
+    // The ordinals, in the input batch, of the arguments of each call.
+    val argOrdinals: Array[Array[Int]] = udfs.map { udf =>
+      udf.children.map { arg =>
+        BindReferences.bindReference(arg, child.output) match {
+          case b: BoundReference => b.ordinal
+          case other => throw SparkException.internalError(
+            s"The argument $other of ${udf.name} is not a column of the 
child.")
+        }
+      }.toArray
+    }.toArray
+    val names: Array[String] = udfs.map(_.name).toArray
+    val resultTypes: Array[DataType] = udfs.map(_.dataType).toArray
+    val numOutputRows = longMetric("numOutputRows")
+    val numOutputBatches = longMetric("numOutputBatches")
+    val evalTime = longMetric("evalTime")
+    val numInputColumns = child.output.length
+
+    child.executeColumnar().mapPartitionsInternal { batches =>
+      new Iterator[ColumnarBatch] {
+        // The results of the current batch, which this node owns.
+        private var results: Array[ColumnVector] = Array.empty
+
+        private def closeResults(): Unit = {
+          results.foreach(r => if (r != null) r.close())
+          results = Array.empty
+        }
+
+        Option(TaskContext.get()).foreach(_.addTaskCompletionListener[Unit](_ 
=> closeResults()))
+
+        private def isInputColumn(batch: ColumnarBatch, vector: ColumnVector): 
Boolean = {
+          var i = 0
+          while (i < numInputColumns) {
+            if (batch.column(i) eq vector) return true
+            i += 1
+          }
+          false
+        }
+
+        override def hasNext: Boolean = {
+          val more = batches.hasNext
+          if (!more) closeResults()
+          more
+        }
+
+        override def next(): ColumnarBatch = {
+          closeResults()
+          val batch = batches.next()
+          val numRows = batch.numRows()
+          val columns = new Array[ColumnVector](numInputColumns + 
functions.length)
+          var i = 0
+          while (i < numInputColumns) {
+            columns(i) = batch.column(i)
+            i += 1
+          }
+          val start = System.nanoTime()
+          val newResults = new Array[ColumnVector](functions.length)
+          results = newResults
+          i = 0
+          while (i < functions.length) {
+            val args = new ColumnarBatch(argOrdinals(i).map(batch.column), 
numRows)
+            val result = functions(i).eval(args)

Review Comment:
   **[26] An exception from the function is not wrapped, so the error does not 
name the function.** `ScalaUDF` (L1209-1216, and the generated code at 
L1175-1185) and the Hive UDF evaluators (hiveUDFEvaluators.scala L62) wrap an 
exception from user code in `FAILED_EXECUTE_UDF` with 
`QueryExecutionErrors.failedExecuteUserDefinedFunctionError(functionName, 
inputTypes, outputType, e)`. Here, an exception from `eval`, e.g. an 
`ArithmeticException`, fails the task as it is, so when an operator has several 
calls, or nested calls, only the stack trace tells which function failed. The 
new errors of this node (`COLUMNAR_UDF_RESULT_TYPE_MISMATCH`, 
`COLUMNAR_UDF_RESULT_IS_ARGUMENT`) already name the function. Could this call 
wrap the exception in the same error, as `ScalaUDF` does, with a test?



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