alamb commented on code in PR #27453: URL: https://github.com/apache/datafusion/pull/27453#discussion_r4671440618
########## datafusion/physical-plan/src/execution_plan.rs: ########## @@ -2014,11 +2014,16 @@ pub fn check_not_null_constraints( /// /// While this function enables plan reuse, it does not allow the same plan to be executed if it (OR): /// -/// * uses dynamic filters and the original plan (or another reset copy) is still -/// executing, because operators such as `HashJoinExec` and `AggregateExec` -/// reset their shared dynamic filter in place, +/// * uses dynamic filters and the original plan runs after the copy was made +/// (or is still running), because a scan keeps the dynamic filter it was +/// given at planning time, which the original plan updates, +/// * uses a `HashJoinExec` dynamic filter and another reset copy is still +/// executing, because `HashJoinExec` resets its shared dynamic filter in place, /// * represents a recursive query. /// +/// For the same reason, the dynamic filters of `SortExec` and `AggregateExec` Review Comment: The docs https://github.com/apache/datafusion/blob/657d2cab48a5c3586fcfcb1bec416c333de9e966/datafusion/physical-plan/src/execution_plan.rs#L444-L471 say ```rust /// This method is called when an [`ExecutionPlan`] needs to be re-executed, /// such as in recursive queries. Unlike [`ExecutionPlan::replace_children`], this method /// ensures that any stateful components (e.g., [`DynamicFilterPhysicalExpr`]) /// are reset to their initial state. ``` If that is the case, why are we caveating HashJoin / AggregateExec? ########## datafusion/physical-plan/src/execution_plan.rs: ########## @@ -2014,11 +2014,16 @@ pub fn check_not_null_constraints( /// /// While this function enables plan reuse, it does not allow the same plan to be executed if it (OR): /// -/// * uses dynamic filters and the original plan (or another reset copy) is still -/// executing, because operators such as `HashJoinExec` and `AggregateExec` -/// reset their shared dynamic filter in place, +/// * uses dynamic filters and the original plan runs after the copy was made +/// (or is still running), because a scan keeps the dynamic filter it was +/// given at planning time, which the original plan updates, +/// * uses a `HashJoinExec` dynamic filter and another reset copy is still +/// executing, because `HashJoinExec` resets its shared dynamic filter in place, /// * represents a recursive query. /// +/// For the same reason, the dynamic filters of `SortExec` and `AggregateExec` Review Comment: I don't understand this statement or caveats. If this function is supposed to reset all the plan states, it should totally detach the dynamic filter states. I realize there was a pre-existing comment here about some cases it doesn't work in but just adding another case makes it more confusing I did some research and https://github.com/apache/datafusion/pull/26669#pullrequestreview-6028726791 So basically can we please find some more general purpose way to sort out dynamic filters / recursive cts / document how user defined plans that have dynamic filters interact ########## datafusion/physical-plan/src/aggregates/mod.rs: ########## @@ -2282,14 +2282,36 @@ impl ExecutionPlan for AggregateExec { } fn reset_state(self: Arc<Self>) -> Result<Arc<dyn ExecutionPlan>> { + let mut aggregate = (*self).clone(); + // Our dynamic filter, its bounds and execution metrics are the state + // we need to reset. if let Some(dynamic_filter) = &self.dynamic_filter { + // A scan below keeps the filter it was given at planning time, so + // clear its bounds before the plan runs again. dynamic_filter.filter.reset(); for accumulator_filter in &dynamic_filter.accumulator_dyn_filter_info { *accumulator_filter.shared_bound.lock() = ScalarValue::Null; } + // Each copy gets its own filter and bounds, so a copy that runs + // first cannot tighten the filter another copy reads. See + // <https://github.com/apache/datafusion/issues/27452>. + aggregate.dynamic_filter = Some(Arc::new(AggrDynFilter { Review Comment: this is really messy code I think (all the inline creation / copy, etc). Can we put it behind a method (perhaps a `reset()` method on `AggrDynFilter`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
