Apache DataFusion SQL Query Engine
Extract Explain, Analyze, Extension in LogicalPlan as independent struct (#1317)
Carlosbcd12589ee8f
diff --git a/ballista/rust/core/src/serde/logical_plan/to_proto.rs b/ballista/rust/core/src/serde/logical_plan/to_proto.rsindex b5ecda2e73..da36c9f06a 100644--- a/ballista/rust/core/src/serde/logical_plan/to_proto.rs+++ b/ballista/rust/core/src/serde/logical_plan/to_proto.rs@@ -973,24 +973,24 @@ impl TryInto<protobuf::LogicalPlanNode> for &LogicalPlan {)),})}- LogicalPlan::Analyze { verbose, input, .. } => {- let input: protobuf::LogicalPlanNode = input.as_ref().try_into()?;+ LogicalPlan::Analyze(a) => {+ let input: protobuf::LogicalPlanNode = a.input.as_ref().try_into()?;Ok(protobuf::LogicalPlanNode {logical_plan_type: Some(LogicalPlanType::Analyze(Box::new(protobuf::AnalyzeNode {input: Some(Box::new(input)),- verbose: *verbose,+ verbose: a.verbose,},))),})}- LogicalPlan::Explain { verbose, plan, .. } => {- let input: protobuf::LogicalPlanNode = plan.as_ref().try_into()?;+ LogicalPlan::Explain(a) => {+ let input: protobuf::LogicalPlanNode = a.plan.as_ref().try_into()?;Ok(protobuf::LogicalPlanNode {logical_plan_type: Some(LogicalPlanType::Explain(Box::new(protobuf::ExplainNode {input: Some(Box::new(input)),- verbose: *verbose,+ verbose: a.verbose,},))),})diff --git a/datafusion/src/execution/context.rs b/datafusion/src/execution/context.rsindex b79c4fa294..08e2e702d5 100644--- a/datafusion/src/execution/context.rs+++ b/datafusion/src/execution/context.rs@@ -76,6 +76,7 @@ use crate::physical_optimizer::coalesce_batches::CoalesceBatches;use crate::physical_optimizer::merge_exec::AddCoalescePartitionsExec;use crate::physical_optimizer::repartition::Repartition;+use crate::logical_plan::plan::ExplainPlan;use crate::physical_plan::planner::DefaultPhysicalPlanner;use crate::physical_plan::udf::ScalarUDF;use crate::physical_plan::ExecutionPlan;@@ -650,28 +651,23 @@ impl ExecutionContext {/// Optimizes the logical plan by applying optimizer rules.pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan> {- if let LogicalPlan::Explain {- verbose,- plan,- stringified_plans,- schema,- } = plan- {- let mut stringified_plans = stringified_plans.clone();+ if let LogicalPlan::Explain(e) = plan {+ let mut stringified_plans = e.stringified_plans.clone();// optimize the child plan, capturing the output of each optimizer- let plan = self.optimize_internal(plan, |optimized_plan, optimizer| {- let optimizer_name = optimizer.name().to_string();- let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };- stringified_plans.push(optimized_plan.to_stringified(plan_type));- })?;-- Ok(LogicalPlan::Explain {- verbose: *verbose,+ let plan =+ self.optimize_internal(e.plan.as_ref(), |optimized_plan, optimizer| {+ let optimizer_name = optimizer.name().to_string();+ let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };+ stringified_plans.push(optimized_plan.to_stringified(plan_type));+ })?;++ Ok(LogicalPlan::Explain(ExplainPlan {+ verbose: e.verbose,plan: Arc::new(plan),stringified_plans,- schema: schema.clone(),- })+ schema: e.schema.clone(),+ }))} else {self.optimize_internal(plan, |_, _| {})}@@ -1221,33 +1217,28 @@ mod tests {.build().unwrap();- if let LogicalPlan::Explain {- stringified_plans, ..- } = &plan- {- assert_eq!(stringified_plans.len(), 1);+ if let LogicalPlan::Explain(e) = &plan {+ assert_eq!(e.stringified_plans.len(), 1);} else {panic!("plan was not an explain: {:?}", plan);}// now optimize the plan and expect to see more planslet optimized_plan = ExecutionContext::new().optimize(&plan).unwrap();- if let LogicalPlan::Explain {- stringified_plans, ..- } = &optimized_plan- {+ if let LogicalPlan::Explain(e) = &optimized_plan {// should have more than one planassert!(- stringified_plans.len() > 1,+ e.stringified_plans.len() > 1,"plans: {:#?}",- stringified_plans+ e.stringified_plans);// should have at least one optimized plan- let opt = stringified_plans+ let opt = e+ .stringified_plans.iter().any(|p| matches!(p.plan_type, PlanType::OptimizedLogicalPlan { .. }));- assert!(opt, "plans: {:#?}", stringified_plans);+ assert!(opt, "plans: {:#?}", e.stringified_plans);} else {panic!("plan was not an explain: {:?}", plan);}diff --git a/datafusion/src/logical_plan/builder.rs b/datafusion/src/logical_plan/builder.rsindex d9de6fe818..64b6c25d0e 100644--- a/datafusion/src/logical_plan/builder.rs+++ b/datafusion/src/logical_plan/builder.rs@@ -25,7 +25,9 @@ use crate::datasource::{MemTable, TableProvider,};use crate::error::{DataFusionError, Result};-use crate::logical_plan::plan::{TableScanPlan, ToStringifiedPlan};+use crate::logical_plan::plan::{+ AnalyzePlan, ExplainPlan, TableScanPlan, ToStringifiedPlan,+};use crate::prelude::*;use crate::scalar::ScalarValue;use arrow::{@@ -696,21 +698,21 @@ impl LogicalPlanBuilder {let schema = schema.to_dfschema_ref()?;if analyze {- Ok(Self::from(LogicalPlan::Analyze {+ Ok(Self::from(LogicalPlan::Analyze(AnalyzePlan {verbose,input: Arc::new(self.plan.clone()),schema,- }))+ })))} else {let stringified_plans =vec![self.plan.to_stringified(PlanType::InitialLogicalPlan)];- Ok(Self::from(LogicalPlan::Explain {+ Ok(Self::from(LogicalPlan::Explain(ExplainPlan {verbose,plan: Arc::new(self.plan.clone()),stringified_plans,schema,- }))+ })))}}diff --git a/datafusion/src/logical_plan/plan.rs b/datafusion/src/logical_plan/plan.rsindex cec0e0910b..d9f3d9ab4e 100644--- a/datafusion/src/logical_plan/plan.rs+++ b/datafusion/src/logical_plan/plan.rs@@ -74,6 +74,39 @@ pub struct TableScanPlan {pub limit: Option<usize>,}+/// Produces a relation with string representations of+/// various parts of the plan+#[derive(Clone)]+pub struct ExplainPlan {+ /// Should extra (detailed, intermediate plans) be included?+ pub verbose: bool,+ /// The logical plan that is being EXPLAIN'd+ pub plan: Arc<LogicalPlan>,+ /// Represent the various stages plans have gone through+ pub stringified_plans: Vec<StringifiedPlan>,+ /// The output schema of the explain (2 columns of text)+ pub schema: DFSchemaRef,+}++/// Runs the actual plan, and then prints the physical plan with+/// with execution metrics.+#[derive(Clone)]+pub struct AnalyzePlan {+ /// Should extra detail be included?+ pub verbose: bool,+ /// The logical plan that is being EXPLAIN ANALYZE'd+ pub input: Arc<LogicalPlan>,+ /// The output schema of the explain (2 columns of text)+ pub schema: DFSchemaRef,+}++/// Extension operator defined outside of DataFusion+#[derive(Clone)]+pub struct ExtensionPlan {+ /// The runtime extension operator+ pub node: Arc<dyn UserDefinedLogicalNode + Send + Sync>,+}+/// A LogicalPlan represents the different types of relational/// operators (such as Projection, Filter, etc) and can be created by/// the SQL query planner and the DataFrame API.@@ -236,31 +269,12 @@ pub enum LogicalPlan {},/// Produces a relation with string representations of/// various parts of the plan- Explain {- /// Should extra (detailed, intermediate plans) be included?- verbose: bool,- /// The logical plan that is being EXPLAIN'd- plan: Arc<LogicalPlan>,- /// Represent the various stages plans have gone through- stringified_plans: Vec<StringifiedPlan>,- /// The output schema of the explain (2 columns of text)- schema: DFSchemaRef,- },+ Explain(ExplainPlan),/// Runs the actual plan, and then prints the physical plan with/// with execution metrics.- Analyze {- /// Should extra detail be included?- verbose: bool,- /// The logical plan that is being EXPLAIN ANALYZE'd- input: Arc<LogicalPlan>,- /// The output schema of the explain (2 columns of text)- schema: DFSchemaRef,- },+ Analyze(AnalyzePlan),/// Extension operator defined outside of DataFusion- Extension {- /// The runtime extension operator- node: Arc<dyn UserDefinedLogicalNode + Send + Sync>,- },+ Extension(ExtensionPlan),}impl LogicalPlan {@@ -282,9 +296,9 @@ impl LogicalPlan {LogicalPlan::Repartition { input, .. } => input.schema(),LogicalPlan::Limit { input, .. } => input.schema(),LogicalPlan::CreateExternalTable { schema, .. } => schema,- LogicalPlan::Explain { schema, .. } => schema,- LogicalPlan::Analyze { schema, .. } => schema,- LogicalPlan::Extension { node } => node.schema(),+ LogicalPlan::Explain(explain) => &explain.schema,+ LogicalPlan::Analyze(analyze) => &analyze.schema,+ LogicalPlan::Extension(extension) => extension.node.schema(),LogicalPlan::Union { schema, .. } => schema,LogicalPlan::CreateMemoryTable { input, .. } => input.schema(),LogicalPlan::DropTable { schema, .. } => schema,@@ -324,9 +338,9 @@ impl LogicalPlan {LogicalPlan::Union { schema, .. } => {vec![schema]}- LogicalPlan::Extension { node } => vec![node.schema()],- LogicalPlan::Explain { schema, .. }- | LogicalPlan::Analyze { schema, .. }+ LogicalPlan::Extension(extension) => vec![extension.node.schema()],+ LogicalPlan::Explain(ExplainPlan { schema, .. })+ | LogicalPlan::Analyze(AnalyzePlan { schema, .. })| LogicalPlan::EmptyRelation { schema, .. }| LogicalPlan::CreateExternalTable { schema, .. } => vec![schema],LogicalPlan::Limit { input, .. }@@ -374,7 +388,7 @@ impl LogicalPlan {.flat_map(|(l, r)| vec![Expr::Column(l.clone()), Expr::Column(r.clone())]).collect(),LogicalPlan::Sort { expr, .. } => expr.clone(),- LogicalPlan::Extension { node } => node.expressions(),+ LogicalPlan::Extension(extension) => extension.node.expressions(),// plans without expressionsLogicalPlan::TableScan { .. }| LogicalPlan::EmptyRelation { .. }@@ -404,10 +418,10 @@ impl LogicalPlan {LogicalPlan::Join { left, right, .. } => vec![left, right],LogicalPlan::CrossJoin { left, right, .. } => vec![left, right],LogicalPlan::Limit { input, .. } => vec![input],- LogicalPlan::Extension { node } => node.inputs(),+ LogicalPlan::Extension(extension) => extension.node.inputs(),LogicalPlan::Union { inputs, .. } => inputs.iter().collect(),- LogicalPlan::Explain { plan, .. } => vec![plan],- LogicalPlan::Analyze { input: plan, .. } => vec![plan],+ LogicalPlan::Explain(explain) => vec![&explain.plan],+ LogicalPlan::Analyze(analyze) => vec![&analyze.input],LogicalPlan::CreateMemoryTable { input, .. } => vec![input],// plans without inputsLogicalPlan::TableScan { .. }@@ -548,16 +562,16 @@ impl LogicalPlan {}LogicalPlan::Limit { input, .. } => input.accept(visitor)?,LogicalPlan::CreateMemoryTable { input, .. } => input.accept(visitor)?,- LogicalPlan::Extension { node } => {- for input in node.inputs() {+ LogicalPlan::Extension(extension) => {+ for input in extension.node.inputs() {if !input.accept(visitor)? {return Ok(false);}}true}- LogicalPlan::Explain { plan, .. } => plan.accept(visitor)?,- LogicalPlan::Analyze { input: plan, .. } => plan.accept(visitor)?,+ LogicalPlan::Explain(explain) => explain.plan.accept(visitor)?,+ LogicalPlan::Analyze(analyze) => analyze.input.accept(visitor)?,// plans without inputsLogicalPlan::TableScan { .. }| LogicalPlan::EmptyRelation { .. }@@ -889,7 +903,7 @@ impl LogicalPlan {LogicalPlan::Explain { .. } => write!(f, "Explain"),LogicalPlan::Analyze { .. } => write!(f, "Analyze"),LogicalPlan::Union { .. } => write!(f, "Union"),- LogicalPlan::Extension { ref node } => node.fmt_for_explain(f),+ LogicalPlan::Extension(e) => e.node.fmt_for_explain(f),}}}diff --git a/datafusion/src/optimizer/projection_push_down.rs b/datafusion/src/optimizer/projection_push_down.rsindex 0b2deccef3..e1abb5c2e4 100644--- a/datafusion/src/optimizer/projection_push_down.rs+++ b/datafusion/src/optimizer/projection_push_down.rs@@ -20,7 +20,7 @@use crate::error::{DataFusionError, Result};use crate::execution::context::ExecutionProps;-use crate::logical_plan::plan::TableScanPlan;+use crate::logical_plan::plan::{AnalyzePlan, TableScanPlan};use crate::logical_plan::{build_join_schema, Column, DFField, DFSchema, DFSchemaRef, LogicalPlan,LogicalPlanBuilder, ToDFSchema,@@ -355,30 +355,27 @@ fn optimize_plan(LogicalPlan::Explain { .. } => Err(DataFusionError::Internal("Unsupported logical plan: Explain must be root of the plan".to_string(),)),- LogicalPlan::Analyze {- input,- verbose,- schema,- } => {+ LogicalPlan::Analyze(a) => {// make sure we keep all the columns from the input plan- let required_columns = input+ let required_columns = a+ .input.schema().fields().iter().map(|f| f.qualified_column()).collect::<HashSet<Column>>();- Ok(LogicalPlan::Analyze {+ Ok(LogicalPlan::Analyze(AnalyzePlan {input: Arc::new(optimize_plan(optimizer,- input,+ &a.input,&required_columns,false,execution_props,)?),- verbose: *verbose,- schema: schema.clone(),- })+ verbose: a.verbose,+ schema: a.schema.clone(),+ }))}LogicalPlan::Union {inputs,diff --git a/datafusion/src/optimizer/utils.rs b/datafusion/src/optimizer/utils.rsindex 52beb69552..259c35a647 100644--- a/datafusion/src/optimizer/utils.rs+++ b/datafusion/src/optimizer/utils.rs@@ -23,6 +23,7 @@ use arrow::record_batch::RecordBatch;use super::optimizer::OptimizerRule;use crate::execution::context::{ExecutionContextState, ExecutionProps};+use crate::logical_plan::plan::{AnalyzePlan, ExtensionPlan};use crate::logical_plan::{build_join_schema, Column, DFSchema, DFSchemaRef, Expr, ExprRewriter, LogicalPlan,LogicalPlanBuilder, Operator, Partitioning, Recursion, RewriteRecursion,@@ -228,26 +229,24 @@ pub fn from_plan(name: name.clone(),})}- LogicalPlan::Extension { node } => Ok(LogicalPlan::Extension {- node: node.from_template(expr, inputs),- }),+ LogicalPlan::Extension(e) => Ok(LogicalPlan::Extension(ExtensionPlan {+ node: e.node.from_template(expr, inputs),+ })),LogicalPlan::Union { schema, alias, .. } => Ok(LogicalPlan::Union {inputs: inputs.to_vec(),schema: schema.clone(),alias: alias.clone(),}),- LogicalPlan::Analyze {- verbose, schema, ..- } => {+ LogicalPlan::Analyze(a) => {assert!(expr.is_empty());assert_eq!(inputs.len(), 1);- Ok(LogicalPlan::Analyze {- verbose: *verbose,- schema: schema.clone(),+ Ok(LogicalPlan::Analyze(AnalyzePlan {+ verbose: a.verbose,+ schema: a.schema.clone(),input: Arc::new(inputs[0].clone()),- })+ }))}- LogicalPlan::Explain { .. } => {+ LogicalPlan::Explain(_) => {// Explain should be handled specially in the optimizers;// If this assert fails it means some optimizer pass is// trying to optimize Explain directlydiff --git a/datafusion/src/physical_plan/planner.rs b/datafusion/src/physical_plan/planner.rsindex dc7cf25934..5287e71d19 100644--- a/datafusion/src/physical_plan/planner.rs+++ b/datafusion/src/physical_plan/planner.rs@@ -819,20 +819,16 @@ impl DefaultPhysicalPlanner {SchemaRef::new(Schema::empty()),)))}- LogicalPlan::Explain { .. } => Err(DataFusionError::Internal(+ LogicalPlan::Explain (_) => Err(DataFusionError::Internal("Unsupported logical plan: Explain must be root of the plan".to_string(),)),- LogicalPlan::Analyze {- verbose,- input,- schema,- } => {- let input = self.create_initial_plan(input, ctx_state).await?;- let schema = SchemaRef::new(schema.as_ref().to_owned().into());- Ok(Arc::new(AnalyzeExec::new(*verbose, input, schema)))+ LogicalPlan::Analyze(a) => {+ let input = self.create_initial_plan(&a.input, ctx_state).await?;+ let schema = SchemaRef::new((*a.schema).clone().into());+ Ok(Arc::new(AnalyzeExec::new(a.verbose, input, schema)))}- LogicalPlan::Extension { node } => {- let physical_inputs = futures::stream::iter(node.inputs())+ LogicalPlan::Extension(e) => {+ let physical_inputs = futures::stream::iter(e.node.inputs()).then(|lp| self.create_initial_plan(lp, ctx_state)).try_collect::<Vec<_>>().await?;@@ -845,8 +841,8 @@ impl DefaultPhysicalPlanner {} else {planner.plan_extension(self,- node.as_ref(),- &node.inputs(),+ e.node.as_ref(),+ &e.node.inputs(),&physical_inputs,ctx_state,)@@ -854,17 +850,17 @@ impl DefaultPhysicalPlanner {},)?;let plan = maybe_plan.ok_or_else(|| DataFusionError::Plan(format!(- "No installed planner was able to convert the custom node to an execution plan: {:?}", node+ "No installed planner was able to convert the custom node to an execution plan: {:?}", e.node)))?;// Ensure the ExecutionPlan's schema matches the// declared logical schema to catch and warn about// logic errors when creating user defined plans.- if !node.schema().matches_arrow_schema(&plan.schema()) {+ if !e.node.schema().matches_arrow_schema(&plan.schema()) {Err(DataFusionError::Plan(format!("Extension planner for {:?} created an ExecutionPlan with mismatched schema. \LogicalPlan schema: {:?}, ExecutionPlan schema: {:?}",- node, node.schema(), plan.schema()+ e.node, e.node.schema(), plan.schema())))} else {Ok(plan)@@ -1395,19 +1391,13 @@ impl DefaultPhysicalPlanner {logical_plan: &LogicalPlan,ctx_state: &ExecutionContextState,) -> Result<Option<Arc<dyn ExecutionPlan>>> {- if let LogicalPlan::Explain {- verbose,- plan,- stringified_plans,- schema,- } = logical_plan- {+ if let LogicalPlan::Explain(e) = logical_plan {use PlanType::*;- let mut stringified_plans = stringified_plans.clone();+ let mut stringified_plans = e.stringified_plans.clone();- stringified_plans.push(plan.to_stringified(FinalLogicalPlan));+ stringified_plans.push(e.plan.to_stringified(FinalLogicalPlan));- let input = self.create_initial_plan(plan, ctx_state).await?;+ let input = self.create_initial_plan(e.plan.as_ref(), ctx_state).await?;stringified_plans.push(displayable(input.as_ref()).to_stringified(InitialPhysicalPlan));@@ -1422,9 +1412,9 @@ impl DefaultPhysicalPlanner {.push(displayable(input.as_ref()).to_stringified(FinalPhysicalPlan));Ok(Some(Arc::new(ExplainExec::new(- SchemaRef::new(schema.as_ref().to_owned().into()),+ SchemaRef::new(e.schema.as_ref().to_owned().into()),stringified_plans,- *verbose,+ e.verbose,))))} else {Ok(None)@@ -1473,6 +1463,7 @@ mod tests {use super::*;use crate::datasource::object_store::local::LocalFileSystem;use crate::execution::options::CsvReadOptions;+ use crate::logical_plan::plan::ExtensionPlan;use crate::logical_plan::{DFField, DFSchema, DFSchemaRef};use crate::physical_plan::{expressions, DisplayFormatType, Partitioning, Statistics,@@ -1623,9 +1614,9 @@ mod tests {async fn default_extension_planner() {let ctx_state = make_ctx_state();let planner = DefaultPhysicalPlanner::default();- let logical_plan = LogicalPlan::Extension {+ let logical_plan = LogicalPlan::Extension(ExtensionPlan {node: Arc::new(NoOpExtensionNode::default()),- };+ });let plan = planner.create_physical_plan(&logical_plan, &ctx_state).await;@@ -1652,9 +1643,9 @@ mod tests {BadExtensionPlanner {},)]);- let logical_plan = LogicalPlan::Extension {+ let logical_plan = LogicalPlan::Extension(ExtensionPlan {node: Arc::new(NoOpExtensionNode::default()),- };+ });let plan = planner.create_physical_plan(&logical_plan, &ctx_state).await;diff --git a/datafusion/src/sql/planner.rs b/datafusion/src/sql/planner.rsindex 77eee2946c..87c37493c7 100644--- a/datafusion/src/sql/planner.rs+++ b/datafusion/src/sql/planner.rs@@ -65,6 +65,7 @@ use super::{},};use crate::logical_plan::builder::project_with_alias;+use crate::logical_plan::plan::{AnalyzePlan, ExplainPlan};/// The ContextProvider trait allows the query planner to obtain meta-data about tables and/// functions referenced in SQL statements@@ -328,20 +329,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let schema = schema.to_dfschema_ref()?;if analyze {- Ok(LogicalPlan::Analyze {+ Ok(LogicalPlan::Analyze(AnalyzePlan {verbose,input: plan,schema,- })+ }))} else {let stringified_plans =vec![plan.to_stringified(PlanType::InitialLogicalPlan)];- Ok(LogicalPlan::Explain {+ Ok(LogicalPlan::Explain(ExplainPlan {verbose,plan,stringified_plans,schema,- })+ }))}}diff --git a/datafusion/src/test/user_defined.rs b/datafusion/src/test/user_defined.rsindex 9a850d5275..394d865da1 100644--- a/datafusion/src/test/user_defined.rs+++ b/datafusion/src/test/user_defined.rs@@ -23,12 +23,13 @@ use std::{sync::Arc,};+use crate::logical_plan::plan::ExtensionPlan;use crate::logical_plan::{DFSchemaRef, Expr, LogicalPlan, UserDefinedLogicalNode};/// Create a new user defined plan node, for testingpub fn new(input: LogicalPlan) -> LogicalPlan {let node = Arc::new(TestUserDefinedPlanNode { input });- LogicalPlan::Extension { node }+ LogicalPlan::Extension(ExtensionPlan { node })}struct TestUserDefinedPlanNode {diff --git a/datafusion/tests/user_defined_plan.rs b/datafusion/tests/user_defined_plan.rsindex adb83ac0a2..46bc70bb8a 100644--- a/datafusion/tests/user_defined_plan.rs+++ b/datafusion/tests/user_defined_plan.rs@@ -86,6 +86,7 @@ use std::{any::Any, collections::BTreeMap, fmt, sync::Arc};use async_trait::async_trait;use datafusion::execution::context::ExecutionProps;+use datafusion::logical_plan::plan::ExtensionPlan;use datafusion::logical_plan::DFSchemaRef;/// Execute the specified sql and return the resulting record batches@@ -295,13 +296,13 @@ impl OptimizerRule for TopKOptimizerRule {{if expr.len() == 1 {// we found a sort with a single sort expr, replace with a a TopK- return Ok(LogicalPlan::Extension {+ return Ok(LogicalPlan::Extension(ExtensionPlan {node: Arc::new(TopKPlanNode {k: *n,input: self.optimize(input.as_ref(), execution_props)?,expr: expr[0].clone(),}),- });+ }));}}}