Skip to content

Commit

Permalink
rename cascades_group to group
Browse files Browse the repository at this point in the history
  • Loading branch information
connortsui20 committed Dec 1, 2024
1 parent ce1b93d commit 194ae5e
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 122 deletions.
2 changes: 2 additions & 0 deletions optd-mvp/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ of a problem.

TODO explain the fingerprinting algorithm and how it relates to group merging

Union find data structure with a circular linked list for linear iteration

When taking the fingerprint of an expression, the child groups of an expression need to be root groups. If they are not, we need to try again.
Assuming that all children are root groups, the fingerprint we make for any expression that fulfills that is valid and can be looked up for duplicates.
In order to maintain that correctness, on a merge of two sets, the smaller one requires that a new fingerprint be generated for every expression that has a group in that smaller set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "cascades_group")]
#[sea_orm(table_name = "group")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub status: i8,
pub winner: Option<i32>,
pub cost: Option<i64>,
pub parent_id: Option<i32>,
pub next_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "Entity",
from = "Column::NextId",
to = "Column::Id",
on_update = "Cascade",
on_delete = "SetNull"
)]
SelfRef2,
#[sea_orm(
belongs_to = "Entity",
from = "Column::ParentId",
to = "Column::Id",
on_update = "Cascade",
on_delete = "SetNull"
)]
SelfRef,
SelfRef1,
#[sea_orm(has_many = "super::logical_children::Entity")]
LogicalChildren,
#[sea_orm(has_many = "super::logical_expression::Entity")]
Expand Down Expand Up @@ -56,7 +65,7 @@ impl Related<super::logical_expression::Entity> for Entity {
super::logical_children::Relation::LogicalExpression.def()
}
fn via() -> Option<RelationDef> {
Some(super::logical_children::Relation::CascadesGroup.def().rev())
Some(super::logical_children::Relation::Group.def().rev())
}
}

Expand All @@ -65,11 +74,7 @@ impl Related<super::physical_expression::Entity> for Entity {
super::physical_children::Relation::PhysicalExpression.def()
}
fn via() -> Option<RelationDef> {
Some(
super::physical_children::Relation::CascadesGroup
.def()
.rev(),
)
Some(super::physical_children::Relation::Group.def().rev())
}
}

Expand Down
10 changes: 5 additions & 5 deletions optd-mvp/src/entities/logical_children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::cascades_group::Entity",
belongs_to = "super::group::Entity",
from = "Column::GroupId",
to = "super::cascades_group::Column::Id",
to = "super::group::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
CascadesGroup,
Group,
#[sea_orm(
belongs_to = "super::logical_expression::Entity",
from = "Column::LogicalExpressionId",
Expand All @@ -31,9 +31,9 @@ pub enum Relation {
LogicalExpression,
}

impl Related<super::cascades_group::Entity> for Entity {
impl Related<super::group::Entity> for Entity {
fn to() -> RelationDef {
Relation::CascadesGroup.def()
Relation::Group.def()
}
}

Expand Down
14 changes: 7 additions & 7 deletions optd-mvp/src/entities/logical_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::fingerprint::Entity")]
Fingerprint,
#[sea_orm(
belongs_to = "super::cascades_group::Entity",
belongs_to = "super::group::Entity",
from = "Column::GroupId",
to = "super::cascades_group::Column::Id",
to = "super::group::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
CascadesGroup,
#[sea_orm(has_many = "super::fingerprint::Entity")]
Fingerprint,
Group,
#[sea_orm(has_many = "super::logical_children::Entity")]
LogicalChildren,
}
Expand All @@ -40,9 +40,9 @@ impl Related<super::logical_children::Entity> for Entity {
}
}

