Skip to content

Commit 150f6a8

Browse files
lianchengdavies
authored andcommitted
[SPARK-11595] [SQL] Fixes ADD JAR when the input path contains URL scheme
Author: Cheng Lian <lian@databricks.com> Closes #9569 from liancheng/spark-11595.fix-add-jar.
1 parent f138cb8 commit 150f6a8

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2Suites.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.apache.thrift.transport.TSocket
4141
import org.scalatest.BeforeAndAfterAll
4242

4343
import org.apache.spark.sql.hive.HiveContext
44+
import org.apache.spark.sql.hive.test.TestHive
4445
import org.apache.spark.sql.test.ProcessTestUtils.ProcessOutputCapturer
4546
import org.apache.spark.util.Utils
4647
import org.apache.spark.{Logging, SparkFunSuite}

sql/hive/src/main/scala/org/apache/spark/sql/hive/client/ClientWrapper.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package org.apache.spark.sql.hive.client
1919

2020
import java.io.{File, PrintStream}
2121
import java.util.{Map => JMap}
22-
import javax.annotation.concurrent.GuardedBy
2322

2423
import scala.collection.JavaConverters._
2524
import scala.language.reflectiveCalls
@@ -548,7 +547,15 @@ private[hive] class ClientWrapper(
548547
}
549548

550549
def addJar(path: String): Unit = {
551-
clientLoader.addJar(path)
550+
val uri = new Path(path).toUri
551+
val jarURL = if (uri.getScheme == null) {
552+
// `path` is a local file path without a URL scheme
553+
new File(path).toURI.toURL
554+
} else {
555+
// `path` is a URL with a scheme
556+
uri.toURL
557+
}
558+
clientLoader.addJar(jarURL)
552559
runSqlHive(s"ADD JAR $path")
553560
}
554561

sql/hive/src/main/scala/org/apache/spark/sql/hive/client/IsolatedClientLoader.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ import java.lang.reflect.InvocationTargetException
2222
import java.net.{URL, URLClassLoader}
2323
import java.util
2424

25-
import scala.collection.mutable
2625
import scala.language.reflectiveCalls
2726
import scala.util.Try
2827

2928
import org.apache.commons.io.{FileUtils, IOUtils}
3029

3130
import org.apache.spark.Logging
3231
import org.apache.spark.deploy.SparkSubmitUtils
33-
import org.apache.spark.util.{MutableURLClassLoader, Utils}
34-
3532
import org.apache.spark.sql.catalyst.util.quietly
3633
import org.apache.spark.sql.hive.HiveContext
34+
import org.apache.spark.util.{MutableURLClassLoader, Utils}
3735

3836
/** Factory for `IsolatedClientLoader` with specific versions of hive. */
3937
private[hive] object IsolatedClientLoader {
@@ -190,9 +188,8 @@ private[hive] class IsolatedClientLoader(
190188
new NonClosableMutableURLClassLoader(isolatedClassLoader)
191189
}
192190

193-
private[hive] def addJar(path: String): Unit = synchronized {
194-
val jarURL = new java.io.File(path).toURI.toURL
195-
classLoader.addURL(jarURL)
191+
private[hive] def addJar(path: URL): Unit = synchronized {
192+
classLoader.addURL(path)
196193
}
197194

198195
/** The isolated client interface to Hive. */

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class HiveQuerySuite extends HiveComparisonTest with BeforeAndAfter {
927927
test("SPARK-2263: Insert Map<K, V> values") {
928928
sql("CREATE TABLE m(value MAP<INT, STRING>)")
929929
sql("INSERT OVERWRITE TABLE m SELECT MAP(key, value) FROM src LIMIT 10")
930-
sql("SELECT * FROM m").collect().zip(sql("SELECT * FROM src LIMIT 10").collect()).map {
930+
sql("SELECT * FROM m").collect().zip(sql("SELECT * FROM src LIMIT 10").collect()).foreach {
931931
case (Row(map: Map[_, _]), Row(key: Int, value: String)) =>
932932
assert(map.size === 1)
933933
assert(map.head === (key, value))
@@ -961,10 +961,12 @@ class HiveQuerySuite extends HiveComparisonTest with BeforeAndAfter {
961961

962962
test("CREATE TEMPORARY FUNCTION") {
963963
val funcJar = TestHive.getHiveFile("TestUDTF.jar").getCanonicalPath
964-
sql(s"ADD JAR $funcJar")
964+
val jarURL = s"file://$funcJar"
965+
sql(s"ADD JAR $jarURL")
965966
sql(
966967
"""CREATE TEMPORARY FUNCTION udtf_count2 AS
967-
| 'org.apache.spark.sql.hive.execution.GenericUDTFCount2'""".stripMargin)
968+
|'org.apache.spark.sql.hive.execution.GenericUDTFCount2'
969+
""".stripMargin)
968970
assert(sql("DESCRIBE FUNCTION udtf_count2").count > 1)
969971
sql("DROP TEMPORARY FUNCTION udtf_count2")
970972
}

0 commit comments

Comments
 (0)