Skip to content

Commit 3bb5dcb

Browse files
committed
Precalculate sort_options for all structs
1 parent 1d44595 commit 3bb5dcb

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
@@ -846,6 +846,8 @@ pub struct FirstValueAccumulator {
846846
orderings: Vec<ScalarValue>,
847847
// Stores the applicable ordering requirement.
848848
ordering_req: LexOrdering,
849+
// derived from `ordering_req`.
850+
sort_options: Vec<SortOptions>,
849851
// Stores whether incoming data already satisfies the ordering requirement.
850852
is_input_pre_ordered: bool,
851853
// Ignore null values.
@@ -865,11 +867,13 @@ impl FirstValueAccumulator {
865867
.iter()
866868
.map(ScalarValue::try_from)
867869
.collect::<Result<_>>()?;
870+
let sort_options = get_sort_options(&ordering_req);
868871
ScalarValue::try_from(data_type).map(|first| Self {
869872
first,
870873
is_set: false,
871874
orderings,
872875
ordering_req,
876+
sort_options,
873877
is_input_pre_ordered,
874878
ignore_nulls,
875879
})
@@ -942,12 +946,8 @@ impl Accumulator for FirstValueAccumulator {
942946
let row = get_row_at_idx(values, first_idx)?;
943947
if !self.is_set
944948
|| (!self.is_input_pre_ordered
945-
&& compare_rows(
946-
&self.orderings,
947-
&row[1..],
948-
&get_sort_options(&self.ordering_req),
949-
)?
950-
.is_gt())
949+
&& compare_rows(&self.orderings, &row[1..], &self.sort_options)?
950+
.is_gt())
951951
{
952952
self.update_with_new_row(row);
953953
}
@@ -975,10 +975,10 @@ impl Accumulator for FirstValueAccumulator {
975975
let mut first_row = get_row_at_idx(&filtered_states, first_idx)?;
976976
// When collecting orderings, we exclude the is_set flag from the state.
977977
let first_ordering = &first_row[1..is_set_idx];
978-
let sort_options = get_sort_options(&self.ordering_req);
979978
// Either there is no existing value, or there is an earlier version in new data.
980979
if !self.is_set
981-
|| compare_rows(&self.orderings, first_ordering, &sort_options)?.is_gt()
980+
|| compare_rows(&self.orderings, first_ordering, &self.sort_options)?
981+
.is_gt()
982982
{
983983
// Update with first value in the state. Note that we should exclude the
984984
// is_set flag from the state. Otherwise, we will end up with a state
@@ -1335,6 +1335,8 @@ struct LastValueAccumulator {
13351335
orderings: Vec<ScalarValue>,
13361336
// Stores the applicable ordering requirement.
13371337
ordering_req: LexOrdering,
1338+
// derived from `ordering_req`.
1339+
sort_options: Vec<SortOptions>,
13381340
// Stores whether incoming data already satisfies the ordering requirement.
13391341
is_input_pre_ordered: bool,
13401342
// Ignore null values.
@@ -1354,11 +1356,13 @@ impl LastValueAccumulator {
13541356
.iter()
13551357
.map(ScalarValue::try_from)
13561358
.collect::<Result<_>>()?;
1359+
let sort_options = get_sort_options(&ordering_req);
13571360
ScalarValue::try_from(data_type).map(|last| Self {
13581361
last,
13591362
is_set: false,
13601363
orderings,
13611364
ordering_req,
1365+
sort_options,
13621366
is_input_pre_ordered,
13631367
ignore_nulls,
13641368
})
@@ -1431,12 +1435,7 @@ impl Accumulator for LastValueAccumulator {
14311435
// Update when there is a more recent entry
14321436
if !self.is_set
14331437
|| self.is_input_pre_ordered
1434-
|| compare_rows(
1435-
&self.orderings,
1436-
orderings,
1437-
&get_sort_options(&self.ordering_req),
1438-
)?
1439-
.is_lt()
1438+
|| compare_rows(&self.orderings, orderings, &self.sort_options)?.is_lt()
14401439
{
14411440
self.update_with_new_row(row);
14421441
}
@@ -1464,12 +1463,12 @@ impl Accumulator for LastValueAccumulator {
14641463
let mut last_row = get_row_at_idx(&filtered_states, last_idx)?;
14651464
// When collecting orderings, we exclude the is_set flag from the state.
14661465
let last_ordering = &last_row[1..is_set_idx];
1467-
let sort_options = get_sort_options(&self.ordering_req);
14681466
// Either there is no existing value, or there is a newer (latest)
14691467
// version in the new data:
14701468
if !self.is_set
14711469
|| self.is_input_pre_ordered
1472-
|| compare_rows(&self.orderings, last_ordering, &sort_options)?.is_lt()
1470+
|| compare_rows(&self.orderings, last_ordering, &self.sort_options)?
1471+
.is_lt()
14731472
{
14741473
// Update with last value in the state. Note that we should exclude the
14751474
// is_set flag from the state. Otherwise, we will end up with a state

0 commit comments

Comments
 (0)