1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::prelude::*;
#[cfg(feature = "temporal")]
use polars_core::export::chrono::{Duration as ChronoDuration, NaiveDate, NaiveDateTime};
use polars_core::prelude::*;
#[derive(Clone, PartialEq)]
pub enum LiteralValue {
Null,
Boolean(bool),
Utf8(String),
#[cfg(feature = "dtype-u8")]
UInt8(u8),
#[cfg(feature = "dtype-u16")]
UInt16(u16),
UInt32(u32),
UInt64(u64),
#[cfg(feature = "dtype-i8")]
Int8(i8),
#[cfg(feature = "dtype-i16")]
Int16(i16),
Int32(i32),
Int64(i64),
Float32(f32),
Float64(f64),
Range {
low: i64,
high: i64,
data_type: DataType,
},
#[cfg(all(feature = "temporal", feature = "dtype-datetime"))]
DateTime(NaiveDateTime, TimeUnit),
#[cfg(all(feature = "temporal", feature = "dtype-duration"))]
Duration(ChronoDuration, TimeUnit),
Series(NoEq<Series>),
}
impl LiteralValue {
pub fn get_datatype(&self) -> DataType {
match self {
LiteralValue::Boolean(_) => DataType::Boolean,
#[cfg(feature = "dtype-u8")]
LiteralValue::UInt8(_) => DataType::UInt8,
#[cfg(feature = "dtype-u16")]
LiteralValue::UInt16(_) => DataType::UInt16,
LiteralValue::UInt32(_) => DataType::UInt32,
LiteralValue::UInt64(_) => DataType::UInt64,
#[cfg(feature = "dtype-i8")]
LiteralValue::Int8(_) => DataType::Int8,
#[cfg(feature = "dtype-i16")]
LiteralValue::Int16(_) => DataType::Int16,
LiteralValue::Int32(_) => DataType::Int32,
LiteralValue::Int64(_) => DataType::Int64,
LiteralValue::Float32(_) => DataType::Float32,
LiteralValue::Float64(_) => DataType::Float64,
LiteralValue::Utf8(_) => DataType::Utf8,
LiteralValue::Range { data_type, .. } => data_type.clone(),
#[cfg(all(feature = "temporal", feature = "dtype-datetime"))]
LiteralValue::DateTime(_, tu) => DataType::Datetime(*tu, None),
#[cfg(all(feature = "temporal", feature = "dtype-duration"))]
LiteralValue::Duration(_, tu) => DataType::Duration(*tu),
LiteralValue::Series(s) => s.dtype().clone(),
LiteralValue::Null => DataType::Null,
}
}
}
pub trait Literal {
fn lit(self) -> Expr;
}
impl Literal for String {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::Utf8(self))
}
}
impl<'a> Literal for &'a str {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::Utf8(self.to_owned()))
}
}
macro_rules! make_literal {
($TYPE:ty, $SCALAR:ident) => {
impl Literal for $TYPE {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::$SCALAR(self))
}
}
};
}
make_literal!(bool, Boolean);
make_literal!(f32, Float32);
make_literal!(f64, Float64);
#[cfg(feature = "dtype-i8")]
make_literal!(i8, Int8);
#[cfg(feature = "dtype-i16")]
make_literal!(i16, Int16);
make_literal!(i32, Int32);
make_literal!(i64, Int64);
#[cfg(feature = "dtype-u8")]
make_literal!(u8, UInt8);
#[cfg(feature = "dtype-u16")]
make_literal!(u16, UInt16);
make_literal!(u32, UInt32);
make_literal!(u64, UInt64);
pub struct Null {}
pub const NULL: Null = Null {};
impl Literal for Null {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::Null)
}
}
#[cfg(all(feature = "temporal", feature = "dtype-datetime"))]
impl Literal for NaiveDateTime {
fn lit(self) -> Expr {
if in_nanoseconds_window(&self) {
Expr::Literal(LiteralValue::DateTime(self, TimeUnit::Nanoseconds))
} else {
Expr::Literal(LiteralValue::DateTime(self, TimeUnit::Milliseconds))
}
}
}
#[cfg(all(feature = "temporal", feature = "dtype-duration"))]
impl Literal for ChronoDuration {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::Duration(self, TimeUnit::Nanoseconds))
}
}
#[cfg(all(feature = "temporal", feature = "dtype-datetime"))]
impl Literal for NaiveDate {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::DateTime(
self.and_hms(0, 0, 0),
TimeUnit::Milliseconds,
))
}
}
impl Literal for Series {
fn lit(self) -> Expr {
Expr::Literal(LiteralValue::Series(NoEq::new(self)))
}
}
pub fn lit<L: Literal>(t: L) -> Expr {
t.lit()
}