Repository: spark
Updated Branches:
  refs/heads/branch-2.0 0a651aa26 -> 741801921


[SPARK-16529][SQL][TEST] `withTempDatabase` should set `default` database 
before dropping

## What changes were proposed in this pull request?

`SQLTestUtils.withTempDatabase` is a frequently used test harness to setup a 
temporary table and clean up finally. This issue improves like the following 
for usability.

```scala
-    try f(dbName) finally spark.sql(s"DROP DATABASE $dbName CASCADE")
+    try f(dbName) finally {
+      if (spark.catalog.currentDatabase == dbName) {
+        spark.sql(s"USE ${DEFAULT_DATABASE}")
+      }
+      spark.sql(s"DROP DATABASE $dbName CASCADE")
+    }
```

In case of forgetting to reset the databaes, `withTempDatabase` will not raise 
Exception.

## How was this patch tested?

This improves test harness.

Author: Dongjoon Hyun <dongj...@apache.org>

Closes #14184 from dongjoon-hyun/SPARK-16529.

(cherry picked from commit c576f9fb90853cce2e8e5dcc32a536a0f49cbbd8)
Signed-off-by: Cheng Lian <l...@databricks.com>


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

Branch: refs/heads/branch-2.0
Commit: 7418019218e5a2bd4ae948bb1984816f161925cf
Parents: 0a651aa
Author: Dongjoon Hyun <dongj...@apache.org>
Authored: Fri Jul 15 00:51:11 2016 +0800
Committer: Cheng Lian <l...@databricks.com>
Committed: Fri Jul 15 00:51:56 2016 +0800

----------------------------------------------------------------------
 .../test/scala/org/apache/spark/sql/test/SQLTestUtils.scala  | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/74180192/sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
----------------------------------------------------------------------
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
index 853dd0f..26bd3fb 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
@@ -29,6 +29,7 @@ import org.scalatest.BeforeAndAfterAll
 import org.apache.spark.SparkFunSuite
 import org.apache.spark.sql._
 import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
+import org.apache.spark.sql.catalyst.catalog.SessionCatalog.DEFAULT_DATABASE
 import org.apache.spark.sql.catalyst.FunctionIdentifier
 import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
 import org.apache.spark.sql.catalyst.util._
@@ -196,7 +197,12 @@ private[sql] trait SQLTestUtils
       fail("Failed to create temporary database", cause)
     }
 
-    try f(dbName) finally spark.sql(s"DROP DATABASE $dbName CASCADE")
+    try f(dbName) finally {
+      if (spark.catalog.currentDatabase == dbName) {
+        spark.sql(s"USE ${DEFAULT_DATABASE}")
+      }
+      spark.sql(s"DROP DATABASE $dbName CASCADE")
+    }
   }
 
   /**


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

Reply via email to