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

support join on or predicates rewrite to Union All #55376

Open
stephen-shelby opened this issue Jan 23, 2025 · 1 comment
Open

support join on or predicates rewrite to Union All #55376

stephen-shelby opened this issue Jan 23, 2025 · 1 comment
Labels
type/enhancement Make an enhancement to StarRocks

Comments

@stephen-shelby
Copy link
Contributor

stephen-shelby commented Jan 23, 2025

Enhancement

Take the following SQL as an example:
select * from left_table l join right_table r on l.k1 = r.v1 or l.k1 = r.v2

currently, we will rewrite the join type to nestloop join. This performance is relatively poor.

Actually, we can rewrite this sql to

select * from left_table l join right_table r on l.k1 = r.v1 and l.k1 = r.k2
union all
select * from left_table l join right_table r on l.k1 != r.v1 and l.k1 = r.k2
union all
select * from left_table l join right_table r on l.k1 = r.v1 and l.k1 != r.k2

we want to rewrite join on or predicate to union all. this will have a higher performance than nest loop join.

you could write a rule to rewrite this kind query.

you need to test whether the performance is better than nest loop join in the following two case:

  • multiple or predicate
  • non-related equal predicate like on l.k1 = r.v1 or l.k2 = r.v2
@stephen-shelby stephen-shelby added the type/enhancement Make an enhancement to StarRocks label Jan 23, 2025
@KKould
Copy link

KKould commented Feb 1, 2025

i think this optimization can also be applied to this case: #52399, using the on predicate of inner join to convert cross join into union all

In what the issue expects (T0 X T1 X T2), they can become sql like this:

select a, null as b, null as c from t0
union all
select null as a, b, null as c from t1
union all
select null as a, null as b, null as c from t2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement Make an enhancement to StarRocks
Projects
None yet
Development

No branches or pull requests

2 participants