Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference visitor TreeNode APIs, change ExecutionPlan::children() and PhysicalExpr::children() return references #10543

Merged
merged 6 commits into from
May 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
avoid where clause
peter-toth committed May 23, 2024
commit d1b0b4ad29ce9fce59f203e22477b9cf0de20cf9
37 changes: 14 additions & 23 deletions datafusion/expr/src/logical_plan/tree_node.rs
Original file line number Diff line number Diff line change
@@ -71,10 +71,10 @@ impl TreeNode for LogicalPlan {
/// subqueries, for example such as are in [`Expr::Exists`].
///
/// [`Expr::Exists`]: crate::Expr::Exists
fn map_children<F>(self, mut f: F) -> Result<Transformed<Self>>
where
F: FnMut(Self) -> Result<Transformed<Self>>,
{
fn map_children<F: FnMut(Self) -> Result<Transformed<Self>>>(
self,
mut f: F,
) -> Result<Transformed<Self>> {
Ok(match self {
LogicalPlan::Projection(Projection {
expr,
@@ -373,24 +373,18 @@ pub fn unwrap_arc(plan: Arc<LogicalPlan>) -> LogicalPlan {
}

/// Applies `f` to rewrite a `Arc<LogicalPlan>` without copying, if possible
fn rewrite_arc<F>(
fn rewrite_arc<F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>>(
plan: Arc<LogicalPlan>,
mut f: F,
) -> Result<Transformed<Arc<LogicalPlan>>>
where
F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>,
{
) -> Result<Transformed<Arc<LogicalPlan>>> {
f(unwrap_arc(plan))?.map_data(|new_plan| Ok(Arc::new(new_plan)))
}

/// rewrite a `Vec` of `Arc<LogicalPlan>` without copying, if possible
fn rewrite_arcs<F>(
fn rewrite_arcs<F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>>(
input_plans: Vec<Arc<LogicalPlan>>,
mut f: F,
) -> Result<Transformed<Vec<Arc<LogicalPlan>>>>
where
F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>,
{
) -> Result<Transformed<Vec<Arc<LogicalPlan>>>> {
input_plans
.into_iter()
.map_until_stop_and_collect(|plan| rewrite_arc(plan, &mut f))
@@ -401,13 +395,10 @@ where
///
/// Should be removed when we have an API for in place modifications of the
/// extension to avoid these copies
fn rewrite_extension_inputs<F>(
fn rewrite_extension_inputs<F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>>(
extension: Extension,
f: F,
) -> Result<Transformed<Extension>>
where
F: FnMut(LogicalPlan) -> Result<Transformed<LogicalPlan>>,
{
) -> Result<Transformed<Extension>> {
let Extension { node } = extension;

node.inputs()
@@ -736,10 +727,10 @@ impl LogicalPlan {

/// Visits a plan similarly to [`Self::visit`], including subqueries that
/// may appear in expressions such as `IN (SELECT ...)`.
pub fn visit_with_subqueries<V>(&self, visitor: &mut V) -> Result<TreeNodeRecursion>
where
for<'n> V: TreeNodeVisitor<'n, Node = Self>,
{
pub fn visit_with_subqueries<V: for<'n> TreeNodeVisitor<'n, Node = Self>>(
&self,
visitor: &mut V,
) -> Result<TreeNodeRecursion> {
visitor
.f_down(self)?
.visit_children(|| {
30 changes: 12 additions & 18 deletions datafusion/expr/src/tree_node.rs
Original file line number Diff line number Diff line change
@@ -368,42 +368,36 @@ impl TreeNode for Expr {
}
}

fn transform_box<F>(be: Box<Expr>, f: &mut F) -> Result<Transformed<Box<Expr>>>
where
F: FnMut(Expr) -> Result<Transformed<Expr>>,
{
fn transform_box<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
be: Box<Expr>,
f: &mut F,
) -> Result<Transformed<Box<Expr>>> {
Ok(f(*be)?.update_data(Box::new))
}

fn transform_option_box<F>(
fn transform_option_box<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
obe: Option<Box<Expr>>,
f: &mut F,
) -> Result<Transformed<Option<Box<Expr>>>>
where
F: FnMut(Expr) -> Result<Transformed<Expr>>,
{
) -> Result<Transformed<Option<Box<Expr>>>> {
obe.map_or(Ok(Transformed::no(None)), |be| {
Ok(transform_box(be, f)?.update_data(Some))
})
}

/// &mut transform a Option<`Vec` of `Expr`s>
pub fn transform_option_vec<F>(
pub fn transform_option_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
ove: Option<Vec<Expr>>,
f: &mut F,
) -> Result<Transformed<Option<Vec<Expr>>>>
where
F: FnMut(Expr) -> Result<Transformed<Expr>>,
{
) -> Result<Transformed<Option<Vec<Expr>>>> {
ove.map_or(Ok(Transformed::no(None)), |ve| {
Ok(transform_vec(ve, f)?.update_data(Some))
})
}

/// &mut transform a `Vec` of `Expr`s
fn transform_vec<F>(ve: Vec<Expr>, f: &mut F) -> Result<Transformed<Vec<Expr>>>
where
F: FnMut(Expr) -> Result<Transformed<Expr>>,
{
fn transform_vec<F: FnMut(Expr) -> Result<Transformed<Expr>>>(
ve: Vec<Expr>,
f: &mut F,
) -> Result<Transformed<Vec<Expr>>> {
ve.into_iter().map_until_stop_and_collect(f)
}