Path: blob/main/crates/polars-plan/src/dsl/function_expr/range.rs
8327 views
use std::fmt;12use polars_core::prelude::*;3use polars_ops::series::ClosedInterval;4#[cfg(feature = "temporal")]5use polars_time::{ClosedWindow, Duration};6#[cfg(feature = "serde")]7use serde::{Deserialize, Serialize};89use super::{DataTypeExpr, Expr, FunctionExpr};1011#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]12#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]13#[cfg(any(feature = "dtype-date", feature = "dtype-datetime"))]14#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]15// Date and Datetime range functions requires three of four optional parameters.16// The combination of parameters used determines the dtype and usage of the input expressions.17pub enum DateRangeArgs {18StartEndInterval,19StartEndSamples,20StartIntervalSamples,21EndIntervalSamples,22}2324impl DateRangeArgs {25pub fn parse(26start: Option<Expr>,27end: Option<Expr>,28interval: Option<Duration>,29num_samples: Option<Expr>,30) -> PolarsResult<(Vec<Expr>, DateRangeArgs)> {31match (start, end, interval, num_samples) {32(Some(start), Some(end), Some(_), None) => {33Ok((vec![start, end], DateRangeArgs::StartEndInterval))34},35(Some(start), Some(end), None, Some(num_samples)) => Ok((36vec![start, end, num_samples],37DateRangeArgs::StartEndSamples,38)),39(Some(start), None, Some(_), Some(num_samples)) => Ok((40vec![start, num_samples],41DateRangeArgs::StartIntervalSamples,42)),43(None, Some(end), Some(_), Some(num_samples)) => {44Ok((vec![end, num_samples], DateRangeArgs::EndIntervalSamples))45},46_ => {47polars_bail!(InvalidOperation: "Exactly three of 'start', 'end', 'interval', and 'num_samples' must be supplied.");48},49}50}51}5253#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]54#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]55#[derive(Clone, PartialEq, Debug, Hash)]56pub enum RangeFunction {57IntRange {58step: i64,59dtype: DataTypeExpr,60},61IntRanges {62dtype: DataTypeExpr,63},64LinearSpace {65closed: ClosedInterval,66},67LinearSpaces {68closed: ClosedInterval,69array_width: Option<usize>,70},71#[cfg(feature = "dtype-date")]72DateRange {73interval: Option<Duration>,74closed: ClosedWindow,75arg_type: DateRangeArgs,76},77#[cfg(feature = "dtype-date")]78DateRanges {79interval: Option<Duration>,80closed: ClosedWindow,81arg_type: DateRangeArgs,82},83#[cfg(feature = "dtype-datetime")]84DatetimeRange {85interval: Option<Duration>,86closed: ClosedWindow,87time_unit: Option<TimeUnit>,88time_zone: Option<TimeZone>,89arg_type: DateRangeArgs,90},91#[cfg(feature = "dtype-datetime")]92DatetimeRanges {93interval: Option<Duration>,94closed: ClosedWindow,95time_unit: Option<TimeUnit>,96time_zone: Option<TimeZone>,97arg_type: DateRangeArgs,98},99#[cfg(feature = "dtype-time")]100TimeRange {101interval: Duration,102closed: ClosedWindow,103},104#[cfg(feature = "dtype-time")]105TimeRanges {106interval: Duration,107closed: ClosedWindow,108},109}110111impl From<RangeFunction> for FunctionExpr {112fn from(value: RangeFunction) -> Self {113Self::Range(value)114}115}116117impl fmt::Display for RangeFunction {118fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {119use RangeFunction::*;120let s = match self {121IntRange { .. } => "int_range",122IntRanges { .. } => "int_ranges",123LinearSpace { .. } => "linear_space",124LinearSpaces { .. } => "linear_spaces",125#[cfg(feature = "dtype-date")]126DateRange { .. } => "date_range",127#[cfg(feature = "dtype-date")]128DateRanges { .. } => "date_ranges",129#[cfg(feature = "dtype-datetime")]130DatetimeRange { .. } => "datetime_range",131#[cfg(feature = "dtype-datetime")]132DatetimeRanges { .. } => "datetime_ranges",133#[cfg(feature = "dtype-time")]134TimeRange { .. } => "time_range",135#[cfg(feature = "dtype-time")]136TimeRanges { .. } => "time_ranges",137};138write!(f, "{s}")139}140}141142143