Coverage Report

Created: 2024-11-22 21:49

/root/doris/be/src/exec/olap_utils.h
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
18
#pragma once
19
20
#include <fmt/core.h>
21
#include <gen_cpp/Opcodes_types.h>
22
#include <glog/logging.h>
23
#include <math.h>
24
25
#include "common/logging.h"
26
#include "olap/olap_tuple.h"
27
#include "runtime/primitive_type.h"
28
29
namespace doris {
30
31
using CompareLargeFunc = bool (*)(const void*, const void*);
32
33
static const char* NEGATIVE_INFINITY = "-oo";
34
static const char* POSITIVE_INFINITY = "+oo";
35
36
struct OlapScanRange {
37
public:
38
0
    OlapScanRange() : begin_include(true), end_include(true) {
39
0
        begin_scan_range.add_value(NEGATIVE_INFINITY);
40
0
        end_scan_range.add_value(POSITIVE_INFINITY);
41
0
    }
42
    OlapScanRange(bool begin, bool end, std::vector<std::string>& begin_range,
43
                  std::vector<std::string>& end_range)
44
            : begin_include(begin),
45
              end_include(end),
46
              begin_scan_range(begin_range),
47
0
              end_scan_range(end_range) {}
48
49
    bool begin_include;
50
    bool end_include;
51
    OlapTuple begin_scan_range;
52
    OlapTuple end_scan_range;
53
54
0
    std::string debug_string() const {
55
0
        fmt::memory_buffer buf;
56
0
        DCHECK_EQ(begin_scan_range.size(), end_scan_range.size());
57
0
        for (int i = 0; i < begin_scan_range.size(); i++) {
58
0
            fmt::format_to(buf, "({}, {})\n", begin_scan_range[i], end_scan_range[i]);
59
0
        }
60
0
        return fmt::to_string(buf);
61
0
    }
62
};
63
64
enum SQLFilterOp {
65
    FILTER_LARGER = 0,
66
    FILTER_LARGER_OR_EQUAL = 1,
67
    FILTER_LESS = 2,
68
    FILTER_LESS_OR_EQUAL = 3,
69
    FILTER_IN = 4,
70
    FILTER_NOT_IN = 5
71
};
72
73
template <PrimitiveType>
74
constexpr bool always_false_v = false;
75
76
0
inline SQLFilterOp to_olap_filter_type(TExprOpcode::type type, bool opposite) {
77
0
    switch (type) {
78
0
    case TExprOpcode::LT:
79
0
        return opposite ? FILTER_LARGER : FILTER_LESS;
80
0
81
0
    case TExprOpcode::LE:
82
0
        return opposite ? FILTER_LARGER_OR_EQUAL : FILTER_LESS_OR_EQUAL;
83
0
84
0
    case TExprOpcode::GT:
85
0
        return opposite ? FILTER_LESS : FILTER_LARGER;
86
0
87
0
    case TExprOpcode::GE:
88
0
        return opposite ? FILTER_LESS_OR_EQUAL : FILTER_LARGER_OR_EQUAL;
89
0
90
0
    case TExprOpcode::EQ:
91
0
        return opposite ? FILTER_NOT_IN : FILTER_IN;
92
0
93
0
    case TExprOpcode::NE:
94
0
        return opposite ? FILTER_IN : FILTER_NOT_IN;
95
0
96
0
    case TExprOpcode::EQ_FOR_NULL:
97
0
        return FILTER_IN;
98
0
99
0
    default:
100
0
        VLOG_CRITICAL << "TExprOpcode: " << type;
101
0
        DCHECK(false);
102
0
    }
103
0
104
0
    return FILTER_IN;
105
0
}
106
107
0
inline SQLFilterOp to_olap_filter_type(const std::string& function_name, bool opposite) {
108
0
    if (function_name == "lt") {
109
0
        return opposite ? FILTER_LARGER : FILTER_LESS;
110
0
    } else if (function_name == "gt") {
111
0
        return opposite ? FILTER_LESS : FILTER_LARGER;
112
0
    } else if (function_name == "le") {
113
0
        return opposite ? FILTER_LARGER_OR_EQUAL : FILTER_LESS_OR_EQUAL;
114
0
    } else if (function_name == "ge") {
115
0
        return opposite ? FILTER_LESS_OR_EQUAL : FILTER_LARGER_OR_EQUAL;
116
0
    } else if (function_name == "eq") {
117
0
        return opposite ? FILTER_NOT_IN : FILTER_IN;
118
0
    } else if (function_name == "ne") {
119
0
        return opposite ? FILTER_IN : FILTER_NOT_IN;
120
0
    } else if (function_name == "in") {
121
0
        return opposite ? FILTER_NOT_IN : FILTER_IN;
122
0
    } else if (function_name == "not_in") {
123
0
        return opposite ? FILTER_IN : FILTER_NOT_IN;
124
0
    } else {
125
0
        DCHECK(false) << "Function Name: " << function_name;
126
0
        return FILTER_IN;
127
0
    }
128
0
}
129
130
enum class MatchType {
131
    UNKNOWN = -1,
132
    MATCH_ANY = 0,
133
    MATCH_ALL = 1,
134
    MATCH_PHRASE = 2,
135
    MATCH_PHRASE_PREFIX = 8,
136
    MATCH_REGEXP = 9,
137
    MATCH_PHRASE_EDGE = 10,
138
};
139
140
0
inline MatchType to_match_type(TExprOpcode::type type) {
141
0
    switch (type) {
142
0
    case TExprOpcode::type::MATCH_ANY:
143
0
        return MatchType::MATCH_ANY;
144
0
        break;
145
0
    case TExprOpcode::type::MATCH_ALL:
146
0
        return MatchType::MATCH_ALL;
147
0
        break;
148
0
    case TExprOpcode::type::MATCH_PHRASE:
149
0
        return MatchType::MATCH_PHRASE;
150
0
        break;
151
0
    case TExprOpcode::type::MATCH_PHRASE_PREFIX:
152
0
        return MatchType::MATCH_PHRASE_PREFIX;
153
0
        break;
154
0
    case TExprOpcode::type::MATCH_REGEXP:
155
0
        return MatchType::MATCH_REGEXP;
156
0
        break;
157
0
    case TExprOpcode::type::MATCH_PHRASE_EDGE:
158
0
        return MatchType::MATCH_PHRASE_EDGE;
159
0
        break;
160
0
    default:
161
0
        VLOG_CRITICAL << "TExprOpcode: " << type;
162
0
        DCHECK(false);
163
0
    }
164
0
    return MatchType::MATCH_ANY;
165
0
}
166
167
0
inline MatchType to_match_type(const std::string& condition_op) {
168
0
    if (condition_op.compare("match_any") == 0) {
169
0
        return MatchType::MATCH_ANY;
170
0
    } else if (condition_op.compare("match_all") == 0) {
171
0
        return MatchType::MATCH_ALL;
172
0
    } else if (condition_op.compare("match_phrase") == 0) {
173
0
        return MatchType::MATCH_PHRASE;
174
0
    } else if (condition_op.compare("match_phrase_prefix") == 0) {
175
0
        return MatchType::MATCH_PHRASE_PREFIX;
176
0
    } else if (condition_op.compare("match_regexp") == 0) {
177
0
        return MatchType::MATCH_REGEXP;
178
0
    } else if (condition_op.compare("match_phrase_edge") == 0) {
179
0
        return MatchType::MATCH_PHRASE_EDGE;
180
0
    }
181
0
    return MatchType::UNKNOWN;
182
0
}
183
184
100
inline bool is_match_condition(const std::string& op) {
185
100
    if (0 == strcasecmp(op.c_str(), "match_any") || 0 == strcasecmp(op.c_str(), "match_all") ||
186
100
        0 == strcasecmp(op.c_str(), "match_phrase") ||
187
100
        0 == strcasecmp(op.c_str(), "match_phrase_prefix") ||
188
100
        0 == strcasecmp(op.c_str(), "match_regexp") ||
189
100
        0 == strcasecmp(op.c_str(), "match_phrase_edge")) {
190
0
        return true;
191
0
    }
192
100
    return false;
193
100
}
194
195
0
inline bool is_match_operator(const TExprOpcode::type& op_type) {
196
0
    return TExprOpcode::MATCH_ANY == op_type || TExprOpcode::MATCH_ALL == op_type ||
197
0
           TExprOpcode::MATCH_PHRASE == op_type || TExprOpcode::MATCH_PHRASE_PREFIX == op_type ||
198
0
           TExprOpcode::MATCH_REGEXP == op_type || TExprOpcode::MATCH_PHRASE_EDGE == op_type;
199
0
}
200
201
} // namespace doris