icydb-core 0.192.3

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
use super::*;

fn assert_verbose_access_choice(verbose: &str, expected_choice_prefix: &str, context: &str) {
    let diagnostics = session_verbose_diagnostics_map(verbose);

    assert!(
        diagnostics
            .get("diag.r.access_choice_chosen")
            .is_some_and(|choice| choice.starts_with(expected_choice_prefix)),
        "{context} must project one deterministic order-compatible access family",
    );
    assert_eq!(
        diagnostics.get("diag.r.access_choice_chosen_reason"),
        Some(&"order_compatible_preferred".to_string()),
        "{context} must report the canonical order-compatibility tie-break",
    );
    assert!(
        diagnostics
            .get("diag.r.access_choice_rejections")
            .is_some_and(|rejections| rejections.contains("order_compatible_preferred")),
        "{context} must report that at least one competing route lost on the canonical order-compatibility tie-break",
    );
}

fn assert_verbose_access_choice_reason(
    verbose: &str,
    expected_choice_prefix: &str,
    expected_reason: &str,
    context: &str,
) {
    let diagnostics = session_verbose_diagnostics_map(verbose);

    assert!(
        diagnostics
            .get("diag.r.access_choice_chosen")
            .is_some_and(|choice| choice.starts_with(expected_choice_prefix)),
        "{context} must project one deterministic access family",
    );
    assert_eq!(
        diagnostics.get("diag.r.access_choice_chosen_reason"),
        Some(&expected_reason.to_string()),
        "{context} must report the expected canonical access-choice reason",
    );
    assert!(
        diagnostics
            .get("diag.r.access_choice_rejections")
            .is_some_and(|rejections| rejections.contains(expected_reason)),
        "{context} must report that at least one competing route lost on the same canonical access-choice reason",
    );
}

#[test]
fn fluent_load_explain_execution_surface_adapters_are_available() {
    reset_session_sql_store();
    let session = sql_session();
    let query = session
        .load::<SessionSqlEntity>()
        .filter(crate::db::FieldRef::new("id").eq(Ulid::from_u128(9_201)))
        .order_term(crate::db::asc("id"));
    let descriptor = query
        .explain_execution()
        .expect("fluent execution descriptor explain should build");

    let text = query
        .explain_execution_text()
        .expect("fluent execution text explain should build");
    assert!(
        text.contains("ByKeyLookup"),
        "fluent execution text surface should include root node type",
    );
    assert_eq!(
        text,
        descriptor.render_text_tree(),
        "fluent execution text surface should be canonical descriptor text rendering",
    );

    let json = query
        .explain_execution_json()
        .expect("fluent execution json explain should build");
    assert!(
        json.contains("\"node_type\":\"ByKeyLookup\""),
        "fluent execution json surface should include canonical root node type",
    );
    assert!(
        json.contains("\"admission\":{\"lane\":\"diagnostic_explain\",\"decision\":\"rejected\""),
        "fluent execution json surface should include finalized admission facts: {json}",
    );
    assert!(
        json.contains(&format!(
            "\"execution\":{}",
            descriptor.render_json_canonical()
        )),
        "fluent execution json surface should keep the canonical descriptor under the execution object: {json}",
    );

    let verbose = query
        .explain_execution_verbose()
        .expect("fluent execution verbose explain should build");
    assert!(
        verbose.contains("diag.r.secondary_order_pushdown="),
        "fluent execution verbose surface should include diagnostics",
    );
}

#[test]
fn session_fluent_verbose_prefix_choice_prefers_order_compatible_index_when_rank_ties() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();
    let verbose = session
        .load::<SessionDeterministicChoiceEntity>()
        .filter(crate::db::FieldRef::new("tier").eq("gold"))
        .order_term(crate::db::asc("handle"))
        .order_term(crate::db::asc("id"))
        .explain_execution_verbose()
        .expect("session deterministic prefix verbose explain should build");

    assert_verbose_access_choice(
        &verbose,
        "IndexPrefix(",
        "session fluent verbose prefix explain",
    );
}

#[test]
fn session_fluent_verbose_range_choice_matrix_prefers_order_compatible_index_when_rank_ties() {
    for (context, descending) in [
        ("session fluent verbose range explain", false),
        ("session descending verbose range explain", true),
    ] {
        reset_indexed_session_sql_store();
        let session = indexed_sql_session();
        let mut query =
            session
                .load::<SessionDeterministicRangeEntity>()
                .filter(crate::db::FilterExpr::and(vec![
                    crate::db::FieldRef::new("tier").eq("gold"),
                    crate::db::FieldRef::new("score").gt(10_u64),
                ]));
        query = if descending {
            query
                .order_term(crate::db::desc("score"))
                .order_term(crate::db::desc("label"))
                .order_term(crate::db::desc("id"))
        } else {
            query
                .order_term(crate::db::asc("score"))
                .order_term(crate::db::asc("label"))
                .order_term(crate::db::asc("id"))
        };
        let verbose = query
            .explain_execution_verbose()
            .unwrap_or_else(|err| panic!("{context} should build: {err}"));

        assert_verbose_access_choice(&verbose, "IndexRange(", context);
    }
}

