use arrow::array::types::{IntervalDayTime, IntervalMonthDayNano};
use arrow::array::{
Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, AsArray, LargeStringArray,
PrimitiveArray, StringArray, StringArrayType, StringViewArray, downcast_primitive,
};
use arrow::buffer::ScalarBuffer;
use arrow::datatypes::{DataType, i256};
use datafusion_common::Result;
use datafusion_common::exec_datafusion_err;
use datafusion_common::float_semantics::compare_floats;
use half::f16;
use std::cmp::Ordering;
use std::fmt::Write as _;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
pub trait Comparable<Rhs: ?Sized = Self> {
fn comp(&self, other: &Rhs) -> Ordering;
}
impl Comparable<String> for str {
fn comp(&self, other: &String) -> Ordering {
self.cmp(other.as_str())
}
}
impl Comparable for String {
fn comp(&self, other: &Self) -> Ordering {
self.cmp(other)
}
}
impl Comparable for Option<String> {
fn comp(&self, other: &Self) -> Ordering {
self.cmp(other)
}
}
pub trait ValueType: Comparable + Clone + Debug + Default {}
impl<T> ValueType for T where T: Comparable + Clone + Debug + Default {}
const VACANT_MAP_IDX: usize = usize::MAX;
struct HeapItem<VAL: ValueType> {
val: VAL,
map_idx: usize,
}
impl<VAL: ValueType> HeapItem<VAL> {
fn is_vacant(&self) -> bool {
self.map_idx == VACANT_MAP_IDX
}
fn is_occupied(&self) -> bool {
self.map_idx != VACANT_MAP_IDX
}
fn map_idx(&self) -> Option<usize> {
self.is_occupied().then_some(self.map_idx)
}
fn take(&mut self) -> Option<Self> {
self.is_occupied().then(|| std::mem::take(self))
}
fn as_ref(&self) -> &Self {
debug_assert!(self.is_occupied());
self
}
fn as_mut(&mut self) -> &mut Self {
debug_assert!(self.is_occupied());
self
}
}
impl<VAL: ValueType> Default for HeapItem<VAL> {
fn default() -> Self {
Self {
val: VAL::default(),
map_idx: VACANT_MAP_IDX,
}
}
}
struct TopKHeap<VAL: ValueType> {
desc: bool,
len: usize,
capacity: usize,
heap: Vec<HeapItem<VAL>>,
}
pub trait ArrowHeap {
fn value_type(&self) -> &DataType;
fn set_batch(&mut self, vals: ArrayRef);
fn is_worse(&self, idx: usize) -> bool;
fn worst_map_idx(&self) -> usize;
fn insert(&mut self, row_idx: usize, map_idx: usize, map: &mut Vec<(usize, usize)>);
fn replace_if_better(
&mut self,
heap_idx: usize,
row_idx: usize,
map: &mut Vec<(usize, usize)>,
);
fn drain(&mut self) -> (ArrayRef, Vec<usize>);
}
pub struct PrimitiveHeap<VAL: ArrowPrimitiveType>
where
<VAL as ArrowPrimitiveType>::Native: Comparable,
{
batch: PrimitiveArray<VAL>,
heap: TopKHeap<VAL::Native>,
desc: bool,
value_type: DataType,
}
impl<VAL: ArrowPrimitiveType> PrimitiveHeap<VAL>
where
<VAL as ArrowPrimitiveType>::Native: Comparable,
{
pub fn new(limit: usize, desc: bool, value_type: DataType) -> Self {
Self {
batch: PrimitiveArray::<VAL>::new_null(0).with_data_type(value_type.clone()),
heap: TopKHeap::new(limit, desc),
desc,
value_type,
}
}
}
impl<VAL: ArrowPrimitiveType> ArrowHeap for PrimitiveHeap<VAL>
where
<VAL as ArrowPrimitiveType>::Native: Comparable,
{
fn value_type(&self) -> &DataType {
&self.value_type
}
fn set_batch(&mut self, vals: ArrayRef) {
self.batch = vals.as_primitive().clone();
}
fn is_worse(&self, row_idx: usize) -> bool {
if !self.heap.is_full() {
return false;
}
let new_val = self.batch.value(row_idx);
let worst_val = self.heap.worst_val().expect("Missing root");
(!self.desc && new_val > *worst_val) || (self.desc && new_val < *worst_val)
}
fn worst_map_idx(&self) -> usize {
self.heap.worst_map_idx()
}
fn insert(&mut self, row_idx: usize, map_idx: usize, map: &mut Vec<(usize, usize)>) {
let new_val = self.batch.value(row_idx);
self.heap.append_or_replace(&new_val, map_idx, map);
}
fn replace_if_better(
&mut self,
heap_idx: usize,
row_idx: usize,
map: &mut Vec<(usize, usize)>,
) {
let new_val = self.batch.value(row_idx);
self.heap.replace_if_better(&new_val, heap_idx, map);
}
fn drain(&mut self) -> (ArrayRef, Vec<usize>) {
let nulls = None;
let (vals, map_idxs) = self.heap.drain();
let arr = PrimitiveArray::<VAL>::new(ScalarBuffer::from(vals), nulls)
.with_data_type(self.value_type.clone());
(Arc::new(arr), map_idxs)
}
}
pub struct StringHeap<S: Array>
where
for<'a> &'a S: StringArrayType<'a>,
{
batch: S,
heap: TopKHeap<String>,
desc: bool,
}
impl<S> StringHeap<S>
where
S: Array + From<Vec<Option<String>>>,
for<'a> &'a S: StringArrayType<'a>,
{
pub fn new(limit: usize, desc: bool) -> Self {
let batch = S::from(Vec::new());
Self {
batch,
heap: TopKHeap::new(limit, desc),
desc,
}
}
}
impl<S> ArrowHeap for StringHeap<S>
where
S: Array + Clone + From<Vec<String>> + 'static,
for<'a> &'a S: StringArrayType<'a>,
{
fn value_type(&self) -> &DataType {
self.batch.data_type()
}
fn set_batch(&mut self, vals: ArrayRef) {
self.batch = vals
.as_any()
.downcast_ref::<S>()
.expect("Unsupported data type")
.clone();
}
fn is_worse(&self, row_idx: usize) -> bool {
if !self.heap.is_full() {
return false;
}
let new_val = (&self.batch).value(row_idx);
let worst_val = self.heap.worst_val().expect("Missing root").as_str();
(!self.desc && new_val > worst_val) || (self.desc && new_val < worst_val)
}
fn worst_map_idx(&self) -> usize {
self.heap.worst_map_idx()
}
fn insert(&mut self, row_idx: usize, map_idx: usize, map: &mut Vec<(usize, usize)>) {
let new_val = (&self.batch).value(row_idx);
self.heap.append_or_replace(new_val, map_idx, map);
}
fn replace_if_better(
&mut self,
heap_idx: usize,
row_idx: usize,
map: &mut Vec<(usize, usize)>,
) {
let new_val = (&self.batch).value(row_idx);
self.heap.replace_if_better(new_val, heap_idx, map);
}
fn drain(&mut self) -> (ArrayRef, Vec<usize>) {
let (vals, map_idxs) = self.heap.drain();
let vals = Arc::new(S::from(vals));
(vals, map_idxs)
}
}
impl<VAL: ValueType> TopKHeap<VAL> {
pub fn new(limit: usize, desc: bool) -> Self {
Self {
desc,
capacity: limit,
len: 0,
heap: (0..=limit).map(|_| HeapItem::default()).collect::<Vec<_>>(),
}
}
pub fn worst_val(&self) -> Option<&VAL> {
let root = self.heap.first()?;
root.is_occupied().then_some(&root.val)
}
pub fn worst_map_idx(&self) -> usize {
self.heap[0].map_idx().unwrap_or(0)
}
pub fn is_full(&self) -> bool {
self.len >= self.capacity
}
pub fn len(&self) -> usize {
self.len
}
pub fn append_or_replace<Q>(
&mut self,
new_val: &Q,
map_idx: usize,
map: &mut Vec<(usize, usize)>,
) where
Q: ToOwned<Owned = VAL> + ?Sized,
{
if self.is_full() {
self.replace_root(new_val, map_idx, map);
} else {
self.append(new_val, map_idx, map);
}
}
fn append<Q>(&mut self, new_val: &Q, map_idx: usize, mapper: &mut Vec<(usize, usize)>)
where
Q: ToOwned<Owned = VAL> + ?Sized,
{
let hi = &mut self.heap[self.len];
debug_assert!(hi.is_vacant());
hi.map_idx = map_idx;
new_val.clone_into(&mut hi.val);
self.heapify_up(self.len, mapper);
self.len += 1;
}
fn pop(&mut self, map: &mut Vec<(usize, usize)>) -> Option<HeapItem<VAL>> {
if self.len() == 0 {
return None;
}
if self.len() == 1 {
self.len = 0;
return self.heap[0].take();
}
self.swap(0, self.len - 1, map);
let former_root = self.heap[self.len - 1].take();
self.len -= 1;
self.heapify_down(0, map);
former_root
}
pub fn drain(&mut self) -> (Vec<VAL>, Vec<usize>) {
let mut map = Vec::new();
let mut vals = Vec::with_capacity(self.len);
let mut map_idxs = Vec::with_capacity(self.len);
while let Some(worst_hi) = self.pop(&mut map) {
vals.push(worst_hi.val);
map_idxs.push(worst_hi.map_idx);
map.clear();
}
vals.reverse();
map_idxs.reverse();
(vals, map_idxs)
}
fn replace_root<Q>(
&mut self,
new_val: &Q,
map_idx: usize,
mapper: &mut Vec<(usize, usize)>,
) where
Q: ToOwned<Owned = VAL> + ?Sized,
{
let hi = self.heap[0].as_mut();
new_val.clone_into(&mut hi.val);
hi.map_idx = map_idx;
self.heapify_down(0, mapper);
}
pub fn replace_if_better<Q>(
&mut self,
new_val: &Q,
heap_idx: usize,
mapper: &mut Vec<(usize, usize)>,
) where
Q: ToOwned<Owned = VAL> + Comparable<VAL> + ?Sized,
{
let existing = self.heap[heap_idx].as_mut();
if (!self.desc && new_val.comp(&existing.val) != Ordering::Less)
|| (self.desc && new_val.comp(&existing.val) != Ordering::Greater)
{
return;
}
new_val.clone_into(&mut existing.val);
self.heapify_down(heap_idx, mapper);
}
fn heapify_up(&mut self, mut idx: usize, mapper: &mut Vec<(usize, usize)>) {
let desc = self.desc;
while idx != 0 {
let parent_idx = (idx - 1) / 2;
let node = self.heap[idx].as_ref();
let parent = self.heap[parent_idx].as_ref();
if (!desc && node.val.comp(&parent.val) != Ordering::Greater)
|| (desc && node.val.comp(&parent.val) != Ordering::Less)
{
return;
}
self.swap(idx, parent_idx, mapper);
idx = parent_idx;
}
}
#[inline]
fn swap(&mut self, a_idx: usize, b_idx: usize, mapper: &mut Vec<(usize, usize)>) {
self.heap.swap(a_idx, b_idx);
let b_hi = self.heap[b_idx].as_ref();
let a_hi = self.heap[a_idx].as_ref();
mapper.extend([(b_hi.map_idx, b_idx), (a_hi.map_idx, a_idx)]);
}
fn heapify_down(&mut self, mut node_idx: usize, mapper: &mut Vec<(usize, usize)>) {
let desc = self.desc;
loop {
let left_child = node_idx * 2 + 1;
let entry = self.heap[node_idx].as_ref();
let mut best_idx = node_idx;
let mut best_val = &entry.val;
for child_idx in left_child..=left_child + 1 {
if let Some(child) = self.heap.get(child_idx)
&& child.is_occupied()
&& ((!desc && child.val.comp(best_val) == Ordering::Greater)
|| (desc && child.val.comp(best_val) == Ordering::Less))
{
best_val = &child.val;
best_idx = child_idx;
}
}
if best_val.comp(&entry.val) == Ordering::Equal {
break;
}
self.swap(best_idx, node_idx, mapper);
node_idx = best_idx;
}
}
fn _tree_print(&self, idx: usize, prefix: &str, is_tail: bool, output: &mut String) {
if let Some(hi) = self.heap.get(idx)
&& hi.is_occupied()
{
let connector = if idx != 0 {
if is_tail { "└── " } else { "├── " }
} else {
""
};
writeln!(
output,
"{}{}val={:?} idx={}, bucket={}",
prefix, connector, hi.val, idx, hi.map_idx
)
.ok();
let new_prefix = if is_tail { "" } else { "│ " };
let child_prefix = format!("{prefix}{new_prefix}");
let left_idx = idx * 2 + 1;
let right_idx = idx * 2 + 2;
let left_exists = left_idx < self.len;
let right_exists = right_idx < self.len;
if left_exists {
self._tree_print(left_idx, &child_prefix, !right_exists, output);
}
if right_exists {
self._tree_print(right_idx, &child_prefix, true, output);
}
}
}
}
impl<VAL: ValueType> Display for TopKHeap<VAL> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut output = String::new();
if !self.heap.is_empty() {
self._tree_print(0, "", true, &mut output);
}
write!(f, "{output}")
}
}
impl<VAL: ValueType> Debug for HeapItem<VAL> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("bucket=")?;
Debug::fmt(&self.map_idx, f)?;
f.write_str(" val=")?;
Debug::fmt(&self.val, f)?;
f.write_str("\n")?;
Ok(())
}
}
impl<VAL: ValueType> Eq for HeapItem<VAL> {}
impl<VAL: ValueType> PartialEq<Self> for HeapItem<VAL> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl<VAL: ValueType> PartialOrd<Self> for HeapItem<VAL> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<VAL: ValueType> Ord for HeapItem<VAL> {
fn cmp(&self, other: &Self) -> Ordering {
let res = self.val.comp(&other.val);
if res != Ordering::Equal {
return res;
}
self.map_idx.cmp(&other.map_idx)
}
}
macro_rules! compare_float {
($($t:ty),+) => {
$(impl Comparable for Option<$t> {
fn comp(&self, other: &Self) -> Ordering {
match (self, other) {
(Some(me), Some(other)) => compare_floats(*me, *other),
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(None, None) => Ordering::Equal,
}
}
})+
$(impl Comparable for $t {
fn comp(&self, other: &Self) -> Ordering {
compare_floats(*self, *other)
}
})+
};
}
macro_rules! compare_integer {
($($t:ty),+) => {
$(impl Comparable for Option<$t> {
fn comp(&self, other: &Self) -> Ordering {
self.cmp(other)
}
})+
$(impl Comparable for $t {
fn comp(&self, other: &Self) -> Ordering {
self.cmp(other)
}
})+
};
}
compare_integer!(i8, i16, i32, i64, i128, i256);
compare_integer!(u8, u16, u32, u64);
compare_integer!(IntervalDayTime, IntervalMonthDayNano);
compare_float!(f16, f32, f64);
pub fn is_supported_heap_type(vt: &DataType) -> bool {
vt.is_primitive()
|| matches!(
vt,
DataType::Utf8 | DataType::Utf8View | DataType::LargeUtf8
)
}
pub fn new_heap(
limit: usize,
desc: bool,
vt: DataType,
) -> Result<Box<dyn ArrowHeap + Send>> {
macro_rules! downcast_helper {
($vt:ty, $d:ident) => {
return Ok(Box::new(PrimitiveHeap::<$vt>::new(limit, desc, vt)))
};
}
downcast_primitive! {
vt => (downcast_helper, vt),
DataType::Utf8 => return Ok(Box::new(StringHeap::<StringArray>::new(limit, desc))),
DataType::LargeUtf8 => return Ok(Box::new(StringHeap::<LargeStringArray>::new(limit, desc))),
DataType::Utf8View => return Ok(Box::new(StringHeap::<StringViewArray>::new(limit, desc))),
_ => {}
}
Err(exec_datafusion_err!(
"Unsupported TopK aggregate value type: {vt:?}"
))
}
#[cfg(test)]
mod tests {
use insta::assert_snapshot;
use super::*;
#[test]
fn should_append() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(10, false);
heap.append_or_replace(&1, 1, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @"val=1 idx=0, bucket=1");
Ok(())
}
#[test]
fn should_heapify_up() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(10, false);
heap.append_or_replace(&1, 1, &mut map);
assert_eq!(map, vec![]);
heap.append_or_replace(&2, 2, &mut map);
assert_eq!(map, vec![(2, 0), (1, 1)]);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=2 idx=0, bucket=2
└── val=1 idx=1, bucket=1
");
Ok(())
}
#[test]
fn should_heapify_down() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(3, false);
heap.append_or_replace(&1, 1, &mut map);
heap.append_or_replace(&2, 2, &mut map);
heap.append_or_replace(&3, 3, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=3 idx=0, bucket=3
├── val=1 idx=1, bucket=1
└── val=2 idx=2, bucket=2
");
let mut map = vec![];
heap.append_or_replace(&0, 0, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=2 idx=0, bucket=2
├── val=1 idx=1, bucket=1
└── val=0 idx=2, bucket=0
");
assert_eq!(map, vec![(2, 0), (0, 2)]);
Ok(())
}
#[test]
fn should_replace() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(4, false);
heap.append_or_replace(&1, 1, &mut map);
heap.append_or_replace(&2, 2, &mut map);
heap.append_or_replace(&3, 3, &mut map);
heap.append_or_replace(&4, 4, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=4 idx=0, bucket=4
├── val=3 idx=1, bucket=3
│ └── val=1 idx=3, bucket=1
└── val=2 idx=2, bucket=2
");
let mut map = vec![];
heap.replace_if_better(&0, 1, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=4 idx=0, bucket=4
├── val=1 idx=1, bucket=1
│ └── val=0 idx=3, bucket=3
└── val=2 idx=2, bucket=2
");
assert_eq!(map, vec![(1, 1), (3, 3)]);
Ok(())
}
#[test]
fn should_find_worst() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(10, false);
heap.append_or_replace(&1, 1, &mut map);
heap.append_or_replace(&2, 2, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=2 idx=0, bucket=2
└── val=1 idx=1, bucket=1
");
assert_eq!(heap.worst_val(), Some(&2));
assert_eq!(heap.worst_map_idx(), 2);
Ok(())
}
#[test]
fn should_drain() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::new(10, false);
heap.append_or_replace(&1, 1, &mut map);
heap.append_or_replace(&2, 2, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r"
val=2 idx=0, bucket=2
└── val=1 idx=1, bucket=1
");
let (vals, map_idxs) = heap.drain();
assert_eq!(vals, vec![1, 2]);
assert_eq!(map_idxs, vec![1, 2]);
assert_eq!(heap.len(), 0);
Ok(())
}
#[test]
fn should_append_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(10, false);
heap.append_or_replace("A", 1, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"val="A" idx=0, bucket=1"#);
Ok(())
}
#[test]
fn should_heapify_up_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(10, false);
heap.append_or_replace("A", 1, &mut map);
assert_eq!(map, vec![]);
heap.append_or_replace("B", 2, &mut map);
assert_eq!(map, vec![(2, 0), (1, 1)]);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="B" idx=0, bucket=2
└── val="A" idx=1, bucket=1
"#);
Ok(())
}
#[test]
fn should_heapify_down_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(3, false);
heap.append_or_replace("B", 1, &mut map);
heap.append_or_replace("C", 2, &mut map);
heap.append_or_replace("D", 3, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="D" idx=0, bucket=3
├── val="B" idx=1, bucket=1
└── val="C" idx=2, bucket=2
"#);
let mut map = vec![];
heap.append_or_replace("A", 0, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="C" idx=0, bucket=2
├── val="B" idx=1, bucket=1
└── val="A" idx=2, bucket=0
"#);
assert_eq!(map, vec![(2, 0), (0, 2)]);
Ok(())
}
#[test]
fn should_replace_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(4, false);
heap.append_or_replace("B", 1, &mut map);
heap.append_or_replace("C", 2, &mut map);
heap.append_or_replace("D", 3, &mut map);
heap.append_or_replace("E", 4, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="E" idx=0, bucket=4
├── val="D" idx=1, bucket=3
│ └── val="B" idx=3, bucket=1
└── val="C" idx=2, bucket=2
"#);
let mut map = vec![];
heap.replace_if_better("A", 1, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="E" idx=0, bucket=4
├── val="B" idx=1, bucket=1
│ └── val="A" idx=3, bucket=3
└── val="C" idx=2, bucket=2
"#);
assert_eq!(map, vec![(1, 1), (3, 3)]);
Ok(())
}
#[test]
fn should_find_worst_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(10, false);
heap.append_or_replace("A", 1, &mut map);
heap.append_or_replace("B", 2, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="B" idx=0, bucket=2
└── val="A" idx=1, bucket=1
"#);
assert_eq!(heap.worst_val().map(String::as_str), Some("B"));
assert_eq!(heap.worst_map_idx(), 2);
Ok(())
}
#[test]
fn should_drain_string() -> Result<()> {
let mut map = vec![];
let mut heap = TopKHeap::<String>::new(10, false);
heap.append_or_replace("A", 1, &mut map);
heap.append_or_replace("B", 2, &mut map);
let actual = heap.to_string();
assert_snapshot!(actual, @r#"
val="B" idx=0, bucket=2
└── val="A" idx=1, bucket=1
"#);
let (vals, map_idxs) = heap.drain();
assert_eq!(vals, vec!["A", "B"]);
assert_eq!(map_idxs, vec![1, 2]);
assert_eq!(heap.len(), 0);
Ok(())
}
}