@@ -284,6 +284,34 @@ pub enum PredicateOperator {
284284 NotBetween ,
285285}
286286
287+ impl PredicateOperator {
288+ /// The operator selecting the complement of the non-NULL rows, matching
289+ /// Java `LeafFunction.negate`. `None` when there is no such operator.
290+ fn negate ( self ) -> Option < Self > {
291+ Some ( match self {
292+ Self :: IsNull => Self :: IsNotNull ,
293+ Self :: IsNotNull => Self :: IsNull ,
294+ Self :: Eq => Self :: NotEq ,
295+ Self :: NotEq => Self :: Eq ,
296+ Self :: Lt => Self :: GtEq ,
297+ Self :: GtEq => Self :: Lt ,
298+ Self :: LtEq => Self :: Gt ,
299+ Self :: Gt => Self :: LtEq ,
300+ Self :: In => Self :: NotIn ,
301+ Self :: NotIn => Self :: In ,
302+ Self :: Between => Self :: NotBetween ,
303+ Self :: NotBetween => Self :: Between ,
304+ Self :: StartsWith
305+ | Self :: EndsWith
306+ | Self :: Contains
307+ | Self :: ArrayContains
308+ | Self :: ArraysOverlap
309+ | Self :: ArrayContainsAll
310+ | Self :: Like => return None ,
311+ } )
312+ }
313+ }
314+
287315impl fmt:: Display for PredicateOperator {
288316 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
289317 match self {
@@ -424,37 +452,48 @@ impl Predicate {
424452
425453 /// Negate a predicate with simplification.
426454 ///
455+ /// Like Java `Predicate.negate`, leaf operators are flipped (`=` / `!=`,
456+ /// `<` / `>=`, `IS NULL` / `IS NOT NULL`, `IN` / `NOT IN`, ...) and AND/OR
457+ /// are pushed down with De Morgan, so a NULL input still evaluates to false.
458+ /// A plain `!p` would select NULL rows for `NOT(p = 'a')`.
459+ ///
427460 /// - `NOT(NOT(p))` → `p` (double negation elimination)
428461 /// - `NOT(AlwaysTrue)` → `AlwaysFalse`
429462 /// - `NOT(AlwaysFalse)` → `AlwaysTrue`
463+ /// - Leaves without a negated operator stay wrapped in `Not`.
430464 pub fn negate ( predicate : Predicate ) -> Predicate {
431465 match predicate {
432466 Predicate :: Not ( inner) => * inner,
433467 Predicate :: AlwaysTrue => Predicate :: AlwaysFalse ,
434468 Predicate :: AlwaysFalse => Predicate :: AlwaysTrue ,
469+ Predicate :: And ( children) => {
470+ Predicate :: or ( children. into_iter ( ) . map ( Predicate :: negate) . collect ( ) )
471+ }
472+ Predicate :: Or ( children) => {
473+ Predicate :: and ( children. into_iter ( ) . map ( Predicate :: negate) . collect ( ) )
474+ }
435475 Predicate :: Leaf {
436476 column,
437477 index,
438478 data_type,
439479 op,
440480 literals,
441- } if literals. is_empty ( )
442- && matches ! ( op, PredicateOperator :: In | PredicateOperator :: NotIn ) =>
443- {
444- // Java negates the leaf function, retaining its NULL exclusion.
445- Predicate :: Leaf {
481+ } => match op. negate ( ) {
482+ Some ( op) => Predicate :: Leaf {
446483 column,
447484 index,
448485 data_type,
449- op : if op == PredicateOperator :: In {
450- PredicateOperator :: NotIn
451- } else {
452- PredicateOperator :: In
453- } ,
486+ op,
454487 literals,
455- }
456- }
457- other => Predicate :: Not ( Box :: new ( other) ) ,
488+ } ,
489+ None => Predicate :: Not ( Box :: new ( Predicate :: Leaf {
490+ column,
491+ index,
492+ data_type,
493+ op,
494+ literals,
495+ } ) ) ,
496+ } ,
458497 }
459498 }
460499
@@ -2153,7 +2192,22 @@ pub(crate) fn eval_row(predicate: &Predicate, row: &BinaryRow) -> Result<bool> {
21532192 }
21542193 Ok ( false )
21552194 }
2156- Predicate :: Not ( inner) => Ok ( !eval_row ( inner, row) ?) ,
2195+ Predicate :: Not ( inner) => match inner. as_ref ( ) {
2196+ // `negate` keeps `Not` only around leaves without a negated
2197+ // operator. Those leaves are false for NULL input, and so is
2198+ // their negation (Java `NotStartsWith`, `NotLike`, ...).
2199+ Predicate :: Leaf {
2200+ index,
2201+ data_type,
2202+ op,
2203+ literals,
2204+ ..
2205+ } if !matches ! ( op, PredicateOperator :: IsNull | PredicateOperator :: IsNotNull ) => {
2206+ let datum = extract_datum ( row, * index, data_type) ?;
2207+ Ok ( datum. is_some ( ) && !eval_leaf ( * op, datum. as_ref ( ) , literals) )
2208+ }
2209+ _ => Ok ( !eval_row ( inner, row) ?) ,
2210+ } ,
21572211 Predicate :: Leaf {
21582212 index,
21592213 data_type,
@@ -2872,13 +2926,39 @@ mod tests {
28722926 #[ test]
28732927 fn test_not ( ) {
28742928 let pb = PredicateBuilder :: new ( & test_fields ( ) ) ;
2875- let p = pb. equal ( "id" , Datum :: Int ( 1 ) ) . unwrap ( ) ;
2876- let negated = Predicate :: negate ( p . clone ( ) ) ;
2929+ let eq = pb. equal ( "id" , Datum :: Int ( 1 ) ) . unwrap ( ) ;
2930+ let gt = pb . greater_than ( "hr" , Datum :: Int ( 2 ) ) . unwrap ( ) ;
28772931
2878- match & negated {
2879- Predicate :: Not ( inner) => assert_eq ! ( inner. as_ref( ) , & p) ,
2880- other => panic ! ( "expected Not, got {other:?}" ) ,
2881- }
2932+ // Like Java, leaves flip their operator instead of becoming `Not`.
2933+ assert_eq ! (
2934+ Predicate :: negate( eq. clone( ) ) ,
2935+ pb. not_equal( "id" , Datum :: Int ( 1 ) ) . unwrap( )
2936+ ) ;
2937+ assert_eq ! (
2938+ Predicate :: negate( Predicate :: and( vec![ eq. clone( ) , gt. clone( ) ] ) ) ,
2939+ Predicate :: or( vec![
2940+ pb. not_equal( "id" , Datum :: Int ( 1 ) ) . unwrap( ) ,
2941+ pb. less_or_equal( "hr" , Datum :: Int ( 2 ) ) . unwrap( ) ,
2942+ ] )
2943+ ) ;
2944+ assert_eq ! (
2945+ Predicate :: negate( Predicate :: or( vec![ eq. clone( ) , gt. clone( ) ] ) ) ,
2946+ Predicate :: and( vec![
2947+ pb. not_equal( "id" , Datum :: Int ( 1 ) ) . unwrap( ) ,
2948+ pb. less_or_equal( "hr" , Datum :: Int ( 2 ) ) . unwrap( ) ,
2949+ ] )
2950+ ) ;
2951+ assert_eq ! (
2952+ Predicate :: negate( pb. is_null( "id" ) . unwrap( ) ) ,
2953+ pb. is_not_null( "id" ) . unwrap( )
2954+ ) ;
2955+
2956+ // No negated operator: stays `Not`.
2957+ let starts = pb. starts_with ( "name" , Datum :: String ( "a" . into ( ) ) ) . unwrap ( ) ;
2958+ assert_eq ! (
2959+ Predicate :: negate( starts. clone( ) ) ,
2960+ Predicate :: Not ( Box :: new( starts) )
2961+ ) ;
28822962 }
28832963
28842964 // ======================== field_idx_to_partition_idx ========================
@@ -2957,7 +3037,7 @@ mod tests {
29573037 fn test_display_not ( ) {
29583038 let pb = PredicateBuilder :: new ( & test_fields ( ) ) ;
29593039 let p = pb. equal ( "id" , Datum :: Int ( 1 ) ) . unwrap ( ) ;
2960- let negated = Predicate :: negate ( p ) ;
3040+ let negated = Predicate :: Not ( Box :: new ( p ) ) ;
29613041 assert_eq ! ( format!( "{negated}" ) , "NOT (id = 1)" ) ;
29623042 }
29633043
@@ -3377,6 +3457,27 @@ mod tests {
33773457 & row
33783458 )
33793459 . unwrap( ) ) ;
3460+ // ... and so is its negation.
3461+ assert ! ( !eval_row(
3462+ & Predicate :: negate( make_leaf(
3463+ "x" ,
3464+ 0 ,
3465+ PredicateOperator :: Eq ,
3466+ vec![ Datum :: Int ( 42 ) ]
3467+ ) ) ,
3468+ & row
3469+ )
3470+ . unwrap( ) ) ;
3471+ assert ! ( !eval_row(
3472+ & Predicate :: Not ( Box :: new( make_leaf(
3473+ "x" ,
3474+ 0 ,
3475+ PredicateOperator :: Gt ,
3476+ vec![ Datum :: Int ( 42 ) ]
3477+ ) ) ) ,
3478+ & row
3479+ )
3480+ . unwrap( ) ) ;
33803481 // IsNull / IsNotNull
33813482 assert ! ( eval_row( & make_leaf( "x" , 0 , PredicateOperator :: IsNull , vec![ ] ) , & row) . unwrap( ) ) ;
33823483 assert ! ( !eval_row(
0 commit comments