-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: support correlated subqueries #3288
Comments
Yeah, this is what I meant the other day when I stated that only basic subqueries work. Subqueries can utilize variables from their surrounding context, but we don't handle that yet. |
For reference, the query: query IIIII nosort
SELECT a+b*2+c*3+d*4+e*5,
CASE WHEN a<b-3 THEN 111 WHEN a<=b THEN 222
WHEN a<b+3 THEN 333 ELSE 444 END,
abs(b-c),
(a+b+c+d+e)/5,
a+b*2+c*3
FROM t1
WHERE (e>c OR e<d)
AND d>e
AND EXISTS(SELECT 1 FROM t1 AS x WHERE x.b<t1.b)
ORDER BY 4,2,1,3,5
I don't think that's what's happening here. |
Closing in favour of #3291. |
You're correct that this is not a correlated subquery, but we need to support those as well. |
Yikes! Correlated subqueries look scary (I had no idea such a thing was possible) and seem impossible to support efficiently except in limited cases (when they can be transformed into joins, which is what amazon redshift seems to do). I'm entirely comfortable with not supporting them. |
Apparently correlated subqueries can always be transformed into joins. At least, I recall reading literature that stated that. The point of using a correlated subquery instead of a join is that the query can sometimes be written more naturally that way. I don't think we should support correlated subqueries before we support joins and then we should only support them by transforming them into joins. |
@knz is this done? |
Not yet, wanna take it? |
I was just making sure this wasn't done with the other JOIN work and that the issue should remain open. |
This feature is required by the ActiveRecord query issued in #12783. In case it's useful, here is a simple query that requires this feature that Postgres supports and we don't. In Postgres:
In CockroachDB:
|
The example I posted seems to be a limited subcase of generalized correlated queries. Since ActiveRecord only needs that limited subcase, I've opened #12993 to track this particular subcase. |
I take it back. The query in #12783 does actually need full correlated subquery support. |
Prior to this patch, a client trying to use an unsupported correlated query would encounter an obscure error like "column v does not exist" or "no data source matches prefix". Given that the new optimizer code can determine whether a query is correlated, we can use this information to enhance the error message. Before: ``` > select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid); pq: no data source matches prefix: a > select * from pg_class a where exists (select * from kv where v::oid = oid); pq: column "oid" does not exist ``` After: ``` > select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid); pq: no data source matches prefix: a HINT: some correlated subqueries are not supported yet - see cockroachdb#3288 > select * from pg_class a where exists (select * from kv where v::oid = oid); pq: column "oid" does not exist HINT: some correlated subqueries are not supported yet - see cockroachdb#3288 ``` Note: some correlated queries do not benefit from this improvement, specifically those for which the optimizer code aborts early before it has detected correlation (e.g. because of some other unrelated feature). Release note (sql change): CockroachDB will now report a hint in the error message if it encounters a correlated query that it does not support yet.
27390: sql: properly reject nested generators in ROWS FROM r=knz a=knz Fixes #27389. Prior to this patch, invalid uses of SRFs as arguments to other functions in ROWS FROM were not properly rejected, and were only caught at evaluation time (i.e. much too late). This patch fixes it by rejecting these uses early. Release note (bug fix): invalid uses of set-generating functions in FROM clauses are now reported with the same error code as PostgreSQL. 27396: sql: help the user understand unsupported correlation r=knz a=knz Fixes #24684. Prior to this patch, a client trying to use an unsupported correlated query would encounter an obscure error like "column v does not exist" or "no data source matches prefix". Given that the new optimizer code can determine whether a query is correlated, we can use this information to enhance the error message. Before: ``` > select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid); pq: no data source matches prefix: a > select * from pg_class a where exists (select * from kv where v::oid = oid); pq: column "oid" does not exist ``` After: ``` > select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid); pq: no data source matches prefix: a HINT: some correlated subqueries are not supported yet - see #3288 > select * from pg_class a where exists (select * from kv where v::oid = oid); pq: column "oid" does not exist HINT: some correlated subqueries are not supported yet - see #3288 ``` Note: some correlated queries do not benefit from this improvement, specifically those for which the optimizer code aborts early before it has detected correlation (e.g. because of some other unrelated feature). Release note (sql change): CockroachDB will now report a hint in the error message if it encounters a correlated query that it does not support yet. Co-authored-by: Raphael 'kena' Poss <[email protected]>
Zendesk ticket #2713 has been linked to this issue. |
Correlated subquery support is now part of 2.1, so closing this issue down. |
I'm running v2.1.0-beta.20180904, hoping to get Symfony+Doctrine ORM working with Cockroachdb. The query below, however, returns this error message:
Can you provide guidance? I'd assume that either the correlated subquery stuff somehow didn't make it into v2.1.0-beta.20180904, or that the error message is also used for a different issue.
/cc @andy-kimball @knz |
sqlfmt formatted for the interested |
I think the issue should stay open until all correlated subqueries are supported, including the apply operator for those that cannot be decorrelated. @andy-kimball Lauren and I are planning to make a writeup in the docs of the limits of decorrelation currently. Perhaps we could link to that from here. |
The posted query has two issues that prevent decorrelation:
|
Thanks for clarifying. Typically, I'd be more than happy to simply rewrite a query or two. In this case though, this query is generated by Doctrine when it tries to see what the current schema looks like when creating migration scripts (./doctrine migrations:diff) As such, there's not an easy work-around where I can just rewrite one or two of my own queries. For the time being I'll just use mysql instead, but figured this bit of context might help in determining the appropriate priority for support of the posted query.. |
@jordanlewis can we close this now? |
It seems this doesn't work for DELETE queries yet. When I run this, I get the expected result:
But when I replace SELECT with DELETE it breaks:
I'm on cockroach 2.1.6 |
@Fornax96 I expect this to work on our upcoming 19.1 release. You can try a beta here (https://www.cockroachlabs.com/docs/releases/v19.1.0-rc.2.html). |
Awesome, I'll try it out. |
Upgraded to 19.1, it works now! |
I'm closing this issue out, since we've now released 19.1 with support for correlated subqueries both in read-only and mutation statements. |
Hi I'm not sure if this is related to this issue, but this is where I ended up on a search of my problem. I'm trying stuff out in cockroachdb, learning the ropes still, and I'm trying to use the
But I am getting an error:
If I run these separately they work fine (supplying the data as needed). I also tried:
but I get:
I know there are workarounds, but these introduce (worse) race conditions that I would like to avoid if possible. Am I doing something wrong, or is what I am trying to do not yet implemented? Thanks. build: CCL v19.2.6 @ 2020/04/06 18:05:31 (go1.12.12) Edit: |
@insaner this is a legitimate need but it's a new issue. Please file a new issue and copy-paste the text of your submission. |
Perfect, thanks for looking into this. I've filed a new issue. |
In case someone else comes upon this issue, this does work in CRDB and PG, it's just that the syntax wasn't quite right. It should be like this instead:
|
https://github.com/cockroachdb/sqllogictest/blob/a88396b84bb1fe62edf2e072e585d68c0b2ecdca/test/select1.test#L109
Fails with:
logic_test.go:420: ../../sqllogictest/test/select1.test:109: expected success, but found pq: qualified name "x.b" not found
I believe this is because the subquery references both
x
andt1
(wheret1
is from the parentFROM
clause), but onlyx
is in the visible tables list.The description above was conflating two bugs into one. This is a real bug, but it isn't caused by the linked query.
The text was updated successfully, but these errors were encountered: