Skip to content

Commit

Permalink
restructure everything and add rules module
Browse files Browse the repository at this point in the history
  • Loading branch information
connortsui20 committed Jan 28, 2025
1 parent dadf40c commit 7d405d1
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 67 deletions.
4 changes: 3 additions & 1 deletion optd-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#![allow(unused)]
#![allow(unused_variables)]

pub mod expression;
pub mod memo;
pub mod operator;
pub mod plan;
pub mod rules;

/// TODO make distinction between relational groups and scalar groups.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GroupId(u64);

/// TODO Add docs.
#[allow(dead_code)]
pub struct ExprId(u64);
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::expression::{relational::logical::LogicalExpr, Expr};
use crate::plan::partial_logical_plan::PartialLogicalPlan;
use crate::plan::partial_physical_plan::PartialPhysicalPlan;
use crate::expression::Expr;
use crate::{ExprId, GroupId};

use super::Memo;
pub struct Memo;

impl Memo {
pub async fn add_expr(&mut self, logical_expr: Expr) -> (ExprId, GroupId) {
Expand Down
4 changes: 0 additions & 4 deletions optd-types/src/memo/mod.rs

This file was deleted.

56 changes: 0 additions & 56 deletions optd-types/src/memo/rule.rs

This file was deleted.

2 changes: 1 addition & 1 deletion optd-types/src/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, sync::Arc};
use std::marker::PhantomData;

pub mod logical;
pub mod physical;
Expand Down
2 changes: 1 addition & 1 deletion optd-types/src/plan/physical_plan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::operator::{logical::LogicalOperator, physical::PhysicalOperator, ScalarOperator};
use crate::operator::{physical::PhysicalOperator, ScalarOperator};
use std::sync::Arc;

/// TODO Add docs.
Expand Down
13 changes: 13 additions & 0 deletions optd-types/src/rules/implementation/hash_join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub struct HashJoinRule;

impl ImplementationRule for HashJoinRule {
async fn check_pattern(&self, expr: Expr, memo: &Memo) -> Vec<PartialPhysicalPlan> {
todo!()
}

fn apply(&self, expr: PartialPhysicalPlan) -> Vec<Expr> {
todo!()
}
}
31 changes: 31 additions & 0 deletions optd-types/src/rules/implementation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{expression::Expr, memo::Memo, plan::partial_physical_plan::PartialPhysicalPlan};

#[allow(dead_code)]
#[trait_variant::make(Send)]
pub trait ImplementationRule {
/// Checks if the implementation rule matches the current expression and its children.
/// Returns a vector of partially materialized physical plans.
///
/// This returns a vector because the rule matching the input root expression could have matched
/// with multiple child expressions.
///
/// For example, let's say the input expression is `Filter(G1)`, and the group G1 has
/// two expressions `e1 = HashJoin(HashJoin(A, B), C)` and `e2 = HashJoin(A, HashJoin(B, C))`.
///
/// If the rule wants to match against `Filter(HashJoin(?L, ?R))`, then this function will
/// partially materialize two expressions `Filter(e1)` and `Filter(e2)`. It is then up to the
/// memo table API to apply modifications to the partially materialized physical plans (for
/// example, a pushing a filter predicate into the condition of the `HashJoin`).
///
/// TODO: Ideally this should return a `Stream` instead of a fully materialized Vector.
async fn check_pattern(&self, expr: Expr, memo: &Memo) -> Vec<PartialPhysicalPlan>;

/// Applys modifications to a partially materialized physical plan.
///
/// These changes can create new expressions (logical, physical, and scalar).
fn apply(&self, expr: PartialPhysicalPlan) -> Vec<Expr>;
}

pub mod hash_join;
pub mod physical_filter;
pub mod table_scan;
13 changes: 13 additions & 0 deletions optd-types/src/rules/implementation/physical_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub struct PhysicalFilterRule;

impl ImplementationRule for PhysicalFilterRule {
async fn check_pattern(&self, expr: Expr, memo: &Memo) -> Vec<PartialPhysicalPlan> {
todo!()
}

fn apply(&self, expr: PartialPhysicalPlan) -> Vec<Expr> {
todo!()
}
}
13 changes: 13 additions & 0 deletions optd-types/src/rules/implementation/table_scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub struct TableScanRule;

impl ImplementationRule for TableScanRule {
async fn check_pattern(&self, expr: Expr, memo: &Memo) -> Vec<PartialPhysicalPlan> {
todo!()
}

fn apply(&self, expr: PartialPhysicalPlan) -> Vec<Expr> {
todo!()
}
}
5 changes: 5 additions & 0 deletions optd-types/src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! This module rules.
mod transformation;

mod implementation;
13 changes: 13 additions & 0 deletions optd-types/src/rules/transformation/join_associativity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub struct JoinAssociativityRule;

impl TransformationRule for JoinAssociativityRule {
async fn check_pattern(&self, expr: LogicalExpr, memo: &Memo) -> Vec<PartialLogicalPlan> {
todo!()
}

fn apply(&self, expr: PartialLogicalPlan) -> Vec<Expr> {
todo!()
}
}
13 changes: 13 additions & 0 deletions optd-types/src/rules/transformation/join_commutativity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub struct JoinCommutativityRule;

impl TransformationRule for JoinCommutativityRule {
async fn check_pattern(&self, expr: LogicalExpr, memo: &Memo) -> Vec<PartialLogicalPlan> {
todo!()
}

fn apply(&self, expr: PartialLogicalPlan) -> Vec<Expr> {
todo!()
}
}
35 changes: 35 additions & 0 deletions optd-types/src/rules/transformation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{
expression::{relational::logical::LogicalExpr, Expr},
memo::Memo,
plan::partial_logical_plan::PartialLogicalPlan,
};

#[allow(dead_code)]
#[trait_variant::make(Send)]
pub trait TransformationRule {
/// Checks if the transformation rule matches the current expression and its children.
/// Returns a vector of partially materialized logical plans.
///
/// This returns a vector because the rule matching the input root expression could have matched
/// with multiple child expressions.
///
/// For example, let's say the input expression is `Filter(G1)`, and the group G1 has two
/// expressions `e1 = Join(Join(A, B), C)` and `e2 = Join(A, Join(B, C))`.
///
/// If the rule wants to match against `Filter(Join(?L, ?R))`, then this function will partially
/// materialize two expressions `Filter(e1)` and `Filter(e2)`. It is then up to the memo table
/// API to apply modifications to the partially materialized logical plans (for example, a
/// filter pushdown under a `Join`).
///
/// TODO: Ideally this should return a `Stream` instead of a fully materialized Vector.
async fn check_pattern(&self, expr: LogicalExpr, memo: &Memo) -> Vec<PartialLogicalPlan>;

/// Applys modifications to a partially materialized logical plan.
///
/// These changes can create new logical or scalar expressions. However, note that
/// transformation rules will _not_ create new physical expressions.
fn apply(&self, expr: PartialLogicalPlan) -> Vec<Expr>;
}

pub mod join_associativity;
pub mod join_commutativity;

0 comments on commit 7d405d1

Please sign in to comment.