aedb 0.2.7

Embedded Rust storage engine with transactional commits, WAL durability, and snapshot-consistent reads
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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
use crate::catalog::Catalog;
use crate::catalog::namespace_key;
use crate::catalog::types::Value;
use crate::query::error::QueryError;
use crate::storage::encoded_key::EncodedKey;
use std::collections::{HashMap, HashSet};
use std::ops::Bound;

use super::predicate::collect_eq_constraints;

type IndexBounds = (Bound<EncodedKey>, Bound<EncodedKey>);

enum IndexLookup<'a> {
    Range {
        column: &'a str,
        bounds: IndexBounds,
        predicate_exact: bool,
    },
    Eq {
        column: &'a str,
        value: &'a Value,
        predicate_exact: bool,
    },
    In {
        column: &'a str,
        values: &'a [Value],
        predicate_exact: bool,
    },
}

#[derive(Debug, Clone)]
pub(super) struct IndexLookupResult {
    pub pks: Vec<EncodedKey>,
    pub selected_indexes: Vec<String>,
    pub plan_trace: Vec<String>,
    pub predicate_exact: bool,
}

pub(super) fn indexed_pks_for_predicate_limited(
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    table_name: &str,
    table: &crate::storage::keyspace::TableData,
    predicate: &crate::query::plan::Expr,
    candidate_limit: Option<usize>,
) -> Result<Option<IndexLookupResult>, QueryError> {
    indexed_pks_for_predicate_inner(
        catalog,
        project_id,
        scope_id,
        table_name,
        table,
        predicate,
        candidate_limit,
        false,
    )
}

pub(super) fn indexed_pks_for_predicate_with_trace(
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    table_name: &str,
    table: &crate::storage::keyspace::TableData,
    predicate: &crate::query::plan::Expr,
) -> Result<Option<IndexLookupResult>, QueryError> {
    indexed_pks_for_predicate_inner(
        catalog, project_id, scope_id, table_name, table, predicate, None, true,
    )
}

fn indexed_pks_for_predicate_inner(
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    table_name: &str,
    table: &crate::storage::keyspace::TableData,
    predicate: &crate::query::plan::Expr,
    candidate_limit: Option<usize>,
    include_diagnostics: bool,
) -> Result<Option<IndexLookupResult>, QueryError> {
    let mut result = indexed_pks_for_predicate_uncapped(
        catalog,
        project_id,
        scope_id,
        table_name,
        table,
        predicate,
        candidate_limit,
        include_diagnostics,
    )?;
    if let Some(result) = result.as_mut()
        && result.predicate_exact
        && let Some(limit) = candidate_limit
    {
        result.pks.truncate(limit);
    }
    Ok(result)
}

fn indexed_pks_for_predicate_uncapped(
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    table_name: &str,
    table: &crate::storage::keyspace::TableData,
    predicate: &crate::query::plan::Expr,
    candidate_limit: Option<usize>,
    include_diagnostics: bool,
) -> Result<Option<IndexLookupResult>, QueryError> {
    use crate::query::plan::Expr;

    match predicate {
        Expr::And(lhs, rhs) => {
            let left = indexed_pks_for_predicate_uncapped(
                catalog,
                project_id,
                scope_id,
                table_name,
                table,
                lhs,
                None,
                include_diagnostics,
            )?;
            let right = indexed_pks_for_predicate_uncapped(
                catalog,
                project_id,
                scope_id,
                table_name,
                table,
                rhs,
                None,
                include_diagnostics,
            )?;
            return Ok(match (left, right) {
                (Some(left), Some(right)) => Some(IndexLookupResult {
                    pks: intersect_pks(left.pks, right.pks),
                    selected_indexes: merge_selected_indexes_if_diagnostic(
                        include_diagnostics,
                        left.selected_indexes,
                        right.selected_indexes,
                    ),
                    predicate_exact: left.predicate_exact && right.predicate_exact,
                    plan_trace: merge_trace_if_diagnostic(
                        include_diagnostics,
                        "AND predicate combines indexed candidates with intersection",
                        left.plan_trace,
                        right.plan_trace,
                    ),
                }),
                (Some(left), None) => Some(IndexLookupResult {
                    plan_trace: merge_trace_single_if_diagnostic(
                        include_diagnostics,
                        "AND predicate uses indexed left side; right side will be residual filter",
                        left.plan_trace,
                    ),
                    predicate_exact: false,
                    ..left
                }),
                (None, Some(right)) => Some(IndexLookupResult {
                    plan_trace: merge_trace_single_if_diagnostic(
                        include_diagnostics,
                        "AND predicate uses indexed right side; left side will be residual filter",
                        right.plan_trace,
                    ),
                    predicate_exact: false,
                    ..right
                }),
                (None, None) => None,
            });
        }
        Expr::Or(lhs, rhs) => {
            let left = indexed_pks_for_predicate_uncapped(
                catalog,
                project_id,
                scope_id,
                table_name,
                table,
                lhs,
                None,
                include_diagnostics,
            )?;
            let right = indexed_pks_for_predicate_uncapped(
                catalog,
                project_id,
                scope_id,
                table_name,
                table,
                rhs,
                None,
                include_diagnostics,
            )?;
            return Ok(match (left, right) {
                (Some(left), Some(right)) => Some(IndexLookupResult {
                    pks: union_pks(left.pks, right.pks),
                    selected_indexes: merge_selected_indexes_if_diagnostic(
                        include_diagnostics,
                        left.selected_indexes,
                        right.selected_indexes,
                    ),
                    predicate_exact: left.predicate_exact && right.predicate_exact,
                    plan_trace: merge_trace_if_diagnostic(
                        include_diagnostics,
                        "OR predicate combines indexed candidates with union",
                        left.plan_trace,
                        right.plan_trace,
                    ),
                }),
                _ => None,
            });
        }
        _ => {}
    }

    let Some(lookup) = extract_indexable_predicate(predicate) else {
        let mut equalities = HashMap::new();
        let eq_only = collect_eq_constraints(predicate, &mut equalities);
        if !eq_only {
            return Ok(None);
        }
        // Composite + leftmost-prefix support for conjunctions of equality predicates.
        let ns = namespace_key(project_id, scope_id);
        let mut best: Option<(String, usize)> = None;
        for ((p, t, idx_name), idx_def) in &catalog.indexes {
            if p != &ns || t != table_name || !table.indexes.contains_key(idx_name) {
                continue;
            }
            if let Some(filter) = &idx_def.partial_filter
                && !expr_implied_by_eq_constraints(filter, &equalities)
            {
                continue;
            }
            let mut prefix_cols = 0usize;
            for col in &idx_def.columns {
                if equalities.contains_key(col) {
                    prefix_cols += 1;
                } else {
                    break;
                }
            }
            if prefix_cols == 0 {
                continue;
            }
            if best.as_ref().map(|(_, c)| *c).unwrap_or(0) < prefix_cols {
                best = Some((idx_name.clone(), prefix_cols));
            }
        }
        let Some((idx_name, prefix_cols)) = best else {
            return Ok(None);
        };
        let selected_index =
            table
                .indexes
                .get(&idx_name)
                .ok_or_else(|| QueryError::InvalidQuery {
                    reason: "index not found".into(),
                })?;
        let idx_def = catalog
            .indexes
            .get(&(ns, table_name.to_string(), idx_name.clone()))
            .ok_or_else(|| QueryError::InvalidQuery {
                reason: "index definition not found".into(),
            })?;
        let prefix_values = idx_def
            .columns
            .iter()
            .take(prefix_cols)
            .filter_map(|c| equalities.get(c).cloned())
            .collect::<Vec<_>>();
        let encoded = EncodedKey::from_values(&prefix_values);
        let pks = if prefix_cols == idx_def.columns.len() {
            selected_index.scan_eq_limit(&encoded, candidate_limit.unwrap_or(usize::MAX))
        } else {
            match candidate_limit {
                Some(limit) => selected_index.scan_prefix_window(Some(&encoded), 0, limit),
                None => selected_index.scan_prefix(&encoded),
            }
        };
        return Ok(Some(IndexLookupResult {
            pks,
            selected_indexes: selected_indexes_if_diagnostic(include_diagnostics, &idx_name),
            predicate_exact: true,
            plan_trace: plan_trace_if_diagnostic(include_diagnostics, || {
                format!(
                    "selected composite index '{idx_name}' with leftmost prefix columns={prefix_cols}"
                )
            }),
        }));
    };
    let column = match &lookup {
        IndexLookup::Range { column, .. }
        | IndexLookup::Eq { column, .. }
        | IndexLookup::In { column, .. } => *column,
    };

    let mut selected_index_name: Option<&str> = None;
    let ns = namespace_key(project_id, scope_id);
    let mut equalities = None;
    for ((p, t, idx_name), idx_def) in &catalog.indexes {
        if p != &ns
            || t != table_name
            || idx_def.columns.len() != 1
            || idx_def.columns[0] != column
            || !table.indexes.contains_key(idx_name)
        {
            continue;
        }
        if let Some(filter) = &idx_def.partial_filter {
            let equalities = equalities.get_or_insert_with(|| {
                let mut equalities = HashMap::new();
                collect_eq_constraints(predicate, &mut equalities);
                equalities
            });
            if !expr_implied_by_eq_constraints(filter, equalities) {
                continue;
            }
        }
        selected_index_name = Some(idx_name.as_str());
        break;
    }

    let Some(index_name) = selected_index_name else {
        if let IndexLookup::Eq { column, value, .. } = lookup
            && let Some(result) = indexed_pks_for_leftmost_composite_eq(
                catalog,
                project_id,
                scope_id,
                table_name,
                table,
                column,
                value,
                predicate,
                candidate_limit,
                include_diagnostics,
            )?
        {
            return Ok(Some(result));
        }
        return Ok(None);
    };
    let Some(index) = table.indexes.get(index_name) else {
        return Ok(None);
    };
    let trace_column = include_diagnostics.then(|| column.to_string());

    let predicate_exact = lookup.predicate_exact();
    let lookup_limit = if predicate_exact {
        candidate_limit
    } else {
        None
    };
    let pks = match lookup {
        IndexLookup::Range { bounds, .. } => {
            let pks =
                index.scan_range_limit(bounds.0, bounds.1, lookup_limit.unwrap_or(usize::MAX));
            pks
        }
        IndexLookup::Eq { value, .. } => index.scan_eq_limit(
            &EncodedKey::from_values(std::slice::from_ref(value)),
            lookup_limit.unwrap_or(usize::MAX),
        ),
        IndexLookup::In { values, .. } => {
            let limit = lookup_limit.unwrap_or(usize::MAX);
            let mut pks = Vec::new();
            for value in values {
                let remaining = limit.saturating_sub(pks.len());
                if remaining == 0 {
                    break;
                }
                pks.extend(index.scan_eq_limit(
                    &EncodedKey::from_values(std::slice::from_ref(value)),
                    remaining,
                ));
            }
            pks
        }
    };
    Ok(Some(IndexLookupResult {
        pks,
        selected_indexes: selected_indexes_if_diagnostic(include_diagnostics, index_name),
        predicate_exact,
        plan_trace: plan_trace_if_diagnostic(include_diagnostics, || {
            let column = trace_column.as_deref().unwrap_or("");
            format!("selected single-column index '{index_name}' for predicate on '{column}'")
        }),
    }))
}

