Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 80a5027

Browse files
committed
docs: cost model and rule engine
Signed-off-by: Alex Chi <[email protected]>
1 parent 6fd6f38 commit 80a5027

10 files changed

+1140
-13
lines changed

docs/src/cost_model.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
# Cost Model
2+
3+
Developers can plug their own cost models into optd. The cost must be represented as a vector of `f64`s, where the first element in the vector is the weighted cost. The optimizer will use weighted cost internally for cost comparison and select the winner for a group.
4+
5+
The cost model interface can be found in `optd-core/src/cost.rs`, and the core of the cost model is the cost computation process implemented in `CostModel::compute_cost`.
6+
7+
```rust
8+
pub trait CostModel<T: RelNodeTyp>: 'static + Send + Sync {
9+
fn compute_cost(
10+
&self,
11+
node: &T,
12+
data: &Option<Value>,
13+
children: &[Cost],
14+
context: Option<RelNodeContext>,
15+
) -> Cost;
16+
}
17+
```
18+
19+
`compute_cost` takes the cost of the children, the current plan node information, and some contexts of the current node. The context will be useful for adaptive optimization, and it contains the group ID and the expression ID of the current plan node, so that the adaptive cost model can use runtime information from the last run to compute the cost.
20+
21+
The optd Datafusion cost model stores 4 elements in the cost vector: weighted cost, row count, compute cost and I/O cost. The cost of the plan nodes and the SQL expressions can all be computed solely based on these information.
22+
23+
Contrary to other optimizer frameworks like Calcite, optd does not choose to implement the cost model as part of the plan node member functions. In optd, developers write all cost computation things in one file, so that testing and debugging the cost model all happens in one file (or in one `impl`).

docs/src/optd-cascades/optd-cascades-1.svg

Lines changed: 2 additions & 2 deletions
Loading

docs/src/optd-cascades/optd-cascades-2.svg

Lines changed: 2 additions & 2 deletions
Loading

docs/src/optd-cascades/optd-cascades-3.svg

Lines changed: 2 additions & 2 deletions
Loading

docs/src/optd-cascades/optd-cascades-4.svg

Lines changed: 2 additions & 2 deletions
Loading

docs/src/optd-cascades/optd-plan-repr-1.svg

Lines changed: 2 additions & 2 deletions
Loading

docs/src/optd-cascades/optd-plan-repr-2.svg

Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 179 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)