-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Prepared physical plan reusage #14342
Comments
About physical placeholders challenges. For example, postgresql uses the following heuristic to choose "use generic plan" or "rebuild plan with inlined parameters when they become known" https://github.com/postgres/postgres/blob/master/src/backend/utils/cache/plancache.c#L1045-L1093 |
One thing that might be quite a bit faster than replanning the entire query could be to make a (deep) copy of the plan via https://docs.rs/datafusion/latest/datafusion/physical_plan/trait.ExecutionPlan.html#tymethod.with_new_children Here is an example that @jonahgao did when re-starting a recursive query datafusion/datafusion/physical-plan/src/recursive_query.rs Lines 371 to 389 in e718c1a
I believe this will also reset the metrics.
BTW if you have examples of such queries it would be great to add them to our planning benchmarks:
This sounds like a reasonable idea -- however some things like expression simplification only happen in As long as there is a way to choose the behavior (early replacement or deferred replacement) I think that would be good |
Yes, we can deep copy a physical plan to solve the problem with metrics. But this is not a solution for me, because physical plan can hold too much resources in the heap (for example, So, what do you think about an idea to extract metrics to the some place like
Nice idea, I'll take it up.
Yes, exactly, I don't want to force the user to use physical placeholders, just add the ability to have them in the plan and resolve them from context when the execution begins. |
I think this will be challenging and it isn't clear to me how it would work. It seems like it would mean when you call execute you'll have to update the
It seems like this field is
Maybe we can apply the same Another possibility might be to add a method like the following to the ExecutionPlan: trait ExecutionPlan {
fn reset_metrics(&mut self) -> Result<()>{ not_implemented_err!("Plan does not implement resetting metrics"}
...
} Since execution plans are Another possibility would be to |
Problem
While implementing the saving of prepared statements in our storage based on the DataFusion, we encountered the following issue:
It is inefficient to save the logical plan and rebuild the physical plan from it at execution time.
The physical planner and optimizer are quite heavy, and for large queries with many columns, building the physical plan can take 100-200 ms.
After reading the optimizer's code, I realized that it includes a lot of complex logic, which is generally quite difficult to optimize. Moreover, the time required for plan construction is an uncontrollable variable, as the optimizations can be arbitrarily complex in order to produce a good plan.
The current API does not allow reusing physical plans for multiple executions due to the following reasons:
Metrics. Since metrics are stored within the plans, streams are forced to share a pointer to the same metrics during execution. If physical plans are reused, streams will compete for writing to the metrics, which leads to non-scalability.
Lack of physical placeholders. This makes it impossible to substitute parameters during the execute(...) stage.
Possible solution
To address these issues, I created experimental patches that:
Move metric storage out of Execution Plans into
TaskContext
. The set of metrics is associated with a node of the ExecutionPlan, for example, metrics can be associated withProjectionExec
.Introduce physical placeholders implemented as a
PhysicalExpr
trait. These placeholders are resolved at the execute(...) stage, fetching parameters from theTaskContext
.The experimental patches can be found here:
https://github.com/tarantool/datafusion/commits/askalt/physical-placehdolders/
Challenges related to physical placeholders
For example, since the values of placeholders are not known during the optimizer passes, certain optimizations that depend on these values cannot be performed. In some cases, rebuilding the plan based on the parameters could be beneficial.
These issues can either be addressed by the user or resolved directly in DataFusion at a later stage.
In total
I’m creating this issue to discuss:
The text was updated successfully, but these errors were encountered: