datafusion_python/
errors.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use core::fmt;
19use std::error::Error;
20use std::fmt::Debug;
21
22use datafusion::arrow::error::ArrowError;
23use datafusion::error::DataFusionError as InnerDataFusionError;
24use prost::EncodeError;
25use pyo3::{exceptions::PyException, PyErr};
26
27pub type PyDataFusionResult<T> = std::result::Result<T, PyDataFusionError>;
28
29#[derive(Debug)]
30pub enum PyDataFusionError {
31    ExecutionError(Box<InnerDataFusionError>),
32    ArrowError(ArrowError),
33    Common(String),
34    PythonError(PyErr),
35    EncodeError(EncodeError),
36}
37
38impl fmt::Display for PyDataFusionError {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        match self {
41            PyDataFusionError::ExecutionError(e) => write!(f, "DataFusion error: {e:?}"),
42            PyDataFusionError::ArrowError(e) => write!(f, "Arrow error: {e:?}"),
43            PyDataFusionError::PythonError(e) => write!(f, "Python error {e:?}"),
44            PyDataFusionError::Common(e) => write!(f, "{e}"),
45            PyDataFusionError::EncodeError(e) => write!(f, "Failed to encode substrait plan: {e}"),
46        }
47    }
48}
49
50impl From<ArrowError> for PyDataFusionError {
51    fn from(err: ArrowError) -> PyDataFusionError {
52        PyDataFusionError::ArrowError(err)
53    }
54}
55
56impl From<InnerDataFusionError> for PyDataFusionError {
57    fn from(err: InnerDataFusionError) -> PyDataFusionError {
58        PyDataFusionError::ExecutionError(Box::new(err))
59    }
60}
61
62impl From<PyErr> for PyDataFusionError {
63    fn from(err: PyErr) -> PyDataFusionError {
64        PyDataFusionError::PythonError(err)
65    }
66}
67
68impl From<PyDataFusionError> for PyErr {
69    fn from(err: PyDataFusionError) -> PyErr {
70        match err {
71            PyDataFusionError::PythonError(py_err) => py_err,
72            _ => PyException::new_err(err.to_string()),
73        }
74    }
75}
76
77impl Error for PyDataFusionError {}
78
79pub fn py_type_err(e: impl Debug) -> PyErr {
80    PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!("{e:?}"))
81}
82
83pub fn py_runtime_err(e: impl Debug) -> PyErr {
84    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!("{e:?}"))
85}
86
87pub fn py_datafusion_err(e: impl Debug) -> PyErr {
88    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!("{e:?}"))
89}
90
91pub fn py_unsupported_variant_err(e: impl Debug) -> PyErr {
92    PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("{e:?}"))
93}
94
95pub fn to_datafusion_err(e: impl Debug) -> InnerDataFusionError {
96    InnerDataFusionError::Execution(format!("{e:?}"))
97}