-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: regex simplification of anchored patterns produces wrong results #22727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b5770f9
2631cf6
ae2d6b8
6f659f2
f8641a2
2d3d402
0d10a3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,12 +204,171 @@ SELECT * FROM test WHERE column1 ~ 'z' | |
| ---- | ||
| Bazzz | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 ~ '^Bazzz$' | ||
| ---- | ||
| Bazzz | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 ~ '^(foo|Bazzz)$' | ||
| ---- | ||
| foo | ||
| Bazzz | ||
|
|
||
| statement ok | ||
| CREATE TABLE test_regex_utf8view(s VARCHAR) AS VALUES ('foo'), ('Bazzz'); | ||
|
|
||
| statement ok | ||
| set datafusion.explain.logical_plan_only = true | ||
|
|
||
| # `~` anchored literal -> `= Utf8View(..)` | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s ~ '^Bazzz$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s = Utf8View("Bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `~*` anchored literal -> `ILIKE Utf8View(..)` | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s ~* '^bazzz$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s ILIKE Utf8View("bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `~` anchored alternation -> OR of `= Utf8View(..)` comparisons. | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s ~ '^(foo|Bazzz)$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s = Utf8View("foo") OR test_regex_utf8view.s = Utf8View("Bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `~*` anchored alternation -> NOT simplified: it falls back to a regex match, | ||
| # because `IN`/`=` cannot express case-insensitive matching. | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s ~* '^(foo|bazzz)$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s ~* Utf8View("^(foo|bazzz)$") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `!~` -> `!= Utf8View(..)` | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s !~ '^Bazzz$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s != Utf8View("Bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `!~*` -> `NOT ILIKE Utf8View(..)` | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s !~* '^bazzz$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s NOT ILIKE Utf8View("bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `!~` anchored alternation -> AND of `!= Utf8View(..)` comparisons. | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s !~ '^(foo|Bazzz)$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s != Utf8View("foo") AND test_regex_utf8view.s != Utf8View("Bazzz") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| # `!~*` anchored alternation -> NOT simplified: it falls back to a regex match, | ||
| # same reason as the `~*` alternation above. | ||
| query TT | ||
| EXPLAIN SELECT * FROM test_regex_utf8view WHERE s !~* '^(foo|bazzz)$' | ||
| ---- | ||
| logical_plan | ||
| 01)Filter: test_regex_utf8view.s !~* Utf8View("^(foo|bazzz)$") | ||
| 02)--TableScan: test_regex_utf8view projection=[s] | ||
|
|
||
| statement ok | ||
| set datafusion.explain.logical_plan_only = false | ||
|
|
||
| # Result assertions | ||
| query T | ||
| SELECT * FROM test_regex_utf8view WHERE s ~ '^Bazzz$' | ||
| ---- | ||
| Bazzz | ||
|
|
||
| query T | ||
| SELECT * FROM test_regex_utf8view WHERE s ~ '^(foo|Bazzz)$' | ||
| ---- | ||
| foo | ||
| Bazzz | ||
|
|
||
| # Case-insensitive anchored match over Utf8View: must be simplified to ILIKE | ||
| # (not a case-sensitive Eq) and must keep operand types as Utf8View. | ||
| query T | ||
| SELECT * FROM test_regex_utf8view WHERE s ~* '^bazzz$' | ||
| ---- | ||
| Bazzz | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How this asserts the expected result ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good to me, it makes the intent clearer. I've already added EXPLAIN assertions for all the anchored cases. |
||
|
|
||
| # Case-insensitive anchored alternation over Utf8View | ||
| query T rowsort | ||
| SELECT * FROM test_regex_utf8view WHERE s ~* '^(foo|bazzz)$' | ||
| ---- | ||
| Bazzz | ||
| foo | ||
|
|
||
| query T rowsort | ||
| SELECT * FROM test_regex_utf8view WHERE s !~ '^Bazzz$' | ||
| ---- | ||
| foo | ||
|
|
||
| query T rowsort | ||
| SELECT * FROM test_regex_utf8view WHERE s !~* '^bazzz$' | ||
| ---- | ||
| foo | ||
|
|
||
| # Both rows match the alternation, so the negated forms return nothing. | ||
| query T rowsort | ||
| SELECT * FROM test_regex_utf8view WHERE s !~ '^(foo|Bazzz)$' | ||
| ---- | ||
|
|
||
| query T rowsort | ||
| SELECT * FROM test_regex_utf8view WHERE s !~* '^(foo|bazzz)$' | ||
| ---- | ||
|
|
||
| statement ok | ||
| DROP TABLE test_regex_utf8view; | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 ~* 'z' | ||
| ---- | ||
| Bazzz | ||
| ZZZZZ | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 ~* '^barrr$' | ||
| ---- | ||
| Barrr | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 ~* '^(barrr|bazzz)$' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests with negation+regex are missing (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| ---- | ||
| Barrr | ||
| Bazzz | ||
|
|
||
| query T rowsort | ||
| SELECT * FROM test WHERE column1 !~ '^Bazzz$' | ||
| ---- | ||
| Barrr | ||
| ZZZZZ | ||
| foo | ||
|
|
||
| query T rowsort | ||
| SELECT * FROM test WHERE column1 !~* '^barrr$' | ||
| ---- | ||
| Bazzz | ||
| ZZZZZ | ||
| foo | ||
|
|
||
| query T | ||
| SELECT * FROM test WHERE column1 !~ 'z' | ||
| ---- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question to educate myself: How the values here are Utf8View ?
I'd expect some casting to achieve that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's because the config
map_string_types_to_utf8viewdefaults totrue, so aVARCHARcolumn is planned asUtf8Viewin sltdatafusion/datafusion/common/src/config.rs
Lines 292 to 295 in e1d8d46