Xianqing He created IMPALA-15550:
------------------------------------
Summary: Optimize regexp_replace() for constant patterns
Key: IMPALA-15550
URL: https://issues.apache.org/jira/browse/IMPALA-15550
Project: IMPALA
Issue Type: Improvement
Components: Backend
Affects Versions: Impala 5.0.0
Reporter: Xianqing He
h3. Problem
regexp_replace() with a constant pattern still runs the full RE2 path on every
evaluation: std::string conversion, RE2::GlobalReplace() and a copy of the
result. BI queries often evaluate simple expressions such as
{{upper(regexp_replace(col, '[0-9]', ''))}} repeatedly in grouping expressions,
paying this cost several times per row.
h3. Proposed change
Add fast paths for constant patterns that are a plain literal or a single ASCII
character class:
* Plain literal: reuse the StringSearch-based replacement loop of replace().
* Character class: match bytes directly instead of calling RE2. Positive
classes support any replacement; negated classes only support deletion on
ASCII-only input.
* Fall back to RE2 with unchanged behavior for everything else: backslash in
the replacement (RE2 escapes/backreferences), negated class with non-empty
replacement or non-ASCII input, empty pattern, quantifiers/groups/predefined
classes/non-ASCII classes, non-constant patterns, and results exceeding
StringVal::MAX_LENGTH.
regexp_replace() also gets its own prepare/close state instead of sharing
RegexpPrepare/RegexpClose with regexp_extract() and regexp_match_count().
h3. Testing
* New expr-test.cc cases cover the literal and class fast paths, fallbacks
(backslash replacement, negated classes with non-ASCII input, '[a-]', groups),
empty/no/full matches and invalid UTF-8.
* expr-benchmark shows ~2.3x for {{regexp_replace(col, '[0-9]', '')}} and ~1.5x
for a literal pattern, with larger gains when codegen is enabled.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)