Skip to content

fix: type ROWS 1 set-returning functions as arrays - #1077

Closed
peter941221 wants to merge 4 commits into
supabase:masterfrom
peter941221:fix/typegen-setof-rows-one
Closed

peter941221 wants to merge 4 commits into
supabase:masterfrom
peter941221:fix/typegen-setof-rows-one

Conversation

@peter941221

@peter941221 peter941221 commented Jun 1, 2026 •

Copy link
Copy Markdown

Problem

TypeScript type generation used prorows > 1 to decide whether an RPC return type should be an array.

prorows comes from PostgreSQL ROWS n, which is a planner estimate. It does not change the runtime shape of a set-returning function. A function declared as RETURNS SETOF ... ROWS 1 still returns a list through PostgREST.

That made generated RPC types collapse RETURNS SETOF ... ROWS 1 functions to a single object.

Change

Use is_set_returning_function as the return-shape signal for generated TypeScript RPC types.

This keeps scalar and table-returning functions on the existing single-object path, while RETURNS SETOF ... ROWS 1 now gets an array Returns type.

The existing snapshots now reflect the corrected Returns and SetofOptions.isOneToOne behavior for the ROWS 1 fixtures. This PR also adds a dedicated regression test that contrasts the two relevant cases directly:

  • function_using_table_returns stays a single object
  • function_using_setof_rows_one still generates an array even with ROWS 1

Issue

Addresses supabase/supabase#46525.

Validation

  • npm run check
  • npx prettier --check src/server/templates/typescript.ts test/server/typegen.ts
  • npx -p node@20 node ./node_modules/vitest/vitest.mjs run test/index.test.ts -t "typegen: typescript keeps ROWS 1 set-returning functions as arrays" --reporter verbose

@peter941221
peter941221 marked this pull request as ready for review June 1, 2026 06:25
@peter941221
peter941221 requested review from a team, avallete and soedirgo as code owners June 1, 2026 06:25
@SahilArate

Copy link
Copy Markdown

The one-line fix is correct and well-targeted is_set_returning_function is exactly the right signal here.
One thing worth noting: the PR updates existing snapshot tests but doesn't add a dedicated regression test for the specific scenario from the issue a RETURNS TABLE (...) ROWS 1 function. Without it, a future refactor could silently reintroduce the same regression.
The commenter vaguul on issue #46525 suggested a clean pair of cases worth adding: one scalar function that should type as a single object, and one ROWS 1 set-returning function that should type as []. Adding that to test/server/typegen.ts would make the intent explicit and protect against regression.

@peter941221

Copy link
Copy Markdown
Author

Added a dedicated regression test in test/server/typegen.ts for the two cases from the review: function_using_table_returns stays a single object, while function_using_setof_rows_one still generates an array even with ROWS 1. I also kept the assertion focused on the generated function fragments so it locks the return-shape behavior without depending on the full inline snapshot formatting.

@avallete avallete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@peter941221

Copy link
Copy Markdown
Author

I retraced this through the downstream consumer and the patch is changing the contract in the wrong place.

The generated type for function_using_setof_rows_one already keeps Returns as a single row and marks SetofOptions as isOneToOne: true and isSetofReturn: true:
https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/test/types.generated.ts#L754-L765

postgrest-js uses that combination to infer an array for direct rpc calls:
https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/src/types/common/rpc.ts#L126-L131

That matches the existing runtime tests:
direct rpc returns an array
https://github.com/supabase/supabase-js/blob/a25062e9285fa8e3bd702c59ddda0d87ad1fcc27/packages/core/postgrest-js/test/advanced_rpc.test.ts#L1230-L1258

embedded function joins return an object
https://github.com/supabase/supabase-js/blob/a25062e9285fa8e3bd702c59ddda0d87ad1fcc27/packages/core/postgrest-js/test/embeded_functions_join.test.ts#L382-L428

My patch turns Returns into Row[] and flips isOneToOne to false, which removes the direct-rpc vs embedded distinction. I am closing this PR.

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.

3 participants