Skip to main content

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("operator doesn't exist: {base:?} {operator} {pattern:?}")]
83    RegexOnNonString {
84        base: Value,
85        pattern: Value,
86        operator: String,
87    },
88
89    #[error("extract format not matched: {value:?} FROM {field:?})")]
90    ExtractFormatNotMatched { value: Value, field: DateTimeField },
91
92    #[error("big endian export not supported for {0} type")]
93    BigEndianExportNotSupported(String),
94
95    #[error("invalid json string: {0}")]
96    InvalidJsonString(String),
97
98    #[error("json object type is required")]
99    JsonObjectTypeRequired,
100
101    #[error("json array type is required")]
102    JsonArrayTypeRequired,
103
104    #[error("unreachable - failed to parse json number: {0}")]
105    UnreachableJsonNumberParseFailure(String),
106
107    #[error("selector requires MAP or LIST types")]
108    SelectorRequiresMapOrListTypes,
109
110    #[error("overflow occurred: {lhs:?} {operator} {rhs:?}")]
111    BinaryOperationOverflow {
112        lhs: Value,
113        rhs: Value,
114        operator: NumericBinaryOperator,
115    },
116
117    #[error("non numeric value in sqrt {0:?}")]
118    SqrtOnNonNumeric(Value),
119
120    #[error("non-string parameter in position: {} IN {}", String::from(.from), String::from(.sub))]
121    NonStringParameterInPosition { from: Value, sub: Value },
122
123    #[error("non-string parameter in find idx: {}, {}", String::from(.sub), String::from(.from))]
124    NonStringParameterInFindIdx { sub: Value, from: Value },
125
126    #[error("non positive offset in find idx: {0}")]
127    NonPositiveIntegerOffsetInFindIdx(String),
128
129    #[error("failed to convert Value to Expr")]
130    ValueToExprConversionFailure,
131}
132
133#[derive(Debug, PartialEq, Eq, Serialize, Display)]
134pub enum NumericBinaryOperator {
135    #[strum(to_string = "+")]
136    Add,
137    #[strum(to_string = "-")]
138    Subtract,
139    #[strum(to_string = "*")]
140    Multiply,
141    #[strum(to_string = "/")]
142    Divide,
143    #[strum(to_string = "%")]
144    Modulo,
145    #[strum(to_string = "&")]
146    BitwiseAnd,
147    #[strum(to_string = "<<")]
148    BitwiseShiftLeft,
149    #[strum(to_string = ">>")]
150    BitwiseShiftRight,
151}