-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod logical_optimizer; | ||
mod rule; | ||
pub mod rule; | ||
|
||
pub use logical_optimizer::{LogicalOptimizer, LogicalOptimizerRule}; |
This file contains 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 |
---|---|---|
@@ -1 +1,71 @@ | ||
use crate::optimizer::logical_optimizer::ApplyOrder; | ||
use crate::optimizer::LogicalOptimizerRule; | ||
use crate::planner::logical_plan::{EmptyRelation, LogicalPlan}; | ||
use crate::BustubxResult; | ||
|
||
pub struct EliminateLimit; | ||
|
||
impl LogicalOptimizerRule for EliminateLimit { | ||
fn try_optimize(&self, plan: &LogicalPlan) -> BustubxResult<Option<LogicalPlan>> { | ||
if let LogicalPlan::Limit(limit) = plan { | ||
match limit.limit { | ||
Some(fetch) => { | ||
if fetch == 0 { | ||
return Ok(Some(LogicalPlan::EmptyRelation(EmptyRelation { | ||
produce_one_row: false, | ||
schema: limit.input.schema().clone(), | ||
}))); | ||
} | ||
} | ||
None => { | ||
if limit.offset == 0 { | ||
let input = limit.input.as_ref(); | ||
// input also can be Limit, so we should apply again. | ||
return Ok(Some( | ||
self.try_optimize(input)?.unwrap_or_else(|| input.clone()), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"EliminateLimit" | ||
} | ||
|
||
fn apply_order(&self) -> Option<ApplyOrder> { | ||
Some(ApplyOrder::BottomUp) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::optimizer::rule::EliminateLimit; | ||
use crate::optimizer::LogicalOptimizer; | ||
use crate::planner::logical_plan::LogicalPlan; | ||
use crate::Database; | ||
use std::sync::Arc; | ||
|
||
fn build_optimizer() -> LogicalOptimizer { | ||
LogicalOptimizer::with_rules(vec![Arc::new(EliminateLimit)]) | ||
} | ||
#[test] | ||
fn eliminate_limit() { | ||
let mut db = Database::new_temp().unwrap(); | ||
db.run("create table t1 (a int)").unwrap(); | ||
|
||
let plan = db.create_logical_plan("select a from t1 limit 0").unwrap(); | ||
let optimized_plan = build_optimizer().optimize(&plan).unwrap(); | ||
assert!(matches!(optimized_plan, LogicalPlan::EmptyRelation(_))); | ||
|
||
let plan = db.create_logical_plan("select a from t1 offset 0").unwrap(); | ||
let optimized_plan = build_optimizer().optimize(&plan).unwrap(); | ||
if let LogicalPlan::Project(p) = optimized_plan { | ||
assert!(matches!(p.input.as_ref(), LogicalPlan::TableScan(_))); | ||
} else { | ||
panic!("the first node should be project"); | ||
} | ||
} | ||
} |
This file contains 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