Skip to content

feat(datafusion): support CASE in filter expressions - #11605

Merged
Xuanwo merged 1 commit into
lance-format:mainfrom
iSparshP:feat/case-in-filter-expressions
Dec 2, 2026
Merged

Xuanwo merged 1 commit into
lance-format:mainfrom
iSparshP:feat/case-in-filter-expressions

Conversation

@iSparshP

@iSparshP iSparshP commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

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

  • simple CASE (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 in x = 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 fold CASE WHEN x = 1.2 THEN 1 ELSE 0 END = 1 into (x = 1.2) IS NOT DISTINCT FROM true, a predicate on x with no cast.
  • resolve_expr walks into CASE nodes wherever they sit (under comparisons, IN list items, BETWEEN bounds, casts compared with a column, and the decimal IN/BETWEEN rewrite) and coerces the WHEN predicates. a THEN/ELSE result that is itself a predicate (comparison, AND/OR, NOT, IS [NOT] TRUE/FALSE, IN list, BETWEEN) is resolved the same way a standalone filter is, so CASE WHEN flag THEN value = 1.2 ELSE FALSE END on a float32 column matches like value = 1.2 does. 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 like id + 300 on a uint8 column keeps its promotion, also when it sits inside a comparison in a result (THEN id + 300 > 301).
  • comparisons inside function arguments are parsed by datafusion's SqlToRel, not lance's parser, so 0.06 there 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 (eg coalesce(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 (eg i = 'abc' inside a CASE or a function) now fails in optimize_expr with datafusion's cast error instead of at parse time; at the top level it still fails at parse time like before.
  • a simple CASE that datafusion builds inside a function argument (eg 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.
  • a volatile operand in a simple CASE (eg 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.
  • update SET values and sql add_columns go through the same Planner::parse_expr, so they accept CASE too. parse_expr itself is covered by test_case_result_predicates, but i haven't tested the update / add_columns paths end to end in this PR.

how it was tested

  • replaced test_case_filters_remain_unsupported with test_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)
  • the coercion / signed-zero / arithmetic reproducers from the feat(datafusion): support FLOOR, CEIL, and SQL-standard SUBSTRING filters #9584 review threads are included as cases: under_is_true, not_predicate, literal_operand, zero_operand, simple_case_folded_zero(_swapped) and the CASE cases in test_arithmetic_filter_preserves_numeric_promotion
  • added test_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 both parse_filter and parse_expr on a float32 column
  • added test_case_result_type_mismatch (THEN TRUE ELSE 1 fails datafusion coercion as InvalidInput) and decimal_predicates_with_case_operands (CASE as a non-literal IN item / BETWEEN bound on a decimal column)
  • added test_comparisons_in_function_arguments (through both parse_filter and parse_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 columns
  • extended test_arithmetic_filter_preserves_numeric_promotion (also with a comparison on id + 300 inside 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.rs folded_constant_comparisons_use_ieee_semantics and optimizing_twice_changes_nothing
  • cargo test -p lance-datafusion
  • cargo fmt --all, cargo clippy -p lance-datafusion --tests --benches --no-deps -- -D warnings (the full cargo clippy --all wasn't run locally; without --no-deps the run stops on an existing unused mark_huggingface_rate_limited in lance-io)

docs

added CASE to the filter push-down list in read_and_write.md.

@github-actions github-actions Bot added A-docs Documentation enhancement New feature or request labels Nov 29, 2026
@iSparshP
iSparshP force-pushed the feat/case-in-filter-expressions branch from 8c71a9e to ff12bd4 Compare December 2, 2026 10:50
@iSparshP
iSparshP marked this pull request as ready for review December 2, 2026 10:56

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Dec 2, 2026
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
iSparshP force-pushed the feat/case-in-filter-expressions branch from ff12bd4 to ef9ca14 Compare December 2, 2026 12:15
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Dec 2, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026
@Xuanwo
Xuanwo merged commit 813c5c2 into lance-format:main Dec 2, 2026
44 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Documentation enhancement New feature or request K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: support FLOOR, CEIL, CASE and SUBSTRING in filter expressions

2 participants