Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions python/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def table_exist(self, name: str) -> bool:
return name in self.tables


class CustomErrorSchemaProvider(CustomSchemaProvider):
def table(self, name: str) -> Table | None:
message = f"{name} is not an acceptable name"
raise ValueError(message)


class CustomCatalogProvider(dfn.catalog.CatalogProvider):
def __init__(self):
self.schemas = {"my_schema": CustomSchemaProvider()}
Expand Down Expand Up @@ -219,6 +225,33 @@ def test_schema_register_table_with_pyarrow_dataset(ctx: SessionContext):
schema.deregister_table(table_name)


def test_exception_not_mangled(ctx: SessionContext):
"""Test registering all python providers and running a query against them."""

catalog_name = "custom_catalog"
schema_name = "custom_schema"

ctx.register_catalog_provider(catalog_name, CustomCatalogProvider())

catalog = ctx.catalog(catalog_name)

# Clean out previous schemas if they exist so we can start clean
for schema_name in catalog.schema_names():
catalog.deregister_schema(schema_name, cascade=False)

catalog.register_schema(schema_name, CustomErrorSchemaProvider())

schema = catalog.schema(schema_name)

for table_name in schema.table_names():
schema.deregister_table(table_name)

schema.register_table("test_table", create_dataset())

with pytest.raises(ValueError, match="^test_table is not an acceptable name$"):
ctx.sql(f"select * from {catalog_name}.{schema_name}.test_table")


def test_in_end_to_end_python_providers(ctx: SessionContext):
"""Test registering all python providers and running a query against them."""

Expand Down
5 changes: 4 additions & 1 deletion python/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@


def test_no_table(ctx):
with pytest.raises(Exception, match="DataFusion error"):
with pytest.raises(
ValueError,
match="^Error during planning: table 'datafusion.public.b' not found$",
):
ctx.sql("SELECT a FROM b").collect()


Expand Down
3 changes: 2 additions & 1 deletion src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ impl SchemaProvider for RustWrappedPySchemaProvider {
&self,
name: &str,
) -> datafusion::common::Result<Option<Arc<dyn TableProvider>>, DataFusionError> {
self.table_inner(name).map_err(to_datafusion_err)
self.table_inner(name)
.map_err(|e| DataFusionError::External(Box::new(e)))
}

fn register_table(
Expand Down
7 changes: 5 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ use crate::catalog::{
use crate::common::data_type::PyScalarValue;
use crate::dataframe::PyDataFrame;
use crate::dataset::Dataset;
use crate::errors::{py_datafusion_err, PyDataFusionError, PyDataFusionResult};
use crate::errors::{
from_datafusion_error, py_datafusion_err, PyDataFusionError, PyDataFusionResult,
};
use crate::expr::sort_expr::PySortExpr;
use crate::options::PyCsvReadOptions;
use crate::physical_plan::PyExecutionPlan;
Expand Down Expand Up @@ -465,7 +467,8 @@ impl PySessionContext {

let mut df = wait_for_future(py, async {
self.ctx.sql_with_options(&query, options).await
})??;
})?
.map_err(from_datafusion_error)?;

if !param_values.is_empty() {
df = df.with_param_values(param_values)?;
Expand Down
12 changes: 11 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt::Debug;
use datafusion::arrow::error::ArrowError;
use datafusion::error::DataFusionError as InnerDataFusionError;
use prost::EncodeError;
use pyo3::exceptions::PyException;
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::PyErr;

pub type PyDataFusionResult<T> = std::result::Result<T, PyDataFusionError>;
Expand Down Expand Up @@ -96,3 +96,13 @@ pub fn py_unsupported_variant_err(e: impl Debug) -> PyErr {
pub fn to_datafusion_err(e: impl Debug) -> InnerDataFusionError {
InnerDataFusionError::Execution(format!("{e:?}"))
}

pub fn from_datafusion_error(err: InnerDataFusionError) -> PyErr {
match err {
InnerDataFusionError::External(boxed) => match boxed.downcast::<PyErr>() {
Ok(py_err) => *py_err,
Err(original_boxed) => PyValueError::new_err(format!("{original_boxed}")),
},
_ => PyValueError::new_err(format!("{err}")),
}
}