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

The filter of outer table happens multiple time after optimizing in-subquery to join #4914

Closed
ygf11 opened this issue Jan 15, 2023 · 2 comments · Fixed by #4944
Closed

The filter of outer table happens multiple time after optimizing in-subquery to join #4914

ygf11 opened this issue Jan 15, 2023 · 2 comments · Fixed by #4944
Labels
enhancement New feature or request

Comments

@ygf11
Copy link
Contributor

ygf11 commented Jan 15, 2023

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

Create table:

❯ create table t1(a int, b int, c int) as values(1,1,1);
❯ create table t2(a int, b int, c int) as values(1,1,1);

run query:

❯ explain verbose select * from t1 where t1.a in (select t2.a from t2) 
                                   and t1.b in(select t2.b from t2) 
                                   and t1.c > 10;

The output plan of decorrelate_where_in:

| logical_plan after decorrelate_where_in                    | Projection: t1.a, t1.b, t1.c                                                                                                          |
|                                                            |   Filter: t1.c > Int32(10) -- filter                                                                                                           |
|                                                            |     LeftSemi Join: t1.b = __correlated_sq_2.b                                                                                         |
|                                                            |       Filter: t1.c > Int32(10) -- filter                                                                                                         |
|                                                            |         LeftSemi Join: t1.a = __correlated_sq_1.a                                                                                     |
|                                                            |           TableScan: t1                                                                                                               |
|                                                            |           SubqueryAlias: __correlated_sq_1                                                                                            |
|                                                            |             Projection: t2.a AS a                                                                                                     |
|                                                            |               TableScan: t2                                                                                                           |
|                                                            |       SubqueryAlias: __correlated_sq_2                                                                                                |
|                                                            |         Projection: t2.b AS b                                                                                                         |
|                                                            |           TableScan: t2  

We can see Filter: t1.c > Int32(10) happen twice.

Describe the solution you'd like
I would like to add the Filter only once.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@ygf11 ygf11 added the enhancement New feature or request label Jan 15, 2023
@ygf11 ygf11 changed the title the outer table filter will happen multiple time when optimize in-subquery to join The filter of outer table happens multiple time when optimize in-subquery to join Jan 15, 2023
@ygf11 ygf11 changed the title The filter of outer table happens multiple time when optimize in-subquery to join The filter of outer table happens multiple time after optimizing in-subquery to join Jan 15, 2023
@ygf11
Copy link
Contributor Author

ygf11 commented Jan 17, 2023

The DecorrelateWhereExists also has same problem.

❯ explain verbose
select *
from t1
where exists
(
    select 1 from t2 where t1.a = t2.a
)
      and exists
(
    select 1 from t2 where t1.b = t2.b
)
      and t1.c > 0;

| logical_plan after decorrelate_where_exists                | Projection: t1.a, t1.b, t1.c                                                                                                          |
|                                                            |   Filter: t1.c > Int32(0) --- filter                                                                                                            |
|                                                            |     LeftSemi Join: t1.b = t2.b                                                                                                        |
|                                                            |       Filter: t1.c > Int32(0) ---filter                                                                                                        |
|                                                            |         LeftSemi Join: t1.a = t2.a                                                                                                    |
|                                                            |           TableScan: t1                                                                                                               |
|                                                            |           TableScan: t2                                                                                                               |
|                                                            |       TableScan: t2  

@alippai
Copy link
Contributor

alippai commented Jan 18, 2023

Filtering before the join can lead to significant speedups when the selectivity is high enough. Is this something to consider? Or will we see it in the TPCH benchmarks? never mind, I see this is mainly about push downs, sorry for the noise :)

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

Successfully merging a pull request may close this issue.

2 participants