-
Notifications
You must be signed in to change notification settings - Fork 4.1k
sql: disallow SRFs in aggregate functions #84616
Copy link
Copy link
Closed
Labels
C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-queriesSQL Queries TeamSQL Queries Team
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-queriesSQL Queries TeamSQL Queries Team
Type
Projects
Status
Done