wasm-dbms 0.11.0

Runtime-agnostic DBMS engine for WASM environments
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
// Rust guideline compliant 2026-10-09
// X-WHERE-CLAUSE, M-CANONICAL-DOCS

//! Chooses between the access-path planner and the legacy planner, and turns
//! access paths into persistent record addresses under a budget.

use std::ops::Bound;

use wasm_dbms_api::prelude::{ColumnDef, Filter, IndexDef, MemoryResult, Value};
use wasm_dbms_memory::RecordAddress;
use wasm_dbms_memory::prelude::MemoryAccess;

use super::access_planner::{AccessPath, is_fallible, plan_filter};
use super::filter_analyzer::{IndexPlan, analyze_filter};
use super::index_reader::{IndexReader, IndexScan};

/// Largest number of candidate addresses one index range may produce.
pub const SCAN_BUDGET: usize = 4_096;
/// Largest number of candidate addresses one composed plan may produce.
pub const TOTAL_BUDGET: usize = 16_384;
/// Intersections stop reading further indexes once at most this many
/// candidates remain; fetching them is cheaper than another index read.
pub const SMALL_CANDIDATE_SET: usize = 8;

/// An access path plus the filter its candidates must still satisfy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlannedAccess {
    /// Candidate-producing path.
    pub path: AccessPath,
    /// Filter checked on every stored, unchanged candidate row.
    pub verify: Option<Filter>,
}

/// Plans `filter` for index access.
///
/// Filters containing LIKE or JSON predicates can fail during evaluation, so
/// they keep the legacy single-column plan and residual: evaluating them only
/// on a different candidate set could hide or reveal errors. Every other
/// filter uses the access-path planner and is fully re-checked.
pub fn plan_access(
    filter: &Filter,
    indexes: &'static [IndexDef],
    columns: &'static [ColumnDef],
) -> Option<PlannedAccess> {
    if is_fallible(filter) {
        let analyzed = analyze_filter(filter, indexes)?;
        let path = legacy_path(&analyzed.plan, indexes)?;
        return Some(PlannedAccess {
            path,
            verify: analyzed.remaining_filter,
        });
    }
    plan_filter(filter, indexes, columns).map(|path| PlannedAccess {
        path,
        verify: Some(filter.clone()),
    })
}

/// Converts a legacy single-column plan into an access path.
fn legacy_path(plan: &IndexPlan, indexes: &'static [IndexDef]) -> Option<AccessPath> {
    let columns = indexes
        .iter()
        .map(IndexDef::columns)
        .find(|columns| *columns == [plan.column()])?;
    let path = match plan {
        IndexPlan::Eq { value, .. } => {
            AccessPath::Scan(IndexScan::prefix(columns, vec![value.clone()]))
        }
        IndexPlan::Range { start, end, .. } => {
            let bound =
                |value: &Option<Value>| value.clone().map_or(Bound::Unbounded, Bound::Included);
            AccessPath::Scan(IndexScan::range(
                columns,
                Vec::new(),
                bound(start),
                bound(end),
            ))
        }
        IndexPlan::In { values, .. } => {
            let mut values = values.clone();
            values.sort();
            values.dedup();
            AccessPath::Union(
                values
                    .into_iter()
                    .map(|value| AccessPath::Scan(IndexScan::prefix(columns, vec![value])))
                    .collect(),
            )
        }
    };
    Some(path)
}

/// Remaining candidate-address budget of one plan execution.
#[derive(Debug)]
pub struct Budget {
    per_scan: usize,
    remaining: usize,
    entries_read: u64,
}

impl Budget {
    /// Creates a budget of `per_scan` addresses per index range and `total`
    /// addresses overall.
    pub fn new(per_scan: usize, total: usize) -> Self {
        Self {
            per_scan,
            remaining: total,
            entries_read: 0,
        }
    }

    /// Creates the default budget: [`SCAN_BUDGET`] and [`TOTAL_BUDGET`].
    pub fn default_limits() -> Self {
        Self::new(SCAN_BUDGET, TOTAL_BUDGET)
    }

    /// Returns how many index entries the execution read.
    pub fn entries_read(&self) -> u64 {
        self.entries_read
    }
}

