924060929 commented on PR #64064:
URL: https://github.com/apache/doris/pull/64064#issuecomment-4841547593
Nice optimization — the pattern-shape guards (escape handling, char-class
tracking, inline-flag detection) are careful. One correctness issue on the
`regexp_replace` → `regexp_replace_one` rewrite:
**A quantified leading anchor defeats the "single match" assumption.**
`startsWithUnescapedCaret()` only checks `charAt(0) == '^'`, not whether the
`^` is an *effective* anchor. When the leading `^` can match zero times (`^?`,
`^*`, `^{0,n}`), the anchor is optional and the pattern can match at multiple
positions, so `GlobalReplace` ≠ `Replace`. These patterns still pass every
current guard (`charAt(0) == '^'`, no `|`, no `(?m)`) and get rewritten.
Verified on a live instance (RE2 behavior, independent of this PR):
```sql
regexp_replace('aa','^?a','X') -- 'XX' -> regexp_replace_one: 'Xa'
(wrong)
regexp_replace('aaa','^*a','X') -- 'XXX' -> 'Xaa' (wrong)
regexp_replace('aa','^{0,2}a','X') -- 'XX' -> 'Xa' (wrong)
```
Suggestion: treat the leading `^` as an anchor only when it is **not**
immediately followed by a zero-allowing quantifier (`?`, `*`, `{0,…}`); `^+` /
`^{1,}` stay safe. The trailing-`$` side isn't affected (a quantifier would
push `$` off the last position), and the `regexp_extract` `.*$` trim doesn't
rely on anchoring (verified equivalent).
Param handling otherwise looks good: arity (3/4 for replace, fixed-3 for
extract) is safe, and the group-index `>= 1` check is correct — no upper bound
needed, since the BE returns empty for out-of-range groups identically
before/after the rewrite.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]