Repository navigation
ESQL avg() stats leads to arithmetic overflow exception on large data #99575
Description
Activity
- addedTeam:QL (Deprecated)Meta label for query languages teamMeta label for query languages team:Analytics/ES|QLAKA ESQLAKA ESQL
on Sep 14, 2023 elasticsearchmachine commented
on Sep 14, 2023 CollaboratorMore actionsPinging @elastic/es-ql (Team:QL)
elasticsearchmachine commented
on Sep 14, 2023 CollaboratorMore actionsPinging @elastic/elasticsearch-esql (:Query Languages/ES|QL)
- addedTeam:AnalyticsMeta label for analytical engine team (ESQL/Aggs/Geo)Meta label for analytical engine team (ESQL/Aggs/Geo)
on Jan 2, 2024 - removedTeam:QL (Deprecated)Meta label for query languages teamMeta label for query languages team
on Jan 2, 2024 elasticsearchmachine commented
on Jan 2, 2024 CollaboratorMore actionsPinging @elastic/es-analytics-geo (Team:Analytics)
elasticsearchmachine commented
on Jul 2, 2024 CollaboratorMore actionsPinging @elastic/es-analytical-engine (Team:Analytics)
@alex-spies Raised some questions about if we should always cast to a double before summing. Doing it isn't terribly hard, although it does create some headaches in the optimizer tests, but I agree it isn't clear that it's the best way forward. I've also opened #110437 to capture the 500 error on overflow in aggregations, which we should fix regardless of what we do here.
I think it's worth discussing if we want to have this cast or not, and if we decide we don't, close this in favor of #110437. I'm labeling this
team-discussto note that.another option: don't use a surrogate, but instead have an intermediate state:
@IntermediateState(name = "mean", type = "DOUBLE"), @IntermediateState(name = "count", type = "LONG")comparable to StdDev.
- addedbackport-neededIndicate whether a gh issue needs to backport to any active release.Indicate whether a gh issue needs to backport to any active release.and removed
on Apr 22, 2026 - added a commit that references this issue
on May 29, 2026 - added a commit that references this issue
on May 29, 2026
In the
nyc_taxisdataset used in benchmarking, I’m getting an arithmetic exception when calculating avg of many longs.The query:
And the result:
In ESQL arithmetic operations can lead to numerical overflows, and the principle is to return
nulland add a warning. We could do that in this case. However, since theavgfunction will return a double anyway, we could cast the longs to doubles up-front. Right nowavg()is implemented as asum()/count()and the division will return a double, we could instead change this tosum(to_double())/count()and get the conversion to double done one step earlier.As a test, the following workaround works:
So, three options here:
nulland warnings for aggregations that have numerical overflowavg()->sum(to_double())/count()sum(), requiring support for an additional type (big integer)