Skip to content

Commit 7c5a690

Browse files
committed
WIP/claude: speed up sorted aggregations by skipping some compute
1 parent a4a36cf commit 7c5a690

9 files changed

Lines changed: 494 additions & 37 deletions

File tree

‎datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ use std::sync::Arc;
2020
use crate::aggregates::group_values::multi_group_by::Nulls;
2121
use crate::aggregates::group_values::multi_group_by::{GroupColumn, nulls_equal_to};
2222
use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;
23+
use ahash::RandomState;
2324
use arrow::array::{Array as _, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder};
2425
use datafusion_common::Result;
26+
use datafusion_common::hash_utils::{HashValue, combine_hashes};
2527
use itertools::izip;
2628

2729
/// An implementation of [`GroupColumn`] for booleans
@@ -191,6 +193,60 @@ impl<const NULLABLE: bool> GroupColumn for BooleanGroupValueBuilder<NULLABLE> {
191193

192194
Arc::new(BooleanArray::new(new_builder.finish(), first_n_nulls))
193195
}
196+
197+
fn input_rows_equal(&self, array: &ArrayRef, row_a: usize, row_b: usize) -> bool {
198+
if NULLABLE {
199+
let a_null = array.is_null(row_a);
200+
let b_null = array.is_null(row_b);
201+
if a_null || b_null {
202+
return a_null && b_null;
203+
}
204+
}
205+
let arr = array.as_boolean();
206+
arr.value(row_a) == arr.value(row_b)
207+
}
208+
209+
fn hash_input_row(
210+
&self,
211+
array: &ArrayRef,
212+
row: usize,
213+
random_state: &RandomState,
214+
rehash: bool,
215+
current_hash: u64,
216+
) -> u64 {
217+
if NULLABLE && array.is_null(row) {
218+
return current_hash;
219+
}
220+
let value = array.as_boolean().value(row);
221+
let h = value.hash_one(random_state);
222+
if rehash {
223+
combine_hashes(h, current_hash)
224+
} else {
225+
h
226+
}
227+
}
228+
229+
fn compute_boundaries(&self, array: &ArrayRef, boundaries: &mut [bool]) {
230+
let arr = array.as_boolean();
231+
if NULLABLE && array.null_count() > 0 {
232+
for row in 1..array.len() {
233+
if boundaries[row] {
234+
continue;
235+
}
236+
let prev_null = array.is_null(row - 1);
237+
let curr_null = array.is_null(row);
238+
if prev_null != curr_null || (!prev_null && arr.value(row - 1) != arr.value(row)) {
239+
boundaries[row] = true;
240+
}
241+
}
242+
} else {
243+
for row in 1..array.len() {
244+
if !boundaries[row] && arr.value(row - 1) != arr.value(row) {
245+
boundaries[row] = true;
246+
}
247+
}
248+
}
249+
}
194250
}
195251

196252
#[cfg(test)]

