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 13:59:43 -0800
commite83f13e8d37ca33f4e183e977d077221b90c6025 (patch)
treeca4225da3ff6200063eab253c4b111435c4f862d /sql/catalyst
parentc238fb423d1011bd1b1e6201d769b72e52664fc6 (diff)
downloadspark-e83f13e8d37ca33f4e183e977d077221b90c6025.tar.gz
spark-e83f13e8d37ca33f4e183e977d077221b90c6025.tar.bz2
spark-e83f13e8d37ca33f4e183e977d077221b90c6025.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
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,