cloud-fan commented on code in PR #40397: URL: https://github.com/apache/spark/pull/40397#discussion_r1139898978
########## sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala: ########## @@ -49,68 +48,137 @@ private[hive] case class HiveSimpleUDF( name: String, funcWrapper: HiveFunctionWrapper, children: Seq[Expression]) extends Expression with HiveInspectors - with CodegenFallback - with Logging with UserDefinedExpression { + @transient + private lazy val evaluator = new HiveSimpleUDFEvaluator(funcWrapper, children) + override lazy val deterministic: Boolean = isUDFDeterministic && children.forall(_.deterministic) override def nullable: Boolean = true @transient - lazy val function = funcWrapper.createFunction[UDF]() + private val isUDFDeterministic = { + val udfType = evaluator.function.getClass.getAnnotation(classOf[HiveUDFType]) + udfType != null && udfType.deterministic() && !udfType.stateful() + } + + override def foldable: Boolean = isUDFDeterministic && children.forall(_.foldable) + + override lazy val dataType: DataType = javaTypeToDataType(evaluator.method.getGenericReturnType) + + // TODO: Finish input output types. + override def eval(input: InternalRow): Any = { + children.zipWithIndex.map { + case (child, idx) => + evaluator.setArg(idx, child.eval(input)) + } + evaluator.evaluate() + } + + override def toString: String = { + s"$nodeName#${funcWrapper.functionClassName}(${children.mkString(",")})" + } + + override def prettyName: String = name + + override def sql: String = s"$name(${children.map(_.sql).mkString(", ")})" + + override protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]): Expression = + copy(children = newChildren) + + protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val refEvaluator = ctx.addReferenceObj("evaluator", evaluator) + val evals = children.map(_.genCode(ctx)) + + val setValues = evals.zipWithIndex.map { + case (eval, i) => + s""" + |if (${eval.isNull}) { + | $refEvaluator.setArg($i, null); + |} else { + | $refEvaluator.setArg($i, ${eval.value}); + |} + |""".stripMargin + } + + val resultType = CodeGenerator.boxedType(dataType) + val resultTerm = ctx.freshName("result") + ev.copy(code = + code""" + |${evals.map(_.code).mkString("\n")} + |${setValues.mkString("\n")} + |$resultType $resultTerm = null; + |boolean ${ev.isNull} = false; + |try { + | $resultTerm = ($resultType) $refEvaluator.evaluate(); + | ${ev.isNull} = $resultTerm == null; + |} catch (Throwable e) { + | throw QueryExecutionErrors.failedExecuteUserDefinedFunctionError( Review Comment: shall we move the try-catch to `evaluator.evaluate()`? -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org