Skip to content

Commit

Permalink
pull out reverse sort exprs
Browse files Browse the repository at this point in the history
Signed-off-by: jayzhan211 <[email protected]>
  • Loading branch information
jayzhan211 committed Apr 16, 2024
1 parent 51aa6f9 commit 8edad9e
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions datafusion/core/src/physical_optimizer/convert_first_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ fn try_convert_first_last_if_better(

if aggr_fun_expr.is_some_and(|e| e.fun().name() == "FIRST_VALUE") {
let mut first_value = aggr_fun_expr.unwrap().clone();

if eq_properties.ordering_satisfy_requirement(&concat_slices(
prefix_requirement,
&aggr_req,
Expand All @@ -229,17 +228,7 @@ fn try_convert_first_last_if_better(
format!("LAST_VALUE({})", expr)
};

let sort_exprs = first_value.sort_exprs();
let reversed_sort_exprs = sort_exprs
.iter()
.map(|e| {
if let Expr::Sort(s) = e {
Expr::Sort(s.reverse())
} else {
e.clone()
}
})
.collect::<Vec<_>>();
let reversed_sort_exprs = reverse_sort_exprs(&first_value);

// Converting to LAST_VALUE enables more efficient execution
// given the existing ordering:
Expand Down Expand Up @@ -282,17 +271,7 @@ fn try_convert_first_last_if_better(
format!("FIRST_VALUE({})", expr)
};

let sort_exprs = last_value.sort_exprs();
let reversed_sort_exprs = sort_exprs
.iter()
.map(|e| {
if let Expr::Sort(s) = e {
Expr::Sort(s.reverse())
} else {
e.clone()
}
})
.collect::<Vec<_>>();
let reversed_sort_exprs = reverse_sort_exprs(&last_value);

let first_value = last_value
.with_name(name)
Expand All @@ -313,3 +292,16 @@ fn try_convert_first_last_if_better(

Ok(())
}

fn reverse_sort_exprs(e: &AggregateFunctionExpr) -> Vec<Expr> {
e.sort_exprs()
.iter()
.map(|e| {
if let Expr::Sort(s) = e {
Expr::Sort(s.reverse())
} else {
e.clone()
}
})
.collect::<Vec<_>>()
}

0 comments on commit 8edad9e

Please sign in to comment.