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

order by is not working as expected when more than one group by is used #674

Closed
dheerajravindranath opened this issue Aug 10, 2020 · 3 comments
Labels
bug Something isn't working SQL

Comments

@dheerajravindranath
Copy link

{"query": "select id, sum(marks) marks from mark_tables group by id order by marks desc"} This is working as expected! But when we give more than one column in group by is not working fine! {"query": "select id, name, sum(marks) marks from mark_tables group by id, name order by marks desc"}. Please, give me some suggestions on this. Is there any workaround for this?

@penghuo
Copy link
Contributor

penghuo commented Aug 12, 2020

Thanks for reporting the issue.

Could you provide some test data for debuging? Have you try to explain the query by using the explain API. It will help to know what is DSL of this SQL.

@penghuo penghuo added the bug Something isn't working label Aug 13, 2020
@dai-chen
Copy link
Member

dai-chen commented Sep 1, 2020

It seems both Term Aggregation and Composite Aggregation API can only support sort inside bucket.

One workaround is to adjust the aggregation order based on order by clause:

SELECT OriginCountry, OriginCityName FROM flights GROUP BY OriginCountry, OriginCityName:
{
  "size" : 0,
  "_source" : false,
  "stored_fields" : "_none_",
  "aggregations" : {
    "groupby" : {
      "composite" : {
        "size" : 1000,
        "sources" : [
          {
            "da66945c" : {
              "terms" : {
                "field" : "OriginCountry.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          },
          {
            "1af15150" : {
              "terms" : {
                "field" : "OriginCityName.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          }
        ]
      }
    }
  }
}

SELECT OriginCountry, OriginCityName FROM flights GROUP BY OriginCountry, OriginCityName ORDER BY OriginCityName DESC
{
  "size" : 0,
  "_source" : false,
  "stored_fields" : "_none_",
  "aggregations" : {
    "groupby" : {
      "composite" : {
        "size" : 1000,
        "sources" : [
          {
            "1af15150" : {
              "terms" : {
                "field" : "OriginCityName.keyword",
                "missing_bucket" : true,
                "order" : "desc"
              }
            }
          },
          {
            "da66945c" : {
              "terms" : {
                "field" : "OriginCountry.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          }
        ]
      }
    }
  }
}

@dai-chen dai-chen added the SQL label Sep 1, 2020
dai-chen added a commit to dai-chen/sql that referenced this issue Oct 15, 2020
dai-chen added a commit that referenced this issue Oct 20, 2020
* Change grammar and ast builder to support order by

* Add UT for order by function

* Pass jacoco for sql

* Fix order by function issue

* Improve test framework to support order by comparison

* Add more comparison tests

* Add test for issue #123

* Add test for issue #674

* Avoid sort again when explain diff for order by query

* Replace aggregator in order by clause

* Skip date_format test

* Add doctest

* Fix doctest format

* Address PR comments
@dai-chen
Copy link
Member

The issue has been fixed in new query engine (which is for now experimental and disabled by default). You can enable it to preview by changing the plugin setting: https://github.com/opendistro-for-elasticsearch/sql/blob/master/docs/user/admin/settings.rst#opendistro-sql-engine-new-enabled. Thanks!

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

No branches or pull requests

3 participants