feat(grammar): RAISE, ASSERT, EXCEPTION handlers and GET DIAGNOSTICS - #372
Draft
sebsnyk wants to merge 2 commits into
Draft
feat(grammar): RAISE, ASSERT, EXCEPTION handlers and GET DIAGNOSTICS#372sebsnyk wants to merge 2 commits into
sebsnyk wants to merge 2 commits into
Conversation
Function and procedure bodies could hold SQL statements and RETURN but none of the control flow that PL/pgSQL and PL/SQL bodies are made of. Adds assignment with `:=` or `=`, `NULL`, `IF ... ELSIF ... ELSE ... END IF`, `CASE ... END CASE`, `LOOP`, `WHILE ... LOOP`, `FOR ... IN` over an integer range, a query or a cursor, `FOREACH ... IN ARRAY`, `EXIT` and `CONTINUE` with an optional label and `WHEN`, `<<label>>` on blocks and loops, nested `DECLARE ... BEGIN ... END` blocks and a bare `RETURN`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Adds the error-handling statements of PL/pgSQL and PL/SQL bodies: `RAISE` with a level, a format string and arguments, a condition name, `SQLSTATE` or `USING` options; `ASSERT`; `EXCEPTION WHEN ... THEN` handlers at the end of a function body, procedure body or block, with `OR`-joined conditions, `SQLSTATE` and `OTHERS`; and `GET [CURRENT | STACKED] DIAGNOSTICS`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Once a body can hold control flow (#371), the next thing every real PL/pgSQL or PL/SQL routine trips on is error handling:
RAISE NOTICE,RAISE EXCEPTION ... USING, anEXCEPTION WHEN others THENsection, and in testsASSERTandGET DIAGNOSTICS. The #236 sample stops at itsRAISE EXCEPTION. Against real sources:ERRORnodes with #371src/test/regress/sql/plpgsql.sqlsource/core/ut_utils.pkbsql/functions/check_default.sqlutPLSQL and pg_partman do not move because their remaining errors are
%TYPE, cursors,PERFORM/EXECUTEand Oracle'sIS ... END name;headers, which come next.Solution
A new
grammar/statements/error-handling.js:raise_statement:RAISE [DEBUG | LOG | INFO | NOTICE | WARNING | EXCEPTION] [format, args... | condition | SQLSTATE 'x'] [USING option = expr, ...], and a bareRAISE. EachUSINGoption is araise_option.assert_statement:ASSERT condition [, message].exception_handler:WHEN cond [OR cond]... THEN statements, where a condition isOTHERS,SQLSTATE 'x'or a name. Akeyword_exceptionfollowed by one or more handlers may closefunction_body,procedure_bodyandblock.get_diagnostics:GET [CURRENT | STACKED] DIAGNOSTICS var = item, ....All four are reachable only from bodies and blocks, as in #371.
One shape to check: a condition name in
RAISE unique_violationparses as(field (identifier)), because a separate identifier alternative would be ambiguous with a format expression. In anexception_handlerthe name is a plain(identifier).Notes
tree-sitter test: 524 corpus tests on feat(grammar): procedural control flow in function and procedure bodies #371, 531 with this PR (7 new intest/corpus/error_handling.txt), plus the 2 highlight tests. NoERROR/MISSING;scripts/test-keywords.shpasses.STATE_COUNT30898 → 31012 (+0.4% on top of feat(grammar): procedural control flow in function and procedure bodies #371; +1.3% overmain),parser.c42.3 MB → 42.5 MB.RETURNING * INTO session_record, which belongs withSELECT ... INTO STRICTandEXECUTE ... INTOin a following PR.