@@ -41,9 +41,7 @@ use crate::{
4141} ;
4242
4343use arrow:: datatypes:: { DataType , Field , Schema , SchemaRef } ;
44- use datafusion_common:: tree_node:: {
45- Transformed , TransformedResult , TreeNode , TreeNodeRecursion ,
46- } ;
44+ use datafusion_common:: tree_node:: { Transformed , TreeNode , TreeNodeRecursion } ;
4745use datafusion_common:: {
4846 aggregate_functional_dependencies, internal_err, plan_err, Column , Constraints ,
4947 DFSchema , DFSchemaRef , DataFusionError , Dependency , FunctionalDependence ,
@@ -645,39 +643,6 @@ impl LogicalPlan {
645643 Ok ( LogicalPlan :: Values ( Values { schema, values } ) )
646644 }
647645 LogicalPlan :: Filter ( Filter { predicate, input } ) => {
648- // todo: should this logic be moved to Filter::try_new?
649-
650- // filter predicates should not contain aliased expressions so we remove any aliases
651- // before this logic was added we would have aliases within filters such as for
652- // benchmark q6:
653- //
654- // lineitem.l_shipdate >= Date32(\"8766\")
655- // AND lineitem.l_shipdate < Date32(\"9131\")
656- // AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount >=
657- // Decimal128(Some(49999999999999),30,15)
658- // AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount <=
659- // Decimal128(Some(69999999999999),30,15)
660- // AND lineitem.l_quantity < Decimal128(Some(2400),15,2)
661-
662- let predicate = predicate
663- . transform_down ( |expr| {
664- match expr {
665- Expr :: Exists { .. }
666- | Expr :: ScalarSubquery ( _)
667- | Expr :: InSubquery ( _) => {
668- // subqueries could contain aliases so we don't recurse into those
669- Ok ( Transformed :: new ( expr, false , TreeNodeRecursion :: Jump ) )
670- }
671- Expr :: Alias ( _) => Ok ( Transformed :: new (
672- expr. unalias ( ) ,
673- true ,
674- TreeNodeRecursion :: Jump ,
675- ) ) ,
676- _ => Ok ( Transformed :: no ( expr) ) ,
677- }
678- } )
679- . data ( ) ?;
680-
681646 Filter :: try_new ( predicate, input) . map ( LogicalPlan :: Filter )
682647 }
683648 LogicalPlan :: Repartition ( _) => Ok ( self ) ,
@@ -878,7 +843,7 @@ impl LogicalPlan {
878843 }
879844 LogicalPlan :: Filter { .. } => {
880845 assert_eq ! ( 1 , expr. len( ) ) ;
881- let predicate = expr. pop ( ) . unwrap ( ) . unalias_nested ( ) . data ;
846+ let predicate = expr. pop ( ) . unwrap ( ) ;
882847
883848 Filter :: try_new ( predicate, Arc :: new ( inputs. swap_remove ( 0 ) ) )
884849 . map ( LogicalPlan :: Filter )
@@ -2117,6 +2082,9 @@ pub struct Filter {
21172082
21182083impl Filter {
21192084 /// Create a new filter operator.
2085+ ///
2086+ /// Notes: as Aliases have no effect on the output of a filter operator,
2087+ /// they are removed from the predicate expression.
21202088 pub fn try_new ( predicate : Expr , input : Arc < LogicalPlan > ) -> Result < Self > {
21212089 // Filter predicates must return a boolean value so we try and validate that here.
21222090 // Note that it is not always possible to resolve the predicate expression during plan
@@ -2940,7 +2908,7 @@ mod tests {
29402908 use crate :: logical_plan:: table_scan;
29412909 use crate :: { col, exists, in_subquery, lit, placeholder, GroupingSet } ;
29422910
2943- use datafusion_common:: tree_node:: TreeNodeVisitor ;
2911+ use datafusion_common:: tree_node:: { TransformedResult , TreeNodeVisitor } ;
29442912 use datafusion_common:: { not_impl_err, Constraint , ScalarValue } ;
29452913
29462914 use crate :: test:: function_stub:: count;
@@ -3500,11 +3468,8 @@ digraph {
35003468 } ) ) ;
35013469 let col = schema. field_names ( ) [ 0 ] . clone ( ) ;
35023470
3503- let filter = Filter :: try_new (
3504- Expr :: Column ( col. into ( ) ) . eq ( Expr :: Literal ( ScalarValue :: Int32 ( Some ( 1 ) ) ) ) ,
3505- scan,
3506- )
3507- . unwrap ( ) ;
3471+ let filter =
3472+ Filter :: try_new ( Expr :: Column ( col. into ( ) ) . eq ( lit ( 1i32 ) ) , scan) . unwrap ( ) ;
35083473 assert ! ( filter. is_scalar( ) ) ;
35093474 }
35103475
@@ -3522,8 +3487,7 @@ digraph {
35223487 . build ( )
35233488 . unwrap ( ) ;
35243489
3525- let external_filter =
3526- col ( "foo" ) . eq ( Expr :: Literal ( ScalarValue :: Boolean ( Some ( true ) ) ) ) ;
3490+ let external_filter = col ( "foo" ) . eq ( lit ( true ) ) ;
35273491
35283492 // after transformation, because plan is not the same anymore,
35293493 // the parent plan is built again with call to LogicalPlan::with_new_inputs -> with_new_exprs
0 commit comments