Skip to content

Commit cb2b63e

Browse files
committed
make id type concrete by moving out of trait CostModelStorageLayer
1 parent 19f34b4 commit cb2b63e

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

optd-persistent/src/cost_model/interface.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ use sea_orm_migration::prelude::*;
1111
use serde_json::json;
1212
use std::sync::Arc;
1313

14+
pub type GroupId = i32;
15+
pub type TableId = i32;
16+
pub type AttrId = i32;
17+
pub type ExprId = i32;
18+
pub type EpochId = i32;
19+
pub type StatId = i32;
20+
1421
/// TODO: documentation
1522
pub enum CatalogSource {
1623
Iceberg(),
@@ -79,63 +86,51 @@ pub struct Cost {
7986
/// TODO: documentation
8087
#[trait_variant::make(Send)]
8188
pub trait CostModelStorageLayer {
82-
type GroupId;
83-
type TableId;
84-
type AttrId;
85-
type ExprId;
86-
type EpochId;
87-
type StatId;
88-
89-
// TODO: Change EpochId to event::Model::epoch_id
90-
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<Self::EpochId>;
89+
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<EpochId>;
9190

92-
async fn update_stats_from_catalog(&self, c: CatalogSource) -> StorageResult<Self::EpochId>;
91+
async fn update_stats_from_catalog(&self, c: CatalogSource) -> StorageResult<EpochId>;
9392

9493
async fn update_stats(
9594
&self,
9695
stat: Stat,
9796
epoch_option: EpochOption,
98-
) -> StorageResult<Option<Self::EpochId>>;
97+
) -> StorageResult<Option<EpochId>>;
9998

100-
async fn store_cost(
101-
&self,
102-
expr_id: Self::ExprId,
103-
cost: Cost,
104-
epoch_id: Self::EpochId,
105-
) -> StorageResult<()>;
99+
async fn store_cost(&self, expr_id: ExprId, cost: Cost, epoch_id: EpochId)
100+
-> StorageResult<()>;
106101

107102
async fn store_expr_stats_mappings(
108103
&self,
109-
expr_id: Self::ExprId,
110-
stat_ids: Vec<Self::StatId>,
104+
expr_id: ExprId,
105+
stat_ids: Vec<StatId>,
111106
) -> StorageResult<()>;
112107

113108
/// Get the statistics for a given table.
114109
///
115110
/// If `epoch_id` is None, it will return the latest statistics.
116111
async fn get_stats_for_table(
117112
&self,
118-
table_id: Self::TableId,
113+
table_id: TableId,
119114
// TODO: Add enum for stat_type
120115
stat_type: i32,
121-
epoch_id: Option<Self::EpochId>,
116+
epoch_id: Option<EpochId>,
122117
) -> StorageResult<Option<Json>>;
123118

124119
/// Get the (joint) statistics for one or more attributes.
125120
///
126121
/// If `epoch_id` is None, it will return the latest statistics.
127122
async fn get_stats_for_attr(
128123
&self,
129-
attr_ids: Vec<Self::AttrId>,
124+
attr_ids: Vec<AttrId>,
130125
stat_type: i32,
131-
epoch_id: Option<Self::EpochId>,
126+
epoch_id: Option<EpochId>,
132127
) -> StorageResult<Option<Json>>;
133128

134129
async fn get_cost_analysis(
135130
&self,
136-
expr_id: Self::ExprId,
137-
epoch_id: Self::EpochId,
131+
expr_id: ExprId,
132+
epoch_id: EpochId,
138133
) -> StorageResult<Option<Cost>>;
139134

140-
async fn get_cost(&self, expr_id: Self::ExprId) -> StorageResult<Option<Cost>>;
135+
async fn get_cost(&self, expr_id: ExprId) -> StorageResult<Option<Cost>>;
141136
}

optd-persistent/src/cost_model/orm.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ use sea_orm::{
1313
use serde_json::json;
1414

1515
use super::catalog::mock_catalog::{self, MockCatalog};
16-
use super::interface::{CatalogSource, EpochOption, Stat};
16+
use super::interface::{AttrId, CatalogSource, EpochId, EpochOption, ExprId, Stat, StatId};
1717

1818
impl BackendManager {
19-
fn get_description_from_attr_ids(
20-
&self,
21-
attr_ids: Vec<<BackendManager as CostModelStorageLayer>::AttrId>,
22-
) -> String {
19+
fn get_description_from_attr_ids(&self, attr_ids: Vec<AttrId>) -> String {
2320
let mut attr_ids = attr_ids;
2421
attr_ids.sort();
2522
attr_ids
@@ -31,15 +28,8 @@ impl BackendManager {
3128
}
3229

3330
impl CostModelStorageLayer for BackendManager {
34-
type GroupId = i32;
35-
type TableId = i32;
36-
type AttrId = i32;
37-
type ExprId = i32;
38-
type EpochId = i32;
39-
type StatId = i32;
40-
4131
/// TODO: documentation
42-
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<Self::EpochId> {
32+
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<EpochId> {
4333
let new_event = event::ActiveModel {
4434
source_variant: sea_orm::ActiveValue::Set(source),
4535
timestamp: sea_orm::ActiveValue::Set(Utc::now()),
@@ -51,7 +41,7 @@ impl CostModelStorageLayer for BackendManager {
5141
}
5242

5343
/// TODO: documentation
54-
async fn update_stats_from_catalog(&self, c: CatalogSource) -> StorageResult<Self::EpochId> {
44+
async fn update_stats_from_catalog(&self, c: CatalogSource) -> StorageResult<EpochId> {
5545
let transaction = self.db.begin().await?;
5646
let source = match c {
5747
CatalogSource::Mock => "Mock",
@@ -208,7 +198,7 @@ impl CostModelStorageLayer for BackendManager {
208198
&self,
209199
stat: Stat,
210200
epoch_option: EpochOption,
211-
) -> StorageResult<Option<Self::EpochId>> {
201+
) -> StorageResult<Option<EpochId>> {
212202
let transaction = self.db.begin().await?;
213203
// 0. Check if the stat already exists. If exists, get stat_id, else insert into statistic table.
214204
let stat_id = match stat.table_id {
@@ -353,8 +343,8 @@ impl CostModelStorageLayer for BackendManager {
353343
/// TODO: documentation
354344
async fn store_expr_stats_mappings(
355345
&self,
356-
expr_id: Self::ExprId,
357-
stat_ids: Vec<Self::StatId>,
346+
expr_id: ExprId,
347+
stat_ids: Vec<StatId>,
358348
) -> StorageResult<()> {
359349
let to_insert_mappings = stat_ids
360350
.iter()
@@ -376,7 +366,7 @@ impl CostModelStorageLayer for BackendManager {
376366
&self,
377367
table_id: i32,
378368
stat_type: i32,
379-
epoch_id: Option<Self::EpochId>,
369+
epoch_id: Option<EpochId>,
380370
) -> StorageResult<Option<Json>> {
381371
match epoch_id {
382372
Some(epoch_id) => Ok(VersionedStatistic::find()
@@ -402,9 +392,9 @@ impl CostModelStorageLayer for BackendManager {
402392
/// TODO: documentation
403393
async fn get_stats_for_attr(
404394
&self,
405-
mut attr_ids: Vec<Self::AttrId>,
395+
mut attr_ids: Vec<AttrId>,
406396
stat_type: i32,
407-
epoch_id: Option<Self::EpochId>,
397+
epoch_id: Option<EpochId>,
408398
) -> StorageResult<Option<Json>> {
409399
let attr_num = attr_ids.len() as i32;
410400
// The description is to concat `attr_ids` using commas
@@ -440,8 +430,8 @@ impl CostModelStorageLayer for BackendManager {
440430
/// TODO: documentation
441431
async fn get_cost_analysis(
442432
&self,
443-
expr_id: Self::ExprId,
444-
epoch_id: Self::EpochId,
433+
expr_id: ExprId,
434+
epoch_id: EpochId,
445435
) -> StorageResult<Option<Cost>> {
446436
let cost = PlanCost::find()
447437
.filter(plan_cost::Column::PhysicalExpressionId.eq(expr_id))
@@ -457,7 +447,7 @@ impl CostModelStorageLayer for BackendManager {
457447
}))
458448
}
459449

460-
async fn get_cost(&self, expr_id: Self::ExprId) -> StorageResult<Option<Cost>> {
450+
async fn get_cost(&self, expr_id: ExprId) -> StorageResult<Option<Cost>> {
461451
let cost = PlanCost::find()
462452
.filter(plan_cost::Column::PhysicalExpressionId.eq(expr_id))
463453
.order_by_desc(plan_cost::Column::EpochId)
@@ -475,9 +465,9 @@ impl CostModelStorageLayer for BackendManager {
475465
/// TODO: documentation
476466
async fn store_cost(
477467
&self,
478-
physical_expression_id: Self::ExprId,
468+
physical_expression_id: ExprId,
479469
cost: Cost,
480-
epoch_id: Self::EpochId,
470+
epoch_id: EpochId,
481471
) -> StorageResult<()> {
482472
let expr_exists = PhysicalExpression::find_by_id(physical_expression_id)
483473
.one(&self.db)

0 commit comments

Comments
 (0)