Skip to content

Commit ee575f1

Browse files
lianchengmarmbrus
authored andcommitted
[SPARK-2219][SQL] Added support for the "add jar" command
Adds logical and physical command classes for the "add jar" command. Note that this PR conflicts with and should be merged after apache#2215. Author: Cheng Lian <[email protected]> Closes apache#2242 from liancheng/add-jar and squashes the following commits: e43a2f1 [Cheng Lian] Updates AddJar according to conventions introduced in apache#2215 b99107f [Cheng Lian] Added test case for ADD JAR command 095b2c7 [Cheng Lian] Also forward ADD JAR command to Hive 9be031b [Cheng Lian] Trims Jar path string 8195056 [Cheng Lian] Added support for the "add jar" command
1 parent 3eb6ef3 commit ee575f1

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ private[hive] case class SourceCommand(filePath: String) extends Command
4444

4545
private[hive] case class AddFile(filePath: String) extends Command
4646

47+
private[hive] case class AddJar(path: String) extends Command
48+
4749
private[hive] case class DropTable(tableName: String, ifExists: Boolean) extends Command
4850

4951
private[hive] case class AnalyzeTable(tableName: String) extends Command
@@ -231,7 +233,7 @@ private[hive] object HiveQl {
231233
} else if (sql.trim.toLowerCase.startsWith("uncache table")) {
232234
CacheCommand(sql.trim.drop(14).trim, false)
233235
} else if (sql.trim.toLowerCase.startsWith("add jar")) {
234-
NativeCommand(sql)
236+
AddJar(sql.trim.drop(8).trim)
235237
} else if (sql.trim.toLowerCase.startsWith("add file")) {
236238
AddFile(sql.trim.drop(9))
237239
} else if (sql.trim.toLowerCase.startsWith("dfs")) {
@@ -1018,9 +1020,9 @@ private[hive] object HiveQl {
10181020

10191021
/* Other functions */
10201022
case Token("TOK_FUNCTION", Token(RAND(), Nil) :: Nil) => Rand
1021-
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: Nil) =>
1023+
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: Nil) =>
10221024
Substring(nodeToExpr(string), nodeToExpr(pos), Literal(Integer.MAX_VALUE, IntegerType))
1023-
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: length :: Nil) =>
1025+
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: length :: Nil) =>
10241026
Substring(nodeToExpr(string), nodeToExpr(pos), nodeToExpr(length))
10251027

10261028
/* UDFs - Must be last otherwise will preempt built in functions */

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveStrategies.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ private[hive] trait HiveStrategies {
195195

196196
case class HiveCommandStrategy(context: HiveContext) extends Strategy {
197197
def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
198-
case logical.NativeCommand(sql) =>
199-
NativeCommand(sql, plan.output)(context) :: Nil
198+
case logical.NativeCommand(sql) => NativeCommand(sql, plan.output)(context) :: Nil
200199

201200
case hive.DropTable(tableName, ifExists) => execution.DropTable(tableName, ifExists) :: Nil
202201

202+
case hive.AddJar(path) => execution.AddJar(path) :: Nil
203+
203204
case hive.AnalyzeTable(tableName) => execution.AnalyzeTable(tableName) :: Nil
204205

205206
case describe: logical.DescribeCommand =>

sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/commands.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,19 @@ case class DropTable(tableName: String, ifExists: Boolean) extends LeafNode with
6060
Seq.empty[Row]
6161
}
6262
}
63+
64+
/**
65+
* :: DeveloperApi ::
66+
*/
67+
@DeveloperApi
68+
case class AddJar(path: String) extends LeafNode with Command {
69+
def hiveContext = sqlContext.asInstanceOf[HiveContext]
70+
71+
override def output = Seq.empty
72+
73+
override protected[sql] lazy val sideEffectResult: Seq[Row] = {
74+
hiveContext.runSqlHive(s"ADD JAR $path")
75+
hiveContext.sparkContext.addJar(path)
76+
Seq.empty[Row]
77+
}
78+
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.apache.spark.sql.hive.execution
1919

20+
import java.io.File
21+
2022
import scala.util.Try
2123

22-
import org.apache.spark.sql.{SchemaRDD, Row}
24+
import org.apache.spark.SparkException
2325
import org.apache.spark.sql.hive._
2426
import org.apache.spark.sql.hive.test.TestHive
2527
import org.apache.spark.sql.hive.test.TestHive._
@@ -313,7 +315,7 @@ class HiveQuerySuite extends HiveComparisonTest {
313315
"SELECT srcalias.KEY, SRCALIAS.value FROM sRc SrCAlias WHERE SrCAlias.kEy < 15")
314316

315317
test("case sensitivity: registered table") {
316-
val testData: SchemaRDD =
318+
val testData =
317319
TestHive.sparkContext.parallelize(
318320
TestData(1, "str1") ::
319321
TestData(2, "str2") :: Nil)
@@ -467,7 +469,7 @@ class HiveQuerySuite extends HiveComparisonTest {
467469
}
468470

469471
// Describe a registered temporary table.
470-
val testData: SchemaRDD =
472+
val testData =
471473
TestHive.sparkContext.parallelize(
472474
TestData(1, "str1") ::
473475
TestData(1, "str2") :: Nil)
@@ -495,6 +497,23 @@ class HiveQuerySuite extends HiveComparisonTest {
495497
}
496498
}
497499

500+
test("ADD JAR command") {
501+
val testJar = TestHive.getHiveFile("data/files/TestSerDe.jar").getCanonicalPath
502+
sql("CREATE TABLE alter1(a INT, b INT)")
503+
intercept[Exception] {
504+
sql(
505+
"""ALTER TABLE alter1 SET SERDE 'org.apache.hadoop.hive.serde2.TestSerDe'
506+
|WITH serdeproperties('s1'='9')
507+
""".stripMargin)
508+
}
509+
sql(s"ADD JAR $testJar")
510+
sql(
511+
"""ALTER TABLE alter1 SET SERDE 'org.apache.hadoop.hive.serde2.TestSerDe'
512+
|WITH serdeproperties('s1'='9')
513+
""".stripMargin)
514+
sql("DROP TABLE alter1")
515+
}
516+
498517
test("parse HQL set commands") {
499518
// Adapted from its SQL counterpart.
500519
val testKey = "spark.sql.key.usedfortestonly"

0 commit comments

Comments
 (0)