@@ -36,7 +36,7 @@ use arrow::{
3636} ;
3737use datafusion_common:: { DataFusionError , Result , internal_err, plan_err} ;
3838use datafusion_expr:: interval_arithmetic:: Interval ;
39- use datafusion_expr:: sort_properties:: { ExprProperties , SortProperties } ;
39+ use datafusion_expr:: sort_properties:: ExprProperties ;
4040#[ expect( deprecated) ]
4141use datafusion_expr:: statistics:: Distribution :: {
4242 self , Bernoulli , Exponential , Gaussian , Generic , Uniform ,
@@ -92,6 +92,13 @@ fn negate_timestamp_array<T: ArrowTimestampType>(array: &dyn Array) -> Result<Ar
9292}
9393
9494fn negate_interval ( interval : & Interval ) -> Result < Option < Interval > > {
95+ // Integer array negation wraps at the signed minimum. A lower-unbounded
96+ // interval may contain that value, whose negation can make the result
97+ // discontinuous and unsafe to represent as a single bounded interval.
98+ if interval. data_type ( ) . is_signed_integer ( ) && interval. lower ( ) . is_null ( ) {
99+ return Ok ( None ) ;
100+ }
101+
95102 match interval. arithmetic_negate ( ) {
96103 Ok ( interval) => Ok ( Some ( interval) ) ,
97104 Err ( error)
@@ -212,31 +219,13 @@ impl PhysicalExpr for NegativeExpr {
212219 }
213220 }
214221
215- /// Negation reverses the child ordering unless signed integer values may wrap .
222+ /// The ordering of a [`NegativeExpr`] is simply the reverse of its child .
216223 fn get_properties ( & self , children : & [ ExprProperties ] ) -> Result < ExprProperties > {
217- let ( range, overflowed) = match negate_interval ( & children[ 0 ] . range ) ? {
218- Some ( range) => ( range, false ) ,
219- None => (
220- Interval :: make_unbounded ( & children[ 0 ] . range . data_type ( ) ) ?,
221- true ,
222- ) ,
223- } ;
224- let may_wrap = children[ 0 ] . range . data_type ( ) . is_signed_integer ( )
225- && ( children[ 0 ] . range . lower ( ) . is_null ( ) || overflowed) ;
226- let sort_properties = if overflowed || may_wrap {
227- // Do not let an ordering claim suppress evaluation when negation
228- // may overflow or wrap at the signed minimum.
229- SortProperties :: Unordered
230- } else {
231- match children[ 0 ] . sort_properties {
232- SortProperties :: Singleton => SortProperties :: Singleton ,
233- _ => -children[ 0 ] . sort_properties ,
234- }
235- } ;
236224 Ok ( ExprProperties {
237- sort_properties,
238- range,
225+ sort_properties : -children [ 0 ] . sort_properties ,
226+ range : children [ 0 ] . range . clone ( ) . arithmetic_negate ( ) ? ,
239227 preserves_lex_ordering : false ,
228+ // Negation is one-to-one but reverses the ordering direction.
240229 strictly_order_preserving : false ,
241230 } )
242231 }
@@ -315,12 +304,10 @@ mod tests {
315304 use crate :: expressions:: { Column , col} ;
316305
317306 use arrow:: array:: * ;
318- use arrow:: compute:: SortOptions ;
319307 use arrow:: datatypes:: DataType :: { Float32 , Float64 , Int8 , Int16 , Int32 , Int64 } ;
320308 use arrow:: datatypes:: * ;
321309 use datafusion_common:: cast:: as_primitive_array;
322310 use datafusion_common:: { DataFusionError , ScalarValue } ;
323- use datafusion_expr:: sort_properties:: SortProperties ;
324311
325312 use datafusion_physical_expr_common:: physical_expr:: fmt_sql;
326313
@@ -433,7 +420,7 @@ mod tests {
433420 }
434421
435422 #[ test]
436- fn test_overflowing_bounds_are_conservative ( ) -> Result < ( ) > {
423+ fn test_negation_bounds_are_conservative ( ) -> Result < ( ) > {
437424 let negative_expr = NegativeExpr :: new ( Arc :: new ( Column :: new ( "a" , 0 ) ) ) ;
438425 let child_interval = Interval :: make ( Some ( i8:: MIN ) , Some ( 1_i8 ) ) ?;
439426 let unbounded = Interval :: make_unbounded ( & Int8 ) ?;
@@ -447,48 +434,14 @@ mod tests {
447434 Some ( vec![ ] )
448435 ) ;
449436
450- let properties = negative_expr
451- . get_properties ( & [ ExprProperties :: new_unknown ( )
452- . with_order ( SortProperties :: Ordered ( SortOptions :: default ( ) ) )
453- . with_range ( child_interval) ] ) ?;
454- assert_eq ! ( properties. range, unbounded) ;
455- assert_eq ! ( properties. sort_properties, SortProperties :: Unordered ) ;
456-
457- let singleton_properties =
458- negative_expr. get_properties ( & [ ExprProperties :: new_unknown ( )
459- . with_order ( SortProperties :: Singleton )
460- . with_range ( Interval :: make ( Some ( i8:: MIN ) , Some ( i8:: MIN ) ) ?) ] ) ?;
437+ let lower_unbounded = Interval :: make ( None , Some ( -1_i8 ) ) ?;
461438 assert_eq ! (
462- singleton_properties. sort_properties,
463- SortProperties :: Unordered
464- ) ;
465-
466- let unknown_properties =
467- negative_expr. get_properties ( & [ ExprProperties :: new_unknown ( )
468- . with_order ( SortProperties :: Ordered ( SortOptions :: default ( ) ) )
469- . with_range ( unbounded) ] ) ?;
470- assert_eq ! (
471- unknown_properties. sort_properties,
472- SortProperties :: Unordered
473- ) ;
474-
475- let unknown_singleton_properties =
476- negative_expr. get_properties ( & [ ExprProperties :: new_unknown ( )
477- . with_order ( SortProperties :: Singleton )
478- . with_range ( Interval :: make_unbounded ( & Int8 ) ?) ] ) ?;
479- assert_eq ! (
480- unknown_singleton_properties. sort_properties,
481- SortProperties :: Unordered
439+ negative_expr. evaluate_bounds( & [ & lower_unbounded] ) ?,
440+ unbounded
482441 ) ;
483-
484- let safe_interval = Interval :: make ( Some ( i8:: MIN + 1 ) , None ) ?;
485- let safe_properties =
486- negative_expr. get_properties ( & [ ExprProperties :: new_unknown ( )
487- . with_order ( SortProperties :: Ordered ( SortOptions :: default ( ) ) )
488- . with_range ( safe_interval) ] ) ?;
489442 assert_eq ! (
490- safe_properties . sort_properties ,
491- - SortProperties :: Ordered ( SortOptions :: default ( ) )
443+ negative_expr . propagate_constraints ( & lower_unbounded , & [ & child_interval ] ) ? ,
444+ Some ( vec! [ ] )
492445 ) ;
493446 Ok ( ( ) )
494447 }
0 commit comments