Skip to content

Commit

Permalink
Use ComputeError instead
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jul 8, 2024
1 parent 30e73b4 commit 46e91d1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
19 changes: 7 additions & 12 deletions py-polars/src/dataframe/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::io::{BufReader, BufWriter, Cursor};
use std::ops::Deref;

use polars_io::mmap::ReaderBytes;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyBytes;

use super::PyDataFrame;
use crate::error::PyPolarsErr;
use crate::exceptions::ComputeError;
use crate::file::{get_file_like, get_mmap_bytes_reader};
use crate::prelude::*;

Expand Down Expand Up @@ -48,7 +48,7 @@ impl PyDataFrame {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
ciborium::into_writer(&self.df, writer)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Serialize into a JSON string.
Expand All @@ -57,7 +57,7 @@ impl PyDataFrame {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &self.df)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Deserialize a file-like object containing binary data into a DataFrame.
Expand All @@ -66,7 +66,7 @@ impl PyDataFrame {
let file = get_file_like(py_f, false)?;
let reader = BufReader::new(file);
let df = ciborium::from_reader::<DataFrame, _>(reader)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))?;
.map_err(|err| ComputeError::new_err(err.to_string()))?;
Ok(df.into())
}

Expand All @@ -81,14 +81,9 @@ impl PyDataFrame {
py.allow_threads(move || {
let mmap_read: ReaderBytes = (&mut mmap_bytes_r).into();
let bytes = mmap_read.deref();
match serde_json::from_slice::<DataFrame>(bytes) {
Ok(df) => Ok(df.into()),
Err(e) => {
let msg = format!("{e}");
let e = PyPolarsErr::from(PolarsError::ComputeError(msg.into()));
Err(PyErr::from(e))
},
}
let df = serde_json::from_slice::<DataFrame>(bytes)
.map_err(|err| ComputeError::new_err(err.to_string()))?;
Ok(df.into())
})
}
}
11 changes: 5 additions & 6 deletions py-polars/src/expr/serde.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::io::{BufReader, BufWriter, Cursor};

use polars::lazy::prelude::Expr;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::PyBytes;

use crate::error::PyPolarsErr;
use crate::exceptions::ComputeError;
use crate::file::get_file_like;
use crate::prelude::polars_err;
use crate::PyExpr;

#[pymethods]
Expand Down Expand Up @@ -40,7 +39,7 @@ impl PyExpr {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
ciborium::into_writer(&self.inner, writer)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Serialize into a JSON string.
Expand All @@ -49,7 +48,7 @@ impl PyExpr {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &self.inner)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Deserialize a file-like object containing binary data into an Expr.
Expand All @@ -58,7 +57,7 @@ impl PyExpr {
let file = get_file_like(py_f, false)?;
let reader = BufReader::new(file);
let expr = ciborium::from_reader::<Expr, _>(reader)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))?;
.map_err(|err| ComputeError::new_err(err.to_string()))?;
Ok(expr.into())
}

Expand All @@ -83,7 +82,7 @@ impl PyExpr {

let inner: Expr = serde_json::from_str(json).map_err(|_| {
let msg = "could not deserialize input into an expression";
PyPolarsErr::from(polars_err!(ComputeError: msg))
ComputeError::new_err(msg)
})?;
Ok(inner.into())
}
Expand Down
10 changes: 5 additions & 5 deletions py-polars/src/lazyframe/serde.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::{BufReader, BufWriter};

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::PyBytes;

use super::PyLazyFrame;
use crate::error::PyPolarsErr;
use crate::exceptions::ComputeError;
use crate::file::get_file_like;
use crate::prelude::*;

Expand Down Expand Up @@ -40,7 +40,7 @@ impl PyLazyFrame {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
ciborium::into_writer(&self.ldf.logical_plan, writer)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Serialize into a JSON string.
Expand All @@ -49,7 +49,7 @@ impl PyLazyFrame {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &self.ldf.logical_plan)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))
.map_err(|err| ComputeError::new_err(err.to_string()))
}

/// Deserialize a file-like object containing binary data into a LazyFrame.
Expand All @@ -58,7 +58,7 @@ impl PyLazyFrame {
let file = get_file_like(py_f, false)?;
let reader = BufReader::new(file);
let lp = ciborium::from_reader::<DslPlan, _>(reader)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))?;
.map_err(|err| ComputeError::new_err(err.to_string()))?;
Ok(LazyFrame::from(lp).into())
}

Expand All @@ -82,7 +82,7 @@ impl PyLazyFrame {
let json = unsafe { std::mem::transmute::<&'_ str, &'static str>(json.as_str()) };

let lp = serde_json::from_str::<DslPlan>(json)
.map_err(|err| PyValueError::new_err(format!("{err:?}")))?;
.map_err(|err| ComputeError::new_err(err.to_string()))?;
Ok(LazyFrame::from(lp).into())
}
}
2 changes: 1 addition & 1 deletion py-polars/tests/unit/dataframe/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,6 @@ def test_df_deserialize_validation() -> None:
def test_df_serialize_invalid_type() -> None:
df = pl.DataFrame({"a": [object()]})
with pytest.raises(
ValueError, match="serializing data of type Object is not supported"
ComputeError, match="serializing data of type Object is not supported"
):
df.serialize()
2 changes: 1 addition & 1 deletion py-polars/tests/unit/lazyframe/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_lf_serde_to_from_file(lf: pl.LazyFrame, tmp_path: Path) -> None:
def test_lf_deserialize_validation() -> None:
f = io.BytesIO(b"hello world!")
with pytest.raises(ComputeError, match="expected value at line 1 column 1"):
pl.DataFrame.deserialize(f, format="json")
pl.LazyFrame.deserialize(f, format="json")


@pytest.mark.write_disk()
Expand Down

0 comments on commit 46e91d1

Please sign in to comment.