feat: add RegexMatchIndexes so regex filters on string columns use indexes #20987
All Vouch review confidence checks passed
Residual risk 0.30 = Change Risk (0.52) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #20987"
general
weight: 0.35
amaechler
Description
Current State
Regex filters on string columns currently use
DruidPredicateIndexes. Every dictionary value is decoded from UTF-8 and run throughMatcher.find(). This is slow on high cardinality columns. On top of this, the index cost model often skips the index entirely.This is sad because there are plenty of real world scenarios with a structure that an index could use. For example
^/api/v2.only needs a dictionary range,^abc\zis a single lookup, and.timeout.is a substring search that needs no regex at all.New Semantic Index
RegexMatchIndexesMatching uses
Matcher.find()semantics, which is same asRegexDimFilterNull values never match and are reported as unknown
Returning
NULLmeans no index, and the filter falls back to existing predicate indexThe door is open for an implementation to return a superset index if it reports
isExact() == false. This lets column types that can only narrow candidates implement the interface.predicateis an optional check that accepts the same values aspattern. It is unused in this PR, but will b used by follow ups that are not in scope in this PR so this review can focus on the guts of the approach.RegexAnalysisRegexAnalysisis a conservative parser for a subset of Java regex syntax that matters for indexing. Anything it does not fully understand yields no facts, which means no index and fallback to existing behavior.There are two kinds of facts:
Exact: The pattern matches exactly the described values, so the index can skip the regex.
Necessary: Every match has evidence of being a potential match, so the index only narrows candidates and the regex must still be run on them.
String column implementation:
IndexedUtf8RegexMatchIndexesServed by
StringUtf8ColumnIndexSupplier(so legacy string columns and top-levelautostring columns both get it). Strategies, strongest first:1. Exact match (
^abc\z): one dictionary lookup.2. Prefix match* (
^abc,^abc.): binary search for the run of dictionary values whose UTF-8 bytes start with the prefix; union their bitmaps without decoding.3. Contains match (
abc,.abc.): scan the dictionary's UTF-8 bytes for the literal; no decoding and no regex. UTF-8 is self-synchronizing, so a byte match is a character match.4. Required prefix (
^abc.\.json\z): the same range as 2, then run the regex only on values in the range that contain every other required literal.5. Required literals (
timeout/\d+\.json): full scan, skip values missing any literal by byte search, run the regex on the rest.Every index returned is exact and invertible. Unsupported patterns return null.
Testing/Correctnes
The parser is checked by brute force, not by examples.
RegexAnalysisExhaustiveTestgenerates every pattern up to a few tokens long from a set of regex building blocks. For each pattern that compiles, it checks every claim the analysis makes against Matcher.find() on every short string, plus strings built around the claimed literals. Java's regex engine is the reference, so the test never has to define what correct means itself. A fast tier runs in every build. An extended tier, with more tokens and flags and enabled by a system property, takes about 13 minutes and passes.The analysis fails safe: any construct it doesn't recognize produces no claim, so the pattern falls back to today's predicate index.
IndexedUtf8RegexMatchIndexesTestchecks each index strategy against a brute-force scan on GenericIndexed and front-coded dictionaries.RegexFilterTestruns the filter across all segment types throughBaseFilterTest, including NOT and nullable and multi-value columns.Benchmarks
RegexMatchIndexBenchmarkcomputes the filter's index on a string column. 1M distinct URL-like values, JMH average time, 3 forks. master =880b924d9dwith only the benchmark added.| Pattern | Index path | master generic (ms) | PR generic (ms) | master front-coded (ms) | PR front-coded (ms) |
|---|---|---:|---:|---:|---:|
|
^/api/v2/orders/00000042\.json\z| exact lookup | 29.2 | 0.0006 | 34.0 | 0.0006 ||
^/api/v2/.| prefix range | 42.7 | 1.8 | 43.1 | 1.7 ||
^/api/.\.json\z| prefix range + regex | 50.5 | 11.0 | 52.1 | 23.2 ||
.timeout.| byte search | 1,227.8 | 18.9 | 1,152.3 | 29.1 ||
json| byte search | 80.5 | 23.6 | 92.6 | 32.7 ||
timeout/\d+\.json| literal prefilter + regex | 30.5 | 22.3 | 40.9 | 28.9 ||
[a-z]+\d{3}| none (unchanged) | 396.8 | 371.2 | 377.2 | 378.5 ||
NOT ^/api/v2/.| prefix range | 45.2 | 2.1 | 39.8 | 4.9 ||
NOT .timeout.| byte search | 1,038.3 | 17.1 | 1,274.7 | 29.0 |I found that Front-coded prefix ranges are sometimes slower than generic ones (e.g.
NOT ^/api/v2/.above). The time is in fetching and unioning one bitmap per value, the same path master's LIKE prefix uses, and it varies between JVM runs of identical code. Full results at 100k and 1M. I will paste more exhaustive benchmark tables in the comments (this description is already getting long)follow-ups
Planned as separate PRs that build on this interface without changing the analysis:
1.
RegexMatchIndexesfor nested string fields (NestedFieldColumnIndexSupplier).2. LIKE: translate the pattern to a single anchored regex and use
RegexMatchIndexes. This changes$to\zin the generated pattern, fixing an inconsistency where'abc\n' LIKE 'abc'matched via the row matcher but not via the index.3.
CONTAINS_STRING/ICONTAINS_STRING: these passpredicate(String.contains/containsIgnoreCase) so the per-value check keeps today's exact semantics.Release note
Regex filters (
REGEXP_LIKEand the nativeregexfilter) on string columns now use the dictionary to answer common pattern shapes directly: exact matches (^abc\z), prefixes (^abc), and substrings (abc,.abc.), and they skip values that cannot contain required literal text. These filters can be orders of magnitude faster on high-cardinality columns. Results are unchanged.<hr>
##### Key changed/added classes in this PR
RegexMatchIndexes(new semantic index)RegexAnalysis(new)IndexedUtf8RegexMatchIndexes(new)StringUtf8ColumnIndexSupplierRegexFilter,Filters*
RegexMatchIndexBenchmark(new)<hr>
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
This PR has: