Skip to main content

partition/
error.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16
17use common_error::ext::{ErrorExt, RetryHint};
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 partition info"))]
48    GetPartitionInfo {
49        source: common_meta::error::Error,
50        #[snafu(implicit)]
51        location: Location,
52    },
53
54    #[snafu(display("Failed to get meta info from cache, error: {}", err_msg))]
55    GetCache {
56        err_msg: String,
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 partition input, reason: {}", reason))]
105    InvalidPartitionInput {
106        reason: String,
107        #[snafu(implicit)]
108        location: Location,
109    },
110
111    #[snafu(display("Invalid DeleteRequest, reason: {}", reason))]
112    InvalidDeleteRequest {
113        reason: String,
114        #[snafu(implicit)]
115        location: Location,
116    },
117
118    #[snafu(display("Failed to convert DataFusion's ScalarValue: {:?}", value))]
119    ConvertScalarValue {
120        value: ScalarValue,
121        #[snafu(implicit)]
122        location: Location,
123        source: datatypes::error::Error,
124    },
125
126    #[snafu(display("Failed to find leader of table id {} region {}", table_id, region_id))]
127    FindLeader {
128        table_id: TableId,
129        region_id: RegionId,
130        #[snafu(implicit)]
131        location: Location,
132    },
133
134    #[snafu(display("Unexpected table route type: {}", err_msg))]
135    UnexpectedLogicalRouteTable {
136        #[snafu(implicit)]
137        location: Location,
138        err_msg: String,
139        source: common_meta::error::Error,
140    },
141
142    #[snafu(display("Invalid partition expr: {:?}", expr))]
143    InvalidExpr {
144        expr: PartitionExpr,
145        #[snafu(implicit)]
146        location: Location,
147    },
148
149    #[snafu(display("Unexpected operand: {:?}, want Expr", operand))]
150    NoExprOperand {
151        operand: Operand,
152        #[snafu(implicit)]
153        location: Location,
154    },
155
156    #[snafu(display("Undefined column: {}", column))]
157    UndefinedColumn {
158        column: String,
159        #[snafu(implicit)]
160        location: Location,
161    },
162
163    #[snafu(display("Unexpected: {err_msg}"))]
164    Unexpected {
165        err_msg: String,
166        #[snafu(implicit)]
167        location: Location,
168    },
169
170    #[snafu(display("Failed to convert to vector"))]
171    ConvertToVector {
172        source: datatypes::error::Error,
173        #[snafu(implicit)]
174        location: Location,
175    },
176
177    #[snafu(display("Failed to evaluate record batch"))]
178    EvaluateRecordBatch {
179        #[snafu(source)]
180        error: datafusion_common::error::DataFusionError,
181        #[snafu(implicit)]
182        location: Location,
183    },
184
185    #[snafu(display("Failed to compute arrow kernel"))]
186    ComputeArrowKernel {
187        #[snafu(source)]
188        error: arrow::error::ArrowError,
189        #[snafu(implicit)]
190        location: Location,
191    },
192
193    #[snafu(display("Unexpected evaluation result column type: {}", data_type))]
194    UnexpectedColumnType {
195        data_type: arrow::datatypes::DataType,
196        #[snafu(implicit)]
197        location: Location,
198    },
199
200    #[snafu(display("Failed to convert to DataFusion's Schema"))]
201    ToDFSchema {
202        #[snafu(source)]
203        error: datafusion_common::error::DataFusionError,
204        #[snafu(implicit)]
205        location: Location,
206    },
207
208    #[snafu(display("Failed to create physical expression"))]
209    CreatePhysicalExpr {
210        #[snafu(source)]
211        error: datafusion_common::error::DataFusionError,
212        #[snafu(implicit)]
213        location: Location,
214    },
215
216    #[snafu(display("Failed to coerce partition expr against the schema"))]
217    CoercePartitionExpr {
218        #[snafu(source)]
219        error: datafusion_common::error::DataFusionError,
220        #[snafu(implicit)]
221        location: Location,
222    },
223
224    #[snafu(display("Partition expr value is not supported: {:?}", value))]
225    UnsupportedPartitionExprValue {
226        value: Value,
227        #[snafu(implicit)]
228        location: Location,
229    },
230
231    #[snafu(display("Failed to convert partition expr value to ScalarValue: {:?}", value))]
232    ConvertPartitionExprValue {
233        value: Value,
234        #[snafu(implicit)]
235        location: Location,
236        source: datatypes::error::Error,
237    },
238
239    #[snafu(display("Duplicate expr: {:?}", expr))]
240    DuplicateExpr {
241        expr: PartitionExpr,
242        #[snafu(implicit)]
243        location: Location,
244    },
245
246    #[snafu(display("Checkpoint `{}` is not covered", checkpoint))]
247    CheckpointNotCovered {
248        checkpoint: String,
249        #[snafu(implicit)]
250        location: Location,
251    },
252
253    #[snafu(display("Checkpoint `{}` is overlapped", checkpoint))]
254    CheckpointOverlapped {
255        checkpoint: String,
256        #[snafu(implicit)]
257        location: Location,
258    },
259}
260
261impl ErrorExt for Error {
262    fn status_code(&self) -> StatusCode {
263        match self {
264            Error::GetCache { .. } => StatusCode::StorageUnavailable,
265            Error::FindLeader { .. } => StatusCode::TableUnavailable,
266
267            Error::InvalidExpr { .. }
268            | Error::NoExprOperand { .. }
269            | Error::UndefinedColumn { .. }
270            | Error::DuplicateExpr { .. }
271            | Error::CheckpointNotCovered { .. }
272            | Error::CheckpointOverlapped { .. } => StatusCode::InvalidArguments,
273
274            Error::RegionKeysSize { .. }
275            | Error::InvalidInsertRequest { .. }
276            | Error::InvalidPartitionInput { .. }
277            | Error::InvalidDeleteRequest { .. } => StatusCode::InvalidArguments,
278
279            Error::ConvertScalarValue { .. }
280            | Error::SerializeJson { .. }
281            | Error::DeserializeJson { .. } => StatusCode::Internal,
282
283            Error::Unexpected { .. } | Error::FindRegionRoutes { .. } => StatusCode::Unexpected,
284            Error::TableRouteNotFound { .. } => StatusCode::TableNotFound,
285            Error::TableRouteManager { source, .. } => source.status_code(),
286            Error::GetPartitionInfo { source, .. } => source.status_code(),
287            Error::UnexpectedLogicalRouteTable { source, .. } => source.status_code(),
288            Error::ConvertToVector { source, .. } => source.status_code(),
289            Error::EvaluateRecordBatch { .. } => StatusCode::Internal,
290            Error::ComputeArrowKernel { .. } => StatusCode::Internal,
291            Error::UnexpectedColumnType { .. } => StatusCode::Internal,
292            Error::ToDFSchema { .. } => StatusCode::Internal,
293            Error::CreatePhysicalExpr { .. } => StatusCode::Internal,
294            Error::CoercePartitionExpr { .. } => StatusCode::Internal,
295            Error::UnsupportedPartitionExprValue { .. } => StatusCode::InvalidArguments,
296            Error::ConvertPartitionExprValue { .. } => StatusCode::InvalidArguments,
297        }
298    }
299
300    fn as_any(&self) -> &dyn Any {
301        self
302    }
303
304    fn retry_hint(&self) -> RetryHint {
305        match self {
306            Error::GetCache { .. } | Error::FindLeader { .. } => RetryHint::Retryable,
307            Error::TableRouteManager { source, .. }
308            | Error::GetPartitionInfo { source, .. }
309            | Error::UnexpectedLogicalRouteTable { source, .. } => source.retry_hint(),
310            Error::ConvertToVector { source, .. } => source.retry_hint(),
311            _ => RetryHint::NonRetryable,
312        }
313    }
314}
315
316pub type Result<T> = std::result::Result<T, Error>;