Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Sum aggregation returns 0 which all documents have null values for the field #17581

Open
gaobinlong opened this issue Mar 13, 2025 · 2 comments
Labels
bug Something isn't working Search:Aggregations

Comments

@gaobinlong
Copy link
Collaborator

gaobinlong commented Mar 13, 2025

Describe the bug

When performing sum aggregation on a field which doesn't exist in all the documents or all documents have explicit null value for that field, the aggregation result shows 0, which is not as expected, null makes more sense. And you can see that, the avg/max/min aggregation returns null.

Request:

GET test1/_search
{
  "size": 0,
  "aggs": {
    "stats-agg": {
      "stats": {
        "field": "a"
      }
    }
  }
}

result:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "stats-agg": {
      "count": 0,
      "min": null,
      "max": null,
      "avg": null,
      "sum": 0
    }
  }
}

Related component

Search:Aggregations

To Reproduce

  1. DELETE test1
PUT test1/_mapping
{
  "properties":{
    "a":{
      "type":"integer"
    }
  }
}
POST test1/_doc?refresh
{
  "a":null
}
  1. Check the result
GET test1/_search
{
  "size": 0,
  "aggs": {
    "stats-agg": {
      "stats": {
        "field": "a"
      }
    }
  }
}

Expected behavior

Sum aggregation returns null rather than 0 for the case that all documents have null values for the field the aggregation performs on.

Additional Details

No response

@sandeshkr419
Copy link
Contributor

[Search Triage] The sum for an empty set will be 0 - for max, min, avg - you can't have a deterministic value when your set is empty or you don't have any data, but your sum will be initialized as 0 generally.

Why do you think null makes more sense?

@gaobinlong
Copy link
Collaborator Author

Why do you think null makes more sense?

I think 0 is not able to help distinguish the case of all documents have null values for the aggregation field and the case of the actual sum equals to 0. And in most databases like mysql, sqlserver and postgresql, sum null values returns null rather than 0.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Search:Aggregations
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants