William1104 commented on a change in pull request #24747:
[SPARK-27772][SQL][TEST] SQLTestUtils Refactoring
URL: https://github.com/apache/spark/pull/24747#discussion_r289496262
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/test/SQLTestUtils.scala
##########
@@ -255,61 +254,41 @@ 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 =>
- }
- }
+ Utils.tryWithSafeFinally(f)(viewNames.foreach(spark.catalog.dropTempView))
Review comment:
Hi @srowen ,
Yes. the code change the behaviour slightly. The original code would
completely ignore the `NoSuchTableException`. The new one (via
`Utils.tryWithSafeFinally`) would add the `NoSuchTableException` as a
suppressed exception to the original exception or simply rethrow the
`NoSuchTableException` if we did not hit any exception in the try block.
I feel like the new behaviour is more desirable. Otherwise, it will be very
easy for a developer to miss any typo on view names as the test would not fail
even if the provided view name is never correct.
Somehow, the original code suppresses the `NoSuchTableException` outside the
foreach loop. Therefore, the loop would be terminated immediately when hitting
the `NoSuchTableException`. If it is not the desirable behaviour, I guess the
original code should be something like
```
viewNames.foreach { viewName =>
try spark.catalog.dropTempView(viewName) catch {
// If the test failed part way, we don't want to mask the failure by
failing to remove
// temp views that never got created.
case _: NoSuchTableException =>
}
}
```
What about I change the code into something like this?
```
Utils.tryWithSafeFinally(f) {
var firstException: Throwable = null
viewNames.foreach { viewName =>
try spark.catalog.dropTempView(viewName) catch {
case t: Throwable =>
if (firstException == null) {
firstException = t
} else {
firstException.addSuppressed(t)
}
}
}
if (firstException != null) {
throw firstException
}
}
```
However, the code looks over complicated and very procedure... I don't like
this either.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]