Skip to content

Commit acab4e7

Browse files
gatorsmilecloud-fan
authored andcommitted
[SPARK-23001][SQL] Fix NullPointerException when DESC a database with NULL description
## What changes were proposed in this pull request? When users' DB description is NULL, users might hit `NullPointerException`. This PR is to fix the issue. ## How was this patch tested? Added test cases Author: gatorsmile <[email protected]> Closes #20215 from gatorsmile/SPARK-23001. (cherry picked from commit 87c98de) Signed-off-by: Wenchen Fan <[email protected]>
1 parent 0d943d9 commit acab4e7

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private[hive] class HiveClientImpl(
348348
Option(client.getDatabase(dbName)).map { d =>
349349
CatalogDatabase(
350350
name = d.getName,
351-
description = d.getDescription,
351+
description = Option(d.getDescription).getOrElse(""),
352352
locationUri = CatalogUtils.stringToURI(d.getLocationUri),
353353
properties = Option(d.getParameters).map(_.asScala.toMap).orNull)
354354
}.getOrElse(throw new NoSuchDatabaseException(dbName))

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveExternalCatalogSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ class HiveExternalCatalogSuite extends ExternalCatalogSuite {
107107
.filter(_.contains("Num Buckets")).head
108108
assert(bucketString.contains("10"))
109109
}
110+
111+
test("SPARK-23001: NullPointerException when running desc database") {
112+
val catalog = newBasicCatalog()
113+
catalog.createDatabase(newDb("dbWithNullDesc").copy(description = null), ignoreIfExists = false)
114+
assert(catalog.getDatabase("dbWithNullDesc").description == "")
115+
}
110116
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/client/VersionsSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ class VersionsSuite extends SparkFunSuite with Logging {
164164
client.createDatabase(tempDB, ignoreIfExists = true)
165165
}
166166

167+
test(s"$version: createDatabase with null description") {
168+
withTempDir { tmpDir =>
169+
val dbWithNullDesc =
170+
CatalogDatabase("dbWithNullDesc", description = null, tmpDir.toURI, Map())
171+
client.createDatabase(dbWithNullDesc, ignoreIfExists = true)
172+
assert(client.getDatabase("dbWithNullDesc").description == "")
173+
}
174+
}
175+
167176
test(s"$version: setCurrentDatabase") {
168177
client.setCurrentDatabase("default")
169178
}

0 commit comments

Comments
 (0)