Skip to content

Commit

Permalink
Add LogicalOptimizerRule trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 28, 2024
1 parent 7ae3ed8 commit cc23bf3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bustubx/src/optimizer/logical_optimizer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::error::BustubxResult;
use crate::planner::logical_plan::LogicalPlan;
use std::sync::Arc;

/// `LogicalOptimizerRule` transforms one [`LogicalPlan`] into another which
/// computes the same results, but in a potentially more efficient
/// way. If there are no suitable transformations for the input plan,
/// the optimizer can simply return it as is.
pub trait LogicalOptimizerRule {
/// Try and rewrite `plan` to an optimized form, returning None if the plan cannot be
/// optimized by this rule.
fn try_optimize(&self, plan: &LogicalPlan) -> BustubxResult<Option<LogicalPlan>>;

/// A human readable name for this optimizer rule
fn name(&self) -> &str;

/// How should the rule be applied by the optimizer
///
/// If a rule use default None, it should traverse recursively plan inside itself
fn apply_order(&self) -> Option<ApplyOrder> {
None
}
}

pub enum ApplyOrder {
TopDown,
BottomUp,
}

#[derive(Clone)]
pub struct LogicalOptimizer {
/// All optimizer rules to apply
pub rules: Vec<Arc<dyn LogicalOptimizerRule + Send + Sync>>,
}

impl LogicalOptimizer {
pub fn new() -> Self {
let rules: Vec<Arc<dyn LogicalOptimizerRule + Sync + Send>> = vec![];

Self { rules }
}

pub fn optimize(&self, plan: &LogicalPlan) -> BustubxResult<LogicalPlan> {
todo!()
}
}
3 changes: 3 additions & 0 deletions bustubx/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use crate::planner::logical_plan::LogicalPlan;

use self::{physical_optimizer::PhysicalOptimizer, physical_plan::PhysicalPlan};

mod logical_optimizer;
pub mod physical_optimizer;
pub mod physical_plan;

pub use logical_optimizer::{LogicalOptimizer, LogicalOptimizerRule};

Check warning on line 11 in bustubx/src/optimizer/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `LogicalOptimizerRule`, `LogicalOptimizer`

pub struct Optimizer {
physical_optimizer: PhysicalOptimizer,
}
Expand Down

0 comments on commit cc23bf3

Please sign in to comment.