Skip to content

Commit

Permalink
drop table under specific database
Browse files Browse the repository at this point in the history
  • Loading branch information
baishuo committed Jun 23, 2015
1 parent c4d2343 commit 2bc3e7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ case class DropTable(
override def run(sqlContext: SQLContext): Seq[InternalRow] = {
val hiveContext = sqlContext.asInstanceOf[HiveContext]
val ifExistsClause = if (ifExists) "IF EXISTS " else ""
val databaseName = hiveContext.catalog.client.currentDatabase
try {
hiveContext.cacheManager.tryUncacheQuery(hiveContext.table(tableName))
} catch {
Expand All @@ -68,8 +69,8 @@ case class DropTable(
case e: Throwable => log.warn(s"${e.getMessage}", e)
}
hiveContext.invalidateTable(tableName)
hiveContext.runSqlHive(s"DROP TABLE $ifExistsClause$tableName")
hiveContext.catalog.unregisterTable(Seq(tableName))
hiveContext.runSqlHive(s"DROP TABLE $ifExistsClause$databaseName.$tableName")
hiveContext.catalog.unregisterTable(Seq(databaseName, tableName))
Seq.empty[InternalRow]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,22 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with BeforeA
sqlContext.sql("""use default""")
sqlContext.sql("""drop database if exists testdb8156 CASCADE""")
}

test("SPARK-8561:drop table under specific database ") {

val df = (1 to 3).map(i => (i, s"val_$i", i * 2)).toDF("a", "b", "c")
sqlContext.sql("""create database if not exists testdb8561""")
sqlContext.sql("""use testdb8561""")
df.write
.format("parquet")
.mode(SaveMode.Overwrite)
.saveAsTable("testdb8561_tbl1")

assert(sqlContext.sql("show TABLES in testdb8561").collect().size === 1)
sqlContext.sql("drop TABLE testdb8561_tbl1")
assert(sqlContext.sql("show TABLES in testdb8561").collect().size === 0)

sqlContext.sql("""use default""")
sqlContext.sql("""drop database if exists testdb8561 CASCADE""")
}
}

0 comments on commit 2bc3e7a

Please sign in to comment.