Skip to content

Commit

Permalink
refactor[rust]: implement Display for FunctionExpr (#4684)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 2, 2022
1 parent 383fdb7 commit d247d02
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 110 deletions.
3 changes: 1 addition & 2 deletions polars/polars-lazy/src/dsl/dt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ impl DateLikeNameSpace {
/// This will take leap years/ months into account.
#[cfg(feature = "date_offset")]
pub fn offset_by(self, by: Duration) -> Expr {
self.0
.map_private(FunctionExpr::DateOffset(by), "dt.offset_by")
self.0.map_private(FunctionExpr::DateOffset(by))
}
}
9 changes: 9 additions & 0 deletions polars/polars-lazy/src/dsl/function_expr/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ pub enum ListFunction {
Concat,
}

impl Display for ListFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
ListFunction::Concat => write!(f, "concat"),
}
}
}

#[cfg(feature = "is_in")]
pub(super) fn contains(args: &mut [Series]) -> Result<Series> {
let list = &args[0];
Expand Down
66 changes: 50 additions & 16 deletions polars/polars-lazy/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ mod temporal;
#[cfg(feature = "trigonometry")]
mod trigonometry;

use std::fmt::{Display, Formatter};

#[cfg(feature = "list")]
pub(super) use list::ListFunction;
use polars_core::prelude::*;
Expand All @@ -41,6 +43,8 @@ pub(super) use self::nan::NanFunction;
pub(super) use self::strings::StringFunction;
#[cfg(feature = "dtype-struct")]
pub(super) use self::struct_::StructFunction;
#[cfg(feature = "trigonometry")]
pub(super) use self::trigonometry::TrigonometricFunction;
use super::*;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -96,22 +100,52 @@ pub enum FunctionExpr {
Shift(i64),
}

#[cfg(feature = "trigonometry")]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
pub enum TrigonometricFunction {
Sin,
Cos,
Tan,
ArcSin,
ArcCos,
ArcTan,
Sinh,
Cosh,
Tanh,
ArcSinh,
ArcCosh,
ArcTanh,
impl Display for FunctionExpr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;

match self {
FunctionExpr::NullCount => write!(f, "null_count"),
FunctionExpr::Pow => write!(f, "pow"),
#[cfg(feature = "row_hash")]
FunctionExpr::Hash(_, _, _, _) => write!(f, "hash"),
#[cfg(feature = "is_in")]
FunctionExpr::IsIn => write!(f, "is_in"),
#[cfg(feature = "arg_where")]
FunctionExpr::ArgWhere => write!(f, "arg_where"),
#[cfg(feature = "search_sorted")]
FunctionExpr::SearchSorted => write!(f, "search_sorted"),
#[cfg(feature = "strings")]
FunctionExpr::StringExpr(s) => write!(f, "{}", s),
#[cfg(feature = "date_offset")]
FunctionExpr::DateOffset(_) => write!(f, "dt.offset_by"),
#[cfg(feature = "trigonometry")]
FunctionExpr::Trigonometry(func) => write!(f, "{}", func),
#[cfg(feature = "sign")]
FunctionExpr::Sign => write!(f, "sign"),
FunctionExpr::FillNull { .. } => write!(f, "fill_null"),
#[cfg(feature = "is_in")]
FunctionExpr::ListContains => write!(f, "arr.contains"),
#[cfg(all(feature = "rolling_window", feature = "moment"))]
FunctionExpr::RollingSkew { .. } => write!(f, "rolling_skew"),
FunctionExpr::ShiftAndFill { .. } => write!(f, "shift_and_fill"),
FunctionExpr::Nan(_) => write!(f, "nan"),
#[cfg(feature = "round_series")]
FunctionExpr::Clip { min, max } => match (min, max) {
(Some(_), Some(_)) => write!(f, "clip"),
(None, Some(_)) => write!(f, "clip_max"),
(Some(_), None) => write!(f, "clip_min"),
_ => unreachable!(),
},
#[cfg(feature = "list")]
FunctionExpr::ListExpr(func) => write!(f, "{}", func),
#[cfg(feature = "dtype-struct")]
FunctionExpr::StructExpr(func) => write!(f, "{}", func),
#[cfg(feature = "top_k")]
FunctionExpr::TopK { .. } => write!(f, "top_k"),
FunctionExpr::Shift(_) => write!(f, "shift"),
}
}
}

