query/datafusion/
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_error::ext::ErrorExt;
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use datafusion::error::DataFusionError;
21use snafu::{Location, Snafu};
22
23/// Inner error of datafusion based query engine.
24#[derive(Snafu)]
25#[snafu(visibility(pub))]
26#[stack_trace_debug]
27pub enum InnerError {
28    #[snafu(display("DataFusion error"))]
29    Datafusion {
30        #[snafu(source)]
31        error: DataFusionError,
32        #[snafu(implicit)]
33        location: Location,
34    },
35
36    #[snafu(display("Fail to convert arrow schema"))]
37    ConvertSchema {
38        #[snafu(implicit)]
39        location: Location,
40        source: datatypes::error::Error,
41    },
42
43    #[snafu(display("Failed to convert DataFusion's recordbatch stream"))]
44    ConvertDfRecordBatchStream {
45        #[snafu(implicit)]
46        location: Location,
47        source: common_recordbatch::error::Error,
48    },
49}
50
51impl ErrorExt for InnerError {
52    fn status_code(&self) -> StatusCode {
53        use InnerError::*;
54
55        match self {
56            // TODO(yingwen): Further categorize datafusion error.
57            Datafusion { .. } => StatusCode::EngineExecuteQuery,
58            ConvertSchema { .. } => StatusCode::Unexpected,
59            ConvertDfRecordBatchStream { source, .. } => source.status_code(),
60        }
61    }
62
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66}