From 8c68cf4041d3d7dbac3e92c262a12da28ade9cf5 Mon Sep 17 00:00:00 2001 From: Tpt Date: Sun, 11 Jun 2023 18:15:47 +0200 Subject: [PATCH] Makes SPARQL AVG aggregate function simpler --- lib/src/sparql/eval.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index c3a9b033..a9a6abbd 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -4533,30 +4533,30 @@ impl Accumulator for SumAccumulator { #[derive(Debug, Default)] struct AvgAccumulator { sum: SumAccumulator, - count: CountAccumulator, + count: i64, } impl Accumulator for AvgAccumulator { fn add(&mut self, element: Option) { - self.sum.add(element.clone()); - self.count.add(element); + self.sum.add(element); + self.count += 1; } fn state(&self) -> Option { let sum = self.sum.state()?; - let count = self.count.state()?; - if count == EncodedTerm::from(0) { + if self.count == 0 { Some(0.into()) } else { //TODO: deduplicate? //TODO: duration? - match NumericBinaryOperands::new(sum, count)? { - NumericBinaryOperands::Float(v1, v2) => Some((v1 / v2).into()), - NumericBinaryOperands::Double(v1, v2) => Some((v1 / v2).into()), - NumericBinaryOperands::Integer(v1, v2) => { - Decimal::from(v1).checked_div(v2).map(Into::into) + let count = Integer::from(self.count); + match sum { + EncodedTerm::FloatLiteral(sum) => Some((sum / Float::from(count)).into()), + EncodedTerm::DoubleLiteral(sum) => Some((sum / Double::from(count)).into()), + EncodedTerm::IntegerLiteral(sum) => { + Some(Decimal::from(sum).checked_div(count)?.into()) } - NumericBinaryOperands::Decimal(v1, v2) => v1.checked_div(v2).map(Into::into), + EncodedTerm::DecimalLiteral(sum) => Some(sum.checked_div(count)?.into()), _ => None, } }