Repository: spark
Updated Branches:
  refs/heads/master e36ec38d8 -> 5f6943345


[SPARK-22171][SQL] Describe Table Extended Failed when Table Owner is Empty

## What changes were proposed in this pull request?

Users could hit `java.lang.NullPointerException` when the tables were created 
by Hive and the table's owner is `null` that are got from Hive metastore. `DESC 
EXTENDED` failed with the error:

> SQLExecutionException: java.lang.NullPointerException at 
> scala.collection.immutable.StringOps$.length$extension(StringOps.scala:47) at 
> scala.collection.immutable.StringOps.length(StringOps.scala:47) at 
> scala.collection.IndexedSeqOptimized$class.isEmpty(IndexedSeqOptimized.scala:27)
>  at scala.collection.immutable.StringOps.isEmpty(StringOps.scala:29) at 
> scala.collection.TraversableOnce$class.nonEmpty(TraversableOnce.scala:111) at 
> scala.collection.immutable.StringOps.nonEmpty(StringOps.scala:29) at 
> org.apache.spark.sql.catalyst.catalog.CatalogTable.toLinkedHashMap(interface.scala:300)
>  at 
> org.apache.spark.sql.execution.command.DescribeTableCommand.describeFormattedTableInfo(tables.scala:565)
>  at 
> org.apache.spark.sql.execution.command.DescribeTableCommand.run(tables.scala:543)
>  at 
> org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:66)
>  at

## How was this patch tested?
Added a unit test case

Author: gatorsmile <[email protected]>

Closes #19395 from gatorsmile/desc.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/5f694334
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/5f694334
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/5f694334

Branch: refs/heads/master
Commit: 5f694334534e4425fb9e8abf5b7e3e5efdfcef50
Parents: e36ec38
Author: gatorsmile <[email protected]>
Authored: Tue Oct 3 21:27:58 2017 -0700
Committer: gatorsmile <[email protected]>
Committed: Tue Oct 3 21:27:58 2017 -0700

----------------------------------------------------------------------
 .../spark/sql/catalyst/catalog/interface.scala  |  2 +-
 .../sql/catalyst/analysis/CatalogSuite.scala    | 37 ++++++++++++++++++++
 .../spark/sql/hive/client/HiveClientImpl.scala  |  2 +-
 3 files changed, 39 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/5f694334/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
index 1965144..fe2af91 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
@@ -307,7 +307,7 @@ case class CatalogTable(
 
     identifier.database.foreach(map.put("Database", _))
     map.put("Table", identifier.table)
-    if (owner.nonEmpty) map.put("Owner", owner)
+    if (owner != null && owner.nonEmpty) map.put("Owner", owner)
     map.put("Created Time", new Date(createTime).toString)
     map.put("Last Access", new Date(lastAccessTime).toString)
     map.put("Created By", "Spark " + createVersion)

http://git-wip-us.apache.org/repos/asf/spark/blob/5f694334/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
new file mode 100644
index 0000000..d670053
--- /dev/null
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, 
CatalogTable, CatalogTableType}
+import org.apache.spark.sql.types.StructType
+
+
+class CatalogSuite extends AnalysisTest {
+
+  test("desc table when owner is set to null") {
+    val table = CatalogTable(
+      identifier = TableIdentifier("tbl", Some("db1")),
+      tableType = CatalogTableType.MANAGED,
+      storage = CatalogStorageFormat.empty,
+      owner = null,
+      schema = new StructType().add("col1", "int").add("col2", "string"),
+      provider = Some("parquet"))
+    table.toLinkedHashMap
+  }
+}

http://git-wip-us.apache.org/repos/asf/spark/blob/5f694334/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala
----------------------------------------------------------------------
diff --git 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala
index c4e48c9..66165c7 100644
--- 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala
+++ 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala
@@ -461,7 +461,7 @@ private[hive] class HiveClientImpl(
         // in table properties. This means, if we have bucket spec in both 
hive metastore and
         // table properties, we will trust the one in table properties.
         bucketSpec = bucketSpec,
-        owner = h.getOwner,
+        owner = Option(h.getOwner).getOrElse(""),
         createTime = h.getTTable.getCreateTime.toLong * 1000,
         lastAccessTime = h.getLastAccessTime.toLong * 1000,
         storage = CatalogStorageFormat(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to