fix: preserve NULL semantics when simplifying col ~ '.*' - #24380
Merged
Conversation
`simplify_regex_expr` rewrote `col ~ '.*'` to `col IS NOT NULL`. For a NULL input that returns `false`, but `NULL ~ '.*'` is `NULL` under three-valued logic, so the rewrite produces wrong results in a projection context. (The `!~` branch was already NULL-aware; only the `~` branch dropped the NULL.) Rewrite to `col IS NOT NULL OR NULL`, which is `true` for a non-NULL string and `NULL` for a NULL input. In a WHERE filter this is equivalent to the old `IS NOT NULL` (both reject the NULL row), so filter results are unchanged; only the plan text and projection-context values differ. Existing filter-plan expectations updated and a projection regression test added. Closes apache#24379. Co-authored-by: Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24380 +/- ##
==========================================
- Coverage 81.19% 81.18% -0.01%
==========================================
Files 1110 1110
Lines 388616 388740 +124
Branches 388616 388740 +124
==========================================
+ Hits 315529 315617 +88
- Misses 54506 54535 +29
- Partials 18581 18588 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alamb
approved these changes
Aug 15, 2026
Member
Author
|
Thank you @alamb for review! |
imtherealnaska
pushed a commit
to imtherealnaska/datafusion
that referenced
this pull request
Aug 16, 2026
## Which issue does this PR close? - Closes apache#24379. ## Rationale for this change `simplify_regex_expr` rewrites `col ~ '.*'` to `col IS NOT NULL`. For a NULL input that returns `false`, but `NULL ~ '.*'` is `NULL` under three-valued logic — so the rewrite produces wrong results in a projection context: ```sql SELECT s, s ~ '.*' FROM (VALUES (CAST(NULL AS VARCHAR)), ('x')) t(s); -- NULL row currently returns `false`; it should be NULL ``` The `!~` (`RegexNotMatch`) branch of the same rule is already NULL-aware (`col IS NULL AND NULL`); only the `~` branch dropped the NULL. ## What changes are included in this PR? - Rewrite `col ~ '.*'` to `col IS NOT NULL OR NULL` — `true` for a non-NULL string, `NULL` for a NULL input. - In a WHERE filter both FALSE and NULL reject the row, so filter *results* are unchanged; only the plan text and projection-context values differ. Existing filter-plan expectations in `simplify_expr.slt` and the `test_simplify_regex_special_cases` unit test are updated accordingly, and a projection regression test is added. ## Are these changes tested? Yes. - New projection regression test in `simplify_expr.slt` asserting `col ~ '.*'` returns `true`/`true`/`NULL` for `'foo'`/`''`/`NULL`. - Updated the two filter-context plan expectations (logical + physical) that previously encoded the `IS NOT NULL` rewrite. - `simplify_expr.slt`, the `regexp/*` SLTs, and the optimizer simplify unit tests all pass. ## Are there any user-facing changes? `col ~ '.*'` in a projection now returns `NULL` for a NULL input instead of `false`, matching SQL semantics. No API changes.
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.
Which issue does this PR close?
col ~ '.*'tocol IS NOT NULLdrops NULL semantics #24379.Rationale for this change
simplify_regex_exprrewritescol ~ '.*'tocol IS NOT NULL. For a NULL input that returnsfalse, butNULL ~ '.*'isNULLunder three-valued logic — so the rewrite produces wrong results in a projection context:The
!~(RegexNotMatch) branch of the same rule is already NULL-aware (col IS NULL AND NULL); only the~branch dropped the NULL.What changes are included in this PR?
col ~ '.*'tocol IS NOT NULL OR NULL—truefor a non-NULL string,NULLfor a NULL input.simplify_expr.sltand thetest_simplify_regex_special_casesunit test are updated accordingly, and a projection regression test is added.Are these changes tested?
Yes.
simplify_expr.sltassertingcol ~ '.*'returnstrue/true/NULLfor'foo'/''/NULL.IS NOT NULLrewrite.simplify_expr.slt, theregexp/*SLTs, and the optimizer simplify unit tests all pass.Are there any user-facing changes?
col ~ '.*'in a projection now returnsNULLfor a NULL input instead offalse, matching SQL semantics. No API changes.