macro_rules! wrap {
Expand Down
28 changes: 28 additions & 0 deletions polars/polars-lazy/src/dsl/function_expr/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ pub enum StringFunction {
Lowercase,
}

impl Display for StringFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
StringFunction::Contains { .. } => write!(f, "str.contains"),
StringFunction::StartsWith(_) => write!(f, "str.starts_with"),
StringFunction::EndsWith(_) => write!(f, "str.ends_with"),
StringFunction::Extract { .. } => write!(f, "str.extract"),
#[cfg(feature = "string_justify")]
StringFunction::Zfill(_) => write!(f, "str.zfill"),
#[cfg(feature = "string_justify")]
StringFunction::LJust { .. } => write!(f, "str.ljust"),
#[cfg(feature = "string_justify")]
StringFunction::RJust { .. } => write!(f, "str.rjust"),
StringFunction::ExtractAll(_) => write!(f, "str.extract_all"),
StringFunction::CountMatch(_) => write!(f, "str.count_match"),
#[cfg(feature = "temporal")]
StringFunction::Strptime(_) => write!(f, "str.strptime"),
#[cfg(feature = "concat_str")]
StringFunction::Concat(_) => write!(f, "str.concat"),
#[cfg(feature = "regex")]
StringFunction::Replace { .. } => write!(f, "str.replace"),
StringFunction::Uppercase => write!(f, "str.uppercase"),
StringFunction::Lowercase => write!(f, "str.lowercase"),
}
}
}

pub(super) fn uppercase(s: &Series) -> Result<Series> {
let ca = s.utf8()?;
Ok(ca.to_uppercase().into_series())
Expand Down
10 changes: 10 additions & 0 deletions polars/polars-lazy/src/dsl/function_expr/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ pub enum StructFunction {
FieldByName(Arc<str>),
}

impl Display for StructFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
StructFunction::FieldByIndex(_) => write!(f, "struct.field_by_name"),
StructFunction::FieldByName(_) => write!(f, "struct.field_by_index"),
}
}
}

pub(super) fn get_by_index(s: &Series, index: i64) -> Result<Series> {
let s = s.struct_()?;
let (index, _) = slice_offsets(index, 0, s.fields().len());
Expand Down
37 changes: 37 additions & 0 deletions polars/polars-lazy/src/dsl/function_expr/trigonometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@ use polars_core::export::num;

use super::*;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
pub enum TrigonometricFunction {
Sin,
Cos,
Tan,
ArcSin,
ArcCos,
ArcTan,
Sinh,
Cosh,
Tanh,
ArcSinh,
ArcCosh,
ArcTanh,
}

impl Display for TrigonometricFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
TrigonometricFunction::Sin => write!(f, "sin"),
TrigonometricFunction::Cos => write!(f, "cos"),
TrigonometricFunction::Tan => write!(f, "tan"),
TrigonometricFunction::ArcSin => write!(f, "arcsin"),
TrigonometricFunction::ArcCos => write!(f, "arccos"),
TrigonometricFunction::ArcTan => write!(f, "arctan"),
TrigonometricFunction::Sinh => write!(f, "sinh"),
TrigonometricFunction::Cosh => write!(f, "cosh"),
TrigonometricFunction::Tanh => write!(f, "tanh"),
TrigonometricFunction::ArcSinh => write!(f, "arcsinh"),
TrigonometricFunction::ArcCosh => write!(f, "arccosh"),
TrigonometricFunction::ArcTanh => write!(f, "arctanh"),
}
}
}

pub(super) fn apply_trigonometric_function(
s: &Series,
trig_function: TrigonometricFunction,
Expand Down

0 comments on commit d247d02

Please sign in to comment.