‎datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rs‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use arrow::array::{
2626
use arrow::buffer::{OffsetBuffer, ScalarBuffer};
2727
use arrow::datatypes::{ByteArrayType, DataType, GenericBinaryType};
2828
use datafusion_common::utils::proxy::VecAllocExt;
29+
use ahash::RandomState;
30+
use datafusion_common::hash_utils::{HashValue, combine_hashes};
2931
use datafusion_common::{Result, exec_datafusion_err};
3032
use datafusion_physical_expr_common::binary_map::{INITIAL_BUFFER_CAPACITY, OutputType};
3133
use itertools::izip;
@@ -426,6 +428,95 @@ where
426428
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
427429
}
428430
}
431+
432+
fn input_rows_equal(&self, array: &ArrayRef, row_a: usize, row_b: usize) -> bool {
433+
let a_null = array.is_null(row_a);
434+
let b_null = array.is_null(row_b);
435+
if a_null || b_null {
436+
return a_null && b_null;
437+
}
438+
match self.output_type {
439+
OutputType::Binary => {
440+
let arr = array.as_bytes::<GenericBinaryType<O>>();
441+
arr.value(row_a) == arr.value(row_b)
442+
}
443+
OutputType::Utf8 => {
444+
let arr = array.as_bytes::<GenericStringType<O>>();
445+
arr.value(row_a) == arr.value(row_b)
446+
}
447+
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
448+
}
449+
}
450+
451+
fn hash_input_row(
452+
&self,
453+
array: &ArrayRef,
454+
row: usize,
455+
random_state: &RandomState,
456+
rehash: bool,
457+
current_hash: u64,
458+
) -> u64 {
459+
if array.is_null(row) {
460+
return current_hash;
461+
}
462+
let h = match self.output_type {
463+
OutputType::Binary => {
464+
let arr = array.as_bytes::<GenericBinaryType<O>>();
465+
arr.value(row).hash_one(random_state)
466+
}
467+
OutputType::Utf8 => {
468+
let arr = array.as_bytes::<GenericStringType<O>>();
469+
arr.value(row).hash_one(random_state)
470+
}
471+
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
472+
};
473+
if rehash {
474+
combine_hashes(h, current_hash)
475+
} else {
476+
h
477+
}
478+
}
479+
480+
fn compute_boundaries(&self, array: &ArrayRef, boundaries: &mut [bool]) {
481+
let has_nulls = array.null_count() > 0;
482+
match self.output_type {
483+
OutputType::Binary => {
484+
let arr = array.as_bytes::<GenericBinaryType<O>>();
485+
for row in 1..array.len() {
486+
if boundaries[row] {
487+
continue;
488+
}
489+
if has_nulls {
490+
let prev_null = array.is_null(row - 1);
491+
let curr_null = array.is_null(row);
492+
if prev_null != curr_null || (!prev_null && arr.value(row - 1) != arr.value(row)) {
493+
boundaries[row] = true;
494+
}
495+
} else if arr.value(row - 1) != arr.value(row) {
496+
boundaries[row] = true;
497+
}
498+
}
499+
}
500+
OutputType::Utf8 => {
501+
let arr = array.as_bytes::<GenericStringType<O>>();
502+
for row in 1..array.len() {
503+
if boundaries[row] {
504+
continue;
505+
}
506+
if has_nulls {
507+
let prev_null = array.is_null(row - 1);
508+
let curr_null = array.is_null(row);
509+
if prev_null != curr_null || (!prev_null && arr.value(row - 1) != arr.value(row)) {
510+
boundaries[row] = true;
511+
}
512+
} else if arr.value(row - 1) != arr.value(row) {
513+
boundaries[row] = true;
514+
}
515+
}
516+
}
517+
_ => unreachable!("View types should use `ArrowBytesViewMap`"),
518+
}
519+
}
429520
}
430521

431522
#[cfg(test)]

