Skip to content

Commit a91cbd3

Browse files
hvanhovellyhuai
authored andcommitted
[SPARK-19218][SC-6086] SET -v should not cause a NPE when a configuration uses NULL as its default value
This is a backport of apache@c4a6519 #### What changes were proposed in this pull request? This PR aims to fix the following two things: 1. `sql("SET -v").collect()` or `sql("SET -v").show()` throws a NullPointerException when a String configuration with default value `null` has been defined. 2. Currently, `SET` and `SET -v` commands show unsorted result. This PR sorts the result. ## How was this patch tested? Added a regression test to SQLQuerySuite. Author: Herman van Hovell <[email protected]> Author: Dongjoon Hyun <[email protected]> Closes apache#265 from hvanhovell/SPARK-19218.
1 parent 964256e commit a91cbd3

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/command/SetCommand.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,17 @@ case class SetCommand(kv: Option[(String, Option[String])]) extends RunnableComm
7979
// Queries all key-value pairs that are set in the SQLConf of the sparkSession.
8080
case None =>
8181
val runFunc = (sparkSession: SparkSession) => {
82-
sparkSession.conf.getAll.map { case (k, v) => Row(k, v) }.toSeq
82+
sparkSession.conf.getAll.toSeq.sorted.map { case (k, v) => Row(k, v) }
8383
}
8484
(keyValueOutput, runFunc)
8585

8686
// Queries all properties along with their default values and docs that are defined in the
8787
// SQLConf of the sparkSession.
8888
case Some(("-v", None)) =>
8989
val runFunc = (sparkSession: SparkSession) => {
90-
sparkSession.sessionState.conf.getAllDefinedConfs.map { case (key, defaultValue, doc) =>
91-
Row(key, defaultValue, doc)
90+
sparkSession.sessionState.conf.getAllDefinedConfs.sorted.map {
91+
case (key, defaultValue, doc) =>
92+
Row(key, Option(defaultValue).getOrElse("<undefined>"), doc)
9293
}
9394
}
9495
val schema = StructType(

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,33 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
985985
spark.sessionState.conf.clear()
986986
}
987987

988+
test("SPARK-19218 SET command should show a result in a sorted order") {
989+
val overrideConfs = sql("SET").collect()
990+
sql(s"SET test.key3=1")
991+
sql(s"SET test.key2=2")
992+
sql(s"SET test.key1=3")
993+
val result = sql("SET").collect()
994+
assert(result ===
995+
(overrideConfs ++ Seq(
996+
Row("test.key1", "3"),
997+
Row("test.key2", "2"),
998+
Row("test.key3", "1"))).sortBy(_.getString(0))
999+
)
1000+
spark.sessionState.conf.clear()
1001+
}
1002+
1003+
test("SPARK-19218 `SET -v` should not fail with null value configuration") {
1004+
import SQLConf._
1005+
val confEntry = buildConf("spark.test").doc("doc").stringConf.createWithDefault(null)
1006+
1007+
try {
1008+
val result = sql("SET -v").collect()
1009+
assert(result === result.sortBy(_.getString(0)))
1010+
} finally {
1011+
SQLConf.unregister(confEntry)
1012+
}
1013+
}
1014+
9881015
test("SET commands with illegal or inappropriate argument") {
9891016
spark.sessionState.conf.clear()
9901017
// Set negative mapred.reduce.tasks for automatically determining

0 commit comments

Comments
 (0)