sift-query-core 0.1.0

Backend-independent query model and strict parameter parser for SiftQuery
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
use crate::{ConfigError, FieldId, ParamError, ScalarValue};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RegisteredOperator {
    pub operator: CustomOperator,
    pub field_type: crate::ValueType,
    pub input_type: crate::ValueType,
}

/// Trusted server-side SQL template for a custom operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CustomOperator {
    name: &'static str,
    template: &'static str,
    arity: u8,
    types: Option<(crate::ValueType, crate::ValueType)>,
    exists_kind: Option<ExistsKind>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ExistsKind {
    Exists,
    NotExists,
}

impl CustomOperator {
    pub fn new(name: &'static str, template: &'static str, arity: u8) -> Result<Self, ConfigError> {
        if name.is_empty()
            || name.len() > 64
            || template.len() > 4096
            || !name
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
        {
            return Err(ConfigError::Invalid(
                "custom operator name must be ASCII alphanumeric or underscore".to_owned(),
            ));
        }
        if !template.contains("{field}")
            || template.contains(';')
            || template.contains("--")
            || template.contains("/*")
            || template.contains('$')
            || template.contains('\0')
        {
            return Err(ConfigError::Invalid(
                "custom operator template contains a forbidden token".to_owned(),
            ));
        }
        validate_template_placeholders(template, arity)?;
        Ok(Self {
            name,
            template,
            arity,
            types: None,
            exists_kind: None,
        })
    }

    /// Declares a trusted correlated `EXISTS (SELECT ...)` relation filter.
    /// The complete SQL fragment is server-owned; all `{0}` values remain
    /// bound parameters and `{field}` is replaced by the selected outer field.
    pub fn exists(
        name: &'static str,
        template: &'static str,
        arity: u8,
    ) -> Result<Self, ConfigError> {
        Self::exists_kind(name, template, arity, ExistsKind::Exists)
    }

    /// Declares a trusted correlated `NOT EXISTS (SELECT ...)` relation filter.
    pub fn not_exists(
        name: &'static str,
        template: &'static str,
        arity: u8,
    ) -> Result<Self, ConfigError> {
        Self::exists_kind(name, template, arity, ExistsKind::NotExists)
    }

    fn exists_kind(
        name: &'static str,
        template: &'static str,
        arity: u8,
        kind: ExistsKind,
    ) -> Result<Self, ConfigError> {
        let upper = template.trim_start().to_ascii_uppercase();
        let prefix = match kind {
            ExistsKind::Exists => "EXISTS (SELECT",
            ExistsKind::NotExists => "NOT EXISTS (SELECT",
        };
        if !upper.starts_with(prefix) {
            return Err(ConfigError::Invalid(
                "exists template must start with EXISTS (SELECT ...) or NOT EXISTS (SELECT ...)"
                    .to_owned(),
            ));
        }
        let mut operator = Self::new(name, template, arity)?;
        operator.exists_kind = Some(kind);
        Ok(operator)
    }

    /// A trusted operator with distinct field/result and query input types.
    pub fn typed(
        name: &'static str,
        template: &'static str,
        arity: u8,
        field_type: crate::ValueType,
        input_type: crate::ValueType,
    ) -> Result<Self, ConfigError> {
        let mut operator = Self::new(name, template, arity)?;
        operator.types = Some((field_type, input_type));
        Ok(operator)
    }

    pub const fn types(self) -> Option<(crate::ValueType, crate::ValueType)> {
        self.types
    }

    #[must_use]
    pub const fn name(self) -> &'static str {
        self.name
    }

    #[must_use]
    pub const fn template(self) -> &'static str {
        self.template
    }

    #[must_use]
    pub const fn arity(self) -> u8 {
        self.arity
    }

    #[must_use]
    pub const fn is_exists(self) -> bool {
        matches!(self.exists_kind, Some(ExistsKind::Exists))
    }

    #[must_use]
    pub const fn is_not_exists(self) -> bool {
        matches!(self.exists_kind, Some(ExistsKind::NotExists))
    }
}

