Skip to content

Commit f0bf016

Browse files
alambviirya
andauthored
Minor: Move PlanType, StringifiedPlan and ToStringifiedPlan datafusion_common (#6571)
* Move DisplayablePlan to `datafusion_common` * Update uses * Update datafusion/common/src/display.rs Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com> --------- Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
1 parent 1af846b commit f0bf016

File tree

7 files changed

+124
-98
lines changed

7 files changed

+124
-98
lines changed

datafusion/common/src/display.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

datafusion/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod column;
2020
pub mod config;
2121
pub mod delta;
2222
mod dfschema;
23+
pub mod display;
2324
mod error;
2425
mod join_type;
2526
pub mod parsers;

datafusion/core/src/physical_plan/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
use std::fmt;
2323

24-
use crate::logical_expr::{StringifiedPlan, ToStringifiedPlan};
24+
use datafusion_common::display::{StringifiedPlan, ToStringifiedPlan};
2525

2626
use super::{accept, ExecutionPlan, ExecutionPlanVisitor};
2727

datafusion/core/src/physical_plan/explain.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
use std::any::Any;
2121
use std::sync::Arc;
2222

23+
use datafusion_common::display::StringifiedPlan;
24+
2325
use datafusion_common::{DataFusionError, Result};
2426

25-
use crate::{
26-
logical_expr::StringifiedPlan,
27-
physical_plan::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics},
28-
};
27+
use crate::physical_plan::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics};
2928
use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};
3029
use log::trace;
3130

