Skip to content
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

[SPARK-6747][SQL] Support List<> as a return type in Hive UDF #5395

Closed
wants to merge 5 commits into from
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 @@ -22,9 +22,11 @@ import org.apache.hadoop.hive.serde2.objectinspector._
import org.apache.hadoop.hive.serde2.objectinspector.primitive._
import org.apache.hadoop.hive.serde2.{io => hiveIo}
import org.apache.hadoop.{io => hadoopIo}
import org.apache.spark.Logging
import org.apache.spark.annotation.DeveloperApi

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types
import org.apache.spark.sql.{AnalysisException, types}
import org.apache.spark.sql.types._

/* Implicit conversions */
Expand Down Expand Up @@ -169,7 +171,7 @@ import scala.collection.JavaConversions._
* e.g. date_add(printf("%s-%s-%s", a,b,c), 3)
* We don't need to unwrap the data for printf and wrap it again and passes in data_add
*/
private[hive] trait HiveInspectors {
private[hive] trait HiveInspectors extends Logging {

def javaClassToDataType(clz: Class[_]): DataType = clz match {
// writable
Expand Down Expand Up @@ -213,8 +215,16 @@ private[hive] trait HiveInspectors {

case c: Class[_] if c.isArray => ArrayType(javaClassToDataType(c.getComponentType))

// list type
case c: Class[_] if c == classOf[java.util.List[java.lang.Object]] =>
logWarning("Failed to catch a component type in List<> because of type erasure in JVM," +
" so you need to cast it into the correct type by yourself")
ArrayType(NullType)

// Hive seems to return this for struct types?
case c: Class[_] if c == classOf[java.lang.Object] => NullType

case c => throw new AnalysisException(s"Unsupported java type $c")
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.Arrays;
import java.util.List;

public class UDFToListInt extends UDF {
public List<Integer> evaluate(Object o) {
return Arrays.asList(1, 2, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.Arrays;
import java.util.List;

public class UDFToListString extends UDF {
public List<String> evaluate(Object o) {
return Arrays.asList("data1", "data2", "data3");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line at the end of file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ class HiveUdfSuite extends QueryTest {
TestHive.reset()
}

test("UDFToListString") {
val testData = TestHive.sparkContext.parallelize(StringCaseClass("") :: Nil).toDF()
testData.registerTempTable("inputTable")

sql(s"CREATE TEMPORARY FUNCTION testUDFToListString AS '${classOf[UDFToListString].getName}'")
checkAnswer(
sql("SELECT testUDFToListString(s) FROM inputTable"),
Seq(Row("data1" :: "data2" :: "data3" :: Nil)))
sql("DROP TEMPORARY FUNCTION IF EXISTS testUDFToListString")

TestHive.reset()
}

test("UDFToListInt") {
val testData = TestHive.sparkContext.parallelize(StringCaseClass("") :: Nil).toDF()
testData.registerTempTable("inputTable")

sql(s"CREATE TEMPORARY FUNCTION testUDFToListInt AS '${classOf[UDFToListInt].getName}'")
checkAnswer(
sql("SELECT testUDFToListInt(s) FROM inputTable"),
Seq(Row(1 :: 2 :: 3 :: Nil)))
sql("DROP TEMPORARY FUNCTION IF EXISTS testUDFToListInt")

TestHive.reset()
}

test("UDFListListInt") {
val testData = TestHive.sparkContext.parallelize(
ListListIntCaseClass(Nil) ::
Expand Down