Coverage Report

Created: 2026-03-16 03:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/row_ranges.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
18
#pragma once
19
20
#include <roaring/roaring.hh>
21
#include <string>
22
#include <vector>
23
24
#include "absl/strings/substitute.h"
25
#include "common/cast_set.h"
26
#include "common/logging.h"
27
#include "storage/segment/common.h"
28
29
namespace doris {
30
#include "common/compile_check_begin.h"
31
namespace segment_v2 {
32
33
// RowRange stands for range[From, To), From is inclusive,
34
// To is exclusive. It is used for row id range calculation.
35
class RowRange {
36
public:
37
    // Returns true if two ranges are overlapped or false.
38
    // The union range will be returned through range.
39
104k
    static bool range_union(const RowRange& left, const RowRange& right, RowRange* range) {
40
104k
        if (left._from <= right._from) {
41
104k
            if (left._to >= right._from) {
42
93.9k
                range->_from = left._from;
43
93.9k
                range->_to = std::max(left._to, right._to);
44
93.9k
                return true;
45
93.9k
            }
46
18.4E
        } else if (right._to >= left._from) {
47
0
            range->_from = right._from;
48
0
            range->_to = std::max(left._to, right._to);
49
0
            return true;
50
0
        }
51
        // return a invalid range
52
10.3k
        range->_from = 0;
53
10.3k
        range->_to = 0;
54
10.3k
        return false;
55
104k
    }
56
57
    // Returns true if the two ranges are intersected or false.
58
    // The intersection of the two ranges is returned through range.
59
1.40M
    static bool range_intersection(const RowRange& left, const RowRange& right, RowRange* range) {
60
1.40M
        if (left._from <= right._from) {
61
487k
            if (left._to > right._from) {
62
487k
                range->_from = right._from;
63
487k
                range->_to = std::min(left._to, right._to);
64
487k
                return true;
65
487k
            }
66
921k
        } else if (right._to > left._from) {
67
921k
            range->_from = left._from;
68
921k
            range->_to = std::min(left._to, right._to);
69
921k
            return true;
70
921k
        }
71
        // return a invalid range
72
18.4E
        range->_from = 0;
73
18.4E
        range->_to = 0;
74
18.4E
        return false;
75
1.40M
    }
76
77
1.53M
    RowRange() : _from(0), _to(0) {}
78
79
    // Creates a range of [from, to) (from inclusive and to exclusive; empty ranges are invalid)
80
6.11M
    RowRange(int64_t from, int64_t to) : _from(from), _to(to) {}
81
82
    bool is_valid() const { return _from < _to; }
83
84
14.3M
    size_t count() const { return _to - _from; }
85
86
6.59M
    bool is_before(const RowRange& other) const { return _to <= other._from; }
87
88
6.41M
    bool is_after(const RowRange& other) const { return _from >= other._to; }
89
90
4.90M
    int64_t from() const { return _from; }
91
92
4.43M
    int64_t to() const { return _to; }
93
94
0
    std::string to_string() const { return absl::Substitute("[$0-$1)", _from, _to); }
95
96
1.20M
    uint64_t get_digest(uint64_t seed) const {
97
1.20M
        uint64_t hash = seed;
98
1.20M
        hash = hash * 31 + _from;
99
1.20M
        hash = hash * 31 + _to;
100
1.20M
        return hash;
101
1.20M
    }
102
103
private:
104
    int64_t _from;
105
    int64_t _to;
106
};
107
108
class RowRanges {
109
public:
110
24.8M
    RowRanges() : _count(0) {}
111
112
982k
    void clear() {
113
982k
        _ranges.clear();
114
982k
        _count = 0;
115
982k
    }
116
117
    // Creates a new RowRanges object with the single range [0, row_count).
118
429k
    static RowRanges create_single(uint64_t row_count) {
119
429k
        RowRanges ranges;
120
429k
        ranges.add(RowRange(0, row_count));
121
429k
        return ranges;
122
429k
    }
123
124
    // Creates a new RowRanges object with the single range [from, to).
125
2.20M
    static RowRanges create_single(int64_t from, int64_t to) {
126
2.20M
        DCHECK(from <= to);
127
2.20M
        RowRanges ranges;
128
2.20M
        ranges.add(RowRange(from, to));
129
2.20M
        return ranges;
130
2.20M
    }
131
132
    // Calculates the union of the two specified RowRanges object. The union of two range is calculated if there are
133
    // elements between them. Otherwise, the two disjunct ranges are stored separately.
134
    // For example:
135
    // [113, 241) ∪ [221, 340) = [113, 340)
136
    // [113, 230) ∪ [230, 340) = [113, 340]
137
    // while
138
    // [113, 230) ∪ [231, 340) = [113, 230), [231, 340)
139
2.20M
    static void ranges_union(const RowRanges& left, const RowRanges& right, RowRanges* result) {
140
2.20M
        RowRanges tmp_range;
141
2.20M
        auto it1 = left._ranges.begin();
142
2.20M
        auto it2 = right._ranges.begin();
143
        // merge and add
144
2.27M
        while (it1 != left._ranges.end() && it2 != right._ranges.end()) {
145
73.3k
            if (it1->is_after(*it2)) {
146
0
                tmp_range.add(*it2);
147
0
                ++it2;
148
73.3k
            } else {
149
73.3k
                tmp_range.add(*it1);
150
73.3k
                ++it1;
151
73.3k
            }
152
73.3k
        }
153
2.39M
        while (it1 != left._ranges.end()) {
154
188k
            tmp_range.add(*it1);
155
188k
            ++it1;
156
188k
        }
157
2.51M
        while (it2 != right._ranges.end()) {
158
315k
            tmp_range.add(*it2);
159
315k
            ++it2;
160
315k
        }
161
2.20M
        *result = std::move(tmp_range);
162
2.20M
    }
163
164
    // Calculates the intersection of the two specified RowRanges object. Two ranges intersect if they have common
165
    // elements otherwise the result is empty.
166
    // For example:
167
    // [113, 241) ∩ [221, 340) = [221, 241)
168
    // while
169
    // [113, 230) ∩ [230, 340) = <EMPTY>
170
    //
171
    // The result RowRanges object will contain all the row indexes there were contained in both of the specified objects
172
    static void ranges_intersection(const RowRanges& left, const RowRanges& right,
173
2.56M
                                    RowRanges* result) {
174
2.56M
        RowRanges tmp_range;
175
2.56M
        int right_index = 0;
176
5.13M
        for (auto it1 = left._ranges.begin(); it1 != left._ranges.end(); ++it1) {
177
2.56M
            const RowRange& range1 = *it1;
178
8.80M
            for (int i = right_index; i < right._ranges.size(); ++i) {
179
6.59M
                const RowRange& range2 = right._ranges[i];
180
6.59M
                if (range1.is_before(range2)) {
181
363k
                    break;
182
6.23M
                } else if (range1.is_after(range2)) {
183
4.82M
                    right_index = i + 1;
184
4.82M
                    continue;
185
4.82M
                }
186
1.40M
                RowRange merge_range;
187
1.40M
                bool ret = RowRange::range_intersection(range1, range2, &merge_range);
188
1.40M
                DCHECK(ret);
189
1.40M
                tmp_range.add(merge_range);
190
1.40M
            }
191
2.56M
        }
192
2.56M
        *result = std::move(tmp_range);
193
2.56M
    }
194
195
2.92M
    static roaring::Roaring ranges_to_roaring(const RowRanges& ranges) {
196
2.92M
        roaring::Roaring result;
197
4.31M
        for (auto it = ranges._ranges.begin(); it != ranges._ranges.end(); ++it) {
198
1.38M
            result.addRange(it->from(), it->to());
199
1.38M
        }
200
2.92M
        return result;
201
2.92M
    }
202
203
3.02M
    size_t count() { return _count; }
204
205
4.40M
    bool is_empty() { return _count == 0; }
206
207
0
    bool contain(rowid_t from, rowid_t to) {
208
0
        // binary search
209
0
        RowRange tmp_range = RowRange(from, to);
210
0
        size_t start = 0;
211
0
        size_t end = _ranges.size();
212
0
        while (start <= end) {
213
0
            size_t mid = (start + end) / 2;
214
0
            if (_ranges[mid].is_before(tmp_range)) {
215
0
                start = mid;
216
0
            } else if (_ranges[mid].is_after(tmp_range)) {
217
0
                end = mid - 1;
218
0
            } else {
219
0
                return true;
220
0
            }
221
0
        }
222
0
        return false;
223
0
    }
224
225
    int64_t from() {
226
        DCHECK(!is_empty());
227
        return _ranges[0].from();
228
    }
229
230
1.15M
    int64_t to() {
231
1.15M
        DCHECK(!is_empty());
232
1.15M
        return _ranges[_ranges.size() - 1].to();
233
1.15M
    }
234
235
2.67M
    size_t range_size() const { return _ranges.size(); }
236
237
1.71M
    RowRange get_range(size_t index) const { return _ranges[index]; }
238
239
84.2k
    int64_t get_range_from(size_t range_index) const { return _ranges[range_index].from(); }
240
241
168k
    int64_t get_range_to(size_t range_index) const { return _ranges[range_index].to(); }
242
243
    size_t get_range_count(size_t range_index) const { return _ranges[range_index].count(); }
244
245
0
    std::string to_string() {
246
0
        std::string result;
247
0
        for (auto range : _ranges) {
248
0
            result += range.to_string() + " ";
249
0
        }
250
0
        return result;
251
0
    }
252
253
    // Adds a range to the end of the list of ranges. It maintains the disjunct ascending order(*) of the ranges by
254
    // trying to union the specified range to the last ranges in the list. The specified range shall be larger(*) than
255
    // the last one or might be overlapped with some of the last ones.
256
8.09M
    void add(const RowRange& range) {
257
8.09M
        if (range.count() == 0) {
258
1.88M
            return;
259
1.88M
        }
260
6.20M
        RowRange range_to_add = range;
261
6.30M
        for (int i = cast_set<int>(_ranges.size()) - 1; i >= 0; --i) {
262
104k
            const RowRange last = _ranges[i];
263
104k
            DCHECK(!last.is_after(range));
264
104k
            RowRange u;
265
104k
            bool ret = RowRange::range_union(last, range_to_add, &u);
266
104k
            if (!ret) {
267
                // range do not intersect with the last
268
10.3k
                break;
269
10.3k
            }
270
93.9k
            range_to_add = u;
271
93.9k
            _ranges.erase(_ranges.begin() + i);
272
93.9k
            _count -= last.count();
273
93.9k
        }
274
6.20M
        _ranges.emplace_back(range_to_add);
275
6.20M
        _count += range_to_add.count();
276
6.20M
    }
277
278
1.20M
    uint64_t get_digest(uint64_t seed) const {
279
1.20M
        for (auto range : _ranges) {
280
1.20M
            seed = range.get_digest(seed);
281
1.20M
        }
282
1.20M
        return seed;
283
1.20M
    }
284
285
private:
286
    std::vector<RowRange> _ranges;
287
    size_t _count;
288
};
289
290
} // namespace segment_v2
291
#include "common/compile_check_end.h"
292
} // namespace doris