[ 
https://issues.apache.org/jira/browse/SPARK-27772?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

William Wong updated SPARK-27772:
---------------------------------
    Description: 
{code:java}
// code placeholder
{code}
The current `SQLTestUtils` created many `withXXX` utility functions to clean up 
tables/views/caches created for testing purpose. 

Some of those `withXXX` functions would ignore certain exceptions, like 
`NoSuchTableException` in the clean up block (finally block). 

```
 /**
 * Drops temporary view `viewNames` after calling `f`.
 */
 protected def withTempView(viewNames: String*)(f: => Unit): Unit = {
   try f finally
Unknown macro: \{    // 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 =>     }  }
}

```

Maybe it is not the best option. If a test hit an exception in the 'f' closure, 
no matter what exception we hit in the finally block, we should not mask that 
exception with any other exception hit in the finally block. The exception 
caught in the finally block should be reattached to the original exception as a 
suppressed exception. The idea is similar to how java handle 
'try-with-resources' statement. 

 

A proposed to create following function to standardise those 'withXXX' 
functions. 

```

/**
 * 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 withFinallyBlock(tryBlock: => Unit)(finallyBlock: => Unit): Unit = 
{
   var fromTryBlock : Throwable = null
   try tryBlock catch \{     case cause : Throwable =>       fromTryBlock = 
cause       throw cause     }
finally {
       if (fromTryBlock != null)
Unknown macro: \{        try finallyBlock catch {           case 
fromFinallyBlock : Throwable =>             
fromTryBlock.addSuppressed(fromFinallyBlock)             throw fromTryBlock     
     }      }
else
{         finallyBlock       }
  }
 }

```

 

  was:
The current `SQLTestUtils` created many `withXXX` utility functions to clean up 
tables/views/caches created for testing purpose. 

Some of those `withXXX` functions would ignore certain exceptions, like 
`NoSuchTableException` in the clean up block (finally block). 

```
/**
 * 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 =>
    }
  }
}

```

Maybe it is not the best option. If a test hit an exception in the 'f' closure, 
no matter what exception we hit in the finally block, we should not mask that 
exception with any other exception hit in the finally block. The exception 
caught in the finally block should be reattached to the original exception as a 
suppressed exception. The idea is similar to how java handle 
'try-with-resources' statement. 

 

A proposed to create following function to standardise those 'withXXX' 
functions. 

```

/**
 * 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 withFinallyBlock(tryBlock: => Unit)(finallyBlock: => Unit): Unit = {
  var fromTryBlock : Throwable = null
  try tryBlock catch {
    case cause : Throwable =>
      fromTryBlock = cause
      throw cause
    } finally {
      if (fromTryBlock != null) {
        try finallyBlock catch {
          case fromFinallyBlock : Throwable =>
            fromTryBlock.addSuppressed(fromFinallyBlock)
            throw fromTryBlock 
        }
      } else {
        finallyBlock 
     }
  }
}

```

 


> SQLTestUtils Refactoring
> ------------------------
>
>                 Key: SPARK-27772
>                 URL: https://issues.apache.org/jira/browse/SPARK-27772
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 3.0.0
>            Reporter: William Wong
>            Priority: Minor
>
> {code:java}
> // code placeholder
> {code}
> The current `SQLTestUtils` created many `withXXX` utility functions to clean 
> up tables/views/caches created for testing purpose. 
> Some of those `withXXX` functions would ignore certain exceptions, like 
> `NoSuchTableException` in the clean up block (finally block). 
> ```
>  /**
>  * Drops temporary view `viewNames` after calling `f`.
>  */
>  protected def withTempView(viewNames: String*)(f: => Unit): Unit = {
>    try f finally
> Unknown macro: \{    // 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 =>     }  }
> }
> ```
> Maybe it is not the best option. If a test hit an exception in the 'f' 
> closure, no matter what exception we hit in the finally block, we should not 
> mask that exception with any other exception hit in the finally block. The 
> exception caught in the finally block should be reattached to the original 
> exception as a suppressed exception. The idea is similar to how java handle 
> 'try-with-resources' statement. 
>  
> A proposed to create following function to standardise those 'withXXX' 
> functions. 
> ```
> /**
>  * 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 withFinallyBlock(tryBlock: => Unit)(finallyBlock: => Unit): Unit 
> = {
>    var fromTryBlock : Throwable = null
>    try tryBlock catch \{     case cause : Throwable =>       fromTryBlock = 
> cause       throw cause     }
> finally {
>        if (fromTryBlock != null)
> Unknown macro: \{        try finallyBlock catch {           case 
> fromFinallyBlock : Throwable =>             
> fromTryBlock.addSuppressed(fromFinallyBlock)             throw fromTryBlock   
>        }      }
> else
> {         finallyBlock       }
>   }
>  }
> ```
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

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

Reply via email to