Skip to content

[ES|QL] Aggregation on conversion functions may return wrong results #127200

@fang-xing-esql

Description

@fang-xing-esql

It happens when the conversion functions reference multi/union typed fields that come from multiple indices, this happens to the other conversion functions too, not specific for ::date, ::date_nanos. Aggregation on single index with conversion functions work as expected.

Here are the steps to reproduce this issue.

curl -u elastic:password -H "Content-Type: application/json" "127.0.0.1:9200/test1" -XPUT -d '{
  "mappings": {
        "properties": {
          "millis": { "type": "date" },
          "nanos": { "type": "date_nanos"},
          "num": { "type": "long" }
        }
  }
}'

curl -u elastic:password -H "Content-Type: application/json" "127.0.0.1:9200/test2" -XPUT -d '{
  "settings": {"index": {"mode": "lookup"}},
  "mappings": {
        "properties": {
          "millis": { "type": "date_nanos" },
          "nanos": { "type": "date"},
          "num": { "type": "long" }
        }
  }
}'

curl -X PUT "localhost:9200/test1/_bulk?refresh&pretty" -H 'Content-Type: application/json' -d'
{"index": {}}
{"millis":"2023-10-23T13:55:01.543Z","nanos":"2023-10-23T13:55:01.543123456Z","num":1698069301543123456}
{"index": {}}
{"millis":"2023-10-23T13:55:01.543Z","nanos":"2023-10-23T12:55:01.543123456Z","num":1698069301543123456}
{"index": {}}
{"millis":"1999-10-23T12:15:03.360Z","nanos":["2023-03-23T12:15:03.360103847Z", "2023-02-23T13:33:34.937193000Z", "2023-01-23T13:55:01.543123456Z"],"num":0}
'

curl -X PUT "localhost:9200/test2/_bulk?refresh&pretty" -H 'Content-Type: application/json' -d'
{"index": {}}
{"millis":"2023-10-23T13:55:01.543123456Z","nanos":"2023-10-23T13:55:01.543Z","num":1698069301543123456}
{"index": {}}
{"millis":"1999-10-23T12:15:03.360103847Z","nanos":["2023-03-23T12:15:03.360Z", "2023-02-23T13:33:34.937Z", "2023-01-23T13:55:01.543Z"],"num":0}
'

+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{ 
  "query": "from test* | stats count(millis::date)"
}
'
count(millis::date)
-------------------
0                  ===> WRONG RESULTS
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{ 
  "query": "from test* | stats count(millis::date_nanos)"
}
'
count(millis::date_nanos)
-------------------------
0                        ===> WRONG RESULTS
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test1 | stats count(millis)"
}
'
 count(millis) 
---------------
3              
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test1 | stats count(millis::date)"
}
'
count(millis::date)
-------------------
3                  
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test1 | stats count(millis::date_nanos)"
}
'
count(millis::date_nanos)
-------------------------
3                        
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test2 | stats count(millis)"
}
'
 count(millis) 
---------------
2              
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test2 | stats count(millis::date)"
}
'
count(millis::date)
-------------------
2                  
+ curl -u elastic:password -X POST 'localhost:9200/_query?format=txt&pretty' -H 'Content-Type: application/json' '-d
{
  "query": "from test2 | stats count(millis::date_nanos)"
}
'
count(millis::date_nanos)
-------------------------
2   

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions