1use std::any::Any;
16
17use common_error::ext::ErrorExt;
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use datafusion::error::DataFusionError;
21use snafu::{Location, Snafu};
22
23#[derive(Snafu)]
24#[snafu(visibility(pub))]
25#[stack_trace_debug]
26pub enum Error {
27 #[snafu(display("Internal error during building DataFusion plan"))]
28 DataFusionPlanning {
29 #[snafu(source)]
30 error: DataFusionError,
31 #[snafu(implicit)]
32 location: Location,
33 },
34
35 #[snafu(display(
36 "Illegal range: offset {}, length {}, array len {}",
37 offset,
38 length,
39 len,
40 ))]
41 IllegalRange {
42 offset: u32,
43 length: u32,
44 len: usize,
45 #[snafu(implicit)]
46 location: Location,
47 },
48
49 #[snafu(display("Failed to deserialize"))]
50 Deserialize {
51 #[snafu(source)]
52 error: prost::DecodeError,
53 #[snafu(implicit)]
54 location: Location,
55 },
56
57 #[snafu(display("Empty range is not expected"))]
58 EmptyRange {
59 #[snafu(implicit)]
60 location: Location,
61 },
62
63 #[snafu(display("Cannot find column {col}"))]
64 ColumnNotFound {
65 col: String,
66 #[snafu(implicit)]
67 location: Location,
68 },
69}
70
71impl ErrorExt for Error {
72 fn status_code(&self) -> StatusCode {
73 use Error::*;
74 match self {
75 Deserialize { .. } => StatusCode::Unexpected,
76 IllegalRange { .. } | ColumnNotFound { .. } | EmptyRange { .. } => {
77 StatusCode::InvalidArguments
78 }
79
80 DataFusionPlanning { .. } => StatusCode::PlanQuery,
81 }
82 }
83
84 fn as_any(&self) -> &dyn Any {
85 self
86 }
87}
88
89pub type Result<T> = std::result::Result<T, Error>;
90
91impl From<Error> for DataFusionError {
92 fn from(err: Error) -> Self {
93 DataFusionError::External(Box::new(err))
94 }
95}
96
97pub(crate) fn ensure(
98 predicate: bool,
99 error: DataFusionError,
100) -> std::result::Result<(), DataFusionError> {
101 if predicate {
102 Ok(())
103 } else {
104 Err(error)
105 }
106}