Skip to content

Commit 639bcd6

Browse files
committed
Add comment for cost model ORM
1 parent b7dbac4 commit 639bcd6

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

optd-persistent/src/entities/statistic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Model {
1212
pub created_time: DateTimeUtc,
1313
pub number_of_attributes: i32,
1414
pub statistic_type: i32,
15-
pub data: String,
15+
pub description: String,
1616
}
1717

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

optd-persistent/src/migrator/cost_model/m20241029_000001_event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Every time we insert/update statistics, we need to insert a new
2+
//! row into this table to record the event.
3+
14
use sea_orm_migration::{prelude::*, schema::*};
25

36
#[derive(Iden)]

optd-persistent/src/migrator/cost_model/m20241029_000001_physical_expression_to_statistic_junction.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//! This table stores for a physical expression, which statistics are used, so we
2+
//! don't need to compute it again. It is especially useful for update_stats, where
3+
//! we need to invalidate all the costs based on the physical_expression_id, so we
4+
//! need to use this table to get the physical_expression_id via statistic_id.
5+
//!
6+
//! **NOTE:** When we compute the cost for a physical expression, we should also
7+
//! insert related mappings into this table.
8+
19
use crate::migrator::cost_model::statistic::Statistic;
210
use crate::migrator::memo::physical_expression::PhysicalExpression;
311

optd-persistent/src/migrator/cost_model/m20241029_000001_plan_cost.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
/*
2-
Table plan_cost {
3-
id integer PK
4-
physical_expression_id integer [ref: > physical_expression.id]
5-
epoch_id integer [ref: > event.epoch_id]
6-
cost integer
7-
// Whether the cost is valid or not. If the latest cost for an expr is invalid, then we need to recompute the cost.
8-
// We need to invalidate the cost when the related stats are updated.
9-
is_valid boolean
10-
}
11-
*/
1+
//! When a statistic is updated, then all the related costs should be invalidated. (IsValid is set to false)
2+
//! This design (using IsValid flag) is based on the assumption that update_stats will not be called very frequently.
3+
//! It favors the compute_cost performance over the update_stats performance.
124
135
use crate::migrator::cost_model::event::Event;
146
use crate::migrator::memo::physical_expression::PhysicalExpression;
@@ -21,6 +13,8 @@ pub enum PlanCost {
2113
PhysicalExpressionId,
2214
EpochId,
2315
Cost,
16+
// Whether the cost is valid or not. If the latest cost for an expr is invalid, then we need to recompute the cost.
17+
// We need to invalidate the cost when the related stats are updated.
2418
IsValid,
2519
}
2620

optd-persistent/src/migrator/cost_model/m20241029_000001_statistic.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! This table stores the statistic infos. One sole statistic only has one row in this table.
2+
//!
3+
//! If we want to insert a new statistic, we should first insert one row into this table, then add a new
4+
//! event, and finally insert the statistic value into the versioned_statistic table.
5+
//! If we want to update a statistic, we should first find the real statistic id from this table, then
6+
//! add a new event, and finally insert the statistic value into the versioned_statistic table.
7+
18
use crate::migrator::catalog::m20241029_000001_table_metadata::TableMetadata;
29
use sea_orm_migration::{prelude::*, schema::*};
310

@@ -6,11 +13,18 @@ pub enum Statistic {
613
Table,
714
Id,
815
Name,
16+
// null if not a table statistic.
917
TableId,
1018
CreatedTime,
19+
// 0 if a table statistic.
1120
NumberOfAttributes,
21+
// TODO(lanlou): Should we make another table to explain the type mapping?
1222
StatisticType,
13-
Data,
23+
// Store the sorted attribute ids of this statistic, to support quick lookup (OR we can use junction table to look up)
24+
// For example, if we want to store the statistic of attributes [1, 2, 3], we can store it as "1,2,3".
25+
// During lookup, we should first sort the attribute ids, and then look up.
26+
// OR we can use statistic_to_attribute_junction table to look up.
27+
Description,
1428
}
1529

1630
#[derive(DeriveMigrationName)]
@@ -37,7 +51,7 @@ impl MigrationTrait for Migration {
3751
.col(timestamp(Statistic::CreatedTime))
3852
.col(integer(Statistic::NumberOfAttributes))
3953
.col(integer(Statistic::StatisticType))
40-
.col(string(Statistic::Data))
54+
.col(string(Statistic::Description))
4155
.to_owned(),
4256
)
4357
.await

optd-persistent/src/migrator/cost_model/m20241029_000001_versioned_statistic.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! This table stores the versioned statistics. It includes all the histories of the statistics.
2+
//!
3+
//! If a statistic is updated/inserted, please insert one new row into this table.
4+
15
use crate::migrator::cost_model::{event::Event, statistic::Statistic};
26
use sea_orm_migration::{prelude::*, schema::*};
37

@@ -6,6 +10,7 @@ pub enum VersionedStatistic {
610
Table,
711
Id,
812
EpochId,
13+
// Real statistic id.
914
StatisticId,
1015
StatisticValue,
1116
}

schema/all_tables.dbml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Table statistic {
3737
created_time timestamp
3838
number_of_attributes integer // 0 if a table statistic
3939
statistic_type integer // Should we make another table to explain the type mapping?
40-
data varchar // Store the sorted attribute ids of this statistic, to support quick lookup (OR we can use junction table to look up)
40+
description varchar // Store the sorted attribute ids of this statistic, to support quick lookup (OR we can use junction table to look up)
4141
}
4242

4343
Table versioned_statistic {

0 commit comments

Comments
 (0)