fn validate_template_placeholders(template: &str, arity: u8) -> Result<(), ConfigError> {
    let mut referenced = vec![false; usize::from(arity)];
    let mut remaining = template;
    while let Some(start) = remaining.find('{') {
        let after = &remaining[start + 1..];
        let Some(end) = after.find('}') else {
            return Err(ConfigError::Invalid(
                "custom operator template has an unclosed placeholder".to_owned(),
            ));
        };
        let token = &after[..end];
        if token != "field" {
            let index = token.parse::<usize>().map_err(|_| {
                ConfigError::Invalid(format!("unknown custom operator placeholder {{{token}}}"))
            })?;
            let Some(slot) = referenced.get_mut(index) else {
                return Err(ConfigError::Invalid(format!(
                    "custom operator placeholder {{{index}}} exceeds arity {arity}"
                )));
            };
            *slot = true;
        }
        remaining = &after[end + 1..];
    }
    if referenced.iter().any(|seen| !seen) {
        return Err(ConfigError::Invalid(
            "custom operator template must reference every value".to_owned(),
        ));
    }
    Ok(())
}

/// M1 field operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Operator {
    Eq,
    Ne,
    Gt,
    Ge,
    Lt,
    Le,
    Between,
    NotBetween,
    InList,
    NotIn,
    IsNull,
    NotNull,
    Empty,
    NotEmpty,
    StartWith,
    EndWith,
    Contain,
    OrLike,
    NotLike,
    AlwaysTrue,
    AlwaysFalse,
    Custom(CustomOperator),
}

impl Operator {
    pub fn input_type(self, field_type: crate::ValueType) -> crate::ValueType {
        match self {
            Self::Custom(operator) => operator.types.map_or(field_type, |types| types.1),
            _ => field_type,
        }
    }

