reddb-io-engine 1.28.1

Shared RedDB query execution and storage primitives for native and WebAssembly runtimes.
Documentation
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
//! Canonical ORDER BY and top-K over already-admitted runtime records.
use super::{
    field_resolution::resolve_runtime_field,
    values::{runtime_partial_cmp, text_abbrev_key},
};
use crate::query::unified::UnifiedRecord;
use reddb_rql::ast::{Expr, FieldRef, OrderByClause};
use reddb_types::Value;
use std::cmp::Ordering;

/// Hosts retain expression effects, secret policy and identity-key semantics.
/// As on the native path, an expression miss is an absent sort key. Hosts own
/// any separate validation/error policy before calling these ordering helpers.
pub trait OrderingHost {
    fn evaluate_expression(&self, expression: &Expr, record: &UnifiedRecord) -> Option<Value>;
    fn secret_query_input(&self, value: Value) -> Option<Value>;
    fn record_identity_key(&self, record: &UnifiedRecord) -> String;
    fn topk_disabled(&self) -> bool;
}

/// Hosts with operating failures distinguish unreadable keys from missing values.
/// Keys are extracted before the sort or top-K selection mutates the records.
pub trait FallibleOrderingHost: OrderingHost {
    type Error;
    fn try_resolve_field(
        &self,
        record: &UnifiedRecord,
        field: &FieldRef,
        table_name: Option<&str>,
        table_alias: Option<&str>,
    ) -> Result<Option<Value>, Self::Error> {
        Ok(resolve_runtime_field(
            record,
            field,
            table_name,
            table_alias,
        ))
    }
    fn try_evaluate_expression(
        &self,
        expression: &Expr,
        record: &UnifiedRecord,
    ) -> Result<Option<Value>, Self::Error>;
}

struct OptionalOrderingHost<'a, H>(&'a H);
impl<H: OrderingHost> OrderingHost for OptionalOrderingHost<'_, H> {
    fn evaluate_expression(&self, expression: &Expr, record: &UnifiedRecord) -> Option<Value> {
        self.0.evaluate_expression(expression, record)
    }
    fn secret_query_input(&self, value: Value) -> Option<Value> {
        self.0.secret_query_input(value)
    }
    fn record_identity_key(&self, record: &UnifiedRecord) -> String {
        self.0.record_identity_key(record)
    }
    fn topk_disabled(&self) -> bool {
        self.0.topk_disabled()
    }
}
impl<H: OrderingHost> FallibleOrderingHost for OptionalOrderingHost<'_, H> {
    type Error = std::convert::Infallible;
    fn try_evaluate_expression(
        &self,
        expression: &Expr,
        record: &UnifiedRecord,
    ) -> Result<Option<Value>, Self::Error> {
        Ok(self.0.evaluate_expression(expression, record))
    }
}
fn optional_ordering_result<T>(result: Result<T, std::convert::Infallible>) -> T {
    match result {
        Ok(value) => value,
        Err(never) => match never {},
    }
}

pub fn compare_runtime_order_with_host<H: OrderingHost>(
    host: &H,
    left: &UnifiedRecord,
    right: &UnifiedRecord,
    clauses: &[OrderByClause],
    table_name: Option<&str>,
    table_alias: Option<&str>,
) -> Ordering {
    optional_ordering_result(try_compare_runtime_order_with_host(
        &OptionalOrderingHost(host),
        left,
        right,
        clauses,
        table_name,
        table_alias,
    ))
}

