[SERVER-31776] $avg pipeline accumulator may overflow Created: 31/Oct/17  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Simon Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: qexec-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Execution
Sprint: Query 2020-08-24
Participants:

 Description   

The $avg pipeline group accumulator computes the average by summing all elements then divide by the count.

src/mongo/db/pipeline/accumulator_avg.cpp :

return Value(_nonDecimalTotal.getDouble() / static_cast<double>(_count));

This may lead to an overflow in the sum for large number of records or large numbers.

To be evaluated for performance reasons : using a stream-average to compute the average.

count++
avg += (x - avg) / count;



 Comments   
Comment by Ramon Fernandez Marina [ 31/Oct/17 ]

Thanks for your detailed report Uyttendaele, I've sent it to the Query team for investigation.

Generated at Thu Feb 08 04:28:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.