LuciferYang commented on code in PR #39555:
URL: https://github.com/apache/spark/pull/39555#discussion_r1083674070
##########
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala:
##########
@@ -154,17 +154,19 @@ private[hive] case class HiveGenericUDF(
function.initializeAndFoldConstants(argumentInspectors.toArray)
}
+ // Visible for codegen
@transient
- private lazy val unwrapper = unwrapperFor(returnInspector)
+ lazy val unwrapper = unwrapperFor(returnInspector)
@transient
private lazy val isUDFDeterministic = {
val udfType = function.getClass.getAnnotation(classOf[HiveUDFType])
udfType != null && udfType.deterministic() && !udfType.stateful()
}
+ // Visible for codegen
@transient
- private lazy val deferredObjects = argumentInspectors.zip(children).map {
case (inspect, child) =>
+ lazy val deferredObjects = argumentInspectors.zip(children).map { case
(inspect, child) =>
Review Comment:
ditto
##########
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala:
##########
@@ -192,6 +194,48 @@ private[hive] case class HiveGenericUDF(
override protected def withNewChildrenInternal(newChildren:
IndexedSeq[Expression]): Expression =
copy(children = newChildren)
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ val refTerm = ctx.addReferenceObj("this", this)
+ val childrenEvals = children.map(_.genCode(ctx))
+
+ val setDeferredObjects = childrenEvals.zipWithIndex.map {
+ case (eval, i) =>
+ val deferredObjectAdapterClz =
classOf[DeferredObjectAdapter].getCanonicalName
+ s"""
+ |if (${eval.isNull}) {
+ | (($deferredObjectAdapterClz)
$refTerm.deferredObjects()[$i]).set(null);
Review Comment:
The initial value of `func` is null.
`set(null)` seem to be a protective operation
##########
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala:
##########
@@ -154,17 +154,19 @@ private[hive] case class HiveGenericUDF(
function.initializeAndFoldConstants(argumentInspectors.toArray)
}
+ // Visible for codegen
@transient
- private lazy val unwrapper = unwrapperFor(returnInspector)
+ lazy val unwrapper = unwrapperFor(returnInspector)
Review Comment:
change to public should add type annotation
##########
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala:
##########
@@ -192,6 +194,48 @@ private[hive] case class HiveGenericUDF(
override protected def withNewChildrenInternal(newChildren:
IndexedSeq[Expression]): Expression =
copy(children = newChildren)
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ val refTerm = ctx.addReferenceObj("this", this)
+ val childrenEvals = children.map(_.genCode(ctx))
+
+ val setDeferredObjects = childrenEvals.zipWithIndex.map {
+ case (eval, i) =>
+ val deferredObjectAdapterClz =
classOf[DeferredObjectAdapter].getCanonicalName
+ s"""
+ |if (${eval.isNull}) {
+ | (($deferredObjectAdapterClz)
$refTerm.deferredObjects()[$i]).set(null);
+ |} else {
+ | (($deferredObjectAdapterClz)
$refTerm.deferredObjects()[$i]).set(${eval.value});
+ |}
+ |""".stripMargin
+ }
+
+ val resultType = CodeGenerator.boxedType(dataType)
+ val resultTerm = ctx.freshName("result")
+ ev.copy(code =
+ code"""
+ |${childrenEvals.map(_.code).mkString("\n")}
+ |${setDeferredObjects.mkString("\n")}
+ |$resultType $resultTerm = null;
+ |boolean ${ev.isNull} = false;
+ |try {
+ | $resultTerm = ($resultType) $refTerm.unwrapper().apply(
+ | $refTerm.function().evaluate($refTerm.deferredObjects()));
+ | ${ev.isNull} = $resultTerm == null;
+ |} catch (Throwable e) {
+ | throw QueryExecutionErrors.failedExecuteUserDefinedFunctionError(
Review Comment:
For safety, better to add a case check to exception scenario
--
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]