hasura / hasura/graphql-engine
cannot prioritize other columns in `order_by` when `distinct_on` is present
- Dominant language
- TypeScript
- Stars
- 32.1k
- Forks
- 3k
- PR merge metrics
- PR metrics pending
Description
### Version Information
Server Version: 2.40.2
### Environment
OSS
### What is the current behaviour?
When `distinct_on` is provided, the `order_by` conditions do not effect the results. Only the `distinct_on` is respected in the `order_by`, any conditions after are treated as tiebreakers.
### What is the expected behaviour?
I had expected there to be a way to order by other columns other than the `distinct_on` column.
### How to reproduce the issue?
1. Given you have a table of elected official terms, with elected_official_id (text), and end_at (timestamp without tz). Form a query that has both distinct_on and order_by
```graphql
query GetRecentElectedOfficials {
elected_official_terms(
limit: 10,
order_by: [
{elected_official_id: desc},
{end_at: desc}
],
distinct_on: elected_official_id
) {
elected_official_id
end_at
}
}
```
2. Notice the results are not in order by end_at, sorting by elected_official_id takes priority.
```json
{
"data": {
"elected_official_terms": [
{
"elected_official_id": "Z000018",
"end_at": "2025-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000017",
"end_at": "2023-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000016",
"end_at": "1975-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000014",
"end_at": "1987-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000013",
"end_at": "1987-03-06T00:00:00+00:00"
},
{
"elected_official_id": "Z000012",
"end_at": "1859-03-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000011",
"end_at": "1936-08-07T00:00:00+00:00"
},
{
"elected_official_id": "Z000010",
"end_at": "1975-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000009",
"end_at": "1949-01-03T00:00:00+00:00"
},
{
"elected_official_id": "Z000008",
"end_at": "1997-01-03T00:00:00+00:00"
}
]
}
}
```
> If I remove the `distinct_on` and the `elected_official_id` from `order_by`, then the results are ordered as expected.
> ```graphql
> query GetEOsForPlace {
> elected_official_terms(
> limit: 10,
> order_by: [
> {end_at: desc}
> ],
> ) {
> elected_official_id
> end_at
> }
> }
> ```
### Screenshots or Screencast

### Any possible solutions/workarounds you're aware of?
Nope
### Keywords
`distinct_on`
`order_by`
sorting
Contributor guide
Assessment
This issue has not been assessed yet.