icydb 0.86.1

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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use crate::{
    traits::{EntityKind, FieldValue},
    value::Value,
};
use candid::CandidType;
use icydb_core::db::{
    CoercionId, CompareOp, ComparePredicate, FilterExpr as CoreFilterExpr,
    OrderDirection as CoreOrderDirection, Predicate, QueryError, SortExpr as CoreSortExpr,
};
use serde::Deserialize;

//
// FilterExpr
//
// Serialized, planner-agnostic predicate language.
//
// This enum is intentionally isomorphic to the subset of core::Predicate that is:
// - deterministic
// - schema-visible
// - safe across API boundaries
//
// No planner hints, no implicit semantics, no overloaded operators.
// Any new Predicate variant must be explicitly reviewed for exposure here.
//

#[derive(CandidType, Clone, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum FilterExpr {
    /// Always true.
    True,
    /// Always false.
    False,

    And(Vec<Self>),
    Or(Vec<Self>),
    Not(Box<Self>),

    // ─────────────────────────────────────────────────────────────
    // Scalar comparisons
    // ─────────────────────────────────────────────────────────────
    Eq {
        field: String,
        value: Value,
    },
    Ne {
        field: String,
        value: Value,
    },
    Lt {
        field: String,
        value: Value,
    },
    Lte {
        field: String,
        value: Value,
    },
    Gt {
        field: String,
        value: Value,
    },
    Gte {
        field: String,
        value: Value,
    },

    In {
        field: String,
        values: Vec<Value>,
    },
    NotIn {
        field: String,
        values: Vec<Value>,
    },

    // ─────────────────────────────────────────────────────────────
    // Collection predicates
    // ─────────────────────────────────────────────────────────────
    /// Collection contains value.
    Contains {
        field: String,
        value: Value,
    },

    // ─────────────────────────────────────────────────────────────
    // Text predicates (explicit, no overloading)
    // ─────────────────────────────────────────────────────────────
    /// Case-sensitive substring match.
    TextContains {
        field: String,
        value: Value,
    },

    /// Case-insensitive substring match.
    TextContainsCi {
        field: String,
        value: Value,
    },

    StartsWith {
        field: String,
        value: Value,
    },
    StartsWithCi {
        field: String,
        value: Value,
    },

    EndsWith {
        field: String,
        value: Value,
    },
    EndsWithCi {
        field: String,
        value: Value,
    },

    // ─────────────────────────────────────────────────────────────
    // Presence / nullability
    // ─────────────────────────────────────────────────────────────
    /// Field is present and explicitly null.
    IsNull {
        field: String,
    },

    /// Field is present and not null.
    /// Equivalent to: NOT IsNull AND NOT IsMissing
    IsNotNull {
        field: String,
    },

    /// Field is not present at all.
    IsMissing {
        field: String,
    },

    /// Field is present but empty (collection or string).
    IsEmpty {
        field: String,
    },

    /// Field is present and non-empty.
    IsNotEmpty {
        field: String,
    },
}

impl FilterExpr {
    // ─────────────────────────────────────────────────────────────
    // Lowering
    // ─────────────────────────────────────────────────────────────

    /// Lower this API-level filter expression into core predicate IR.
    ///
    /// Lowering applies explicit coercion policies so execution semantics are stable.
    #[expect(clippy::too_many_lines)]
    pub fn lower<E: EntityKind>(&self) -> Result<CoreFilterExpr, QueryError> {
        let lower_pred =
            |expr: &Self| -> Result<Predicate, QueryError> { Ok(expr.lower::<E>()?.0) };

        let pred = match self {
            Self::True => Predicate::True,
            Self::False => Predicate::False,

            Self::And(xs) => {
                Predicate::and(xs.iter().map(lower_pred).collect::<Result<Vec<_>, _>>()?)
            }
            Self::Or(xs) => {
                Predicate::or(xs.iter().map(lower_pred).collect::<Result<Vec<_>, _>>()?)
            }
            Self::Not(x) => Predicate::not(lower_pred(x)?),

            Self::Eq { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Eq,
                value.clone(),
                CoercionId::Strict,
            )),

            Self::Ne { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Ne,
                value.clone(),
                CoercionId::Strict,
            )),

