You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SPARK-48353][SQL] Introduction of Exception Handling mechanism in SQL Scripting
### What changes were proposed in this pull request?
This pull request introduces the logic of error handling inside SQL Scripting language. Now, it is possible to:
- declare conditions for specific SQL States (currently only valid in scope where they are defined)
- declare handlers to catch and process errors that are risen during statement execution
Rules for selecting the most appropriate handler:
- Named condition handlers are most specific.
- SQLSTATE handlers are next in specificity.
- Generic NOT FOUND and SQLEXCEPTION handlers are least specific.
Note: Handlers defined in the innermost compound statement where the exception was raised are considered.
### Why are the changes needed?
The intent is to add the possibility for user to handle SQL errors in a custom defined way.
### Limitations
- Currently, only `EXIT` handler is supported. Support for `CONTINUE` handlers will come in the future.
- It is only possible to declare condition in its full form by specifying SQLSTATE. Short form with default SQLSTATE is not yet supported.
### Does this PR introduce any user-facing change?
No.
### How was this patch tested?
There are already existing test suites for SQL scripting that have been improved to test new functionalities:
- `SqlScriptingParserSuite`
- `SqlScriptingExecutionSuite`
- `SqlScriptingE2eSuite`
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes#49427 from miland-db/milan-dankovic_data/refactor-execution-4-condition-handlers.
Authored-by: Milan Dankovic <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
(cherry picked from commit 0f163a5)
Signed-off-by: Wenchen Fan <[email protected]>
Copy file name to clipboardexpand all lines: common/utils/src/main/resources/error/error-conditions.json
+91
Original file line number
Diff line number
Diff line change
@@ -1247,6 +1247,30 @@
1247
1247
],
1248
1248
"sqlState" : "42614"
1249
1249
},
1250
+
"DUPLICATE_CONDITION_IN_SCOPE" : {
1251
+
"message" : [
1252
+
"Found duplicate condition <condition> in the scope. Please, remove one of them."
1253
+
],
1254
+
"sqlState" : "42734"
1255
+
},
1256
+
"DUPLICATE_EXCEPTION_HANDLER" : {
1257
+
"message" : [
1258
+
"Found duplicate handlers. Please, remove one of them."
1259
+
],
1260
+
"subClass" : {
1261
+
"CONDITION" : {
1262
+
"message" : [
1263
+
"Found duplicate handlers for the same condition <condition>."
1264
+
]
1265
+
},
1266
+
"SQLSTATE" : {
1267
+
"message" : [
1268
+
"Found duplicate handlers for the same SQLSTATE <sqlState>."
1269
+
]
1270
+
}
1271
+
},
1272
+
"sqlState" : "42734"
1273
+
},
1250
1274
"DUPLICATE_KEY" : {
1251
1275
"message" : [
1252
1276
"Found duplicate keys <keyColumn>."
@@ -2433,6 +2457,29 @@
2433
2457
],
2434
2458
"sqlState" : "42K05"
2435
2459
},
2460
+
"INVALID_ERROR_CONDITION_DECLARATION" : {
2461
+
"message" : [
2462
+
"Invalid condition declaration."
2463
+
],
2464
+
"subClass" : {
2465
+
"ONLY_AT_BEGINNING" : {
2466
+
"message" : [
2467
+
"Condition <conditionName> can only be declared at the beginning of the compound."
2468
+
]
2469
+
},
2470
+
"QUALIFIED_CONDITION_NAME" : {
2471
+
"message" : [
2472
+
"Condition <conditionName> cannot be qualified."
2473
+
]
2474
+
},
2475
+
"SPECIAL_CHARACTER_FOUND" : {
2476
+
"message" : [
2477
+
"Special character found in condition name <conditionName>. Only alphanumeric characters and underscores are allowed."
2478
+
]
2479
+
}
2480
+
},
2481
+
"sqlState" : "42K0R"
2482
+
},
2436
2483
"INVALID_ESC" : {
2437
2484
"message" : [
2438
2485
"Found an invalid escape string: <invalidEscape>. The escape string must contain only one character."
@@ -2601,6 +2648,39 @@
2601
2648
},
2602
2649
"sqlState" : "HY000"
2603
2650
},
2651
+
"INVALID_HANDLER_DECLARATION" : {
2652
+
"message" : [
2653
+
"Invalid handler declaration."
2654
+
],
2655
+
"subClass" : {
2656
+
"CONDITION_NOT_FOUND" : {
2657
+
"message" : [
2658
+
"Condition <condition> not found."
2659
+
]
2660
+
},
2661
+
"DUPLICATE_CONDITION_IN_HANDLER_DECLARATION" : {
2662
+
"message" : [
2663
+
"Found duplicate condition <condition> in the handler declaration. Please, remove one of them."
2664
+
]
2665
+
},
2666
+
"DUPLICATE_SQLSTATE_IN_HANDLER_DECLARATION" : {
2667
+
"message" : [
2668
+
"Found duplicate sqlState <sqlState> in the handler declaration. Please, remove one of them."
2669
+
]
2670
+
},
2671
+
"INVALID_CONDITION_COMBINATION" : {
2672
+
"message" : [
2673
+
"Invalid combination of conditions in the handler declaration. SQLEXCEPTION and NOT FOUND cannot be used together with other condition/sqlstate values."
2674
+
]
2675
+
},
2676
+
"WRONG_PLACE_OF_DECLARATION" : {
2677
+
"message" : [
2678
+
"Handlers must be declared after variable/condition declaration, and before other statements."
2679
+
]
2680
+
}
2681
+
},
2682
+
"sqlState" : "42K0Q"
2683
+
},
2604
2684
"INVALID_IDENTIFIER" : {
2605
2685
"message" : [
2606
2686
"The unquoted identifier <ident> is invalid and must be back quoted as: `<ident>`.",
@@ -3257,6 +3337,12 @@
3257
3337
},
3258
3338
"sqlState" : "42616"
3259
3339
},
3340
+
"INVALID_SQLSTATE" : {
3341
+
"message" : [
3342
+
"Invalid SQLSTATE value: '<sqlState>'. SQLSTATE must be exactly 5 characters long and contain only A-Z and 0-9. SQLSTATE must not start with '00', '01', or 'XX'."
3343
+
],
3344
+
"sqlState" : "428B3"
3345
+
},
3260
3346
"INVALID_SQL_ARG" : {
3261
3347
"message" : [
3262
3348
"The argument <name> of `sql()` is invalid. Consider to replace it either by a SQL literal or by collection constructor functions such as `map()`, `array()`, `struct()`."
@@ -5485,6 +5571,11 @@
5485
5571
"Attach a comment to the namespace <namespace>."
5486
5572
]
5487
5573
},
5574
+
"CONTINUE_EXCEPTION_HANDLER" : {
5575
+
"message" : [
5576
+
"CONTINUE exception handler is not supported. Use EXIT handler."
5577
+
]
5578
+
},
5488
5579
"DESC_TABLE_COLUMN_JSON" : {
5489
5580
"message" : [
5490
5581
"DESC TABLE COLUMN AS JSON not supported for individual columns."
0 commit comments