Skip to content

Commit 8c4191f

Browse files
committed
rename AttrRefPred -> AttrIndexPred and revert back to initial design
1 parent 11a3a4e commit 8c4191f

File tree

14 files changed

+230
-230
lines changed

14 files changed

+230
-230
lines changed

optd-cost-model/src/common/nodes.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ impl std::fmt::Display for PhysicalNodeType {
5656
pub enum PredicateType {
5757
List,
5858
Constant(ConstantType),
59-
AttrRef,
60-
ExternAttributeRef,
61-
// TODO(lanlou): Id -> Id(IdType)
62-
Id,
59+
AttrIndex,
6360
UnOp(UnOpType),
6461
BinOp(BinOpType),
6562
LogOp(LogOpType),
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::common::{
2+
nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode},
3+
values::Value,
4+
};
5+
6+
/// [`AttributeIndexPred`] represents the position of an attribute in a schema or
7+
/// [`GroupAttrRefs`].
8+
///
9+
/// The `data` field holds the index of the attribute in the schema or [`GroupAttrRefs`].
10+
#[derive(Clone, Debug)]
11+
pub struct AttrIndexPred(pub ArcPredicateNode);
12+
13+
impl AttrIndexPred {
14+
pub fn new(attr_idx: u64) -> AttrIndexPred {
15+
AttrIndexPred(
16+
PredicateNode {
17+
typ: PredicateType::AttrIndex,
18+
children: vec![],
19+
data: Some(Value::UInt64(attr_idx)),
20+
}
21+
.into(),
22+
)
23+
}
24+
25+
/// Gets the attribute index.
26+
pub fn attr_index(&self) -> u64 {
27+
self.0.data.as_ref().unwrap().as_u64()
28+
}
29+
}
30+
31+
impl ReprPredicateNode for AttrIndexPred {
32+
fn into_pred_node(self) -> ArcPredicateNode {
33+
self.0
34+
}
35+
36+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
37+
if pred_node.typ != PredicateType::AttrIndex {
38+
return None;
39+
}
40+
Some(Self(pred_node))
41+
}
42+
}

optd-cost-model/src/common/predicates/attr_ref_pred.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

optd-cost-model/src/common/predicates/id_pred.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

optd-cost-model/src/common/predicates/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
pub mod attr_ref_pred;
1+
pub mod attr_index_pred;
22
pub mod bin_op_pred;
33
pub mod cast_pred;
44
pub mod constant_pred;
55
pub mod data_type_pred;
66
pub mod func_pred;
7-
pub mod id_pred;
87
pub mod in_list_pred;
98
pub mod like_pred;
109
pub mod list_pred;

optd-cost-model/src/cost/agg.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
common::{
33
nodes::{ArcPredicateNode, PredicateType, ReprPredicateNode},
4-
predicates::{attr_ref_pred::AttrRefPred, list_pred::ListPred},
4+
predicates::{attr_index_pred::AttrIndexPred, list_pred::ListPred},
55
types::TableId,
66
},
77
cost_model::CostModelImpl,
@@ -25,17 +25,18 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
2525

2626
for node in &group_by.0.children {
2727
match node.typ {
28-
PredicateType::AttrRef => {
28+
PredicateType::AttrIndex => {
2929
let attr_ref =
30-
AttrRefPred::from_pred_node(node.clone()).ok_or_else(|| {
30+
AttrIndexPred::from_pred_node(node.clone()).ok_or_else(|| {
3131
SemanticError::InvalidPredicate(
3232
"Expected AttributeRef predicate".to_string(),
3333
)
3434
})?;
35-
if attr_ref.is_derived() {
35+
let is_derived = todo!();
36+
if is_derived {
3637
row_cnt *= DEFAULT_NUM_DISTINCT;
3738
} else {
38-
let table_id = attr_ref.table_id();
39+
let table_id = todo!();
3940
let attr_idx = attr_ref.attr_index();
4041
// TODO: Only query ndistinct instead of all kinds of stats.
4142
let stats_option =
@@ -74,7 +75,7 @@ mod tests {
7475
values::Value,
7576
},
7677
cost_model::tests::{
77-
attr_ref, cnst, create_mock_cost_model, empty_list, empty_per_attr_stats, list,
78+
attr_index, cnst, create_mock_cost_model, empty_list, empty_per_attr_stats, list,
7879
TestPerAttributeStats,
7980
},
8081
stats::{utilities::simple_map::SimpleMap, MostCommonValues, DEFAULT_NUM_DISTINCT},
@@ -94,14 +95,14 @@ mod tests {
9495
);
9596

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

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

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

158159
// Group by two columns should return the product of the n-distinct of the columns.
159-
let group_bys = list(vec![
160-
attr_ref(table_id, attr1_base_idx),
161-
attr_ref(table_id, attr2_base_idx),
162-
]);
160+
let group_bys = list(vec![attr_index(attr1_base_idx), attr_index(attr2_base_idx)]); // TODO: Fix this
163161
assert_eq!(
164162
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),
165163
EstimatedStatistic((attr1_ndistinct * attr2_ndistinct) as f64)
@@ -168,9 +166,10 @@ mod tests {
168166
// Group by multiple columns should return the product of the n-distinct of the columns. If one of the columns
169167
// does not have stats, it should use the default value instead.
170168
let group_bys = list(vec![
171-
attr_ref(table_id, attr1_base_idx),
172-
attr_ref(table_id, attr2_base_idx),
173-
attr_ref(table_id, attr3_base_idx),
169+
// TODO: Fix this
170+
attr_index(attr1_base_idx),
171+
attr_index(attr2_base_idx),
172+
attr_index(attr3_base_idx),
174173
]);
175174
assert_eq!(
176175
cost_model.get_agg_row_cnt(group_bys).await.unwrap(),

optd-cost-model/src/cost/filter/comp_op.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
common::{
55
nodes::{ArcPredicateNode, PredicateType, ReprPredicateNode},
66
predicates::{
7-
attr_ref_pred::AttrRefPred, bin_op_pred::BinOpType, cast_pred::CastPred,
7+
attr_index_pred::AttrIndexPred, bin_op_pred::BinOpType, cast_pred::CastPred,
88
constant_pred::ConstantPred,
99
},
1010
values::Value,
@@ -41,7 +41,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
4141
.first()
4242
.expect("we just checked that attr_ref_exprs.len() == 1");
4343
let attr_ref_idx = attr_ref_expr.attr_index();
44-
let table_id = attr_ref_expr.table_id();
44+
let table_id = todo!();
4545

4646
// TODO: Consider attribute is a derived attribute
4747
if values.len() == 1 {
@@ -118,7 +118,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
118118
&self,
119119
left: ArcPredicateNode,
120120
right: ArcPredicateNode,
121-
) -> CostModelResult<(Vec<AttrRefPred>, Vec<Value>, Vec<ArcPredicateNode>, bool)> {
121+
) -> CostModelResult<(Vec<AttrIndexPred>, Vec<Value>, Vec<ArcPredicateNode>, bool)> {
122122
let mut attr_ref_exprs = vec![];
123123
let mut values = vec![];
124124
let mut non_attr_ref_exprs = vec![];
@@ -166,11 +166,11 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
166166
.into_pred_node();
167167
false
168168
}
169-
PredicateType::AttrRef => {
170-
let attr_ref_expr = AttrRefPred::from_pred_node(cast_expr_child)
169+
PredicateType::AttrIndex => {
170+
let attr_ref_expr = AttrIndexPred::from_pred_node(cast_expr_child)
171171
.expect("we already checked that the type is AttributeRef");
172172
let attr_ref_idx = attr_ref_expr.attr_index();
173-
let table_id = attr_ref_expr.table_id();
173+
let table_id = todo!();
174174
cast_node = attr_ref_expr.into_pred_node();
175175
// The "invert" cast is to invert the cast so that we're casting the
176176
// non_cast_node to the attribute's original type.
@@ -185,7 +185,7 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
185185
let invert_cast_data_type = &attribute_info.typ.into_data_type();
186186

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

220220
// Sort nodes into attr_ref_exprs, values, and non_attr_ref_exprs
221221
match uncasted_left.as_ref().typ {
222-
PredicateType::AttrRef => {
222+
PredicateType::AttrIndex => {
223223
is_left_attr_ref = true;
224224
attr_ref_exprs.push(
225-
AttrRefPred::from_pred_node(uncasted_left)
225+
AttrIndexPred::from_pred_node(uncasted_left)
226226
.expect("we already checked that the type is AttributeRef"),
227227
);
228228
}
@@ -240,9 +240,9 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
240240
}
241241
}
242242
match uncasted_right.as_ref().typ {
243-
PredicateType::AttrRef => {
243+
PredicateType::AttrIndex => {
244244
attr_ref_exprs.push(
245-
AttrRefPred::from_pred_node(uncasted_right)
245+
AttrIndexPred::from_pred_node(uncasted_right)
246246
.expect("we already checked that the type is AttributeRef"),
247247
);
248248
}

0 commit comments

Comments
 (0)