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
#[cfg(feature = "list_eval")]
use std::sync::Mutex;
#[cfg(feature = "list_eval")]
use polars_arrow::utils::CustomIterTools;
#[cfg(feature = "list_eval")]
use polars_core::prelude::*;
#[cfg(feature = "list_eval")]
use polars_plan::dsl::*;
#[cfg(feature = "list_eval")]
use rayon::prelude::*;
use crate::prelude::*;
pub trait IntoListNameSpace {
fn into_list_name_space(self) -> ListNameSpace;
}
impl IntoListNameSpace for ListNameSpace {
fn into_list_name_space(self) -> ListNameSpace {
self
}
}
pub trait ListNameSpaceExtension: IntoListNameSpace + Sized {
#[cfg(feature = "list_eval")]
fn eval(self, expr: Expr, parallel: bool) -> Expr {
let this = self.into_list_name_space();
use crate::physical_plan::exotic::prepare_expression_for_context;
use crate::physical_plan::state::ExecutionState;
let expr2 = expr.clone();
let func = move |s: Series| {
for e in expr.into_iter() {
match e {
#[cfg(feature = "dtype-categorical")]
Expr::Cast {
data_type: DataType::Categorical(_),
..
} => {
return Err(PolarsError::ComputeError(
"Casting to 'Categorical' not allowed in 'arr.eval'".into(),
))
}
Expr::Column(name) => {
if !name.is_empty() {
return Err(PolarsError::ComputeError(r#"Named columns not allowed in 'arr.eval'. Consider using 'element' or 'col("")'."#.into()));
}
}
_ => {}
}
}
let lst = s.list()?;
if lst.is_empty() {
let fld = field_to_dtype(lst.ref_field(), &expr);
return Ok(Series::new_empty(s.name(), fld.data_type()));
}
let phys_expr =
prepare_expression_for_context("", &expr, &lst.inner_dtype(), Context::Default)?;
let state = ExecutionState::new();
let mut err = None;
let mut ca: ListChunked = if parallel {
let m_err = Mutex::new(None);
let ca: ListChunked = lst
.par_iter()
.map(|opt_s| {
opt_s.and_then(|s| {
let df = DataFrame::new_no_checks(vec![s]);
let out = phys_expr.evaluate(&df, &state);
match out {
Ok(s) => Some(s),
Err(e) => {
*m_err.lock().unwrap() = Some(e);
None
}
}
})
})
.collect();
err = m_err.lock().unwrap().take();
ca
} else {
let mut df_container = DataFrame::new_no_checks(vec![]);
lst.into_iter()
.map(|s| {
s.and_then(|s| {
df_container.get_columns_mut().push(s);
let out = phys_expr.evaluate(&df_container, &state);
df_container.get_columns_mut().clear();
match out {
Ok(s) => Some(s),
Err(e) => {
err = Some(e);
None
}
}
})
})
.collect_trusted()
};
ca.rename(s.name());
match err {
None => Ok(ca.into_series()),
Some(e) => Err(e),
}
};
this.0
.map(
func,
GetOutput::map_field(move |f| field_to_dtype(f, &expr2)),
)
.with_fmt("eval")
}
}
#[cfg(feature = "list_eval")]
fn field_to_dtype(f: &Field, expr: &Expr) -> Field {
let dtype = f
.data_type()
.inner_dtype()
.cloned()
.unwrap_or_else(|| f.data_type().clone());
let df = Series::new_empty("", &dtype).into_frame();
#[cfg(feature = "python")]
let out = {
use pyo3::Python;
Python::with_gil(|py| py.allow_threads(|| df.lazy().select([expr.clone()]).collect()))
};
#[cfg(not(feature = "python"))]
let out = { df.lazy().select([expr.clone()]).collect() };
match out {
Ok(out) => {
let dtype = out.get_columns()[0].dtype();
Field::new(f.name(), DataType::List(Box::new(dtype.clone())))
}
Err(_) => Field::new(f.name(), DataType::Null),
}
}
impl ListNameSpaceExtension for ListNameSpace {}