fix/perf: Optimize ~! '.*' case to False instead of Eq ""#20702
Open
petern48 wants to merge 3 commits intoapache:mainfrom
Open
fix/perf: Optimize ~! '.*' case to False instead of Eq ""#20702petern48 wants to merge 3 commits intoapache:mainfrom
~! '.*' case to False instead of Eq ""#20702petern48 wants to merge 3 commits intoapache:mainfrom
Conversation
False instead of Eq ""~! '.*' case to False instead of Eq ""
d65ffef to
1ed89c9
Compare
alamb
reviewed
Mar 4, 2026
| /// - combinations (alternatives) of the above, will be concatenated with `OR` or `AND` | ||
| /// - `EQ .*` to NotNull | ||
| /// - `NE .*` means IS EMPTY | ||
| /// - `NE .*` to false (.* matches non-empty and empty strings, and NULL !~ '.*' results in NULL so this can never be true) |
Contributor
There was a problem hiding this comment.
I think it is only false in the context of filters (and this code can currently be used for both filters and regular expressions)
I think null !~ '.*' is actually null (not false)
I think a correct rewrite is
CASE
WHEN col IS NOT NULL THEN FALSE
ELSE NULL
END| )?; | ||
|
|
||
| // Test `!= ".*"` transforms to checking if the column is empty | ||
| // Test `!~ ".*"` transforms to false |
Contributor
There was a problem hiding this comment.
can we please also add a test explicitly for a null input?
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?
.*pattern toEq ""operation #20701Rationale for this change
What changes are included in this PR?
A pre-existing optimization rule for the
!~ .*(regexp not match) case rewrote the plan toEq "", which would return empty strings as part of the result. This is incorrect and doesn't match the output without the optimization rule.Instead, this PR rewrites the plan to simply
lit(false). The reasoning why it's always false is as follows:.*pattern matches all strings including empty strings (""), so empty strings should not be returnedNULL !~ .*evaluates toNULL(hence not true), so the query also doesn't return null rows.I've confirmed this behavior matches the result of running queries manually with the optimization rule turned off.
Are these changes tested?
Fixed expected output in tests.
Are there any user-facing changes?
Yes, a minor bug fix. When querying
s !~ .*, empty strings will no longer be included in the result which is consistent with the behavior without the optimization rule.