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

Move the extract_join_keys to optimizer #4711

Merged
merged 5 commits into from
Dec 27, 2022

Conversation

ygf11
Copy link
Contributor

@ygf11 ygf11 commented Dec 22, 2022

Which issue does this PR close?

Closes #4710.

Rationale for this change

See #4710

What changes are included in this PR?

  • Remove the usage of extract join keys in sql planner.
  • Add ExtractEquijoinPredicate to optimizer.

Are these changes tested?

Yes.

Are there any user-facing changes?

@github-actions github-actions bot added optimizer Optimizer rules sql SQL Planner labels Dec 22, 2022
@ygf11 ygf11 marked this pull request as ready for review December 22, 2022 13:15
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ygf11 -- this PR looks very nice 👌

@@ -806,7 +805,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
) -> Result<LogicalPlan> {
match constraint {
JoinConstraint::On(sql_expr) => {
let mut keys: Vec<(Expr, Expr)> = vec![];
// let mut keys: Vec<(Expr, Expr)> = vec![];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forget it. Removed now.


Ok(plan)
}
_ => Ok(Some(optimize_children(self, plan, config)?)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackwener has been removing direct recursion in optimizer rules -- for example see #4687

Perhaps we could do the same with this rule too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do it.

Utilizing the traverse of optimizer now.

)?
.build()?;
let expected = vec![
"Left Join: t1.a = t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 314 to 318
Some(binary_expr(
(col("t1.a") + lit(10i64)).gt_eq(col("t2.a") * lit(2u32)),
Operator::And,
col("t1.b").lt(lit(100i32)),
)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thunk you can use .and here if you want (untested)

Suggested change
Some(binary_expr(
(col("t1.a") + lit(10i64)).gt_eq(col("t2.a") * lit(2u32)),
Operator::And,
col("t1.b").lt(lit(100i32)),
)),
Some(
col("t1.a") + lit(10i64)).gt_eq(col("t2.a") * lit(2u32))
.and(col("t1.b").lt(lit(100i32)))
)),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is short and clear, fixed.

}

#[cfg(test)]
mod tests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice tests

Copy link
Member

@jackwener jackwener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice job to me👍 except a possible problem.

Comment on lines -829 to -832
// expression that didn't match equi-join pattern
let mut filter = vec![];

// extract join keys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to see them are removed from planner

@jackwener
Copy link
Member

jackwener commented Dec 24, 2022

I'm not sure if some special case will meet problem. like:

A join (B join C on B.id = C.id) on A.id = B.id and B.key = C.key.

B.key = C.key will be pushdown in PushdownFilter. But now this rule is front of it. Can this rule ignore B.key = C.key? or we need reorder rule?

@jackwener
Copy link
Member

jackwener commented Dec 24, 2022

I'm not sure if some special case will meet problem. like:

A join (B join C on B.id = C.id) on A.id = B.id and B.key = C.key.

B.key = C.key will be pushdown in PushdownFilter. But now this rule is front of it. Can this rule ignore B.key = C.key? or we need reorder rule?

I think carefully it's ok, because it's OK when it exist in planner, it don't cause problem to just move it into optimizer.

and @ygf11 added a new test for it.

So LGTM.

@ygf11
Copy link
Contributor Author

ygf11 commented Dec 24, 2022

@jackwener Thanks for review. I think it is ok. Following up are the reason.

But now this rule is previous. Can this rule ignore B.key = C.key? or we need reorder rule?

No, this rule will ignore B.key = C.key.

Because this rule will treat eq expression as equijoin only when each side of eq belong to one input.
For B.key = C.key, B.key and C.key are both belong to B join C, no key is belong to A so this predicate will be added to the filter.

@@ -237,6 +238,7 @@ impl Optimizer {
let rules: Vec<Arc<dyn OptimizerRule + Sync + Send>> = vec![
Arc::new(InlineTableScan::new()),
Arc::new(TypeCoercion::new()),
Arc::new(ExtractEquijoinPredicate::new()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some subquery will be converted to the join in the following up SubqueryFilterToJoin rule., and I am not sure if this rule is right in this position

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ygf11 ygf11 Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this rule should be placed behind SubqueryFilterToJoin rule. @jackwener also suggested about it in #4725 (comment).

I think it is ok now, because SubqueryFilterToJoin and other similar rule directly specify the equijoin predicate, so they do not depends ExtractEquijoinPredicate currently.

I plan to do this movement after #4725 merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this rule should be placed behind SubqueryFilterToJoin rule. @jackwener also suggested about it in #4725 (comment).

I think it is ok now, because SubqueryFilterToJoin and other similar rule directly specify the equijoin predicate, so they do not depends ExtractEquijoinPredicate currently.

Yes, other rule will convert query to join with specified equal-join predicate and non-equal-join predicate.

I plan to do this movement after #4725 merged.

LGTM

Copy link
Contributor

@liukun4515 liukun4515 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
If there is no other comments until tomorrow, i will merge it.

Thanks @ygf11

@liukun4515 liukun4515 merged commit 734c211 into apache:master Dec 27, 2022
@ursabot
Copy link

ursabot commented Dec 27, 2022

Benchmark runs are scheduled for baseline = 9331ee3 and contender = 734c211. 734c211 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move the extracting join keys logic to optimizer
5 participants