-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Draft: add fast-path for with_new_children
#19792
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
base: main
Are you sure you want to change the base?
Conversation
with_new_childrenwith_new_children
72ff575 to
796f731
Compare
|
Also added a typical analytical query plan re-usage benchmark. On the $ cargo bench --profile=release-nonlto --bench plan_reuse |
796f731 to
5601c4f
Compare
|
I filed a ticket to track this idea |
|
run benchmark sql_planner |
|
🤖 |
Could you move the plan_reuse benchmark into its own PR (as I think it is valuable both for this PR and others, and it makes it easier to automatically compare performance) |
|
Benchmark script failed with exit code 101. Last 10 lines of output: Click to expand |
Done in #19806 |
5601c4f to
99cf634
Compare
This patch adds a benchmark which is intended to measure overhead of actions, required to perform making an independent instance of the execution plan to re-execute it, avoiding re-planning stage. There are several typical plans that are tested, covering projection, aggregation, filtration, re-partition. Also, the function `reset_plan_states(...)` is publically exported.
This patch aims to implement a fast-path for the ExecutionPlan::with_new_children function for some plans, moving closer to a physical plan re-use implementation and improving planning performance. If the passed children properties are the same as in self, we do not actually recompute self's properties (which could be costly if projection mapping is required). Instead, we just replace the children and re-use self's properties as-is. To be able to compare two different properties -- ExecutionPlan::properties(...) signature is modified and now returns `&Arc<PlanProperties>`. If `children` properties are the same in `with_new_children` -- we clone our properties arc and then a parent plan will consider our properties as unchanged, doing the same. Also, there are other improvenets, all changes: - Return `&Arc<PlanProperties>` from `ExecutionPlan::properties(...)` instead of a reference. - Implement `with_new_children` fast-path if there is no children properties changes for plans: * SortExec * RepartitionExec * ProjectionExec * FilterExec * CoalescePartitionsExec * AggregateExec - Store `Arc<[usize]>` instead of vector within `FilterExec`. - Store `Arc<[Arc<AggregateFunctionExpr>]>` instead of vec for aggr expr and filters. - Store `Arc<[ProjectionExpr]> instead of vec in `ProjectionExprs` struct. - Get `Option<&[usize]>` instead of option vec ref in `project_schema` -- it makes API more flexible. Note: currently, `reset_plan_states` does not allow to re-use plan in general: it is not supported for dynamic filters and recursive queries features, as in this case state reset should update pointers in the children plans.
99cf634 to
b81dd66
Compare
alamb
left a comment
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.
Thanks @askalt -- this is quite clever and I think it looks very promising
I also think we may be able to potentially make with_new_children even faster by checking the children as well -- and if they are the same there is no reason to recompute everything either.
However, this likely won't help your usecase as the children will likely change (their states need to be reset) 🤔
| db: CustomDataSource, | ||
| projected_schema: SchemaRef, | ||
| cache: PlanProperties, | ||
| cache: Arc<PlanProperties>, |
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.
💯 for this change
| mut children: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| if has_same_children_properties(&self, &children)? { | ||
| // Avoid properties re-computation. |
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 think we coulda avoid even more copying / cloning in the common case using Arc::make_mut https://doc.rust-lang.org/std/sync/struct.Arc.html#method.make_mut
Something like
if has_same_children_properties(&self, &children)? {
// Avoid properties re-computation.
let me = Arc::make_mut(&mut self);
me.input = children.swap_remove(0);
me.metrics = ExecutionPlanMetricsSet::new();
return Ok(self);
}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.
Now that I write that, I wonder if we could use make_mut to avoid lots of cloning in general 🤔
We could check if the new children are the same as the existing children and if so return self 🤔
| /// * uses dynamic filters, | ||
| /// * represents a recursive query. | ||
| /// | ||
| pub fn reset_plan_states(plan: Arc<dyn ExecutionPlan>) -> Result<Arc<dyn ExecutionPlan>> { |
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.
👍
PlanPropertiesredundently #19796This patch aims to implement a fast-path for the ExecutionPlan::with_new_children function for some plans, moving closer to a physical plan re-use implementation and improving planning performance. If the passed children properties are the same as in self, we do not actually recompute self's properties (which could be costly if projection mapping is required). Instead, we just replace the children and re-use self's properties as-is.
To be able to compare two different properties -- ExecutionPlan::properties(...) signature is modified and now returns
&Arc<PlanProperties>. Ifchildrenproperties are the same inwith_new_children-- we clone our properties arc and then a parent plan will consider our properties as unchanged, doing the same.Also, there are other improvenets, all changes:
&Arc<PlanProperties>fromExecutionPlan::properties(...)instead of a reference.with_new_childrenfast-path if there is no children properties changes for plans:reset_plan_statesfunction.Arc<[usize]>instead of vector withinFilterExec.Arc<[Arc<AggregateFunctionExpr>]>instead of vec for aggr expr and filters.Arc<[ProjectionExpr]> instead of vec inProjectionExprs` struct.Option<&[usize]>instead of option vec ref inproject_schema-- it makes APImore flexible.
Note: currently,
reset_plan_statesdoes not allow to re-use plan in general: it is notsupported for dynamic filters and recursive queries features, as in this case state reset
should update pointers in the children plans.