Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: Move PlanType, StringifiedPlan and ToStringifiedPlan datafusion_common #6571

Merged
merged 5 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions datafusion/common/src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//! Types for plan display

use std::{
fmt::{self, Display, Formatter},
sync::Arc,
};

/// Represents which type of plan, when storing multiple
/// for use in EXPLAIN plans
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PlanType {
/// The initial LogicalPlan provided to DataFusion
InitialLogicalPlan,
/// The LogicalPlan which results from applying an analyzer pass
AnalyzedLogicalPlan {
/// The name of the analyzer which produced this plan
analyzer_name: String,
},
/// The LogicalPlan after all analyzer passes have been applied
FinalAnalyzedLogicalPlan,
/// The LogicalPlan which results from applying an optimizer pass
OptimizedLogicalPlan {
/// The name of the optimizer which produced this plan
optimizer_name: String,
},
/// The final, fully optimized LogicalPlan that was converted to a physical plan
FinalLogicalPlan,
/// The initial physical plan, prepared for execution
InitialPhysicalPlan,
/// The ExecutionPlan which results from applying an optimizer pass
OptimizedPhysicalPlan {
/// The name of the optimizer which produced this plan
optimizer_name: String,
},
/// The final, fully optimized physical which would be executed
FinalPhysicalPlan,
}

impl Display for PlanType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
PlanType::InitialLogicalPlan => write!(f, "initial_logical_plan"),
PlanType::AnalyzedLogicalPlan { analyzer_name } => {
write!(f, "logical_plan after {analyzer_name}")
}
PlanType::FinalAnalyzedLogicalPlan => write!(f, "analyzed_logical_plan"),
PlanType::OptimizedLogicalPlan { optimizer_name } => {
write!(f, "logical_plan after {optimizer_name}")
}
PlanType::FinalLogicalPlan => write!(f, "logical_plan"),
PlanType::InitialPhysicalPlan => write!(f, "initial_physical_plan"),
PlanType::OptimizedPhysicalPlan { optimizer_name } => {
write!(f, "physical_plan after {optimizer_name}")
}
PlanType::FinalPhysicalPlan => write!(f, "physical_plan"),
}
}
}

/// Represents some sort of execution plan, in String form
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StringifiedPlan {
/// An identifier of what type of plan this string represents
pub plan_type: PlanType,
/// The string representation of the plan
pub plan: Arc<String>,
}

impl StringifiedPlan {
/// Create a new Stringified plan of `plan_type` with string
/// representation `plan`
pub fn new(plan_type: PlanType, plan: impl Into<String>) -> Self {
StringifiedPlan {
plan_type,
plan: Arc::new(plan.into()),
}
}

/// returns true if this plan should be displayed. Generally
alamb marked this conversation as resolved.
Show resolved Hide resolved
/// `verbose_mode = true` will display all available plans
pub fn should_display(&self, verbose_mode: bool) -> bool {
match self.plan_type {
PlanType::FinalLogicalPlan | PlanType::FinalPhysicalPlan => true,
_ => verbose_mode,
}
}
}

/// Trait for something that can be formatted as a stringified plan
pub trait ToStringifiedPlan {
/// Create a stringified plan with the specified type
fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan;
}
1 change: 1 addition & 0 deletions datafusion/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod column;
pub mod config;
pub mod delta;
mod dfschema;
pub mod display;
mod error;
pub mod from_slice;
pub mod parsers;
Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/src/physical_plan/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use std::fmt;

use crate::logical_expr::{StringifiedPlan, ToStringifiedPlan};
use datafusion_common::display::{StringifiedPlan, ToStringifiedPlan};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole point of this PR is to remove the logical_expr from the use statements in physical_plan


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

Expand Down
3 changes: 2 additions & 1 deletion datafusion/core/src/physical_plan/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
use std::any::Any;
use std::sync::Arc;

use datafusion_common::display::StringifiedPlan;

use crate::{
error::{DataFusionError, Result},
logical_expr::StringifiedPlan,
physical_plan::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics},
};
use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};
Expand Down
4 changes: 3 additions & 1 deletion datafusion/core/src/physical_plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ use crate::logical_expr::{
};
use crate::logical_expr::{
CrossJoin, Expr, LogicalPlan, Partitioning as LogicalPartitioning, PlanType,
Repartition, ToStringifiedPlan, Union, UserDefinedLogicalNode,
Repartition, Union, UserDefinedLogicalNode,
};
use datafusion_common::display::ToStringifiedPlan;

use crate::logical_expr::{Limit, Values};
use crate::physical_expr::create_physical_expr;
use crate::physical_optimizer::optimizer::PhysicalOptimizerRule;
Expand Down
8 changes: 4 additions & 4 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
logical_plan::{
Aggregate, Analyze, CrossJoin, Distinct, EmptyRelation, Explain, Filter, Join,
JoinConstraint, JoinType, Limit, LogicalPlan, Partitioning, PlanType, Prepare,
Projection, Repartition, Sort, SubqueryAlias, TableScan, ToStringifiedPlan,
Union, Unnest, Values, Window,
Projection, Repartition, Sort, SubqueryAlias, TableScan, Union, Unnest, Values,
Window,
},
utils::{
can_hash, expand_qualified_wildcard, expand_wildcard,
Expand All @@ -40,8 +40,8 @@ use crate::{
};
use arrow::datatypes::{DataType, Schema, SchemaRef};
use datafusion_common::{
Column, DFField, DFSchema, DFSchemaRef, DataFusionError, OwnedTableReference, Result,
ScalarValue, TableReference, ToDFSchema,
display::ToStringifiedPlan, Column, DFField, DFSchema, DFSchemaRef, DataFusionError,
OwnedTableReference, Result, ScalarValue, TableReference, ToDFSchema,
};
use std::any::Any;
use std::cmp::Ordering;
Expand Down
90 changes: 3 additions & 87 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::sync::Arc;

// backwards compatibility
pub use datafusion_common::display::{PlanType, StringifiedPlan, ToStringifiedPlan};
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For such things, I'm wondering as no deprecated message, it is possibly we can remove this kind of compatibility later?

Copy link
Contributor Author

@alamb alamb Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add a deprecated message (that could point people to use the new location) but it didn't seem to work.

I think we can remove the pub use whenever we want, it will simply mean users of the crate will have to update their pub uses (and will technically be a breaking API change)

I guess I am hoping that over time we can remove some of the old pub use but we don't really have a structured plan for doing so. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, for such re-exported things, seems it is impossible to add deprecated message. So the only way is just to remove this kind of old APIs after some time (e.g., few releases?).


use super::DdlStatement;

/// A LogicalPlan represents the different types of relational
Expand Down Expand Up @@ -1721,93 +1724,6 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}

/// Represents which type of plan, when storing multiple
/// for use in EXPLAIN plans
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PlanType {
/// The initial LogicalPlan provided to DataFusion
InitialLogicalPlan,
/// The LogicalPlan which results from applying an analyzer pass
AnalyzedLogicalPlan {
/// The name of the analyzer which produced this plan
analyzer_name: String,
},
/// The LogicalPlan after all analyzer passes have been applied
FinalAnalyzedLogicalPlan,
/// The LogicalPlan which results from applying an optimizer pass
OptimizedLogicalPlan {
/// The name of the optimizer which produced this plan
optimizer_name: String,
},
/// The final, fully optimized LogicalPlan that was converted to a physical plan
FinalLogicalPlan,
/// The initial physical plan, prepared for execution
InitialPhysicalPlan,
/// The ExecutionPlan which results from applying an optimizer pass
OptimizedPhysicalPlan {
/// The name of the optimizer which produced this plan
optimizer_name: String,
},
/// The final, fully optimized physical which would be executed
FinalPhysicalPlan,
}

impl Display for PlanType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
PlanType::InitialLogicalPlan => write!(f, "initial_logical_plan"),
PlanType::AnalyzedLogicalPlan { analyzer_name } => {
write!(f, "logical_plan after {analyzer_name}")
}
PlanType::FinalAnalyzedLogicalPlan => write!(f, "analyzed_logical_plan"),
PlanType::OptimizedLogicalPlan { optimizer_name } => {
write!(f, "logical_plan after {optimizer_name}")
}
PlanType::FinalLogicalPlan => write!(f, "logical_plan"),
PlanType::InitialPhysicalPlan => write!(f, "initial_physical_plan"),
PlanType::OptimizedPhysicalPlan { optimizer_name } => {
write!(f, "physical_plan after {optimizer_name}")
}
PlanType::FinalPhysicalPlan => write!(f, "physical_plan"),
}
}
}

/// Represents some sort of execution plan, in String form
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StringifiedPlan {
/// An identifier of what type of plan this string represents
pub plan_type: PlanType,
/// The string representation of the plan
pub plan: Arc<String>,
}

impl StringifiedPlan {
/// Create a new Stringified plan of `plan_type` with string
/// representation `plan`
pub fn new(plan_type: PlanType, plan: impl Into<String>) -> Self {
StringifiedPlan {
plan_type,
plan: Arc::new(plan.into()),
}
}

/// returns true if this plan should be displayed. Generally
/// `verbose_mode = true` will display all available plans
pub fn should_display(&self, verbose_mode: bool) -> bool {
match self.plan_type {
PlanType::FinalLogicalPlan | PlanType::FinalPhysicalPlan => true,
_ => verbose_mode,
}
}
}

/// Trait for something that can be formatted as a stringified plan
pub trait ToStringifiedPlan {
/// Create a stringified plan with the specified type
fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan;
}

/// Unnest a column that contains a nested list type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Unnest {
Expand Down