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

opt: bug in aggregate scoping rules #59071

Open
rytaft opened this issue Jan 15, 2021 · 1 comment
Open

opt: bug in aggregate scoping rules #59071

rytaft opened this issue Jan 15, 2021 · 1 comment
Labels
A-sql-optimizer SQL logical planning and optimizations. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-productivity Severe issues that impede the productivity of CockroachDB developers. T-sql-queries SQL Queries Team

Comments

@rytaft
Copy link
Collaborator

rytaft commented Jan 15, 2021

Consider the following example:

CREATE TABLE testing (name text);
insert into testing values ('a'), ('b'), ('c');

We can create an array from these values successfully, but then when we try to unnest the array, the optimizer returns a cryptic error:

demo@127.0.0.1:26257/movr> SELECT string_to_array(string_agg(name, ' '), ' ') FROM testing;
         string_to_array
---------------------------------
  {a,b,c}
(1 row)

demo@127.0.0.1:26257/movr> SELECT unnest(string_to_array(string_agg(name, ' '), ' ')) FROM testing;
ERROR: unnest(): string_to_array(): string_agg(): aggregate functions are not allowed in SELECT
SQLSTATE: 42803

This works fine in Postgres, however:

postgres=# SELECT unnest(string_to_array(string_agg(name, ' '), ' ')) FROM testing;
 unnest 
--------
 a
 b
 c
(3 rows)

While we fix this bug, users can do the following as a workaround:

SELECT unnest(x.name_agg) FROM (select string_to_array(string_agg(name, ' '), ' ') AS name_agg FROM testing) x;

Jira issue: CRDB-3335

@rytaft rytaft added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-optimizer SQL logical planning and optimizations. S-3-productivity Severe issues that impede the productivity of CockroachDB developers. labels Jan 15, 2021
@rytaft rytaft added this to Triage in BACKLOG, NO NEW ISSUES: SQL Optimizer via automation Jan 15, 2021
@rytaft rytaft moved this from Triage to Plan enhancements (higher priority) in BACKLOG, NO NEW ISSUES: SQL Optimizer Feb 24, 2021
@jlinder jlinder added the T-sql-queries SQL Queries Team label Jun 16, 2021
@github-actions
Copy link

github-actions bot commented Sep 5, 2023

We have marked this issue as stale because it has been inactive for
18 months. If this issue is still relevant, removing the stale label
or adding a comment will keep it active. Otherwise, we'll close it in
10 days to keep the issue queue tidy. Thank you for your contribution
to CockroachDB!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-optimizer SQL logical planning and optimizations. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-productivity Severe issues that impede the productivity of CockroachDB developers. T-sql-queries SQL Queries Team
Projects
BACKLOG, NO NEW ISSUES: SQL Optimizer
Plan enhancements (higher priority)
Status: Backlog
Development

No branches or pull requests

2 participants