-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathcase_when_bench.rs
More file actions
297 lines (262 loc) · 9.04 KB
/
case_when_bench.rs
File metadata and controls
297 lines (262 loc) · 9.04 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#![allow(clippy::unwrap_used)]
#![allow(clippy::cast_possible_truncation)]
use std::sync::LazyLock;
use divan::Bencher;
use vortex_array::ArrayRef;
use vortex_array::Canonical;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::BoolArray;
use vortex_array::arrays::StructArray;
use vortex_array::expr::case_when;
use vortex_array::expr::case_when_no_else;
use vortex_array::expr::eq;
use vortex_array::expr::get_item;
use vortex_array::expr::gt;
use vortex_array::expr::lit;
use vortex_array::expr::lt;
use vortex_array::expr::nested_case_when;
use vortex_array::expr::root;
use vortex_array::session::ArraySession;
use vortex_buffer::Buffer;
use vortex_session::VortexSession;
static SESSION: LazyLock<VortexSession> =
LazyLock::new(|| VortexSession::empty().with::<ArraySession>());
fn main() {
divan::main();
}
fn make_struct_array(size: usize) -> ArrayRef {
let data: Buffer<i32> = (0..size as i32).collect();
let field = data.into_array();
StructArray::from_fields(&[("value", field)])
.unwrap()
.into_array()
}
/// Array with boolean columns cycling through thirds: `c0[i] = i%3==0`, `c1[i] = i%3==1`.
fn make_fragmented_array(size: usize) -> ArrayRef {
StructArray::from_fields(&[
(
"c0",
BoolArray::from_iter((0..size).map(|i| i % 3 == 0)).into_array(),
),
(
"c1",
BoolArray::from_iter((0..size).map(|i| i % 3 == 1)).into_array(),
),
])
.unwrap()
.into_array()
}
/// Benchmark a simple binary CASE WHEN with varying array sizes.
#[divan::bench(args = [1000, 10000, 100000])]
fn case_when_simple(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value > 500 THEN 100 ELSE 0 END
let expr = case_when(
gt(get_item("value", root()), lit(500i32)),
lit(100i32),
lit(0i32),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark n-ary CASE WHEN with 3 conditions.
#[divan::bench(args = [1000, 10000])]
fn case_when_nary_3_conditions(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value > 750 THEN 3 WHEN value > 500 THEN 2 WHEN value > 250 THEN 1 ELSE 0 END
let expr = nested_case_when(
vec![
(gt(get_item("value", root()), lit(750i32)), lit(3i32)),
(gt(get_item("value", root()), lit(500i32)), lit(2i32)),
(gt(get_item("value", root()), lit(250i32)), lit(1i32)),
],
Some(lit(0i32)),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark n-ary CASE WHEN with 10 conditions.
#[divan::bench(args = [1000, 10000])]
fn case_when_nary_10_conditions(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
let pairs: Vec<_> = (0..10)
.map(|i| {
let threshold = (i + 1) * (size as i32 / 10);
(
gt(get_item("value", root()), lit(threshold)),
lit((i + 1) * 100),
)
})
.collect();
let expr = nested_case_when(pairs, Some(lit(0i32)));
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark n-ary CASE WHEN with equality conditions (lookup-table style).
#[divan::bench(args = [1000, 10000])]
fn case_when_nary_equality_lookup(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// Map specific values: CASE WHEN value = 0 THEN 'a' WHEN value = 1 THEN 'b' ... ELSE 'other' END
let pairs: Vec<_> = (0..5)
.map(|i| (eq(get_item("value", root()), lit(i)), lit(i * 10)))
.collect();
let expr = nested_case_when(pairs, Some(lit(-1i32)));
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark CASE WHEN without ELSE clause (result is nullable).
#[divan::bench(args = [1000, 10000, 100000])]
fn case_when_without_else(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value > 500 THEN 100 END
let expr = case_when_no_else(gt(get_item("value", root()), lit(500i32)), lit(100i32));
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark CASE WHEN where all conditions are true.
#[divan::bench(args = [1000, 10000, 100000])]
fn case_when_all_true(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value >= 0 THEN 100 ELSE 0 END (always true for our data)
let expr = case_when(
gt(get_item("value", root()), lit(-1i32)),
lit(100i32),
lit(0i32),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark n-ary CASE WHEN where the first branch dominates (~90% of rows).
/// This highlights the early-exit and deferred-merge optimizations: subsequent conditions
/// match no remaining rows and are skipped entirely.
#[divan::bench(args = [1000, 10000])]
fn case_when_nary_early_dominant(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value < 90% THEN 1 WHEN value < 95% THEN 2 WHEN value < 97.5% THEN 3 ELSE 4
let t1 = (size as i32 * 9) / 10;
let t2 = (size as i32 * 19) / 20;
let t3 = (size as i32 * 39) / 40;
let expr = nested_case_when(
vec![
(lt(get_item("value", root()), lit(t1)), lit(1i32)),
(lt(get_item("value", root()), lit(t2)), lit(2i32)),
(lt(get_item("value", root()), lit(t3)), lit(3i32)),
],
Some(lit(4i32)),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark CASE WHEN where all conditions are false.
#[divan::bench(args = [1000, 10000, 100000])]
fn case_when_all_false(bencher: Bencher, size: usize) {
let array = make_struct_array(size);
// CASE WHEN value > 1000000 THEN 100 ELSE 0 END (always false for our data)
let expr = case_when(
gt(get_item("value", root()), lit(1_000_000i32)),
lit(100i32),
lit(0i32),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}
/// Benchmark CASE WHEN cycling through 3 branches per row (triggers merge_row_by_row).
/// Run length = 1; exercises branch 0, branch 1, and the else fallback at every 3rd row.
#[divan::bench(args = [100, 1000])]
fn case_when_fragmented(bencher: Bencher, size: usize) {
let array = make_fragmented_array(size);
// CASE WHEN c0 THEN 0 WHEN c1 THEN 1 ELSE 2 END
let expr = nested_case_when(
vec![
(get_item("c0", root()), lit(0i32)),
(get_item("c1", root()), lit(1i32)),
],
Some(lit(2i32)),
);
bencher
.with_inputs(|| (&expr, &array))
.bench_refs(|(expr, array)| {
let mut ctx = SESSION.create_execution_ctx();
array
.clone()
.apply(expr)
.unwrap()
.execute::<Canonical>(&mut ctx)
.unwrap()
});
}