diff options
author | Takeshi YAMAMURO <linguin.m.s@gmail.com> | 2015-07-08 10:33:27 -0700 |
---|---|---|
committer | Michael Armbrust <michael@databricks.com> | 2015-07-08 10:33:27 -0700 |
commit | 3e831a26965a5e92210431f9ad6935f70aa01b48 (patch) | |
tree | ab5729063a9c987e46e66f94d201da76d0407790 /sql/hive | |
parent | 6722aca809ddc28aa20abf3bbb2e0de8629a9903 (diff) | |
download | spark-3e831a26965a5e92210431f9ad6935f70aa01b48.tar.gz spark-3e831a26965a5e92210431f9ad6935f70aa01b48.tar.bz2 spark-3e831a26965a5e92210431f9ad6935f70aa01b48.zip |
[SPARK-6912] [SQL] Throw an AnalysisException when unsupported Java Map<K,V> types used in Hive UDF
To make UDF developers understood, throw an exception when unsupported Map<K,V> types used in Hive UDF. This fix is the same with #7248.
Author: Takeshi YAMAMURO <linguin.m.s@gmail.com>
Closes #7257 from maropu/ThrowExceptionWhenMapUsed and squashes the following commits:
916099a [Takeshi YAMAMURO] Fix style errors
7886dcc [Takeshi YAMAMURO] Throw an exception when Map<> used in Hive UDF
Diffstat (limited to 'sql/hive')
4 files changed, 108 insertions, 0 deletions
diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala index 924e2ba3ba..4cba17524a 100644 --- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala +++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala @@ -226,6 +226,12 @@ private[hive] trait HiveInspectors { "List type in java is unsupported because " + "JVM type erasure makes spark fail to catch a component type in List<>") + // java map type unsupported + case c: Class[_] if c == classOf[java.util.Map[_, _]] => + throw new AnalysisException( + "Map type in java is unsupported because " + + "JVM type erasure makes spark fail to catch key and value types in Map<>") + case c => throw new AnalysisException(s"Unsupported java type $c") } diff --git a/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToIntIntMap.java b/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToIntIntMap.java new file mode 100644 index 0000000000..b3e8bcbbd8 --- /dev/null +++ b/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToIntIntMap.java @@ -0,0 +1,35 @@ +/* + * 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.execution; + +import org.apache.hadoop.hive.ql.exec.UDF; + +import java.util.HashMap; +import java.util.Map; + +public class UDFToIntIntMap extends UDF { + public Map<Integer, Integer> evaluate(Object o) { + return new HashMap<Integer, Integer>() { + { + put(1, 1); + put(2, 1); + put(3, 1); + } + }; + } +} diff --git a/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToStringIntMap.java b/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToStringIntMap.java new file mode 100644 index 0000000000..9eea5c9a88 --- /dev/null +++ b/sql/hive/src/test/java/org/apache/spark/sql/hive/execution/UDFToStringIntMap.java @@ -0,0 +1,35 @@ +/* + * 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.execution; + +import org.apache.hadoop.hive.ql.exec.UDF; + +import java.util.HashMap; +import java.util.Map; + +public class UDFToStringIntMap extends UDF { + public Map<String, Integer> evaluate(Object o) { + return new HashMap<String, Integer>() { + { + put("key1", 1); + put("key2", 2); + put("key3", 3); + } + }; + } +} diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveUDFSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveUDFSuite.scala index 44686204c2..eaaa88e170 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveUDFSuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveUDFSuite.scala @@ -163,6 +163,38 @@ class HiveUDFSuite extends QueryTest { TestHive.reset() } + test("UDFToStringIntMap") { + val testData = TestHive.sparkContext.parallelize(StringCaseClass("") :: Nil).toDF() + testData.registerTempTable("inputTable") + + sql(s"CREATE TEMPORARY FUNCTION testUDFToStringIntMap " + + s"AS '${classOf[UDFToStringIntMap].getName}'") + val errMsg = intercept[AnalysisException] { + sql("SELECT testUDFToStringIntMap(s) FROM inputTable") + } + assert(errMsg.getMessage === "Map type in java is unsupported because " + + "JVM type erasure makes spark fail to catch key and value types in Map<>;") + + sql("DROP TEMPORARY FUNCTION IF EXISTS testUDFToStringIntMap") + TestHive.reset() + } + + test("UDFToIntIntMap") { + val testData = TestHive.sparkContext.parallelize(StringCaseClass("") :: Nil).toDF() + testData.registerTempTable("inputTable") + + sql(s"CREATE TEMPORARY FUNCTION testUDFToIntIntMap " + + s"AS '${classOf[UDFToIntIntMap].getName}'") + val errMsg = intercept[AnalysisException] { + sql("SELECT testUDFToIntIntMap(s) FROM inputTable") + } + assert(errMsg.getMessage === "Map type in java is unsupported because " + + "JVM type erasure makes spark fail to catch key and value types in Map<>;") + + sql("DROP TEMPORARY FUNCTION IF EXISTS testUDFToIntIntMap") + TestHive.reset() + } + test("UDFListListInt") { val testData = TestHive.sparkContext.parallelize( ListListIntCaseClass(Nil) :: |