Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Aggregation query with nested field returns garbage #155

Open
galkk opened this issue Aug 19, 2019 · 0 comments
Open

Aggregation query with nested field returns garbage #155

galkk opened this issue Aug 19, 2019 · 0 comments
Labels

Comments

@galkk
Copy link
Contributor

galkk commented Aug 19, 2019

Aggregation query returns very strange results.
Expected results: either clear error message that the query is not supported or correct results

GET /_opendistro/_sql
{
  "query": """
    SELECT e.name, COUNT(p.title)
    FROM employess_with_missing e, e.projects p
  """
}

returns

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "projects.title@NESTED" : {
      "doc_count" : 3,
      "COUNT(nested=com.amazon.opendistroforelasticsearch.sql.parser.NestedType@1f3b445b)" : {
        "value" : 0
      }
    }
  }
}

Expected result:
Bob Smith: 0
Ian Smith: 0
Joh Smith: 0
Susan: 1
Jane: 1

The results with GROUP BY do not make sense as well, although given that we work with nested fields then I'm not sure that we should have aggregation there, so it's debatable

GET /_opendistro/_sql
{
  "query": """
    SELECT e.name, COUNT(p.title)
    FROM employess_with_missing e, e.projects p
    GROUP BY e.name
  """
}

Test data

DELETE employess_with_missing

PUT employess_with_missing
{
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "projects": {
          "type": "nested"
        }
      }
    }
}

GET /employess_with_missing

PUT /employess_with_missing/_doc/1?pretty
{
"name": "Bob Smith"
}

PUT /employess_with_missing/_doc/2?pretty
{
"name": "Ian Smith", "title": null, "age": null,
 "projects" :null
}


PUT /employess_with_missing/_doc/3?pretty
{
"name": "John Smith", "title": null, "age": null,
 "projects" : []
}

PUT /employess_with_missing/_doc/4?pretty
{
"name": "Susan Smith", "title": "Dev Mgr", "age": 33,
 "projects" : [
   {
      "name": "War games #1",
      "start_date": "2017-05-01"
   }
   ]
}

PUT /employess_with_missing/_doc/5?pretty
{
"name": "Jane Smith", "title": "Software Eng 2", "age": 25,
 "projects" : [
   {
      "name": "War games #1",
      "start_date": "2017-05-01",
      "end_date": "2018-06-20"
   },
   {
      "name": "Peace games #1",
      "start_date": "2018-06-21"
   }   
   ]
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants