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

Feat: Support correlated subqueries #192

Open
2 of 4 tasks
crwen opened this issue Mar 30, 2024 · 4 comments
Open
2 of 4 tasks

Feat: Support correlated subqueries #192

crwen opened this issue Mar 30, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@crwen
Copy link
Member

crwen commented Mar 30, 2024

Feature Request

there is something wrong when binding correlated subqueries because parent context is ignored.

explain select a,b from t where a in (select a from t1 where a > t.a);
ERROR:  invalid table: t

After fix this bug, we may have the plan like this:

                               PLAN
------------------------------------------------------------------
 Projection [t.a, t.b] [Project]                                 +
   LeftSemi Join On t.a = (t1.a) as (_temp_table_0_.a) [HashJoin]+
     Scan t -> [a, b] [SeqScan]                                  +
     Projection [(t1.a) as (_temp_table_0_.a)] [Project]         +
       Projection [t1.a] [Project]                               +
         Filter (t1.a > t.a), Is Having: false [Filter]          +
           Scan t1 -> [a] [SeqScan]

Filter (t1.a > t.a) can't be handle correctly because it doesn't have the data of t. So we need to pull up this filter.

Maybe we could pull up all filters, and then push them down. so the things we should do next is here:

@KKould
Copy link
Member

KKould commented Mar 30, 2024

It seems that there is something wrong with HashJoin, I'm debugging now, I'll merge this first

@KKould
Copy link
Member

KKould commented Mar 30, 2024

It seems that there is something wrong with HashJoin, I'm debugging now, I'll merge this first

ops, I found the problem. In correlated subqueries, there is no left data in right.
so this case will return empty data (wrong)

let fnck_sql = DataBaseBuilder::path(temp_dir.path())
    .build()
    .await?;
let _ = fnck_sql
    .run("create table t2(id int primary key, a int not null, b int not null);")
    .await?;
let _ = fnck_sql
    .run("create table t3(id int primary key, a int not null, c int not null);")
    .await?;
let _ = fnck_sql
    .run("insert into t2 values (0, 1, 2), (3, 4, 5), (8, 0, 0);")
    .await?;
let _ = fnck_sql
    .run("insert into t3 values (0, 2, 2), (3, 8, 5);")
    .await?;
let (schema, tuples) = fnck_sql.run("select id,a,b from t2 where id in (select a from t3 where a > t2.a);").await?;
println!("{}", create_table(&schema, &tuples));

there is only t3.a on the right but no t2.a, so filter always returns false

+--------------------------------------------------------------------+
| PLAN                                                               |
+====================================================================+
| Projection [t2.id, t2.a, t2.b] [Project]                           |
|   LeftSemi Join On t2.id = (t3.a) as (_temp_table_0_.a) [HashJoin] |
|     Scan t2 -> [id, a, b] [SeqScan]                                |
|     Projection [(t3.a) as (_temp_table_0_.a)] [Project]            |
|       Projection [t3.a] [Project]                                  |
|         Filter (t3.a > t2.a), Is Having: false [Filter]            |
|           Scan t3 -> [a] [SeqScan]                                 |
+--------------------------------------------------------------------+

I think this problem is caused by NormalizationRule: PushPredicateThroughJoin

@crwen
Copy link
Member Author

crwen commented Mar 30, 2024

I think this problem is caused by NormalizationRule: PushPredicateThroughJoin

I don't think it would help. Filter is never pulled up since binding, so it won't be pushed down.

@KKould
Copy link
Member

KKould commented Mar 30, 2024

yep, I made a mistake. This seems to be a feature that has not yet been implemented.

@KKould KKould added enhancement New feature or request bug Something isn't working labels Mar 30, 2024
@KKould KKould removed the bug Something isn't working label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants