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

ORDER BY doesn't work as expected on cardinality aggregations #378

Open
canberk-oguz opened this issue Mar 12, 2020 · 1 comment
Open

ORDER BY doesn't work as expected on cardinality aggregations #378

canberk-oguz opened this issue Mar 12, 2020 · 1 comment
Labels
bug Something isn't working investigation SQL

Comments

@canberk-oguz
Copy link

canberk-oguz commented Mar 12, 2020

I realized that ORDER BY doesn't work properly on COUNT(DISTINCT x) queries. Instead of adding the given sort field, it adds the _count field as a default sort field.

GET /_opendistro/_sql/_explain
{
  "query": "SELECT count(distinct id) as bal FROM accounts GROUP BY topic_id ORDER BY bal DESC"
}

Incorrect DSL:

{
  "from" : 0,
  "size" : 0,
  "_source" : {
    "includes" : [
      "COUNT"
    ],
    "excludes" : [ ]
  },
  "aggregations" : {
    "topic_id" : {
      "terms" : {
        "field" : "topic_id",
        "size" : 200,
        "min_doc_count" : 1,
        "shard_min_doc_count" : 0,
        "show_term_doc_count_error" : false,
        "order" : [
          {
            "_count" : "desc"
          },
          {
            "_key" : "asc"
          }
        ]
      },
      "aggregations" : {
        "bal" : {
          "cardinality" : {
            "field" : "id"
          }
        }
      }
    }
  }
}

Expected DSL:

{
  "from" : 0,
  "size" : 0,
  "_source" : {
    "includes" : [
      "COUNT"
    ],
    "excludes" : [ ]
  },
  "aggregations" : {
    "topic_id" : {
      "terms" : {
        "field" : "topic_id",
        "size" : 200,
        "min_doc_count" : 1,
        "shard_min_doc_count" : 0,
        "show_term_doc_count_error" : false,
        "order" : [
          {
            "bal" : "desc"
          },
          {
            "_key" : "asc"
          }
        ]
      },
      "aggregations" : {
        "bal" : {
          "cardinality" : {
            "field" : "id"
          }
        }
      }
    }
  }
}
@penghuo
Copy link
Contributor

penghuo commented Mar 12, 2020

Thanks for reporting the issue. Will look into this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working investigation SQL
Projects
None yet
Development

No branches or pull requests

3 participants