diff --git a/bustubx/src/catalog/column.rs b/bustubx/src/catalog/column.rs index 1e56019..df20ff0 100644 --- a/bustubx/src/catalog/column.rs +++ b/bustubx/src/catalog/column.rs @@ -28,10 +28,4 @@ impl Column { nullable: false, } } - - pub fn from_sqlparser_column(column_def: &ColumnDef) -> Self { - let column_name = column_def.name.to_string(); - let column_type: DataType = (&column_def.data_type).try_into().unwrap(); - Self::new(column_name, column_type) - } } diff --git a/bustubx/src/planner/logical_planner/plan_expr.rs b/bustubx/src/planner/logical_planner/bind_expr.rs similarity index 89% rename from bustubx/src/planner/logical_planner/plan_expr.rs rename to bustubx/src/planner/logical_planner/bind_expr.rs index 97851a2..8e4151d 100644 --- a/bustubx/src/planner/logical_planner/plan_expr.rs +++ b/bustubx/src/planner/logical_planner/bind_expr.rs @@ -4,22 +4,22 @@ use crate::planner::LogicalPlanner; use crate::{BustubxError, BustubxResult}; impl LogicalPlanner<'_> { - pub fn plan_expr(&self, sql: &sqlparser::ast::Expr) -> BustubxResult { + pub fn bind_expr(&self, sql: &sqlparser::ast::Expr) -> BustubxResult { match sql { sqlparser::ast::Expr::Identifier(ident) => Ok(Expr::Column(ColumnExpr { relation: None, name: ident.value.clone(), })), sqlparser::ast::Expr::BinaryOp { left, op, right } => { - let left = Box::new(self.plan_expr(left)?); - let right = Box::new(self.plan_expr(right)?); + let left = Box::new(self.bind_expr(left)?); + let right = Box::new(self.bind_expr(right)?); Ok(Expr::BinaryExpr(BinaryExpr { left, op: op.try_into()?, right, })) } - sqlparser::ast::Expr::Value(value) => self.plan_value(value), + sqlparser::ast::Expr::Value(value) => self.bind_value(value), sqlparser::ast::Expr::CompoundIdentifier(idents) => match idents.as_slice() { [col] => Ok(Expr::Column(ColumnExpr { relation: None, @@ -56,7 +56,7 @@ impl LogicalPlanner<'_> { } } - pub fn plan_value(&self, value: &sqlparser::ast::Value) -> BustubxResult { + pub fn bind_value(&self, value: &sqlparser::ast::Value) -> BustubxResult { match value { sqlparser::ast::Value::Number(s, _) => { let num: i64 = s.parse::().map_err(|e| { diff --git a/bustubx/src/planner/logical_planner/logical_planner.rs b/bustubx/src/planner/logical_planner/logical_planner.rs index 3b276ef..e238f7d 100644 --- a/bustubx/src/planner/logical_planner/logical_planner.rs +++ b/bustubx/src/planner/logical_planner/logical_planner.rs @@ -34,11 +34,11 @@ impl<'a> LogicalPlanner<'a> { } } - pub fn plan_order_by_expr( + pub fn bind_order_by_expr( &self, order_by: &sqlparser::ast::OrderByExpr, ) -> BustubxResult { - let expr = self.plan_expr(&order_by.expr)?; + let expr = self.bind_expr(&order_by.expr)?; Ok(OrderByExpr { expr: Box::new(expr), asc: order_by.asc.unwrap_or(true), @@ -46,7 +46,7 @@ impl<'a> LogicalPlanner<'a> { }) } - pub fn plan_table_name( + pub fn bind_table_name( &self, table_name: &sqlparser::ast::ObjectName, ) -> BustubxResult { diff --git a/bustubx/src/planner/logical_planner/mod.rs b/bustubx/src/planner/logical_planner/mod.rs index f78bfee..da1ac41 100644 --- a/bustubx/src/planner/logical_planner/mod.rs +++ b/bustubx/src/planner/logical_planner/mod.rs @@ -1,7 +1,7 @@ +mod bind_expr; mod logical_planner; mod plan_create_index; mod plan_create_table; -mod plan_expr; mod plan_insert; mod plan_query; mod plan_set_expr; diff --git a/bustubx/src/planner/logical_planner/plan_create_index.rs b/bustubx/src/planner/logical_planner/plan_create_index.rs index ee15eb9..32d2b37 100644 --- a/bustubx/src/planner/logical_planner/plan_create_index.rs +++ b/bustubx/src/planner/logical_planner/plan_create_index.rs @@ -16,10 +16,10 @@ impl<'a> LogicalPlanner<'a> { .map_or(Err(BustubxError::Plan("".to_string())), |ident| { Ok(ident.value.clone()) })?; - let table = self.plan_table_name(table_name)?; + let table = self.bind_table_name(table_name)?; let mut columns_expr = vec![]; for col in columns.iter() { - let col_expr = self.plan_order_by_expr(&col)?; + let col_expr = self.bind_order_by_expr(&col)?; columns_expr.push(col_expr); } let table_schema = self diff --git a/bustubx/src/planner/logical_planner/plan_create_table.rs b/bustubx/src/planner/logical_planner/plan_create_table.rs index 6889415..07398be 100644 --- a/bustubx/src/planner/logical_planner/plan_create_table.rs +++ b/bustubx/src/planner/logical_planner/plan_create_table.rs @@ -11,7 +11,7 @@ impl<'a> LogicalPlanner<'a> { name: &sqlparser::ast::ObjectName, column_defs: &Vec, ) -> BustubxResult { - let name = self.plan_table_name(name)?; + let name = self.bind_table_name(name)?; let mut columns = vec![]; for col_def in column_defs { columns.push(Column::new( diff --git a/bustubx/src/planner/logical_planner/plan_insert.rs b/bustubx/src/planner/logical_planner/plan_insert.rs index fb493c6..cd4984f 100644 --- a/bustubx/src/planner/logical_planner/plan_insert.rs +++ b/bustubx/src/planner/logical_planner/plan_insert.rs @@ -13,7 +13,7 @@ impl<'a> LogicalPlanner<'a> { source: &sqlparser::ast::Query, ) -> BustubxResult { let values = self.plan_set_expr(source.body.as_ref())?; - let table = self.plan_table_name(table_name)?; + let table = self.bind_table_name(table_name)?; let table_schema = self .context .catalog diff --git a/bustubx/src/planner/logical_planner/plan_query.rs b/bustubx/src/planner/logical_planner/plan_query.rs index 50564f2..3835d76 100644 --- a/bustubx/src/planner/logical_planner/plan_query.rs +++ b/bustubx/src/planner/logical_planner/plan_query.rs @@ -25,7 +25,7 @@ impl<'a> LogicalPlanner<'a> { let mut order_by_exprs = vec![]; for order in order_by { - order_by_exprs.push(self.plan_order_by_expr(order)?); + order_by_exprs.push(self.bind_order_by_expr(order)?); } Ok(LogicalPlan::Sort(Sort { @@ -48,7 +48,7 @@ impl<'a> LogicalPlanner<'a> { let limit = match limit { None => None, Some(limit_expr) => { - let n = match self.plan_expr(&limit_expr)? { + let n = match self.bind_expr(&limit_expr)? { Expr::Literal(lit) => match lit.value { ScalarValue::Int64(Some(v)) if v >= 0 => Ok(v as usize), _ => Err(BustubxError::Plan(format!( @@ -67,7 +67,7 @@ impl<'a> LogicalPlanner<'a> { let offset = match offset { None => 0, - Some(offset_expr) => match self.plan_expr(&offset_expr.value)? { + Some(offset_expr) => match self.bind_expr(&offset_expr.value)? { Expr::Literal(lit) => match lit.value { ScalarValue::Int64(Some(v)) => { if v < 0 { diff --git a/bustubx/src/planner/logical_planner/plan_set_expr.rs b/bustubx/src/planner/logical_planner/plan_set_expr.rs index a1b0e20..48b0de8 100644 --- a/bustubx/src/planner/logical_planner/plan_set_expr.rs +++ b/bustubx/src/planner/logical_planner/plan_set_expr.rs @@ -35,11 +35,11 @@ impl LogicalPlanner<'_> { let mut exprs = vec![]; for select_item in project { match select_item { - sqlparser::ast::SelectItem::UnnamedExpr(expr) => exprs.push(self.plan_expr(expr)?), + sqlparser::ast::SelectItem::UnnamedExpr(expr) => exprs.push(self.bind_expr(expr)?), sqlparser::ast::SelectItem::ExprWithAlias { expr, alias } => { exprs.push(Expr::Alias(Alias { name: alias.value.clone(), - expr: Box::new(self.plan_expr(expr)?), + expr: Box::new(self.bind_expr(expr)?), })) } sqlparser::ast::SelectItem::Wildcard(_) => { @@ -80,7 +80,7 @@ impl LogicalPlanner<'_> { match selection { None => Ok(input), Some(predicate) => { - let predicate = self.plan_expr(predicate)?; + let predicate = self.bind_expr(predicate)?; Ok(LogicalPlan::Filter(Filter { input: Arc::new(input), predicate, @@ -162,7 +162,7 @@ impl LogicalPlanner<'_> { ) -> BustubxResult { match constraint { sqlparser::ast::JoinConstraint::On(expr) => { - let expr = self.plan_expr(expr)?; + let expr = self.bind_expr(expr)?; let schema = Arc::new(build_join_schema(left.schema(), right.schema(), join_type)?); Ok(LogicalPlan::Join(Join { left: Arc::new(left), @@ -205,7 +205,7 @@ impl LogicalPlanner<'_> { match relation { sqlparser::ast::TableFactor::Table { name, alias, .. } => { // TODO handle alias - let table_ref = self.plan_table_name(name)?; + let table_ref = self.bind_table_name(name)?; // TODO get schema by full table name let schema = self .context @@ -241,7 +241,7 @@ impl LogicalPlanner<'_> { for row in values.rows.iter() { let mut record = vec![]; for item in row { - record.push(self.plan_expr(item)?); + record.push(self.bind_expr(item)?); } result.push(record); }