Repository navigation
fix(catalog): leave the types of polymorphic SQL function parameters unknown in the body - #942
Merged
Merged
Conversation
psteinroe
added a commit
that referenced
this pull request
Oct 26, 2026
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: ```sql 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 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.
Stacked on #939.
The typecheck rules gave the parameters of a SQL function their declared types inside the body, including polymorphic ones. With
$1 anyelement, a call likearray_prepend($1, $2)was matched againstarray_prepend(anycompatible, anycompatiblearray)withanyelementas the actual argument type, which no candidate accepts:This is the false positive #939 found in
polymorphism.sql.Postgres never analyses such a body with the declared types. When
check_function_bodiesis on,fmgr_sql_validatoronly raw-parses the body of a function with polymorphic arguments. At call time,prepare_sql_fn_parse_infoswaps each polymorphic parameter type for the type of the call's argument before it parses the body (functions.c#L286). So the type of a polymorphic parameter in the body is whatever the caller passes, and we can't know it from the body.BEGIN ATOMICbodies, which are analysed at creation with the declared types, aren't affected: Postgres rejects polymorphic arguments there ("SQL function with unquoted function body cannot have polymorphic arguments").The fix:
function_param_typenow returns unknown for parameters of a pseudo-type. Polymorphic types are the only pseudo-types SQL functions accept as parameters (pg_proc.c#L873). Expressions that use these parameters are therefore unknown, and nothing is reported for them. Parameters of concrete types (including arrays) still have their declared types.Tests: a new
functionArgumentMismatch/function_parameters.sqlspec. It checks that a concrete parameter is still checked (length(n)withn intis reported) and thatarray_prepend($1, $2)andlength(a)with polymorphic parameters aren't. Thepolymorphismentry is gone from the harness'sKNOWN_FALSE_POSITIVES, which is now empty. The regression snapshots only lose their "Known false positives" section.Not covered: we don't infer the result types of polymorphic SQL functions at call sites from the body. That already went through the function's declared signature and is unchanged.