icydb-core 0.182.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
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
//! Module: db::query::plan::tests::structural_guards
//! Covers structural guardrails enforced during query planning.
//! Does not own: cross-module orchestration outside this module.
//! Boundary: exposes this module API while keeping implementation details internal.

use super::PlanModelEntity;
use crate::{
    db::{
        access::AccessPath,
        predicate::MissingRowPolicy,
        query::plan::{
            AccessPlannedQuery, AggregateKind, FieldSlot, GroupAggregateSpec,
            GroupDistinctPolicyReason, GroupSpec, GroupedExecutionConfig, expr::Expr,
            global_distinct_group_spec_for_aggregate_identity,
            resolve_global_distinct_field_aggregate,
        },
        test_support::source_guard::{
            collect_rust_sources, relative_rust_source_path, runtime_source_without_test_items,
        },
    },
    traits::EntitySchema,
    value::Value,
};
use std::{fs, path::Path};

fn assert_global_distinct_builder_signature(
    builder: fn(
        AggregateKind,
        &str,
        GroupedExecutionConfig,
    ) -> Result<GroupSpec, GroupDistinctPolicyReason>,
) {
    let _ = builder;
}
#[test]
fn planner_global_distinct_shape_builder_contract_is_semantic_only() {
    assert_global_distinct_builder_signature(global_distinct_group_spec_for_aggregate_identity);
}

#[test]
fn planner_distinct_resolution_projects_identity_shape_handle() {
    let execution = GroupedExecutionConfig::with_hard_limits(64, 4096);
    let group_fields = Vec::<FieldSlot>::new();
    let aggregates = vec![GroupAggregateSpec {
        kind: AggregateKind::Count,
        input_expr: Some(Box::new(crate::db::query::plan::expr::Expr::Field(
            crate::db::query::plan::expr::FieldId::new("tag"),
        ))),
        filter_expr: None,
        distinct: true,
    }];

    let resolved = resolve_global_distinct_field_aggregate(
        group_fields.as_slice(),
        aggregates.as_slice(),
        None::<&Expr>,
    )
    .expect("global distinct identity shape should resolve without policy rejection")
    .expect("global distinct candidate should project one aggregate identity handle");

    assert_eq!(resolved.kind(), AggregateKind::Count);
    assert_eq!(resolved.target_field(), "tag");

    let identity_shape = global_distinct_group_spec_for_aggregate_identity(
        resolved.kind(),
        resolved.target_field(),
        execution,
    )
    .expect("aggregate identity handle should lower into grouped shape");
    let aggregate_expr_shape = GroupSpec::global_distinct_shape_from_aggregate_expr(
        &crate::db::count_by("tag").distinct(),
        execution,
    );

    assert_eq!(
        identity_shape, aggregate_expr_shape,
        "global distinct grouped shape should be derivable from one aggregate identity handle",
    );
}

#[test]
fn planner_distinct_resolution_requires_planner_visibility_boundary() {
    let model = <PlanModelEntity as EntitySchema>::MODEL;
    let unresolved = FieldSlot::resolve(model, "missing");

    assert!(
        unresolved.is_none(),
        "planner field-slot resolution should remain the canonical grouped field identity boundary",
    );
}

#[test]
fn planner_bool_expr_semantics_are_not_routed_through_predicate_runtime_paths() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_root = crate_root.join("src/db");
    let mut sources = Vec::new();
    collect_rust_sources(source_root.as_path(), &mut sources);
    sources.sort();

    let mut forbidden_hits = Vec::new();
    for source_path in sources {
        if source_path
            .components()
            .any(|part| part.as_os_str() == "tests")
            || source_path
                .file_name()
                .is_some_and(|name| name == "tests.rs")
        {
            continue;
        }

        let relative = relative_rust_source_path(crate_root, source_path.as_path());
        if relative.starts_with("src/db/predicate/") {
            continue;
        }

        let source = fs::read_to_string(&source_path)
            .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
        let runtime_source = runtime_source_without_test_items(source.as_str());
        for forbidden in [
            "predicate::canonicalize_grouped_having_bool_expr",
            "predicate::canonicalize_scalar_where_bool_expr",
            "predicate::normalize_bool_expr",
            "predicate::is_normalized_bool_expr",
        ] {
            if runtime_source.contains(forbidden) {
                forbidden_hits.push(format!("{relative}: {forbidden}"));
            }
        }
    }

    assert!(
        forbidden_hits.is_empty(),
        "planner-owned boolean semantics should flow through query::plan::expr, not predicate re-exports: {forbidden_hits:?}",
    );
}

#[test]
fn planner_truth_admission_stays_below_normalize_and_case() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/canonicalize/truth_admission.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));

    let forbidden_patterns = [
        "canonicalize::normalize",
        "canonicalize::case",
        "super::normalize",
        "super::case",
        "normalize_bool_expr",
        "canonicalize_normalized_bool_case",
        "lower_searched_case_to_boolean",
        "rewrite_affine_numeric_compare_expr",
    ];
    let forbidden_hits = forbidden_patterns
        .iter()
        .copied()
        .filter(|pattern| source.contains(pattern))
        .collect::<Vec<_>>();

    assert!(
        forbidden_hits.is_empty(),
        "truth admission must remain a pure lower-level predicate owner and must not depend on normalize, case, or rewrite modules: {forbidden_hits:?}",
    );
}

#[test]
fn planner_bool_associative_dedup_requires_determinism_contract() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/canonicalize/normalize.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
    let expected_sequence = [
        "    assert!(",
        "        children.iter().all(expr_is_deterministic),",
        "        \"associative boolean dedup requires deterministic child expressions\",",
        "    );",
        "    children.dedup();",
    ]
    .join("\n");

    assert!(
        source.contains(&expected_sequence),
        "associative boolean dedup must keep a release-enforced determinism assertion immediately adjacent to children.dedup()",
    );
}

#[test]
fn planner_expr_canonicalize_does_not_depend_on_downstream_stages() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_root = crate_root.join("src/db/query/plan/expr/canonicalize");
    let mut sources = Vec::new();
    collect_rust_sources(source_root.as_path(), &mut sources);
    sources.sort();

    let forbidden_patterns = [
        "type_inference",
        "infer_expr_type",
        "infer_typed_expr",
        "TypedExpr",
        "predicate_compile",
        "compile_normalized_bool_expr_to_predicate",
        "CompiledPredicate",
        "projection_eval",
        "eval_projection_function_call",
        "collapse_true_only_boolean_admission",
        "db::predicate",
        "Predicate::",
    ];
    let mut forbidden_hits = Vec::new();
    for source_path in sources {
        let relative = relative_rust_source_path(crate_root, source_path.as_path());
        let source = fs::read_to_string(&source_path)
            .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
        let runtime_source = runtime_source_without_test_items(source.as_str());

        forbidden_hits.extend(
            forbidden_patterns
                .iter()
                .copied()
                .filter(|pattern| runtime_source.contains(pattern))
                .map(|pattern| format!("{relative}: {pattern}")),
        );
    }

    assert!(
        forbidden_hits.is_empty(),
        "canonicalize must remain the first expression stage and must not depend on downstream type, predicate, or projection stages: {forbidden_hits:?}",
    );
}

#[test]
fn planner_expr_truth_value_policy_does_not_depend_on_pipeline_stages() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/truth_value.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
    let runtime_source = runtime_source_without_test_items(source.as_str());
    let forbidden_patterns = [
        "canonicalize",
        "CanonicalExpr",
        "normalize_bool_expr",
        "type_inference",
        "TypedExpr",
        "infer_expr_type",
        "predicate_compile",
        "CompiledPredicate",
        "Predicate::",
        "projection_eval",
        "eval_projection_function_call",
    ];
    let forbidden_hits = forbidden_patterns
        .iter()
        .copied()
        .filter(|pattern| runtime_source.contains(pattern))
        .collect::<Vec<_>>();

    assert!(
        forbidden_hits.is_empty(),
        "truth-value admission must remain a stage-neutral evaluated-value policy and must not depend on canonicalize, type inference, predicate compile, or projection eval: {forbidden_hits:?}",
    );
}

#[test]
fn planner_expr_type_inference_does_not_call_predicate_compile() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_root = crate_root.join("src/db/query/plan/expr/type_inference");
    let forbidden_patterns = [
        "predicate_compile",
        "canonicalize",
        "CanonicalExpr",
        "normalize_bool_expr",
        "rewrite_affine_numeric_compare_expr",
        "compile_normalized_bool_expr_to_predicate",
        "CompiledPredicate",
        "derive_normalized_bool_expr_predicate_subset",
        "projection_eval",
        "eval_projection_function_call",
        "db::predicate",
        "predicate::",
    ];
    let mut sources = Vec::new();
    collect_rust_sources(source_root.as_path(), &mut sources);
    sources.sort();

    let mut forbidden_hits = Vec::new();
    for source_path in sources {
        if source_path
            .components()
            .any(|part| part.as_os_str() == "tests")
            || source_path
                .file_name()
                .is_some_and(|name| name == "tests.rs")
        {
            continue;
        }

        let relative = relative_rust_source_path(crate_root, source_path.as_path());
        let source = fs::read_to_string(&source_path)
            .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
        let runtime_source = runtime_source_without_test_items(source.as_str());

        forbidden_hits.extend(
            forbidden_patterns
                .iter()
                .copied()
                .filter(|pattern| runtime_source.contains(pattern))
                .map(|pattern| format!("{relative}: {pattern}")),
        );
    }

    assert!(
        forbidden_hits.is_empty(),
        "type inference must not call downstream predicate compilation or predicate runtime surfaces: {forbidden_hits:?}",
    );
}

