Skip to content

Commit

Permalink
remove evaluate_join from expr
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 31, 2024
1 parent ad9b2d8 commit 2f14c9d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 34 deletions.
4 changes: 2 additions & 2 deletions bustubx/src/planner/expr/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Alias {
pub expr: Box<Expr>,
}
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)
}
}
6 changes: 3 additions & 3 deletions bustubx/src/planner/expr/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub struct BinaryOp {
pub rarg: Box<Expr>,
}
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,
Expand Down
7 changes: 3 additions & 4 deletions bustubx/src/planner/expr/column_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
19 changes: 4 additions & 15 deletions bustubx/src/planner/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
3 changes: 1 addition & 2 deletions bustubx/src/planner/physical_plan/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion bustubx/src/planner/physical_plan/nested_loop_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/physical_plan/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
8 changes: 2 additions & 6 deletions bustubx/src/planner/physical_plan/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2f14c9d

Please sign in to comment.