Skip to main content

GroupSpec

Struct GroupSpec 

Source
pub struct GroupSpec { /* private fields */ }
Expand description

Groups of rows with equal key values, and aggregates over each group.

use std::sync::Arc;
use arrow_array::{ArrayRef, Decimal128Array, Int64Array, RecordBatch, StringArray};
use densa_data::{Aggregate, GroupSpec, LiveTable};

let trades = |ids: Vec<i64>, desks: Vec<&str>, qty: Vec<i64>| {
    RecordBatch::try_from_iter([
        ("id", Arc::new(Int64Array::from(ids)) as ArrayRef),
        ("desk", Arc::new(StringArray::from(desks)) as ArrayRef),
        ("qty", Arc::new(Int64Array::from(qty)) as ArrayRef),
    ])
};
let mut table = LiveTable::new(trades(vec![], vec![], vec![])?.schema(), "id")?;
let by_desk = table.add_groups(
    GroupSpec::new()
        .group_by("desk")
        .aggregate(Aggregate::count())
        .aggregate(Aggregate::sum("qty")),
)?;
table.upsert(trades(vec![1, 2, 3], vec!["fx", "rates", "fx"], vec![5, 7, 10])?)?;
table.upsert(trades(vec![3], vec!["rates"], vec![10])?)?; // trade 3 moves desk

let totals = table.groups(by_desk).unwrap().to_batch()?; // fx: 1 trade, rates: 2
let qty = totals.column(2).as_any().downcast_ref::<Decimal128Array>().unwrap();
assert_eq!((qty.value(0), qty.value(1)), (5, 17));

Implementations§

Source§

impl GroupSpec

Source

pub fn new() -> Self

No keys and no aggregates yet. Without keys there is exactly one group, of every counted row, even when there are none.

Source

pub fn group_by(self, column: impl Into<String>) -> Self

Groups by column too, after the key columns given before it. Groups are listed by key in the order views sort: ascending, nulls last. -0.0 and 0.0 are one key, and so are all NaNs.

Source

pub fn aggregate(self, aggregate: Aggregate) -> Self

Adds an aggregate column. Every key and aggregate needs its own name.

Source

pub fn filter(self, filter: Filter) -> Self

Counts only rows for which filter is true.

Trait Implementations§

Source§

impl Clone for GroupSpec

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 Debug for GroupSpec

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for GroupSpec

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for GroupSpec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for GroupSpec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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.