1use std::any::Any;
16
17use arrow_schema::ArrowError;
18use common_error::ext::{ErrorExt, RetryHint, retry_hint_from_io_error};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use datafusion::parquet::errors::ParquetError;
22use object_store::error::retry_hint_from_opendal_error;
23use snafu::{Location, Snafu};
24use url::ParseError;
25
26#[derive(Snafu)]
27#[snafu(visibility(pub))]
28#[stack_trace_debug]
29pub enum Error {
30 #[snafu(display("Invalid packed snapshot: {reason}"))]
31 InvalidPackedSnapshot { reason: String },
32
33 #[snafu(display("Parquet write cancelled"))]
34 ParquetWriteCancelled {},
35
36 #[snafu(display("Parquet writer limits must be positive"))]
37 InvalidParquetWriterLimits {},
38
39 #[snafu(display("Parquet writer resource limit exceeded: {reason}"))]
40 ParquetWriterResource { reason: String },
41
42 #[snafu(display("Unsupported compression type: {}", compression_type))]
43 UnsupportedCompressionType {
44 compression_type: String,
45 #[snafu(implicit)]
46 location: Location,
47 },
48
49 #[snafu(display("Unsupported backend protocol: {}, url: {}", protocol, url))]
50 UnsupportedBackendProtocol {
51 protocol: String,
52 #[snafu(implicit)]
53 location: Location,
54 url: String,
55 },
56
57 #[snafu(display("Unsupported format protocol: {}", format))]
58 UnsupportedFormat {
59 format: String,
60 #[snafu(implicit)]
61 location: Location,
62 },
63
64 #[snafu(display("empty host: {}", url))]
65 EmptyHostPath {
66 url: String,
67 #[snafu(implicit)]
68 location: Location,
69 },
70
71 #[snafu(display("Invalid url: {}", url))]
72 InvalidUrl {
73 url: String,
74 #[snafu(source)]
75 error: ParseError,
76 #[snafu(implicit)]
77 location: Location,
78 },
79
80 #[snafu(display(
81 "SQL access to the local filesystem is disabled for '{}'; use S3, OSS, GCS, or AzBlob instead",
82 path
83 ))]
84 LocalFileAccessDisabled {
85 path: String,
86 #[snafu(implicit)]
87 location: Location,
88 },
89
90 #[snafu(display(
91 "Local filesystem path '{}' is outside the configured copy root or is unsafe: {}; use a path relative to the copy root or use S3, OSS, GCS, or AzBlob",
92 path,
93 reason
94 ))]
95 LocalFileAccessDenied {
96 path: String,
97 reason: String,
98 #[snafu(implicit)]
99 location: Location,
100 },
101
102 #[snafu(display(
103 "{} location has no credentials, and query.allow_server_credentials is false; provide credentials in the connection options",
104 backend
105 ))]
106 ServerCredentialsDisallowed {
107 backend: String,
108 #[snafu(implicit)]
109 location: Location,
110 },
111
112 #[snafu(display(
113 "Local filesystem path '{}' does not exist within the configured copy root",
114 path
115 ))]
116 LocalFilePathNotFound {
117 path: String,
118 #[snafu(implicit)]
119 location: Location,
120 },
121
122 #[snafu(display("Location must include a file or object name: '{}'", path))]
123 MissingObjectName {
124 path: String,
125 #[snafu(implicit)]
126 location: Location,
127 },
128
129 #[snafu(display("Invalid local filesystem root '{}'", root))]
130 InvalidLocalFileRoot {
131 root: String,
132 #[snafu(source)]
133 error: std::io::Error,
134 #[snafu(implicit)]
135 location: Location,
136 },
137
138 #[snafu(display("Invalid local filesystem root '{}': {}", root, reason))]
139 InvalidLocalFileRootConfig {
140 root: String,
141 reason: String,
142 #[snafu(implicit)]
143 location: Location,
144 },
145
146 #[snafu(display("Failed to build backend"))]
147 BuildBackend {
148 #[snafu(source)]
149 error: object_store::Error,
150 #[snafu(implicit)]
151 location: Location,
152 },
153
154 #[snafu(display("Failed to build orc reader"))]
155 OrcReader {
156 #[snafu(implicit)]
157 location: Location,
158 #[snafu(source)]
159 error: orc_rust::error::OrcError,
160 },
161
162 #[snafu(display("Failed to read object from path: {}", path))]
163 ReadObject {
164 path: String,
165 #[snafu(implicit)]
166 location: Location,
167 #[snafu(source)]
168 error: object_store::Error,
169 },
170
171 #[snafu(display("Failed to write object to path: {}", path))]
172 WriteObject {
173 path: String,
174 #[snafu(implicit)]
175 location: Location,
176 #[snafu(source)]
177 error: object_store::Error,
178 },
179
180 #[snafu(display("Failed to write"))]
181 AsyncWrite {
182 #[snafu(source)]
183 error: std::io::Error,
184 #[snafu(implicit)]
185 location: Location,
186 },
187
188 #[snafu(display("Failed to write record batch"))]
189 WriteRecordBatch {
190 #[snafu(implicit)]
191 location: Location,
192 #[snafu(source)]
193 error: ArrowError,
194 },
195
196 #[snafu(display("Failed to encode record batch"))]
197 EncodeRecordBatch {
198 #[snafu(implicit)]
199 location: Location,
200 #[snafu(source)]
201 error: ParquetError,
202 },
203
204 #[snafu(display("Failed to read record batch"))]
205 ReadRecordBatch {
206 #[snafu(implicit)]
207 location: Location,
208 #[snafu(source)]
209 error: datafusion::error::DataFusionError,
210 },
211
212 #[snafu(display("Failed to read parquet"))]
213 ReadParquetSnafu {
214 #[snafu(implicit)]
215 location: Location,
216 #[snafu(source)]
217 error: datafusion::parquet::errors::ParquetError,
218 },
219
220 #[snafu(display("Failed to convert parquet to schema"))]
221 ParquetToSchema {
222 #[snafu(implicit)]
223 location: Location,
224 #[snafu(source)]
225 error: datafusion::parquet::errors::ParquetError,
226 },
227
228 #[snafu(display("Failed to infer schema from file"))]
229 InferSchema {
230 #[snafu(implicit)]
231 location: Location,
232 #[snafu(source)]
233 error: arrow_schema::ArrowError,
234 },
235
236 #[snafu(display("Failed to list object in path: {}", path))]
237 ListObjects {
238 path: String,
239 #[snafu(implicit)]
240 location: Location,
241 #[snafu(source)]
242 error: object_store::Error,
243 },
244
245 #[snafu(display("Invalid connection: {}", msg))]
246 InvalidConnection {
247 msg: String,
248 #[snafu(implicit)]
249 location: Location,
250 },
251
252 #[snafu(display("Failed to join handle"))]
253 JoinHandle {
254 #[snafu(implicit)]
255 location: Location,
256 #[snafu(source)]
257 error: tokio::task::JoinError,
258 },
259
260 #[snafu(display("Failed to parse format {} with value: {}", key, value))]
261 ParseFormat {
262 key: String,
263 value: String,
264 #[snafu(implicit)]
265 location: Location,
266 },
267
268 #[snafu(display("Failed to merge schema"))]
269 MergeSchema {
270 #[snafu(source)]
271 error: arrow_schema::ArrowError,
272 #[snafu(implicit)]
273 location: Location,
274 },
275
276 #[snafu(display("Failed to write parquet file, path: {}", path))]
277 WriteParquet {
278 path: String,
279 #[snafu(implicit)]
280 location: Location,
281 #[snafu(source)]
282 error: parquet::errors::ParquetError,
283 },
284
285 #[snafu(transparent)]
286 DataFusion {
287 #[snafu(implicit)]
288 location: Location,
289 #[snafu(source)]
290 error: datafusion::error::DataFusionError,
291 },
292}
293
294pub type Result<T> = std::result::Result<T, Error>;
295
296impl ErrorExt for Error {
297 fn status_code(&self) -> StatusCode {
298 use Error::*;
299 match self {
300 ParquetWriteCancelled {} => StatusCode::Cancelled,
301 InvalidPackedSnapshot { .. } => StatusCode::InvalidArguments,
302 InvalidParquetWriterLimits {} => StatusCode::InvalidArguments,
303 ParquetWriterResource { .. } => StatusCode::Suspended,
304 BuildBackend { .. }
305 | ListObjects { .. }
306 | ReadObject { .. }
307 | WriteObject { .. }
308 | AsyncWrite { .. }
309 | WriteParquet { .. } => StatusCode::StorageUnavailable,
310
311 UnsupportedBackendProtocol { .. }
312 | UnsupportedCompressionType { .. }
313 | UnsupportedFormat { .. }
314 | InvalidConnection { .. }
315 | InvalidUrl { .. }
316 | LocalFileAccessDisabled { .. }
317 | LocalFileAccessDenied { .. }
318 | ServerCredentialsDisallowed { .. }
319 | LocalFilePathNotFound { .. }
320 | MissingObjectName { .. }
321 | InvalidLocalFileRoot { .. }
322 | InvalidLocalFileRootConfig { .. }
323 | EmptyHostPath { .. }
324 | InferSchema { .. }
325 | ReadParquetSnafu { .. }
326 | ParquetToSchema { .. }
327 | ParseFormat { .. }
328 | MergeSchema { .. } => StatusCode::InvalidArguments,
329
330 JoinHandle { .. }
331 | ReadRecordBatch { .. }
332 | WriteRecordBatch { .. }
333 | EncodeRecordBatch { .. }
334 | OrcReader { .. } => StatusCode::Unexpected,
335
336 DataFusion { .. } => StatusCode::Internal,
337 }
338 }
339
340 fn as_any(&self) -> &dyn Any {
341 self
342 }
343
344 fn retry_hint(&self) -> RetryHint {
345 use Error::*;
346
347 match self {
348 BuildBackend { error, .. }
349 | ListObjects { error, .. }
350 | ReadObject { error, .. }
351 | WriteObject { error, .. } => retry_hint_from_opendal_error(error),
352 AsyncWrite { error, .. } => retry_hint_from_io_error(error),
353 WriteParquet { .. } => RetryHint::Retryable,
354 _ => RetryHint::NonRetryable,
355 }
356 }
357}