diff --git a/bustubx/src/planner/expr/alias.rs b/bustubx/src/planner/expr/alias.rs index a090fad..b93ce1f 100644 --- a/bustubx/src/planner/expr/alias.rs +++ b/bustubx/src/planner/expr/alias.rs @@ -9,7 +9,7 @@ pub struct Alias { pub expr: Box, } impl Alias { - pub fn evaluate(&self, tuple: Option<&Tuple>, schema: Option<&Schema>) -> ScalarValue { - self.expr.evaluate(tuple, schema) + pub fn evaluate(&self, tuple: Option<&Tuple>) -> ScalarValue { + self.expr.evaluate(tuple) } } diff --git a/bustubx/src/planner/expr/binary_op.rs b/bustubx/src/planner/expr/binary_op.rs index 1368bc8..68c1224 100644 --- a/bustubx/src/planner/expr/binary_op.rs +++ b/bustubx/src/planner/expr/binary_op.rs @@ -45,9 +45,9 @@ pub struct BinaryOp { pub rarg: Box, } impl BinaryOp { - pub fn evaluate(&self, tuple: Option<&Tuple>, schema: Option<&Schema>) -> ScalarValue { - let l = self.larg.evaluate(tuple, schema); - let r = self.rarg.evaluate(tuple, schema); + pub fn evaluate(&self, tuple: Option<&Tuple>) -> ScalarValue { + let l = self.larg.evaluate(tuple); + let r = self.rarg.evaluate(tuple); match self.op { // BinaryOperator::Plus => l + r, // BinaryOperator::Minus => l - r, diff --git a/bustubx/src/planner/expr/column_ref.rs b/bustubx/src/planner/expr/column_ref.rs index 8e59591..0ee1a3e 100644 --- a/bustubx/src/planner/expr/column_ref.rs +++ b/bustubx/src/planner/expr/column_ref.rs @@ -8,12 +8,11 @@ pub struct ColumnRef { pub col_name: String, } impl ColumnRef { - pub fn evaluate(&self, tuple: Option<&Tuple>, schema: Option<&Schema>) -> ScalarValue { - if tuple.is_none() || schema.is_none() { + pub fn evaluate(&self, tuple: Option<&Tuple>) -> ScalarValue { + if tuple.is_none() { panic!("tuple or schema is none") } let tuple = tuple.unwrap(); - let schema = schema.unwrap(); - tuple.get_value_by_col_name(schema, &self.col_name) + tuple.get_value_by_col_name(&tuple.schema, &self.col_name) } } diff --git a/bustubx/src/planner/expr/mod.rs b/bustubx/src/planner/expr/mod.rs index 603e456..5537bad 100644 --- a/bustubx/src/planner/expr/mod.rs +++ b/bustubx/src/planner/expr/mod.rs @@ -15,24 +15,13 @@ pub enum Expr { BinaryOp(BinaryOp), } impl Expr { - pub fn evaluate(&self, tuple: Option<&Tuple>, schema: Option<&Schema>) -> ScalarValue { + pub fn evaluate(&self, tuple: Option<&Tuple>) -> ScalarValue { match self { Expr::Constant(c) => c.evaluate(), - Expr::ColumnRef(c) => c.evaluate(tuple, schema), - Expr::BinaryOp(b) => b.evaluate(tuple, schema), - Expr::Alias(a) => a.evaluate(tuple, schema), + Expr::ColumnRef(c) => c.evaluate(tuple), + Expr::BinaryOp(b) => b.evaluate(tuple), + Expr::Alias(a) => a.evaluate(tuple), _ => unimplemented!(), } } - - pub fn evaluate_join(&self, left_tuple: &Tuple, right_tuple: &Tuple) -> ScalarValue { - // combine left and right tuple, left and right schema - let tuple = Tuple::try_merge(vec![left_tuple.clone(), right_tuple.clone()]).unwrap(); - let schema = Schema::try_merge(vec![ - left_tuple.schema.as_ref().clone(), - right_tuple.schema.as_ref().clone(), - ]) - .unwrap(); - self.evaluate(Some(&tuple), Some(&schema)) - } } diff --git a/bustubx/src/planner/physical_plan/filter.rs b/bustubx/src/planner/physical_plan/filter.rs index 1ffe3fb..08e4c34 100644 --- a/bustubx/src/planner/physical_plan/filter.rs +++ b/bustubx/src/planner/physical_plan/filter.rs @@ -29,8 +29,7 @@ impl VolcanoExecutor for PhysicalFilter { return None; } let tuple = next_tuple.unwrap(); - let output_schema = self.input.output_schema(); - let compare_res = self.predicate.evaluate(Some(&tuple), Some(&output_schema)); + let compare_res = self.predicate.evaluate(Some(&tuple)); if let ScalarValue::Boolean(Some(v)) = compare_res { if v { return Some(tuple); diff --git a/bustubx/src/planner/physical_plan/nested_loop_join.rs b/bustubx/src/planner/physical_plan/nested_loop_join.rs index 8788073..8639a4e 100644 --- a/bustubx/src/planner/physical_plan/nested_loop_join.rs +++ b/bustubx/src/planner/physical_plan/nested_loop_join.rs @@ -68,7 +68,9 @@ impl VolcanoExecutor for PhysicalNestedLoopJoin { return Some(Tuple::try_merge(vec![left_tuple, right_tuple]).unwrap()); } else { let condition = self.condition.clone().unwrap(); - let evaluate_res = condition.evaluate_join(&left_tuple, &right_tuple); + let merged_tuple = + Tuple::try_merge(vec![left_tuple.clone(), right_tuple.clone()]).unwrap(); + let evaluate_res = condition.evaluate(Some(&merged_tuple)); // TODO support left/right join after null support added if let ScalarValue::Boolean(Some(v)) = evaluate_res { if v { diff --git a/bustubx/src/planner/physical_plan/project.rs b/bustubx/src/planner/physical_plan/project.rs index f666927..c06d2a1 100644 --- a/bustubx/src/planner/physical_plan/project.rs +++ b/bustubx/src/planner/physical_plan/project.rs @@ -29,7 +29,7 @@ impl VolcanoExecutor for PhysicalProject { } let mut new_values = Vec::new(); for expr in &self.expressions { - new_values.push(expr.evaluate(next_tuple.as_ref(), Some(&self.input.output_schema()))); + new_values.push(expr.evaluate(next_tuple.as_ref())); } return Some(Tuple::new(self.output_schema(), new_values)); } diff --git a/bustubx/src/planner/physical_plan/sort.rs b/bustubx/src/planner/physical_plan/sort.rs index f19f017..cd4f8f1 100644 --- a/bustubx/src/planner/physical_plan/sort.rs +++ b/bustubx/src/planner/physical_plan/sort.rs @@ -46,12 +46,8 @@ impl VolcanoExecutor for PhysicalSort { let mut ordering = std::cmp::Ordering::Equal; let mut index = 0; while ordering == std::cmp::Ordering::Equal && index < self.order_bys.len() { - let a_value = self.order_bys[index] - .expression - .evaluate(Some(a), Some(&self.input.output_schema())); - let b_value = self.order_bys[index] - .expression - .evaluate(Some(b), Some(&self.input.output_schema())); + let a_value = self.order_bys[index].expression.evaluate(Some(a)); + let b_value = self.order_bys[index].expression.evaluate(Some(b)); ordering = if self.order_bys[index].desc { b_value.compare(&a_value) } else {