-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cost-model): implement compute_operation_cost
#44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
ea20998
904f275
23c444d
8fb462c
4a73c33
8fec3c2
4a6bd01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ use crate::{ | |
| }, | ||
| memo_ext::MemoExt, | ||
| stats::AttributeCombValueStats, | ||
| storage::CostModelStorageManager, | ||
| storage::{self, CostModelStorageManager}, | ||
| ComputeCostContext, Cost, CostModel, CostModelResult, EstimatedStatistic, StatValue, | ||
| }; | ||
|
|
||
|
|
@@ -43,24 +43,111 @@ impl<S: CostModelStorageManager> CostModelImpl<S> { | |
|
|
||
| #[async_trait::async_trait] | ||
| impl<S: CostModelStorageManager + Send + Sync + 'static> CostModel for CostModelImpl<S> { | ||
| /// TODO: should we add epoch_id? | ||
| async fn compute_operation_cost( | ||
| &self, | ||
| node: &PhysicalNodeType, | ||
| node: PhysicalNodeType, | ||
| predicates: &[ArcPredicateNode], | ||
| children_costs: &[Cost], | ||
| children_stats: &[EstimatedStatistic], | ||
| context: ComputeCostContext, | ||
| ) -> CostModelResult<Cost> { | ||
| todo!() | ||
| let res = self.storage_manager.get_cost(context.expr_id).await; | ||
| if let Ok((Some(cost), _)) = res { | ||
| return Ok(cost); | ||
| }; | ||
| let mut output_statistic = None; | ||
| if let Ok((_, Some(statistic))) = res { | ||
| output_statistic = Some(statistic); | ||
| }; | ||
| let output_cost = match node { | ||
| PhysicalNodeType::PhysicalScan => { | ||
| let output_statistic_data = output_statistic.unwrap_or( | ||
| self.derive_statistics( | ||
| node, | ||
| predicates, | ||
| children_stats, | ||
| context.clone(), | ||
| false, | ||
| ) | ||
| .await?, | ||
| ); | ||
| output_statistic = Some(output_statistic_data.clone()); | ||
| Cost { | ||
| compute_cost: 0.0, | ||
| io_cost: output_statistic_data.0, | ||
| } | ||
| } | ||
| PhysicalNodeType::PhysicalEmptyRelation => Cost { | ||
| compute_cost: 0.1, | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalLimit => Cost { | ||
| compute_cost: children_costs[0].compute_cost, | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalFilter => Cost { | ||
| // TODO: now this equation is specific to optd, and try to make this equation more general | ||
| compute_cost: children_costs[1].compute_cost * children_stats[0].0, | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalNestedLoopJoin(join_typ) => { | ||
| let child_compute_cost = children_costs[2].compute_cost; | ||
| Cost { | ||
| compute_cost: children_stats[0].0 * children_stats[1].0 * child_compute_cost | ||
| + children_stats[0].0, | ||
| io_cost: 0.0, | ||
| } | ||
| } | ||
| // TODO: we should document that the first child is the left table, which is used to build | ||
| // the hash table. | ||
| PhysicalNodeType::PhysicalHashJoin(join_typ) => Cost { | ||
| compute_cost: children_stats[0].0 * 2.0 + children_stats[1].0, | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalAgg => Cost { | ||
| compute_cost: children_stats[0].0 | ||
| * (children_costs[1].compute_cost + children_costs[2].compute_cost), | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalProjection => Cost { | ||
| compute_cost: children_stats[0].0 * children_costs[1].compute_cost, | ||
| io_cost: 0.0, | ||
| }, | ||
| PhysicalNodeType::PhysicalSort => Cost { | ||
| compute_cost: children_stats[0].0 * children_stats[0].0.ln_1p().max(1.0), | ||
| io_cost: 0.0, | ||
| }, | ||
| }; | ||
| let res = self | ||
| .storage_manager | ||
| .store_cost( | ||
| context.expr_id, | ||
| Some(output_cost.clone()), | ||
| output_statistic, | ||
| None, | ||
| ) | ||
| .await; | ||
| if res.is_err() { | ||
| eprintln!("Failed to store output cost"); | ||
| } | ||
| Ok(output_cost) | ||
| } | ||
|
|
||
| /// TODO: should we add epoch_id? | ||
| async fn derive_statistics( | ||
| &self, | ||
| node: PhysicalNodeType, | ||
| predicates: &[ArcPredicateNode], | ||
| children_statistics: &[EstimatedStatistic], | ||
| context: ComputeCostContext, | ||
| store_output_statistic: bool, | ||
| ) -> CostModelResult<EstimatedStatistic> { | ||
| match node { | ||
| let res = self.storage_manager.get_cost(context.expr_id).await; | ||
| if let Ok((_, Some(statistic))) = res { | ||
| return Ok(statistic); | ||
| } | ||
| let output_statistic = match node { | ||
| PhysicalNodeType::PhysicalScan => { | ||
| let table_id = TableId(predicates[0].data.as_ref().unwrap().as_u64()); | ||
| let row_cnt = self | ||
|
|
@@ -114,7 +201,17 @@ impl<S: CostModelStorageManager + Send + Sync + 'static> CostModel for CostModel | |
| PhysicalNodeType::PhysicalSort | PhysicalNodeType::PhysicalProjection => { | ||
| Ok(children_statistics[0].clone()) | ||
| } | ||
| } | ||
| }?; | ||
| if store_output_statistic { | ||
| let res = self | ||
| .storage_manager | ||
| .store_cost(context.expr_id, None, Some(output_statistic.clone()), None) | ||
| .await; | ||
| if res.is_err() { | ||
| eprintln!("Failed to store output statistic"); | ||
| } | ||
| }; | ||
| Ok(output_statistic) | ||
|
Comment on lines
+205
to
+214
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might cause problem if some stats are missed from the storage. We should in the future consider having sth like a retry queue to store failed database requests. But I'm good with this for now. Maybe we can add a comment here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I think the retry should be handled by the storage manager, and I will add a comment there. I don't think it is a big deal, since maybe we can recalculate it again? |
||
| } | ||
|
|
||
| async fn update_statistics( | ||
|
|
@@ -167,3 +264,5 @@ impl<S: CostModelStorageManager> CostModelImpl<S> { | |
| .await | ||
| } | ||
| } | ||
|
|
||
| // TODO: Add tests for `derive_statistic`` and `compute_operation_cost`. | ||
Uh oh!
There was an error while loading. Please reload this page.