Skip to content

Commit 5bedaae

Browse files
committed
use f64 for cost calculation
1 parent bb40546 commit 5bedaae

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

datafusion/optimizer/src/reorder_join/cost.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use datafusion_common::{plan_datafusion_err, plan_err, stats::Precision, Result}
22
use datafusion_expr::{Join, JoinType, LogicalPlan};
33

44
pub trait JoinCostEstimator: std::fmt::Debug {
5-
fn cardinality(&self, plan: &LogicalPlan) -> Option<usize> {
5+
fn cardinality(&self, plan: &LogicalPlan) -> Option<f64> {
66
estimate_cardinality(plan).ok()
77
}
88

@@ -13,8 +13,8 @@ pub trait JoinCostEstimator: std::fmt::Debug {
1313
}
1414
}
1515

16-
fn cost(&self, selectivity: f64, cardinality: usize) -> f64 {
17-
selectivity * cardinality as f64
16+
fn cost(&self, selectivity: f64, cardinality: f64) -> f64 {
17+
selectivity * cardinality
1818
}
1919
}
2020

@@ -24,15 +24,15 @@ pub struct DefaultCostEstimator;
2424

2525
impl JoinCostEstimator for DefaultCostEstimator {}
2626

27-
fn estimate_cardinality(plan: &LogicalPlan) -> Result<usize> {
27+
fn estimate_cardinality(plan: &LogicalPlan) -> Result<f64> {
2828
match plan {
2929
LogicalPlan::Filter(filter) => {
3030
let input_cardinality = estimate_cardinality(&filter.input)?;
31-
Ok((0.1 * input_cardinality as f64) as usize)
31+
Ok(0.1 * input_cardinality)
3232
}
3333
LogicalPlan::Aggregate(agg) => {
3434
let input_cardinality = estimate_cardinality(&agg.input)?;
35-
Ok((0.1 * input_cardinality as f64) as usize)
35+
Ok(0.1 * input_cardinality)
3636
}
3737
LogicalPlan::TableScan(scan) => {
3838
let statistics = scan
@@ -42,7 +42,7 @@ fn estimate_cardinality(plan: &LogicalPlan) -> Result<usize> {
4242
if let Precision::Exact(num_rows) | Precision::Inexact(num_rows) =
4343
statistics.num_rows
4444
{
45-
Ok(num_rows)
45+
Ok(num_rows as f64)
4646
} else {
4747
plan_err!("Number of rows not available")
4848
}

datafusion/optimizer/src/reorder_join/left_deep_join_plan.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ pub fn query_graph_to_optimal_left_deep_join_plan(
167167
struct QueryNode {
168168
node_id: NodeId,
169169
// T in [IbarakiKameda84]
170-
selectivity: usize,
170+
selectivity: f64,
171171
// C in [IbarakiKameda84]
172172
cost: f64,
173173
}
174174

175175
impl QueryNode {
176176
fn rank(&self) -> f64 {
177-
(self.selectivity - 1) as f64 / self.cost
177+
(self.selectivity - 1.0) / self.cost
178178
}
179179
}
180180

@@ -303,7 +303,7 @@ impl<'graph> PrecedenceTreeNode<'graph> {
303303
let node = query_graph
304304
.get_node(node_id)
305305
.ok_or_else(|| plan_datafusion_err!("Root node not found"))?;
306-
let input_cardinality = cost_estimator.cardinality(&node.plan).unwrap_or(1);
306+
let input_cardinality = cost_estimator.cardinality(&node.plan).unwrap_or(1.0);
307307

308308
let children = node
309309
.connections()
@@ -331,7 +331,7 @@ impl<'graph> PrecedenceTreeNode<'graph> {
331331
Ok(PrecedenceTreeNode {
332332
query_nodes: vec![QueryNode {
333333
node_id,
334-
selectivity: (selectivity * input_cardinality as f64) as usize,
334+
selectivity: (selectivity * input_cardinality),
335335
cost: if is_root {
336336
0.0
337337
} else {
@@ -348,15 +348,15 @@ impl<'graph> PrecedenceTreeNode<'graph> {
348348
let (cardinality, cost) =
349349
self.query_nodes
350350
.iter()
351-
.fold((1, 0.0), |(cardinality, cost), node| {
352-
let cost = cost + cardinality as f64 * node.cost;
351+
.fold((1.0, 0.0), |(cardinality, cost), node| {
352+
let cost = cost + cardinality * node.cost;
353353
let cardinality = cardinality * node.selectivity;
354354
(cardinality, cost)
355355
});
356356
if cost == 0.0 {
357357
0.0
358358
} else {
359-
(cardinality.saturating_sub(1)) as f64 / cost
359+
(cardinality - 1.0) as f64 / cost
360360
}
361361
}
362362

@@ -642,12 +642,12 @@ impl<'graph> PrecedenceTreeNode<'graph> {
642642
self.cost_recursive(self.query_nodes[0].selectivity, 0.0)
643643
}
644644

645-
fn cost_recursive(&self, cardinality: usize, cost: f64) -> Result<f64> {
645+
fn cost_recursive(&self, cardinality: f64, cost: f64) -> Result<f64> {
646646
let cost = match self.children.len() {
647-
0 => cost + cardinality as f64 * self.query_nodes[0].cost,
647+
0 => cost + cardinality * self.query_nodes[0].cost,
648648
1 => self.children[0].cost_recursive(
649649
cardinality * self.query_nodes[0].selectivity,
650-
cost + cardinality as f64 * self.query_nodes[0].cost,
650+
cost + cardinality * self.query_nodes[0].cost,
651651
)?,
652652
_ => {
653653
return plan_err!(

0 commit comments

Comments
 (0)