1use std::fmt::Display;
11use std::fmt::Formatter;
12
13#[expect(deprecated)]
14pub use boolean::and_kleene;
15#[expect(deprecated)]
16pub use boolean::or_kleene;
17use prost::Message;
18use vortex_error::VortexResult;
19use vortex_error::vortex_bail;
20use vortex_session::VortexSession;
21use vortex_session::registry::CachedId;
22
23use crate::AnyColumnar;
24use crate::ArrayRef;
25use crate::ExecutionCtx;
26use crate::arrays::Constant;
27use crate::arrays::ScalarFnArray;
28use crate::dtype::DType;
29use crate::dtype::Nullability;
30use crate::expr::BoundExpression;
31use crate::expr::bound;
32use crate::expr::display::ExprDisplay;
33use crate::matcher::Matcher;
34use crate::proto::expr as pb;
35use crate::require_input;
36use crate::scalar_fn::Arity;
37use crate::scalar_fn::ChildName;
38use crate::scalar_fn::ExecutionArgs;
39use crate::scalar_fn::ReduceNode;
40use crate::scalar_fn::ScalarFnExecutionResult;
41use crate::scalar_fn::ScalarFnId;
42use crate::scalar_fn::ScalarFnVTable;
43use crate::scalar_fn::ScalarFnVTableExt;
44use crate::scalar_fn::fns::literal::Literal;
45use crate::scalar_fn::fns::operators::CompareOperator;
46use crate::scalar_fn::fns::operators::Operator;
47
48pub mod boolean;
49pub use boolean::BooleanExecuteAdaptor;
50pub use boolean::BooleanKernel;
51pub(crate) use boolean::execute_boolean;
52pub use boolean::kleene_boolean_buffer_scalar;
53pub use boolean::kleene_boolean_buffers;
54mod compare;
55pub use compare::*;
56mod numeric;
57pub(crate) use numeric::*;
58mod primitive_operand;
59
60use crate::scalar::NumericOperator;
61use crate::scalar::Scalar;
62
63#[derive(Clone)]
65pub struct Binary;
66
67impl Binary {
68 pub fn try_new(
77 lhs: ArrayRef,
78 rhs: ArrayRef,
79 operator: Operator,
80 ) -> VortexResult<ScalarFnArray> {
81 ScalarFnArray::try_new(Binary.bind(operator), [lhs, rhs])
82 }
83}
84
85const KLEENE_LUT: [[[bool; 2]; 2]; 2] = [
88 [[false, true], [true, true]], [[false, false], [false, true]], ];
91
92impl ScalarFnVTable for Binary {
93 type Options = Operator;
94
95 fn id(&self) -> ScalarFnId {
96 static ID: CachedId = CachedId::new("vortex.binary");
97 *ID
98 }
99
100 fn serialize(&self, instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
101 Ok(Some(
102 pb::BinaryOpts {
103 op: (*instance).into(),
104 }
105 .encode_to_vec(),
106 ))
107 }
108
109 fn deserialize(
110 &self,
111 _metadata: &[u8],
112 _session: &VortexSession,
113 ) -> VortexResult<Self::Options> {
114 let opts = pb::BinaryOpts::decode(_metadata)?;
115 Operator::try_from(opts.op)
116 }
117
118 fn arity(&self, _options: &Self::Options) -> Arity {
119 Arity::Exact(2)
120 }
121
122 fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
123 match child_idx {
124 0 => ChildName::from("lhs"),
125 1 => ChildName::from("rhs"),
126 _ => unreachable!("Binary has only two children"),
127 }
128 }
129
130 fn fmt_sql(
131 &self,
132 operator: &Operator,
133 expr: &dyn ExprDisplay,
134 f: &mut Formatter<'_>,
135 ) -> std::fmt::Result {
136 write!(f, "(")?;
137 Display::fmt(expr.display_child(0), f)?;
138 write!(f, " {} ", operator)?;
139 Display::fmt(expr.display_child(1), f)?;
140 write!(f, ")")
141 }
142
143 fn return_dtype(&self, operator: &Operator, arg_dtypes: &[DType]) -> VortexResult<DType> {
144 let lhs = &arg_dtypes[0];
145 let rhs = &arg_dtypes[1];
146
147 if operator.is_arithmetic() {
148 if lhs.is_primitive() && lhs.eq_ignore_nullability(rhs) {
149 return Ok(lhs.with_nullability(lhs.nullability() | rhs.nullability()));
150 }
151
152 if let DType::Decimal(decimal_dtype, _) = lhs
153 && lhs.eq_ignore_nullability(rhs)
154 {
155 let numeric_op = NumericOperator::try_from(*operator)?;
156 return Ok(DType::Decimal(
157 numeric_op_result_decimal_dtype(*decimal_dtype, numeric_op)?,
158 lhs.nullability() | rhs.nullability(),
159 ));
160 }
161 vortex_bail!(
162 "incompatible types for arithmetic operation: {} {}",
163 lhs,
164 rhs
165 );
166 }
167
168 if operator.is_comparison() {
169 validate_comparison_dtypes(lhs, rhs)?;
170 }
171
172 if matches!(operator, Operator::And | Operator::Or)
173 && !(lhs.is_boolean() && rhs.is_boolean())
174 {
175 vortex_bail!("'{operator}' requires bool operands, got {lhs} and {rhs}",);
176 }
177
178 Ok(DType::Bool((lhs.is_nullable() || rhs.is_nullable()).into()))
179 }
180
181 fn execute(
182 &self,
183 op: &Operator,
184 args: &dyn ExecutionArgs,
185 ctx: &mut ExecutionCtx,
186 ) -> VortexResult<ArrayRef> {
187 let lhs = args.get(0)?;
188 let rhs = args.get(1)?;
189
190 match op {
191 Operator::Eq => execute_compare(&lhs, &rhs, CompareOperator::Eq, ctx),
192 Operator::NotEq => execute_compare(&lhs, &rhs, CompareOperator::NotEq, ctx),
193 Operator::Lt => execute_compare(&lhs, &rhs, CompareOperator::Lt, ctx),
194 Operator::Lte => execute_compare(&lhs, &rhs, CompareOperator::Lte, ctx),
195 Operator::Gt => execute_compare(&lhs, &rhs, CompareOperator::Gt, ctx),
196 Operator::Gte => execute_compare(&lhs, &rhs, CompareOperator::Gte, ctx),
197 Operator::And => execute_boolean(lhs, rhs, Operator::And, ctx),
198 Operator::Or => execute_boolean(lhs, rhs, Operator::Or, ctx),
199 Operator::Add => execute_numeric(&lhs, &rhs, NumericOperator::Add, ctx),
200 Operator::Sub => execute_numeric(&lhs, &rhs, NumericOperator::Sub, ctx),
201 Operator::Mul => execute_numeric(&lhs, &rhs, NumericOperator::Mul, ctx),
202 Operator::Div => execute_numeric(&lhs, &rhs, NumericOperator::Div, ctx),
203 }
204 }
205
206 fn simplify(
207 &self,
208 operator: &Operator,
209 expr: &BoundExpression,
210 ) -> VortexResult<Option<BoundExpression>> {
211 let lhs = expr.child(0);
212 let rhs = expr.child(1);
213
214 let is_literal_null =
215 |expr: &BoundExpression| expr.as_opt::<Literal>().is_some_and(Scalar::is_null);
216
217 if operator.is_comparison() && (is_literal_null(lhs) || is_literal_null(rhs)) {
220 return Ok(Some(bound::lit(Scalar::null(DType::Bool(
221 Nullability::Nullable,
222 )))));
223 }
224
225 let bool_literal = |expr: &BoundExpression| {
226 expr.as_opt::<Literal>()?
227 .as_bool_opt()
228 .map(|value| value.value())
229 };
230
231 let result_nullable = lhs.dtype().is_nullable() || rhs.dtype().is_nullable();
249 let nullability = Nullability::from(result_nullable);
250 let bool_lit = |value: bool| bound::lit(Scalar::bool(value, nullability));
251 let widen = |child: &BoundExpression| {
252 if child.dtype().is_nullable() == result_nullable {
253 child.clone()
254 } else {
255 bound::cast(child.clone(), DType::Bool(nullability))
256 }
257 };
258
259 Ok(match operator {
260 Operator::And => match (bool_literal(lhs), bool_literal(rhs)) {
261 (Some(Some(false)), _) | (_, Some(Some(false))) => Some(bool_lit(false)),
262 (Some(Some(true)), _) => Some(widen(rhs)),
263 (_, Some(Some(true))) => Some(widen(lhs)),
264 (Some(None), Some(None)) => Some(widen(lhs)),
265 _ => None,
266 },
267 Operator::Or => match (bool_literal(lhs), bool_literal(rhs)) {
268 (Some(Some(true)), _) | (_, Some(Some(true))) => Some(bool_lit(true)),
269 (Some(Some(false)), _) => Some(widen(rhs)),
270 (_, Some(Some(false))) => Some(widen(lhs)),
271 (Some(None), Some(None)) => Some(widen(lhs)),
272 _ => None,
273 },
274 _ => None,
275 })
276 }
277
278 fn reduce<T: ReduceNode>(&self, operator: &Operator, node: &T) -> VortexResult<Option<T>> {
279 if !matches!(operator, Operator::And | Operator::Or) {
280 return Ok(None);
281 }
282 let left = node.child(0);
283 let right = node.child(1);
284
285 let left_const = left.as_constant();
286 let right_const = right.as_constant();
287
288 let left_const = left_const
292 .and_then(|s| s.value().cloned())
293 .map(|v| v.as_bool());
294 let right_const = right_const
295 .and_then(|s| s.value().cloned())
296 .map(|v| v.as_bool());
297 let is_and = *operator == Operator::And;
298 let nullability = node.node_dtype()?.nullability();
299
300 let (constant, other) = match (left_const, right_const) {
301 (None, None) => return Ok(None),
302 (Some(left_const), Some(right_const)) => {
303 return Ok(Some(left.new_constant(Scalar::bool(
304 KLEENE_LUT[is_and as usize][left_const as usize][right_const as usize],
305 nullability,
306 ))));
307 }
308 (Some(constant), None) => (constant, right),
309 (None, Some(constant)) => (constant, left),
310 };
311
312 if constant == is_and {
315 return Ok((other.node_dtype()?.nullability() == nullability).then_some(other));
316 }
317 Ok(Some(
319 other.new_constant(Scalar::bool(constant, nullability)),
320 ))
321 }
322
323 fn execute_step(
324 &self,
325 op: &Operator,
326 args: &dyn ExecutionArgs,
327 ctx: &mut ExecutionCtx,
328 ) -> VortexResult<ScalarFnExecutionResult> {
329 let columnar = |idx| args.input_matches(idx, AnyColumnar::matches);
330 let ready = (columnar(0)? && columnar(1)?)
331 || match op {
334 Operator::And | Operator::Or => {
335 let constant = |idx| args.input_matches(idx, |array| array.is::<Constant>());
336 constant(0)? || constant(1)?
337 }
338 _ => {
339 let null = |idx| {
340 args.input_matches(idx, |array| {
341 array.as_constant().is_some_and(|scalar| scalar.is_null())
342 })
343 };
344 null(0)? || null(1)?
345 }
346 };
347 if !ready {
348 require_input!(args, 0 => AnyColumnar);
349 require_input!(args, 1 => AnyColumnar);
350 }
351 self.execute(op, args, ctx)
352 .map(ScalarFnExecutionResult::done)
353 }
354
355 fn is_strict(&self, operator: &Operator) -> bool {
356 !matches!(operator, Operator::And | Operator::Or)
357 }
358
359 fn is_infallible(&self, operator: &Operator) -> bool {
360 matches!(
362 operator,
363 Operator::Eq
364 | Operator::NotEq
365 | Operator::Gt
366 | Operator::Gte
367 | Operator::Lt
368 | Operator::Lte
369 | Operator::And
370 | Operator::Or
371 )
372 }
373}
374
375#[cfg(test)]
376mod tests {
377 use std::sync::LazyLock;
378
379 use vortex_error::VortexExpect;
380 use vortex_error::VortexResult;
381 use vortex_session::VortexSession;
382
383 use super::*;
384 use crate::Canonical;
385 use crate::IntoArray;
386 use crate::VortexSessionExecute;
387 use crate::arrays::Bool;
388 use crate::arrays::BoolArray;
389 use crate::arrays::Constant;
390 use crate::arrays::ConstantArray;
391 use crate::arrays::PrimitiveArray;
392 use crate::arrays::ScalarFn;
393 use crate::arrays::scalar_fn::ExactScalarFn;
394 use crate::arrays::scalar_fn::ScalarFnArrayExt;
395 use crate::assert_arrays_eq;
396 use crate::builtins::ArrayBuiltins;
397 use crate::dtype::DType;
398 use crate::dtype::Nullability;
399 use crate::dtype::PType;
400 use crate::expr::Expression;
401 use crate::expr::and;
402 use crate::expr::and_collect;
403 use crate::expr::col;
404 use crate::expr::eq;
405 use crate::expr::gt;
406 use crate::expr::gt_eq;
407 use crate::expr::lit;
408 use crate::expr::lt;
409 use crate::expr::lt_eq;
410 use crate::expr::not_eq;
411 use crate::expr::or;
412 use crate::expr::or_collect;
413 use crate::expr::test_harness;
414 use crate::optimizer::ArrayOptimizer;
415 use crate::scalar::Scalar;
416
417 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
418
419 #[test]
420 fn and_collect_balanced() {
421 let values = vec![lit(1), lit(2), lit(3), lit(4), lit(5)];
422
423 insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @r"
424 vortex.binary(and)
425 ├── lhs: vortex.binary(and)
426 │ ├── lhs: vortex.literal(1i32)
427 │ └── rhs: vortex.literal(2i32)
428 └── rhs: vortex.binary(and)
429 ├── lhs: vortex.binary(and)
430 │ ├── lhs: vortex.literal(3i32)
431 │ └── rhs: vortex.literal(4i32)
432 └── rhs: vortex.literal(5i32)
433 ");
434
435 let values = vec![lit(1), lit(2), lit(3), lit(4)];
437 insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @r"
438 vortex.binary(and)
439 ├── lhs: vortex.binary(and)
440 │ ├── lhs: vortex.literal(1i32)
441 │ └── rhs: vortex.literal(2i32)
442 └── rhs: vortex.binary(and)
443 ├── lhs: vortex.literal(3i32)
444 └── rhs: vortex.literal(4i32)
445 ");
446
447 let values = vec![lit(1)];
449 insta::assert_snapshot!(and_collect(values.into_iter()).unwrap().display_tree(), @"vortex.literal(1i32)");
450
451 let values: Vec<Expression> = vec![];
453 assert!(and_collect(values.into_iter()).is_none());
454 }
455
456 #[test]
457 fn or_collect_balanced() {
458 let values = vec![lit(1), lit(2), lit(3), lit(4)];
460 insta::assert_snapshot!(or_collect(values.into_iter()).unwrap().display_tree(), @r"
461 vortex.binary(or)
462 ├── lhs: vortex.binary(or)
463 │ ├── lhs: vortex.literal(1i32)
464 │ └── rhs: vortex.literal(2i32)
465 └── rhs: vortex.binary(or)
466 ├── lhs: vortex.literal(3i32)
467 └── rhs: vortex.literal(4i32)
468 ");
469 }
470
471 #[test]
472 fn dtype() {
473 let dtype = test_harness::struct_dtype();
474 let bool1: Expression = col("bool1");
475 let bool2: Expression = col("bool2");
476 assert_eq!(
477 and(bool1.clone(), bool2.clone())
478 .return_dtype(&dtype)
479 .unwrap(),
480 DType::Bool(Nullability::NonNullable)
481 );
482 assert_eq!(
483 or(bool1, bool2).return_dtype(&dtype).unwrap(),
484 DType::Bool(Nullability::NonNullable)
485 );
486
487 let col1: Expression = col("col1");
488 let col2: Expression = col("col2");
489
490 assert_eq!(
491 eq(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(),
492 DType::Bool(Nullability::Nullable)
493 );
494 assert_eq!(
495 not_eq(col1.clone(), col2.clone())
496 .return_dtype(&dtype)
497 .unwrap(),
498 DType::Bool(Nullability::Nullable)
499 );
500 assert_eq!(
501 gt(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(),
502 DType::Bool(Nullability::Nullable)
503 );
504 assert_eq!(
505 gt_eq(col1.clone(), col2.clone())
506 .return_dtype(&dtype)
507 .unwrap(),
508 DType::Bool(Nullability::Nullable)
509 );
510 assert_eq!(
511 lt(col1.clone(), col2.clone()).return_dtype(&dtype).unwrap(),
512 DType::Bool(Nullability::Nullable)
513 );
514 assert_eq!(
515 lt_eq(col1.clone(), col2.clone())
516 .return_dtype(&dtype)
517 .unwrap(),
518 DType::Bool(Nullability::Nullable)
519 );
520
521 assert_eq!(
522 or(lt(col1.clone(), col2.clone()), not_eq(col1, col2))
523 .return_dtype(&dtype)
524 .unwrap(),
525 DType::Bool(Nullability::Nullable)
526 );
527 }
528
529 #[test]
530 fn comparison_with_typed_null_simplifies_after_type_check() -> VortexResult<()> {
531 let dtype = test_harness::struct_dtype();
532
533 let expr = eq(
534 col("col1"),
535 lit(Scalar::null(DType::Primitive(
536 PType::U16,
537 Nullability::Nullable,
538 ))),
539 );
540
541 assert_eq!(
542 expr.bind(&dtype)?.optimize_recursive()?,
543 lit(Scalar::null(DType::Bool(Nullability::Nullable))).bind(&dtype)?
544 );
545 Ok(())
546 }
547
548 #[test]
549 fn comparison_with_incompatible_null_still_type_checks() {
550 let dtype = test_harness::struct_dtype();
551 let expr = eq(
552 col("col1"),
553 lit(Scalar::null(DType::Utf8(Nullability::Nullable))),
554 );
555
556 assert!(expr.bind(&dtype).is_err());
557 }
558
559 #[test]
560 fn test_display_print() {
561 let expr = gt(lit(1), lit(2));
562 assert_eq!(format!("{expr}"), "(1i32 > 2i32)");
563 }
564
565 #[test]
568 fn test_struct_comparison() {
569 use crate::IntoArray;
570 use crate::arrays::StructArray;
571
572 let lhs_struct = StructArray::from_fields(&[
574 ("a", PrimitiveArray::from_iter([1i32]).into_array()),
575 ("b", PrimitiveArray::from_iter([3i32]).into_array()),
576 ])
577 .unwrap()
578 .into_array();
579
580 let rhs_struct_equal = StructArray::from_fields(&[
581 ("a", PrimitiveArray::from_iter([1i32]).into_array()),
582 ("b", PrimitiveArray::from_iter([3i32]).into_array()),
583 ])
584 .unwrap()
585 .into_array();
586
587 let rhs_struct_different = StructArray::from_fields(&[
588 ("a", PrimitiveArray::from_iter([1i32]).into_array()),
589 ("b", PrimitiveArray::from_iter([4i32]).into_array()),
590 ])
591 .unwrap()
592 .into_array();
593
594 let result_equal = lhs_struct.binary(rhs_struct_equal, Operator::Eq).unwrap();
596 assert_eq!(
597 result_equal
598 .execute_scalar(0, &mut SESSION.create_execution_ctx())
599 .vortex_expect("value"),
600 Scalar::bool(true, Nullability::NonNullable),
601 "Equal structs should be equal"
602 );
603
604 let result_different = lhs_struct
605 .binary(rhs_struct_different, Operator::Eq)
606 .unwrap();
607 assert_eq!(
608 result_different
609 .execute_scalar(0, &mut SESSION.create_execution_ctx())
610 .vortex_expect("value"),
611 Scalar::bool(false, Nullability::NonNullable),
612 "Different structs should not be equal"
613 );
614 }
615
616 #[test]
617 fn test_or_kleene_validity() {
618 let mut ctx = SESSION.create_execution_ctx();
619 use crate::arrays::StructArray;
620 use crate::expr::col;
621
622 let struct_arr = StructArray::from_fields(&[
623 ("a", BoolArray::from_iter([Some(true)]).into_array()),
624 (
625 "b",
626 BoolArray::from_iter([Option::<bool>::None]).into_array(),
627 ),
628 ])
629 .unwrap()
630 .into_array();
631
632 let expr = or(col("a"), col("b"));
633 let result = struct_arr.apply(&expr).unwrap();
634
635 assert_arrays_eq!(
636 result,
637 BoolArray::from_iter([Some(true)]).into_array(),
638 &mut ctx
639 )
640 }
641
642 #[test]
643 fn test_and_reduce() -> VortexResult<()> {
644 let mut ctx = SESSION.create_execution_ctx();
645 let left = BoolArray::from_iter([true, true, false]).into_array();
646 let right = ConstantArray::new(true, 3).into_array();
647
648 let array = Binary::try_new(left.clone(), right.clone(), Operator::And)?
649 .into_array()
650 .optimize()?; assert!(array.is::<Bool>()); assert_arrays_eq!(array, left, &mut ctx);
653
654 let array = Binary::try_new(right.clone(), left.clone(), Operator::And)?
655 .into_array()
656 .optimize()?;
657 assert!(array.is::<Bool>()); assert_arrays_eq!(array, left, &mut ctx);
659
660 let array = Binary::try_new(left.clone(), right.clone(), Operator::Or)?
661 .into_array()
662 .optimize()?;
663 assert_eq!(array.as_constant(), Some(&true.into())); let array = Binary::try_new(right.clone(), left, Operator::Or)?
666 .into_array()
667 .optimize()?;
668 assert_eq!(array.as_constant(), Some(&true.into())); let array = Binary::try_new(right.clone(), right.clone(), Operator::Or)?
671 .into_array()
672 .optimize()?;
673 assert_eq!(array.as_constant(), Some(&true.into())); let left_false = ConstantArray::new(false, 3).into_array();
676
677 let array = Binary::try_new(right.clone(), left_false.clone(), Operator::Or)?
678 .into_array()
679 .optimize()?;
680 assert_eq!(array.as_constant(), Some(&true.into()));
681 let array = Binary::try_new(left_false, right, Operator::And)?
682 .into_array()
683 .optimize()?;
684 assert_eq!(array.as_constant(), Some(&false.into()));
685
686 Ok(())
687 }
688
689 #[test]
690 fn test_and_reduce_nullable() -> VortexResult<()> {
691 let mut ctx = SESSION.create_execution_ctx();
692 let nullable_true =
693 ConstantArray::new(Scalar::bool(true, Nullability::Nullable), 3).into_array();
694 let right = BoolArray::from_iter([true, false, true]).into_array();
695
696 let array = Binary::try_new(nullable_true, right, Operator::And)?
697 .into_array()
698 .optimize()?;
699 assert_arrays_eq!(
700 array,
701 BoolArray::from_iter([Some(true), Some(false), Some(true)]),
702 &mut ctx
703 );
704 Ok(())
705 }
706
707 #[test]
710 fn test_short_circuit_keeps_nullability() -> VortexResult<()> {
711 let nullable = BoolArray::from_iter([Some(true), None, Some(false)]).into_array();
712 let non_nullable = BoolArray::from_iter([true, false, true]).into_array();
713 let false_ = ConstantArray::new(false, 3).into_array();
714 let nullable_false =
715 ConstantArray::new(Scalar::bool(false, Nullability::Nullable), 3).into_array();
716
717 let and_false = Binary::try_new(nullable, false_, Operator::And)?
718 .into_array()
719 .optimize()?;
720 assert_eq!(and_false.dtype(), &DType::Bool(Nullability::Nullable));
721 assert!(and_false.is::<Constant>());
722
723 let or_false = Binary::try_new(non_nullable, nullable_false, Operator::Or)?
724 .into_array()
725 .optimize()?;
726 assert_eq!(or_false.dtype(), &DType::Bool(Nullability::Nullable));
727 assert_arrays_eq!(
728 or_false,
729 BoolArray::from_iter([Some(true), Some(false), Some(true)]),
730 &mut SESSION.create_execution_ctx()
731 );
732 Ok(())
733 }
734
735 #[test]
736 fn test_and_reject_non_bool() {
737 let lhs = ConstantArray::new(7i32, 3).into_array();
738 let rhs = PrimitiveArray::from_iter([1i32, 2, 3]).into_array();
739 assert!(Binary::try_new(lhs, rhs, Operator::And).is_err());
740 }
741
742 #[test]
743 fn test_isnull_and_reduce() -> VortexResult<()> {
744 let mut ctx = SESSION.create_execution_ctx();
745 let left = BoolArray::from_iter([Some(true), Some(true), None]).into_array();
746 let right = BoolArray::from_iter([true, false, true]).into_array();
747
748 for (lhs, rhs) in [(left.clone(), right.clone()), (right.clone(), left)] {
751 let array = lhs
752 .binary(rhs.clone(), Operator::And)?
753 .is_null()?
754 .optimize()?;
755 assert_eq!(*array.as_::<ExactScalarFn<Binary>>().options, Operator::And);
756 assert_arrays_eq!(
757 array.as_::<ScalarFn>().get_child(0), BoolArray::from_iter([false, false, true]),
759 &mut ctx
760 );
761 assert_arrays_eq!(array.as_::<ScalarFn>().get_child(1), right, &mut ctx);
762 assert_arrays_eq!(array, BoolArray::from_iter([false, false, true]), &mut ctx);
763 }
764 Ok(())
765 }
766
767 #[test]
768 fn test_isnotnull_or_reduce() -> VortexResult<()> {
769 let mut ctx = SESSION.create_execution_ctx();
770 let left = BoolArray::from_iter([Some(true), Some(true), None]).into_array();
771 let right = BoolArray::from_iter([true, false, true]).into_array();
772
773 for (lhs, rhs) in [(left.clone(), right.clone()), (right.clone(), left)] {
776 let array = lhs
777 .binary(rhs.clone(), Operator::Or)?
778 .is_not_null()?
779 .optimize()?;
780 assert_eq!(*array.as_::<ExactScalarFn<Binary>>().options, Operator::Or);
781 assert_arrays_eq!(
782 array.as_::<ScalarFn>().get_child(0), BoolArray::from_iter([true, true, false]),
784 &mut ctx
785 );
786 assert_arrays_eq!(array.as_::<ScalarFn>().get_child(1), right, &mut ctx);
787 assert_arrays_eq!(array, BoolArray::from_iter([true, true, true]), &mut ctx);
788 }
789 Ok(())
790 }
791
792 #[test]
795 fn executes_non_columnar_inputs_iteratively() -> VortexResult<()> {
796 let mut ctx = SESSION.create_execution_ctx();
797 let a = PrimitiveArray::from_iter([1i32, 5, 9, 12]).into_array();
798 let b = PrimitiveArray::from_iter([2i32, 2, 2, 2]).into_array();
799 let sum = a.binary(b.clone(), Operator::Add)?;
800 let difference = a.binary(b, Operator::Sub)?;
801 let ten = ConstantArray::new(10i32, 4).into_array();
802 let result = sum
803 .binary(ten.clone(), Operator::Gt)?
804 .binary(difference.binary(ten, Operator::Lt)?, Operator::And)?;
805
806 assert_arrays_eq!(
807 result,
808 BoolArray::from_iter([false, false, true, false]),
809 &mut ctx
810 );
811 Ok(())
812 }
813
814 #[test]
817 fn deciding_constant_skips_other_operand() -> VortexResult<()> {
818 let mut ctx = SESSION.create_execution_ctx();
819 let failing = PrimitiveArray::from_iter([1i32, 2, 3])
820 .into_array()
821 .binary(ConstantArray::new(0i32, 3).into_array(), Operator::Div)?
822 .binary(ConstantArray::new(1i32, 3).into_array(), Operator::Gt)?;
823 assert!(failing.clone().execute::<Canonical>(&mut ctx).is_err());
824
825 let result = ConstantArray::new(false, 3)
826 .into_array()
827 .binary(failing, Operator::And)?;
828
829 assert_arrays_eq!(
830 result,
831 BoolArray::from_iter([false, false, false]),
832 &mut ctx
833 );
834 Ok(())
835 }
836}