Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-50449][SQL] Fix SQL Scripting grammar allowing empty bodies for loops, IF and CASE #48989

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dusantism-db
Copy link
Contributor

@dusantism-db dusantism-db commented Nov 27, 2024

What changes were proposed in this pull request?

Before this PR, SQL Scripting grammar allowed for loops, IF and CASE to have empty bodies. Example:

WHILE 1 = 1 DO END WHILE;

If they have an empty body, an internal error is thrown during execution. This PR changes the grammar so that loops, IF and CASE must have at least one statement in their bodies.

Note that this does not completely fix the internal error issue. It is still possible to have something like

WHILE 1 = 1 DO 
  BEGIN
  END;
END WHILE;

where the same error is still thrown, except this construct is correct grammar wise.
This issue will be fixed by a separate PR, as non-trivial interpreter logic changes are required.

Why are the changes needed?

The existing grammar was wrong.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Unit tests that make sure parsing loops, IF and CASE with empty bodies throws an error.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label Nov 27, 2024
condition = "PARSE_SYNTAX_ERROR",
parameters = Map("error" -> "'LOOP'", "hint" -> ""))
}

test("loop with if else block") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it also make sense to add test with for with empty body, in this PR or your other one, depending on which you merge first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add it to the one which gets merged last, or if they both get merged then I will create a separate PR.

@@ -623,6 +652,21 @@ class SqlScriptingParserSuite extends SparkFunSuite with SQLHelper {
assert(whileStmt.label.contains("lbl"))
}

test("while with empty body") {
Copy link
Contributor

@miland-db miland-db Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add test like the one in PR description:

WHILE 1=1 DO
  BEGIN
  END;
END WHILE;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add that one in the follow up PR, as that case is still not fixed.

Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dusantism-db Could you open an JIRA and prepend it to PR's title, please.

@dusantism-db dusantism-db changed the title [SQL] Fix SQL Scripting grammar allowing empty bodies for loops, IF and CASE [SPARK-50449][SQL] Fix SQL Scripting grammar allowing empty bodies for loops, IF and CASE Nov 28, 2024
@dusantism-db
Copy link
Contributor Author

@MaxGekk done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants