@@ -92,6 +92,10 @@ impl OptimizerRule for ConstantFolding {
9292 . expressions ( )
9393 . into_iter ( )
9494 . map ( |e| {
95+ // We need to keep original expression name, if any.
96+ // Constant folding should not change expression name.
97+ let name = & e. name ( plan. schema ( ) ) ;
98+
9599 // TODO iterate until no changes are made
96100 // during rewrite (evaluating constants can
97101 // enable new simplifications and
@@ -101,7 +105,18 @@ impl OptimizerRule for ConstantFolding {
101105 // fold constants and then simplify
102106 . rewrite ( & mut const_evaluator) ?
103107 . rewrite ( & mut simplifier) ?;
104- Ok ( new_e)
108+
109+ let new_name = & new_e. name ( plan. schema ( ) ) ;
110+
111+ if let ( Ok ( expr_name) , Ok ( new_expr_name) ) = ( name, new_name) {
112+ if expr_name != new_expr_name {
113+ Ok ( new_e. alias ( expr_name) )
114+ } else {
115+ Ok ( new_e)
116+ }
117+ } else {
118+ Ok ( new_e)
119+ }
105120 } )
106121 . collect :: < Result < Vec < _ > > > ( ) ?;
107122
@@ -626,8 +641,8 @@ mod tests {
626641
627642 let expected = "\
628643 Projection: #test.a\
629- \n Filter: NOT #test.c\
630- \n Filter: #test.b\
644+ \n Filter: NOT #test.c AS test.c = Boolean(false) \
645+ \n Filter: #test.b AS test.b = Boolean(true) \
631646 \n TableScan: test projection=None";
632647
633648 assert_optimized_plan_eq ( & plan, expected) ;
@@ -647,8 +662,8 @@ mod tests {
647662 let expected = "\
648663 Projection: #test.a\
649664 \n Limit: 1\
650- \n Filter: #test.c\
651- \n Filter: NOT #test.b\
665+ \n Filter: #test.c AS test.c != Boolean(false) \
666+ \n Filter: NOT #test.b AS test.b != Boolean(true) \
652667 \n TableScan: test projection=None";
653668
654669 assert_optimized_plan_eq ( & plan, expected) ;
@@ -665,7 +680,7 @@ mod tests {
665680
666681 let expected = "\
667682 Projection: #test.a\
668- \n Filter: NOT #test.b AND #test.c\
683+ \n Filter: NOT #test.b AND #test.c AS test.b != Boolean(true) AND test.c = Boolean(true) \
669684 \n TableScan: test projection=None";
670685
671686 assert_optimized_plan_eq ( & plan, expected) ;
@@ -682,7 +697,7 @@ mod tests {
682697
683698 let expected = "\
684699 Projection: #test.a\
685- \n Filter: NOT #test.b OR NOT #test.c\
700+ \n Filter: NOT #test.b OR NOT #test.c AS test.b != Boolean(true) OR test.c = Boolean(false) \
686701 \n TableScan: test projection=None";
687702
688703 assert_optimized_plan_eq ( & plan, expected) ;
@@ -699,7 +714,7 @@ mod tests {
699714
700715 let expected = "\
701716 Projection: #test.a\
702- \n Filter: #test.b\
717+ \n Filter: #test.b AS NOT test.b = Boolean(false) \
703718 \n TableScan: test projection=None";
704719
705720 assert_optimized_plan_eq ( & plan, expected) ;
@@ -714,7 +729,7 @@ mod tests {
714729 . build ( ) ?;
715730
716731 let expected = "\
717- Projection: #test.a, #test.d, NOT #test.b\
732+ Projection: #test.a, #test.d, NOT #test.b AS test.b = Boolean(false) \
718733 \n TableScan: test projection=None";
719734
720735 assert_optimized_plan_eq ( & plan, expected) ;
@@ -733,7 +748,7 @@ mod tests {
733748 . build ( ) ?;
734749
735750 let expected = "\
736- Aggregate: groupBy=[[#test.a, #test.c]], aggr=[[MAX(#test.b), MIN(#test.b)]]\
751+ Aggregate: groupBy=[[#test.a, #test.c]], aggr=[[MAX(#test.b) AS MAX(test.b = Boolean(true)) , MIN(#test.b)]]\
737752 \n Projection: #test.a, #test.c, #test.b\
738753 \n TableScan: test projection=None";
739754
@@ -789,7 +804,7 @@ mod tests {
789804 . build ( )
790805 . unwrap ( ) ;
791806
792- let expected = "Projection: TimestampNanosecond(1599566400000000000)\
807+ let expected = "Projection: TimestampNanosecond(1599566400000000000) AS totimestamp(Utf8( \" 2020-09-08T12:00:00+00:00 \" )) \
793808 \n TableScan: test projection=None"
794809 . to_string ( ) ;
795810 let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
@@ -824,7 +839,7 @@ mod tests {
824839 . build ( )
825840 . unwrap ( ) ;
826841
827- let expected = "Projection: Int32(0)\
842+ let expected = "Projection: Int32(0) AS CAST(Utf8( \" 0 \" ) AS Int32) \
828843 \n TableScan: test projection=None";
829844 let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
830845 assert_eq ! ( expected, actual) ;
@@ -873,7 +888,7 @@ mod tests {
873888 // expect the same timestamp appears in both exprs
874889 let actual = get_optimized_plan_formatted ( & plan, & time) ;
875890 let expected = format ! (
876- "Projection: TimestampNanosecond({}), TimestampNanosecond({}) AS t2\
891+ "Projection: TimestampNanosecond({}) AS now() , TimestampNanosecond({}) AS t2\
877892 \n TableScan: test projection=None",
878893 time. timestamp_nanos( ) ,
879894 time. timestamp_nanos( )
@@ -897,7 +912,8 @@ mod tests {
897912 . unwrap ( ) ;
898913
899914 let actual = get_optimized_plan_formatted ( & plan, & time) ;
900- let expected = "Projection: NOT #test.a\
915+ let expected =
916+ "Projection: NOT #test.a AS Boolean(true) OR Boolean(false) != test.a\
901917 \n TableScan: test projection=None";
902918
903919 assert_eq ! ( actual, expected) ;
@@ -929,7 +945,7 @@ mod tests {
929945
930946 // Note that constant folder runs and folds the entire
931947 // expression down to a single constant (true)
932- let expected = "Filter: Boolean(true)\
948+ let expected = "Filter: Boolean(true) AS CAST(now() AS Int64) < CAST(totimestamp(Utf8( \" 2020-09-08T12:05:00+00:00 \" )) AS Int64) + Int32(50000) \
933949 \n TableScan: test projection=None";
934950 let actual = get_optimized_plan_formatted ( & plan, & time) ;
935951
0 commit comments