Skip to main content

Groups

Struct Groups 

Source
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<'_>

Source

pub fn len(&self) -> usize

Number of groups: the rows of to_batch. A spec without keys always has one.

Source

pub fn is_empty(&self) -> bool

Whether there are no groups: the spec has keys and no row is counted.

Source

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.

Source

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).

Trait Implementations§

Source§

impl<'a> Clone for Groups<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Groups<'a>

Source§

impl<'a> Debug for Groups<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Groups<'a>

§

impl<'a> !UnwindSafe for Groups<'a>

§

impl<'a> Freeze for Groups<'a>

§

impl<'a> Send for Groups<'a>

§

impl<'a> Sync for Groups<'a>

§

impl<'a> Unpin for Groups<'a>

§

impl<'a> UnsafeUnpin for Groups<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.