Skip to main content

table/
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::{BoxedError, ErrorExt};
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use common_query::error::datafusion_status_code;
21use datafusion::error::DataFusionError;
22use datatypes::arrow::error::ArrowError;
23use snafu::{Location, Snafu};
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27/// Default error implementation of table.
28#[derive(Snafu)]
29#[snafu(visibility(pub))]
30#[stack_trace_debug]
31pub enum Error {
32    #[snafu(display("DataFusion error"))]
33    Datafusion {
34        #[snafu(source)]
35        error: DataFusionError,
36        #[snafu(implicit)]
37        location: Location,
38    },
39
40    #[snafu(display("Failed to convert Arrow schema"))]
41    SchemaConversion {
42        source: datatypes::error::Error,
43        #[snafu(implicit)]
44        location: Location,
45    },
46
47    #[snafu(display("Table projection error"))]
48    TableProjection {
49        #[snafu(source)]
50        error: ArrowError,
51        #[snafu(implicit)]
52        location: Location,
53    },
54
55    #[snafu(display("Failed to create record batch for Tables"))]
56    TablesRecordBatch {
57        #[snafu(implicit)]
58        location: Location,
59        source: BoxedError,
60    },
61
62    #[snafu(display("Column {column_name} already exists in table {table_name}"))]
63    ColumnExists {
64        column_name: String,
65        table_name: String,
66        #[snafu(implicit)]
67        location: Location,
68    },
69
70    #[snafu(display("Failed to build schema, msg: {}", msg))]
71    SchemaBuild {
72        #[snafu(implicit)]
73        location: Location,
74        source: datatypes::error::Error,
75        msg: String,
76    },
77
78    #[snafu(display("Column {} not exists in table {}", column_name, table_name))]
79    ColumnNotExists {
80        column_name: String,
81        table_name: String,
82        #[snafu(implicit)]
83        location: Location,
84    },
85
86    #[snafu(display(
87        "Not allowed to remove index column {} from table {}",
88        column_name,
89        table_name
90    ))]
91    RemoveColumnInIndex {
92        column_name: String,
93        table_name: String,
94        #[snafu(implicit)]
95        location: Location,
96    },
97
98    #[snafu(display(
99        "Not allowed to remove partition column {} from table {}",
100        column_name,
101        table_name
102    ))]
103    RemovePartitionColumn {
104        column_name: String,
105        table_name: String,
106        #[snafu(implicit)]
107        location: Location,
108    },
109
110    #[snafu(display(
111        "Failed to build column descriptor for table: {}, column: {}",
112        table_name,
113        column_name,
114    ))]
115    BuildColumnDescriptor {
116        #[snafu(source)]
117        error: store_api::storage::ColumnDescriptorBuilderError,
118        table_name: String,
119        column_name: String,
120        #[snafu(implicit)]
121        location: Location,
122    },
123
124    #[snafu(display("Failed to operate table"))]
125    TableOperation { source: BoxedError },
126
127    #[snafu(display("Unsupported operation: {}", operation))]
128    Unsupported { operation: String },
129
130    #[snafu(display(
131        "Cannot set {} on a logical table; configure it on the physical table",
132        key
133    ))]
134    PhysicalTableOption {
135        key: String,
136        #[snafu(implicit)]
137        location: Location,
138    },
139
140    #[snafu(display("Failed to parse table option, key: {}, value: {}", key, value))]
141    ParseTableOption {
142        key: String,
143        value: String,
144        #[snafu(implicit)]
145        location: Location,
146    },
147
148    #[snafu(display(
149        "Conflicting table options: {}={} and {}={}",
150        first_key,
151        first_value,
152        second_key,
153        second_value
154    ))]
155    ConflictingTableOptions {
156        first_key: String,
157        first_value: String,
158        second_key: String,
159        second_value: String,
160        #[snafu(implicit)]
161        location: Location,
162    },
163
164    #[snafu(display("Invalid alter table({}) request: {}", table, err))]
165    InvalidAlterRequest {
166        table: String,
167        #[snafu(implicit)]
168        location: Location,
169        err: String,
170    },
171
172    #[snafu(display("Missing time index column in table: {}", table_name))]
173    MissingTimeIndexColumn {
174        table_name: String,
175        #[snafu(implicit)]
176        location: Location,
177    },
178
179    #[snafu(display("Invalid column option, column name: {}, error: {}", column_name, msg))]
180    InvalidColumnOption {
181        column_name: String,
182        msg: String,
183        #[snafu(implicit)]
184        location: Location,
185    },
186
187    #[snafu(display("Failed to set fulltext options for column {}", column_name))]
188    SetFulltextOptions {
189        column_name: String,
190        source: datatypes::Error,
191        #[snafu(implicit)]
192        location: Location,
193    },
194
195    #[snafu(display("Failed to set skipping index options for column {}", column_name))]
196    SetSkippingOptions {
197        column_name: String,
198        source: datatypes::Error,
199        #[snafu(implicit)]
200        location: Location,
201    },
202
203    #[snafu(display("Failed to unset skipping index options for column {}", column_name))]
204    UnsetSkippingOptions {
205        column_name: String,
206        source: datatypes::Error,
207        #[snafu(implicit)]
208        location: Location,
209    },
210
211    #[snafu(display("Invalid table name: '{s}'"))]
212    InvalidTableName { s: String },
213
214    #[snafu(display("Failed to cast default value, reason: {}", reason))]
215    CastDefaultValue {
216        reason: String,
217        source: datatypes::Error,
218        #[snafu(implicit)]
219        location: Location,
220    },
221
222    #[snafu(display("Sql common error"))]
223    SqlCommon {
224        source: common_sql::error::Error,
225        #[snafu(implicit)]
226        location: Location,
227    },
228}
229
230impl ErrorExt for Error {
231    fn status_code(&self) -> StatusCode {
232        match self {
233            Error::Datafusion { error, .. } => datafusion_status_code::<Self>(error, None),
234            Error::SchemaConversion { .. } | Error::TableProjection { .. } => {
235                StatusCode::EngineExecuteQuery
236            }
237            Error::RemoveColumnInIndex { .. }
238            | Error::RemovePartitionColumn { .. }
239            | Error::BuildColumnDescriptor { .. }
240            | Error::InvalidAlterRequest { .. } => StatusCode::InvalidArguments,
241            Error::CastDefaultValue { source, .. } => source.status_code(),
242            Error::TablesRecordBatch { .. } => StatusCode::Unexpected,
243            Error::ColumnExists { .. } => StatusCode::TableColumnExists,
244            Error::SqlCommon { source, .. } => source.status_code(),
245            Error::SchemaBuild { source, .. } | Error::SetFulltextOptions { source, .. } => {
246                source.status_code()
247            }
248            Error::TableOperation { source } => source.status_code(),
249            Error::InvalidColumnOption { .. } => StatusCode::InvalidArguments,
250            Error::ColumnNotExists { .. } => StatusCode::TableColumnNotFound,
251            Error::Unsupported { .. } => StatusCode::Unsupported,
252            Error::PhysicalTableOption { .. }
253            | Error::ParseTableOption { .. }
254            | Error::ConflictingTableOptions { .. } => StatusCode::InvalidArguments,
255            Error::MissingTimeIndexColumn { .. } => StatusCode::IllegalState,
256            Error::SetSkippingOptions { .. }
257            | Error::UnsetSkippingOptions { .. }
258            | Error::InvalidTableName { .. } => StatusCode::InvalidArguments,
259        }
260    }
261
262    fn as_any(&self) -> &dyn Any {
263        self
264    }
265}
266
267impl From<Error> for DataFusionError {
268    fn from(e: Error) -> DataFusionError {
269        DataFusionError::External(Box::new(e))
270    }
271}