Skip to content

Commit d9697fe

Browse files
cxzl25HyukjinKwon
authored andcommitted
[SPARK-28012][SQL] Hive UDF supports struct type foldable expression
## What changes were proposed in this pull request? Currently using hive udf, the parameter is struct type, there will be an exception thrown. No handler for Hive UDF 'xxxUDF': java.lang.RuntimeException: Hive doesn't support the constant type [StructType(StructField(name,StringType,true), StructField(value,DecimalType(3,1),true))] ## How was this patch tested? added new UT Closes apache#24846 from cxzl25/hive_udf_literal_struct_type. Authored-by: sychen <[email protected]> Signed-off-by: HyukjinKwon <[email protected]>
1 parent 4968f87 commit d9697fe

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,8 @@ private[hive] trait HiveInspectors {
847847

848848
ObjectInspectorFactory.getStandardConstantMapObjectInspector(keyOI, valueOI, jmap)
849849
}
850+
case Literal(_, dt: StructType) =>
851+
toInspector(dt)
850852
// We will enumerate all of the possible constant expressions, throw exception if we missed
851853
case Literal(_, dt) => sys.error(s"Hive doesn't support the constant type [$dt].")
852854
// ideally, we don't test the foldable here(but in optimizer), however, some of the

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveUDFSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,17 @@ class HiveUDFSuite extends QueryTest with TestHiveSingleton with SQLTestUtils {
652652
}
653653
}
654654
}
655+
test("SPARK-28012 Hive UDF supports struct type foldable expression") {
656+
withUserDefinedFunction("testUDFStructType" -> false) {
657+
// Simulate a hive udf that supports struct parameters
658+
sql("CREATE FUNCTION testUDFStructType AS '" +
659+
s"${classOf[GenericUDFArray].getName}'")
660+
checkAnswer(
661+
sql("SELECT testUDFStructType(named_struct('name', 'xx', 'value', 1))[0].value"),
662+
Seq(Row(1)))
663+
}
664+
}
665+
655666
}
656667

657668
class TestPair(x: Int, y: Int) extends Writable with Serializable {

0 commit comments

Comments
 (0)