Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python)!: Implement binary serialization of LazyFrame/DataFrame/Expr and set it as the default format #17223

Merged
merged 18 commits into from
Jun 28, 2024

Conversation

stinodego
Copy link
Member

@stinodego stinodego commented Jun 26, 2024

Closes #17108

Changes

  • Add format parameter to the serialize/deserialize methods on DataFrame/LazyFrame/Expr with the options "json" (existing) and "binary" (new).
  • Set "binary" as the default format. This is a breaking change. You can retain the previous behavior by specifying format="json"

Example

Before:

>>> lf = pl.LazyFrame({"a": [1, 2, 3]}).sum()
>>> serialized = lf.serialize()
>>> serialized
'{"MapFunction":{"input":{"DataFrameScan":{"df":{"columns":[{"name":...'
>>> from io import StringIO
>>> pl.LazyFrame.deserialize(StringIO(serialized)).collect()
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 6   │
└─────┘

After:

>>> lf = pl.LazyFrame({"a": [1, 2, 3]}).sum()
>>> serialized = lf.serialize()
>>> serialized
b'\xa1kMapFunction\xa2einput\xa1mDataFrameScan\xa4bdf...'
>>> from io import BytesIO  # Note: using BytesIO instead of StringIO
>>> pl.LazyFrame.deserialize(BytesIO(serialized)).collect()
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 6   │
└─────┘

@github-actions github-actions bot added breaking Change that breaks backwards compatibility enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jun 26, 2024
fn deserialize_binary(py_f: PyObject) -> PyResult<Self> {
let file = get_file_like(py_f, false)?;
let reader = BufReader::new(file);
let lp = ciborium::from_reader::<DslPlan, _>(reader)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ciborium also offers a from_reader_with_buffer which allows finetuning the size of the buffer (default is 4K). I don't really know what size data we're usually looking at, so leaving this at default for now.

Copy link

codecov bot commented Jun 27, 2024

Codecov Report

Attention: Patch coverage is 81.71206% with 47 lines in your changes missing coverage. Please review.

Project coverage is 80.84%. Comparing base (4d2f928) to head (86860a3).
Report is 3 commits behind head on main.

Files Patch % Lines
py-polars/src/dataframe/serde.rs 59.32% 24 Missing ⚠️
py-polars/polars/dataframe/frame.py 66.66% 4 Missing and 2 partials ⚠️
py-polars/polars/lazyframe/frame.py 64.70% 4 Missing and 2 partials ⚠️
py-polars/polars/_utils/serde.py 89.28% 2 Missing and 1 partial ⚠️
py-polars/polars/expr/expr.py 66.66% 2 Missing and 1 partial ⚠️
py-polars/polars/expr/meta.py 70.00% 2 Missing and 1 partial ⚠️
py-polars/src/expr/serde.rs 98.27% 1 Missing ⚠️
py-polars/src/lazyframe/serde.rs 98.21% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17223      +/-   ##
==========================================
+ Coverage   80.82%   80.84%   +0.01%     
==========================================
  Files        1466     1470       +4     
  Lines      192313   192381      +68     
  Branches     2745     2750       +5     
==========================================
+ Hits       155446   155538      +92     
+ Misses      36364    36335      -29     
- Partials      503      508       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stinodego stinodego marked this pull request as ready for review June 27, 2024 14:42
@stinodego stinodego requested a review from ritchie46 June 27, 2024 14:43
@ritchie46 ritchie46 merged commit 855b3fc into main Jun 28, 2024
27 checks passed
@ritchie46 ritchie46 deleted the serde-binary branch June 28, 2024 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Change that breaks backwards compatibility enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change default serialization format for serialize/deserialize methods to binary
2 participants