Skip to content

Commit 4e9896b

Browse files
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 * Formatting
1 parent 623936e commit 4e9896b

10 files changed

Lines changed: 1558 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)