corium-query 0.1.7

Corium query implementation
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Query AST and parsing from EDN forms (map and vector shapes).

use std::collections::BTreeSet;

use crate::QueryError;
use crate::edn::Edn;

/// A logic variable (`?name`).
pub type Var = String;

/// Default database source name.
pub const DEFAULT_SRC: &str = "$";

/// A parsed query.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Query {
    /// The `:find` specification.
    pub find: FindSpec,
    /// Extra grouping variables (`:with`).
    pub with: Vec<Var>,
    /// Input bindings (`:in`), defaulting to `[$]`.
    pub inputs: Vec<InSpec>,
    /// The `:where` clauses.
    pub wheres: Vec<Clause>,
}

/// The shape of `:find`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FindSpec {
    /// Relation of tuples.
    Rel(Vec<FindElem>),
    /// Collection `[?x …]`.
    Coll(FindElem),
    /// Single tuple `[?x ?y]`.
    Tuple(Vec<FindElem>),
    /// Scalar `?x .`.
    Scalar(FindElem),
}

impl FindSpec {
    /// All find elements in order.
    #[must_use]
    pub fn elems(&self) -> Vec<&FindElem> {
        match self {
            Self::Rel(elems) | Self::Tuple(elems) => elems.iter().collect(),
            Self::Coll(elem) | Self::Scalar(elem) => vec![elem],
        }
    }
}

/// One element of a find specification.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FindElem {
    /// A plain variable.
    Var(Var),
    /// An aggregate call `(op args… ?var)`.
    Aggregate(Aggregate),
    /// A pull expression `(pull ?e pattern)`.
    Pull(Var, Edn),
}

impl FindElem {
    /// The variable this element projects.
    #[must_use]
    pub fn var(&self) -> &Var {
        match self {
            Self::Var(v) | Self::Pull(v, _) => v,
            Self::Aggregate(agg) => &agg.var,
        }
    }
}

/// An aggregate find element.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Aggregate {
    /// Operation name (`count`, `sum`, …).
    pub op: String,
    /// Optional leading constant argument (`(min 3 ?x)`).
    pub n: Option<i64>,
    /// Aggregated variable.
    pub var: Var,
}

/// One `:in` binding.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InSpec {
    /// A database source (`$`, `$hist`, …).
    Db(String),
    /// A rule set (`%`).
    Rules,
    /// Scalar binding `?x`.
    Scalar(Var),
    /// Tuple binding `[?x ?y]`.
    Tuple(Vec<Var>),
    /// Collection binding `[?x …]`.
    Coll(Var),
    /// Relation binding `[[?x ?y]]`.
    Rel(Vec<Var>),
}

/// A term in a pattern or call position.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Term {
    /// A variable.
    Var(Var),
    /// The blank `_`.
    Blank,
    /// A constant EDN form, resolved against a database at execution.
    Const(Edn),
}

impl Term {
    fn parse(form: &Edn) -> Self {
        match form.as_symbol() {
            Some("_") => Self::Blank,
            Some(sym) if sym.starts_with('?') => Self::Var(sym.to_owned()),
            _ => Self::Const(form.clone()),
        }
    }
}

/// A data pattern with up to five positions (`added` binds on history views).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Pattern {
    /// Database source name.
    pub src: String,
    /// Entity position.
    pub e: Term,
    /// Attribute position.
    pub a: Term,
    /// Value position.
    pub v: Term,
    /// Transaction position.
    pub tx: Term,
    /// Assertion flag position.
    pub added: Term,
}

/// A `:where` clause.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Clause {
    /// A data pattern.
    Pattern(Pattern),
    /// A predicate `[(pred args…)]`.
    Pred {
        /// Predicate name.
        name: String,
        /// Argument terms.
        args: Vec<Term>,
    },
    /// A function `[(f args…) binding]`.
    Fn {
        /// Function name.
        name: String,
        /// Argument terms.
        args: Vec<Term>,
        /// Output binding.
        binding: Binding,
    },
    /// `not` / `not-join`.
    Not {
        /// Database source name.
        src: String,
        /// Join variables for `not-join`; `None` for plain `not`.
        vars: Option<Vec<Var>>,
        /// Negated clauses.
        clauses: Vec<Clause>,
    },
    /// `or` / `or-join`.
    Or {
        /// Database source name.
        src: String,
        /// Join variables for `or-join`; `None` for plain `or`.
        vars: Option<Vec<Var>>,
        /// Alternative clause groups.
        branches: Vec<Vec<Clause>>,
    },
    /// A rule invocation `(rule-name args…)`.
    RuleCall {
        /// Rule name.
        name: String,
        /// Argument terms.
        args: Vec<Term>,
    },
}

/// One rule definition.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RuleDef {
    /// Rule name.
    pub name: String,
    /// Head variables that must be bound at invocation.
    pub required: Vec<Var>,
    /// Remaining head variables.
    pub free: Vec<Var>,
    /// Body clauses.
    pub clauses: Vec<Clause>,
}

impl RuleDef {
    /// All head variables in argument order.
    #[must_use]
    pub fn head_vars(&self) -> Vec<&Var> {
        self.required.iter().chain(self.free.iter()).collect()
    }
}

fn parse_error(message: impl Into<String>) -> QueryError {
    QueryError::Parse(message.into())
}

fn expect_var(form: &Edn) -> Result<Var, QueryError> {
    match form.as_symbol() {
        Some(sym) if sym.starts_with('?') => Ok(sym.to_owned()),
        _ => Err(parse_error(format!("expected variable, got {form}"))),
    }
}

fn is_src_symbol(form: &Edn) -> bool {
    form.as_symbol().is_some_and(|s| s.starts_with('$'))
}

/// Parses a query from its EDN map form or vector form.
///
/// # Errors
/// Returns [`QueryError::Parse`] for malformed queries and
/// [`QueryError::Unbound`] when find/with variables never appear in `:where`
/// or `:in`.
pub fn parse_query(form: &Edn) -> Result<Query, QueryError> {
    let map = normalize_to_map(form)?;
    let find_form = map
        .get(&Edn::keyword("find"))
        .ok_or_else(|| parse_error("query requires :find"))?;
    let find_items = find_form
        .as_seq()
        .ok_or_else(|| parse_error(":find must be a vector"))?;
    let find = parse_find(find_items)?;

    let with = match map.get(&Edn::keyword("with")) {
        None => Vec::new(),
        Some(form) => form
            .as_seq()
            .ok_or_else(|| parse_error(":with must be a vector"))?
            .iter()
            .map(expect_var)
            .collect::<Result<_, _>>()?,
    };

    let inputs = match map.get(&Edn::keyword("in")) {
        None => vec![InSpec::Db(DEFAULT_SRC.to_owned())],
        Some(form) => form
            .as_seq()
            .ok_or_else(|| parse_error(":in must be a vector"))?
            .iter()
            .map(parse_in_spec)
            .collect::<Result<_, _>>()?,
    };

    let wheres = match map.get(&Edn::keyword("where")) {
        None => Vec::new(),
        Some(form) => parse_clauses(
            form.as_seq()
                .ok_or_else(|| parse_error(":where must be a vector"))?,
        )?,
    };

    let query = Query {
        find,
        with,
        inputs,
        wheres,
    };
    validate(&query)?;
    Ok(query)
}

