1use 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#[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("Unsupported operation: {}", operation))]
125 Unsupported { operation: String },
126
127 #[snafu(display(
128 "Cannot set {} on a logical table; configure it on the physical table",
129 key
130 ))]
131 PhysicalTableOption {
132 key: String,
133 #[snafu(implicit)]
134 location: Location,
135 },
136
137 #[snafu(display("Failed to parse table option, key: {}, value: {}", key, value))]
138 ParseTableOption {
139 key: String,
140 value: String,
141 #[snafu(implicit)]
142 location: Location,
143 },
144
145 #[snafu(display(
146 "Conflicting table options: {}={} and {}={}",
147 first_key,
148 first_value,
149 second_key,
150 second_value
151 ))]
152 ConflictingTableOptions {
153 first_key: String,
154 first_value: String,
155 second_key: String,
156 second_value: String,
157 #[snafu(implicit)]
158 location: Location,
159 },
160
161 #[snafu(display("Invalid alter table({}) request: {}", table, err))]
162 InvalidAlterRequest {
163 table: String,
164 #[snafu(implicit)]
165 location: Location,
166 err: String,
167 },
168
169 #[snafu(display("Missing time index column in table: {}", table_name))]
170 MissingTimeIndexColumn {
171 table_name: String,
172 #[snafu(implicit)]
173 location: Location,
174 },
175
176 #[snafu(display("Invalid column option, column name: {}, error: {}", column_name, msg))]
177 InvalidColumnOption {
178 column_name: String,
179 msg: String,
180 #[snafu(implicit)]
181 location: Location,
182 },
183
184 #[snafu(display("Failed to set fulltext options for column {}", column_name))]
185 SetFulltextOptions {
186 column_name: String,
187 source: datatypes::Error,
188 #[snafu(implicit)]
189 location: Location,
190 },
191
192 #[snafu(display("Failed to set skipping index options for column {}", column_name))]
193 SetSkippingOptions {
194 column_name: String,
195 source: datatypes::Error,
196 #[snafu(implicit)]
197 location: Location,
198 },
199
200 #[snafu(display("Failed to unset skipping index options for column {}", column_name))]
201 UnsetSkippingOptions {
202 column_name: String,
203 source: datatypes::Error,
204 #[snafu(implicit)]
205 location: Location,
206 },
207
208 #[snafu(display("Invalid table name: '{s}'"))]
209 InvalidTableName { s: String },
210
211 #[snafu(display("Failed to cast default value, reason: {}", reason))]
212 CastDefaultValue {
213 reason: String,
214 source: datatypes::Error,
215 #[snafu(implicit)]
216 location: Location,
217 },
218}
219
220impl ErrorExt for Error {
221 fn status_code(&self) -> StatusCode {
222 match self {
223 Error::Datafusion { error, .. } => datafusion_status_code::<Self>(error, None),
224 Error::SchemaConversion { .. } | Error::TableProjection { .. } => {
225 StatusCode::EngineExecuteQuery
226 }
227 Error::RemoveColumnInIndex { .. }
228 | Error::RemovePartitionColumn { .. }
229 | Error::BuildColumnDescriptor { .. }
230 | Error::InvalidAlterRequest { .. } => StatusCode::InvalidArguments,
231 Error::CastDefaultValue { source, .. } => source.status_code(),
232 Error::TablesRecordBatch { .. } => StatusCode::Unexpected,
233 Error::ColumnExists { .. } => StatusCode::TableColumnExists,
234 Error::SchemaBuild { source, .. } | Error::SetFulltextOptions { source, .. } => {
235 source.status_code()
236 }
237 Error::InvalidColumnOption { .. } => StatusCode::InvalidArguments,
238 Error::ColumnNotExists { .. } => StatusCode::TableColumnNotFound,
239 Error::Unsupported { .. } => StatusCode::Unsupported,
240 Error::PhysicalTableOption { .. }
241 | Error::ParseTableOption { .. }
242 | Error::ConflictingTableOptions { .. } => StatusCode::InvalidArguments,
243 Error::MissingTimeIndexColumn { .. } => StatusCode::IllegalState,
244 Error::SetSkippingOptions { .. }
245 | Error::UnsetSkippingOptions { .. }
246 | Error::InvalidTableName { .. } => StatusCode::InvalidArguments,
247 }
248 }
249
250 fn as_any(&self) -> &dyn Any {
251 self
252 }
253}
254
255impl From<Error> for DataFusionError {
256 fn from(e: Error) -> DataFusionError {
257 DataFusionError::External(Box::new(e))
258 }
259}