|
| 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 | +//! Implementation of physical plan display. See |
| 19 | +//! [`crate::physical_plan::displayable`] for examples of how to |
| 20 | +//! format |
| 21 | +
|
| 22 | +use std::fmt; |
| 23 | + |
| 24 | +use super::{accept, ExecutionPlan, ExecutionPlanVisitor}; |
| 25 | + |
| 26 | +/// Options for controlling how each [`ExecutionPlan`] should format itself |
| 27 | +#[derive(Debug, Clone, Copy)] |
| 28 | +pub enum DisplayFormatType { |
| 29 | + /// Default, compact format. Example: `FilterExec: c12 < 10.0` |
| 30 | + Default, |
| 31 | +} |
| 32 | + |
| 33 | +/// Wraps an `ExecutionPlan` with various ways to display this plan |
| 34 | +pub struct DisplayableExecutionPlan<'a> { |
| 35 | + inner: &'a dyn ExecutionPlan, |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a> DisplayableExecutionPlan<'a> { |
| 39 | + /// Create a wrapper around an [`'ExecutionPlan'] which can be |
| 40 | + /// pretty printed in a variety of ways |
| 41 | + pub fn new(inner: &'a dyn ExecutionPlan) -> Self { |
| 42 | + Self { inner } |
| 43 | + } |
| 44 | + |
| 45 | + /// Return a `format`able structure that produces a single line |
| 46 | + /// per node. |
| 47 | + /// |
| 48 | + /// ```text |
| 49 | + /// ProjectionExec: expr=[a] |
| 50 | + /// CoalesceBatchesExec: target_batch_size=4096 |
| 51 | + /// FilterExec: a < 5 |
| 52 | + /// RepartitionExec: partitioning=RoundRobinBatch(16) |
| 53 | + /// CsvExec: source=...", |
| 54 | + /// ``` |
| 55 | + pub fn indent(&self) -> impl fmt::Display + 'a { |
| 56 | + struct Wrapper<'a>(&'a dyn ExecutionPlan); |
| 57 | + impl<'a> fmt::Display for Wrapper<'a> { |
| 58 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 59 | + let t = DisplayFormatType::Default; |
| 60 | + let mut visitor = IndentVisitor { t, f, indent: 0 }; |
| 61 | + accept(self.0, &mut visitor) |
| 62 | + } |
| 63 | + } |
| 64 | + Wrapper(self.inner) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Formats plans with a single line per node. |
| 69 | +struct IndentVisitor<'a, 'b> { |
| 70 | + /// How to format each node |
| 71 | + t: DisplayFormatType, |
| 72 | + /// Write to this formatter |
| 73 | + f: &'a mut fmt::Formatter<'b>, |
| 74 | + ///with_schema: bool, |
| 75 | + indent: usize, |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> { |
| 79 | + type Error = fmt::Error; |
| 80 | + fn pre_visit( |
| 81 | + &mut self, |
| 82 | + plan: &dyn ExecutionPlan, |
| 83 | + ) -> std::result::Result<bool, Self::Error> { |
| 84 | + write!(self.f, "{:indent$}", "", indent = self.indent * 2)?; |
| 85 | + plan.fmt_as(self.t, self.f)?; |
| 86 | + writeln!(self.f)?; |
| 87 | + self.indent += 1; |
| 88 | + Ok(true) |
| 89 | + } |
| 90 | +} |
0 commit comments