LuciferYang commented on code in PR #39555:
URL: https://github.com/apache/spark/pull/39555#discussion_r1083687701
##########
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 the 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]