pub struct Groups<'a> { /* private fields */ }Expand description
The groups of a GroupSpec, ordered by key. Cheap to get.
A group whose last row leaves (deleted, moved to another group, or out of a window) is
gone from them. Read them with to_batch: the key columns first, then
each aggregate in the order the spec added them.
use std::sync::Arc;
use densa_data::arrow_array::cast::AsArray;
use densa_data::arrow_array::types::{Float64Type, UInt64Type};
use densa_data::arrow_array::{ArrayRef, Float64Array, RecordBatch, StringArray};
use densa_data::{Aggregate, GroupSpec, LiveTable};
let hosts = RecordBatch::try_from_iter([
("host", Arc::new(StringArray::from(vec!["h1", "h2", "h3"])) as ArrayRef),
("region", Arc::new(StringArray::from(vec!["eu", "eu", "us"])) as ArrayRef),
("ms", Arc::new(Float64Array::from(vec![10.0, 30.0, 5.0])) as ArrayRef),
])?;
let mut table = LiveTable::new(hosts.schema(), "host")?;
let by_region = table.add_groups(
GroupSpec::new()
.group_by("region")
.aggregate(Aggregate::count())
.aggregate(Aggregate::mean("ms")),
)?;
table.upsert(hosts)?;
let batch = table.groups(by_region).unwrap().to_batch()?;
let regions = batch.column(0).as_string::<i32>(); // the key, in its column's type
let hosts = batch.column(1).as_primitive::<UInt64Type>(); // counts are UInt64
let means = batch.column(2).as_primitive::<Float64Type>(); // means are Float64
assert_eq!((regions.value(0), hosts.value(0), means.value(0)), ("eu", 2, 20.0));
assert_eq!((regions.value(1), hosts.value(1), means.value(1)), ("us", 1, 5.0));Implementations§
Source§impl Groups<'_>
impl Groups<'_>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of groups: the rows of to_batch. A spec without keys always
has one.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether there are no groups: the spec has keys and no row is counted.
Sourcepub fn revision(&self) -> u64
pub fn revision(&self) -> u64
Increases whenever a group changes (and sometimes when a bulk change recounts the
same groups): rebuild a display of to_batch only when it moved.
Sourcepub fn to_batch(&self) -> Result<RecordBatch, DataError>
pub fn to_batch(&self) -> Result<RecordBatch, DataError>
The groups as one record batch, one row per group in key order: the key columns,
then each aggregate. A bucketed key holds each bucket’s start. Keys, min and max have
the column’s type, except that dictionary
columns come out decoded (as their value type) and floats canonical (0.0 for -0.0,
one NaN). Counts are UInt64, never null. Sums of integers are Decimal128(38, 0),
which holds any sum a table can produce; sums of decimals Decimal128(38, scale),
null if the exact sum needs more than 38 digits; sums of floats Float64. Means are
Float64. Any other aggregate over no values is null. Allocates.
§Errors
DataError::Arrow if Arrow cannot assemble a column (over 2 GiB of strings).