From 6409af3dc78789122f92098ef53a4b501a567219 Mon Sep 17 00:00:00 2001 From: Daniel Mesejo Date: Mon, 2 Sep 2024 17:28:59 +0200 Subject: [PATCH] feat: better exception and message for table not found (#851) closes #796 --- python/datafusion/tests/test_context.py | 7 +++++++ src/context.rs | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/python/datafusion/tests/test_context.py b/python/datafusion/tests/test_context.py index 0184280c..4af00a3b 100644 --- a/python/datafusion/tests/test_context.py +++ b/python/datafusion/tests/test_context.py @@ -466,6 +466,13 @@ def test_table_exist(ctx): assert ctx.table_exist("t") is True +def test_table_not_found(ctx): + from uuid import uuid4 + + with pytest.raises(KeyError): + ctx.table(f"not-found-{uuid4()}") + + def test_read_json(ctx): path = os.path.dirname(os.path.abspath(__file__)) diff --git a/src/context.rs b/src/context.rs index 4433d94c..3ab78349 100644 --- a/src/context.rs +++ b/src/context.rs @@ -765,7 +765,8 @@ impl PySessionContext { } pub fn table(&self, name: &str, py: Python) -> PyResult { - let x = wait_for_future(py, self.ctx.table(name)).map_err(DataFusionError::from)?; + let x = wait_for_future(py, self.ctx.table(name)) + .map_err(|e| PyKeyError::new_err(e.to_string()))?; Ok(PyDataFrame::new(x)) }