Skip to content

Commit 3ae37b9

Browse files
adachij2002liancheng
authored andcommitted
[SPARK-6694][SQL]SparkSQL CLI must be able to specify an option --database on the command line.
SparkSQL CLI has an option --database as follows. But, the option --database is ignored. ``` $ spark-sql --help : CLI options: : --database <databasename> Specify the database to use ``` Author: Jin Adachi <[email protected]> Author: adachij <[email protected]> Closes apache#5345 from adachij2002/SPARK-6694 and squashes the following commits: 8659084 [Jin Adachi] Merge branch 'master' of https://github.com/apache/spark into SPARK-6694 0301eb9 [Jin Adachi] Merge branch 'master' of https://github.com/apache/spark into SPARK-6694 df81086 [Jin Adachi] Modify code style. 846f83e [Jin Adachi] Merge branch 'master' of https://github.com/apache/spark into SPARK-6694 dbe8c63 [Jin Adachi] Change file permission to 644. 7b58f42 [Jin Adachi] Merge branch 'master' of https://github.com/apache/spark into SPARK-6694 c581d06 [Jin Adachi] Add an option --database test db56122 [Jin Adachi] Merge branch 'SPARK-6694' of https://github.com/adachij2002/spark into SPARK-6694 ee09fa5 [adachij] Merge branch 'master' into SPARK-6694 c804c03 [adachij] SparkSQL CLI must be able to specify an option --database on the command line.
1 parent de4fa6b commit 3ae37b9

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLCLIDriver.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ private[hive] object SparkSQLCLIDriver {
145145
case e: UnsupportedEncodingException => System.exit(3)
146146
}
147147

148+
// use the specified database if specified
149+
cli.processSelectDatabase(sessionState);
150+
148151
// Execute -i init files (always in silent mode)
149152
cli.processInitFiles(sessionState)
150153

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,31 @@ import scala.concurrent.{Await, Promise}
2525
import scala.sys.process.{Process, ProcessLogger}
2626

2727
import org.apache.hadoop.hive.conf.HiveConf.ConfVars
28-
import org.scalatest.{BeforeAndAfterAll, FunSuite}
28+
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite}
2929

3030
import org.apache.spark.Logging
3131
import org.apache.spark.util.Utils
3232

33-
class CliSuite extends FunSuite with BeforeAndAfterAll with Logging {
33+
class CliSuite extends FunSuite with BeforeAndAfter with Logging {
34+
val warehousePath = Utils.createTempDir()
35+
val metastorePath = Utils.createTempDir()
36+
37+
before {
38+
warehousePath.delete()
39+
metastorePath.delete()
40+
}
41+
42+
after {
43+
warehousePath.delete()
44+
metastorePath.delete()
45+
}
46+
3447
def runCliWithin(
3548
timeout: FiniteDuration,
3649
extraArgs: Seq[String] = Seq.empty)(
37-
queriesAndExpectedAnswers: (String, String)*) {
50+
queriesAndExpectedAnswers: (String, String)*): Unit = {
3851

3952
val (queries, expectedAnswers) = queriesAndExpectedAnswers.unzip
40-
val warehousePath = Utils.createTempDir()
41-
warehousePath.delete()
42-
val metastorePath = Utils.createTempDir()
43-
metastorePath.delete()
4453
val cliScript = "../../bin/spark-sql".split("/").mkString(File.separator)
4554

4655
val command = {
@@ -95,8 +104,6 @@ class CliSuite extends FunSuite with BeforeAndAfterAll with Logging {
95104
""".stripMargin, cause)
96105
throw cause
97106
} finally {
98-
warehousePath.delete()
99-
metastorePath.delete()
100107
process.destroy()
101108
}
102109
}
@@ -124,4 +131,24 @@ class CliSuite extends FunSuite with BeforeAndAfterAll with Logging {
124131
test("Single command with -e") {
125132
runCliWithin(1.minute, Seq("-e", "SHOW DATABASES;"))("" -> "OK")
126133
}
134+
135+
test("Single command with --database") {
136+
runCliWithin(1.minute)(
137+
"CREATE DATABASE hive_test_db;"
138+
-> "OK",
139+
"USE hive_test_db;"
140+
-> "OK",
141+
"CREATE TABLE hive_test(key INT, val STRING);"
142+
-> "OK",
143+
"SHOW TABLES;"
144+
-> "Time taken: "
145+
)
146+
147+
runCliWithin(1.minute, Seq("--database", "hive_test_db", "-e", "SHOW TABLES;"))(
148+
""
149+
-> "OK",
150+
""
151+
-> "hive_test"
152+
)
153+
}
127154
}

0 commit comments

Comments
 (0)