Coverage Report

Created: 2026-03-24 20:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/result_block_buffer.cpp
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
#include "runtime/result_block_buffer.h"
19
20
#include <gen_cpp/Data_types.h>
21
#include <gen_cpp/PaloInternalService_types.h>
22
#include <gen_cpp/internal_service.pb.h>
23
#include <glog/logging.h>
24
#include <google/protobuf/stubs/callback.h>
25
// IWYU pragma: no_include <bits/chrono.h>
26
#include <chrono> // IWYU pragma: keep
27
#include <limits>
28
#include <ostream>
29
#include <string>
30
#include <utility>
31
#include <vector>
32
33
#include "arrow/type_fwd.h"
34
#include "common/config.h"
35
#include "core/block/block.h"
36
#include "exec/pipeline/dependency.h"
37
#include "exec/sink/writer/varrow_flight_result_writer.h"
38
#include "exec/sink/writer/vmysql_result_writer.h"
39
#include "runtime/runtime_profile.h"
40
#include "runtime/thread_context.h"
41
#include "util/thrift_util.h"
42
43
namespace doris {
44
#include "common/compile_check_begin.h"
45
46
template <typename ResultCtxType>
47
ResultBlockBuffer<ResultCtxType>::ResultBlockBuffer(TUniqueId id, RuntimeState* state,
48
                                                    int buffer_size)
49
26
        : _fragment_id(std::move(id)),
50
26
          _is_close(false),
51
26
          _batch_size(state->batch_size()),
52
26
          _timezone(state->timezone()),
53
26
          _be_exec_version(state->be_exec_version()),
54
26
          _fragment_transmission_compression_type(state->fragement_transmission_compression_type()),
55
26
          _buffer_limit(buffer_size) {
56
26
    _mem_tracker = MemTrackerLimiter::create_shared(
57
26
            MemTrackerLimiter::Type::QUERY,
58
26
            fmt::format("ResultBlockBuffer#FragmentInstanceId={}", print_id(_fragment_id)));
59
26
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEEC2ENS_9TUniqueIdEPNS_12RuntimeStateEi
Line
Count
Source
49
10
        : _fragment_id(std::move(id)),
50
10
          _is_close(false),
51
10
          _batch_size(state->batch_size()),
52
10
          _timezone(state->timezone()),
53
10
          _be_exec_version(state->be_exec_version()),
54
10
          _fragment_transmission_compression_type(state->fragement_transmission_compression_type()),
55
10
          _buffer_limit(buffer_size) {
56
10
    _mem_tracker = MemTrackerLimiter::create_shared(
57
10
            MemTrackerLimiter::Type::QUERY,
58
10
            fmt::format("ResultBlockBuffer#FragmentInstanceId={}", print_id(_fragment_id)));
59
10
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEEC2ENS_9TUniqueIdEPNS_12RuntimeStateEi
Line
Count
Source
49
16
        : _fragment_id(std::move(id)),
50
16
          _is_close(false),
51
16
          _batch_size(state->batch_size()),
52
16
          _timezone(state->timezone()),
53
16
          _be_exec_version(state->be_exec_version()),
54
16
          _fragment_transmission_compression_type(state->fragement_transmission_compression_type()),
55
16
          _buffer_limit(buffer_size) {
56
16
    _mem_tracker = MemTrackerLimiter::create_shared(
57
16
            MemTrackerLimiter::Type::QUERY,
58
16
            fmt::format("ResultBlockBuffer#FragmentInstanceId={}", print_id(_fragment_id)));
59
16
}
60
61
template <typename ResultCtxType>
62
Status ResultBlockBuffer<ResultCtxType>::close(const TUniqueId& id, Status exec_status,
63
12
                                               int64_t num_rows) {
64
12
    std::unique_lock<std::mutex> l(_lock);
65
12
    _returned_rows.fetch_add(num_rows);
66
    // close will be called multiple times and error status needs to be collected.
67
12
    if (!exec_status.ok()) {
68
8
        _status = exec_status;
69
8
    }
70
71
12
    auto it = _result_sink_dependencies.find(id);
72
12
    if (it != _result_sink_dependencies.end()) {
73
8
        it->second->set_always_ready();
74
8
        _result_sink_dependencies.erase(it);
75
8
    } else {
76
4
        _status = Status::InternalError("Instance {} is not found in ResultBlockBuffer",
77
4
                                        print_id(id));
78
4
    }
79
12
    if (!_result_sink_dependencies.empty()) {
80
4
        return _status;
81
4
    }
82
83
8
    _is_close = true;
84
8
    _arrow_data_arrival.notify_all();
85
86
8
    if (!_waiting_rpc.empty()) {
87
8
        if (_status.ok()) {
88
4
            for (auto& ctx : _waiting_rpc) {
89
4
                ctx->on_close(_packet_num, _returned_rows);
90
4
            }
91
4
        } else {
92
4
            for (auto& ctx : _waiting_rpc) {
93
4
                ctx->on_failure(_status);
94
4
            }
95
4
        }
96
8
        _waiting_rpc.clear();
97
8
    }
98
99
8
    return _status;
100
12
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE5closeERKNS_9TUniqueIdENS_6StatusEl
Line
Count
Source
63
6
                                               int64_t num_rows) {
64
6
    std::unique_lock<std::mutex> l(_lock);
65
6
    _returned_rows.fetch_add(num_rows);
66
    // close will be called multiple times and error status needs to be collected.
67
6
    if (!exec_status.ok()) {
68
4
        _status = exec_status;
69
4
    }
70
71
6
    auto it = _result_sink_dependencies.find(id);
72
6
    if (it != _result_sink_dependencies.end()) {
73
4
        it->second->set_always_ready();
74
4
        _result_sink_dependencies.erase(it);
75
4
    } else {
76
2
        _status = Status::InternalError("Instance {} is not found in ResultBlockBuffer",
77
2
                                        print_id(id));
78
2
    }
79
6
    if (!_result_sink_dependencies.empty()) {
80
2
        return _status;
81
2
    }
82
83
4
    _is_close = true;
84
4
    _arrow_data_arrival.notify_all();
85
86
4
    if (!_waiting_rpc.empty()) {
87
4
        if (_status.ok()) {
88
2
            for (auto& ctx : _waiting_rpc) {
89
2
                ctx->on_close(_packet_num, _returned_rows);
90
2
            }
91
2
        } else {
92
2
            for (auto& ctx : _waiting_rpc) {
93
2
                ctx->on_failure(_status);
94
2
            }
95
2
        }
96
4
        _waiting_rpc.clear();
97
4
    }
98
99
4
    return _status;
100
6
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE5closeERKNS_9TUniqueIdENS_6StatusEl
Line
Count
Source
63
6
                                               int64_t num_rows) {
64
6
    std::unique_lock<std::mutex> l(_lock);
65
6
    _returned_rows.fetch_add(num_rows);
66
    // close will be called multiple times and error status needs to be collected.
67
6
    if (!exec_status.ok()) {
68
4
        _status = exec_status;
69
4
    }
70
71
6
    auto it = _result_sink_dependencies.find(id);
72
6
    if (it != _result_sink_dependencies.end()) {
73
4
        it->second->set_always_ready();
74
4
        _result_sink_dependencies.erase(it);
75
4
    } else {
76
2
        _status = Status::InternalError("Instance {} is not found in ResultBlockBuffer",
77
2
                                        print_id(id));
78
2
    }
79
6
    if (!_result_sink_dependencies.empty()) {
80
2
        return _status;
81
2
    }
82
83
4
    _is_close = true;
84
4
    _arrow_data_arrival.notify_all();
85
86
4
    if (!_waiting_rpc.empty()) {
87
4
        if (_status.ok()) {
88
2
            for (auto& ctx : _waiting_rpc) {
89
2
                ctx->on_close(_packet_num, _returned_rows);
90
2
            }
91
2
        } else {
92
2
            for (auto& ctx : _waiting_rpc) {
93
2
                ctx->on_failure(_status);
94
2
            }
95
2
        }
96
4
        _waiting_rpc.clear();
97
4
    }
98
99
4
    return _status;
100
6
}
101
102
template <typename ResultCtxType>
103
6
void ResultBlockBuffer<ResultCtxType>::cancel(const Status& reason) {
104
6
    std::unique_lock<std::mutex> l(_lock);
105
6
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker);
106
6
    if (_status.ok()) {
107
6
        _status = reason;
108
6
    }
109
6
    _arrow_data_arrival.notify_all();
110
6
    for (auto& ctx : _waiting_rpc) {
111
4
        ctx->on_failure(reason);
112
4
    }
113
6
    _waiting_rpc.clear();
114
6
    _update_dependency();
115
6
    _result_batch_queue.clear();
116
6
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE6cancelERKNS_6StatusE
Line
Count
Source
103
2
void ResultBlockBuffer<ResultCtxType>::cancel(const Status& reason) {
104
2
    std::unique_lock<std::mutex> l(_lock);
105
2
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker);
106
2
    if (_status.ok()) {
107
2
        _status = reason;
108
2
    }
109
2
    _arrow_data_arrival.notify_all();
110
2
    for (auto& ctx : _waiting_rpc) {
111
2
        ctx->on_failure(reason);
112
2
    }
113
2
    _waiting_rpc.clear();
114
2
    _update_dependency();
115
2
    _result_batch_queue.clear();
116
2
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE6cancelERKNS_6StatusE
Line
Count
Source
103
4
void ResultBlockBuffer<ResultCtxType>::cancel(const Status& reason) {
104
4
    std::unique_lock<std::mutex> l(_lock);
105
4
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker);
106
4
    if (_status.ok()) {
107
4
        _status = reason;
108
4
    }
109
4
    _arrow_data_arrival.notify_all();
110
4
    for (auto& ctx : _waiting_rpc) {
111
2
        ctx->on_failure(reason);
112
2
    }
113
4
    _waiting_rpc.clear();
114
4
    _update_dependency();
115
4
    _result_batch_queue.clear();
116
4
}
117
118
template <typename ResultCtxType>
119
void ResultBlockBuffer<ResultCtxType>::set_dependency(
120
18
        const TUniqueId& id, std::shared_ptr<Dependency> result_sink_dependency) {
121
18
    std::unique_lock<std::mutex> l(_lock);
122
18
    _result_sink_dependencies[id] = result_sink_dependency;
123
18
    _update_dependency();
124
18
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE14set_dependencyERKNS_9TUniqueIdESt10shared_ptrINS_10DependencyEE
Line
Count
Source
120
10
        const TUniqueId& id, std::shared_ptr<Dependency> result_sink_dependency) {
121
10
    std::unique_lock<std::mutex> l(_lock);
122
10
    _result_sink_dependencies[id] = result_sink_dependency;
123
10
    _update_dependency();
124
10
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE14set_dependencyERKNS_9TUniqueIdESt10shared_ptrINS_10DependencyEE
Line
Count
Source
120
8
        const TUniqueId& id, std::shared_ptr<Dependency> result_sink_dependency) {
121
8
    std::unique_lock<std::mutex> l(_lock);
122
8
    _result_sink_dependencies[id] = result_sink_dependency;
123
8
    _update_dependency();
124
8
}
125
126
template <typename ResultCtxType>
127
72
void ResultBlockBuffer<ResultCtxType>::_update_dependency() {
128
72
    if (!_status.ok()) {
129
14
        for (auto it : _result_sink_dependencies) {
130
12
            it.second->set_ready();
131
12
        }
132
14
        return;
133
14
    }
134
135
58
    for (auto it : _result_sink_dependencies) {
136
52
        if (_instance_rows[it.first] > _batch_size) {
137
12
            it.second->block();
138
40
        } else {
139
40
            it.second->set_ready();
140
40
        }
141
52
    }
142
58
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE18_update_dependencyEv
Line
Count
Source
127
38
void ResultBlockBuffer<ResultCtxType>::_update_dependency() {
128
38
    if (!_status.ok()) {
129
6
        for (auto it : _result_sink_dependencies) {
130
6
            it.second->set_ready();
131
6
        }
132
6
        return;
133
6
    }
134
135
32
    for (auto it : _result_sink_dependencies) {
136
30
        if (_instance_rows[it.first] > _batch_size) {
137
8
            it.second->block();
138
22
        } else {
139
22
            it.second->set_ready();
140
22
        }
141
30
    }
142
32
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE18_update_dependencyEv
Line
Count
Source
127
34
void ResultBlockBuffer<ResultCtxType>::_update_dependency() {
128
34
    if (!_status.ok()) {
129
8
        for (auto it : _result_sink_dependencies) {
130
6
            it.second->set_ready();
131
6
        }
132
8
        return;
133
8
    }
134
135
26
    for (auto it : _result_sink_dependencies) {
136
22
        if (_instance_rows[it.first] > _batch_size) {
137
4
            it.second->block();
138
18
        } else {
139
18
            it.second->set_ready();
140
18
        }
141
22
    }
142
26
}
143
144
template <typename ResultCtxType>
145
30
Status ResultBlockBuffer<ResultCtxType>::get_batch(std::shared_ptr<ResultCtxType> ctx) {
146
30
    std::lock_guard<std::mutex> l(_lock);
147
30
    SCOPED_ATTACH_TASK(_mem_tracker);
148
30
    Defer defer {[&]() { _update_dependency(); }};
_ZZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE9get_batchESt10shared_ptrIS1_EENKUlvE_clEv
Line
Count
Source
148
16
    Defer defer {[&]() { _update_dependency(); }};
_ZZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE9get_batchESt10shared_ptrIS1_EENKUlvE_clEv
Line
Count
Source
148
14
    Defer defer {[&]() { _update_dependency(); }};
149
30
    if (!_status.ok()) {
150
4
        ctx->on_failure(_status);
151
4
        return _status;
152
4
    }
153
26
    if (!_result_batch_queue.empty()) {
154
6
        auto result = _result_batch_queue.front();
155
6
        _result_batch_queue.pop_front();
156
6
        for (auto it : _instance_rows_in_queue.front()) {
157
6
            _instance_rows[it.first] -= it.second;
158
6
        }
159
6
        _instance_rows_in_queue.pop_front();
160
6
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
161
6
        _packet_num++;
162
6
        return Status::OK();
163
6
    }
164
20
    if (_is_close) {
165
4
        if (!_status.ok()) {
166
0
            ctx->on_failure(_status);
167
0
            return Status::OK();
168
0
        }
169
4
        ctx->on_close(_packet_num, _returned_rows);
170
4
        LOG(INFO) << fmt::format(
171
4
                "ResultBlockBuffer finished, fragment_id={}, is_close={}, is_cancelled={}, "
172
4
                "packet_num={}, peak_memory_usage={}",
173
4
                print_id(_fragment_id), _is_close, !_status.ok(), _packet_num,
174
4
                _mem_tracker->peak_consumption());
175
4
        return Status::OK();
176
4
    }
177
    // no ready data, push ctx to waiting list
178
16
    _waiting_rpc.push_back(ctx);
179
16
    return Status::OK();
180
20
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE9get_batchESt10shared_ptrIS1_E
Line
Count
Source
145
16
Status ResultBlockBuffer<ResultCtxType>::get_batch(std::shared_ptr<ResultCtxType> ctx) {
146
16
    std::lock_guard<std::mutex> l(_lock);
147
16
    SCOPED_ATTACH_TASK(_mem_tracker);
148
16
    Defer defer {[&]() { _update_dependency(); }};
149
16
    if (!_status.ok()) {
150
2
        ctx->on_failure(_status);
151
2
        return _status;
152
2
    }
153
14
    if (!_result_batch_queue.empty()) {
154
4
        auto result = _result_batch_queue.front();
155
4
        _result_batch_queue.pop_front();
156
4
        for (auto it : _instance_rows_in_queue.front()) {
157
4
            _instance_rows[it.first] -= it.second;
158
4
        }
159
4
        _instance_rows_in_queue.pop_front();
160
4
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
161
4
        _packet_num++;
162
4
        return Status::OK();
163
4
    }
164
10
    if (_is_close) {
165
2
        if (!_status.ok()) {
166
0
            ctx->on_failure(_status);
167
0
            return Status::OK();
168
0
        }
169
2
        ctx->on_close(_packet_num, _returned_rows);
170
2
        LOG(INFO) << fmt::format(
171
2
                "ResultBlockBuffer finished, fragment_id={}, is_close={}, is_cancelled={}, "
172
2
                "packet_num={}, peak_memory_usage={}",
173
2
                print_id(_fragment_id), _is_close, !_status.ok(), _packet_num,
174
2
                _mem_tracker->peak_consumption());
175
2
        return Status::OK();
176
2
    }
177
    // no ready data, push ctx to waiting list
178
8
    _waiting_rpc.push_back(ctx);
179
8
    return Status::OK();
180
10
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE9get_batchESt10shared_ptrIS1_E
Line
Count
Source
145
14
Status ResultBlockBuffer<ResultCtxType>::get_batch(std::shared_ptr<ResultCtxType> ctx) {
146
14
    std::lock_guard<std::mutex> l(_lock);
147
14
    SCOPED_ATTACH_TASK(_mem_tracker);
148
14
    Defer defer {[&]() { _update_dependency(); }};
149
14
    if (!_status.ok()) {
150
2
        ctx->on_failure(_status);
151
2
        return _status;
152
2
    }
153
12
    if (!_result_batch_queue.empty()) {
154
2
        auto result = _result_batch_queue.front();
155
2
        _result_batch_queue.pop_front();
156
2
        for (auto it : _instance_rows_in_queue.front()) {
157
2
            _instance_rows[it.first] -= it.second;
158
2
        }
159
2
        _instance_rows_in_queue.pop_front();
160
2
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
161
2
        _packet_num++;
162
2
        return Status::OK();
163
2
    }
164
10
    if (_is_close) {
165
2
        if (!_status.ok()) {
166
0
            ctx->on_failure(_status);
167
0
            return Status::OK();
168
0
        }
169
2
        ctx->on_close(_packet_num, _returned_rows);
170
2
        LOG(INFO) << fmt::format(
171
2
                "ResultBlockBuffer finished, fragment_id={}, is_close={}, is_cancelled={}, "
172
2
                "packet_num={}, peak_memory_usage={}",
173
2
                print_id(_fragment_id), _is_close, !_status.ok(), _packet_num,
174
2
                _mem_tracker->peak_consumption());
175
2
        return Status::OK();
176
2
    }
177
    // no ready data, push ctx to waiting list
178
8
    _waiting_rpc.push_back(ctx);
179
8
    return Status::OK();
180
10
}
181
182
template <typename ResultCtxType>
183
Status ResultBlockBuffer<ResultCtxType>::add_batch(RuntimeState* state,
184
22
                                                   std::shared_ptr<InBlockType>& result) {
185
22
    std::unique_lock<std::mutex> l(_lock);
186
187
22
    if (!_status.ok()) {
188
4
        return _status;
189
4
    }
190
191
18
    if (_waiting_rpc.empty()) {
192
14
        auto sz = 0;
193
14
        auto num_rows = 0;
194
14
        size_t batch_size = 0;
195
14
        if constexpr (std::is_same_v<InBlockType, Block>) {
196
8
            num_rows = cast_set<int>(result->rows());
197
8
            batch_size = result->bytes();
198
8
        } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
199
6
            num_rows = cast_set<int>(result->result_batch.rows.size());
200
22
            for (const auto& row : result->result_batch.rows) {
201
22
                batch_size += row.size();
202
22
            }
203
6
        }
204
14
        if (!_result_batch_queue.empty()) {
205
6
            if constexpr (std::is_same_v<InBlockType, Block>) {
206
4
                sz = cast_set<int>(_result_batch_queue.back()->rows());
207
4
            } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
208
2
                sz = cast_set<int>(_result_batch_queue.back()->result_batch.rows.size());
209
2
            }
210
6
            if (sz + num_rows < _buffer_limit &&
211
6
                (batch_size + _last_batch_bytes) <= config::thrift_max_message_size) {
212
6
                if constexpr (std::is_same_v<InBlockType, Block>) {
213
4
                    auto last_block = _result_batch_queue.back();
214
8
                    for (size_t i = 0; i < last_block->columns(); i++) {
215
4
                        last_block->mutate_columns()[i]->insert_range_from(
216
4
                                *result->get_by_position(i).column, 0, num_rows);
217
4
                    }
218
4
                } else {
219
2
                    std::vector<std::string>& back_rows =
220
2
                            _result_batch_queue.back()->result_batch.rows;
221
2
                    std::vector<std::string>& result_rows = result->result_batch.rows;
222
2
                    back_rows.insert(back_rows.end(), std::make_move_iterator(result_rows.begin()),
223
2
                                     std::make_move_iterator(result_rows.end()));
224
2
                }
225
6
                _last_batch_bytes += batch_size;
226
6
            } else {
227
0
                _instance_rows_in_queue.emplace_back();
228
0
                _result_batch_queue.push_back(std::move(result));
229
0
                _last_batch_bytes = batch_size;
230
0
                _arrow_data_arrival
231
0
                        .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
232
0
            }
233
8
        } else {
234
8
            _instance_rows_in_queue.emplace_back();
235
8
            _result_batch_queue.push_back(std::move(result));
236
8
            _last_batch_bytes = batch_size;
237
8
            _arrow_data_arrival
238
8
                    .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
239
8
        }
240
14
        _instance_rows[state->fragment_instance_id()] += num_rows;
241
14
        _instance_rows_in_queue.back()[state->fragment_instance_id()] += num_rows;
242
14
    } else {
243
4
        auto ctx = _waiting_rpc.front();
244
4
        _waiting_rpc.pop_front();
245
4
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
246
4
        _packet_num++;
247
4
    }
248
249
18
    _update_dependency();
250
18
    return Status::OK();
251
18
}
_ZN5doris17ResultBlockBufferINS_22GetArrowResultBatchCtxEE9add_batchEPNS_12RuntimeStateERSt10shared_ptrINS_5BlockEE
Line
Count
Source
184
12
                                                   std::shared_ptr<InBlockType>& result) {
185
12
    std::unique_lock<std::mutex> l(_lock);
186
187
12
    if (!_status.ok()) {
188
2
        return _status;
189
2
    }
190
191
10
    if (_waiting_rpc.empty()) {
192
8
        auto sz = 0;
193
8
        auto num_rows = 0;
194
8
        size_t batch_size = 0;
195
8
        if constexpr (std::is_same_v<InBlockType, Block>) {
196
8
            num_rows = cast_set<int>(result->rows());
197
8
            batch_size = result->bytes();
198
        } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
199
            num_rows = cast_set<int>(result->result_batch.rows.size());
200
            for (const auto& row : result->result_batch.rows) {
201
                batch_size += row.size();
202
            }
203
        }
204
8
        if (!_result_batch_queue.empty()) {
205
4
            if constexpr (std::is_same_v<InBlockType, Block>) {
206
4
                sz = cast_set<int>(_result_batch_queue.back()->rows());
207
            } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
208
                sz = cast_set<int>(_result_batch_queue.back()->result_batch.rows.size());
209
            }
210
4
            if (sz + num_rows < _buffer_limit &&
211
4
                (batch_size + _last_batch_bytes) <= config::thrift_max_message_size) {
212
4
                if constexpr (std::is_same_v<InBlockType, Block>) {
213
4
                    auto last_block = _result_batch_queue.back();
214
8
                    for (size_t i = 0; i < last_block->columns(); i++) {
215
4
                        last_block->mutate_columns()[i]->insert_range_from(
216
4
                                *result->get_by_position(i).column, 0, num_rows);
217
4
                    }
218
                } else {
219
                    std::vector<std::string>& back_rows =
220
                            _result_batch_queue.back()->result_batch.rows;
221
                    std::vector<std::string>& result_rows = result->result_batch.rows;
222
                    back_rows.insert(back_rows.end(), std::make_move_iterator(result_rows.begin()),
223
                                     std::make_move_iterator(result_rows.end()));
224
                }
225
4
                _last_batch_bytes += batch_size;
226
4
            } else {
227
0
                _instance_rows_in_queue.emplace_back();
228
0
                _result_batch_queue.push_back(std::move(result));
229
0
                _last_batch_bytes = batch_size;
230
0
                _arrow_data_arrival
231
0
                        .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
232
0
            }
233
4
        } else {
234
4
            _instance_rows_in_queue.emplace_back();
235
4
            _result_batch_queue.push_back(std::move(result));
236
4
            _last_batch_bytes = batch_size;
237
4
            _arrow_data_arrival
238
4
                    .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
239
4
        }
240
8
        _instance_rows[state->fragment_instance_id()] += num_rows;
241
8
        _instance_rows_in_queue.back()[state->fragment_instance_id()] += num_rows;
242
8
    } else {
243
2
        auto ctx = _waiting_rpc.front();
244
2
        _waiting_rpc.pop_front();
245
2
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
246
2
        _packet_num++;
247
2
    }
248
249
10
    _update_dependency();
250
10
    return Status::OK();
251
10
}
_ZN5doris17ResultBlockBufferINS_17GetResultBatchCtxEE9add_batchEPNS_12RuntimeStateERSt10shared_ptrINS_16TFetchDataResultEE
Line
Count
Source
184
10
                                                   std::shared_ptr<InBlockType>& result) {
185
10
    std::unique_lock<std::mutex> l(_lock);
186
187
10
    if (!_status.ok()) {
188
2
        return _status;
189
2
    }
190
191
8
    if (_waiting_rpc.empty()) {
192
6
        auto sz = 0;
193
6
        auto num_rows = 0;
194
6
        size_t batch_size = 0;
195
        if constexpr (std::is_same_v<InBlockType, Block>) {
196
            num_rows = cast_set<int>(result->rows());
197
            batch_size = result->bytes();
198
6
        } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
199
6
            num_rows = cast_set<int>(result->result_batch.rows.size());
200
22
            for (const auto& row : result->result_batch.rows) {
201
22
                batch_size += row.size();
202
22
            }
203
6
        }
204
6
        if (!_result_batch_queue.empty()) {
205
            if constexpr (std::is_same_v<InBlockType, Block>) {
206
                sz = cast_set<int>(_result_batch_queue.back()->rows());
207
2
            } else if constexpr (std::is_same_v<InBlockType, TFetchDataResult>) {
208
2
                sz = cast_set<int>(_result_batch_queue.back()->result_batch.rows.size());
209
2
            }
210
2
            if (sz + num_rows < _buffer_limit &&
211
2
                (batch_size + _last_batch_bytes) <= config::thrift_max_message_size) {
212
                if constexpr (std::is_same_v<InBlockType, Block>) {
213
                    auto last_block = _result_batch_queue.back();
214
                    for (size_t i = 0; i < last_block->columns(); i++) {
215
                        last_block->mutate_columns()[i]->insert_range_from(
216
                                *result->get_by_position(i).column, 0, num_rows);
217
                    }
218
2
                } else {
219
2
                    std::vector<std::string>& back_rows =
220
2
                            _result_batch_queue.back()->result_batch.rows;
221
2
                    std::vector<std::string>& result_rows = result->result_batch.rows;
222
2
                    back_rows.insert(back_rows.end(), std::make_move_iterator(result_rows.begin()),
223
2
                                     std::make_move_iterator(result_rows.end()));
224
2
                }
225
2
                _last_batch_bytes += batch_size;
226
2
            } else {
227
0
                _instance_rows_in_queue.emplace_back();
228
0
                _result_batch_queue.push_back(std::move(result));
229
0
                _last_batch_bytes = batch_size;
230
0
                _arrow_data_arrival
231
0
                        .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
232
0
            }
233
4
        } else {
234
4
            _instance_rows_in_queue.emplace_back();
235
4
            _result_batch_queue.push_back(std::move(result));
236
4
            _last_batch_bytes = batch_size;
237
4
            _arrow_data_arrival
238
4
                    .notify_one(); // Only valid for get_arrow_batch(std::shared_ptr<Block>,)
239
4
        }
240
6
        _instance_rows[state->fragment_instance_id()] += num_rows;
241
6
        _instance_rows_in_queue.back()[state->fragment_instance_id()] += num_rows;
242
6
    } else {
243
2
        auto ctx = _waiting_rpc.front();
244
2
        _waiting_rpc.pop_front();
245
2
        RETURN_IF_ERROR(ctx->on_data(result, _packet_num, this));
246
2
        _packet_num++;
247
2
    }
248
249
8
    _update_dependency();
250
8
    return Status::OK();
251
8
}
252
253
template class ResultBlockBuffer<GetArrowResultBatchCtx>;
254
template class ResultBlockBuffer<GetResultBatchCtx>;
255
256
#include "common/compile_check_end.h"
257
} // namespace doris