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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Defines [`QuantifierConstraint`] and [`QuantifierConstraintValidator`].

use super::{ValidationError, ValidationResult, Validator};
use crate::query_model::model::*;
use std::ops::{Bound, RangeBounds};

/// A model for constraints imposed on the input or ranging quantifiers of a query box.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct QuantifierConstraint {
    /// Lower bound for the number of allowed quantifiers that match the `allowed_types` bitmask.
    min: Bound<usize>,
    /// Upper bound for the number of allowed quantifiers that match the `allowed_types` bitmask.
    max: Bound<usize>,
    /// The allowed [`QuantifierType`] variant discriminants.
    allowed_types: QuantifierType,
    /// Indicates whether the input of a quantifier with an `allowed_type` must be a select box.
    select_input: bool,
    /// Indicates whether the parent of a quantifier with an `allowed_type` must be a select box.
    select_parent: bool,
}

impl QuantifierConstraint {
    /// Check if the given [`Quantifier`] satisfy the `allowed_types` of the this constraint.
    fn satisfies_q_type<'a>(&self, quantifier: &'a Quantifier) -> bool {
        self.allowed_types.intersects(quantifier.quantifier_type)
    }
    /// Check if the boxes referenced by the given [`Quantifier`] satisfy the `input_is_select`
    /// and `parent_is_select` of the this constraint.
    fn satisfies_b_type<'a>(&self, quantifier: &'a Quantifier, model: &'a Model) -> bool {
        let input_select_satisfied =
            !self.select_input || model.get_box(quantifier.input_box).is_select();
        let parent_select_satisfied =
            !self.select_parent || model.get_box(quantifier.parent_box).is_select();
        input_select_satisfied && parent_select_satisfied
    }
}

impl RangeBounds<usize> for QuantifierConstraint {
    fn start_bound(&self) -> Bound<&usize> {
        match self.min {
            Bound::Included(ref value) => Bound::Included(value),
            Bound::Excluded(ref value) => Bound::Excluded(value),
            Bound::Unbounded => Bound::Unbounded,
        }
    }

    fn end_bound(&self) -> Bound<&usize> {
        match self.max {
            Bound::Included(ref value) => Bound::Included(value),
            Bound::Excluded(ref value) => Bound::Excluded(value),
            Bound::Unbounded => Bound::Unbounded,
        }
    }
}

/// Check a const-sized array of `constraints` against a sequence of `quantifiers`,
/// and call `on_failure` for constraints that are not satisfied by the sequence.
///
/// A `constraint[i]` is violated iff one of the following conditions are met:
/// - The number of `quantifiers` with satisfied type is within the given range.
/// - All `quantifiers` with satisfied type also satisfy the quantifier box constraints.
fn check_constraints<'a, const N: usize>(
    constraints: &[QuantifierConstraint; N],
    quantifiers: impl Iterator<Item = BoundRef<'a, Quantifier>>,
    model: &'a Model,
    mut on_failure: impl FnMut(&QuantifierConstraint) -> (),
) {
    // count quantifiers with satisfied type and associated box types
    let mut q_type_counts = [0; N];
    let mut b_type_counts = [0; N];
    for q in quantifiers {
        for i in 0..N {
            if constraints[i].satisfies_q_type(&q) {
                q_type_counts[i] += 1;
                b_type_counts[i] += usize::from(constraints[i].satisfies_b_type(&q, model));
            }
        }
    }
    // call on_failure for violated constraints
    for i in 0..N {
        let q_type_count_ok = constraints[i].contains(&q_type_counts[i]);
        let b_type_count_ok = q_type_counts[i] == b_type_counts[i];
        if !(q_type_count_ok && b_type_count_ok) {
            on_failure(&constraints[i])
        }
    }
}

/// [`QuantifierConstraint`] constants used by the [`QuantifierConstraintValidator`].
mod constants {
    use super::*;

    pub(crate) const ZERO_ARBITRARY: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(0),
        allowed_types: QuantifierType::all(),
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ZERO_NOT_SUBQUERY: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(0),
        allowed_types: QuantifierType::all().difference(QuantifierType::SUBQUERY),
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ZERO_NOT_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(0),
        allowed_types: QuantifierType::all().difference(QuantifierType::FOREACH),
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ZERO_PRESERVED_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(0),
        allowed_types: QuantifierType::PRESERVED_FOREACH,
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ONE_OR_MORE_NOT_PRES_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(1),
        max: Bound::Unbounded,
        allowed_types: QuantifierType::all().difference(QuantifierType::PRESERVED_FOREACH),
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ONE_OR_MORE_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(1),
        max: Bound::Unbounded,
        allowed_types: QuantifierType::FOREACH,
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ONE_FOREACH_INPUT_SELECT: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(1),
        max: Bound::Included(1),
        allowed_types: QuantifierType::FOREACH,
        select_input: true,
        select_parent: false,
    };

    pub(crate) const ONE_FOREACH_PARENT_SELECT: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(1),
        max: Bound::Included(1),
        allowed_types: QuantifierType::FOREACH,
        select_input: false,
        select_parent: true,
    };

    pub(crate) const ONE_OR_TWO_PRES_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(1),
        max: Bound::Included(2),
        allowed_types: QuantifierType::PRESERVED_FOREACH,
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ZERO_OR_ONE_FOREACH: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(1),
        allowed_types: QuantifierType::FOREACH,
        select_input: false,
        select_parent: false,
    };

    pub(crate) const TWO_OUTER_JOIN_INPUTS: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(2),
        max: Bound::Included(2),
        allowed_types: QuantifierType::PRESERVED_FOREACH.union(QuantifierType::FOREACH),
        select_input: false,
        select_parent: false,
    };

    pub(crate) const ZERO_NOT_OUTER_JOIN_INPUTS: QuantifierConstraint = QuantifierConstraint {
        min: Bound::Included(0),
        max: Bound::Included(0),
        allowed_types: TWO_OUTER_JOIN_INPUTS.allowed_types.complement(),
        select_input: false,
        select_parent: false,
    };
}

/// A [`Validator`] that checks the following quantifier constraints:
/// - `Get` and `Values` boxes cannot have input
///    quantifiers.
/// - `TableFunction` boxes can only have subquery input quantifiers.
/// - `Union`, `Except` and `Intersect` boxes can only have input
///    quantifiers of type `Foreach`.
/// - A `Grouping` box must have a single input quantifier of type
///   `Foreach` ranging over the contents of a `Select` box.
/// - A `Grouping` box must have a single ranging quantifier of type
///   `Foreach` that has a `Select` box as its parent.
/// - A `Select` box must have one or more input quantifiers of
///   arbitrary type (except `PreservedForeach`).
/// - An `OuterJoin` box must have one or two input quantifiers of
///   type `PreservedForeach`.
/// - An `OuterJoin` box must have at most one input quantifier of
///   type `Foreach`.
/// - An `OuterJoin` box must have exactly two input quantifiers of
///   type `Foreach` or `PreservedForeach`.
#[derive(Default)]
pub(crate) struct QuantifierConstraintValidator;

impl Validator for QuantifierConstraintValidator {
    fn validate(&self, model: &Model) -> ValidationResult {
        let mut errors = vec![];

        for b in model.boxes_iter() {
            use constants::*;
            use BoxType::*;
            match b.box_type {
                Get(..) | Values(..) => {
                    let constraints = [ZERO_ARBITRARY];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    })
                }
                CallTable(..) => {
                    let constraints = [ZERO_NOT_SUBQUERY];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    })
                }
                Union | Except | Intersect => {
                    let constraints = [ONE_OR_MORE_FOREACH, ZERO_NOT_FOREACH];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    })
                }
                Grouping(..) => {
                    let constraints = [ONE_FOREACH_INPUT_SELECT, ZERO_NOT_FOREACH];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    });
                    let constraints = [ONE_FOREACH_PARENT_SELECT, ZERO_NOT_FOREACH];
                    check_constraints(&constraints, b.ranging_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidRangingQuantifiers(b.id, c.clone()))
                    });
                }
                Select(..) => {
                    let constraints = [ONE_OR_MORE_NOT_PRES_FOREACH, ZERO_PRESERVED_FOREACH];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    });
                }
                OuterJoin(..) => {
                    let constraints = [
                        ONE_OR_TWO_PRES_FOREACH,
                        ZERO_OR_ONE_FOREACH,
                        TWO_OUTER_JOIN_INPUTS,
                        ZERO_NOT_OUTER_JOIN_INPUTS,
                    ];
                    check_constraints(&constraints, b.input_quantifiers(), model, |c| {
                        errors.push(ValidationError::InvalidInputQuantifiers(b.id, c.clone()))
                    });
                }
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

#[cfg(test)]
mod tests {
    use mz_expr::TableFunc;

    use super::constants::*;
    use super::*;
    use crate::query_model::test::util::*;
    use std::collections::HashSet;

    /// Tests constraints for quantifiers incident with [`BoxType::Get`]
    /// and [`BoxType::Values`] boxes (happy case).
    #[test]
    fn test_invalid_get_values_ok() {
        let mut model = Model::default();

        let box_get = model.make_box(qgm::get(1).into());
        let box_values = model.make_box(Values::default().into());
        let box_tgt = model.make_box(Select::default().into());

        model.make_quantifier(QuantifierType::FOREACH, box_get, box_tgt);
        model.make_quantifier(QuantifierType::FOREACH, box_values, box_tgt);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Get`]
    /// and [`BoxType::Values`] boxes (bad case).
    #[test]
    fn test_invalid_get_values_not_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_get = model.make_box(qgm::get(1).into());
        let box_values = model.make_box(Values::default().into());

        model.make_quantifier(QuantifierType::FOREACH, box_src, box_get);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_values);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([
            ValidationError::InvalidInputQuantifiers(box_get, ZERO_ARBITRARY),
            ValidationError::InvalidInputQuantifiers(box_values, ZERO_ARBITRARY),
        ]);
        assert_eq!(errors_act, errors_exp);
    }

    /// Tests constraints for quantifiers incident with [`BoxType::CallTable`]
    /// boxes (happy case).
    #[test]
    fn test_invalid_call_table_ok() {
        use TableFunc::GenerateSeriesInt32;

        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let call_table = CallTable::new(GenerateSeriesInt32, vec![]);
        let box_table_fn = model.make_box(call_table.into());
        let box_tgt = model.make_box(Select::default().into());

        model.make_quantifier(QuantifierType::SCALAR, box_src, box_table_fn);
        model.make_quantifier(QuantifierType::FOREACH, box_table_fn, box_tgt);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::CallTable`]
    /// boxes (bad case).
    #[test]
    fn test_invalid_call_table_not_ok() {
        use TableFunc::GenerateSeriesInt32;

        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let call_table = CallTable::new(GenerateSeriesInt32, vec![]);
        let box_table_fn = model.make_box(call_table.into());

        model.make_quantifier(QuantifierType::SCALAR, box_src, box_table_fn);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_table_fn);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([ValidationError::InvalidInputQuantifiers(
            box_table_fn,
            ZERO_NOT_SUBQUERY,
        )]);
        assert_eq!(errors_act, errors_exp);
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Grouping`],
    /// [`BoxType::Except`], and [`BoxType::Intersect`] boxes (happy case).
    #[test]
    fn test_invalid_union_except_intersect_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_union = model.make_box(BoxType::Union);
        let box_except = model.make_box(BoxType::Except);
        let box_intersect = model.make_box(BoxType::Intersect);

        model.make_quantifier(QuantifierType::FOREACH, box_src, box_union);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_union);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_except);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_except);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_intersect);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_intersect);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Grouping`],
    /// [`BoxType::Except`], and [`BoxType::Intersect`] boxes (bad case).
    #[test]
    fn test_invalid_union_except_intersect_not_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_union = model.make_box(BoxType::Union);
        let box_except = model.make_box(BoxType::Except);
        let box_intersect = model.make_box(BoxType::Intersect);

        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_src, box_union);
        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_src, box_except);
        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_src, box_intersect);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([
            ValidationError::InvalidInputQuantifiers(box_union, ONE_OR_MORE_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_union, ZERO_NOT_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_except, ONE_OR_MORE_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_except, ZERO_NOT_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_intersect, ONE_OR_MORE_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_intersect, ZERO_NOT_FOREACH),
        ]);
        assert_eq!(errors_act, errors_exp);
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Grouping`] boxes (happy case).
    #[test]
    fn test_invalid_grouping_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_select = model.make_box(Select::default().into());
        let box_grouping = model.make_box(Grouping::default().into());
        let box_dst = model.make_box(Select::default().into());

        model.make_quantifier(QuantifierType::FOREACH, box_src, box_select);
        model.make_quantifier(QuantifierType::FOREACH, box_select, box_grouping);
        model.make_quantifier(QuantifierType::FOREACH, box_grouping, box_dst);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Grouping`] boxes (bad case).
    #[test]
    fn test_invalid_grouping_not_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_grouping = model.make_box(Grouping::default().into());
        let box_dst = model.make_box(BoxType::Union);

        model.make_quantifier(QuantifierType::EXISTENTIAL, box_src, box_grouping);
        model.make_quantifier(QuantifierType::FOREACH, box_src, box_grouping);
        model.make_quantifier(QuantifierType::FOREACH, box_grouping, box_dst);
        model.make_quantifier(QuantifierType::FOREACH, box_grouping, box_dst);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([
            ValidationError::InvalidInputQuantifiers(box_grouping, ZERO_NOT_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_grouping, ONE_FOREACH_INPUT_SELECT),
            ValidationError::InvalidRangingQuantifiers(box_grouping, ONE_FOREACH_PARENT_SELECT),
        ]);
        assert_eq!(errors_act, errors_exp);
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Select`] boxes (happy case).
    #[test]
    fn test_invalid_select_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_select = model.make_box(Select::default().into());

        model.make_quantifier(QuantifierType::FOREACH, box_src, box_select);
        model.make_quantifier(QuantifierType::EXISTENTIAL, box_src, box_select);
        model.make_quantifier(QuantifierType::SCALAR, box_src, box_select);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::Select`] boxes (bad case).
    #[test]
    fn test_invalid_select_not_ok() {
        let mut model = Model::default();

        let box_src = model.make_box(qgm::get(0).into());
        let box_select = model.make_box(Select::default().into());

        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_src, box_select);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([
            ValidationError::InvalidInputQuantifiers(box_select, ONE_OR_MORE_NOT_PRES_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_select, ZERO_PRESERVED_FOREACH),
        ]);
        assert_eq!(errors_act, errors_exp);
    }

    /// Tests constraints for quantifiers incident with [`BoxType::OuterJoin`] boxes (happy case).
    #[test]
    fn test_outer_join_ok() {
        let mut model = Model::default();

        let box_lhs = model.make_box(qgm::get(0).into());
        let box_rhs = model.make_box(qgm::get(1).into());
        let box_outer_join_1 = model.make_box(OuterJoin::default().into());
        let box_outer_join_2 = model.make_box(OuterJoin::default().into());

        model.make_quantifier(QuantifierType::FOREACH, box_lhs, box_outer_join_1);
        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_rhs, box_outer_join_1);
        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_lhs, box_outer_join_2);
        model.make_quantifier(QuantifierType::PRESERVED_FOREACH, box_rhs, box_outer_join_2);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_ok());
    }

    /// Tests constraints for quantifiers incident with [`BoxType::OuterJoin`] boxes (bad case).
    #[test]
    fn test_outer_join_not_ok() {
        let mut model = Model::default();

        let box_lhs = model.make_box(qgm::get(0).into());
        let box_rhs = model.make_box(qgm::get(1).into());
        let box_outer_join_1 = model.make_box(OuterJoin::default().into());
        let box_outer_join_2 = model.make_box(OuterJoin::default().into());

        model.make_quantifier(QuantifierType::EXISTENTIAL, box_lhs, box_outer_join_1);
        model.make_quantifier(QuantifierType::SCALAR, box_rhs, box_outer_join_1);
        model.make_quantifier(QuantifierType::FOREACH, box_lhs, box_outer_join_2);
        model.make_quantifier(QuantifierType::FOREACH, box_rhs, box_outer_join_2);

        let result = QuantifierConstraintValidator::default().validate(&model);
        assert!(result.is_err());

        let errors_act: HashSet<_> = result.unwrap_err().drain(..).collect();
        let errors_exp = HashSet::from([
            ValidationError::InvalidInputQuantifiers(box_outer_join_1, ONE_OR_TWO_PRES_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_outer_join_1, TWO_OUTER_JOIN_INPUTS),
            ValidationError::InvalidInputQuantifiers(box_outer_join_1, ZERO_NOT_OUTER_JOIN_INPUTS),
            ValidationError::InvalidInputQuantifiers(box_outer_join_2, ONE_OR_TWO_PRES_FOREACH),
            ValidationError::InvalidInputQuantifiers(box_outer_join_2, ZERO_OR_ONE_FOREACH),
        ]);
        assert_eq!(errors_act, errors_exp);
    }
}