/// Rewrites the vector form `[:find … :in … :where …]` into map shape.
fn normalize_to_map(form: &Edn) -> Result<Edn, QueryError> {
    match form {
        Edn::Map(_) => Ok(form.clone()),
        Edn::Vector(items) => {
            let mut pairs: Vec<(Edn, Edn)> = Vec::new();
            let mut current: Option<(Edn, Vec<Edn>)> = None;
            for item in items {
                if let Edn::Keyword(k) = item {
                    if k.namespace.is_none() {
                        if let Some((key, values)) = current.take() {
                            pairs.push((key, Edn::Vector(values)));
                        }
                        current = Some((Edn::Keyword(k.clone()), Vec::new()));
                        continue;
                    }
                }
                match &mut current {
                    Some((_, values)) => values.push(item.clone()),
                    None => return Err(parse_error("vector query must start with a keyword")),
                }
            }
            if let Some((key, values)) = current.take() {
                pairs.push((key, Edn::Vector(values)));
            }
            pairs.sort_by(|left, right| left.0.cmp(&right.0));
            Ok(Edn::Map(pairs))
        }
        _ => Err(parse_error("query must be a map or vector")),
    }
}

fn parse_find(items: &[Edn]) -> Result<FindSpec, QueryError> {
    if items.is_empty() {
        return Err(parse_error(":find requires at least one element"));
    }
    // Scalar: `?x .`
    if items.len() == 2 && items[1].as_symbol() == Some(".") {
        return Ok(FindSpec::Scalar(parse_find_elem(&items[0])?));
    }
    // Collection `[?x …]` or single tuple `[?x ?y]`.
    if items.len() == 1 {
        if let Edn::Vector(inner) = &items[0] {
            if inner.last().and_then(Edn::as_symbol) == Some("...") {
                if inner.len() != 2 {
                    return Err(parse_error("collection find takes one element"));
                }
                return Ok(FindSpec::Coll(parse_find_elem(&inner[0])?));
            }
            return Ok(FindSpec::Tuple(
                inner
                    .iter()
                    .map(parse_find_elem)
                    .collect::<Result<_, _>>()?,
            ));
        }
    }
    Ok(FindSpec::Rel(
        items
            .iter()
            .map(parse_find_elem)
            .collect::<Result<_, _>>()?,
    ))
}

fn parse_find_elem(form: &Edn) -> Result<FindElem, QueryError> {
    match form {
        Edn::Symbol(sym) if sym.starts_with('?') => Ok(FindElem::Var(sym.clone())),
        Edn::List(items) => {
            let (op, rest) = items
                .split_first()
                .ok_or_else(|| parse_error("empty find call"))?;
            let op = op
                .as_symbol()
                .ok_or_else(|| parse_error("find call must start with a symbol"))?;
            if op == "pull" {
                let [entity, pattern] = rest else {
                    return Err(parse_error("pull takes an entity variable and a pattern"));
                };
                return Ok(FindElem::Pull(expect_var(entity)?, pattern.clone()));
            }
            match rest {
                [var] => Ok(FindElem::Aggregate(Aggregate {
                    op: op.to_owned(),
                    n: None,
                    var: expect_var(var)?,
                })),
                [Edn::Long(n), var] => Ok(FindElem::Aggregate(Aggregate {
                    op: op.to_owned(),
                    n: Some(*n),
                    var: expect_var(var)?,
                })),
                _ => Err(parse_error(format!("malformed aggregate ({op} …)"))),
            }
        }
        _ => Err(parse_error(format!("bad find element {form}"))),
    }
}

fn parse_in_spec(form: &Edn) -> Result<InSpec, QueryError> {
    match form {
        Edn::Symbol(sym) if sym.starts_with('$') => Ok(InSpec::Db(sym.clone())),
        Edn::Symbol(sym) if sym == "%" => Ok(InSpec::Rules),
        Edn::Symbol(sym) if sym.starts_with('?') => Ok(InSpec::Scalar(sym.clone())),
        Edn::Vector(items) => {
            if items.last().and_then(Edn::as_symbol) == Some("...") {
                if items.len() != 2 {
                    return Err(parse_error("collection binding takes one variable"));
                }
                return Ok(InSpec::Coll(expect_var(&items[0])?));
            }
            if items.len() == 1 {
                if let Edn::Vector(inner) = &items[0] {
                    return Ok(InSpec::Rel(
                        inner.iter().map(expect_var).collect::<Result<_, _>>()?,
                    ));
                }
            }
            Ok(InSpec::Tuple(
                items.iter().map(expect_var).collect::<Result<_, _>>()?,
            ))
        }
        _ => Err(parse_error(format!("bad :in binding {form}"))),
    }
}

