From 6c3a8907989e76866fa60b9b27bd2992e28b2e55 Mon Sep 17 00:00:00 2001 From: Lan Lou Date: Wed, 6 Nov 2024 18:33:34 -0500 Subject: [PATCH] Separate memo and cost model storage_layer trait --- .../src/cost_model_orm_manager_impl.rs | 86 +++++++++ .../src/cost_model_storage_layer.rs | 75 ++++++++ optd-persistent/src/lib.rs | 13 +- optd-persistent/src/memo_orm_manager_impl.rs | 83 +++++++++ ...storage_layer.rs => memo_storage_layer.rs} | 65 +------ optd-persistent/src/orm_manager.rs | 166 +----------------- 6 files changed, 261 insertions(+), 227 deletions(-) create mode 100644 optd-persistent/src/cost_model_orm_manager_impl.rs create mode 100644 optd-persistent/src/cost_model_storage_layer.rs create mode 100644 optd-persistent/src/memo_orm_manager_impl.rs rename optd-persistent/src/{storage_layer.rs => memo_storage_layer.rs} (70%) diff --git a/optd-persistent/src/cost_model_orm_manager_impl.rs b/optd-persistent/src/cost_model_orm_manager_impl.rs new file mode 100644 index 0000000..ef8edce --- /dev/null +++ b/optd-persistent/src/cost_model_orm_manager_impl.rs @@ -0,0 +1,86 @@ +#![allow(dead_code, unused_imports, unused_variables)] + +use crate::memo_storage_layer::MemoStorageLayer; +use crate::orm_manager::ORMManager; + +impl MemoStorageLayer for ORMManager { + async fn get_group_winner_from_group_id( + &self, + group_id: i32, + ) -> crate::StorageResult> { + todo!() + } + + async fn add_new_expr( + &mut self, + expr: crate::memo_storage_layer::Expression, + ) -> crate::StorageResult<(crate::GroupId, crate::ExprId)> { + todo!() + } + + async fn add_expr_to_group( + &mut self, + expr: crate::memo_storage_layer::Expression, + group_id: crate::GroupId, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_group_id(&self, expr_id: crate::ExprId) -> crate::StorageResult { + todo!() + } + + async fn get_expr_memoed( + &self, + expr_id: crate::ExprId, + ) -> crate::StorageResult { + todo!() + } + + async fn get_all_group_ids(&self) -> crate::StorageResult> { + todo!() + } + + async fn get_group( + &self, + group_id: crate::GroupId, + ) -> crate::StorageResult { + todo!() + } + + async fn update_group_winner( + &mut self, + group_id: crate::GroupId, + latest_winner: Option, + ) -> crate::StorageResult<()> { + todo!() + } + + async fn get_all_exprs_in_group( + &self, + group_id: crate::GroupId, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_group_info( + &self, + group_id: crate::GroupId, + ) -> crate::StorageResult<&Option> { + todo!() + } + + async fn get_predicate_binding( + &self, + group_id: crate::GroupId, + ) -> crate::StorageResult> { + todo!() + } + + async fn try_get_predicate_binding( + &self, + group_id: crate::GroupId, + ) -> crate::StorageResult> { + todo!() + } +} diff --git a/optd-persistent/src/cost_model_storage_layer.rs b/optd-persistent/src/cost_model_storage_layer.rs new file mode 100644 index 0000000..0dc5ae2 --- /dev/null +++ b/optd-persistent/src/cost_model_storage_layer.rs @@ -0,0 +1,75 @@ +#![allow(dead_code, unused_imports)] + +use crate::entities::cascades_group; +use crate::entities::event::Model as event_model; +use crate::entities::logical_expression; +use crate::entities::physical_expression; +use crate::{EpochId, ExprId, StatId, StorageResult}; +use sea_orm::*; +use sea_orm_migration::prelude::*; +use serde_json::json; +use std::sync::Arc; + +pub enum CatalogSource { + Iceberg(), +} + +pub trait CostModelStorageLayer { + // TODO: Change EpochId to event::Model::epoch_id + async fn create_new_epoch(&mut self, source: String, data: String) -> StorageResult; + + async fn update_stats_from_catalog( + &self, + c: CatalogSource, + epoch_id: EpochId, + ) -> StorageResult<()>; + + // i32 in `stats:i32` is a placeholder for the stats type + async fn update_stats(&self, stats: i32, epoch_id: EpochId) -> StorageResult<()>; + + async fn store_cost(&self, expr_id: ExprId, cost: i32, epoch_id: EpochId) -> StorageResult<()>; + + async fn store_expr_stats_mappings( + &self, + expr_id: ExprId, + stat_ids: Vec, + ) -> StorageResult<()>; + + /// Get the statistics for a given table. + /// + /// If `epoch_id` is None, it will return the latest statistics. + async fn get_stats_for_table( + &self, + table_id: i32, + stat_type: i32, + epoch_id: Option, + ) -> StorageResult>; + + /// Get the statistics for a given attribute. + /// + /// If `epoch_id` is None, it will return the latest statistics. + async fn get_stats_for_attr( + &self, + attr_id: i32, + stat_type: i32, + epoch_id: Option, + ) -> StorageResult>; + + /// Get the joint statistics for a list of attributes. + /// + /// If `epoch_id` is None, it will return the latest statistics. + async fn get_stats_for_attrs( + &self, + attr_ids: Vec, + stat_type: i32, + epoch_id: Option, + ) -> StorageResult>; + + async fn get_cost_analysis( + &self, + expr_id: ExprId, + epoch_id: EpochId, + ) -> StorageResult>; + + async fn get_cost(&self, expr_id: ExprId) -> StorageResult>; +} diff --git a/optd-persistent/src/lib.rs b/optd-persistent/src/lib.rs index 8e1e149..e63da11 100644 --- a/optd-persistent/src/lib.rs +++ b/optd-persistent/src/lib.rs @@ -1,12 +1,23 @@ use sea_orm::*; use sea_orm_migration::prelude::*; +mod cost_model_orm_manager_impl; +mod cost_model_storage_layer; mod entities; +mod memo_orm_manager_impl; +mod memo_storage_layer; mod migrator; mod orm_manager; -mod storage_layer; + use migrator::Migrator; +pub type GroupId = i32; +pub type ExprId = i32; +pub type EpochId = i32; +pub type StatId = i32; + +pub type StorageResult = Result; + pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc"; pub const DATABASE_FILE: &str = "./sqlite.db"; pub const TEST_DATABASE_URL: &str = "sqlite:./test.db?mode=rwc"; diff --git a/optd-persistent/src/memo_orm_manager_impl.rs b/optd-persistent/src/memo_orm_manager_impl.rs new file mode 100644 index 0000000..c8d3b44 --- /dev/null +++ b/optd-persistent/src/memo_orm_manager_impl.rs @@ -0,0 +1,83 @@ +#![allow(dead_code, unused_imports, unused_variables)] + +use crate::cost_model_storage_layer::CostModelStorageLayer; +use crate::memo_storage_layer::MemoStorageLayer; +use crate::orm_manager::ORMManager; + +impl CostModelStorageLayer for ORMManager { + async fn create_new_epoch( + &mut self, + source: String, + data: String, + ) -> crate::StorageResult { + todo!() + } + + async fn update_stats_from_catalog( + &self, + c: crate::cost_model_storage_layer::CatalogSource, + epoch_id: crate::EpochId, + ) -> crate::StorageResult<()> { + todo!() + } + + async fn update_stats(&self, stats: i32, epoch_id: crate::EpochId) -> crate::StorageResult<()> { + todo!() + } + + async fn store_cost( + &self, + expr_id: crate::ExprId, + cost: i32, + epoch_id: crate::EpochId, + ) -> crate::StorageResult<()> { + todo!() + } + + async fn store_expr_stats_mappings( + &self, + expr_id: crate::ExprId, + stat_ids: Vec, + ) -> crate::StorageResult<()> { + todo!() + } + + async fn get_stats_for_table( + &self, + table_id: i32, + stat_type: i32, + epoch_id: Option, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_stats_for_attr( + &self, + attr_id: i32, + stat_type: i32, + epoch_id: Option, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_stats_for_attrs( + &self, + attr_ids: Vec, + stat_type: i32, + epoch_id: Option, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_cost_analysis( + &self, + expr_id: crate::ExprId, + epoch_id: crate::EpochId, + ) -> crate::StorageResult> { + todo!() + } + + async fn get_cost(&self, expr_id: crate::ExprId) -> crate::StorageResult> { + todo!() + } +} diff --git a/optd-persistent/src/storage_layer.rs b/optd-persistent/src/memo_storage_layer.rs similarity index 70% rename from optd-persistent/src/storage_layer.rs rename to optd-persistent/src/memo_storage_layer.rs index 93590f6..b5280f7 100644 --- a/optd-persistent/src/storage_layer.rs +++ b/optd-persistent/src/memo_storage_layer.rs @@ -4,21 +4,12 @@ use crate::entities::cascades_group; use crate::entities::event::Model as event_model; use crate::entities::logical_expression; use crate::entities::physical_expression; +use crate::{ExprId, GroupId, StorageResult}; use sea_orm::*; use sea_orm_migration::prelude::*; use serde_json::json; use std::sync::Arc; -pub type GroupId = i32; -pub type ExprId = i32; -pub type EpochId = i32; - -pub type StorageResult = Result; - -pub enum CatalogSource { - Iceberg(), -} - pub enum Expression { LogicalExpression(logical_expression::Model), PhysicalExpression(physical_expression::Model), @@ -37,59 +28,7 @@ pub enum Expression { // The optd WinnerInfo struct makes everything too coupled. pub struct WinnerInfo {} -pub trait StorageLayer { - // TODO: Change EpochId to event::Model::epoch_id - async fn create_new_epoch(&mut self, source: String, data: String) -> StorageResult; - - async fn update_stats_from_catalog( - &self, - c: CatalogSource, - epoch_id: EpochId, - ) -> StorageResult<()>; - - // i32 in `stats:i32` is a placeholder for the stats type - async fn update_stats(&self, stats: i32, epoch_id: EpochId) -> StorageResult<()>; - - async fn store_cost(&self, expr_id: ExprId, cost: i32, epoch_id: EpochId) -> StorageResult<()>; - - /// Get the statistics for a given table. - /// - /// If `epoch_id` is None, it will return the latest statistics. - async fn get_stats_for_table( - &self, - table_id: i32, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult>; - - /// Get the statistics for a given attribute. - /// - /// If `epoch_id` is None, it will return the latest statistics. - async fn get_stats_for_attr( - &self, - attr_id: i32, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult>; - - /// Get the joint statistics for a list of attributes. - /// - /// If `epoch_id` is None, it will return the latest statistics. - async fn get_stats_for_attrs( - &self, - attr_ids: Vec, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult>; - - async fn get_cost_analysis( - &self, - expr_id: ExprId, - epoch_id: EpochId, - ) -> StorageResult>; - - async fn get_cost(&self, expr_id: ExprId) -> StorageResult>; - +pub trait MemoStorageLayer { async fn get_group_winner_from_group_id( &self, group_id: i32, diff --git a/optd-persistent/src/orm_manager.rs b/optd-persistent/src/orm_manager.rs index 35c8885..11d0c9a 100644 --- a/optd-persistent/src/orm_manager.rs +++ b/optd-persistent/src/orm_manager.rs @@ -1,11 +1,8 @@ #![allow(dead_code, unused_imports, unused_variables)] -use crate::entities::physical_expression; -use crate::storage_layer::{self, EpochId, StorageLayer, StorageResult}; -use crate::DATABASE_URL; -use sea_orm::DatabaseConnection; -use sea_orm::*; -use sea_orm_migration::prelude::*; +use sea_orm::{Database, DatabaseConnection}; + +use crate::{EpochId, DATABASE_URL}; pub struct ORMManager { db_conn: DatabaseConnection, @@ -25,160 +22,3 @@ impl ORMManager { } } } - -impl StorageLayer for ORMManager { - async fn create_new_epoch( - &mut self, - source: String, - data: String, - ) -> StorageResult { - todo!() - } - - async fn update_stats_from_catalog( - &self, - c: storage_layer::CatalogSource, - epoch_id: storage_layer::EpochId, - ) -> StorageResult<()> { - todo!() - } - - async fn update_stats( - &self, - stats: i32, - epoch_id: storage_layer::EpochId, - ) -> StorageResult<()> { - todo!() - } - - async fn store_cost( - &self, - expr_id: storage_layer::ExprId, - cost: i32, - epoch_id: storage_layer::EpochId, - ) -> StorageResult<()> { - todo!() - } - - async fn get_stats_for_table( - &self, - table_id: i32, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult> { - todo!() - } - - async fn get_stats_for_attr( - &self, - attr_id: i32, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult> { - todo!() - } - - async fn get_stats_for_attrs( - &self, - attr_ids: Vec, - stat_type: i32, - epoch_id: Option, - ) -> StorageResult> { - todo!() - } - - async fn get_cost_analysis( - &self, - expr_id: storage_layer::ExprId, - epoch_id: storage_layer::EpochId, - ) -> StorageResult> { - todo!() - } - - async fn get_cost(&self, expr_id: storage_layer::ExprId) -> StorageResult> { - todo!() - } - - async fn get_group_winner_from_group_id( - &self, - group_id: i32, - ) -> StorageResult> { - todo!() - } - - async fn add_new_expr( - &mut self, - expr: storage_layer::Expression, - ) -> StorageResult<(storage_layer::GroupId, storage_layer::ExprId)> { - todo!() - } - - async fn add_expr_to_group( - &mut self, - expr: storage_layer::Expression, - group_id: storage_layer::GroupId, - ) -> StorageResult> { - todo!() - } - - async fn get_group_id( - &self, - expr_id: storage_layer::ExprId, - ) -> StorageResult { - todo!() - } - - async fn get_expr_memoed( - &self, - expr_id: storage_layer::ExprId, - ) -> StorageResult { - todo!() - } - - async fn get_all_group_ids(&self) -> StorageResult> { - todo!() - } - - async fn get_group( - &self, - group_id: storage_layer::GroupId, - ) -> StorageResult { - todo!() - } - - async fn update_group_winner( - &mut self, - group_id: storage_layer::GroupId, - latest_winner: Option, - ) -> StorageResult<()> { - todo!() - } - - async fn get_all_exprs_in_group( - &self, - group_id: storage_layer::GroupId, - ) -> StorageResult> { - todo!() - } - - async fn get_group_info( - &self, - group_id: storage_layer::GroupId, - ) -> StorageResult<&Option> { - todo!() - } - - async fn get_predicate_binding( - &self, - group_id: storage_layer::GroupId, - ) -> StorageResult> { - todo!() - } - - async fn try_get_predicate_binding( - &self, - group_id: storage_layer::GroupId, - ) -> StorageResult> { - todo!() - } -}