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

Refactor: Add LogicalPlan::observe_expressions to walk expressions #4906

Merged
merged 1 commit into from
Jan 15, 2023

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Jan 14, 2023

Which issue does this PR close?

Inspired by the bug in #4900 and discussion with @avantgardnerio on #4701 (comment)

Rationale for this change

I realized while reading #4900 there were likely other places in Expr that could have subqueries so there may be other bugs lurking similar to #4898

However, there was no good way to walk all expressions in a LogicalPlan other than to call LogicalPlan::expressions() which clones them all

What changes are included in this PR?

  1. Add LogicalPlan::inspect_expressions that calls a function on each expression
  2. Rewrite subquery traversal to use this this function

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the logical-expr Logical plan and expressions label Jan 14, 2023
@@ -233,42 +233,60 @@ impl LogicalPlan {
/// logical plan node. This does not include expressions in any
/// children
pub fn expressions(self: &LogicalPlan) -> Vec<Expr> {
let mut exprs = vec![];
self.observe_expressions(|e| exprs.push(e.clone()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The basic idea is to refactor the current code to clone all expressions to just call a function


fn collect_subqueries(expr: &Expr, sub: &mut Vec<Arc<LogicalPlan>>) {
match expr {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This expr is missing all sorts of possible locations for subqueries (like IS NULL); @askoa fixed one in #4900 but there are very likely more

@alamb alamb changed the title Alamb/walk all exprs Refactor: Add LogicalPlan::observe_expressions to walk expressions Jan 14, 2023
@github-actions github-actions bot added the optimizer Optimizer rules label Jan 14, 2023
@alamb alamb changed the title Refactor: Add LogicalPlan::observe_expressions to walk expressions Refactor: Add LogicalPlan::observe_expressions to walk expressions Jan 14, 2023
@alamb alamb marked this pull request as ready for review January 14, 2023 13:20
// use a synthetic plan so the visitor sees a
// LogicalPlan::Subquery (even though it is
// actually a Subquery alias)
let synthetic_plan = LogicalPlan::Subquery(subquery.clone());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

while this behavior is strange it is consistent with what collect_subqueries was doing

@@ -396,7 +396,7 @@ fn push_down_all_join(
.chain(once(keep_condition.into_iter().reduce(Expr::and).unwrap()))
.collect()
} else {
plan.expressions()
expr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

drive by cleanup to avoid (yet another) copy of the expressions

/// Conveniece function for using a mutable function as an expression visiitor
///
/// TODO make this match names in physical plan
pub fn walk_expr_down<F, E>(expr: &Expr, f: F) -> std::result::Result<(), E>
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

@avantgardnerio avantgardnerio left a comment

Choose a reason for hiding this comment

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

Makes sense 👍

@alamb alamb merged commit d49c805 into apache:master Jan 15, 2023
@ursabot
Copy link

ursabot commented Jan 15, 2023

Benchmark runs are scheduled for baseline = d37dccf and contender = d49c805. d49c805 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
logical-expr Logical plan and expressions optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants