1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::base::{database::ColumnType, math::decimal::DecimalError};
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
use thiserror::Error;

/// Errors from operations on columns.
#[derive(Error, Debug, PartialEq, Eq)]
pub enum ColumnOperationError {
    /// Two columns do not have the same length
    #[error("Columns have different lengths: {0} != {1}")]
    DifferentColumnLength(usize, usize),

    /// Incorrect `ColumnType` in binary operations
    #[error("{operator:?}(lhs: {left_type:?}, rhs: {right_type:?}) is not supported")]
    BinaryOperationInvalidColumnType {
        /// `BinaryOperator` that caused the error
        operator: BinaryOperator,
        /// `ColumnType` of left operand
        left_type: ColumnType,
        /// `ColumnType` of right operand
        right_type: ColumnType,
    },

    /// Incorrect `ColumnType` in unary operations
    #[error("{operator:?}(operand: {operand_type:?}) is not supported")]
    UnaryOperationInvalidColumnType {
        /// `UnaryOperator` that caused the error
        operator: UnaryOperator,
        /// `ColumnType` of the operand
        operand_type: ColumnType,
    },

    /// Overflow in integer operations
    #[error("Overflow in integer operation: {0}")]
    IntegerOverflow(String),

    /// Division by zero
    #[error("Division by zero")]
    DivisionByZero,

    /// Errors related to decimal operations
    #[error(transparent)]
    DecimalConversionError(#[from] DecimalError),
}

/// Result type for column operations
pub type ColumnOperationResult<T> = std::result::Result<T, ColumnOperationError>;