-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-32877][SQL] Fix Hive UDF not support decimal type in complex type #29749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,8 @@ private[hive] case class HiveSimpleUDF( | |
| with HiveInspectors | ||
| with CodegenFallback | ||
| with Logging | ||
| with UserDefinedExpression { | ||
| with UserDefinedExpression | ||
| with ImplicitCastInputTypes { | ||
|
|
||
| override lazy val deterministic: Boolean = isUDFDeterministic && children.forall(_.deterministic) | ||
|
|
||
|
|
@@ -57,8 +58,19 @@ private[hive] case class HiveSimpleUDF( | |
| lazy val function = funcWrapper.createFunction[UDF]() | ||
|
|
||
| @transient | ||
| private lazy val method = | ||
| function.getResolver.getEvalMethod(children.map(_.dataType.toTypeInfo).asJava) | ||
| private lazy val method = { | ||
| // the simple UDF method must be 'evaluate' | ||
| val methods = function.getClass.getMethods.filter(_.getName == "evaluate") | ||
| val passedMethod = methods.filter(_.getGenericParameterTypes.length == children.length) | ||
|
|
||
| // no matching parameter num for evaluate method | ||
| if (passedMethod.isEmpty) { | ||
| throw new NoMatchingMethodException(function.getClass, | ||
| children.map(_.dataType.toTypeInfo).asJava, methods.toSeq.asJava) | ||
| } | ||
| // if there exists many method, we choose the first | ||
| methods.head | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally, UDF just has one method of |
||
| } | ||
|
|
||
| @transient | ||
| private lazy val arguments = children.map(toInspector).toArray | ||
|
|
@@ -69,6 +81,10 @@ private[hive] case class HiveSimpleUDF( | |
| udfType != null && udfType.deterministic() && !udfType.stateful() | ||
| } | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = { | ||
| method.getGenericParameterTypes.map(javaTypeToDataType) | ||
| } | ||
|
|
||
| override def foldable: Boolean = isUDFDeterministic && children.forall(_.foldable) | ||
|
|
||
| // Create parameter converters | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("cannot resolve 'default.testArraySum(1)' due to data type mismatch")) | ||
|
|
||
| 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 { | ||
|
|
@@ -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 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScalaUDFdoesn't extendImplicitCastInputTypeseither. Does it have the same problem?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
ImplicitTypeCastshas checkedScalaUDFascase udf: ScalaUDF if udf.inputTypes.nonEmpty =>