matanper opened a new issue, #71065:
URL: https://github.com/apache/doris/issues/71065

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   The same symptom was reported in #27457 and #53071. Both were closed by the 
stale bot without a fix. The REGEXP paths have since been fixed on master by 
#68501, but the LIKE paths and `split_by_regexp` still log.
   
   ### Version
   
   master (`7d457d52b8`). Also 4.1.4, where REGEXP is affected as well.
   
   RE2 is constructed with `log_errors` left at its default (`true`) for a 
query's pattern in:
   
   - `be/src/exprs/function/like.cpp:212-215`, `LikeSearchState::clone`, the 
RE2 fallback for a LIKE predicate pushed down to storage. It is built per 
segment iterator.
   - `be/src/exprs/function/like.cpp:999-1002`, 
`FunctionLike::construct_like_const_state`, the RE2 fallback for a constant 
LIKE pattern that Hyperscan does not compile.
   - `be/src/exprs/function/function_split_by_regexp.cpp:229`, the options for 
every `split_by_regexp` pattern.
   
   `FunctionLikeBase::compile_regex` (`like.cpp:519-525`, used by REGEXP) and 
`StringFunctions::compile_regex` already call `set_log_errors(false)`.
   
   ### What's Wrong?
   
   When a pattern's DFA does not fit in RE2's `max_mem` (8 MiB by default), 
every search fails over from the DFA to the NFA. With `log_errors` on, RE2 
writes one line per search to stderr:
   
   ```
   .../thirdparty/src/re2-2021-02-02/re2/re2.cc:749: DFA out of memory: pattern 
length 3006, program size 21020, list count 12011, bytemap range 8
   ```
   
   That is one line per row the predicate evaluates. When the BE runs with 
`--console`, or with stderr redirected to `be.out`, nothing rotates that file. 
A single query over a few million rows writes hundreds of MB in minutes. 
Repeated queries fill the log volume, and the BE then fails.
   
   The NFA fallback is also far slower than the DFA. For a 143-character REGEXP 
made of bounded repeats 
(`(?i)bearer\s+([A-Za-z0-9._~+/=-]{8,1000}[A-Za-z0-9._~+/=-]{0,1000}...{0,96})`),
 which Hyperscan rejects with "Pattern is too large", RE2's DFA does not fit in 
8 MiB or 16 MiB. It does fit in 32 MiB. Over 200k rows of 40-640 bytes, the 
search took 8.3 µs per row at 8 MiB and 0.79 µs per row at 32 MiB or 64 MiB. 
Peak RSS was the same in each case, because the DFA cache grows on demand.
   
   ### What You Expected?
   
   - A query's pattern never makes RE2 write to stderr. Compile errors already 
reach the user through a `Status`, and an NFA fallback still returns the right 
result.
   - Ideally, the RE2 memory bound for a query's patterns can be configured, so 
that a large bounded-repeat pattern keeps the DFA.
   
   ### How to Reproduce?
   
   Start the BE with `start_be.sh --console`, or watch `be.out`, then run:
   
   ```sql
   CREATE TABLE re2_dfa_oom (id INT, s STRING)
   DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1
   PROPERTIES ("replication_num" = "1");
   
   INSERT INTO re2_dfa_oom
   SELECT number, repeat('abcdefghij', 30) FROM numbers("number" = "1000000");
   
   -- The LIKE converts to `.*a` + 3000 x `.` + `b.*`. Hyperscan rejects it 
with "Pattern is too
   -- large", so the BE falls back to RE2, whose DFA for this program (21020 
instructions) does not
   -- fit in 8 MiB.
   SELECT count(*) FROM re2_dfa_oom WHERE s LIKE concat('%a', repeat('_', 
3000), 'b%');
   ```
   
   `be.out` gains about one "DFA out of memory" line per row, about 1,000,000 
lines in total.
   
   We checked the mechanism outside Doris against the bundled `re2-2021-02-02` 
and Hyperscan. We used the options Doris uses: `set_never_nl(false)`, 
`set_dot_nl(true)` and default `max_mem`, plus `HS_FLAG_DOTALL | 
HS_FLAG_ALLOWEMPTY | HS_FLAG_UTF8`. `hs_compile` returns `Pattern is too 
large.`, and `RE2::PartialMatch` logs one line per call: 200 lines for 200 rows.
   
   ### Anything Else?
   
   `be.out` is not rotated at all (#71059), so the volume of these lines is 
what fills the disk.
   
   The proposed fix:
   
   - Build every RE2 that compiles a query's pattern from one options helper 
that sets `log_errors(false)`. That covers the two LIKE sites, 
`split_by_regexp`, and the inverted-index `get_regex_prefix` helpers, whose 
`PossibleMatchRange` also runs the DFA.
   - Add a BE config for `max_mem`, defaulting to 32 MiB. It is a per-object 
bound and is allocated lazily, so that large bounded-repeat patterns keep the 
DFA.
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   We have a patch and can open a PR.
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
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]

Reply via email to