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

remove recursion in optimizer rules #4650

Merged
merged 1 commit into from
Dec 20, 2022
Merged

Conversation

jackwener
Copy link
Member

@jackwener jackwener commented Dec 15, 2022

Which issue does this PR close?

Closes #.

Rationale for this change

Remove recursion in optimizer rules. and utilize the optimizer traverse the plan tree.

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

Comment on lines +1330 to +1341
pub predicate: Expr,
/// The incoming logical plan
input: Arc<LogicalPlan>,
pub input: Arc<LogicalPlan>,
Copy link
Member Author

Choose a reason for hiding this comment

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

pub them because I need pattern-match in rules.

Discussion about it in #4464.

cc @alamb @andygrove @tustvold .

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is ok -- it would be nice to add some comments explaining a Filter should not be created directly but instead use try_new() and that these fields are only pub to support pattern matching

Comment on lines 47 to 52
LogicalPlan::Filter(Filter {
predicate: Expr::Literal(ScalarValue::Boolean(Some(v))),
input,
}) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Nested Pattern-match here, so I make filter public

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if you could use a match guard?

   LogicalPlan::Filter(filter) if matches(!filter.expr(), Expr::Literal(ScalarValue::Boolean(Some(v))))) { 
...

@github-actions github-actions bot added core Core datafusion crate logical-expr Logical plan and expressions optimizer Optimizer rules labels Dec 15, 2022
@jackwener
Copy link
Member Author

Rely on #4618

@alamb alamb marked this pull request as draft December 15, 2022 19:51
@alamb
Copy link
Contributor

alamb commented Dec 15, 2022

Marking as draft as it builds on #4618

@jackwener jackwener marked this pull request as ready for review December 19, 2022 05:32
@jackwener jackwener force-pushed the remove_recursion branch 2 times, most recently from b21faa9 to 4e2afb7 Compare December 19, 2022 05:40
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.

As usual this is looking great @jackwener . Thank you 🙏

From my perspective this PR is basically ready to go except for:

  1. If possible I think it would be worth the time trying to avoid making Filter fields pub as we have discussed earlier. I left a suggestion on how maybe that could be done.
  2. Double check the Top Down vs Bottom Up changes in sql optimizer passes

Really nicely done and thank you again.

Comment on lines +1330 to +1341
pub predicate: Expr,
/// The incoming logical plan
input: Arc<LogicalPlan>,
pub input: Arc<LogicalPlan>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is ok -- it would be nice to add some comments explaining a Filter should not be created directly but instead use try_new() and that these fields are only pub to support pattern matching

}
}

fn name(&self) -> &str {
"decorrelate_where_in"
}

fn apply_order(&self) -> Option<ApplyOrder> {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a really nice pattern 👨‍🍳 👌

@@ -1,17 +1,17 @@
Sort: supplier.s_name ASC NULLS LAST
Projection: supplier.s_name, supplier.s_address
LeftSemi Join: supplier.s_suppkey = __sq_2.ps_suppkey
LeftSemi Join: supplier.s_suppkey = __sq_1.ps_suppkey
Copy link
Contributor

Choose a reason for hiding this comment

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

these changes imply the decorrelate passes used to be applied bottom up and after this PR they are applied top-down

Is that intentional? Or maybe I am misreading the diff 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

😂 original implementation mix TopDown and BottomUp.
It use TopDown overall, but use BottomUp when match the subPlan.
I think it's a small mistake in original code but don't affect correctness.

Comment on lines 47 to 52
LogicalPlan::Filter(Filter {
predicate: Expr::Literal(ScalarValue::Boolean(Some(v))),
input,
}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if you could use a match guard?

   LogicalPlan::Filter(filter) if matches(!filter.expr(), Expr::Literal(ScalarValue::Boolean(Some(v))))) { 
...

@jackwener
Copy link
Member Author

If possible I think it would be worth the time trying to avoid making Filter fields pub as we have discussed earlier. I left a suggestion on how maybe that could be done.

I find a good way to resolve it by adding #[non_exhaustive]

@alamb
Copy link
Contributor

alamb commented Dec 20, 2022

I find a good way to resolve it by adding #[non_exhaustive]

This is reasonable (it will prevent construction outside of the crate, so in datafusion core and planner) 👍 nice

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 @jackwener

@alamb alamb merged commit 2792113 into apache:master Dec 20, 2022
@ursabot
Copy link

ursabot commented Dec 20, 2022

Benchmark runs are scheduled for baseline = fe477e4 and contender = 2792113. 2792113 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
core Core datafusion crate logical-expr Logical plan and expressions optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants