Skip to main content

operator/
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_datasource::file_format::Format;
18use common_error::define_into_tonic_status;
19use common_error::ext::{BoxedError, ErrorExt, RetryHint};
20use common_error::status_code::StatusCode;
21use common_macro::stack_trace_debug;
22use common_query::error::Error as QueryResult;
23use datafusion::parquet;
24use datafusion_common::DataFusionError;
25use datatypes::arrow::error::ArrowError;
26use object_store::error::retry_hint_from_opendal_error;
27use snafu::{Location, Snafu};
28use table::metadata::TableType;
29
30#[derive(Snafu)]
31#[snafu(visibility(pub))]
32#[stack_trace_debug]
33pub enum Error {
34    #[snafu(display("Table already exists: `{}`", table))]
35    TableAlreadyExists {
36        table: String,
37        #[snafu(implicit)]
38        location: Location,
39    },
40
41    #[snafu(display("Failed to cast result: `{}`", source))]
42    Cast {
43        #[snafu(source)]
44        source: QueryResult,
45        #[snafu(implicit)]
46        location: Location,
47    },
48
49    #[snafu(display("View already exists: `{name}`"))]
50    ViewAlreadyExists {
51        name: String,
52        #[snafu(implicit)]
53        location: Location,
54    },
55
56    #[snafu(display("Failed to build admin function args: {msg}"))]
57    BuildAdminFunctionArgs { msg: String },
58
59    #[snafu(display("Failed to execute admin function {msg}"))]
60    ExecuteAdminFunction {
61        msg: String,
62        #[snafu(source)]
63        error: DataFusionError,
64        #[snafu(implicit)]
65        location: Location,
66    },
67
68    #[snafu(display("Admin function execution was cancelled"))]
69    AdminFunctionCancelled,
70
71    #[snafu(display("Expected {expected} args, but actual {actual}"))]
72    FunctionArityMismatch { expected: usize, actual: usize },
73
74    #[snafu(display("Failed to invalidate table cache"))]
75    InvalidateTableCache {
76        #[snafu(implicit)]
77        location: Location,
78        source: common_meta::error::Error,
79    },
80
81    #[snafu(display("Failed to execute ddl"))]
82    ExecuteDdl {
83        #[snafu(implicit)]
84        location: Location,
85        source: common_meta::error::Error,
86    },
87
88    #[snafu(display("Unexpected, violated: {}", violated))]
89    Unexpected {
90        violated: String,
91        #[snafu(implicit)]
92        location: Location,
93    },
94
95    #[snafu(display("external error"))]
96    External {
97        #[snafu(implicit)]
98        location: Location,
99        source: BoxedError,
100    },
101
102    #[snafu(display("Failed to insert data"))]
103    RequestInserts {
104        #[snafu(implicit)]
105        location: Location,
106        source: common_meta::error::Error,
107    },
108
109    #[snafu(display("Failed to delete data"))]
110    RequestDeletes {
111        #[snafu(implicit)]
112        location: Location,
113        source: common_meta::error::Error,
114    },
115
116    #[snafu(display("Failed to send request to region"))]
117    RequestRegion {
118        #[snafu(implicit)]
119        location: Location,
120        source: common_meta::error::Error,
121    },
122
123    #[snafu(display("Unsupported region request"))]
124    UnsupportedRegionRequest {
125        #[snafu(implicit)]
126        location: Location,
127    },
128
129    #[snafu(display("Failed to parse SQL"))]
130    ParseSql {
131        #[snafu(implicit)]
132        location: Location,
133        source: sql::error::Error,
134    },
135
136    #[snafu(display("Failed to convert identifier: {}", ident))]
137    ConvertIdentifier {
138        ident: String,
139        #[snafu(implicit)]
140        location: Location,
141        #[snafu(source)]
142        error: datafusion::error::DataFusionError,
143    },
144
145    #[snafu(display("Failed to extract table names"))]
146    ExtractTableNames {
147        #[snafu(implicit)]
148        location: Location,
149        source: query::error::Error,
150    },
151
152    #[snafu(display("Column datatype error"))]
153    ColumnDataType {
154        #[snafu(implicit)]
155        location: Location,
156        source: api::error::Error,
157    },
158
159    #[snafu(display("Invalid column proto definition, column: {}", column))]
160    InvalidColumnDef {
161        column: String,
162        #[snafu(implicit)]
163        location: Location,
164        source: api::error::Error,
165    },
166
167    #[snafu(display("Invalid statement to create view"))]
168    InvalidViewStmt {
169        #[snafu(implicit)]
170        location: Location,
171    },
172
173    #[snafu(display("Expect {expected} columns for view {view_name}, but found {actual}"))]
174    ViewColumnsMismatch {
175        view_name: String,
176        expected: usize,
177        actual: usize,
178    },
179
180    #[snafu(display("Invalid view \"{view_name}\": {msg}"))]
181    InvalidView {
182        msg: String,
183        view_name: String,
184        #[snafu(implicit)]
185        location: Location,
186    },
187
188    #[snafu(display("Failed to convert column default constraint, column: {}", column_name))]
189    ConvertColumnDefaultConstraint {
190        column_name: String,
191        #[snafu(implicit)]
192        location: Location,
193        source: datatypes::error::Error,
194    },
195
196    #[snafu(display("Failed to convert datafusion schema"))]
197    ConvertSchema {
198        source: datatypes::error::Error,
199        #[snafu(implicit)]
200        location: Location,
201    },
202
203    #[snafu(display("Failed to convert expr to struct"))]
204    InvalidExpr {
205        #[snafu(implicit)]
206        location: Location,
207        source: common_meta::error::Error,
208    },
209
210    #[snafu(display("Invalid partition"))]
211    InvalidPartition {
212        #[snafu(implicit)]
213        location: Location,
214        source: partition::error::Error,
215    },
216
217    #[snafu(display("Invalid SQL, error: {}", err_msg))]
218    InvalidSql {
219        err_msg: String,
220        #[snafu(implicit)]
221        location: Location,
222    },
223
224    #[snafu(display("Invalid InsertRequest, reason: {}", reason))]
225    InvalidInsertRequest {
226        reason: String,
227        #[snafu(implicit)]
228        location: Location,
229    },
230
231    #[snafu(display("Invalid DeleteRequest, reason: {}", reason))]
232    InvalidDeleteRequest {
233        reason: String,
234        #[snafu(implicit)]
235        location: Location,
236    },
237
238    #[snafu(display("Table not found: {}", table_name))]
239    TableNotFound { table_name: String },
240
241    #[snafu(display("Admin function not found: {}", name))]
242    AdminFunctionNotFound { name: String },
243
244    #[snafu(display("Flow not found: {}", flow_name))]
245    FlowNotFound { flow_name: String },
246
247    #[snafu(display("Failed to join task"))]
248    JoinTask {
249        #[snafu(source)]
250        error: common_runtime::JoinError,
251        #[snafu(implicit)]
252        location: Location,
253    },
254
255    #[snafu(display("General catalog error"))]
256    Catalog {
257        #[snafu(implicit)]
258        location: Location,
259        source: catalog::error::Error,
260    },
261
262    #[snafu(display("Failed to find view info for: {}", view_name))]
263    FindViewInfo {
264        view_name: String,
265        #[snafu(implicit)]
266        location: Location,
267        source: common_meta::error::Error,
268    },
269
270    #[snafu(display("View info not found: {}", view_name))]
271    ViewInfoNotFound {
272        view_name: String,
273        #[snafu(implicit)]
274        location: Location,
275    },
276
277    #[snafu(display("View not found: {}", view_name))]
278    ViewNotFound {
279        view_name: String,
280        #[snafu(implicit)]
281        location: Location,
282    },
283
284    #[snafu(display("Failed to find table partition rule for table {}", table_name))]
285    FindTablePartitionRule {
286        table_name: String,
287        #[snafu(implicit)]
288        location: Location,
289        source: partition::error::Error,
290    },
291
292    #[snafu(display("Failed to split insert request"))]
293    SplitInsert {
294        source: partition::error::Error,
295        #[snafu(implicit)]
296        location: Location,
297    },
298
299    #[snafu(display("Failed to split delete request"))]
300    SplitDelete {
301        source: partition::error::Error,
302        #[snafu(implicit)]
303        location: Location,
304    },
305
306    #[snafu(display("Failed to find leader for region"))]
307    FindRegionLeader {
308        source: partition::error::Error,
309        #[snafu(implicit)]
310        location: Location,
311    },
312
313    #[snafu(display("Failed to build CreateExpr on insertion"))]
314    BuildCreateExprOnInsertion {
315        #[snafu(implicit)]
316        location: Location,
317        source: common_grpc_expr::error::Error,
318    },
319
320    #[snafu(display("Failed to find schema, schema info: {}", schema_info))]
321    SchemaNotFound {
322        schema_info: String,
323        #[snafu(implicit)]
324        location: Location,
325    },
326
327    #[snafu(display("Schema {} already exists", name))]
328    SchemaExists {
329        name: String,
330        #[snafu(implicit)]
331        location: Location,
332    },
333
334    #[snafu(display("Schema `{name}` is in use"))]
335    SchemaInUse {
336        name: String,
337        #[snafu(implicit)]
338        location: Location,
339    },
340
341    #[snafu(display("Schema `{name}` is read-only"))]
342    SchemaReadOnly {
343        name: String,
344        #[snafu(implicit)]
345        location: Location,
346    },
347
348    #[snafu(display("Table `{name}` is read-only"))]
349    TableReadOnly {
350        name: String,
351        #[snafu(implicit)]
352        location: Location,
353    },
354
355    #[snafu(display(
356        "The definition of table `{name}` is managed by GreptimeDB; it cannot be created or altered (DROP recreates it on the next write)"
357    ))]
358    TableDdlReserved {
359        name: String,
360        #[snafu(implicit)]
361        location: Location,
362    },
363
364    #[snafu(display("Table occurs error"))]
365    Table {
366        #[snafu(implicit)]
367        location: Location,
368        source: table::error::Error,
369    },
370
371    #[snafu(display("Cannot find column by name: {}", msg))]
372    ColumnNotFound {
373        msg: String,
374        #[snafu(implicit)]
375        location: Location,
376    },
377
378    #[snafu(display("Failed to execute statement"))]
379    ExecuteStatement {
380        #[snafu(implicit)]
381        location: Location,
382        source: query::error::Error,
383    },
384
385    #[snafu(display("Failed to plan statement"))]
386    PlanStatement {
387        #[snafu(implicit)]
388        location: Location,
389        source: query::error::Error,
390    },
391
392    #[snafu(display("Failed to parse query"))]
393    ParseQuery {
394        #[snafu(implicit)]
395        location: Location,
396        source: query::error::Error,
397    },
398
399    #[snafu(display("Failed to execute logical plan"))]
400    ExecLogicalPlan {
401        #[snafu(implicit)]
402        location: Location,
403        source: query::error::Error,
404    },
405
406    #[snafu(display("Failed to build DataFusion logical plan"))]
407    BuildDfLogicalPlan {
408        #[snafu(source)]
409        error: datafusion_common::DataFusionError,
410        #[snafu(implicit)]
411        location: Location,
412    },
413
414    #[snafu(display("Failed to convert AlterExpr to AlterRequest"))]
415    AlterExprToRequest {
416        #[snafu(implicit)]
417        location: Location,
418        source: common_grpc_expr::error::Error,
419    },
420
421    #[snafu(display("Failed to build table meta for table: {}", table_name))]
422    BuildTableMeta {
423        table_name: String,
424        #[snafu(source)]
425        error: table::metadata::TableMetaBuilderError,
426        #[snafu(implicit)]
427        location: Location,
428    },
429
430    #[snafu(display("Not supported: {}", feat))]
431    NotSupported { feat: String },
432
433    #[snafu(display("Failed to find new columns on insertion"))]
434    FindNewColumnsOnInsertion {
435        #[snafu(implicit)]
436        location: Location,
437        source: common_grpc_expr::error::Error,
438    },
439
440    #[snafu(display("Failed to convert into vectors"))]
441    IntoVectors {
442        #[snafu(implicit)]
443        location: Location,
444        source: datatypes::error::Error,
445    },
446
447    #[snafu(display("Failed to describe schema for given statement"))]
448    DescribeStatement {
449        #[snafu(implicit)]
450        location: Location,
451        source: query::error::Error,
452    },
453
454    #[snafu(display("Illegal primary keys definition: {}", msg))]
455    IllegalPrimaryKeysDef {
456        msg: String,
457        #[snafu(implicit)]
458        location: Location,
459    },
460
461    #[snafu(display("Unrecognized table option"))]
462    UnrecognizedTableOption {
463        #[snafu(implicit)]
464        location: Location,
465        source: table::error::Error,
466    },
467
468    #[snafu(display("Missing time index column"))]
469    MissingTimeIndexColumn {
470        #[snafu(implicit)]
471        location: Location,
472        source: table::error::Error,
473    },
474
475    #[snafu(display("Failed to build regex"))]
476    BuildRegex {
477        #[snafu(implicit)]
478        location: Location,
479        #[snafu(source)]
480        error: regex::Error,
481    },
482
483    #[snafu(display("Failed to insert value into table: {}", table_name))]
484    Insert {
485        table_name: String,
486        #[snafu(implicit)]
487        location: Location,
488        source: table::error::Error,
489    },
490
491    #[snafu(display("Unsupported format: {:?}", format))]
492    UnsupportedFormat {
493        #[snafu(implicit)]
494        location: Location,
495        format: Format,
496    },
497
498    #[snafu(display("Failed to parse file format"))]
499    ParseFileFormat {
500        #[snafu(implicit)]
501        location: Location,
502        source: common_datasource::error::Error,
503    },
504
505    #[snafu(display("Failed to build data source backend"))]
506    BuildBackend {
507        #[snafu(implicit)]
508        location: Location,
509        source: common_datasource::error::Error,
510    },
511
512    #[snafu(display("Failed to list objects"))]
513    ListObjects {
514        #[snafu(implicit)]
515        location: Location,
516        source: common_datasource::error::Error,
517    },
518
519    #[snafu(display("Failed to infer schema from path: {}", path))]
520    InferSchema {
521        path: String,
522        #[snafu(implicit)]
523        location: Location,
524        source: common_datasource::error::Error,
525    },
526
527    #[snafu(display("Failed to write stream to path: {}", path))]
528    WriteStreamToFile {
529        path: String,
530        #[snafu(implicit)]
531        location: Location,
532        source: common_datasource::error::Error,
533    },
534
535    #[snafu(display("Failed to read object in path: {}", path))]
536    ReadObject {
537        path: String,
538        #[snafu(implicit)]
539        location: Location,
540        #[snafu(source)]
541        error: object_store::Error,
542    },
543
544    #[snafu(display("Failed to read record batch"))]
545    ReadDfRecordBatch {
546        #[snafu(source)]
547        error: datafusion::error::DataFusionError,
548        #[snafu(implicit)]
549        location: Location,
550    },
551
552    #[snafu(display("Failed to read parquet file metadata"))]
553    ReadParquetMetadata {
554        #[snafu(source)]
555        error: parquet::errors::ParquetError,
556        #[snafu(implicit)]
557        location: Location,
558    },
559
560    #[snafu(display("Failed to build record batch"))]
561    BuildRecordBatch {
562        #[snafu(implicit)]
563        location: Location,
564        source: common_recordbatch::error::Error,
565    },
566
567    #[snafu(display("Failed to read orc schema"))]
568    ReadOrc {
569        source: common_datasource::error::Error,
570        #[snafu(implicit)]
571        location: Location,
572    },
573
574    #[snafu(display("Failed to build parquet record batch stream"))]
575    BuildParquetRecordBatchStream {
576        #[snafu(implicit)]
577        location: Location,
578        #[snafu(source)]
579        error: parquet::errors::ParquetError,
580    },
581
582    #[snafu(display("Failed to build file stream"))]
583    BuildFileStream {
584        #[snafu(implicit)]
585        location: Location,
586        #[snafu(source)]
587        error: common_datasource::error::Error,
588    },
589
590    #[snafu(display(
591        "Schema datatypes not match at index {}, expected table schema: {}, actual file schema: {}",
592        index,
593        table_schema,
594        file_schema
595    ))]
596    InvalidSchema {
597        index: usize,
598        table_schema: String,
599        file_schema: String,
600        #[snafu(implicit)]
601        location: Location,
602    },
603
604    #[snafu(display(
605        "CSV header mismatch in path: {}, unknown columns: {:?}, missing columns: {:?}, duplicate columns: {:?}",
606        path,
607        unknown_columns,
608        missing_columns,
609        duplicate_columns
610    ))]
611    CsvHeaderMismatch {
612        path: String,
613        unknown_columns: Vec<String>,
614        missing_columns: Vec<String>,
615        duplicate_columns: Vec<String>,
616        #[snafu(implicit)]
617        location: Location,
618    },
619
620    #[snafu(display("Failed to project schema"))]
621    ProjectSchema {
622        #[snafu(source)]
623        error: ArrowError,
624        #[snafu(implicit)]
625        location: Location,
626    },
627
628    #[snafu(display("Failed to encode object into json"))]
629    EncodeJson {
630        #[snafu(source)]
631        error: serde_json::error::Error,
632        #[snafu(implicit)]
633        location: Location,
634    },
635
636    #[snafu(display("Invalid COPY parameter, key: {}, value: {}", key, value))]
637    InvalidCopyParameter {
638        key: String,
639        value: String,
640        #[snafu(implicit)]
641        location: Location,
642    },
643
644    #[snafu(display("Invalid COPY DATABASE location, must end with '/': {}", value))]
645    InvalidCopyDatabasePath {
646        value: String,
647        #[snafu(implicit)]
648        location: Location,
649    },
650
651    #[snafu(display("Table metadata manager error"))]
652    TableMetadataManager {
653        source: common_meta::error::Error,
654        #[snafu(implicit)]
655        location: Location,
656    },
657
658    #[snafu(display("Missing insert body"))]
659    MissingInsertBody {
660        source: sql::error::Error,
661        #[snafu(implicit)]
662        location: Location,
663    },
664
665    #[snafu(display("Failed to parse sql value"))]
666    ParseSqlValue {
667        source: sql::error::Error,
668        #[snafu(implicit)]
669        location: Location,
670    },
671
672    #[snafu(display("Failed to build default value, column: {}", column))]
673    ColumnDefaultValue {
674        column: String,
675        #[snafu(implicit)]
676        location: Location,
677        source: datatypes::error::Error,
678    },
679
680    #[snafu(display(
681        "No valid default value can be built automatically, column: {}",
682        column,
683    ))]
684    ColumnNoneDefaultValue {
685        column: String,
686        #[snafu(implicit)]
687        location: Location,
688    },
689
690    #[snafu(display("Failed to prepare file table"))]
691    PrepareFileTable {
692        #[snafu(implicit)]
693        location: Location,
694        source: query::error::Error,
695    },
696
697    #[snafu(display("Failed to infer file table schema"))]
698    InferFileTableSchema {
699        #[snafu(implicit)]
700        location: Location,
701        source: query::error::Error,
702    },
703
704    #[snafu(display("The schema of the file table is incompatible with the table schema"))]
705    SchemaIncompatible {
706        #[snafu(implicit)]
707        location: Location,
708        source: query::error::Error,
709    },
710
711    #[snafu(display("Invalid table name: {}", table_name))]
712    InvalidTableName {
713        table_name: String,
714        #[snafu(implicit)]
715        location: Location,
716    },
717
718    #[snafu(display("Invalid view name: {name}"))]
719    InvalidViewName {
720        name: String,
721        #[snafu(implicit)]
722        location: Location,
723    },
724
725    #[snafu(display("Invalid flow name: {name}"))]
726    InvalidFlowName {
727        name: String,
728        #[snafu(implicit)]
729        location: Location,
730    },
731
732    #[cfg(feature = "enterprise")]
733    #[snafu(display("Invalid trigger name: {name}"))]
734    InvalidTriggerName {
735        name: String,
736        #[snafu(implicit)]
737        location: Location,
738    },
739
740    #[snafu(display("Empty {} expr", name))]
741    EmptyDdlExpr {
742        name: String,
743        #[snafu(implicit)]
744        location: Location,
745    },
746
747    #[snafu(display("Failed to create logical tables: {}", reason))]
748    CreateLogicalTables {
749        reason: String,
750        #[snafu(implicit)]
751        location: Location,
752    },
753
754    #[snafu(display("Invalid partition rule: {}", reason))]
755    InvalidPartitionRule {
756        reason: String,
757        #[snafu(implicit)]
758        location: Location,
759    },
760
761    #[snafu(display("Failed to serialize partition expression"))]
762    SerializePartitionExpr {
763        #[snafu(implicit)]
764        location: Location,
765        source: partition::error::Error,
766    },
767
768    #[snafu(display("Failed to deserialize partition expression"))]
769    DeserializePartitionExpr {
770        #[snafu(source)]
771        source: partition::error::Error,
772        #[snafu(implicit)]
773        location: Location,
774    },
775
776    #[snafu(display("Invalid configuration value."))]
777    InvalidConfigValue {
778        source: session::session_config::Error,
779        #[snafu(implicit)]
780        location: Location,
781    },
782
783    #[snafu(display("Invalid timestamp range, start: `{}`, end: `{}`", start, end))]
784    InvalidTimestampRange {
785        start: String,
786        end: String,
787        #[snafu(implicit)]
788        location: Location,
789    },
790
791    #[snafu(display("Failed to convert between logical plan and substrait plan"))]
792    SubstraitCodec {
793        #[snafu(implicit)]
794        location: Location,
795        source: substrait::error::Error,
796    },
797
798    #[snafu(display(
799        "Show create table only for base table. {} is {}",
800        table_name,
801        table_type
802    ))]
803    ShowCreateTableBaseOnly {
804        table_name: String,
805        table_type: TableType,
806        #[snafu(implicit)]
807        location: Location,
808    },
809    #[snafu(display("Create physical expr error"))]
810    PhysicalExpr {
811        #[snafu(source)]
812        error: common_recordbatch::error::Error,
813        #[snafu(implicit)]
814        location: Location,
815    },
816
817    #[snafu(display("Failed to upgrade catalog manager reference"))]
818    UpgradeCatalogManagerRef {
819        #[snafu(implicit)]
820        location: Location,
821    },
822
823    #[snafu(display("Invalid json text: {}", json))]
824    InvalidJsonFormat {
825        #[snafu(implicit)]
826        location: Location,
827        json: String,
828    },
829
830    #[snafu(display("Cursor {name} is not found"))]
831    CursorNotFound { name: String },
832
833    #[snafu(display("A cursor named {name} already exists"))]
834    CursorExists { name: String },
835
836    #[snafu(display("Column options error"))]
837    ColumnOptions {
838        #[snafu(source)]
839        source: api::error::Error,
840        #[snafu(implicit)]
841        location: Location,
842    },
843
844    #[snafu(display("Failed to create partition rules"))]
845    CreatePartitionRules {
846        #[snafu(source)]
847        source: sql::error::Error,
848        #[snafu(implicit)]
849        location: Location,
850    },
851
852    #[snafu(display("Failed to decode arrow flight data"))]
853    DecodeFlightData {
854        source: common_grpc::error::Error,
855        #[snafu(implicit)]
856        location: Location,
857    },
858
859    #[snafu(display("Failed to perform arrow compute"))]
860    ComputeArrow {
861        #[snafu(source)]
862        error: ArrowError,
863        #[snafu(implicit)]
864        location: Location,
865    },
866
867    #[snafu(display("Invalid time index type: {}", ty))]
868    InvalidTimeIndexType {
869        ty: arrow::datatypes::DataType,
870        #[snafu(implicit)]
871        location: Location,
872    },
873
874    #[snafu(display("Invalid timezone: {}", timezone))]
875    InvalidTimezone {
876        timezone: String,
877        #[snafu(source)]
878        source: common_time::error::Error,
879        #[snafu(implicit)]
880        location: Location,
881    },
882
883    #[snafu(display("Invalid process id: {}", id))]
884    InvalidProcessId { id: String },
885
886    #[snafu(display("ProcessManager is not present, this can be caused by misconfiguration."))]
887    ProcessManagerMissing {
888        #[snafu(implicit)]
889        location: Location,
890    },
891
892    #[snafu(display("Sql common error"))]
893    SqlCommon {
894        source: common_sql::error::Error,
895        #[snafu(implicit)]
896        location: Location,
897    },
898
899    #[snafu(display("Failed to convert partition expression to protobuf"))]
900    PartitionExprToPb {
901        source: partition::error::Error,
902        #[snafu(implicit)]
903        location: Location,
904    },
905
906    #[snafu(display(
907        "{} not supported when transforming to {} format type",
908        format,
909        file_format
910    ))]
911    TimestampFormatNotSupported {
912        file_format: String,
913        format: String,
914        #[snafu(implicit)]
915        location: Location,
916    },
917
918    #[cfg(feature = "enterprise")]
919    #[snafu(display("Too large duration"))]
920    TooLargeDuration {
921        #[snafu(source)]
922        error: prost_types::DurationError,
923        #[snafu(implicit)]
924        location: Location,
925    },
926
927    #[cfg(feature = "enterprise")]
928    #[snafu(display("Not trigger querier is specified"))]
929    MissingTriggerQuerier {
930        #[snafu(implicit)]
931        location: Location,
932    },
933
934    #[cfg(feature = "enterprise")]
935    #[snafu(display("Trigger querier error"))]
936    TriggerQuerier {
937        source: BoxedError,
938        #[snafu(implicit)]
939        location: Location,
940    },
941}
942
943pub type Result<T> = std::result::Result<T, Error>;
944
945impl ErrorExt for Error {
946    fn status_code(&self) -> StatusCode {
947        match self {
948            Error::Cast { source, .. } => source.status_code(),
949            Error::InvalidSql { .. }
950            | Error::InvalidConfigValue { .. }
951            | Error::InvalidInsertRequest { .. }
952            | Error::InvalidDeleteRequest { .. }
953            | Error::IllegalPrimaryKeysDef { .. }
954            | Error::SchemaNotFound { .. }
955            | Error::SchemaExists { .. }
956            | Error::SchemaInUse { .. }
957            | Error::ColumnNotFound { .. }
958            | Error::BuildRegex { .. }
959            | Error::InvalidSchema { .. }
960            | Error::CsvHeaderMismatch { .. }
961            | Error::ProjectSchema { .. }
962            | Error::UnsupportedFormat { .. }
963            | Error::ColumnNoneDefaultValue { .. }
964            | Error::PrepareFileTable { .. }
965            | Error::InferFileTableSchema { .. }
966            | Error::SchemaIncompatible { .. }
967            | Error::ConvertSchema { .. }
968            | Error::UnsupportedRegionRequest { .. }
969            | Error::InvalidTableName { .. }
970            | Error::InvalidViewName { .. }
971            | Error::InvalidFlowName { .. }
972            | Error::InvalidView { .. }
973            | Error::InvalidExpr { .. }
974            | Error::AdminFunctionNotFound { .. }
975            | Error::ViewColumnsMismatch { .. }
976            | Error::InvalidViewStmt { .. }
977            | Error::ConvertIdentifier { .. }
978            | Error::BuildAdminFunctionArgs { .. }
979            | Error::FunctionArityMismatch { .. }
980            | Error::InvalidPartition { .. }
981            | Error::PhysicalExpr { .. }
982            | Error::InvalidJsonFormat { .. }
983            | Error::PartitionExprToPb { .. }
984            | Error::CursorNotFound { .. }
985            | Error::CursorExists { .. }
986            | Error::CreatePartitionRules { .. } => StatusCode::InvalidArguments,
987            Error::TableAlreadyExists { .. } | Error::ViewAlreadyExists { .. } => {
988                StatusCode::TableAlreadyExists
989            }
990            Error::NotSupported { .. }
991            | Error::ShowCreateTableBaseOnly { .. }
992            | Error::SchemaReadOnly { .. }
993            | Error::TableReadOnly { .. }
994            | Error::TableDdlReserved { .. } => StatusCode::Unsupported,
995            Error::TableMetadataManager { source, .. } => source.status_code(),
996            Error::ParseSql { source, .. } => source.status_code(),
997            Error::InvalidateTableCache { source, .. } => source.status_code(),
998            Error::ParseFileFormat { source, .. } | Error::InferSchema { source, .. } => {
999                source.status_code()
1000            }
1001            Error::Table { source, .. } | Error::Insert { source, .. } => source.status_code(),
1002            Error::ConvertColumnDefaultConstraint { source, .. }
1003            | Error::IntoVectors { source, .. } => source.status_code(),
1004            Error::RequestInserts { source, .. } | Error::FindViewInfo { source, .. } => {
1005                source.status_code()
1006            }
1007            Error::RequestRegion { source, .. } => source.status_code(),
1008            Error::RequestDeletes { source, .. } => source.status_code(),
1009            Error::SubstraitCodec { source, .. } => source.status_code(),
1010            Error::ColumnDataType { source, .. } | Error::InvalidColumnDef { source, .. } => {
1011                source.status_code()
1012            }
1013            Error::MissingTimeIndexColumn { source, .. } => source.status_code(),
1014            Error::BuildDfLogicalPlan { .. }
1015            | Error::BuildTableMeta { .. }
1016            | Error::MissingInsertBody { .. } => StatusCode::Internal,
1017            Error::ExecuteAdminFunction { .. }
1018            | Error::EncodeJson { .. }
1019            | Error::DeserializePartitionExpr { .. }
1020            | Error::SerializePartitionExpr { .. } => StatusCode::Unexpected,
1021            Error::AdminFunctionCancelled => StatusCode::Cancelled,
1022            Error::ViewNotFound { .. }
1023            | Error::ViewInfoNotFound { .. }
1024            | Error::TableNotFound { .. } => StatusCode::TableNotFound,
1025            Error::FlowNotFound { .. } => StatusCode::FlowNotFound,
1026            Error::JoinTask { .. } => StatusCode::Internal,
1027            Error::BuildParquetRecordBatchStream { .. }
1028            | Error::BuildFileStream { .. }
1029            | Error::WriteStreamToFile { .. }
1030            | Error::ReadDfRecordBatch { .. }
1031            | Error::Unexpected { .. } => StatusCode::Unexpected,
1032            Error::Catalog { source, .. } => source.status_code(),
1033            Error::BuildCreateExprOnInsertion { source, .. }
1034            | Error::FindNewColumnsOnInsertion { source, .. } => source.status_code(),
1035            Error::ExecuteStatement { source, .. }
1036            | Error::ExtractTableNames { source, .. }
1037            | Error::PlanStatement { source, .. }
1038            | Error::ParseQuery { source, .. }
1039            | Error::ExecLogicalPlan { source, .. }
1040            | Error::DescribeStatement { source, .. } => source.status_code(),
1041            Error::AlterExprToRequest { source, .. } => source.status_code(),
1042            Error::External { source, .. } => source.status_code(),
1043            Error::FindTablePartitionRule { source, .. }
1044            | Error::SplitInsert { source, .. }
1045            | Error::SplitDelete { source, .. }
1046            | Error::FindRegionLeader { source, .. } => source.status_code(),
1047            Error::UnrecognizedTableOption { .. } => StatusCode::InvalidArguments,
1048            Error::ReadObject { .. }
1049            | Error::ReadParquetMetadata { .. }
1050            | Error::ReadOrc { .. } => StatusCode::StorageUnavailable,
1051            Error::ListObjects { source, .. } | Error::BuildBackend { source, .. } => {
1052                source.status_code()
1053            }
1054            Error::ExecuteDdl { source, .. } => source.status_code(),
1055            Error::InvalidCopyParameter { .. } | Error::InvalidCopyDatabasePath { .. } => {
1056                StatusCode::InvalidArguments
1057            }
1058            Error::ColumnDefaultValue { source, .. } => source.status_code(),
1059            Error::EmptyDdlExpr { .. }
1060            | Error::InvalidPartitionRule { .. }
1061            | Error::ParseSqlValue { .. }
1062            | Error::InvalidTimestampRange { .. } => StatusCode::InvalidArguments,
1063            Error::CreateLogicalTables { .. } => StatusCode::Unexpected,
1064            Error::BuildRecordBatch { source, .. } => source.status_code(),
1065            Error::UpgradeCatalogManagerRef { .. } => StatusCode::Internal,
1066            Error::ColumnOptions { source, .. } => source.status_code(),
1067            Error::DecodeFlightData { source, .. } => source.status_code(),
1068            Error::ComputeArrow { .. } => StatusCode::Internal,
1069            Error::InvalidTimeIndexType { .. } | Error::InvalidTimezone { .. } => {
1070                StatusCode::InvalidArguments
1071            }
1072            Error::InvalidProcessId { .. } => StatusCode::InvalidArguments,
1073            Error::ProcessManagerMissing { .. } => StatusCode::Unexpected,
1074            Error::TimestampFormatNotSupported { .. } => StatusCode::InvalidArguments,
1075            Error::SqlCommon { source, .. } => source.status_code(),
1076            #[cfg(feature = "enterprise")]
1077            Error::InvalidTriggerName { .. } => StatusCode::InvalidArguments,
1078            #[cfg(feature = "enterprise")]
1079            Error::TooLargeDuration { .. } => StatusCode::InvalidArguments,
1080            #[cfg(feature = "enterprise")]
1081            Error::MissingTriggerQuerier { .. } => StatusCode::Internal,
1082            #[cfg(feature = "enterprise")]
1083            Error::TriggerQuerier { source, .. } => source.status_code(),
1084        }
1085    }
1086
1087    fn as_any(&self) -> &dyn Any {
1088        self
1089    }
1090
1091    fn retry_hint(&self) -> RetryHint {
1092        match self {
1093            Error::ReadObject { error, .. } => retry_hint_from_opendal_error(error),
1094            Error::ReadParquetMetadata { .. } => RetryHint::Retryable,
1095            Error::InvalidateTableCache { source, .. }
1096            | Error::ExecuteDdl { source, .. }
1097            | Error::RequestInserts { source, .. }
1098            | Error::RequestDeletes { source, .. }
1099            | Error::RequestRegion { source, .. }
1100            | Error::FindViewInfo { source, .. }
1101            | Error::TableMetadataManager { source, .. } => source.retry_hint(),
1102
1103            Error::ParseFileFormat { source, .. }
1104            | Error::InferSchema { source, .. }
1105            | Error::ListObjects { source, .. }
1106            | Error::BuildBackend { source, .. }
1107            | Error::ReadOrc { source, .. } => source.retry_hint(),
1108
1109            Error::ExtractTableNames { source, .. }
1110            | Error::ExecuteStatement { source, .. }
1111            | Error::PlanStatement { source, .. }
1112            | Error::ParseQuery { source, .. }
1113            | Error::ExecLogicalPlan { source, .. }
1114            | Error::DescribeStatement { source, .. } => source.retry_hint(),
1115
1116            Error::FindTablePartitionRule { source, .. }
1117            | Error::SplitInsert { source, .. }
1118            | Error::SplitDelete { source, .. }
1119            | Error::FindRegionLeader { source, .. } => source.retry_hint(),
1120
1121            Error::BuildCreateExprOnInsertion { source, .. }
1122            | Error::FindNewColumnsOnInsertion { source, .. }
1123            | Error::AlterExprToRequest { source, .. } => source.retry_hint(),
1124
1125            Error::ConvertColumnDefaultConstraint { source, .. }
1126            | Error::IntoVectors { source, .. }
1127            | Error::ColumnDefaultValue { source, .. } => source.retry_hint(),
1128
1129            Error::ColumnDataType { source, .. }
1130            | Error::InvalidColumnDef { source, .. }
1131            | Error::ColumnOptions { source, .. } => source.retry_hint(),
1132
1133            Error::Table { source, .. }
1134            | Error::Insert { source, .. }
1135            | Error::MissingTimeIndexColumn { source, .. } => source.retry_hint(),
1136
1137            Error::Cast { source, .. } => source.retry_hint(),
1138            Error::ParseSql { source, .. } => source.retry_hint(),
1139            Error::Catalog { source, .. } => source.retry_hint(),
1140            Error::SubstraitCodec { source, .. } => source.retry_hint(),
1141            Error::External { source, .. } => source.retry_hint(),
1142            Error::BuildRecordBatch { source, .. } => source.retry_hint(),
1143            Error::DecodeFlightData { source, .. } => source.retry_hint(),
1144            Error::SqlCommon { source, .. } => source.retry_hint(),
1145            Error::ConvertSchema { source, .. } => source.retry_hint(),
1146            Error::WriteStreamToFile { source, .. } => source.retry_hint(),
1147            Error::PrepareFileTable { source, .. } | Error::InferFileTableSchema { source, .. } => {
1148                source.retry_hint()
1149            }
1150            #[cfg(feature = "enterprise")]
1151            Error::TriggerQuerier { source, .. } => source.retry_hint(),
1152            _ => RetryHint::NonRetryable,
1153        }
1154    }
1155}
1156
1157define_into_tonic_status!(Error);