Skip to content

Commit 66cd4f4

Browse files
committed
Add cost model trait
1 parent 174171a commit 66cd4f4

File tree

7 files changed

+260
-2
lines changed

7 files changed

+260
-2
lines changed

optd-persistent/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ sea-orm = { version = "1.1", features = [
1414
] }
1515
sea-orm-migration = "1.1"
1616
serde_json = "1.0"
17-
1817
tokio = { version = "1.41", features = ["full"] }
18+
trait-variant = "0.1"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use optd_persistent::{migrate, TEST_DATABASE_URL};
2+
use sea_orm::*;
3+
use sea_orm_migration::prelude::*;
4+
5+
#[tokio::main]
6+
async fn main() {
7+
let _ = std::fs::remove_file(TEST_DATABASE_URL);
8+
9+
let db = Database::connect(TEST_DATABASE_URL)
10+
.await
11+
.expect("Unable to connect to the database");
12+
13+
migrate(&db)
14+
.await
15+
.expect("Something went wrong during migration");
16+
17+
db.execute(sea_orm::Statement::from_string(
18+
sea_orm::DatabaseBackend::Sqlite,
19+
"PRAGMA foreign_keys = ON;".to_owned(),
20+
))
21+
.await
22+
.expect("Unable to enable foreign keys");
23+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#![allow(dead_code, unused_imports)]
2+
3+
use crate::entities::cascades_group;
4+
use crate::entities::event::Model as event_model;
5+
use crate::entities::logical_expression;
6+
use crate::entities::physical_expression;
7+
use crate::CostModelStorageResult;
8+
use sea_orm::*;
9+
use sea_orm_migration::prelude::*;
10+
use serde_json::json;
11+
use std::sync::Arc;
12+
13+
pub enum CatalogSource {
14+
Iceberg(),
15+
}
16+
17+
#[trait_variant::make(Send)]
18+
pub trait CostModelStorageLayer {
19+
type GroupId;
20+
type TableId;
21+
type AttrId;
22+
type ExprId;
23+
type EpochId;
24+
type StatId;
25+
26+
// TODO: Change EpochId to event::Model::epoch_id
27+
async fn create_new_epoch(
28+
&mut self,
29+
source: String,
30+
data: String,
31+
) -> CostModelStorageResult<Self::EpochId>;
32+
33+
async fn update_stats_from_catalog(
34+
&self,
35+
c: CatalogSource,
36+
epoch_id: Self::EpochId,
37+
) -> CostModelStorageResult<()>;
38+
39+
// i32 in `stats:i32` is a placeholder for the stats type
40+
async fn update_stats(&self, stats: i32, epoch_id: Self::EpochId)
41+
-> CostModelStorageResult<()>;
42+
43+
async fn store_cost(
44+
&self,
45+
expr_id: Self::ExprId,
46+
cost: i32,
47+
epoch_id: Self::EpochId,
48+
) -> CostModelStorageResult<()>;
49+
50+
async fn store_expr_stats_mappings(
51+
&self,
52+
expr_id: Self::ExprId,
53+
stat_ids: Vec<Self::StatId>,
54+
) -> CostModelStorageResult<()>;
55+
56+
/// Get the statistics for a given table.
57+
///
58+
/// If `epoch_id` is None, it will return the latest statistics.
59+
async fn get_stats_for_table(
60+
&self,
61+
table_id: Self::TableId,
62+
// TODO: Add enum for stat_type
63+
stat_type: i32,
64+
epoch_id: Option<Self::EpochId>,
65+
) -> CostModelStorageResult<Option<f32>>;
66+
67+
/// Get the statistics for a given attribute.
68+
///
69+
/// If `epoch_id` is None, it will return the latest statistics.
70+
async fn get_stats_for_attr(
71+
&self,
72+
attr_id: Self::AttrId,
73+
stat_type: i32,
74+
epoch_id: Option<Self::EpochId>,
75+
) -> CostModelStorageResult<Option<f32>>;
76+
77+
/// Get the joint statistics for a list of attributes.
78+
///
79+
/// If `epoch_id` is None, it will return the latest statistics.
80+
async fn get_stats_for_attrs(
81+
&self,
82+
attr_ids: Vec<Self::AttrId>,
83+
stat_type: i32,
84+
epoch_id: Option<Self::EpochId>,
85+
) -> CostModelStorageResult<Option<f32>>;
86+
87+
async fn get_cost_analysis(
88+
&self,
89+
expr_id: Self::ExprId,
90+
epoch_id: Self::EpochId,
91+
) -> CostModelStorageResult<Option<i32>>;
92+
93+
async fn get_cost(&self, expr_id: Self::ExprId) -> CostModelStorageResult<Option<i32>>;
94+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod interface;
2+
pub mod orm;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#![allow(dead_code, unused_imports, unused_variables)]
2+
3+
use crate::{BackendManager, CostModelStorageLayer, CostModelStorageResult};
4+
5+
use super::interface::CatalogSource;
6+
7+
impl CostModelStorageLayer for BackendManager {
8+
type GroupId = i32;
9+
type TableId = i32;
10+
type AttrId = i32;
11+
type ExprId = i32;
12+
type EpochId = i32;
13+
type StatId = i32;
14+
15+
async fn create_new_epoch(
16+
&mut self,
17+
source: String,
18+
data: String,
19+
) -> CostModelStorageResult<Self::EpochId> {
20+
todo!()
21+
}
22+
23+
async fn update_stats_from_catalog(
24+
&self,
25+
c: CatalogSource,
26+
epoch_id: Self::EpochId,
27+
) -> CostModelStorageResult<()> {
28+
todo!()
29+
}
30+
31+
async fn update_stats(
32+
&self,
33+
stats: i32,
34+
epoch_id: Self::EpochId,
35+
) -> CostModelStorageResult<()> {
36+
todo!()
37+
}
38+
39+
async fn store_cost(
40+
&self,
41+
expr_id: Self::ExprId,
42+
cost: i32,
43+
epoch_id: Self::EpochId,
44+
) -> CostModelStorageResult<()> {
45+
todo!()
46+
}
47+
48+
async fn store_expr_stats_mappings(
49+
&self,
50+
expr_id: Self::ExprId,
51+
stat_ids: Vec<Self::StatId>,
52+
) -> CostModelStorageResult<()> {
53+
todo!()
54+
}
55+
56+
#[doc = " Get the statistics for a given table."]
57+
#[doc = " If `epoch_id` is None, it will return the latest statistics."]
58+
async fn get_stats_for_table(
59+
&self,
60+
table_id: i32,
61+
stat_type: i32,
62+
epoch_id: Option<Self::EpochId>,
63+
) -> CostModelStorageResult<Option<f32>> {
64+
todo!()
65+
}
66+
67+
#[doc = " Get the statistics for a given attribute."]
68+
#[doc = " If `epoch_id` is None, it will return the latest statistics."]
69+
async fn get_stats_for_attr(
70+
&self,
71+
attr_id: i32,
72+
stat_type: i32,
73+
epoch_id: Option<Self::EpochId>,
74+
) -> CostModelStorageResult<Option<f32>> {
75+
todo!()
76+
}
77+
78+
#[doc = " Get the joint statistics for a list of attributes."]
79+
#[doc = " If `epoch_id` is None, it will return the latest statistics."]
80+
async fn get_stats_for_attrs(
81+
&self,
82+
attr_ids: Vec<i32>,
83+
stat_type: i32,
84+
epoch_id: Option<Self::EpochId>,
85+
) -> CostModelStorageResult<Option<f32>> {
86+
todo!()
87+
}
88+
89+
async fn get_cost_analysis(
90+
&self,
91+
expr_id: Self::ExprId,
92+
epoch_id: Self::EpochId,
93+
) -> CostModelStorageResult<Option<i32>> {
94+
todo!()
95+
}
96+
97+
async fn get_cost(&self, expr_id: Self::ExprId) -> CostModelStorageResult<Option<i32>> {
98+
todo!()
99+
}
100+
}

optd-persistent/src/entities/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
2+
#![allow(dead_code, unused_imports, unused_variables)]
23

34
pub use super::attribute::Entity as Attribute;
45
pub use super::attribute_constraint_junction::Entity as AttributeConstraintJunction;

optd-persistent/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1+
#![allow(dead_code)]
2+
3+
use std::sync::atomic::AtomicUsize;
4+
15
use sea_orm::*;
26
use sea_orm_migration::prelude::*;
37

4-
mod migrator;
58
use migrator::Migrator;
69

10+
mod entities;
11+
mod migrator;
12+
13+
mod cost_model;
14+
pub use cost_model::interface::CostModelStorageLayer;
15+
16+
pub type CostModelStorageResult<T> = Result<T, CostModelError>;
17+
18+
pub enum CostModelError {
19+
// TODO: Add more error types
20+
Database(DbErr),
21+
}
22+
23+
impl From<DbErr> for CostModelError {
24+
fn from(value: DbErr) -> Self {
25+
CostModelError::Database(value)
26+
}
27+
}
28+
29+
pub struct BackendManager {
30+
db: DatabaseConnection,
31+
latest_epoch_id: AtomicUsize,
32+
}
33+
34+
impl BackendManager {
35+
/// Creates a new `BackendManager`.
36+
pub async fn new() -> CostModelStorageResult<Self> {
37+
Ok(Self {
38+
db: Database::connect(DATABASE_URL).await?,
39+
latest_epoch_id: AtomicUsize::new(0),
40+
})
41+
}
42+
}
43+
744
pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc";
845
pub const DATABASE_FILE: &str = "./sqlite.db";
46+
pub const TEST_DATABASE_URL: &str = "sqlite:./test.db?mode=rwc";
947

1048
pub async fn migrate(db: &DatabaseConnection) -> Result<(), DbErr> {
1149
Migrator::refresh(db).await

0 commit comments

Comments
 (0)