Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memo trait #17

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions optd-cost-model/src/cost/agg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 6 additions & 6 deletions optd-persistent/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion optd-persistent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sea-orm = { version = "1.1.1", features = [
"with-json",
] }
sea-orm-migration = "1.0.0"
serde_json = "1.0"
serde_json = "1.0.118" # Hash implementation on serde_json::Value
tokio = { version = "1.0.1", features = ["macros", "rt-multi-thread"] }
trait-variant = "0.1.2"

Expand Down
40 changes: 24 additions & 16 deletions optd-persistent/src/cost_model/orm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,13 @@ impl CostModelStorageLayer for BackendManager {
match res {
Ok(insert_res) => insert_res.last_insert_id,
Err(_) => {
return Err(BackendError::BackendError(format!(
"failed to insert statistic {:?} into statistic table",
stat
)))
return Err(BackendError::CostModel(
format!(
"failed to insert statistic {:?} into statistic table",
stat
)
.into(),
))
}
}
}
Expand Down Expand Up @@ -450,10 +453,13 @@ impl CostModelStorageLayer for BackendManager {
.collect::<Vec<_>>();

if attr_ids.len() != attr_base_indices.len() {
return Err(BackendError::BackendError(format!(
"Not all attributes found for table_id {} and base indices {:?}",
table_id, attr_base_indices
)));
return Err(BackendError::CostModel(
format!(
"Not all attributes found for table_id {} and base indices {:?}",
table_id, attr_base_indices
)
.into(),
));
}

self.get_stats_for_attr(attr_ids, stat_type, epoch_id).await
Expand Down Expand Up @@ -505,10 +511,13 @@ impl CostModelStorageLayer for BackendManager {
.one(&self.db)
.await?;
if expr_exists.is_none() {
return Err(BackendError::BackendError(format!(
"physical expression id {} not found when storing cost",
physical_expression_id
)));
return Err(BackendError::CostModel(
format!(
"physical expression id {} not found when storing cost",
physical_expression_id
)
.into(),
));
}

// Check if epoch_id exists in Event table
Expand All @@ -518,10 +527,9 @@ impl CostModelStorageLayer for BackendManager {
.await
.unwrap();
if epoch_exists.is_none() {
return Err(BackendError::BackendError(format!(
"epoch id {} not found when storing cost",
epoch_id
)));
return Err(BackendError::CostModel(
format!("epoch id {} not found when storing cost", epoch_id).into(),
));
}

let new_cost = plan_cost::ActiveModel {
Expand Down
60 changes: 60 additions & 0 deletions optd-persistent/src/entities/predicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "predicate")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub data: Json,
pub variant: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::predicate_logical_expression_junction::Entity")]
PredicateLogicalExpressionJunction,
#[sea_orm(has_many = "super::predicate_physical_expression_junction::Entity")]
PredicatePhysicalExpressionJunction,
}

impl Related<super::predicate_logical_expression_junction::Entity> for Entity {
fn to() -> RelationDef {
Relation::PredicateLogicalExpressionJunction.def()
}
}

impl Related<super::predicate_physical_expression_junction::Entity> for Entity {
fn to() -> RelationDef {
Relation::PredicatePhysicalExpressionJunction.def()
}
}

impl Related<super::logical_expression::Entity> for Entity {
fn to() -> RelationDef {
super::predicate_logical_expression_junction::Relation::LogicalExpression.def()
}
fn via() -> Option<RelationDef> {
Some(
super::predicate_logical_expression_junction::Relation::Predicate
.def()
.rev(),
)
}
}

impl Related<super::physical_expression::Entity> for Entity {
fn to() -> RelationDef {
super::predicate_physical_expression_junction::Relation::PhysicalExpression.def()
}
fn via() -> Option<RelationDef> {
Some(
super::predicate_physical_expression_junction::Relation::Predicate
.def()
.rev(),
)
}
}

impl ActiveModelBehavior for ActiveModel {}
34 changes: 34 additions & 0 deletions optd-persistent/src/entities/predicate_children.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "predicate_children")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub parent_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub child_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::predicate::Entity",
from = "Column::ChildId",
to = "super::predicate::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Predicate2,
#[sea_orm(
belongs_to = "super::predicate::Entity",
from = "Column::ParentId",
to = "super::predicate::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Predicate1,
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "predicate_logical_expression_junction")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub logical_expr_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub predicate_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::logical_expression::Entity",
from = "Column::LogicalExprId",
to = "super::logical_expression::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
LogicalExpression,
#[sea_orm(
belongs_to = "super::predicate::Entity",
from = "Column::PredicateId",
to = "super::predicate::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Predicate,
}

