Skip to content
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

Remove Result.unwrap from single_distinct_to_groupby #3345

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Changes from all commits
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
31 changes: 14 additions & 17 deletions datafusion/optimizer/src/single_distinct_to_groupby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn optimize(plan: &LogicalPlan) -> Result<LogicalPlan> {
schema,
group_expr,
}) => {
if is_single_distinct_agg(plan) && !contains_grouping_set(group_expr) {
if is_single_distinct_agg(plan)? && !contains_grouping_set(group_expr) {
let mut group_fields_set = HashSet::new();
let base_group_expr = grouping_set_to_exprlist(group_expr)?;
let mut all_group_args: Vec<Expr> = group_expr.clone();
Expand Down Expand Up @@ -159,27 +159,24 @@ fn optimize_children(plan: &LogicalPlan) -> Result<LogicalPlan> {
from_plan(plan, &expr, &new_inputs)
}

fn is_single_distinct_agg(plan: &LogicalPlan) -> bool {
fn is_single_distinct_agg(plan: &LogicalPlan) -> Result<bool> {
match plan {
LogicalPlan::Aggregate(Aggregate { aggr_expr, .. }) => {
let mut fields_set = HashSet::new();
aggr_expr
.iter()
.filter(|expr| {
let mut is_distinct = false;
if let Expr::AggregateFunction { distinct, args, .. } = expr {
is_distinct = *distinct;
args.iter().for_each(|expr| {
fields_set.insert(expr.name().unwrap());
})
let mut count = 0;
for expr in aggr_expr {
if let Expr::AggregateFunction { distinct, args, .. } = expr {
if *distinct {
count += 1;
}
is_distinct
})
.count()
== aggr_expr.len()
&& fields_set.len() == 1
for expr in args {
fields_set.insert(expr.name()?);
Copy link
Contributor

Choose a reason for hiding this comment

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

Higher-order functions look good on interview code, but they could use a better story regarding error-propagation :/

}
}
}
Ok(count == aggr_expr.len() && fields_set.len() == 1)
}
_ => false,
_ => Ok(false),
}
}

Expand Down