|
| 1 | +use super::{ExpressionEvaluationError, ExpressionEvaluationResult}; |
| 2 | +use crate::base::{ |
| 3 | + database::{OwnedColumn, OwnedTable}, |
| 4 | + math::decimal::{try_into_to_scalar, Precision}, |
| 5 | + scalar::Scalar, |
| 6 | +}; |
| 7 | +use proof_of_sql_parser::{ |
| 8 | + intermediate_ast::{BinaryOperator, Expression, Literal, UnaryOperator}, |
| 9 | + Identifier, |
| 10 | +}; |
| 11 | + |
| 12 | +impl<S: Scalar> OwnedTable<S> { |
| 13 | + /// Evaluate an expression on the table. |
| 14 | + pub fn evaluate(&self, expr: &Expression) -> ExpressionEvaluationResult<OwnedColumn<S>> { |
| 15 | + match expr { |
| 16 | + Expression::Column(identifier) => self.evaluate_column(identifier), |
| 17 | + Expression::Literal(lit) => self.evaluate_literal(lit), |
| 18 | + Expression::Binary { op, left, right } => self.evaluate_binary_expr(*op, left, right), |
| 19 | + Expression::Unary { op, expr } => self.evaluate_unary_expr(*op, expr), |
| 20 | + _ => Err(ExpressionEvaluationError::Unsupported(format!( |
| 21 | + "Expression {:?} is not supported yet", |
| 22 | + expr |
| 23 | + ))), |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + fn evaluate_column( |
| 28 | + &self, |
| 29 | + identifier: &Identifier, |
| 30 | + ) -> ExpressionEvaluationResult<OwnedColumn<S>> { |
| 31 | + Ok(self |
| 32 | + .inner_table() |
| 33 | + .get(identifier) |
| 34 | + .ok_or(ExpressionEvaluationError::ColumnNotFound( |
| 35 | + identifier.to_string(), |
| 36 | + ))? |
| 37 | + .clone()) |
| 38 | + } |
| 39 | + |
| 40 | + fn evaluate_literal(&self, lit: &Literal) -> ExpressionEvaluationResult<OwnedColumn<S>> { |
| 41 | + let len = self.num_rows(); |
| 42 | + match lit { |
| 43 | + Literal::Boolean(b) => Ok(OwnedColumn::Boolean(vec![*b; len])), |
| 44 | + Literal::BigInt(i) => Ok(OwnedColumn::BigInt(vec![*i; len])), |
| 45 | + Literal::Int128(i) => Ok(OwnedColumn::Int128(vec![*i; len])), |
| 46 | + Literal::Decimal(d) => { |
| 47 | + let scale = d.scale(); |
| 48 | + let precision = Precision::new(d.precision())?; |
| 49 | + let scalar = try_into_to_scalar(d, precision, scale)?; |
| 50 | + Ok(OwnedColumn::Decimal75(precision, scale, vec![scalar; len])) |
| 51 | + } |
| 52 | + Literal::VarChar(s) => Ok(OwnedColumn::VarChar(vec![s.clone(); len])), |
| 53 | + Literal::Timestamp(its) => Ok(OwnedColumn::TimestampTZ( |
| 54 | + its.timeunit(), |
| 55 | + its.timezone(), |
| 56 | + vec![its.timestamp().timestamp(); len], |
| 57 | + )), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fn evaluate_unary_expr( |
| 62 | + &self, |
| 63 | + op: UnaryOperator, |
| 64 | + expr: &Expression, |
| 65 | + ) -> ExpressionEvaluationResult<OwnedColumn<S>> { |
| 66 | + let column = self.evaluate(expr)?; |
| 67 | + match op { |
| 68 | + UnaryOperator::Not => Ok(column.element_wise_not()?), |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fn evaluate_binary_expr( |
| 73 | + &self, |
| 74 | + op: BinaryOperator, |
| 75 | + left: &Expression, |
| 76 | + right: &Expression, |
| 77 | + ) -> ExpressionEvaluationResult<OwnedColumn<S>> { |
| 78 | + let left = self.evaluate(left)?; |
| 79 | + let right = self.evaluate(right)?; |
| 80 | + match op { |
| 81 | + BinaryOperator::And => Ok(left.element_wise_and(&right)?), |
| 82 | + BinaryOperator::Or => Ok(left.element_wise_or(&right)?), |
| 83 | + BinaryOperator::Equal => Ok(left.element_wise_eq(&right)?), |
| 84 | + BinaryOperator::GreaterThanOrEqual => Ok(left.element_wise_ge(&right)?), |
| 85 | + BinaryOperator::LessThanOrEqual => Ok(left.element_wise_le(&right)?), |
| 86 | + BinaryOperator::Add => Ok((left + right)?), |
| 87 | + BinaryOperator::Subtract => Ok((left - right)?), |
| 88 | + BinaryOperator::Multiply => Ok((left * right)?), |
| 89 | + BinaryOperator::Division => Ok((left / right)?), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments