@@ -34,8 +34,8 @@ use datafusion_common::tree_node::{RewriteRecursion, TreeNode, TreeNodeRewriter}
34
34
use datafusion_common:: { DFSchema , DFSchemaRef , DataFusionError , Result , ScalarValue } ;
35
35
use datafusion_expr:: expr:: { InList , InSubquery , ScalarFunction } ;
36
36
use datafusion_expr:: {
37
- and, expr, lit, or, BinaryExpr , BuiltinScalarFunction , ColumnarValue , Expr , Like ,
38
- Volatility ,
37
+ and, expr, lit, or, BinaryExpr , BuiltinScalarFunction , Case , ColumnarValue , Expr ,
38
+ Like , Volatility ,
39
39
} ;
40
40
use datafusion_physical_expr:: { create_physical_expr, execution_props:: ExecutionProps } ;
41
41
@@ -1069,17 +1069,20 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
1069
1069
//
1070
1070
// Note: the rationale for this rewrite is that the expr can then be further
1071
1071
// simplified using the existing rules for AND/OR
1072
- Expr :: Case ( case)
1073
- if !case. when_then_expr . is_empty ( )
1074
- && case. when_then_expr . len ( ) < 3 // The rewrite is O(n!) so limit to small number
1075
- && info. is_boolean_type ( & case. when_then_expr [ 0 ] . 1 ) ? =>
1072
+ Expr :: Case ( Case {
1073
+ expr : None ,
1074
+ when_then_expr,
1075
+ else_expr,
1076
+ } ) if !when_then_expr. is_empty ( )
1077
+ && when_then_expr. len ( ) < 3 // The rewrite is O(n!) so limit to small number
1078
+ && info. is_boolean_type ( & when_then_expr[ 0 ] . 1 ) ? =>
1076
1079
{
1077
1080
// The disjunction of all the when predicates encountered so far
1078
1081
let mut filter_expr = lit ( false ) ;
1079
1082
// The disjunction of all the cases
1080
1083
let mut out_expr = lit ( false ) ;
1081
1084
1082
- for ( when, then) in case . when_then_expr {
1085
+ for ( when, then) in when_then_expr {
1083
1086
let case_expr = when
1084
1087
. as_ref ( )
1085
1088
. clone ( )
@@ -1090,7 +1093,7 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
1090
1093
filter_expr = filter_expr. or ( * when) ;
1091
1094
}
1092
1095
1093
- if let Some ( else_expr) = case . else_expr {
1096
+ if let Some ( else_expr) = else_expr {
1094
1097
let case_expr = filter_expr. not ( ) . and ( * else_expr) ;
1095
1098
out_expr = out_expr. or ( case_expr) ;
1096
1099
}
@@ -2819,9 +2822,9 @@ mod tests {
2819
2822
2820
2823
#[ test]
2821
2824
fn simplify_expr_case_when_then_else ( ) {
2822
- // CASE WHERE c2 != false THEN "ok" == "not_ok" ELSE c2 == true
2825
+ // CASE WHEN c2 != false THEN "ok" == "not_ok" ELSE c2 == true
2823
2826
// -->
2824
- // CASE WHERE c2 THEN false ELSE c2
2827
+ // CASE WHEN c2 THEN false ELSE c2
2825
2828
// -->
2826
2829
// false
2827
2830
assert_eq ! (
@@ -2836,9 +2839,9 @@ mod tests {
2836
2839
col( "c2" ) . not( ) . and( col( "c2" ) ) // #1716
2837
2840
) ;
2838
2841
2839
- // CASE WHERE c2 != false THEN "ok" == "ok" ELSE c2
2842
+ // CASE WHEN c2 != false THEN "ok" == "ok" ELSE c2
2840
2843
// -->
2841
- // CASE WHERE c2 THEN true ELSE c2
2844
+ // CASE WHEN c2 THEN true ELSE c2
2842
2845
// -->
2843
2846
// c2
2844
2847
//
@@ -2856,7 +2859,7 @@ mod tests {
2856
2859
col( "c2" ) . or( col( "c2" ) . not( ) . and( col( "c2" ) ) ) // #1716
2857
2860
) ;
2858
2861
2859
- // CASE WHERE ISNULL(c2) THEN true ELSE c2
2862
+ // CASE WHEN ISNULL(c2) THEN true ELSE c2
2860
2863
// -->
2861
2864
// ISNULL(c2) OR c2
2862
2865
//
@@ -2873,7 +2876,7 @@ mod tests {
2873
2876
. or( col( "c2" ) . is_not_null( ) . and( col( "c2" ) ) )
2874
2877
) ;
2875
2878
2876
- // CASE WHERE c1 then true WHERE c2 then false ELSE true
2879
+ // CASE WHEN c1 then true WHEN c2 then false ELSE true
2877
2880
// --> c1 OR (NOT(c1) AND c2 AND FALSE) OR (NOT(c1 OR c2) AND TRUE)
2878
2881
// --> c1 OR (NOT(c1) AND NOT(c2))
2879
2882
// --> c1 OR NOT(c2)
@@ -2892,7 +2895,7 @@ mod tests {
2892
2895
col( "c1" ) . or( col( "c1" ) . not( ) . and( col( "c2" ) . not( ) ) )
2893
2896
) ;
2894
2897
2895
- // CASE WHERE c1 then true WHERE c2 then true ELSE false
2898
+ // CASE WHEN c1 then true WHEN c2 then true ELSE false
2896
2899
// --> c1 OR (NOT(c1) AND c2 AND TRUE) OR (NOT(c1 OR c2) AND FALSE)
2897
2900
// --> c1 OR (NOT(c1) AND c2)
2898
2901
// --> c1 OR c2
0 commit comments