Coverage Report

Created: 2026-09-17 08:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/bitmap_intersect.h
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
#pragma once
18
#include <parallel_hashmap/phmap.h>
19
20
#include "common/cast_set.h"
21
#include "core/string_ref.h"
22
#include "core/value/bitmap_value.h"
23
24
namespace doris {
25
26
namespace detail {
27
class Helper {
28
public:
29
    static const int DATETIME_PACKED_TIME_BYTE_SIZE = 8;
30
    static const int DATETIME_TYPE_BYTE_SIZE = 4;
31
    static const int DECIMAL_BYTE_SIZE = 16;
32
33
    // serialize_size start
34
    template <typename T>
35
20
    static int32_t serialize_size(const T& v) {
36
20
        return sizeof(T);
37
20
    }
Unexecuted instantiation: _ZN5doris6detail6Helper14serialize_sizeIaEEiRKT_
Unexecuted instantiation: _ZN5doris6detail6Helper14serialize_sizeIsEEiRKT_
_ZN5doris6detail6Helper14serialize_sizeIiEEiRKT_
Line
Count
Source
35
20
    static int32_t serialize_size(const T& v) {
36
20
        return sizeof(T);
37
20
    }
Unexecuted instantiation: _ZN5doris6detail6Helper14serialize_sizeIlEEiRKT_
Unexecuted instantiation: _ZN5doris6detail6Helper14serialize_sizeInEEiRKT_
38
39
    // write_to start
40
    template <typename T>
41
20
    static char* write_to(const T& v, char* dest) {
42
20
        size_t type_size = sizeof(T);
43
20
        memcpy(dest, &v, type_size);
44
20
        dest += type_size;
45
20
        return dest;
46
20
    }
Unexecuted instantiation: _ZN5doris6detail6Helper8write_toIaEEPcRKT_S3_
Unexecuted instantiation: _ZN5doris6detail6Helper8write_toIsEEPcRKT_S3_
_ZN5doris6detail6Helper8write_toIiEEPcRKT_S3_
Line
Count
Source
41
20
    static char* write_to(const T& v, char* dest) {
42
20
        size_t type_size = sizeof(T);
43
20
        memcpy(dest, &v, type_size);
44
20
        dest += type_size;
45
20
        return dest;
46
20
    }
Unexecuted instantiation: _ZN5doris6detail6Helper8write_toIlEEPcRKT_S3_
Unexecuted instantiation: _ZN5doris6detail6Helper8write_toInEEPcRKT_S3_
47
48
    // read_from start
49
    template <typename T>
50
28
    static void read_from(const char** src, T* result) {
51
28
        size_t type_size = sizeof(T);
52
28
        memcpy(result, *src, type_size);
53
28
        *src += type_size;
54
28
    }
Unexecuted instantiation: _ZN5doris6detail6Helper9read_fromIaEEvPPKcPT_
Unexecuted instantiation: _ZN5doris6detail6Helper9read_fromIsEEvPPKcPT_
_ZN5doris6detail6Helper9read_fromIiEEvPPKcPT_
Line
Count
Source
50
28
    static void read_from(const char** src, T* result) {
51
28
        size_t type_size = sizeof(T);
52
28
        memcpy(result, *src, type_size);
53
28
        *src += type_size;
54
28
    }
Unexecuted instantiation: _ZN5doris6detail6Helper9read_fromIlEEvPPKcPT_
Unexecuted instantiation: _ZN5doris6detail6Helper9read_fromInEEvPPKcPT_
55
};
56
57
template <>
58
0
inline char* Helper::write_to<VecDateTimeValue>(const VecDateTimeValue& v, char* dest) {
59
0
    *(int64_t*)dest = v.to_int64_datetime_packed();
60
0
    dest += DATETIME_PACKED_TIME_BYTE_SIZE;
61
0
    *(int*)dest = v.type();
62
0
    dest += DATETIME_TYPE_BYTE_SIZE;
63
0
    return dest;
64
0
}
65
66
template <>
67
0
inline char* Helper::write_to<DecimalV2Value>(const DecimalV2Value& v, char* dest) {
68
0
    __int128 value = v.value();
69
0
    memcpy(dest, &value, DECIMAL_BYTE_SIZE);
70
0
    dest += DECIMAL_BYTE_SIZE;
71
0
    return dest;
72
0
}
73
74
template <>
75
0
inline char* Helper::write_to<StringRef>(const StringRef& v, char* dest) {
76
0
    *(int32_t*)dest = cast_set<int32_t>(v.size);
77
0
    dest += 4;
78
0
    memcpy(dest, v.data, v.size);
79
0
    dest += v.size;
80
0
    return dest;
81
0
}
82
83
template <>
84
20
inline char* Helper::write_to<std::string>(const std::string& v, char* dest) {
85
20
    *(uint32_t*)dest = cast_set<uint32_t>(v.size());
86
20
    dest += 4;
87
20
    memcpy(dest, v.c_str(), v.size());
88
20
    dest += v.size();
89
20
    return dest;
90
20
}
91
// write_to end
92
93
template <>
94
0
inline int32_t Helper::serialize_size<VecDateTimeValue>(const VecDateTimeValue& v) {
95
0
    return Helper::DATETIME_PACKED_TIME_BYTE_SIZE + Helper::DATETIME_TYPE_BYTE_SIZE;
96
0
}
97
98
template <>
99
0
inline int32_t Helper::serialize_size<DecimalV2Value>(const DecimalV2Value& v) {
100
0
    return Helper::DECIMAL_BYTE_SIZE;
101
0
}
102
103
template <>
104
0
inline int32_t Helper::serialize_size<StringRef>(const StringRef& v) {
105
0
    return cast_set<int32_t>(v.size + 4);
106
0
}
107
108
template <>
109
20
inline int32_t Helper::serialize_size<std::string>(const std::string& v) {
110
20
    return cast_set<int32_t>(v.size() + 4);
111
20
}
112
// serialize_size end
113
114
template <>
115
0
inline void Helper::read_from<VecDateTimeValue>(const char** src, VecDateTimeValue* result) {
116
0
    result->from_packed_time(*(int64_t*)(*src));
117
0
    *src += DATETIME_PACKED_TIME_BYTE_SIZE;
118
0
    if (*(int*)(*src) == TIME_DATE) {
119
0
        result->cast_to_date();
120
0
    }
121
0
    *src += DATETIME_TYPE_BYTE_SIZE;
122
0
}
123
124
template <>
125
0
inline void Helper::read_from<DecimalV2Value>(const char** src, DecimalV2Value* result) {
126
0
    __int128 v = 0;
127
0
    memcpy(&v, *src, DECIMAL_BYTE_SIZE);
128
0
    *src += DECIMAL_BYTE_SIZE;
129
0
    *result = DecimalV2Value(v);
130
0
}
131
132
template <>
133
0
inline void Helper::read_from<StringRef>(const char** src, StringRef* result) {
134
0
    int32_t length = *(int32_t*)(*src);
135
0
    *src += 4;
136
0
    *result = StringRef((char*)*src, length);
137
0
    *src += length;
138
0
}
139
140
template <>
141
28
inline void Helper::read_from<std::string>(const char** src, std::string* result) {
142
28
    int32_t length = *(int32_t*)(*src);
143
28
    *src += 4;
144
28
    *result = std::string((char*)*src, length);
145
28
    *src += length;
146
28
}
147
// read_from end
148
} // namespace detail
149
150
// Calculate the intersection of two or more bitmaps
151
// Usage: intersect_count(bitmap_column_to_count, filter_column, filter_values ...)
152
// Example: intersect_count(user_id, event, 'A', 'B', 'C'), meaning find the intersect count of user_id in all A/B/C 3 bitmaps
153
// Todo(kks) Use Array type instead of variable arguments
154
template <typename T>
155
struct BitmapIntersect {
156
public:
157
148
    BitmapIntersect() = default;
Unexecuted instantiation: _ZN5doris15BitmapIntersectINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC2Ev
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaEC2Ev
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsEC2Ev
_ZN5doris15BitmapIntersectIiEC2Ev
Line
Count
Source
157
148
    BitmapIntersect() = default;
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlEC2Ev
Unexecuted instantiation: _ZN5doris15BitmapIntersectInEC2Ev
158
159
    explicit BitmapIntersect(const char* src) { deserialize(src); }
160
161
76
    void add_key(const T key) {
162
76
        BitmapValue empty_bitmap;
163
76
        _bitmaps[key] = empty_bitmap;
164
76
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE7add_keyES6_
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE7add_keyEa
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE7add_keyEs
_ZN5doris15BitmapIntersectIiE7add_keyEi
Line
Count
Source
161
76
    void add_key(const T key) {
162
76
        BitmapValue empty_bitmap;
163
76
        _bitmaps[key] = empty_bitmap;
164
76
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE7add_keyEl
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE7add_keyEn
165
166
80
    void update(const T& key, const BitmapValue& bitmap) {
167
80
        if (_bitmaps.find(key) != _bitmaps.end()) {
168
40
            _bitmaps[key] |= bitmap;
169
40
        }
170
80
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE6updateERKS6_RKNS_11BitmapValueE
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE6updateERKaRKNS_11BitmapValueE
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE6updateERKsRKNS_11BitmapValueE
_ZN5doris15BitmapIntersectIiE6updateERKiRKNS_11BitmapValueE
Line
Count
Source
166
80
    void update(const T& key, const BitmapValue& bitmap) {
167
80
        if (_bitmaps.find(key) != _bitmaps.end()) {
168
40
            _bitmaps[key] |= bitmap;
169
40
        }
170
80
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE6updateERKlRKNS_11BitmapValueE
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE6updateERKnRKNS_11BitmapValueE
171
172
20
    bool has_same_keys(const BitmapIntersect& other) const {
173
20
        if (_bitmaps.size() != other._bitmaps.size()) {
174
0
            return false;
175
0
        }
176
20
        for (const auto& [key, bitmap] : _bitmaps) {
177
20
            if (!other._bitmaps.contains(key)) {
178
16
                return false;
179
16
            }
180
20
        }
181
4
        return true;
182
20
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIaE13has_same_keysERKS1_
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIsE13has_same_keysERKS1_
_ZNK5doris15BitmapIntersectIiE13has_same_keysERKS1_
Line
Count
Source
172
20
    bool has_same_keys(const BitmapIntersect& other) const {
173
20
        if (_bitmaps.size() != other._bitmaps.size()) {
174
0
            return false;
175
0
        }
176
20
        for (const auto& [key, bitmap] : _bitmaps) {
177
20
            if (!other._bitmaps.contains(key)) {
178
16
                return false;
179
16
            }
180
20
        }
181
4
        return true;
182
20
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIlE13has_same_keysERKS1_
Unexecuted instantiation: _ZNK5doris15BitmapIntersectInE13has_same_keysERKS1_
183
184
28
    void merge(const BitmapIntersect& other) {
185
28
        for (auto& kv : other._bitmaps) {
186
28
            if (_bitmaps.find(kv.first) != _bitmaps.end()) {
187
4
                _bitmaps[kv.first] |= kv.second;
188
24
            } else {
189
24
                _bitmaps[kv.first] = kv.second;
190
24
            }
191
28
        }
192
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE5mergeERKS1_
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE5mergeERKS1_
_ZN5doris15BitmapIntersectIiE5mergeERKS1_
Line
Count
Source
184
28
    void merge(const BitmapIntersect& other) {
185
28
        for (auto& kv : other._bitmaps) {
186
28
            if (_bitmaps.find(kv.first) != _bitmaps.end()) {
187
4
                _bitmaps[kv.first] |= kv.second;
188
24
            } else {
189
24
                _bitmaps[kv.first] = kv.second;
190
24
            }
191
28
        }
192
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE5mergeERKS1_
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE5mergeERKS1_
193
194
    // intersection
195
56
    BitmapValue intersect() const {
196
56
        BitmapValue result;
197
56
        if (_bitmaps.empty()) {
198
0
            return result;
199
0
        }
200
56
        auto it = _bitmaps.begin();
201
56
        result |= it->second;
202
56
        it++;
203
56
        for (; it != _bitmaps.end(); it++) {
204
0
            result &= it->second;
205
0
        }
206
56
        return result;
207
56
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIaE9intersectEv
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIsE9intersectEv
_ZNK5doris15BitmapIntersectIiE9intersectEv
Line
Count
Source
195
56
    BitmapValue intersect() const {
196
56
        BitmapValue result;
197
56
        if (_bitmaps.empty()) {
198
0
            return result;
199
0
        }
200
56
        auto it = _bitmaps.begin();
201
56
        result |= it->second;
202
56
        it++;
203
56
        for (; it != _bitmaps.end(); it++) {
204
0
            result &= it->second;
205
0
        }
206
56
        return result;
207
56
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIlE9intersectEv
Unexecuted instantiation: _ZNK5doris15BitmapIntersectInE9intersectEv
208
209
    // calculate the intersection for _bitmaps's bitmap values
210
56
    int64_t intersect_count() const {
211
56
        if (_bitmaps.empty()) {
212
0
            return 0;
213
0
        }
214
56
        return intersect().cardinality();
215
56
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIaE15intersect_countEv
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIsE15intersect_countEv
_ZNK5doris15BitmapIntersectIiE15intersect_countEv
Line
Count
Source
210
56
    int64_t intersect_count() const {
211
56
        if (_bitmaps.empty()) {
212
0
            return 0;
213
0
        }
214
56
        return intersect().cardinality();
215
56
    }
Unexecuted instantiation: _ZNK5doris15BitmapIntersectIlE15intersect_countEv
Unexecuted instantiation: _ZNK5doris15BitmapIntersectInE15intersect_countEv
216
217
    // the serialize size
218
28
    size_t size() {
219
28
        size_t size = 4;
220
28
        for (auto& kv : _bitmaps) {
221
20
            size += detail::Helper::serialize_size(kv.first);
222
20
            size += kv.second.getSizeInBytes();
223
20
        }
224
28
        return size;
225
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE4sizeEv
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE4sizeEv
_ZN5doris15BitmapIntersectIiE4sizeEv
Line
Count
Source
218
28
    size_t size() {
219
28
        size_t size = 4;
220
28
        for (auto& kv : _bitmaps) {
221
20
            size += detail::Helper::serialize_size(kv.first);
222
20
            size += kv.second.getSizeInBytes();
223
20
        }
224
28
        return size;
225
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE4sizeEv
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE4sizeEv
226
227
    //must call size() first
228
28
    void serialize(char* dest) {
229
28
        char* writer = dest;
230
28
        *(int32_t*)writer = cast_set<int32_t>(_bitmaps.size());
231
28
        writer += 4;
232
28
        for (auto& kv : _bitmaps) {
233
20
            writer = detail::Helper::write_to(kv.first, writer);
234
20
            kv.second.write_to(writer);
235
20
            writer += kv.second.getSizeInBytes();
236
20
        }
237
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE9serializeEPc
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE9serializeEPc
_ZN5doris15BitmapIntersectIiE9serializeEPc
Line
Count
Source
228
28
    void serialize(char* dest) {
229
28
        char* writer = dest;
230
28
        *(int32_t*)writer = cast_set<int32_t>(_bitmaps.size());
231
28
        writer += 4;
232
28
        for (auto& kv : _bitmaps) {
233
20
            writer = detail::Helper::write_to(kv.first, writer);
234
20
            kv.second.write_to(writer);
235
20
            writer += kv.second.getSizeInBytes();
236
20
        }
237
28
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE9serializeEPc
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE9serializeEPc
238
239
36
    void deserialize(const char* src) {
240
36
        const char* reader = src;
241
36
        int32_t bitmaps_size = *(int32_t*)reader;
242
36
        reader += 4;
243
64
        for (int32_t i = 0; i < bitmaps_size; i++) {
244
28
            T key;
245
28
            detail::Helper::read_from(&reader, &key);
246
28
            BitmapValue bitmap(reader);
247
28
            reader += bitmap.getSizeInBytes();
248
28
            _bitmaps[key] = bitmap;
249
28
        }
250
36
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIaE11deserializeEPKc
Unexecuted instantiation: _ZN5doris15BitmapIntersectIsE11deserializeEPKc
_ZN5doris15BitmapIntersectIiE11deserializeEPKc
Line
Count
Source
239
36
    void deserialize(const char* src) {
240
36
        const char* reader = src;
241
36
        int32_t bitmaps_size = *(int32_t*)reader;
242
36
        reader += 4;
243
64
        for (int32_t i = 0; i < bitmaps_size; i++) {
244
28
            T key;
245
28
            detail::Helper::read_from(&reader, &key);
246
28
            BitmapValue bitmap(reader);
247
28
            reader += bitmap.getSizeInBytes();
248
28
            _bitmaps[key] = bitmap;
249
28
        }
250
36
    }
Unexecuted instantiation: _ZN5doris15BitmapIntersectIlE11deserializeEPKc
Unexecuted instantiation: _ZN5doris15BitmapIntersectInE11deserializeEPKc
Unexecuted instantiation: _ZN5doris15BitmapIntersectINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE11deserializeEPKc
251
252
protected:
253
    std::map<T, BitmapValue> _bitmaps;
254
};
255
256
template <>
257
struct BitmapIntersect<std::string_view> {
258
public:
259
148
    BitmapIntersect() = default;
260
261
0
    explicit BitmapIntersect(const char* src) { deserialize(src); }
262
263
76
    void add_key(const std::string_view key) {
264
76
        BitmapValue empty_bitmap;
265
76
        _bitmaps[key] = empty_bitmap;
266
76
    }
267
268
80
    void update(const std::string_view& key, const BitmapValue& bitmap) {
269
80
        if (_bitmaps.find(key) != _bitmaps.end()) {
270
40
            _bitmaps[key] |= bitmap;
271
40
        }
272
80
    }
273
274
20
    bool has_same_keys(const BitmapIntersect& other) const {
275
20
        if (_bitmaps.size() != other._bitmaps.size()) {
276
0
            return false;
277
0
        }
278
20
        for (const auto& [key, bitmap] : _bitmaps) {
279
20
            if (!other._bitmaps.contains(key)) {
280
16
                return false;
281
16
            }
282
20
        }
283
4
        return true;
284
20
    }
285
286
28
    void merge(const BitmapIntersect& other) {
287
28
        for (auto& kv : other._bitmaps) {
288
28
            if (_bitmaps.find(kv.first) != _bitmaps.end()) {
289
4
                _bitmaps[kv.first] |= kv.second;
290
24
            } else {
291
24
                _bitmaps[kv.first] = kv.second;
292
24
            }
293
28
        }
294
28
    }
295
296
    // intersection
297
56
    BitmapValue intersect() const {
298
56
        BitmapValue result;
299
56
        auto it = _bitmaps.begin();
300
56
        result |= it->second;
301
56
        it++;
302
56
        for (; it != _bitmaps.end(); it++) {
303
0
            result &= it->second;
304
0
        }
305
56
        return result;
306
56
    }
307
308
    // calculate the intersection for _bitmaps's bitmap values
309
56
    int64_t intersect_count() const {
310
56
        if (_bitmaps.empty()) {
311
0
            return 0;
312
0
        }
313
56
        return intersect().cardinality();
314
56
    }
315
316
    // the serialize size
317
28
    size_t size() {
318
28
        size_t size = 4;
319
28
        for (auto& kv : _bitmaps) {
320
20
            size += detail::Helper::serialize_size(kv.first);
321
20
            size += kv.second.getSizeInBytes();
322
20
        }
323
28
        return size;
324
28
    }
325
326
    //must call size() first
327
28
    void serialize(char* dest) {
328
28
        char* writer = dest;
329
28
        *(int32_t*)writer = cast_set<int32_t>(_bitmaps.size());
330
28
        writer += 4;
331
28
        for (auto& kv : _bitmaps) {
332
20
            writer = detail::Helper::write_to(kv.first, writer);
333
20
            kv.second.write_to(writer);
334
20
            writer += kv.second.getSizeInBytes();
335
20
        }
336
28
    }
337
338
36
    void deserialize(const char* src) {
339
36
        const char* reader = src;
340
36
        int32_t bitmaps_size = *(int32_t*)reader;
341
36
        reader += 4;
342
64
        for (int32_t i = 0; i < bitmaps_size; i++) {
343
28
            std::string key;
344
28
            detail::Helper::read_from(&reader, &key);
345
28
            BitmapValue bitmap(reader);
346
28
            reader += bitmap.getSizeInBytes();
347
28
            _bitmaps[key] = bitmap;
348
28
        }
349
36
    }
350
351
protected:
352
    phmap::flat_hash_map<std::string, BitmapValue> _bitmaps;
353
};
354
} // namespace doris