Coverage Report

Created: 2026-03-14 06:50

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