Skip to main content

datafusion_common/display/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Types for plan display
6
7mod graphviz;
8pub mod human_readable;
9pub use graphviz::*;
10
11use std::{
12    fmt::{self, Display, Formatter},
13    sync::Arc,
14};
15
16/// Represents which type of plan, when storing multiple
17/// for use in EXPLAIN plans
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
19pub enum PlanType {
20    /// The initial LogicalPlan provided to DataFusion
21    InitialLogicalPlan,
22    /// The LogicalPlan which results from applying an analyzer pass
23    AnalyzedLogicalPlan {
24        /// The name of the analyzer which produced this plan
25        analyzer_name: String,
26    },
27    /// The LogicalPlan after all analyzer passes have been applied
28    FinalAnalyzedLogicalPlan,
29    /// The LogicalPlan which results from applying an optimizer pass
30    OptimizedLogicalPlan {
31        /// The name of the optimizer which produced this plan
32        optimizer_name: String,
33    },
34    /// The final, fully optimized LogicalPlan that was converted to a physical plan
35    FinalLogicalPlan,
36    /// The initial physical plan, prepared for execution
37    InitialPhysicalPlan,
38    /// The initial physical plan with stats, prepared for execution
39    InitialPhysicalPlanWithStats,
40    /// The initial physical plan with schema, prepared for execution
41    InitialPhysicalPlanWithSchema,
42    /// The ExecutionPlan which results from applying an optimizer pass
43    OptimizedPhysicalPlan {
44        /// The name of the optimizer which produced this plan
45        optimizer_name: String,
46    },
47    /// The final, fully optimized physical plan which would be executed
48    FinalPhysicalPlan,
49    /// The final with stats, fully optimized physical plan which would be executed
50    FinalPhysicalPlanWithStats,
51    /// The final with schema, fully optimized physical plan which would be executed
52    FinalPhysicalPlanWithSchema,
53    /// An error creating the physical plan
54    PhysicalPlanError,
55}
56
57impl Display for PlanType {
58    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
59        match self {
60            PlanType::InitialLogicalPlan => write!(f, "initial_logical_plan"),
61            PlanType::AnalyzedLogicalPlan { analyzer_name } => {
62                write!(f, "logical_plan after {analyzer_name}")
63            }
64            PlanType::FinalAnalyzedLogicalPlan => write!(f, "analyzed_logical_plan"),
65            PlanType::OptimizedLogicalPlan { optimizer_name } => {
66                write!(f, "logical_plan after {optimizer_name}")
67            }
68            PlanType::FinalLogicalPlan => write!(f, "logical_plan"),
69            PlanType::InitialPhysicalPlan => write!(f, "initial_physical_plan"),
70            PlanType::InitialPhysicalPlanWithStats => {
71                write!(f, "initial_physical_plan_with_stats")
72            }
73            PlanType::InitialPhysicalPlanWithSchema => {
74                write!(f, "initial_physical_plan_with_schema")
75            }
76            PlanType::OptimizedPhysicalPlan { optimizer_name } => {
77                write!(f, "physical_plan after {optimizer_name}")
78            }
79            PlanType::FinalPhysicalPlan => write!(f, "physical_plan"),
80            PlanType::FinalPhysicalPlanWithStats => write!(f, "physical_plan_with_stats"),
81            PlanType::FinalPhysicalPlanWithSchema => {
82                write!(f, "physical_plan_with_schema")
83            }
84            PlanType::PhysicalPlanError => write!(f, "physical_plan_error"),
85        }
86    }
87}
88
89/// Represents some sort of execution plan, in String form
90#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
91pub struct StringifiedPlan {
92    /// An identifier of what type of plan this string represents
93    pub plan_type: PlanType,
94    /// The string representation of the plan
95    pub plan: Arc<String>,
96}
97
98impl StringifiedPlan {
99    /// Create a new Stringified plan of `plan_type` with string
100    /// representation `plan`
101    pub fn new(plan_type: PlanType, plan: impl Into<String>) -> Self {
102        StringifiedPlan {
103            plan_type,
104            plan: Arc::new(plan.into()),
105        }
106    }
107
108    /// Returns true if this plan should be displayed. Generally
109    /// `verbose_mode = true` will display all available plans
110    pub fn should_display(&self, verbose_mode: bool) -> bool {
111        match self.plan_type {
112            PlanType::FinalLogicalPlan
113            | PlanType::FinalPhysicalPlan
114            | PlanType::PhysicalPlanError => true,
115            _ => verbose_mode,
116        }
117    }
118}
119
120/// Trait for something that can be formatted as a stringified plan
121pub trait ToStringifiedPlan {
122    /// Create a stringified plan with the specified type
123    fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan;
124}