|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.hive |
| 19 | + |
| 20 | +import org.apache.spark.sql.{QueryTest, Row} |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression} |
| 22 | +import org.apache.spark.sql.hive.HiveShim.HiveFunctionWrapper |
| 23 | +import org.apache.spark.sql.hive.test.TestHiveSingleton |
| 24 | +import org.apache.spark.sql.test.SQLTestUtils |
| 25 | +import org.apache.spark.sql.types.{IntegerType, StringType} |
| 26 | +import org.apache.spark.util.Utils |
| 27 | + |
| 28 | +class HiveUDFDynamicLoadSuite extends QueryTest with SQLTestUtils with TestHiveSingleton { |
| 29 | + |
| 30 | + case class UDFTestInformation( |
| 31 | + identifier: String, |
| 32 | + funcName: String, |
| 33 | + className: String, |
| 34 | + fnVerifyQuery: () => Unit, |
| 35 | + fnCreateHiveUDFExpression: () => Expression) |
| 36 | + |
| 37 | + private val udfTestInfos: Seq[UDFTestInformation] = Array( |
| 38 | + // UDF |
| 39 | + // UDFExampleAdd2 is slightly modified version of UDFExampleAdd in hive/contrib, |
| 40 | + // which adds two integers or doubles. |
| 41 | + UDFTestInformation( |
| 42 | + "UDF", |
| 43 | + "udf_add2", |
| 44 | + "org.apache.hadoop.hive.contrib.udf.example.UDFExampleAdd2", |
| 45 | + () => { |
| 46 | + checkAnswer(sql("SELECT udf_add2(1, 2)"), Row(3) :: Nil) |
| 47 | + }, |
| 48 | + () => { |
| 49 | + HiveSimpleUDF( |
| 50 | + "default.udf_add2", |
| 51 | + HiveFunctionWrapper("org.apache.hadoop.hive.contrib.udf.example.UDFExampleAdd2"), |
| 52 | + Array( |
| 53 | + AttributeReference("a", IntegerType, nullable = false)(), |
| 54 | + AttributeReference("b", IntegerType, nullable = false)())) |
| 55 | + }), |
| 56 | + |
| 57 | + // GenericUDF |
| 58 | + // GenericUDFTrim2 is cloned version of GenericUDFTrim in hive/contrib. |
| 59 | + UDFTestInformation( |
| 60 | + "GENERIC_UDF", |
| 61 | + "generic_udf_trim2", |
| 62 | + "org.apache.hadoop.hive.contrib.udf.example.GenericUDFTrim2", |
| 63 | + () => { |
| 64 | + checkAnswer(sql("SELECT generic_udf_trim2(' hello ')"), Row("hello") :: Nil) |
| 65 | + }, |
| 66 | + () => { |
| 67 | + HiveGenericUDF( |
| 68 | + "default.generic_udf_trim2", |
| 69 | + HiveFunctionWrapper("org.apache.hadoop.hive.contrib.udf.example.GenericUDFTrim2"), |
| 70 | + Array(AttributeReference("a", StringType, nullable = false)()) |
| 71 | + ) |
| 72 | + } |
| 73 | + ), |
| 74 | + |
| 75 | + // AbstractGenericUDAFResolver |
| 76 | + // GenericUDAFSum2 is cloned version of GenericUDAFSum in hive/exec. |
| 77 | + UDFTestInformation( |
| 78 | + "GENERIC_UDAF", |
| 79 | + "generic_udaf_sum2", |
| 80 | + "org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum2", |
| 81 | + () => { |
| 82 | + import spark.implicits._ |
| 83 | + val df = Seq((0: Integer) -> 0, (1: Integer) -> 1, (2: Integer) -> 2, (3: Integer) -> 3) |
| 84 | + .toDF("key", "value").createOrReplaceTempView("t") |
| 85 | + checkAnswer(sql("SELECT generic_udaf_sum2(value) FROM t GROUP BY key % 2"), |
| 86 | + Row(2) :: Row(4) :: Nil) |
| 87 | + }, |
| 88 | + () => { |
| 89 | + HiveUDAFFunction( |
| 90 | + "default.generic_udaf_sum2", |
| 91 | + HiveFunctionWrapper("org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum2"), |
| 92 | + Array(AttributeReference("a", IntegerType, nullable = false)()) |
| 93 | + ) |
| 94 | + } |
| 95 | + ), |
| 96 | + |
| 97 | + // UDAF |
| 98 | + // UDAFExampleMax2 is cloned version of UDAFExampleMax in hive/contrib. |
| 99 | + UDFTestInformation( |
| 100 | + "UDAF", |
| 101 | + "udaf_max2", |
| 102 | + "org.apache.hadoop.hive.contrib.udaf.example.UDAFExampleMax2", |
| 103 | + () => { |
| 104 | + import spark.implicits._ |
| 105 | + val df = Seq((0: Integer) -> 0, (1: Integer) -> 1, (2: Integer) -> 2, (3: Integer) -> 3) |
| 106 | + .toDF("key", "value").createOrReplaceTempView("t") |
| 107 | + checkAnswer(sql("SELECT udaf_max2(value) FROM t GROUP BY key % 2"), |
| 108 | + Row(2) :: Row(3) :: Nil) |
| 109 | + }, |
| 110 | + () => { |
| 111 | + HiveUDAFFunction( |
| 112 | + "default.udaf_max2", |
| 113 | + HiveFunctionWrapper("org.apache.hadoop.hive.contrib.udaf.example.UDAFExampleMax2"), |
| 114 | + Array(AttributeReference("a", IntegerType, nullable = false)()), |
| 115 | + isUDAFBridgeRequired = true |
| 116 | + ) |
| 117 | + } |
| 118 | + ), |
| 119 | + |
| 120 | + // GenericUDTF |
| 121 | + // GenericUDTFCount3 is slightly modified version of GenericUDTFCount2 in hive/contrib, |
| 122 | + // which emits the count for three times. |
| 123 | + UDFTestInformation( |
| 124 | + "GENERIC_UDTF", |
| 125 | + "udtf_count3", |
| 126 | + "org.apache.hadoop.hive.contrib.udtf.example.GenericUDTFCount3", |
| 127 | + () => { |
| 128 | + checkAnswer( |
| 129 | + sql("SELECT udtf_count3(a) FROM (SELECT 1 AS a FROM src LIMIT 3) t"), |
| 130 | + Row(3) :: Row(3) :: Row(3) :: Nil) |
| 131 | + }, |
| 132 | + () => { |
| 133 | + HiveGenericUDTF( |
| 134 | + "default.udtf_count3", |
| 135 | + HiveFunctionWrapper("org.apache.hadoop.hive.contrib.udtf.example.GenericUDTFCount3"), |
| 136 | + Array.empty[Expression] |
| 137 | + ) |
| 138 | + } |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + udfTestInfos.foreach { udfInfo => |
| 143 | + // The test jars are built from below commit: |
| 144 | + // https://github.com/HeartSaVioR/hive/commit/12f3f036b6efd0299cd1d457c0c0a65e0fd7e5f2 |
| 145 | + // which contain new UDF classes to be dynamically loaded and tested via Spark. |
| 146 | + |
| 147 | + // This jar file should not be placed to the classpath. |
| 148 | + val jarPath = "src/test/noclasspath/hive-test-udfs.jar" |
| 149 | + val jarUrl = s"file://${System.getProperty("user.dir")}/$jarPath" |
| 150 | + |
| 151 | + test("Spark should be able to run Hive UDF using jar regardless of " + |
| 152 | + s"current thread context classloader (${udfInfo.identifier}") { |
| 153 | + Utils.withContextClassLoader(Utils.getSparkClassLoader) { |
| 154 | + withUserDefinedFunction(udfInfo.funcName -> false) { |
| 155 | + val sparkClassLoader = Thread.currentThread().getContextClassLoader |
| 156 | + |
| 157 | + sql(s"CREATE FUNCTION ${udfInfo.funcName} AS '${udfInfo.className}' USING JAR '$jarUrl'") |
| 158 | + |
| 159 | + assert(Thread.currentThread().getContextClassLoader eq sparkClassLoader) |
| 160 | + |
| 161 | + // JAR will be loaded at first usage, and it will change the current thread's |
| 162 | + // context classloader to jar classloader in sharedState. |
| 163 | + // See SessionState.addJar for details. |
| 164 | + udfInfo.fnVerifyQuery() |
| 165 | + |
| 166 | + assert(Thread.currentThread().getContextClassLoader ne sparkClassLoader) |
| 167 | + assert(Thread.currentThread().getContextClassLoader eq |
| 168 | + spark.sqlContext.sharedState.jarClassLoader) |
| 169 | + |
| 170 | + val udfExpr = udfInfo.fnCreateHiveUDFExpression() |
| 171 | + // force initializing - this is what we do in HiveSessionCatalog |
| 172 | + udfExpr.dataType |
| 173 | + |
| 174 | + // Roll back to the original classloader and run query again. Without this line, the test |
| 175 | + // would pass, as thread's context classloader is changed to jar classloader. But thread |
| 176 | + // context classloader can be changed from others as well which would fail the query; one |
| 177 | + // example is spark-shell, which thread context classloader rolls back automatically. This |
| 178 | + // mimics the behavior of spark-shell. |
| 179 | + Thread.currentThread().setContextClassLoader(sparkClassLoader) |
| 180 | + |
| 181 | + udfInfo.fnVerifyQuery() |
| 182 | + |
| 183 | + val newExpr = udfExpr.makeCopy(udfExpr.productIterator.map(_.asInstanceOf[AnyRef]) |
| 184 | + .toArray) |
| 185 | + newExpr.dataType |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments