Skip to content

Commit

Permalink
rename AttrRefPred -> AttrIndexPred and revert back to initial design
Browse files Browse the repository at this point in the history
  • Loading branch information
xx01cyx committed Nov 18, 2024
1 parent 11a3a4e commit 8c4191f
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 230 deletions.
5 changes: 1 addition & 4 deletions optd-cost-model/src/common/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ impl std::fmt::Display for PhysicalNodeType {
pub enum PredicateType {
List,
Constant(ConstantType),
AttrRef,
ExternAttributeRef,
// TODO(lanlou): Id -> Id(IdType)
Id,
AttrIndex,
UnOp(UnOpType),
BinOp(BinOpType),
LogOp(LogOpType),
Expand Down
42 changes: 42 additions & 0 deletions optd-cost-model/src/common/predicates/attr_index_pred.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::common::{
nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode},
values::Value,
};

/// [`AttributeIndexPred`] represents the position of an attribute in a schema or
/// [`GroupAttrRefs`].
///
/// The `data` field holds the index of the attribute in the schema or [`GroupAttrRefs`].
#[derive(Clone, Debug)]
pub struct AttrIndexPred(pub ArcPredicateNode);

impl AttrIndexPred {
pub fn new(attr_idx: u64) -> AttrIndexPred {
AttrIndexPred(
PredicateNode {
typ: PredicateType::AttrIndex,
children: vec![],
data: Some(Value::UInt64(attr_idx)),
}
.into(),
)
}

/// Gets the attribute index.
pub fn attr_index(&self) -> u64 {
self.0.data.as_ref().unwrap().as_u64()
}
}

impl ReprPredicateNode for AttrIndexPred {
fn into_pred_node(self) -> ArcPredicateNode {
self.0
}

fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
if pred_node.typ != PredicateType::AttrIndex {
return None;
}
Some(Self(pred_node))
}
}
74 changes: 0 additions & 74 deletions optd-cost-model/src/common/predicates/attr_ref_pred.rs

This file was deleted.

43 changes: 0 additions & 43 deletions optd-cost-model/src/common/predicates/id_pred.rs

This file was deleted.

