-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduce a common trait TreeNode for ExecutionPlan, PhysicalExpr, LogicalExpr, LogicalPlan #5630
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7252d26
Repalce the TreeNodeVisitor with Closure and change the TreeNodeRewri…
kyotoYaho 3041506
Reuse TreeNode for physical expression
kyotoYaho 90e31ee
Implement TreeNode for logical Expr
kyotoYaho 5a44a48
Implement TreeNode for logical plan
kyotoYaho dad4390
Remove ExprRewriter
kyotoYaho 8111d74
Rename transform_using to rewrite and collect_using to visit in TreeNode
kyotoYaho 33138cb
Remove PlanVisitor
kyotoYaho 7babedd
Merge branch 'main' into issue-5609
kyotoYaho 7e51a74
Fix merge main branch
kyotoYaho 8a21d7d
Remove the rewrite.rs introduced by 258af4b
kyotoYaho 7563b33
Fix PR comments
kyotoYaho 15f0a78
Merge branch 'main' into issue-5609
kyotoYaho 7937a9c
Minor fix
kyotoYaho 992104f
Remove duplicated `TreeNode` definition in physical-expr
alamb 017fa78
Remove duplication in physical_plan
alamb 0f1aa4b
Merge pull request #70 from alamb/alamb/reduce_duplication
yahoNanJing 92cc2f1
Introduce enum Transformed to avoid clone in the TreeNode
kyotoYaho 128d6ed
Rename the trait ArcWithChildren to DynTreeNode
kyotoYaho 5a1f73b
Merge remote-tracking branch 'origin/main' into issue-5609
kyotoYaho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! This module provides common traits for visiting or rewriting tree nodes easily. | ||
|
||
use std::sync::Arc; | ||
|
||
use crate::Result; | ||
|
||
/// Trait for tree node. It can be [`ExecutionPlan`], [`PhysicalExpr`], [`LogicalPlan`], [`Expr`], etc. | ||
pub trait TreeNode: Sized { | ||
/// Use preorder to iterate the node on the tree so that we can stop fast for some cases. | ||
/// | ||
/// [`op`] can be used to collect some info from the tree node | ||
/// or do some checking for the tree node. | ||
fn apply<F>(&self, op: &mut F) -> Result<VisitRecursion> | ||
where | ||
F: FnMut(&Self) -> Result<VisitRecursion>, | ||
{ | ||
match op(self)? { | ||
VisitRecursion::Continue => {} | ||
// If the recursion should skip, do not apply to its children. And let the recursion continue | ||
VisitRecursion::Skip => return Ok(VisitRecursion::Continue), | ||
// If the recursion should stop, do not apply to its children | ||
VisitRecursion::Stop => return Ok(VisitRecursion::Stop), | ||
}; | ||
|
||
self.apply_children(&mut |node| node.apply(op)) | ||
} | ||
|
||
/// Visit the tree node using the given [TreeNodeVisitor] | ||
/// It performs a depth first walk of an node and its children. | ||
/// | ||
/// For an node tree such as | ||
/// ```text | ||
/// ParentNode | ||
/// left: ChildNode1 | ||
/// right: ChildNode2 | ||
/// ``` | ||
/// | ||
/// The nodes are visited using the following order | ||
/// ```text | ||
/// pre_visit(ParentNode) | ||
/// pre_visit(ChildNode1) | ||
/// post_visit(ChildNode1) | ||
/// pre_visit(ChildNode2) | ||
/// post_visit(ChildNode2) | ||
/// post_visit(ParentNode) | ||
/// ``` | ||
/// | ||
/// If an Err result is returned, recursion is stopped immediately | ||
/// | ||
/// If [`VisitRecursion::Stop`] is returned on a call to pre_visit, no | ||
/// children of that node will be visited, nor is post_visit | ||
/// called on that node. Details see [`TreeNodeVisitor`] | ||
/// | ||
/// If using the default [`post_visit`] with nothing to do, the [`apply`] should be preferred | ||
fn visit<V: TreeNodeVisitor<N = Self>>( | ||
&self, | ||
visitor: &mut V, | ||
) -> Result<VisitRecursion> { | ||
match visitor.pre_visit(self)? { | ||
VisitRecursion::Continue => {} | ||
// If the recursion should skip, do not apply to its children. And let the recursion continue | ||
VisitRecursion::Skip => return Ok(VisitRecursion::Continue), | ||
// If the recursion should stop, do not apply to its children | ||
VisitRecursion::Stop => return Ok(VisitRecursion::Stop), | ||
}; | ||
|
||
match self.apply_children(&mut |node| node.visit(visitor))? { | ||
VisitRecursion::Continue => {} | ||
// If the recursion should skip, do not apply to its children. And let the recursion continue | ||
VisitRecursion::Skip => return Ok(VisitRecursion::Continue), | ||
// If the recursion should stop, do not apply to its children | ||
VisitRecursion::Stop => return Ok(VisitRecursion::Stop), | ||
} | ||
|
||
visitor.post_visit(self) | ||
} | ||
|
||
/// Convenience utils for writing optimizers rule: recursively apply the given `op` to the node tree. | ||
/// When `op` does not apply to a given node, it is left unchanged. | ||
/// The default tree traversal direction is transform_up(Postorder Traversal). | ||
fn transform<F>(self, op: &F) -> Result<Self> | ||
where | ||
F: Fn(Self) -> Result<Transformed<Self>>, | ||
{ | ||
self.transform_up(op) | ||
} | ||
|
||
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' to the node and all of its | ||
/// children(Preorder Traversal). | ||
/// When the `op` does not apply to a given node, it is left unchanged. | ||
fn transform_down<F>(self, op: &F) -> Result<Self> | ||
where | ||
F: Fn(Self) -> Result<Transformed<Self>>, | ||
{ | ||
let after_op = op(self)?.into(); | ||
after_op.map_children(|node| node.transform_down(op)) | ||
} | ||
|
||
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' first to all of its | ||
/// children and then itself(Postorder Traversal). | ||
/// When the `op` does not apply to a given node, it is left unchanged. | ||
fn transform_up<F>(self, op: &F) -> Result<Self> | ||
where | ||
F: Fn(Self) -> Result<Transformed<Self>>, | ||
{ | ||
let after_op_children = self.map_children(|node| node.transform_up(op))?; | ||
|
||
let new_node = op(after_op_children)?.into(); | ||
Ok(new_node) | ||
} | ||
|
||
/// Transform the tree node using the given [TreeNodeRewriter] | ||
/// It performs a depth first walk of an node and its children. | ||
/// | ||
/// For an node tree such as | ||
/// ```text | ||
/// ParentNode | ||
/// left: ChildNode1 | ||
/// right: ChildNode2 | ||
/// ``` | ||
/// | ||
/// The nodes are visited using the following order | ||
/// ```text | ||
/// pre_visit(ParentNode) | ||
/// pre_visit(ChildNode1) | ||
/// mutate(ChildNode1) | ||
/// pre_visit(ChildNode2) | ||
/// mutate(ChildNode2) | ||
/// mutate(ParentNode) | ||
/// ``` | ||
/// | ||
/// If an Err result is returned, recursion is stopped immediately | ||
/// | ||
/// If [`false`] is returned on a call to pre_visit, no | ||
/// children of that node will be visited, nor is mutate | ||
/// called on that node | ||
/// | ||
/// If using the default [`pre_visit`] with [`true`] returned, the [`transform`] should be preferred | ||
fn rewrite<R: TreeNodeRewriter<N = Self>>(self, rewriter: &mut R) -> Result<Self> { | ||
let need_mutate = match rewriter.pre_visit(&self)? { | ||
RewriteRecursion::Mutate => return rewriter.mutate(self), | ||
RewriteRecursion::Stop => return Ok(self), | ||
RewriteRecursion::Continue => true, | ||
RewriteRecursion::Skip => false, | ||
}; | ||
|
||
let after_op_children = self.map_children(|node| node.rewrite(rewriter))?; | ||
|
||
// now rewrite this node itself | ||
if need_mutate { | ||
rewriter.mutate(after_op_children) | ||
} else { | ||
Ok(after_op_children) | ||
} | ||
} | ||
|
||
/// Apply the closure `F` to the node's children | ||
fn apply_children<F>(&self, op: &mut F) -> Result<VisitRecursion> | ||
where | ||
F: FnMut(&Self) -> Result<VisitRecursion>; | ||
|
||
/// Apply transform `F` to the node's children, the transform `F` might have a direction(Preorder or Postorder) | ||
fn map_children<F>(self, transform: F) -> Result<Self> | ||
where | ||
F: FnMut(Self) -> Result<Self>; | ||
} | ||
|
||
/// Implements the [visitor | ||
/// pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for recursively walking [`TreeNode`]s. | ||
/// | ||
/// [`TreeNodeVisitor`] allows keeping the algorithms | ||
/// separate from the code to traverse the structure of the `TreeNode` | ||
/// tree and makes it easier to add new types of tree node and | ||
/// algorithms. | ||
/// | ||
/// When passed to[`TreeNode::visit`], [`TreeNode::pre_visit`] | ||
/// and [`TreeNode::post_visit`] are invoked recursively | ||
/// on an node tree. | ||
/// | ||
/// If an [`Err`] result is returned, recursion is stopped | ||
/// immediately. | ||
/// | ||
/// If [`VisitRecursion::Stop`] is returned on a call to pre_visit, no | ||
/// children of that tree node are visited, nor is post_visit | ||
/// called on that tree node | ||
/// | ||
/// If [`VisitRecursion::Stop`] is returned on a call to post_visit, no | ||
/// siblings of that tree node are visited, nor is post_visit | ||
/// called on its parent tree node | ||
/// | ||
/// If [`VisitRecursion::Skip`] is returned on a call to pre_visit, no | ||
/// children of that tree node are visited. | ||
pub trait TreeNodeVisitor: Sized { | ||
/// The node type which is visitable. | ||
type N: TreeNode; | ||
|
||
/// Invoked before any children of `node` are visited. | ||
fn pre_visit(&mut self, node: &Self::N) -> Result<VisitRecursion>; | ||
|
||
/// Invoked after all children of `node` are visited. Default | ||
/// implementation does nothing. | ||
fn post_visit(&mut self, _node: &Self::N) -> Result<VisitRecursion> { | ||
Ok(VisitRecursion::Continue) | ||
} | ||
} | ||
|
||
/// Trait for potentially recursively transform an [`TreeNode`] node | ||
/// tree. When passed to `TreeNode::rewrite`, `TreeNodeRewriter::mutate` is | ||
/// invoked recursively on all nodes of a tree. | ||
pub trait TreeNodeRewriter: Sized { | ||
/// The node type which is rewritable. | ||
type N: TreeNode; | ||
|
||
/// Invoked before (Preorder) any children of `node` are rewritten / | ||
/// visited. Default implementation returns `Ok(Recursion::Continue)` | ||
fn pre_visit(&mut self, _node: &Self::N) -> Result<RewriteRecursion> { | ||
Ok(RewriteRecursion::Continue) | ||
} | ||
|
||
/// Invoked after (Postorder) all children of `node` have been mutated and | ||
/// returns a potentially modified node. | ||
fn mutate(&mut self, node: Self::N) -> Result<Self::N>; | ||
} | ||
|
||
/// Controls how the [TreeNode] recursion should proceed for [`rewrite`]. | ||
#[derive(Debug)] | ||
pub enum RewriteRecursion { | ||
/// Continue rewrite this node tree. | ||
Continue, | ||
/// Call 'op' immediately and return. | ||
Mutate, | ||
/// Do not rewrite the children of this node. | ||
Stop, | ||
/// Keep recursive but skip apply op on this node | ||
Skip, | ||
} | ||
|
||
/// Controls how the [TreeNode] recursion should proceed for [`visit`]. | ||
#[derive(Debug)] | ||
pub enum VisitRecursion { | ||
/// Continue the visit to this node tree. | ||
Continue, | ||
/// Keep recursive but skip applying op on the children | ||
Skip, | ||
/// Stop the visit to this node tree. | ||
Stop, | ||
} | ||
|
||
pub enum Transformed<T> { | ||
/// The item was transformed / rewritten somehow | ||
Yes(T), | ||
/// The item was not transformed | ||
No(T), | ||
} | ||
|
||
impl<T> Transformed<T> { | ||
pub fn into(self) -> T { | ||
match self { | ||
Transformed::Yes(t) => t, | ||
Transformed::No(t) => t, | ||
} | ||
} | ||
|
||
pub fn into_pair(self) -> (T, bool) { | ||
match self { | ||
Transformed::Yes(t) => (t, true), | ||
Transformed::No(t) => (t, false), | ||
} | ||
} | ||
} | ||
|
||
/// Helper trait for implementing [`TreeNode`] that have children stored as Arc's | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very clever design. I tried this refactor before and somewhat gave up because of Rust's orphan rule (i.e. you cannot |
||
/// | ||
/// If some trait object, such as `dyn T`, implements this trait, | ||
/// its related Arc<dyn T> will automatically implement [`TreeNode`] | ||
pub trait DynTreeNode { | ||
/// Returns all children of the specified TreeNode | ||
fn arc_children(&self) -> Vec<Arc<Self>>; | ||
|
||
/// construct a new self with the specified children | ||
fn with_new_arc_children( | ||
&self, | ||
arc_self: Arc<Self>, | ||
new_children: Vec<Arc<Self>>, | ||
) -> Result<Arc<Self>>; | ||
} | ||
|
||
/// Blanket implementation for Arc for any tye that implements | ||
/// [`DynTreeNode`] (such as Arc<dyn PhysicalExpr>) | ||
impl<T: DynTreeNode + ?Sized> TreeNode for Arc<T> { | ||
fn apply_children<F>(&self, op: &mut F) -> Result<VisitRecursion> | ||
where | ||
F: FnMut(&Self) -> Result<VisitRecursion>, | ||
{ | ||
for child in self.arc_children() { | ||
match op(&child)? { | ||
VisitRecursion::Continue => {} | ||
VisitRecursion::Skip => return Ok(VisitRecursion::Continue), | ||
VisitRecursion::Stop => return Ok(VisitRecursion::Stop), | ||
} | ||
} | ||
|
||
Ok(VisitRecursion::Continue) | ||
} | ||
|
||
fn map_children<F>(self, transform: F) -> Result<Self> | ||
where | ||
F: FnMut(Self) -> Result<Self>, | ||
{ | ||
let children = self.arc_children(); | ||
if !children.is_empty() { | ||
let new_children: Result<Vec<_>> = | ||
children.into_iter().map(transform).collect(); | ||
let arc_self = Arc::clone(&self); | ||
self.with_new_arc_children(arc_self, new_children?) | ||
} else { | ||
Ok(self) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API looks very nice 👌