Skip to content

Commit b0566c5

Browse files
notashesadriangb
andauthored
fix: update filter predicates for min/max aggregates only if bounds change (#20380)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of #20324 (dynamic filter update overhead for aggregates) ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Right now `AggregateStream::maybe_update_dyn_filter()` unconditionally updates filter predicates after every batch even without any change. Which triggers predicate rebuilds for file pruners even though we converge towards the bounds quite early on. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> We only conditionally update filter predicates if the bounds change. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> The existing test suits pass. The existing clickbench regression shows virtually no change. <details> <summary>Benchmark Results</summary> The existing benchmarks don't reproduce this as it gets the data from stats. So we can use a trick like `"EventDate" + 0` to force full table scan. ```sql SET datafusion.execution.parquet.binary_as_string = true; SET datafusion.execution.parquet.pushdown_filters = false; CREATE EXTERNAL TABLE hits STORED AS PARQUET LOCATION 'benchmarks/data/hits_partitioned'; CREATE VIEW hits_view AS SELECT "EventDate" + 0 AS "EventDate" FROM hits; -- Warmup SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; -- dyn_off x5: hard-coded filter, dynamic filter disabled SET datafusion.optimizer.enable_aggregate_dynamic_filter_pushdown = false; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view WHERE "EventDate" > 0 AND "EventDate" < 99999999; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view WHERE "EventDate" > 0 AND "EventDate" < 99999999; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view WHERE "EventDate" > 0 AND "EventDate" < 99999999; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view WHERE "EventDate" > 0 AND "EventDate" < 99999999; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view WHERE "EventDate" > 0 AND "EventDate" < 99999999; -- dyn_on x5: dynamic filter enabled SET datafusion.optimizer.enable_aggregate_dynamic_filter_pushdown = true; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; SELECT MIN("EventDate"), MAX("EventDate") FROM hits_view; ``` ### Before (baseline) | Iteration | dyn_off | dyn_on | |-----------|---------|--------| | 1 | 39ms | 44ms | | 2 | 39ms | 44ms | | 3 | 40ms | 44ms | | 4 | 39ms | 45ms | | 5 | 38ms | 45ms | | **Avg** | **39.0ms** | **44.4ms** | We can see that that it's a little slow to execute with dynamic filters on. ### After (this PR) | Iteration | dyn_off | dyn_on | |-----------|---------|--------| | 1 | 40ms | 33ms | | 2 | 41ms | 32ms | | 3 | 40ms | 32ms | | 4 | 42ms | 32ms | | 5 | 39ms | 31ms | | **Avg** | **40.4ms** | **32.0ms** | And we get approx 20% boost with the patch (in my local setup) </details> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
1 parent d47bd59 commit b0566c5

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

datafusion/physical-plan/src/aggregates/no_grouping.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ impl AggregateStreamInner {
161161
return Ok(());
162162
};
163163

164+
let mut bounds_changed = false;
165+
164166
for acc_info in &filter_state.supported_accumulators_info {
165167
let acc =
166168
self.accumulators
@@ -176,20 +178,27 @@ impl AggregateStreamInner {
176178
let current_bound = acc.evaluate()?;
177179
{
178180
let mut bound = acc_info.shared_bound.lock();
179-
match acc_info.aggr_type {
181+
let new_bound = match acc_info.aggr_type {
180182
DynamicFilterAggregateType::Max => {
181-
*bound = scalar_max(&bound, &current_bound)?;
183+
scalar_max(&bound, &current_bound)?
182184
}
183185
DynamicFilterAggregateType::Min => {
184-
*bound = scalar_min(&bound, &current_bound)?;
186+
scalar_min(&bound, &current_bound)?
185187
}
188+
};
189+
if new_bound != *bound {
190+
*bound = new_bound;
191+
bounds_changed = true;
186192
}
187193
}
188194
}
189195

190-
// Step 2: Sync the dynamic filter physical expression with reader
191-
let predicate = self.build_dynamic_filter_from_accumulator_bounds()?;
192-
filter_state.filter.update(predicate)?;
196+
// Step 2: Sync the dynamic filter physical expression with reader,
197+
// but only if any bound actually changed.
198+
if bounds_changed {
199+
let predicate = self.build_dynamic_filter_from_accumulator_bounds()?;
200+
filter_state.filter.update(predicate)?;
201+
}
193202

194203
Ok(())
195204
}

0 commit comments

Comments
 (0)