aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst
diff options
context:
space:
mode:
authorCheng Hao <hao.cheng@intel.com>2014-11-03 13:59:43 -0800
committerMichael Armbrust <michael@databricks.com>2014-11-03 14:00:06 -0800
commit6104754f711da9eb0c09daf377bcd750d2d23f8a (patch)
treede7c554f6499d18a62f4fec33cdce1b4d70620a9 /sql/catalyst
parent572300ba8a5f24b52f19d7033a456248da20bfed (diff)
downloadspark-6104754f711da9eb0c09daf377bcd750d2d23f8a.tar.gz
spark-6104754f711da9eb0c09daf377bcd750d2d23f8a.tar.bz2
spark-6104754f711da9eb0c09daf377bcd750d2d23f8a.zip
[SPARK-4152] [SQL] Avoid data change in CTAS while table already existed
CREATE TABLE t1 (a String); CREATE TABLE t1 AS SELECT key FROM src; – throw exception CREATE TABLE if not exists t1 AS SELECT key FROM src; – expect do nothing, currently it will overwrite the t1, which is incorrect. Author: Cheng Hao <hao.cheng@intel.com> Closes #3013 from chenghao-intel/ctas_unittest and squashes the following commits: 194113e [Cheng Hao] fix bug in CTAS when table already existed (cherry picked from commit e83f13e8d37ca33f4e183e977d077221b90c6025) Signed-off-by: Michael Armbrust <michael@databricks.com>
Diffstat (limited to 'sql/catalyst')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala22
1 files changed, 22 insertions, 0 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala
index 2059a91ba0..0415d74bd8 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala
@@ -28,6 +28,8 @@ trait Catalog {
def caseSensitive: Boolean
+ def tableExists(db: Option[String], tableName: String): Boolean
+
def lookupRelation(
databaseName: Option[String],
tableName: String,
@@ -82,6 +84,14 @@ class SimpleCatalog(val caseSensitive: Boolean) extends Catalog {
tables.clear()
}
+ override def tableExists(db: Option[String], tableName: String): Boolean = {
+ val (dbName, tblName) = processDatabaseAndTableName(db, tableName)
+ tables.get(tblName) match {
+ case Some(_) => true
+ case None => false
+ }
+ }
+
override def lookupRelation(
databaseName: Option[String],
tableName: String,
@@ -107,6 +117,14 @@ trait OverrideCatalog extends Catalog {
// TODO: This doesn't work when the database changes...
val overrides = new mutable.HashMap[(Option[String],String), LogicalPlan]()
+ abstract override def tableExists(db: Option[String], tableName: String): Boolean = {
+ val (dbName, tblName) = processDatabaseAndTableName(db, tableName)
+ overrides.get((dbName, tblName)) match {
+ case Some(_) => true
+ case None => super.tableExists(db, tableName)
+ }
+ }
+
abstract override def lookupRelation(
databaseName: Option[String],
tableName: String,
@@ -149,6 +167,10 @@ object EmptyCatalog extends Catalog {
val caseSensitive: Boolean = true
+ def tableExists(db: Option[String], tableName: String): Boolean = {
+ throw new UnsupportedOperationException
+ }
+
def lookupRelation(
databaseName: Option[String],
tableName: String,