← Back to Pull requests

feat: add RegexMatchIndexes so regex filters on string columns use indexes #20987

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Dec 01, 2026 01:05 UTC
Author

Description

Current State

Regex filters on string columns currently use DruidPredicateIndexes. Every dictionary value is decoded from UTF-8 and run through Matcher.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\z is a single lookup, and .timeout. is a substring search that needs no regex at all.

New Semantic Index RegexMatchIndexes

@Nullable
BitmapColumnIndex forPattern(Pattern pattern, @Nullable Predicate<String> predicate);

Matching uses Matcher.find() semantics, which is same as RegexDimFilter
Null values never match and are reported as unknown
Returning NULL means no index, and the filter falls back to existing predicate index
The 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.
predicate is an optional check that accepts the same values as pattern. 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.

RegexAnalysis

RegexAnalysis is 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: IndexedUtf8RegexMatchIndexes

Served by StringUtf8ColumnIndexSupplier (so legacy string columns and top-level auto string 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. RegexAnalysisExhaustiveTest generates 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.

IndexedUtf8RegexMatchIndexesTest checks each index strategy against a brute-force scan on GenericIndexed and front-coded dictionaries. RegexFilterTest runs the filter across all segment types through BaseFilterTest, including NOT and nullable and multi-value columns.

Benchmarks

RegexMatchIndexBenchmark computes the filter's index on a string column. 1M distinct URL-like values, JMH average time, 3 forks. master = 880b924d9d with 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. RegexMatchIndexes for nested string fields (NestedFieldColumnIndexSupplier).
2. LIKE: translate the pattern to a single anchored regex and use RegexMatchIndexes. This changes $ to \z in 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 pass predicate (String.contains / containsIgnoreCase) so the per-value check keeps today's exact semantics.

Release note

Regex filters (REGEXP_LIKE and the native regex filter) 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)
StringUtf8ColumnIndexSupplier
RegexFilter, 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:

  • been self-reviewed.

  • added documentation for new or modified features or behaviors.

  • a release note entry in the PR description.

  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added or updated version, license, or notice information in licenses.yaml

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

  • added integration tests.

  • been tested in a test Druid cluster.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: amaechler Review duration: 3417m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%
    Repository