impl Related<super::logical_expression::Entity> for Entity {
fn to() -> RelationDef {
Relation::LogicalExpression.def()
}
}

impl Related<super::predicate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Predicate.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "predicate_physical_expression_junction")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub physical_expr_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub predicate_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::physical_expression::Entity",
from = "Column::PhysicalExprId",
to = "super::physical_expression::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
PhysicalExpression,
#[sea_orm(
belongs_to = "super::predicate::Entity",
from = "Column::PredicateId",
to = "super::predicate::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Predicate,
}

impl Related<super::physical_expression::Entity> for Entity {
fn to() -> RelationDef {
Relation::PhysicalExpression.def()
}
}

impl Related<super::predicate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Predicate.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
43 changes: 40 additions & 3 deletions optd-persistent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod migrator;
pub mod cost_model;
pub use cost_model::interface::CostModelStorageLayer;

mod memo;
pub use memo::interface::Memo;

/// The filename of the SQLite database for migration.
pub const DATABASE_FILENAME: &str = "sqlite.db";
/// The URL of the SQLite database for migration.
Expand All @@ -39,17 +42,48 @@ fn get_sqlite_url(file: &str) -> String {
format!("sqlite:{}?mode=rwc", file)
}

pub type StorageResult<T> = Result<T, BackendError>;
#[derive(Debug)]
pub enum CostModelError {
// TODO: Add more error types
UnknownStatisticType,
VersionedStatisticNotFound,
CustomError(String),
}

/// TODO convert this to `thiserror`
#[derive(Debug)]
/// The different kinds of errors that might occur while running operations on a memo table.
pub enum MemoError {
UnknownGroup,
UnknownLogicalExpression,
UnknownPhysicalExpression,
InvalidExpression,
}

/// TODO convert this to `thiserror`
#[derive(Debug)]
pub enum BackendError {
Memo(MemoError),
DatabaseError(DbErr),
CostModel(CostModelError),
BackendError(String),
}

impl From<String> for BackendError {
impl From<String> for CostModelError {
fn from(value: String) -> Self {
BackendError::BackendError(value)
CostModelError::CustomError(value)
}
}

impl From<CostModelError> for BackendError {
fn from(value: CostModelError) -> Self {
BackendError::CostModel(value)
}
}

impl From<MemoError> for BackendError {
fn from(value: MemoError) -> Self {
BackendError::Memo(value)
}
}

Expand All @@ -59,6 +93,9 @@ impl From<DbErr> for BackendError {
}
}

/// A type alias for a result with [`BackendError`] as the error type.
pub type StorageResult<T> = Result<T, BackendError>;

pub struct BackendManager {
db: DatabaseConnection,
}
Expand Down
11 changes: 11 additions & 0 deletions optd-persistent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ use optd_persistent::DATABASE_URL;

#[tokio::main]
async fn main() {
basic_demo().await;
memo_demo().await;
}

async fn memo_demo() {
let _db = Database::connect(DATABASE_URL).await.unwrap();

todo!()
}

async fn basic_demo() {
let db = Database::connect(DATABASE_URL).await.unwrap();

// Create a new `CascadesGroup`.
Expand Down
Loading