You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "mslapek (via GitHub)" <gi...@apache.org> on 2023/03/16 18:03:58 UTC
[GitHub] [arrow-datafusion] mslapek opened a new pull request, #5623: Add LogicalPlanSignature and use in the optimizer loop
mslapek opened a new pull request, #5623:
URL: https://github.com/apache/arrow-datafusion/pull/5623
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #3892
Removes a blocker for #4628 (allows to have only one `LogicalPlan` in RAM)
Attractive alternative for returning `Option<LogicalPlan>` in the optimizer loop (see the comment https://github.com/apache/arrow-datafusion/issues/4628#issuecomment-1470106106)
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
Explained in the issue.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
Added a new structure `LogicalPlanSignature` with `compute` method.
Used `LogicalPlanSignature` in the main optimization loop to detect cycles with hash-set.
Additionally raised default `max_passes` in optimizer, because we can detect cycles.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
New unit-tests in the new module with `LogicalPlanSignature`.
Existing tests for the optimizer loop.
# Are there any user-facing changes?
Changed default `max_passes` in optimizer.
NO API changes. The new module with `LogicalPlanSignature` is **private** (`mod` vs `pub use`).
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add LogicalPlanSignature and use in the optimizer loop [datafusion]
Posted by "findepi (via GitHub)" <gi...@apache.org>.
findepi commented on code in PR #5623:
URL: https://github.com/apache/datafusion/pull/5623#discussion_r1664596130
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -330,15 +324,14 @@ impl Optimizer {
}
log_plan(&format!("Optimized plan (pass {i})"), &new_plan);
- // TODO this is an expensive way to see if the optimizer did anything and
- // it would be better to change the OptimizerRule trait to return an Option
- // instead
- if old_plan.as_ref() == &new_plan {
+ // HashSet::insert returns, whether the value was newly inserted.
+ let plan_is_fresh =
+ previous_plans.insert(LogicalPlanSignature::new(&new_plan));
+ if !plan_is_fresh {
Review Comment:
@mslapek thanks for response!
> What motivated you to review this merged (one year ago) PR?
I find the source PR as an efficient way to ask a question about the code and reach people with context -- code author(s) and reviewers -- in one shot.
> Yes, a cycle shows a logical error in the optimizer - because at each step the performance should be improved.
Agreed!
> At the same time, it does NOT imply a correctness error. The plan probably will be still correct (but not optimal).
Agreed!
> We should take the perspective of a user. If a data scientist does research, should we harm the availability of the database?
Suboptimal plan can be orders of magnitude more expensive to execute, so allowing it to run may cause unavailability for others, but I see your point. It's especially difficult to transition from bug lenient treatment to more strict. It should be gradual. I like the idea of having this initially controlled with a flag. In tests and on CI this flag should be set to "fail". Then we can switch the flag on for runtime as well.
@alamb @jackwener thoughts?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add LogicalPlanSignature and use in the optimizer loop [datafusion]
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on code in PR #5623:
URL: https://github.com/apache/datafusion/pull/5623#discussion_r1664441817
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -330,15 +324,14 @@ impl Optimizer {
}
log_plan(&format!("Optimized plan (pass {i})"), &new_plan);
- // TODO this is an expensive way to see if the optimizer did anything and
- // it would be better to change the OptimizerRule trait to return an Option
- // instead
- if old_plan.as_ref() == &new_plan {
+ // HashSet::insert returns, whether the value was newly inserted.
+ let plan_is_fresh =
+ previous_plans.insert(LogicalPlanSignature::new(&new_plan));
+ if !plan_is_fresh {
Review Comment:
Yes, a cycle shows a logical error in the optimizer - because at each step the performance should be improved.
At the same time, it does NOT imply a correctness error. The plan probably will be still correct (but not optimal).
> IMO cycle should be error condition
We should take the perspective of a user. If a data scientist does research, should we harm the availability of the database?
IMO Maybe a log of this situation could be useful, with a configuration flag turning this into an error...
@findepi Btw. What motivated you to review this merged (one year ago) PR?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add LogicalPlanSignature and use in the optimizer loop [datafusion]
Posted by "findepi (via GitHub)" <gi...@apache.org>.
findepi commented on code in PR #5623:
URL: https://github.com/apache/datafusion/pull/5623#discussion_r1664186593
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -330,15 +324,14 @@ impl Optimizer {
}
log_plan(&format!("Optimized plan (pass {i})"), &new_plan);
- // TODO this is an expensive way to see if the optimizer did anything and
- // it would be better to change the OptimizerRule trait to return an Option
- // instead
- if old_plan.as_ref() == &new_plan {
+ // HashSet::insert returns, whether the value was newly inserted.
+ let plan_is_fresh =
+ previous_plans.insert(LogicalPlanSignature::new(&new_plan));
+ if !plan_is_fresh {
Review Comment:
At this point we detected optimization cycle. Cycles are bad, so we exit. Exiting isn't ideal because our plan is optimized yet. We simply stopped applying rules.
IMO cycle should be error condition
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add LogicalPlanSignature and use in the optimizer loop [datafusion]
Posted by "findepi (via GitHub)" <gi...@apache.org>.
findepi commented on code in PR #5623:
URL: https://github.com/apache/datafusion/pull/5623#discussion_r1667008120
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -330,15 +324,14 @@ impl Optimizer {
}
log_plan(&format!("Optimized plan (pass {i})"), &new_plan);
- // TODO this is an expensive way to see if the optimizer did anything and
- // it would be better to change the OptimizerRule trait to return an Option
- // instead
- if old_plan.as_ref() == &new_plan {
+ // HashSet::insert returns, whether the value was newly inserted.
+ let plan_is_fresh =
+ previous_plans.insert(LogicalPlanSignature::new(&new_plan));
+ if !plan_is_fresh {
Review Comment:
filed https://github.com/apache/datafusion/issues/11285 for this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
[GitHub] [arrow-datafusion] mslapek commented on pull request #5623: Add LogicalPlanSignature and use in the optimizer loop
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on PR #5623:
URL: https://github.com/apache/arrow-datafusion/pull/5623#issuecomment-1478996770
@alamb @jackwener Thanks for the review! 🪀
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb merged pull request #5623: Add LogicalPlanSignature and use in the optimizer loop
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5623:
URL: https://github.com/apache/arrow-datafusion/pull/5623
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mslapek commented on a diff in pull request #5623: Add LogicalPlanSignature and use in the optimizer loop
Posted by "mslapek (via GitHub)" <gi...@apache.org>.
mslapek commented on code in PR #5623:
URL: https://github.com/apache/arrow-datafusion/pull/5623#discussion_r1141080502
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -419,6 +412,34 @@ impl Optimizer {
}
}
+/// Returns an error if plans have different schemas.
Review Comment:
The Boy Scout Rule: "Leave the campground cleaner than you found it" 🤣
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5623: Add LogicalPlanSignature and use in the optimizer loop
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5623:
URL: https://github.com/apache/arrow-datafusion/pull/5623#discussion_r1141036350
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -419,6 +412,34 @@ impl Optimizer {
}
}
+/// Returns an error if plans have different schemas.
Review Comment:
This is a nice refactor
##########
datafusion/optimizer/src/plan_signature.rs:
##########
@@ -0,0 +1,132 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::{
+ collections::hash_map::DefaultHasher,
+ convert::Infallible,
+ hash::{Hash, Hasher},
+ num::NonZeroUsize,
+};
+
+use datafusion_expr::{LogicalPlan, PlanVisitor};
+
+/// Non-unique identifier of a [`LogicalPlan`].
+///
+/// See [`LogicalPlanSignature::compute`] for details.
+#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+pub struct LogicalPlanSignature {
+ node_number: NonZeroUsize,
+ plan_hash: u64,
+}
+
+impl LogicalPlanSignature {
+ /// Returns [`LogicalPlanSignature`] of the given [`LogicalPlan`].
+ ///
+ /// It is a kind of [`LogicalPlan`] hashing with stronger guarantees.
+ ///
+ /// # Guarantees
+ ///
+ /// Consider two [`LogicalPlan`]s `p1` and `p2`.
+ ///
+ /// If `p1` and `p2` have a different number of [`LogicalPlan`]s, then
+ /// they will have different [`LogicalPlanSignature`]s.
+ ///
+ /// If `p1` and `p2` have a different [`Hash`], then
+ /// they will have different [`LogicalPlanSignature`]s.
+ ///
+ /// # Caveats
+ ///
+ /// The intention of [`LogicalPlanSignature`] is to have a lower chance
+ /// of hash collisions.
+ ///
+ /// There exist different [`LogicalPlan`]s with the same
+ /// [`LogicalPlanSignature`].
+ ///
+ /// When two [`LogicalPlan`]s differ only in metadata, then they will have
+ /// the same [`LogicalPlanSignature`]s (due to hash implementation in
+ /// [`LogicalPlan`]).
+ pub fn compute(plan: &LogicalPlan) -> Self {
Review Comment:
I think `pub fn new()` would be more idiomatic to construct a new plan
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add LogicalPlanSignature and use in the optimizer loop [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5623:
URL: https://github.com/apache/datafusion/pull/5623#discussion_r1666983056
##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -330,15 +324,14 @@ impl Optimizer {
}
log_plan(&format!("Optimized plan (pass {i})"), &new_plan);
- // TODO this is an expensive way to see if the optimizer did anything and
- // it would be better to change the OptimizerRule trait to return an Option
- // instead
- if old_plan.as_ref() == &new_plan {
+ // HashSet::insert returns, whether the value was newly inserted.
+ let plan_is_fresh =
+ previous_plans.insert(LogicalPlanSignature::new(&new_plan));
+ if !plan_is_fresh {
Review Comment:
Thank you for bringing this up @mslapek and @findepi
> Suboptimal plan can be orders of magnitude more expensive to execute, so allowing it to run may cause unavailability for others, but I see your point. It's especially difficult to transition from bug lenient treatment to more strict. It should be gradual. I like the idea of having this initially controlled with a flag. In tests and on CI this flag should be set to "fail". Then we can switch the flag on for runtime as well.
My thoughts are that I can see the tradeoffs with both behavior:
1. Fail fast (raise an error if cycle is detected) that @findepi increases the liklihood that such an error would be found and fixed quickly (as it would prevent the query in question from running)
2. Lenient (ignore error) user still gets their answer (though maybe woudl take much longer)
One middle ground might be as @findepi suggests and use a flag -- we could default to raising an error if a cycle was detected but have a way for users to ignore the error and proceed anyways. As long as the error message said how to work around the error I think it would be fine.
In fact we have a similar setting already for failed optimizer rules, for many of the same reasons discussed, that we could model the behavior on `datafusion.optimizer.skip_failed_rules`: https://datafusion.apache.org/user-guide/configs.html
datafusion.optimizer.skip_failed_rules | false | When set to true, the logical plan optimizer will produce warning messages if any optimization rules produce errors and then proceed to the next rule. When set to false, any rules that produce errors will cause the query to fail
-- | -- | --
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org