/// Returns the sorted, distinct persistent addresses produced by `path`, or
/// `None` when the budget runs out. Never returns a partial candidate set.
pub fn materialize<MA>(
    reader: &IndexReader<'_>,
    path: &AccessPath,
    budget: &mut Budget,
    mm: &mut MA,
) -> MemoryResult<Option<Vec<RecordAddress>>>
where
    MA: MemoryAccess,
{
    match path {
        AccessPath::Empty => Ok(Some(Vec::new())),
        AccessPath::Scan(scan) => {
            let limit = budget.per_scan.min(budget.remaining);
            match reader.addresses(scan, limit, mm)? {
                Some(mut addresses) => {
                    budget.remaining -= addresses.len();
                    budget.entries_read += addresses.len() as u64;
                    addresses.sort_unstable();
                    addresses.dedup();
                    Ok(Some(addresses))
                }
                None => {
                    // The cursor read `limit + 1` entries before giving up.
                    budget.remaining -= limit;
                    budget.entries_read += limit as u64 + 1;
                    Ok(None)
                }
            }
        }
        AccessPath::Union(paths) => {
            let mut union = Vec::new();
            for path in paths {
                match materialize(reader, path, budget, mm)? {
                    Some(addresses) => union.extend(addresses),
                    None => return Ok(None),
                }
            }
            union.sort_unstable();
            union.dedup();
            Ok(Some(union))
        }
        AccessPath::Intersection(paths) => {
            let mut current: Option<Vec<RecordAddress>> = None;
            for path in paths {
                if current
                    .as_ref()
                    .is_some_and(|set| set.len() <= SMALL_CANDIDATE_SET)
                {
                    break;
                }
                // An over-budget member is skipped: the others still bound a
                // superset of the matching rows.
                let Some(addresses) = materialize(reader, path, budget, mm)? else {
                    continue;
                };
                current = Some(match current {
                    Some(set) => intersect_sorted(&set, &addresses),
                    None => addresses,
                });
            }
            Ok(current)
        }
    }
}