impl Related<super::cascades_group::Entity> for Entity {
impl Related<super::group::Entity> for Entity {
fn to() -> RelationDef {
super::logical_children::Relation::CascadesGroup.def()
super::logical_children::Relation::Group.def()
}
fn via() -> Option<RelationDef> {
Some(
Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pub mod prelude;

pub mod cascades_group;
pub mod fingerprint;
pub mod group;
pub mod logical_children;
pub mod logical_expression;
pub mod physical_children;
Expand Down
10 changes: 5 additions & 5 deletions optd-mvp/src/entities/physical_children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::cascades_group::Entity",
belongs_to = "super::group::Entity",
from = "Column::GroupId",
to = "super::cascades_group::Column::Id",
to = "super::group::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
CascadesGroup,
Group,
#[sea_orm(
belongs_to = "super::physical_expression::Entity",
from = "Column::PhysicalExpressionId",
Expand All @@ -31,9 +31,9 @@ pub enum Relation {
PhysicalExpression,
}

impl Related<super::cascades_group::Entity> for Entity {
impl Related<super::group::Entity> for Entity {
fn to() -> RelationDef {
Relation::CascadesGroup.def()
Relation::Group.def()
}
}

Expand Down
10 changes: 5 additions & 5 deletions optd-mvp/src/entities/physical_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::cascades_group::Entity",
belongs_to = "super::group::Entity",
from = "Column::GroupId",
to = "super::cascades_group::Column::Id",
to = "super::group::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
CascadesGroup,
Group,
#[sea_orm(has_many = "super::physical_children::Entity")]
PhysicalChildren,
}
Expand All @@ -32,9 +32,9 @@ impl Related<super::physical_children::Entity> for Entity {
}
}

impl Related<super::cascades_group::Entity> for Entity {
impl Related<super::group::Entity> for Entity {
fn to() -> RelationDef {
super::physical_children::Relation::CascadesGroup.def()
super::physical_children::Relation::Group.def()
}
fn via() -> Option<RelationDef> {
Some(
Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![allow(unused_imports)]

pub use super::cascades_group::Entity as CascadesGroup;
pub use super::fingerprint::Entity as Fingerprint;
pub use super::group::Entity as Group;
pub use super::logical_children::Entity as LogicalChildren;
pub use super::logical_expression::Entity as LogicalExpression;
pub use super::physical_children::Entity as PhysicalChildren;
Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/expression/logical_expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Definition of logical expressions / relations in the Cascades query optimization framework.
//! Definition of logical expressions / relations in our query optimization framework.
//!
//! FIXME: All fields are placeholders.
//!
Expand Down
4 changes: 2 additions & 2 deletions optd-mvp/src/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! In-memory representation of Cascades logical and physical expression / operators / relations.
//! In-memory representation of logical and physical expression / operators / relations.
//!
//! TODO more docs.
Expand All @@ -8,7 +8,7 @@ pub use logical_expression::*;
mod physical_expression;
pub use physical_expression::*;

/// The representation of a Cascades expression.
/// The representation of an expression.
///
/// TODO more docs.
#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/expression/physical_expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Definition of physical expressions / operators in the Cascades query optimization framework.
//! Definition of physical expressions / operators in our query optimization framework.
//!
//! FIXME: All fields are placeholders.
//!
Expand Down
3 changes: 1 addition & 2 deletions optd-mvp/src/memo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! This module contains items related to the memo table, which is key to the Cascades query
//! optimization framework.
//! This module contains items related to the memo table.
//!
//! TODO more docs.
Expand Down
42 changes: 19 additions & 23 deletions optd-mvp/src/memo/persistent/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl PersistentMemo {
}

delete_all! {
cascades_group,
group,
fingerprint,
logical_expression,
logical_children,
Expand All @@ -54,13 +54,13 @@ impl PersistentMemo {
};
}

/// Retrieves a [`cascades_group::Model`] given its ID.
/// Retrieves a [`group::Model`] given its ID.
///
/// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
///
/// FIXME: use an in-memory representation of a group instead.
pub async fn get_group(&self, group_id: GroupId) -> OptimizerResult<cascades_group::Model> {
Ok(cascades_group::Entity::find_by_id(group_id.0)
pub async fn get_group(&self, group_id: GroupId) -> OptimizerResult<group::Model> {
Ok(group::Entity::find_by_id(group_id.0)
.one(&self.db)
.await?
.ok_or(MemoError::UnknownGroup(group_id))?)
Expand All @@ -78,11 +78,7 @@ impl PersistentMemo {

// Traverse up the path and find the root group, keeping track of groups we have visited.
let mut path = vec![];
loop {
let Some(parent_id) = curr_group.parent_id else {
break;
};

while let Some(parent_id) = curr_group.parent_id {
let next_group = self.get_group(GroupId(parent_id)).await?;
path.push(curr_group);
curr_group = next_group;
Expand Down Expand Up @@ -468,29 +464,29 @@ impl PersistentMemo {
}

// The expression does not exist yet, so we need to create a new group and new expression.
let group = cascades_group::ActiveModel {
winner: Set(None),
let group = group::ActiveModel {
status: Set(0), // `GroupStatus::InProgress` status.
..Default::default()
};

// Create the new group.
let res = cascades_group::Entity::insert(group).exec(&self.db).await?;
let group_res = group::Entity::insert(group).exec(&self.db).await?;
let group_id = group_res.last_insert_id;

// Insert the input expression into the newly created group.
let model: logical_expression::Model = logical_expression.clone().into();
let mut active_model = model.into_active_model();
active_model.group_id = Set(res.last_insert_id);
active_model.id = NotSet;
let new_model = active_model.insert(&self.db).await?;
let expression: logical_expression::Model = logical_expression.clone().into();
let mut active_expression = expression.into_active_model();
active_expression.group_id = Set(group_id);
active_expression.id = NotSet;
let new_expression = active_expression.insert(&self.db).await?;

let group_id = new_model.group_id;
let expr_id = new_model.id;
let group_id = new_expression.group_id;
let expr_id = new_expression.id;

// Insert the child groups of the expression into the junction / children table.
logical_children::Entity::insert_many(children.iter().copied().map(|child_id| {
logical_children::ActiveModel {
logical_expression_id: Set(new_model.id),
logical_expression_id: Set(new_expression.id),
group_id: Set(child_id.0),
}
}))
Expand All @@ -499,8 +495,8 @@ impl PersistentMemo {
.await?;

// Finally, insert the fingerprint of the logical expression as well.
let new_expr: LogicalExpression = new_model.into();
let kind = new_expr.kind();
let new_logical_expression: LogicalExpression = new_expression.into();
let kind = new_logical_expression.kind();

// In order to calculate a correct fingerprint, we will want to use the IDs of the root
// groups of the children instead of the child ID themselves.
Expand All @@ -509,7 +505,7 @@ impl PersistentMemo {
let root_id = self.get_root_group(child_id).await?;
rewrites.push((child_id, root_id));
}
let hash = new_expr.fingerprint_with_rewrite(&rewrites);
let hash = new_logical_expression.fingerprint_with_rewrite(&rewrites);

let fingerprint = fingerprint::ActiveModel {
id: NotSet,
Expand Down
Loading

0 comments on commit 194ae5e

Please sign in to comment.