pub fn try_compare_runtime_order_with_host<H: FallibleOrderingHost>(
    host: &H,
    left: &UnifiedRecord,
    right: &UnifiedRecord,
    clauses: &[OrderByClause],
    table_name: Option<&str>,
    table_alias: Option<&str>,
) -> Result<Ordering, H::Error> {
    for clause in clauses {
        // Fase 1.6: when the ORDER BY item is an expression (CAST,
        // arithmetic, CASE, etc.), evaluate it against each record
        // and compare the resulting Values. Bare-column clauses fall
        // back to the direct field resolver which is cheaper for the
        // common case.
        let (left_value, right_value) = if let Some(ref expr) = clause.expr {
            (
                host.try_evaluate_expression(expr, left)?,
                host.try_evaluate_expression(expr, right)?,
            )
        } else {
            (
                host.try_resolve_field(left, &clause.field, table_name, table_alias)?,
                host.try_resolve_field(right, &clause.field, table_name, table_alias)?,
            )
        };
        let ordering = compare_runtime_optional_values(
            &|value| host.secret_query_input(value),
            left_value.as_ref(),
            right_value.as_ref(),
            clause.nulls_first,
        );

        if ordering != Ordering::Equal {
            return Ok(if clause.ascending {
                ordering
            } else {
                ordering.reverse()
            });
        }
    }

    Ok(host
        .record_identity_key(left)
        .cmp(&host.record_identity_key(right)))
}

/// Sort `records` by `order_by` using the Schwartzian transform:
/// extract sort keys once per record (O(n)), sort by the extracted keys
/// (O(n log n) value comparisons, no HashMap lookups), then reorder.
///
/// For a naive `sort_by(compare_runtime_order)`, the sort calls
/// `resolve_runtime_field` O(n log n) times — once per comparison.
/// With pre-extraction, field resolution is O(n) regardless of sort depth.
/// A single sort key, carrying the full `Value` plus an optional `u64` abbreviated key
/// for `Text` values so the comparator can skip full string comparisons in the common case.
struct SortKey {
    value: Option<Value>,
    abbrev: Option<u64>,
}

impl SortKey {
    fn new(value: Option<Value>) -> Self {
        let abbrev = match &value {
            Some(Value::Text(s)) => Some(text_abbrev_key(s)),
            _ => None,
        };
        SortKey { value, abbrev }
    }
}

pub fn sort_records_by_order_by_with_host<H: OrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    table_name: Option<&str>,
    table_alias: Option<&str>,
) {
    optional_ordering_result(try_sort_records_by_order_by_with_host(
        &OptionalOrderingHost(host),
        records,
        order_by,
        table_name,
        table_alias,
    ))
}

pub fn try_sort_records_by_order_by_with_host<H: FallibleOrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    table_name: Option<&str>,
    table_alias: Option<&str>,
) -> Result<(), H::Error> {
    if order_by.is_empty() || records.len() < 2 {
        return Ok(());
    }

    // Extract sort keys once per record — O(n × k) where k = ORDER BY clauses.
    // Layout: a single flat `Vec<SortKey>` of length n*k where row i's
    // keys live at `keys_flat[i*k .. (i+1)*k]`. One allocation total
    // instead of one per row, and the index permutation in `idxs`
    // stays a contiguous `Vec<usize>` — both vectors are sized exactly
    // up front so the inner sort never reallocates.
    let n = records.len();
    let k = order_by.len();
    let mut keys_flat: Vec<SortKey> = Vec::with_capacity(n * k);
    for rec in records.iter() {
        for clause in order_by.iter() {
            let v = if let Some(ref expr) = clause.expr {
                host.try_evaluate_expression(expr, rec)?
            } else {
                host.try_resolve_field(rec, &clause.field, table_name, table_alias)?
            };
            keys_flat.push(SortKey::new(v));
        }
    }
    let mut idxs: Vec<usize> = (0..n).collect();

    // Sort by extracted keys — O(n log n).
    // Text: compare abbreviated u64 key first; only fall through to full str::cmp on tie.
    // Non-text: delegate to the existing value comparator as before.
    let cmp_keys = |a: usize, b: usize| -> Ordering {
        let la = a * k;
        let lb = b * k;
        for (j, clause) in order_by.iter().enumerate() {
            let lk = &keys_flat[la + j];
            let rk = &keys_flat[lb + j];
            let ord = match (&lk.abbrev, &rk.abbrev, &lk.value, &rk.value) {
                (Some(la_a), Some(ra), Some(Value::Text(ls)), Some(Value::Text(rs))) => {
                    match la_a.cmp(ra) {
                        Ordering::Equal => ls.as_ref().cmp(rs.as_ref()),
                        other => other,
                    }
                }
                _ => compare_runtime_optional_values(
                    &|value| host.secret_query_input(value),
                    lk.value.as_ref(),
                    rk.value.as_ref(),
                    clause.nulls_first,
                ),
            };
            if ord != Ordering::Equal {
                return if clause.ascending { ord } else { ord.reverse() };
            }
        }
        Ordering::Equal
    };
    idxs.sort_by(|&a, &b| cmp_keys(a, b));

    // Reorder records in-place using the sorted index permutation
    let orig: Vec<_> = std::mem::take(records);
    let mut out = Vec::with_capacity(n);
    for i in idxs {
        out.push(orig[i].clone());
    }
    *records = out;
    Ok(())
}

