Skip to content

Commit c6b2efc

Browse files
authored
Stop copying LogicalPlan and Exprs in CommonSubexprEliminate (2-3% planning speed improvement) (#10835)
* Stop copying LogicalPlan and Exprs in `CommonSubexprEliminate` * thread transformed * Update unary to report transformed correctly * Preserve through window transforms * track aggregate * Avoid re-computing Aggregate schema * Update datafusion/optimizer/src/common_subexpr_eliminate.rs * Avoid unecessary setting transform flat * Cleanup unaliasing
1 parent ea0ba99 commit c6b2efc

File tree

3 files changed

+439
-224
lines changed

3 files changed

+439
-224
lines changed

datafusion/common/src/tree_node.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ impl<T> Transformed<T> {
615615
}
616616
}
617617

618+
/// Create a `Transformed` with `transformed and [`TreeNodeRecursion::Continue`].
619+
pub fn new_transformed(data: T, transformed: bool) -> Self {
620+
Self::new(data, transformed, TreeNodeRecursion::Continue)
621+
}
622+
618623
/// Wrapper for transformed data with [`TreeNodeRecursion::Continue`] statement.
619624
pub fn yes(data: T) -> Self {
620625
Self::new(data, true, TreeNodeRecursion::Continue)

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -870,37 +870,7 @@ impl LogicalPlan {
870870
LogicalPlan::Filter { .. } => {
871871
assert_eq!(1, expr.len());
872872
let predicate = expr.pop().unwrap();
873-
874-
// filter predicates should not contain aliased expressions so we remove any aliases
875-
// before this logic was added we would have aliases within filters such as for
876-
// benchmark q6:
877-
//
878-
// lineitem.l_shipdate >= Date32(\"8766\")
879-
// AND lineitem.l_shipdate < Date32(\"9131\")
880-
// AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount >=
881-
// Decimal128(Some(49999999999999),30,15)
882-
// AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount <=
883-
// Decimal128(Some(69999999999999),30,15)
884-
// AND lineitem.l_quantity < Decimal128(Some(2400),15,2)
885-
886-
let predicate = predicate
887-
.transform_down(|expr| {
888-
match expr {
889-
Expr::Exists { .. }
890-
| Expr::ScalarSubquery(_)
891-
| Expr::InSubquery(_) => {
892-
// subqueries could contain aliases so we don't recurse into those
893-
Ok(Transformed::new(expr, false, TreeNodeRecursion::Jump))
894-
}
895-
Expr::Alias(_) => Ok(Transformed::new(
896-
expr.unalias(),
897-
true,
898-
TreeNodeRecursion::Jump,
899-
)),
900-
_ => Ok(Transformed::no(expr)),
901-
}
902-
})
903-
.data()?;
873+
let predicate = Filter::remove_aliases(predicate)?.data;
904874

905875
Filter::try_new(predicate, Arc::new(inputs.swap_remove(0)))
906876
.map(LogicalPlan::Filter)
@@ -2230,6 +2200,38 @@ impl Filter {
22302200
}
22312201
false
22322202
}
2203+
2204+
/// Remove aliases from a predicate for use in a `Filter`
2205+
///
2206+
/// filter predicates should not contain aliased expressions so we remove
2207+
/// any aliases.
2208+
///
2209+
/// before this logic was added we would have aliases within filters such as
2210+
/// for benchmark q6:
2211+
///
2212+
/// ```sql
2213+
/// lineitem.l_shipdate >= Date32(\"8766\")
2214+
/// AND lineitem.l_shipdate < Date32(\"9131\")
2215+
/// AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount >=
2216+
/// Decimal128(Some(49999999999999),30,15)
2217+
/// AND CAST(lineitem.l_discount AS Decimal128(30, 15)) AS lineitem.l_discount <=
2218+
/// Decimal128(Some(69999999999999),30,15)
2219+
/// AND lineitem.l_quantity < Decimal128(Some(2400),15,2)
2220+
/// ```
2221+
pub fn remove_aliases(predicate: Expr) -> Result<Transformed<Expr>> {
2222+
predicate.transform_down(|expr| {
2223+
match expr {
2224+
Expr::Exists { .. } | Expr::ScalarSubquery(_) | Expr::InSubquery(_) => {
2225+
// subqueries could contain aliases so we don't recurse into those
2226+
Ok(Transformed::new(expr, false, TreeNodeRecursion::Jump))
2227+
}
2228+
Expr::Alias(Alias { expr, .. }) => {
2229+
Ok(Transformed::new(*expr, true, TreeNodeRecursion::Jump))
2230+
}
2231+
_ => Ok(Transformed::no(expr)),
2232+
}
2233+
})
2234+
}
22332235
}
22342236

22352237
/// Window its input based on a set of window spec and window function (e.g. SUM or RANK)

0 commit comments

Comments
 (0)