apache / apache/datafusion

Avoid check function type by matching names in the optimizer

Open
#18,643 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?

When implementing optimizer rules, it's common to check if a expression is certain function. Now it's commonly done through function name match check. A potential risk is: datafusion dependents might override the function implementation in the function registry, in such cases those optimizer rules should not be enabled, it's better to figure out a way to accurately check the function type.

I believe now they're done through name match because all function implementations live in a different crate than optimizer, and we don't want to add a crate dependency between them, so we can't do the trait object downcasting for type check.

One way I can think of is add a `source()` API in UDFs, so we can differentiate if this is builtin or added by the 3rd party, and in the optimizer we can check it safer like `fun.source() == BuiltIn && fun.name() == "min"`

Current optimizer examples for checking function type by name matching:

https://github.com/apache/datafusion/blob/0d52a1eebe403790a480ba404c2fe65b1aa1e897/datafusion/optimizer/src/single_distinct_to_groupby.rs#L90-L92
https://github.com/apache/datafusion/blob/0d52a1eebe403790a480ba404c2fe65b1aa1e897/datafusion/optimizer/src/analyzer/resolve_grouping_function.rs#L151-L153
https://github.com/apache/datafusion/blob/0d52a1eebe403790a480ba404c2fe65b1aa1e897/datafusion/optimizer/src/decorrelate.rs#L496

### 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

Start by reviewing the function-name checks in datafusion/optimizer/src/single_distinct_to_groupby.rs, datafusion/optimizer/src/analyzer/resolve_grouping_function.rs, and datafusion/optimizer/src/decorrelate.rs. Then inspect the UDF and function-registry APIs to determine how built-in functions can be distinguished from overridden implementations without an optimizer dependency on their implementation crate; done means the optimizer checks are safe and the affected rules are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
data-engineering
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.