    pub fn accepts_field_type(self, field_type: crate::ValueType) -> bool {
        match self {
            Self::Custom(operator) => operator.types.is_none_or(|types| types.0 == field_type),
            _ => true,
        }
    }
    #[must_use]
    pub const fn short_name(self) -> &'static str {
        match self {
            Self::Eq => "eq",
            Self::Ne => "ne",
            Self::Gt => "gt",
            Self::Ge => "ge",
            Self::Lt => "lt",
            Self::Le => "le",
            Self::Between => "bt",
            Self::NotBetween => "nb",
            Self::InList => "il",
            Self::NotIn => "ni",
            Self::IsNull => "nl",
            Self::NotNull => "nn",
            Self::Empty => "ey",
            Self::NotEmpty => "ny",
            Self::StartWith => "sw",
            Self::EndWith => "ew",
            Self::Contain => "ct",
            Self::OrLike => "ol",
            Self::NotLike => "nk",
            Self::AlwaysTrue => "at",
            Self::AlwaysFalse => "af",
            Self::Custom(operator) => operator.name(),
        }
    }

    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "eq" => Some(Self::Eq),
            "ne" => Some(Self::Ne),
            "gt" => Some(Self::Gt),
            "ge" => Some(Self::Ge),
            "lt" => Some(Self::Lt),
            "le" => Some(Self::Le),
            "bt" => Some(Self::Between),
            "nb" => Some(Self::NotBetween),
            "il" => Some(Self::InList),
            "ni" => Some(Self::NotIn),
            "nl" => Some(Self::IsNull),
            "nn" => Some(Self::NotNull),
            "ey" => Some(Self::Empty),
            "ny" => Some(Self::NotEmpty),
            "sw" => Some(Self::StartWith),
            "ew" => Some(Self::EndWith),
            "ct" => Some(Self::Contain),
            "ol" => Some(Self::OrLike),
            "nk" => Some(Self::NotLike),
            "at" => Some(Self::AlwaysTrue),
            "af" => Some(Self::AlwaysFalse),
            _ => None,
        }
    }

    #[must_use]
    pub const fn expected_arity(self) -> &'static str {
        match self {
            Self::Between | Self::NotBetween => "2",
            Self::InList | Self::NotIn | Self::OrLike => "1..N",
            Self::IsNull
            | Self::NotNull
            | Self::Empty
            | Self::NotEmpty
            | Self::AlwaysTrue
            | Self::AlwaysFalse => "0",
            Self::Custom(_) => "custom",
            _ => "1",
        }
    }

    #[must_use]
    pub fn accepts_arity(self, actual: usize) -> bool {
        match self {
            Self::Between | Self::NotBetween => actual == 2,
            Self::InList | Self::NotIn | Self::OrLike => actual > 0,
            Self::IsNull
            | Self::NotNull
            | Self::Empty
            | Self::NotEmpty
            | Self::AlwaysTrue
            | Self::AlwaysFalse => actual == 0,
            Self::Custom(operator) => actual == usize::from(operator.arity()),
            _ => actual == 1,
        }
    }

    #[must_use]
    pub const fn supports_ignore_case(self) -> bool {
        matches!(
            self,
            Self::Eq
                | Self::Ne
                | Self::StartWith
                | Self::EndWith
                | Self::Contain
                | Self::OrLike
                | Self::NotLike
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PredicatePhase {
    Row,
    Group,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Condition {
    pub field: FieldId,
    pub operator: Operator,
    pub values: Vec<ScalarValue>,
    pub ignore_case: bool,
    pub phase: PredicatePhase,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct NodeId(pub u16);

#[derive(Debug, Clone, PartialEq)]
pub enum PredicateNode {
    Condition(Condition),
    And(Vec<NodeId>),
    Or(Vec<NodeId>),
}

/// Compact predicate storage with index-based edges.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PredicateArena {
    nodes: Vec<PredicateNode>,
    root: Option<NodeId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PhaseClass {
    None,
    Row,
    Group,
    Mixed,
}

impl PredicateArena {
    #[must_use]
    pub fn empty() -> Self {
        Self::default()
    }

    pub fn from_condition(condition: Condition) -> Self {
        let mut arena = Self::default();
        let root = arena.push(PredicateNode::Condition(condition));
        arena.root = Some(root);
        arena
    }

    #[must_use]
    pub fn nodes(&self) -> &[PredicateNode] {
        &self.nodes
    }

    #[must_use]
    pub const fn root(&self) -> Option<NodeId> {
        self.root
    }

    #[must_use]
    pub fn node(&self, id: NodeId) -> Option<&PredicateNode> {
        self.nodes.get(usize::from(id.0))
    }

    fn push(&mut self, node: PredicateNode) -> NodeId {
        let id = u16::try_from(self.nodes.len()).expect("predicate budget exceeds u16");
        self.nodes.push(node);
        NodeId(id)
    }

    pub(crate) fn combine_and(arenas: impl IntoIterator<Item = Self>) -> Self {
        Self::combine(arenas, true)
    }

    /// Combines independent predicates with AND.
    pub fn all(arenas: impl IntoIterator<Item = Self>) -> Self {
        Self::combine_and(arenas)
    }

    fn combine(arenas: impl IntoIterator<Item = Self>, and: bool) -> Self {
        let mut output = Self::default();
        let mut roots = Vec::new();
        for arena in arenas {
            let offset = u16::try_from(output.nodes.len()).expect("predicate budget exceeds u16");
            for node in arena.nodes {
                output.nodes.push(remap_node(node, offset));
            }
            if let Some(root) = arena.root {
                roots.push(NodeId(root.0 + offset));
            }
        }
        output.root = match roots.len() {
            0 => None,
            1 => roots.first().copied(),
            _ => Some(output.push(if and {
                PredicateNode::And(roots)
            } else {
                PredicateNode::Or(roots)
            })),
        };
        output
    }

    pub(crate) fn combine_or(arenas: impl IntoIterator<Item = Self>) -> Self {
        Self::combine(arenas, false)
    }

    /// Combines independent predicates with OR.
    pub fn any(arenas: impl IntoIterator<Item = Self>) -> Self {
        Self::combine_or(arenas)
    }

    /// Splits an AND-separable tree into WHERE and HAVING trees.
    pub fn partition(&self) -> Result<(Self, Self), ParamError> {
        let Some(root) = self.root else {
            return Ok((Self::default(), Self::default()));
        };
        self.validate_or_phases(root)?;
        let mut row = Self::default();
        let mut group = Self::default();
        row.root = self.copy_phase(root, PredicatePhase::Row, &mut row);
        group.root = self.copy_phase(root, PredicatePhase::Group, &mut group);
        Ok((row, group))
    }

    /// Returns a predicate with conditions on `field` removed.
    ///
    /// This is used by facet self-exclusion. The remaining boolean tree keeps
    /// its original shape; a branch that contains only the excluded field is
    /// treated as an empty branch so the facet can evaluate the rest of the
    /// query without accidentally broadening the server-owned predicates.
    #[must_use]
    pub fn without_field(&self, field: FieldId) -> Self {
        let mut output = Self::default();
        let Some(root) = self.root else {
            return output;
        };
        output.root = self.copy_without_field(root, field, &mut output);
        output
    }

    fn copy_without_field(&self, id: NodeId, field: FieldId, output: &mut Self) -> Option<NodeId> {
        match self.node(id).expect("valid predicate node") {
            PredicateNode::Condition(condition) => (condition.field != field)
                .then(|| output.push(PredicateNode::Condition(condition.clone()))),
            PredicateNode::And(children) => {
                let copied = children
                    .iter()
                    .filter_map(|child| self.copy_without_field(*child, field, output))
                    .collect::<Vec<_>>();
                collapse(output, copied, true)
            }
            PredicateNode::Or(children) => {
                let copied = children
                    .iter()
                    .filter_map(|child| self.copy_without_field(*child, field, output))
                    .collect::<Vec<_>>();
                collapse(output, copied, false)
            }
        }
    }

    fn validate_or_phases(&self, id: NodeId) -> Result<PhaseClass, ParamError> {
        let node = self.node(id).expect("valid predicate node");
        match node {
            PredicateNode::Condition(condition) => Ok(match condition.phase {
                PredicatePhase::Row => PhaseClass::Row,
                PredicatePhase::Group => PhaseClass::Group,
            }),
            PredicateNode::And(children) => {
                let mut class = PhaseClass::None;
                for child in children {
                    class = merge_phase(class, self.validate_or_phases(*child)?);
                }
                Ok(class)
            }
            PredicateNode::Or(children) => {
                let mut class = PhaseClass::None;
                for child in children {
                    let child_class = self.validate_or_phases(*child)?;
                    if child_class == PhaseClass::Mixed {
                        return Err(ParamError::MixedPredicatePhase);
                    }
                    class = merge_phase(class, child_class);
                    if class == PhaseClass::Mixed {
                        return Err(ParamError::MixedPredicatePhase);
                    }
                }
                Ok(class)
            }
        }
    }

    fn copy_phase(&self, id: NodeId, phase: PredicatePhase, output: &mut Self) -> Option<NodeId> {
        match self.node(id).expect("valid predicate node") {
            PredicateNode::Condition(condition) => (condition.phase == phase)
                .then(|| output.push(PredicateNode::Condition(condition.clone()))),
            PredicateNode::And(children) => {
                let copied = children
                    .iter()
                    .filter_map(|child| self.copy_phase(*child, phase, output))
                    .collect::<Vec<_>>();
                collapse(output, copied, true)
            }
            PredicateNode::Or(children) => {
                let copied = children
                    .iter()
                    .filter_map(|child| self.copy_phase(*child, phase, output))
                    .collect::<Vec<_>>();
                collapse(output, copied, false)
            }
        }
    }
}

fn collapse(arena: &mut PredicateArena, children: Vec<NodeId>, and: bool) -> Option<NodeId> {
    match children.len() {
        0 => None,
        1 => children.first().copied(),
        _ => Some(arena.push(if and {
            PredicateNode::And(children)
        } else {
            PredicateNode::Or(children)
        })),
    }
}

fn remap_node(node: PredicateNode, offset: u16) -> PredicateNode {
    match node {
        PredicateNode::Condition(condition) => PredicateNode::Condition(condition),
        PredicateNode::And(children) => PredicateNode::And(
            children
                .into_iter()
                .map(|id| NodeId(id.0 + offset))
                .collect(),
        ),
        PredicateNode::Or(children) => PredicateNode::Or(
            children
                .into_iter()
                .map(|id| NodeId(id.0 + offset))
                .collect(),
        ),
    }
}

const fn merge_phase(left: PhaseClass, right: PhaseClass) -> PhaseClass {
    match (left, right) {
        (PhaseClass::None, value) | (value, PhaseClass::None) => value,
        (PhaseClass::Row, PhaseClass::Row) => PhaseClass::Row,
        (PhaseClass::Group, PhaseClass::Group) => PhaseClass::Group,
        _ => PhaseClass::Mixed,
    }
}