Skip to content

Commit 16a3148

Browse files
authored
Remove redundant unalias_nested calls for creating Filter's (#11340)
* Remove uncessary unalias_nested calls when creating Filter * simplify
1 parent 1e0c06e commit 16a3148

File tree

3 files changed

+13
-56
lines changed

3 files changed

+13
-56
lines changed

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ use crate::{
4141
};
4242

4343
use 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};
4745
use 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

21182083
impl 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

datafusion/optimizer/src/common_subexpr_eliminate.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,9 @@ impl CommonSubexprEliminate {
342342
let input = unwrap_arc(input);
343343
let expr = vec![predicate];
344344
self.try_unary_plan(expr, input, config)?
345-
.transform_data(|(mut new_expr, new_input)| {
345+
.map_data(|(mut new_expr, new_input)| {
346346
assert_eq!(new_expr.len(), 1); // passed in vec![predicate]
347-
let new_predicate = new_expr
348-
.pop()
349-
.unwrap()
350-
.unalias_nested()
351-
.update_data(|new_predicate| (new_predicate, new_input));
352-
Ok(new_predicate)
353-
})?
354-
.map_data(|(new_predicate, new_input)| {
347+
let new_predicate = new_expr.pop().unwrap();
355348
Filter::try_new(new_predicate, Arc::new(new_input))
356349
.map(LogicalPlan::Filter)
357350
})

datafusion/optimizer/src/push_down_filter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,11 @@ impl OptimizerRule for PushDownFilter {
761761

762762
// Push down non-unnest filter predicate
763763
// Unnest
764-
// Unenst Input (Projection)
764+
// Unnest Input (Projection)
765765
// -> rewritten to
766766
// Unnest
767767
// Filter
768-
// Unenst Input (Projection)
768+
// Unnest Input (Projection)
769769

770770
let unnest_input = std::mem::take(&mut unnest.input);
771771

0 commit comments

Comments
 (0)