apache / apache/datafusion

Support different aggregate expression (outside SELECT list) in ORDER BY list

Open
#12,007 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Is your feature request related to a problem or challenge?

In aggregate query with order-by: order-by list should support expressions with `group by` columns, or aggregated `aggregate` columns, this syntax is widely supported (by DuckDB, postgres and likely many others)

For aggregate function in order by list case, now DataFusion only supports aggregate functions which have appeared in `SELECT` list

See example
```
-- Setup
DataFusion CLI v41.0.0
> create table t1 (groupby1 int, agg1 int, agg2 int);
```
```
-- This is supported
select groupby1, sum(agg1)
from t1
group by groupby1
order by groupby1, sum(agg1);

-- Not supported yet in DataFusion
select groupby1, sum(agg1)
from t1
group by groupby1
order by max(agg2);
```

### Describe the solution you'd like

_No response_

### Describe alternatives you've considered

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the two aggregate ORDER BY examples in DataFusion CLI v41.0.0, then trace how aggregate expressions in ORDER BY are handled and why only aggregates also present in SELECT are supported. Done means the query ordering by max(agg2) succeeds alongside the existing sum(agg1) case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.