apache / apache/datafusion

Distinct on expressions mismatch ORDER BY expressions because of a qualified column

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

Description

### Describe the bug

When planning a query with distinct on, the distinct on expressions must match the order by expressions.
There is a check for that, however the check checks if the expressions are equal without normalizing column names first.
Because of that planning queries like "SELECT DISTINCT ON (t.a) * FROM t ORDER BY a" won't work while ""SELECT DISTINCT ON (t.a) * FROM t ORDER BY t.a" will.

### To Reproduce

Add the following to plan.rs unit tests and run:

```
#[test]
fn distinct_on_expr_order_by_match() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);

let table_scan =
table_scan(Some(TableReference::bare("t")), &schema, None)?.build()?;
let p = DistinctOn::try_new(
vec![col("a")],
vec![],
Some(vec![SortExpr::new(col("t.a"), true, false)]),
Arc::new(table_scan),
)?;
Ok(())
}
```

### Expected behavior

Test should pass

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the plan.rs unit tests and the DistinctOn::try_new path shown in the reproduction. Inspect the existing DISTINCT ON and ORDER BY expression comparison, then add the named test so qualified and unqualified column references match; done means the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.