Coverage Report

Created: 2026-07-08 15:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/exprs/string_functions.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/exprs/string-functions.cpp
19
// and modified by Doris
20
21
#include "exprs/string_functions.h"
22
23
#include <re2/re2.h>
24
#include <re2/stringpiece.h>
25
26
#include <sstream>
27
28
#include "util/string_util.h"
29
30
// NOTE: be careful not to use string::append.  It is not performant.
31
namespace doris {
32
33
// This function sets options in the RE2 library before pattern matching.
34
bool StringFunctions::set_re2_options(const StringRef& match_parameter, std::string* error_str,
35
20
                                      re2::RE2::Options* opts) {
36
44
    for (int i = 0; i < match_parameter.size; i++) {
37
28
        char match = match_parameter.data[i];
38
28
        switch (match) {
39
8
        case 'i':
40
8
            opts->set_case_sensitive(false);
41
8
            break;
42
4
        case 'c':
43
4
            opts->set_case_sensitive(true);
44
4
            break;
45
6
        case 'm':
46
6
            opts->set_posix_syntax(true);
47
6
            opts->set_one_line(false);
48
6
            break;
49
6
        case 'n':
50
6
            opts->set_never_nl(false);
51
6
            opts->set_dot_nl(true);
52
6
            break;
53
4
        default:
54
4
            std::stringstream error;
55
4
            error << "Illegal match parameter " << match;
56
4
            *error_str = error.str();
57
4
            return false;
58
28
        }
59
28
    }
60
16
    return true;
61
20
}
62
63
// The caller owns the returned regex. Returns nullptr if the pattern could not be compiled.
64
bool StringFunctions::compile_regex(const StringRef& pattern, std::string* error_str,
65
                                    const StringRef& match_parameter,
66
84
                                    const StringRef& options_value, std::unique_ptr<re2::RE2>& re) {
67
84
    re2::StringPiece pattern_sp(pattern.data, pattern.size);
68
84
    re2::RE2::Options options;
69
    // Disable error logging in case e.g. every row causes an error
70
84
    options.set_log_errors(false);
71
    // ATTN(cmy): no set it, or the lazy mode of regex won't work. See Doris #6587
72
    // Return the leftmost longest match (rather than the first match).
73
    // options.set_longest_match(true);
74
84
    options.set_dot_nl(true);
75
76
84
    if ((options_value.data != nullptr) && (options_value.size > 0)) {
77
0
        auto options_split = split(options_value.to_string(), ",");
78
0
        for (const auto& option : options_split) {
79
0
            if (iequal("ignore_invalid_escape", option)) {
80
0
                options.set_ignore_replace_escape(true);
81
0
            } else {
82
                // "none" do nothing, and could add more options for future extensibility.
83
0
            }
84
0
        }
85
0
    }
86
87
84
    if (match_parameter.size > 0 &&
88
84
        !StringFunctions::set_re2_options(match_parameter, error_str, &options)) {
89
2
        return false;
90
2
    }
91
82
    re.reset(new re2::RE2(pattern_sp, options));
92
82
    if (!re->ok()) {
93
2
        std::stringstream ss;
94
2
        ss << "Could not compile regexp pattern: " << std::string(pattern.data, pattern.size)
95
2
           << std::endl
96
2
           << "Error: " << re->error();
97
2
        *error_str = ss.str();
98
2
        re.reset();
99
2
        return false;
100
2
    }
101
80
    return true;
102
82
}
103
104
} // namespace doris