Skip to content

Commit 38d5598

Browse files
committed
add memo trait
This commit adds a `Memo` trait and a first draft of an implementation of the `Memo` trait via the backed ORM-mapped database. pushing entities
1 parent 16a7a96 commit 38d5598

22 files changed

+992
-31
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

optd-persistent/src/cost_model/orm.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,13 @@ impl CostModelStorageLayer for BackendManager {
238238
match res {
239239
Ok(insert_res) => insert_res.last_insert_id,
240240
Err(_) => {
241-
return Err(BackendError::BackendError(format!(
242-
"failed to insert statistic {:?} into statistic table",
243-
stat
244-
)))
241+
return Err(BackendError::CostModel(
242+
format!(
243+
"failed to insert statistic {:?} into statistic table",
244+
stat
245+
)
246+
.into(),
247+
))
245248
}
246249
}
247250
}
@@ -450,10 +453,13 @@ impl CostModelStorageLayer for BackendManager {
450453
.collect::<Vec<_>>();
451454

452455
if attr_ids.len() != attr_base_indices.len() {
453-
return Err(BackendError::BackendError(format!(
454-
"Not all attributes found for table_id {} and base indices {:?}",
455-
table_id, attr_base_indices
456-
)));
456+
return Err(BackendError::CostModel(
457+
format!(
458+
"Not all attributes found for table_id {} and base indices {:?}",
459+
table_id, attr_base_indices
460+
)
461+
.into(),
462+
));
457463
}
458464

459465
self.get_stats_for_attr(attr_ids, stat_type, epoch_id).await
@@ -505,10 +511,13 @@ impl CostModelStorageLayer for BackendManager {
505511
.one(&self.db)
506512
.await?;
507513
if expr_exists.is_none() {
508-
return Err(BackendError::BackendError(format!(
509-
"physical expression id {} not found when storing cost",
510-
physical_expression_id
511-
)));
514+
return Err(BackendError::CostModel(
515+
format!(
516+
"physical expression id {} not found when storing cost",
517+
physical_expression_id
518+
)
519+
.into(),
520+
));
512521
}
513522

514523
// Check if epoch_id exists in Event table
@@ -518,10 +527,9 @@ impl CostModelStorageLayer for BackendManager {
518527
.await
519528
.unwrap();
520529
if epoch_exists.is_none() {
521-
return Err(BackendError::BackendError(format!(
522-
"epoch id {} not found when storing cost",
523-
epoch_id
524-
)));
530+
return Err(BackendError::CostModel(
531+
format!("epoch id {} not found when storing cost", epoch_id).into(),
532+
));
525533
}
526534

527535
let new_cost = plan_cost::ActiveModel {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1
2+
3+
use sea_orm::entity::prelude::*;
4+
5+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6+
#[sea_orm(table_name = "predicate")]
7+
pub struct Model {
8+
#[sea_orm(primary_key)]
9+
pub id: i32,
10+
pub data: Json,
11+
pub variant: i32,
12+
}
13+
14+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15+
pub enum Relation {
16+
#[sea_orm(has_many = "super::predicate_logical_expression_junction::Entity")]
17+
PredicateLogicalExpressionJunction,
18+
#[sea_orm(has_many = "super::predicate_physical_expression_junction::Entity")]
19+
PredicatePhysicalExpressionJunction,
20+
}
21+
22+
impl Related<super::predicate_logical_expression_junction::Entity> for Entity {
23+
fn to() -> RelationDef {
24+
Relation::PredicateLogicalExpressionJunction.def()
25+
}
26+
}
27+
28+
impl Related<super::predicate_physical_expression_junction::Entity> for Entity {
29+
fn to() -> RelationDef {
30+
Relation::PredicatePhysicalExpressionJunction.def()
31+
}
32+
}
33+
34+
impl Related<super::logical_expression::Entity> for Entity {
35+
fn to() -> RelationDef {
36+
super::predicate_logical_expression_junction::Relation::LogicalExpression.def()
37+
}
38+
fn via() -> Option<RelationDef> {
39+
Some(
40+
super::predicate_logical_expression_junction::Relation::Predicate
41+
.def()
42+
.rev(),
43+
)
44+
}
45+
}
46+
47+
impl Related<super::physical_expression::Entity> for Entity {
48+
fn to() -> RelationDef {
49+
super::predicate_physical_expression_junction::Relation::PhysicalExpression.def()
50+
}
51+
fn via() -> Option<RelationDef> {
52+
Some(
53+
super::predicate_physical_expression_junction::Relation::Predicate
54+
.def()
55+
.rev(),
56+
)
57+
}
58+
}
59+
60+
impl ActiveModelBehavior for ActiveModel {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1
2+
3+
use sea_orm::entity::prelude::*;
4+
5+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6+
#[sea_orm(table_name = "predicate_children")]
7+
pub struct Model {
8+
#[sea_orm(primary_key, auto_increment = false)]
9+
pub parent_id: i32,
10+
#[sea_orm(primary_key, auto_increment = false)]
11+
pub child_id: i32,
12+
}
13+
14+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15+
pub enum Relation {
16+
#[sea_orm(
17+
belongs_to = "super::predicate::Entity",
18+
from = "Column::ChildId",
19+
to = "super::predicate::Column::Id",
20+
on_update = "Cascade",
21+
on_delete = "Cascade"
22+
)]
23+
Predicate2,
24+
#[sea_orm(
25+
belongs_to = "super::predicate::Entity",
26+
from = "Column::ParentId",
27+
to = "super::predicate::Column::Id",
28+
on_update = "Cascade",
29+
on_delete = "Cascade"
30+
)]
31+
Predicate1,
32+
}
33+
34+
impl ActiveModelBehavior for ActiveModel {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1
2+
3+
use sea_orm::entity::prelude::*;
4+
5+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6+
#[sea_orm(table_name = "predicate_logical_expression_junction")]
7+
pub struct Model {
8+
#[sea_orm(primary_key, auto_increment = false)]
9+
pub logical_expr_id: i32,
10+
#[sea_orm(primary_key, auto_increment = false)]
11+
pub predicate_id: i32,
12+
}
13+
14+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15+
pub enum Relation {
16+
#[sea_orm(
17+
belongs_to = "super::logical_expression::Entity",
18+
from = "Column::LogicalExprId",
19+
to = "super::logical_expression::Column::Id",
20+
on_update = "Cascade",
21+
on_delete = "Cascade"
22+
)]
23+
LogicalExpression,
24+
#[sea_orm(
25+
belongs_to = "super::predicate::Entity",
26+
from = "Column::PredicateId",
27+
to = "super::predicate::Column::Id",
28+
on_update = "Cascade",
29+
on_delete = "Cascade"
30+
)]
31+
Predicate,
32+
}
33+
34+
impl Related<super::logical_expression::Entity> for Entity {
35+
fn to() -> RelationDef {
36+
Relation::LogicalExpression.def()
37+
}
38+
}
39+
40+
impl Related<super::predicate::Entity> for Entity {
41+
fn to() -> RelationDef {
42+
Relation::Predicate.def()
43+
}
44+
}
45+
46+
impl ActiveModelBehavior for ActiveModel {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1
2+
3+
use sea_orm::entity::prelude::*;
4+
5+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6+
#[sea_orm(table_name = "predicate_physical_expression_junction")]
7+
pub struct Model {
8+
#[sea_orm(primary_key, auto_increment = false)]
9+
pub physical_expr_id: i32,
10+
#[sea_orm(primary_key, auto_increment = false)]
11+
pub predicate_id: i32,
12+
}
13+
14+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15+
pub enum Relation {
16+
#[sea_orm(
17+
belongs_to = "super::physical_expression::Entity",
18+
from = "Column::PhysicalExprId",
19+
to = "super::physical_expression::Column::Id",
20+
on_update = "Cascade",
21+
on_delete = "Cascade"
22+
)]
23+
PhysicalExpression,
24+
#[sea_orm(
25+
belongs_to = "super::predicate::Entity",
26+
from = "Column::PredicateId",
27+
to = "super::predicate::Column::Id",
28+
on_update = "Cascade",
29+
on_delete = "Cascade"
30+
)]
31+
Predicate,
32+
}
33+
34+
impl Related<super::physical_expression::Entity> for Entity {
35+
fn to() -> RelationDef {
36+
Relation::PhysicalExpression.def()
37+
}
38+
}
39+
40+
impl Related<super::predicate::Entity> for Entity {
41+
fn to() -> RelationDef {
42+
Relation::Predicate.def()
43+
}
44+
}
45+
46+
impl ActiveModelBehavior for ActiveModel {}

optd-persistent/src/lib.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ mod migrator;
1313
pub mod cost_model;
1414
pub use cost_model::interface::CostModelStorageLayer;
1515

16+
mod memo;
17+
pub use memo::interface::Memo;
18+
1619
/// The filename of the SQLite database for migration.
1720
pub const DATABASE_FILENAME: &str = "sqlite.db";
1821
/// The URL of the SQLite database for migration.
@@ -39,17 +42,48 @@ fn get_sqlite_url(file: &str) -> String {
3942
format!("sqlite:{}?mode=rwc", file)
4043
}
4144

42-
pub type StorageResult<T> = Result<T, BackendError>;
45+
#[derive(Debug)]
46+
pub enum CostModelError {
47+
// TODO: Add more error types
48+
UnknownStatisticType,
49+
VersionedStatisticNotFound,
50+
CustomError(String),
51+
}
4352

53+
/// TODO convert this to `thiserror`
54+
#[derive(Debug)]
55+
/// The different kinds of errors that might occur while running operations on a memo table.
56+
pub enum MemoError {
57+
UnknownGroup,
58+
UnknownLogicalExpression,
59+
UnknownPhysicalExpression,
60+
InvalidExpression,
61+
}
62+
63+
/// TODO convert this to `thiserror`
4464
#[derive(Debug)]
4565
pub enum BackendError {
66+
Memo(MemoError),
4667
DatabaseError(DbErr),
68+
CostModel(CostModelError),
4769
BackendError(String),
4870
}
4971

