Commit 5533c81
[SPARK-48355][SQL] Support for CASE statement
### What changes were proposed in this pull request?
Add support for [case statements](https://docs.google.com/document/d/1cpSuR3KxRuTSJ4ZMQ73FJ4_-hjouNNU2zfI4vri6yhs/edit#heading=h.ofijhkunigv) to sql scripting. There are 2 types of case statement - simple and searched (EXAMPLES BELOW). Proposed changes are:
- Add `caseStatement` grammar rule to SqlBaseParser.g4
- Add visit case statement methods to `AstBuilder`
- Add `SearchedCaseStatement` and `SearchedCaseStatementExec` classes, to enable them to be run in sql scripts.
The reason only searched case nodes are added is that, in the current implementation, a simple case is parsed into a searched case, by creating internal `EqualTo` expressions to compare the main case expression to the expressions in the when clauses. This approach is similar to the existing case **expressions**, which are parsed in the same way. The problem with this approach is that the main expression is unnecessarily evaluated N times, where N is the number of when clauses, which can be quite inefficient, for example if the expression is a complex query. Optimally, the main expression would be evaluated once, and then compared to the other expressions. I'm open to suggestions as to what the best approach to achieve this would be.
Simple case compares one expression (case variable) to others, until an equal one is found. Else clause is optional.
```
BEGIN
CASE 1
WHEN 1 THEN
SELECT 1;
WHEN 2 THEN
SELECT 2;
ELSE
SELECT 3;
END CASE;
END
```
Searched case evaluates boolean expressions. Else clause is optional.
```
BEGIN
CASE
WHEN 1 = 1 THEN
SELECT 1;
WHEN 2 IN (1,2,3) THEN
SELECT 2;
ELSE
SELECT 3;
END CASE;
END
```
### Why are the changes needed?
Case statements are currently not implemented in sql scripting.
### Does this PR introduce _any_ user-facing change?
Yes, users will now be able to use case statements in their sql scripts.
### How was this patch tested?
Tests for both simple and searched case statements are added to SqlScriptingParserSuite, SqlScriptingExecutionNodeSuite and SqlScriptingInterpreterSuite.
### Was this patch authored or co-authored using generative AI tooling?
No
Closes apache#47672 from dusantism-db/sql-scripting-case-statement.
Authored-by: Dušan Tišma <dusan.tisma@databricks.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>1 parent aa54ed1 commit 5533c81
File tree
8 files changed
+920
-4
lines changed- sql
- api/src/main/antlr4/org/apache/spark/sql/catalyst/parser
- catalyst/src
- main/scala/org/apache/spark/sql/catalyst/parser
- test/scala/org/apache/spark/sql/catalyst/parser
- core/src
- main/scala/org/apache/spark/sql/scripting
- test/scala/org/apache/spark/sql/scripting
8 files changed
+920
-4
lines changedLines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| |||
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| |||
Lines changed: 47 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
264 | 310 | | |
265 | 311 | | |
266 | 312 | | |
| |||
292 | 338 | | |
293 | 339 | | |
294 | 340 | | |
295 | | - | |
| 341 | + | |
296 | 342 | | |
297 | 343 | | |
298 | 344 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
0 commit comments