$avg pipeline accumulator may overflow

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Query Execution
    • Query 2020-08-24
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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;
      

              Assignee:
              [DO NOT USE] Backlog - Query Execution
              Reporter:
              Simon
              Votes:
              0 Vote for this issue
              Watchers:
              14 Start watching this issue

                Created:
                Updated: