use super::float_sum::{Binary32Sum, Binary64Sum};
use super::{
super::Plan, Aggregate, Completed, GroupOrder, GroupSummaries, GroupSummary, GroupValue, Query,
Reducer, SummaryMeasure,
};
use crate::{
Decimal, Entity, EntitySchema, Error, FieldType, IndexValue, TypeBody, TypeSchema, Value,
ValueSchema, codec::Encoder,
};
use ethnum::{I256, U256};
use ic_storage::Memory;
impl<E: Entity, M: Memory> Query<'_, E, M> {
pub fn sum<R: Value>(self) -> Result<Aggregate<Option<R>>, Error> {
observe!(query, {
let (plan, reducer) = self.numeric_plan::<R>(None, Operation::Sum)?;
self.scalar_value(plan, reducer)
})
}
pub fn group_sum<K: Value, R: Value>(self) -> Result<Aggregate<Vec<GroupValue<K, R>>>, Error> {
observe!(query, {
let (plan, reducer) = self.numeric_plan::<R>(Some(K::definition()), Operation::Sum)?;
self.group_values(plan, reducer)
})
}
pub fn avg<R: Value>(self) -> Result<Aggregate<Option<R>>, Error> {
observe!(query, {
let (plan, reducer) = self.numeric_plan::<R>(None, Operation::Average)?;
self.scalar_value(plan, reducer)
})
}
pub fn group_avg<K: Value, R: Value>(self) -> Result<Aggregate<Vec<GroupValue<K, R>>>, Error> {
observe!(query, {
let (plan, reducer) =
self.numeric_plan::<R>(Some(K::definition()), Operation::Average)?;
self.group_values(plan, reducer)
})
}
pub fn group_summary<K: Value, S: Value, A: Value>(
self,
) -> Result<GroupSummaries<K, S, A>, Error> {
observe!(query, {
let (mut plan, initial, sum) = self.summary_plan::<K, S, A>()?;
super::super::order::select_unique_aggregate_path(E::SCHEMA, &self.request, &mut plan)?;
let (groups, page) = self.grouped(&plan, initial, Some(sum))?;
let (value, output_bytes) = super::group_output(
groups,
self.request.rows,
self.request.bytes,
decode_summary,
)?;
Ok(Aggregate {
value,
output_bytes,
examined: page.examined,
fetched: page.fetched,
})
})
}
pub(super) fn summary_plan<K: Value, S: Value, A: Value>(
&self,
) -> Result<(Plan<'static>, Reducer, ValueSchema), Error> {
let request = &self.request.groups;
let order = request.order.unwrap_or_default();
if !matches!(
order,
GroupOrder::Key | GroupOrder::KeyDescending | GroupOrder::Summary(..)
) {
return Err(Error::InvalidQuery);
}
let (plan, initial) = self.numeric_plan::<A>(Some(K::definition()), Operation::Average)?;
let sum = NumericAggregate::admit(E::SCHEMA, S::definition(), Operation::Sum)?;
let Reducer::Numeric(average) = &initial else {
return Err(Error::Corrupt);
};
if sum.position != average.position {
return Err(Error::InvalidQuery);
}
Ok((plan, initial, sum.output))
}
pub(super) fn numeric_plan<R: Value>(
&self,
group: Option<&TypeSchema>,
operation: Operation,
) -> Result<(Plan<'static>, Reducer), Error> {
let mut plan = self.aggregate_plan(group, group.is_some())?;
let state = NumericAggregate::admit(E::SCHEMA, R::definition(), operation)?;
plan.selected |= 1 << state.position;
Ok((plan, Reducer::Numeric(state)))
}
}
pub(super) fn decode_summary<K: Value, S: Value, A: Value>(
key: &[u8],
result: Completed,
) -> Result<GroupSummary<K, S, A>, Error> {
let Completed::Summary {
count,
sum,
average,
..
} = result
else {
return Err(Error::Corrupt);
};
Ok(GroupSummary {
key: super::decode_record(key)?,
count,
sum: sum.as_deref().map(super::decode_record).transpose()?,
average: average.as_deref().map(super::decode_record).transpose()?,
})
}
#[derive(Clone, Copy)]
#[repr(u8)]
pub(super) enum Operation {
Sum = 1,
Average = 2,
}
#[derive(Clone)]
pub(in crate::store::query) struct NumericAggregate {
pub(super) position: usize,
source_scale: u32,
output: ValueSchema,
total: Total,
count: u64,
pub(super) operation: Operation,
}
#[derive(Clone)]
enum Total {
Float32(Box<Binary32Sum>),
Float64(Box<Binary64Sum>),
Signed(I256),
Unsigned(U256),
}
impl Total {
fn integer_zero(signed: bool) -> Self {
if signed {
Self::Signed(I256::ZERO)
} else {
Self::Unsigned(U256::ZERO)
}
}
}
type EncodedValue = (Vec<u8>, Vec<u8>);
impl NumericAggregate {
fn admit(
entity: &EntitySchema,
output: &TypeSchema,
operation: Operation,
) -> Result<Self, Error> {
let TypeBody::Record([result]) = output.body else {
return Err(Error::InvalidQuery);
};
let position = entity
.field_position(result.name)
.ok_or(Error::InvalidQuery)?;
let source = &entity.fields[position];
if source.optional != result.optional {
return Err(Error::InvalidQuery);
}
let input = crate::key::operand_type(source.value);
if matches!(result.value, FieldType::Named(_)) {
super::admit_projection(entity, output)?;
}
let result_type = crate::key::operand_type(result.value);
let source_scale = match input {
FieldType::Decimal { scale } => scale,
_ => 0,
};
let total = match (operation, input, result_type) {
(_, FieldType::Float32, FieldType::Float32) => Total::Float32(Box::default()),
(_, FieldType::Float64, FieldType::Float64) => Total::Float64(Box::default()),
(
Operation::Sum,
FieldType::Decimal { scale: from },
FieldType::Decimal { scale: to },
) if from == to => Total::Signed(I256::ZERO),
(
Operation::Average,
FieldType::Decimal { .. },
FieldType::Decimal { scale: 0..=28 },
) => Total::Signed(I256::ZERO),
(Operation::Average, source, FieldType::Decimal { scale: 0..=28 }) => {
Total::integer_zero(integer(source).ok_or(Error::InvalidQuery)?.0)
}
(Operation::Average, _, _) => return Err(Error::InvalidQuery),
(Operation::Sum, source, result) => {
let (signed, bits) = integer(source).ok_or(Error::InvalidQuery)?;
let (result_signed, result_bits) = integer(result).ok_or(Error::InvalidQuery)?;
if signed != result_signed || bits > result_bits {
return Err(Error::InvalidQuery);
}
Total::integer_zero(signed)
}
};
Ok(Self {
position,
source_scale,
output: ValueSchema {
value: result.value,
optional: result.optional,
},
total,
count: 0,
operation,
})
}
pub(super) fn push(&mut self, values: &[IndexValue<'_>]) -> Result<(), Error> {
let value = values[self.position];
if value == IndexValue::Absent {
return Ok(());
}
match &mut self.total {
Total::Float32(total) => {
let IndexValue::Float32(value) = value else {
return Err(Error::Corrupt);
};
total.add(value);
}
Total::Float64(total) => {
let IndexValue::Float64(value) = value else {
return Err(Error::Corrupt);
};
total.add(value);
}
Total::Signed(total) => {
let value = match value {
IndexValue::Int8(n) => i128::from(n),
IndexValue::Int16(n) => i128::from(n),
IndexValue::Int32(n) => i128::from(n),
IndexValue::Int64(n) => i128::from(n),
IndexValue::Int128(n) => n,
IndexValue::Decimal(n) => n
.coefficient_at_scale(self.source_scale)
.ok_or(Error::Corrupt)?,
_ => return Err(Error::Corrupt),
};
*total = total
.checked_add(I256::from(value))
.ok_or(Error::ArithmeticOverflow)?;
}
Total::Unsigned(total) => {
let value = match value {
IndexValue::Nat8(n) => U256::from(n),
IndexValue::Nat16(n) => U256::from(n),
IndexValue::Nat32(n) => U256::from(n),
IndexValue::Nat64(n) => U256::from(n),
IndexValue::Nat128(n) => U256::from(n),
IndexValue::U256(n) => n.into(),
_ => return Err(Error::Corrupt),
};
*total = total.checked_add(value).ok_or(Error::ArithmeticOverflow)?;
}
}
self.count = self.count.checked_add(1).ok_or(Error::ArithmeticOverflow)?;
Ok(())
}
pub(super) fn with_checkpoint<T>(&self, write: impl FnOnce(u64, &[u8]) -> T) -> T {
match &self.total {
Total::Signed(n) => write(self.count, &n.to_be_bytes()),
Total::Unsigned(n) => write(self.count, &n.to_be_bytes()),
Total::Float32(sum) => write(self.count, sum.checkpoint().as_flattened()),
Total::Float64(sum) => write(self.count, sum.checkpoint().as_flattened()),
}
}
pub(super) fn restore(&mut self, count: u64, bytes: &[u8]) -> Result<(), Error> {
match &mut self.total {
Total::Signed(n) => {
*n = I256::from_be_bytes(bytes.try_into().map_err(|_| Error::Corrupt)?)
}
Total::Unsigned(n) => {
*n = U256::from_be_bytes(bytes.try_into().map_err(|_| Error::Corrupt)?)
}
Total::Float32(sum) => sum.restore(count, bytes)?,
Total::Float64(sum) => sum.restore(count, bytes)?,
}
self.count = count;
Ok(())
}
pub(super) fn total_bytes(&self) -> usize {
match self.total {
Total::Float32(_) => Binary32Sum::BYTES,
Total::Float64(_) => Binary64Sum::BYTES,
Total::Signed(_) | Total::Unsigned(_) => 32,
}
}
pub(super) fn retained_bytes(&self) -> usize {
self.total_bytes()
+ if matches!(self.operation, Operation::Average) {
8
} else {
0
}
}
pub(super) fn output(&self) -> ValueSchema {
self.output
}
pub(super) fn finish_summary(
self,
output: ValueSchema,
selection: &super::Selection,
) -> Result<(Completed, bool), Error> {
let (sum_key, sum) = self
.encode_result(
output,
Operation::Sum,
selection.needs_summary_key(SummaryMeasure::Sum),
)?
.unzip();
let (avg_key, average) = self
.encode_result(
self.output,
Operation::Average,
selection.needs_summary_key(SummaryMeasure::Average),
)?
.unzip();
let count_key = self.count.to_be_bytes();
let accepted = selection.accepts(|measure| match measure {
Some(SummaryMeasure::Count) => Some(&count_key),
Some(SummaryMeasure::Sum) => sum_key.as_deref(),
Some(SummaryMeasure::Average) => avg_key.as_deref(),
None => None,
});
let sort_key = match selection.order {
GroupOrder::Summary(SummaryMeasure::Sum, _) => sum_key,
GroupOrder::Summary(SummaryMeasure::Average, _) => avg_key,
_ => None,
};
Ok((
Completed::Summary {
count: self.count,
sum: sum.map(Vec::into_boxed_slice),
average: average.map(Vec::into_boxed_slice),
sort_key: sort_key.map(Vec::into_boxed_slice),
},
accepted,
))
}
pub(super) fn finish(self, needs_key: bool) -> Result<Completed, Error> {
self.encode_result(self.output, self.operation, needs_key)
.map(Completed::Value)
}
fn encode_result(
&self,
schema: ValueSchema,
operation: Operation,
needs_key: bool,
) -> Result<Option<EncodedValue>, Error> {
if self.count == 0 {
return Ok(None);
}
let result_type = crate::key::operand_type(schema.value);
let rounded;
let total =
if let (Operation::Average, FieldType::Decimal { scale }) = (operation, result_type) {
let numerator = match self.total {
Total::Float32(_) | Total::Float64(_) => return Err(Error::Corrupt),
Total::Signed(n) => n,
Total::Unsigned(n) => n.try_into().map_err(|_| Error::ArithmeticOverflow)?,
};
rounded = Total::Signed(rounded_average(
numerator,
self.count,
self.source_scale,
scale,
)?);
&rounded
} else {
&self.total
};
let mut output = Encoder::new();
if schema.optional {
output.boolean(true)?;
}
macro_rules! encode {
($method:ident, $variant:ident, $value:expr) => {{
let value = $value.try_into().map_err(|_| Error::ArithmeticOverflow)?;
output.$method(value)?;
IndexValue::$variant(value)
}};
}
let divisor = match operation {
Operation::Sum => 1,
Operation::Average => self.count,
};
let value = match (total, result_type) {
(Total::Float32(sum), FieldType::Float32) => {
let value = sum.finish(divisor)?;
output.float32(value)?;
IndexValue::Float32(value)
}
(Total::Float64(sum), FieldType::Float64) => {
let value = sum.finish(divisor)?;
output.float64(value)?;
IndexValue::Float64(value)
}
(Total::Signed(n), FieldType::Int8) => encode!(int8, Int8, *n),
(Total::Signed(n), FieldType::Int16) => encode!(int16, Int16, *n),
(Total::Signed(n), FieldType::Int32) => encode!(int32, Int32, *n),
(Total::Signed(n), FieldType::Int64) => encode!(int64, Int64, *n),
(Total::Signed(n), FieldType::Int128) => encode!(int128, Int128, *n),
(Total::Signed(n), FieldType::Decimal { scale }) => {
let coefficient = (*n).try_into().map_err(|_| Error::ArithmeticOverflow)?;
let value = Decimal::try_new(coefficient, scale).ok_or(Error::Corrupt)?;
output.decimal(value, scale)?;
IndexValue::Decimal(value)
}
(Total::Unsigned(n), FieldType::Nat8) => encode!(nat8, Nat8, *n),
(Total::Unsigned(n), FieldType::Nat16) => encode!(nat16, Nat16, *n),
(Total::Unsigned(n), FieldType::Nat32) => encode!(nat32, Nat32, *n),
(Total::Unsigned(n), FieldType::Nat64) => encode!(nat64, Nat64, *n),
(Total::Unsigned(n), FieldType::Nat128) => encode!(nat128, Nat128, *n),
(Total::Unsigned(n), FieldType::U256) => {
let value = (*n).into();
output.u256(value)?;
IndexValue::U256(value)
}
_ => return Err(Error::Corrupt),
};
let mut key = Vec::new();
if needs_key {
value.append_component(schema.optional, &mut key);
}
Ok(Some((key, output.finish())))
}
}
fn rounded_average(
total: I256,
count: u64,
source_scale: u32,
result_scale: u32,
) -> Result<I256, Error> {
let factor = I256::from(10_i128.pow(source_scale.abs_diff(result_scale)));
let (numerator, denominator) = if result_scale >= source_scale {
(
total.checked_mul(factor).ok_or(Error::ArithmeticOverflow)?,
I256::from(count),
)
} else {
(total, I256::from(count) * factor)
};
let quotient = numerator / denominator;
let twice_remainder = (numerator % denominator).abs() * 2;
let round = twice_remainder > denominator
|| (twice_remainder == denominator && quotient % 2 != I256::ZERO);
Ok(if round {
quotient
+ if numerator < I256::ZERO {
I256::MINUS_ONE
} else {
I256::ONE
}
} else {
quotient
})
}
fn integer(value: FieldType) -> Option<(bool, u16)> {
Some(match value {
FieldType::Nat8 => (false, 8),
FieldType::Int8 => (true, 8),
FieldType::Nat16 => (false, 16),
FieldType::Int16 => (true, 16),
FieldType::Nat32 => (false, 32),
FieldType::Int32 => (true, 32),
FieldType::Nat64 => (false, 64),
FieldType::Int64 => (true, 64),
FieldType::Nat128 => (false, 128),
FieldType::Int128 => (true, 128),
FieldType::U256 => (false, 256),
_ => return None,
})
}