Commit 4e9896b
authored
Add Case-When Expression and tests (#12)
* fix: Ensure CastExpr/CastColumnExpr/ScalarFunctionExpr check children in can_be_pushed_down
The can_be_pushed_down function was returning true for CastExpr and CastColumnExpr
without checking if their child expressions are convertible. This caused runtime
errors when the child contained expressions like CaseExpr that convert() cannot
handle.
Also fixed ScalarFunctionExpr to recursively check its arguments.
Fixes spiceai/spiceai#9037
* Add Case-When Expression and tests
* Implement execute()
* Add additional tests and fix type issue
* Fix toml lint
* feat(case_when): implement lazy evaluation to avoid side effects in unevaluated branches
This implements proper lazy evaluation in CaseWhen expression to ensure
that THEN/ELSE branches are only evaluated for rows where they apply.
This is critical for correctness when expressions have side effects like
divide-by-zero panics.
The implementation:
1. Evaluates conditions in order, tracking which rows have been matched
2. For each condition, computes an effective mask (condition AND NOT matched)
3. Uses filter() to create a scoped array with only matching rows
4. Evaluates THEN expression only on the filtered scope
5. Uses scatter_with_mask() to expand results back to original positions
6. Short-circuits when all rows are matched or all conditions fail
This fixes TPC-DS Q73 which has a pattern like:
CASE WHEN hd_vehicle_count > 0
THEN hd_dep_count/hd_vehicle_count
ELSE NULL END
Previously, the division would be evaluated for all rows including those
where hd_vehicle_count=0, causing a divide-by-zero panic. Now the division
is only evaluated for rows where the condition is true.
Added test: test_evaluate_divide_by_zero_protected_by_case_when
* Formatting1 parent 623936e commit 4e9896b
10 files changed
Lines changed: 1558 additions & 15 deletions
File tree
- vortex-array/src/expr
- exprs
- vortex-datafusion/src/convert
- vortex-file/src
- vortex-io
- vortex-proto
- proto
- src/generated
- vortex-session/src
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments