Skip to content

Commit dd724c8

Browse files
cloud-fangatorsmile
authored andcommitted
[SPARK-18989][SQL] DESC TABLE should not fail with format class not found
## What changes were proposed in this pull request? When we describe a table, we only wanna see the information of this table, not read it, so it's ok even if the format class is not present at the classpath. ## How was this patch tested? new regression test Author: Wenchen Fan <wenchen@databricks.com> Closes #16388 from cloud-fan/hive.
1 parent 8a7db8a commit dd724c8

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,15 @@ private[hive] class HiveClientImpl(
408408
lastAccessTime = h.getLastAccessTime.toLong * 1000,
409409
storage = CatalogStorageFormat(
410410
locationUri = shim.getDataLocation(h),
411-
inputFormat = Option(h.getInputFormatClass).map(_.getName),
412-
outputFormat = Option(h.getOutputFormatClass).map(_.getName),
411+
// To avoid ClassNotFound exception, we try our best to not get the format class, but get
412+
// the class name directly. However, for non-native tables, there is no interface to get
413+
// the format class name, so we may still throw ClassNotFound in this case.
414+
inputFormat = Option(h.getTTable.getSd.getInputFormat).orElse {
415+
Option(h.getStorageHandler).map(_.getInputFormatClass.getName)
416+
},
417+
outputFormat = Option(h.getTTable.getSd.getOutputFormat).orElse {
418+
Option(h.getStorageHandler).map(_.getOutputFormatClass.getName)
419+
},
413420
serde = Option(h.getSerializationLib),
414421
compressed = h.getTTable.getSd.isCompressed,
415422
properties = Option(h.getTTable.getSd.getSerdeInfo.getParameters)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,29 @@ class HiveSparkSubmitSuite
311311
runSparkSubmit(args)
312312
}
313313

314+
test("SPARK-18989: DESC TABLE should not fail with format class not found") {
315+
val unusedJar = TestUtils.createJarWithClasses(Seq.empty)
316+
317+
val argsForCreateTable = Seq(
318+
"--class", SPARK_18989_CREATE_TABLE.getClass.getName.stripSuffix("$"),
319+
"--name", "SPARK-18947",
320+
"--master", "local-cluster[2,1,1024]",
321+
"--conf", "spark.ui.enabled=false",
322+
"--conf", "spark.master.rest.enabled=false",
323+
"--jars", TestHive.getHiveFile("hive-contrib-0.13.1.jar").getCanonicalPath,
324+
unusedJar.toString)
325+
runSparkSubmit(argsForCreateTable)
326+
327+
val argsForShowTables = Seq(
328+
"--class", SPARK_18989_DESC_TABLE.getClass.getName.stripSuffix("$"),
329+
"--name", "SPARK-18947",
330+
"--master", "local-cluster[2,1,1024]",
331+
"--conf", "spark.ui.enabled=false",
332+
"--conf", "spark.master.rest.enabled=false",
333+
unusedJar.toString)
334+
runSparkSubmit(argsForShowTables)
335+
}
336+
314337
// NOTE: This is an expensive operation in terms of time (10 seconds+). Use sparingly.
315338
// This is copied from org.apache.spark.deploy.SparkSubmitSuite
316339
private def runSparkSubmit(args: Seq[String]): Unit = {
@@ -853,3 +876,26 @@ object SPARK_18360 {
853876
}
854877
}
855878
}
879+
880+
object SPARK_18989_CREATE_TABLE {
881+
def main(args: Array[String]): Unit = {
882+
val spark = SparkSession.builder().enableHiveSupport().getOrCreate()
883+
spark.sql(
884+
"""
885+
|CREATE TABLE IF NOT EXISTS base64_tbl(val string) STORED AS
886+
|INPUTFORMAT 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'
887+
|OUTPUTFORMAT 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextOutputFormat'
888+
""".stripMargin)
889+
}
890+
}
891+
892+
object SPARK_18989_DESC_TABLE {
893+
def main(args: Array[String]): Unit = {
894+
val spark = SparkSession.builder().enableHiveSupport().getOrCreate()
895+
try {
896+
spark.sql("DESC base64_tbl")
897+
} finally {
898+
spark.sql("DROP TABLE IF EXISTS base64_tbl")
899+
}
900+
}
901+
}

0 commit comments

Comments
 (0)