1use std::any::Any;
16
17use common_error::ext::ErrorExt;
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use datafusion_common::ScalarValue;
21use datatypes::arrow;
22use datatypes::prelude::Value;
23use snafu::{Location, Snafu};
24use store_api::storage::RegionId;
25use table::metadata::TableId;
26
27use crate::expr::{Operand, PartitionExpr};
28
29#[derive(Snafu)]
30#[snafu(visibility(pub))]
31#[stack_trace_debug]
32pub enum Error {
33 #[snafu(display("Failed to find table route: {}", table_id))]
34 TableRouteNotFound {
35 table_id: TableId,
36 #[snafu(implicit)]
37 location: Location,
38 },
39
40 #[snafu(display("Table route manager error"))]
41 TableRouteManager {
42 source: common_meta::error::Error,
43 #[snafu(implicit)]
44 location: Location,
45 },
46
47 #[snafu(display("Failed to get meta info from cache, error: {}", err_msg))]
48 GetCache {
49 err_msg: String,
50 #[snafu(implicit)]
51 location: Location,
52 },
53
54 #[snafu(display("Failed to find table routes for table id {}", table_id))]
55 FindTableRoutes {
56 table_id: TableId,
57 #[snafu(implicit)]
58 location: Location,
59 },
60
61 #[snafu(display(
62 "Failed to find region routes for table {}, region id: {}",
63 table_id,
64 region_id
65 ))]
66 FindRegionRoutes {
67 table_id: TableId,
68 region_id: u64,
69 #[snafu(implicit)]
70 location: Location,
71 },
72
73 #[snafu(display("Failed to serialize value to json"))]
74 SerializeJson {
75 #[snafu(source)]
76 error: serde_json::Error,
77 #[snafu(implicit)]
78 location: Location,
79 },
80
81 #[snafu(display("Failed to deserialize value from json"))]
82 DeserializeJson {
83 #[snafu(source)]
84 error: serde_json::Error,
85 #[snafu(implicit)]
86 location: Location,
87 },
88
89 #[snafu(display("Expect {} region keys, actual {}", expect, actual))]
90 RegionKeysSize {
91 expect: usize,
92 actual: usize,
93 #[snafu(implicit)]
94 location: Location,
95 },
96
97 #[snafu(display("Invalid InsertRequest, reason: {}", reason))]
98 InvalidInsertRequest {
99 reason: String,
100 #[snafu(implicit)]
101 location: Location,
102 },
103
104 #[snafu(display("Invalid DeleteRequest, reason: {}", reason))]
105 InvalidDeleteRequest {
106 reason: String,
107 #[snafu(implicit)]
108 location: Location,
109 },
110
111 #[snafu(display("Invalid table route data, table id: {}, msg: {}", table_id, err_msg))]
112 InvalidTableRouteData {
113 table_id: TableId,
114 err_msg: String,
115 #[snafu(implicit)]
116 location: Location,
117 },
118
119 #[snafu(display("Failed to convert DataFusion's ScalarValue: {:?}", value))]
120 ConvertScalarValue {
121 value: ScalarValue,
122 #[snafu(implicit)]
123 location: Location,
124 source: datatypes::error::Error,
125 },
126
127 #[snafu(display("Failed to find leader of table id {} region {}", table_id, region_id))]
128 FindLeader {
129 table_id: TableId,
130 region_id: RegionId,
131 #[snafu(implicit)]
132 location: Location,
133 },
134
135 #[snafu(display("Unexpected table route type: {}", err_msg))]
136 UnexpectedLogicalRouteTable {
137 #[snafu(implicit)]
138 location: Location,
139 err_msg: String,
140 source: common_meta::error::Error,
141 },
142
143 #[snafu(display("Conjunct expr with non-expr is invalid"))]
144 ConjunctExprWithNonExpr {
145 expr: PartitionExpr,
146 #[snafu(implicit)]
147 location: Location,
148 },
149
150 #[snafu(display("Unclosed value {} on column {}", value, column))]
151 UnclosedValue {
152 value: String,
153 column: String,
154 #[snafu(implicit)]
155 location: Location,
156 },
157
158 #[snafu(display("Invalid partition expr: {:?}", expr))]
159 InvalidExpr {
160 expr: PartitionExpr,
161 #[snafu(implicit)]
162 location: Location,
163 },
164
165 #[snafu(display("Unexpected operand: {:?}, want Expr", operand))]
166 NoExprOperand {
167 operand: Operand,
168 #[snafu(implicit)]
169 location: Location,
170 },
171
172 #[snafu(display("Undefined column: {}", column))]
173 UndefinedColumn {
174 column: String,
175 #[snafu(implicit)]
176 location: Location,
177 },
178
179 #[snafu(display("Unexpected: {err_msg}"))]
180 Unexpected {
181 err_msg: String,
182 #[snafu(implicit)]
183 location: Location,
184 },
185
186 #[snafu(display("Failed to convert to vector"))]
187 ConvertToVector {
188 source: datatypes::error::Error,
189 #[snafu(implicit)]
190 location: Location,
191 },
192
193 #[snafu(display("Failed to evaluate record batch"))]
194 EvaluateRecordBatch {
195 #[snafu(source)]
196 error: datafusion_common::error::DataFusionError,
197 #[snafu(implicit)]
198 location: Location,
199 },
200
201 #[snafu(display("Failed to compute arrow kernel"))]
202 ComputeArrowKernel {
203 #[snafu(source)]
204 error: arrow::error::ArrowError,
205 #[snafu(implicit)]
206 location: Location,
207 },
208
209 #[snafu(display("Unexpected evaluation result column type: {}", data_type))]
210 UnexpectedColumnType {
211 data_type: arrow::datatypes::DataType,
212 #[snafu(implicit)]
213 location: Location,
214 },
215
216 #[snafu(display("Failed to convert to DataFusion's Schema"))]
217 ToDFSchema {
218 #[snafu(source)]
219 error: datafusion_common::error::DataFusionError,
220 #[snafu(implicit)]
221 location: Location,
222 },
223
224 #[snafu(display("Failed to create physical expression"))]
225 CreatePhysicalExpr {
226 #[snafu(source)]
227 error: datafusion_common::error::DataFusionError,
228 #[snafu(implicit)]
229 location: Location,
230 },
231
232 #[snafu(display("Partition expr value is not supported: {:?}", value))]
233 UnsupportedPartitionExprValue {
234 value: Value,
235 #[snafu(implicit)]
236 location: Location,
237 },
238}
239
240impl ErrorExt for Error {
241 fn status_code(&self) -> StatusCode {
242 match self {
243 Error::GetCache { .. } => StatusCode::StorageUnavailable,
244 Error::FindLeader { .. } => StatusCode::TableUnavailable,
245
246 Error::ConjunctExprWithNonExpr { .. }
247 | Error::UnclosedValue { .. }
248 | Error::InvalidExpr { .. }
249 | Error::NoExprOperand { .. }
250 | Error::UndefinedColumn { .. } => StatusCode::InvalidArguments,
251
252 Error::RegionKeysSize { .. }
253 | Error::InvalidInsertRequest { .. }
254 | Error::InvalidDeleteRequest { .. } => StatusCode::InvalidArguments,
255
256 Error::ConvertScalarValue { .. }
257 | Error::SerializeJson { .. }
258 | Error::DeserializeJson { .. } => StatusCode::Internal,
259
260 Error::Unexpected { .. }
261 | Error::InvalidTableRouteData { .. }
262 | Error::FindTableRoutes { .. }
263 | Error::FindRegionRoutes { .. } => StatusCode::Unexpected,
264 Error::TableRouteNotFound { .. } => StatusCode::TableNotFound,
265 Error::TableRouteManager { source, .. } => source.status_code(),
266 Error::UnexpectedLogicalRouteTable { source, .. } => source.status_code(),
267 Error::ConvertToVector { source, .. } => source.status_code(),
268 Error::EvaluateRecordBatch { .. } => StatusCode::Internal,
269 Error::ComputeArrowKernel { .. } => StatusCode::Internal,
270 Error::UnexpectedColumnType { .. } => StatusCode::Internal,
271 Error::ToDFSchema { .. } => StatusCode::Internal,
272 Error::CreatePhysicalExpr { .. } => StatusCode::Internal,
273 Error::UnsupportedPartitionExprValue { .. } => StatusCode::InvalidArguments,
274 }
275 }
276
277 fn as_any(&self) -> &dyn Any {
278 self
279 }
280}
281
282pub type Result<T> = std::result::Result<T, Error>;