-
Notifications
You must be signed in to change notification settings - Fork 849
feat: support self join elimination #19169
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3d3454e
feat: support self join elimination
SkyFan2002 fc3a812
fix marker join
SkyFan2002 5f5e617
fix subquery decorrelate
SkyFan2002 ceb6b54
fix marker join
SkyFan2002 db548e2
fix column id
SkyFan2002 f61c07a
fix logic test
SkyFan2002 645ab39
refactor and add test
SkyFan2002 043786f
fix
SkyFan2002 c1ac8ae
fix
SkyFan2002 85829ab
fix
SkyFan2002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/query/sql/src/planner/optimizer/optimizers/eliminate_self_join.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2021 Datafuse Labs | ||
| // | ||
| // Licensed 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::sync::Arc; | ||
| use std::time::Instant; | ||
|
|
||
| use databend_common_exception::Result; | ||
| use log::info; | ||
|
|
||
| use crate::optimizer::Optimizer; | ||
| use crate::optimizer::OptimizerContext; | ||
| use crate::optimizer::ir::SExpr; | ||
| use crate::optimizer::optimizers::DPhpyOptimizer; | ||
| use crate::optimizer::optimizers::recursive::RecursiveRuleOptimizer; | ||
| use crate::optimizer::optimizers::rule::RuleID; | ||
|
|
||
| pub struct EliminateSelfJoinOptimizer { | ||
| opt_ctx: Arc<OptimizerContext>, | ||
| } | ||
|
|
||
| impl EliminateSelfJoinOptimizer { | ||
| pub fn new(opt_ctx: Arc<OptimizerContext>) -> Self { | ||
| Self { opt_ctx } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl Optimizer for EliminateSelfJoinOptimizer { | ||
| fn name(&self) -> String { | ||
| "EliminateSelfJoinOptimizer".to_string() | ||
| } | ||
|
|
||
| async fn optimize(&mut self, s_expr: &SExpr) -> Result<SExpr> { | ||
| // `EagerAggregation` is used here as a speculative pre-rewrite to expose patterns that | ||
| // `EliminateSelfJoin` can match. If no self-join is actually eliminated, we intentionally | ||
| // return the original input plan to avoid keeping the eager-aggregation rewrite as a | ||
| // standalone optimization. | ||
| let start = Instant::now(); | ||
| static RULES_EAGER_AGGREGATION: &[RuleID] = &[RuleID::EagerAggregation]; | ||
| let optimizer = RecursiveRuleOptimizer::new(self.opt_ctx.clone(), RULES_EAGER_AGGREGATION); | ||
| let s_expr_after_eager_aggregation = optimizer.optimize_sync(s_expr)?; | ||
|
|
||
| static RULES_ELIMINATE_SELF_JOIN: &[RuleID] = &[RuleID::EliminateSelfJoin]; | ||
| let optimizer = | ||
| RecursiveRuleOptimizer::new(self.opt_ctx.clone(), RULES_ELIMINATE_SELF_JOIN); | ||
| let s_expr_after_eliminate_self_join = | ||
| optimizer.optimize_sync(&s_expr_after_eager_aggregation)?; | ||
|
|
||
| let duration = start.elapsed(); | ||
|
|
||
| if s_expr_after_eliminate_self_join == s_expr_after_eager_aggregation { | ||
| return Ok(s_expr.clone()); | ||
| } | ||
|
|
||
| // EliminateSelfJoinOptimizer should ideally run before Dphyp in the optimizer pipeline. | ||
| // However, due to issues with the current EagerAggregation implementation, running it | ||
| // before Dphyp causes TPC-H Q18 optimization to fail. Therefore, EliminateSelfJoinOptimizer | ||
| // is placed after Dphyp, and we run Dphyp again here to ensure join reordering after | ||
| // eliminating self-joins. | ||
| let s_expr_after_dphyp = DPhpyOptimizer::new(self.opt_ctx.clone()) | ||
| .optimize_async(&s_expr_after_eliminate_self_join) | ||
| .await?; | ||
|
|
||
| info!("EliminateSelfJoinOptimizer: {}ms", duration.as_millis()); | ||
|
|
||
| Ok(s_expr_after_dphyp) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.