Skip to content

sql: set-returning udfs don't always respect ordering #144013

@DrewKimball

Description

@DrewKimball

When a set-returning UDF is specified directly in the SELECT list rather than as a data source, we apply a transformation to the last body statement so that multiple columns are wrapped into a single tuple column. This step loses the ordering if one was specified by the user, and so results can be returned in a non-deterministic order. Here's a logic test that demonstrates this behavior when run under the fakedist-vec-off configuration:

statement ok
CREATE TABLE ab (
  a INT PRIMARY KEY,
  b INT
)

statement ok
INSERT INTO ab SELECT i, i*10 FROM generate_series(1, 4) g(i)

statement ok
CREATE FUNCTION all_ab() RETURNS SETOF ab LANGUAGE SQL AS $$
  SELECT a, b FROM ab ORDER BY a
$$

query II nosort
SELECT * FROM all_ab()
----
1 10
2 20
3 30
4 40

query T nosort
SELECT all_ab()
----
(1,10)
(2,20)
(3,30)
(4,40)

Jira issue: CRDB-49208

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sql-routineUDFs and Stored ProceduresA-sql-udfC-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-queriesSQL Queries Team

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions