diff --git a/bustubx/src/planner/logical_plan_v2/create_index.rs b/bustubx/src/planner/logical_plan_v2/create_index.rs new file mode 100644 index 0000000..3a0be9f --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/create_index.rs @@ -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, +} diff --git a/bustubx/src/planner/logical_plan_v2/create_table.rs b/bustubx/src/planner/logical_plan_v2/create_table.rs index b7526d9..dfefd55 100644 --- a/bustubx/src/planner/logical_plan_v2/create_table.rs +++ b/bustubx/src/planner/logical_plan_v2/create_table.rs @@ -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, } diff --git a/bustubx/src/planner/logical_plan_v2/filter.rs b/bustubx/src/planner/logical_plan_v2/filter.rs new file mode 100644 index 0000000..f9d4977 --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/filter.rs @@ -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, +} diff --git a/bustubx/src/planner/logical_plan_v2/insert.rs b/bustubx/src/planner/logical_plan_v2/insert.rs new file mode 100644 index 0000000..4d391e5 --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/insert.rs @@ -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, +} diff --git a/bustubx/src/planner/logical_plan_v2/join.rs b/bustubx/src/planner/logical_plan_v2/join.rs new file mode 100644 index 0000000..31ee121 --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/join.rs @@ -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, + /// Right input + pub right: Arc, + pub join_type: JoinType, + pub condition: Option, +} diff --git a/bustubx/src/planner/logical_plan_v2/limit.rs b/bustubx/src/planner/logical_plan_v2/limit.rs new file mode 100644 index 0000000..c1f4e6d --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/limit.rs @@ -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, + pub offset: Option, + pub input: Arc, +} diff --git a/bustubx/src/planner/logical_plan_v2/mod.rs b/bustubx/src/planner/logical_plan_v2/mod.rs index a0f97ca..5ad33fd 100644 --- a/bustubx/src/planner/logical_plan_v2/mod.rs +++ b/bustubx/src/planner/logical_plan_v2/mod.rs @@ -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), } diff --git a/bustubx/src/planner/logical_plan_v2/project.rs b/bustubx/src/planner/logical_plan_v2/project.rs new file mode 100644 index 0000000..a7b6ab3 --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/project.rs @@ -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, + pub input: Arc, +} diff --git a/bustubx/src/planner/logical_plan_v2/sort.rs b/bustubx/src/planner/logical_plan_v2/sort.rs new file mode 100644 index 0000000..6c36dcf --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/sort.rs @@ -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, + pub input: Arc, + pub limit: Option, +} + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct OrderByExpr { + /// The expression to sort on + pub expr: Box, + /// The direction of the sort + pub asc: bool, + /// Whether to put Nulls before all other data values + pub nulls_first: bool, +} diff --git a/bustubx/src/planner/logical_plan_v2/table_scan.rs b/bustubx/src/planner/logical_plan_v2/table_scan.rs new file mode 100644 index 0000000..6892603 --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/table_scan.rs @@ -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, + // TODO project push down + pub filters: Vec, + pub limit: Option, +} diff --git a/bustubx/src/planner/logical_plan_v2/values.rs b/bustubx/src/planner/logical_plan_v2/values.rs new file mode 100644 index 0000000..d19facb --- /dev/null +++ b/bustubx/src/planner/logical_plan_v2/values.rs @@ -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>, +} diff --git a/bustubx/src/planner/logical_planner/logical_planner.rs b/bustubx/src/planner/logical_planner/logical_planner.rs index 0ba0de6..49d18d7 100644 --- a/bustubx/src/planner/logical_planner/logical_planner.rs +++ b/bustubx/src/planner/logical_planner/logical_planner.rs @@ -1,3 +1,4 @@ +use crate::BustubxResult; use sqlparser::ast::{JoinConstraint, JoinOperator, Statement, TableFactor, TableWithJoins}; use std::sync::Arc; @@ -5,6 +6,7 @@ use crate::catalog::{Catalog, DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME}; use 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::{ @@ -195,4 +197,16 @@ impl<'a> LogicalPlanner<'a> { _ => unimplemented!(), } } + + pub fn plan_order_by( + &self, + order_by: &sqlparser::ast::OrderByExpr, + ) -> BustubxResult { + 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), + }) + } }