-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extract Explain, Analyze, Extension in LogicalPlan as independent struct #1317
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments like above. |
||
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 plans | ||
let 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 plan | ||
assert!( | ||
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this code style is better than above style. |
||
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, | ||
})) | ||
}))) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to import the Analyze struct and use this code style
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I don't have a strong desire which is better. When the struct has many variables, I'd like to use
StructVar(s) => {s.v1, s.v2 ...}
to keep code concise. If there are a few variables, it doesn't matter, both styles make sense to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree either style seems fine to me