icydb-core 0.94.0

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
use super::*;

// Expected ordered-route shape for one prefix-ordered window shape.
enum PrefixOrderedRouteExpectation {
    TopNSeekAccessSatisfied,
    MaterializedSort,
}

// Expected index-range limit-pushdown visibility for one prefix-ordered
// window shape.
enum IndexRangeLimitPushdownExpectation {
    Allowed,
    Forbidden,
}

// Expected EXPLAIN route properties for one prefix-ordered window shape.
struct PrefixRouteExpectations<'a> {
    access_name: &'a str,
    ordered_route: PrefixOrderedRouteExpectation,
    index_range_limit_pushdown: IndexRangeLimitPushdownExpectation,
}

// Build the shared equality-prefix suffix-order filter once so the descriptor
// cases differ only on direction and pagination.
fn equality_prefix_suffix_order_predicate() -> Predicate {
    Predicate::And(vec![
        Predicate::Compare(ComparePredicate::with_coercion(
            "tier",
            CompareOp::Eq,
            Value::Text("gold".to_string()),
            CoercionId::Strict,
        )),
        Predicate::Compare(ComparePredicate::with_coercion(
            "score",
            CompareOp::Eq,
            Value::Uint(20),
            CoercionId::Strict,
        )),
    ])
}

// Build one equality-prefix suffix-order execution descriptor for the requested
// direction and optional offset window.
fn equality_prefix_suffix_order_descriptor(
    session: &DbSession<SessionSqlCanister>,
    descending: bool,
    offset: Option<u32>,
) -> ExplainExecutionNodeDescriptor {
    let mut load = session
        .load::<SessionDeterministicRangeEntity>()
        .filter(equality_prefix_suffix_order_predicate());
    load = if descending {
        load.order_by_desc("label").order_by_desc("id")
    } else {
        load.order_by("label").order_by("id")
    };
    if let Some(offset) = offset {
        load = load.offset(offset);
    }

    load.limit(2)
        .explain_execution()
        .expect("session equality-prefix suffix-order explain_execution should build")
}

// Build one unique-prefix offset execution descriptor for the requested
// direction so the tests only state the expected route properties.
fn unique_prefix_offset_descriptor(
    session: &DbSession<SessionSqlCanister>,
    descending: bool,
) -> ExplainExecutionNodeDescriptor {
    let mut load = session
        .load::<SessionUniquePrefixOffsetEntity>()
        .filter(Predicate::Compare(ComparePredicate::with_coercion(
            "tier",
            CompareOp::Eq,
            Value::Text("gold".to_string()),
            CoercionId::Strict,
        )));
    load = if descending {
        load.order_by_desc("handle").order_by_desc("id")
    } else {
        load.order_by("handle").order_by("id")
    };

    load.limit(2)
        .offset(1)
        .explain_execution()
        .expect("session unique-prefix offset explain_execution should build")
}

// Assert the shared EXPLAIN contract for index-prefix ordered windows while
// letting each test override only the route properties that actually differ.
fn assert_prefix_route_descriptor(
    descriptor: &ExplainExecutionNodeDescriptor,
    expectations: PrefixRouteExpectations<'_>,
    context: &str,
) {
    assert_eq!(
        descriptor.node_type(),
        ExplainExecutionNodeType::IndexPrefixScan,
        "{context} should stay on the chosen index-prefix route",
    );
    assert!(
        descriptor.access_strategy().is_some_and(
            |access| matches!(access, ExplainAccessPath::IndexPrefix { name, .. } if *name == expectations.access_name)
        ),
        "{context} should expose the chosen order-compatible composite index",
    );
    assert!(
        explain_execution_find_first_node(
            descriptor,
            ExplainExecutionNodeType::SecondaryOrderPushdown
        )
        .is_some(),
        "{context} should expose secondary order pushdown",
    );
    match expectations.ordered_route {
        PrefixOrderedRouteExpectation::TopNSeekAccessSatisfied => {
            assert!(
                explain_execution_find_first_node(descriptor, ExplainExecutionNodeType::TopNSeek)
                    .is_some(),
                "{context} should keep the expected Top-N seek behavior",
            );
            assert!(
                explain_execution_find_first_node(
                    descriptor,
                    ExplainExecutionNodeType::OrderByAccessSatisfied
                )
                .is_some(),
                "{context} should keep the expected access-satisfied ordering behavior",
            );
            assert!(
                explain_execution_find_first_node(
                    descriptor,
                    ExplainExecutionNodeType::OrderByMaterializedSort
                )
                .is_none(),
                "{context} should not materialize ordering on this route",
            );
        }
        PrefixOrderedRouteExpectation::MaterializedSort => {
            assert!(
                explain_execution_find_first_node(descriptor, ExplainExecutionNodeType::TopNSeek)
                    .is_none(),
                "{context} should not keep Top-N seek on this route",
            );
            assert!(
                explain_execution_find_first_node(
                    descriptor,
                    ExplainExecutionNodeType::OrderByAccessSatisfied
                )
                .is_none(),
                "{context} should not mark ordering as access satisfied",
            );
            assert!(
                explain_execution_find_first_node(
                    descriptor,
                    ExplainExecutionNodeType::OrderByMaterializedSort
                )
                .is_some(),
                "{context} should keep the expected materialized-sort behavior",
            );
        }
    }
    match expectations.index_range_limit_pushdown {
        IndexRangeLimitPushdownExpectation::Allowed => {}
        IndexRangeLimitPushdownExpectation::Forbidden => {
            assert!(
                explain_execution_find_first_node(
                    descriptor,
                    ExplainExecutionNodeType::IndexRangeLimitPushdown
                )
                .is_none(),
                "{context} must not pretend to be an index-range limit-pushdown shape",
            );
        }
    }
}

