Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(hogql): match LIKE patterns in linear time (#97530)

George Munyorofac60ac7d9ff
diff --git a/posthog/hogql/test/test_utils.py b/posthog/hogql/test/test_utils.py
index 068b5a86d77b..06fa5962e1dc 100644
--- a/posthog/hogql/test/test_utils.py
+++ b/posthog/hogql/test/test_utils.py
@@ -1,3 +1,5 @@
+import time
+
from posthog.test.base import BaseTest
from unittest import TestCase
@@ -344,6 +346,17 @@ class TestLikeMatches(BaseTest):
f"but ClickHouse returned {clickhouse_result}",
)
+ def test_like_matches_is_linear_for_many_wildcards(self) -> None:
+ # A pattern of N `%` followed by a non-matching char used to translate to N `.*` groups and
+ # backtrack super-linearly against a short text: ~hours of CPU for N=1000 (ReDoS, CWE-1333).
+ # The linear matcher must stay well under a second, so a regex reintroduction hangs this test.
+ pattern = "%" * 1000 + "x"
+ start = time.perf_counter()
+ result = like_matches(pattern, "null")
+ elapsed = time.perf_counter() - start
+ self.assertFalse(result)
+ self.assertLess(elapsed, 2.0, f"like_matches took {elapsed:.2f}s — wildcard matching is not linear")
+
class TestUtils(BaseTest):
@parameterized.expand(
diff --git a/posthog/hogql/utils.py b/posthog/hogql/utils.py
index 330779d027ae..c5567f2de380 100644
--- a/posthog/hogql/utils.py
+++ b/posthog/hogql/utils.py
@@ -1,10 +1,12 @@
-import re
from dataclasses import fields
from typing import Any, Union, get_args, get_origin
from posthog.hogql import ast
from posthog.hogql.ast import AST, AST_CLASSES, Constant, Expr, HogQLXAttribute, HogQLXTag
+_LIKE_ANY = object() # '%' token: matches any run of characters, including empty
+_LIKE_ONE = object() # '_' token: matches exactly one character
+
def like_matches(pattern: str, text: str) -> bool:
"""
@@ -16,26 +18,52 @@ def like_matches(pattern: str, text: str) -> bool:
- _ matches exactly one character
- \\ escapes the next character (\\%, \\_, \\\\)
- Other characters match literally (case-sensitive)
+
+ Uses a greedy linear-scan matcher (O(len(pattern) * len(text))) rather than a
+ translated regex: a regex with one `.*` per `%` backtracks super-linearly, so a
+ pattern of many `%` against a short text pins a CPU for minutes (ReDoS, CWE-1333).
"""
- # Convert SQL LIKE pattern to regex
- regex_parts: list[str] = []
+ tokens: list[object] = []
i = 0
- while i < len(pattern):
+ n = len(pattern)
+ while i < n:
char = pattern[i]
if char == "%":
- regex_parts.append(".*")
+ tokens.append(_LIKE_ANY)
elif char == "_":
- regex_parts.append(".")
- elif char == "\\" and i + 1 < len(pattern):
- # Escape sequence - next char is literal
+ tokens.append(_LIKE_ONE)
+ elif char == "\\" and i + 1 < n:
i += 1
- regex_parts.append(re.escape(pattern[i]))
+ tokens.append(pattern[i])
else:
- regex_parts.append(re.escape(char))
+ tokens.append(char)
i += 1
- regex_pattern = f"^{''.join(regex_parts)}$"
- return bool(re.match(regex_pattern, text, re.DOTALL))
+ # Greedy two-pointer wildcard match: advance through text and tokens together, and
+ # on a mismatch fall back to the most recent `%`, letting it consume one more char.
+ t = 0
+ p = 0
+ star_p = -1
+ star_t = 0
+ tlen = len(text)
+ plen = len(tokens)
+ while t < tlen:
+ if p < plen and (tokens[p] is _LIKE_ONE or tokens[p] == text[t]):
+ t += 1
+ p += 1
+ elif p < plen and tokens[p] is _LIKE_ANY:
+ star_p = p
+ star_t = t
+ p += 1
+ elif star_p != -1:
+ p = star_p + 1
+ star_t += 1
+ t = star_t
+ else:
+ return False
+ while p < plen and tokens[p] is _LIKE_ANY:
+ p += 1
+ return p == plen
def ilike_matches(pattern: str, text: str) -> bool: