datafusion_physical_expr_common/
tree_node.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! This module provides common traits for visiting or rewriting tree nodes easily.
19
20use std::fmt::{self, Display, Formatter};
21use std::sync::Arc;
22
23use crate::physical_expr::{with_new_children_if_necessary, PhysicalExpr};
24
25use datafusion_common::tree_node::{ConcreteTreeNode, DynTreeNode};
26use datafusion_common::Result;
27
28impl DynTreeNode for dyn PhysicalExpr {
29    fn arc_children(&self) -> Vec<&Arc<Self>> {
30        self.children()
31    }
32
33    fn with_new_arc_children(
34        &self,
35        arc_self: Arc<Self>,
36        new_children: Vec<Arc<Self>>,
37    ) -> Result<Arc<Self>> {
38        with_new_children_if_necessary(arc_self, new_children)
39    }
40}
41
42/// A node object encapsulating a [`PhysicalExpr`] node with a payload. Since there are
43/// two ways to access child plans—directly from the plan  and through child nodes—it's
44/// recommended to perform mutable operations via [`Self::update_expr_from_children`].
45#[derive(Debug)]
46pub struct ExprContext<T: Sized> {
47    /// The physical expression associated with this context.
48    pub expr: Arc<dyn PhysicalExpr>,
49    /// Custom data payload of the node.
50    pub data: T,
51    /// Child contexts of this node.
52    pub children: Vec<Self>,
53}
54
55impl<T> ExprContext<T> {
56    pub fn new(expr: Arc<dyn PhysicalExpr>, data: T, children: Vec<Self>) -> Self {
57        Self {
58            expr,
59            data,
60            children,
61        }
62    }
63
64    pub fn update_expr_from_children(mut self) -> Result<Self> {
65        let children_expr = self.children.iter().map(|c| Arc::clone(&c.expr)).collect();
66        self.expr = with_new_children_if_necessary(self.expr, children_expr)?;
67        Ok(self)
68    }
69}
70
71impl<T: Default> ExprContext<T> {
72    pub fn new_default(plan: Arc<dyn PhysicalExpr>) -> Self {
73        let children = plan
74            .children()
75            .into_iter()
76            .cloned()
77            .map(Self::new_default)
78            .collect();
79        Self::new(plan, Default::default(), children)
80    }
81}
82
83impl<T: Display> Display for ExprContext<T> {
84    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
85        write!(f, "expr: {:?}", self.expr)?;
86        write!(f, "data:{}", self.data)?;
87        write!(f, "")
88    }
89}
90
91impl<T> ConcreteTreeNode for ExprContext<T> {
92    fn children(&self) -> &[Self] {
93        &self.children
94    }
95
96    fn take_children(mut self) -> (Self, Vec<Self>) {
97        let children = std::mem::take(&mut self.children);
98        (self, children)
99    }
100
101    fn with_new_children(mut self, children: Vec<Self>) -> Result<Self> {
102        self.children = children;
103        self.update_expr_from_children()
104    }
105}