#[allow(clippy::too_many_arguments)]
fn indexed_pks_for_leftmost_composite_eq(
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    table_name: &str,
    table: &crate::storage::keyspace::TableData,
    column: &str,
    value: &Value,
    predicate: &crate::query::plan::Expr,
    candidate_limit: Option<usize>,
    include_diagnostics: bool,
) -> Result<Option<IndexLookupResult>, QueryError> {
    let ns = namespace_key(project_id, scope_id);
    let mut equalities = HashMap::new();
    collect_eq_constraints(predicate, &mut equalities);
    if !equalities.contains_key(column) {
        equalities.insert(column.to_string(), value.clone());
    }

    let mut best: Option<(&str, usize)> = None;
    for ((p, t, idx_name), idx_def) in &catalog.indexes {
        if p != &ns
            || t != table_name
            || idx_def.columns.len() <= 1
            || idx_def.columns.first().map(String::as_str) != Some(column)
            || !table.indexes.contains_key(idx_name)
        {
            continue;
        }
        if let Some(filter) = &idx_def.partial_filter
            && !expr_implied_by_eq_constraints(filter, &equalities)
        {
            continue;
        }
        let mut prefix_cols = 0usize;
        for col in &idx_def.columns {
            if equalities.contains_key(col) {
                prefix_cols += 1;
            } else {
                break;
            }
        }
        if prefix_cols == 0 {
            continue;
        }
        if best.as_ref().map(|(_, cols)| *cols).unwrap_or(0) < prefix_cols {
            best = Some((idx_name.as_str(), prefix_cols));
        }
    }

    let Some((idx_name, prefix_cols)) = best else {
        return Ok(None);
    };
    let selected_index = table
        .indexes
        .get(idx_name)
        .ok_or_else(|| QueryError::InvalidQuery {
            reason: "index not found".into(),
        })?;
    let idx_def = catalog
        .indexes
        .get(&(ns, table_name.to_string(), idx_name.to_string()))
        .ok_or_else(|| QueryError::InvalidQuery {
            reason: "index definition not found".into(),
        })?;
    let prefix_values = idx_def
        .columns
        .iter()
        .take(prefix_cols)
        .filter_map(|c| equalities.get(c).cloned())
        .collect::<Vec<_>>();
    let encoded = EncodedKey::from_values(&prefix_values);
    let pks = if prefix_cols == idx_def.columns.len() {
        selected_index.scan_eq_limit(&encoded, candidate_limit.unwrap_or(usize::MAX))
    } else {
        match candidate_limit {
            Some(limit) => selected_index.scan_prefix_window(Some(&encoded), 0, limit),
            None => selected_index.scan_prefix(&encoded),
        }
    };

    Ok(Some(IndexLookupResult {
        pks,
        selected_indexes: selected_indexes_if_diagnostic(include_diagnostics, idx_name),
        predicate_exact: true,
        plan_trace: plan_trace_if_diagnostic(include_diagnostics, || {
            format!(
                "selected composite index '{idx_name}' with leftmost prefix columns={prefix_cols}"
            )
        }),
    }))
}