#[test]
fn planner_expr_predicate_compile_does_not_rerun_type_inference() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/predicate/compile.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
    let runtime_source = runtime_source_without_test_items(source.as_str());
    let forbidden_patterns = [
        "TruthAdmission::",
        "TruthAdmission {",
        "TruthWrapperScope::",
        "TruthWrapperScope {",
        "affine_field_offset",
        "compile_ready_",
        "infer_expr_type",
        "ExprType",
        "type_inference",
        "SchemaInfo",
        "FieldKind",
        "classify_field_kind",
        ".field_kind(",
        "canonicalize_",
        "lower_searched_case_to_boolean",
        "normalize_bool_expr",
        "normalize_bool_expr_impl",
        "normalize_bool_case_expr",
        "bool_expr_normalized_order",
        "collect_normalized_bool_associative_children",
        "expr_is_deterministic",
        "rebuild_normalized_bool_associative_chain",
        "simplify_bool_expr_constants",
        "rewrite_affine_numeric_compare_expr",
    ];
    let forbidden_hits = forbidden_patterns
        .iter()
        .copied()
        .filter(|pattern| runtime_source.contains(pattern))
        .collect::<Vec<_>>();

    assert!(
        forbidden_hits.is_empty(),
        "predicate compilation must consume already-typed canonical boolean shape instead of re-running type inference or recreating canonicalization helpers: {forbidden_hits:?}",
    );
}

#[test]
fn planner_expr_predicate_artifact_requires_canonical_expr() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/predicate/compile.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));

    assert!(
        source.contains("expr: &CanonicalExpr"),
        "compiled predicate artifacts must be produced from CanonicalExpr, not raw Expr",
    );
    assert!(
        !source.contains("compiled_predicate(\n    expr: &Expr"),
        "compiled predicate artifact entrypoints must not accept raw Expr",
    );
    assert!(
        source.contains("derive_canonical_bool_expr_predicate_subset"),
        "predicate subset derivation must expose a CanonicalExpr artifact entrypoint",
    );
}

#[test]
fn planner_expr_projection_eval_does_not_canonicalize_or_import_predicates() {
    let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_path = crate_root.join("src/db/query/plan/expr/projection_eval.rs");
    let source = fs::read_to_string(&source_path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", source_path.display()));
    let runtime_source = runtime_source_without_test_items(source.as_str());
    let forbidden_patterns = [
        "CoercionId::",
        "CoercionSpec::",
        "CompareFieldsPredicate",
        "CompareOp::",
        "ComparePredicate",
        "MembershipCompareLeaf",
        "Predicate::",
        "enum Predicate",
        "struct Predicate",
        "canonicalize_",
        "normalize_bool_expr",
        "simplify_bool_expr_constants",
        "rewrite_affine_numeric_compare_expr",
        "compile_normalized_bool_expr_to_predicate",
        "compile_canonical_bool_expr_to_compiled_predicate",
        "CompiledPredicate",
        "derive_normalized_bool_expr_predicate_subset",
        "type_inference",
        "infer_expr_type",
        "infer_typed_expr",
        "TypedExpr",
        "query::plan::expr::canonicalize",
        "db::predicate",
        "predicate::",
        "fn collapse_true_only_boolean_admission",
    ];
    let forbidden_hits = forbidden_patterns
        .iter()
        .copied()
        .filter(|pattern| runtime_source.contains(pattern))
        .collect::<Vec<_>>();

    assert!(
        forbidden_hits.is_empty(),
        "projection evaluation must not perform canonicalization, import predicate runtime semantics, or construct predicate AST nodes: {forbidden_hits:?}",
    );
}

#[test]
fn grouped_and_scalar_projection_specs_share_planner_projection_boundary() {
    let model = <PlanModelEntity as EntitySchema>::MODEL;
    let scalar: AccessPlannedQuery =
        AccessPlannedQuery::new(AccessPath::<Value>::FullScan, MissingRowPolicy::Ignore);
    let grouped: AccessPlannedQuery =
        AccessPlannedQuery::new(AccessPath::<Value>::FullScan, MissingRowPolicy::Ignore)
            .into_grouped(GroupSpec {
                group_fields: vec![
                    FieldSlot::resolve(model, "tag").expect("tag field should resolve"),
                ],
                aggregates: vec![GroupAggregateSpec {
                    kind: AggregateKind::Count,
                    input_expr: None,
                    filter_expr: None,
                    distinct: false,
                }],
                execution: GroupedExecutionConfig::unbounded(),
            });

    let scalar_projection = scalar.projection_spec(model);
    let grouped_projection = grouped.projection_spec(model);

    assert_eq!(
        scalar_projection.len(),
        model.fields.len(),
        "scalar projection should remain planner-owned and model-driven",
    );
    assert_eq!(
        grouped_projection.len(),
        2,
        "grouped projection should remain planner-owned and include grouped key + aggregate outputs",
    );
}