Skip to content

fix(workspace): analyse every statement of a SQL function body - #947

Open
psteinroe wants to merge 1 commit into
fix/polymorphic-function-paramsfrom
fix/multi-statement-sql-function-bodies
Open

psteinroe wants to merge 1 commit into
fix/polymorphic-function-paramsfrom
fix/multi-statement-sql-function-bodies

Conversation

@psteinroe

@psteinroe psteinroe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #942.

The workspace treated the body of a LANGUAGE sql function as a single statement. A body with several statements doesn't parse as one, so it was reported as a syntax error, and none of its statements were linted or typechecked. An empty body got the same error:

create function add_user(p_name text) returns int language sql as $$
  insert into users (name) values (p_name);
  select count(*)::int from users;
$$;
-- before: syntax error "No root node found in parse result" over the whole body
-- after: no syntax error; both statements are analysed with `p_name` in scope

create function noop() returns void language sql as $$ $$;
-- before: the same syntax error
-- after: nothing

SqlFunctionBody::statements (in pgls_query_ext, next to sql_function_body) splits the body the way Postgres does (pg_parse_query), using the statement locations libpg_query returns. Each statement becomes a child of the CREATE FUNCTION, with its range in the document and its trailing semicolon, like top-level statements. Two cases stay as before:

  • A body with a single statement is still one child spanning the whole body, so hover, completions and the formatter see exactly what they saw before.
  • A body that doesn't parse is also still one child, so its syntax error is still reported for the body.

An empty body now has no children.

The formatter only rewrote bodies with exactly one child, and it now checks that explicitly. A body with several statements is left as written. Before this PR it was skipped because it didn't parse; without the check, the last statement would have replaced the whole body.

The regression harness and the rule specs use the same split, so they now analyse 357–373 body statements per version, up from 347–359. There are no new findings on accepted statements and no changes to rejected-statement findings, only the header counts.

Tests: unit tests for statements() (single statement, several with and without semicolons, empty, unparsable), document tests for a body with several statements (no syntax error, function context, document ranges) and an empty body, and a formatter test that keeps a body with several statements as written.

Not covered: BEGIN ATOMIC, RETURN, '' and E'...' bodies. #918 and #941 (on the #888 stack) extract those, and they conflict with this stack in the body extraction (document.rs, sql_function.rs). Whichever lands second should split string bodies with SqlFunctionBody::statements.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant