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::DataFusionError;
21use datatypes::prelude::{ConcreteDataType, Value};
22use snafu::{Location, Snafu};
23use sqlparser::parser::ParserError;
24
25use crate::parsers::error::TQLError;
26
27pub type Result<T> = std::result::Result<T, Error>;
28
29#[derive(Snafu)]
33#[snafu(visibility(pub))]
34#[stack_trace_debug]
35pub enum Error {
36 #[snafu(display("SQL statement is not supported, keyword: {}", keyword))]
37 Unsupported {
38 keyword: String,
39 #[snafu(implicit)]
40 location: Location,
41 },
42
43 #[snafu(display(
44 "Unexpected token while parsing SQL statement, expected: '{}', found: {}",
45 expected,
46 actual,
47 ))]
48 Unexpected {
49 expected: String,
50 actual: String,
51 #[snafu(source)]
52 error: ParserError,
53 #[snafu(implicit)]
54 location: Location,
55 },
56
57 #[snafu(display("Invalid SQL syntax"))]
59 Syntax {
60 #[snafu(source)]
61 error: ParserError,
62 #[snafu(implicit)]
63 location: Location,
64 },
65
66 #[snafu(display("Invalid TQL syntax"))]
68 TQLSyntax {
69 #[snafu(source)]
70 error: TQLError,
71 #[snafu(implicit)]
72 location: Location,
73 },
74
75 #[snafu(display("Missing time index constraint"))]
76 MissingTimeIndex {},
77
78 #[snafu(display("Invalid time index: {}", msg))]
79 InvalidTimeIndex {
80 msg: String,
81 #[snafu(implicit)]
82 location: Location,
83 },
84
85 #[snafu(display("Invalid SQL, error: {}", msg))]
86 InvalidSql {
87 msg: String,
88 #[snafu(implicit)]
89 location: Location,
90 },
91
92 #[snafu(display(
93 "Unexpected token while parsing SQL statement, expected: '{}', found: {}",
94 expected,
95 actual,
96 ))]
97 UnexpectedToken {
98 expected: String,
99 actual: String,
100 #[snafu(implicit)]
101 location: Location,
102 },
103
104 #[snafu(display("Invalid column option, column name: {}, error: {}", name, msg))]
105 InvalidColumnOption {
106 name: String,
107 msg: String,
108 #[snafu(implicit)]
109 location: Location,
110 },
111
112 #[snafu(display("SQL data type not supported yet: {:?}", t))]
113 SqlTypeNotSupported {
114 t: crate::ast::DataType,
115 #[snafu(implicit)]
116 location: Location,
117 },
118 #[snafu(display("ConcreteDataType not supported yet: {:?}", t))]
119 ConcreteTypeNotSupported {
120 t: ConcreteDataType,
121 #[snafu(implicit)]
122 location: Location,
123 },
124
125 #[snafu(display("Failed to parse value: {}", msg))]
126 ParseSqlValue {
127 msg: String,
128 #[snafu(implicit)]
129 location: Location,
130 },
131
132 #[snafu(display("Invalid database name: {}", name))]
133 InvalidDatabaseName {
134 name: String,
135 #[snafu(implicit)]
136 location: Location,
137 },
138
139 #[snafu(display("Invalid interval provided: {}", reason))]
140 InvalidInterval {
141 reason: String,
142 #[snafu(implicit)]
143 location: Location,
144 },
145
146 #[snafu(display("Unrecognized database option key: {}", key))]
147 InvalidDatabaseOption {
148 key: String,
149 #[snafu(implicit)]
150 location: Location,
151 },
152
153 #[snafu(display("Invalid database option value for {}: {}, {}", key, value, reason))]
154 InvalidDatabaseOptionValue {
155 key: String,
156 value: String,
157 reason: String,
158 #[snafu(implicit)]
159 location: Location,
160 },
161
162 #[snafu(display("Invalid table name: {}", name))]
163 InvalidTableName {
164 name: String,
165 #[snafu(implicit)]
166 location: Location,
167 },
168
169 #[snafu(display("Invalid flow name: {}", name))]
170 InvalidFlowName {
171 name: String,
172 #[snafu(implicit)]
173 location: Location,
174 },
175
176 #[cfg(feature = "enterprise")]
177 #[snafu(display("Invalid trigger name: {}", name))]
178 InvalidTriggerName {
179 name: String,
180 #[snafu(implicit)]
181 location: Location,
182 },
183
184 #[snafu(display("Invalid flow query: {}", reason))]
185 InvalidFlowQuery {
186 reason: String,
187 #[snafu(implicit)]
188 location: Location,
189 },
190
191 #[snafu(display("Invalid default constraint, column: {}", column))]
192 InvalidDefault {
193 column: String,
194 #[snafu(implicit)]
195 location: Location,
196 source: datatypes::error::Error,
197 },
198
199 #[snafu(display("Unrecognized table option key: {}", key))]
200 InvalidTableOption {
201 key: String,
202 #[snafu(implicit)]
203 location: Location,
204 },
205
206 #[snafu(display("Invalid expr as option value, error: {error}"))]
207 InvalidExprAsOptionValue {
208 error: String,
209 #[snafu(implicit)]
210 location: Location,
211 },
212
213 #[snafu(display("Failed to convert data type to gRPC data type defined in proto"))]
214 ConvertToGrpcDataType {
215 #[snafu(implicit)]
216 location: Location,
217 source: api::error::Error,
218 },
219
220 #[snafu(display("Unable to convert statement {} to DataFusion statement", statement))]
221 ConvertToDfStatement {
222 statement: String,
223 #[snafu(implicit)]
224 location: Location,
225 },
226
227 #[snafu(display("Unable to convert value {} to sql value", value))]
228 ConvertValue {
229 value: Value,
230 #[snafu(implicit)]
231 location: Location,
232 },
233
234 #[snafu(display("Failed to convert to logical TQL expression"))]
235 ConvertToLogicalExpression {
236 #[snafu(source)]
237 error: DataFusionError,
238 #[snafu(implicit)]
239 location: Location,
240 },
241
242 #[snafu(display("Failed to simplify TQL expression"))]
243 Simplification {
244 #[snafu(source)]
245 error: DataFusionError,
246 #[snafu(implicit)]
247 location: Location,
248 },
249
250 #[snafu(display(
251 "Permission denied while operating catalog {} from current catalog {}",
252 target,
253 current
254 ))]
255 PermissionDenied {
256 target: String,
257 current: String,
258 #[snafu(implicit)]
259 location: Location,
260 },
261
262 #[snafu(display("Failed to set fulltext option"))]
263 SetFulltextOption {
264 source: datatypes::error::Error,
265 #[snafu(implicit)]
266 location: Location,
267 },
268
269 #[snafu(display("Failed to set SKIPPING index option"))]
270 SetSkippingIndexOption {
271 source: datatypes::error::Error,
272 #[snafu(implicit)]
273 location: Location,
274 },
275
276 #[snafu(display("Failed to set COMPRESSION option"))]
277 SetCompressionOption {
278 source: datatypes::error::Error,
279 #[snafu(implicit)]
280 location: Location,
281 },
282
283 #[snafu(display(
284 "Invalid partition number: {}, should be in range [2, 65536]",
285 partition_num
286 ))]
287 InvalidPartitionNumber {
288 partition_num: u32,
289 #[snafu(implicit)]
290 location: Location,
291 },
292
293 #[cfg(feature = "enterprise")]
294 #[snafu(display("Missing `{}` clause", name))]
295 MissingClause {
296 name: String,
297 #[snafu(implicit)]
298 location: Location,
299 },
300
301 #[cfg(feature = "enterprise")]
302 #[snafu(display("Unrecognized trigger webhook option key: {}", key))]
303 InvalidTriggerWebhookOption {
304 key: String,
305 #[snafu(implicit)]
306 location: Location,
307 },
308
309 #[cfg(feature = "enterprise")]
310 #[snafu(display("Must specify at least one notify channel"))]
311 MissingNotifyChannel {
312 #[snafu(implicit)]
313 location: Location,
314 },
315
316 #[snafu(display("Sql common error"))]
317 SqlCommon {
318 source: common_sql::error::Error,
319 #[snafu(implicit)]
320 location: Location,
321 },
322
323 #[cfg(feature = "enterprise")]
324 #[snafu(display("Duplicate clauses `{}` in a statement", clause))]
325 DuplicateClause {
326 clause: String,
327 #[snafu(implicit)]
328 location: Location,
329 },
330
331 #[snafu(transparent)]
332 Datatypes {
333 #[snafu(implicit)]
334 location: Location,
335 source: datatypes::error::Error,
336 },
337}
338
339impl ErrorExt for Error {
340 fn status_code(&self) -> StatusCode {
341 use Error::*;
342
343 match self {
344 Unsupported { .. } => StatusCode::Unsupported,
345 Unexpected { .. }
346 | Syntax { .. }
347 | TQLSyntax { .. }
348 | MissingTimeIndex { .. }
349 | InvalidTimeIndex { .. }
350 | InvalidSql { .. }
351 | ParseSqlValue { .. }
352 | SqlTypeNotSupported { .. }
353 | ConcreteTypeNotSupported { .. }
354 | UnexpectedToken { .. }
355 | InvalidDefault { .. } => StatusCode::InvalidSyntax,
356
357 #[cfg(feature = "enterprise")]
358 MissingClause { .. } | MissingNotifyChannel { .. } | DuplicateClause { .. } => {
359 StatusCode::InvalidSyntax
360 }
361
362 InvalidColumnOption { .. }
363 | InvalidExprAsOptionValue { .. }
364 | InvalidDatabaseName { .. }
365 | InvalidDatabaseOption { .. }
366 | InvalidDatabaseOptionValue { .. }
367 | InvalidTableName { .. }
368 | InvalidFlowName { .. }
369 | InvalidFlowQuery { .. }
370 | InvalidTableOption { .. }
371 | ConvertToLogicalExpression { .. }
372 | Simplification { .. }
373 | InvalidInterval { .. }
374 | InvalidPartitionNumber { .. } => StatusCode::InvalidArguments,
375
376 #[cfg(feature = "enterprise")]
377 InvalidTriggerName { .. } => StatusCode::InvalidArguments,
378
379 #[cfg(feature = "enterprise")]
380 InvalidTriggerWebhookOption { .. } => StatusCode::InvalidArguments,
381
382 Datatypes { source, .. } => source.status_code(),
383
384 ConvertToGrpcDataType { source, .. } => source.status_code(),
385 SqlCommon { source, .. } => source.status_code(),
386 ConvertToDfStatement { .. } => StatusCode::Internal,
387 ConvertValue { .. } => StatusCode::Unsupported,
388
389 PermissionDenied { .. } => StatusCode::PermissionDenied,
390 SetFulltextOption { .. } | SetSkippingIndexOption { .. } => StatusCode::Unexpected,
391 SetCompressionOption { source, .. } => source.status_code(),
392 }
393 }
394
395 fn as_any(&self) -> &dyn Any {
396 self
397 }
398}