Skip to content

sql: discard final rows from SETOF VOID functions - #174978

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-103119-setof-void-cardinality
Open

Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-103119-setof-void-cardinality

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 10, 2026 •

Copy link
Copy Markdown

Selecting from a SQL UDF declared RETURNS SETOF VOID currently exposes a NULL row from its final SELECT NULL. This change makes that function produce no result rows, while still executing its body.

The existing DiscardLastStmtResult path already runs the final statement without adding its output to the routine's result buffer. buildUDF now selects that path for a set-returning routine whose result family is VOID, as well as for routines that use an explicit result buffer. Checking both properties keeps scalar VOID functions and non-VOID set-returning functions on their existing paths.

The issue explicitly leaves open whether CockroachDB should adopt PostgreSQL's behavior here. This PR implements that compatibility choice; it does not assume that the API decision has already been made.

CREATE FUNCTION f() RETURNS SETOF VOID LANGUAGE SQL AS 'SELECT NULL';
SELECT f();
-- Before: one NULL row. After: no rows.

The regression exercises calls in both the SELECT list and FROM clause, checks that a volatile function still performs an INSERT, and checks scalar VOID and SETOF INT results.

Validation

  • bazel test //pkg/sql:sql_test --test_arg=-test.run=^TestSetReturningVoidCardinality$ passed.
  • The same test added to unmodified 8812064a failed because each SETOF VOID call returned [[NULL]] instead of an empty result.
  • Candidate and baseline runs used two build jobs, disabled sharding, one Go-test parallel worker, and a fixed seed. CockroachDB's randomized test knobs, test tenants and DRPC were disabled.

The earlier broad //pkg/sql:sql_test run did not complete cleanly on this machine. TestTenantStatementTimeoutAdmissionQueueCancellation also timed out when run alone on the unmodified baseline. Other resource-related failures passed when rerun with one job. I am reporting the focused regression results here, not a clean full SQL-suite pass.

Fixes #103119

Release note (backward-incompatible change): SQL functions declared RETURNS SETOF VOID no longer produce a row from their final body statement. Applications relying on that row should use a scalar VOID function when one return row is required. Function bodies still execute, including their side effects.

A SETOF VOID SQL function currently exposes the result of its final
statement as a NULL row. Mark that result for discard while still executing
the body. Scalar VOID and non-VOID set-returning functions retain their
existing behavior.

Add an execution regression for SELECT-list and FROM-clause calls, body
side effects, scalar VOID and SETOF INT. The regression fails on the
unmodified baseline and passes with this change.

Production patch generated with Codex CLI through MetamorphicRepair.

Fixes cockroachdb#103119

Release note (backward-incompatible change): SQL functions declared RETURNS SETOF VOID
no longer produce a row from their final body statement. Applications relying on that
row should use a scalar VOID function when one return row is required. Function bodies
still execute, including their side effects.
@Alignyx
Alignyx requested a review from a team as a code owner September 10, 2026 11:39
@Alignyx
Alignyx requested review from andyyang890 and removed request for a team September 10, 2026 11:39
@blathers-crl

blathers-crl Bot commented Sep 10, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 10, 2026

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

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: UDF returning SETOF VOID should produce zero rows

1 participant