Skip to content

Commit 297de26

Browse files
committed
address comment
1 parent 56b11c4 commit 297de26

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

datafusion/functions-aggregate/src/first_last.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,21 +553,22 @@ where
553553
{
554554
fn update_batch(
555555
&mut self,
556-
values_with_orderings: &[ArrayRef],
556+
// e.g. first_value(a order by b): values_and_order_cols will be [a, b]
557+
values_and_order_cols: &[ArrayRef],
557558
group_indices: &[usize],
558559
opt_filter: Option<&BooleanArray>,
559560
total_num_groups: usize,
560561
) -> Result<()> {
561562
self.resize_states(total_num_groups);
562563

563-
let vals = values_with_orderings[0].as_primitive::<T>();
564+
let vals = values_and_order_cols[0].as_primitive::<T>();
564565

565566
let mut ordering_buf = Vec::with_capacity(self.ordering_req.len());
566567

567568
// The overhead of calling `extract_row_at_idx_to_buf` is somewhat high, so we need to minimize its calls as much as possible.
568569
for (group_idx, idx) in self
569570
.get_filtered_min_of_each_group(
570-
&values_with_orderings[1..],
571+
&values_and_order_cols[1..],
571572
group_indices,
572573
opt_filter,
573574
vals,
@@ -576,7 +577,7 @@ where
576577
.into_iter()
577578
{
578579
extract_row_at_idx_to_buf(
579-
&values_with_orderings[1..],
580+
&values_and_order_cols[1..],
580581
idx,
581582
&mut ordering_buf,
582583
)?;
@@ -643,7 +644,7 @@ where
643644

644645
let mut ordering_buf = Vec::with_capacity(self.ordering_req.len());
645646

646-
let (is_set_arr, val_and_orderings) = match values.split_last() {
647+
let (is_set_arr, val_and_order_cols) = match values.split_last() {
647648
Some(result) => result,
648649
None => return internal_err!("Empty row in FISRT_VALUE"),
649650
};
@@ -653,15 +654,15 @@ where
653654
let vals = values[0].as_primitive::<T>();
654655
// The overhead of calling `extract_row_at_idx_to_buf` is somewhat high, so we need to minimize its calls as much as possible.
655656
let groups = self.get_filtered_min_of_each_group(
656-
&val_and_orderings[1..],
657+
&val_and_order_cols[1..],
657658
group_indices,
658659
opt_filter,
659660
vals,
660661
Some(is_set_arr),
661662
)?;
662663

663664
for (group_idx, idx) in groups.into_iter() {
664-
extract_row_at_idx_to_buf(&val_and_orderings[1..], idx, &mut ordering_buf)?;
665+
extract_row_at_idx_to_buf(&val_and_order_cols[1..], idx, &mut ordering_buf)?;
665666

666667
if self.should_update_state(group_idx, &ordering_buf)? {
667668
self.update_state(

datafusion/sqllogictest/test_files/group_by.slt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,23 @@ TUR [100.0, 75.0] 175
26662666
# Some of the Aggregators can be reversed, by this way we can still run aggregators without re-ordering
26672667
# that have contradictory requirements at first glance.
26682668

2669+
statement ok
2670+
CREATE TABLE null_group (
2671+
a INT, b INT, c INT, d INT
2672+
) as VALUES
2673+
(6, 6, null, null),
2674+
(6, 6, 1, null),
2675+
(6, 6, null, 1)
2676+
2677+
query III rowsort
2678+
select c, d, first_value(a order by b) from null_group group by c, d;
2679+
----
2680+
1 NULL 6
2681+
NULL 1 6
2682+
NULL NULL 6
2683+
2684+
2685+
26692686
statement ok
26702687
CREATE TABLE first_null (
26712688
k INT,

0 commit comments

Comments
 (0)