Skip to main content

uqa_execution/
projected_predicate.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Scalar predicates compiled against positional storage projections.
8
9use uqa_core::Value;
10use uqa_sql::ast::BinaryOp;
11use uqa_sql::expr::IntegerWidth;
12use uqa_sql::{SQLError, SQLParam};
13
14use crate::{RowSchema, ScalarExpr};
15
16mod compile;
17mod evaluate;
18#[cfg(test)]
19mod production_tests;
20
21/// A predicate whose column lookups are resolved once to projection slots.
22/// Unsupported expressions return `None` and continue through the canonical
23/// map-backed evaluator.
24pub struct ProjectedPredicate {
25    expression: ProjectedExpr,
26    _memory: Option<uqa_core::memory::MemoryReservation>,
27}
28
29#[derive(Clone, Copy)]
30pub(super) enum ProjectedIntPredicate {
31    Comparison {
32        field: usize,
33        op: BinaryOp,
34        literal: i64,
35        field_on_left: bool,
36    },
37    Between {
38        field: usize,
39        low: i64,
40        high: i64,
41    },
42}
43
44pub(super) enum ProjectedExpr {
45    Field(usize),
46    Literal(Value),
47    Binary {
48        op: BinaryOp,
49        lhs: Box<Self>,
50        rhs: Box<Self>,
51        integer_width: Option<IntegerWidth>,
52        real_arithmetic: bool,
53    },
54    UnaryMinus(Box<Self>),
55    IntFieldComparison {
56        field: usize,
57        op: BinaryOp,
58        literal: i64,
59        field_on_left: bool,
60    },
61    Not(Box<Self>),
62    And(Vec<Self>),
63    IntFieldConjunction(Vec<ProjectedIntPredicate>),
64    Or(Vec<Self>),
65    IsNull {
66        expression: Box<Self>,
67        negated: bool,
68    },
69    Between {
70        expression: Box<Self>,
71        low: Box<Self>,
72        high: Box<Self>,
73    },
74    IntFieldBetween {
75        field: usize,
76        low: i64,
77        high: i64,
78    },
79    InList {
80        expression: Box<Self>,
81        list: Vec<Self>,
82        negated: bool,
83    },
84    Like {
85        expression: Box<Self>,
86        pattern: uqa_sql::expr::CompiledLikePattern,
87    },
88    Cast {
89        expression: Box<Self>,
90        ty: String,
91    },
92}
93
94impl ProjectedPredicate {
95    pub fn compile(
96        expression: &ScalarExpr,
97        fields: &[String],
98        params: &[SQLParam],
99    ) -> Result<Option<Self>, SQLError> {
100        Self::compile_with_schema(expression, &RowSchema::new(fields.to_vec()), params)
101    }
102
103    /// Compile against structured SQL identities instead of interpreting punctuation in public labels as qualification metadata.
104    pub fn compile_with_schema(
105        expression: &ScalarExpr,
106        schema: &RowSchema,
107        params: &[SQLParam],
108    ) -> Result<Option<Self>, SQLError> {
109        Self::compile_with_schema_with_control(
110            expression,
111            schema,
112            params,
113            &uqa_core::memory::ProductionControl::uncontrolled(),
114        )
115    }
116
117    pub fn compile_with_schema_with_control(
118        expression: &ScalarExpr,
119        schema: &RowSchema,
120        params: &[SQLParam],
121        control: &uqa_core::memory::ProductionControl<'_>,
122    ) -> Result<Option<Self>, SQLError> {
123        let expression = uqa_sql::bind_type_introspection_owned(
124            expression.copy_for_rewrite(control)?,
125            schema,
126            params,
127            None,
128            control,
129        )?;
130        Self::compile_bound_with_control(&expression, schema, params, control)
131    }
132
133    #[cfg(test)]
134    pub(crate) fn compile_bound(
135        expression: &ScalarExpr,
136        schema: &RowSchema,
137        params: &[SQLParam],
138    ) -> Result<Option<Self>, SQLError> {
139        Self::compile_bound_with_control(
140            expression,
141            schema,
142            params,
143            &uqa_core::memory::ProductionControl::uncontrolled(),
144        )
145    }
146
147    pub(crate) fn compile_bound_with_control(
148        expression: &ScalarExpr,
149        schema: &RowSchema,
150        params: &[SQLParam],
151        control: &uqa_core::memory::ProductionControl<'_>,
152    ) -> Result<Option<Self>, SQLError> {
153        control.check()?;
154        let mut local = true;
155        expression.try_visit(&mut |node| {
156            control.check()?;
157            local &= match node {
158                ScalarExpr::Column(name) => schema.unqualified_position(name).is_some(),
159                ScalarExpr::QualifiedColumn { qualifier, column } => {
160                    schema.qualified_position(qualifier, column).is_some()
161                }
162                ScalarExpr::InternalColumn(_)
163                | ScalarExpr::ScalarSubquery(_)
164                | ScalarExpr::Exists { .. }
165                | ScalarExpr::InSubquery { .. } => false,
166                _ => true,
167            };
168            Ok::<_, SQLError>(true)
169        })?;
170        // A projected predicate only owns local positional inputs. Expressions needing another row or a subquery belong to the caller's scoped evaluator, including their type binding.
171        if !local {
172            return Ok(None);
173        }
174        match compile::compile(expression, schema, params, control) {
175            Ok(expression) => Ok(expression.map(|expression| {
176                let (expression, memory) = expression.into_parts();
177                Self {
178                    expression,
179                    _memory: memory,
180                }
181            })),
182            Err(SQLError::Unsupported(_)) => Ok(None),
183            Err(error) => Err(error),
184        }
185    }
186
187    #[inline]
188    pub fn keep(&self, values: &[&Value]) -> Result<bool, SQLError> {
189        evaluate::keep(&self.expression, values)
190    }
191
192    /// Evaluate against backend-owned values through a positional projection without constructing an intermediate reference array. A `usize::MAX` projection slot reads as SQL NULL.
193    #[inline]
194    pub fn keep_indexed(&self, values: &[Value], projection: &[usize]) -> Result<bool, SQLError> {
195        evaluate::keep_indexed(&self.expression, values, projection)
196    }
197
198    /// Evaluate directly against a composite physical row. Column names and
199    /// qualifiers were resolved to logical positions during compilation, so
200    /// the hot path neither builds a named row nor allocates a reference list.
201    #[inline]
202    pub fn keep_row(&self, row: &crate::PhysicalRowView<'_>) -> Result<bool, SQLError> {
203        evaluate::keep_row(&self.expression, row)
204    }
205}
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210    use crate::RowSchemaExecution;
211    use crate::{eval_scalar, ScalarEvalContext};
212    use uqa_sql::ast::ColumnType;
213    use uqa_sql::expr::truthy;
214
215    #[test]
216    fn predicates_with_outer_attributes_keep_the_scoped_evaluator() {
217        let column = uqa_sql::ast::InternalRelationId::allocate().column(0);
218        for input in [
219            ScalarExpr::InternalColumn(column),
220            ScalarExpr::qualified_column("outer", "n"),
221            ScalarExpr::ScalarSubquery(0),
222        ] {
223            let expression = ScalarExpr::Binary {
224                op: BinaryOp::Equal,
225                lhs: Box::new(input),
226                rhs: Box::new(ScalarExpr::Literal(Value::Int(10))),
227            };
228            assert!(ProjectedPredicate::compile_with_schema(
229                &expression,
230                &RowSchema::default(),
231                &[]
232            )
233            .unwrap()
234            .is_none());
235        }
236    }
237
238    #[test]
239    fn positional_predicate_preserves_null_and_short_circuit_semantics() {
240        let expression = ScalarExpr::And(vec![
241            ScalarExpr::Between {
242                expr: Box::new(ScalarExpr::Column("x".into())),
243                low: Box::new(ScalarExpr::Literal(Value::Int(2))),
244                high: Box::new(ScalarExpr::Literal(Value::Int(4))),
245            },
246            ScalarExpr::IsNull {
247                expr: Box::new(ScalarExpr::Column("y".into())),
248                negated: false,
249            },
250        ]);
251        let predicate = ProjectedPredicate::compile(&expression, &["x".into(), "y".into()], &[])
252            .unwrap()
253            .unwrap();
254        assert!(predicate.keep(&[&Value::Int(3), &Value::Null]).unwrap());
255        assert!(!predicate.keep(&[&Value::Int(5), &Value::Null]).unwrap());
256        assert!(!predicate.keep(&[&Value::Null, &Value::Null]).unwrap());
257
258        let stored = [Value::Null, Value::Int(3)];
259        assert!(predicate.keep_indexed(&stored, &[1, 0]).unwrap());
260        assert!(!predicate.keep_indexed(&stored, &[usize::MAX, 0]).unwrap());
261    }
262
263    #[test]
264    fn projected_q1_and_q6_predicates_match_the_canonical_evaluator() {
265        let fields = vec!["discount".into(), "quantity".into(), "ship_day".into()];
266        let q6 = ScalarExpr::And(vec![
267            ScalarExpr::Between {
268                expr: Box::new(ScalarExpr::Column("ship_day".into())),
269                low: Box::new(ScalarExpr::Param(1)),
270                high: Box::new(ScalarExpr::Literal(Value::Int(2_190))),
271            },
272            ScalarExpr::Between {
273                expr: Box::new(ScalarExpr::Column("discount".into())),
274                low: Box::new(ScalarExpr::Literal(Value::Int(2))),
275                high: Box::new(ScalarExpr::Literal(Value::Int(8))),
276            },
277            ScalarExpr::Binary {
278                op: BinaryOp::Greater,
279                lhs: Box::new(ScalarExpr::Literal(Value::Int(40))),
280                rhs: Box::new(ScalarExpr::Column("quantity".into())),
281            },
282        ]);
283        let params = vec![SQLParam::typed_scalar(
284            Value::Int(365),
285            ColumnType::SmallInteger,
286        )];
287        let predicate = ProjectedPredicate::compile(&q6, &fields, &params)
288            .unwrap()
289            .unwrap();
290        assert!(matches!(
291            &predicate.expression,
292            ProjectedExpr::IntFieldConjunction(items) if items.len() == 3
293        ));
294        let discounts = [Value::Null, Value::Int(1), Value::Int(2), Value::Int(8)];
295        let quantities = [
296            Value::Null,
297            Value::Int(39),
298            Value::Int(40),
299            Value::Float(39.5),
300        ];
301        let ship_days = [
302            Value::Null,
303            Value::Int(364),
304            Value::Int(365),
305            Value::Int(2_190),
306            Value::Int(2_191),
307        ];
308        for discount in &discounts {
309            for quantity in &quantities {
310                for ship_day in &ship_days {
311                    assert_projected_parity(
312                        &q6,
313                        &predicate,
314                        &fields,
315                        &[discount.clone(), quantity.clone(), ship_day.clone()],
316                        &params,
317                    );
318                }
319            }
320        }
321
322        let q1 = ScalarExpr::Binary {
323            op: BinaryOp::LessEqual,
324            lhs: Box::new(ScalarExpr::Column("ship_day".into())),
325            rhs: Box::new(ScalarExpr::Literal(Value::Int(2_449))),
326        };
327        let predicate = ProjectedPredicate::compile(&q1, &fields, &[])
328            .unwrap()
329            .unwrap();
330        for ship_day in [Value::Null, Value::Int(2_449), Value::Int(2_450)] {
331            assert_projected_parity(
332                &q1,
333                &predicate,
334                &fields,
335                &[Value::Int(0), Value::Int(0), ship_day],
336                &[],
337            );
338        }
339    }
340
341    #[test]
342    fn projected_integer_comparisons_match_every_canonical_operator_and_operand_order() {
343        let fields = vec!["value".into()];
344        for op in [
345            BinaryOp::Equal,
346            BinaryOp::NotEqual,
347            BinaryOp::Less,
348            BinaryOp::LessEqual,
349            BinaryOp::Greater,
350            BinaryOp::GreaterEqual,
351        ] {
352            for field_on_left in [true, false] {
353                let field = ScalarExpr::Column("value".into());
354                let literal = ScalarExpr::Literal(Value::Int(7));
355                let expression = ScalarExpr::Binary {
356                    op,
357                    lhs: Box::new(if field_on_left {
358                        field.clone()
359                    } else {
360                        literal.clone()
361                    }),
362                    rhs: Box::new(if field_on_left {
363                        literal.clone()
364                    } else {
365                        field.clone()
366                    }),
367                };
368                let predicate = ProjectedPredicate::compile(&expression, &fields, &[])
369                    .unwrap()
370                    .unwrap();
371                for value in [
372                    Value::Null,
373                    Value::Int(6),
374                    Value::Int(7),
375                    Value::Int(8),
376                    Value::Float(7.0),
377                ] {
378                    assert_projected_parity(&expression, &predicate, &fields, &[value], &[]);
379                }
380            }
381        }
382    }
383
384    #[test]
385    fn projected_like_predicates_match_the_canonical_evaluator() {
386        for (operator, pattern) in [
387            ("LIKE", "%"),
388            ("LIKE", "%green%"),
389            ("LIKE", "%special%requests%"),
390            ("LIKE", "a_c"),
391            ("ILIKE", "%GREEN%"),
392        ] {
393            let expression = parsed_expression(&format!("text {operator} '{pattern}'"));
394            let fields = vec!["text".into()];
395            let predicate = ProjectedPredicate::compile(&expression, &fields, &[])
396                .unwrap()
397                .unwrap();
398            for value in [
399                Value::Str("forest green part".into()),
400                Value::FixedChar("GREEN   ".into()),
401                Value::Str("a-c".into()),
402                Value::Str("special pending requests".into()),
403                Value::Null,
404            ] {
405                assert_projected_parity(&expression, &predicate, &fields, &[value], &[]);
406            }
407        }
408    }
409
410    #[test]
411    fn qualified_like_runs_directly_on_a_composite_physical_row() {
412        let expression = parsed_expression("o.comment NOT LIKE '%special%requests%'");
413        let left_schema =
414            crate::RowSchema::with_qualified_types("c", vec!["id".into()], vec![None]);
415        let right_schema =
416            crate::RowSchema::with_qualified_types("o", vec!["comment".into()], vec![None]);
417        let schema = crate::RowSchema::join(&left_schema, &right_schema, std::iter::empty());
418        let predicate = ProjectedPredicate::compile_with_schema(&expression, &schema, &[])
419            .unwrap()
420            .unwrap();
421
422        let accepted = crate::PhysicalRow::concat(
423            &crate::PhysicalRow::from_values(vec![Value::Int(1)]),
424            &crate::PhysicalRow::from_values(vec![Value::Str("ordinary order".into())]),
425        );
426        let rejected = crate::PhysicalRow::concat(
427            &crate::PhysicalRow::from_values(vec![Value::Int(1)]),
428            &crate::PhysicalRow::from_values(vec![Value::Str("special pending requests".into())]),
429        );
430        assert!(predicate.keep_row(&schema.view(&accepted)).unwrap());
431        assert!(!predicate.keep_row(&schema.view(&rejected)).unwrap());
432    }
433
434    fn parsed_expression(source: &str) -> ScalarExpr {
435        let uqa_sql::Statement::Select(mut query) = uqa_sql::compile(&format!("SELECT {source}"))
436            .unwrap()
437            .remove(0)
438        else {
439            panic!("scalar SELECT");
440        };
441        uqa_sql::plan::ExpressionPlan::lower(query.projections.remove(0).expr).scalar
442    }
443
444    #[test]
445    fn session_dependent_datetime_predicates_keep_the_invoking_evaluator() {
446        let schema = RowSchema::with_types(
447            ["d", "t", "z"].map(str::to_owned).to_vec(),
448            [
449                ColumnType::Date,
450                ColumnType::Timestamp,
451                ColumnType::TimestampTz,
452            ]
453            .map(Some)
454            .to_vec(),
455        );
456        for sql in [
457            "d = z",
458            "z < d",
459            "d BETWEEN z AND z",
460            "d IN (z)",
461            "d = ANY(ARRAY[TIMESTAMPTZ '2020-01-01+00'])",
462            "d <> ALL(ARRAY[TIMESTAMPTZ '2020-01-01+00'])",
463        ] {
464            assert!(
465                ProjectedPredicate::compile_with_schema(&parsed_expression(sql), &schema, &[])
466                    .unwrap()
467                    .is_none(),
468                "{sql}"
469            );
470        }
471        for sql in ["d = d", "d = t", "t = d"] {
472            assert!(
473                ProjectedPredicate::compile_with_schema(&parsed_expression(sql), &schema, &[])
474                    .unwrap()
475                    .is_some(),
476                "{sql}"
477            );
478        }
479    }
480
481    #[test]
482    fn session_dependent_cast_predicates_keep_the_invoking_evaluator() {
483        let schema = RowSchema::with_types(
484            ["d", "t", "z", "s", "n"].map(str::to_owned).to_vec(),
485            [
486                ColumnType::Date,
487                ColumnType::Timestamp,
488                ColumnType::TimestampTz,
489                ColumnType::Text,
490                ColumnType::Integer,
491            ]
492            .map(Some)
493            .to_vec(),
494        );
495        for sql in [
496            "d::timestamptz = z",
497            "t::timestamptz = z",
498            "z::timestamp = t",
499            "z::date = d",
500            "d::timestamp::timestamptz = z",
501            "d::text = s",
502            "s::date = d",
503            "s::timestamptz = z",
504        ] {
505            assert!(
506                ProjectedPredicate::compile_with_schema(&parsed_expression(sql), &schema, &[])
507                    .unwrap()
508                    .is_none(),
509                "{sql}"
510            );
511        }
512        for sql in ["d::timestamp = t", "n::bigint = n", "n::text = s"] {
513            assert!(
514                ProjectedPredicate::compile_with_schema(&parsed_expression(sql), &schema, &[])
515                    .unwrap()
516                    .is_some(),
517                "{sql}"
518            );
519        }
520        assert!(ProjectedPredicate::compile(
521            &parsed_expression("x::smallint = 1"),
522            &["x".into()],
523            &[],
524        )
525        .unwrap()
526        .is_none());
527    }
528
529    #[test]
530    fn projected_predicate_retains_analyzed_datetime_literals() {
531        let expression = ScalarExpr::Binary {
532            op: BinaryOp::Less,
533            lhs: Box::new(ScalarExpr::Column("day".into())),
534            rhs: Box::new(ScalarExpr::Cast {
535                implicit: false,
536                expr: Box::new(ScalarExpr::Literal(Value::Str("1995-03-15".into()))),
537                ty: "date".into(),
538            }),
539        };
540        // Text input still needs the invoking DateStyle. An analyzed temporal constant already owns its value and remains eligible for positional evaluation.
541        assert!(
542            ProjectedPredicate::compile(&expression, &["day".into()], &[])
543                .unwrap()
544                .is_none()
545        );
546        let ScalarExpr::Binary { op, lhs, .. } = expression else {
547            unreachable!();
548        };
549        let expression = ScalarExpr::Binary {
550            op,
551            lhs,
552            rhs: Box::new(ScalarExpr::TypedLiteral {
553                value: Value::Temporal(uqa_core::TemporalValue::Date { days: 9204 }),
554                ty: "date".into(),
555                bound_type: Some(ColumnType::Date),
556                collation_oid: None,
557                composite_source: None,
558                parameter_index: None,
559            }),
560        };
561        let predicate = ProjectedPredicate::compile(&expression, &["day".into()], &[])
562            .unwrap()
563            .unwrap();
564
565        let ProjectedExpr::Binary { rhs, .. } = &predicate.expression else {
566            panic!("expected a compiled comparison");
567        };
568        assert!(matches!(
569            rhs.as_ref(),
570            ProjectedExpr::Literal(Value::Temporal(_))
571        ));
572    }
573
574    #[test]
575    fn projected_predicate_preserves_unary_minus_integer_width() {
576        let expression = ScalarExpr::Binary {
577            op: BinaryOp::Equal,
578            lhs: Box::new(ScalarExpr::UnaryMinus(Box::new(ScalarExpr::Cast {
579                implicit: false,
580                expr: Box::new(ScalarExpr::Column("x".into())),
581                ty: "smallint".into(),
582            }))),
583            rhs: Box::new(ScalarExpr::Literal(Value::Int(-1))),
584        };
585        let schema = RowSchema::with_types(vec!["x".into()], vec![Some(ColumnType::Integer)]);
586        let predicate = ProjectedPredicate::compile_with_schema(&expression, &schema, &[])
587            .unwrap()
588            .unwrap();
589
590        assert!(predicate.keep(&[&Value::Int(1)]).unwrap());
591        let error = predicate
592            .keep(&[&Value::Int(i64::from(i16::MIN))])
593            .expect_err("negating smallint minimum must overflow");
594        assert_eq!(error.sqlstate(), Some("22003"));
595    }
596
597    fn assert_projected_parity(
598        expression: &ScalarExpr,
599        predicate: &ProjectedPredicate,
600        fields: &[String],
601        values: &[Value],
602        params: &[SQLParam],
603    ) {
604        let row = fields
605            .iter()
606            .cloned()
607            .zip(values.iter().cloned())
608            .collect::<uqa_sql::ResultRow>();
609        let expected = eval_scalar(expression, &ScalarEvalContext::new(Some(&row), params))
610            .map(|value| truthy(&value));
611        let references = values.iter().collect::<Vec<_>>();
612        let actual = predicate.keep(&references);
613        match (expected, actual) {
614            (Ok(expected), Ok(actual)) => assert_eq!(actual, expected, "row: {row:?}"),
615            (Err(expected), Err(actual)) => assert_eq!(actual.to_string(), expected.to_string()),
616            (expected, actual) => panic!("projected result {actual:?} != canonical {expected:?}"),
617        }
618    }
619
620    fn membership(dispatch: &str, operator: &str, elements: Vec<Value>, oid: u32) -> ScalarExpr {
621        ScalarExpr::Func {
622            name: dispatch.into(),
623            binding: Some(uqa_sql::ast::FunctionBinding {
624                object_id: None,
625                name: if dispatch == "__any_op" {
626                    "ANY operator"
627                } else {
628                    "ALL operator"
629                }
630                .into(),
631                argument_types: Vec::new(),
632                builtin: true,
633                dispatch: Some(if dispatch == "__any_op" {
634                    uqa_sql::ast::FunctionDispatch::AnyOperator
635                } else {
636                    uqa_sql::ast::FunctionDispatch::AllOperator
637                }),
638                invocation: None,
639                value_binding: None,
640                resolution_error: None,
641                input_collation: None,
642            }),
643            args: vec![
644                ScalarExpr::Column("mode".into()),
645                ScalarExpr::Literal(Value::Array(
646                    uqa_core::ArrayValue::try_new(elements)
647                        .unwrap()
648                        .with_element_type_oid(Some(oid)),
649                )),
650                ScalarExpr::Literal(Value::Str(operator.into())),
651            ],
652            distinct: false,
653            order_by: Vec::new(),
654            order_syntax: uqa_sql::ast::FunctionOrderSyntax::Legacy,
655            filter: None,
656        }
657    }
658
659    #[test]
660    fn literal_array_membership_matches_canonical_any_and_all() {
661        let modes = || {
662            vec![
663                Value::FixedChar("AIR".into()),
664                Value::FixedChar("AIR REG".into()),
665            ]
666        };
667        let with_null = || vec![Value::Int(1), Value::Null];
668        let mut typed = membership("__any_op", "=", modes(), 1042);
669        if let ScalarExpr::Func { args, .. } = &mut typed {
670            let ScalarExpr::Literal(value) = args[1].clone() else {
671                unreachable!("literal array")
672            };
673            args[1] = ScalarExpr::TypedLiteral {
674                value,
675                ty: "pg_catalog.\"bpchar\"[]".into(),
676                collation_oid: None,
677                composite_source: None,
678                bound_type: Some(ColumnType::Array(Box::new(ColumnType::Bpchar))),
679                parameter_index: None,
680            };
681        }
682        let cases = [
683            typed,
684            membership("__any_op", "=", modes(), 1042),
685            membership("__all_op", "<>", modes(), 1042),
686            membership("__any_op", "=", with_null(), 23),
687            membership("__all_op", "<>", with_null(), 23),
688        ];
689        let rows = [
690            Value::FixedChar("AIR".into()),
691            Value::FixedChar("MAIL".into()),
692            Value::Int(1),
693            Value::Int(2),
694            Value::Null,
695        ];
696        for expression in &cases {
697            let predicate = ProjectedPredicate::compile(expression, &["mode".into()], &[])
698                .unwrap()
699                .unwrap_or_else(|| panic!("{expression:?} compiles"));
700            for value in &rows {
701                if matches!(
702                    (value, &expression),
703                    (Value::Int(_), ScalarExpr::Func { args, .. })
704                        if matches!(&args[1], ScalarExpr::Literal(Value::Array(array))
705                            if array.element_type_oid() == Some(1042))
706                ) {
707                    continue;
708                }
709                let row = uqa_sql::ResultRow::from([("mode".into(), value.clone())]);
710                let canonical =
711                    eval_scalar(expression, &ScalarEvalContext::new(Some(&row), &[])).unwrap();
712                assert_eq!(
713                    predicate.keep(&[value]).unwrap(),
714                    truthy(&canonical),
715                    "{expression:?} with {value:?}"
716                );
717            }
718        }
719    }
720
721    #[test]
722    fn empty_ordered_or_user_typed_membership_keeps_the_canonical_evaluator() {
723        for expression in [
724            membership("__any_op", "=", Vec::new(), 23),
725            membership("__any_op", "<", vec![Value::Int(1)], 23),
726            membership("__all_op", "=", vec![Value::Int(1)], 23),
727            membership("__any_op", "=", vec![Value::Int(1)], 16_384),
728        ] {
729            assert!(
730                ProjectedPredicate::compile(&expression, &["mode".into()], &[])
731                    .unwrap()
732                    .is_none(),
733                "{expression:?}"
734            );
735        }
736    }
737}