use std::{cmp::Ordering, fmt, mem, ops::Range, sync::Arc};
#[cfg(feature = "decimal")]
use rust_decimal::{Decimal, prelude::ToPrimitive as _};
use super::{
Projection, ViewSpec,
display::{AggregateValues, GroupData},
};
use crate::{
model::{CellValue, ColumnSet, GridColumns, GridRow, OwnedValue},
store::{RowId, StoreSnapshot},
};
pub trait AggregateFn<R: GridRow>: Send + Sync + 'static {
fn aggregate(&self, column: R::Column, rows: &mut dyn Iterator<Item = &R>) -> OwnedValue;
fn combine(&self, _parts: &[&OwnedValue]) -> Option<OwnedValue> {
None
}
fn depends_on(&self, column: R::Column) -> ColumnSet {
let mut columns = ColumnSet::EMPTY;
columns.insert(column.ix());
columns
}
fn name(&self) -> &str {
""
}
}
#[non_exhaustive]
pub enum Aggregate<R: GridRow> {
Sum,
Avg,
Min,
Max,
Count,
Custom(Arc<dyn AggregateFn<R>>),
}
impl<R: GridRow> Clone for Aggregate<R> {
fn clone(&self) -> Self {
match self {
Self::Sum => Self::Sum,
Self::Avg => Self::Avg,
Self::Min => Self::Min,
Self::Max => Self::Max,
Self::Count => Self::Count,
Self::Custom(function) => Self::Custom(function.clone()),
}
}
}
impl<R: GridRow> fmt::Debug for Aggregate<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Sum => f.write_str("Sum"),
Self::Avg => f.write_str("Avg"),
Self::Min => f.write_str("Min"),
Self::Max => f.write_str("Max"),
Self::Count => f.write_str("Count"),
Self::Custom(function) => f.debug_tuple("Custom").field(&function.name()).finish(),
}
}
}
impl<R: GridRow> Aggregate<R> {
pub(super) fn depends_on(&self, column: R::Column) -> ColumnSet {
match self {
Self::Custom(function) => function.depends_on(column),
Self::Sum | Self::Avg | Self::Min | Self::Max | Self::Count => {
let mut columns = ColumnSet::EMPTY;
columns.insert(column.ix());
columns
}
}
}
}
impl<R: GridRow> ViewSpec<R> {
pub fn with_aggregates(
mut self,
aggregates: impl IntoIterator<Item = (R::Column, Aggregate<R>)>,
) -> Self {
let mut unique: Vec<(R::Column, Aggregate<R>)> = Vec::new();
for (column, aggregate) in aggregates {
match unique.iter_mut().find(|(kept, _)| *kept == column) {
Some(kept) => kept.1 = aggregate,
None => unique.push((column, aggregate)),
}
}
self.aggregates = (!unique.is_empty()).then(|| unique.into());
self
}
pub fn aggregates(&self) -> &[(R::Column, Aggregate<R>)] {
self.aggregates.as_deref().unwrap_or_default()
}
pub fn aggregate_columns(&self) -> ColumnSet {
let mut columns = ColumnSet::EMPTY;
for (column, aggregate) in self.aggregates() {
columns.union_with(aggregate.depends_on(*column));
}
columns
}
}
pub(super) struct ViewAggregates<'s, R: GridRow> {
columns: ColumnSet,
list: Vec<(R::Column, &'s Aggregate<R>)>,
built_in: Vec<usize>,
custom: Vec<(usize, &'s dyn AggregateFn<R>)>,
}
impl<'s, R: GridRow> ViewAggregates<'s, R> {
pub(super) fn new(spec: &'s ViewSpec<R>) -> Self {
let mut list: Vec<(R::Column, &Aggregate<R>)> = spec
.aggregates()
.iter()
.map(|(column, aggregate)| (*column, aggregate))
.collect();
list.sort_by_key(|(column, _)| column.ix());
let mut columns = ColumnSet::EMPTY;
let mut built_in = Vec::new();
let mut custom = Vec::new();
for (ix, (column, aggregate)) in list.iter().enumerate() {
columns.insert(column.ix());
match aggregate {
Aggregate::Custom(function) => custom.push((ix, &**function)),
Aggregate::Sum
| Aggregate::Avg
| Aggregate::Min
| Aggregate::Max
| Aggregate::Count => built_in.push(ix),
}
}
Self {
columns,
list,
built_in,
custom,
}
}
pub(super) fn columns(&self) -> ColumnSet {
self.columns
}
pub(super) fn is_empty(&self) -> bool {
self.list.is_empty()
}
fn start<'a>(&self) -> impl Iterator<Item = Partial<'a>> + '_ {
self.list
.iter()
.map(|(_, aggregate)| Partial::start(aggregate))
}
#[inline]
fn add<'a>(&self, partials: &mut [Partial<'a>], row: &'a R) {
for &ix in &self.built_in {
partials[ix].add(row.value(self.list[ix].0));
}
}
fn merge<'a>(&self, partials: &mut [Partial<'a>], part: &[Partial<'a>]) {
for &ix in &self.built_in {
partials[ix].merge(&part[ix]);
}
}
fn finish(&self, partials: &[Partial<'_>], values: &mut [OwnedValue]) {
for &ix in &self.built_in {
values[ix] = partials[ix].finish();
}
}
}
#[derive(Clone, Copy, Debug)]
enum Partial<'a> {
Sum(Numbers),
Avg(Numbers),
Min(CellValue<'a>),
Max(CellValue<'a>),
Count(u64),
Custom,
}
#[derive(Clone, Copy, Debug, Default)]
struct Numbers {
int: i128,
float: f64,
has_float: bool,
count: u64,
#[cfg(feature = "decimal")]
decimal: Decimals,
}
impl Numbers {
fn add(&mut self, value: CellValue<'_>) {
match value {
CellValue::Int(value) => {
self.int += i128::from(value);
self.count += 1;
}
CellValue::Float(value) => {
self.float += value;
self.has_float = true;
self.count += 1;
}
#[cfg(feature = "decimal")]
CellValue::Decimal(value) => {
self.decimal.add(value);
self.count += 1;
}
_ => {}
}
}
fn merge(&mut self, other: &Self) {
self.int += other.int;
self.float += other.float;
self.has_float |= other.has_float;
self.count += other.count;
#[cfg(feature = "decimal")]
self.decimal.merge(&other.decimal);
}
fn sum(&self) -> OwnedValue {
if self.count == 0 {
return OwnedValue::Empty;
}
#[cfg(feature = "decimal")]
if self.decimal.has_values && !self.has_float {
return self
.decimal
.exact_sum(self.int)
.map_or_else(OwnedValue::Float, OwnedValue::Decimal);
}
if self.has_float {
OwnedValue::Float(self.float_sum())
} else {
i64::try_from(self.int).map_or(OwnedValue::Float(self.int as f64), OwnedValue::Int)
}
}
fn mean(&self) -> OwnedValue {
if self.count == 0 {
return OwnedValue::Empty;
}
#[cfg(feature = "decimal")]
if self.decimal.has_values && !self.has_float {
return self
.decimal
.mean(self.int, self.count)
.map_or_else(OwnedValue::Float, OwnedValue::Decimal);
}
OwnedValue::Float(self.float_sum() / self.count as f64)
}
fn float_sum(&self) -> f64 {
let sum = self.float + self.int as f64;
#[cfg(feature = "decimal")]
if self.decimal.has_values {
return sum + self.decimal.sum.to_f64();
}
sum
}
}
#[cfg(feature = "decimal")]
#[derive(Clone, Copy, Debug, Default)]
struct Decimals {
sum: DecimalSum,
scale: u32,
has_values: bool,
}
#[cfg(feature = "decimal")]
#[derive(Clone, Copy, Debug)]
enum DecimalSum {
Exact(Decimal),
Overflow(f64),
}
#[cfg(feature = "decimal")]
impl Default for DecimalSum {
fn default() -> Self {
Self::Exact(Decimal::ZERO)
}
}
#[cfg(feature = "decimal")]
impl DecimalSum {
fn plus(self, other: Self) -> Self {
match (self, other) {
(Self::Exact(a), Self::Exact(b)) => a
.checked_add(b)
.map_or_else(|| Self::Overflow(as_f64(a) + as_f64(b)), Self::Exact),
(a, b) => Self::Overflow(a.to_f64() + b.to_f64()),
}
}
fn to_f64(self) -> f64 {
match self {
Self::Exact(sum) => as_f64(sum),
Self::Overflow(sum) => sum,
}
}
}
#[cfg(feature = "decimal")]
fn as_f64(value: Decimal) -> f64 {
value.to_f64().unwrap_or_default()
}
#[cfg(feature = "decimal")]
impl Decimals {
fn add(&mut self, value: Decimal) {
self.sum = self.sum.plus(DecimalSum::Exact(value));
self.scale = self.scale.max(value.scale());
self.has_values = true;
}
fn merge(&mut self, other: &Self) {
if other.has_values {
self.sum = self.sum.plus(other.sum);
self.scale = self.scale.max(other.scale);
self.has_values = true;
}
}
fn exact_sum(&self, int: i128) -> Result<Decimal, f64> {
let int = Decimal::try_from_i128_with_scale(int, 0)
.map_or_else(|_| DecimalSum::Overflow(int as f64), DecimalSum::Exact);
match self.sum.plus(int) {
DecimalSum::Exact(sum) => Ok(sum),
DecimalSum::Overflow(sum) => Err(sum),
}
}
fn mean(&self, int: i128, count: u64) -> Result<Decimal, f64> {
let sum = self.exact_sum(int).map_err(|sum| sum / count as f64)?;
sum.checked_div(Decimal::from(count))
.map(|mean| mean.round_dp(self.scale + 2))
.ok_or_else(|| as_f64(sum) / count as f64)
}
}
impl<'a> Partial<'a> {
fn start<R: GridRow>(aggregate: &Aggregate<R>) -> Self {
match aggregate {
Aggregate::Sum => Self::Sum(Numbers::default()),
Aggregate::Avg => Self::Avg(Numbers::default()),
Aggregate::Min => Self::Min(CellValue::Empty),
Aggregate::Max => Self::Max(CellValue::Empty),
Aggregate::Count => Self::Count(0),
Aggregate::Custom(_) => Self::Custom,
}
}
#[inline]
fn add(&mut self, value: CellValue<'a>) {
match self {
Self::Sum(numbers) | Self::Avg(numbers) => numbers.add(value),
Self::Min(min) => keep_extreme(min, value, Ordering::Less),
Self::Max(max) => keep_extreme(max, value, Ordering::Greater),
Self::Count(count) => *count += u64::from(!matches!(value, CellValue::Empty)),
Self::Custom => {}
}
}
fn merge(&mut self, other: &Self) {
match (self, other) {
(Self::Sum(numbers), Self::Sum(other)) | (Self::Avg(numbers), Self::Avg(other)) => {
numbers.merge(other)
}
(Self::Min(min), Self::Min(other)) => keep_extreme(min, *other, Ordering::Less),
(Self::Max(max), Self::Max(other)) => keep_extreme(max, *other, Ordering::Greater),
(Self::Count(count), Self::Count(other)) => *count += other,
_ => {}
}
}
fn finish(&self) -> OwnedValue {
match self {
Self::Sum(numbers) => numbers.sum(),
Self::Avg(numbers) => numbers.mean(),
Self::Min(value) | Self::Max(value) => OwnedValue::from_cell(*value),
Self::Count(count) => OwnedValue::Int(i64::try_from(*count).unwrap_or(i64::MAX)),
Self::Custom => OwnedValue::Empty,
}
}
}
#[inline]
fn keep_extreme<'a>(extreme: &mut CellValue<'a>, value: CellValue<'a>, side: Ordering) {
if !matches!(value, CellValue::Empty)
&& (matches!(extreme, CellValue::Empty) || value.cmp_total(extreme) == side)
{
*extreme = value;
}
}
#[derive(Clone, Debug)]
pub(super) struct KeyedGroup {
pub(super) depth: u16,
pub(super) end: u32,
pub(super) rows: Range<u32>,
}
pub(super) struct GroupValues {
pub(super) values: Vec<OwnedValue>,
pub(super) live: Vec<u32>,
pub(super) totals: Vec<OwnedValue>,
pub(super) total_live: usize,
}
pub(super) fn aggregate_groups<'r, 'a, R: GridRow, T>(
aggregates: &ViewAggregates<'_, R>,
groups: &[KeyedGroup],
levels: u16,
rows: &'r [T],
row: impl Fn(&'r T) -> Option<&'a R> + Copy,
) -> GroupValues {
let width = aggregates.list.len();
let is_leaf = |group: &KeyedGroup| usize::from(group.depth) + 1 == usize::from(levels);
let rows_of = |span: Range<usize>| {
groups[span]
.iter()
.filter(move |group| is_leaf(group))
.flat_map(move |group| {
rows[group.rows.start as usize..group.rows.end as usize]
.iter()
.filter_map(row)
})
};
let mut partials: Vec<Partial<'a>> = Vec::with_capacity(groups.len() * width);
for _ in groups {
partials.extend(aggregates.start());
}
let mut values = vec![OwnedValue::Empty; groups.len() * width];
let mut live = vec![0u32; groups.len()];
for (ix, group) in groups.iter().enumerate().rev() {
let run = ix * width..(ix + 1) * width;
if is_leaf(group) {
let partial = &mut partials[run.clone()];
for row in rows_of(ix..ix + 1) {
live[ix] += 1;
aggregates.add(partial, row);
}
for &(place, function) in &aggregates.custom {
let column = aggregates.list[place].0;
values[ix * width + place] = function.aggregate(column, &mut rows_of(ix..ix + 1));
}
} else {
let span = ix + 1..group.end as usize;
let (head, tail) = partials.split_at_mut((ix + 1) * width);
let mut child = span.start;
while child < span.end {
live[ix] += live[child];
let part = (child - span.start) * width;
aggregates.merge(&mut head[run.clone()], &tail[part..part + width]);
child = groups[child].end as usize;
}
for &(place, function) in &aggregates.custom {
let column = aggregates.list[place].0;
let (head, tail) = values.split_at_mut((ix + 1) * width);
let parts = children(groups, span.clone())
.map(|child| &tail[(child - span.start) * width + place])
.collect::<Vec<_>>();
head[ix * width + place] = function
.combine(&parts)
.unwrap_or_else(|| function.aggregate(column, &mut rows_of(span.clone())));
}
}
aggregates.finish(&partials[run.clone()], &mut values[run]);
}
let mut total: Vec<Partial<'a>> = aggregates.start().collect();
let mut total_live = 0;
for top in children(groups, 0..groups.len()) {
total_live += live[top] as usize;
aggregates.merge(&mut total, &partials[top * width..(top + 1) * width]);
}
let mut totals = vec![OwnedValue::Empty; width];
aggregates.finish(&total, &mut totals);
for &(place, function) in &aggregates.custom {
let column = aggregates.list[place].0;
let parts = children(groups, 0..groups.len())
.map(|top| &values[top * width + place])
.collect::<Vec<_>>();
totals[place] = (!parts.is_empty())
.then(|| function.combine(&parts))
.flatten()
.unwrap_or_else(|| function.aggregate(column, &mut rows_of(0..groups.len())));
}
GroupValues {
values,
live,
totals,
total_live,
}
}
fn children(groups: &[KeyedGroup], span: Range<usize>) -> impl Iterator<Item = usize> + '_ {
let mut next = span.start;
std::iter::from_fn(move || {
let child = next;
(child < span.end).then(|| {
next = groups[child].end as usize;
child
})
})
}
pub(super) fn flat_totals<'a, R: GridRow>(
aggregates: &ViewAggregates<'_, R>,
rows: impl Iterator<Item = &'a R> + Clone,
) -> (Vec<OwnedValue>, usize) {
let mut partials: Vec<Partial<'a>> = aggregates.start().collect();
let mut count = 0;
for row in rows.clone() {
count += 1;
aggregates.add(&mut partials, row);
}
let mut totals = vec![OwnedValue::Empty; aggregates.list.len()];
aggregates.finish(&partials, &mut totals);
for &(place, function) in &aggregates.custom {
let column = aggregates.list[place].0;
totals[place] = function.aggregate(column, &mut rows.clone());
}
(totals, count)
}
pub(super) fn refresh<R: GridRow>(
previous: &Projection,
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
) -> Projection {
let aggregates = ViewAggregates::new(spec);
let row = |id: &RowId| snapshot.row(*id).map(|row| &**row);
let Some(data) = previous.group_data() else {
let (totals, live) = flat_totals(&aggregates, previous.rows().iter().filter_map(row));
return previous
.duplicate()
.with_store_version(snapshot.version())
.with_row_count(live)
.with_totals(AggregateValues::new(aggregates.columns(), totals));
};
let mut key_order: Vec<usize> = (0..data.groups.len()).collect();
key_order.sort_unstable_by_key(|&ix| data.groups[ix].key_ix);
let mut keyed: Vec<KeyedGroup> = key_order
.iter()
.map(|&ix| {
let group = &data.groups[ix];
KeyedGroup {
depth: group.depth(),
end: 0,
rows: group.rows.clone(),
}
})
.collect();
let mut open: Vec<usize> = Vec::new();
for ix in 0..keyed.len() {
while let Some(&top) = open.last()
&& keyed[top].depth >= keyed[ix].depth
{
keyed[top].end = ix as u32;
open.pop();
}
open.push(ix);
}
for top in open {
keyed[top].end = keyed.len() as u32;
}
let mut computed = aggregate_groups(&aggregates, &keyed, data.levels, previous.rows(), row);
let width = aggregates.columns().len();
let mut groups = data.groups.clone();
let mut values = vec![OwnedValue::Empty; groups.len() * width];
for (key_ix, &ix) in key_order.iter().enumerate() {
groups[ix].live = computed.live[key_ix];
for (value, computed) in values[ix * width..(ix + 1) * width]
.iter_mut()
.zip(&mut computed.values[key_ix * width..(key_ix + 1) * width])
{
*value = mem::replace(computed, OwnedValue::Empty);
}
}
let columns = aggregates.columns();
previous.with_group_values(
GroupData::new(data.levels, groups, AggregateValues::new(columns, values)),
AggregateValues::new(columns, computed.totals),
computed.total_live,
snapshot.version(),
)
}