Skip to content

sql: disallow SRFs in aggregate functions #84616

@maryliag

Description

@maryliag

To reproduce

create table t (a int, b int, c STRING[]);
insert into t values (1, 1, ARRAY['a', 'b']);
insert into t values (1, 2, ARRAY['c', 'd']);
insert into t values (2, 2, ARRAY['c']);
insert into t values (1, 1, ARRAY['e', 'f']);

It returns an error if try to aggregate the c column with unnest after any other type of aggregation:

> select a, max(b), array_agg(unnest(c)) from t group by a;
ERROR: column "c" must appear in the GROUP BY clause or be used in an aggregate function
SQLSTATE: 42803

but if the select has that array aggregation before any other aggregation, it works

> select a, array_agg(unnest(c)), max(b) from t group by a;
  a |   array_agg   | max
----+---------------+------
  1 | {a,b,c,d,e,f} |   2
  2 | {c}           |   2

If I run the unnest first, it also works

> select a, max(b), array_agg(x) from (select a, b, unnest(c) as x from t) group by a;
  a | max |   array_agg
----+-----+----------------
  1 |   2 | {a,b,c,d,e,f}
  2 |   2 | {c}
(2 rows)

Jira issue: CRDB-17765

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-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

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions