icydb-core 0.180.4

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
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
//! Module: predicate::normalize
//! Responsibility: deterministic predicate normalization and enum-literal adjustment.
//! Does not own: runtime evaluation or schema field-slot resolution.
//! Boundary: normalize before validation/planning/fingerprinting.

use crate::{
    db::{
        predicate::{
            CoercionId, CoercionSpec, CompareOp, ComparePredicate, MembershipCompareLeaf,
            Predicate, collapse_membership_compare_leaves, encoding::encode_predicate_sort_key,
            simplify::simplify_and_compare_constraints,
        },
        schema::{SchemaInfo, ValidateError},
    },
    model::{classify_field_kind, field::FieldKind},
    types::{IntBig, NatBig},
    value::{Value, canonicalize_value_set},
};

/// Normalize a predicate into a canonical, deterministic form.
///
/// Normalization guarantees:
/// - Logical equivalence is preserved
/// - Nested AND / OR nodes are flattened
/// - Neutral elements are removed (True / False)
/// - Double negation is eliminated
/// - Child predicates are deterministically ordered
///
/// Note: this pass does not normalize literal values (numeric width, collation).
/// Ordering uses the structural `Value` representation.
///
/// This is used to ensure:
/// - stable planner output
/// - consistent caching / equality checks
/// - predictable test behavior
#[must_use]
pub(in crate::db) fn normalize(predicate: &Predicate) -> Predicate {
    // Normalize recursively while preserving logical equivalence.
    match predicate {
        Predicate::True => Predicate::True,
        Predicate::False => Predicate::False,

        Predicate::And(children) => normalize_and(children),
        Predicate::Or(children) => normalize_or(children),
        Predicate::Not(inner) => normalize_not(inner),

        Predicate::Compare(cmp) => Predicate::Compare(cmp.clone()),
        Predicate::CompareFields(cmp) => Predicate::CompareFields(cmp.clone()),

        Predicate::IsNull { field } => Predicate::IsNull {
            field: field.clone(),
        },
        Predicate::IsNotNull { field } => Predicate::IsNotNull {
            field: field.clone(),
        },
        Predicate::IsMissing { field } => Predicate::IsMissing {
            field: field.clone(),
        },
        Predicate::IsEmpty { field } => Predicate::IsEmpty {
            field: field.clone(),
        },
        Predicate::IsNotEmpty { field } => Predicate::IsNotEmpty {
            field: field.clone(),
        },
        Predicate::TextContains { field, value } => Predicate::TextContains {
            field: field.clone(),
            value: value.clone(),
        },
        Predicate::TextContainsCi { field, value } => Predicate::TextContainsCi {
            field: field.clone(),
            value: value.clone(),
        },
    }
}

///
/// Normalize enum literals in predicates against schema enum metadata.
///
/// Contract:
/// - strict enum literals (`path = Some`) must match the schema enum path
/// - loose enum literals (`path = None`) are resolved once at filter construction
/// - predicate semantics stay strict at runtime (`Eq` is unchanged)
///
pub(in crate::db) fn normalize_enum_literals(
    schema: &SchemaInfo,
    predicate: &Predicate,
) -> Result<Predicate, ValidateError> {
    // Enum literal normalization only rewrites enum payload shape, not operators.
    match predicate {
        Predicate::True => Ok(Predicate::True),
        Predicate::False => Ok(Predicate::False),
        Predicate::And(children) => {
            let mut normalized = Vec::with_capacity(children.len());
            for child in children {
                normalized.push(normalize_enum_literals(schema, child)?);
            }

            Ok(Predicate::And(normalized))
        }
        Predicate::Or(children) => {
            let mut normalized = Vec::with_capacity(children.len());
            for child in children {
                normalized.push(normalize_enum_literals(schema, child)?);
            }

            Ok(Predicate::Or(normalized))
        }
        Predicate::Not(inner) => Ok(Predicate::Not(Box::new(normalize_enum_literals(
            schema, inner,
        )?))),
        Predicate::Compare(cmp) => Ok(Predicate::Compare(normalize_compare_with_schema(
            schema, cmp,
        )?)),
        Predicate::CompareFields(cmp) => Ok(Predicate::CompareFields(
            normalize_compare_fields_with_schema(schema, cmp),
        )),
        Predicate::IsNull { field } => Ok(Predicate::IsNull {
            field: field.clone(),
        }),
        Predicate::IsNotNull { field } => Ok(Predicate::IsNotNull {
            field: field.clone(),
        }),
        Predicate::IsMissing { field } => Ok(Predicate::IsMissing {
            field: field.clone(),
        }),
        Predicate::IsEmpty { field } => Ok(Predicate::IsEmpty {
            field: field.clone(),
        }),
        Predicate::IsNotEmpty { field } => Ok(Predicate::IsNotEmpty {
            field: field.clone(),
        }),
        Predicate::TextContains { field, value } => Ok(Predicate::TextContains {
            field: field.clone(),
            value: value.clone(),
        }),
        Predicate::TextContainsCi { field, value } => Ok(Predicate::TextContainsCi {
            field: field.clone(),
            value: value.clone(),
        }),
    }
}

fn normalize_compare_with_schema(
    schema: &SchemaInfo,
    cmp: &ComparePredicate,
) -> Result<ComparePredicate, ValidateError> {
    let Some(field_kind) = schema.field_kind(&cmp.field) else {
        return Ok(cmp.clone());
    };

    let value = normalize_compare_value_for_kind(
        &cmp.field,
        cmp.op,
        &cmp.value,
        field_kind,
        cmp.coercion(),
    )?;
    Ok(ComparePredicate {
        field: cmp.field.clone(),
        op: cmp.op,
        value,
        coercion: cmp.coercion.clone(),
    })
}

fn normalize_compare_fields_with_schema(
    schema: &SchemaInfo,
    cmp: &crate::db::predicate::CompareFieldsPredicate,
) -> crate::db::predicate::CompareFieldsPredicate {
    let Some(left_kind) = schema.field_kind(&cmp.left_field) else {
        return cmp.clone();
    };
    let Some(right_kind) = schema.field_kind(&cmp.right_field) else {
        return cmp.clone();
    };

    let left_field = cmp.left_field.clone();
    let right_field = cmp.right_field.clone();
    let coercion =
        normalize_compare_fields_coercion(cmp.op, left_kind, right_kind, cmp.coercion.id);

    crate::db::predicate::CompareFieldsPredicate::with_coercion(
        left_field,
        cmp.op,
        right_field,
        coercion,
    )
}

const fn normalize_compare_fields_coercion(
    op: CompareOp,
    left_kind: &FieldKind,
    right_kind: &FieldKind,
    current: CoercionId,
) -> CoercionId {
    if op.is_equality_family() {
        if field_kinds_support_numeric_widen(left_kind, right_kind) {
            CoercionId::NumericWiden
        } else {
            current
        }
    } else if op.is_ordering_family() {
        if matches!(left_kind, FieldKind::Text { .. })
            && matches!(right_kind, FieldKind::Text { .. })
        {
            CoercionId::Strict
        } else {
            current
        }
    } else {
        current
    }
}

const fn field_kinds_support_numeric_widen(left_kind: &FieldKind, right_kind: &FieldKind) -> bool {
    classify_field_kind(left_kind).supports_predicate_numeric_widen()
        && classify_field_kind(right_kind).supports_predicate_numeric_widen()
}

fn normalize_compare_value_for_kind(
    field: &str,
    op: CompareOp,
    value: &Value,
    field_kind: &FieldKind,
    coercion: &CoercionSpec,
) -> Result<Value, ValidateError> {
    match op {
        CompareOp::In | CompareOp::NotIn => {
            let Value::List(values) = value else {
                return Ok(value.clone());
            };

            let Value::List(mut normalized) =
                normalize_list_value_for_kind(field, values.as_slice(), field_kind, coercion, op)?
            else {
                unreachable!("normalized compare-list kind should always return list value");
            };

            // Membership predicates are set-shaped: duplicates and input order
            // must not survive normalization because planner/cache identity and
            // runtime semantics both treat these lists as canonical value sets.
            canonicalize_value_set(&mut normalized);

            Ok(Value::List(normalized))
        }
        CompareOp::Contains => {
            let element_kind = match field_kind {
                FieldKind::List(inner) | FieldKind::Set(inner) => *inner,
                _ => return Ok(value.clone()),
            };

            normalize_value_for_kind(field, value, element_kind, coercion, op)
        }
        _ => normalize_value_for_kind(field, value, field_kind, coercion, op),
    }
}

fn normalize_value_for_kind(
    field: &str,
    value: &Value,
    expected_kind: &FieldKind,
    coercion: &CoercionSpec,
    op: CompareOp,
) -> Result<Value, ValidateError> {
    match expected_kind {
        FieldKind::Enum { path, .. } => normalize_enum_value(field, value, path),
        FieldKind::Relation { key_kind, .. } => {
            normalize_value_for_kind(field, value, key_kind, coercion, op)
        }
        FieldKind::List(inner) => {
            let Value::List(values) = value else {
                return Ok(value.clone());
            };

            normalize_list_value_for_kind(field, values.as_slice(), inner, coercion, op)
        }
        FieldKind::Set(inner) => {
            let Value::List(values) = value else {
                return Ok(value.clone());
            };

            let Value::List(mut normalized) =
                normalize_list_value_for_kind(field, values.as_slice(), inner, coercion, op)?
            else {
                unreachable!("normalized list kind should always return list value");
            };

            // Canonical set literal normalization must match the same
            // deterministic sort + dedup rule used by access planning.
            canonicalize_value_set(&mut normalized);

            Ok(Value::List(normalized))
        }
        FieldKind::Map {
            key,
            value: map_value,
        } => {
            let Value::Map(entries) = value else {
                return Ok(value.clone());
            };

            let mut normalized = Vec::with_capacity(entries.len());
            for (entry_key, entry_value) in entries {
                let key = normalize_value_for_kind(field, entry_key, key, coercion, op)?;
                let value = normalize_value_for_kind(field, entry_value, map_value, coercion, op)?;
                normalized.push((key, value));
            }

            Ok(Value::Map(normalized))
        }
        FieldKind::Account
        | FieldKind::Blob { .. }
        | FieldKind::Bool
        | FieldKind::Date
        | FieldKind::Decimal { .. }
        | FieldKind::Duration
        | FieldKind::Float32
        | FieldKind::Float64
        | FieldKind::Principal
        | FieldKind::Subaccount
        | FieldKind::Text { .. }
        | FieldKind::Timestamp
        | FieldKind::Ulid
        | FieldKind::Unit
        | FieldKind::Structured { .. } => Ok(value.clone()),
        FieldKind::Int8
        | FieldKind::Int16
        | FieldKind::Int32
        | FieldKind::Int64
        | FieldKind::Int128
        | FieldKind::IntBig { .. }
        | FieldKind::Nat8
        | FieldKind::Nat16
        | FieldKind::Nat32
        | FieldKind::Nat64
        | FieldKind::Nat128
        | FieldKind::NatBig { .. } => Ok(normalize_numeric_value_for_kind(
            value,
            expected_kind,
            coercion,
            op,
        )),
    }
}

// Canonicalize equality-like numeric literals onto the runtime field kind so
// planner identity does not depend on parser-chosen integer wrappers. Ordered
// NumericWiden comparisons keep their original transport shape because their
// literal wrapper is still part of the current planner contract.
fn normalize_numeric_value_for_kind(
    value: &Value,
    expected_kind: &FieldKind,
    coercion: &CoercionSpec,
    op: CompareOp,
) -> Value {
    if matches!(coercion.id, CoercionId::NumericWiden)
        && matches!(
            op,
            CompareOp::Lt | CompareOp::Lte | CompareOp::Gt | CompareOp::Gte
        )
    {
        return value.clone();
    }

    if !value.supports_numeric_coercion() {
        return value.clone();
    }

    let normalized = match expected_kind {
        FieldKind::Int64 => value
            .to_numeric_decimal()
            .and_then(<i64 as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::Int64),
        FieldKind::Int128 => value
            .to_numeric_decimal()
            .and_then(<i128 as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::Int128),
        FieldKind::IntBig { .. } => value
            .to_numeric_decimal()
            .and_then(<IntBig as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::IntBig),
        FieldKind::Nat64 => value
            .to_numeric_decimal()
            .and_then(<u64 as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::Nat64),
        FieldKind::Nat128 => value
            .to_numeric_decimal()
            .and_then(<u128 as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::Nat128),
        FieldKind::NatBig { .. } => value
            .to_numeric_decimal()
            .and_then(<NatBig as crate::traits::NumericValue>::try_from_decimal)
            .map(Value::NatBig),
        _ => None,
    };

    normalized.unwrap_or_else(|| value.clone())
}

// Normalize one list-shaped literal by recursively rewriting each item against
// the expected element kind while preserving list cardinality and order.
fn normalize_list_value_for_kind(
    field: &str,
    values: &[Value],
    expected_kind: &FieldKind,
    coercion: &CoercionSpec,
    op: CompareOp,
) -> Result<Value, ValidateError> {
    let mut normalized = Vec::with_capacity(values.len());
    for item in values {
        normalized.push(normalize_value_for_kind(
            field,
            item,
            expected_kind,
            coercion,
            op,
        )?);
    }

    Ok(Value::List(normalized))
}

fn normalize_enum_value(
    field: &str,
    value: &Value,
    expected_path: &str,
) -> Result<Value, ValidateError> {
    let Value::Enum(enum_value) = value else {
        return Ok(value.clone());
    };

    if let Some(path) = enum_value.path() {
        if path != expected_path {
            return Err(ValidateError::invalid_literal(
                field,
                "enum path does not match field enum type",
            ));
        }

        return Ok(value.clone());
    }

    let mut normalized = enum_value.clone();
    normalized.set_path(Some(expected_path.to_string()));
    Ok(Value::Enum(normalized))
}

///
/// Normalize a NOT expression.
///
/// Eliminates double negation:
///     NOT (NOT x)  →  x
///
fn normalize_not(inner: &Predicate) -> Predicate {
    let normalized = normalize(inner);

    if let Predicate::Not(double) = normalized {
        return normalize(&double);
    }

    Predicate::Not(Box::new(normalized))
}

///
/// Normalize an AND expression.
///
/// Rules:
/// - AND(True, x)        → x
/// - AND(False, x)       → False
/// - AND(AND(a, b), c)   → AND(a, b, c)
/// - AND()               → True
///
/// Children are sorted deterministically.
///
fn normalize_and(children: &[Predicate]) -> Predicate {
    let mut out = Vec::new();

    for child in children {
        let normalized = normalize(child);

        match normalized {
            Predicate::True => {}
            Predicate::False => return Predicate::False,
            Predicate::And(grandchildren) => out.extend(grandchildren),
            other => out.push(other),
        }
    }

    if out.is_empty() {
        return Predicate::True;
    }

    // Compare-pair simplification scans all conjunction children directly, so
    // it does not require a pre-sorted shape to preserve semantics.
    let Some(mut out) = simplify_and_compare_constraints(out) else {
        return Predicate::False;
    };

    // Canonicalize after simplification because compare folding can replace or
    // remove children and therefore change deterministic evaluation order.
    canonicalize_predicate_children_for_eval(&mut out);

    if out.len() == 1 {
        return out.remove(0);
    }

    Predicate::And(out)
}

