Coverage Report

Created: 2026-10-29 19:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/split_source_connector.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 <algorithm>
21
#include <atomic>
22
23
#include "common/config.h"
24
#include "core/custom_allocator.h"
25
#include "runtime/runtime_state.h"
26
#include "util/client_cache.h"
27
28
namespace doris {
29
30
/*
31
 * Multiple scanners within a scan node share a split source.
32
 * Each scanner call `get_next` to get the next scan range. A fast scanner will immediately obtain
33
 * the next scan range, so there is no situation of data skewing.
34
 */
35
class SplitSourceConnector {
36
public:
37
16
    SplitSourceConnector() = default;
38
16
    virtual ~SplitSourceConnector() = default;
39
40
    /**
41
     * Get the next scan range. has_next should be to true to fetch the next scan range.
42
     * @param has_next whether exists the next scan range
43
     * @param range the obtained next scan range
44
     * @param scanner_stop the caller's local stop signal, which can be set without query cancellation
45
     */
46
    virtual Status get_next(bool* has_next, TFileRangeDesc* range,
47
                            const std::atomic_bool* scanner_stop) = 0;
48
49
    virtual int num_scan_ranges() = 0;
50
51
    virtual TFileScanRangeParams* get_params() = 0;
52
53
0
    virtual bool all_ranges_have_table_level_row_count() const { return false; }
54
55
protected:
56
    template <typename T, typename V1 = std::vector<T>, typename V2 = std::vector<T>>
57
        requires(std::is_same_v<std::remove_cvref_t<V1>,
58
                                std::vector<T, typename V1::allocator_type>> &&
59
                 std::is_same_v<std::remove_cvref_t<V2>,
60
                                std::vector<T, typename V2::allocator_type>>)
61
11
    void _merge_ranges(V1& merged_ranges, const V2& scan_ranges) {
62
11
        if (scan_ranges.size() <= _max_scanners) {
63
11
            merged_ranges.assign(scan_ranges.begin(), scan_ranges.end());
64
11
            return;
65
11
        }
66
67
        // There is no need for the number of scanners to exceed the number of threads in thread pool.
68
        // scan_ranges is sorted by path(as well as partition path) in FE, so merge scan ranges in order.
69
        // In the insert statement, reading data in partition order can reduce the memory usage of BE
70
        // and prevent the generation of smaller tables.
71
0
        merged_ranges.resize(_max_scanners);
72
0
        int num_ranges = static_cast<int>(scan_ranges.size()) / _max_scanners;
73
0
        int num_add_one = static_cast<int>(scan_ranges.size()) - num_ranges * _max_scanners;
74
0
        int scan_index = 0;
75
0
        int range_index = 0;
76
0
        for (int i = 0; i < num_add_one; ++i) {
77
0
            merged_ranges[scan_index] = scan_ranges[range_index++];
78
0
            auto& ranges =
79
0
                    merged_ranges[scan_index++].scan_range.ext_scan_range.file_scan_range.ranges;
80
0
            for (int j = 0; j < num_ranges; j++) {
81
0
                auto& tmp_merged_ranges =
82
0
                        scan_ranges[range_index++].scan_range.ext_scan_range.file_scan_range.ranges;
83
0
                ranges.insert(ranges.end(), tmp_merged_ranges.begin(), tmp_merged_ranges.end());
84
0
            }
85
0
        }
86
0
        for (int i = num_add_one; i < _max_scanners; ++i) {
87
0
            merged_ranges[scan_index] = scan_ranges[range_index++];
88
0
            auto& ranges =
89
0
                    merged_ranges[scan_index++].scan_range.ext_scan_range.file_scan_range.ranges;
90
0
            for (int j = 0; j < num_ranges - 1; j++) {
91
0
                auto& tmp_merged_ranges =
92
0
                        scan_ranges[range_index++].scan_range.ext_scan_range.file_scan_range.ranges;
93
0
                ranges.insert(ranges.end(), tmp_merged_ranges.begin(), tmp_merged_ranges.end());
94
0
            }
95
0
        }
96
0
        LOG(INFO) << "Merge " << scan_ranges.size() << " scan ranges to " << merged_ranges.size();
97
0
    }
_ZN5doris20SplitSourceConnector13_merge_rangesINS_16TScanRangeParamsESt6vectorIS2_NS_18CustomStdAllocatorIS2_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEES3_IS2_SaIS2_EEQaasr3stdE9is_same_vINSt12remove_cvrefIT0_E4typeES3_IT_NSD_14allocator_typeEEEsr3stdE9is_same_vINSC_IT1_E4typeES3_ISG_NSJ_14allocator_typeEEEEEvRSD_RKSJ_
Line
Count
Source
61
11
    void _merge_ranges(V1& merged_ranges, const V2& scan_ranges) {
62
11
        if (scan_ranges.size() <= _max_scanners) {
63
11
            merged_ranges.assign(scan_ranges.begin(), scan_ranges.end());
64
11
            return;
65
11
        }
66
67
        // There is no need for the number of scanners to exceed the number of threads in thread pool.
68
        // scan_ranges is sorted by path(as well as partition path) in FE, so merge scan ranges in order.
69
        // In the insert statement, reading data in partition order can reduce the memory usage of BE
70
        // and prevent the generation of smaller tables.
71
0
        merged_ranges.resize(_max_scanners);
72
0
        int num_ranges = static_cast<int>(scan_ranges.size()) / _max_scanners;
73
0
        int num_add_one = static_cast<int>(scan_ranges.size()) - num_ranges * _max_scanners;
74
0
        int scan_index = 0;
75
0
        int range_index = 0;
76
0
        for (int i = 0; i < num_add_one; ++i) {
77
0
            merged_ranges[scan_index] = scan_ranges[range_index++];
78
0
            auto& ranges =
79
0
                    merged_ranges[scan_index++].scan_range.ext_scan_range.file_scan_range.ranges;
80
0
            for (int j = 0; j < num_ranges; j++) {
81
0
                auto& tmp_merged_ranges =
82
0
                        scan_ranges[range_index++].scan_range.ext_scan_range.file_scan_range.ranges;
83
0
                ranges.insert(ranges.end(), tmp_merged_ranges.begin(), tmp_merged_ranges.end());
84
0
            }
85
0
        }
86
0
        for (int i = num_add_one; i < _max_scanners; ++i) {
87
0
            merged_ranges[scan_index] = scan_ranges[range_index++];
88
0
            auto& ranges =
89
0
                    merged_ranges[scan_index++].scan_range.ext_scan_range.file_scan_range.ranges;
90
0
            for (int j = 0; j < num_ranges - 1; j++) {
91
0
                auto& tmp_merged_ranges =
92
0
                        scan_ranges[range_index++].scan_range.ext_scan_range.file_scan_range.ranges;
93
0
                ranges.insert(ranges.end(), tmp_merged_ranges.begin(), tmp_merged_ranges.end());
94
0
            }
95
0
        }
96
        LOG(INFO) << "Merge " << scan_ranges.size() << " scan ranges to " << merged_ranges.size();
97
0
    }
Unexecuted instantiation: _ZN5doris20SplitSourceConnector13_merge_rangesINS_19TScanRangeLocationsESt6vectorIS2_NS_18CustomStdAllocatorIS2_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEES3_IS2_SaIS2_EEQaasr3stdE9is_same_vINSt12remove_cvrefIT0_E4typeES3_IT_NSD_14allocator_typeEEEsr3stdE9is_same_vINSC_IT1_E4typeES3_ISG_NSJ_14allocator_typeEEEEEvRSD_RKSJ_
98
99
protected:
100
    int _max_scanners;
101
};
102
103
/**
104
 * The file splits are already assigned in `TFileScanRange.ranges`. Scan node has need to
105
 * fetch the scan ranges from frontend.
106
 *
107
 * In cases where the number of files is small, the splits are directly transmitted to backend.
108
 */
109
class LocalSplitSourceConnector : public SplitSourceConnector {
110
private:
111
    std::mutex _range_lock;
112
    DorisVector<TScanRangeParams> _scan_ranges;
113
    int _scan_index = 0;
114
    int _range_index = 0;
115
116
public:
117
11
    LocalSplitSourceConnector(const std::vector<TScanRangeParams>& scan_ranges, int max_scanners) {
118
11
        _max_scanners = max_scanners;
119
11
        _merge_ranges<TScanRangeParams>(_scan_ranges, scan_ranges);
120
11
    }
121
122
    Status get_next(bool* has_next, TFileRangeDesc* range,
123
                    const std::atomic_bool* scanner_stop) override;
124
125
10
    int num_scan_ranges() override { return static_cast<int>(_scan_ranges.size()); }
126
127
4
    TFileScanRangeParams* get_params() override {
128
4
        if (_scan_ranges.size() > 0 &&
129
4
            _scan_ranges[0].scan_range.ext_scan_range.file_scan_range.__isset.params) {
130
            // for compatibility.
131
4
            return &_scan_ranges[0].scan_range.ext_scan_range.file_scan_range.params;
132
4
        }
133
0
        throw Exception(
134
0
                Status::FatalError("Unreachable, params is got by file_scan_range_params_map"));
135
4
    }
136
137
3
    bool all_ranges_have_table_level_row_count() const override {
138
        // Every assigned range must carry a proven count; one fallback range would still require
139
        // decoding the projected carrier through the selected scanner.
140
6
        return !_scan_ranges.empty() && std::ranges::all_of(_scan_ranges, [](const auto& params) {
141
6
            const auto& ranges = params.scan_range.ext_scan_range.file_scan_range.ranges;
142
6
            return !ranges.empty() && std::ranges::all_of(ranges, [](const auto& range) {
143
6
                return range.__isset.table_format_params &&
144
6
                       range.table_format_params.__isset.table_level_row_count &&
145
6
                       range.table_format_params.table_level_row_count >= 0;
146
6
            });
147
6
        });
148
3
    }
149
};
150
151
/**
152
 * The file splits are lazily generated in frontend, and saved as a split source in frontend.
153
 * The scan node needs to fetch file splits from the frontend service. Each split source is identified by
154
 * a unique ID, and the ID is stored in `TFileScanRange.split_source.split_source_id`
155
 *
156
 * In the case of a large number of files, backend can scan data while obtaining splits information.
157
 */
158
class RemoteSplitSourceConnector : public SplitSourceConnector {
159
private:
160
    std::mutex _range_lock;
161
    RuntimeState* _state;
162
    RuntimeProfile::Counter* _get_split_timer;
163
    int64_t _split_source_id;
164
    int _num_splits;
165
166
    DorisVector<TScanRangeLocations> _scan_ranges;
167
    bool _last_batch = false;
168
    int _scan_index = 0;
169
    int _range_index = 0;
170
171
public:
172
    RemoteSplitSourceConnector(RuntimeState* state, RuntimeProfile::Counter* get_split_timer,
173
                               int64_t split_source_id, int num_splits, int max_scanners)
174
1
            : _state(state),
175
1
              _get_split_timer(get_split_timer),
176
1
              _split_source_id(split_source_id),
177
1
              _num_splits(num_splits) {
178
1
        _max_scanners = max_scanners;
179
1
    }
180
181
    Status get_next(bool* has_next, TFileRangeDesc* range,
182
                    const std::atomic_bool* scanner_stop) override;
183
184
    /*
185
     * Remote split source is fetched in batch mode, and the splits are generated while scanning,
186
     * so the number of scan ranges may not be accurate.
187
     */
188
0
    int num_scan_ranges() override { return _num_splits; }
189
190
0
    TFileScanRangeParams* get_params() override {
191
0
        throw Exception(
192
0
                Status::FatalError("Unreachable, params is got by file_scan_range_params_map"));
193
0
    }
194
};
195
196
} // namespace doris