datafusion/core/src/physical_plan/planner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ use crate::logical_expr::{
3232
};
3333
use crate::logical_expr::{
3434
CrossJoin, Expr, LogicalPlan, Partitioning as LogicalPartitioning, PlanType,
35-
Repartition, ToStringifiedPlan, Union, UserDefinedLogicalNode,
35+
Repartition, Union, UserDefinedLogicalNode,
3636
};
37+
use datafusion_common::display::ToStringifiedPlan;
38+
3739
use crate::logical_expr::{Limit, Values};
3840
use crate::physical_expr::create_physical_expr;
3941
use crate::physical_optimizer::optimizer::PhysicalOptimizerRule;

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::{
2929
logical_plan::{
3030
Aggregate, Analyze, CrossJoin, Distinct, EmptyRelation, Explain, Filter, Join,
3131
JoinConstraint, JoinType, Limit, LogicalPlan, Partitioning, PlanType, Prepare,
32-
Projection, Repartition, Sort, SubqueryAlias, TableScan, ToStringifiedPlan,
33-
Union, Unnest, Values, Window,
32+
Projection, Repartition, Sort, SubqueryAlias, TableScan, Union, Unnest, Values,
33+
Window,
3434
},
3535
utils::{
3636
can_hash, expand_qualified_wildcard, expand_wildcard,
@@ -40,8 +40,8 @@ use crate::{
4040
};
4141
use arrow::datatypes::{DataType, Schema, SchemaRef};
4242
use datafusion_common::{
43-
Column, DFField, DFSchema, DFSchemaRef, DataFusionError, OwnedTableReference, Result,
44-
ScalarValue, TableReference, ToDFSchema,
43+
display::ToStringifiedPlan, Column, DFField, DFSchema, DFSchemaRef, DataFusionError,
44+
OwnedTableReference, Result, ScalarValue, TableReference, ToDFSchema,
4545
};
4646
use std::any::Any;
4747
use std::cmp::Ordering;

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ use std::fmt::{self, Debug, Display, Formatter};
4242
use std::hash::{Hash, Hasher};
4343
use std::sync::Arc;
4444

45-
// backwards compatible
45+
// backwards compatibility
46+
pub use datafusion_common::display::{PlanType, StringifiedPlan, ToStringifiedPlan};
4647
pub use datafusion_common::{JoinConstraint, JoinType};
4748

4849
use super::DdlStatement;
@@ -1650,93 +1651,6 @@ pub enum Partitioning {
16501651
DistributeBy(Vec<Expr>),
16511652
}
16521653

1653-
/// Represents which type of plan, when storing multiple
1654-
/// for use in EXPLAIN plans
1655-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1656-
pub enum PlanType {
1657-
/// The initial LogicalPlan provided to DataFusion
1658-
InitialLogicalPlan,
1659-
/// The LogicalPlan which results from applying an analyzer pass
1660-
AnalyzedLogicalPlan {
1661-
/// The name of the analyzer which produced this plan
1662-
analyzer_name: String,
1663-
},
1664-
/// The LogicalPlan after all analyzer passes have been applied
1665-
FinalAnalyzedLogicalPlan,
1666-
/// The LogicalPlan which results from applying an optimizer pass
1667-
OptimizedLogicalPlan {
1668-
/// The name of the optimizer which produced this plan
1669-
optimizer_name: String,
1670-
},
1671-
/// The final, fully optimized LogicalPlan that was converted to a physical plan
1672-
FinalLogicalPlan,
1673-
/// The initial physical plan, prepared for execution
1674-
InitialPhysicalPlan,
1675-
/// The ExecutionPlan which results from applying an optimizer pass
1676-
OptimizedPhysicalPlan {
1677-
/// The name of the optimizer which produced this plan
1678-
optimizer_name: String,
1679-
},
1680-
/// The final, fully optimized physical which would be executed
1681-
FinalPhysicalPlan,
1682-
}
1683-
1684-
impl Display for PlanType {
1685-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1686-
match self {
1687-
PlanType::InitialLogicalPlan => write!(f, "initial_logical_plan"),
1688-
PlanType::AnalyzedLogicalPlan { analyzer_name } => {
1689-
write!(f, "logical_plan after {analyzer_name}")
1690-
}
1691-
PlanType::FinalAnalyzedLogicalPlan => write!(f, "analyzed_logical_plan"),
1692-
PlanType::OptimizedLogicalPlan { optimizer_name } => {
1693-
write!(f, "logical_plan after {optimizer_name}")
1694-
}
1695-
PlanType::FinalLogicalPlan => write!(f, "logical_plan"),
1696-
PlanType::InitialPhysicalPlan => write!(f, "initial_physical_plan"),
1697-
PlanType::OptimizedPhysicalPlan { optimizer_name } => {
1698-
write!(f, "physical_plan after {optimizer_name}")
1699-
}
1700-
PlanType::FinalPhysicalPlan => write!(f, "physical_plan"),
1701-
}
1702-
}
1703-
}
1704-
1705-
/// Represents some sort of execution plan, in String form
1706-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1707-
pub struct StringifiedPlan {
1708-
/// An identifier of what type of plan this string represents
1709-
pub plan_type: PlanType,
1710-
/// The string representation of the plan
1711-
pub plan: Arc<String>,
1712-
}
1713-
1714-
impl StringifiedPlan {
1715-
/// Create a new Stringified plan of `plan_type` with string
1716-
/// representation `plan`
1717-
pub fn new(plan_type: PlanType, plan: impl Into<String>) -> Self {
1718-
StringifiedPlan {
1719-
plan_type,
1720-
plan: Arc::new(plan.into()),
1721-
}
1722-
}
1723-
1724-
/// returns true if this plan should be displayed. Generally
1725-
/// `verbose_mode = true` will display all available plans
1726-
pub fn should_display(&self, verbose_mode: bool) -> bool {
1727-
match self.plan_type {
1728-
PlanType::FinalLogicalPlan | PlanType::FinalPhysicalPlan => true,
1729-
_ => verbose_mode,
1730-
}
1731-
}
1732-
}
1733-
1734-
/// Trait for something that can be formatted as a stringified plan
1735-
pub trait ToStringifiedPlan {
1736-
/// Create a stringified plan with the specified type
1737-
fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan;
1738-
}
1739-
17401654
/// Unnest a column that contains a nested list type.
17411655
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17421656
pub struct Unnest {

0 commit comments

Comments
 (0)