This is an automated email from the ASF dual-hosted git repository. wenchen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push: new 23cf9266f323 [SPARK-52489][SQL] Forbid duplicate SQLEXCEPTION and NOT FOUND handlers inside SQL Script 23cf9266f323 is described below commit 23cf9266f323e8d042e9fd8c8ed8a2c762289449 Author: Milan Dankovic <milan.danko...@databricks.com> AuthorDate: Thu Jun 19 09:03:07 2025 +0800 [SPARK-52489][SQL] Forbid duplicate SQLEXCEPTION and NOT FOUND handlers inside SQL Script ### What changes were proposed in this pull request? In this PR we forbid duplicate SQLEXCEPTION or NOT FOUND exception handlers to be defined in the same scope. This was already done for different conditions and sqlstates but was not done for these 2 types of exception handlers. Code like this should fail: ``` BEGIN DECLARE EXIT HANDLER FOR NOT FOUND BEGIN SELECT 1; END; DECLARE EXIT HANDLER FOR NOT FOUND BEGIN SELECT 2; END; END ``` ### Why are the changes needed? This is a bug fix. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? New tests in `SqlScriptingInterpreterSuite`. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #51168 from miland-db/milan-dankovic_data/forbid-duplicate-handlers. Authored-by: Milan Dankovic <milan.danko...@databricks.com> Signed-off-by: Wenchen Fan <wenc...@databricks.com> --- .../sql/scripting/SqlScriptingInterpreter.scala | 13 ++++++- .../scripting/SqlScriptingInterpreterSuite.scala | 45 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala b/sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala index 9ab45c4003f8..e0e11183d321 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala @@ -122,12 +122,21 @@ case class SqlScriptingInterpreter(session: SparkSession) { // Get NOT FOUND handler. notFoundHandler = if (handler.exceptionHandlerTriggers.notFound) { - Some(handlerExec) + if (notFoundHandler.isDefined) { + throw SqlScriptingErrors.duplicateHandlerForSameCondition(CurrentOrigin.get, "NOT FOUND") + } else { + Some(handlerExec) + } } else None // Get SQLEXCEPTION handler. sqlExceptionHandler = if (handler.exceptionHandlerTriggers.sqlException) { - Some(handlerExec) + if (sqlExceptionHandler.isDefined) { + throw SqlScriptingErrors + .duplicateHandlerForSameCondition(CurrentOrigin.get, "SQLEXCEPTION") + } else { + Some(handlerExec) + } } else None }) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreterSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreterSuite.scala index 85e37d4b2309..44aa50acf642 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreterSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreterSuite.scala @@ -3478,4 +3478,49 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession { verifySqlScriptResult(sqlScript, expected) } } + + test("Duplicate SQLEXCEPTION Handler") { + val sqlScript = + """ + |BEGIN + | DECLARE EXIT HANDLER FOR SQLEXCEPTION + | BEGIN + | SELECT 1; + | END; + | DECLARE EXIT HANDLER FOR SQLEXCEPTION + | BEGIN + | SELECT 2; + | END; + | + |END""".stripMargin + checkError( + exception = intercept[SqlScriptingException] { + runSqlScript(sqlScript) + }, + condition = "DUPLICATE_EXCEPTION_HANDLER.CONDITION", + parameters = Map("condition" -> "SQLEXCEPTION") + ) + } + + test("Duplicate NOT FOUND Handler") { + val sqlScript = + """ + |BEGIN + | DECLARE EXIT HANDLER FOR NOT FOUND + | BEGIN + | SELECT 1; + | END; + | DECLARE EXIT HANDLER FOR NOT FOUND + | BEGIN + | SELECT 2; + | END; + |END""".stripMargin + checkError( + exception = intercept[SqlScriptingException] { + runSqlScript(sqlScript) + }, + condition = "DUPLICATE_EXCEPTION_HANDLER.CONDITION", + parameters = Map("condition" -> "NOT FOUND") + ) + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org