akupchinskiy commented on code in PR #27047:
URL: https://github.com/apache/datafusion/pull/27047#discussion_r4739359385
##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -493,107 +480,93 @@ impl<S: ValueState> FirstLastGroupsAccumulator<S> {
})
}
- fn should_update_state(
- &self,
- group_idx: usize,
- new_ordering_values: &[ScalarValue],
- ) -> Result<bool> {
- if !self.is_sets.get_bit(group_idx) {
- return Ok(true);
- }
-
- debug_assert_eq!(new_ordering_values.len(), self.ordering_req.len());
- let current_ordering = &self.orderings[group_idx];
- compare_rows(current_ordering, new_ordering_values,
&self.sort_options).map(|x| {
- if self.pick_first_in_group {
- x.is_gt()
- } else {
- x.is_lt()
- }
- })
- }
-
- fn take_orderings(&mut self, emit_to: EmitTo) -> Vec<Vec<ScalarValue>> {
- let result = emit_to.take_needed(&mut self.orderings);
-
- match emit_to {
- EmitTo::All => self.size_of_orderings = 0,
- EmitTo::First(_) => {
- self.size_of_orderings -=
- result.iter().map(ScalarValue::size_of_vec).sum::<usize>()
- }
- }
-
- result
- }
-
fn resize_states(&mut self, new_size: usize) {
self.state.resize(new_size);
-
- if self.orderings.len() < new_size {
- let current_len = self.orderings.len();
-
- self.orderings
- .resize(new_size, self.default_orderings.clone());
-
- self.size_of_orderings += (new_size - current_len)
- * ScalarValue::size_of_vec(
- // Note: In some cases (such as in the unit test below)
- // ScalarValue::size_of_vec(&self.default_orderings) !=
ScalarValue::size_of_vec(&self.default_orderings.clone())
- // This may be caused by the different vec.capacity()
values?
- self.orderings.last().unwrap(),
- );
- }
-
+ self.orderings.resize(new_size);
self.is_sets.resize(new_size);
self.extreme_of_each_group_buf.0.resize(new_size, 0);
self.extreme_of_each_group_buf.1.resize(new_size);
}
- fn update_state(
+ /// Makes each batch row of `winners` (`(group_idx, idx_in_batch)`, at most
+ /// one per group) the group's winner when it beats the stored one
(strictly,
+ /// so ties keep the stored winner), or always when `replace`.
+ fn update_winners(
&mut self,
- group_idx: usize,
- orderings: &[ScalarValue],
- array: &ArrayRef,
- idx: usize,
+ ordering_cols: &[ArrayRef],
+ vals: &ArrayRef,
+ winners: &[(usize, usize)],
+ replace: bool,
) -> Result<()> {
- self.state.update(group_idx, array, idx)?;
- self.is_sets.set_bit(group_idx, true);
-
- debug_assert_eq!(orderings.len(), self.ordering_req.len());
- let old_size = ScalarValue::size_of_vec(&self.orderings[group_idx]);
- self.orderings[group_idx].clear();
- self.orderings[group_idx].extend_from_slice(orderings);
- let new_size = ScalarValue::size_of_vec(&self.orderings[group_idx]);
- self.size_of_orderings = self.size_of_orderings - old_size + new_size;
+ if winners.is_empty() {
+ return Ok(());
+ }
+ let pick_first = self.pick_first_in_group;
+ let beats = |current_vs_new: std::cmp::Ordering| {
+ if pick_first {
+ current_vs_new.is_gt()
+ } else {
+ current_vs_new.is_lt()
+ }
+ };
+ match &mut self.orderings {
+ GroupOrderings::Rows(r) => {
Review Comment:
Done in 766e59a4d6: the logic is in `GroupOrderings::update_winners` now;
the accumulator only passes a callback that updates the value.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]