/// Chooses between full sort and quickselect-based top-k based on
/// `limit`, `offset`, and the kill-switch. When `limit` is unset or
/// `records.len()` is already close to `offset + limit`, the heap
/// overhead of top-k doesn't pay off and we fall through to the
/// existing sort path. Output is identical in every branch.
pub fn sort_or_top_k_records_with_host<H: OrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    offset: Option<u64>,
    limit: Option<u64>,
    table_name: Option<&str>,
    table_alias: Option<&str>,
) {
    optional_ordering_result(try_sort_or_top_k_records_with_host(
        &OptionalOrderingHost(host),
        records,
        order_by,
        offset,
        limit,
        table_name,
        table_alias,
    ))
}

pub fn try_sort_or_top_k_records_with_host<H: FallibleOrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    offset: Option<u64>,
    limit: Option<u64>,
    table_name: Option<&str>,
    table_alias: Option<&str>,
) -> Result<(), H::Error> {
    if order_by.is_empty() {
        return Ok(());
    }
    let effective_k = match (offset, limit) {
        (_, None) => None,
        (off, Some(lim)) => {
            Some(usize::try_from(off.unwrap_or(0).saturating_add(lim)).unwrap_or(usize::MAX))
        }
    };
    if !host.topk_disabled() {
        if let Some(k) = effective_k {
            // Only take the top-k path when it actually saves work:
            // n > 2k means quickselect + sort-of-k beats full sort.
            if k > 0 && records.len() > k.saturating_mul(2) {
                try_top_k_records_by_order_by_with_host(
                    host,
                    records,
                    order_by,
                    k,
                    table_name,
                    table_alias,
                )?;
                return Ok(());
            }
        }
    }
    try_sort_records_by_order_by_with_host(host, records, order_by, table_name, table_alias)?;
    Ok(())
}

/// Top-K variant of `sort_records_by_order_by_with_host`. For ORDER BY + LIMIT k
/// with n > k, full O(n log n) sort is wasteful — quickselect picks the
/// k smallest in O(n) average, then sort only those k. Mirrors the
/// `select_nth_unstable_by` pattern used by `query_direct`'s
/// `parse_simple_ordered_complex_select` fast path so semantics stay
/// identical across code paths.
///
/// Output is bit-identical to `sort_records_by_order_by_with_host(...) +
/// records.truncate(k)` because the comparator used here is a **strict
/// total order** — ties are broken by original index, so no two records
/// ever compare Equal and the unstable partition degenerates to stable
/// semantics.
pub fn top_k_records_by_order_by_with_host<H: OrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    k: usize,
    table_name: Option<&str>,
    table_alias: Option<&str>,
) {
    optional_ordering_result(try_top_k_records_by_order_by_with_host(
        &OptionalOrderingHost(host),
        records,
        order_by,
        k,
        table_name,
        table_alias,
    ))
}

