|
| 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 | +//! Types for plan display |
| 19 | +
|
| 20 | +use std::{ |
| 21 | + fmt::{self, Display, Formatter}, |
| 22 | + sync::Arc, |
| 23 | +}; |
| 24 | + |
| 25 | +/// Represents which type of plan, when storing multiple |
| 26 | +/// for use in EXPLAIN plans |
| 27 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 28 | +pub enum PlanType { |
| 29 | + /// The initial LogicalPlan provided to DataFusion |
| 30 | + InitialLogicalPlan, |
| 31 | + /// The LogicalPlan which results from applying an analyzer pass |
| 32 | + AnalyzedLogicalPlan { |
| 33 | + /// The name of the analyzer which produced this plan |
| 34 | + analyzer_name: String, |
| 35 | + }, |
| 36 | + /// The LogicalPlan after all analyzer passes have been applied |
| 37 | + FinalAnalyzedLogicalPlan, |
| 38 | + /// The LogicalPlan which results from applying an optimizer pass |
| 39 | + OptimizedLogicalPlan { |
| 40 | + /// The name of the optimizer which produced this plan |
| 41 | + optimizer_name: String, |
| 42 | + }, |
| 43 | + /// The final, fully optimized LogicalPlan that was converted to a physical plan |
| 44 | + FinalLogicalPlan, |
| 45 | + /// The initial physical plan, prepared for execution |
| 46 | + InitialPhysicalPlan, |
| 47 | + /// The ExecutionPlan which results from applying an optimizer pass |
| 48 | + OptimizedPhysicalPlan { |
| 49 | + /// The name of the optimizer which produced this plan |
| 50 | + optimizer_name: String, |
| 51 | + }, |
| 52 | + /// The final, fully optimized physical which would be executed |
| 53 | + FinalPhysicalPlan, |
| 54 | +} |
| 55 | + |
| 56 | +impl Display for PlanType { |
| 57 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 58 | + match self { |
| 59 | + PlanType::InitialLogicalPlan => write!(f, "initial_logical_plan"), |
| 60 | + PlanType::AnalyzedLogicalPlan { analyzer_name } => { |
| 61 | + write!(f, "logical_plan after {analyzer_name}") |
| 62 | + } |
| 63 | + PlanType::FinalAnalyzedLogicalPlan => write!(f, "analyzed_logical_plan"), |
| 64 | + PlanType::OptimizedLogicalPlan { optimizer_name } => { |
| 65 | + write!(f, "logical_plan after {optimizer_name}") |
| 66 | + } |
| 67 | + PlanType::FinalLogicalPlan => write!(f, "logical_plan"), |
| 68 | + PlanType::InitialPhysicalPlan => write!(f, "initial_physical_plan"), |
| 69 | + PlanType::OptimizedPhysicalPlan { optimizer_name } => { |
| 70 | + write!(f, "physical_plan after {optimizer_name}") |
| 71 | + } |
| 72 | + PlanType::FinalPhysicalPlan => write!(f, "physical_plan"), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Represents some sort of execution plan, in String form |
| 78 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 79 | +pub struct StringifiedPlan { |
| 80 | + /// An identifier of what type of plan this string represents |
| 81 | + pub plan_type: PlanType, |
| 82 | + /// The string representation of the plan |
| 83 | + pub plan: Arc<String>, |
| 84 | +} |
| 85 | + |
| 86 | +impl StringifiedPlan { |
| 87 | + /// Create a new Stringified plan of `plan_type` with string |
| 88 | + /// representation `plan` |
| 89 | + pub fn new(plan_type: PlanType, plan: impl Into<String>) -> Self { |
| 90 | + StringifiedPlan { |
| 91 | + plan_type, |
| 92 | + plan: Arc::new(plan.into()), |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Returns true if this plan should be displayed. Generally |
| 97 | + /// `verbose_mode = true` will display all available plans |
| 98 | + pub fn should_display(&self, verbose_mode: bool) -> bool { |
| 99 | + match self.plan_type { |
| 100 | + PlanType::FinalLogicalPlan | PlanType::FinalPhysicalPlan => true, |
| 101 | + _ => verbose_mode, |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Trait for something that can be formatted as a stringified plan |
| 107 | +pub trait ToStringifiedPlan { |
| 108 | + /// Create a stringified plan with the specified type |
| 109 | + fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan; |
| 110 | +} |
0 commit comments