1use std::sync::Arc;
7
8use vortex_error::VortexExpect;
9use vortex_error::VortexResult;
10use vortex_error::vortex_ensure;
11use vortex_error::vortex_panic;
12use vortex_utils::iter::ReduceBalancedIterExt;
13
14use crate::aggregate_fn::NumericalAggregateOpts;
15use crate::dtype::DType;
16use crate::dtype::FieldName;
17use crate::dtype::FieldNames;
18use crate::dtype::Nullability;
19use crate::expr::BoundExpression;
20use crate::expr::Expression;
21use crate::expr::Lambda;
22use crate::scalar::Scalar;
23use crate::scalar::ScalarValue;
24use crate::scalar_fn::EmptyOptions;
25use crate::scalar_fn::ScalarFnVTableExt;
26use crate::scalar_fn::fns::between::Between;
27use crate::scalar_fn::fns::between::BetweenOptions;
28use crate::scalar_fn::fns::between::StrictComparison;
29use crate::scalar_fn::fns::binary::Binary;
30use crate::scalar_fn::fns::byte_length::ByteLength;
31use crate::scalar_fn::fns::case_when::CaseWhen;
32use crate::scalar_fn::fns::case_when::CaseWhenOptions;
33use crate::scalar_fn::fns::cast::Cast;
34use crate::scalar_fn::fns::dynamic::DynamicComparison;
35use crate::scalar_fn::fns::dynamic::DynamicComparisonExpr;
36use crate::scalar_fn::fns::dynamic::Rhs;
37use crate::scalar_fn::fns::dynamic_expression::DynamicExpression;
38use crate::scalar_fn::fns::dynamic_expression::DynamicExpressionOptions;
39use crate::scalar_fn::fns::dynamic_expression::DynamicExpressionSource;
40use crate::scalar_fn::fns::ext_storage::ExtStorage;
41use crate::scalar_fn::fns::fill_null::FillNull;
42use crate::scalar_fn::fns::get_item::GetItem;
43use crate::scalar_fn::fns::is_nan::IsNan;
44use crate::scalar_fn::fns::is_not_null::IsNotNull;
45use crate::scalar_fn::fns::is_null::IsNull;
46use crate::scalar_fn::fns::like::Like;
47use crate::scalar_fn::fns::like::LikeOptions;
48use crate::scalar_fn::fns::list_contains::ListContains;
49use crate::scalar_fn::fns::list_contains::ListContainsOptions;
50use crate::scalar_fn::fns::list_length::ListLength;
51use crate::scalar_fn::fns::list_sum::ListSum;
52use crate::scalar_fn::fns::list_transform::ListTransform;
53use crate::scalar_fn::fns::list_transform::ListTransformOptions;
54use crate::scalar_fn::fns::literal::Literal;
55use crate::scalar_fn::fns::mask::Mask;
56use crate::scalar_fn::fns::merge::DuplicateHandling;
57use crate::scalar_fn::fns::merge::Merge;
58use crate::scalar_fn::fns::not::Not;
59use crate::scalar_fn::fns::operators::CompareOperator;
60use crate::scalar_fn::fns::operators::Operator;
61use crate::scalar_fn::fns::pack::Pack;
62use crate::scalar_fn::fns::pack::PackOptions;
63use crate::scalar_fn::fns::replace_time_zone::ReplaceTimeZone;
64use crate::scalar_fn::fns::replace_time_zone::ReplaceTimeZoneOptions;
65use crate::scalar_fn::fns::select::FieldSelection;
66use crate::scalar_fn::fns::select::Select;
67use crate::scalar_fn::fns::variant_get::VariantGet;
68use crate::scalar_fn::fns::variant_get::VariantGetOptions;
69use crate::scalar_fn::fns::variant_get::VariantPath;
70use crate::scalar_fn::fns::zip::Zip;
71
72pub fn root() -> Expression {
77 Expression::Root
78}
79
80pub fn bound_root(dtype: DType) -> BoundExpression {
82 BoundExpression::new_root(dtype)
83}
84
85pub fn is_root(expr: &Expression) -> bool {
87 expr.is_root()
88}
89
90pub fn lit(value: impl Into<Scalar>) -> Expression {
110 Literal.new_expr(value.into(), [])
111}
112
113pub fn bound_lit(value: impl Into<Scalar>) -> BoundExpression {
115 Literal
116 .try_new_bound_expr(value.into(), [])
117 .vortex_expect("literal expressions are always well-typed")
118}
119
120pub fn col(field: impl Into<FieldName>) -> Expression {
131 GetItem.new_expr(field.into(), vec![root()])
132}
133
134pub fn bound_col(field: impl Into<FieldName>, scope: DType) -> BoundExpression {
136 bound_get_item(field, bound_root(scope))
137}
138
139pub fn get_item(field: impl Into<FieldName>, child: Expression) -> Expression {
148 GetItem.new_expr(field.into(), vec![child])
149}
150
151pub fn bound_get_item(field: impl Into<FieldName>, child: BoundExpression) -> BoundExpression {
153 GetItem
154 .try_new_bound_expr(field.into(), [child])
155 .vortex_expect("get-item expressions must reference a field in the child dtype")
156}
157
158pub fn var(name: impl Into<FieldName>) -> Expression {
173 Expression::Variable(name.into())
174}
175
176pub fn variant_get(
183 child: Expression,
184 path: impl Into<VariantPath>,
185 dtype: Option<DType>,
186) -> Expression {
187 VariantGet.new_expr(VariantGetOptions::new(path.into(), dtype), vec![child])
188}
189
190pub fn bound_variant_get(
192 child: BoundExpression,
193 path: impl Into<VariantPath>,
194 dtype: Option<DType>,
195) -> BoundExpression {
196 VariantGet
197 .try_new_bound_expr(VariantGetOptions::new(path.into(), dtype), [child])
198 .vortex_expect("variant-get expressions require a Variant child")
199}
200
201pub fn case_when(
205 condition: Expression,
206 then_value: Expression,
207 else_value: Expression,
208) -> Expression {
209 let options = CaseWhenOptions {
210 num_when_then_pairs: 1,
211 has_else: true,
212 };
213 CaseWhen.new_expr(options, [condition, then_value, else_value])
214}
215
216pub fn bound_case_when(
218 condition: BoundExpression,
219 then_value: BoundExpression,
220 else_value: BoundExpression,
221) -> BoundExpression {
222 let options = CaseWhenOptions {
223 num_when_then_pairs: 1,
224 has_else: true,
225 };
226 CaseWhen
227 .try_new_bound_expr(options, [condition, then_value, else_value])
228 .vortex_expect("case expressions must have boolean conditions and matching branch dtypes")
229}
230
231pub fn case_when_no_else(condition: Expression, then_value: Expression) -> Expression {
233 let options = CaseWhenOptions {
234 num_when_then_pairs: 1,
235 has_else: false,
236 };
237 CaseWhen.new_expr(options, [condition, then_value])
238}
239
240pub fn bound_case_when_no_else(
242 condition: BoundExpression,
243 then_value: BoundExpression,
244) -> BoundExpression {
245 let options = CaseWhenOptions {
246 num_when_then_pairs: 1,
247 has_else: false,
248 };
249 CaseWhen
250 .try_new_bound_expr(options, [condition, then_value])
251 .vortex_expect("case expressions must have boolean conditions")
252}
253
254pub fn nested_case_when(
256 when_then_pairs: Vec<(Expression, Expression)>,
257 else_value: Option<Expression>,
258) -> Expression {
259 assert!(
260 !when_then_pairs.is_empty(),
261 "nested_case_when requires at least one when/then pair"
262 );
263
264 let has_else = else_value.is_some();
265 let mut children = Vec::with_capacity(when_then_pairs.len() * 2 + usize::from(has_else));
266 for (condition, then_value) in &when_then_pairs {
267 children.push(condition.clone());
268 children.push(then_value.clone());
269 }
270 if let Some(else_expr) = else_value {
271 children.push(else_expr);
272 }
273
274 let Ok(num_when_then_pairs) = u32::try_from(when_then_pairs.len()) else {
275 vortex_panic!("nested_case_when has too many when/then pairs");
276 };
277 let options = CaseWhenOptions {
278 num_when_then_pairs,
279 has_else,
280 };
281 CaseWhen.new_expr(options, children)
282}
283
284pub fn bound_nested_case_when(
286 when_then_pairs: Vec<(BoundExpression, BoundExpression)>,
287 else_value: Option<BoundExpression>,
288) -> BoundExpression {
289 assert!(
290 !when_then_pairs.is_empty(),
291 "nested_case_when requires at least one when/then pair"
292 );
293
294 let Ok(num_when_then_pairs) = u32::try_from(when_then_pairs.len()) else {
295 vortex_panic!("nested_case_when has too many when/then pairs");
296 };
297 let has_else = else_value.is_some();
298 let mut children = Vec::with_capacity(when_then_pairs.len() * 2 + usize::from(has_else));
299 for (condition, then_value) in when_then_pairs {
300 children.push(condition);
301 children.push(then_value);
302 }
303 if let Some(else_expr) = else_value {
304 children.push(else_expr);
305 }
306
307 let options = CaseWhenOptions {
308 num_when_then_pairs,
309 has_else,
310 };
311 CaseWhen
312 .try_new_bound_expr(options, children)
313 .vortex_expect("case expressions must have boolean conditions and matching branch dtypes")
314}
315
316pub fn binary(operator: Operator, lhs: Expression, rhs: Expression) -> Expression {
320 Binary
321 .try_new_expr(operator, [lhs, rhs])
322 .vortex_expect("Failed to create binary expression")
323}
324
325pub fn bound_binary(
327 operator: Operator,
328 lhs: BoundExpression,
329 rhs: BoundExpression,
330) -> BoundExpression {
331 Binary
332 .try_new_bound_expr(operator, [lhs, rhs])
333 .vortex_expect("binary expressions must have compatible operand dtypes")
334}
335
336pub fn eq(lhs: Expression, rhs: Expression) -> Expression {
359 Binary
360 .try_new_expr(Operator::Eq, [lhs, rhs])
361 .vortex_expect("Failed to create Eq binary expression")
362}
363
364pub fn bound_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
366 bound_binary(Operator::Eq, lhs, rhs)
367}
368
369pub fn not_eq(lhs: Expression, rhs: Expression) -> Expression {
392 Binary
393 .try_new_expr(Operator::NotEq, [lhs, rhs])
394 .vortex_expect("Failed to create NotEq binary expression")
395}
396
397pub fn bound_not_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
399 bound_binary(Operator::NotEq, lhs, rhs)
400}
401
402pub fn gt_eq(lhs: Expression, rhs: Expression) -> Expression {
425 Binary
426 .try_new_expr(Operator::Gte, [lhs, rhs])
427 .vortex_expect("Failed to create Gte binary expression")
428}
429
430pub fn bound_gt_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
432 bound_binary(Operator::Gte, lhs, rhs)
433}
434
435pub fn gt(lhs: Expression, rhs: Expression) -> Expression {
458 Binary
459 .try_new_expr(Operator::Gt, [lhs, rhs])
460 .vortex_expect("Failed to create Gt binary expression")
461}
462
463pub fn bound_gt(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
465 bound_binary(Operator::Gt, lhs, rhs)
466}
467
468pub fn lt_eq(lhs: Expression, rhs: Expression) -> Expression {
491 Binary
492 .try_new_expr(Operator::Lte, [lhs, rhs])
493 .vortex_expect("Failed to create Lte binary expression")
494}
495
496pub fn bound_lt_eq(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
498 bound_binary(Operator::Lte, lhs, rhs)
499}
500
501pub fn lt(lhs: Expression, rhs: Expression) -> Expression {
524 Binary
525 .try_new_expr(Operator::Lt, [lhs, rhs])
526 .vortex_expect("Failed to create Lt binary expression")
527}
528
529pub fn bound_lt(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
531 bound_binary(Operator::Lt, lhs, rhs)
532}
533
534pub fn or(lhs: Expression, rhs: Expression) -> Expression {
555 Binary
556 .try_new_expr(Operator::Or, [lhs, rhs])
557 .vortex_expect("Failed to create Or binary expression")
558}
559
560pub fn bound_or(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
562 bound_binary(Operator::Or, lhs, rhs)
563}
564
565pub fn or_collect<I>(iter: I) -> Option<Expression>
572where
573 I: IntoIterator<Item = Expression>,
574{
575 iter.into_iter().reduce_balanced(or)
576}
577
578pub fn bound_or_collect<I>(iter: I) -> Option<BoundExpression>
580where
581 I: IntoIterator<Item = BoundExpression>,
582{
583 iter.into_iter().reduce_balanced(bound_or)
584}
585
586pub fn and(lhs: Expression, rhs: Expression) -> Expression {
607 Binary
608 .try_new_expr(Operator::And, [lhs, rhs])
609 .vortex_expect("Failed to create And binary expression")
610}
611
612pub fn bound_and(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
614 bound_binary(Operator::And, lhs, rhs)
615}
616
617pub fn and_collect<I>(iter: I) -> Option<Expression>
624where
625 I: IntoIterator<Item = Expression>,
626{
627 iter.into_iter().reduce_balanced(and)
628}
629
630pub fn bound_and_collect<I>(iter: I) -> Option<BoundExpression>
632where
633 I: IntoIterator<Item = BoundExpression>,
634{
635 iter.into_iter().reduce_balanced(bound_and)
636}
637
638pub fn checked_add(lhs: Expression, rhs: Expression) -> Expression {
658 Binary
659 .try_new_expr(Operator::Add, [lhs, rhs])
660 .vortex_expect("Failed to create Add binary expression")
661}
662
663pub fn bound_checked_add(lhs: BoundExpression, rhs: BoundExpression) -> BoundExpression {
665 bound_binary(Operator::Add, lhs, rhs)
666}
667
668pub fn not(operand: Expression) -> Expression {
679 Not.new_expr(EmptyOptions, vec![operand])
680}
681
682pub fn bound_not(operand: BoundExpression) -> BoundExpression {
684 Not.try_new_bound_expr(EmptyOptions, [operand])
685 .vortex_expect("not expressions require a boolean operand")
686}
687
688pub fn between(
706 arr: Expression,
707 lower: Expression,
708 upper: Expression,
709 options: BetweenOptions,
710) -> Expression {
711 Between
712 .try_new_expr(options, [arr, lower, upper])
713 .vortex_expect("Failed to create Between expression")
714}
715
716pub fn bound_between(
718 arr: BoundExpression,
719 lower: BoundExpression,
720 upper: BoundExpression,
721 options: BetweenOptions,
722) -> BoundExpression {
723 Between
724 .try_new_bound_expr(options, [arr, lower, upper])
725 .vortex_expect("between expressions require compatible operand dtypes")
726}
727
728pub fn select(field_names: impl Into<FieldNames>, child: Expression) -> Expression {
738 Select
739 .try_new_expr(FieldSelection::Include(field_names.into()), [child])
740 .vortex_expect("Failed to create Select expression")
741}
742
743pub fn bound_select(field_names: impl Into<FieldNames>, child: BoundExpression) -> BoundExpression {
745 Select
746 .try_new_bound_expr(FieldSelection::Include(field_names.into()), [child])
747 .vortex_expect("select expressions require fields from a struct child")
748}
749
750pub fn select_exclude(fields: impl Into<FieldNames>, child: Expression) -> Expression {
759 Select
760 .try_new_expr(FieldSelection::Exclude(fields.into()), [child])
761 .vortex_expect("Failed to create Select expression")
762}
763
764pub fn bound_select_exclude(
766 fields: impl Into<FieldNames>,
767 child: BoundExpression,
768) -> BoundExpression {
769 Select
770 .try_new_bound_expr(FieldSelection::Exclude(fields.into()), [child])
771 .vortex_expect("select expressions require fields from a struct child")
772}
773
774pub fn pack(
784 elements: impl IntoIterator<Item = (impl Into<FieldName>, Expression)>,
785 nullability: Nullability,
786) -> Expression {
787 let (names, values): (Vec<_>, Vec<_>) = elements
788 .into_iter()
789 .map(|(name, value)| (name.into(), value))
790 .unzip();
791 Pack.new_expr(
792 PackOptions {
793 names: names.into(),
794 nullability,
795 },
796 values,
797 )
798}
799
800pub fn bound_pack(
802 elements: impl IntoIterator<Item = (impl Into<FieldName>, BoundExpression)>,
803 nullability: Nullability,
804) -> BoundExpression {
805 let (names, values): (Vec<_>, Vec<_>) = elements
806 .into_iter()
807 .map(|(name, value)| (name.into(), value))
808 .unzip();
809 Pack.try_new_bound_expr(
810 PackOptions {
811 names: names.into(),
812 nullability,
813 },
814 values,
815 )
816 .vortex_expect("pack expressions must have one name per child")
817}
818
819pub fn cast(child: Expression, target: DType) -> Expression {
831 Cast.try_new_expr(target, [child])
832 .vortex_expect("Failed to create Cast expression")
833}
834
835pub fn bound_cast(child: BoundExpression, target: DType) -> BoundExpression {
837 Cast.try_new_bound_expr(target, [child])
838 .vortex_expect("cast expressions require a supported source and target dtype")
839}
840
841pub fn fill_null(child: Expression, fill_value: Expression) -> Expression {
850 FillNull.new_expr(EmptyOptions, [child, fill_value])
851}
852
853pub fn bound_fill_null(child: BoundExpression, fill_value: BoundExpression) -> BoundExpression {
855 FillNull
856 .try_new_bound_expr(EmptyOptions, [child, fill_value])
857 .vortex_expect("fill-null expressions require compatible child and fill dtypes")
858}
859
860pub fn is_null(child: Expression) -> Expression {
871 IsNull.new_expr(EmptyOptions, vec![child])
872}
873
874pub fn bound_is_null(child: BoundExpression) -> BoundExpression {
876 IsNull
877 .try_new_bound_expr(EmptyOptions, [child])
878 .vortex_expect("is-null expressions are always well-typed")
879}
880
881pub fn is_nan(child: Expression) -> Expression {
893 IsNan.new_expr(EmptyOptions, vec![child])
894}
895
896pub fn bound_is_nan(child: BoundExpression) -> BoundExpression {
898 IsNan
899 .try_new_bound_expr(EmptyOptions, [child])
900 .vortex_expect("is-nan expressions are always well-typed")
901}
902
903pub fn is_not_null(child: Expression) -> Expression {
914 IsNotNull.new_expr(EmptyOptions, vec![child])
915}
916
917pub fn bound_is_not_null(child: BoundExpression) -> BoundExpression {
919 IsNotNull
920 .try_new_bound_expr(EmptyOptions, [child])
921 .vortex_expect("is-not-null expressions are always well-typed")
922}
923
924pub fn like(child: Expression, pattern: Expression) -> Expression {
928 Like.new_expr(
929 LikeOptions {
930 negated: false,
931 case_insensitive: false,
932 },
933 [child, pattern],
934 )
935}
936
937pub fn bound_like(child: BoundExpression, pattern: BoundExpression) -> BoundExpression {
939 bound_like_with_options(child, pattern, false, false)
940}
941
942pub fn ilike(child: Expression, pattern: Expression) -> Expression {
944 Like.new_expr(
945 LikeOptions {
946 negated: false,
947 case_insensitive: true,
948 },
949 [child, pattern],
950 )
951}
952
953pub fn bound_ilike(child: BoundExpression, pattern: BoundExpression) -> BoundExpression {
955 bound_like_with_options(child, pattern, false, true)
956}
957
958pub fn not_like(child: Expression, pattern: Expression) -> Expression {
960 Like.new_expr(
961 LikeOptions {
962 negated: true,
963 case_insensitive: false,
964 },
965 [child, pattern],
966 )
967}
968
969pub fn bound_not_like(child: BoundExpression, pattern: BoundExpression) -> BoundExpression {
971 bound_like_with_options(child, pattern, true, false)
972}
973
974pub fn not_ilike(child: Expression, pattern: Expression) -> Expression {
976 Like.new_expr(
977 LikeOptions {
978 negated: true,
979 case_insensitive: true,
980 },
981 [child, pattern],
982 )
983}
984
985pub fn bound_not_ilike(child: BoundExpression, pattern: BoundExpression) -> BoundExpression {
987 bound_like_with_options(child, pattern, true, true)
988}
989
990fn bound_like_with_options(
991 child: BoundExpression,
992 pattern: BoundExpression,
993 negated: bool,
994 case_insensitive: bool,
995) -> BoundExpression {
996 Like.try_new_bound_expr(
997 LikeOptions {
998 negated,
999 case_insensitive,
1000 },
1001 [child, pattern],
1002 )
1003 .vortex_expect("like expressions require UTF-8 or binary operands")
1004}
1005
1006pub fn mask(array: Expression, mask: Expression) -> Expression {
1010 Mask.new_expr(EmptyOptions, [array, mask])
1011}
1012
1013pub fn bound_mask(array: BoundExpression, mask: BoundExpression) -> BoundExpression {
1015 Mask.try_new_bound_expr(EmptyOptions, [array, mask])
1016 .vortex_expect("mask expressions require a boolean mask")
1017}
1018
1019pub fn merge(elements: impl IntoIterator<Item = impl Into<Expression>>) -> Expression {
1032 use itertools::Itertools as _;
1033 let values = elements.into_iter().map(|value| value.into()).collect_vec();
1034 Merge.new_expr(DuplicateHandling::default(), values)
1035}
1036
1037pub fn bound_merge(elements: impl IntoIterator<Item = BoundExpression>) -> BoundExpression {
1039 bound_merge_opts(elements, DuplicateHandling::default())
1040}
1041
1042pub fn merge_opts(
1044 elements: impl IntoIterator<Item = impl Into<Expression>>,
1045 duplicate_handling: DuplicateHandling,
1046) -> Expression {
1047 use itertools::Itertools as _;
1048 let values = elements.into_iter().map(|value| value.into()).collect_vec();
1049 Merge.new_expr(duplicate_handling, values)
1050}
1051
1052pub fn bound_merge_opts(
1054 elements: impl IntoIterator<Item = BoundExpression>,
1055 duplicate_handling: DuplicateHandling,
1056) -> BoundExpression {
1057 Merge
1058 .try_new_bound_expr(duplicate_handling, elements)
1059 .vortex_expect("merge expressions require non-nullable struct children")
1060}
1061
1062pub fn zip_expr(mask: Expression, if_true: Expression, if_false: Expression) -> Expression {
1071 Zip.new_expr(EmptyOptions, [if_true, if_false, mask])
1072}
1073
1074pub fn bound_zip_expr(
1076 mask: BoundExpression,
1077 if_true: BoundExpression,
1078 if_false: BoundExpression,
1079) -> BoundExpression {
1080 Zip.try_new_bound_expr(EmptyOptions, [if_true, if_false, mask])
1081 .vortex_expect("zip expressions require a boolean mask and compatible value dtypes")
1082}
1083
1084pub fn dynamic_with_options(options: DynamicComparisonExpr, lhs: Expression) -> Expression {
1088 DynamicComparison.new_expr(options, [lhs])
1089}
1090
1091pub fn bound_dynamic_with_options(
1093 options: DynamicComparisonExpr,
1094 lhs: BoundExpression,
1095) -> BoundExpression {
1096 DynamicComparison
1097 .try_new_bound_expr(options, [lhs])
1098 .vortex_expect("dynamic comparisons require a compatible left-hand dtype")
1099}
1100
1101pub fn dynamic(
1103 operator: CompareOperator,
1104 rhs_value: impl Fn() -> Option<ScalarValue> + Send + Sync + 'static,
1105 rhs_dtype: DType,
1106 default: bool,
1107 lhs: Expression,
1108) -> Expression {
1109 dynamic_with_options(
1110 DynamicComparisonExpr {
1111 operator,
1112 rhs: Arc::new(Rhs {
1113 value: Arc::new(rhs_value),
1114 dtype: rhs_dtype,
1115 }),
1116 default,
1117 },
1118 lhs,
1119 )
1120}
1121
1122pub fn bound_dynamic(
1124 operator: CompareOperator,
1125 rhs_value: impl Fn() -> Option<ScalarValue> + Send + Sync + 'static,
1126 rhs_dtype: DType,
1127 default: bool,
1128 lhs: BoundExpression,
1129) -> BoundExpression {
1130 bound_dynamic_with_options(
1131 DynamicComparisonExpr {
1132 operator,
1133 rhs: Arc::new(Rhs {
1134 value: Arc::new(rhs_value),
1135 dtype: rhs_dtype,
1136 }),
1137 default,
1138 },
1139 lhs,
1140 )
1141}
1142
1143pub fn dynamic_expression(
1150 source: Arc<dyn DynamicExpressionSource>,
1151 inputs: impl IntoIterator<Item = Expression>,
1152) -> Expression {
1153 DynamicExpression.new_expr(DynamicExpressionOptions::new(source), inputs)
1154}
1155
1156pub fn bound_dynamic_expression(
1158 source: Arc<dyn DynamicExpressionSource>,
1159 inputs: impl IntoIterator<Item = BoundExpression>,
1160) -> BoundExpression {
1161 DynamicExpression
1162 .try_new_bound_expr(DynamicExpressionOptions::new(source), inputs)
1163 .vortex_expect("dynamic expressions accept any inputs")
1164}
1165
1166pub fn list_contains(list: Expression, value: Expression) -> Expression {
1178 list_contains_opts(list, value, ListContainsOptions::default())
1179}
1180
1181pub fn list_contains_opts(
1184 list: Expression,
1185 value: Expression,
1186 options: ListContainsOptions,
1187) -> Expression {
1188 ListContains.new_expr(options, [list, value])
1189}
1190
1191pub fn in_list(needle: Expression, list: Expression) -> Expression {
1200 list_contains_opts(
1201 list,
1202 needle,
1203 ListContainsOptions {
1204 sql_null_semantics: true,
1205 },
1206 )
1207}
1208
1209pub fn in_list_of(
1232 needle: Expression,
1233 element_dtype: DType,
1234 elements: Vec<Scalar>,
1235) -> VortexResult<Expression> {
1236 let element_dtype = if elements.iter().any(Scalar::is_null) {
1237 element_dtype.as_nullable()
1238 } else {
1239 element_dtype.as_nonnullable()
1240 };
1241 let mut elements = elements
1242 .iter()
1243 .map(|element| {
1244 vortex_ensure!(
1245 element.dtype() == &DType::Null
1246 || element.dtype().eq_ignore_nullability(&element_dtype),
1247 "IN list element {element} is not of type {element_dtype}"
1248 );
1249 element.cast(&element_dtype)
1250 })
1251 .collect::<VortexResult<Vec<_>>>()?;
1252
1253 if let Some((low, high)) = contiguous_integer_bounds(&elements) {
1254 let options = BetweenOptions {
1255 lower_strict: StrictComparison::NonStrict,
1256 upper_strict: StrictComparison::NonStrict,
1257 };
1258 return Ok(between(
1259 needle,
1260 lit(low.clone()),
1261 lit(high.clone()),
1262 options,
1263 ));
1264 }
1265
1266 if elements
1270 .windows(2)
1271 .all(|pair| pair[0].partial_cmp(&pair[1]).is_some())
1272 {
1273 elements.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
1274 elements.dedup();
1275 }
1276 let list = Scalar::list(Arc::new(element_dtype), elements, Nullability::NonNullable);
1277 Ok(in_list(needle, lit(list)))
1278}
1279
1280fn contiguous_integer_bounds(scalars: &[Scalar]) -> Option<(&Scalar, &Scalar)> {
1283 let mut values = scalars
1284 .iter()
1285 .map(|scalar| {
1286 if !matches!(scalar.dtype(), DType::Primitive(ptype, _) if ptype.is_int()) {
1288 return None;
1289 }
1290 let primitive = scalar.as_primitive_opt()?;
1291 let value = if primitive.ptype().is_unsigned_int() {
1292 primitive.as_opt::<u64>()??.into()
1293 } else {
1294 i128::from(primitive.as_opt::<i64>()??)
1295 };
1296 Some((value, scalar))
1297 })
1298 .collect::<Option<Vec<(i128, &Scalar)>>>()?;
1299 values.sort_unstable_by_key(|(value, _)| *value);
1300 values.dedup_by_key(|(value, _)| *value);
1301 let (low, low_scalar) = values.first()?;
1302 let (high, high_scalar) = values.last()?;
1303 (high - low + 1 == i128::try_from(values.len()).ok()?).then_some((*low_scalar, *high_scalar))
1304}
1305
1306pub fn bound_list_contains(list: BoundExpression, value: BoundExpression) -> BoundExpression {
1308 ListContains
1309 .try_new_bound_expr(ListContainsOptions::default(), [list, value])
1310 .vortex_expect("list-contains expressions require a list child")
1311}
1312
1313pub fn bound_in_list(needle: BoundExpression, list: BoundExpression) -> BoundExpression {
1315 ListContains
1316 .try_new_bound_expr(
1317 ListContainsOptions {
1318 sql_null_semantics: true,
1319 },
1320 [list, needle],
1321 )
1322 .vortex_expect("list-contains expressions require a list child")
1323}
1324
1325pub fn byte_length(input: Expression) -> Expression {
1335 ByteLength.new_expr(EmptyOptions, [input])
1336}
1337
1338pub fn bound_byte_length(input: BoundExpression) -> BoundExpression {
1340 ByteLength
1341 .try_new_bound_expr(EmptyOptions, [input])
1342 .vortex_expect("byte-length expressions require a variable-length binary child")
1343}
1344
1345pub fn ext_storage(input: Expression) -> Expression {
1354 ExtStorage.new_expr(EmptyOptions, [input])
1355}
1356
1357pub fn bound_ext_storage(input: BoundExpression) -> BoundExpression {
1359 ExtStorage
1360 .try_new_bound_expr(EmptyOptions, [input])
1361 .vortex_expect("extension-storage expressions require an extension child")
1362}
1363
1364pub fn list_length(input: Expression) -> Expression {
1375 ListLength.new_expr(EmptyOptions, [input])
1376}
1377
1378pub fn bound_list_length(input: BoundExpression) -> BoundExpression {
1380 ListLength
1381 .try_new_bound_expr(EmptyOptions, [input])
1382 .vortex_expect("list-length expressions require a list child")
1383}
1384
1385pub fn list_sum(input: Expression) -> Expression {
1400 ListSum.new_expr(NumericalAggregateOpts::default(), [input])
1401}
1402
1403pub fn bound_list_sum(input: BoundExpression) -> BoundExpression {
1405 ListSum
1406 .try_new_bound_expr(NumericalAggregateOpts::default(), [input])
1407 .vortex_expect("list-sum expressions require a numeric list child")
1408}
1409
1410pub fn list_sum_opts(input: Expression, options: NumericalAggregateOpts) -> Expression {
1413 ListSum.new_expr(options, [input])
1414}
1415
1416pub fn bound_list_sum_opts(
1418 input: BoundExpression,
1419 options: NumericalAggregateOpts,
1420) -> BoundExpression {
1421 ListSum
1422 .try_new_bound_expr(options, [input])
1423 .vortex_expect("list-sum expressions require a numeric list child")
1424}
1425
1426pub fn list_transform(list: Expression, lambda: Lambda) -> Expression {
1439 ListTransform.new_expr(ListTransformOptions::Lambda(lambda), [list])
1440}
1441
1442pub fn replace_time_zone(
1447 input: Expression,
1448 ambiguous: Expression,
1449 options: ReplaceTimeZoneOptions,
1450) -> Expression {
1451 ReplaceTimeZone.new_expr(options, [input, ambiguous])
1452}
1453
1454pub fn bound_replace_time_zone(
1456 input: BoundExpression,
1457 ambiguous: BoundExpression,
1458 options: ReplaceTimeZoneOptions,
1459) -> BoundExpression {
1460 ReplaceTimeZone
1461 .try_new_bound_expr(options, [input, ambiguous])
1462 .vortex_expect("timezone replacement requires a timestamp and a UTF-8 ambiguity policy")
1463}
1464
1465pub mod bound {
1471 pub use super::bound_and as and;
1472 pub use super::bound_and_collect as and_collect;
1473 pub use super::bound_between as between;
1474 pub use super::bound_binary as binary;
1475 pub use super::bound_byte_length as byte_length;
1476 pub use super::bound_case_when as case_when;
1477 pub use super::bound_case_when_no_else as case_when_no_else;
1478 pub use super::bound_cast as cast;
1479 pub use super::bound_checked_add as checked_add;
1480 pub use super::bound_col as col;
1481 pub use super::bound_dynamic as dynamic;
1482 pub use super::bound_dynamic_expression as dynamic_expression;
1483 pub use super::bound_dynamic_with_options as dynamic_with_options;
1484 pub use super::bound_eq as eq;
1485 pub use super::bound_ext_storage as ext_storage;
1486 pub use super::bound_fill_null as fill_null;
1487 pub use super::bound_get_item as get_item;
1488 pub use super::bound_gt as gt;
1489 pub use super::bound_gt_eq as gt_eq;
1490 pub use super::bound_ilike as ilike;
1491 pub use super::bound_in_list as in_list;
1492 pub use super::bound_is_nan as is_nan;
1493 pub use super::bound_is_not_null as is_not_null;
1494 pub use super::bound_is_null as is_null;
1495 pub use super::bound_like as like;
1496 pub use super::bound_list_contains as list_contains;
1497 pub use super::bound_list_length as list_length;
1498 pub use super::bound_list_sum as list_sum;
1499 pub use super::bound_list_sum_opts as list_sum_opts;
1500 pub use super::bound_lit as lit;
1501 pub use super::bound_lt as lt;
1502 pub use super::bound_lt_eq as lt_eq;
1503 pub use super::bound_mask as mask;
1504 pub use super::bound_merge as merge;
1505 pub use super::bound_merge_opts as merge_opts;
1506 pub use super::bound_nested_case_when as nested_case_when;
1507 pub use super::bound_not as not;
1508 pub use super::bound_not_eq as not_eq;
1509 pub use super::bound_not_ilike as not_ilike;
1510 pub use super::bound_not_like as not_like;
1511 pub use super::bound_or as or;
1512 pub use super::bound_or_collect as or_collect;
1513 pub use super::bound_pack as pack;
1514 pub use super::bound_replace_time_zone as replace_time_zone;
1515 pub use super::bound_root as root;
1516 pub use super::bound_select as select;
1517 pub use super::bound_select_exclude as select_exclude;
1518 pub use super::bound_variant_get as variant_get;
1519 pub use super::bound_zip_expr as zip_expr;
1520}