Coverage Report

Created: 2026-04-01 07:58

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