Coverage Report

Created: 2026-06-05 05:38

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