Skip to content

Commit 8eb4cdb

Browse files
committed
Stop copying LogicalPlan and Exprs in CommonSubexprEliminate
1 parent 6b70214 commit 8eb4cdb

File tree

2 files changed

+380
-217
lines changed

2 files changed

+380
-217
lines changed

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 35 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,40 @@ 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(_) => Ok(Transformed::new(
2229+
expr.unalias(),
2230+
true,
2231+
TreeNodeRecursion::Jump,
2232+
)),
2233+
_ => Ok(Transformed::no(expr)),
2234+
}
2235+
})
2236+
}
22332237
}
22342238

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

0 commit comments

Comments
 (0)