#[test]
fn session_explain_execution_equality_prefix_suffix_order_matrix_is_stable() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();

    // Phase 1: lock the four directional/window variants for the same
    // equality-prefix suffix-order family under one matrix so the explain
    // contract stays explicit without four near-identical wrappers.
    let cases = [
        (
            false,
            None,
            PrefixRouteExpectations {
                access_name: "z_tier_score_label_idx",
                ordered_route: PrefixOrderedRouteExpectation::TopNSeekAccessSatisfied,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Forbidden,
            },
            "equality-prefix suffix-order roots",
        ),
        (
            true,
            None,
            PrefixRouteExpectations {
                access_name: "z_tier_score_label_idx",
                ordered_route: PrefixOrderedRouteExpectation::MaterializedSort,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Forbidden,
            },
            "descending equality-prefix suffix-order roots",
        ),
        (
            false,
            Some(1),
            PrefixRouteExpectations {
                access_name: "z_tier_score_label_idx",
                ordered_route: PrefixOrderedRouteExpectation::TopNSeekAccessSatisfied,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Allowed,
            },
            "equality-prefix suffix-order offset roots",
        ),
        (
            true,
            Some(1),
            PrefixRouteExpectations {
                access_name: "z_tier_score_label_idx",
                ordered_route: PrefixOrderedRouteExpectation::MaterializedSort,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Allowed,
            },
            "descending equality-prefix suffix-order offset roots",
        ),
    ];

    // Phase 2: run the shared descriptor assertion across every route variant.
    for (descending, offset, expectations, context) in cases {
        let descriptor = equality_prefix_suffix_order_descriptor(&session, descending, offset);
        assert_prefix_route_descriptor(&descriptor, expectations, context);
    }
}

#[test]
fn session_execute_equality_prefix_suffix_order_offset_windows_preserve_ordered_rows() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();

    // Phase 1: seed one deterministic equality-prefix suffix-order dataset so
    // the offset window can validate the retained ordered page on both scan
    // directions.
    for (id, tier, score, handle, label) in [
        (9_041_u128, "gold", 20_u64, "h-amber", "amber"),
        (9_042_u128, "gold", 20_u64, "h-bravo", "bravo"),
        (9_043_u128, "gold", 20_u64, "h-charlie", "charlie"),
        (9_044_u128, "gold", 20_u64, "h-delta", "delta"),
        (9_045_u128, "silver", 20_u64, "h-echo", "echo"),
    ] {
        session
            .insert(SessionDeterministicRangeEntity {
                id: Ulid::from_u128(id),
                tier: tier.to_string(),
                score,
                handle: handle.to_string(),
                label: label.to_string(),
            })
            .expect("equality-prefix suffix-order offset seed insert should succeed");
    }

    // Phase 2: execute one ascending and one descending offset window on the
    // same equality-prefix suffix-order shape.
    let asc = session
        .load::<SessionDeterministicRangeEntity>()
        .filter(Predicate::And(vec![
            Predicate::Compare(ComparePredicate::with_coercion(
                "tier",
                CompareOp::Eq,
                Value::Text("gold".to_string()),
                CoercionId::Strict,
            )),
            Predicate::Compare(ComparePredicate::with_coercion(
                "score",
                CompareOp::Eq,
                Value::Uint(20),
                CoercionId::Strict,
            )),
        ]))
        .order_by("label")
        .order_by("id")
        .offset(1)
        .limit(2)
        .execute()
        .and_then(crate::db::LoadQueryResult::into_rows)
        .expect("ascending equality-prefix suffix-order offset window should execute");
    let desc = session
        .load::<SessionDeterministicRangeEntity>()
        .filter(Predicate::And(vec![
            Predicate::Compare(ComparePredicate::with_coercion(
                "tier",
                CompareOp::Eq,
                Value::Text("gold".to_string()),
                CoercionId::Strict,
            )),
            Predicate::Compare(ComparePredicate::with_coercion(
                "score",
                CompareOp::Eq,
                Value::Uint(20),
                CoercionId::Strict,
            )),
        ]))
        .order_by_desc("label")
        .order_by_desc("id")
        .offset(1)
        .limit(2)
        .execute()
        .and_then(crate::db::LoadQueryResult::into_rows)
        .expect("descending equality-prefix suffix-order offset window should execute");

    let asc_labels = asc
        .iter()
        .map(|row| row.entity_ref().label.as_str())
        .collect::<Vec<_>>();
    let desc_labels = desc
        .iter()
        .map(|row| row.entity_ref().label.as_str())
        .collect::<Vec<_>>();

    assert_eq!(
        asc_labels,
        vec!["bravo", "charlie"],
        "ascending equality-prefix suffix-order offset windows should preserve the chosen suffix order",
    );
    assert_eq!(
        desc_labels,
        vec!["charlie", "bravo"],
        "descending equality-prefix suffix-order offset windows should preserve the reversed ordered window even when execution falls back downstream",
    );
}

