Skip to content

Commit

Permalink
add initial rust structs
Browse files Browse the repository at this point in the history
Signed-off-by: Yuchen Liang <[email protected]>
  • Loading branch information
yliang412 committed Jan 19, 2025
1 parent a7462f8 commit 303fc9b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ resolver = "2"
diesel = { version = "2.2", features = [
"sqlite",
"returning_clauses_for_sqlite_3_35",
"chrono",
] }
chrono = "0.4.39"
1 change: 1 addition & 0 deletions optd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
diesel.workspace = true
chrono.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE physical_exprs (
-- The physical property dervied based on the properties of the children nodes.
derived_phys_prop_id BIGINT NOT NULL,
-- The cost associated with the physical expression.
cost REAL NOT NULL,
cost DOUBLE NOT NULL,
-- Time at which the physical expression is created.
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
FOREIGN KEY (typ_desc) REFERENCES physical_typ_descs(id) ON DELETE CASCADE ON UPDATE CASCADE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE scalar_exprs (
-- Time at which the logical expression is created.
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
-- The cost associated computing the scalar expression.
cost REAL, -- TODO: This can be NULL, do we want a seperate table?
cost DOUBLE, -- TODO: This can be NULL, do we want a seperate table?
FOREIGN KEY (typ_desc) REFERENCES scalar_typ_descs(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (group_id) REFERENCES scalar_groups(id) ON DELETE CASCADE ON UPDATE CASCADE
);
3 changes: 2 additions & 1 deletion optd/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod schema;
pub mod models;
pub mod schema;
120 changes: 120 additions & 0 deletions optd/src/storage/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use diesel::prelude::*;

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::logical_exprs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct LogicalExpr {
pub id: i64,
pub typ_desc: i64,
pub group_id: i64,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::logical_props)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct LogicalProp {
pub id: i64,
pub group_id: i64,
pub card_est: i64,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::logical_typ_descs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct LogicalTypDesc {
pub id: i64,
pub name: String,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::physical_exprs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct PhysicalExpr {
pub id: i64,
pub typ_desc: i64,
pub group_id: i64,
pub derived_phys_prop_id: i64,
pub cost: f64,
pub created_at: chrono::NaiveDateTime,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::physical_props)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct PhysicalProp {
pub id: i64,
pub payload: Vec<u8>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::physical_typ_descs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct PhysicalTypDesc {
pub id: i64,
pub name: String,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::rel_groups)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct RelGroup {
pub id: i64,
pub status: i32,
pub created_at: chrono::NaiveDateTime,
pub rep_id: Option<i64>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::rel_group_winners)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct RelGroupWinner {
pub group_id: i64,
pub required_phys_prop_id: i64,
pub physical_expr_id: i64,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::scalar_exprs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct ScalarExpr {
pub id: i64,
pub typ_desc: i64,
pub group_id: i64,
pub created_at: chrono::NaiveDateTime,
pub cost: Option<f64>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::scalar_groups)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct ScalarGroup {
pub id: i64,
pub status: i32,
pub created_at: chrono::NaiveDateTime,
pub rep_id: Option<i64>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::scalar_group_winners)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct ScalarGroupWinner {
pub group_id: i64,
pub scalar_expr_id: i64,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::scalar_props)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct ScalarProp {
pub id: i64,
pub payload: Vec<u8>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = super::schema::scalar_typ_descs)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct ScalarTyeDesc {
pub id: i64,
pub name: String,
}
4 changes: 2 additions & 2 deletions optd/src/storage/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ diesel::table! {
typ_desc -> BigInt,
group_id -> BigInt,
derived_phys_prop_id -> BigInt,
cost -> Float,
cost -> Double,
created_at -> Timestamp,
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ diesel::table! {
typ_desc -> BigInt,
group_id -> BigInt,
created_at -> Timestamp,
cost -> Nullable<Float>,
cost -> Nullable<Double>,
}
}

Expand Down

0 comments on commit 303fc9b

Please sign in to comment.