Skip to main content

flow/expr/
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
15//! Error handling for expression evaluation.
16
17use std::any::Any;
18
19use arrow_schema::ArrowError;
20use common_error::ext::{BoxedError, ErrorExt};
21use common_error::status_code::StatusCode;
22use common_macro::stack_trace_debug;
23use datafusion_common::DataFusionError;
24use datatypes::data_type::ConcreteDataType;
25use snafu::{Location, Snafu};
26
27/// EvalError is about errors happen on columnar evaluation
28///
29/// TODO(discord9): add detailed location of column/operator(instead of code) to errors tp help identify related column
30#[derive(Snafu)]
31#[snafu(visibility(pub))]
32#[stack_trace_debug]
33pub enum EvalError {
34    #[snafu(display("Fail to cast value of type {from} to given type {to}"))]
35    CastValue {
36        from: ConcreteDataType,
37        to: ConcreteDataType,
38        source: datatypes::Error,
39        #[snafu(implicit)]
40        location: Location,
41    },
42
43    #[snafu(display("{msg}"))]
44    DataType {
45        msg: String,
46        source: datatypes::Error,
47        #[snafu(implicit)]
48        location: Location,
49    },
50
51    #[snafu(display("Invalid argument: {reason}"))]
52    InvalidArgument {
53        reason: String,
54        #[snafu(implicit)]
55        location: Location,
56    },
57
58    #[snafu(display("Internal error: {reason}"))]
59    Internal {
60        reason: String,
61        #[snafu(implicit)]
62        location: Location,
63    },
64
65    #[snafu(display("Arrow error: {error:?}, context: {context}"))]
66    Arrow {
67        #[snafu(source)]
68        error: ArrowError,
69        context: String,
70        #[snafu(implicit)]
71        location: Location,
72    },
73
74    #[snafu(display("DataFusion error: {error:?}, context: {context}"))]
75    Datafusion {
76        #[snafu(source)]
77        error: DataFusionError,
78        context: String,
79        #[snafu(implicit)]
80        location: Location,
81    },
82
83    #[snafu(display("External error"))]
84    External {
85        #[snafu(implicit)]
86        location: Location,
87        source: BoxedError,
88    },
89}
90
91impl ErrorExt for EvalError {
92    fn status_code(&self) -> StatusCode {
93        use EvalError::*;
94        match self {
95            InvalidArgument { .. } => StatusCode::InvalidArguments,
96
97            CastValue { source, .. } | DataType { source, .. } => source.status_code(),
98
99            Internal { .. } | Arrow { .. } | Datafusion { .. } | External { .. } => {
100                StatusCode::Internal
101            }
102        }
103    }
104
105    fn as_any(&self) -> &dyn Any {
106        self
107    }
108}