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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Aggregate input kernels: straight-line reductions over a vector,
//! validity- and selection-aware. These feed the aggregate operators;
//! the grouping machinery lives with the executor.
use crate::sel::SelVector;
use crate::vector::{ValueVector, VecEncoding};
/// Wrapping sum of an Int64-domain vector. The no-null no-selection flat
/// case is the B3 scan+sum inner loop and must stay a bare reduction the
/// auto-vectorizer folds.
pub fn sum_i64(v: &ValueVector, sel: Option<&SelVector>) -> i64 {
match (v.encoding, sel, &v.validity) {
(VecEncoding::Constant, sel, _) => {
let n = match sel {
Some(s) => s.len() as i64,
None => i64::from(v.len),
};
v.constant_value::<i64>().wrapping_mul(n)
}
(VecEncoding::Flat, None, None) => {
let mut acc = 0i64;
for &x in v.values::<i64>() {
acc = acc.wrapping_add(x);
}
acc
}
(VecEncoding::Flat, None, Some(valid)) => {
// Branch-free: multiply each value by its validity bit.
let mut acc = 0i64;
for (i, &x) in v.values::<i64>().iter().enumerate() {
acc = acc.wrapping_add(x.wrapping_mul(i64::from(valid.get(i))));
}
acc
}
(VecEncoding::Flat, Some(s), _) => {
let vals = v.values::<i64>();
let mut acc = 0i64;
for &row in s.as_slice() {
let i = row as usize;
if v.is_valid(i) {
acc = acc.wrapping_add(vals[i]);
}
}
acc
}
(VecEncoding::Dict { .. }, _, _) => unreachable!("no i64 dict vectors"),
}
}
pub fn sum_f64(v: &ValueVector, sel: Option<&SelVector>) -> f64 {
match (v.encoding, sel) {
(VecEncoding::Constant, sel) => {
let n = match sel {
Some(s) => s.len() as f64,
None => f64::from(v.len),
};
v.constant_value::<f64>() * n
}
(VecEncoding::Flat, None) => {
let mut acc = 0f64;
for (i, &x) in v.values::<f64>().iter().enumerate() {
if v.is_valid(i) {
acc += x;
}
}
acc
}
(VecEncoding::Flat, Some(s)) => {
let vals = v.values::<f64>();
let mut acc = 0f64;
for &row in s.as_slice() {
let i = row as usize;
if v.is_valid(i) {
acc += vals[i];
}
}
acc
}
(VecEncoding::Dict { .. }, _) => unreachable!("no f64 dict vectors"),
}
}
/// Non-null row count under the selection.
pub fn count_valid(v: &ValueVector, sel: Option<&SelVector>) -> usize {
match (&v.validity, sel) {
(None, None) => v.len(),
(None, Some(s)) => s.len(),
(Some(valid), None) => valid.count_ones(),
(Some(valid), Some(s)) => s
.as_slice()
.iter()
.filter(|&&row| valid.get(row as usize))
.count(),
}
}
macro_rules! minmax {
($name:ident, $better:tt) => {
/// Extreme of the valid rows, None when every row is null.
pub fn $name(v: &ValueVector, sel: Option<&SelVector>) -> Option<i64> {
let mut best: Option<i64> = None;
let mut consider = |x: i64| match best {
Some(b) if !(x $better b) => {}
_ => best = Some(x),
};
match (v.encoding, sel) {
(VecEncoding::Constant, _) => {
if count_valid(v, sel) > 0 {
consider(v.constant_value::<i64>());
}
}
// A column with nothing missing in it, which is the
// scan this reduction is on the hot path of, and the
// one shape here written for the vectorizer rather than
// for the reader. The accumulator is an i64 and not an
// Option of one, and the row is a select rather than a
// branch, which is the pair of things that turn into a
// lane wide minimum. The loop below it, folding an
// Option that is empty until the first row over a
// validity nobody here has to ask about, takes a row at
// a time and measured 0 vector instructions on both
// architectures; this one takes 31 on aarch64.
//
// Where it lands is the ISA's to say. NEON compares two
// 64 bit lanes with cmgt and selects with bsl, so the
// reduction folds. A generic x86-64 build has neither:
// the packed 64 bit compare is SSE4.2 and the packed 64
// bit minimum is AVX-512, and with only SSE2 to hand
// LLVM leaves the loop scalar. The disassembly gate
// knows that and asks for lanes on the one where they
// are reachable.
(VecEncoding::Flat, None) if v.validity.is_none() => {
let vals = v.values::<i64>();
if let Some((&first, rest)) = vals.split_first() {
let mut acc = first;
for &x in rest {
acc = if x $better acc { x } else { acc };
}
consider(acc);
}
}
(VecEncoding::Flat, None) => {
for (i, &x) in v.values::<i64>().iter().enumerate() {
if v.is_valid(i) {
consider(x);
}
}
}
(VecEncoding::Flat, Some(s)) => {
let vals = v.values::<i64>();
for &row in s.as_slice() {
if v.is_valid(row as usize) {
consider(vals[row as usize]);
}
}
}
(VecEncoding::Dict { .. }, _) => unreachable!("no i64 dict vectors"),
}
best
}
};
}
minmax!(min_i64, <);
minmax!(max_i64, >);
#[cfg(test)]
mod tests {
use super::*;
use crate::arena::MorselArena;
use crate::bitmap::Bitmap;
use crate::vector::PhysType;
#[test]
fn sums_and_extremes() {
let mut arena = MorselArena::new();
let v = ValueVector::flat_from(&mut arena, PhysType::Int64, &[3i64, -1, 7, 0]);
assert_eq!(sum_i64(&v, None), 9);
assert_eq!(min_i64(&v, None), Some(-1));
assert_eq!(max_i64(&v, None), Some(7));
assert_eq!(count_valid(&v, None), 4);
}
#[test]
fn nulls_are_skipped() {
let mut arena = MorselArena::new();
let mut v = ValueVector::flat_from(&mut arena, PhysType::Int64, &[5i64, 100, 2]);
let mut valid = Bitmap::new_in(&mut arena, 3, true);
valid.clear(1);
v.validity = Some(valid);
assert_eq!(sum_i64(&v, None), 7);
assert_eq!(max_i64(&v, None), Some(5));
assert_eq!(count_valid(&v, None), 2);
}
#[test]
fn constant_scales_by_count() {
let mut arena = MorselArena::new();
let v = ValueVector::constant(&mut arena, PhysType::Int64, 4i64, 10);
assert_eq!(sum_i64(&v, None), 40);
}
}