use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;
use crate::row::{Ctx, Row, Val};
use super::distinct::DistinctSet;
use super::value::OrderKey;
use super::{fmt_num_typed, lexical, Agg, ExistsCache, GroupSpec};
use crate::index::GraphIndex;
enum Accum {
CountStar(u64),
Count {
slot: Option<usize>,
distinct: bool,
n: u64,
seen: DistinctSet,
},
Sum {
slot: Option<usize>,
sum: f64,
err: bool,
},
Avg {
slot: Option<usize>,
sum: f64,
count: u64,
err: bool,
},
Sample {
slot: Option<usize>,
done: bool,
val: Option<Arc<str>>,
},
GroupConcat {
slot: Option<usize>,
sep: String,
distinct: bool,
parts: Vec<String>,
seen: HashSet<String>,
err: bool,
},
MinMax {
slot: Option<usize>,
want_min: bool,
best: Option<OrderKey>,
err: bool,
},
}
impl Accum {
fn new(agg: &Agg, ctx: &Ctx) -> Accum {
let slot = |v: &str| ctx.slots.slot(v);
match agg {
Agg::CountStar { .. } => Accum::CountStar(0),
Agg::Count(v, d) => Accum::Count {
slot: slot(v),
distinct: *d,
n: 0,
seen: DistinctSet::new(ctx.rete.spill_threshold()),
},
Agg::Sum(v) => Accum::Sum {
slot: slot(v),
sum: 0.0,
err: false,
},
Agg::Avg(v) => Accum::Avg {
slot: slot(v),
sum: 0.0,
count: 0,
err: false,
},
Agg::Sample(v) => Accum::Sample {
slot: slot(v),
done: false,
val: None,
},
Agg::GroupConcat(v, sep, d) => Accum::GroupConcat {
slot: slot(v),
sep: sep.clone(),
distinct: *d,
parts: Vec::new(),
seen: HashSet::new(),
err: false,
},
Agg::Min(v) => Accum::MinMax {
slot: slot(v),
want_min: true,
best: None,
err: false,
},
Agg::Max(v) => Accum::MinMax {
slot: slot(v),
want_min: false,
best: None,
err: false,
},
}
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
fn slot_mut(&mut self) -> Option<&mut Option<usize>> {
match self {
Accum::CountStar(_) => None,
Accum::Count { slot, .. }
| Accum::Sum { slot, .. }
| Accum::Avg { slot, .. }
| Accum::Sample { slot, .. }
| Accum::GroupConcat { slot, .. }
| Accum::MinMax { slot, .. } => Some(slot),
}
}
fn update(&mut self, ctx: &Ctx, row: &Row) {
match self {
Accum::CountStar(n) => *n += 1,
Accum::Count {
slot,
distinct,
n,
seen,
} => {
if let Some(v) = slot.and_then(|s| row[s].as_ref()) {
if *distinct {
seen.insert(v);
} else {
*n += 1;
}
}
}
Accum::Sum { slot, sum, err } => {
if *err {
return;
}
match slot
.and_then(|s| row[s].as_ref())
.and_then(|v| ctx.resolver.num(v))
{
Some(x) => *sum += x,
None => *err = true,
}
}
Accum::Avg {
slot,
sum,
count,
err,
} => match slot.and_then(|s| row[s].as_ref()) {
None => *err = true,
Some(v) => {
*count += 1;
if *err {
return;
}
match ctx.resolver.num(v) {
Some(x) => *sum += x,
None => *err = true, }
}
},
Accum::Sample { slot, done, val } => {
if !*done {
if let Some(v) = slot.and_then(|s| row[s].as_ref()) {
*done = true;
*val = ctx.resolver.str_of(v);
}
}
}
Accum::GroupConcat {
slot,
distinct,
parts,
seen,
err,
..
} => {
if *err {
return;
}
let part = slot
.and_then(|s| row[s].as_ref())
.and_then(|v| ctx.resolver.str_of(v))
.and_then(|t| {
let tag = match crate::terms::literal_parts(&t)? {
(_, "") => None,
(_, sfx) if sfx.starts_with('@') => Some(sfx[1..].to_string()),
(_, "^^<http://www.w3.org/2001/XMLSchema#string>") => None,
_ => return None,
};
Some((lexical(&t), tag))
});
match part {
Some((p, tag)) => {
let key = match &tag {
Some(l) => format!("{p}@{l}"),
None => p.clone(),
};
if !*distinct || seen.insert(key) {
parts.push(p);
}
}
None => {
*err = true;
parts.clear();
seen.clear();
}
}
}
Accum::MinMax {
slot,
want_min,
best,
err,
} => {
if *err {
return;
}
let Some(t) = slot
.and_then(|s| row[s].as_ref())
.and_then(|v| ctx.resolver.str_of(v))
else {
if *want_min {
*err = true;
*best = None;
}
return;
};
let t = OrderKey::new(t);
let take = match best.as_ref() {
None => true,
Some(cur) => {
let o = t.cmp(cur);
if *want_min {
o == std::cmp::Ordering::Less
} else {
o == std::cmp::Ordering::Greater
}
}
};
if take {
*best = Some(t);
}
}
}
}
fn finalize(self) -> Option<String> {
match self {
Accum::CountStar(n) => Some(fmt_num_typed(n as f64)),
Accum::Count {
slot,
distinct,
n,
seen,
} => {
if slot.is_none() {
return Some(fmt_num_typed(0.0)); }
let c = if distinct {
seen.count() as f64
} else {
n as f64
};
Some(fmt_num_typed(c))
}
Accum::Sum { sum, err, .. } => (!err).then(|| fmt_num_typed(sum)),
Accum::Avg {
sum, count, err, ..
} => {
if count == 0 {
return Some(fmt_num_typed(0.0));
}
if err {
return None; }
Some(fmt_num_typed(sum / count as f64))
}
Accum::Sample { val, .. } => val.map(|t| t.to_string()),
Accum::GroupConcat {
sep, parts, err, ..
} => {
(!err).then(|| crate::terms::make_literal(&parts.join(&sep), None, None))
}
Accum::MinMax { best, .. } => best.map(|k| k.token.to_string()),
}
}
}
pub(super) fn aggregate<I>(ctx: &Ctx, index: &GraphIndex, rows: I, g: &GroupSpec) -> Vec<Row>
where
I: IntoIterator<Item = Row>,
{
let groups =
fold_groups(ctx, with_pre(ctx, index, rows, g), g, usize::MAX, None).expect("no group cap");
let groups = groups
.into_iter()
.map(|(key, accs)| (key, accs.into_iter().map(Accum::finalize).collect()));
emit_groups(ctx, g, groups)
}
pub(super) fn apply_pre(
ctx: &Ctx,
index: &GraphIndex,
row: &mut Row,
g: &GroupSpec,
cache: &mut ExistsCache,
) {
for (var, expr) in &g.pre {
if let Some(slot) = ctx.slots.slot(var) {
row[slot] = expr.value_in(ctx, index, row, cache).map(Val::Str);
}
}
}
fn with_pre<'a, I>(
ctx: &'a Ctx,
index: &'a GraphIndex,
rows: I,
g: &'a GroupSpec,
) -> impl Iterator<Item = Row> + 'a
where
I: IntoIterator<Item = Row>,
I::IntoIter: 'a,
{
let mut cache = ExistsCache::new();
rows.into_iter().map(move |mut r| {
apply_pre(ctx, index, &mut r, g, &mut cache);
r
})
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) fn fold_slots(ctx: &Ctx, g: &GroupSpec) -> Vec<usize> {
let mut keep: Vec<usize> = Vec::new();
let mut add = |v: &str| {
if let Some(s) = ctx.slots.slot(v) {
if !keep.contains(&s) {
keep.push(s);
}
}
};
for v in &g.by {
add(v);
}
for (_, agg) in &g.aggs {
match agg {
Agg::CountStar { .. } => {}
Agg::Count(v, _)
| Agg::Sum(v)
| Agg::Avg(v)
| Agg::Min(v)
| Agg::Max(v)
| Agg::Sample(v)
| Agg::GroupConcat(v, _, _) => add(v),
}
}
keep
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) fn aggregate_folded<I>(ctx: &Ctx, rows: I, g: &GroupSpec, keep: &[usize]) -> Vec<Row>
where
I: IntoIterator<Item = Row>,
{
let groups = fold_groups(ctx, rows, g, usize::MAX, Some(keep)).expect("no group cap");
let groups = groups
.into_iter()
.map(|(key, accs)| (key, accs.into_iter().map(Accum::finalize).collect()));
emit_groups(ctx, g, groups)
}
fn fold_groups<I>(
ctx: &Ctx,
rows: I,
g: &GroupSpec,
max_groups: usize,
cut: Option<&[usize]>,
) -> Option<BTreeMap<Vec<Option<Val>>, Vec<Accum>>>
where
I: IntoIterator<Item = Row>,
{
let at = |slot: usize| match cut {
Some(keep) => keep.iter().position(|&k| k == slot),
None => Some(slot),
};
let by_slots: Vec<Option<usize>> =
g.by.iter()
.map(|v| ctx.slots.slot(v).and_then(at))
.collect();
let new_accs = || -> Vec<Accum> {
g.aggs
.iter()
.map(|(_, a)| {
let mut acc = Accum::new(a, ctx);
if cut.is_some() {
if let Some(slot) = acc.slot_mut() {
*slot = slot.and_then(at);
}
}
acc
})
.collect()
};
let mut groups: BTreeMap<Vec<Option<Val>>, Vec<Accum>> = BTreeMap::new();
for r in rows {
let key: Vec<Option<Val>> = by_slots
.iter()
.map(|s| s.and_then(|i| r[i].clone()))
.collect();
let accs = groups.entry(key).or_insert_with(new_accs);
for acc in accs.iter_mut() {
acc.update(ctx, &r);
}
if groups.len() > max_groups {
return None;
}
}
if g.by.is_empty() && groups.is_empty() {
groups.insert(Vec::new(), new_accs());
}
Some(groups)
}
fn emit_groups(
ctx: &Ctx,
g: &GroupSpec,
groups: impl Iterator<Item = (Vec<Option<Val>>, Vec<Option<String>>)>,
) -> Vec<Row> {
let by_slots: Vec<Option<usize>> = g.by.iter().map(|v| ctx.slots.slot(v)).collect();
let mut out = Vec::new();
for (key, finals) in groups {
let mut row = ctx.slots.empty_row();
for (slot, val) in by_slots.iter().zip(key.into_iter()) {
if let (Some(i), Some(v)) = (slot, val) {
row[*i] = Some(v);
}
}
for ((res_var, _), val) in g.aggs.iter().zip(finals.into_iter()) {
if let Some(slot) = ctx.slots.slot(res_var) {
if let Some(val) = val {
row[slot] = Some(ctx.resolver.canon_term(&val));
}
}
}
out.push(row);
}
out
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) enum SVal {
Id(i64),
Str(Box<str>),
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) enum Partial {
Count(u64),
Sum {
sum: f64,
err: bool,
},
Avg {
sum: f64,
count: u64,
err: bool,
},
Sample(Option<Box<str>>),
Concat {
sep: String,
parts: Vec<String>,
err: bool,
},
MinMax {
want_min: bool,
best: Option<Box<str>>,
err: bool,
},
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) type PartialGroups = Vec<(Vec<Option<SVal>>, Vec<Partial>)>;
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) fn mergeable(g: &GroupSpec) -> bool {
g.aggs.iter().all(|(_, a)| match a {
Agg::CountStar { .. } => true,
Agg::Count(_, distinct) => !distinct,
Agg::GroupConcat(_, _, distinct) => !distinct,
Agg::Sum(_) | Agg::Avg(_) | Agg::Min(_) | Agg::Max(_) | Agg::Sample(_) => true,
})
}
impl Accum {
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
fn into_partial(self) -> Partial {
match self {
Accum::CountStar(n) => Partial::Count(n),
Accum::Count { n, .. } => Partial::Count(n),
Accum::Sum { sum, err, .. } => Partial::Sum { sum, err },
Accum::Avg {
sum, count, err, ..
} => Partial::Avg { sum, count, err },
Accum::Sample { val, .. } => Partial::Sample(val.map(|v| Box::from(&*v))),
Accum::GroupConcat {
sep, parts, err, ..
} => Partial::Concat { sep, parts, err },
Accum::MinMax {
want_min,
best,
err,
..
} => Partial::MinMax {
want_min,
best: best.map(|k| Box::from(&*k.token)),
err,
},
}
}
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
impl Partial {
fn merge(&mut self, next: Partial) {
match (self, next) {
(Partial::Count(a), Partial::Count(b)) => *a += b,
(Partial::Sum { sum, err }, Partial::Sum { sum: s, err: e }) => {
*sum += s;
*err |= e;
}
(
Partial::Avg { sum, count, err },
Partial::Avg {
sum: s,
count: c,
err: e,
},
) => {
*sum += s;
*count += c;
*err |= e;
}
(Partial::Sample(a), Partial::Sample(b)) => {
if a.is_none() {
*a = b;
}
}
(
Partial::Concat { parts, err, .. },
Partial::Concat {
parts: p, err: e, ..
},
) => {
*err |= e;
if *err {
parts.clear();
} else {
parts.extend(p);
}
}
(
Partial::MinMax {
want_min,
best,
err,
},
Partial::MinMax {
best: b, err: e, ..
},
) => {
*err |= e;
if *err {
*best = None;
return;
}
let Some(b) = b else { return };
let take = match best.as_deref() {
None => true,
Some(cur) => {
let o = OrderKey::new(Arc::from(&*b)).cmp(&OrderKey::new(Arc::from(cur)));
if *want_min {
o == std::cmp::Ordering::Less
} else {
o == std::cmp::Ordering::Greater
}
}
};
if take {
*best = Some(b);
}
}
_ => unreachable!("partial aggregates of different kinds"),
}
}
fn finalize(self) -> Option<String> {
match self {
Partial::Count(n) => Some(fmt_num_typed(n as f64)),
Partial::Sum { sum, err } => (!err).then(|| fmt_num_typed(sum)),
Partial::Avg { sum, count, err } => {
if count == 0 {
return Some(fmt_num_typed(0.0));
}
if err {
return None;
}
Some(fmt_num_typed(sum / count as f64))
}
Partial::Sample(val) => val.map(String::from),
Partial::Concat { sep, parts, err } => {
(!err).then(|| crate::terms::make_literal(&parts.join(&sep), None, None))
}
Partial::MinMax { best, .. } => best.map(String::from),
}
}
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
pub(super) fn aggregate_partial<I>(
ctx: &Ctx,
index: &GraphIndex,
rows: I,
g: &GroupSpec,
max_groups: usize,
) -> Option<PartialGroups>
where
I: IntoIterator<Item = Row>,
{
let groups = fold_groups(ctx, with_pre(ctx, index, rows, g), g, max_groups, None)?;
Some(
groups
.into_iter()
.map(|(key, accs)| {
let key = key
.into_iter()
.map(|v| {
v.map(|v| match v {
Val::Id(i) => SVal::Id(i),
Val::Str(s) => SVal::Str(Box::from(&*s)),
})
})
.collect();
(key, accs.into_iter().map(Accum::into_partial).collect())
})
.collect(),
)
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
#[derive(Default)]
pub(super) struct Merger {
groups: BTreeMap<Vec<Option<Val>>, Vec<Partial>>,
}
#[cfg_attr(not(feature = "parallel"), allow(dead_code))]
impl Merger {
pub(super) fn absorb(&mut self, shard: PartialGroups) {
for (key, partials) in shard {
let key: Vec<Option<Val>> = key
.into_iter()
.map(|v| {
v.map(|v| match v {
SVal::Id(i) => Val::Id(i),
SVal::Str(s) => Val::Str(Arc::from(&*s)),
})
})
.collect();
match self.groups.entry(key) {
std::collections::btree_map::Entry::Vacant(e) => {
e.insert(partials);
}
std::collections::btree_map::Entry::Occupied(mut e) => {
for (have, next) in e.get_mut().iter_mut().zip(partials) {
have.merge(next);
}
}
}
}
}
pub(super) fn finish(mut self, ctx: &Ctx, g: &GroupSpec) -> Vec<Row> {
if g.by.is_empty() && self.groups.is_empty() {
let zero = g
.aggs
.iter()
.map(|(_, a)| Accum::new(a, ctx).into_partial())
.collect();
self.groups.insert(Vec::new(), zero);
}
let groups = self
.groups
.into_iter()
.map(|(key, ps)| (key, ps.into_iter().map(Partial::finalize).collect()));
emit_groups(ctx, g, groups)
}
}
#[cfg(test)]
fn compute_agg(ctx: &Ctx, agg: &Agg, members: &[Row]) -> Option<String> {
let mut acc = Accum::new(agg, ctx);
for m in members {
acc.update(ctx, m);
}
acc.finalize()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dictionary::DictionaryBuilder;
use crate::file::{write_file_v05 as write_file, Rete};
use crate::index::GraphIndexBuilder;
use crate::row::{Slots, Val};
use crate::sparql::{ArithOp, FExpr};
fn fixture() -> Rete {
let triples = [("<s>", "<p>", "\"dictionary value\"")];
let mut builder = DictionaryBuilder::new();
for (s, p, o) in triples {
builder.observe(s, p, o);
}
let dict = builder.build();
let mut index = GraphIndexBuilder::new();
for (s, p, o) in triples {
index.push(dict.encode(s, p, o).unwrap());
}
Rete::open(&write_file(&dict, &index.build(), false, &[], 0)).unwrap()
}
fn val(s: &str) -> Option<Val> {
Some(Val::Str(Arc::from(s)))
}
fn context(rete: &Rete) -> Ctx<'_> {
let mut slots = Slots::new();
for name in ["g", "v", "text", "pre", "result"] {
slots.add(name);
}
Ctx::new(rete, slots)
}
fn row(ctx: &Ctx, group: &str, number: Option<&str>, text: Option<&str>) -> Row {
let mut row = ctx.slots.empty_row();
row[ctx.slots.slot("g").unwrap()] = val(group);
row[ctx.slots.slot("v").unwrap()] = number.and_then(val);
row[ctx.slots.slot("text").unwrap()] = text.and_then(val);
row
}
#[test]
fn aggregate_functions_cover_empty_distinct_numeric_and_error_semantics() {
let rete = fixture();
let ctx = context(&rete);
let bound = vec![
row(&ctx, "\"a\"", Some("1"), Some("\"z\"")),
row(&ctx, "\"a\"", Some("2"), Some("\"a\"")),
row(&ctx, "\"a\"", Some("2"), Some("\"a\"")),
];
let mut rows = bound.clone();
rows.push(row(&ctx, "\"a\"", None, None));
assert_eq!(
compute_agg(&ctx, &Agg::CountStar { distinct: false }, &rows),
Some(fmt_num_typed(4.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Count("v".into(), false), &rows),
Some(fmt_num_typed(3.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Count("v".into(), true), &rows),
Some(fmt_num_typed(2.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Count("missing".into(), true), &rows),
Some(fmt_num_typed(0.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Sum("v".into()), &bound),
Some(fmt_num_typed(5.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Avg("v".into()), &bound),
Some(fmt_num_typed(5.0 / 3.0))
);
assert_eq!(compute_agg(&ctx, &Agg::Sum("v".into()), &rows), None);
assert_eq!(compute_agg(&ctx, &Agg::Avg("v".into()), &rows), None);
assert_eq!(compute_agg(&ctx, &Agg::Sum("missing".into()), &rows), None);
assert_eq!(
compute_agg(&ctx, &Agg::Avg("missing".into()), &rows),
Some(fmt_num_typed(0.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Sample("text".into()), &rows),
Some("\"z\"".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Sample("missing".into()), &rows),
None
);
assert_eq!(
compute_agg(
&ctx,
&Agg::GroupConcat("text".into(), "|".into(), false),
&bound
),
Some("\"z|a|a\"".into())
);
assert_eq!(
compute_agg(
&ctx,
&Agg::GroupConcat("text".into(), "|".into(), true),
&bound
),
Some("\"z|a\"".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Min("v".into()), &bound),
Some("1".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Max("v".into()), &bound),
Some("2".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Min("text".into()), &bound),
Some("\"a\"".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Max("text".into()), &bound),
Some("\"z\"".into())
);
for agg in [
Agg::GroupConcat("text".into(), "|".into(), false),
Agg::GroupConcat("text".into(), "|".into(), true),
Agg::GroupConcat("missing".into(), "|".into(), true),
Agg::Min("v".into()),
Agg::Min("text".into()),
Agg::Min("missing".into()),
Agg::Max("missing".into()),
] {
assert_eq!(compute_agg(&ctx, &agg, &rows), None, "{agg:?}");
}
assert_eq!(
compute_agg(&ctx, &Agg::Max("v".into()), &rows),
Some("2".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Max("text".into()), &rows),
Some("\"z\"".into())
);
assert_eq!(
compute_agg(&ctx, &Agg::Avg("v".into()), &[]),
Some(fmt_num_typed(0.0))
);
assert_eq!(
compute_agg(&ctx, &Agg::Sum("missing".into()), &[]),
Some(fmt_num_typed(0.0))
);
assert_eq!(
compute_agg(
&ctx,
&Agg::GroupConcat("text".into(), "|".into(), false),
&[]
),
Some("\"\"".into())
);
assert_eq!(compute_agg(&ctx, &Agg::Max("v".into()), &[]), None);
let invalid = vec![row(&ctx, "\"a\"", Some("\"oops\""), None)];
assert_eq!(compute_agg(&ctx, &Agg::Avg("v".into()), &invalid), None);
assert_eq!(compute_agg(&ctx, &Agg::Sum("v".into()), &invalid), None);
}
#[test]
fn grouping_preserves_keys_materializes_pre_expressions_and_emits_empty_group() {
let rete = fixture();
let ctx = context(&rete);
let rows = vec![
row(&ctx, "\"b\"", Some("2"), None),
row(&ctx, "\"a\"", Some("1"), None),
row(&ctx, "\"a\"", Some("3"), None),
];
let spec = GroupSpec {
by: vec!["g".into()],
aggs: vec![("result".into(), Agg::Sum("pre".into()))],
pre: vec![(
"pre".into(),
FExpr::Arith(
ArithOp::Mul,
Box::new(FExpr::Var("v".into())),
Box::new(FExpr::Const("2".into())),
),
)],
};
let grouped = aggregate(&ctx, rete.default_index(), rows, &spec);
assert_eq!(grouped.len(), 2);
let g = ctx.slots.slot("g").unwrap();
let result = ctx.slots.slot("result").unwrap();
assert_eq!(
&*ctx
.resolver
.str_of(grouped[0][g].as_ref().unwrap())
.unwrap(),
"\"a\""
);
assert_eq!(
ctx.resolver.num(grouped[0][result].as_ref().unwrap()),
Some(8.0)
);
assert_eq!(
&*ctx
.resolver
.str_of(grouped[1][g].as_ref().unwrap())
.unwrap(),
"\"b\""
);
assert_eq!(
ctx.resolver.num(grouped[1][result].as_ref().unwrap()),
Some(4.0)
);
let empty = GroupSpec {
by: vec![],
aggs: vec![("result".into(), Agg::CountStar { distinct: true })],
pre: vec![],
};
let grouped = aggregate(&ctx, rete.default_index(), Vec::new(), &empty);
assert_eq!(grouped.len(), 1);
assert_eq!(
ctx.resolver.num(grouped[0][result].as_ref().unwrap()),
Some(0.0)
);
}
}