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
use arrow::array::{PrimitiveArray, Utf8Array};
use arrow::types::NativeType;
use num::{NumCast, ToPrimitive};
use crate::array::PolarsArray;
use crate::index::IdxSize;
#[inline]
pub unsafe fn take_agg_no_null_primitive_iter_unchecked<
T: NativeType + ToPrimitive,
TOut: NumCast + NativeType,
I: IntoIterator<Item = usize>,
F: Fn(TOut, TOut) -> TOut,
>(
arr: &PrimitiveArray<T>,
indices: I,
f: F,
init: TOut,
) -> TOut {
debug_assert!(!arr.has_validity());
let array_values = arr.values().as_slice();
indices.into_iter().fold(init, |acc, idx| {
f(
acc,
NumCast::from(*array_values.get_unchecked(idx)).unwrap_unchecked(),
)
})
}
#[inline]
pub unsafe fn take_agg_primitive_iter_unchecked<
T: NativeType,
I: IntoIterator<Item = usize>,
F: Fn(T, T) -> T,
>(
arr: &PrimitiveArray<T>,
indices: I,
f: F,
init: T,
len: IdxSize,
) -> Option<T> {
let array_values = arr.values().as_slice();
let validity = arr.validity().expect("null buffer should be there");
let mut null_count = 0 as IdxSize;
let out = indices.into_iter().fold(init, |acc, idx| {
if validity.get_bit_unchecked(idx) {
f(acc, *array_values.get_unchecked(idx))
} else {
null_count += 1;
acc
}
});
if null_count == len {
None
} else {
Some(out)
}
}
#[inline]
pub unsafe fn take_agg_primitive_iter_unchecked_count_nulls<
T: NativeType + ToPrimitive,
TOut: NumCast + NativeType,
I: IntoIterator<Item = usize>,
F: Fn(TOut, TOut) -> TOut,
>(
arr: &PrimitiveArray<T>,
indices: I,
f: F,
init: TOut,
len: IdxSize,
) -> Option<(TOut, IdxSize)> {
let array_values = arr.values().as_slice();
let validity = arr.validity().expect("null buffer should be there");
let mut null_count = 0 as IdxSize;
let out = indices.into_iter().fold(init, |acc, idx| {
if validity.get_bit_unchecked(idx) {
f(
acc,
NumCast::from(*array_values.get_unchecked(idx)).unwrap_unchecked(),
)
} else {
null_count += 1;
acc
}
});
if null_count == len {
None
} else {
Some((out, null_count))
}
}
#[inline]
pub unsafe fn take_agg_utf8_iter_unchecked<
'a,
I: IntoIterator<Item = usize>,
F: Fn(&'a str, &'a str) -> &'a str,
>(
arr: &'a Utf8Array<i64>,
indices: I,
f: F,
len: IdxSize,
) -> Option<&str> {
let mut null_count = 0 as IdxSize;
let validity = arr.validity().unwrap();
let out = indices
.into_iter()
.map(|idx| {
if validity.get_bit_unchecked(idx) {
Some(arr.value_unchecked(idx))
} else {
None
}
})
.reduce(|acc, opt_val| match (acc, opt_val) {
(Some(acc), Some(str_val)) => Some(f(acc, str_val)),
(_, None) => {
null_count += 1;
acc
}
(None, Some(str_val)) => Some(str_val),
});
if null_count == len {
None
} else {
out.flatten()
}
}
#[inline]
pub unsafe fn take_agg_utf8_iter_unchecked_no_null<
'a,
I: IntoIterator<Item = usize>,
F: Fn(&'a str, &'a str) -> &'a str,
>(
arr: &'a Utf8Array<i64>,
indices: I,
f: F,
) -> Option<&str> {
indices
.into_iter()
.map(|idx| arr.value_unchecked(idx))
.reduce(|acc, str_val| f(acc, str_val))
}