Unify SQL planning for `ORDER BY`, `HAVING`, `DISTINCT`, etc
- 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?
As @jonahgao [points out](https://github.com/apache/datafusion/pull/10234#issuecomment-2087754040) in https://github.com/apache/datafusion/pull/10234:
> `select x from foo order by y` can is covered by [add_missing_columns](https://github.com/apache/datafusion/blob/f8c623fe045d70a87eac8dc8620b74ff73be56d5/datafusion/expr/src/logical_plan/builder.rs#L437), by blindly adding columns into the descendant projection node. Another issue is that we should not run add_missing_columns for other SetExprs except SELECT.
In https://github.com/apache/datafusion/pull/10234 @jonahgao added a more general solution to use the merged schema from the select list and the FROM clause to handle resolving `HAVING` and set operations
However, both codepaths now exist, which makes for fairly complicated planning process
### Describe the solution you'd like
> I think that we should handle ORDER BY similarly to HAVING, use the merged schema, add the missing columns directly in the select list, instead of traversing the plan looking for projection node. Their processing logic may be reusable. I agree it might be good to have a broader discussion about this.
@jonahgao in https://github.com/apache/datafusion/pull/10234#issuecomment-2087760241
### Describe alternatives you've considered
One alternative might be [add_missing_columns](https://github.com/apache/datafusion/blob/f8c623fe045d70a87eac8dc8620b74ff73be56d5/datafusion/expr/src/logical_plan/builder.rs#L437) and using the new `order_by_to_sort_expr` options added in https://github.com/apache/datafusion/pull/10234
### Additional context
_No response_
Contributor guide
Research direction
Start with datafusion/expr/src/logical_plan/builder.rs around add_missing_columns, then read the planning changes and discussion in pull request #10234. Compare the existing ORDER BY path with the merged-schema handling for HAVING and set operations; done means the duplicated planning approaches are unified while preserving the stated SELECT-only behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, sql
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100