@@ -20,19 +20,24 @@ use vortex_error::vortex_bail;
2020use vortex_session:: VortexSession ;
2121use vortex_session:: registry:: CachedId ;
2222
23+ use crate :: AnyColumnar ;
2324use crate :: ArrayRef ;
2425use crate :: ExecutionCtx ;
26+ use crate :: arrays:: Constant ;
2527use crate :: arrays:: ScalarFnArray ;
2628use crate :: dtype:: DType ;
2729use crate :: dtype:: Nullability ;
2830use crate :: expr:: BoundExpression ;
2931use crate :: expr:: bound;
3032use crate :: expr:: display:: ExprDisplay ;
33+ use crate :: matcher:: Matcher ;
3134use crate :: proto:: expr as pb;
35+ use crate :: require_input;
3236use crate :: scalar_fn:: Arity ;
3337use crate :: scalar_fn:: ChildName ;
3438use crate :: scalar_fn:: ExecutionArgs ;
3539use crate :: scalar_fn:: ReduceNode ;
40+ use crate :: scalar_fn:: ScalarFnExecutionResult ;
3641use crate :: scalar_fn:: ScalarFnId ;
3742use crate :: scalar_fn:: ScalarFnVTable ;
3843use crate :: scalar_fn:: ScalarFnVTableExt ;
@@ -315,6 +320,38 @@ impl ScalarFnVTable for Binary {
315320 ) )
316321 }
317322
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+ // A constant operand can decide the result, such as `false AND x` or `x < null`, so
332+ // `execute` does not need the other operand executed.
333+ || 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+
318355 fn is_strict ( & self , operator : & Operator ) -> bool {
319356 !matches ! ( operator, Operator :: And | Operator :: Or )
320357 }
@@ -344,6 +381,7 @@ mod tests {
344381 use vortex_session:: VortexSession ;
345382
346383 use super :: * ;
384+ use crate :: Canonical ;
347385 use crate :: IntoArray ;
348386 use crate :: VortexSessionExecute ;
349387 use crate :: arrays:: Bool ;
@@ -750,4 +788,49 @@ mod tests {
750788 }
751789 Ok ( ( ) )
752790 }
791+
792+ /// Comparisons over arithmetic, joined by `AND`: every input is a scalar function, so each
793+ /// operator executes its inputs through the executor.
794+ #[ 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+ /// A constant `false` decides `AND` without executing the other operand, which here fails
815+ /// when executed.
816+ #[ 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+ }
753836}
0 commit comments