diff --git a/src/Functions/MatchImpl.h b/src/Functions/MatchImpl.h index 002b876f1e8..3622db234b5 100644 --- a/src/Functions/MatchImpl.h +++ b/src/Functions/MatchImpl.h @@ -118,16 +118,13 @@ struct MatchImpl if (haystack_offsets.empty()) return; - /// Fast path for [I]LIKE, because the result is always true or false - /// col [i]like '%%' - /// col not [i]like '%%' - /// col like '%' - /// col not [i]like '%' - /// match(like, '^$') - if ((is_like && (needle == "%%" or needle == "%")) || (!is_like && needle == ".*")) + /// Shortcut for the silly but practical case that the pattern matches everything/nothing independently of the haystack: + /// - col [not] [i]like '%' / '%%' + /// - match(col, '.*') + if ((is_like && (needle == "%" or needle == "%%")) || (!is_like && (needle == ".*" || needle == ".*?"))) { - for (auto & re : res) - re = !negate; + for (auto & x : res) + x = !negate; return; } @@ -280,6 +277,16 @@ struct MatchImpl if (haystack.empty()) return; + /// Shortcut for the silly but practical case that the pattern matches everything/nothing independently of the haystack: + /// - col [not] [i]like '%' / '%%' + /// - match(col, '.*') + if ((is_like && (needle == "%" or needle == "%%")) || (!is_like && (needle == ".*" || needle == ".*?"))) + { + for (auto & x : res) + x = !negate; + return; + } + /// Special case that the [I]LIKE expression reduces to finding a substring in a string String strstr_pattern; if (is_like && impl::likePatternIsSubstring(needle, strstr_pattern))