Repository navigation
feat(datafusion): support CASE in filter expressions - #11605
Merged
Xuanwo merged 1 commit intoDec 2, 2026
Merged
Conversation
iSparshP
force-pushed
the
feat/case-in-filter-expressions
branch
from
December 2, 2026 10:50
8c71a9e to
ff12bd4
Compare
iSparshP
marked this pull request as ready for review
December 2, 2026 10:56
Searched and simple CASE now plan in filters. Simple CASE is lowered to `operand = value` per WHEN so each comparison gets the same literal narrowing, exact decimal parsing and signed-zero handling as a plain filter, and DataFusion can simplify `CASE ... END = lit` into a predicate on the column. resolve_expr does the same lowering for a simple CASE that DataFusion builds in a function argument, and narrows literals in every nested comparison, such as one in a WHEN, a THEN/ELSE result, a function argument or a comparison on a boolean, wherever the CASE sits, including IN items, BETWEEN bounds and casts compared with a column; arithmetic and other results are left for DataFusion to promote and unify. Narrowing a nested comparison is best effort: one whose literal does not convert to the column type, such as a double compared with a decimal or a date string in a function argument, is left for DataFusion to coerce as before. A volatile simple-CASE operand is rejected, also inside a function argument, since lowering would evaluate it once per WHEN. Closes lance-format#9560
iSparshP
force-pushed
the
feat/case-in-filter-expressions
branch
from
December 2, 2026 12:15
ff12bd4 to
ef9ca14
Compare
Contributor
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The reported result-branch coercion issue is fixed. CASE comparisons now preserve literal narrowing and signed-zero behavior across branches and function arguments, while arithmetic retains DataFusion's type promotion.
Xuanwo
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #9560. floor, ceil and substring landed in #9584, this is the CASE part.
why
every CASE form failed with "is not supported SQL in lance" from planner.rs, so when/then logic couldn't be pushed down into lance.
how
CASE x WHEN v THEN ...) is rewritten into the searched form (CASE WHEN x = v THEN ...) while parsing the sql ast. each WHEN goes through the normal comparison path, so float32 literals get narrowed, decimals stay exact and -0.0 / 0.0 match like they do inx = 0.0. this was the review feedback on the first CASE attempt in feat(datafusion): support FLOOR, CEIL, and SQL-standard SUBSTRING filters #9584. it also lets datafusion foldCASE WHEN x = 1.2 THEN 1 ELSE 0 END = 1into(x = 1.2) IS NOT DISTINCT FROM true, a predicate on x with no cast.CASE WHEN flag THEN value = 1.2 ELSE FALSE ENDon a float32 column matches likevalue = 1.2does. any other result (arithmetic, columns, literals) is left to datafusion's type coercion, and comparisons nested inside a function in a result get narrowed too (see below), so arithmetic likeid + 300on a uint8 column keeps its promotion, also when it sits inside a comparison in a result (THEN id + 300 > 301).0.06there is a Float64 and'2024-01-02'a plain Utf8. narrowing a nested comparison is therefore best effort: if the literal can't be converted to the column type (egcoalesce(d = 0.06, FALSE)on a decimal column,coalesce(dt = '2024-01-02', FALSE)on a date column,coalesce(i = '2', FALSE)on an int column) the comparison is left as is and datafusion coerces it, same as on main. a literal that can't be coerced at all (egi = 'abc'inside a CASE or a function) now fails inoptimize_exprwith datafusion's cast error instead of at parse time; at the top level it still fails at parse time like before.coalesce(CASE f32 WHEN 1.2 THEN TRUE ELSE FALSE END, FALSE)) is lowered to equalities during resolve_expr, so it gets the same narrowing and signed-zero handling as one at the top level.random()) returns an error pointing at the searched form, since the rewrite would evaluate it once per WHEN. this is checked for a CASE inside a function argument too.Planner::parse_expr, so they accept CASE too.parse_expritself is covered bytest_case_result_predicates, but i haven't tested the update / add_columns paths end to end in this PR.how it was tested
test_case_filters_remain_unsupportedwithtest_case_filters(searched/simple forms, float32 literal narrowing with no cast on the column, signed zero on both sides, nested CASE, missing ELSE, NULL operand and WHEN NULL, string/bool/numeric results, comparing to a column, CASE inside an IN list / BETWEEN bound / CAST, under AND/NOT/IS TRUE)under_is_true,not_predicate,literal_operand,zero_operand,simple_case_folded_zero(_swapped)and the CASE cases intest_arithmetic_filter_preserves_numeric_promotiontest_case_result_predicates: comparisons returned by THEN / ELSE (searched and simple form), a nested CASE in a result returning a comparison, AND/NOT and IN list in a result, each through bothparse_filterandparse_expron a float32 columntest_case_result_type_mismatch(THEN TRUE ELSE 1 fails datafusion coercion as InvalidInput) anddecimal_predicates_with_case_operands(CASE as a non-literal IN item / BETWEEN bound on a decimal column)test_comparisons_in_function_arguments(through bothparse_filterandparse_expr): decimal vs double literal (=,>,IN, a value between two hundredths,nullif), int vs string, date and timestamp vs string inside coalesce,(dt = '2024-01-02') = TRUE, a searched CASE inside coalesce, and a simple CASE inside coalesce / abs on float32, float64 (signed zero) and decimal columnstest_arithmetic_filter_preserves_numeric_promotion(also with a comparison onid + 300inside THEN and under NOT in ELSE),test_parse_filter_uint64_literal_above_i64_max,test_unsupported_scalar_expression_forms(volatile operand at the top level and inside coalesce),test_resolve_double_literal_on_float32,decimal_predicates(exact 0.055 in a simple CASE), and in signed_zero.rsfolded_constant_comparisons_use_ieee_semanticsandoptimizing_twice_changes_nothingcargo test -p lance-datafusioncargo fmt --all,cargo clippy -p lance-datafusion --tests --benches --no-deps -- -D warnings(the fullcargo clippy --allwasn't run locally; without--no-depsthe run stops on an existing unusedmark_huggingface_rate_limitedin lance-io)docs
added CASE to the filter push-down list in read_and_write.md.