Coverage Report

Created: 2026-04-03 05:44

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