Skip to content

Commit ce35aa0

Browse files
committed
add tableExists in HiveClient
1 parent 8aadc95 commit ce35aa0

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,7 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
455455
}
456456

457457
override def tableExists(db: String, table: String): Boolean = withClient {
458-
try {
459-
client.getTableOption(db, table).isDefined
460-
} catch {
461-
case e: AnalysisException if e.message.contains("Hive index table is not supported") => true
462-
}
458+
client.tableExists(db, table)
463459
}
464460

465461
override def listTables(db: String): Seq[String] = withClient {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ private[hive] trait HiveClient {
6868
/** List the names of all the databases that match the specified pattern. */
6969
def listDatabases(pattern: String): Seq[String]
7070

71+
/** Return whether a table/view with the specified name exists. */
72+
def tableExists(dbName: String, tableName: String): Boolean
73+
7174
/** Returns the specified table, or throws [[NoSuchTableException]]. */
7275
final def getTable(dbName: String, tableName: String): CatalogTable = {
7376
getTableOption(dbName, tableName).getOrElse(throw new NoSuchTableException(dbName, tableName))

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ private[hive] class HiveClientImpl(
347347
client.getDatabasesByPattern(pattern).asScala
348348
}
349349

350+
override def tableExists(dbName: String, tableName: String): Boolean = withHiveState {
351+
Option(client.getTable(dbName, tableName, false /* do not throw exception */)).nonEmpty
352+
}
353+
350354
override def getTableOption(
351355
dbName: String,
352356
tableName: String): Option[CatalogTable] = withHiveState {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ class VersionsSuite extends SparkFunSuite with Logging {
218218
holdDDLTime = false)
219219
}
220220

221+
test(s"$version: tableExists") {
222+
// No exception should be thrown
223+
assert(client.tableExists("default", "src"))
224+
assert(!client.tableExists("default", "nonexistent"))
225+
}
226+
221227
test(s"$version: getTable") {
222228
// No exception should be thrown
223229
client.getTable("default", "src")

0 commit comments

Comments
 (0)