///
/// Normalize an OR expression.
///
/// Rules:
/// - OR(False, x)       → x
/// - OR(True, x)        → True
/// - OR(OR(a, b), c)    → OR(a, b, c)
/// - OR()               → False
///
/// Children are sorted deterministically.
///
fn normalize_or(children: &[Predicate]) -> Predicate {
    let mut out = Vec::new();

    for child in children {
        let normalized = normalize(child);

        match normalized {
            Predicate::False => {}
            Predicate::True => return Predicate::True,
            Predicate::Or(grandchildren) => out.extend(grandchildren),
            other => out.push(other),
        }
    }

    if out.is_empty() {
        return Predicate::False;
    }

    // Canonicalize disjunction children once before OR-specific rewrites so the
    // collapse-to-IN check sees one deterministic shape.
    canonicalize_predicate_children_for_eval(&mut out);

    // Collapse canonical same-field equality disjunctions into one IN compare
    // at the predicate authority boundary.
    if let Some(collapsed) = collapse_same_field_or_eq_to_in(out.as_slice()) {
        return collapsed;
    }

    if out.len() == 1 {
        return out.remove(0);
    }

    Predicate::Or(out)
}

// Collapse `field = a OR field = b ...` into `field IN [a, b, ...]` when:
// - all children are equality compares
// - all children target the same field
// - all children share one supported coercion family
// - all equality literals are scalar-ish (not list/map payloads)
fn collapse_same_field_or_eq_to_in(children: &[Predicate]) -> Option<Predicate> {
    if children.len() < 2 {
        return None;
    }

    let mut leaves = Vec::with_capacity(children.len());

    for child in children {
        let Predicate::Compare(compare) = child else {
            return None;
        };
        if compare.op != CompareOp::Eq {
            return None;
        }
        if !matches!(
            compare.coercion.id,
            CoercionId::Strict | CoercionId::TextCasefold
        ) {
            return None;
        }
        if !or_eq_compare_value_is_in_safe(&compare.value) {
            return None;
        }
        leaves.push(MembershipCompareLeaf::new(
            compare.field.as_str(),
            compare.value.clone(),
            compare.coercion.id,
        ));
    }

    collapse_membership_compare_leaves(leaves, CompareOp::In).map(Predicate::Compare)
}

// Keep OR->IN canonicalization fail-closed for collection/map literals because
// list-like equality remains a distinct validation/runtime surface from `IN`.
const fn or_eq_compare_value_is_in_safe(value: &Value) -> bool {
    !matches!(value, Value::List(_) | Value::Map(_))
}

// Return a stable heuristic rank for predicate evaluation cost. Lower ranks
// are evaluated first after normalization.
const fn predicate_eval_cost_rank(predicate: &Predicate) -> u8 {
    match predicate {
        Predicate::True | Predicate::False => 0,
        Predicate::Compare(_)
        | Predicate::CompareFields(_)
        | Predicate::IsNull { .. }
        | Predicate::IsNotNull { .. }
        | Predicate::IsMissing { .. }
        | Predicate::IsEmpty { .. }
        | Predicate::IsNotEmpty { .. } => 1,
        Predicate::Not(_) => 2,
        Predicate::TextContains { .. } | Predicate::TextContainsCi { .. } => 3,
        Predicate::And(_) | Predicate::Or(_) => 4,
    }
}

// Canonicalize predicate child ordering for deterministic normalization and
// cheap-first short-circuit behavior.
fn canonicalize_predicate_children_for_eval(out: &mut Vec<Predicate>) {
    out.sort_by(canonical_cmp_predicate_for_eval);
    out.dedup();
}

// Compare predicate children with the same deterministic rank-first ordering
// used by normalization, without routing through the cached-key tuple surface.
fn canonical_cmp_predicate_for_eval(left: &Predicate, right: &Predicate) -> std::cmp::Ordering {
    let rank = predicate_eval_cost_rank(left).cmp(&predicate_eval_cost_rank(right));
    if rank != std::cmp::Ordering::Equal {
        return rank;
    }

    sort_key(left).cmp(&sort_key(right))
}

///
/// Generate a deterministic, length-prefixed key for a predicate.
///
/// This key is used **only for sorting**, not for display.
/// Ordering ensures:
/// - planner determinism
/// - stable normalization
/// - predictable equality
///
fn sort_key(predicate: &Predicate) -> Vec<u8> {
    encode_predicate_sort_key(predicate)
}

///
/// TESTS
///

#[cfg(test)]
mod tests;