Skip to content

Commit 589bfd4

Browse files
authored
Minor: add examples for using displayable to show ExxecutionPlans (#13636)
1 parent 749cd6f commit 589bfd4

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

datafusion/physical-plan/src/display.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,39 @@ pub enum DisplayFormatType {
3838
Verbose,
3939
}
4040

41-
/// Wraps an `ExecutionPlan` with various ways to display this plan
41+
/// Wraps an `ExecutionPlan` with various methods for formatting
42+
///
43+
///
44+
/// # Example
45+
/// ```
46+
/// # use std::sync::Arc;
47+
/// # use arrow_schema::{Field, Schema, DataType};
48+
/// # use datafusion_expr::Operator;
49+
/// # use datafusion_physical_expr::expressions::{binary, col, lit};
50+
/// # use datafusion_physical_plan::{displayable, ExecutionPlan};
51+
/// # use datafusion_physical_plan::empty::EmptyExec;
52+
/// # use datafusion_physical_plan::filter::FilterExec;
53+
/// # let schema = Schema::new(vec![Field::new("i", DataType::Int32, false)]);
54+
/// # let plan = EmptyExec::new(Arc::new(schema));
55+
/// # let i = col("i", &plan.schema()).unwrap();
56+
/// # let predicate = binary(i, Operator::Eq, lit(1), &plan.schema()).unwrap();
57+
/// # let plan: Arc<dyn ExecutionPlan> = Arc::new(FilterExec::try_new(predicate, Arc::new(plan)).unwrap());
58+
/// // Get a one line description (Displayable)
59+
/// let display_plan = displayable(plan.as_ref());
60+
///
61+
/// // you can use the returned objects to format plans
62+
/// // where you can use `Display` such as format! or println!
63+
/// assert_eq!(
64+
/// &format!("The plan is: {}", display_plan.one_line()),
65+
/// "The plan is: FilterExec: i@0 = 1\n"
66+
/// );
67+
/// // You can also print out the plan and its children in indented mode
68+
/// assert_eq!(display_plan.indent(false).to_string(),
69+
/// "FilterExec: i@0 = 1\
70+
/// \n EmptyExec\
71+
/// \n"
72+
/// );
73+
/// ```
4274
#[derive(Debug, Clone)]
4375
pub struct DisplayableExecutionPlan<'a> {
4476
inner: &'a dyn ExecutionPlan,

datafusion/physical-plan/src/execution_plan.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,11 @@ pub fn with_new_children_if_necessary(
706706
}
707707
}
708708

709-
/// Return a [wrapper](DisplayableExecutionPlan) around an
709+
/// Return a [`DisplayableExecutionPlan`] wrapper around an
710710
/// [`ExecutionPlan`] which can be displayed in various easier to
711711
/// understand ways.
712+
///
713+
/// See examples on [`DisplayableExecutionPlan`]
712714
pub fn displayable(plan: &dyn ExecutionPlan) -> DisplayableExecutionPlan<'_> {
713715
DisplayableExecutionPlan::new(plan)
714716
}

0 commit comments

Comments
 (0)