apache / apache/datafusion

Improve error message for invalid aggregate queries

Open
#12,006 4 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?

This query is invalid because its `order by` clause has an aggregated column
```
select min(v2) from t1 group by v1 order by v2;
```

However its error message is confusing (run in datafusion-cli:)
```
DataFusion CLI v41.0.0
> create table t1(v1 int, v2 int);
0 row(s) fetched.
Elapsed 0.073 seconds.

> select min(v2) from t1 group by v1 order by v2;
Schema error: No field named t1.v2. Valid fields are "min(t1.v2)".
```

Reference for duckdb's error message which is more understandable
```
D select min(v2) from t1 group by v1 order by v2;
Binder Error: column "v2" must appear in the GROUP BY clause or must be part of an aggregate function.
Either add it to the GROUP BY list, or use "ANY_VALUE(v2)" if the exact value of "v2" is not important.
LINE 1: ...t min(v2) from t1 group by v1 order by v2;
```

### Describe the solution you'd like

Use error message similar to duckdb's

Note use different aggregate function (as suggested by DuckDB's error message to use `ANY_VALUE`) is not supported for DataFusion (yet), we should change this part of the error message
```
-- Allowed
select min(v2) from t1 group by v1 order by v1;
-- Allowed
select min(v2) from t1 group by v1 order by min(v2);
-- Not supported yet by DataFusion, but widely supported by other systems
select min(v2) from t1 group by v1 order by first_value(v2);
```

### Describe alternatives you've considered

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the query in datafusion-cli and inspect how the invalid aggregate query produces its current schema error. Compare the result with the requested DuckDB-style guidance, accounting for the listed DataFusion limitation around ANY_VALUE and first_value. Add coverage for the allowed and invalid ORDER BY examples, with completion shown by a clearer error message.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.