gluesql_core/data/value/
error.rs

1use {
2    crate::{
3        ast::{DataType, DateTimeField},
4        data::Value,
5    },
6    serde::Serialize,
7    std::fmt::Debug,
8    strum_macros::Display,
9    thiserror::Error,
10};
11
12#[derive(Error, Serialize, Debug, PartialEq)]
13pub enum ValueError {
14    #[error("failed to convert value({value:?}) to data type({data_type})")]
15    ConvertFailed { value: Value, data_type: DataType },
16
17    #[error("incompatible data type, data type: {data_type:#?}, value: {value:#?}")]
18    IncompatibleDataType { data_type: DataType, value: Value },
19
20    #[error("null value on not null field")]
21    NullValueOnNotNullField,
22
23    #[error("failed to convert Float to Decimal: {0}")]
24    FloatToDecimalConversionFailure(f64),
25
26    #[error("failed to UUID: {0}")]
27    FailedToParseUUID(String),
28
29    #[error("failed to parse point: {0}")]
30    FailedToParsePoint(String),
31
32    #[error("non-numeric values {lhs:?} {operator} {rhs:?}")]
33    NonNumericMathOperation {
34        lhs: Value,
35        rhs: Value,
36        operator: NumericBinaryOperator,
37    },
38
39    #[error("the divisor should not be zero")]
40    DivisorShouldNotBeZero,
41
42    #[error("unary plus operation for non numeric value")]
43    UnaryPlusOnNonNumeric,
44
45    #[error("unary minus operation for non numeric value")]
46    UnaryMinusOnNonNumeric,
47
48    #[error("unary factorial operation for non numeric value")]
49    FactorialOnNonNumeric,
50
51    #[error("unary factorial operation for non integer value")]
52    FactorialOnNonInteger,
53
54    #[error("unary factorial operation for negative numeric value")]
55    FactorialOnNegativeNumeric,
56
57    #[error("unary factorial operation overflow")]
58    FactorialOverflow,
59
60    #[error("unary bit_not operation for non numeric value")]
61    UnaryBitwiseNotOnNonNumeric,
62
63    #[error("unary bit_not operation for non integer value")]
64    UnaryBitwiseNotOnNonInteger,
65
66    #[error("unimplemented cast: {value:?} as {data_type}")]
67    UnimplementedCast { value: Value, data_type: DataType },
68
69    #[error("failed to cast from hex string to bytea: {0}")]
70    CastFromHexToByteaFailed(String),
71
72    #[error("unreachable integer overflow: {0}")]
73    UnreachableIntegerOverflow(String),
74
75    #[error("operator doesn't exist: {base:?} {case} {pattern:?}", case = if *case_sensitive { "LIKE" } else { "ILIKE" })]
76    LikeOnNonString {
77        base: Value,
78        pattern: Value,
79        case_sensitive: bool,
80    },
81
82    #[error("extract format not matched: {value:?} FROM {field:?})")]
83    ExtractFormatNotMatched { value: Value, field: DateTimeField },
84
85    #[error("big endian export not supported for {0} type")]
86    BigEndianExportNotSupported(String),
87
88    #[error("invalid json string: {0}")]
89    InvalidJsonString(String),
90
91    #[error("json object type is required")]
92    JsonObjectTypeRequired,
93
94    #[error("json array type is required")]
95    JsonArrayTypeRequired,
96
97    #[error("unreachable - failed to parse json number: {0}")]
98    UnreachableJsonNumberParseFailure(String),
99
100    #[error("selector requires MAP or LIST types")]
101    SelectorRequiresMapOrListTypes,
102
103    #[error("overflow occurred: {lhs:?} {operator} {rhs:?}")]
104    BinaryOperationOverflow {
105        lhs: Value,
106        rhs: Value,
107        operator: NumericBinaryOperator,
108    },
109
110    #[error("non numeric value in sqrt {0:?}")]
111    SqrtOnNonNumeric(Value),
112
113    #[error("non-string parameter in position: {} IN {}", String::from(.from), String::from(.sub))]
114    NonStringParameterInPosition { from: Value, sub: Value },
115
116    #[error("non-string parameter in find idx: {}, {}", String::from(.sub), String::from(.from))]
117    NonStringParameterInFindIdx { sub: Value, from: Value },
118
119    #[error("non positive offset in find idx: {0}")]
120    NonPositiveIntegerOffsetInFindIdx(String),
121
122    #[error("failed to convert Value to Expr")]
123    ValueToExprConversionFailure,
124}
125
126#[derive(Debug, PartialEq, Eq, Serialize, Display)]
127pub enum NumericBinaryOperator {
128    #[strum(to_string = "+")]
129    Add,
130    #[strum(to_string = "-")]
131    Subtract,
132    #[strum(to_string = "*")]
133    Multiply,
134    #[strum(to_string = "/")]
135    Divide,
136    #[strum(to_string = "%")]
137    Modulo,
138    #[strum(to_string = "&")]
139    BitwiseAnd,
140    #[strum(to_string = "<<")]
141    BitwiseShiftLeft,
142    #[strum(to_string = ">>")]
143    BitwiseShiftRight,
144}