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
22use crate::data_type::ConcreteDataType;
23
24/// Shared error message for rejecting a time index type change that is not a
25/// widening timestamp unit change; used by both the region and table layer.
26pub fn time_index_not_widening_error(
27    column_name: &str,
28    from_type: &ConcreteDataType,
29    to_type: &ConcreteDataType,
30) -> String {
31    format!(
32        "time index column '{column_name}' only supports widening its timestamp \
33         unit, cannot change type from '{from_type}' to '{to_type}'"
34    )
35}
36
37#[derive(Snafu)]
38#[snafu(visibility(pub))]
39#[stack_trace_debug]
40pub enum Error {
41    #[snafu(display("Failed to serialize data"))]
42    Serialize {
43        #[snafu(source)]
44        error: serde_json::Error,
45        #[snafu(implicit)]
46        location: Location,
47    },
48
49    #[snafu(display("Failed to deserialize data, json: {}", json))]
50    Deserialize {
51        #[snafu(source)]
52        error: serde_json::Error,
53        #[snafu(implicit)]
54        location: Location,
55        json: String,
56    },
57
58    #[snafu(display("Failed to convert datafusion type: {}", from))]
59    Conversion {
60        from: String,
61        #[snafu(implicit)]
62        location: Location,
63    },
64
65    #[snafu(display("Bad array access, Index out of bounds: {}, size: {}", index, size))]
66    BadArrayAccess {
67        index: usize,
68        size: usize,
69        #[snafu(implicit)]
70        location: Location,
71    },
72
73    #[snafu(display("Unknown vector, {}", msg))]
74    UnknownVector {
75        msg: String,
76        #[snafu(implicit)]
77        location: Location,
78    },
79
80    #[snafu(display("Unsupported arrow data type, type: {:?}", arrow_type))]
81    UnsupportedArrowType {
82        arrow_type: arrow::datatypes::DataType,
83        #[snafu(implicit)]
84        location: Location,
85    },
86
87    #[snafu(display("Unsupported operation: {} for vector: {}", op, vector_type))]
88    UnsupportedOperation {
89        op: String,
90        vector_type: String,
91        #[snafu(implicit)]
92        location: Location,
93    },
94
95    #[snafu(display("Unimplemented: {feat}"))]
96    Unimplemented {
97        feat: String,
98        #[snafu(implicit)]
99        location: Location,
100    },
101
102    #[snafu(display("Failed to parse version in schema meta, value: {}", value))]
103    ParseSchemaVersion {
104        value: String,
105        #[snafu(source)]
106        error: std::num::ParseIntError,
107        #[snafu(implicit)]
108        location: Location,
109    },
110
111    #[snafu(display("Invalid timestamp index: {}", index))]
112    InvalidTimestampIndex {
113        index: usize,
114        #[snafu(implicit)]
115        location: Location,
116    },
117
118    #[snafu(display("{}", msg))]
119    CastType {
120        msg: String,
121        #[snafu(implicit)]
122        location: Location,
123    },
124
125    #[snafu(display("Failed to cast arrow time i32 type into i64"))]
126    CastTimeType {
127        #[snafu(source)]
128        error: std::num::TryFromIntError,
129        #[snafu(implicit)]
130        location: Location,
131    },
132
133    #[snafu(display("Arrow failed to compute"))]
134    ArrowCompute {
135        #[snafu(source)]
136        error: arrow::error::ArrowError,
137        #[snafu(implicit)]
138        location: Location,
139    },
140
141    #[snafu(display("Failed to project arrow schema"))]
142    ProjectArrowSchema {
143        #[snafu(source)]
144        error: arrow::error::ArrowError,
145        #[snafu(implicit)]
146        location: Location,
147    },
148
149    #[snafu(display("Unsupported column default constraint expression: {}", expr))]
150    UnsupportedDefaultExpr {
151        expr: String,
152        #[snafu(implicit)]
153        location: Location,
154    },
155
156    #[snafu(display("Default value should not be null for non null column"))]
157    NullDefault {
158        #[snafu(implicit)]
159        location: Location,
160    },
161
162    #[snafu(display("Incompatible default value type, reason: {}", reason))]
163    DefaultValueType {
164        reason: String,
165        #[snafu(implicit)]
166        location: Location,
167    },
168
169    #[snafu(display("Duplicated metadata for {}", key))]
170    DuplicateMeta {
171        key: String,
172        #[snafu(implicit)]
173        location: Location,
174    },
175
176    #[snafu(display("Failed to convert value into scalar value, reason: {}", reason))]
177    ToScalarValue {
178        reason: String,
179        #[snafu(implicit)]
180        location: Location,
181    },
182
183    #[snafu(display("Invalid timestamp precision: {}", precision))]
184    InvalidTimestampPrecision {
185        precision: u64,
186        #[snafu(implicit)]
187        location: Location,
188    },
189
190    #[snafu(display("Column {} already exists", column))]
191    DuplicateColumn {
192        column: String,
193        #[snafu(implicit)]
194        location: Location,
195    },
196
197    #[snafu(display("Failed to unpack value to given type: {}", reason))]
198    TryFromValue {
199        reason: String,
200        #[snafu(implicit)]
201        location: Location,
202    },
203
204    #[snafu(display("Failed to specify the precision {} and scale {}", precision, scale))]
205    InvalidPrecisionOrScale {
206        precision: u8,
207        scale: i8,
208        #[snafu(source)]
209        error: arrow::error::ArrowError,
210        #[snafu(implicit)]
211        location: Location,
212    },
213
214    #[snafu(display("Invalid JSON: {}", value))]
215    InvalidJson {
216        value: String,
217        #[snafu(implicit)]
218        location: Location,
219    },
220
221    #[snafu(display("Invalid JSON2 layout: {reason}"))]
222    InvalidJson2Layout {
223        reason: String,
224        #[snafu(implicit)]
225        location: Location,
226    },
227
228    #[snafu(display("Invalid JSON2 settings: {reason}"))]
229    InvalidJson2Settings {
230        reason: String,
231        #[snafu(implicit)]
232        location: Location,
233    },
234
235    #[snafu(display("Invalid Vector: {}", msg))]
236    InvalidVector {
237        msg: String,
238        #[snafu(implicit)]
239        location: Location,
240    },
241
242    #[snafu(display("Value exceeds the precision {} bound", precision))]
243    ValueExceedsPrecision {
244        precision: u8,
245        #[snafu(source)]
246        error: arrow::error::ArrowError,
247        #[snafu(implicit)]
248        location: Location,
249    },
250
251    #[snafu(display("Failed to convert Arrow array to scalars"))]
252    ConvertArrowArrayToScalars {
253        #[snafu(source)]
254        error: datafusion_common::DataFusionError,
255        #[snafu(implicit)]
256        location: Location,
257    },
258
259    #[snafu(display("Failed to convert scalar value to Arrow array"))]
260    ConvertScalarToArrowArray {
261        #[snafu(source)]
262        error: datafusion_common::DataFusionError,
263        #[snafu(implicit)]
264        location: Location,
265    },
266
267    #[snafu(display("Failed to parse extended type in metadata: {}", value))]
268    ParseExtendedType {
269        value: String,
270        #[snafu(implicit)]
271        location: Location,
272    },
273
274    #[snafu(display("Invalid fulltext option: {}", msg))]
275    InvalidFulltextOption {
276        msg: String,
277        #[snafu(implicit)]
278        location: Location,
279    },
280
281    #[snafu(display("Invalid skipping index option: {}", msg))]
282    InvalidSkippingIndexOption {
283        msg: String,
284        #[snafu(implicit)]
285        location: Location,
286    },
287
288    #[snafu(display("Inconsistent struct field count {field_len} and item count {item_len}"))]
289    InconsistentStructFieldsAndItems {
290        field_len: usize,
291        item_len: usize,
292        #[snafu(implicit)]
293        location: Location,
294    },
295
296    #[snafu(display("Failed to process JSONB value"))]
297    InvalidJsonb {
298        error: jsonb::Error,
299        #[snafu(implicit)]
300        location: Location,
301    },
302
303    #[snafu(display("Failed to parse or serialize arrow metadata"))]
304    ArrowMetadata {
305        #[snafu(source)]
306        error: arrow::error::ArrowError,
307        #[snafu(implicit)]
308        location: Location,
309    },
310
311    #[snafu(display("Failed to align JSON value, reason: {}", reason))]
312    AlignJsonValue {
313        reason: String,
314        #[snafu(implicit)]
315        location: Location,
316    },
317
318    #[snafu(display("Failed to align JSON array, reason: {reason}"))]
319    AlignJsonArray {
320        reason: String,
321        #[snafu(implicit)]
322        location: Location,
323    },
324
325    #[snafu(display("Non-object json is not supported currently"))]
326    UnsupportedJsonType {
327        #[snafu(implicit)]
328        location: Location,
329    },
330
331    #[snafu(display("unexpected: {reason}"))]
332    Unexpected {
333        reason: String,
334        #[snafu(implicit)]
335        location: Location,
336    },
337}
338
339impl ErrorExt for Error {
340    fn status_code(&self) -> StatusCode {
341        use Error::*;
342        match self {
343            UnsupportedOperation { .. }
344            | Unimplemented { .. }
345            | UnsupportedArrowType { .. }
346            | UnsupportedJsonType { .. }
347            | UnsupportedDefaultExpr { .. } => StatusCode::Unsupported,
348
349            DuplicateColumn { .. }
350            | BadArrayAccess { .. }
351            | NullDefault { .. }
352            | InvalidTimestampIndex { .. }
353            | DefaultValueType { .. }
354            | DuplicateMeta { .. }
355            | InvalidTimestampPrecision { .. }
356            | InvalidPrecisionOrScale { .. }
357            | InvalidJson { .. }
358            | InvalidJson2Layout { .. }
359            | InvalidJson2Settings { .. }
360            | InvalidJsonb { .. }
361            | InvalidVector { .. }
362            | InvalidFulltextOption { .. }
363            | InvalidSkippingIndexOption { .. } => StatusCode::InvalidArguments,
364
365            ValueExceedsPrecision { .. }
366            | CastType { .. }
367            | CastTimeType { .. }
368            | Conversion { .. } => StatusCode::IllegalState,
369
370            Serialize { .. }
371            | Deserialize { .. }
372            | UnknownVector { .. }
373            | ParseSchemaVersion { .. }
374            | ArrowCompute { .. }
375            | ProjectArrowSchema { .. }
376            | ToScalarValue { .. }
377            | TryFromValue { .. }
378            | ConvertArrowArrayToScalars { .. }
379            | ConvertScalarToArrowArray { .. }
380            | ParseExtendedType { .. }
381            | InconsistentStructFieldsAndItems { .. }
382            | ArrowMetadata { .. }
383            | AlignJsonValue { .. }
384            | AlignJsonArray { .. } => StatusCode::Internal,
385
386            Unexpected { .. } => StatusCode::Unexpected,
387        }
388    }
389
390    fn as_any(&self) -> &dyn Any {
391        self
392    }
393}
394
395pub type Result<T> = std::result::Result<T, Error>;