datafusion_physical_plan/
explain.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//! Defines the EXPLAIN operator
19
20use std::any::Any;
21use std::sync::Arc;
22
23use super::{DisplayAs, PlanProperties, SendableRecordBatchStream};
24use crate::execution_plan::{Boundedness, EmissionType};
25use crate::stream::RecordBatchStreamAdapter;
26use crate::{DisplayFormatType, ExecutionPlan, Partitioning};
27
28use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};
29use datafusion_common::display::StringifiedPlan;
30use datafusion_common::{internal_err, Result};
31use datafusion_execution::TaskContext;
32use datafusion_physical_expr::EquivalenceProperties;
33
34use log::trace;
35
36/// Explain execution plan operator. This operator contains the string
37/// values of the various plans it has when it is created, and passes
38/// them to its output.
39#[derive(Debug, Clone)]
40pub struct ExplainExec {
41    /// The schema that this exec plan node outputs
42    schema: SchemaRef,
43    /// The strings to be printed
44    stringified_plans: Vec<StringifiedPlan>,
45    /// control which plans to print
46    verbose: bool,
47    cache: PlanProperties,
48}
49
50impl ExplainExec {
51    /// Create a new ExplainExec
52    pub fn new(
53        schema: SchemaRef,
54        stringified_plans: Vec<StringifiedPlan>,
55        verbose: bool,
56    ) -> Self {
57        let cache = Self::compute_properties(Arc::clone(&schema));
58        ExplainExec {
59            schema,
60            stringified_plans,
61            verbose,
62            cache,
63        }
64    }
65
66    /// The strings to be printed
67    pub fn stringified_plans(&self) -> &[StringifiedPlan] {
68        &self.stringified_plans
69    }
70
71    /// Access to verbose
72    pub fn verbose(&self) -> bool {
73        self.verbose
74    }
75
76    /// This function creates the cache object that stores the plan properties such as schema, equivalence properties, ordering, partitioning, etc.
77    fn compute_properties(schema: SchemaRef) -> PlanProperties {
78        PlanProperties::new(
79            EquivalenceProperties::new(schema),
80            Partitioning::UnknownPartitioning(1),
81            EmissionType::Final,
82            Boundedness::Bounded,
83        )
84    }
85}
86
87impl DisplayAs for ExplainExec {
88    fn fmt_as(
89        &self,
90        t: DisplayFormatType,
91        f: &mut std::fmt::Formatter,
92    ) -> std::fmt::Result {
93        match t {
94            DisplayFormatType::Default | DisplayFormatType::Verbose => {
95                write!(f, "ExplainExec")
96            }
97        }
98    }
99}
100
101impl ExecutionPlan for ExplainExec {
102    fn name(&self) -> &'static str {
103        "ExplainExec"
104    }
105
106    /// Return a reference to Any that can be used for downcasting
107    fn as_any(&self) -> &dyn Any {
108        self
109    }
110
111    fn properties(&self) -> &PlanProperties {
112        &self.cache
113    }
114
115    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
116        // This is a leaf node and has no children
117        vec![]
118    }
119
120    fn with_new_children(
121        self: Arc<Self>,
122        _: Vec<Arc<dyn ExecutionPlan>>,
123    ) -> Result<Arc<dyn ExecutionPlan>> {
124        Ok(self)
125    }
126
127    fn execute(
128        &self,
129        partition: usize,
130        context: Arc<TaskContext>,
131    ) -> Result<SendableRecordBatchStream> {
132        trace!("Start ExplainExec::execute for partition {} of context session_id {} and task_id {:?}", partition, context.session_id(), context.task_id());
133        if 0 != partition {
134            return internal_err!("ExplainExec invalid partition {partition}");
135        }
136        let mut type_builder =
137            StringBuilder::with_capacity(self.stringified_plans.len(), 1024);
138        let mut plan_builder =
139            StringBuilder::with_capacity(self.stringified_plans.len(), 1024);
140
141        let plans_to_print = self
142            .stringified_plans
143            .iter()
144            .filter(|s| s.should_display(self.verbose));
145
146        // Identify plans that are not changed
147        let mut prev: Option<&StringifiedPlan> = None;
148
149        for p in plans_to_print {
150            type_builder.append_value(p.plan_type.to_string());
151            match prev {
152                Some(prev) if !should_show(prev, p) => {
153                    plan_builder.append_value("SAME TEXT AS ABOVE");
154                }
155                Some(_) | None => {
156                    plan_builder.append_value(&*p.plan);
157                }
158            }
159            prev = Some(p);
160        }
161
162        let record_batch = RecordBatch::try_new(
163            Arc::clone(&self.schema),
164            vec![
165                Arc::new(type_builder.finish()),
166                Arc::new(plan_builder.finish()),
167            ],
168        )?;
169
170        trace!(
171            "Before returning RecordBatchStream in ExplainExec::execute for partition {} of context session_id {} and task_id {:?}", partition, context.session_id(), context.task_id());
172
173        Ok(Box::pin(RecordBatchStreamAdapter::new(
174            Arc::clone(&self.schema),
175            futures::stream::iter(vec![Ok(record_batch)]),
176        )))
177    }
178}
179
180/// If this plan should be shown, given the previous plan that was
181/// displayed.
182///
183/// This is meant to avoid repeating the same plan over and over again
184/// in explain plans to make clear what is changing
185fn should_show(previous_plan: &StringifiedPlan, this_plan: &StringifiedPlan) -> bool {
186    // if the plans are different, or if they would have been
187    // displayed in the normal explain (aka non verbose) plan
188    (previous_plan.plan != this_plan.plan) || this_plan.should_display(false)
189}