elastic / elasticsearch

Free and Open Source, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
69.63k stars 24.64k forks source link

[ESQL] Sum aggregation returns a 500 status code for overflow #110437

Open not-napoleon opened 2 months ago

not-napoleon commented 2 months ago

Elasticsearch Version

main

Installed Plugins

No response

Java Version

bundled

OS Version

na

Problem Description

When scalar addition encounters an integer overflow, it sets the value to null and adds a warning. When the SUM aggregation encounters an integer overflow, it crashes the query and returns a 500. This is also the root cause of https://github.com/elastic/elasticsearch/issues/99575, as the AVG surrogates to SUM and COUNT.

Steps to Reproduce

Create an index with a long field and to documents with MAX_LONG as their value:

PUT http://localhost:9200/test
content-type: application/json

{
  "mappings": {
    "properties": {
      "l": {
        "type": "long" 
      }
    }
  }
}

PUT http://localhost:9200/test/_doc/1
content-type: application/json

{ "l": "9223372036854775807"}

PUT http://localhost:9200/test/_doc/2
content-type: application/json

{ "l": "9223372036854775807"}

Attempt to aggregate the sum of those documents

POST http://localhost:9200/_query?format=txt
content-type: application/json

{
  "query": "FROM test | STATS SUM(l)"
}

Expected Response:

SUM(l)       
-------------------
null

Actual Response:

{
  "error": {
    "root_cause": [
      {
        "type": "arithmetic_exception",
        "reason": "long overflow"
      }
    ],
    "type": "arithmetic_exception",
    "reason": "long overflow"
  },
  "status": 500
}

Logs (if relevant)

No response

elasticsearchmachine commented 2 months ago

Pinging @elastic/es-analytical-engine (Team:Analytics)

not-napoleon commented 2 months ago

Blocked on https://github.com/elastic/elasticsearch/issues/110443