1use std::borrow::Cow;
4use std::collections::{HashMap, HashSet};
5use std::fmt::{Display, Formatter};
6use std::sync::Arc;
7
8use itertools::Itertools;
9use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
10
11pub use self::column_names::{
12 column_expr, column_expr_ref, column_name, column_pred, joined_column_expr, joined_column_name,
13 ColumnName,
14};
15pub use self::scalars::{ArrayData, DecimalData, MapData, Scalar, StructData};
16use crate::kernel_predicates::{
17 DirectDataSkippingPredicateEvaluator, DirectPredicateEvaluator,
18 IndirectDataSkippingPredicateEvaluator,
19};
20use crate::schema::SchemaRef;
21use crate::transforms::ExpressionTransform;
22use crate::{DataType, DeltaResult, DynPartialEq};
23
24mod column_names;
25pub(crate) mod literal_expression_transform;
26pub(crate) use literal_expression_transform::literal_expression_transform;
27mod scalars;
28
29pub type ExpressionRef = std::sync::Arc<Expression>;
30pub type PredicateRef = std::sync::Arc<Predicate>;
31
32#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
38pub enum UnaryPredicateOp {
39 IsNull,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
45pub enum BinaryPredicateOp {
46 LessThan,
48 GreaterThan,
50 Equal,
52 Distinct,
54 In,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
60pub enum UnaryExpressionOp {
61 ToJson,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
67pub enum BinaryExpressionOp {
68 Plus,
70 Minus,
72 Multiply,
74 Divide,
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
80pub enum VariadicExpressionOp {
81 Coalesce,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
87pub enum JunctionPredicateOp {
88 And,
90 Or,
92}
93
94pub type ScalarExpressionEvaluator<'a> = dyn Fn(&Expression) -> Option<Scalar> + 'a;
102
103pub trait OpaqueExpressionOp: DynPartialEq + std::fmt::Debug {
105 fn name(&self) -> &str;
107
108 fn eval_expr_scalar(
119 &self,
120 eval_expr: &ScalarExpressionEvaluator<'_>,
121 exprs: &[Expression],
122 ) -> DeltaResult<Scalar>;
123}
124
125pub trait OpaquePredicateOp: DynPartialEq + std::fmt::Debug {
127 fn name(&self) -> &str;
129
130 fn eval_pred_scalar(
143 &self,
144 eval_expr: &ScalarExpressionEvaluator<'_>,
145 eval_pred: &DirectPredicateEvaluator<'_>,
146 exprs: &[Expression],
147 inverted: bool,
148 ) -> DeltaResult<Option<bool>>;
149
150 fn eval_as_data_skipping_predicate(
160 &self,
161 evaluator: &DirectDataSkippingPredicateEvaluator<'_>,
162 exprs: &[Expression],
163 inverted: bool,
164 ) -> Option<bool>;
165
166 fn as_data_skipping_predicate(
179 &self,
180 evaluator: &IndirectDataSkippingPredicateEvaluator<'_>,
181 exprs: &[Expression],
182 inverted: bool,
183 ) -> Option<Predicate>;
184}
185
186pub type OpaqueExpressionOpRef = Arc<dyn OpaqueExpressionOp>;
188
189pub type OpaquePredicateOpRef = Arc<dyn OpaquePredicateOp>;
191
192#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
197pub struct UnaryPredicate {
198 pub op: UnaryPredicateOp,
200 pub expr: Box<Expression>,
202}
203
204#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
205pub struct BinaryPredicate {
206 pub op: BinaryPredicateOp,
208 pub left: Box<Expression>,
210 pub right: Box<Expression>,
212}
213
214#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
215pub struct UnaryExpression {
216 pub op: UnaryExpressionOp,
218 pub expr: Box<Expression>,
220}
221
222#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
223pub struct BinaryExpression {
224 pub op: BinaryExpressionOp,
226 pub left: Box<Expression>,
228 pub right: Box<Expression>,
230}
231
232#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
233pub struct VariadicExpression {
234 pub op: VariadicExpressionOp,
236 pub exprs: Vec<Expression>,
238}
239
240#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
244pub struct ParseJsonExpression {
245 pub json_expr: Box<Expression>,
247 pub output_schema: SchemaRef,
249}
250
251#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
252pub struct JunctionPredicate {
253 pub op: JunctionPredicateOp,
255 pub preds: Vec<Predicate>,
257}
258
259#[derive(Clone, Debug)]
266pub struct OpaquePredicate {
267 pub op: OpaquePredicateOpRef,
268 pub exprs: Vec<Expression>,
269}
270fn fail_serialize_opaque_predicate<S>(
271 _value: &OpaquePredicate,
272 _serializer: S,
273) -> Result<S::Ok, S::Error>
274where
275 S: Serializer,
276{
277 Err(ser::Error::custom("Cannot serialize an Opaque Predicate"))
278}
279
280fn fail_deserialize_opaque_predicate<'de, D>(_deserializer: D) -> Result<OpaquePredicate, D::Error>
281where
282 D: Deserializer<'de>,
283{
284 Err(de::Error::custom("Cannot deserialize an Opaque Predicate"))
285}
286
287impl OpaquePredicate {
288 pub(crate) fn new(
289 op: OpaquePredicateOpRef,
290 exprs: impl IntoIterator<Item = Expression>,
291 ) -> Self {
292 let exprs = exprs.into_iter().collect();
293 Self { op, exprs }
294 }
295}
296
297#[derive(Clone, Debug)]
304pub struct OpaqueExpression {
305 pub op: OpaqueExpressionOpRef,
306 pub exprs: Vec<Expression>,
307}
308
309impl OpaqueExpression {
310 pub(crate) fn new(
311 op: OpaqueExpressionOpRef,
312 exprs: impl IntoIterator<Item = Expression>,
313 ) -> Self {
314 let exprs = exprs.into_iter().collect();
315 Self { op, exprs }
316 }
317}
318
319fn fail_serialize_opaque_expression<S>(
320 _value: &OpaqueExpression,
321 _serializer: S,
322) -> Result<S::Ok, S::Error>
323where
324 S: Serializer,
325{
326 Err(ser::Error::custom("Cannot serialize an Opaque Expression"))
327}
328
329fn fail_deserialize_opaque_expression<'de, D>(
330 _deserializer: D,
331) -> Result<OpaqueExpression, D::Error>
332where
333 D: Deserializer<'de>,
334{
335 Err(de::Error::custom("Cannot deserialize an Opaque Expression"))
336}
337
338#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
341pub struct FieldTransform {
342 pub exprs: Vec<ExpressionRef>,
344 pub is_replace: bool,
346 pub optional: bool,
349}
350
351#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
359pub struct Transform {
360 pub input_path: Option<ColumnName>,
363 pub field_transforms: HashMap<String, FieldTransform>,
365 pub prepended_fields: Vec<ExpressionRef>,
367}
368
369impl Transform {
370 pub fn new_top_level() -> Self {
373 Self::default()
374 }
375
376 pub fn new_nested<A>(path: impl IntoIterator<Item = A>) -> Self
379 where
380 ColumnName: FromIterator<A>,
381 {
382 Self {
383 input_path: Some(ColumnName::new(path)),
384 ..Default::default()
385 }
386 }
387
388 pub fn with_dropped_field(mut self, name: impl Into<String>) -> Self {
390 let field_transform = self.field_transform(name);
391 field_transform.is_replace = true;
392 self
393 }
394
395 pub fn with_dropped_field_if_exists(mut self, name: impl Into<String>) -> Self {
397 let field_transform = self.field_transform(name);
398 field_transform.is_replace = true;
399 field_transform.optional = true;
400 self
401 }
402
403 pub fn with_replaced_field(mut self, name: impl Into<String>, expr: ExpressionRef) -> Self {
405 let field_transform = self.field_transform(name);
406 field_transform.exprs.push(expr);
407 field_transform.is_replace = true;
408 self
409 }
410
411 pub fn with_inserted_field(
415 mut self,
416 after: Option<impl Into<String>>,
417 expr: ExpressionRef,
418 ) -> Self {
419 match after {
420 Some(field_name) => self.field_transform(field_name).exprs.push(expr),
421 None => self.prepended_fields.push(expr),
422 }
423 self
424 }
425
426 pub fn is_identity(&self) -> bool {
429 self.prepended_fields.is_empty() && self.field_transforms.is_empty()
430 }
431
432 pub fn input_path(&self) -> Option<&ColumnName> {
434 self.input_path.as_ref()
435 }
436
437 fn field_transform(&mut self, field_name: impl Into<String>) -> &mut FieldTransform {
439 self.field_transforms.entry(field_name.into()).or_default()
440 }
441}
442
443#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
449pub enum Expression {
450 Literal(Scalar),
452 Column(ColumnName),
454 Predicate(Box<Predicate>), Struct(Vec<ExpressionRef>, Option<ExpressionRef>),
459 Transform(Transform),
462 Unary(UnaryExpression),
464 Binary(BinaryExpression),
466 Variadic(VariadicExpression),
468 #[serde(serialize_with = "fail_serialize_opaque_expression")]
471 #[serde(deserialize_with = "fail_deserialize_opaque_expression")]
472 Opaque(OpaqueExpression),
473 Unknown(String),
482 ParseJson(ParseJsonExpression),
484 MapToStruct(MapToStructExpression),
487}
488
489#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
495pub enum Predicate {
496 BooleanExpression(Expression),
498 Not(Box<Predicate>),
505 Unary(UnaryPredicate),
507 Binary(BinaryPredicate),
509 Junction(JunctionPredicate),
511 #[serde(serialize_with = "fail_serialize_opaque_predicate")]
514 #[serde(deserialize_with = "fail_deserialize_opaque_predicate")]
515 Opaque(OpaquePredicate),
516 Unknown(String),
525}
526
527impl BinaryPredicateOp {
532 pub(crate) fn is_null_intolerant(&self) -> bool {
534 use BinaryPredicateOp::*;
535 match self {
536 LessThan | GreaterThan | Equal => true,
537 Distinct | In => false, }
539 }
540}
541
542impl JunctionPredicateOp {
543 pub(crate) fn invert(&self) -> JunctionPredicateOp {
544 use JunctionPredicateOp::*;
545 match self {
546 And => Or,
547 Or => And,
548 }
549 }
550}
551
552impl UnaryExpression {
553 pub(crate) fn new(op: UnaryExpressionOp, expr: impl Into<Expression>) -> Self {
554 let expr = Box::new(expr.into());
555 Self { op, expr }
556 }
557}
558
559impl UnaryPredicate {
560 pub(crate) fn new(op: UnaryPredicateOp, expr: impl Into<Expression>) -> Self {
561 let expr = Box::new(expr.into());
562 Self { op, expr }
563 }
564}
565
566impl BinaryExpression {
567 pub(crate) fn new(
568 op: BinaryExpressionOp,
569 left: impl Into<Expression>,
570 right: impl Into<Expression>,
571 ) -> Self {
572 let left = Box::new(left.into());
573 let right = Box::new(right.into());
574 Self { op, left, right }
575 }
576}
577
578impl BinaryPredicate {
579 pub(crate) fn new(
580 op: BinaryPredicateOp,
581 left: impl Into<Expression>,
582 right: impl Into<Expression>,
583 ) -> Self {
584 let left = Box::new(left.into());
585 let right = Box::new(right.into());
586 Self { op, left, right }
587 }
588}
589
590impl VariadicExpression {
591 pub(crate) fn new(
592 op: VariadicExpressionOp,
593 exprs: impl IntoIterator<Item = impl Into<Expression>>,
594 ) -> Self {
595 let exprs = exprs.into_iter().map(Into::into).collect();
596 Self { op, exprs }
597 }
598}
599
600impl ParseJsonExpression {
601 pub(crate) fn new(json_expr: impl Into<Expression>, output_schema: SchemaRef) -> Self {
602 Self {
603 json_expr: Box::new(json_expr.into()),
604 output_schema,
605 }
606 }
607}
608
609#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
621pub struct MapToStructExpression {
622 pub map_expr: Box<Expression>,
624}
625
626impl MapToStructExpression {
627 pub(crate) fn new(map_expr: impl Into<Expression>) -> Self {
628 Self {
629 map_expr: Box::new(map_expr.into()),
630 }
631 }
632}
633
634impl JunctionPredicate {
635 pub(crate) fn new(op: JunctionPredicateOp, preds: Vec<Predicate>) -> Self {
636 Self { op, preds }
637 }
638}
639
640impl Expression {
641 pub fn references(&self) -> HashSet<&ColumnName> {
643 let mut references = GetColumnReferences::default();
644 let _ = references.transform_expr(self);
645 references.0
646 }
647
648 pub fn column<A>(field_names: impl IntoIterator<Item = A>) -> Expression
650 where
651 ColumnName: FromIterator<A>,
652 {
653 ColumnName::new(field_names).into()
654 }
655
656 pub fn literal(value: impl Into<Scalar>) -> Self {
658 Self::Literal(value.into())
659 }
660
661 pub const fn null_literal(data_type: DataType) -> Self {
663 Self::Literal(Scalar::Null(data_type))
664 }
665
666 pub fn from_pred(value: Predicate) -> Self {
668 match value {
669 Predicate::BooleanExpression(expr) => expr,
670 _ => Self::Predicate(Box::new(value)),
671 }
672 }
673
674 pub fn struct_from(exprs: impl IntoIterator<Item = impl Into<Arc<Self>>>) -> Self {
681 Self::Struct(exprs.into_iter().map(Into::into).collect(), None)
682 }
683
684 pub fn struct_with_nullability_from(
689 exprs: impl IntoIterator<Item = impl Into<Arc<Self>>>,
690 nullability_predicate: impl Into<Arc<Self>>,
691 ) -> Self {
692 Self::Struct(
693 exprs.into_iter().map(Into::into).collect(),
694 Some(nullability_predicate.into()),
695 )
696 }
697
698 pub fn transform(transform: Transform) -> Self {
700 Self::Transform(transform)
701 }
702
703 pub fn is_null(self) -> Predicate {
705 Predicate::is_null(self)
706 }
707
708 pub fn is_not_null(self) -> Predicate {
710 Predicate::is_not_null(self)
711 }
712
713 pub fn eq(self, other: impl Into<Self>) -> Predicate {
715 Predicate::eq(self, other)
716 }
717
718 pub fn ne(self, other: impl Into<Self>) -> Predicate {
720 Predicate::ne(self, other)
721 }
722
723 pub fn le(self, other: impl Into<Self>) -> Predicate {
725 Predicate::le(self, other)
726 }
727
728 pub fn lt(self, other: impl Into<Self>) -> Predicate {
730 Predicate::lt(self, other)
731 }
732
733 pub fn ge(self, other: impl Into<Self>) -> Predicate {
735 Predicate::ge(self, other)
736 }
737
738 pub fn gt(self, other: impl Into<Self>) -> Predicate {
740 Predicate::gt(self, other)
741 }
742
743 pub fn distinct(self, other: impl Into<Self>) -> Predicate {
745 Predicate::distinct(self, other)
746 }
747
748 pub fn unary(op: UnaryExpressionOp, expr: impl Into<Expression>) -> Self {
750 Self::Unary(UnaryExpression::new(op, expr))
751 }
752
753 pub fn binary(
755 op: BinaryExpressionOp,
756 lhs: impl Into<Expression>,
757 rhs: impl Into<Expression>,
758 ) -> Self {
759 Self::Binary(BinaryExpression::new(op, lhs, rhs))
760 }
761
762 pub fn variadic(
764 op: VariadicExpressionOp,
765 exprs: impl IntoIterator<Item = impl Into<Expression>>,
766 ) -> Self {
767 Self::Variadic(VariadicExpression::new(op, exprs))
768 }
769
770 pub fn coalesce(exprs: impl IntoIterator<Item = impl Into<Expression>>) -> Self {
775 Self::variadic(VariadicExpressionOp::Coalesce, exprs)
776 }
777
778 pub fn opaque(
780 op: impl OpaqueExpressionOp,
781 exprs: impl IntoIterator<Item = Expression>,
782 ) -> Self {
783 Self::Opaque(OpaqueExpression::new(Arc::new(op), exprs))
784 }
785
786 pub fn unknown(name: impl Into<String>) -> Self {
788 Self::Unknown(name.into())
789 }
790
791 pub fn parse_json(json_expr: impl Into<Expression>, output_schema: SchemaRef) -> Self {
794 Self::ParseJson(ParseJsonExpression::new(json_expr, output_schema))
795 }
796
797 pub fn map_to_struct(map_expr: impl Into<Expression>) -> Self {
801 Self::MapToStruct(MapToStructExpression::new(map_expr))
802 }
803}
804
805impl Predicate {
806 pub fn references(&self) -> HashSet<&ColumnName> {
808 let mut references = GetColumnReferences::default();
809 let _ = references.transform_pred(self);
810 references.0
811 }
812
813 pub fn column<A>(field_names: impl IntoIterator<Item = A>) -> Predicate
815 where
816 ColumnName: FromIterator<A>,
817 {
818 Self::from_expr(ColumnName::new(field_names))
819 }
820
821 pub const fn literal(value: bool) -> Self {
823 Self::BooleanExpression(Expression::Literal(Scalar::Boolean(value)))
824 }
825
826 pub const fn null_literal() -> Self {
828 Self::BooleanExpression(Expression::Literal(Scalar::Null(DataType::BOOLEAN)))
829 }
830
831 pub fn from_expr(expr: impl Into<Expression>) -> Self {
833 match expr.into() {
834 Expression::Predicate(p) => *p,
835 expr => Predicate::BooleanExpression(expr),
836 }
837 }
838
839 pub fn not(pred: impl Into<Self>) -> Self {
841 Self::Not(Box::new(pred.into()))
842 }
843
844 pub fn is_null(expr: impl Into<Expression>) -> Predicate {
846 Self::unary(UnaryPredicateOp::IsNull, expr)
847 }
848
849 pub fn is_not_null(expr: impl Into<Expression>) -> Predicate {
851 Self::not(Self::is_null(expr))
852 }
853
854 pub fn eq(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
856 Self::binary(BinaryPredicateOp::Equal, a, b)
857 }
858
859 pub fn ne(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
861 Self::not(Self::binary(BinaryPredicateOp::Equal, a, b))
862 }
863
864 pub fn le(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
866 Self::not(Self::binary(BinaryPredicateOp::GreaterThan, a, b))
867 }
868
869 pub fn lt(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
871 Self::binary(BinaryPredicateOp::LessThan, a, b)
872 }
873
874 pub fn ge(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
876 Self::not(Self::binary(BinaryPredicateOp::LessThan, a, b))
877 }
878
879 pub fn gt(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
881 Self::binary(BinaryPredicateOp::GreaterThan, a, b)
882 }
883
884 pub fn distinct(a: impl Into<Expression>, b: impl Into<Expression>) -> Self {
886 Self::binary(BinaryPredicateOp::Distinct, a, b)
887 }
888
889 pub fn and(a: impl Into<Self>, b: impl Into<Self>) -> Self {
891 Self::and_from([a.into(), b.into()])
892 }
893
894 pub fn or(a: impl Into<Self>, b: impl Into<Self>) -> Self {
896 Self::or_from([a.into(), b.into()])
897 }
898
899 pub fn and_from(preds: impl IntoIterator<Item = Self>) -> Self {
902 Self::junction(JunctionPredicateOp::And, preds)
903 }
904
905 pub fn or_from(preds: impl IntoIterator<Item = Self>) -> Self {
908 Self::junction(JunctionPredicateOp::Or, preds)
909 }
910
911 pub fn unary(op: UnaryPredicateOp, expr: impl Into<Expression>) -> Self {
913 let expr = Box::new(expr.into());
914 Self::Unary(UnaryPredicate { op, expr })
915 }
916
917 pub fn binary(
919 op: BinaryPredicateOp,
920 lhs: impl Into<Expression>,
921 rhs: impl Into<Expression>,
922 ) -> Self {
923 Self::Binary(BinaryPredicate {
924 op,
925 left: Box::new(lhs.into()),
926 right: Box::new(rhs.into()),
927 })
928 }
929
930 pub fn junction(op: JunctionPredicateOp, preds: impl IntoIterator<Item = Self>) -> Self {
938 let mut preds: Vec<_> = preds.into_iter().collect();
939 match preds.len() {
940 0 => match op {
941 JunctionPredicateOp::And => Self::literal(true),
942 JunctionPredicateOp::Or => Self::literal(false),
943 },
944 1 => preds.remove(0),
946 _ => Self::Junction(JunctionPredicate { op, preds }),
947 }
948 }
949
950 pub fn opaque(op: impl OpaquePredicateOp, exprs: impl IntoIterator<Item = Expression>) -> Self {
952 Self::Opaque(OpaquePredicate::new(Arc::new(op), exprs))
953 }
954
955 pub fn unknown(name: impl Into<String>) -> Self {
957 Self::Unknown(name.into())
958 }
959}
960
961impl PartialEq for OpaquePredicate {
966 fn eq(&self, other: &Self) -> bool {
967 self.op.dyn_eq(other.op.any_ref()) && self.exprs == other.exprs
968 }
969}
970
971impl PartialEq for OpaqueExpression {
972 fn eq(&self, other: &Self) -> bool {
973 self.op.dyn_eq(other.op.any_ref()) && self.exprs == other.exprs
974 }
975}
976
977impl Display for UnaryExpressionOp {
978 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
979 use UnaryExpressionOp::*;
980 match self {
981 ToJson => write!(f, "TO_JSON"),
982 }
983 }
984}
985
986impl Display for BinaryExpressionOp {
987 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
988 use BinaryExpressionOp::*;
989 match self {
990 Plus => write!(f, "+"),
991 Minus => write!(f, "-"),
992 Multiply => write!(f, "*"),
993 Divide => write!(f, "/"),
994 }
995 }
996}
997
998impl Display for VariadicExpressionOp {
999 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1000 use VariadicExpressionOp::*;
1001 match self {
1002 Coalesce => write!(f, "COALESCE"),
1003 }
1004 }
1005}
1006
1007impl Display for BinaryPredicateOp {
1008 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1009 use BinaryPredicateOp::*;
1010 match self {
1011 LessThan => write!(f, "<"),
1012 GreaterThan => write!(f, ">"),
1013 Equal => write!(f, "="),
1014 Distinct => write!(f, "DISTINCT"),
1018 In => write!(f, "IN"),
1019 }
1020 }
1021}
1022
1023fn format_child_list<T: Display>(children: &[T]) -> String {
1025 children.iter().map(|c| format!("{c}")).join(", ")
1026}
1027
1028impl Display for Expression {
1029 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1030 use Expression::*;
1031 match self {
1032 Literal(l) => write!(f, "{l}"),
1033 Column(name) => write!(f, "Column({name})"),
1034 Predicate(p) => write!(f, "{p}"),
1035 Struct(exprs, _) => write!(f, "Struct({})", format_child_list(exprs)),
1036 Transform(transform) => {
1037 write!(f, "Transform(")?;
1038 let mut sep = "";
1039 if !transform.prepended_fields.is_empty() {
1040 let prepended_fields = format_child_list(&transform.prepended_fields);
1041 write!(f, "prepend [{prepended_fields}]")?;
1042 sep = ", ";
1043 }
1044 for (field_name, field_transform) in &transform.field_transforms {
1045 let insertions = &field_transform.exprs;
1046 if insertions.is_empty() {
1047 if field_transform.is_replace {
1048 write!(f, "{sep}drop {field_name}")?;
1049 } else {
1050 continue; }
1052 } else {
1053 let insertions = format_child_list(insertions);
1054 if field_transform.is_replace {
1055 write!(f, "{sep}replace {field_name} with [{insertions}]")?;
1056 } else {
1057 write!(f, "{sep}after {field_name} insert [{insertions}]")?;
1058 }
1059 }
1060 sep = ", ";
1061 }
1062 write!(f, ")")
1063 }
1064 Unary(UnaryExpression { op, expr }) => write!(f, "{op}({expr})"),
1065 Binary(BinaryExpression { op, left, right }) => write!(f, "{left} {op} {right}"),
1066 Variadic(VariadicExpression { op, exprs }) => {
1067 write!(f, "{op}({})", format_child_list(exprs))
1068 }
1069 Opaque(OpaqueExpression { op, exprs }) => {
1070 write!(f, "{op:?}({})", format_child_list(exprs))
1071 }
1072 Unknown(name) => write!(f, "<unknown: {name}>"),
1073 ParseJson(p) => {
1074 write!(
1075 f,
1076 "PARSE_JSON({}, <schema:{} fields>)",
1077 p.json_expr,
1078 p.output_schema.fields().len()
1079 )
1080 }
1081 MapToStruct(m) => write!(f, "MAP_TO_STRUCT({})", m.map_expr),
1082 }
1083 }
1084}
1085
1086impl Display for Predicate {
1087 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1088 use Predicate::*;
1089 match self {
1090 BooleanExpression(expr) => write!(f, "{expr}"),
1091 Not(pred) => write!(f, "NOT({pred})"),
1092 Binary(BinaryPredicate {
1093 op: BinaryPredicateOp::Distinct,
1094 left,
1095 right,
1096 }) => write!(f, "DISTINCT({left}, {right})"),
1097 Binary(BinaryPredicate { op, left, right }) => write!(f, "{left} {op} {right}"),
1098 Unary(UnaryPredicate { op, expr }) => match op {
1099 UnaryPredicateOp::IsNull => write!(f, "{expr} IS NULL"),
1100 },
1101 Junction(JunctionPredicate { op, preds }) => {
1102 let op = match op {
1103 JunctionPredicateOp::And => "AND",
1104 JunctionPredicateOp::Or => "OR",
1105 };
1106 write!(f, "{op}({})", format_child_list(preds))
1107 }
1108 Opaque(OpaquePredicate { op, exprs }) => {
1109 write!(f, "{op:?}({})", format_child_list(exprs))
1110 }
1111 Unknown(name) => write!(f, "<unknown: {name}>"),
1112 }
1113 }
1114}
1115
1116impl From<Scalar> for Expression {
1117 fn from(value: Scalar) -> Self {
1118 Self::literal(value)
1119 }
1120}
1121
1122impl From<ColumnName> for Expression {
1123 fn from(value: ColumnName) -> Self {
1124 Self::Column(value)
1125 }
1126}
1127
1128impl From<Predicate> for Expression {
1129 fn from(value: Predicate) -> Self {
1130 Self::from_pred(value)
1131 }
1132}
1133
1134impl From<ColumnName> for Predicate {
1135 fn from(value: ColumnName) -> Self {
1136 Self::from_expr(value)
1137 }
1138}
1139
1140impl<R: Into<Expression>> std::ops::Add<R> for Expression {
1141 type Output = Self;
1142
1143 fn add(self, rhs: R) -> Self::Output {
1144 Self::binary(BinaryExpressionOp::Plus, self, rhs)
1145 }
1146}
1147
1148impl<R: Into<Expression>> std::ops::Sub<R> for Expression {
1149 type Output = Self;
1150
1151 fn sub(self, rhs: R) -> Self {
1152 Self::binary(BinaryExpressionOp::Minus, self, rhs)
1153 }
1154}
1155
1156impl<R: Into<Expression>> std::ops::Mul<R> for Expression {
1157 type Output = Self;
1158
1159 fn mul(self, rhs: R) -> Self {
1160 Self::binary(BinaryExpressionOp::Multiply, self, rhs)
1161 }
1162}
1163
1164impl<R: Into<Expression>> std::ops::Div<R> for Expression {
1165 type Output = Self;
1166
1167 fn div(self, rhs: R) -> Self {
1168 Self::binary(BinaryExpressionOp::Divide, self, rhs)
1169 }
1170}
1171
1172#[derive(Default)]
1174struct GetColumnReferences<'a>(HashSet<&'a ColumnName>);
1175
1176impl<'a> ExpressionTransform<'a> for GetColumnReferences<'a> {
1177 fn transform_expr_column(&mut self, name: &'a ColumnName) -> Option<Cow<'a, ColumnName>> {
1178 self.0.insert(name);
1179 Some(Cow::Borrowed(name))
1180 }
1181}
1182
1183#[cfg(test)]
1184mod tests {
1185 use std::fmt::Debug;
1186
1187 use serde::de::DeserializeOwned;
1188 use serde::Serialize;
1189
1190 use super::{column_expr, column_pred, Expression as Expr, Predicate as Pred};
1191
1192 fn assert_roundtrip<T: Serialize + DeserializeOwned + PartialEq + Debug>(value: &T) {
1194 let json = serde_json::to_string(value).expect("serialization should succeed");
1195 let deserialized: T = serde_json::from_str(&json).expect("deserialization should succeed");
1196 assert_eq!(value, &deserialized, "roundtrip should preserve value");
1197 }
1198
1199 #[test]
1200 fn test_expression_format() {
1201 let cases = [
1202 (column_expr!("x"), "Column(x)"),
1203 (
1204 (column_expr!("x") + Expr::literal(4)) / Expr::literal(10) * Expr::literal(42),
1205 "Column(x) + 4 / 10 * 42",
1206 ),
1207 (
1208 Expr::struct_from([column_expr!("x"), Expr::literal(2), Expr::literal(10)]),
1209 "Struct(Column(x), 2, 10)",
1210 ),
1211 ];
1212
1213 for (expr, expected) in cases {
1214 let result = format!("{expr}");
1215 assert_eq!(result, expected);
1216 }
1217 }
1218
1219 #[test]
1220 fn test_predicate_format() {
1221 let cases = [
1222 (column_pred!("x"), "Column(x)"),
1223 (column_expr!("x").eq(Expr::literal(2)), "Column(x) = 2"),
1224 (
1225 (column_expr!("x") - Expr::literal(4)).lt(Expr::literal(10)),
1226 "Column(x) - 4 < 10",
1227 ),
1228 (
1229 Pred::and(
1230 column_expr!("x").ge(Expr::literal(2)),
1231 column_expr!("x").le(Expr::literal(10)),
1232 ),
1233 "AND(NOT(Column(x) < 2), NOT(Column(x) > 10))",
1234 ),
1235 (
1236 Pred::and_from([
1237 column_expr!("x").ge(Expr::literal(2)),
1238 column_expr!("x").le(Expr::literal(10)),
1239 column_expr!("x").le(Expr::literal(100)),
1240 ]),
1241 "AND(NOT(Column(x) < 2), NOT(Column(x) > 10), NOT(Column(x) > 100))",
1242 ),
1243 (
1244 Pred::or(
1245 column_expr!("x").gt(Expr::literal(2)),
1246 column_expr!("x").lt(Expr::literal(10)),
1247 ),
1248 "OR(Column(x) > 2, Column(x) < 10)",
1249 ),
1250 (
1251 column_expr!("x").eq(Expr::literal("foo")),
1252 "Column(x) = 'foo'",
1253 ),
1254 ];
1255
1256 for (pred, expected) in cases {
1257 let result = format!("{pred}");
1258 assert_eq!(result, expected);
1259 }
1260 }
1261
1262 mod serde_tests {
1265 use std::sync::Arc;
1266
1267 use crate::expressions::scalars::{ArrayData, DecimalData, MapData, StructData};
1268 use crate::expressions::{
1269 column_expr, column_name, BinaryExpressionOp, BinaryPredicateOp, ColumnName,
1270 Expression, Predicate, Scalar, Transform, UnaryExpressionOp,
1271 };
1272 use crate::schema::{ArrayType, DataType, DecimalType, MapType, StructField};
1273 use crate::utils::test_utils::assert_result_error_with_message;
1274
1275 use super::assert_roundtrip;
1276
1277 #[test]
1280 fn test_literal_scalars_roundtrip() {
1281 let cases: Vec<Expression> = vec![
1283 Expression::literal(42i32), Expression::literal(9999999999i64), Expression::literal(123i16), Expression::literal(42i8), Expression::literal(1.12345677_32), Expression::literal(1.12345667_64), Expression::literal("hello world"),
1292 Expression::literal(true),
1293 Expression::literal(false),
1294 Expression::Literal(Scalar::Timestamp(1234567890000000)),
1296 Expression::Literal(Scalar::TimestampNtz(1234567890000000)),
1297 Expression::Literal(Scalar::Date(19000)),
1298 Expression::Literal(Scalar::Binary(vec![1, 2, 3, 4, 5])),
1300 Expression::Literal(Scalar::Decimal(
1302 DecimalData::try_new(12345i128, DecimalType::try_new(10, 2).unwrap()).unwrap(),
1303 )),
1304 ];
1305
1306 for expr in &cases {
1307 assert_roundtrip(expr);
1308 }
1309 }
1310
1311 #[test]
1312 fn test_literal_complex_scalars_roundtrip() {
1313 let cases: Vec<Expression> = vec![
1315 Expression::null_literal(DataType::INTEGER),
1317 Expression::null_literal(DataType::STRING),
1318 Expression::null_literal(DataType::BOOLEAN),
1319 Expression::Literal(Scalar::Array(
1321 ArrayData::try_new(
1322 ArrayType::new(DataType::INTEGER, false),
1323 vec![Scalar::Integer(1), Scalar::Integer(2), Scalar::Integer(3)],
1324 )
1325 .unwrap(),
1326 )),
1327 Expression::Literal(Scalar::Map(
1329 MapData::try_new(
1330 MapType::new(DataType::STRING, DataType::INTEGER, false),
1331 vec![
1332 (Scalar::String("a".to_string()), Scalar::Integer(1)),
1333 (Scalar::String("b".to_string()), Scalar::Integer(2)),
1334 ],
1335 )
1336 .unwrap(),
1337 )),
1338 Expression::Literal(Scalar::Struct(
1340 StructData::try_new(
1341 vec![
1342 StructField::nullable("x", DataType::INTEGER),
1343 StructField::nullable("y", DataType::STRING),
1344 ],
1345 vec![Scalar::Integer(42), Scalar::String("hello".to_string())],
1346 )
1347 .unwrap(),
1348 )),
1349 ];
1350
1351 for expr in &cases {
1352 assert_roundtrip(expr);
1353 }
1354 }
1355
1356 #[test]
1359 fn test_column_expressions_roundtrip() {
1360 let cases: Vec<Expression> = vec![
1361 column_expr!("my_column"),
1362 Expression::column(["parent", "child"]),
1363 Expression::column(["a", "b", "c", "d"]),
1364 ];
1365
1366 for expr in &cases {
1367 assert_roundtrip(expr);
1368 }
1369 }
1370
1371 #[test]
1372 fn test_column_names_roundtrip() {
1373 let cases: Vec<ColumnName> = vec![
1374 column_name!("simple"),
1375 ColumnName::new(["a", "b", "c"]),
1376 ColumnName::new::<&str>([]),
1377 ];
1378
1379 for col in &cases {
1380 assert_roundtrip(col);
1381 }
1382 }
1383
1384 #[test]
1387 fn test_unary_expression_roundtrip() {
1388 let expr = Expression::unary(UnaryExpressionOp::ToJson, column_expr!("data"));
1389 assert_roundtrip(&expr);
1390 }
1391
1392 #[test]
1393 fn test_binary_expressions_roundtrip() {
1394 let ops = [
1395 BinaryExpressionOp::Plus,
1396 BinaryExpressionOp::Minus,
1397 BinaryExpressionOp::Multiply,
1398 BinaryExpressionOp::Divide,
1399 ];
1400
1401 for op in ops {
1402 let expr = Expression::binary(op, column_expr!("a"), Expression::literal(10));
1403 assert_roundtrip(&expr);
1404 }
1405 }
1406
1407 #[test]
1408 fn test_variadic_expression_roundtrip() {
1409 let expr = Expression::coalesce([
1410 column_expr!("a"),
1411 column_expr!("b"),
1412 Expression::literal("default"),
1413 ]);
1414 assert_roundtrip(&expr);
1415 }
1416
1417 #[test]
1418 fn test_nested_arithmetic_expression_roundtrip() {
1419 let left = Expression::binary(
1421 BinaryExpressionOp::Plus,
1422 column_expr!("a"),
1423 column_expr!("b"),
1424 );
1425 let right = Expression::binary(
1426 BinaryExpressionOp::Minus,
1427 column_expr!("c"),
1428 column_expr!("d"),
1429 );
1430 let mul = Expression::binary(BinaryExpressionOp::Multiply, left, right);
1431 let expr = Expression::binary(BinaryExpressionOp::Divide, mul, Expression::literal(2));
1432 assert_roundtrip(&expr);
1433 }
1434
1435 #[test]
1438 fn test_struct_expression_roundtrip() {
1439 let expr = Expression::struct_from([
1440 Arc::new(column_expr!("x")),
1441 Arc::new(Expression::literal(42)),
1442 Arc::new(Expression::literal("hello")),
1443 ]);
1444 assert_roundtrip(&expr);
1445 }
1446
1447 #[test]
1448 fn test_transform_expressions_roundtrip() {
1449 let cases: Vec<Expression> = vec![
1450 Expression::transform(Transform::new_top_level()),
1452 Expression::transform(Transform::new_top_level().with_dropped_field("old_column")),
1454 Expression::transform(
1456 Transform::new_top_level()
1457 .with_replaced_field("original", Arc::new(Expression::literal(0))),
1458 ),
1459 Expression::transform(
1461 Transform::new_top_level()
1462 .with_inserted_field(Some("after_col"), Arc::new(column_expr!("new_col")))
1463 .with_inserted_field(
1464 None::<String>,
1465 Arc::new(Expression::literal("prepended")),
1466 ),
1467 ),
1468 Expression::transform(
1470 Transform::new_nested(["parent", "child"]).with_dropped_field("to_drop"),
1471 ),
1472 ];
1473
1474 for expr in &cases {
1475 assert_roundtrip(expr);
1476 }
1477 }
1478
1479 #[test]
1480 fn test_expression_wrapping_predicate_roundtrip() {
1481 let pred = Predicate::eq(column_expr!("x"), Expression::literal(10));
1482 let expr = Expression::from_pred(pred);
1483 assert_roundtrip(&expr);
1484 }
1485
1486 #[test]
1487 fn test_expression_unknown_roundtrip() {
1488 let expr = Expression::unknown("some_unknown_function()");
1489 assert_roundtrip(&expr);
1490 }
1491
1492 #[test]
1493 fn test_map_to_struct_expression_roundtrip() {
1494 let cases: Vec<Expression> = vec![
1495 Expression::map_to_struct(column_expr!("pv")),
1496 Expression::map_to_struct(Expression::literal("ignored")),
1497 ];
1498
1499 for expr in &cases {
1500 assert_roundtrip(expr);
1501 }
1502 }
1503
1504 #[test]
1507 fn test_predicate_basics_roundtrip() {
1508 let cases: Vec<Predicate> = vec![
1509 Predicate::from_expr(column_expr!("is_active")),
1511 Predicate::literal(true),
1513 Predicate::literal(false),
1514 Predicate::not(Predicate::from_expr(column_expr!("x"))),
1516 Predicate::not(Predicate::not(Predicate::gt(
1518 column_expr!("x"),
1519 Expression::literal(5),
1520 ))),
1521 Predicate::unknown("some_unknown_predicate()"),
1523 Predicate::is_null(column_expr!("nullable_col")),
1525 Predicate::is_not_null(column_expr!("nullable_col")),
1526 ];
1527
1528 for pred in &cases {
1529 assert_roundtrip(pred);
1530 }
1531 }
1532
1533 #[test]
1534 fn test_predicate_null_literal_roundtrip() {
1535 let pred = Predicate::null_literal();
1536 assert_roundtrip(&pred);
1537 }
1538
1539 #[test]
1540 fn test_predicate_comparisons_roundtrip() {
1541 let cases: Vec<Predicate> = vec![
1542 Predicate::eq(column_expr!("x"), Expression::literal(42)),
1543 Predicate::ne(column_expr!("status"), Expression::literal("active")),
1544 Predicate::lt(column_expr!("age"), Expression::literal(18)),
1545 Predicate::le(column_expr!("price"), Expression::literal(100)),
1546 Predicate::gt(column_expr!("score"), Expression::literal(90)),
1547 Predicate::ge(column_expr!("quantity"), Expression::literal(1)),
1548 Predicate::distinct(column_expr!("a"), column_expr!("b")),
1549 ];
1550
1551 for pred in &cases {
1552 assert_roundtrip(pred);
1553 }
1554 }
1555
1556 #[test]
1557 fn test_predicate_in_roundtrip() {
1558 let array_data = ArrayData::try_new(
1559 ArrayType::new(DataType::INTEGER, false),
1560 vec![Scalar::Integer(1), Scalar::Integer(2), Scalar::Integer(3)],
1561 )
1562 .unwrap();
1563 let pred = Predicate::binary(
1564 BinaryPredicateOp::In,
1565 column_expr!("x"),
1566 Expression::Literal(Scalar::Array(array_data)),
1567 );
1568 assert_roundtrip(&pred);
1569 }
1570
1571 #[test]
1572 fn test_predicate_junctions_roundtrip() {
1573 let cases: Vec<Predicate> = vec![
1574 Predicate::and(
1576 Predicate::gt(column_expr!("x"), Expression::literal(0)),
1577 Predicate::lt(column_expr!("x"), Expression::literal(100)),
1578 ),
1579 Predicate::or(
1581 Predicate::eq(column_expr!("status"), Expression::literal("active")),
1582 Predicate::eq(column_expr!("status"), Expression::literal("pending")),
1583 ),
1584 Predicate::and_from([
1586 Predicate::gt(column_expr!("x"), Expression::literal(0)),
1587 Predicate::lt(column_expr!("x"), Expression::literal(100)),
1588 Predicate::is_not_null(column_expr!("x")),
1589 ]),
1590 Predicate::or_from([
1592 Predicate::eq(column_expr!("type"), Expression::literal("A")),
1593 Predicate::eq(column_expr!("type"), Expression::literal("B")),
1594 Predicate::eq(column_expr!("type"), Expression::literal("C")),
1595 ]),
1596 Predicate::or(
1598 Predicate::and(
1599 Predicate::gt(column_expr!("a"), Expression::literal(0)),
1600 Predicate::lt(column_expr!("b"), Expression::literal(100)),
1601 ),
1602 Predicate::eq(column_expr!("c"), Expression::literal("special")),
1603 ),
1604 ];
1605
1606 for pred in &cases {
1607 assert_roundtrip(pred);
1608 }
1609 }
1610
1611 #[test]
1614 fn test_deeply_nested_structures_roundtrip() {
1615 let add = Expression::binary(
1617 BinaryExpressionOp::Plus,
1618 column_expr!("a"),
1619 column_expr!("b"),
1620 );
1621 let mul = Expression::binary(
1622 BinaryExpressionOp::Multiply,
1623 column_expr!("c"),
1624 column_expr!("d"),
1625 );
1626 let coalesce = Expression::coalesce([add, mul, Expression::literal(0)]);
1627 let pred = Predicate::gt(coalesce, Expression::literal(100));
1628 assert_roundtrip(&pred);
1629
1630 let inner_pred = Predicate::and(
1632 Predicate::eq(column_expr!("x"), Expression::literal(1)),
1633 Predicate::gt(
1634 Expression::binary(
1635 BinaryExpressionOp::Plus,
1636 column_expr!("y"),
1637 column_expr!("z"),
1638 ),
1639 Expression::literal(10),
1640 ),
1641 );
1642 let expr = Expression::from_pred(inner_pred);
1643 assert_roundtrip(&expr);
1644 }
1645
1646 #[test]
1649 fn test_opaque_expression_serialize_fails() {
1650 use crate::expressions::{OpaqueExpressionOp, ScalarExpressionEvaluator};
1651 use crate::DeltaResult;
1652
1653 #[derive(Debug, PartialEq)]
1654 struct TestOpaqueExprOp;
1655
1656 impl OpaqueExpressionOp for TestOpaqueExprOp {
1657 fn name(&self) -> &str {
1658 "test_opaque"
1659 }
1660 fn eval_expr_scalar(
1661 &self,
1662 _eval_expr: &ScalarExpressionEvaluator<'_>,
1663 _exprs: &[Expression],
1664 ) -> DeltaResult<Scalar> {
1665 Ok(Scalar::Integer(0))
1666 }
1667 }
1668
1669 let expr = Expression::opaque(TestOpaqueExprOp, [Expression::literal(1)]);
1670 let result = serde_json::to_string(&expr);
1671 assert_result_error_with_message(result, "Cannot serialize an Opaque Expression");
1672 }
1673
1674 #[test]
1675 fn test_opaque_predicate_serialize_fails() {
1676 use crate::expressions::{OpaquePredicateOp, ScalarExpressionEvaluator};
1677 use crate::kernel_predicates::{
1678 DirectDataSkippingPredicateEvaluator, DirectPredicateEvaluator,
1679 IndirectDataSkippingPredicateEvaluator,
1680 };
1681 use crate::DeltaResult;
1682
1683 #[derive(Debug, PartialEq)]
1684 struct TestOpaquePredOp;
1685
1686 impl OpaquePredicateOp for TestOpaquePredOp {
1687 fn name(&self) -> &str {
1688 "test_opaque_pred"
1689 }
1690 fn eval_pred_scalar(
1691 &self,
1692 _eval_expr: &ScalarExpressionEvaluator<'_>,
1693 _eval_pred: &DirectPredicateEvaluator<'_>,
1694 _exprs: &[Expression],
1695 _inverted: bool,
1696 ) -> DeltaResult<Option<bool>> {
1697 Ok(Some(true))
1698 }
1699 fn eval_as_data_skipping_predicate(
1700 &self,
1701 _evaluator: &DirectDataSkippingPredicateEvaluator<'_>,
1702 _exprs: &[Expression],
1703 _inverted: bool,
1704 ) -> Option<bool> {
1705 Some(true)
1706 }
1707 fn as_data_skipping_predicate(
1708 &self,
1709 _evaluator: &IndirectDataSkippingPredicateEvaluator<'_>,
1710 _exprs: &[Expression],
1711 _inverted: bool,
1712 ) -> Option<Predicate> {
1713 None
1714 }
1715 }
1716
1717 let pred = Predicate::opaque(TestOpaquePredOp, [Expression::literal(1)]);
1718 let result = serde_json::to_string(&pred);
1719 assert_result_error_with_message(result, "Cannot serialize an Opaque Predicate");
1720 }
1721 }
1722
1723 #[test]
1724 fn single_element_and_from_returns_unwrapped_predicate() {
1725 let inner = Pred::gt(column_expr!("x"), Expr::literal(0));
1726 let result = Pred::and_from([inner.clone()]);
1727 assert_eq!(result, inner);
1728 }
1729
1730 #[test]
1731 fn single_element_or_from_returns_unwrapped_predicate() {
1732 let inner = Pred::gt(column_expr!("x"), Expr::literal(0));
1733 let result = Pred::or_from([inner.clone()]);
1734 assert_eq!(result, inner);
1735 }
1736
1737 #[test]
1738 fn multi_element_and_from_returns_junction() {
1739 let p1 = Pred::gt(column_expr!("x"), Expr::literal(0));
1740 let p2 = Pred::lt(column_expr!("x"), Expr::literal(100));
1741 let result = Pred::and_from([p1.clone(), p2.clone()]);
1742 assert!(matches!(result, Pred::Junction(ref j) if j.preds.len() == 2));
1743 assert_eq!(result, Pred::and(p1, p2));
1744 }
1745
1746 #[test]
1747 fn empty_and_from_returns_identity_literal() {
1748 let result = Pred::and_from(std::iter::empty());
1749 assert_eq!(result, Pred::literal(true));
1750 }
1751
1752 #[test]
1753 fn empty_or_from_returns_identity_literal() {
1754 let result = Pred::or_from(std::iter::empty());
1755 assert_eq!(result, Pred::literal(false));
1756 }
1757}