mingmwang commented on code in PR #5322: URL: https://github.com/apache/arrow-datafusion/pull/5322#discussion_r1116468631
########## datafusion/physical-expr/src/utils.rs: ########## @@ -235,6 +234,82 @@ pub fn ordering_satisfy_concrete<F: FnOnce() -> EquivalenceProperties>( } } +#[derive(Clone, Debug)] +pub struct ExprTreeNode<T> { + expr: Arc<dyn PhysicalExpr>, + data: Option<T>, + child_nodes: Vec<ExprTreeNode<T>>, +} + +impl<T> ExprTreeNode<T> { + pub fn new(expr: Arc<dyn PhysicalExpr>) -> Self { + ExprTreeNode { + expr, + data: None, + child_nodes: vec![], + } + } + + pub fn expression(&self) -> &Arc<dyn PhysicalExpr> { + &self.expr + } + + pub fn children(&self) -> Vec<ExprTreeNode<T>> { + self.expr + .children() + .into_iter() + .map(ExprTreeNode::new) + .collect() + } +} + +impl<T: Clone> TreeNodeRewritable for ExprTreeNode<T> { + fn map_children<F>(mut self, transform: F) -> Result<Self> + where + F: FnMut(Self) -> Result<Self>, + { + self.child_nodes = self + .children() + .into_iter() + .map(transform) + .collect::<Result<Vec<_>>>()?; + Ok(self) + } +} + +/// This function converts the [PhysicalExpr] tree into a DAG by collecting identical +/// expressions in one node. Caller specifies the node type in this DAG via the +/// `constructor` argument, which constructs nodes in this DAG from the [ExprTreeNode] +/// ancillary object. +pub fn build_dag<T, F>( + expr: Arc<dyn PhysicalExpr>, + constructor: &F, +) -> Result<(NodeIndex, StableGraph<T, usize>)> Review Comment: Is there any UT for the `build_dag` ? I try to understand the implementation but there are so many structs involved here and I get lost soon. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org