Skip to content

Commit f0554ce

Browse files
committed
Precalculate sort_options for all structs
1 parent 14929f0 commit f0554ce

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

datafusion/functions-aggregate/src/first_last.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ pub struct FirstValueAccumulator {
830830
orderings: Vec<ScalarValue>,
831831
// Stores the applicable ordering requirement.
832832
ordering_req: LexOrdering,
833+
// derived from `ordering_req`.
834+
sort_options: Vec<SortOptions>,
833835
// Stores whether incoming data already satisfies the ordering requirement.
834836
is_input_pre_ordered: bool,
835837
// Ignore null values.
@@ -849,11 +851,13 @@ impl FirstValueAccumulator {
849851
.iter()
850852
.map(ScalarValue::try_from)
851853
.collect::<Result<_>>()?;
854+
let sort_options = get_sort_options(&ordering_req);
852855
ScalarValue::try_from(data_type).map(|first| Self {
853856
first,
854857
is_set: false,
855858
orderings,
856859
ordering_req,
860+
sort_options,
857861
is_input_pre_ordered,
858862
ignore_nulls,
859863
})
@@ -926,12 +930,8 @@ impl Accumulator for FirstValueAccumulator {
926930
let row = get_row_at_idx(values, first_idx)?;
927931
if !self.is_set
928932
|| (!self.is_input_pre_ordered
929-
&& compare_rows(
930-
&self.orderings,
931-
&row[1..],
932-
&get_sort_options(&self.ordering_req),
933-
)?
934-
.is_gt())
933+
&& compare_rows(&self.orderings, &row[1..], &self.sort_options)?
934+
.is_gt())
935935
{
936936
self.update_with_new_row(row);
937937
}
@@ -959,10 +959,10 @@ impl Accumulator for FirstValueAccumulator {
959959
let mut first_row = get_row_at_idx(&filtered_states, first_idx)?;
960960
// When collecting orderings, we exclude the is_set flag from the state.
961961
let first_ordering = &first_row[1..is_set_idx];
962-
let sort_options = get_sort_options(&self.ordering_req);
963962
// Either there is no existing value, or there is an earlier version in new data.
964963
if !self.is_set
965-
|| compare_rows(&self.orderings, first_ordering, &sort_options)?.is_gt()
964+
|| compare_rows(&self.orderings, first_ordering, &self.sort_options)?
965+
.is_gt()
966966
{
967967
// Update with first value in the state. Note that we should exclude the
968968
// is_set flag from the state. Otherwise, we will end up with a state
@@ -1219,6 +1219,8 @@ struct LastValueAccumulator {
12191219
orderings: Vec<ScalarValue>,
12201220
// Stores the applicable ordering requirement.
12211221
ordering_req: LexOrdering,
1222+
// derived from `ordering_req`.
1223+
sort_options: Vec<SortOptions>,
12221224
// Stores whether incoming data already satisfies the ordering requirement.
12231225
is_input_pre_ordered: bool,
12241226
// Ignore null values.
@@ -1238,11 +1240,13 @@ impl LastValueAccumulator {
12381240
.iter()
12391241
.map(ScalarValue::try_from)
12401242
.collect::<Result<_>>()?;
1243+
let sort_options = get_sort_options(&ordering_req);
12411244
ScalarValue::try_from(data_type).map(|last| Self {
12421245
last,
12431246
is_set: false,
12441247
orderings,
12451248
ordering_req,
1249+
sort_options,
12461250
is_input_pre_ordered,
12471251
ignore_nulls,
12481252
})
@@ -1315,12 +1319,7 @@ impl Accumulator for LastValueAccumulator {
13151319
// Update when there is a more recent entry
13161320
if !self.is_set
13171321
|| self.is_input_pre_ordered
1318-
|| compare_rows(
1319-
&self.orderings,
1320-
orderings,
1321-
&get_sort_options(&self.ordering_req),
1322-
)?
1323-
.is_lt()
1322+
|| compare_rows(&self.orderings, orderings, &self.sort_options)?.is_lt()
13241323
{
13251324
self.update_with_new_row(row);
13261325
}
@@ -1348,12 +1347,12 @@ impl Accumulator for LastValueAccumulator {
13481347
let mut last_row = get_row_at_idx(&filtered_states, last_idx)?;
13491348
// When collecting orderings, we exclude the is_set flag from the state.
13501349
let last_ordering = &last_row[1..is_set_idx];
1351-
let sort_options = get_sort_options(&self.ordering_req);
13521350
// Either there is no existing value, or there is a newer (latest)
13531351
// version in the new data:
13541352
if !self.is_set
13551353
|| self.is_input_pre_ordered
1356-
|| compare_rows(&self.orderings, last_ordering, &sort_options)?.is_lt()
1354+
|| compare_rows(&self.orderings, last_ordering, &self.sort_options)?
1355+
.is_lt()
13571356
{
13581357
// Update with last value in the state. Note that we should exclude the
13591358
// is_set flag from the state. Otherwise, we will end up with a state

0 commit comments

Comments
 (0)