use super::ast::{AggregateExpr, AggregateOp};
use crate::storage::schema::Value;
#[cfg(test)]
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(test)]
pub(crate) static MATERIALIZED_COUNT: AtomicUsize = AtomicUsize::new(0);
#[cfg(test)]
pub(crate) fn note_materialized() {
MATERIALIZED_COUNT.fetch_add(1, Ordering::Relaxed);
}
#[cfg(not(test))]
#[inline]
pub(crate) fn note_materialized() {}
enum Slot {
CountStar { count: u64 },
CountColumn { count: u64 },
Sum { sum: f64, seen_any: bool },
Avg { sum: f64, count: u64 },
Min { current: Option<Value> },
Max { current: Option<Value> },
}
impl Slot {
fn for_op(op: AggregateOp) -> Self {
match op {
AggregateOp::CountStar => Slot::CountStar { count: 0 },
AggregateOp::CountColumn => Slot::CountColumn { count: 0 },
AggregateOp::Sum => Slot::Sum {
sum: 0.0,
seen_any: false,
},
AggregateOp::Avg => Slot::Avg { sum: 0.0, count: 0 },
AggregateOp::Min => Slot::Min { current: None },
AggregateOp::Max => Slot::Max { current: None },
}
}
}
pub(super) struct GroupAccumulator {
slots: Vec<Slot>,
}
impl GroupAccumulator {
pub(super) fn new(aggregates: &[AggregateExpr]) -> Self {
Self {
slots: aggregates.iter().map(|a| Slot::for_op(a.op)).collect(),
}
}
pub(super) fn accumulate(&mut self, aggregates: &[AggregateExpr], inputs: &[Value]) {
for (slot, expr) in self.slots.iter_mut().zip(aggregates.iter()) {
match slot {
Slot::CountStar { count } => {
*count += 1;
}
Slot::CountColumn { count } => {
if let Some(v) = inputs.get(expr.input_index) {
if !matches!(v, Value::Null) {
*count += 1;
}
}
}
Slot::Sum { sum, seen_any } => {
if let Some(v) = inputs.get(expr.input_index) {
if let Some(n) = numeric_value(v) {
*sum += n;
*seen_any = true;
}
}
}
Slot::Avg { sum, count } => {
if let Some(v) = inputs.get(expr.input_index) {
if let Some(n) = numeric_value(v) {
*sum += n;
*count += 1;
}
}
}
Slot::Min { current } => {
if let Some(v) = inputs.get(expr.input_index) {
update_extreme(current, v, std::cmp::Ordering::Less);
}
}
Slot::Max { current } => {
if let Some(v) = inputs.get(expr.input_index) {
update_extreme(current, v, std::cmp::Ordering::Greater);
}
}
}
}
}
pub(super) fn finalize(self) -> Vec<Value> {
note_materialized();
self.slots
.into_iter()
.map(|slot| match slot {
Slot::CountStar { count } | Slot::CountColumn { count } => {
Value::Integer(count as i64)
}
Slot::Sum { sum, seen_any } => {
if seen_any {
sum_f64_to_value(sum)
} else {
Value::Null
}
}
Slot::Avg { sum, count } => {
if count == 0 {
Value::Null
} else {
Value::Float(sum / count as f64)
}
}
Slot::Min { current } | Slot::Max { current } => current.unwrap_or(Value::Null),
})
.collect()
}
}
fn sum_f64_to_value(f: f64) -> Value {
if f.fract() == 0.0 && f >= i64::MIN as f64 && f <= i64::MAX as f64 {
Value::Integer(f as i64)
} else {
Value::Float(f)
}
}
fn numeric_value(v: &Value) -> Option<f64> {
match v {
Value::Integer(i) => Some(*i as f64),
Value::UnsignedInteger(u) => Some(*u as f64),
Value::Float(f) if f.is_finite() => Some(*f),
Value::Decimal(d) => Some(crate::storage::schema::decimal_to_f64(*d)),
Value::Boolean(b) => Some(if *b { 1.0 } else { 0.0 }),
_ => None,
}
}
fn update_extreme(current: &mut Option<Value>, candidate: &Value, target: std::cmp::Ordering) {
if matches!(candidate, Value::Null) {
return;
}
let Some(cand_key) = crate::storage::schema::value_to_canonical_key(candidate) else {
return;
};
match current {
None => {
*current = Some(candidate.clone());
}
Some(cur) => {
let Some(cur_key) = crate::storage::schema::value_to_canonical_key(cur) else {
*current = Some(candidate.clone());
return;
};
if cur_key.family() != cand_key.family() {
return;
}
if cand_key.cmp(&cur_key) == target {
*current = Some(candidate.clone());
}
}
}
}
#[cfg(test)]
mod tests {
use super::numeric_value;
use crate::runtime::query_exec::aggregate::value_to_f64;
use crate::storage::schema::Value;
#[test]
fn decimal_sum_agrees_with_the_legacy_aggregate_path() {
let cases = [
Value::Decimal(387_600), Value::Decimal(-771_500), Value::Decimal(1_234_567), Value::Decimal(0),
Value::Decimal(i64::MAX),
Value::Decimal(i64::MIN),
Value::Integer(42),
Value::UnsignedInteger(7),
Value::Float(1.5),
Value::Null,
];
for value in cases {
assert_eq!(
numeric_value(&value),
value_to_f64(&value),
"planner and legacy aggregate casts disagree on {value:?}"
);
}
}
#[test]
fn planner_and_legacy_diverge_on_bigint_boolean_and_nonfinite_float() {
assert_eq!(numeric_value(&Value::BigInt(9)), None);
assert_eq!(value_to_f64(&Value::BigInt(9)), Some(9.0));
assert_eq!(numeric_value(&Value::Boolean(true)), Some(1.0));
assert_eq!(value_to_f64(&Value::Boolean(true)), None);
assert_eq!(numeric_value(&Value::Float(f64::NAN)), None);
assert!(value_to_f64(&Value::Float(f64::NAN)).is_some_and(f64::is_nan));
}
#[test]
fn decimal_aggregation_matches_the_rendered_value() {
assert_eq!(numeric_value(&Value::Decimal(1_234_567)), Some(123.4567));
}
}