1use 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
24pub 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 #[cfg(feature = "enterprise")]
282 #[snafu(display("Invalid sketch option: {}", msg))]
283 InvalidSketchOption {
284 msg: String,
285 #[snafu(implicit)]
286 location: Location,
287 },
288
289 #[snafu(display("Invalid skipping index option: {}", msg))]
290 InvalidSkippingIndexOption {
291 msg: String,
292 #[snafu(implicit)]
293 location: Location,
294 },
295
296 #[snafu(display("Invalid compression option: {}", msg))]
297 InvalidCompressionOption {
298 msg: String,
299 #[snafu(implicit)]
300 location: Location,
301 },
302
303 #[snafu(display("Inconsistent struct field count {field_len} and item count {item_len}"))]
304 InconsistentStructFieldsAndItems {
305 field_len: usize,
306 item_len: usize,
307 #[snafu(implicit)]
308 location: Location,
309 },
310
311 #[snafu(display("Failed to process JSONB value"))]
312 InvalidJsonb {
313 error: jsonb::Error,
314 #[snafu(implicit)]
315 location: Location,
316 },
317
318 #[snafu(display("Failed to parse or serialize arrow metadata"))]
319 ArrowMetadata {
320 #[snafu(source)]
321 error: arrow::error::ArrowError,
322 #[snafu(implicit)]
323 location: Location,
324 },
325
326 #[snafu(display("Failed to align JSON value, reason: {}", reason))]
327 AlignJsonValue {
328 reason: String,
329 #[snafu(implicit)]
330 location: Location,
331 },
332
333 #[snafu(display("Failed to align JSON array, reason: {reason}"))]
334 AlignJsonArray {
335 reason: String,
336 #[snafu(implicit)]
337 location: Location,
338 },
339
340 #[snafu(display("Non-object json is not supported currently"))]
341 UnsupportedJsonType {
342 #[snafu(implicit)]
343 location: Location,
344 },
345
346 #[snafu(display("Invalid series: {reason}"))]
347 InvalidSeries {
348 reason: String,
349 #[snafu(implicit)]
350 location: Location,
351 },
352
353 #[snafu(display("unexpected: {reason}"))]
354 Unexpected {
355 reason: String,
356 #[snafu(implicit)]
357 location: Location,
358 },
359}
360
361impl ErrorExt for Error {
362 fn status_code(&self) -> StatusCode {
363 use Error::*;
364 match self {
365 UnsupportedOperation { .. }
366 | Unimplemented { .. }
367 | UnsupportedArrowType { .. }
368 | UnsupportedJsonType { .. }
369 | UnsupportedDefaultExpr { .. } => StatusCode::Unsupported,
370
371 DuplicateColumn { .. }
372 | BadArrayAccess { .. }
373 | NullDefault { .. }
374 | InvalidTimestampIndex { .. }
375 | DefaultValueType { .. }
376 | DuplicateMeta { .. }
377 | InvalidTimestampPrecision { .. }
378 | InvalidPrecisionOrScale { .. }
379 | InvalidJson { .. }
380 | InvalidJson2Layout { .. }
381 | InvalidJson2Settings { .. }
382 | InvalidJsonb { .. }
383 | InvalidVector { .. }
384 | InvalidFulltextOption { .. }
385 | InvalidSkippingIndexOption { .. }
386 | InvalidCompressionOption { .. }
387 | InvalidSeries { .. } => StatusCode::InvalidArguments,
388
389 #[cfg(feature = "enterprise")]
390 InvalidSketchOption { .. } => StatusCode::InvalidArguments,
391
392 ValueExceedsPrecision { .. }
393 | CastType { .. }
394 | CastTimeType { .. }
395 | Conversion { .. } => StatusCode::IllegalState,
396
397 Serialize { .. }
398 | Deserialize { .. }
399 | UnknownVector { .. }
400 | ParseSchemaVersion { .. }
401 | ArrowCompute { .. }
402 | ProjectArrowSchema { .. }
403 | ToScalarValue { .. }
404 | TryFromValue { .. }
405 | ConvertArrowArrayToScalars { .. }
406 | ConvertScalarToArrowArray { .. }
407 | ParseExtendedType { .. }
408 | InconsistentStructFieldsAndItems { .. }
409 | ArrowMetadata { .. }
410 | AlignJsonValue { .. }
411 | AlignJsonArray { .. } => StatusCode::Internal,
412
413 Unexpected { .. } => StatusCode::Unexpected,
414 }
415 }
416
417 fn as_any(&self) -> &dyn Any {
418 self
419 }
420}
421
422pub type Result<T> = std::result::Result<T, Error>;