#[test]
fn session_execute_unique_prefix_offset_windows_preserve_ordered_rows() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();
    seed_unique_prefix_offset_session_entities(
        &session,
        &[
            (9_881, "gold", "amber", "A"),
            (9_882, "gold", "bravo", "B"),
            (9_883, "gold", "charlie", "C"),
            (9_884, "gold", "delta", "D"),
            (9_885, "silver", "echo", "E"),
        ],
    );

    let asc = session
        .load::<SessionUniquePrefixOffsetEntity>()
        .filter(Predicate::Compare(ComparePredicate::with_coercion(
            "tier",
            CompareOp::Eq,
            Value::Text("gold".to_string()),
            CoercionId::Strict,
        )))
        .order_by("handle")
        .order_by("id")
        .limit(2)
        .offset(1)
        .execute()
        .and_then(crate::db::LoadQueryResult::into_rows)
        .expect("unique-prefix ascending offset window should execute");
    let asc_handles = asc
        .iter()
        .map(|row| row.entity_ref().handle.clone())
        .collect::<Vec<_>>();

    let desc = session
        .load::<SessionUniquePrefixOffsetEntity>()
        .filter(Predicate::Compare(ComparePredicate::with_coercion(
            "tier",
            CompareOp::Eq,
            Value::Text("gold".to_string()),
            CoercionId::Strict,
        )))
        .order_by_desc("handle")
        .order_by_desc("id")
        .limit(2)
        .offset(1)
        .execute()
        .and_then(crate::db::LoadQueryResult::into_rows)
        .expect("unique-prefix descending offset window should execute");
    let desc_handles = desc
        .iter()
        .map(|row| row.entity_ref().handle.clone())
        .collect::<Vec<_>>();

    assert_eq!(
        asc_handles,
        vec!["bravo".to_string(), "charlie".to_string()],
        "unique-prefix ascending offset windows should preserve the secondary index order without materialized drift",
    );
    assert_eq!(
        desc_handles,
        vec!["charlie".to_string(), "bravo".to_string()],
        "unique-prefix descending offset windows should preserve the reversed secondary index order without materialized drift",
    );
}

#[test]
fn session_explain_execution_unique_prefix_offset_matrix_is_stable() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();

    // Phase 1: lock the ascending and descending unique-prefix offset routes
    // under one small matrix because both variants share the same direct
    // Top-N contract with only scan direction changing.
    let cases = [
        (
            false,
            PrefixRouteExpectations {
                access_name: "tier_handle_unique",
                ordered_route: PrefixOrderedRouteExpectation::TopNSeekAccessSatisfied,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Allowed,
            },
            "unique-prefix offset roots",
        ),
        (
            true,
            PrefixRouteExpectations {
                access_name: "tier_handle_unique",
                ordered_route: PrefixOrderedRouteExpectation::TopNSeekAccessSatisfied,
                index_range_limit_pushdown: IndexRangeLimitPushdownExpectation::Allowed,
            },
            "descending unique-prefix offset roots",
        ),
    ];

    // Phase 2: reuse the shared explain assertion across both directions.
    for (descending, expectations, context) in cases {
        let descriptor = unique_prefix_offset_descriptor(&session, descending);
        assert_prefix_route_descriptor(&descriptor, expectations, context);
    }
}