apache / apache/datafusion

Create physical scalar expression in `functions` modules from string (name)

Open
#9,892 16 comments 1 reaction 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?

#### Create physical scalar expression in functions modules from string
Currently more and more built-in scalar functions are moved to `functions` modules, e.g. https://github.com/apache/arrow-datafusion/pull/9435. It avoids using a long enum of all built-in scalar functions which is hard to maintain. But for Comet, we rely the ability to create a physical scalar expression from string (e.g., `datepart`).

Previously it is easy and just calls `BuiltinScalarFunction::from_str` to get `BuiltinScalarFunction`. But now I don't see such convenient function to do that.

`FunctionRegistry` provides `udf` which can return a reference to `ScalarUDF`. But it requires these UDFs must be registered. As we don't know what UDFs will be used, we need to register all built-in UDFs in the registry. The flaw is, it will create `ScalarUDF`s for all built-in UDFs even they are not actually used in the queries.

I think we still need an approach that can simply create a physical scalar expression in `functions` modules from string. So we can create corresponding `ScalarUDF` on demand.

Another approach might be to avoid creating `ScalarUDF`s when registering built-in scalar functions.

#### Avoid creating `ScalarUDF`s before they are actually used when registering in `FunctionRegistry`

Actually, I am also wondering if it is necessary to create and register all these `ScalaUDF`s in DataFusion's `FunctionRegistry` before these scalar UDFs are actually used.

For example, Spark's `FunctionRegistry` registers expression builders instead of creating actual expressions when registering built-in expressions. A built-in expression is created only if it is actually used by a query.

### 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 tracing FunctionRegistry::udf and the built-in functions modules, then compare them with the previous BuiltinScalarFunction::from_str path. Determine how a name can produce a physical scalar expression or ScalarUDF on demand without registering every built-in function first. Done means the issue's string-based lookup and lazy-creation goal are resolved with appropriate coverage.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.