Skip to content

Commit 6ab314f

Browse files
authored
Merge pull request #18 from cmu-db/cost-model-ORM-trait
Add cost model trait
2 parents 174171a + 8a51cdd commit 6ab314f

File tree

7 files changed

+255
-2
lines changed

7 files changed

+255
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use optd_persistent::{migrate, TEST_DATABASE_FILE, 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_FILE);
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+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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::StorageResult;
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+
) -> StorageResult<Self::EpochId>;
32+
33+
async fn update_stats_from_catalog(
34+
&self,
35+
c: CatalogSource,
36+
epoch_id: Self::EpochId,
37+
) -> StorageResult<()>;
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) -> StorageResult<()>;
41+
42+
async fn store_cost(
43+
&self,
44+
expr_id: Self::ExprId,
45+
cost: i32,
46+
epoch_id: Self::EpochId,
47+
) -> StorageResult<()>;
48+
49+
async fn store_expr_stats_mappings(
50+
&self,
51+
expr_id: Self::ExprId,
52+
stat_ids: Vec<Self::StatId>,
53+
) -> StorageResult<()>;
54+
55+
/// Get the statistics for a given table.
56+
///
57+
/// If `epoch_id` is None, it will return the latest statistics.
58+
async fn get_stats_for_table(
59+
&self,
60+
table_id: Self::TableId,
61+
// TODO: Add enum for stat_type
62+
stat_type: i32,
63+
epoch_id: Option<Self::EpochId>,
64+
) -> StorageResult<Option<f32>>;
65+
66+
/// Get the statistics for a given attribute.
67+
///
68+
/// If `epoch_id` is None, it will return the latest statistics.
69+
async fn get_stats_for_attr(
70+
&self,
71+
attr_id: Self::AttrId,
72+
stat_type: i32,
73+
epoch_id: Option<Self::EpochId>,
74+
) -> StorageResult<Option<f32>>;
75+
76+
/// Get the joint statistics for a list of attributes.
77+
///
78+
/// If `epoch_id` is None, it will return the latest statistics.
79+
async fn get_stats_for_attrs(
80+
&self,
81+
attr_ids: Vec<Self::AttrId>,
82+
stat_type: i32,
83+
epoch_id: Option<Self::EpochId>,
84+
) -> StorageResult<Option<f32>>;
85+
86+
async fn get_cost_analysis(
87+
&self,
88+
expr_id: Self::ExprId,
89+
epoch_id: Self::EpochId,
90+
) -> StorageResult<Option<i32>>;
91+
92+
async fn get_cost(&self, expr_id: Self::ExprId) -> StorageResult<Option<i32>>;
93+
}
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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#![allow(dead_code, unused_imports, unused_variables)]
2+
3+
use crate::{BackendManager, CostModelStorageLayer, StorageResult};
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+
) -> StorageResult<Self::EpochId> {
20+
todo!()
21+
}
22+
23+
async fn update_stats_from_catalog(
24+
&self,
25+
c: CatalogSource,
26+
epoch_id: Self::EpochId,
27+
) -> StorageResult<()> {
28+
todo!()
29+
}
30+
31+
async fn update_stats(&self, stats: i32, epoch_id: Self::EpochId) -> StorageResult<()> {
32+
todo!()
33+
}
34+
35+
async fn store_cost(
36+
&self,
37+
expr_id: Self::ExprId,
38+
cost: i32,
39+
epoch_id: Self::EpochId,
40+
) -> StorageResult<()> {
41+
todo!()
42+
}
43+
44+
async fn store_expr_stats_mappings(
45+
&self,
46+
expr_id: Self::ExprId,
47+
stat_ids: Vec<Self::StatId>,
48+
) -> StorageResult<()> {
49+
todo!()
50+
}
51+
52+
async fn get_stats_for_table(
53+
&self,
54+
table_id: i32,
55+
stat_type: i32,
56+
epoch_id: Option<Self::EpochId>,
57+
) -> StorageResult<Option<f32>> {
58+
todo!()
59+
}
60+
61+
async fn get_stats_for_attr(
62+
&self,
63+
attr_id: i32,
64+
stat_type: i32,
65+
epoch_id: Option<Self::EpochId>,
66+
) -> StorageResult<Option<f32>> {
67+
todo!()
68+
}
69+
70+
async fn get_stats_for_attrs(
71+
&self,
72+
attr_ids: Vec<i32>,
73+
stat_type: i32,
74+
epoch_id: Option<Self::EpochId>,
75+
) -> StorageResult<Option<f32>> {
76+
todo!()
77+
}
78+
79+
async fn get_cost_analysis(
80+
&self,
81+
expr_id: Self::ExprId,
82+
epoch_id: Self::EpochId,
83+
) -> StorageResult<Option<i32>> {
84+
todo!()
85+
}
86+
87+
async fn get_cost(&self, expr_id: Self::ExprId) -> StorageResult<Option<i32>> {
88+
todo!()
89+
}
90+
}

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: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
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 StorageResult<T> = Result<T, BackendError>;
17+
18+
pub enum CostModelError {
19+
// TODO: Add more error types
20+
UnknownStatisticType,
21+
}
22+
23+
pub enum BackendError {
24+
CostModel(CostModelError),
25+
Database(DbErr),
26+
// Add other variants as needed for different error types
27+
}
28+
29+
impl From<CostModelError> for BackendError {
30+
fn from(value: CostModelError) -> Self {
31+
BackendError::CostModel(value)
32+
}
33+
}
34+
35+
impl From<DbErr> for BackendError {
36+
fn from(value: DbErr) -> Self {
37+
BackendError::Database(value)
38+
}
39+
}
40+
41+
pub struct BackendManager {
42+
db: DatabaseConnection,
43+
latest_epoch_id: AtomicUsize,
44+
}
45+
46+
impl BackendManager {
47+
/// Creates a new `BackendManager`.
48+
pub async fn new() -> StorageResult<Self> {
49+
Ok(Self {
50+
db: Database::connect(DATABASE_URL).await?,
51+
latest_epoch_id: AtomicUsize::new(0),
52+
})
53+
}
54+
}
55+
756
pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc";
857
pub const DATABASE_FILE: &str = "./sqlite.db";
58+
pub const TEST_DATABASE_URL: &str = "sqlite:./test.db?mode=rwc";
59+
pub const TEST_DATABASE_FILE: &str = "./test.db";
960

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

0 commit comments

Comments
 (0)