50-
impl From<String> for BackendError {
72+
impl From<String> for CostModelError {
5173
fn from(value: String) -> Self {
52-
BackendError::BackendError(value)
74+
CostModelError::CustomError(value)
75+
}
76+
}
77+
78+
impl From<CostModelError> for BackendError {
79+
fn from(value: CostModelError) -> Self {
80+
BackendError::CostModel(value)
81+
}
82+
}
83+
84+
impl From<MemoError> for BackendError {
85+
fn from(value: MemoError) -> Self {
86+
BackendError::Memo(value)
5387
}
5488
}
5589

@@ -59,6 +93,9 @@ impl From<DbErr> for BackendError {
5993
}
6094
}
6195

96+
/// A type alias for a result with [`BackendError`] as the error type.
97+
pub type StorageResult<T> = Result<T, BackendError>;
98+
6299
pub struct BackendManager {
63100
db: DatabaseConnection,
64101
}

optd-persistent/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ use optd_persistent::DATABASE_URL;
1717

1818
#[tokio::main]
1919
async fn main() {
20+
basic_demo().await;
21+
memo_demo().await;
22+
}
23+
24+
async fn memo_demo() {
25+
let _db = Database::connect(DATABASE_URL).await.unwrap();
26+
27+
todo!()
28+
}
29+
30+
async fn basic_demo() {
2031
let db = Database::connect(DATABASE_URL).await.unwrap();
2132

2233
// Create a new `CascadesGroup`.

0 commit comments

Comments
 (0)