You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor nvl2 Function to Support Lazy Evaluation and Simplification via CASE Expression (apache#18191)
## Which issue does this PR close?
* Closesapache#17983
## Rationale for this change
The current implementation of the `nvl2` function in DataFusion eagerly
evaluates all its arguments, which can lead to unnecessary computation
and incorrect behavior when handling expressions that should only be
conditionally evaluated. This PR introduces **lazy evaluation** for
`nvl2`, aligning its behavior with other conditional expressions like
`coalesce` and improving both performance and correctness.
This change also introduces a **simplification rule** that rewrites
`nvl2` expressions into equivalent `CASE` statements, allowing for
better optimization during query planning and execution.
## What changes are included in this PR?
* Refactored `nvl2` implementation in
`datafusion/functions/src/core/nvl2.rs`:
* Added support for **short-circuit (lazy) evaluation** using
`short_circuits()`.
* Implemented **simplify()** method to rewrite expressions into `CASE`
form.
* Introduced **return_field_from_args()** for correct nullability and
type inference.
* Replaced the previous eager `nvl2_func()` logic with an optimized,
more declarative approach.
* Added comprehensive **unit tests**:
* `test_nvl2_short_circuit` in `dataframe_functions.rs` verifies correct
short-circuit behavior.
* `test_create_physical_expr_nvl2` in `expr_api/mod.rs` validates
physical expression creation and output correctness.
## Are these changes tested?
✅ Yes, multiple new tests are included:
* **`test_nvl2_short_circuit`** ensures `nvl2` does not evaluate
unnecessary branches.
* **`test_create_physical_expr_nvl2`** checks the correctness of
evaluation and type coercion behavior.
All existing and new tests pass successfully.
## Are there any user-facing changes?
Yes, but they are **non-breaking** and **performance-enhancing**:
* `nvl2` now evaluates lazily, meaning only the required branch is
computed based on the nullity of the test expression.
* Expression simplification will yield more optimized query plans.
There are **no API-breaking changes**. However, users may observe
improved performance and reduced computation for expressions involving
`nvl2`.
---------
Co-authored-by: Jeffrey Vo <[email protected]>
0 commit comments