fn selected_indexes_if_diagnostic(include_diagnostics: bool, index_name: &str) -> Vec<String> {
    if include_diagnostics {
        vec![index_name.to_string()]
    } else {
        Vec::new()
    }
}

fn plan_trace_if_diagnostic<F>(include_diagnostics: bool, trace: F) -> Vec<String>
where
    F: FnOnce() -> String,
{
    if include_diagnostics {
        vec![trace()]
    } else {
        Vec::new()
    }
}

fn merge_selected_indexes_if_diagnostic(
    include_diagnostics: bool,
    left: Vec<String>,
    right: Vec<String>,
) -> Vec<String> {
    if !include_diagnostics {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(left.len() + right.len());
    for name in left.into_iter().chain(right) {
        if !out.contains(&name) {
            out.push(name);
        }
    }
    out
}

fn merge_trace_if_diagnostic(
    include_diagnostics: bool,
    header: &str,
    mut left: Vec<String>,
    right: Vec<String>,
) -> Vec<String> {
    if !include_diagnostics {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(1 + left.len() + right.len());
    out.push(header.to_string());
    out.append(&mut left);
    out.extend(right);
    out
}

fn merge_trace_single_if_diagnostic(
    include_diagnostics: bool,
    header: &str,
    mut trace: Vec<String>,
) -> Vec<String> {
    if !include_diagnostics {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(1 + trace.len());
    out.push(header.to_string());
    out.append(&mut trace);
    out
}

fn intersect_pks(left: Vec<EncodedKey>, right: Vec<EncodedKey>) -> Vec<EncodedKey> {
    let mut right_set: HashSet<EncodedKey> = HashSet::with_capacity(right.len());
    right_set.extend(right);
    let mut out = Vec::with_capacity(left.len().min(right_set.len()));
    for pk in left {
        if right_set.contains(&pk) {
            out.push(pk);
        }
    }
    out
}

fn union_pks(left: Vec<EncodedKey>, right: Vec<EncodedKey>) -> Vec<EncodedKey> {
    let mut seen: HashSet<EncodedKey> = HashSet::with_capacity(left.len() + right.len());
    let mut out = Vec::with_capacity(left.len() + right.len());
    for pk in left.into_iter().chain(right) {
        if seen.insert(pk.clone()) {
            out.push(pk);
        }
    }
    out
}

fn expr_implied_by_eq_constraints(
    expr: &crate::query::plan::Expr,
    equalities: &HashMap<String, Value>,
) -> bool {
    use crate::query::plan::Expr;
    match expr {
        Expr::Eq(col, val) => equalities.get(col) == Some(val),
        Expr::And(lhs, rhs) => {
            expr_implied_by_eq_constraints(lhs, equalities)
                && expr_implied_by_eq_constraints(rhs, equalities)
        }
        _ => false,
    }
}

fn extract_indexable_predicate(predicate: &crate::query::plan::Expr) -> Option<IndexLookup<'_>> {
    use crate::query::plan::Expr;

    match predicate {
        Expr::Eq(c, v) => Some(IndexLookup::Eq {
            column: c,
            value: v,
            predicate_exact: true,
        }),
        Expr::In(c, values) => Some(IndexLookup::In {
            column: c,
            values,
            predicate_exact: true,
        }),
        Expr::Lt(c, v) => Some(IndexLookup::Range {
            column: c,
            bounds: (
                Bound::Unbounded,
                Bound::Excluded(EncodedKey::from_values(std::slice::from_ref(v))),
            ),
            predicate_exact: true,
        }),
        Expr::Lte(c, v) => Some(IndexLookup::Range {
            column: c,
            bounds: (
                Bound::Unbounded,
                Bound::Included(EncodedKey::from_values(std::slice::from_ref(v))),
            ),
            predicate_exact: true,
        }),
        Expr::Gt(c, v) => Some(IndexLookup::Range {
            column: c,
            bounds: (
                Bound::Excluded(EncodedKey::from_values(std::slice::from_ref(v))),
                Bound::Unbounded,
            ),
            predicate_exact: true,
        }),
        Expr::Gte(c, v) => Some(IndexLookup::Range {
            column: c,
            bounds: (
                Bound::Included(EncodedKey::from_values(std::slice::from_ref(v))),
                Bound::Unbounded,
            ),
            predicate_exact: true,
        }),
        Expr::Between(c, lo, hi) => Some(IndexLookup::Range {
            column: c,
            bounds: (
                Bound::Included(EncodedKey::from_values(std::slice::from_ref(lo))),
                Bound::Included(EncodedKey::from_values(std::slice::from_ref(hi))),
            ),
            predicate_exact: true,
        }),
        Expr::Like(c, pattern) => {
            let prefix = like_prefix(pattern)?;
            let start = Bound::Included(EncodedKey::from_values(&[Value::Text(
                prefix.clone().into(),
            )]));
            let end = match next_prefix(&prefix) {
                Some(next) => Bound::Excluded(EncodedKey::from_values(&[Value::Text(next.into())])),
                None => Bound::Unbounded,
            };
            Some(IndexLookup::Range {
                column: c,
                bounds: (start, end),
                predicate_exact: like_prefix_is_exact(pattern),
            })
        }
        _ => None,
    }
}

impl IndexLookup<'_> {
    fn predicate_exact(&self) -> bool {
        match self {
            Self::Range {
                predicate_exact, ..
            }
            | Self::Eq {
                predicate_exact, ..
            }
            | Self::In {
                predicate_exact, ..
            } => *predicate_exact,
        }
    }
}

fn like_prefix(pattern: &str) -> Option<String> {
    if !pattern.ends_with('%') {
        return None;
    }
    let mut prefix = String::new();
    for ch in pattern.chars() {
        if ch == '%' || ch == '_' {
            break;
        }
        prefix.push(ch);
    }
    if prefix.is_empty() {
        return None;
    }
    Some(prefix)
}

fn like_prefix_is_exact(pattern: &str) -> bool {
    let mut wildcard_seen = false;
    for ch in pattern.chars() {
        match ch {
            '%' => wildcard_seen = true,
            '_' => return false,
            _ if wildcard_seen => return false,
            _ => {}
        }
    }
    wildcard_seen
}

fn next_prefix(prefix: &str) -> Option<String> {
    let mut bytes = prefix.as_bytes().to_vec();
    for byte_index in (0..bytes.len()).rev() {
        if bytes[byte_index] != u8::MAX {
            bytes[byte_index] += 1;
            bytes.truncate(byte_index + 1);
            return String::from_utf8(bytes).ok();
        }
    }
    None
}