Skip to content

Commit

Permalink
Merge pull request #56253 from ClickHouse/backport/23.8/54696
Browse files Browse the repository at this point in the history
Backport #54696 to 23.8: Fix: avoid using regex match, possibly containing alternation, as a key condition.
  • Loading branch information
rschu1ze committed Nov 2, 2023
2 parents f6d503e + 378b062 commit 2a5df64
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Storages/MergeTree/KeyCondition.cpp
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.
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
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}

0 comments on commit 2a5df64

Please sign in to comment.