proof_of_sql/base/database/
column_operation_error.rs

1use crate::base::{database::ColumnType, math::decimal::DecimalError};
2use alloc::string::String;
3use core::result::Result;
4use snafu::Snafu;
5
6/// Errors from operations on columns.
7#[derive(Snafu, Debug, PartialEq, Eq)]
8pub enum ColumnOperationError {
9    /// Two columns do not have the same length
10    #[snafu(display("Columns have different lengths: {len_a} != {len_b}"))]
11    DifferentColumnLength {
12        /// The length of the first column
13        len_a: usize,
14        /// The length of the second column
15        len_b: usize,
16    },
17
18    /// Incorrect `ColumnType` in binary operations
19    #[snafu(display("{operator:?}(lhs: {left_type:?}, rhs: {right_type:?}) is not supported"))]
20    BinaryOperationInvalidColumnType {
21        /// Binary operator that caused the error
22        operator: String,
23        /// `ColumnType` of left operand
24        left_type: ColumnType,
25        /// `ColumnType` of right operand
26        right_type: ColumnType,
27    },
28
29    /// Incorrect `ColumnType` in unary operations
30    #[snafu(display("{operator:?}(operand: {operand_type:?}) is not supported"))]
31    UnaryOperationInvalidColumnType {
32        /// Unary operator that caused the error
33        operator: String,
34        /// `ColumnType` of the operand
35        operand_type: ColumnType,
36    },
37
38    /// Overflow in integer operations
39    #[snafu(display("Overflow in integer operation: {error}"))]
40    IntegerOverflow {
41        /// The underlying overflow error
42        error: String,
43    },
44
45    /// Division by zero
46    #[snafu(display("Division by zero"))]
47    DivisionByZero,
48
49    /// Errors related to decimal operations
50    #[snafu(transparent)]
51    DecimalConversionError {
52        /// The underlying source error
53        source: DecimalError,
54    },
55
56    /// Errors related to unioning columns of different types
57    #[snafu(display(
58        "Cannot union columns of different types: {correct_type:?} and {actual_type:?}"
59    ))]
60    UnionDifferentTypes {
61        /// The correct data type
62        correct_type: ColumnType,
63        /// The type of the column that caused the error
64        actual_type: ColumnType,
65    },
66
67    /// Errors related to index out of bounds
68    #[snafu(display("Index out of bounds: {index} >= {len}"))]
69    IndexOutOfBounds {
70        /// The index that caused the error
71        index: usize,
72        /// The length of the column
73        len: usize,
74    },
75
76    /// Errors related to casting between signed and unsigned types. This error can be
77    /// used as a signal that a casting operation is currently unsupported.
78    /// For example, an i8 can fit inside of a u8 iff it is greater than zero. The library
79    /// needs to have a way to check *and* prove that this condition is true. If the library
80    /// does not have a proving mechanism in place for this check, then this error is
81    /// then used to indicate that the operation is not supported.
82    #[snafu(display("Cannot fit {left_type} into {right_type} without losing data"))]
83    SignedCastingError {
84        /// `ColumnType` of left operand
85        left_type: ColumnType,
86        /// `ColumnType` of right operand
87        right_type: ColumnType,
88    },
89
90    /// Errors related to casting between two types.
91    #[snafu(display("Cannot fit {left_type} into {right_type} without losing data"))]
92    CastingError {
93        /// `ColumnType` of left operand
94        left_type: ColumnType,
95        /// `ColumnType` of right operand
96        right_type: ColumnType,
97    },
98
99    /// Errors related to casting with scaling between two types.
100    #[snafu(display("Cannot fit {left_type} into {right_type} without losing data"))]
101    ScaleCastingError {
102        /// `ColumnType` of left operand
103        left_type: ColumnType,
104        /// `ColumnType` of right operand
105        right_type: ColumnType,
106    },
107}
108
109/// Result type for column operations
110pub type ColumnOperationResult<T> = Result<T, ColumnOperationError>;