Skip to main content

datafusion_physical_expr_common/
tree_node.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! This module provides common traits for visiting or rewriting tree nodes easily.
6
7use std::fmt::{self, Display, Formatter};
8use std::sync::Arc;
9
10use crate::physical_expr::{PhysicalExpr, with_new_children_if_necessary};
11
12use datafusion_common::Result;
13use datafusion_common::tree_node::{ConcreteTreeNode, DynTreeNode};
14
15impl DynTreeNode for dyn PhysicalExpr {
16    fn arc_children(&self) -> Vec<&Arc<Self>> {
17        self.children()
18    }
19
20    fn with_new_arc_children(
21        &self,
22        arc_self: Arc<Self>,
23        new_children: Vec<Arc<Self>>,
24    ) -> Result<Arc<Self>> {
25        with_new_children_if_necessary(arc_self, new_children)
26    }
27}
28
29/// A node object encapsulating a [`PhysicalExpr`] node with a payload. Since there are
30/// two ways to access child plans—directly from the plan  and through child nodes—it's
31/// recommended to perform mutable operations via [`Self::update_expr_from_children`].
32#[derive(Debug)]
33pub struct ExprContext<T: Sized> {
34    /// The physical expression associated with this context.
35    pub expr: Arc<dyn PhysicalExpr>,
36    /// Custom data payload of the node.
37    pub data: T,
38    /// Child contexts of this node.
39    pub children: Vec<Self>,
40}
41
42impl<T> ExprContext<T> {
43    pub fn new(expr: Arc<dyn PhysicalExpr>, data: T, children: Vec<Self>) -> Self {
44        Self {
45            expr,
46            data,
47            children,
48        }
49    }
50
51    pub fn update_expr_from_children(mut self) -> Result<Self> {
52        let children_expr = self.children.iter().map(|c| Arc::clone(&c.expr)).collect();
53        self.expr = with_new_children_if_necessary(self.expr, children_expr)?;
54        Ok(self)
55    }
56}
57
58impl<T: Default> ExprContext<T> {
59    pub fn new_default(plan: Arc<dyn PhysicalExpr>) -> Self {
60        let children = plan
61            .children()
62            .into_iter()
63            .cloned()
64            .map(Self::new_default)
65            .collect();
66        Self::new(plan, Default::default(), children)
67    }
68}
69
70impl<T: Display> Display for ExprContext<T> {
71    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
72        write!(f, "expr: {:?}", self.expr)?;
73        write!(f, "data:{}", self.data)?;
74        write!(f, "")
75    }
76}
77
78impl<T> ConcreteTreeNode for ExprContext<T> {
79    fn children(&self) -> &[Self] {
80        &self.children
81    }
82
83    fn take_children(mut self) -> (Self, Vec<Self>) {
84        let children = std::mem::take(&mut self.children);
85        (self, children)
86    }
87
88    fn with_new_children(mut self, children: Vec<Self>) -> Result<Self> {
89        self.children = children;
90        self.update_expr_from_children()
91    }
92}