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("literal: {literal} is incompatible with data type: {data_type:?}")]
15    IncompatibleLiteralForDataType {
16        data_type: DataType,
17        literal: String,
18    },
19
20    #[error("incompatible data type, data type: {data_type:#?}, value: {value:#?}")]
21    IncompatibleDataType { data_type: DataType, value: Value },
22
23    #[error("null value on not null field")]
24    NullValueOnNotNullField,
25
26    #[error("failed to parse number")]
27    FailedToParseNumber,
28
29    #[error("failed to convert Float to Decimal: {0}")]
30    FloatToDecimalConversionFailure(f64),
31
32    #[error("failed to parse date: {0}")]
33    FailedToParseDate(String),
34
35    #[error("failed to parse timestamp: {0}")]
36    FailedToParseTimestamp(String),
37
38    #[error("failed to parse time: {0}")]
39    FailedToParseTime(String),
40
41    #[error("failed to UUID: {0}")]
42    FailedToParseUUID(String),
43
44    #[error("failed to parse point: {0}")]
45    FailedToParsePoint(String),
46
47    #[error("failed to parse Decimal: {0}")]
48    FailedToParseDecimal(String),
49
50    #[error("failed to parse hex string: {0}")]
51    FailedToParseHexString(String),
52
53    #[error("failed to parse inet string: {0}")]
54    FailedToParseInetString(String),
55
56    #[error("non-numeric values {lhs:?} {operator} {rhs:?}")]
57    NonNumericMathOperation {
58        lhs: Value,
59        rhs: Value,
60        operator: NumericBinaryOperator,
61    },
62
63    #[error("the divisor should not be zero")]
64    DivisorShouldNotBeZero,
65
66    #[error("unary plus operation for non numeric value")]
67    UnaryPlusOnNonNumeric,
68
69    #[error("unary minus operation for non numeric value")]
70    UnaryMinusOnNonNumeric,
71
72    #[error("unary factorial operation for non numeric value")]
73    FactorialOnNonNumeric,
74
75    #[error("unary factorial operation for non integer value")]
76    FactorialOnNonInteger,
77
78    #[error("unary factorial operation for negative numeric value")]
79    FactorialOnNegativeNumeric,
80
81    #[error("unary factorial operation overflow")]
82    FactorialOverflow,
83
84    #[error("GCD or LCM calculation overflowed on trying to get the absolute value of {0}")]
85    GcdLcmOverflow(i64),
86
87    #[error("LCM calculation resulted in a value out of the i64 range")]
88    LcmResultOutOfRange,
89
90    #[error("unary bit_not operation for non numeric value")]
91    UnaryBitwiseNotOnNonNumeric,
92
93    #[error("unary bit_not operation for non integer value")]
94    UnaryBitwiseNotOnNonInteger,
95
96    #[error("unreachable failure on parsing number")]
97    UnreachableNumberParsing,
98
99    #[error("unimplemented cast: {value:?} as {data_type}")]
100    UnimplementedCast { value: Value, data_type: DataType },
101
102    #[error("failed to cast from hex string to bytea: {0}")]
103    CastFromHexToByteaFailed(String),
104
105    #[error("function CONCAT requires at least 1 argument")]
106    EmptyArgNotAllowedInConcat,
107
108    // Cast errors from literal to value
109    #[error("literal cast failed from text to integer: {0}")]
110    LiteralCastFromTextToIntegerFailed(String),
111
112    #[error("literal cast failed from text to Unsigned Int(8): {0}")]
113    LiteralCastFromTextToUnsignedInt8Failed(String),
114
115    #[error("literal cast failed from text to UINT16: {0}")]
116    LiteralCastFromTextToUint16Failed(String),
117
118    #[error("literal cast failed from text to UINT32: {0}")]
119    LiteralCastFromTextToUint32Failed(String),
120
121    #[error("literal cast failed from text to UINT64: {0}")]
122    LiteralCastFromTextToUint64Failed(String),
123
124    #[error("literal cast failed from text to UINT128: {0}")]
125    LiteralCastFromTextToUint128Failed(String),
126
127    #[error("literal cast failed from text to float: {0}")]
128    LiteralCastFromTextToFloatFailed(String),
129
130    #[error("literal cast failed from text to decimal: {0}")]
131    LiteralCastFromTextToDecimalFailed(String),
132
133    #[error("literal cast failed to boolean: {0}")]
134    LiteralCastToBooleanFailed(String),
135
136    #[error("literal cast failed to date: {0}")]
137    LiteralCastToDateFailed(String),
138
139    #[error("literal cast from {1} to {0} failed")]
140    LiteralCastToDataTypeFailed(DataType, String),
141
142    #[error("literal cast failed to Int(8): {0}")]
143    LiteralCastToInt8Failed(String),
144
145    #[error("literal cast failed to Unsigned Int(8): {0}")]
146    LiteralCastToUnsignedInt8Failed(String),
147
148    #[error("literal cast failed to UINT16: {0}")]
149    LiteralCastToUint16Failed(String),
150
151    #[error("literal cast failed to UNIT32: {0}")]
152    LiteralCastToUint32Failed(String),
153
154    #[error("literal cast failed to UNIT64: {0}")]
155    LiteralCastToUint64Failed(String),
156
157    #[error("literal cast failed to UNIT128: {0}")]
158    LiteralCastToUint128Failed(String),
159
160    #[error("literal cast failed to time: {0}")]
161    LiteralCastToTimeFailed(String),
162
163    #[error("literal cast failed to timestamp: {0}")]
164    LiteralCastToTimestampFailed(String),
165
166    #[error("unreachable literal cast from number to integer: {0}")]
167    UnreachableLiteralCastFromNumberToInteger(String),
168
169    #[error("unreachable literal cast from number to float: {0}")]
170    UnreachableLiteralCastFromNumberToFloat(String),
171
172    #[error("unimplemented literal cast: {literal} as {data_type:?}")]
173    UnimplementedLiteralCast {
174        data_type: DataType,
175        literal: String,
176    },
177
178    #[error("unreachable integer overflow: {0}")]
179    UnreachableIntegerOverflow(String),
180
181    #[error("operator doesn't exist: {base:?} {case} {pattern:?}", case = if *case_sensitive { "LIKE" } else { "ILIKE" })]
182    LikeOnNonString {
183        base: Value,
184        pattern: Value,
185        case_sensitive: bool,
186    },
187
188    #[error("extract format not matched: {value:?} FROM {field:?})")]
189    ExtractFormatNotMatched { value: Value, field: DateTimeField },
190
191    #[error("big endian export not supported for {0} type")]
192    BigEndianExportNotSupported(String),
193
194    #[error("invalid json string: {0}")]
195    InvalidJsonString(String),
196
197    #[error("json object type is required")]
198    JsonObjectTypeRequired,
199
200    #[error("json array type is required")]
201    JsonArrayTypeRequired,
202
203    #[error("unreachable - failed to parse json number: {0}")]
204    UnreachableJsonNumberParseFailure(String),
205
206    #[error("selector requires MAP or LIST types")]
207    SelectorRequiresMapOrListTypes,
208
209    #[error("overflow occurred: {lhs:?} {operator} {rhs:?}")]
210    BinaryOperationOverflow {
211        lhs: Value,
212        rhs: Value,
213        operator: NumericBinaryOperator,
214    },
215
216    #[error("non numeric value in sqrt {0:?}")]
217    SqrtOnNonNumeric(Value),
218
219    #[error("non-string parameter in position: {} IN {}", String::from(.from), String::from(.sub))]
220    NonStringParameterInPosition { from: Value, sub: Value },
221
222    #[error("non-string parameter in find idx: {}, {}", String::from(.sub), String::from(.from))]
223    NonStringParameterInFindIdx { sub: Value, from: Value },
224
225    #[error("non positive offset in find idx: {0}")]
226    NonPositiveIntegerOffsetInFindIdx(String),
227
228    #[error("failed to convert Value to Expr")]
229    ValueToExprConversionFailure,
230
231    #[error("failed to convert Value to u32: {0}")]
232    I64ToU32ConversionFailure(String),
233}
234
235#[derive(Debug, PartialEq, Eq, Serialize, Display)]
236pub enum NumericBinaryOperator {
237    #[strum(to_string = "+")]
238    Add,
239    #[strum(to_string = "-")]
240    Subtract,
241    #[strum(to_string = "*")]
242    Multiply,
243    #[strum(to_string = "/")]
244    Divide,
245    #[strum(to_string = "%")]
246    Modulo,
247    #[strum(to_string = "&")]
248    BitwiseAnd,
249    #[strum(to_string = "<<")]
250    BitwiseShiftLeft,
251    #[strum(to_string = ">>")]
252    BitwiseShiftRight,
253}