Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: a function returning SETOF RECORD with OUT params returns one row #128403

Closed
DrewKimball opened this issue Aug 6, 2024 · 1 comment · Fixed by #137251
Closed

sql: a function returning SETOF RECORD with OUT params returns one row #128403

DrewKimball opened this issue Aug 6, 2024 · 1 comment · Fixed by #137251
Labels
A-sql-udf branch-master Failures and bugs on the master branch. branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-queries SQL Queries Team

Comments

@DrewKimball
Copy link
Collaborator

DrewKimball commented Aug 6, 2024

It is possible to use RETURNS SETOF syntax to indicate that a user-defined function returns more than one row. This syntax is compatible with the RECORD return type:

root@localhost:26257/defaultdb> create function f() returns setof record language sql as $$ select t, t::text from generate_series(1, 10) g(t); $$;
CREATE FUNCTION

Time: 128ms total (execution 128ms / network 0ms)

root@localhost:26257/defaultdb> select f();
     f
-----------
  (1,1)
  (2,2)
  (3,3)
  (4,4)
  (5,5)
  (6,6)
  (7,7)
  (8,8)
  (9,9)
  (10,10)
(10 rows)

However, naming the elements of the record using OUT parameters causes CRDB to truncate the result:

root@localhost:26257/defaultdb> create function f(out x int, out y text) returns setof record language sql as $$ select t, t::text from generate_series(1, 10) g(t); $$;
CREATE FUNCTION

Time: 154ms total (execution 154ms / network 0ms)

root@localhost:26257/defaultdb> select f();
    f
---------
  (1,1)
(1 row)

Contrast this with Postgres, which returns all the rows:

postgres=# create function f(out x int, out y text) returns setof record language sql as $$ select t, t::text from generate_series(1, 10) g(t); $$;
CREATE FUNCTION
postgres=# select f();
    f
---------
 (1,1)
 (2,2)
 (3,3)
 (4,4)
 (5,5)
 (6,6)
 (7,7)
 (8,8)
 (9,9)
 (10,10)
(10 rows)

Jira issue: CRDB-40994

@DrewKimball DrewKimball added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team A-sql-udf labels Aug 6, 2024
Copy link

blathers-crl bot commented Aug 6, 2024

Hi @DrewKimball, please add branch-* labels to identify which branch(es) this C-bug affects.

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

@github-project-automation github-project-automation bot moved this to Triage in SQL Queries Aug 6, 2024
@DrewKimball DrewKimball added branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 branch-master Failures and bugs on the master branch. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. labels Aug 6, 2024
@DrewKimball DrewKimball moved this from Triage to 24.3 Release in SQL Queries Aug 13, 2024
@mgartner mgartner moved this from 24.3 Release to 25.1 Release in SQL Queries Oct 31, 2024
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 11, 2024
This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes cockroachdb#128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 12, 2024
This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes cockroachdb#128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.
craig bot pushed a commit that referenced this issue Dec 12, 2024
137251: sql: implement RETURNS TABLE syntax r=DrewKimball a=DrewKimball

#### sql: do not limit set-returning UDF when it has OUT parameters

This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes #128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.

#### sql: implement RETURNS TABLE syntax

This commit implements `RETURNS TABLE` for UDFs. `RETURNS TABLE` is
syntactic sugar for `RETURNS SETOF` with:
- RECORD if there are multiple TABLE parameters, or
- the type of the single TABLE parameter.
The TABLE parameters are added to the list of routine parameters.

Fixes #100226

Release note (sql change): Added support for `RETURNS TABLE` syntax when
creating a UDF.

Co-authored-by: Drew Kimball <[email protected]>
craig bot pushed a commit that referenced this issue Dec 13, 2024
137251: sql: implement RETURNS TABLE syntax r=DrewKimball a=DrewKimball

#### sql: do not limit set-returning UDF when it has OUT parameters

This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes #128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.

#### sql: implement RETURNS TABLE syntax

This commit implements `RETURNS TABLE` for UDFs. `RETURNS TABLE` is
syntactic sugar for `RETURNS SETOF` with:
- RECORD if there are multiple TABLE parameters, or
- the type of the single TABLE parameter.
The TABLE parameters are added to the list of routine parameters.

Fixes #100226

Release note (sql change): Added support for `RETURNS TABLE` syntax when
creating a UDF.

Co-authored-by: Drew Kimball <[email protected]>
@craig craig bot closed this as completed in 3ad8c0f Dec 13, 2024
@github-project-automation github-project-automation bot moved this from 25.1 Release to Done in SQL Queries Dec 13, 2024
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 13, 2024
This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes cockroachdb#128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 13, 2024
This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes cockroachdb#128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.
DrewKimball added a commit to DrewKimball/cockroach that referenced this issue Dec 13, 2024
This commit fixes a bug that caused the `SetOf` option for the UDF
`ReturnType` to be overwritten if the UDF had OUT parameters. The bug
caused a `LIMIT 1` to be imposed on the UDF's final body statement, so
that the UDF returned only a single row.

Fixes cockroachdb#128403

Release note (bug fix): Fixed a bug existing since v24.1 that would
cause a set-returning UDF with OUT parameters to return a single row.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-udf branch-master Failures and bugs on the master branch. branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. T-sql-queries SQL Queries Team
Projects
Archived in project
1 participant