Skip to main content

query/promql/
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;
16use std::time::SystemTime;
17
18use common_error::ext::ErrorExt;
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use common_time::timestamp::TimeUnit;
22use datafusion::error::DataFusionError;
23use promql::error::Error as PromqlError;
24use promql_parser::parser::token::TokenType;
25use promql_parser::parser::{Expr as PromExpr, VectorMatchCardinality};
26use snafu::{Location, Snafu};
27
28#[derive(Snafu)]
29#[snafu(visibility(pub))]
30#[stack_trace_debug]
31pub enum Error {
32    #[snafu(display("Unsupported expr type: {}", name))]
33    UnsupportedExpr {
34        name: String,
35        #[snafu(implicit)]
36        location: Location,
37    },
38
39    #[snafu(display("Unsupported vector matches: {:?}", name))]
40    UnsupportedVectorMatch {
41        name: VectorMatchCardinality,
42        #[snafu(implicit)]
43        location: Location,
44    },
45
46    #[snafu(display("Unexpected token: {:?}", token))]
47    UnexpectedToken {
48        token: TokenType,
49        #[snafu(implicit)]
50        location: Location,
51    },
52
53    #[snafu(display("Internal error during building DataFusion plan"))]
54    DataFusionPlanning {
55        #[snafu(source)]
56        error: datafusion::error::DataFusionError,
57        #[snafu(implicit)]
58        location: Location,
59    },
60
61    #[snafu(display("Unexpected plan or expression: {}", desc))]
62    UnexpectedPlanExpr {
63        desc: String,
64        #[snafu(implicit)]
65        location: Location,
66    },
67
68    #[snafu(display("Unknown table type, downcast failed"))]
69    UnknownTable {
70        #[snafu(implicit)]
71        location: Location,
72    },
73
74    #[snafu(display("Cannot find time index column in table {}", table))]
75    TimeIndexNotFound {
76        table: String,
77        #[snafu(implicit)]
78        location: Location,
79    },
80
81    #[snafu(display("Cannot find value columns in table {}", table))]
82    ValueNotFound {
83        table: String,
84        #[snafu(implicit)]
85        location: Location,
86    },
87
88    #[snafu(display("Failed to create PromQL plan node"))]
89    PromqlPlanNode {
90        #[snafu(source)]
91        source: PromqlError,
92        #[snafu(implicit)]
93        location: Location,
94    },
95
96    #[snafu(display(
97        "Cannot accept multiple vector as function input, PromQL expr: {:?}",
98        expr,
99    ))]
100    MultipleVector {
101        expr: PromExpr,
102        #[snafu(implicit)]
103        location: Location,
104    },
105
106    #[snafu(display(
107        "Table (metric) name not found, this indicates a procedure error in PromQL planner"
108    ))]
109    TableNameNotFound {
110        #[snafu(implicit)]
111        location: Location,
112    },
113
114    #[snafu(display("General catalog error: "))]
115    Catalog {
116        #[snafu(implicit)]
117        location: Location,
118        source: catalog::error::Error,
119    },
120
121    #[snafu(display("Expect a range selector, but not found"))]
122    ExpectRangeSelector {
123        #[snafu(implicit)]
124        location: Location,
125    },
126
127    #[snafu(display("Zero range in range selector"))]
128    ZeroRangeSelector {
129        #[snafu(implicit)]
130        location: Location,
131    },
132
133    #[snafu(display(
134        "The end time must be greater than start time, start: {:?}, end: {:?}",
135        start,
136        end
137    ))]
138    InvalidTimeRange {
139        start: i64,
140        end: i64,
141        #[snafu(implicit)]
142        location: Location,
143    },
144
145    #[snafu(display("Cannot find column {col}"))]
146    ColumnNotFound {
147        col: String,
148        #[snafu(implicit)]
149        location: Location,
150    },
151
152    #[snafu(display("Found multiple metric matchers in selector"))]
153    MultipleMetricMatchers {
154        #[snafu(implicit)]
155        location: Location,
156    },
157
158    #[snafu(display("Expect a metric matcher, but not found"))]
159    NoMetricMatcher {
160        #[snafu(implicit)]
161        location: Location,
162    },
163
164    #[snafu(display("Invalid function argument for {}", fn_name))]
165    FunctionInvalidArgument {
166        fn_name: String,
167        #[snafu(implicit)]
168        location: Location,
169    },
170
171    #[snafu(display(
172        "Attempt to combine two tables with different column sets, left: {:?}, right: {:?}",
173        left,
174        right
175    ))]
176    CombineTableColumnMismatch {
177        left: Vec<String>,
178        right: Vec<String>,
179        #[snafu(implicit)]
180        location: Location,
181    },
182
183    #[snafu(display("Multi fields calculation is not supported in {}", operator))]
184    MultiFieldsNotSupported {
185        operator: String,
186        #[snafu(implicit)]
187        location: Location,
188    },
189
190    #[snafu(display("Matcher operator {matcher_op} is not supported for {matcher}"))]
191    UnsupportedMatcherOp {
192        matcher_op: String,
193        matcher: String,
194        #[snafu(implicit)]
195        location: Location,
196    },
197
198    #[snafu(display("Timestamp out of range for the `@` modifier: {}", timestamp))]
199    AtModifierTimestampOutOfRange {
200        timestamp: String,
201        #[snafu(implicit)]
202        location: Location,
203    },
204
205    #[snafu(display("Timestamp out of range: {} of {:?}", timestamp, unit))]
206    TimestampOutOfRange {
207        timestamp: i64,
208        unit: TimeUnit,
209        #[snafu(implicit)]
210        location: Location,
211    },
212
213    #[snafu(display("Time out of the representable millisecond range: {:?}", time))]
214    SystemTimeOutOfRange {
215        time: SystemTime,
216        #[snafu(implicit)]
217        location: Location,
218    },
219
220    #[snafu(display("Invalid regular expression in label_replace(): {}", regex))]
221    InvalidRegularExpression {
222        regex: String,
223        #[snafu(implicit)]
224        location: Location,
225    },
226
227    #[snafu(display("Invalid destination label name in label_replace(): {}", label_name))]
228    InvalidDestinationLabelName {
229        label_name: String,
230        #[snafu(implicit)]
231        location: Location,
232    },
233}
234
235impl ErrorExt for Error {
236    fn status_code(&self) -> StatusCode {
237        use Error::*;
238        match self {
239            TimeIndexNotFound { .. }
240            | ValueNotFound { .. }
241            | UnsupportedExpr { .. }
242            | UnexpectedToken { .. }
243            | MultipleVector { .. }
244            | ExpectRangeSelector { .. }
245            | ZeroRangeSelector { .. }
246            | InvalidTimeRange { .. }
247            | ColumnNotFound { .. }
248            | FunctionInvalidArgument { .. }
249            | UnsupportedVectorMatch { .. }
250            | CombineTableColumnMismatch { .. }
251            | UnexpectedPlanExpr { .. }
252            | UnsupportedMatcherOp { .. }
253            | TimestampOutOfRange { .. }
254            | SystemTimeOutOfRange { .. }
255            | AtModifierTimestampOutOfRange { .. }
256            | InvalidRegularExpression { .. }
257            | InvalidDestinationLabelName { .. } => StatusCode::InvalidArguments,
258
259            UnknownTable { .. } => StatusCode::Internal,
260
261            PromqlPlanNode { source, .. } => source.status_code(),
262
263            DataFusionPlanning { .. } => StatusCode::PlanQuery,
264
265            TableNameNotFound { .. } => StatusCode::TableNotFound,
266
267            MultipleMetricMatchers { .. } | NoMetricMatcher { .. } => StatusCode::InvalidSyntax,
268
269            MultiFieldsNotSupported { .. } => StatusCode::Unsupported,
270            Catalog { source, .. } => source.status_code(),
271        }
272    }
273
274    fn as_any(&self) -> &dyn Any {
275        self
276    }
277}
278
279pub type Result<T> = std::result::Result<T, Error>;
280
281impl From<Error> for DataFusionError {
282    fn from(err: Error) -> Self {
283        DataFusionError::External(Box::new(err))
284    }
285}