From f0ffff3c8e3e020f6a55bf526e2cda6f28e76c3a Mon Sep 17 00:00:00 2001 From: FHaggs Date: Fri, 25 Jul 2025 13:25:25 -0300 Subject: [PATCH] Modify AggContext to support the kahan algorithm --- core/types.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/types.rs b/core/types.rs index 5cbe08152..f0cf48b8b 100644 --- a/core/types.rs +++ b/core/types.rs @@ -259,6 +259,13 @@ impl Value { _ => panic!("as_blob must be called only for Value::Blob"), } } + pub fn as_float(&self) -> f64 { + match self { + Value::Float(f) => *f, + Value::Integer(i) => *i as f64, + _ => panic!("as_float must be called only for Value::Float or Value::Integer"), + } + } pub fn from_text(text: &str) -> Self { Value::Text(Text::new(text)) @@ -498,7 +505,7 @@ impl Value { #[derive(Debug, Clone, PartialEq)] pub enum AggContext { Avg(Value, Value), // acc and count - Sum(Value), + Sum(Value, f64), // Error term for Kahan-Babushka-Neumaier summation Count(Value), Max(Option), Min(Option), @@ -522,7 +529,7 @@ impl AggContext { pub fn final_value(&self) -> &Value { match self { Self::Avg(acc, _count) => acc, - Self::Sum(acc) => acc, + Self::Sum(acc, _) => acc, Self::Count(count) => count, Self::Max(max) => max.as_ref().unwrap_or(&NULL), Self::Min(min) => min.as_ref().unwrap_or(&NULL), @@ -596,7 +603,7 @@ impl PartialOrd for AggContext { fn partial_cmp(&self, other: &AggContext) -> Option { match (self, other) { (Self::Avg(a, _), Self::Avg(b, _)) => a.partial_cmp(b), - (Self::Sum(a), Self::Sum(b)) => a.partial_cmp(b), + (Self::Sum(a, _), Self::Sum(b, _)) => a.partial_cmp(b), (Self::Count(a), Self::Count(b)) => a.partial_cmp(b), (Self::Max(a), Self::Max(b)) => a.partial_cmp(b), (Self::Min(a), Self::Min(b)) => a.partial_cmp(b),