Skip to content

Commit a2547f2

Browse files
committed
add comments and minor refines
1 parent 625e129 commit a2547f2

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

optd-cost-model/src/common/properties/attr_ref.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
use std::collections::HashSet;
22

3-
use crate::utils::DisjointSets;
3+
use crate::{common::types::TableId, utils::DisjointSets};
44

5-
pub type BaseTableAttrRefs = Vec<AttrRef>;
5+
pub type AttrRefs = Vec<AttrRef>;
66

7+
/// [`BaseTableAttrRef`] represents a reference to an attribute in a base table,
8+
/// i.e. a table existing in the catalog.
79
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
810
pub struct BaseTableAttrRef {
9-
pub table: String,
10-
pub attr_idx: usize,
11+
pub table_id: TableId,
12+
pub attr_idx: u64,
1113
}
1214

15+
/// [`AttrRef`] represents a reference to an attribute in a query.
1316
#[derive(Clone, Debug)]
1417
pub enum AttrRef {
18+
/// Reference to a base table attribute.
1519
BaseTableAttrRef(BaseTableAttrRef),
16-
/// TODO: Better representation of derived attributes (e.g. t.v1 + t.v2).
20+
/// Reference to a derived attribute (e.g. t.v1 + t.v2).
21+
/// TODO: Better representation of derived attributes.
1722
Derived,
1823
}
1924

2025
impl AttrRef {
21-
pub fn base_table_attr_ref(table: String, attr_idx: usize) -> Self {
22-
AttrRef::BaseTableAttrRef(BaseTableAttrRef { table, attr_idx })
26+
pub fn base_table_attr_ref(table_id: TableId, attr_idx: u64) -> Self {
27+
AttrRef::BaseTableAttrRef(BaseTableAttrRef { table_id, attr_idx })
2328
}
2429
}
2530

@@ -29,6 +34,7 @@ impl From<BaseTableAttrRef> for AttrRef {
2934
}
3035
}
3136

37+
/// [`EqPredicate`] represents an equality predicate between two attributes.
3238
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
3339
pub struct EqPredicate {
3440
pub left: BaseTableAttrRef,
@@ -41,13 +47,12 @@ impl EqPredicate {
4147
}
4248
}
4349

44-
/// `SemanticCorrelation` represents the semantic correlation between attributes in a
50+
/// [`SemanticCorrelation`] represents the semantic correlation between attributes in a
4551
/// query. "Semantic" means that the attributes are correlated based on the
4652
/// semantics of the query, not the statistics.
4753
///
48-
/// `SemanticCorrelation` contains equal attributes denoted by disjoint sets of base
54+
/// [`SemanticCorrelation`] contains equal attributes denoted by disjoint sets of base
4955
/// table attributes, e.g. {{ t1.c1 = t2.c1 = t3.c1 }, { t1.c2 = t2.c2 }}.
50-
5156
#[derive(Clone, Debug, Default)]
5257
pub struct SemanticCorrelation {
5358
/// A disjoint set of base table attributes with equal values in the same row.
@@ -155,25 +160,23 @@ impl SemanticCorrelation {
155160
}
156161
}
157162

163+
/// [`GroupAttrRefs`] represents the attributes of a group in a query.
158164
#[derive(Clone, Debug)]
159165
pub struct GroupAttrRefs {
160-
attribute_refs: BaseTableAttrRefs,
166+
attribute_refs: AttrRefs,
161167
/// Correlation of the output attributes of the group.
162168
output_correlation: Option<SemanticCorrelation>,
163169
}
164170

165171
impl GroupAttrRefs {
166-
pub fn new(
167-
attribute_refs: BaseTableAttrRefs,
168-
output_correlation: Option<SemanticCorrelation>,
169-
) -> Self {
172+
pub fn new(attribute_refs: AttrRefs, output_correlation: Option<SemanticCorrelation>) -> Self {
170173
Self {
171174
attribute_refs,
172175
output_correlation,
173176
}
174177
}
175178

176-
pub fn base_table_attribute_refs(&self) -> &BaseTableAttrRefs {
179+
pub fn base_table_attribute_refs(&self) -> &AttrRefs {
177180
&self.attribute_refs
178181
}
179182

@@ -189,19 +192,19 @@ mod tests {
189192
#[test]
190193
fn test_eq_base_table_attribute_sets() {
191194
let attr1 = BaseTableAttrRef {
192-
table: "t1".to_string(),
195+
table_id: TableId(1),
193196
attr_idx: 1,
194197
};
195198
let attr2 = BaseTableAttrRef {
196-
table: "t2".to_string(),
199+
table_id: TableId(2),
197200
attr_idx: 2,
198201
};
199202
let attr3 = BaseTableAttrRef {
200-
table: "t3".to_string(),
203+
table_id: TableId(3),
201204
attr_idx: 3,
202205
};
203206
let attr4 = BaseTableAttrRef {
204-
table: "t4".to_string(),
207+
table_id: TableId(4),
205208
attr_idx: 4,
206209
};
207210
let pred1 = EqPredicate::new(attr1.clone(), attr2.clone());

optd-cost-model/src/common/properties/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
use super::Attribute;
66

7+
/// [`Schema`] represents the schema of a group in the memo. It contains a list of attributes.
78
#[derive(Clone, Debug, Serialize, Deserialize)]
89
pub struct Schema {
910
pub attributes: Vec<Attribute>,

optd-cost-model/src/memo_ext.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ use crate::common::{
33
types::GroupId,
44
};
55

6+
/// [`MemoExt`] is a trait that provides methods to access the schema, column reference, and attribute
7+
/// information of a group in the memo. The information are used by the cost model to compute the cost of
8+
/// an expression.
9+
///
10+
/// [`MemoExt`] should be implemented by the optimizer core to provide the necessary information to the cost
11+
/// model. All information required here is already present in the memo, so the optimizer core should be able
12+
/// to implement this trait without additional work.
613
pub trait MemoExt: Send + Sync + 'static {
14+
/// Get the schema of a group in the memo.
715
fn get_schema(&self, group_id: GroupId) -> Schema;
16+
/// Get the column reference of a group in the memo.
817
fn get_column_ref(&self, group_id: GroupId) -> GroupAttrRefs;
18+
/// Get the attribute information of a given attribute in a group in the memo.
919
fn get_attribute_info(&self, group_id: GroupId, attr_ref_idx: u64) -> Attribute;
1020
}

optd-cost-model/src/utils.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Copyright (c) 2023-2024 CMU Database Group
2-
//
3-
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file or at
4-
// https://opensource.org/licenses/MIT.
5-
61
//! optd's implementation of disjoint sets (union finds). It's send + sync + serializable.
72
83
use std::{collections::HashMap, hash::Hash};

0 commit comments

Comments
 (0)