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
#[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")]
#[cfg_attr(docsrs, doc(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_eval_expr, prepare_expression_for_context};
use crate::physical_plan::state::ExecutionState;
let expr = prepare_eval_expr(expr);
let expr2 = expr.clone();
let func = move |s: Series| {
let lst = s.list()?;
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| {
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([expr2.clone()]).collect())
})
};
#[cfg(not(feature = "python"))]
let out = { df.lazy().select([expr2.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),
}
}),
)
.with_fmt("eval")
}
}
impl ListNameSpaceExtension for ListNameSpace {}