fn parse_clauses(forms: &[Edn]) -> Result<Vec<Clause>, QueryError> {
    forms.iter().map(parse_clause).collect()
}

/// Parses one `:where` clause.
///
/// # Errors
/// Returns [`QueryError::Parse`] for malformed clause forms.
pub fn parse_clause(form: &Edn) -> Result<Clause, QueryError> {
    match form {
        Edn::Vector(items) => parse_vector_clause(items),
        Edn::List(items) => parse_list_clause(items),
        _ => Err(parse_error(format!("bad clause {form}"))),
    }
}

fn parse_vector_clause(items: &[Edn]) -> Result<Clause, QueryError> {
    // `[(f …)]` predicate, `[(f …) binding]` function.
    if let Some(Edn::List(call)) = items.first() {
        let (name, args) = call
            .split_first()
            .ok_or_else(|| parse_error("empty call clause"))?;
        let name = name
            .as_symbol()
            .ok_or_else(|| parse_error("call must start with a symbol"))?
            .to_owned();
        let args = args.iter().map(Term::parse).collect();
        return match &items[1..] {
            [] => Ok(Clause::Pred { name, args }),
            [binding] => Ok(Clause::Fn {
                name,
                args,
                binding: parse_binding(binding)?,
            }),
            _ => Err(parse_error("function clause takes one binding form")),
        };
    }
    // Data pattern with optional leading src.
    let (src, rest) = match items.split_first() {
        Some((first, rest)) if is_src_symbol(first) => {
            (first.as_symbol().unwrap_or(DEFAULT_SRC).to_owned(), rest)
        }
        _ => (DEFAULT_SRC.to_owned(), items),
    };
    if rest.is_empty() || rest.len() > 5 {
        return Err(parse_error("data pattern takes one to five positions"));
    }
    let term = |i: usize| rest.get(i).map_or(Term::Blank, Term::parse);
    Ok(Clause::Pattern(Pattern {
        src,
        e: term(0),
        a: term(1),
        v: term(2),
        tx: term(3),
        added: term(4),
    }))
}

fn parse_list_clause(items: &[Edn]) -> Result<Clause, QueryError> {
    let (src, items) = match items.split_first() {
        Some((first, rest)) if is_src_symbol(first) => {
            (first.as_symbol().unwrap_or(DEFAULT_SRC).to_owned(), rest)
        }
        _ => (DEFAULT_SRC.to_owned(), items),
    };
    let (head, rest) = items
        .split_first()
        .ok_or_else(|| parse_error("empty list clause"))?;
    let head = head
        .as_symbol()
        .ok_or_else(|| parse_error("list clause must start with a symbol"))?;
    match head {
        "not" => Ok(Clause::Not {
            src,
            vars: None,
            clauses: parse_clauses(rest)?,
        }),
        "not-join" => {
            let (vars, clauses) = parse_join_head(rest, "not-join")?;
            Ok(Clause::Not {
                src,
                vars: Some(vars),
                clauses,
            })
        }
        "or" => Ok(Clause::Or {
            src,
            vars: None,
            branches: parse_branches(rest)?,
        }),
        "or-join" => {
            let (vars, branch_forms) = rest
                .split_first()
                .ok_or_else(|| parse_error("or-join requires a variable vector"))?;
            let vars = vars
                .as_seq()
                .ok_or_else(|| parse_error("or-join requires a variable vector"))?
                .iter()
                .map(expect_var)
                .collect::<Result<_, _>>()?;
            Ok(Clause::Or {
                src,
                vars: Some(vars),
                branches: parse_branches(branch_forms)?,
            })
        }
        "and" => Err(parse_error("(and …) is only valid inside or")),
        _ => Ok(Clause::RuleCall {
            name: head.to_owned(),
            args: rest.iter().map(Term::parse).collect(),
        }),
    }
}