3 changes: 1 addition & 2 deletions optd-cost-model/src/common/predicates/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pub mod attr_ref_pred;
pub mod attr_index_pred;
pub mod bin_op_pred;
pub mod cast_pred;
pub mod constant_pred;
pub mod data_type_pred;
pub mod func_pred;
pub mod id_pred;
pub mod in_list_pred;
pub mod like_pred;
pub mod list_pred;
Expand Down
31 changes: 15 additions & 16 deletions optd-cost-model/src/cost/agg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::{
nodes::{ArcPredicateNode, PredicateType, ReprPredicateNode},
predicates::{attr_ref_pred::AttrRefPred, list_pred::ListPred},
predicates::{attr_index_pred::AttrIndexPred, list_pred::ListPred},
types::TableId,
},
cost_model::CostModelImpl,
Expand All @@ -25,17 +25,18 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {

for node in &group_by.0.children {
match node.typ {
PredicateType::AttrRef => {
PredicateType::AttrIndex => {
let attr_ref =
AttrRefPred::from_pred_node(node.clone()).ok_or_else(|| {
AttrIndexPred::from_pred_node(node.clone()).ok_or_else(|| {
SemanticError::InvalidPredicate(
"Expected AttributeRef predicate".to_string(),
)
})?;
if attr_ref.is_derived() {
let is_derived = todo!();
if is_derived {
row_cnt *= DEFAULT_NUM_DISTINCT;
} else {
let table_id = attr_ref.table_id();
let table_id = todo!();
let attr_idx = attr_ref.attr_index();
// TODO: Only query ndistinct instead of all kinds of stats.
let stats_option =
Expand Down Expand Up @@ -74,7 +75,7 @@ mod tests {
values::Value,
},
cost_model::tests::{
attr_ref, cnst, create_mock_cost_model, empty_list, empty_per_attr_stats, list,
attr_index, cnst, create_mock_cost_model, empty_list, empty_per_attr_stats, list,
TestPerAttributeStats,
},
stats::{utilities::simple_map::SimpleMap, MostCommonValues, DEFAULT_NUM_DISTINCT},
Expand All @@ -94,14 +95,14 @@ mod tests {
);

// Group by single column should return the default value since there are no stats.
let group_bys = list(vec![attr_ref(table_id, 0)]);
let group_bys = list(vec![attr_index(0)]);
assert_eq!(
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
EstimatedStatistic(DEFAULT_NUM_DISTINCT as f64)
);

// Group by two columns should return the default value squared since there are no stats.
let group_bys = list(vec![attr_ref(table_id, 0), attr_ref(table_id, 1)]);
let group_bys = list(vec![attr_index(0), attr_index(1)]);
assert_eq!(
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
EstimatedStatistic((DEFAULT_NUM_DISTINCT * DEFAULT_NUM_DISTINCT) as f64)
Expand Down Expand Up @@ -149,17 +150,14 @@ mod tests {
);

// Group by single column should return the n-distinct of the column.
let group_bys = list(vec![attr_ref(table_id, attr1_base_idx)]);
let group_bys = list(vec![attr_index(attr1_base_idx)]); // TODO: Fix this
assert_eq!(
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
EstimatedStatistic(attr1_ndistinct as f64)
);

// Group by two columns should return the product of the n-distinct of the columns.
let group_bys = list(vec![
attr_ref(table_id, attr1_base_idx),
attr_ref(table_id, attr2_base_idx),
]);
let group_bys = list(vec![attr_index(attr1_base_idx), attr_index(attr2_base_idx)]); // TODO: Fix this
assert_eq!(
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
EstimatedStatistic((attr1_ndistinct * attr2_ndistinct) as f64)
Expand All @@ -168,9 +166,10 @@ mod tests {
// Group by multiple columns should return the product of the n-distinct of the columns. If one of the columns
// does not have stats, it should use the default value instead.
let group_bys = list(vec![
attr_ref(table_id, attr1_base_idx),
attr_ref(table_id, attr2_base_idx),
attr_ref(table_id, attr3_base_idx),
// TODO: Fix this
attr_index(attr1_base_idx),
attr_index(attr2_base_idx),
attr_index(attr3_base_idx),
]);
assert_eq!(
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
Expand Down
22 changes: 11 additions & 11 deletions optd-cost-model/src/cost/filter/comp_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
common::{
nodes::{ArcPredicateNode, PredicateType, ReprPredicateNode},
predicates::{
attr_ref_pred::AttrRefPred, bin_op_pred::BinOpType, cast_pred::CastPred,
attr_index_pred::AttrIndexPred, bin_op_pred::BinOpType, cast_pred::CastPred,
constant_pred::ConstantPred,
},
values::Value,
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
.first()
.expect("we just checked that attr_ref_exprs.len() == 1");
let attr_ref_idx = attr_ref_expr.attr_index();
let table_id = attr_ref_expr.table_id();
let table_id = todo!();

// TODO: Consider attribute is a derived attribute
if values.len() == 1 {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
&self,
left: ArcPredicateNode,
right: ArcPredicateNode,
) -> CostModelResult<(Vec<AttrRefPred>, Vec<Value>, Vec<ArcPredicateNode>, bool)> {
) -> CostModelResult<(Vec<AttrIndexPred>, Vec<Value>, Vec<ArcPredicateNode>, bool)> {
let mut attr_ref_exprs = vec![];
let mut values = vec![];
let mut non_attr_ref_exprs = vec![];
Expand Down Expand Up @@ -166,11 +166,11 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
.into_pred_node();
false
}
PredicateType::AttrRef => {
let attr_ref_expr = AttrRefPred::from_pred_node(cast_expr_child)
PredicateType::AttrIndex => {
let attr_ref_expr = AttrIndexPred::from_pred_node(cast_expr_child)
.expect("we already checked that the type is AttributeRef");
let attr_ref_idx = attr_ref_expr.attr_index();
let table_id = attr_ref_expr.table_id();
let table_id = todo!();
cast_node = attr_ref_expr.into_pred_node();
// The "invert" cast is to invert the cast so that we're casting the
// non_cast_node to the attribute's original type.
Expand All @@ -185,7 +185,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
let invert_cast_data_type = &attribute_info.typ.into_data_type();

match non_cast_node.typ {
PredicateType::AttrRef => {
PredicateType::AttrIndex => {
// In general, there's no way to remove the Cast here. We can't move
// the Cast to the other AttributeRef
// because that would lead to an infinite loop. Thus, we just leave
Expand Down Expand Up @@ -219,10 +219,10 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {

// Sort nodes into attr_ref_exprs, values, and non_attr_ref_exprs
match uncasted_left.as_ref().typ {
PredicateType::AttrRef => {
PredicateType::AttrIndex => {
is_left_attr_ref = true;
attr_ref_exprs.push(
AttrRefPred::from_pred_node(uncasted_left)
AttrIndexPred::from_pred_node(uncasted_left)
.expect("we already checked that the type is AttributeRef"),
);
}
Expand All @@ -240,9 +240,9 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
}
}
match uncasted_right.as_ref().typ {
PredicateType::AttrRef => {
PredicateType::AttrIndex => {
attr_ref_exprs.push(
AttrRefPred::from_pred_node(uncasted_right)
AttrIndexPred::from_pred_node(uncasted_right)
.expect("we already checked that the type is AttributeRef"),
);
}
Expand Down
Loading

0 comments on commit 8c4191f

Please sign in to comment.