Skip to content

Commit

Permalink
fix(python): use json for expr pickle (#5476)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 11, 2022
1 parent 0c94ae5 commit 1f642ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl PyExpr {

pub fn __getstate__(&self, py: Python) -> PyResult<PyObject> {
// Used in pickle/pickling
Ok(PyBytes::new(py, &bincode::serialize(&self.inner).unwrap()).to_object(py))
let s = serde_json::to_string(&self.inner).unwrap();
Ok(PyBytes::new(py, s.as_bytes()).to_object(py))
}

pub fn __setstate__(&mut self, py: Python, state: PyObject) -> PyResult<()> {
Expand All @@ -93,8 +94,8 @@ impl PyExpr {
// PyBytes still has a lifetime. Bit its ok, because we drop it immediately
// in this scope
let s = unsafe { std::mem::transmute::<&'_ PyBytes, &'static PyBytes>(s) };
self.inner = serde_json::from_slice(s.as_bytes()).unwrap();

self.inner = bincode::deserialize(s.as_bytes()).unwrap();
Ok(())
}
Err(e) => Err(e),
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ def test_serde_duration() -> None:
serde_df["a_td"],
pl.Series("a_td", [None, timedelta(days=1)], dtype=pl.Duration("ns")),
)


def test_serde_expression_5461() -> None:
e = pl.col("a").sqrt() / pl.col("b").alias("c")
assert pickle.loads(pickle.dumps(e)).meta == e.meta

0 comments on commit 1f642ee

Please sign in to comment.