Skip to content
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

Fix: avoid using regex match, possibly containing alternation, as a key condition. #54696

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Storages/MergeTree/KeyCondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,12 @@ const KeyCondition::AtomMap KeyCondition::atom_map
if (value.getType() != Field::Types::String)
return false;

String prefix = extractFixedPrefixFromRegularExpression(value.get<const String &>());
const String & expression = value.get<const String &>();
// This optimization can't process alternation - this would require a comprehensive parsing of regular expression.
Copy link
Member

Choose a reason for hiding this comment

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

I thought a bit

  • whether the check should rather be moved into extractFixedPrefixFromRegularExpression() but I think it is okay to not do so
  • whether extractFixedPrefixFromRegularExpression() could be replaced by the more sophisticated regex parsing in OptimizedRegularExpression::analyze(), but again no. ::analyze() searches the longest substring and that's not necessarily the prefix.

Therefore, we can merge the fix. Thanks.

if (expression.contains('|'))
return false;

String prefix = extractFixedPrefixFromRegularExpression(expression);
if (prefix.empty())
return false;

Expand Down
1 change: 1 addition & 0 deletions tests/queries/0_stateless/02462_match_regexp_pk.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ SELECT count() FROM mt_match_pk WHERE match(v, '^ab');
SELECT count() FROM mt_match_pk WHERE match(v, '^a.');
SELECT count() FROM mt_match_pk WHERE match(v, '^ab*');
SELECT count() FROM mt_match_pk WHERE match(v, '^ac?');
SELECT count() FROM mt_match_pk WHERE match(v, '^a$|^b'); -- {serverError INDEX_NOT_USED}