fn parse_join_head(forms: &[Edn], what: &str) -> Result<(Vec<Var>, Vec<Clause>), QueryError> {
    let (vars, rest) = forms
        .split_first()
        .ok_or_else(|| parse_error(format!("{what} requires a variable vector")))?;
    let vars = vars
        .as_seq()
        .ok_or_else(|| parse_error(format!("{what} requires a variable vector")))?
        .iter()
        .map(expect_var)
        .collect::<Result<_, _>>()?;
    Ok((vars, parse_clauses(rest)?))
}

fn parse_branches(forms: &[Edn]) -> Result<Vec<Vec<Clause>>, QueryError> {
    forms
        .iter()
        .map(|form| {
            if let Edn::List(items) = form {
                if items.first().and_then(Edn::as_symbol) == Some("and") {
                    return parse_clauses(&items[1..]);
                }
            }
            Ok(vec![parse_clause(form)?])
        })
        .collect()
}

fn parse_binding(form: &Edn) -> Result<Binding, QueryError> {
    let target = |form: &Edn| -> Result<BindTarget, QueryError> {
        match form.as_symbol() {
            Some("_") => Ok(BindTarget::Blank),
            Some(sym) if sym.starts_with('?') => Ok(BindTarget::Var(sym.to_owned())),
            _ => Err(parse_error(format!("bad binding target {form}"))),
        }
    };
    match form {
        Edn::Symbol(sym) if sym.starts_with('?') => Ok(Binding::Scalar(sym.clone())),
        Edn::Vector(items) => {
            if items.last().and_then(Edn::as_symbol) == Some("...") {
                if items.len() != 2 {
                    return Err(parse_error("collection binding takes one target"));
                }
                return Ok(Binding::Coll(target(&items[0])?));
            }
            if items.len() == 1 {
                if let Edn::Vector(inner) = &items[0] {
                    return Ok(Binding::Rel(
                        inner.iter().map(target).collect::<Result<_, _>>()?,
                    ));
                }
            }
            Ok(Binding::Tuple(
                items.iter().map(target).collect::<Result<_, _>>()?,
            ))
        }
        _ => Err(parse_error(format!("bad binding form {form}"))),
    }
}

/// A function clause output binding.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Binding {
    /// `?x`.
    Scalar(Var),
    /// `[?x ?y]`.
    Tuple(Vec<BindTarget>),
    /// `[?x …]`.
    Coll(BindTarget),
    /// `[[?x ?y]]`.
    Rel(Vec<BindTarget>),
}

/// A binding target: a variable or the blank.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BindTarget {
    /// A variable.
    Var(Var),
    /// `_`.
    Blank,
}

/// Parses a rule set from its EDN form: `[[(name head…) clause…]…]`.
///
/// # Errors
/// Returns [`QueryError::Parse`] for malformed rule definitions.
pub fn parse_rules(form: &Edn) -> Result<Vec<RuleDef>, QueryError> {
    let defs = form
        .as_seq()
        .ok_or_else(|| parse_error("rule set must be a vector"))?;
    defs.iter()
        .map(|def| {
            let items = def
                .as_seq()
                .ok_or_else(|| parse_error("rule definition must be a vector"))?;
            let (head, body) = items
                .split_first()
                .ok_or_else(|| parse_error("rule definition requires a head"))?;
            let Edn::List(head_items) = head else {
                return Err(parse_error("rule head must be a list"));
            };
            let (name, head_args) = head_items
                .split_first()
                .ok_or_else(|| parse_error("rule head requires a name"))?;
            let name = name
                .as_symbol()
                .ok_or_else(|| parse_error("rule name must be a symbol"))?
                .to_owned();
            let (required, free) = match head_args.split_first() {
                Some((Edn::Vector(required), rest)) => (
                    required.iter().map(expect_var).collect::<Result<_, _>>()?,
                    rest.iter().map(expect_var).collect::<Result<Vec<_>, _>>()?,
                ),
                _ => (
                    Vec::new(),
                    head_args
                        .iter()
                        .map(expect_var)
                        .collect::<Result<Vec<_>, _>>()?,
                ),
            };
            Ok(RuleDef {
                name,
                required,
                free,
                clauses: parse_clauses(body)?,
            })
        })
        .collect()
}

/// Variables bound by a clause (produced, not merely consumed).
#[must_use]
pub fn clause_vars(clause: &Clause) -> BTreeSet<Var> {
    fn term_var(term: &Term, out: &mut BTreeSet<Var>) {
        if let Term::Var(v) = term {
            out.insert(v.clone());
        }
    }
    let mut out = BTreeSet::new();
    match clause {
        Clause::Pattern(p) => {
            for term in [&p.e, &p.a, &p.v, &p.tx, &p.added] {
                term_var(term, &mut out);
            }
        }
        Clause::Pred { args, .. } | Clause::RuleCall { args, .. } => {
            for term in args {
                term_var(term, &mut out);
            }
        }
        Clause::Fn { args, binding, .. } => {
            for term in args {
                term_var(term, &mut out);
            }
            let mut push = |target: &BindTarget| {
                if let BindTarget::Var(v) = target {
                    out.insert(v.clone());
                }
            };
            match binding {
                Binding::Scalar(v) => {
                    out.insert(v.clone());
                }
                Binding::Coll(t) => push(t),
                Binding::Tuple(ts) | Binding::Rel(ts) => ts.iter().for_each(&mut push),
            }
        }
        Clause::Not { vars, clauses, .. } => match vars {
            Some(vars) => out.extend(vars.iter().cloned()),
            None => {
                for clause in clauses {
                    out.extend(clause_vars(clause));
                }
            }
        },
        Clause::Or { vars, branches, .. } => match vars {
            Some(vars) => out.extend(vars.iter().cloned()),
            None => {
                for branch in branches {
                    for clause in branch {
                        out.extend(clause_vars(clause));
                    }
                }
            }
        },
    }
    out
}

fn validate(query: &Query) -> Result<(), QueryError> {
    let mut available: BTreeSet<Var> = BTreeSet::new();
    for spec in &query.inputs {
        match spec {
            InSpec::Scalar(v) | InSpec::Coll(v) => {
                available.insert(v.clone());
            }
            InSpec::Tuple(vs) | InSpec::Rel(vs) => available.extend(vs.iter().cloned()),
            InSpec::Db(_) | InSpec::Rules => {}
        }
    }
    for clause in &query.wheres {
        available.extend(clause_vars(clause));
    }
    for elem in query.find.elems() {
        if !available.contains(elem.var()) {
            return Err(QueryError::Unbound(elem.var().clone()));
        }
    }
    for var in &query.with {
        if !available.contains(var) {
            return Err(QueryError::Unbound(var.clone()));
        }
    }
    // `or` branches must agree on the variables they bind.
    for clause in &query.wheres {
        validate_or(clause)?;
    }
    Ok(())
}

fn validate_or(clause: &Clause) -> Result<(), QueryError> {
    match clause {
        Clause::Or { vars, branches, .. } => {
            if vars.is_none() {
                let mut expected: Option<BTreeSet<Var>> = None;
                for branch in branches {
                    let mut bound = BTreeSet::new();
                    for clause in branch {
                        bound.extend(clause_vars(clause));
                    }
                    match &expected {
                        None => expected = Some(bound),
                        Some(prev) if *prev == bound => {}
                        Some(_) => {
                            return Err(QueryError::Parse(
                                "or branches must bind the same variables".into(),
                            ));
                        }
                    }
                }
            }
            for branch in branches {
                for clause in branch {
                    validate_or(clause)?;
                }
            }
            Ok(())
        }
        Clause::Not { clauses, .. } => {
            for clause in clauses {
                validate_or(clause)?;
            }
            Ok(())
        }
        _ => Ok(()),
    }
}