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]>
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
@@ -1254,6 +1254,30 @@
1254
1254
],
1255
1255
"sqlState" : "42614"
1256
1256
},
1257
+
"DUPLICATE_CONDITION_IN_SCOPE" : {
1258
+
"message" : [
1259
+
"Found duplicate condition <condition> in the scope. Please, remove one of them."
1260
+
],
1261
+
"sqlState" : "42734"
1262
+
},
1263
+
"DUPLICATE_EXCEPTION_HANDLER" : {
1264
+
"message" : [
1265
+
"Found duplicate handlers. Please, remove one of them."
1266
+
],
1267
+
"subClass" : {
1268
+
"CONDITION" : {
1269
+
"message" : [
1270
+
"Found duplicate handlers for the same condition <condition>."
1271
+
]
1272
+
},
1273
+
"SQLSTATE" : {
1274
+
"message" : [
1275
+
"Found duplicate handlers for the same SQLSTATE <sqlState>."
1276
+
]
1277
+
}
1278
+
},
1279
+
"sqlState" : "42734"
1280
+
},
1257
1281
"DUPLICATE_KEY" : {
1258
1282
"message" : [
1259
1283
"Found duplicate keys <keyColumn>."
@@ -2440,6 +2464,29 @@
2440
2464
],
2441
2465
"sqlState" : "42K05"
2442
2466
},
2467
+
"INVALID_ERROR_CONDITION_DECLARATION" : {
2468
+
"message" : [
2469
+
"Invalid condition declaration."
2470
+
],
2471
+
"subClass" : {
2472
+
"ONLY_AT_BEGINNING" : {
2473
+
"message" : [
2474
+
"Condition <conditionName> can only be declared at the beginning of the compound."
2475
+
]
2476
+
},
2477
+
"QUALIFIED_CONDITION_NAME" : {
2478
+
"message" : [
2479
+
"Condition <conditionName> cannot be qualified."
2480
+
]
2481
+
},
2482
+
"SPECIAL_CHARACTER_FOUND" : {
2483
+
"message" : [
2484
+
"Special character found in condition name <conditionName>. Only alphanumeric characters and underscores are allowed."
2485
+
]
2486
+
}
2487
+
},
2488
+
"sqlState" : "42K0R"
2489
+
},
2443
2490
"INVALID_ESC" : {
2444
2491
"message" : [
2445
2492
"Found an invalid escape string: <invalidEscape>. The escape string must contain only one character."
@@ -2608,6 +2655,39 @@
2608
2655
},
2609
2656
"sqlState" : "HY000"
2610
2657
},
2658
+
"INVALID_HANDLER_DECLARATION" : {
2659
+
"message" : [
2660
+
"Invalid handler declaration."
2661
+
],
2662
+
"subClass" : {
2663
+
"CONDITION_NOT_FOUND" : {
2664
+
"message" : [
2665
+
"Condition <condition> not found."
2666
+
]
2667
+
},
2668
+
"DUPLICATE_CONDITION_IN_HANDLER_DECLARATION" : {
2669
+
"message" : [
2670
+
"Found duplicate condition <condition> in the handler declaration. Please, remove one of them."
2671
+
]
2672
+
},
2673
+
"DUPLICATE_SQLSTATE_IN_HANDLER_DECLARATION" : {
2674
+
"message" : [
2675
+
"Found duplicate sqlState <sqlState> in the handler declaration. Please, remove one of them."
2676
+
]
2677
+
},
2678
+
"INVALID_CONDITION_COMBINATION" : {
2679
+
"message" : [
2680
+
"Invalid combination of conditions in the handler declaration. SQLEXCEPTION and NOT FOUND cannot be used together with other condition/sqlstate values."
2681
+
]
2682
+
},
2683
+
"WRONG_PLACE_OF_DECLARATION" : {
2684
+
"message" : [
2685
+
"Handlers must be declared after variable/condition declaration, and before other statements."
2686
+
]
2687
+
}
2688
+
},
2689
+
"sqlState" : "42K0Q"
2690
+
},
2611
2691
"INVALID_IDENTIFIER" : {
2612
2692
"message" : [
2613
2693
"The unquoted identifier <ident> is invalid and must be back quoted as: `<ident>`.",
@@ -3264,6 +3344,12 @@
3264
3344
},
3265
3345
"sqlState" : "42616"
3266
3346
},
3347
+
"INVALID_SQLSTATE" : {
3348
+
"message" : [
3349
+
"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'."
3350
+
],
3351
+
"sqlState" : "428B3"
3352
+
},
3267
3353
"INVALID_SQL_ARG" : {
3268
3354
"message" : [
3269
3355
"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()`."
@@ -5492,6 +5578,11 @@
5492
5578
"Attach a comment to the namespace <namespace>."
5493
5579
]
5494
5580
},
5581
+
"CONTINUE_EXCEPTION_HANDLER" : {
5582
+
"message" : [
5583
+
"CONTINUE exception handler is not supported. Use EXIT handler."
5584
+
]
5585
+
},
5495
5586
"DESC_TABLE_COLUMN_JSON" : {
5496
5587
"message" : [
5497
5588
"DESC TABLE COLUMN AS JSON not supported for individual columns."
0 commit comments