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

sql: cannot apply ARRAY() over correlated subquery #35710

Closed
knz opened this issue Mar 13, 2019 · 5 comments
Closed

sql: cannot apply ARRAY() over correlated subquery #35710

knz opened this issue Mar 13, 2019 · 5 comments
Labels
A-sql-optimizer SQL logical planning and optimizations. A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team X-anchored-telemetry The issue number is anchored by telemetry references.

Comments

@knz
Copy link
Contributor

knz commented Mar 13, 2019

Consider the following SQL:

CREATE TABLE u (x INT);
insert into u values(7),(8);
CREATE TABLE v (y INT[]);
insert into v values(array[1,2,3]), (array[4,5,6]);
SELECT ARRAY(SELECT (y, 2) FROM u ORDER BY x) FROM v;

This currently fails in CockroachDB with "can't execute a correlated ARRAY.."

PostgreSQL produces this:

                 array
---------------------------------------
 {"(\"{1,2,3}\",2)","(\"{1,2,3}\",2)"}
 {"(\"{1,2,3}\",2)","(\"{1,2,3}\",2)"}
 {"(\"{4,5,6}\",2)","(\"{4,5,6}\",2)"}
(3 rows)

with the following logical plan:

                                QUERY PLAN
---------------------------------------------------------------------------
 Seq Scan on public.v  (cost=0.00..253197.92 rows=1360 width=32)
   Output: (SubPlan 1)
   SubPlan 1
     ->  Sort  (cost=179.78..186.16 rows=2550 width=36)
           Output: (ROW(v.y, 2)), u.x
           Sort Key: u.x
           ->  Seq Scan on public.u  (cost=0.00..35.50 rows=2550 width=36)
                 Output: ROW(v.y, 2), u.x

``

Jira issue: CRDB-4562
@knz knz added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-optimizer SQL logical planning and optimizations. X-anchored-telemetry The issue number is anchored by telemetry references. labels Mar 13, 2019
@Emill
Copy link

Emill commented Apr 7, 2021

Supporting this would be a good feature when we want to query one or multiple levels of related data, which are usually done using joins. But joins have the disadvantage that a lot of duplicated data is returned. Joins with three or more tables can also be problematic from a performance point of view.

Take this as a simple example:

select *, array(select row(tbl2.*) from tbl2 where tbl1.id = tbl2.tbl1id) from tbl1

This fetches all rows in tbl1, together with the related rows in tbl2, without duplicate data.

I'm working on implementing this in EF Core, see npgsql/efcore.pg#1691.

I also see that array_agg is also not working currently together with the row constructor, so that approach can't be used either.

@Emill
Copy link

Emill commented Apr 7, 2021

Seems like the query engine supports it, since casting the row to text first works:

select *, array(select (tbl2.*)::text from tbl2 where tbl1.id = tbl2.tbl1id) from tbl1

so I guess it's just adding support for propagating the plain value (instead of a row materialized as text) that's currently not working.

@knz
Copy link
Contributor Author

knz commented Apr 12, 2021

cc @RaduBerinde for re-triage

@RaduBerinde
Copy link
Member

We are missing array_agg overloads for tuples (array_agg is used internally when decorrelating such queries).

@jlinder jlinder added the T-sql-queries SQL Queries Team label Jun 16, 2021
@jordanlewis
Copy link
Member

No longer repros, and we now support array_agg for tuples as well.

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. A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team X-anchored-telemetry The issue number is anchored by telemetry references.
Projects
Archived in project
Development

No branches or pull requests

5 participants