Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private[hive] case class HiveSimpleUDF(
with HiveInspectors
with CodegenFallback
with Logging
with UserDefinedExpression {
with UserDefinedExpression
with ImplicitCastInputTypes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ScalaUDF doesn't extend ImplicitCastInputTypes either. Does it have the same problem?

Copy link
Contributor Author

@ulysses-you ulysses-you Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ImplicitTypeCasts has checked ScalaUDF as case udf: ScalaUDF if udf.inputTypes.nonEmpty =>


override lazy val deterministic: Boolean = isUDFDeterministic && children.forall(_.deterministic)

Expand All @@ -69,6 +70,12 @@ private[hive] case class HiveSimpleUDF(
udfType != null && udfType.deterministic() && !udfType.stateful()
}

override def inputTypes: Seq[AbstractDataType] = {
method.getGenericParameterTypes.map(javaTypeToDataType).map { dt =>
if (dt.existsRecursively(_.isInstanceOf[NullType])) AnyDataType else dt
}
}

override def foldable: Boolean = isUDFDeterministic && children.forall(_.foldable)

// Create parameter converters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ class HiveUDFSuite extends QueryTest with TestHiveSingleton with SQLTestUtils {
}
}

test("SPARK-32877: Fix Hive UDF not support decimal type in complex type") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not covered in any other test case, it looks worth of having this.

withUserDefinedFunction("testArraySum" -> false) {
sql(s"CREATE FUNCTION testArraySum AS '${classOf[ArraySumUDF].getName}'")
checkAnswer(
sql("SELECT testArraySum(array(1, 1.1, 1.2))"),
Seq(Row(3.3)))

val msg = intercept[AnalysisException] {
sql("SELECT testArraySum(1)")
}.getMessage
assert(msg.contains(s"No handler for UDF/UDAF/UDTF '${classOf[ArraySumUDF].getName}'"))

val msg2 = intercept[AnalysisException] {
sql("SELECT testArraySum(1, 2)")
}.getMessage
assert(msg2.contains(s"No handler for UDF/UDAF/UDTF '${classOf[ArraySumUDF].getName}'"))
}
}
}

class TestPair(x: Int, y: Int) extends Writable with Serializable {
Expand Down Expand Up @@ -741,3 +759,14 @@ class StatelessUDF extends UDF {
result
}
}

class ArraySumUDF extends UDF {
import scala.collection.JavaConverters._
def evaluate(values: java.util.List[java.lang.Double]): java.lang.Double = {
var r = 0d
for (v <- values.asScala) {
r += v
}
r
}
}