@@ -100,6 +100,10 @@ struct SimplifiedFilterPredicate {
100100/// throughout an `AND` / `OR` tree because both operators are monotonic with
101101/// respect to whether their result can be `TRUE`. It is not safe through `NOT`
102102/// or arbitrary expressions, so those are deliberately treated as leaves.
103+ ///
104+ /// The same equivalence lets `IN` lists drop their `NULL` members, and makes
105+ /// `NOT IN` lists that contain one reject every row. See
106+ /// [`simplify_filter_in_list`].
103107fn simplify_filter_predicate ( expr : Expr ) -> SimplifiedFilterPredicate {
104108 match expr {
105109 Expr :: Literal ( ScalarValue :: Boolean ( Some ( true ) ) , _) => {
@@ -114,9 +118,7 @@ fn simplify_filter_predicate(expr: Expr) -> SimplifiedFilterPredicate {
114118 Expr :: BinaryExpr ( binary) if binary. op == Operator :: Or => {
115119 simplify_filter_or ( * binary. left , * binary. right )
116120 }
117- Expr :: InList ( in_list) if in_list. list . iter ( ) . any ( is_null_literal) => {
118- simplify_filter_in_list ( in_list)
119- }
121+ Expr :: InList ( in_list) => simplify_filter_in_list ( in_list) ,
120122 expr => simplified ( FilterPredicate :: Expression ( expr) , false ) ,
121123 }
122124}
@@ -125,7 +127,7 @@ fn is_null_literal(expr: &Expr) -> bool {
125127 matches ! ( expr, Expr :: Literal ( scalar, _) if scalar. is_null( ) )
126128}
127129
128- /// Simplifies an `IN` list that contains `NULL` literals under filter semantics .
130+ /// Simplifies an `IN` list under filter semantics by dropping `NULL` members .
129131///
130132/// `x IN (a, NULL)` is `TRUE` exactly when `x` matches `a`: a `NULL` member can
131133/// only turn `FALSE` into `NULL`, and `NULL` rejects a row just like `FALSE`.
@@ -138,28 +140,36 @@ fn is_null_literal(expr: &Expr) -> bool {
138140/// This is only sound under filter semantics; the same rewrite in a general
139141/// expression context would change `SELECT 3 IN (1, NULL)` from `NULL` to `FALSE`.
140142fn simplify_filter_in_list ( in_list : InList ) -> SimplifiedFilterPredicate {
141- if in_list. negated {
142- return simplified ( FilterPredicate :: RejectsAll ( Expr :: InList ( in_list) ) , true ) ;
143- }
144-
145143 let InList {
146144 expr,
147145 list,
148146 negated,
149147 } = in_list;
148+
149+ let len_before = list. len ( ) ;
150150 let list: Vec < Expr > = list. into_iter ( ) . filter ( |e| !is_null_literal ( e) ) . collect ( ) ;
151151
152- if list. is_empty ( ) {
153- simplified (
154- FilterPredicate :: RejectsAll ( Expr :: Literal ( ScalarValue :: Boolean ( None ) , None ) ) ,
155- true ,
156- )
157- } else {
158- simplified (
152+ if list. len ( ) == len_before {
153+ // No `NULL` members, so there is nothing to simplify.
154+ return simplified (
159155 FilterPredicate :: Expression ( Expr :: InList ( InList :: new ( expr, list, negated) ) ) ,
156+ false ,
157+ ) ;
158+ }
159+
160+ if negated || list. is_empty ( ) {
161+ // `NOT IN` with a `NULL` member, and `IN` over nothing but `NULL`s, are
162+ // both `NULL` for every row that they do not reject outright.
163+ return simplified (
164+ FilterPredicate :: RejectsAll ( Expr :: Literal ( ScalarValue :: Boolean ( None ) , None ) ) ,
160165 true ,
161- )
166+ ) ;
162167 }
168+
169+ simplified (
170+ FilterPredicate :: Expression ( Expr :: InList ( InList :: new ( expr, list, negated) ) ) ,
171+ true ,
172+ )
163173}
164174
165175fn simplify_filter_and ( left : Expr , right : Expr ) -> SimplifiedFilterPredicate {
@@ -540,85 +550,4 @@ mod tests {
540550 EmptyRelation: rows=0
541551 " )
542552 }
543-
544- #[ test]
545- fn filter_in_list_with_null_drops_null ( ) -> Result < ( ) > {
546- // SELECT a FROM test WHERE a IN (1, 2, NULL)
547- // A NULL member can only turn FALSE into NULL, and NULL rejects a row
548- // just like FALSE, so the filter keeps exactly the rows that a IN (1, 2) keeps.
549- let filter_expr = col ( "a" ) . in_list (
550- vec ! [
551- lit( 1u32 ) ,
552- lit( 2u32 ) ,
553- Expr :: Literal ( ScalarValue :: UInt32 ( None ) , None ) ,
554- ] ,
555- false ,
556- ) ;
557-
558- let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
559- . filter ( filter_expr) ?
560- . build ( ) ?;
561-
562- assert_optimized_plan_equal ! ( plan, @r"
563- Filter: test.a IN ([UInt32(1), UInt32(2)])
564- TableScan: test
565- " )
566- }
567-
568- #[ test]
569- fn filter_in_list_of_only_nulls_rejects_all ( ) -> Result < ( ) > {
570- // SELECT a FROM test WHERE a IN (NULL) is NULL for every row.
571- let filter_expr =
572- col ( "a" ) . in_list ( vec ! [ Expr :: Literal ( ScalarValue :: UInt32 ( None ) , None ) ] , false ) ;
573-
574- let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
575- . filter ( filter_expr) ?
576- . build ( ) ?;
577-
578- assert_optimized_plan_equal ! ( plan, @"EmptyRelation: rows=0" )
579- }
580-
581- #[ test]
582- fn filter_not_in_list_with_null_rejects_all ( ) -> Result < ( ) > {
583- // SELECT a FROM test WHERE a NOT IN (1, NULL) is FALSE when a = 1 and
584- // NULL otherwise, so it is never TRUE.
585- let filter_expr = col ( "a" ) . in_list (
586- vec ! [ lit( 1u32 ) , Expr :: Literal ( ScalarValue :: UInt32 ( None ) , None ) ] ,
587- true ,
588- ) ;
589-
590- let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
591- . filter ( filter_expr) ?
592- . build ( ) ?;
593-
594- assert_optimized_plan_equal ! ( plan, @"EmptyRelation: rows=0" )
595- }
596-
597- #[ test]
598- fn filter_in_list_without_null_is_untouched ( ) -> Result < ( ) > {
599- let filter_expr = col ( "a" ) . in_list ( vec ! [ lit( 1u32 ) , lit( 2u32 ) ] , false ) ;
600-
601- let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
602- . filter ( filter_expr) ?
603- . build ( ) ?;
604-
605- assert_optimized_plan_equal ! ( plan, @r"
606- Filter: test.a IN ([UInt32(1), UInt32(2)])
607- TableScan: test
608- " )
609- }
610-
611- #[ test]
612- fn filter_not_in_list_without_null_is_untouched ( ) -> Result < ( ) > {
613- let filter_expr = col ( "a" ) . in_list ( vec ! [ lit( 1u32 ) , lit( 2u32 ) ] , true ) ;
614-
615- let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
616- . filter ( filter_expr) ?
617- . build ( ) ?;
618-
619- assert_optimized_plan_equal ! ( plan, @r"
620- Filter: test.a NOT IN ([UInt32(1), UInt32(2)])
621- TableScan: test
622- " )
623- }
624553}
0 commit comments