viirya commented on code in PR #60551: URL: https://github.com/apache/spark/pull/60551#discussion_r4596906541
########## sql/core/src/main/scala/org/apache/spark/sql/execution/columnar/udf/ExtractColumnarUDFs.scala: ########## @@ -0,0 +1,225 @@ +/* + * 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 scala.collection.mutable + +import org.apache.spark.SparkException +import org.apache.spark.internal.LogKeys.JOIN_CONDITION +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression +import org.apache.spark.sql.catalyst.plans.InnerLike +import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, EvalColumnarUDFs, Filter, Join, LogicalPlan, Project, Subquery} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.{AGGREGATE, COLUMNAR_UDF, JOIN} +import org.apache.spark.sql.errors.QueryCompilationErrors + +/** + * Extracts the calls of columnar user-defined functions from the operators and evaluates them + * in an [[EvalColumnarUDFs]] below the operator, as `ExtractPythonUDFs` does for Python UDFs. + * + * The calls of one operator that need no other call share an [[EvalColumnarUDFs]]. A call whose + * argument is another call gets its own, above that of its argument. An argument that is not + * an attribute is computed by a [[Project]] below, so that the arguments of the calls in an + * [[EvalColumnarUDFs]] are all columns of its child. + * + * Two kinds of calls cannot be evaluated below their operator, and are moved above it first, + * as `ExtractPythonUDFFromJoinCondition` and `ExtractPythonUDFFromAggregate` do: + * - a call in a join condition that reads both sides of the join is moved to a [[Filter]] + * above the join, which is possible for an inner join only; + * - a call in the result of an [[Aggregate]] that reads aggregate functions or grouping + * expressions, or no column at all, is moved to a [[Project]] above the aggregate. + */ +object ExtractColumnarUDFs extends Rule[LogicalPlan] with PredicateHelper { + + def apply(plan: LogicalPlan): LogicalPlan = plan match { + // A correlated subquery is rewritten into a join later, and goes through this rule then. + case s: Subquery if s.correlated => plan + + case _ if !plan.containsPattern(COLUMNAR_UDF) => plan + + case _ => + val prepared = extractFromAggregate(extractFromJoinCondition(plan)) + prepared.transformUpWithPruning(_.containsPattern(COLUMNAR_UDF)) { + case e: EvalColumnarUDFs => e + case p: LogicalPlan => extract(p) + } + } + + private def hasColumnarUDF(e: Expression): Boolean = e.containsPattern(COLUMNAR_UDF) + + // --------------------------------------------------------------------------------------- + // Calls in a join condition that read both sides + // --------------------------------------------------------------------------------------- + + private def hasUDFOverBothSides(e: Expression, join: Join): Boolean = e.exists { Review Comment: Fixed in 7cc0a6c1314. `canEvaluateWithinJoin` returns true for a `ColumnarUDF`, as for a `PythonUDF`, so the predicate becomes the join condition and the extraction then moves the call below the join. The test checks that `add_one(a.x) = b.y` gives a join with a key and not a cartesian product. ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ColumnarUDF.scala: ########## @@ -0,0 +1,73 @@ +/* + * 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.catalyst.expressions + +import org.apache.spark.sql.catalyst.trees.TreePattern.{COLUMNAR_UDF, TreePattern} +import org.apache.spark.sql.columnar.{ColumnarScalarFunction, ColumnarUDFImplementation} +import org.apache.spark.sql.types.{AbstractDataType, DataType} + +/** + * A call of a columnar user-defined function: a scalar function that is evaluated one batch at + * a time, by the `EvalColumnarUDFs` operator that `ExtractColumnarUDFs` plans for it. + * + * A function has any number of implementations. Spark can run `function`, in the JVM. The + * others, `engineImplementations`, are for engines that replace Spark's operators, and Spark + * only carries them. A function without `function` fails when Spark has to run it. + * + * @param name the name of the function + * @param function the implementation that Spark runs, if there is one + * @param engineImplementations the implementations for other engines + * @param inputTypes the types of the arguments, which the analyzer casts the arguments to + * @param dataType the type of the result + * @param children the arguments + * @param udfDeterministic whether the function returns the same result for the same arguments + * @param udfNullable whether the function can return null + * @param resultId the id of the call, which tells two calls of a function that is not + * deterministic apart + */ +case class ColumnarUDF( + name: String, + function: Option[ColumnarScalarFunction], + engineImplementations: Seq[ColumnarUDFImplementation], + inputTypes: Seq[AbstractDataType], + dataType: DataType, + children: Seq[Expression], + udfDeterministic: Boolean = true, + udfNullable: Boolean = true, + resultId: ExprId = NamedExpression.newExprId) + extends Expression with Unevaluable with ImplicitCastInputTypes { + + override lazy val deterministic: Boolean = udfDeterministic && children.forall(_.deterministic) + + override def nullable: Boolean = udfNullable Review Comment: Fixed in 7cc0a6c1314. `ColumnarUDF.expensive` is true. The test checks that a filter over the alias of a call leaves one `EvalColumnarUDFs` node with one call. ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ColumnarUDF.scala: ########## @@ -0,0 +1,73 @@ +/* + * 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.catalyst.expressions + +import org.apache.spark.sql.catalyst.trees.TreePattern.{COLUMNAR_UDF, TreePattern} +import org.apache.spark.sql.columnar.{ColumnarScalarFunction, ColumnarUDFImplementation} +import org.apache.spark.sql.types.{AbstractDataType, DataType} + +/** + * A call of a columnar user-defined function: a scalar function that is evaluated one batch at + * a time, by the `EvalColumnarUDFs` operator that `ExtractColumnarUDFs` plans for it. + * + * A function has any number of implementations. Spark can run `function`, in the JVM. The + * others, `engineImplementations`, are for engines that replace Spark's operators, and Spark + * only carries them. A function without `function` fails when Spark has to run it. + * + * @param name the name of the function + * @param function the implementation that Spark runs, if there is one + * @param engineImplementations the implementations for other engines + * @param inputTypes the types of the arguments, which the analyzer casts the arguments to + * @param dataType the type of the result + * @param children the arguments + * @param udfDeterministic whether the function returns the same result for the same arguments + * @param udfNullable whether the function can return null + * @param resultId the id of the call, which tells two calls of a function that is not + * deterministic apart + */ +case class ColumnarUDF( + name: String, + function: Option[ColumnarScalarFunction], + engineImplementations: Seq[ColumnarUDFImplementation], + inputTypes: Seq[AbstractDataType], + dataType: DataType, + children: Seq[Expression], + udfDeterministic: Boolean = true, + udfNullable: Boolean = true, + resultId: ExprId = NamedExpression.newExprId) Review Comment: Fixed in 7cc0a6c1314. `normalizeExprIds` resets `ColumnarUDF.resultId`, with a test that two plans with the same call are equal after normalization. ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreePatterns.scala: ########## @@ -157,6 +158,7 @@ object TreePattern extends Enumeration { val DESERIALIZE_TO_OBJECT: Value = Value val DF_DROP_COLUMNS: Value = Value val DISTINCT_LIKE: Value = Value + val EVAL_COLUMNAR_UDF: Value = Value Review Comment: Fixed in 7cc0a6c1314. `COLUMNAR_UDF` and `EVAL_COLUMNAR_UDF` are now in `MarkSingleTaskExecution.unsupportedPatterns`, next to the Python ones. -- 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]
