Skip to content

Commit a2ad0ea

Browse files
authored
Merge pull request #23 from cmu-db/mock-catalog
Add simple mock catalog for cost model
2 parents 1f6bc10 + 3296939 commit a2ad0ea

File tree

6 files changed

+171
-3
lines changed

6 files changed

+171
-3
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use crate::cost_model::interface::StatisticType;
2+
3+
use super::IndexType;
4+
5+
pub struct MockDatabaseMetadata {
6+
pub id: i32,
7+
pub name: String,
8+
}
9+
10+
pub struct MockNamespaceMetadata {
11+
pub id: i32,
12+
pub name: String,
13+
pub database_id: i32,
14+
}
15+
16+
pub struct MockTableMetadata {
17+
pub id: i32,
18+
pub name: String,
19+
pub namespace_id: i32,
20+
}
21+
22+
pub struct MockAttribute {
23+
pub id: i32,
24+
pub name: String,
25+
pub attr_index: i32,
26+
pub table_id: i32,
27+
}
28+
29+
pub struct MockStatistic {
30+
pub id: i32,
31+
pub stat_type: i32,
32+
// TODO(lanlou): what should I use for the value type?
33+
pub stat_value: String,
34+
pub attr_ids: Vec<i32>,
35+
pub table_id: Option<i32>,
36+
pub name: String,
37+
}
38+
39+
pub struct MockIndex {
40+
pub id: i32,
41+
pub name: String,
42+
pub table_id: i32,
43+
pub number_of_attributes: i32,
44+
pub index_type: i32,
45+
pub is_unique: bool,
46+
pub nulls_not_distinct: bool,
47+
pub is_primary: bool,
48+
pub is_clustered: bool,
49+
pub is_exclusion: bool,
50+
pub attr_ids: Vec<i32>,
51+
}
52+
53+
pub struct MockTrigger {
54+
pub id: i32,
55+
pub name: String,
56+
pub table_id: i32,
57+
pub parent_trigger_id: i32,
58+
pub function: String,
59+
}
60+
61+
pub struct MockCatalog {
62+
pub databases: Vec<MockDatabaseMetadata>,
63+
pub namespaces: Vec<MockNamespaceMetadata>,
64+
pub tables: Vec<MockTableMetadata>,
65+
pub attributes: Vec<MockAttribute>,
66+
pub statistics: Vec<MockStatistic>,
67+
pub indexes: Vec<MockIndex>,
68+
pub triggers: Vec<MockTrigger>,
69+
// TODO: constraints
70+
}
71+
impl MockCatalog {
72+
pub fn new() -> Self {
73+
let databases: Vec<MockDatabaseMetadata> = vec![MockDatabaseMetadata {
74+
id: 1,
75+
name: "db1".to_string(),
76+
}];
77+
let namespaces: Vec<MockNamespaceMetadata> = vec![MockNamespaceMetadata {
78+
id: 1,
79+
name: "ns1".to_string(),
80+
database_id: 1,
81+
}];
82+
let tables: Vec<MockTableMetadata> = vec![MockTableMetadata {
83+
id: 1,
84+
name: "table1".to_string(),
85+
namespace_id: 1,
86+
}];
87+
let attributes: Vec<MockAttribute> = vec![
88+
MockAttribute {
89+
id: 1,
90+
name: "attr1".to_string(),
91+
attr_index: 1,
92+
table_id: 1,
93+
},
94+
MockAttribute {
95+
id: 2,
96+
name: "attr2".to_string(),
97+
attr_index: 2,
98+
table_id: 1,
99+
},
100+
];
101+
let statistics: Vec<MockStatistic> = vec![
102+
MockStatistic {
103+
id: 1,
104+
stat_type: StatisticType::Count as i32,
105+
stat_value: "100".to_string(),
106+
attr_ids: vec![1],
107+
table_id: None,
108+
name: "CountAttr1".to_string(),
109+
},
110+
MockStatistic {
111+
id: 2,
112+
stat_type: StatisticType::Count as i32,
113+
stat_value: "200".to_string(),
114+
attr_ids: vec![2],
115+
table_id: None,
116+
name: "CountAttr2".to_string(),
117+
},
118+
MockStatistic {
119+
id: 3,
120+
stat_type: StatisticType::Count as i32,
121+
stat_value: "300".to_string(),
122+
attr_ids: vec![],
123+
table_id: Some(1),
124+
name: "Table1Count".to_string(),
125+
},
126+
];
127+
let indexes: Vec<MockIndex> = vec![MockIndex {
128+
id: 1,
129+
name: "index1".to_string(),
130+
table_id: 1,
131+
number_of_attributes: 1,
132+
index_type: IndexType::Hash as i32,
133+
is_unique: false,
134+
nulls_not_distinct: false,
135+
is_primary: true,
136+
is_clustered: false,
137+
is_exclusion: false,
138+
attr_ids: vec![1],
139+
}];
140+
141+
MockCatalog {
142+
databases,
143+
namespaces,
144+
tables,
145+
attributes,
146+
statistics,
147+
indexes,
148+
triggers: vec![],
149+
}
150+
}
151+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod mock_catalog;
2+
3+
pub enum IndexType {
4+
BTree,
5+
Hash,
6+
}

optd-persistent/src/cost_model/interface.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ use std::sync::Arc;
1212

1313
pub enum CatalogSource {
1414
Iceberg(),
15+
Mock,
16+
}
17+
18+
pub enum StatisticType {
19+
Count,
20+
Min,
21+
Max,
1522
}
1623

1724
#[trait_variant::make(Send)]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod catalog;
12
pub mod interface;
23
pub mod orm;

optd-persistent/src/entities/index.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ pub struct Model {
1010
pub table_id: i32,
1111
pub name: String,
1212
pub number_of_attributes: i32,
13+
pub variant_tag: i32,
1314
pub is_unique: bool,
1415
pub nulls_not_distinct: bool,
1516
pub is_primary: bool,
1617
pub is_clustered: bool,
1718
pub is_exclusion: bool,
18-
pub data: Json,
19+
pub description: Json,
1920
}
2021

2122
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

optd-persistent/src/migrator/catalog/m20241029_000001_index.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ pub enum Index {
88
TableId,
99
Name,
1010
NumberOfAttributes,
11+
VariantTag,
1112
IsUnique,
1213
NullsNotDistinct,
1314
IsPrimary,
1415
IsClustered,
1516
IsExclusion,
16-
Data,
17+
Description,
1718
}
1819

1920
#[derive(DeriveMigrationName)]
@@ -38,12 +39,13 @@ impl MigrationTrait for Migration {
3839
)
3940
.col(string(Index::Name))
4041
.col(integer(Index::NumberOfAttributes))
42+
.col(integer(Index::VariantTag))
4143
.col(boolean(Index::IsUnique))
4244
.col(boolean(Index::NullsNotDistinct))
4345
.col(boolean(Index::IsPrimary))
4446
.col(boolean(Index::IsClustered))
4547
.col(boolean(Index::IsExclusion))
46-
.col(json(Index::Data))
48+
.col(json(Index::Description))
4749
.to_owned(),
4850
)
4951
.await

0 commit comments

Comments
 (0)