apache / apache/datafusion

Eliminate distinct of min/max and rewrite count wildcard family in ExprBuilder

Closed
#11,686 0 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?

Given that distinct min/max is the same as non-distinct, it is easier for datafusion if we eliminate distinct as early as possible.

Also, count has the similar issue, so I bring it up the discussion again . Previous discussion in https://github.com/apache/datafusion/pull/11371#discussion_r1677243837

### Describe the solution you'd like

Eliminate distinct for min/max in ExprBuilder

Move `count_wildcard_rule` to ExprBuilder. Standardize `count(*)`, `count()` and `count(1)` 's name to `count_star`

### Describe alternatives you've considered

Eliminate distinct for min/max in analyzer
Similar to `count_wildcard_rule`

The downside is that the name is not *standardized* if we rewrite it in analyzer

```
query TT
explain select count() from t;
----
logical_plan
01)Aggregate: groupBy=[[]], aggr=[[count(Int64(1)) AS count()]]
02)--TableScan: t projection=[]
physical_plan
01)ProjectionExec: expr=[1 as count()]
02)--PlaceholderRowExec

query TT
explain select count(*) from t;
----
logical_plan
01)Aggregate: groupBy=[[]], aggr=[[count(Int64(1)) AS count(*)]]
02)--TableScan: t projection=[]
physical_plan
01)ProjectionExec: expr=[1 as count(*)]
02)--PlaceholderRowExec
```

I think it would be a lot easier for datafusion to handle optimizer if we have standardized name and much more clear what the function is in explain statement
1. `count_star` for count, like Duckdb
2. `min` for min (no `distinct`)
3. `max` for max

_No response_

### Additional context

It is better to start the min/max part after #11013

Contributor guide

Open the contributing guide

Research direction

Start with ExprBuilder and the existing count_wildcard_rule, then review the related analyzer alternative and issue #11013. Done means distinct is eliminated for min/max and count(), count(*) and count(1) use the standardized count_star name in explain output.

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
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.