Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48356][SQL] Support for FOR statement
### What changes were proposed in this pull request? In this PR, support for FOR statement in SQL scripting is introduced. Examples: ``` FOR row AS SELECT * FROM t DO SELECT row.intCol; END FOR; ``` ``` FOR SELECT * FROM t DO SELECT intCol; END FOR; ``` Implementation notes: As local variables for SQL scripting are currently a work in progress, session variables are used to simulate them. When FOR begins executing, session variables are declared for each column in the result set, and optionally for the for variable if it is present ("row" in the example above). On each iteration, these variables are overwritten with the values from the row currently being iterated. The variables are dropped upon loop completion. This means that if a session variable which matches the name of a column in the result set already exists, the for statement will drop that variable after completion. If that variable would be referenced after the for statement, the script would fail as the variable would not exist. This limitation is already present in the current iteration of SQL scripting, and will be fixed once local variables are introduced. Also, with local variables the implementation of for statement will be much simpler. Grammar/parser changes: `forStatement` grammar rule `visitForStatement` rule visitor `ForStatement` logical operator ### Why are the changes needed? FOR statement is an part of SQL scripting control flow logic. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? New tests are introduced to all of the three scripting test suites: `SqlScriptingParserSuite`, `SqlScriptingExecutionNodeSuite` and `SqlScriptingInterpreterSuite`. ### Was this patch authored or co-authored using generative AI tooling? No Closes apache#48794 from dusantism-db/scripting-for-loop. Authored-by: Dušan Tišma <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information