[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 <gatorsmile@gmail.com>

Closes #20215 from gatorsmile/SPARK-23001.
This commit is contained in:
gatorsmile 2018-01-11 18:17:34 +08:00 committed by Wenchen Fan
parent a6647ffbf7
commit 87c98de8b2
3 changed files with 16 additions and 1 deletions

View file

@ -330,7 +330,7 @@ private[hive] class HiveClientImpl(
Option(client.getDatabase(dbName)).map { d => Option(client.getDatabase(dbName)).map { d =>
CatalogDatabase( CatalogDatabase(
name = d.getName, name = d.getName,
description = d.getDescription, description = Option(d.getDescription).getOrElse(""),
locationUri = CatalogUtils.stringToURI(d.getLocationUri), locationUri = CatalogUtils.stringToURI(d.getLocationUri),
properties = Option(d.getParameters).map(_.asScala.toMap).orNull) properties = Option(d.getParameters).map(_.asScala.toMap).orNull)
}.getOrElse(throw new NoSuchDatabaseException(dbName)) }.getOrElse(throw new NoSuchDatabaseException(dbName))

View file

@ -107,4 +107,10 @@ class HiveExternalCatalogSuite extends ExternalCatalogSuite {
.filter(_.contains("Num Buckets")).head .filter(_.contains("Num Buckets")).head
assert(bucketString.contains("10")) assert(bucketString.contains("10"))
} }
test("SPARK-23001: NullPointerException when running desc database") {
val catalog = newBasicCatalog()
catalog.createDatabase(newDb("dbWithNullDesc").copy(description = null), ignoreIfExists = false)
assert(catalog.getDatabase("dbWithNullDesc").description == "")
}
} }

View file

@ -163,6 +163,15 @@ class VersionsSuite extends SparkFunSuite with Logging {
client.createDatabase(tempDB, ignoreIfExists = true) client.createDatabase(tempDB, ignoreIfExists = true)
} }
test(s"$version: createDatabase with null description") {
withTempDir { tmpDir =>
val dbWithNullDesc =
CatalogDatabase("dbWithNullDesc", description = null, tmpDir.toURI, Map())
client.createDatabase(dbWithNullDesc, ignoreIfExists = true)
assert(client.getDatabase("dbWithNullDesc").description == "")
}
}
test(s"$version: setCurrentDatabase") { test(s"$version: setCurrentDatabase") {
client.setCurrentDatabase("default") client.setCurrentDatabase("default")
} }