Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/format.rs
6939 views
1
use std::fmt;
2
3
use crate::prelude::*;
4
5
impl fmt::Display for Expr {
6
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7
fmt::Debug::fmt(self, f)
8
}
9
}
10
11
impl fmt::Debug for Expr {
12
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13
use Expr::*;
14
match self {
15
Window {
16
function,
17
partition_by,
18
order_by,
19
options,
20
} => match options {
21
#[cfg(feature = "dynamic_group_by")]
22
WindowType::Rolling(options) => {
23
write!(
24
f,
25
"{:?}.rolling(by='{}', offset={}, period={})",
26
function, options.index_column, options.offset, options.period
27
)
28
},
29
_ => {
30
if let Some((order_by, _)) = order_by {
31
write!(
32
f,
33
"{function:?}.over(partition_by: {partition_by:?}, order_by: {order_by:?})"
34
)
35
} else {
36
write!(f, "{function:?}.over({partition_by:?})")
37
}
38
},
39
},
40
DataTypeFunction(dtype_fn) => fmt::Debug::fmt(dtype_fn, f),
41
Len => write!(f, "len()"),
42
Explode {
43
input: expr,
44
skip_empty: false,
45
} => write!(f, "{expr:?}.explode()"),
46
Explode {
47
input: expr,
48
skip_empty: true,
49
} => write!(f, "{expr:?}.explode(skip_empty)"),
50
Alias(expr, name) => write!(f, "{expr:?}.alias(\"{name}\")"),
51
Column(name) => write!(f, "col(\"{name}\")"),
52
Literal(v) => write!(f, "{v:?}"),
53
BinaryExpr { left, op, right } => write!(f, "[({left:?}) {op:?} ({right:?})]"),
54
Sort { expr, options } => {
55
if options.descending {
56
write!(f, "{expr:?}.sort(desc)")
57
} else {
58
write!(f, "{expr:?}.sort(asc)")
59
}
60
},
61
SortBy {
62
expr,
63
by,
64
sort_options,
65
} => {
66
write!(
67
f,
68
"{expr:?}.sort_by(by={by:?}, sort_option={sort_options:?})",
69
)
70
},
71
Filter { input, by } => {
72
write!(f, "{input:?}.filter({by:?})")
73
},
74
Gather {
75
expr,
76
idx,
77
returns_scalar,
78
} => {
79
if *returns_scalar {
80
write!(f, "{expr:?}.get({idx:?})")
81
} else {
82
write!(f, "{expr:?}.gather({idx:?})")
83
}
84
},
85
SubPlan(lf, _) => {
86
write!(f, ".subplan({lf:?})")
87
},
88
Agg(agg) => {
89
use AggExpr::*;
90
match agg {
91
Min {
92
input,
93
propagate_nans,
94
} => {
95
if *propagate_nans {
96
write!(f, "{input:?}.nan_min()")
97
} else {
98
write!(f, "{input:?}.min()")
99
}
100
},
101
Max {
102
input,
103
propagate_nans,
104
} => {
105
if *propagate_nans {
106
write!(f, "{input:?}.nan_max()")
107
} else {
108
write!(f, "{input:?}.max()")
109
}
110
},
111
Median(expr) => write!(f, "{expr:?}.median()"),
112
Mean(expr) => write!(f, "{expr:?}.mean()"),
113
First(expr) => write!(f, "{expr:?}.first()"),
114
Last(expr) => write!(f, "{expr:?}.last()"),
115
Implode(expr) => write!(f, "{expr:?}.list()"),
116
NUnique(expr) => write!(f, "{expr:?}.n_unique()"),
117
Sum(expr) => write!(f, "{expr:?}.sum()"),
118
AggGroups(expr) => write!(f, "{expr:?}.groups()"),
119
Count {
120
input,
121
include_nulls: false,
122
} => write!(f, "{input:?}.count()"),
123
Count {
124
input,
125
include_nulls: true,
126
} => write!(f, "{input:?}.len()"),
127
Var(expr, _) => write!(f, "{expr:?}.var()"),
128
Std(expr, _) => write!(f, "{expr:?}.std()"),
129
Quantile { expr, .. } => write!(f, "{expr:?}.quantile()"),
130
}
131
},
132
Cast {
133
expr,
134
dtype,
135
options,
136
} => {
137
if options.is_strict() {
138
write!(f, "{expr:?}.strict_cast({dtype:?})")
139
} else {
140
write!(f, "{expr:?}.cast({dtype:?})")
141
}
142
},
143
Ternary {
144
predicate,
145
truthy,
146
falsy,
147
} => write!(
148
f,
149
".when({predicate:?}).then({truthy:?}).otherwise({falsy:?})",
150
),
151
Function { input, function } => {
152
if input.len() >= 2 {
153
write!(f, "{:?}.{function}({:?})", input[0], &input[1..])
154
} else {
155
write!(f, "{:?}.{function}()", input[0])
156
}
157
},
158
AnonymousFunction {
159
input,
160
fmt_str,
161
function,
162
..
163
} => {
164
let name = match function {
165
LazySerde::Named { name, .. } => name.as_str(),
166
_ => fmt_str.as_str(),
167
};
168
169
if input.len() >= 2 {
170
write!(f, "{:?}.{}({:?})", input[0], name, &input[1..])
171
} else {
172
write!(f, "{:?}.{}()", input[0], name)
173
}
174
},
175
Eval {
176
expr: input,
177
evaluation,
178
variant,
179
} => match variant {
180
EvalVariant::List => write!(f, "{input:?}.list.eval({evaluation:?})"),
181
EvalVariant::Cumulative { min_samples } => write!(
182
f,
183
"{input:?}.Cumulative_eval({evaluation:?}, min_samples={min_samples}"
184
),
185
},
186
Slice {
187
input,
188
offset,
189
length,
190
} => write!(f, "{input:?}.slice(offset={offset:?}, length={length:?})",),
191
KeepName(e) => write!(f, "{e:?}.name.keep()"),
192
RenameAlias { expr, function } => match function {
193
RenameAliasFn::Prefix(s) => write!(f, "{expr:?}.prefix({s})"),
194
RenameAliasFn::Suffix(s) => write!(f, "{expr:?}.suffix({s})"),
195
RenameAliasFn::ToLowercase => write!(f, "{expr:?}.to_lowercase()"),
196
RenameAliasFn::ToUppercase => write!(f, "{expr:?}.to_uppercase()"),
197
#[cfg(feature = "python")]
198
RenameAliasFn::Python(_) => write!(f, "{expr:?}.rename_alias()"),
199
RenameAliasFn::Rust(_) => write!(f, "{expr:?}.rename_alias()"),
200
},
201
Selector(s) => fmt::Display::fmt(s, f),
202
#[cfg(feature = "dtype-struct")]
203
Field(names) => write!(f, "pl.field({names:?})"),
204
}
205
}
206
}
207
208