cockroachdb / cockroach

CockroachDB - the open source, cloud-native distributed SQL database.
https://www.cockroachlabs.com
Other
29.52k stars 3.7k forks source link

plpgsql: correctly handle a set-returning UDF call within a routine #121105

Open DrewKimball opened 3 months ago

DrewKimball commented 3 months ago

On current master, given the following functions:

CREATE FUNCTION generator() RETURNS SETOF INT AS $$
  VALUES (1), (2), (3);
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RETURN (SELECT generator());
  END
$$ LANGUAGE PLpgSQL;

Calling f() will produce a plan like this:

values
 └── tuple
      └── udf: f
           └── body
                └── values
                     └── tuple
                          └── udf: generator
                               └── body
                                    └── values
                                         ├── tuple
                                         │    └── const: 1
                                         ├── tuple
                                         │    └── const: 2
                                         └── tuple
                                              └── const: 3

Note the absence of a project-set - the set-returning UDF is being treated as a scalar UDF. This will have the effect of ignoring any values returned by the generator beyond the first. Postgres also allows this setup, but returns an error if the generator returns more than one row. We should do the same.

Jira issue: CRDB-37076

blathers-crl[bot] commented 3 months ago

Hi @yuzefovich, please add branch-* labels to identify which branch(es) this GA-blocker affects.

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