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
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 text: {}", 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 parse extended type in metadata: {}", value))]
224 ParseExtendedType {
225 value: String,
226 #[snafu(implicit)]
227 location: Location,
228 },
229 #[snafu(display("Invalid fulltext option: {}", msg))]
230 InvalidFulltextOption {
231 msg: String,
232 #[snafu(implicit)]
233 location: Location,
234 },
235 #[snafu(display("Invalid skipping index option: {}", msg))]
236 InvalidSkippingIndexOption {
237 msg: String,
238 #[snafu(implicit)]
239 location: Location,
240 },
241}
242
243impl ErrorExt for Error {
244 fn status_code(&self) -> StatusCode {
245 use Error::*;
246 match self {
247 UnsupportedOperation { .. }
248 | UnsupportedArrowType { .. }
249 | UnsupportedDefaultExpr { .. } => StatusCode::Unsupported,
250
251 DuplicateColumn { .. }
252 | BadArrayAccess { .. }
253 | NullDefault { .. }
254 | InvalidTimestampIndex { .. }
255 | DefaultValueType { .. }
256 | DuplicateMeta { .. }
257 | InvalidTimestampPrecision { .. }
258 | InvalidPrecisionOrScale { .. }
259 | InvalidJson { .. }
260 | InvalidVector { .. }
261 | InvalidFulltextOption { .. }
262 | InvalidSkippingIndexOption { .. } => StatusCode::InvalidArguments,
263
264 ValueExceedsPrecision { .. }
265 | CastType { .. }
266 | CastTimeType { .. }
267 | Conversion { .. } => StatusCode::IllegalState,
268
269 Serialize { .. }
270 | Deserialize { .. }
271 | UnknownVector { .. }
272 | ParseSchemaVersion { .. }
273 | ArrowCompute { .. }
274 | ProjectArrowSchema { .. }
275 | ToScalarValue { .. }
276 | TryFromValue { .. }
277 | ConvertArrowArrayToScalars { .. }
278 | ParseExtendedType { .. } => StatusCode::Internal,
279 }
280 }
281
282 fn as_any(&self) -> &dyn Any {
283 self
284 }
285}
286
287pub type Result<T> = std::result::Result<T, Error>;