Skip to content

Commit 5634fad

Browse files
vinodkcgatorsmile
authored andcommitted
[SPARK-21588][SQL] SQLContext.getConf(key, null) should return null
## What changes were proposed in this pull request? In SQLContext.get(key,null) for a key that is not defined in the conf, and doesn't have a default value defined, throws a NPE. Int happens only when conf has a value converter Added null check on defaultValue inside SQLConf.getConfString to avoid calling entry.valueConverter(defaultValue) ## How was this patch tested? Added unit test Author: vinodkc <vinod.kc.in@gmail.com> Closes #18852 from vinodkc/br_Fix_SPARK-21588. (cherry picked from commit 1ba967b) Signed-off-by: gatorsmile <gatorsmile@gmail.com>
1 parent 734b144 commit 5634fad

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,12 @@ class SQLConf extends Serializable with Logging {
917917
* not set yet, return `defaultValue`.
918918
*/
919919
def getConfString(key: String, defaultValue: String): String = {
920-
val entry = sqlConfEntries.get(key)
921-
if (entry != null && defaultValue != "<undefined>") {
922-
// Only verify configs in the SQLConf object
923-
entry.valueConverter(defaultValue)
920+
if (defaultValue != null && defaultValue != "<undefined>") {
921+
val entry = sqlConfEntries.get(key)
922+
if (entry != null) {
923+
// Only verify configs in the SQLConf object
924+
entry.valueConverter(defaultValue)
925+
}
924926
}
925927
Option(settings.get(key)).getOrElse(defaultValue)
926928
}

sql/core/src/test/scala/org/apache/spark/sql/internal/SQLConfSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,15 @@ class SQLConfSuite extends QueryTest with SharedSQLContext {
270270
val e2 = intercept[AnalysisException](spark.conf.unset(SCHEMA_STRING_LENGTH_THRESHOLD.key))
271271
assert(e2.message.contains("Cannot modify the value of a static config"))
272272
}
273+
274+
test("SPARK-21588 SQLContext.getConf(key, null) should return null") {
275+
withSQLConf(SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
276+
assert("1" == spark.conf.get(SQLConf.SHUFFLE_PARTITIONS.key, null))
277+
assert("1" == spark.conf.get(SQLConf.SHUFFLE_PARTITIONS.key, "<undefined>"))
278+
}
279+
280+
assert(spark.conf.getOption("spark.sql.nonexistent").isEmpty)
281+
assert(null == spark.conf.get("spark.sql.nonexistent", null))
282+
assert("<undefined>" == spark.conf.get("spark.sql.nonexistent", "<undefined>"))
283+
}
273284
}

0 commit comments

Comments
 (0)