Skip to content

Commit

Permalink
Initial support for serde/pickling expressions. (#3096)
Browse files Browse the repository at this point in the history
* partial derive serde

* python: first expression pickled
  • Loading branch information
ritchie46 committed Apr 10, 2022
1 parent e1a6c59 commit 10301f6
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 374 deletions.
1 change: 1 addition & 0 deletions polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ default = [
ndarray = ["polars-core/ndarray"]
# serde support for dataframes and series
serde = ["polars-core/serde"]
serde-lazy = ["polars-core/serde-lazy", "polars-lazy/serde"]
parquet = ["polars-io", "polars-core/parquet", "polars-lazy/parquet", "polars-io/parquet"]
lazy = ["polars-core/lazy", "polars-lazy", "polars-lazy/compile"]
# commented out until UB is fixed
Expand Down
1 change: 1 addition & 0 deletions polars/polars-arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ arrow = { package = "arrow2", git = "https://github.com/ritchie46/arrow2", branc
# arrow = { package = "arrow2", version = "0.10", default-features = false, features = ["compute_concatenate"] }
hashbrown = "0.12"
num = "^0.4"
serde = { version = "1", features = ["derive"], optional = true }
thiserror = "^1.0"

[features]
Expand Down
3 changes: 3 additions & 0 deletions polars/polars-arrow/src/kernels/rolling/no_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use arrow::array::{ArrayRef, PrimitiveArray};
use arrow::datatypes::DataType;
use arrow::types::NativeType;
use num::{Float, NumCast, ToPrimitive, Zero};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Sub};
use std::sync::Arc;

#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum QuantileInterpolOptions {
Nearest,
Lower,
Expand Down
2 changes: 2 additions & 0 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ parquet = ["arrow/io_parquet"]
# scale to terrabytes?
bigidx = ["polars-arrow/bigidx"]

serde-lazy = ["serde", "polars-arrow/serde"]

docs-selection = [
"ndarray",
"is_in",
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub(crate) mod unique;
#[cfg(feature = "zip_with")]
pub mod zip;

#[cfg(feature = "serde-lazy")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "to_list")]
pub trait ToList<T: PolarsDataType> {
fn to_list(&self) -> Result<ListChunked> {
Expand Down Expand Up @@ -503,6 +506,7 @@ pub trait ToDummies<T>: ChunkUnique<T> {
}

#[derive(Default, Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde-lazy", derive(Serialize, Deserialize))]
pub struct SortOptions {
pub descending: bool,
pub nulls_last: bool,
Expand Down
9 changes: 9 additions & 0 deletions polars/polars-core/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ impl PartialOrd for AnyValue<'_> {
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-lazy", derive(Serialize, Deserialize))]
pub enum TimeUnit {
Nanoseconds,
Microseconds,
Expand Down Expand Up @@ -658,6 +659,7 @@ impl TimeUnit {
pub type TimeZone = String;

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde-lazy", derive(Serialize, Deserialize))]
pub enum DataType {
Boolean,
UInt8,
Expand Down Expand Up @@ -686,11 +688,14 @@ pub enum DataType {
#[cfg(feature = "object")]
/// A generic type that can be used in a `Series`
/// &'static str can be used to determine/set inner type
#[cfg_attr(feature = "serde-lazy", serde(skip))]
// how to deserialize a static?
Object(&'static str),
Null,
#[cfg(feature = "dtype-categorical")]
// The RevMapping has the internal state.
// This is ignored with casts, comparisons, hashing etc.
#[cfg_attr(feature = "serde-lazy", serde(skip))]
Categorical(Option<Arc<RevMapping>>),
#[cfg(feature = "dtype-struct")]
Struct(Vec<Field>),
Expand Down Expand Up @@ -823,6 +828,10 @@ impl PartialEq<ArrowDataType> for DataType {

/// Characterizes the name and the [`DataType`] of a column.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-lazy", derive(Serialize, Deserialize))]
// see: https://github.com/serde-rs/serde/issues/1712
// we need this because `DataType` has a `&'static`
#[cfg_attr(feature = "serde-lazy", serde(bound(deserialize = "'de: 'static")))]
pub struct Field {
name: String,
dtype: DataType,
Expand Down
1 change: 1 addition & 0 deletions polars/polars-lazy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ glob = "0.3"
parking_lot = "0.12"
rayon = "1.5"
regex = { version = "1.5", optional = true }
serde = { version = "1", features = ["derive"], optional = true }

polars-arrow = { version = "0.20.0", path = "../polars-arrow" }
polars-core = { version = "0.20.0", path = "../polars-core", features = ["lazy", "private", "zip_with"], default-features = false }
Expand Down

0 comments on commit 10301f6

Please sign in to comment.