Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9293195
feat: logical rewrite for filter on rn=1 to aggregate first_value
saadtajwar Jul 21, 2026
7549d87
helper functions for predicate and window input validation
saadtajwar Jul 22, 2026
7e741ec
finished rewrite
saadtajwar Jul 22, 2026
eb4bead
starting on tests
saadtajwar Jul 22, 2026
c5994d5
added more tests
saadtajwar Jul 22, 2026
ee28107
added docs
saadtajwar Jul 22, 2026
271d291
adding more comments
saadtajwar Jul 22, 2026
02a4c34
more comments
saadtajwar Jul 22, 2026
daa3858
removing debug stuff
saadtajwar Jul 22, 2026
9ae64e2
fixing CI bugs;
saadtajwar Jul 23, 2026
3ac1253
config change
saadtajwar Jul 23, 2026
e4b7e19
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Jul 23, 2026
6d4beb5
making access of config function registry safer
saadtajwar Jul 23, 2026
45c0461
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Jul 23, 2026
e92b951
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Jul 27, 2026
f1275a8
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Jul 31, 2026
7738463
addressing comments for rn name -> rn column
saadtajwar Aug 3, 2026
cc83011
adding comment for function name being safe
saadtajwar Aug 3, 2026
52d0ebe
spelling fix
saadtajwar Aug 4, 2026
c525f66
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Aug 7, 2026
7823342
more tests cases added
saadtajwar Aug 8, 2026
378777a
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Aug 8, 2026
8b62766
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Aug 10, 2026
302b9f6
Merge branch 'main' into saadtajwar/logical-rewrite-filter-top1
saadtajwar Aug 25, 2026
aa71da4
Merge upstream/main into saadtajwar/logical-rewrite-filter-top1
saadtajwar Oct 28, 2026
c254276
chore: update license headers
saadtajwar Oct 28, 2026
a90b0f5
rerun CI
saadtajwar Oct 28, 2026
5c1fd73
Merge branch 'main' of https://github.com/apache/datafusion into saad…
saadtajwar Nov 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions datafusion/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,16 @@ config_namespace! {
/// cause regressions in both memory usage and runtime.
pub enable_window_topn: bool, default = false

/// When set to true, the optimizer will rewrite a "top-1 per group"
/// pattern of the form `Filter(row_number() = 1)` over a `PARTITION BY` window into an
/// `Aggregate(first_value(... ORDER BY ...) GROUP BY partition)`.
/// This avoids buffering / fully sorting the input, which is especially beneficial on wide payloads.
/// Disabled by default because on nested/wide value types the aggregate only becomes memory
/// efficient once the columnar nested-type `GroupsAccumulator` fast
/// path is available; enabling it without that support can route wide
/// payloads onto the slow per-group accumulator path.
pub enable_row_number_to_aggregate: bool, default = false
Comment thread
saadtajwar marked this conversation as resolved.
Resolved

/// When set to true, the optimizer will push TopK (Sort with fetch)
/// below hash repartition when the partition key is a prefix of the
/// sort key, reducing data volume before the shuffle.
Expand Down
1 change: 1 addition & 0 deletions datafusion/core/src/optimizer_rule_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Rule order matters. The default pipeline may change between releases.
| 24 | `extract_leaf_expressions` | Pulls cheap leaf expressions closer to data sources so later pruning and filter rules can act earlier. |
| 25 | `push_down_leaf_projections` | Pushes the helper projections created by leaf extraction toward leaf inputs. |
| 26 | `optimize_projections` | Prunes unused columns and removes unnecessary logical projections. |
| 27 | `replace_filter_top1` | Rewrites `Filter(row_number() = 1)` over a partitioned window into a `first_value` aggregate. |

### Physical Optimizer Rules

Expand Down
1 change: 1 addition & 0 deletions datafusion/optimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod propagate_empty_relation;
pub mod push_down_filter;
pub mod push_down_limit;
pub mod replace_distinct_aggregate;
pub mod replace_filter_top1;
pub mod rewrite_set_comparison;
pub mod scalar_subquery_to_join;
pub mod simplify_expressions;
Expand Down
2 changes: 2 additions & 0 deletions datafusion/optimizer/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::propagate_empty_relation::PropagateEmptyRelation;
use crate::push_down_filter::PushDownFilter;
use crate::push_down_limit::PushDownLimit;
use crate::replace_distinct_aggregate::ReplaceDistinctWithAggregate;
use crate::replace_filter_top1::ReplaceFilterTop1;
use crate::rewrite_set_comparison::RewriteSetComparison;
use crate::scalar_subquery_to_join::ScalarSubqueryToJoin;
use crate::simplify_expressions::SimplifyExpressions;
Expand Down Expand Up @@ -302,6 +303,7 @@ impl Optimizer {
Arc::new(ExtractLeafExpressions::new()),
Arc::new(PushDownLeafProjections::new()),
Arc::new(OptimizeProjections::new()),
Arc::new(ReplaceFilterTop1::new()),
];

Self::with_rules(rules)
Expand Down
Loading
Loading