Commit 22c4214
Refactor
## Which issue does this PR close?
* Closes #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 <jeffrey.vo.australia@gmail.com>nvl2 Function to Support Lazy Evaluation and Simplification via CASE Expression (#18191)1 parent 619123a commit 22c4214
File tree
3 files changed
+84
-48
lines changed- datafusion
- core/tests
- dataframe
- expr_api
- functions/src/core
3 files changed
+84
-48
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
277 | 304 | | |
278 | 305 | | |
279 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
323 | 343 | | |
324 | 344 | | |
325 | 345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 18 | + | |
22 | 19 | | |
23 | 20 | | |
24 | | - | |
25 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
95 | 94 | | |
96 | 95 | | |
97 | 96 | | |
98 | | - | |
99 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
100 | 128 | | |
101 | 129 | | |
102 | 130 | | |
| |||
123 | 151 | | |
124 | 152 | | |
125 | 153 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
0 commit comments