Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/1063#discussion_r15709191
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/pythonUdfs.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
+
+import java.util.{List => JList, Map => JMap}
+
+import net.razorvine.pickle.{Pickler, Unpickler}
+import org.apache.spark.annotation.DeveloperApi
+import org.apache.spark.api.python.PythonRDD
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.types._
+import org.apache.spark.{Accumulator, Logging => SparkLogging}
+
+import scala.collection.JavaConversions._
+
+/**
+ * A serialized version of a Python lambda function. Suitable for use in
a [[PythonRDD]].
+ */
+private[spark] case class PythonUDF(
+ name: String,
+ command: Array[Byte],
+ envVars: JMap[String, String],
+ pythonIncludes: JList[String],
+ pythonExec: String,
+ accumulator: Accumulator[JList[Array[Byte]]],
+ dataType: DataType,
+ children: Seq[Expression]) extends Expression with SparkLogging {
+
+ override def toString = s"PythonUDF#$name(${children.mkString(",")})"
+
+ def nullable: Boolean = true
+ def references: Set[Attribute] = children.flatMap(_.references).toSet
+
+ override def eval(input: Row) = sys.error("PythonUDFs can not be
directly evaluated.")
+}
+
+/**
+ * Extracts PythonUDFs from operators, rewriting the query plan so that
the UDF can be evaluated
+ * alone in a batch.
+ *
+ * This has the limitation that the input to the Python UDF is not allowed
include attributes from
+ * multiple child operators.
+ */
+private[spark] object ExtractPythonUdfs extends Rule[LogicalPlan] {
+ def apply(plan: LogicalPlan) = plan transform {
+ // Skip EvaluatePython nodes.
+ case p: EvaluatePython => p
+
+ case l: LogicalPlan =>
+ // Extract any PythonUDFs from the current operator.
+ val udfs = l.expressions.flatMap(_.collect { case udf: PythonUDF =>
udf})
+ if (udfs.isEmpty) {
+ // If there aren't any, we are done.
+ l
+ } else {
+ // Pick the UDF we are going to evaluate (TODO: Support evaluating
multiple UDFs at a time)
+ // If there is more than one, we will add another evaluation
operator in a subsequent pass.
+ val udf = udfs.head
+
+ var evaluation: EvaluatePython = null
+
+ // Rewrite the child that has the input required for the UDF
+ val newChildren = l.children.map { child =>
+ if (udf.references.subsetOf(child.outputSet)) {
--- End diff --
Is it possible that the input of this UDF needs to refer to attributes from
multiple children? For example, a UDF is used in a Join condition?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---