spark git commit: [SPARK-16229][SQL] Drop Empty Table After CREATE TABLE AS SELECT fails

2016-07-06 Thread wenchen
Repository: spark
Updated Branches:
  refs/heads/branch-2.0 d5d2457e4 -> e956bd775


[SPARK-16229][SQL] Drop Empty Table After CREATE TABLE AS SELECT fails

 What changes were proposed in this pull request?
In `CREATE TABLE AS SELECT`, if the `SELECT` query failed, the table should not 
exist. For example,

```SQL
CREATE TABLE tab
STORED AS TEXTFILE
SELECT 1 AS a, (SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a) t) AS b
```
The above query failed as expected but an empty table `t` is created.

This PR is to drop the created table when hitting any non-fatal exception.

 How was this patch tested?
Added a test case to verify the behavior

Author: gatorsmile 

Closes #13926 from gatorsmile/dropTableAfterException.

(cherry picked from commit 21eadd1d8cbf029197e73ffca1cba54d5a890c01)
Signed-off-by: Wenchen Fan 


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/e956bd77
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/e956bd77
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/e956bd77

Branch: refs/heads/branch-2.0
Commit: e956bd7750882ce259a278e9eac7f64b4fb76286
Parents: d5d2457
Author: gatorsmile 
Authored: Wed Jul 6 21:43:55 2016 +0800
Committer: Wenchen Fan 
Committed: Wed Jul 6 21:45:30 2016 +0800

--
 .../execution/CreateHiveTableAsSelectCommand.scala   | 13 +++--
 .../spark/sql/hive/execution/HiveDDLSuite.scala  | 15 +++
 2 files changed, 26 insertions(+), 2 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/spark/blob/e956bd77/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
--
diff --git 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
index b809938..15a5d79 100644
--- 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
+++ 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
@@ -17,6 +17,8 @@
 
 package org.apache.spark.sql.hive.execution
 
+import scala.util.control.NonFatal
+
 import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
 import org.apache.spark.sql.catalyst.catalog.{CatalogColumn, CatalogTable}
 import org.apache.spark.sql.catalyst.plans.logical.{InsertIntoTable, 
LogicalPlan}
@@ -87,8 +89,15 @@ case class CreateHiveTableAsSelectCommand(
 throw new AnalysisException(s"$tableIdentifier already exists.")
   }
 } else {
-  sparkSession.sessionState.executePlan(InsertIntoTable(
-metastoreRelation, Map(), query, overwrite = true, ifNotExists = 
false)).toRdd
+  try {
+sparkSession.sessionState.executePlan(InsertIntoTable(
+  metastoreRelation, Map(), query, overwrite = true, ifNotExists = 
false)).toRdd
+  } catch {
+case NonFatal(e) =>
+  // drop the created table.
+  sparkSession.sessionState.catalog.dropTable(tableIdentifier, 
ignoreIfNotExists = true)
+  throw e
+  }
 }
 
 Seq.empty[Row]

http://git-wip-us.apache.org/repos/asf/spark/blob/e956bd77/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
--
diff --git 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
index 89f69c8..9d3c4cd 100644
--- 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
+++ 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
@@ -554,6 +554,21 @@ class HiveDDLSuite
 }
   }
 
+  test("Create Cataloged Table As Select - Drop Table After Runtime 
Exception") {
+withTable("tab") {
+  intercept[RuntimeException] {
+sql(
+  """
+|CREATE TABLE tab
+|STORED AS TEXTFILE
+|SELECT 1 AS a, (SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2 
AS a) t) AS b
+  """.stripMargin)
+  }
+  // After hitting runtime exception, we should drop the created table.
+  assert(!spark.sessionState.catalog.tableExists(TableIdentifier("tab")))
+}
+  }
+
   test("desc table for data source table") {
 withTable("tab1") {
   val tabName = "tab1"


-
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org



spark git commit: [SPARK-16229][SQL] Drop Empty Table After CREATE TABLE AS SELECT fails

2016-07-06 Thread wenchen
Repository: spark
Updated Branches:
  refs/heads/master 909c6d812 -> 21eadd1d8


[SPARK-16229][SQL] Drop Empty Table After CREATE TABLE AS SELECT fails

 What changes were proposed in this pull request?
In `CREATE TABLE AS SELECT`, if the `SELECT` query failed, the table should not 
exist. For example,

```SQL
CREATE TABLE tab
STORED AS TEXTFILE
SELECT 1 AS a, (SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a) t) AS b
```
The above query failed as expected but an empty table `t` is created.

This PR is to drop the created table when hitting any non-fatal exception.

 How was this patch tested?
Added a test case to verify the behavior

Author: gatorsmile 

Closes #13926 from gatorsmile/dropTableAfterException.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/21eadd1d
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/21eadd1d
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/21eadd1d

Branch: refs/heads/master
Commit: 21eadd1d8cbf029197e73ffca1cba54d5a890c01
Parents: 909c6d8
Author: gatorsmile 
Authored: Wed Jul 6 21:43:55 2016 +0800
Committer: Wenchen Fan 
Committed: Wed Jul 6 21:43:55 2016 +0800

--
 .../execution/CreateHiveTableAsSelectCommand.scala   | 13 +++--
 .../spark/sql/hive/execution/HiveDDLSuite.scala  | 15 +++
 2 files changed, 26 insertions(+), 2 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/spark/blob/21eadd1d/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
--
diff --git 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
index b809938..15a5d79 100644
--- 
a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
+++ 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateHiveTableAsSelectCommand.scala
@@ -17,6 +17,8 @@
 
 package org.apache.spark.sql.hive.execution
 
+import scala.util.control.NonFatal
+
 import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
 import org.apache.spark.sql.catalyst.catalog.{CatalogColumn, CatalogTable}
 import org.apache.spark.sql.catalyst.plans.logical.{InsertIntoTable, 
LogicalPlan}
@@ -87,8 +89,15 @@ case class CreateHiveTableAsSelectCommand(
 throw new AnalysisException(s"$tableIdentifier already exists.")
   }
 } else {
-  sparkSession.sessionState.executePlan(InsertIntoTable(
-metastoreRelation, Map(), query, overwrite = true, ifNotExists = 
false)).toRdd
+  try {
+sparkSession.sessionState.executePlan(InsertIntoTable(
+  metastoreRelation, Map(), query, overwrite = true, ifNotExists = 
false)).toRdd
+  } catch {
+case NonFatal(e) =>
+  // drop the created table.
+  sparkSession.sessionState.catalog.dropTable(tableIdentifier, 
ignoreIfNotExists = true)
+  throw e
+  }
 }
 
 Seq.empty[Row]

http://git-wip-us.apache.org/repos/asf/spark/blob/21eadd1d/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
--
diff --git 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
index 89f69c8..9d3c4cd 100644
--- 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
+++ 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
@@ -554,6 +554,21 @@ class HiveDDLSuite
 }
   }
 
+  test("Create Cataloged Table As Select - Drop Table After Runtime 
Exception") {
+withTable("tab") {
+  intercept[RuntimeException] {
+sql(
+  """
+|CREATE TABLE tab
+|STORED AS TEXTFILE
+|SELECT 1 AS a, (SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2 
AS a) t) AS b
+  """.stripMargin)
+  }
+  // After hitting runtime exception, we should drop the created table.
+  assert(!spark.sessionState.catalog.tableExists(TableIdentifier("tab")))
+}
+  }
+
   test("desc table for data source table") {
 withTable("tab1") {
   val tabName = "tab1"


-
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org