Skip to content

Commit

Permalink
refactor!: replace operator in unary and binaryop-related errors with…
Browse files Browse the repository at this point in the history
… string

This makes it easier to migrate from a particular implementation of parser
and remove the need to pass in UnaryOp / BinaryOp
  • Loading branch information
iajoiner committed Nov 7, 2024
1 parent 883384b commit 5763f53
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::base::{database::ColumnType, math::decimal::DecimalError};
use alloc::string::String;
use core::result::Result;
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
use snafu::Snafu;

/// Errors from operations on columns.
Expand All @@ -19,8 +18,8 @@ pub enum ColumnOperationError {
/// Incorrect `ColumnType` in binary operations
#[snafu(display("{operator:?}(lhs: {left_type:?}, rhs: {right_type:?}) is not supported"))]
BinaryOperationInvalidColumnType {
/// `BinaryOperator` that caused the error
operator: BinaryOperator,
/// Binary operator that caused the error
operator: String,
/// `ColumnType` of left operand
left_type: ColumnType,
/// `ColumnType` of right operand
Expand All @@ -30,8 +29,8 @@ pub enum ColumnOperationError {
/// Incorrect `ColumnType` in unary operations
#[snafu(display("{operator:?}(operand: {operand_type:?}) is not supported"))]
UnaryOperationInvalidColumnType {
/// `UnaryOperator` that caused the error
operator: UnaryOperator,
/// Unary operator that caused the error
operator: String,
/// `ColumnType` of the operand
operand_type: ColumnType,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use proof_of_sql_parser::intermediate_ast::BinaryOperator;
pub fn try_add_subtract_column_types(
lhs: ColumnType,
rhs: ColumnType,
operator: BinaryOperator,
_operator: BinaryOperator,
) -> ColumnOperationResult<ColumnType> {
if !lhs.is_numeric() || !rhs.is_numeric() {
return Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator,
operator: "+/-".to_string(),
left_type: lhs,
right_type: rhs,
});
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn try_multiply_column_types(
) -> ColumnOperationResult<ColumnType> {
if !lhs.is_numeric() || !rhs.is_numeric() {
return Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Multiply,
operator: "*".to_string(),
left_type: lhs,
right_type: rhs,
});
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn try_divide_column_types(
|| rhs == ColumnType::Scalar
{
return Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Division,
operator: "/".to_string(),
left_type: lhs,
right_type: rhs,
});
Expand Down
21 changes: 10 additions & 11 deletions crates/proof-of-sql/src/base/database/owned_column_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use crate::base::{
scalar::Scalar,
};
use core::ops::{Add, Div, Mul, Sub};
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};

impl<S: Scalar> OwnedColumn<S> {
/// Element-wise NOT operation for a column
pub fn element_wise_not(&self) -> ColumnOperationResult<Self> {
match self {
Self::Boolean(values) => Ok(Self::Boolean(slice_not(values))),
_ => Err(ColumnOperationError::UnaryOperationInvalidColumnType {
operator: UnaryOperator::Not,
operator: "NOT".to_string(),
operand_type: self.column_type(),
}),
}
Expand All @@ -42,7 +41,7 @@ impl<S: Scalar> OwnedColumn<S> {
match (self, rhs) {
(Self::Boolean(lhs), Self::Boolean(rhs)) => Ok(Self::Boolean(slice_and(lhs, rhs))),
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::And,
operator: "AND".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand All @@ -60,7 +59,7 @@ impl<S: Scalar> OwnedColumn<S> {
match (self, rhs) {
(Self::Boolean(lhs), Self::Boolean(rhs)) => Ok(Self::Boolean(slice_or(lhs, rhs))),
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Or,
operator: "OR".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -242,7 +241,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement equality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Equal,
operator: "=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -423,7 +422,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement inequality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::LessThanOrEqual,
operator: "<=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -604,7 +603,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement inequality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::GreaterThanOrEqual,
operator: ">=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -798,7 +797,7 @@ impl<S: Scalar> Add for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Add,
operator: "+".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -996,7 +995,7 @@ impl<S: Scalar> Sub for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Subtract,
operator: "-".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -1194,7 +1193,7 @@ impl<S: Scalar> Mul for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Multiply,
operator: "*".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -1392,7 +1391,7 @@ impl<S: Scalar> Div for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Division,
operator: "/".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down

0 comments on commit 5763f53

Please sign in to comment.