|
| 1 | +use optd_persistent::CostModelStorageLayer; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + common::{ |
| 5 | + nodes::{ArcPredicateNode, PredicateType, ReprPredicateNode}, |
| 6 | + predicates::{attr_ref_pred::AttributeRefPred, list_pred::ListPred}, |
| 7 | + types::TableId, |
| 8 | + }, |
| 9 | + cost_model::CostModelImpl, |
| 10 | + stats::DEFAULT_NUM_DISTINCT, |
| 11 | + CostModelError, CostModelResult, EstimatedStatistic, |
| 12 | +}; |
| 13 | + |
| 14 | +impl<S: CostModelStorageLayer> CostModelImpl<S> { |
| 15 | + pub fn get_agg_row_cnt( |
| 16 | + &self, |
| 17 | + group_by: ArcPredicateNode, |
| 18 | + ) -> CostModelResult<EstimatedStatistic> { |
| 19 | + let group_by = ListPred::from_pred_node(group_by).unwrap(); |
| 20 | + if group_by.is_empty() { |
| 21 | + Ok(EstimatedStatistic(1)) |
| 22 | + } else { |
| 23 | + // Multiply the n-distinct of all the group by columns. |
| 24 | + // TODO: improve with multi-dimensional n-distinct |
| 25 | + let row_cnt = group_by.0.children.iter().try_fold(1, |acc, node| { |
| 26 | + match node.typ { |
| 27 | + PredicateType::AttributeRef => { |
| 28 | + let attr_ref = |
| 29 | + AttributeRefPred::from_pred_node(node.clone()).ok_or_else(|| { |
| 30 | + CostModelError::InvalidPredicate( |
| 31 | + "Expected AttributeRef predicate".to_string(), |
| 32 | + ) |
| 33 | + })?; |
| 34 | + if attr_ref.is_derived() { |
| 35 | + Ok(acc * DEFAULT_NUM_DISTINCT) |
| 36 | + } else { |
| 37 | + let table_id = attr_ref.table_id(); |
| 38 | + let attr_idx = attr_ref.attr_index(); |
| 39 | + let stats_option = |
| 40 | + self.get_attribute_comb_stats(TableId(table_id), &vec![attr_idx])?; |
| 41 | + |
| 42 | + let ndistinct = match stats_option { |
| 43 | + Some(stats) => stats.ndistinct, |
| 44 | + None => { |
| 45 | + // The column type is not supported or stats are missing. |
| 46 | + DEFAULT_NUM_DISTINCT |
| 47 | + } |
| 48 | + }; |
| 49 | + Ok(acc * ndistinct) |
| 50 | + } |
| 51 | + } |
| 52 | + _ => { |
| 53 | + // TODO: Consider the case where `GROUP BY 1`. |
| 54 | + panic!("GROUP BY must have attribute ref predicate") |
| 55 | + } |
| 56 | + } |
| 57 | + })?; |
| 58 | + Ok(EstimatedStatistic(row_cnt)) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments