use std::{array, cmp::Ordering, fmt, iter, mem, ops::Range, sync::Arc};
use super::{
AggregateRow, FadingIds, GroupInfo, GroupPath, Parallelism, Projection, Rebuild, SortDirection,
Structure, ViewSpec,
aggregate::{GroupValues, KeyedGroup, ViewAggregates, aggregate_groups},
directed,
display::{AggregateValues, Group, GroupData, GroupId},
sorted_rows,
};
use crate::{
model::{CellValue, ColumnId, ColumnIx, ColumnSet, GridColumns, GridRow, OwnedValue},
store::{RowId, StoreSnapshot},
};
type KeyFn<R> = Arc<dyn Fn(&R) -> CellValue<'_> + Send + Sync>;
pub struct GroupKey<R: GridRow> {
id: &'static str,
title: Arc<str>,
depends_on: ColumnSet,
key: KeyFn<R>,
}
impl<R: GridRow> Clone for GroupKey<R> {
fn clone(&self) -> Self {
Self {
id: self.id,
title: self.title.clone(),
depends_on: self.depends_on,
key: self.key.clone(),
}
}
}
impl<R: GridRow> fmt::Debug for GroupKey<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GroupKey")
.field("id", &self.id)
.field("title", &self.title)
.field("depends_on", &self.depends_on)
.finish_non_exhaustive()
}
}
impl<R: GridRow> GroupKey<R> {
pub fn new(
id: &'static str,
depends_on: ColumnSet,
key: impl Fn(&R) -> CellValue<'_> + Send + Sync + 'static,
) -> Self {
Self {
id,
title: id.into(),
depends_on,
key: Arc::new(key),
}
}
pub fn with_title(mut self, title: impl Into<Arc<str>>) -> Self {
self.title = title.into();
self
}
pub fn id(&self) -> &'static str {
self.id
}
pub fn title(&self) -> &str {
&self.title
}
pub fn depends_on(&self) -> ColumnSet {
self.depends_on
}
pub(crate) fn value<'r>(&self, row: &'r R) -> CellValue<'r> {
(self.key)(row)
}
}
enum GroupLevel<R: GridRow> {
Column(R::Column),
Key(GroupKey<R>),
}
impl<R: GridRow> Clone for GroupLevel<R> {
fn clone(&self) -> Self {
match self {
Self::Column(column) => Self::Column(*column),
Self::Key(key) => Self::Key(key.clone()),
}
}
}
pub struct GroupBy<R: GridRow> {
level: GroupLevel<R>,
direction: SortDirection,
}
impl<R: GridRow> Clone for GroupBy<R> {
fn clone(&self) -> Self {
Self {
level: self.level.clone(),
direction: self.direction,
}
}
}
impl<R: GridRow> fmt::Debug for GroupBy<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug = f.debug_struct("GroupBy");
match &self.level {
GroupLevel::Column(column) => debug.field("column", column),
GroupLevel::Key(key) => debug.field("key", key),
};
debug.field("direction", &self.direction).finish()
}
}
impl<R: GridRow> GroupBy<R> {
pub fn column(column: R::Column) -> Self {
Self {
level: GroupLevel::Column(column),
direction: SortDirection::Ascending,
}
}
pub fn key(key: GroupKey<R>) -> Self {
Self {
level: GroupLevel::Key(key),
direction: SortDirection::Ascending,
}
}
pub fn with_direction(mut self, direction: SortDirection) -> Self {
self.direction = direction;
self
}
pub fn direction(&self) -> SortDirection {
self.direction
}
pub fn as_column(&self) -> Option<R::Column> {
match &self.level {
GroupLevel::Column(column) => Some(*column),
GroupLevel::Key(_) => None,
}
}
pub fn as_key(&self) -> Option<&GroupKey<R>> {
match &self.level {
GroupLevel::Column(_) => None,
GroupLevel::Key(key) => Some(key),
}
}
pub fn id(&self) -> ColumnId {
match &self.level {
GroupLevel::Column(column) => column.column_id(),
GroupLevel::Key(key) => ColumnId::new_static(key.id),
}
}
pub(crate) fn value<'r>(&self, row: &'r R) -> CellValue<'r> {
match &self.level {
GroupLevel::Column(column) => row.value(*column),
GroupLevel::Key(key) => key.value(row),
}
}
}
pub(crate) struct GroupSpec<R: GridRow> {
levels: Arc<[GroupBy<R>]>,
}
impl<R: GridRow> Clone for GroupSpec<R> {
fn clone(&self) -> Self {
Self {
levels: self.levels.clone(),
}
}
}
impl<R: GridRow> GroupSpec<R> {
pub(crate) fn levels(&self) -> &[GroupBy<R>] {
&self.levels
}
}
pub struct GroupOrderContext<'a> {
path: &'a GroupPath,
row_count: usize,
aggregates: AggregateRow<'a>,
}
impl<'a> GroupOrderContext<'a> {
pub(crate) fn new(path: &'a GroupPath, row_count: usize, aggregates: AggregateRow<'a>) -> Self {
Self {
path,
row_count,
aggregates,
}
}
pub fn path(&self) -> &'a GroupPath {
self.path
}
pub fn value(&self) -> &'a OwnedValue {
self.path
.value(self.path.depth())
.expect("a group's path is not the root")
}
pub fn depth(&self) -> u16 {
self.path.depth()
}
pub fn row_count(&self) -> usize {
self.row_count
}
pub fn aggregate(&self, column: ColumnIx) -> Option<&'a OwnedValue> {
self.aggregates.get(column)
}
}
pub trait GroupOrder<R: GridRow>: Send + Sync + 'static {
fn compare(&self, a: &GroupOrderContext<'_>, b: &GroupOrderContext<'_>) -> Ordering;
fn depends_on(&self) -> ColumnSet {
ColumnSet::EMPTY
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct KeyOrder;
impl<R: GridRow> GroupOrder<R> for KeyOrder {
fn compare(&self, _: &GroupOrderContext<'_>, _: &GroupOrderContext<'_>) -> Ordering {
Ordering::Equal
}
}
#[derive(Clone, Copy, Debug)]
pub struct AggregateOrder {
column: ColumnIx,
direction: SortDirection,
}
impl AggregateOrder {
pub fn new(column: impl GridColumns, direction: SortDirection) -> Self {
Self {
column: column.ix(),
direction,
}
}
pub fn column(&self) -> ColumnIx {
self.column
}
pub fn direction(&self) -> SortDirection {
self.direction
}
}
impl<R: GridRow> GroupOrder<R> for AggregateOrder {
fn compare(&self, a: &GroupOrderContext<'_>, b: &GroupOrderContext<'_>) -> Ordering {
match (a.aggregate(self.column), b.aggregate(self.column)) {
(Some(a), Some(b)) => directed(a.cmp_total(b), self.direction),
_ => Ordering::Equal,
}
}
fn depends_on(&self) -> ColumnSet {
let mut columns = ColumnSet::EMPTY;
columns.insert(self.column);
columns
}
}
pub trait GroupFooter: Send + Sync + 'static {
fn has_footer(&self, group: &GroupInfo<'_>) -> bool;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoGroupFooter;
impl GroupFooter for NoGroupFooter {
fn has_footer(&self, _: &GroupInfo<'_>) -> bool {
false
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct EveryGroupFooter;
impl GroupFooter for EveryGroupFooter {
fn has_footer(&self, _: &GroupInfo<'_>) -> bool {
true
}
}
impl<R: GridRow> ViewSpec<R> {
pub fn with_group_by(mut self, levels: impl IntoIterator<Item = GroupBy<R>>) -> Self {
let mut unique: Vec<GroupBy<R>> = Vec::new();
for level in levels {
debug_assert!(
level
.as_key()
.is_none_or(|key| R::Column::all().iter().all(|column| column.id() != key.id)),
"the group key id {:?} is a column id",
level.id()
);
if !unique.iter().any(|kept| kept.id() == level.id()) {
unique.push(level);
}
}
self.structure = if unique.is_empty() {
Structure::Flat
} else {
Structure::Groups(GroupSpec {
levels: unique.into(),
})
};
self
}
pub fn group_by(&self) -> &[GroupBy<R>] {
match self.structure() {
Structure::Groups(groups) => groups.levels(),
Structure::Flat | Structure::Tree(_) => &[],
}
}
pub fn with_group_order(mut self, order: impl GroupOrder<R>) -> Self {
self.group_order = Some(Arc::new(order));
self
}
pub fn group_order(&self) -> &dyn GroupOrder<R> {
match &self.group_order {
Some(order) => &**order,
None => &KeyOrder,
}
}
pub fn with_group_footer(mut self, footer: impl GroupFooter) -> Self {
self.group_footer = Some(Arc::new(footer));
self
}
pub(crate) fn group_footer(&self) -> &dyn GroupFooter {
match &self.group_footer {
Some(footer) => &**footer,
None => &NoGroupFooter,
}
}
}
pub(super) fn rebuild<R: GridRow>(
engine: &Rebuild,
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
grouping: &GroupSpec<R>,
) -> Projection {
let parallelism = Parallelism::for_rows(snapshot.len(), engine.max_threads());
rebuild_with(snapshot, spec, grouping, parallelism)
}
pub(super) fn rebuild_with<R: GridRow>(
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
grouping: &GroupSpec<R>,
parallelism: Parallelism,
) -> Projection {
let levels = grouping.levels();
let keys = sort_keys(spec, levels);
match keys.len() {
0..=2 => build::<R, 2>(snapshot, spec, levels, &keys, parallelism),
3 => build::<R, 3>(snapshot, spec, levels, &keys, parallelism),
_ => build::<R, MAX_CACHED_KEYS>(snapshot, spec, levels, &keys, parallelism),
}
}
fn build<R: GridRow, const N: usize>(
snapshot: &StoreSnapshot<R>,
spec: &ViewSpec<R>,
levels: &[GroupBy<R>],
keys: &[SortKey<'_, R>],
parallelism: Parallelism,
) -> Projection {
let level_count = levels.len() as u16;
let (mut sorted, fading) = sort_rows::<R, N>(snapshot, spec, keys, parallelism);
let fading = if fading > 0 {
FadingIds::new(snapshot)
} else {
FadingIds::default()
};
let mut table = find_groups(levels, &sorted);
if !fading.is_empty() && drop_fading_groups(&mut sorted, &table, level_count, &fading) {
table = find_groups(levels, &sorted);
}
let aggregates = ViewAggregates::new(spec);
#[cfg(debug_assertions)]
warn_unaggregated_order(spec, aggregates.columns());
let mut computed =
aggregate_groups(&aggregates, &table.groups, level_count, &sorted, |entry| {
(!fading.contains(entry.2)).then_some(&**entry.3)
});
let columns = aggregates.columns();
let width = columns.len();
let order = order_groups(spec.group_order(), &table, &computed, columns);
let expansion = spec.expansion();
let mut caption_ids = vec![0u32; order.len()];
let mut groups = Vec::with_capacity(order.len());
let mut values = Vec::with_capacity(order.len() * width);
let mut rows = Vec::with_capacity(sorted.len());
for (id, &key_ix) in order.iter().enumerate() {
let key_ix = key_ix as usize;
caption_ids[key_ix] = id as u32;
let keyed = &table.groups[key_ix];
let start = rows.len() as u32;
let len = keyed.rows.end - keyed.rows.start;
if usize::from(keyed.depth) + 1 == levels.len() {
rows.extend(
sorted[keyed.rows.start as usize..keyed.rows.end as usize]
.iter()
.map(|entry| entry.2),
);
}
let path = mem::take(&mut table.paths[key_ix]);
let expanded = expansion.is_group_expanded(&path, levels.len());
groups.push(Group {
parent: table.parents[key_ix].map(|parent| GroupId::new(caption_ids[parent] as usize)),
end: (id + keyed.end as usize - key_ix) as u32,
key_ix: key_ix as u32,
rows: start..start + len,
live: computed.live[key_ix],
line: u32::MAX,
expanded,
footer: false,
path,
});
values.extend(
computed.values[key_ix * width..(key_ix + 1) * width]
.iter_mut()
.map(|value| mem::replace(value, OwnedValue::Empty)),
);
}
let mut groups = GroupData::new(level_count, groups, AggregateValues::new(columns, values));
let footer = spec.group_footer();
let footers: Vec<bool> = groups
.table(&rows)
.iter()
.map(|group| footer.has_footer(&group))
.collect();
for (group, footer) in groups.groups.iter_mut().zip(footers) {
group.footer = footer;
}
Projection::with_groups(snapshot, rows, groups)
.with_row_count(computed.total_live)
.with_totals(AggregateValues::new(columns, computed.totals))
}
const MAX_CACHED_KEYS: usize = 4;
struct SortKey<'s, R: GridRow> {
source: KeySource<'s, R>,
direction: SortDirection,
}
enum KeySource<'s, R: GridRow> {
Level(&'s GroupBy<R>),
Column(R::Column),
}
impl<R: GridRow> SortKey<'_, R> {
#[inline]
fn value<'r>(&self, row: &'r R) -> CellValue<'r> {
match &self.source {
KeySource::Level(level) => level.value(row),
KeySource::Column(column) => row.value(*column),
}
}
}
fn sort_keys<'s, R: GridRow>(
spec: &'s ViewSpec<R>,
levels: &'s [GroupBy<R>],
) -> Vec<SortKey<'s, R>> {
levels
.iter()
.map(|level| SortKey {
source: KeySource::Level(level),
direction: level.direction(),
})
.chain(spec.sort().iter().map(|&(column, direction)| SortKey {
source: KeySource::Column(column),
direction,
}))
.collect()
}
type GroupEntry<'a, R, const N: usize> = ([CellValue<'a>; N], u64, RowId, &'a Arc<R>);
#[inline(always)]
fn compare_grouped<R: GridRow, const N: usize>(
a: &GroupEntry<'_, R, N>,
b: &GroupEntry<'_, R, N>,
cached: &[SortDirection; N],
rest: &[SortKey<'_, R>],
) -> Ordering {
for ix in 0..N {
let ordering = directed(a.0[ix].cmp_total(&b.0[ix]), cached[ix]);
if ordering.is_ne() {
return ordering;
}
}
rest.iter()
.map(|key| directed(key.value(a.3).cmp_total(&key.value(b.3)), key.direction))
.find(|ordering| ordering.is_ne())
.unwrap_or(Ordering::Equal)
.then_with(|| a.1.cmp(&b.1))
}
fn sort_rows<'a, R: GridRow, const N: usize>(
snapshot: &'a StoreSnapshot<R>,
spec: &ViewSpec<R>,
keys: &[SortKey<'_, R>],
parallelism: Parallelism,
) -> (Vec<GroupEntry<'a, R, N>>, usize) {
let cached: [SortDirection; N] = array::from_fn(|ix| {
keys.get(ix)
.map_or(SortDirection::Ascending, |key| key.direction)
});
let rest = keys.get(N..).unwrap_or_default();
sorted_rows(
snapshot,
spec,
parallelism,
|id, row, seq| {
let keys =
array::from_fn(|ix| keys.get(ix).map_or(CellValue::Empty, |key| key.value(row)));
(keys, seq, id, row)
},
|a, b| compare_grouped(a, b, &cached, rest),
|entry| *entry,
)
}
fn drop_fading_groups<R: GridRow, const N: usize>(
sorted: &mut Vec<GroupEntry<'_, R, N>>,
table: &KeyTable,
levels: u16,
fading: &FadingIds,
) -> bool {
let is_fading = |group: &KeyedGroup| {
usize::from(group.depth) + 1 == usize::from(levels)
&& sorted[group.rows.start as usize..group.rows.end as usize]
.iter()
.all(|entry| fading.contains(entry.2))
};
if !table.groups.iter().any(is_fading) {
return false;
}
let dropped: Vec<Range<usize>> = table
.groups
.iter()
.filter(|group| is_fading(group))
.map(|group| group.rows.start as usize..group.rows.end as usize)
.collect();
let mut kept = 0;
let mut next = 0;
for range in dropped.into_iter().chain([sorted.len()..sorted.len()]) {
sorted.copy_within(next..range.start, kept);
kept += range.start - next;
next = range.end;
}
sorted.truncate(kept);
true
}
#[derive(Default)]
struct KeyTable {
groups: Vec<KeyedGroup>,
paths: Vec<GroupPath>,
parents: Vec<Option<usize>>,
}
fn find_groups<'a, R: GridRow, const N: usize>(
levels: &[GroupBy<R>],
sorted: &[GroupEntry<'a, R, N>],
) -> KeyTable {
let ids: Vec<ColumnId> = levels.iter().map(GroupBy::id).collect();
let mut table = KeyTable::default();
let mut previous: Vec<CellValue<'a>> = Vec::with_capacity(levels.len());
let mut keys: Vec<CellValue<'a>> = Vec::with_capacity(levels.len());
let mut open: Vec<usize> = Vec::with_capacity(levels.len());
let close = |table: &mut KeyTable, open: &mut Vec<usize>, depth: usize, row_ix: usize| {
while open.len() > depth {
let group = open.pop().expect("open groups");
table.groups[group].end = table.groups.len() as u32;
table.groups[group].rows.end = row_ix as u32;
}
};
for (row_ix, entry) in sorted.iter().enumerate() {
keys.clear();
keys.extend(
levels
.iter()
.enumerate()
.map(|(depth, level)| match entry.0.get(depth) {
Some(&key) => key,
None => level.value(entry.3),
}),
);
let changed = if row_ix == 0 {
0
} else {
keys.iter()
.zip(&previous)
.position(|(key, previous)| key.cmp_total(previous).is_ne())
.unwrap_or(levels.len())
};
close(&mut table, &mut open, changed, row_ix);
for depth in changed..levels.len() {
let parent = open.last().copied();
let level = (ids[depth].clone(), OwnedValue::from_cell(keys[depth]));
let path = match parent {
Some(parent) => GroupPath::new(
table.paths[parent]
.levels()
.iter()
.cloned()
.chain(iter::once(level)),
),
None => GroupPath::new([level]),
};
open.push(table.groups.len());
table.groups.push(KeyedGroup {
depth: depth as u16,
end: 0,
rows: row_ix as u32..row_ix as u32,
});
table.paths.push(path);
table.parents.push(parent);
}
mem::swap(&mut previous, &mut keys);
}
close(&mut table, &mut open, 0, sorted.len());
table
}
fn order_groups<R: GridRow>(
order: &dyn GroupOrder<R>,
table: &KeyTable,
computed: &GroupValues,
columns: ColumnSet,
) -> Vec<u32> {
let width = columns.len();
let context = |ix: u32| {
let ix = ix as usize;
let live = computed.live[ix] as usize;
GroupOrderContext::new(
&table.paths[ix],
live,
AggregateRow::new(
columns,
&computed.values[ix * width..(ix + 1) * width],
live,
),
)
};
let compare = |a: u32, b: u32| order.compare(&context(a), &context(b));
let mut ordered = Vec::with_capacity(table.groups.len());
let mut siblings = Vec::new();
order_siblings(
&table.groups,
0..table.groups.len(),
0,
&compare,
&mut siblings,
&mut ordered,
);
ordered
}
fn order_siblings(
groups: &[KeyedGroup],
span: Range<usize>,
depth: usize,
compare: &impl Fn(u32, u32) -> Ordering,
siblings: &mut Vec<Vec<u32>>,
ordered: &mut Vec<u32>,
) {
if siblings.len() <= depth {
siblings.resize_with(depth + 1, Vec::new);
}
let mut level = mem::take(&mut siblings[depth]);
level.clear();
let mut next = span.start;
while next < span.end {
level.push(next as u32);
next = groups[next].end as usize;
}
level.sort_by(|&a, &b| compare(a, b));
for &group in &level {
ordered.push(group);
let group = group as usize;
let subgroups = group + 1..groups[group].end as usize;
if !subgroups.is_empty() {
order_siblings(groups, subgroups, depth + 1, compare, siblings, ordered);
}
}
siblings[depth] = level;
}
#[cfg(debug_assertions)]
fn warn_unaggregated_order<R: GridRow>(spec: &ViewSpec<R>, aggregated: ColumnSet) {
let order_columns = spec.group_order().depends_on();
if let Some(column) = R::Column::all()
.iter()
.find(|column| order_columns.contains(column.ix()) && !aggregated.contains(column.ix()))
{
log::warn!(
"the group order reads column {:?}, which the view does not aggregate: groups keep the key order",
column.id()
);
}
}