Skip to content

Commit

Permalink
refactor(core): drop children_cost on the cost model, doc property bu…
Browse files Browse the repository at this point in the history
…ilders (#235)

As discussed today :)

Signed-off-by: Alex Chi <[email protected]>
  • Loading branch information
skyzh authored Nov 13, 2024
1 parent 342f2fb commit 91cd0a2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 22 deletions.
1 change: 0 additions & 1 deletion optd-core/src/cascades/tasks/optimize_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ impl<T: NodeType, M: Memo<T>> Task<T, M> for OptimizeInputsTask {
&expr.typ,
&preds,
&input_statistics_ref,
&input_cost,
Some(context.clone()),
Some(optimizer),
);
Expand Down
1 change: 0 additions & 1 deletion optd-core/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub trait CostModel<T: NodeType, M: Memo<T>>: 'static + Send + Sync {
node: &T,
predicates: &[ArcPredNode<T>],
children_stats: &[Option<&Statistics>],
children_costs: &[Cost],
context: Option<RelNodeContext>,
optimizer: Option<&CascadesOptimizer<T, M>>,
) -> Cost;
Expand Down
6 changes: 6 additions & 0 deletions optd-core/src/logical_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ use std::fmt::{Debug, Display};

use crate::nodes::{ArcPredNode, NodeType};

/// The trait enables we store any logical property in the memo table by erasing the concrete type.
/// In the future, we can implement `serialize`/`deserialize` on this trait so that we can serialize
/// the logical properties.
pub trait LogicalProperty: 'static + Any + Send + Sync + Debug + Display {
fn as_any(&self) -> &dyn Any;
}

/// A wrapper around the `LogicalPropertyBuilder` so that we can erase the concrete type and store
/// it safely in the memo table.
pub trait LogicalPropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
fn derive_any(
&self,
Expand All @@ -22,6 +27,7 @@ pub trait LogicalPropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
fn property_name(&self) -> &'static str;
}

/// The trait for building logical properties for a plan node.
pub trait LogicalPropertyBuilder<T: NodeType>: 'static + Send + Sync + Sized {
type Prop: LogicalProperty + Sized + Clone;

Expand Down
6 changes: 6 additions & 0 deletions optd-core/src/physical_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ use itertools::Itertools;

use crate::nodes::{ArcPredNode, NodeType, PlanNode, PlanNodeOrGroup};

/// The trait enables we store any physical property in the memo table by erasing the concrete type.
/// In the future, we can implement `serialize`/`deserialize` on this trait so that we can serialize
/// the physical properties.
pub trait PhysicalProperty: 'static + Any + Send + Sync + Debug + Display {
fn as_any(&self) -> &dyn Any;
fn to_boxed(&self) -> Box<dyn PhysicalProperty>;
}

/// A wrapper around the `PhysicalPropertyBuilder` so that we can erase the concrete type and store
/// it safely in the memo table.
pub trait PhysicalPropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
fn derive_any(
&self,
Expand All @@ -41,6 +46,7 @@ pub trait PhysicalPropertyBuilderAny<T: NodeType>: 'static + Send + Sync {
fn property_name(&self) -> &'static str;
}

/// The trait for building physical properties for a plan node.
pub trait PhysicalPropertyBuilder<T: NodeType>: 'static + Send + Sync + Sized {
type Prop: PhysicalProperty + Clone + Sized;

Expand Down
11 changes: 2 additions & 9 deletions optd-datafusion-repr-adv-cost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,11 @@ impl CostModel<DfNodeType, NaiveMemo<DfNodeType>> for AdvancedCostModel {
node: &DfNodeType,
predicates: &[ArcDfPredNode],
children_stats: &[Option<&Statistics>],
children_costs: &[Cost],
context: Option<RelNodeContext>,
optimizer: Option<&CascadesOptimizer<DfNodeType>>,
) -> Cost {
self.base_model.compute_operation_cost(
node,
predicates,
children_stats,
children_costs,
context,
optimizer,
)
self.base_model
.compute_operation_cost(node, predicates, children_stats, context, optimizer)
}

fn derive_statistics(
Expand Down
11 changes: 2 additions & 9 deletions optd-datafusion-repr/src/cost/adaptive_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,15 @@ impl CostModel<DfNodeType, NaiveMemo<DfNodeType>> for AdaptiveCostModel {
node: &DfNodeType,
predicates: &[ArcDfPredNode],
children: &[Option<&Statistics>],
children_cost: &[Cost],
context: Option<RelNodeContext>,
optimizer: Option<&CascadesOptimizer<DfNodeType>>,
) -> Cost {
if let DfNodeType::PhysicalScan = node {
let row_cnt = self.get_row_cnt(&context);
return DfCostModel::cost(0.0, row_cnt);
}
self.base_model.compute_operation_cost(
node,
predicates,
children,
children_cost,
context,
optimizer,
)
self.base_model
.compute_operation_cost(node, predicates, children, context, optimizer)
}

fn derive_statistics(
Expand Down
1 change: 0 additions & 1 deletion optd-datafusion-repr/src/cost/base_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ impl CostModel<DfNodeType, NaiveMemo<DfNodeType>> for DfCostModel {
node: &DfNodeType,
predicates: &[ArcDfPredNode],
children: &[Option<&Statistics>],
_: &[Cost],
_context: Option<RelNodeContext>,
_optimizer: Option<&CascadesOptimizer<DfNodeType>>,
) -> Cost {
Expand Down
1 change: 0 additions & 1 deletion optd-datafusion-repr/src/testing/dummy_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl CostModel<DfNodeType, NaiveMemo<DfNodeType>> for DummyCostModel {
_: &DfNodeType,
_: &[ArcDfPredNode],
_: &[Option<&Statistics>],
_: &[Cost],
_: Option<RelNodeContext>,
_: Option<&CascadesOptimizer<DfNodeType>>,
) -> Cost {
Expand Down

0 comments on commit 91cd0a2

Please sign in to comment.