pub fn try_top_k_records_by_order_by_with_host<H: FallibleOrderingHost>(
    host: &H,
    records: &mut Vec<UnifiedRecord>,
    order_by: &[OrderByClause],
    k: usize,
    table_name: Option<&str>,
    table_alias: Option<&str>,
) -> Result<(), H::Error> {
    if k == 0 {
        records.clear();
        return Ok(());
    }
    if order_by.is_empty() {
        records.truncate(k);
        return Ok(());
    }
    let n = records.len();
    if n <= k {
        try_sort_records_by_order_by_with_host(host, records, order_by, table_name, table_alias)?;
        return Ok(());
    }

    // Flat key buffer — see `sort_records_by_order_by_with_host` for the
    // layout rationale. One allocation of n*kc keys instead of n.
    let kc = order_by.len();
    let mut keys_flat: Vec<SortKey> = Vec::with_capacity(n * kc);
    for rec in records.iter() {
        for clause in order_by.iter() {
            let v = if let Some(ref expr) = clause.expr {
                host.try_evaluate_expression(expr, rec)?
            } else {
                host.try_resolve_field(rec, &clause.field, table_name, table_alias)?
            };
            keys_flat.push(SortKey::new(v));
        }
    }

    // Returns Ordering::Less when `lhs` should come before `rhs` in the
    // final sorted output. Byte-for-byte identical to the loop inside
    // `sort_records_by_order_by_with_db`.
    let row_cmp = |a: usize, b: usize| -> Ordering {
        let la = a * kc;
        let lb = b * kc;
        for (j, clause) in order_by.iter().enumerate() {
            let lk = &keys_flat[la + j];
            let rk = &keys_flat[lb + j];
            let ord = match (&lk.abbrev, &rk.abbrev, &lk.value, &rk.value) {
                (Some(la_a), Some(ra), Some(Value::Text(ls)), Some(Value::Text(rs))) => {
                    match la_a.cmp(ra) {
                        Ordering::Equal => ls.as_ref().cmp(rs.as_ref()),
                        other => other,
                    }
                }
                _ => compare_runtime_optional_values(
                    &|value| host.secret_query_input(value),
                    lk.value.as_ref(),
                    rk.value.as_ref(),
                    clause.nulls_first,
                ),
            };
            if ord != Ordering::Equal {
                return if clause.ascending { ord } else { ord.reverse() };
            }
        }
        Ordering::Equal
    };

    let mut idxs: Vec<usize> = (0..n).collect();
    // Partition k smallest by (row_cmp, idx) — strict total order makes
    // the unstable partition deterministic and equivalent to stable sort
    // followed by truncate(k).
    idxs.select_nth_unstable_by(k - 1, |&a, &b| row_cmp(a, b).then_with(|| a.cmp(&b)));
    idxs.truncate(k);
    idxs.sort_by(|&a, &b| row_cmp(a, b).then_with(|| a.cmp(&b)));

    let orig: Vec<_> = std::mem::take(records);
    let mut out = Vec::with_capacity(k);
    for i in idxs {
        out.push(orig[i].clone());
    }
    *records = out;
    Ok(())
}

pub fn compare_runtime_optional_values(
    secret_input: &impl Fn(Value) -> Option<Value>,
    left: Option<&Value>,
    right: Option<&Value>,
    nulls_first: bool,
) -> Ordering {
    match (left, right) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => {
            if nulls_first {
                Ordering::Less
            } else {
                Ordering::Greater
            }
        }
        (Some(_), None) => {
            if nulls_first {
                Ordering::Greater
            } else {
                Ordering::Less
            }
        }
        (Some(Value::Null), Some(Value::Null)) => Ordering::Equal,
        (Some(Value::Null), Some(_)) => {
            if nulls_first {
                Ordering::Less
            } else {
                Ordering::Greater
            }
        }
        (Some(_), Some(Value::Null)) => {
            if nulls_first {
                Ordering::Greater
            } else {
                Ordering::Less
            }
        }
        (Some(left), Some(right)) => {
            runtime_partial_cmp(left, right, secret_input).unwrap_or(Ordering::Equal)
        }
    }
}