-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF #40397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d87c5db
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun bfbf075
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun c9e2022
Merge branch 'master' into refactor_HiveSimpleUDF
panbingkun 32fc258
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun a5d7b69
Merge branch 'master' into refactor_HiveSimpleUDF
panbingkun 30d4edf
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun 61ce1ba
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun 218dd44
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun b422a8b
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun a463d74
[SPARK-42052][SQL] Codegen Support for HiveSimpleUDF
panbingkun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFEvaluators.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * 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.hive | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| import org.apache.hadoop.hive.ql.exec.{FunctionRegistry, UDF} | ||
| import org.apache.hadoop.hive.ql.udf.{UDFType => HiveUDFType} | ||
| import org.apache.hadoop.hive.ql.udf.generic.GenericUDF | ||
| import org.apache.hadoop.hive.ql.udf.generic.GenericUDF._ | ||
| import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ConversionHelper | ||
| import org.apache.hadoop.hive.serde2.objectinspector.{ObjectInspector, ObjectInspectorFactory} | ||
| import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.ObjectInspectorOptions | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| import org.apache.spark.sql.errors.QueryExecutionErrors | ||
| import org.apache.spark.sql.hive.HiveShim.HiveFunctionWrapper | ||
| import org.apache.spark.sql.types.DataType | ||
|
|
||
| abstract class HiveUDFEvaluatorBase[UDFType <: AnyRef]( | ||
| funcWrapper: HiveFunctionWrapper, children: Seq[Expression]) | ||
| extends HiveInspectors with Serializable { | ||
|
|
||
| @transient | ||
| lazy val function = funcWrapper.createFunction[UDFType]() | ||
|
|
||
| @transient | ||
| lazy val isUDFDeterministic = { | ||
| val udfType = function.getClass.getAnnotation(classOf[HiveUDFType]) | ||
| udfType != null && udfType.deterministic() && !udfType.stateful() | ||
| } | ||
|
|
||
| def returnType: DataType | ||
|
|
||
| def setArg(index: Int, arg: Any): Unit | ||
|
|
||
| def doEvaluate(): Any | ||
|
|
||
| final def evaluate(): Any = { | ||
| try { | ||
| doEvaluate() | ||
| } catch { | ||
| case e: Throwable => | ||
| throw QueryExecutionErrors.failedExecuteUserDefinedFunctionError( | ||
| s"${funcWrapper.functionClassName}", | ||
| s"${children.map(_.dataType.catalogString).mkString(", ")}", | ||
| s"${returnType.catalogString}", | ||
| e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class HiveSimpleUDFEvaluator( | ||
| funcWrapper: HiveFunctionWrapper, children: Seq[Expression]) | ||
| extends HiveUDFEvaluatorBase[UDF](funcWrapper, children) { | ||
|
|
||
| @transient | ||
| lazy val method = function.getResolver. | ||
| getEvalMethod(children.map(_.dataType.toTypeInfo).asJava) | ||
|
|
||
| @transient | ||
| private lazy val wrappers = children.map(x => wrapperFor(toInspector(x), x.dataType)).toArray | ||
|
|
||
| @transient | ||
| private lazy val arguments = children.map(toInspector).toArray | ||
|
|
||
| // Create parameter converters | ||
| @transient | ||
| private lazy val conversionHelper = new ConversionHelper(method, arguments) | ||
|
|
||
| @transient | ||
| private lazy val inputs: Array[AnyRef] = new Array[AnyRef](children.length) | ||
|
|
||
| override def returnType: DataType = javaTypeToDataType(method.getGenericReturnType) | ||
|
|
||
| override def setArg(index: Int, arg: Any): Unit = { | ||
| inputs(index) = wrappers(index)(arg).asInstanceOf[AnyRef] | ||
| } | ||
|
|
||
| @transient | ||
| private lazy val unwrapper: Any => Any = | ||
| unwrapperFor(ObjectInspectorFactory.getReflectionObjectInspector( | ||
| method.getGenericReturnType, ObjectInspectorOptions.JAVA)) | ||
|
|
||
| override def doEvaluate(): Any = { | ||
| val ret = FunctionRegistry.invoke( | ||
| method, | ||
| function, | ||
| conversionHelper.convertIfNecessary(inputs: _*): _*) | ||
| unwrapper(ret) | ||
| } | ||
| } | ||
|
|
||
| class HiveGenericUDFEvaluator( | ||
| funcWrapper: HiveFunctionWrapper, children: Seq[Expression]) | ||
| extends HiveUDFEvaluatorBase[GenericUDF](funcWrapper, children) { | ||
|
|
||
| @transient | ||
| private lazy val argumentInspectors = children.map(toInspector) | ||
|
|
||
| @transient | ||
| lazy val returnInspector = { | ||
| function.initializeAndFoldConstants(argumentInspectors.toArray) | ||
| } | ||
|
|
||
| @transient | ||
| private lazy val deferredObjects: Array[DeferredObject] = argumentInspectors.zip(children).map { | ||
| case (inspect, child) => new DeferredObjectAdapter(inspect, child.dataType) | ||
| }.toArray[DeferredObject] | ||
|
|
||
| @transient | ||
| private lazy val unwrapper: Any => Any = unwrapperFor(returnInspector) | ||
|
|
||
| override def returnType: DataType = inspectorToDataType(returnInspector) | ||
|
|
||
| def setArg(index: Int, arg: Any): Unit = | ||
| deferredObjects(index).asInstanceOf[DeferredObjectAdapter].set(arg) | ||
|
|
||
| override def doEvaluate(): Any = unwrapper(function.evaluate(deferredObjects)) | ||
| } | ||
|
|
||
| // Adapter from Catalyst ExpressionResult to Hive DeferredObject | ||
| private[hive] class DeferredObjectAdapter(oi: ObjectInspector, dataType: DataType) | ||
| extends DeferredObject with HiveInspectors { | ||
|
|
||
| private val wrapper = wrapperFor(oi, dataType) | ||
| private var func: Any = _ | ||
| def set(func: Any): Unit = { | ||
| this.func = func | ||
| } | ||
| override def prepare(i: Int): Unit = {} | ||
| override def get(): AnyRef = wrapper(func).asInstanceOf[AnyRef] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to add this method here because it will be used in exception handling.