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;
26use pyo3::PyErr;
27
28pub type PyDataFusionResult<T> = std::result::Result<T, PyDataFusionError>;
29
30#[derive(Debug)]
31pub enum PyDataFusionError {
32    ExecutionError(Box<InnerDataFusionError>),
33    ArrowError(ArrowError),
34    Common(String),
35    PythonError(PyErr),
36    EncodeError(EncodeError),
37}
38
39impl fmt::Display for PyDataFusionError {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match self {
42            PyDataFusionError::ExecutionError(e) => write!(f, "DataFusion error: {e:?}"),
43            PyDataFusionError::ArrowError(e) => write!(f, "Arrow error: {e:?}"),
44            PyDataFusionError::PythonError(e) => write!(f, "Python error {e:?}"),
45            PyDataFusionError::Common(e) => write!(f, "{e}"),
46            PyDataFusionError::EncodeError(e) => write!(f, "Failed to encode substrait plan: {e}"),
47        }
48    }
49}
50
51impl From<ArrowError> for PyDataFusionError {
52    fn from(err: ArrowError) -> PyDataFusionError {
53        PyDataFusionError::ArrowError(err)
54    }
55}
56
57impl From<InnerDataFusionError> for PyDataFusionError {
58    fn from(err: InnerDataFusionError) -> PyDataFusionError {
59        PyDataFusionError::ExecutionError(Box::new(err))
60    }
61}
62
63impl From<PyErr> for PyDataFusionError {
64    fn from(err: PyErr) -> PyDataFusionError {
65        PyDataFusionError::PythonError(err)
66    }
67}
68
69impl From<PyDataFusionError> for PyErr {
70    fn from(err: PyDataFusionError) -> PyErr {
71        match err {
72            PyDataFusionError::PythonError(py_err) => py_err,
73            _ => PyException::new_err(err.to_string()),
74        }
75    }
76}
77
78impl Error for PyDataFusionError {}
79
80pub fn py_type_err(e: impl Debug) -> PyErr {
81    PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!("{e:?}"))
82}
83
84pub fn py_runtime_err(e: impl Debug) -> PyErr {
85    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!("{e:?}"))
86}
87
88pub fn py_datafusion_err(e: impl Debug) -> PyErr {
89    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!("{e:?}"))
90}
91
92pub fn py_unsupported_variant_err(e: impl Debug) -> PyErr {
93    PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("{e:?}"))
94}
95
96pub fn to_datafusion_err(e: impl Debug) -> InnerDataFusionError {
97    InnerDataFusionError::Execution(format!("{e:?}"))
98}