/Users/andrewlamb/Software/datafusion/datafusion/physical-plan/src/tree_node.rs
Line | Count | Source (jump to first uncovered line) |
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::{displayable, with_new_children_if_necessary, ExecutionPlan}; |
24 | | |
25 | | use datafusion_common::tree_node::{ConcreteTreeNode, DynTreeNode}; |
26 | | use datafusion_common::Result; |
27 | | |
28 | | impl DynTreeNode for dyn ExecutionPlan { |
29 | 0 | fn arc_children(&self) -> Vec<&Arc<Self>> { |
30 | 0 | self.children() |
31 | 0 | } |
32 | | |
33 | 0 | fn with_new_arc_children( |
34 | 0 | &self, |
35 | 0 | arc_self: Arc<Self>, |
36 | 0 | new_children: Vec<Arc<Self>>, |
37 | 0 | ) -> Result<Arc<Self>> { |
38 | 0 | with_new_children_if_necessary(arc_self, new_children) |
39 | 0 | } |
40 | | } |
41 | | |
42 | | /// A node object beneficial for writing optimizer rules, encapsulating an [`ExecutionPlan`] node with a payload. |
43 | | /// Since there are two ways to access child plans—directly from the plan and through child nodes—it's recommended |
44 | | /// to perform mutable operations via [`Self::update_plan_from_children`]. |
45 | | #[derive(Debug)] |
46 | | pub struct PlanContext<T: Sized> { |
47 | | /// The execution plan associated with this context. |
48 | | pub plan: Arc<dyn ExecutionPlan>, |
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> PlanContext<T> { |
56 | 0 | pub fn new(plan: Arc<dyn ExecutionPlan>, data: T, children: Vec<Self>) -> Self { |
57 | 0 | Self { |
58 | 0 | plan, |
59 | 0 | data, |
60 | 0 | children, |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | 0 | pub fn update_plan_from_children(mut self) -> Result<Self> { |
65 | 0 | let children_plans = self.children.iter().map(|c| Arc::clone(&c.plan)).collect(); |
66 | 0 | self.plan = with_new_children_if_necessary(self.plan, children_plans)?; |
67 | | |
68 | 0 | Ok(self) |
69 | 0 | } |
70 | | } |
71 | | |
72 | | impl<T: Default> PlanContext<T> { |
73 | 0 | pub fn new_default(plan: Arc<dyn ExecutionPlan>) -> Self { |
74 | 0 | let children = plan |
75 | 0 | .children() |
76 | 0 | .into_iter() |
77 | 0 | .cloned() |
78 | 0 | .map(Self::new_default) |
79 | 0 | .collect(); |
80 | 0 | Self::new(plan, Default::default(), children) |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl<T: Display> Display for PlanContext<T> { |
85 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
86 | 0 | let node_string = displayable(self.plan.as_ref()).one_line(); |
87 | 0 | write!(f, "Node plan: {}", node_string)?; |
88 | 0 | write!(f, "Node data: {}", self.data)?; |
89 | 0 | write!(f, "") |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl<T> ConcreteTreeNode for PlanContext<T> { |
94 | 0 | fn children(&self) -> &[Self] { |
95 | 0 | &self.children |
96 | 0 | } |
97 | | |
98 | 0 | fn take_children(mut self) -> (Self, Vec<Self>) { |
99 | 0 | let children = std::mem::take(&mut self.children); |
100 | 0 | (self, children) |
101 | 0 | } |
102 | | |
103 | 0 | fn with_new_children(mut self, children: Vec<Self>) -> Result<Self> { |
104 | 0 | self.children = children; |
105 | 0 | self.update_plan_from_children() |
106 | 0 | } |
107 | | } |