Coverage Report

Created: 2025-04-23 14:20

/root/doris/be/src/exprs/string_functions.cpp
Line
Count
Source (jump to first uncovered line)
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
// NOTE: be careful not to use string::append.  It is not performant.
29
namespace doris {
30
31
// This function sets options in the RE2 library before pattern matching.
32
bool StringFunctions::set_re2_options(const StringRef& match_parameter, std::string* error_str,
33
10
                                      re2::RE2::Options* opts) {
34
22
    for (int i = 0; i < match_parameter.size; i++) {
35
14
        char match = match_parameter.data[i];
36
14
        switch (match) {
37
4
        case 'i':
38
4
            opts->set_case_sensitive(false);
39
4
            break;
40
2
        case 'c':
41
2
            opts->set_case_sensitive(true);
42
2
            break;
43
3
        case 'm':
44
3
            opts->set_posix_syntax(true);
45
3
            opts->set_one_line(false);
46
3
            break;
47
3
        case 'n':
48
3
            opts->set_never_nl(false);
49
3
            opts->set_dot_nl(true);
50
3
            break;
51
2
        default:
52
2
            std::stringstream error;
53
2
            error << "Illegal match parameter " << match;
54
2
            *error_str = error.str();
55
2
            return false;
56
14
        }
57
14
    }
58
8
    return true;
59
10
}
60
61
// The caller owns the returned regex. Returns nullptr if the pattern could not be compiled.
62
bool StringFunctions::compile_regex(const StringRef& pattern, std::string* error_str,
63
                                    const StringRef& match_parameter,
64
48
                                    std::unique_ptr<re2::RE2>& re) {
65
48
    re2::StringPiece pattern_sp(pattern.data, pattern.size);
66
48
    re2::RE2::Options options;
67
    // Disable error logging in case e.g. every row causes an error
68
48
    options.set_log_errors(false);
69
    // ATTN(cmy): no set it, or the lazy mode of regex won't work. See Doris #6587
70
    // Return the leftmost longest match (rather than the first match).
71
    // options.set_longest_match(true);
72
48
    options.set_dot_nl(true);
73
48
    if (match_parameter.size > 0 &&
74
48
        !StringFunctions::set_re2_options(match_parameter, error_str, &options)) {
75
1
        return false;
76
1
    }
77
47
    re.reset(new re2::RE2(pattern_sp, options));
78
47
    if (!re->ok()) {
79
1
        std::stringstream ss;
80
1
        ss << "Could not compile regexp pattern: " << std::string(pattern.data, pattern.size)
81
1
           << std::endl
82
1
           << "Error: " << re->error();
83
1
        *error_str = ss.str();
84
1
        re.reset();
85
1
        return false;
86
1
    }
87
46
    return true;
88
47
}
89
90
} // namespace doris