Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
review: let try_optimize_.. return Option instead of Result
  • Loading branch information
2010YOUY01 committed Sep 22, 2026
commit 2cf27d8e1022d765d46548ac4260ce1b16eb9ef1
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ impl LimitedDistinctAggregation {
aggr: &AggregateExec,
limit: usize,
) -> Option<Transformed<Arc<dyn ExecutionPlan>>> {
// rules for transforming this Aggregate are held in this method
if !aggr.is_unordered_unfiltered_group_by_distinct() {
return None;
}

let new_aggr = aggr.clone().try_optimize_distinct_soft_limit(limit).ok()?;
let new_aggr = aggr.clone().try_optimize_distinct_soft_limit(limit)?;
// An already limited aggregate still permits optimizing its partial child.
Some(new_aggr.update_data(|aggr| Arc::new(aggr) as Arc<dyn ExecutionPlan>))
}
Expand Down
15 changes: 9 additions & 6 deletions datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,25 +916,28 @@ impl AggregateExec {
/// # Consistency
///
/// This is a safe, atomic optimization: it preserves the grouping keys and
/// leaves the aggregate in a consistent state. Inapplicable requests are
/// no-ops; the existing configuration is returned unchanged.
/// leaves the aggregate in a consistent state. Ineligible aggregates return
/// `None`. Eligible aggregates are returned unchanged when the limit is zero
/// or no tighter than the existing limit.
pub fn try_optimize_distinct_soft_limit(
mut self,
limit: usize,
) -> Result<Transformed<Self>> {
) -> Option<Transformed<Self>> {
if !self.is_unordered_unfiltered_group_by_distinct() {
return None;
}
if limit == 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to always return "Ok" -- why is it returning Result?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 2cf27d8

|| !self.is_unordered_unfiltered_group_by_distinct()
|| self
.limit_options()
.is_some_and(|existing| existing.limit <= limit)
{
return Ok(Transformed::no(self));
return Some(Transformed::no(self));
}
self.kind = AggregateKind::DistinctLimit {
group_by: Arc::clone(self.group_by()),
limit,
};
Ok(Transformed::yes(self))
Some(Transformed::yes(self))
}

/// Function used in `OptimizeAggregateOrder` optimizer rule,
Expand Down