use std::io::{BufReader, BufWriter};
use polars_utils::pl_serialize;
use pyo3::prelude::*;
use super::PyLazyFrame;
use crate::exceptions::ComputeError;
use crate::file::get_file_like;
use crate::prelude::*;
#[pymethods]
#[allow(clippy::should_implement_trait)]
impl PyLazyFrame {
fn serialize_binary(&self, py: Python, py_f: PyObject) -> PyResult<()> {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
py.allow_threads(|| {
pl_serialize::SerializeOptions::default()
.serialize_into_writer(writer, &self.ldf.logical_plan)
.map_err(|err| ComputeError::new_err(err.to_string()))
})
}
#[cfg(feature = "json")]
fn serialize_json(&self, py: Python, py_f: PyObject) -> PyResult<()> {
let file = get_file_like(py_f, true)?;
let writer = BufWriter::new(file);
py.allow_threads(|| {
serde_json::to_writer(writer, &self.ldf.logical_plan)
.map_err(|err| ComputeError::new_err(err.to_string()))
})
}
#[staticmethod]
fn deserialize_binary(py: Python, py_f: PyObject) -> PyResult<Self> {
let file = get_file_like(py_f, false)?;
let reader = BufReader::new(file);
let lp: DslPlan = py.allow_threads(|| {
pl_serialize::SerializeOptions::default()
.deserialize_from_reader(reader)
.map_err(|err| ComputeError::new_err(err.to_string()))
})?;
Ok(LazyFrame::from(lp).into())
}
#[staticmethod]
#[cfg(feature = "json")]
fn deserialize_json(py: Python, py_f: PyObject) -> PyResult<Self> {
let mut json = String::new();
get_file_like(py_f, false)?
.read_to_string(&mut json)
.unwrap();
let json = unsafe { std::mem::transmute::<&'_ str, &'static str>(json.as_str()) };
let lp = py.allow_threads(|| {
serde_json::from_str::<DslPlan>(json)
.map_err(|err| ComputeError::new_err(err.to_string()))
})?;
Ok(LazyFrame::from(lp).into())
}
}