Skip to content

Commit 23cf926

Browse files
miland-dbcloud-fan
authored andcommitted
[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 <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 105570b commit 23cf926

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,21 @@ case class SqlScriptingInterpreter(session: SparkSession) {
122122

123123
// Get NOT FOUND handler.
124124
notFoundHandler = if (handler.exceptionHandlerTriggers.notFound) {
125-
Some(handlerExec)
125+
if (notFoundHandler.isDefined) {
126+
throw SqlScriptingErrors.duplicateHandlerForSameCondition(CurrentOrigin.get, "NOT FOUND")
127+
} else {
128+
Some(handlerExec)
129+
}
126130
} else None
127131

128132
// Get SQLEXCEPTION handler.
129133
sqlExceptionHandler = if (handler.exceptionHandlerTriggers.sqlException) {
130-
Some(handlerExec)
134+
if (sqlExceptionHandler.isDefined) {
135+
throw SqlScriptingErrors
136+
.duplicateHandlerForSameCondition(CurrentOrigin.get, "SQLEXCEPTION")
137+
} else {
138+
Some(handlerExec)
139+
}
131140
} else None
132141
})
133142

sql/core/src/test/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreterSuite.scala

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,4 +3478,49 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
34783478
verifySqlScriptResult(sqlScript, expected)
34793479
}
34803480
}
3481+
3482+
test("Duplicate SQLEXCEPTION Handler") {
3483+
val sqlScript =
3484+
"""
3485+
|BEGIN
3486+
| DECLARE EXIT HANDLER FOR SQLEXCEPTION
3487+
| BEGIN
3488+
| SELECT 1;
3489+
| END;
3490+
| DECLARE EXIT HANDLER FOR SQLEXCEPTION
3491+
| BEGIN
3492+
| SELECT 2;
3493+
| END;
3494+
|
3495+
|END""".stripMargin
3496+
checkError(
3497+
exception = intercept[SqlScriptingException] {
3498+
runSqlScript(sqlScript)
3499+
},
3500+
condition = "DUPLICATE_EXCEPTION_HANDLER.CONDITION",
3501+
parameters = Map("condition" -> "SQLEXCEPTION")
3502+
)
3503+
}
3504+
3505+
test("Duplicate NOT FOUND Handler") {
3506+
val sqlScript =
3507+
"""
3508+
|BEGIN
3509+
| DECLARE EXIT HANDLER FOR NOT FOUND
3510+
| BEGIN
3511+
| SELECT 1;
3512+
| END;
3513+
| DECLARE EXIT HANDLER FOR NOT FOUND
3514+
| BEGIN
3515+
| SELECT 2;
3516+
| END;
3517+
|END""".stripMargin
3518+
checkError(
3519+
exception = intercept[SqlScriptingException] {
3520+
runSqlScript(sqlScript)
3521+
},
3522+
condition = "DUPLICATE_EXCEPTION_HANDLER.CONDITION",
3523+
parameters = Map("condition" -> "NOT FOUND")
3524+
)
3525+
}
34813526
}

0 commit comments

Comments
 (0)