‎datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ use crate::aggregates::group_values::multi_group_by::{
2121
use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;
2222
use arrow::array::{Array, ArrayRef, AsArray, ByteView, GenericByteViewArray, make_view};
2323
use arrow::buffer::{Buffer, ScalarBuffer};
24+
use ahash::RandomState;
2425
use arrow::datatypes::ByteViewType;
2526
use datafusion_common::Result;
27+
use datafusion_common::hash_utils::{HashValue, combine_hashes};
2628
use itertools::izip;
2729
use std::marker::PhantomData;
2830
use std::mem::{replace, size_of};
@@ -577,6 +579,146 @@ impl<B: ByteViewType> GroupColumn for ByteViewGroupValueBuilder<B> {
577579
fn take_n(&mut self, n: usize) -> ArrayRef {
578580
self.take_n_inner(n)
579581
}
582+
583+
fn input_rows_equal(&self, array: &ArrayRef, row_a: usize, row_b: usize) -> bool {
584+
let a_null = array.is_null(row_a);
585+
let b_null = array.is_null(row_b);
586+
if a_null || b_null {
587+
return a_null && b_null;
588+
}
589+
// Use the raw bytes for comparison (works for both StringView and BinaryView)
590+
// SAFETY: we checked for nulls above
591+
let arr = array.as_byte_view::<B>();
592+
let a_len = arr.views()[row_a] as u32;
593+
let b_len = arr.views()[row_b] as u32;
594+
if a_len != b_len {
595+
return false;
596+
}
597+
if a_len <= 12 {
598+
// Inlined: compare the view payloads directly (first 4 bytes of value are in high bits)
599+
arr.views()[row_a] == arr.views()[row_b]
600+
} else {
601+
// Not inlined: compare prefixes first, then full data
602+
let a_view = ByteView::from(arr.views()[row_a]);
603+
let b_view = ByteView::from(arr.views()[row_b]);
604+
if a_view.prefix != b_view.prefix {
605+
return false;
606+
}
607+
let bufs = arr.data_buffers();
608+
let a_bytes = unsafe {
609+
bufs.get_unchecked(a_view.buffer_index as usize)
610+
.get_unchecked(a_view.offset as usize..(a_view.offset as usize + a_len as usize))
611+
};
612+
let b_bytes = unsafe {
613+
bufs.get_unchecked(b_view.buffer_index as usize)
614+
.get_unchecked(b_view.offset as usize..(b_view.offset as usize + b_len as usize))
615+
};
616+
a_bytes == b_bytes
617+
}
618+
}
619+
620+
fn hash_input_row(
621+
&self,
622+
array: &ArrayRef,
623+
row: usize,
624+
random_state: &RandomState,
625+
rehash: bool,
626+
current_hash: u64,
627+
) -> u64 {
628+
if array.is_null(row) {
629+
return current_hash;
630+
}
631+
// Get raw bytes for hashing (matches create_hashes byte-view path)
632+
let arr = array.as_byte_view::<B>();
633+
let view_len = arr.views()[row] as u32;
634+
let bytes: &[u8] = if view_len <= 12 {
635+
// Inlined view
636+
let view = arr.views()[row];
637+
unsafe {
638+
std::slice::from_raw_parts(
639+
((&view) as *const u128 as *const u8).add(4),
640+
view_len as usize,
641+
)
642+
}
643+
} else {
644+
let view = ByteView::from(arr.views()[row]);
645+
let bufs = arr.data_buffers();
646+
unsafe {
647+
bufs.get_unchecked(view.buffer_index as usize)
648+
.get_unchecked(view.offset as usize..(view.offset as usize + view_len as usize))
649+
}
650+
};
651+
let h = bytes.hash_one(random_state);
652+
if rehash {
653+
combine_hashes(h, current_hash)
654+
} else {
655+
h
656+
}
657+
}
658+
659+
fn compute_boundaries(&self, array: &ArrayRef, boundaries: &mut [bool]) {
660+
let arr = array.as_byte_view::<B>();
661+
let views = arr.views();
662+
let has_nulls = array.null_count() > 0;
663+
let bufs = arr.data_buffers();
664+
665+
for row in 1..array.len() {
666+
if boundaries[row] {
667+
continue;
668+
}
669+
670+
if has_nulls {
671+
let prev_null = array.is_null(row - 1);
672+
let curr_null = array.is_null(row);
673+
if prev_null != curr_null {
674+
boundaries[row] = true;
675+
continue;
676+
}
677+
if prev_null {
678+
// both null => same group, not a boundary
679+
continue;
680+
}
681+
}
682+
683+
// Compare the two views
684+
let prev_view = views[row - 1];
685+
let curr_view = views[row];
686+
687+
let prev_len = prev_view as u32;
688+
let curr_len = curr_view as u32;
689+
690+
if prev_len != curr_len {
691+
boundaries[row] = true;
692+
continue;
693+
}
694+
695+
if prev_len <= 12 {
696+
// Both inlined: compare the full u128 views
697+
if prev_view != curr_view {
698+
boundaries[row] = true;
699+
}
700+
} else {
701+
// Both non-inlined: compare prefixes, then full data if needed
702+
let pv = ByteView::from(prev_view);
703+
let cv = ByteView::from(curr_view);
704+
if pv.prefix != cv.prefix {
705+
boundaries[row] = true;
706+
} else {
707+
let prev_bytes = unsafe {
708+
bufs.get_unchecked(pv.buffer_index as usize)
709+
.get_unchecked(pv.offset as usize..(pv.offset as usize + prev_len as usize))
710+
};
711+
let curr_bytes = unsafe {
712+
bufs.get_unchecked(cv.buffer_index as usize)
713+
.get_unchecked(cv.offset as usize..(cv.offset as usize + curr_len as usize))
714+
};
715+
if prev_bytes != curr_bytes {
716+
boundaries[row] = true;
717+
}
718+
}
719+
}
720+
}
721+
}
580722
}
581723

582724
#[cfg(test)]

0 commit comments

Comments
 (0)