Skip to content

Commit

Permalink
Add logical plan v2
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 31, 2024
1 parent f009dc7 commit 5c031d0
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 4 deletions.
9 changes: 9 additions & 0 deletions bustubx/src/planner/logical_plan_v2/create_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::catalog::SchemaRef;

#[derive(derive_new::new, Debug, Clone)]
pub struct CreateIndex {
pub index_name: String,
pub table_name: String,
pub table_schema: SchemaRef,
pub key_attrs: Vec<u32>,
}
4 changes: 2 additions & 2 deletions bustubx/src/planner/logical_plan_v2/create_table.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::catalog::Schema;
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;

#[derive(Debug, Clone)]
pub struct CreateTable {
pub name: TableReference,
pub schema: Schema,
pub schema: SchemaRef,
}
11 changes: 11 additions & 0 deletions bustubx/src/planner/logical_plan_v2/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::expression::Expr;
use crate::planner::logical_plan_v2::LogicalPlanV2;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Filter {
/// The predicate expression, which must have Boolean type.
pub predicate: Expr,
/// The incoming logical plan
pub input: Arc<LogicalPlanV2>,
}
11 changes: 11 additions & 0 deletions bustubx/src/planner/logical_plan_v2/insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::planner::logical_plan_v2::LogicalPlanV2;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Insert {
pub table: TableReference,
pub schema: SchemaRef,
pub input: Arc<LogicalPlanV2>,
}
14 changes: 14 additions & 0 deletions bustubx/src/planner/logical_plan_v2/join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::expression::Expr;
use crate::planner::logical_plan_v2::LogicalPlanV2;
use crate::planner::table_ref::join::JoinType;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Join {
/// Left input
pub left: Arc<LogicalPlanV2>,
/// Right input
pub right: Arc<LogicalPlanV2>,
pub join_type: JoinType,
pub condition: Option<Expr>,
}
9 changes: 9 additions & 0 deletions bustubx/src/planner/logical_plan_v2/limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::planner::logical_plan_v2::LogicalPlanV2;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Limit {
pub limit: Option<usize>,
pub offset: Option<usize>,
pub input: Arc<LogicalPlanV2>,
}
32 changes: 30 additions & 2 deletions bustubx/src/planner/logical_plan_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
use crate::planner::logical_plan_v2::create_table::CreateTable;

mod create_index;
mod create_table;
mod filter;
mod insert;
mod join;
mod limit;
mod project;
mod sort;
mod table_scan;
mod values;

pub use create_index::CreateIndex;
pub use create_table::CreateTable;
pub use filter::Filter;
pub use insert::Insert;
pub use join::Join;
pub use limit::Limit;
pub use project::Project;
pub use sort::{OrderByExpr, Sort};
pub use table_scan::TableScan;
pub use values::Values;

#[derive(Debug, Clone)]
pub enum LogicalPlanV2 {
CreateTable(CreateTable),
CreateIndex(CreateIndex),
Filter(Filter),
Insert(Insert),
Join(Join),
Limit(Limit),
Project(Project),
TableScan(TableScan),
Sort(Sort),
Values(Values),
}
9 changes: 9 additions & 0 deletions bustubx/src/planner/logical_plan_v2/project.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::expression::Expr;
use crate::planner::logical_plan_v2::LogicalPlanV2;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Project {
pub expressions: Vec<Expr>,
pub input: Arc<LogicalPlanV2>,
}
20 changes: 20 additions & 0 deletions bustubx/src/planner/logical_plan_v2/sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::expression::Expr;
use crate::planner::logical_plan_v2::LogicalPlanV2;
use std::sync::Arc;

#[derive(derive_new::new, Debug, Clone)]
pub struct Sort {
pub expr: Vec<OrderByExpr>,
pub input: Arc<LogicalPlanV2>,
pub limit: Option<usize>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OrderByExpr {
/// The expression to sort on
pub expr: Box<Expr>,
/// The direction of the sort
pub asc: bool,
/// Whether to put Nulls before all other data values
pub nulls_first: bool,
}
12 changes: 12 additions & 0 deletions bustubx/src/planner/logical_plan_v2/table_scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::catalog::ColumnRef;
use crate::common::table_ref::TableReference;
use crate::expression::Expr;

#[derive(derive_new::new, Debug, Clone)]
pub struct TableScan {
pub table_name: TableReference,
pub columns: Vec<ColumnRef>,
// TODO project push down
pub filters: Vec<Expr>,
pub limit: Option<usize>,
}
8 changes: 8 additions & 0 deletions bustubx/src/planner/logical_plan_v2/values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::catalog::SchemaRef;
use crate::expression::Expr;

#[derive(derive_new::new, Debug, Clone)]
pub struct Values {
pub schema: SchemaRef,
pub values: Vec<Vec<Expr>>,
}
14 changes: 14 additions & 0 deletions bustubx/src/planner/logical_planner/logical_planner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::BustubxResult;
use sqlparser::ast::{JoinConstraint, JoinOperator, Statement, TableFactor, TableWithJoins};
use std::sync::Arc;

use crate::catalog::{Catalog, DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
use crate::common::table_ref::TableReference;

Check warning on line 6 in bustubx/src/planner/logical_planner/logical_planner.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::common::table_ref::TableReference`
use crate::expression::Expr;
use crate::planner::logical_plan::LogicalPlan;
use crate::planner::logical_plan_v2::OrderByExpr;
use crate::planner::operator::LogicalOperator;

use crate::planner::table_ref::{
Expand Down Expand Up @@ -195,4 +197,16 @@ impl<'a> LogicalPlanner<'a> {
_ => unimplemented!(),
}
}

pub fn plan_order_by(
&self,
order_by: &sqlparser::ast::OrderByExpr,
) -> BustubxResult<OrderByExpr> {
let expr = self.plan_expr(&order_by.expr)?;
Ok(OrderByExpr {
expr: Box::new(expr),
asc: order_by.asc.unwrap_or(true),
nulls_first: order_by.nulls_first.unwrap_or(false),
})
}
}

0 comments on commit 5c031d0

Please sign in to comment.