            Self::Lt { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Lt,
                value.clone(),
                CoercionId::NumericWiden,
            )),

            Self::Lte { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Lte,
                value.clone(),
                CoercionId::NumericWiden,
            )),

            Self::Gt { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Gt,
                value.clone(),
                CoercionId::NumericWiden,
            )),

            Self::Gte { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Gte,
                value.clone(),
                CoercionId::NumericWiden,
            )),

            Self::In { field, values } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::In,
                Value::List(values.clone()),
                CoercionId::Strict,
            )),

            Self::NotIn { field, values } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::NotIn,
                Value::List(values.clone()),
                CoercionId::Strict,
            )),

            Self::Contains { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::Contains,
                value.clone(),
                CoercionId::Strict,
            )),

            Self::TextContains { field, value } => Predicate::TextContains {
                field: field.clone(),
                value: value.clone(),
            },

            Self::TextContainsCi { field, value } => Predicate::TextContainsCi {
                field: field.clone(),
                value: value.clone(),
            },

            Self::StartsWith { field, value } => {
                Predicate::Compare(ComparePredicate::with_coercion(
                    field.as_str(),
                    CompareOp::StartsWith,
                    value.clone(),
                    CoercionId::Strict,
                ))
            }

            Self::StartsWithCi { field, value } => {
                Predicate::Compare(ComparePredicate::with_coercion(
                    field.as_str(),
                    CompareOp::StartsWith,
                    value.clone(),
                    CoercionId::TextCasefold,
                ))
            }

            Self::EndsWith { field, value } => Predicate::Compare(ComparePredicate::with_coercion(
                field.as_str(),
                CompareOp::EndsWith,
                value.clone(),
                CoercionId::Strict,
            )),

            Self::EndsWithCi { field, value } => {
                Predicate::Compare(ComparePredicate::with_coercion(
                    field.as_str(),
                    CompareOp::EndsWith,
                    value.clone(),
                    CoercionId::TextCasefold,
                ))
            }

            Self::IsNull { field } => Predicate::IsNull {
                field: field.clone(),
            },

            Self::IsNotNull { field } => Predicate::and(vec![
                Predicate::not(Predicate::IsNull {
                    field: field.clone(),
                }),
                Predicate::not(Predicate::IsMissing {
                    field: field.clone(),
                }),
            ]),

            Self::IsMissing { field } => Predicate::IsMissing {
                field: field.clone(),
            },

            Self::IsEmpty { field } => Predicate::IsEmpty {
                field: field.clone(),
            },

            Self::IsNotEmpty { field } => Predicate::IsNotEmpty {
                field: field.clone(),
            },
        };

        Ok(CoreFilterExpr(pred))
    }

    // ─────────────────────────────────────────────────────────────
    // Boolean
    // ─────────────────────────────────────────────────────────────

    /// Build an `And` expression from a list of child expressions.
    #[must_use]
    pub const fn and(exprs: Vec<Self>) -> Self {
        Self::And(exprs)
    }

    /// Build an `Or` expression from a list of child expressions.
    #[must_use]
    pub const fn or(exprs: Vec<Self>) -> Self {
        Self::Or(exprs)
    }

    /// Negate one child expression.
    #[must_use]
    #[expect(clippy::should_implement_trait)]
    pub fn not(expr: Self) -> Self {
        Self::Not(Box::new(expr))
    }

    // ─────────────────────────────────────────────────────────────
    // Scalar comparisons
    // ─────────────────────────────────────────────────────────────

    /// Compare `field == value`.
    pub fn eq(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Eq {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field != value`.
    pub fn ne(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Ne {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field < value`.
    pub fn lt(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Lt {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field <= value`.
    pub fn lte(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Lte {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field > value`.
    pub fn gt(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Gt {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field >= value`.
    pub fn gte(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Gte {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare `field IN values`.
    pub fn in_list(
        field: impl Into<String>,
        values: impl IntoIterator<Item = impl FieldValue>,
    ) -> Self {
        Self::In {
            field: field.into(),
            values: values.into_iter().map(|v| v.to_value()).collect(),
        }
    }

    /// Compare `field NOT IN values`.
    pub fn not_in(
        field: impl Into<String>,
        values: impl IntoIterator<Item = impl FieldValue>,
    ) -> Self {
        Self::NotIn {
            field: field.into(),
            values: values.into_iter().map(|v| v.to_value()).collect(),
        }
    }

    // ─────────────────────────────────────────────────────────────
    // Collection
    // ─────────────────────────────────────────────────────────────

    /// Compare collection `field CONTAINS value`.
    pub fn contains(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::Contains {
            field: field.into(),
            value: value.to_value(),
        }
    }

    // ─────────────────────────────────────────────────────────────
    // Text predicates
    // ─────────────────────────────────────────────────────────────

    /// Compare case-sensitive substring containment.
    pub fn text_contains(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::TextContains {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare case-insensitive substring containment.
    pub fn text_contains_ci(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::TextContainsCi {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare case-sensitive prefix match.
    pub fn starts_with(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::StartsWith {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare case-insensitive prefix match.
    pub fn starts_with_ci(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::StartsWithCi {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare case-sensitive suffix match.
    pub fn ends_with(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::EndsWith {
            field: field.into(),
            value: value.to_value(),
        }
    }

    /// Compare case-insensitive suffix match.
    pub fn ends_with_ci(field: impl Into<String>, value: impl FieldValue) -> Self {
        Self::EndsWithCi {
            field: field.into(),
            value: value.to_value(),
        }
    }

    // ─────────────────────────────────────────────────────────────
    // Presence / nullability
    // ─────────────────────────────────────────────────────────────

    /// Match rows where `field` is present and null.
    pub fn is_null(field: impl Into<String>) -> Self {
        Self::IsNull {
            field: field.into(),
        }
    }

    /// Match rows where `field` is present and non-null.
    pub fn is_not_null(field: impl Into<String>) -> Self {
        Self::IsNotNull {
            field: field.into(),
        }
    }

    /// Match rows where `field` is absent.
    pub fn is_missing(field: impl Into<String>) -> Self {
        Self::IsMissing {
            field: field.into(),
        }
    }

    /// Match rows where `field` is present and empty.
    pub fn is_empty(field: impl Into<String>) -> Self {
        Self::IsEmpty {
            field: field.into(),
        }
    }

    /// Match rows where `field` is present and non-empty.
    pub fn is_not_empty(field: impl Into<String>) -> Self {
        Self::IsNotEmpty {
            field: field.into(),
        }
    }
}

//
// SortExpr
//

#[derive(CandidType, Clone, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct SortExpr {
    fields: Vec<(String, OrderDirection)>,
}

impl SortExpr {
    /// Build a sort specification from ordered `(field, direction)` pairs.
    #[must_use]
    pub const fn new(fields: Vec<(String, OrderDirection)>) -> Self {
        Self { fields }
    }

    /// Borrow the ordered sort fields.
    #[must_use]
    pub fn fields(&self) -> &[(String, OrderDirection)] {
        &self.fields
    }

    /// Lower this API-level sort expression into core sort IR.
    #[must_use]
    pub fn lower(&self) -> CoreSortExpr {
        let fields = self
            .fields()
            .iter()
            .map(|(field, dir)| {
                let dir = match dir {
                    OrderDirection::Asc => CoreOrderDirection::Asc,
                    OrderDirection::Desc => CoreOrderDirection::Desc,
                };
                (field.clone(), dir)
            })
            .collect();

        CoreSortExpr::new(fields)
    }
}

//
// OrderDirection
//

#[derive(CandidType, Clone, Copy, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum OrderDirection {
    Asc,
    Desc,
}

//
// TESTS
//

#[cfg(test)]
mod tests {
    use super::{FilterExpr, OrderDirection, SortExpr};
    use candid::types::{CandidType, Label, Type, TypeInner};

    fn expect_record_fields(ty: Type) -> Vec<String> {
        match ty.as_ref() {
            TypeInner::Record(fields) => fields
                .iter()
                .map(|field| match field.id.as_ref() {
                    Label::Named(name) => name.clone(),
                    other => panic!("expected named record field, got {other:?}"),
                })
                .collect(),
            other => panic!("expected candid record, got {other:?}"),
        }
    }

    fn expect_variant_labels(ty: Type) -> Vec<String> {
        match ty.as_ref() {
            TypeInner::Variant(fields) => fields
                .iter()
                .map(|field| match field.id.as_ref() {
                    Label::Named(name) => name.clone(),
                    other => panic!("expected named variant label, got {other:?}"),
                })
                .collect(),
            other => panic!("expected candid variant, got {other:?}"),
        }
    }

    fn expect_variant_field_type(ty: Type, variant_name: &str) -> Type {
        match ty.as_ref() {
            TypeInner::Variant(fields) => fields
                .iter()
                .find_map(|field| match field.id.as_ref() {
                    Label::Named(name) if name == variant_name => Some(field.ty.clone()),
                    _ => None,
                })
                .unwrap_or_else(|| panic!("expected variant label `{variant_name}`")),
            other => panic!("expected candid variant, got {other:?}"),
        }
    }

    #[test]
    fn filter_expr_eq_candid_payload_shape_is_stable() {
        let fields = expect_record_fields(expect_variant_field_type(FilterExpr::ty(), "Eq"));

        for field in ["field", "value"] {
            assert!(
                fields.iter().any(|candidate| candidate == field),
                "Eq payload must keep `{field}` field key in Candid shape",
            );
        }
    }

    #[test]
    fn filter_expr_and_candid_payload_shape_is_stable() {
        match expect_variant_field_type(FilterExpr::ty(), "And").as_ref() {
            TypeInner::Vec(_) => {}
            other => panic!("And payload must remain a Candid vec payload, got {other:?}"),
        }
    }

    #[test]
    fn sort_expr_candid_field_name_is_stable() {
        let fields = expect_record_fields(SortExpr::ty());

        assert!(
            fields.iter().any(|candidate| candidate == "fields"),
            "SortExpr must keep `fields` as Candid field key",
        );
    }

    #[test]
    fn order_direction_variant_labels_are_stable() {
        let mut labels = expect_variant_labels(OrderDirection::ty());
        labels.sort_unstable();
        assert_eq!(labels, vec!["Asc".to_string(), "Desc".to_string()]);
    }

    #[test]
    fn filter_expr_text_contains_ci_candid_payload_shape_is_stable() {
        let fields = expect_record_fields(expect_variant_field_type(
            FilterExpr::ty(),
            "TextContainsCi",
        ));

        for field in ["field", "value"] {
            assert!(
                fields.iter().any(|candidate| candidate == field),
                "TextContainsCi payload must keep `{field}` field key in Candid shape",
            );
        }
    }

    #[test]
    fn filter_expr_not_payload_shape_is_stable() {
        match expect_variant_field_type(FilterExpr::ty(), "Not").as_ref() {
            TypeInner::Var(_) | TypeInner::Knot(_) | TypeInner::Variant(_) => {}
            other => panic!("Not payload must keep nested predicate payload, got {other:?}"),
        }
    }

    #[test]
    fn filter_expr_variant_labels_are_stable() {
        let labels = expect_variant_labels(FilterExpr::ty());

        for label in ["Eq", "And", "Not", "TextContainsCi", "IsMissing"] {
            assert!(
                labels.iter().any(|candidate| candidate == label),
                "FilterExpr must keep `{label}` variant label",
            );
        }
    }

    #[test]
    fn query_expr_fixture_constructors_stay_usable() {
        let expr = FilterExpr::and(vec![
            FilterExpr::is_null("deleted_at"),
            FilterExpr::not(FilterExpr::is_missing("name")),
        ]);
        let sort = SortExpr::new(vec![("created_at".to_string(), OrderDirection::Desc)]);

        match expr {
            FilterExpr::And(items) => assert_eq!(items.len(), 2),
            other => panic!("expected And fixture, got {other:?}"),
        }

        assert_eq!(sort.fields().len(), 1);
        assert!(matches!(sort.fields()[0].1, OrderDirection::Desc));
    }
}