You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/05/31 11:47:17 UTC

[GitHub] [spark] srowen commented on a change in pull request #24747: [SPARK-27772][SQL][TEST] SQLTestUtils Refactoring

srowen commented on a change in pull request #24747: [SPARK-27772][SQL][TEST] SQLTestUtils Refactoring
URL: https://github.com/apache/spark/pull/24747#discussion_r289357668
 
 

 ##########
 File path: sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
 ##########
 @@ -255,59 +255,65 @@ private[sql] trait SQLTestUtilsBase
    * Drops temporary view `viewNames` after calling `f`.
    */
   protected def withTempView(viewNames: String*)(f: => Unit): Unit = {
-    try f finally {
-      // If the test failed part way, we don't want to mask the failure by failing to remove
-      // temp views that never got created.
-      try viewNames.foreach(spark.catalog.dropTempView) catch {
-        case _: NoSuchTableException =>
-      }
-    }
+    tryWithFinally(f)(viewNames.foreach(spark.catalog.dropTempView))
   }
 
   /**
    * Drops global temporary view `viewNames` after calling `f`.
    */
   protected def withGlobalTempView(viewNames: String*)(f: => Unit): Unit = {
-    try f finally {
-      // If the test failed part way, we don't want to mask the failure by failing to remove
-      // global temp views that never got created.
-      try viewNames.foreach(spark.catalog.dropGlobalTempView) catch {
-        case _: NoSuchTableException =>
-      }
-    }
+    tryWithFinally(f)(viewNames.foreach(spark.catalog.dropGlobalTempView))
   }
 
   /**
    * Drops table `tableName` after calling `f`.
    */
   protected def withTable(tableNames: String*)(f: => Unit): Unit = {
-    try f finally {
-      tableNames.foreach { name =>
-        spark.sql(s"DROP TABLE IF EXISTS $name")
-      }
-    }
+    tryWithFinally(f)(tableNames.foreach { name =>
+      spark.sql(s"DROP TABLE IF EXISTS $name")
+    })
   }
 
   /**
    * Drops view `viewName` after calling `f`.
    */
   protected def withView(viewNames: String*)(f: => Unit): Unit = {
-    try f finally {
+    tryWithFinally(f)(
       viewNames.foreach { name =>
         spark.sql(s"DROP VIEW IF EXISTS $name")
       }
-    }
+    )
   }
 
   /**
    * Drops cache `cacheName` after calling `f`.
    */
   protected def withCache(cacheNames: String*)(f: => Unit): Unit = {
-    try f finally {
-      cacheNames.foreach { cacheName =>
-        try uncacheTable(cacheName) catch {
-          case _: AnalysisException =>
+    tryWithFinally(f)(cacheNames.foreach(uncacheTable))
+  }
+
+  /**
+   * Executes the given tryBlock and then the given finallyBlock no matter whether tryBlock throws
+   * an exception. If both tryBlock and finallyBlock throw exceptions, the exception thrown
+   * from the finallyBlock with be added to the exception thrown from tryBlock as a
+   * suppress exception. It helps to avoid masking the exception from tryBlock with exception
+   * from finallyBlock
+   */
+  private def tryWithFinally(tryBlock: => Unit)(finallyBlock: => Unit): Unit = {
 
 Review comment:
   This looks like `Utils.tryWithSafeFinally`; can we use that?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org