[GitHub] [spark] panbingkun commented on a diff in pull request #40397: [SPARK-42052][SQL] Codegen Support for HiveSimpleUDF

2023-03-17 Thread via GitHub


panbingkun commented on code in PR #40397:
URL: https://github.com/apache/spark/pull/40397#discussion_r1140316433


##
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFEvaluators.scala:
##
@@ -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
+  val isUDFDeterministic = {

Review Comment:
   Done



-- 
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



[GitHub] [spark] panbingkun commented on a diff in pull request #40397: [SPARK-42052][SQL] Codegen Support for HiveSimpleUDF

2023-03-17 Thread via GitHub


panbingkun commented on code in PR #40397:
URL: https://github.com/apache/spark/pull/40397#discussion_r1140125291


##
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFEvaluators.scala:
##
@@ -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
+  val isUDFDeterministic = {
+val udfType = function.getClass.getAnnotation(classOf[HiveUDFType])
+udfType != null && udfType.deterministic() && !udfType.stateful()
+  }
+
+  def returnType: DataType

Review Comment:
   We have to add this method here because it will be used in exception 
handling.



-- 
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



[GitHub] [spark] panbingkun commented on a diff in pull request #40397: [SPARK-42052][SQL] Codegen Support for HiveSimpleUDF

2023-03-17 Thread via GitHub


panbingkun commented on code in PR #40397:
URL: https://github.com/apache/spark/pull/40397#discussion_r1140123443


##
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:
   Done



-- 
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



[GitHub] [spark] panbingkun commented on a diff in pull request #40397: [SPARK-42052][SQL] Codegen Support for HiveSimpleUDF

2023-03-17 Thread via GitHub


panbingkun commented on code in PR #40397:
URL: https://github.com/apache/spark/pull/40397#discussion_r1140123273


##
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()

Review Comment:
   OK



##
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(
+ |"${funcWrapper.functionClassName}",
+ |"${children.map(_.dataType.catalogString).mkString(", ")}",
+ |"${dataType.catalogString}",
+ |e);
+ |}
+ |${CodeGenerator.javaType(dataType)} ${ev.value} = 
${CodeGenerator.defaultValue(dataType)};
+ |if (!${ev.isNull}) {
+ |  ${ev.value} = $resultTerm;
+ |}
+ |""".stripMargin
+)
+  }
+}
+
+abstract class HiveUDFEvaluatorBase[UDFType <: AnyRef](

Review Comment:
   Ok



-- 
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



[GitHub] [spark] panbingkun commented on a diff in pull request #40397: [SPARK-42052][SQL] Codegen Support for HiveSimpleUDF

2023-03-15 Thread via GitHub


panbingkun commented on code in PR #40397:
URL: https://github.com/apache/spark/pull/40397#discussion_r1137141359


##
sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala:
##
@@ -49,68 +48,140 @@ 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)
+
+  @transient
+  private val isUDFDeterministic = {
+val udfType = 
evaluator.function.getClass.getAnnotation(classOf[HiveUDFType])
+udfType != null && udfType.deterministic() && !udfType.stateful()
+  }
+
   override lazy val deterministic: Boolean = isUDFDeterministic && 
children.forall(_.deterministic)
 
   override def nullable: Boolean = true
 
-  @transient
-  lazy val function = funcWrapper.createFunction[UDF]()
+  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 = {
+evaluator.doGenCode(ctx, ev, dataType)
+  }
+}
+
+abstract class HiveUDFEvaluatorBase[UDFType <: AnyRef](
+funcWrapper: HiveFunctionWrapper, children: Seq[Expression])
+  extends HiveInspectors with Serializable {
 
   @transient
-  private lazy val method =
-
function.getResolver.getEvalMethod(children.map(_.dataType.toTypeInfo).asJava)
+  lazy val function = funcWrapper.createFunction[UDFType]()
+
+  def setArg(index: Int, arg: Any): Unit
+
+  def evaluate(): Any
+
+  final def doGenCode(ctx: CodegenContext, ev: ExprCode, dataType: DataType): 
ExprCode = {

Review Comment:
   OK, Let's reserve some redundant logic first.



-- 
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