/// Intersects two sorted, distinct address lists.
fn intersect_sorted(left: &[RecordAddress], right: &[RecordAddress]) -> Vec<RecordAddress> {
    let (mut i, mut j) = (0, 0);
    let mut out = Vec::with_capacity(left.len().min(right.len()));
    while i < left.len() && j < right.len() {
        match left[i].cmp(&right[j]) {
            std::cmp::Ordering::Less => i += 1,
            std::cmp::Ordering::Greater => j += 1,
            std::cmp::Ordering::Equal => {
                out.push(left[i]);
                i += 1;
                j += 1;
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use std::ops::Bound;

    use wasm_dbms_api::prelude::{ColumnDef, DataTypeKind, Filter, IndexDef, Text, Uint32, Value};
    use wasm_dbms_memory::prelude::{HeapMemoryProvider, MemoryAccess, MemoryManager};
    use wasm_dbms_memory::{IndexLedger, RecordAddress};

    use super::{Budget, PlannedAccess, materialize, plan_access};
    use crate::database::access_planner::AccessPath;
    use crate::database::index_reader::{IndexReader, IndexScan};

    const SHARD: &[&str] = &["shard"];
    const COHORT: &[&str] = &["cohort"];

    fn uint(value: u32) -> Value {
        Value::Uint32(Uint32(value))
    }

    /// 1,000 rows: `shard = i % 10`, `cohort = i / 100`; address page = `i`.
    fn setup() -> (MemoryManager<HeapMemoryProvider>, IndexLedger) {
        let mut mm = MemoryManager::init(HeapMemoryProvider::default());
        let page = mm.claim_page().unwrap();
        IndexLedger::init(page, &[IndexDef(SHARD), IndexDef(COHORT)], &mut mm).unwrap();
        let mut ledger = IndexLedger::load(page, &mut mm).unwrap();
        for i in 0..1_000u32 {
            let address = RecordAddress::new(i, 0);
            ledger
                .insert(SHARD, vec![uint(i % 10)], address, &mut mm)
                .unwrap();
            ledger
                .insert(COHORT, vec![uint(i / 100)], address, &mut mm)
                .unwrap();
        }
        (mm, ledger)
    }

    fn shard(value: u32) -> AccessPath {
        AccessPath::Scan(IndexScan::prefix(SHARD, vec![uint(value)]))
    }

    fn cohort(value: u32) -> AccessPath {
        AccessPath::Scan(IndexScan::prefix(COHORT, vec![uint(value)]))
    }

    fn shard_below(value: u32) -> AccessPath {
        AccessPath::Scan(IndexScan::range(
            SHARD,
            vec![],
            Bound::Unbounded,
            Bound::Excluded(uint(value)),
        ))
    }

    fn pages(addresses: Option<Vec<RecordAddress>>) -> Option<Vec<u32>> {
        addresses.map(|addresses| addresses.into_iter().map(|address| address.page).collect())
    }

    #[test]
    fn test_empty_union_and_intersection_materialize_sorted_distinct_addresses() {
        let (mut mm, ledger) = setup();
        let reader = IndexReader::new(&ledger);
        let mut budget = Budget::default_limits();
        assert_eq!(
            materialize(&reader, &AccessPath::Empty, &mut budget, &mut mm).unwrap(),
            Some(vec![])
        );

        let union = AccessPath::Union(vec![cohort(3), shard(1), cohort(3)]);
        let expected: Vec<u32> = (0..1_000).filter(|i| i / 100 == 3 || i % 10 == 1).collect();
        assert_eq!(
            pages(materialize(&reader, &union, &mut budget, &mut mm).unwrap()),
            Some(expected)
        );

        let intersection = AccessPath::Intersection(vec![shard(1), cohort(3)]);
        let expected: Vec<u32> = (300..400).filter(|i| i % 10 == 1).collect();
        assert_eq!(
            pages(materialize(&reader, &intersection, &mut budget, &mut mm).unwrap()),
            Some(expected)
        );
    }

    #[test]
    fn test_intersection_skips_an_over_budget_member_and_keeps_a_selective_one() {
        let (mut mm, ledger) = setup();
        let reader = IndexReader::new(&ledger);
        let mut budget = Budget::new(150, 1_000);
        let path = AccessPath::Intersection(vec![shard_below(5), cohort(3)]);
        let expected: Vec<u32> = (300..400).collect();
        assert_eq!(
            pages(materialize(&reader, &path, &mut budget, &mut mm).unwrap()),
            Some(expected)
        );
    }

    #[test]
    fn test_over_budget_paths_return_none_instead_of_partial_sets() {
        let (mut mm, ledger) = setup();
        let reader = IndexReader::new(&ledger);
        assert_eq!(
            materialize(
                &reader,
                &shard_below(5),
                &mut Budget::new(499, 10_000),
                &mut mm
            )
            .unwrap(),
            None
        );
        assert_eq!(
            materialize(
                &reader,
                &AccessPath::Union(vec![shard(1), shard(2)]),
                &mut Budget::new(1_000, 150),
                &mut mm
            )
            .unwrap(),
            None
        );
        assert_eq!(
            materialize(
                &reader,
                &AccessPath::Intersection(vec![shard_below(5), shard_below(6)]),
                &mut Budget::new(100, 10_000),
                &mut mm
            )
            .unwrap(),
            None
        );
    }

    #[test]
    fn test_intersection_stops_reading_once_the_candidate_set_is_small() {
        let (mut mm, ledger) = setup();
        let reader = IndexReader::new(&ledger);
        let selective = AccessPath::Scan(IndexScan::prefix(SHARD, vec![uint(99)]));
        let mut budget = Budget::default_limits();
        let path = AccessPath::Intersection(vec![selective, cohort(3)]);
        assert_eq!(
            materialize(&reader, &path, &mut budget, &mut mm).unwrap(),
            Some(vec![])
        );
        assert_eq!(budget.entries_read(), 0);
    }

    const COLUMNS: &[ColumnDef] = &[
        ColumnDef {
            name: "id",
            data_type: DataTypeKind::Uint32,
            auto_increment: false,
            nullable: false,
            primary_key: true,
            unique: false,
            foreign_key: None,
            default: None,
            renamed_from: &[],
        },
        ColumnDef {
            name: "name",
            data_type: DataTypeKind::Text,
            auto_increment: false,
            nullable: false,
            primary_key: false,
            unique: false,
            foreign_key: None,
            default: None,
            renamed_from: &[],
        },
    ];
    const INDEXES: &[IndexDef] = &[IndexDef(&["id"]), IndexDef(&["name"])];

    #[test]
    fn test_plan_access_keeps_the_legacy_plan_for_fallible_filters() {
        let name = Filter::eq("name", Value::Text(Text("a".to_string())));
        let like = Filter::like("name", "a%");

        let planned =
            plan_access(&like.clone().and(name.clone()), INDEXES, COLUMNS).expect("legacy plan");
        assert_eq!(
            planned,
            PlannedAccess {
                path: AccessPath::Scan(IndexScan::prefix(
                    &["name"],
                    vec![Value::Text(Text("a".to_string()))]
                )),
                verify: Some(like.clone()),
            }
        );
        assert_eq!(
            plan_access(&like.clone().or(name.clone()), INDEXES, COLUMNS),
            None
        );

        let plain = name.clone().and(Filter::eq("id", uint(1)));
        assert_eq!(
            plan_access(&plain, INDEXES, COLUMNS),
            Some(PlannedAccess {
                path: AccessPath::Scan(IndexScan::prefix(&["id"], vec![uint(1)])),
                verify: Some(plain.clone()),
            })
        );
    }
}