## Coverage Report

#### Created: 2025-11-22 06:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /Users/andrewlamb/Software/datafusion/datafusion/physical-expr-common/src/tree_node.rs

    Line
    Count
    Source
    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
    20
    use std::fmt::{self, Display, Formatter};
    21
    use std::sync::Arc;
    22
    23
    use crate::physical_expr::{with_new_children_if_necessary, PhysicalExpr};
    24
    25
    use datafusion_common::tree_node::{ConcreteTreeNode, DynTreeNode};
    26
    use datafusion_common::Result;
    27
    28
    impl DynTreeNode for dyn PhysicalExpr {
    29
    3
        fn arc_children(&self) -> Vec<&Arc<Self>> {
    30
    3
            self.children()
    31
    3
        }
    32
    33
    1
        fn with_new_arc_children(
    34
    1
            &self,
    35
    1
            arc_self: Arc<Self>,
    36
    1
            new_children: Vec<Arc<Self>>,
    37
    1
        ) -> Result<Arc<Self>> {
    38
    1
            with_new_children_if_necessary(arc_self, new_children)
    39
    1
        }
    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)]
    46
    pub 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
    55
    impl<T> ExprContext<T> {
    56
    0
        pub fn new(expr: Arc<dyn PhysicalExpr>, data: T, children: Vec<Self>) -> Self {
    57
    0
            Self {
    58
    0
                expr,
    59
    0
                data,
    60
    0
                children,
    61
    0
            }
    62
    0
        }
    63
    64
    0
        pub fn update_expr_from_children(mut self) -> Result<Self> {
    65
    0
            let children_expr = self.children.iter().map(|c| Arc::clone(&c.expr)).collect();
    66
    0
            self.expr = with_new_children_if_necessary(self.expr, children_expr)?;
    67
    0
            Ok(self)
    68
    0
        }
    69
    }
    70
    71
    impl<T: Default> ExprContext<T> {
    72
    0
        pub fn new_default(plan: Arc<dyn PhysicalExpr>) -> Self {
    73
    0
            let children = plan
    74
    0
                .children()
    75
    0
                .into_iter()
    76
    0
                .cloned()
    77
    0
                .map(Self::new_default)
    78
    0
                .collect();
    79
    0
            Self::new(plan, Default::default(), children)
    80
    0
        }
    81
    }
    82
    83
    impl<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
    91
    impl<T> ConcreteTreeNode for ExprContext<T> {
    92
        fn children(&self) -> &[Self] {
    93
            &self.children
    94
        }
    95
    96
    0
        fn take_children(mut self) -> (Self, Vec<Self>) {
    97
    0
            let children = std::mem::take(&mut self.children);
    98
    0
            (self, children)
    99
    0
        }
    100
    101
    0
        fn with_new_children(mut self, children: Vec<Self>) -> Result<Self> {
    102
    0
            self.children = children;
    103
    0
            self.update_expr_from_children()
    104
    0
        }
    105
    }