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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use super::*;
impl VersionStore {
/// Compute COUNT(*) without materializing any rows
///
/// This is the FASTEST path for `SELECT COUNT(*) FROM table`:
/// - No row data is loaded
/// - Only visibility checks are performed
/// - O(n) time, O(1) memory
#[inline]
pub fn count_visible(&self, txn_id: i64) -> usize {
self.count_visible_rows(txn_id)
}
/// Compute SUM(column) without materializing full rows
///
/// OPTIMIZATION: Single-pass approach that combines visibility checking with summing.
/// Avoids Vec allocation and second iteration over indices.
/// Returns (sum, count_non_null) for proper NULL handling.
pub fn sum_column(&self, txn_id: i64, col_idx: usize) -> crate::traits::table::DeferredSum {
use crate::traits::table::DeferredSum;
let checker = match self.visibility_checker.as_ref() {
Some(c) => c,
None => return DeferredSum::new(),
};
// OPTIMIZATION: Separate accumulators to avoid i64->f64 conversion per row.
// Uses i128 for integer accumulation to prevent overflow (i128 holds sum of
// 2^63 rows of i64::MAX without overflow).
let mut sum = DeferredSum::new();
// Helper to accumulate numeric value
#[inline(always)]
fn accumulate_sum(sum: &mut DeferredSum, val: &Value) {
sum.add_value(val);
}
// FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
// SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
//
// LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
// consistent ordering with truncate_all (uncommitted_writes → arena).
let uncommitted_empty = self.uncommitted_writes.read().is_empty();
{
let arena_guard = self.arena.read_guard();
let arena_meta = arena_guard.meta();
let arena_data = arena_guard.data();
let arena_len = arena_guard.len();
if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
// OPTIMIZATION: Cache visibility result for repeated txn_ids
// When rows are inserted in batches, consecutive rows often have the same txn_id.
// Caching avoids repeated thread-local access overhead (~27ms per 100K rows).
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
let mut idx = 0;
while idx < arena_len {
let meta = &arena_meta[idx];
if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
// Check visibility with cache
let version_txn_id = meta.txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if let Some(val) = arena_data[idx].get(col_idx) {
accumulate_sum(&mut sum, val);
}
}
}
idx += 1;
}
return sum;
}
// arena_guard dropped here — no need to hold it for slow path
}
// SLOW PATH: Full iteration over version chains (single pass)
let versions = self.versions.read().clone();
// Cache visibility for slow path too
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
for chain in versions.values() {
let mut current: Option<&VersionChainEntry> = Some(chain);
while let Some(e) = current {
let version_txn_id = e.version.txn_id;
let deleted_at_txn_id = e.version.deleted_at_txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
// This version is visible and not deleted - accumulate its value
if let Some(val) = e.version.data.get(col_idx) {
accumulate_sum(&mut sum, val);
}
}
break; // Always break on first visible version
}
current = e.prev.as_deref();
}
}
sum
}
/// Compute MIN(column) without materializing full rows
///
/// OPTIMIZATION: Single-pass approach that combines visibility checking with min computation.
pub fn min_column(&self, txn_id: i64, col_idx: usize) -> Option<Value> {
let checker = self.visibility_checker.as_ref()?;
let mut min_val: Option<Value> = None;
// Helper to update min value
#[inline(always)]
fn update_min(min_val: &mut Option<Value>, val: &Value) {
if !val.is_null() {
match min_val {
None => *min_val = Some(val.clone()),
Some(ref current) => {
if let Ok(std::cmp::Ordering::Less) = val.compare(current) {
*min_val = Some(val.clone());
}
}
}
}
}
// FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
// SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
//
// LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
// consistent ordering with truncate_all (uncommitted_writes → arena).
let uncommitted_empty = self.uncommitted_writes.read().is_empty();
{
let arena_guard = self.arena.read_guard();
let arena_meta = arena_guard.meta();
let arena_data = arena_guard.data();
let arena_len = arena_guard.len();
if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
// OPTIMIZATION: Cache visibility result for repeated txn_ids
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
let mut idx = 0;
while idx < arena_len {
let meta = &arena_meta[idx];
if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
let version_txn_id = meta.txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if let Some(val) = arena_data[idx].get(col_idx) {
update_min(&mut min_val, val);
}
}
}
idx += 1;
}
return min_val;
}
// arena_guard dropped here — no need to hold it for slow path
}
// SLOW PATH: Full iteration over version chains (single pass)
let versions = self.versions.read().clone();
// Cache visibility for slow path too
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
for chain in versions.values() {
let mut current: Option<&VersionChainEntry> = Some(chain);
while let Some(e) = current {
let version_txn_id = e.version.txn_id;
let deleted_at_txn_id = e.version.deleted_at_txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
if let Some(val) = e.version.data.get(col_idx) {
update_min(&mut min_val, val);
}
}
break; // Always break on first visible version
}
current = e.prev.as_deref();
}
}
min_val
}
/// Compute MAX(column) without materializing full rows
///
/// OPTIMIZATION: Single-pass approach that combines visibility checking with max computation.
pub fn max_column(&self, txn_id: i64, col_idx: usize) -> Option<Value> {
let checker = self.visibility_checker.as_ref()?;
let mut max_val: Option<Value> = None;
// Helper to update max value
#[inline(always)]
fn update_max(max_val: &mut Option<Value>, val: &Value) {
if !val.is_null() {
match max_val {
None => *max_val = Some(val.clone()),
Some(ref current) => {
if let Ok(std::cmp::Ordering::Greater) = val.compare(current) {
*max_val = Some(val.clone());
}
}
}
}
}
// FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
// SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
//
// LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
// consistent ordering with truncate_all (uncommitted_writes → arena).
let uncommitted_empty = self.uncommitted_writes.read().is_empty();
{
let arena_guard = self.arena.read_guard();
let arena_meta = arena_guard.meta();
let arena_data = arena_guard.data();
let arena_len = arena_guard.len();
if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
// OPTIMIZATION: Cache visibility result for repeated txn_ids
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
let mut idx = 0;
while idx < arena_len {
let meta = &arena_meta[idx];
if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
let version_txn_id = meta.txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if let Some(val) = arena_data[idx].get(col_idx) {
update_max(&mut max_val, val);
}
}
}
idx += 1;
}
return max_val;
}
// arena_guard dropped here — no need to hold it for slow path
}
// SLOW PATH: Full iteration over version chains (single pass)
let versions = self.versions.read().clone();
// Cache visibility for slow path too
let mut last_txn_id: i64 = 0;
let mut last_visible: bool = false;
for chain in versions.values() {
let mut current: Option<&VersionChainEntry> = Some(chain);
while let Some(e) = current {
let version_txn_id = e.version.txn_id;
let deleted_at_txn_id = e.version.deleted_at_txn_id;
let is_vis = if version_txn_id == last_txn_id {
last_visible
} else {
let vis = checker.is_visible(version_txn_id, txn_id);
last_txn_id = version_txn_id;
last_visible = vis;
vis
};
if is_vis {
if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
if let Some(val) = e.version.data.get(col_idx) {
update_max(&mut max_val, val);
}
}
break; // Always break on first visible version
}
current = e.prev.as_deref();
}
}
max_val
}
/// Compute multiple column aggregates in a single pass
///
/// This is the most efficient path for queries like:
/// `SELECT SUM(a), AVG(b), MIN(c), MAX(d) FROM table`
///
/// Returns aggregates in the order requested.
pub fn compute_aggregates(
&self,
txn_id: i64,
aggregates: &[(AggregateOp, usize)], // (operation, column_index)
) -> Vec<AggregateResult> {
let empty_result = || {
aggregates
.iter()
.map(|(op, _)| match op {
AggregateOp::Count | AggregateOp::CountStar => AggregateResult::Count(0),
AggregateOp::Sum => AggregateResult::Sum(0.0, 0),
AggregateOp::Min => AggregateResult::Min(None),
AggregateOp::Max => AggregateResult::Max(None),
AggregateOp::Avg => AggregateResult::Avg(0.0, 0),
})
.collect()
};
let checker = match self.visibility_checker.as_ref() {
Some(c) => c,
None => return empty_result(),
};
// Initialize accumulators
let mut results: Vec<AggregateAccumulator> = aggregates
.iter()
.map(|(op, _)| match op {
AggregateOp::Count | AggregateOp::CountStar => AggregateAccumulator::Count(0),
AggregateOp::Sum => AggregateAccumulator::Sum(0, 0.0, 0),
AggregateOp::Min => AggregateAccumulator::Min(None),
AggregateOp::Max => AggregateAccumulator::Max(None),
AggregateOp::Avg => AggregateAccumulator::Avg(0, 0.0, 0),
})
.collect();
// Helper to update accumulator with a value
fn update_accumulator(acc: &mut AggregateAccumulator, op: &AggregateOp, val: &Value) {
match (acc, op) {
(AggregateAccumulator::Count(c), AggregateOp::Count) if !val.is_null() => {
*c += 1;
}
(AggregateAccumulator::Count(c), AggregateOp::CountStar) => {
*c += 1;
}
(AggregateAccumulator::Sum(int_sum, float_sum, cnt), AggregateOp::Sum) => match val
{
Value::Integer(i) => {
*int_sum += *i as i128;
*cnt += 1;
}
Value::Float(f) => {
*float_sum += *f;
*cnt += 1;
}
_ => {}
},
(AggregateAccumulator::Min(min), AggregateOp::Min) if !val.is_null() => match min {
None => *min = Some(val.clone()),
Some(current) => {
if let Ok(std::cmp::Ordering::Less) = val.compare(current) {
*min = Some(val.clone());
}
}
},
(AggregateAccumulator::Max(max), AggregateOp::Max) if !val.is_null() => match max {
None => *max = Some(val.clone()),
Some(current) => {
if let Ok(std::cmp::Ordering::Greater) = val.compare(current) {
*max = Some(val.clone());
}
}
},
(AggregateAccumulator::Avg(int_sum, float_sum, cnt), AggregateOp::Avg) => match val
{
Value::Integer(i) => {
*int_sum += *i as i128;
*cnt += 1;
}
Value::Float(f) => {
*float_sum += *f;
*cnt += 1;
}
_ => {}
},
_ => {}
}
}
// Helper macro: accumulate values from a row-like source by column index
macro_rules! accumulate_from {
($src:expr, $results:expr, $aggregates:expr) => {
for (i, (op, col_idx)) in $aggregates.iter().enumerate() {
if let Some(val) = $src.get(*col_idx) {
update_accumulator(&mut $results[i], op, val);
}
}
};
}
// FAST PATH: Scan arena directly when no uncommitted writes.
// SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
let uncommitted_empty = self.uncommitted_writes.read().is_empty();
if uncommitted_empty && !checker.needs_snapshot_isolation(txn_id) {
let arena_guard = self.arena.read_guard();
let arena_meta = arena_guard.meta();
let arena_data = arena_guard.data();
let arena_len = arena_guard.len();
if arena_len > 0 {
for (idx, meta) in arena_meta.iter().enumerate() {
if meta.txn_id != 0
&& meta.deleted_at_txn_id == 0
&& checker.is_visible(meta.txn_id, txn_id)
{
if let Some(arc_row) = arena_data.get(idx) {
accumulate_from!(arc_row, results, aggregates);
}
}
}
return results
.into_iter()
.map(|acc| match acc {
AggregateAccumulator::Count(c) => AggregateResult::Count(c),
AggregateAccumulator::Sum(is, fs, c) => {
AggregateResult::Sum(is as f64 + fs, c)
}
AggregateAccumulator::Min(v) => AggregateResult::Min(v),
AggregateAccumulator::Max(v) => AggregateResult::Max(v),
AggregateAccumulator::Avg(is, fs, c) => {
AggregateResult::Avg(is as f64 + fs, c)
}
})
.collect();
}
}
// SLOW PATH: CowBTree iteration with arena data retrieval
let versions = self.versions.read().clone();
let arena_guard = self.arena.read_guard();
let arena_data = arena_guard.data();
for chain in versions.values() {
let mut current: Option<&VersionChainEntry> = Some(chain);
while let Some(e) = current {
if checker.is_visible(e.version.txn_id, txn_id) {
if e.version.deleted_at_txn_id == 0
|| !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
{
if let Some(idx) = unpack_arena_idx(e.arena_idx) {
if let Some(arc_row) = arena_data.get(idx) {
accumulate_from!(arc_row, results, aggregates);
} else {
accumulate_from!(e.version.data, results, aggregates);
}
} else {
accumulate_from!(e.version.data, results, aggregates);
}
}
break;
}
current = e.prev.as_ref().map(|b| b.as_ref());
}
}
// Convert accumulators to results
results
.into_iter()
.map(|acc| match acc {
AggregateAccumulator::Count(c) => AggregateResult::Count(c),
AggregateAccumulator::Sum(is, fs, c) => AggregateResult::Sum(is as f64 + fs, c),
AggregateAccumulator::Min(v) => AggregateResult::Min(v),
AggregateAccumulator::Max(v) => AggregateResult::Max(v),
AggregateAccumulator::Avg(is, fs, c) => AggregateResult::Avg(is as f64 + fs, c),
})
.collect()
}
}