You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Enhancement] Constant LIKE/REGEXP pattern is recompiled for every clone of the expression (per scanner and per segment iterator) #69916
FunctionLike and FunctionRegexpLike build their state, including the Hyperscan database (hs_compile) or the RE2/Boost regex for a constant pattern, only in their THREAD_LOCAL open. VExprContext::clone opens THREAD_LOCAL again for every clone, and a pushed-down predicate is cloned once per scanner and once per segment iterator (SegmentIterator::_construct_compound_expr_context). A scan over N segments therefore compiles the same constant pattern about N times.
A case-insensitive alternation such as (?i)(alpha|bravo|...|juliet)[.](exe|dll)$ takes milliseconds to compile. On a table with about ten thousand segments, the open phase (SegmentIteratorInitTimer in the profile) dominates the query and costs minutes of CPU. Matching itself (ExprFilterEvalTime) stays in the tens of milliseconds.
LikeSearchState::clone, used by LikeColumnPredicate, also runs hs_prepare again for each clone.
What you expected to happen
A constant pattern is compiled once per fragment, and its clones reuse it.
How to reproduce
CREATETABLEt (k BIGINT, s STRING) DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 64
PROPERTIES ('replication_num'='1', 'disable_auto_compaction'='true');
-- 60 times:INSERT INTO t SELECTnumber, concat('file', cast(numberAS STRING), '.exe') FROM numbers('number'='5000');
SELECTcount(*) FROM t WHERE s REGEXP 'file1[.]exe$';
SELECTcount(*) FROM t WHERE s REGEXP '(?i)(alpha|bravo|charlie|delta|echo|foxtrot|golf|hotel|india|juliet|file1)[.](exe|dll)$';
With about 3.9k rowsets on one BE, the second query takes about 150 ms against about 72 ms for the first: about 77 ms of compiling. With the change below, both take about 47 ms. The gap grows linearly with the segment count.
Anything else?
Proposed change:
In the FRAGMENT_LOCAL open, compile a constant pattern and store it as fragment state. FunctionContext::clone() already shares that state.
In the THREAD_LOCAL open, share the fragment's Hyperscan database, which is read-only once compiled, and give each state its own scratch through hs_clone_scratch.
Keep compiling RE2 and Boost per state, because RE2 locks its lazily built DFA cache on every search, so one shared RE2 would serialise matching across scanner threads.
Non-constant patterns keep the current per-call path.
LikeSearchState::clone shares the database the same way.
Scope: this applies to LIKE/REGEXP, the only functions that compile a single pattern with Hyperscan. regexp_count, regexp_replace, regexp_extract and split_by_regexp also compile per THREAD_LOCAL open, but they use RE2. Building an RE2 is much cheaper than hs_compile, and sharing one RE2 across clones would contend on its DFA cache lock, so they are better left compiling per clone.
Update. We have run a fix in production. Two additions to the proposal above.
1. Share compiled databases across queries, not only across clones. Even after compiling once per fragment, every fragment instance of every query still compiles its constant LIKE/REGEXP pattern again. Some patterns take seconds to compile, and scheduled queries repeat the same patterns. A per-BE cache of compiled Hyperscan databases, keyed by expression, removes that:
Flags and mode are fixed, so the expression alone is the key.
A compiled database is read-only, so all users share it and each allocates only its own scratch.
Concurrent misses on one expression wait for a single compile.
Eviction is LRU, with a mutable byte cap.
regexp_fn, used when the pattern is constant within a block, uses the cache too.
RE2 and Boost fallbacks stay per state, uncached, because RE2 locks its DFA cache on every search.
The cache complements #66788. #66788 routes quantifiers above 50 to RE2; everything that stays on Hyperscan, including other compile-heavy shapes, now compiles once per BE.
2. A trap when caching failed compiles. A cache that keeps patterns Hyperscan rejects, so they go straight to the RE2 fallback, has to tell rejections apart from transient failures. hyperscan 5.4.2 and vectorscan 5.4.11 report "Unable to allocate memory.", "Internal error." and "Allocator returned misaligned memory." with expression == 0, despite hs_compile.h saying they are negative. So expression >= 0 is not enough on its own; those three messages have to be retried, not cached.
Result: a bounded-repeat (?i) pattern that takes ~14.8 s to compile drops to ~30 ms on every later run on the same BE. Happy to open a PR with both parts.
Activity
matanper commentedon Nov 1, 2026
Update. We have run a fix in production. Two additions to the proposal above.
1. Share compiled databases across queries, not only across clones. Even after compiling once per fragment, every fragment instance of every query still compiles its constant LIKE/REGEXP pattern again. Some patterns take seconds to compile, and scheduled queries repeat the same patterns. A per-BE cache of compiled Hyperscan databases, keyed by expression, removes that:
regexp_fn, used when the pattern is constant within a block, uses the cache too.The cache complements #66788. #66788 routes quantifiers above 50 to RE2; everything that stays on Hyperscan, including other compile-heavy shapes, now compiles once per BE.
2. A trap when caching failed compiles. A cache that keeps patterns Hyperscan rejects, so they go straight to the RE2 fallback, has to tell rejections apart from transient failures. hyperscan 5.4.2 and vectorscan 5.4.11 report "Unable to allocate memory.", "Internal error." and "Allocator returned misaligned memory." with
expression == 0, despitehs_compile.hsaying they are negative. Soexpression >= 0is not enough on its own; those three messages have to be retried, not cached.Result: a bounded-repeat
(?i)pattern that takes ~14.8 s to compile drops to ~30 ms on every later run on the same BE. Happy to open a PR with both parts.