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

fix: Make pl.struct serializable #11169

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/coerce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use polars_core::prelude::*;

#[cfg(feature = "dtype-struct")]
pub fn as_struct(s: &[Series]) -> PolarsResult<Series> {
Ok(StructChunked::new(s[0].name(), s)?.into_series())
}
9 changes: 9 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod bounds;
mod cat;
#[cfg(feature = "round_series")]
mod clip;
mod coerce;
mod concat;
mod correlation;
mod cum;
Expand Down Expand Up @@ -142,6 +143,8 @@ pub enum FunctionExpr {
ArrayExpr(ArrayFunction),
#[cfg(feature = "dtype-struct")]
StructExpr(StructFunction),
#[cfg(feature = "dtype-struct")]
AsStruct,
#[cfg(feature = "top_k")]
TopK {
k: usize,
Expand Down Expand Up @@ -318,6 +321,8 @@ impl Display for FunctionExpr {
ListExpr(func) => return write!(f, "{func}"),
#[cfg(feature = "dtype-struct")]
StructExpr(func) => return write!(f, "{func}"),
#[cfg(feature = "dtype-struct")]
AsStruct => "as_struct",
#[cfg(feature = "top_k")]
TopK { .. } => "top_k",
Shift(_) => "shift",
Expand Down Expand Up @@ -571,6 +576,10 @@ impl From<FunctionExpr> for SpecialEq<Arc<dyn SeriesUdf>> {
FieldByName(name) => map!(struct_::get_by_name, name.clone()),
}
},
#[cfg(feature = "dtype-struct")]
AsStruct => {
map_as_slice!(coerce::as_struct)
},
#[cfg(feature = "top_k")]
TopK { k, descending } => {
map!(top_k, k, descending)
Expand Down
5 changes: 5 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ impl FunctionExpr {
}
},
#[cfg(feature = "dtype-struct")]
AsStruct => Ok(Field::new(
fields[0].name(),
DataType::Struct(fields.to_vec()),
)),
#[cfg(feature = "dtype-struct")]
StructExpr(s) => {
use polars_core::utils::slice_offsets;
use StructFunction::*;
Expand Down
23 changes: 11 additions & 12 deletions crates/polars-plan/src/dsl/functions/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ use super::*;

/// Take several expressions and collect them into a [`StructChunked`].
#[cfg(feature = "dtype-struct")]
pub fn as_struct(exprs: &[Expr]) -> Expr {
map_multiple(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the long run, do we plan to reduce the use of anonymous functions?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, in fact existing anonymous functions should be rewritten to the common style and implementation dispatched to polars-ops.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good, I will find some time to help rewrite this. :)

|s| StructChunked::new(s[0].name(), s).map(|ca| Some(ca.into_series())),
exprs,
GetOutput::map_fields(|fld| Field::new(fld[0].name(), DataType::Struct(fld.to_vec()))),
)
.with_function_options(|mut options| {
options.input_wildcard_expansion = true;
options.fmt_str = "as_struct";
options.pass_name_to_apply = true;
options
})
pub fn as_struct(exprs: Vec<Expr>) -> Expr {
Expr::Function {
input: exprs,
function: FunctionExpr::AsStruct,
options: FunctionOptions {
input_wildcard_expansion: true,
pass_name_to_apply: true,
collect_groups: ApplyOptions::ApplyFlat,
..Default::default()
},
}
}
2 changes: 1 addition & 1 deletion py-polars/src/functions/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn arg_where(condition: PyExpr) -> PyExpr {
#[pyfunction]
pub fn as_struct(exprs: Vec<PyExpr>) -> PyExpr {
let exprs = exprs.to_exprs();
dsl::as_struct(&exprs).into()
dsl::as_struct(exprs).into()
}

#[pyfunction]
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def test_pickling_simple_expression() -> None:
assert str(pickle.loads(buf)) == str(e)


def test_pickling_as_struct_11100() -> None:
e = pl.struct("a")
buf = pickle.dumps(e)
assert str(pickle.loads(buf)) == str(e)


def test_lazyframe_serde() -> None:
lf = pl.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}).lazy().select(pl.col("a"))

Expand Down