-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Optimize group by with limit #22009
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
Draft
Dandandan
wants to merge
5
commits into
apache:main
Choose a base branch
from
Dandandan:optimize-clickbench-q17-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−23
Draft
Optimize group by with limit #22009
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbd05b5
Optimize ClickBench q17 aggregate limit
Dandandan 5c40e58
Remove benchmark allocator cfg changes
Dandandan d611742
Simplify aggregate limit prefiltering
Dandandan 8104f78
Update aggregate limit optimizer tests
Dandandan 2d27206
fix: avoid limit prefilter for FD group keys
Dandandan 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,16 +18,17 @@ | |
| //! [`PushDownLimit`] pushes `LIMIT` earlier in the query plan | ||
|
|
||
| use std::cmp::min; | ||
| use std::collections::HashSet; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::optimizer::ApplyOrder; | ||
| use crate::{OptimizerConfig, OptimizerRule}; | ||
|
|
||
| use datafusion_common::Result; | ||
| use datafusion_common::tree_node::Transformed; | ||
| use datafusion_common::utils::combine_limit; | ||
| use datafusion_expr::logical_plan::{Join, JoinType, Limit, LogicalPlan}; | ||
| use datafusion_expr::{FetchType, SkipType, lit}; | ||
| use datafusion_common::{NullEquality, Result, get_required_group_by_exprs_indices}; | ||
| use datafusion_expr::logical_plan::{Aggregate, Join, JoinType, Limit, LogicalPlan}; | ||
| use datafusion_expr::{Expr, FetchType, LogicalPlanBuilder, SkipType, lit}; | ||
|
|
||
| /// Optimization rule that tries to push down `LIMIT`. | ||
| //. It will push down through projection, limits (taking the smaller limit) | ||
|
|
@@ -47,7 +48,6 @@ impl OptimizerRule for PushDownLimit { | |
| true | ||
| } | ||
|
|
||
| #[expect(clippy::only_used_in_recursion)] | ||
| fn rewrite( | ||
| &self, | ||
| plan: LogicalPlan, | ||
|
|
@@ -123,6 +123,21 @@ impl OptimizerRule for PushDownLimit { | |
| make_limit(skip, fetch, Arc::new(LogicalPlan::Join(join))) | ||
| })), | ||
|
|
||
| LogicalPlan::Aggregate(aggregate) | ||
| if config | ||
| .options() | ||
| .optimizer | ||
| .enable_distinct_aggregation_soft_limit => | ||
| { | ||
| if let Some(aggregate) = | ||
| prefilter_limited_aggregate(aggregate.clone(), fetch + skip)? | ||
| { | ||
| transformed_limit(skip, fetch, aggregate) | ||
| } else { | ||
| original_limit(skip, fetch, LogicalPlan::Aggregate(aggregate)) | ||
| } | ||
| } | ||
|
|
||
| LogicalPlan::Sort(mut sort) => { | ||
| let new_fetch = { | ||
| let sort_fetch = skip + fetch; | ||
|
|
@@ -237,6 +252,99 @@ fn transformed_limit( | |
| Ok(Transformed::yes(make_limit(skip, fetch, Arc::new(input)))) | ||
| } | ||
|
|
||
| /// Rewrite `LIMIT K (GROUP BY keys, aggs)` into a key preselection followed | ||
|
Contributor
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 would probably describe this more like: SELECT aggs(...) GROUP BY keys LIMIT k |
||
| /// by a semi join. This keeps the aggregate itself ordinary while letting the | ||
| /// join's dynamic filter push the selected key set into the second input scan. | ||
| fn prefilter_limited_aggregate( | ||
| aggregate: Aggregate, | ||
| limit: usize, | ||
| ) -> Result<Option<LogicalPlan>> { | ||
| if limit == 0 || aggregate.aggr_expr.is_empty() || aggregate.group_expr.is_empty() { | ||
| return Ok(None); | ||
| } | ||
| if is_key_prefiltered_aggregate(&aggregate) { | ||
| return Ok(None); | ||
| } | ||
| if has_functionally_reducible_group_exprs(&aggregate) { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let mut seen_columns = HashSet::with_capacity(aggregate.group_expr.len()); | ||
| let mut join_columns = Vec::with_capacity(aggregate.group_expr.len()); | ||
| for expr in &aggregate.group_expr { | ||
| let Expr::Column(column) = expr else { | ||
| return Ok(None); | ||
| }; | ||
| if !seen_columns.insert(column.clone()) { | ||
| return Ok(None); | ||
| } | ||
| join_columns.push(column.clone()); | ||
| } | ||
|
|
||
| let key_input = aggregate.input.as_ref().clone(); | ||
| let keys = LogicalPlanBuilder::from(key_input) | ||
| .aggregate(aggregate.group_expr.clone(), Vec::<Expr>::new())? | ||
| .limit(0, Some(limit))? | ||
| .build()?; | ||
|
|
||
| let filtered_input = LogicalPlanBuilder::from(keys) | ||
| .join_detailed( | ||
| aggregate.input.as_ref().clone(), | ||
| JoinType::RightSemi, | ||
| (join_columns.clone(), join_columns), | ||
| None, | ||
| NullEquality::NullEqualsNull, | ||
| )? | ||
| .build()?; | ||
|
|
||
| Aggregate::try_new( | ||
| Arc::new(filtered_input), | ||
| aggregate.group_expr, | ||
| aggregate.aggr_expr, | ||
| ) | ||
| .map(LogicalPlan::Aggregate) | ||
| .map(Some) | ||
| } | ||
|
|
||
| fn has_functionally_reducible_group_exprs(aggregate: &Aggregate) -> bool { | ||
| if aggregate | ||
| .input | ||
| .schema() | ||
| .functional_dependencies() | ||
| .is_empty() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| let group_expr_names = aggregate | ||
| .group_expr | ||
| .iter() | ||
| .map(|expr| expr.schema_name().to_string()) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| get_required_group_by_exprs_indices(aggregate.input.schema(), &group_expr_names) | ||
| .is_some_and(|required_indices| { | ||
| required_indices.len() < aggregate.group_expr.len() | ||
| }) | ||
| } | ||
|
|
||
| fn is_key_prefiltered_aggregate(aggregate: &Aggregate) -> bool { | ||
| let LogicalPlan::Join(join) = aggregate.input.as_ref() else { | ||
| return false; | ||
| }; | ||
| if join.join_type != JoinType::RightSemi { | ||
| return false; | ||
| } | ||
| let LogicalPlan::Limit(limit) = join.left.as_ref() else { | ||
| return false; | ||
| }; | ||
| let LogicalPlan::Aggregate(keys) = limit.input.as_ref() else { | ||
| return false; | ||
| }; | ||
|
|
||
| keys.aggr_expr.is_empty() && keys.group_expr == aggregate.group_expr | ||
| } | ||
|
|
||
| /// Adds a limit to the inputs of a join, if possible | ||
| fn push_down_join(mut join: Join, limit: usize) -> Transformed<Join> { | ||
| use JoinType::*; | ||
|
|
@@ -279,10 +387,11 @@ mod test { | |
| use crate::test::*; | ||
|
|
||
| use crate::OptimizerContext; | ||
| use datafusion_common::DFSchemaRef; | ||
| use arrow::datatypes::Schema; | ||
| use datafusion_common::{Constraint, Constraints, DFSchemaRef}; | ||
| use datafusion_expr::{ | ||
| Expr, Extension, UserDefinedLogicalNodeCore, col, exists, | ||
| logical_plan::builder::LogicalPlanBuilder, | ||
| logical_plan::builder::{LogicalPlanBuilder, table_source_with_constraints}, | ||
| }; | ||
| use datafusion_functions_aggregate::expr_fn::max; | ||
|
|
||
|
|
@@ -583,20 +692,52 @@ mod test { | |
| } | ||
|
|
||
| #[test] | ||
| fn limit_doesnt_push_down_aggregation() -> Result<()> { | ||
| fn limit_prefilters_aggregation() -> Result<()> { | ||
| let table_scan = test_table_scan()?; | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .aggregate(vec![col("a")], vec![max(col("b"))])? | ||
| .limit(0, Some(1000))? | ||
| .build()?; | ||
|
|
||
| // Limit should *not* push down aggregate node | ||
| // Limit preselects group keys before running the aggregate | ||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Limit: skip=0, fetch=1000 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]] | ||
| RightSemi Join: test.a = test.a | ||
| Limit: skip=0, fetch=1000 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[]] | ||
| TableScan: test | ||
| TableScan: test | ||
| " | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn limit_does_not_prefilter_fd_reducible_aggregation() -> Result<()> { | ||
| let constraints = | ||
| Constraints::new_unverified(vec![Constraint::PrimaryKey(vec![0])]); | ||
| let table_source = table_source_with_constraints( | ||
| &Schema::new(test_table_scan_fields()), | ||
| constraints, | ||
| ); | ||
| let table_scan = LogicalPlanBuilder::scan("test", table_source, None)?.build()?; | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .aggregate(vec![col("a"), col("b"), col("c")], vec![max(col("b"))])? | ||
| .limit(0, Some(1000))? | ||
| .build()?; | ||
|
|
||
| // SQL planning may add functionally dependent fields as implicit group | ||
| // keys. Do not turn those redundant keys into semijoin predicates before | ||
| // projection optimization has a chance to simplify them. | ||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Limit: skip=0, fetch=1000 | ||
| Aggregate: groupBy=[[test.a, test.b, test.c]], aggr=[[max(test.b)]] | ||
| TableScan: test | ||
| " | ||
| ) | ||
|
|
@@ -675,14 +816,20 @@ mod test { | |
| .limit(0, Some(10))? | ||
| .build()?; | ||
|
|
||
| // Limit should use deeper LIMIT 1000, but Limit 10 shouldn't push down aggregation | ||
| // Limit should use deeper LIMIT 1000 and preselect group keys for the | ||
| // aggregate using the outer LIMIT 10. | ||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Limit: skip=0, fetch=10 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]] | ||
| Limit: skip=0, fetch=1000 | ||
| TableScan: test, fetch=1000 | ||
| RightSemi Join: test.a = test.a | ||
| Limit: skip=0, fetch=10 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[]] | ||
| Limit: skip=0, fetch=1000 | ||
| TableScan: test, fetch=1000 | ||
| Limit: skip=0, fetch=1000 | ||
| TableScan: test, fetch=1000 | ||
| " | ||
| ) | ||
| } | ||
|
|
@@ -786,21 +933,25 @@ mod test { | |
| } | ||
|
|
||
| #[test] | ||
| fn limit_doesnt_push_down_with_offset_aggregation() -> Result<()> { | ||
| fn limit_with_offset_prefilters_aggregation() -> Result<()> { | ||
| let table_scan = test_table_scan()?; | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .aggregate(vec![col("a")], vec![max(col("b"))])? | ||
| .limit(10, Some(1000))? | ||
| .build()?; | ||
|
|
||
| // Limit should *not* push down aggregate node | ||
| // Limit preselects enough group keys to satisfy offset and fetch | ||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Limit: skip=10, fetch=1000 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]] | ||
| TableScan: test | ||
| RightSemi Join: test.a = test.a | ||
| Limit: skip=0, fetch=1010 | ||
| Aggregate: groupBy=[[test.a]], aggr=[[]] | ||
| TableScan: test | ||
| TableScan: test | ||
| " | ||
| ) | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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.
Did you consider adding a
LIMITdirectly to the GroupByHash operator? I am not sure how much extra complexity that is, but you could probably model similarly to aSUM(a FILTER key IN <hash table>)Though it might add a lot more complexity 🤔
Or we could implement a special GroupBy operator, similar to what @avantgardnerio implemented for GROUPBY with a limit https://github.com/apache/datafusion/tree/main/datafusion/physical-plan/src/aggregates/topk)