cloud-fan commented on a change in pull request #27025: [SPARK-26560][SQL]
Spark should be able to run Hive UDF using jar regardless of current thread
context classloader
URL: https://github.com/apache/spark/pull/27025#discussion_r361947939
##########
File path:
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
##########
@@ -2491,4 +2492,47 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils
with TestHiveSingleton {
}
}
}
+
+ test("SPARK-26560 Spark should be able to run Hive UDF using jar regardless
of " +
+ "current thread context classloader") {
+ // force to use Spark classloader as other test (even in other test
suites) may change the
+ // current thread's context classloader to jar classloader
+ Utils.withContextClassLoader(Utils.getSparkClassLoader) {
+ withUserDefinedFunction("udtf_count3" -> false) {
+ val sparkClassLoader = Thread.currentThread().getContextClassLoader
+
+ // This jar file should not be placed to the classpath;
GenericUDTFCount3 is slightly
+ // modified version of GenericUDTFCount2 in hive/contrib, which emits
the count for
+ // three times.
+ val jarPath = "src/test/noclasspath/TestUDTF-spark-26560.jar"
+ val jarURL = s"file://${System.getProperty("user.dir")}/$jarPath"
+
+ sql(
+ s"""
+ |CREATE FUNCTION udtf_count3
+ |AS
'org.apache.hadoop.hive.contrib.udtf.example.GenericUDTFCount3'
+ |USING JAR '$jarURL'
+ """.stripMargin)
+
+ assert(Thread.currentThread().getContextClassLoader eq
sparkClassLoader)
+
+ // JAR will be loaded at first usage, and it will change the current
thread's
+ // context classloader to jar classloader in sharedState.
+ // See SessionState.addJar for details.
+ checkAnswer(
+ sql("SELECT udtf_count3(a) FROM (SELECT 1 AS a FROM src LIMIT 3) t"),
+ Row(3) :: Row(3) :: Row(3) :: Nil)
+
+ assert(Thread.currentThread().getContextClassLoader ne
sparkClassLoader)
+ assert(Thread.currentThread().getContextClassLoader eq
+ spark.sqlContext.sharedState.jarClassLoader)
+
+ // Roll back to the original classloader and run query again.
+ Thread.currentThread().setContextClassLoader(sparkClassLoader)
Review comment:
got it. Can we add some comments to explain it?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]