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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Specialized read fast paths: single-column aggregate, project+filter+limit,
//! and project+filter+sort+limit over raw row bytes.
use crate::cancel::CancelCheck;
use crate::result::{QueryError, QueryResult};
use powdb_storage::row::{decode_column, RowLayout};
use powdb_storage::types::*;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::ops::ControlFlow;
use crate::executor::compiled::*;
use crate::executor::row_body_base;
use crate::executor::Engine;
use super::*;
impl Engine {
// ─── Specialized fast paths ─────────────────────────────────────────────
//
// These methods are helpers for the `execute_plan` match arms above.
// Each returns `Ok(Some(result))` when the fast path fires, `Ok(None)`
// when the shape isn't supported (caller falls back to generic code).
/// Aggregate sum/avg/min/max over a single fixed-size i64 column, with
/// an optional compiled filter predicate. Walks raw row bytes — zero
/// per-row allocation. Uses i128 accumulator for sum/avg overflow safety.
pub(crate) fn agg_single_col_fast(
&self,
table: &str,
col: &str,
function: AggFunc,
predicate: Option<&Expr>,
) -> Result<Option<QueryResult>, QueryError> {
// Overflow safety (P0-4): this walks raw rehydrated bytes and would
// silently drop any row carrying a value too large to re-inline
// (>= 64KB), undercounting the aggregate. Fall back to the decoded path.
if self.catalog.table_has_overflow(table) {
return Ok(None);
}
let schema = self
.catalog
.schema(table)
.ok_or_else(|| QueryError::TableNotFound(table.to_string()))?
.clone();
let columns: Vec<String> = schema.columns.iter().map(|c| c.name.clone()).collect();
let col_idx = match schema.column_index(col) {
Some(i) => i,
None => return Ok(None),
};
// Only fast-path fixed-size numeric columns (Int/Float) for
// sum/avg/min/max/count. Mission D10: Float parity — prior version
// bailed on Float columns, forcing them through the generic row-
// decoding path that allocated a Vec<Value> per row and dispatched
// on Value::cmp for every compare. f64 decode is structurally the
// same as i64 (load 8 bytes, cast), so the fast path handles both.
let col_type = schema.columns[col_idx].type_id;
if col_type != TypeId::Int && col_type != TypeId::Float {
return Ok(None);
}
let fast = FastLayout::new(&schema);
// Mission C Phase 20b: inline the numeric-column reader instead of
// building a `Box<dyn Fn>`. Eliminates 100K vtable dispatches per
// 100K-row agg scan — every reader call folds directly into the
// hot loop below.
let byte_offset = match fast.fixed_offsets[col_idx] {
Some(o) => o,
None => return Ok(None),
};
let bitmap_byte = col_idx / 8;
let bitmap_bit = (col_idx % 8) as u32;
let body_data_offset = 2 + fast.bitmap_size + byte_offset;
// Optional compiled filter.
let compiled_pred: Option<CompiledPredicate> = match predicate {
Some(pred) => match compile_predicate(pred, &columns, &fast, &schema) {
Some(c) => Some(c),
None => return Ok(None), // let generic path handle it
},
None => None,
};
// Mission C Phase 20b: specialize the inner loop per aggregate
// function. The previous version ran a `match function { ... }`
// *inside* the closure, which kept LLVM from producing optimal
// scalar code for each variant (agg_max regressed ~23% vs the
// baseline Box<dyn Fn> version even though per-row vtable cost
// should have been strictly lower). Pushing the match out of the
// hot loop lets each specialized body fold cleanly into
// `for_each_row_raw` and removes a captured `AggFunc` + match
// dispatch per row.
//
// Mission D10: same specialisation applies to the Float branch.
// For Min/Max we use `f64::total_cmp` so the result matches
// `Value::Ord` — this is the same ordering ORDER BY and the
// top-N sort fast path use, keeping semantics consistent across
// read paths (NaN compares as greatest, -0.0 < +0.0 for
// deterministic tie-breaking).
//
// Mission D11 Phase 1: each inner loop now splits on presence of
// a predicate (`if let Some(pred) = &compiled_pred`) so the hot
// body never re-tests `Option` per row, and reads column bytes
// via `read_i64_unchecked` / `read_f64_unchecked` helpers that
// drop two bounds checks per row (null bitmap byte + value
// slice). Safety is carried by the `FastLayout` invariant that
// `data_offset + 8 <= row_len` for any fixed-size column; see
// the helper doc comments. Hot loops are macro-generated so the
// with-pred / no-pred split can't drift between variants.
let result = match col_type {
TypeId::Int => match function {
AggFunc::Sum | AggFunc::Avg => {
let mut sum_i128: i128 = 0;
let mut count: i64 = 0;
agg_int_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: i64| {
count += 1;
sum_i128 += v as i128;
}
);
if matches!(function, AggFunc::Sum) {
let clamped = sum_i128.clamp(i64::MIN as i128, i64::MAX as i128) as i64;
QueryResult::Scalar(Value::Int(clamped))
} else if count == 0 {
QueryResult::Scalar(Value::Empty)
} else {
let avg = (sum_i128 as f64) / (count as f64);
QueryResult::Scalar(Value::Float(avg))
}
}
AggFunc::Min => {
let mut min_v: Option<i64> = None;
agg_int_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: i64| {
min_v = Some(match min_v {
Some(m) => m.min(v),
None => v,
});
}
);
QueryResult::Scalar(min_v.map(Value::Int).unwrap_or(Value::Empty))
}
AggFunc::Max => {
let mut max_v: Option<i64> = None;
agg_int_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: i64| {
max_v = Some(match max_v {
Some(m) => m.max(v),
None => v,
});
}
);
QueryResult::Scalar(max_v.map(Value::Int).unwrap_or(Value::Empty))
}
AggFunc::Count => {
let mut count: i64 = 0;
agg_int_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|_v: i64| {
count += 1;
}
);
QueryResult::Scalar(Value::Int(count))
}
AggFunc::CountDistinct => {
let mut seen = rustc_hash::FxHashSet::default();
agg_int_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: i64| {
seen.insert(v);
}
);
QueryResult::Scalar(Value::Int(seen.len() as i64))
}
},
TypeId::Float => match function {
AggFunc::Sum => {
// Use a single f64 accumulator. Naive summation is
// sufficient for MVP parity; if precision becomes an
// issue on long scans we can upgrade to Kahan–Neumaier
// compensated sum (~2x scalar cost, zero error growth).
let mut sum: f64 = 0.0;
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: f64| {
sum += v;
}
);
QueryResult::Scalar(Value::Float(sum))
}
AggFunc::Avg => {
let mut sum: f64 = 0.0;
let mut count: i64 = 0;
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: f64| {
sum += v;
count += 1;
}
);
if count == 0 {
QueryResult::Scalar(Value::Empty)
} else {
QueryResult::Scalar(Value::Float(sum / count as f64))
}
}
AggFunc::Min => {
// `total_cmp` for deterministic NaN handling (matches
// Value::Ord). NaN compares greatest, so Min will
// correctly ignore it in favour of any finite value.
let mut min_v: Option<f64> = None;
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: f64| {
min_v = Some(match min_v {
Some(m) => {
if v.total_cmp(&m).is_lt() {
v
} else {
m
}
}
None => v,
});
}
);
QueryResult::Scalar(min_v.map(Value::Float).unwrap_or(Value::Empty))
}
AggFunc::Max => {
let mut max_v: Option<f64> = None;
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: f64| {
max_v = Some(match max_v {
Some(m) => {
if v.total_cmp(&m).is_gt() {
v
} else {
m
}
}
None => v,
});
}
);
QueryResult::Scalar(max_v.map(Value::Float).unwrap_or(Value::Empty))
}
AggFunc::Count => {
let mut count: i64 = 0;
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|_v: f64| {
count += 1;
}
);
QueryResult::Scalar(Value::Int(count))
}
AggFunc::CountDistinct => {
// Hash on `f64::to_bits` — matches `Value::Hash`, so
// distinct NaN bit patterns count as distinct and
// -0.0/+0.0 count as distinct. Consistent with how
// Float values are hashed in every other DISTINCT /
// GROUP BY path.
let mut seen = rustc_hash::FxHashSet::default();
agg_float_loop!(
self,
table,
compiled_pred,
bitmap_byte,
bitmap_bit,
body_data_offset,
|v: f64| {
seen.insert(v.to_bits());
}
);
QueryResult::Scalar(Value::Int(seen.len() as i64))
}
},
_ => unreachable!("type guard above restricts to Int/Float"),
};
Ok(Some(result))
}
/// `Project(Limit(Filter(SeqScan)))` and `Project(Limit(SeqScan))`.
/// Streams rows, decodes only projected columns, stops at the limit.
pub(crate) fn project_filter_limit_fast(
&self,
table: &str,
fields: &[ProjectField],
limit: usize,
predicate: Option<&Expr>,
) -> Result<Option<QueryResult>, QueryError> {
// Overflow safety (P0-4): raw-byte projection over rehydrated rows
// drops any row with a value too large to re-inline (>= 64KB) and
// cannot return such a value; fall back to the decoded generic path.
if self.catalog.table_has_overflow(table) {
return Ok(None);
}
let schema = self
.catalog
.schema(table)
.ok_or_else(|| QueryError::TableNotFound(table.to_string()))?
.clone();
let all_columns: Vec<String> = schema.columns.iter().map(|c| c.name.clone()).collect();
// Each projection field must be a simple `.field` reference for this
// fast path. Aliased or computed fields fall through.
let mut proj_indices: Vec<usize> = Vec::with_capacity(fields.len());
let mut proj_columns: Vec<String> = Vec::with_capacity(fields.len());
for f in fields {
let name = match &f.expr {
Expr::Field(n) => n.clone(),
_ => return Ok(None),
};
let idx = match all_columns.iter().position(|c| c == &name) {
Some(i) => i,
None => return Ok(None),
};
proj_indices.push(idx);
proj_columns.push(f.alias.clone().unwrap_or(name));
}
let fast = FastLayout::new(&schema);
let row_layout = RowLayout::new(&schema);
let compiled_pred: Option<CompiledPredicate> = match predicate {
Some(pred) => match compile_predicate(pred, &all_columns, &fast, &schema) {
Some(c) => Some(c),
None => return Ok(None),
},
None => None,
};
let mut out: Vec<Vec<Value>> = Vec::with_capacity(limit.min(1024));
// Mission D2: use try_for_each_row_raw to actually stop iterating
// once the limit is reached. The previous `done` flag only short-
// circuited the closure body, so a `limit 100` over 100K rows still
// walked all 100K slots — burning ~30x SQLite on scan_filter_project_top100.
// Cooperative cancellation: an unbounded (limit == usize::MAX) projected
// scan over a huge table must stay stoppable.
let mut cancel = CancelCheck::new();
let mut cancel_err: Option<QueryError> = None;
self.catalog
.try_for_each_row_raw(table, |_rid, data| {
if let Err(e) = cancel.tick() {
cancel_err = Some(e);
return ControlFlow::Break(());
}
if let Some(ref pred) = compiled_pred {
if !pred(data) {
return ControlFlow::Continue(());
}
}
let row: Vec<Value> = proj_indices
.iter()
.map(|&ci| decode_column(&schema, &row_layout, data, ci))
.collect();
out.push(row);
if out.len() >= limit {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.map_err(|e| QueryError::StorageError(e.to_string()))?;
if let Some(e) = cancel_err {
return Err(e);
}
Ok(Some(QueryResult::Rows {
columns: proj_columns,
rows: out,
}))
}
/// `Project(Limit(Sort(Filter(SeqScan))))` and `Project(Limit(Sort(SeqScan)))`.
/// Bounded top-N heap over the sort key. Only the sort key needs to be
/// read per row; projected columns are decoded only for the final
/// winning rows when the heap drains.
pub(crate) fn project_filter_sort_limit_fast(
&self,
table: &str,
fields: &[ProjectField],
sort_field: &str,
descending: bool,
limit: usize,
predicate: Option<&Expr>,
) -> Result<Option<QueryResult>, QueryError> {
// Overflow safety (P0-4): raw-byte scan drops/wraps >= 64KB values;
// let the decoded generic path handle v2-capable tables.
if self.catalog.table_has_overflow(table) {
return Ok(None);
}
if limit == 0 {
// Degenerate case — empty result. Let the generic path handle it
// for proper column naming.
return Ok(None);
}
// The top-N heaps never hold more than `limit` rows, but `limit` is an
// attacker-supplied literal (`order .x limit 99999999999`). Reserving
// that capacity up front would allocate gigabytes and abort the
// process before a single row is read. Cap the pre-allocation; the
// heaps still grow on demand up to the true `limit`.
const TOPN_PREALLOC_CAP: usize = 4096;
let prealloc = limit.min(TOPN_PREALLOC_CAP);
let schema = self
.catalog
.schema(table)
.ok_or_else(|| QueryError::TableNotFound(table.to_string()))?
.clone();
let all_columns: Vec<String> = schema.columns.iter().map(|c| c.name.clone()).collect();
// Sort key must be a fixed-size numeric column (Int or Float).
// Mission D10: extended from Int-only. Float sort keys use a
// sortable-u64 transform (see `f64_to_sortable_u64`) so the heap
// path stays keyed on `u64` and the whole branch shape is
// identical to the Int case — no new heap types, no `total_cmp`
// closures in the hot loop.
let sort_idx = match schema.column_index(sort_field) {
Some(i) => i,
None => return Ok(None),
};
let sort_col_type = schema.columns[sort_idx].type_id;
if sort_col_type != TypeId::Int && sort_col_type != TypeId::Float {
return Ok(None);
}
// Each projection field must be a simple `.field`.
let mut proj_indices: Vec<usize> = Vec::with_capacity(fields.len());
let mut proj_columns: Vec<String> = Vec::with_capacity(fields.len());
for f in fields {
let name = match &f.expr {
Expr::Field(n) => n.clone(),
_ => return Ok(None),
};
let idx = match all_columns.iter().position(|c| c == &name) {
Some(i) => i,
None => return Ok(None),
};
proj_indices.push(idx);
proj_columns.push(f.alias.clone().unwrap_or(name));
}
let fast = FastLayout::new(&schema);
let row_layout = RowLayout::new(&schema);
// Mission C Phase 20b: inline numeric-column reader (no Box<dyn Fn>).
let sort_byte_offset = match fast.fixed_offsets[sort_idx] {
Some(o) => o,
None => return Ok(None),
};
let sort_bitmap_byte = sort_idx / 8;
let sort_bitmap_bit = (sort_idx % 8) as u32;
let sort_body_data_offset = 2 + fast.bitmap_size + sort_byte_offset;
let compiled_pred: Option<CompiledPredicate> = match predicate {
Some(pred) => match compile_predicate(pred, &all_columns, &fast, &schema) {
Some(c) => Some(c),
None => return Ok(None),
},
None => None,
};
// Bounded top-N heap. For `order .x desc limit N`, we want the N
// largest values — use a min-heap so the smallest is at the top and
// can be popped when a better candidate arrives. For ascending, use
// a max-heap. We tie-break with a monotonic `seq` counter so the
// result is deterministic and stable.
//
// To keep this simple we maintain two typed heaps and pick by
// direction.
let drained: Vec<Vec<u8>> = match sort_col_type {
TypeId::Int => {
let mut seq: u64 = 0;
let mut heap_desc: BinaryHeap<Reverse<(i64, u64, Vec<u8>)>> =
BinaryHeap::with_capacity(prealloc);
let mut heap_asc: BinaryHeap<(i64, u64, Vec<u8>)> =
BinaryHeap::with_capacity(prealloc);
let mut null_rows: Vec<Vec<u8>> = Vec::with_capacity(prealloc);
for_each_row_raw_cancellable(&self.catalog, table, |_rid, data| {
if let Some(ref pred) = compiled_pred {
if !pred(data) {
return;
}
}
// Inlined int-column reader: null check + i64 decode.
let base = row_body_base(data);
let sort_data_offset = base + sort_body_data_offset;
if data.len() < sort_data_offset + 8
|| data.len() <= base + 2 + sort_bitmap_byte
{
return;
}
let is_null = (data[base + 2 + sort_bitmap_byte] >> sort_bitmap_bit) & 1 == 1;
let id = seq;
seq += 1;
if is_null {
if null_rows.len() < limit {
null_rows.push(data.to_vec());
}
return;
}
let key = i64::from_le_bytes(
data[sort_data_offset..sort_data_offset + 8]
.try_into()
.unwrap_or_else(|_| unreachable!()),
);
if descending {
if heap_desc.len() < limit {
heap_desc.push(Reverse((key, id, data.to_vec())));
} else if let Some(Reverse((top_key, _, _))) = heap_desc.peek() {
if key > *top_key {
heap_desc.pop();
heap_desc.push(Reverse((key, id, data.to_vec())));
}
}
} else if heap_asc.len() < limit {
heap_asc.push((key, id, data.to_vec()));
} else if let Some((top_key, _, _)) = heap_asc.peek() {
if key < *top_key {
heap_asc.pop();
heap_asc.push((key, id, data.to_vec()));
}
}
})?;
let mut drained: Vec<(i64, u64, Vec<u8>)> = if descending {
heap_desc.into_iter().map(|Reverse(t)| t).collect()
} else {
heap_asc.into_iter().collect()
};
if descending {
cooperative_stable_sort_by(&mut drained, self.query_memory_limit, |a, b| {
b.0.cmp(&a.0).then(a.1.cmp(&b.1))
})?;
} else {
cooperative_stable_sort_by(&mut drained, self.query_memory_limit, |a, b| {
a.0.cmp(&b.0).then(a.1.cmp(&b.1))
})?;
}
let mut rows: Vec<Vec<u8>> = drained.into_iter().map(|(_, _, d)| d).collect();
rows.extend(null_rows.into_iter().take(limit.saturating_sub(rows.len())));
rows
}
TypeId::Float => {
// Novel angle: rather than introducing a `TotalF64` newtype
// with `Ord via total_cmp`, transform the f64 bit pattern
// into a sortable `u64` so `BinaryHeap<u64>` orders exactly
// like `f64::total_cmp` would. Classic trick: flip the sign
// bit on positives, flip all bits on negatives. Result:
// - NaN (sign=0) stays greatest, matching total_cmp
// - -0.0 sorts before +0.0, matching total_cmp
// - Hot loop is branch-cheap (one compare + one xor)
let mut seq: u64 = 0;
let mut heap_desc: BinaryHeap<Reverse<(u64, u64, Vec<u8>)>> =
BinaryHeap::with_capacity(prealloc);
let mut heap_asc: BinaryHeap<(u64, u64, Vec<u8>)> =
BinaryHeap::with_capacity(prealloc);
let mut null_rows: Vec<Vec<u8>> = Vec::with_capacity(prealloc);
for_each_row_raw_cancellable(&self.catalog, table, |_rid, data| {
if let Some(ref pred) = compiled_pred {
if !pred(data) {
return;
}
}
let base = row_body_base(data);
let sort_data_offset = base + sort_body_data_offset;
if data.len() < sort_data_offset + 8
|| data.len() <= base + 2 + sort_bitmap_byte
{
return;
}
let is_null = (data[base + 2 + sort_bitmap_byte] >> sort_bitmap_bit) & 1 == 1;
let id = seq;
seq += 1;
if is_null {
if null_rows.len() < limit {
null_rows.push(data.to_vec());
}
return;
}
let bits = u64::from_le_bytes(
data[sort_data_offset..sort_data_offset + 8]
.try_into()
.unwrap_or_else(|_| unreachable!()),
);
let key = f64_bits_to_sortable_u64(bits);
if descending {
if heap_desc.len() < limit {
heap_desc.push(Reverse((key, id, data.to_vec())));
} else if let Some(Reverse((top_key, _, _))) = heap_desc.peek() {
if key > *top_key {
heap_desc.pop();
heap_desc.push(Reverse((key, id, data.to_vec())));
}
}
} else if heap_asc.len() < limit {
heap_asc.push((key, id, data.to_vec()));
} else if let Some((top_key, _, _)) = heap_asc.peek() {
if key < *top_key {
heap_asc.pop();
heap_asc.push((key, id, data.to_vec()));
}
}
})?;
let mut drained: Vec<(u64, u64, Vec<u8>)> = if descending {
heap_desc.into_iter().map(|Reverse(t)| t).collect()
} else {
heap_asc.into_iter().collect()
};
if descending {
cooperative_stable_sort_by(&mut drained, self.query_memory_limit, |a, b| {
b.0.cmp(&a.0).then(a.1.cmp(&b.1))
})?;
} else {
cooperative_stable_sort_by(&mut drained, self.query_memory_limit, |a, b| {
a.0.cmp(&b.0).then(a.1.cmp(&b.1))
})?;
}
let mut rows: Vec<Vec<u8>> = drained.into_iter().map(|(_, _, d)| d).collect();
rows.extend(null_rows.into_iter().take(limit.saturating_sub(rows.len())));
rows
}
_ => unreachable!("type guard above restricts to Int/Float"),
};
let mut cancel = CancelCheck::new();
let mut rows: Vec<Vec<Value>> = Vec::with_capacity(drained.len());
for data in drained {
cancel.tick()?;
rows.push(
proj_indices
.iter()
.map(|&ci| decode_column(&schema, &row_layout, &data, ci))
.collect(),
);
}
Ok(Some(QueryResult::Rows {
columns: proj_columns,
rows,
}))
}
}