Skip to content

Apache DataFusion SQL Query Engine

Add single line description of ExecutionPlan (#2216) (#2217)

Raphael Taylor-Davies7a6317a0e318
diff --git a/datafusion/core/src/physical_plan/display.rs b/datafusion/core/src/physical_plan/display.rs
index 19a859a0f0..aa02af12da 100644
--- a/datafusion/core/src/physical_plan/display.rs
+++ b/datafusion/core/src/physical_plan/display.rs
@@ -101,6 +101,33 @@ impl<'a> DisplayableExecutionPlan<'a> {
show_metrics: self.show_metrics,
}
}
+
+ /// Return a single-line summary of the root of the plan
+ /// Example: `ProjectionExec: expr=[a@0 as a]`.
+ pub fn one_line(&self) -> impl fmt::Display + 'a {
+ struct Wrapper<'a> {
+ plan: &'a dyn ExecutionPlan,
+ show_metrics: ShowMetrics,
+ }
+
+ impl<'a> fmt::Display for Wrapper<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let mut visitor = IndentVisitor {
+ f,
+ t: DisplayFormatType::Default,
+ indent: 0,
+ show_metrics: self.show_metrics,
+ };
+ visitor.pre_visit(self.plan)?;
+ Ok(())
+ }
+ }
+
+ Wrapper {
+ plan: self.inner,
+ show_metrics: self.show_metrics,
+ }
+ }
}
#[derive(Debug, Clone, Copy)]
diff --git a/datafusion/core/src/physical_plan/mod.rs b/datafusion/core/src/physical_plan/mod.rs
index 9d45ece616..f3d9ced624 100644
--- a/datafusion/core/src/physical_plan/mod.rs
+++ b/datafusion/core/src/physical_plan/mod.rs
@@ -318,6 +318,9 @@ pub fn with_new_children_if_necessary(
/// \n RepartitionExec: partitioning=RoundRobinBatch(3)\
/// \n CsvExec: files=[tests/example.csv], has_header=true, limit=None, projection=[a]",
/// plan_string.trim());
+///
+/// let one_line = format!("{}", displayable_plan.one_line());
+/// assert_eq!("ProjectionExec: expr=[a@0 as a]", one_line.trim());
/// }
/// ```
///