Skip to main content

datatypes/
error.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16
17use common_error::ext::ErrorExt;
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use snafu::{Location, Snafu};
21
22#[derive(Snafu)]
23#[snafu(visibility(pub))]
24#[stack_trace_debug]
25pub enum Error {
26    #[snafu(display("Failed to serialize data"))]
27    Serialize {
28        #[snafu(source)]
29        error: serde_json::Error,
30        #[snafu(implicit)]
31        location: Location,
32    },
33
34    #[snafu(display("Failed to deserialize data, json: {}", json))]
35    Deserialize {
36        #[snafu(source)]
37        error: serde_json::Error,
38        #[snafu(implicit)]
39        location: Location,
40        json: String,
41    },
42
43    #[snafu(display("Failed to convert datafusion type: {}", from))]
44    Conversion {
45        from: String,
46        #[snafu(implicit)]
47        location: Location,
48    },
49
50    #[snafu(display("Bad array access, Index out of bounds: {}, size: {}", index, size))]
51    BadArrayAccess {
52        index: usize,
53        size: usize,
54        #[snafu(implicit)]
55        location: Location,
56    },
57
58    #[snafu(display("Unknown vector, {}", msg))]
59    UnknownVector {
60        msg: String,
61        #[snafu(implicit)]
62        location: Location,
63    },
64
65    #[snafu(display("Unsupported arrow data type, type: {:?}", arrow_type))]
66    UnsupportedArrowType {
67        arrow_type: arrow::datatypes::DataType,
68        #[snafu(implicit)]
69        location: Location,
70    },
71
72    #[snafu(display("Unsupported operation: {} for vector: {}", op, vector_type))]
73    UnsupportedOperation {
74        op: String,
75        vector_type: String,
76        #[snafu(implicit)]
77        location: Location,
78    },
79
80    #[snafu(display("Failed to parse version in schema meta, value: {}", value))]
81    ParseSchemaVersion {
82        value: String,
83        #[snafu(source)]
84        error: std::num::ParseIntError,
85        #[snafu(implicit)]
86        location: Location,
87    },
88
89    #[snafu(display("Invalid timestamp index: {}", index))]
90    InvalidTimestampIndex {
91        index: usize,
92        #[snafu(implicit)]
93        location: Location,
94    },
95
96    #[snafu(display("{}", msg))]
97    CastType {
98        msg: String,
99        #[snafu(implicit)]
100        location: Location,
101    },
102
103    #[snafu(display("Failed to cast arrow time i32 type into i64"))]
104    CastTimeType {
105        #[snafu(source)]
106        error: std::num::TryFromIntError,
107        #[snafu(implicit)]
108        location: Location,
109    },
110
111    #[snafu(display("Arrow failed to compute"))]
112    ArrowCompute {
113        #[snafu(source)]
114        error: arrow::error::ArrowError,
115        #[snafu(implicit)]
116        location: Location,
117    },
118
119    #[snafu(display("Failed to project arrow schema"))]
120    ProjectArrowSchema {
121        #[snafu(source)]
122        error: arrow::error::ArrowError,
123        #[snafu(implicit)]
124        location: Location,
125    },
126
127    #[snafu(display("Unsupported column default constraint expression: {}", expr))]
128    UnsupportedDefaultExpr {
129        expr: String,
130        #[snafu(implicit)]
131        location: Location,
132    },
133
134    #[snafu(display("Default value should not be null for non null column"))]
135    NullDefault {
136        #[snafu(implicit)]
137        location: Location,
138    },
139
140    #[snafu(display("Incompatible default value type, reason: {}", reason))]
141    DefaultValueType {
142        reason: String,
143        #[snafu(implicit)]
144        location: Location,
145    },
146
147    #[snafu(display("Duplicated metadata for {}", key))]
148    DuplicateMeta {
149        key: String,
150        #[snafu(implicit)]
151        location: Location,
152    },
153
154    #[snafu(display("Failed to convert value into scalar value, reason: {}", reason))]
155    ToScalarValue {
156        reason: String,
157        #[snafu(implicit)]
158        location: Location,
159    },
160
161    #[snafu(display("Invalid timestamp precision: {}", precision))]
162    InvalidTimestampPrecision {
163        precision: u64,
164        #[snafu(implicit)]
165        location: Location,
166    },
167
168    #[snafu(display("Column {} already exists", column))]
169    DuplicateColumn {
170        column: String,
171        #[snafu(implicit)]
172        location: Location,
173    },
174
175    #[snafu(display("Failed to unpack value to given type: {}", reason))]
176    TryFromValue {
177        reason: String,
178        #[snafu(implicit)]
179        location: Location,
180    },
181
182    #[snafu(display("Failed to specify the precision {} and scale {}", precision, scale))]
183    InvalidPrecisionOrScale {
184        precision: u8,
185        scale: i8,
186        #[snafu(source)]
187        error: arrow::error::ArrowError,
188        #[snafu(implicit)]
189        location: Location,
190    },
191
192    #[snafu(display("Invalid JSON: {}", value))]
193    InvalidJson {
194        value: String,
195        #[snafu(implicit)]
196        location: Location,
197    },
198
199    #[snafu(display("Invalid Vector: {}", msg))]
200    InvalidVector {
201        msg: String,
202        #[snafu(implicit)]
203        location: Location,
204    },
205
206    #[snafu(display("Value exceeds the precision {} bound", precision))]
207    ValueExceedsPrecision {
208        precision: u8,
209        #[snafu(source)]
210        error: arrow::error::ArrowError,
211        #[snafu(implicit)]
212        location: Location,
213    },
214
215    #[snafu(display("Failed to convert Arrow array to scalars"))]
216    ConvertArrowArrayToScalars {
217        #[snafu(source)]
218        error: datafusion_common::DataFusionError,
219        #[snafu(implicit)]
220        location: Location,
221    },
222
223    #[snafu(display("Failed to convert scalar value to Arrow array"))]
224    ConvertScalarToArrowArray {
225        #[snafu(source)]
226        error: datafusion_common::DataFusionError,
227        #[snafu(implicit)]
228        location: Location,
229    },
230
231    #[snafu(display("Failed to parse extended type in metadata: {}", value))]
232    ParseExtendedType {
233        value: String,
234        #[snafu(implicit)]
235        location: Location,
236    },
237
238    #[snafu(display("Invalid fulltext option: {}", msg))]
239    InvalidFulltextOption {
240        msg: String,
241        #[snafu(implicit)]
242        location: Location,
243    },
244
245    #[snafu(display("Invalid skipping index option: {}", msg))]
246    InvalidSkippingIndexOption {
247        msg: String,
248        #[snafu(implicit)]
249        location: Location,
250    },
251
252    #[snafu(display("Inconsistent struct field count {field_len} and item count {item_len}"))]
253    InconsistentStructFieldsAndItems {
254        field_len: usize,
255        item_len: usize,
256        #[snafu(implicit)]
257        location: Location,
258    },
259
260    #[snafu(display("Failed to process JSONB value"))]
261    InvalidJsonb {
262        error: jsonb::Error,
263        #[snafu(implicit)]
264        location: Location,
265    },
266
267    #[snafu(display("Failed to parse or serialize arrow metadata"))]
268    ArrowMetadata {
269        #[snafu(source)]
270        error: arrow::error::ArrowError,
271        #[snafu(implicit)]
272        location: Location,
273    },
274
275    #[snafu(display("Failed to align JSON value, reason: {}", reason))]
276    AlignJsonValue {
277        reason: String,
278        #[snafu(implicit)]
279        location: Location,
280    },
281
282    #[snafu(display("Failed to align JSON array, reason: {reason}"))]
283    AlignJsonArray {
284        reason: String,
285        #[snafu(implicit)]
286        location: Location,
287    },
288
289    #[snafu(display("Non-object json is not supported currently"))]
290    UnsupportedJsonType {
291        #[snafu(implicit)]
292        location: Location,
293    },
294
295    #[snafu(display("unexpected: {reason}"))]
296    Unexpected {
297        reason: String,
298        #[snafu(implicit)]
299        location: Location,
300    },
301}
302
303impl ErrorExt for Error {
304    fn status_code(&self) -> StatusCode {
305        use Error::*;
306        match self {
307            UnsupportedOperation { .. }
308            | UnsupportedArrowType { .. }
309            | UnsupportedJsonType { .. }
310            | UnsupportedDefaultExpr { .. } => StatusCode::Unsupported,
311
312            DuplicateColumn { .. }
313            | BadArrayAccess { .. }
314            | NullDefault { .. }
315            | InvalidTimestampIndex { .. }
316            | DefaultValueType { .. }
317            | DuplicateMeta { .. }
318            | InvalidTimestampPrecision { .. }
319            | InvalidPrecisionOrScale { .. }
320            | InvalidJson { .. }
321            | InvalidJsonb { .. }
322            | InvalidVector { .. }
323            | InvalidFulltextOption { .. }
324            | InvalidSkippingIndexOption { .. } => StatusCode::InvalidArguments,
325
326            ValueExceedsPrecision { .. }
327            | CastType { .. }
328            | CastTimeType { .. }
329            | Conversion { .. } => StatusCode::IllegalState,
330
331            Serialize { .. }
332            | Deserialize { .. }
333            | UnknownVector { .. }
334            | ParseSchemaVersion { .. }
335            | ArrowCompute { .. }
336            | ProjectArrowSchema { .. }
337            | ToScalarValue { .. }
338            | TryFromValue { .. }
339            | ConvertArrowArrayToScalars { .. }
340            | ConvertScalarToArrowArray { .. }
341            | ParseExtendedType { .. }
342            | InconsistentStructFieldsAndItems { .. }
343            | ArrowMetadata { .. }
344            | AlignJsonValue { .. }
345            | AlignJsonArray { .. } => StatusCode::Internal,
346
347            Unexpected { .. } => StatusCode::Unexpected,
348        }
349    }
350
351    fn as_any(&self) -> &dyn Any {
352        self
353    }
354}
355
356pub type Result<T> = std::result::Result<T, Error>;