#[test]
fn session_fluent_verbose_range_choice_prefers_stronger_bounds_before_lexicographic_tiebreak() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();
    let verbose = session
        .load::<SessionRangeStrengthEntity>()
        .filter(crate::db::FilterExpr::and(vec![
            crate::db::FieldRef::new("tier").eq("gold"),
            crate::db::FieldRef::new("score").gt(10_u64),
            crate::db::FieldRef::new("score").lt(20_u64),
            crate::db::FieldRef::new("label").gt("m"),
        ]))
        .explain_execution_verbose()
        .expect("session range-strength verbose explain should build");

    assert_verbose_access_choice_reason(
        &verbose,
        "IndexRange(",
        "stronger_range_bounds_preferred",
        "session fluent verbose range-strength explain",
    );
}

#[test]
fn session_fluent_verbose_choice_prefers_lower_residual_burden_before_order_compatibility() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();
    let verbose = session
        .load::<SessionResidualRankingEntity>()
        .filter(crate::db::FilterExpr::and(vec![
            crate::db::FieldRef::new("active").eq(true),
            crate::db::FieldRef::new("archived").eq(false),
            crate::db::FieldRef::new("tier").eq("gold"),
        ]))
        .explain_execution_verbose()
        .expect("session residual-ranking verbose explain should build");

    assert_verbose_access_choice_reason(
        &verbose,
        "IndexPrefix(",
        "residual_burden_preferred",
        "session fluent verbose residual-ranking explain",
    );
    assert!(
        verbose.contains("Access choice:")
            && verbose.contains("  Candidates:")
            && verbose.contains("  Scoring:")
            && verbose.contains("  Decision:")
            && verbose.contains("reason: residual_burden_preferred"),
        "session fluent verbose residual-ranking explain must render the human-readable access-choice section",
    );
}

#[test]
fn session_fluent_verbose_explain_reports_shared_query_plan_reuse_after_first_build() {
    reset_session_sql_store();
    let session = sql_session();
    let query = session
        .load::<SessionSqlEntity>()
        .filter(crate::db::FieldRef::new("age").gte(20_u64))
        .order_term(crate::db::asc("age"))
        .order_term(crate::db::asc("id"))
        .limit(2);

    let first = query
        .explain_execution_verbose()
        .expect("first fluent verbose explain should build");
    let second = query
        .explain_execution_verbose()
        .expect("second fluent verbose explain should build");
    let first_diagnostics = session_verbose_diagnostics_map(&first);
    let second_diagnostics = session_verbose_diagnostics_map(&second);

    assert_eq!(
        first_diagnostics.get("diag.s.semantic_reuse_artifact"),
        Some(&"shared_prepared_query_plan".to_string()),
        "session fluent verbose explain must label the shipped semantic reuse artifact",
    );
    assert_eq!(
        first_diagnostics.get("diag.s.semantic_reuse"),
        Some(&"miss".to_string()),
        "the first fluent verbose explain should miss the shared prepared query-plan cache",
    );
    assert_eq!(
        second_diagnostics.get("diag.s.semantic_reuse_artifact"),
        Some(&"shared_prepared_query_plan".to_string()),
        "repeat fluent verbose explain must keep the same semantic reuse artifact class",
    );
    assert_eq!(
        second_diagnostics.get("diag.s.semantic_reuse"),
        Some(&"hit".to_string()),
        "the second fluent verbose explain should hit the shared prepared query-plan cache",
    );
}

#[test]
fn session_fluent_verbose_explain_keeps_distinct_semantic_identity_on_reuse_miss() {
    reset_session_sql_store();
    let session = sql_session();
    let left = session
        .load::<SessionSqlEntity>()
        .filter(crate::db::FieldRef::new("age").gte(20_u64))
        .order_term(crate::db::asc("age"))
        .order_term(crate::db::asc("id"))
        .limit(2)
        .explain_execution_verbose()
        .expect("left fluent verbose explain should build");
    let right = session
        .load::<SessionSqlEntity>()
        .filter(crate::db::FieldRef::new("age").gte(20_u64))
        .order_term(crate::db::desc("age"))
        .order_term(crate::db::desc("id"))
        .limit(1)
        .explain_execution_verbose()
        .expect("right fluent verbose explain should build");
    let left_diagnostics = session_verbose_diagnostics_map(&left);
    let right_diagnostics = session_verbose_diagnostics_map(&right);

    for (context, diagnostics) in [
        ("left fluent verbose explain", &left_diagnostics),
        ("right fluent verbose explain", &right_diagnostics),
    ] {
        assert_eq!(
            diagnostics.get("diag.s.semantic_reuse_artifact"),
            Some(&"shared_prepared_query_plan".to_string()),
            "{context} must keep the shipped semantic reuse artifact label visible",
        );
        assert_eq!(
            diagnostics.get("diag.s.semantic_reuse"),
            Some(&"miss".to_string()),
            "{context} must miss reuse when semantic ordering or limit identity differs",
        );
    }
}

#[test]
fn session_fluent_verbose_equality_prefix_suffix_order_matrix_prefers_order_compatible_index_when_rank_ties()
 {
    for (context, descending) in [
        (
            "session fluent verbose equality-prefix suffix-order explain",
            false,
        ),
        (
            "session descending verbose equality-prefix suffix-order explain",
            true,
        ),
    ] {
        reset_indexed_session_sql_store();
        let session = indexed_sql_session();
        let mut query =
            session
                .load::<SessionDeterministicRangeEntity>()
                .filter(crate::db::FilterExpr::and(vec![
                    crate::db::FieldRef::new("tier").eq("gold"),
                    crate::db::FieldRef::new("score").eq(20_u64),
                ]));
        query = if descending {
            query
                .order_term(crate::db::desc("label"))
                .order_term(crate::db::desc("id"))
        } else {
            query
                .order_term(crate::db::asc("label"))
                .order_term(crate::db::asc("id"))
        };
        let verbose = query
            .explain_execution_verbose()
            .unwrap_or_else(|err| panic!("{context} should build: {err}"));

        assert_verbose_access_choice(&verbose, "IndexPrefix(", context);

        if descending {
            let diagnostics = session_verbose_diagnostics_map(&verbose);

            assert_eq!(
                diagnostics.get("diag.r.load_order_route_mode"),
                Some(&"materialized_boundary".to_string()),
                "session descending verbose explain must expose the materialized-boundary route mode for descending non-unique equality-prefix suffix-order shapes",
            );
            assert_eq!(
                diagnostics.get("diag.r.load_order_route_reason"),
                Some(&"descending_non_unique_secondary_prefix_not_admitted".to_string()),
                "session descending verbose explain must expose the planner-owned materialized-boundary reason for descending non-unique equality-prefix suffix-order shapes",
            );
        }
    }
}

#[test]
fn session_fluent_verbose_order_only_choice_prefers_order_compatible_index_when_rank_ties() {
    reset_indexed_session_sql_store();
    let session = indexed_sql_session();
    let verbose = session
        .load::<SessionOrderOnlyChoiceEntity>()
        .order_term(crate::db::asc("alpha"))
        .order_term(crate::db::asc("id"))
        .explain_execution_verbose()
        .expect("session deterministic order-only verbose explain should build");

    assert_verbose_access_choice(
        &verbose,
        "IndexRange(",
        "session fluent verbose order-only explain",
    );

    let diagnostics = session_verbose_diagnostics_map(&verbose);

    assert_eq!(
        diagnostics.get("diag.r.load_order_route_mode"),
        Some(&"direct_streaming".to_string()),
        "session fluent verbose explain must expose the direct ordered-load route mode for admitted order-only fallback shapes",
    );
    assert_eq!(
        diagnostics.get("diag.r.load_order_route_reason"),
        Some(&"none".to_string()),
        "session fluent verbose explain must keep direct order-only fallback admission reason-free once the chosen route is already streaming-safe",
    );
}

#[test]
fn session_fluent_verbose_composite_order_only_choice_matrix_prefers_order_compatible_index_when_rank_ties()
 {
    for (context, descending) in [
        ("session fluent verbose composite order-only explain", false),
        (
            "session descending verbose composite order-only explain",
            true,
        ),
    ] {
        reset_indexed_session_sql_store();
        let session = indexed_sql_session();
        let mut query = session.load::<SessionDeterministicChoiceEntity>();
        query = if descending {
            query
                .order_term(crate::db::desc("tier"))
                .order_term(crate::db::desc("handle"))
                .order_term(crate::db::desc("id"))
        } else {
            query
                .order_term(crate::db::asc("tier"))
                .order_term(crate::db::asc("handle"))
                .order_term(crate::db::asc("id"))
        };
        let verbose = query
            .explain_execution_verbose()
            .unwrap_or_else(|err| panic!("{context} should build: {err}"));

        assert_verbose_access_choice(&verbose, "IndexRange(", context);
    }
}