Coverage Report

Created: 2025-03-11 17:58

/root/doris/be/src/pipeline/dependency.h
Line
Count
Source (jump to first uncovered line)
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
#pragma once
19
20
#include <concurrentqueue.h>
21
#include <sqltypes.h>
22
23
#include <atomic>
24
#include <functional>
25
#include <memory>
26
#include <mutex>
27
#include <thread>
28
#include <utility>
29
30
#include "common/config.h"
31
#include "common/logging.h"
32
#include "gutil/integral_types.h"
33
#include "pipeline/common/agg_utils.h"
34
#include "pipeline/common/join_utils.h"
35
#include "pipeline/common/set_utils.h"
36
#include "pipeline/exec/data_queue.h"
37
#include "pipeline/exec/join/process_hash_table_probe.h"
38
#include "util/stack_util.h"
39
#include "vec/common/sort/partition_sorter.h"
40
#include "vec/common/sort/sorter.h"
41
#include "vec/core/block.h"
42
#include "vec/core/types.h"
43
#include "vec/spill/spill_stream.h"
44
45
namespace doris::vectorized {
46
class AggFnEvaluator;
47
class VSlotRef;
48
} // namespace doris::vectorized
49
50
namespace doris::pipeline {
51
#include "common/compile_check_begin.h"
52
class Dependency;
53
class PipelineTask;
54
struct BasicSharedState;
55
using DependencySPtr = std::shared_ptr<Dependency>;
56
class LocalExchangeSourceLocalState;
57
58
static constexpr auto SLOW_DEPENDENCY_THRESHOLD = 60 * 1000L * 1000L * 1000L;
59
static constexpr auto TIME_UNIT_DEPENDENCY_LOG = 30 * 1000L * 1000L * 1000L;
60
static_assert(TIME_UNIT_DEPENDENCY_LOG < SLOW_DEPENDENCY_THRESHOLD);
61
62
struct BasicSharedState {
63
    ENABLE_FACTORY_CREATOR(BasicSharedState)
64
65
    template <class TARGET>
66
30
    TARGET* cast() {
67
30
        DCHECK(dynamic_cast<TARGET*>(this))
68
0
                << " Mismatch type! Current type is " << typeid(*this).name()
69
0
                << " and expect type is" << typeid(TARGET).name();
70
30
        return reinterpret_cast<TARGET*>(this);
71
30
    }
_ZN5doris8pipeline16BasicSharedState4castINS0_19HashJoinSharedStateEEEPT_v
Line
Count
Source
66
6
    TARGET* cast() {
67
6
        DCHECK(dynamic_cast<TARGET*>(this))
68
0
                << " Mismatch type! Current type is " << typeid(*this).name()
69
0
                << " and expect type is" << typeid(TARGET).name();
70
6
        return reinterpret_cast<TARGET*>(this);
71
6
    }
_ZN5doris8pipeline16BasicSharedState4castINS0_30PartitionedHashJoinSharedStateEEEPT_v
Line
Count
Source
66
3
    TARGET* cast() {
67
3
        DCHECK(dynamic_cast<TARGET*>(this))
68
0
                << " Mismatch type! Current type is " << typeid(*this).name()
69
0
                << " and expect type is" << typeid(TARGET).name();
70
3
        return reinterpret_cast<TARGET*>(this);
71
3
    }
_ZN5doris8pipeline16BasicSharedState4castINS0_15SortSharedStateEEEPT_v
Line
Count
Source
66
3
    TARGET* cast() {
67
3
        DCHECK(dynamic_cast<TARGET*>(this))
68
0
                << " Mismatch type! Current type is " << typeid(*this).name()
69
0
                << " and expect type is" << typeid(TARGET).name();
70
3
        return reinterpret_cast<TARGET*>(this);
71
3
    }
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_20SpillSortSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_25NestedLoopJoinSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_19AnalyticSharedStateEEEPT_v
_ZN5doris8pipeline16BasicSharedState4castINS0_14AggSharedStateEEEPT_v
Line
Count
Source
66
18
    TARGET* cast() {
67
18
        DCHECK(dynamic_cast<TARGET*>(this))
68
0
                << " Mismatch type! Current type is " << typeid(*this).name()
69
0
                << " and expect type is" << typeid(TARGET).name();
70
18
        return reinterpret_cast<TARGET*>(this);
71
18
    }
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_25PartitionedAggSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_16UnionSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_28PartitionSortNodeSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_20MultiCastSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_14SetSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_24LocalExchangeSharedStateEEEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castIS1_EEPT_v
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_16CacheSharedStateEEEPT_v
72
    template <class TARGET>
73
    const TARGET* cast() const {
74
        DCHECK(dynamic_cast<const TARGET*>(this))
75
                << " Mismatch type! Current type is " << typeid(*this).name()
76
                << " and expect type is" << typeid(TARGET).name();
77
        return reinterpret_cast<const TARGET*>(this);
78
    }
79
    std::vector<DependencySPtr> source_deps;
80
    std::vector<DependencySPtr> sink_deps;
81
    int id = 0;
82
    std::set<int> related_op_ids;
83
84
96
    virtual ~BasicSharedState() = default;
85
86
    Dependency* create_source_dependency(int operator_id, int node_id, const std::string& name);
87
88
    Dependency* create_sink_dependency(int dest_id, int node_id, const std::string& name);
89
};
90
91
class Dependency : public std::enable_shared_from_this<Dependency> {
92
public:
93
    ENABLE_FACTORY_CREATOR(Dependency);
94
    Dependency(int id, int node_id, std::string name, bool ready = false)
95
546
            : _id(id), _node_id(node_id), _name(std::move(name)), _ready(ready) {}
96
546
    virtual ~Dependency() = default;
97
98
0
    [[nodiscard]] int id() const { return _id; }
99
46
    [[nodiscard]] virtual std::string name() const { return _name; }
100
0
    BasicSharedState* shared_state() { return _shared_state; }
101
63
    void set_shared_state(BasicSharedState* shared_state) { _shared_state = shared_state; }
102
    virtual std::string debug_string(int indentation_level = 0);
103
333M
    bool ready() const { return _ready; }
104
105
    // Start the watcher. We use it to count how long this dependency block the current pipeline task.
106
17
    void start_watcher() { _watcher.start(); }
107
238
    [[nodiscard]] int64_t watcher_elapse_time() { return _watcher.elapsed_time(); }
108
109
    // Which dependency current pipeline task is blocked by. `nullptr` if this dependency is ready.
110
    [[nodiscard]] virtual Dependency* is_blocked_by(PipelineTask* task = nullptr);
111
    // Notify downstream pipeline tasks this dependency is ready.
112
    virtual void set_ready();
113
13
    void set_ready_to_read() {
114
13
        DCHECK_EQ(_shared_state->source_deps.size(), 1) << debug_string();
115
13
        _shared_state->source_deps.front()->set_ready();
116
13
    }
117
118
0
    void set_ready_to_write() {
119
0
        DCHECK_EQ(_shared_state->sink_deps.size(), 1) << debug_string();
120
0
        _shared_state->sink_deps.front()->set_ready();
121
0
    }
122
123
    // Notify downstream pipeline tasks this dependency is blocked.
124
1.52k
    void block() {
125
1.52k
        if (_always_ready) {
126
7
            return;
127
7
        }
128
1.51k
        std::unique_lock<std::mutex> lc(_always_ready_lock);
129
1.51k
        if (_always_ready) {
130
0
            return;
131
0
        }
132
1.51k
        _ready = false;
133
1.51k
    }
134
135
77
    void set_always_ready() {
136
77
        if (_always_ready) {
137
31
            return;
138
31
        }
139
46
        std::unique_lock<std::mutex> lc(_always_ready_lock);
140
46
        if (_always_ready) {
141
0
            return;
142
0
        }
143
46
        _always_ready = true;
144
46
        set_ready();
145
46
    }
146
147
protected:
148
    void _add_block_task(PipelineTask* task);
149
150
    const int _id;
151
    const int _node_id;
152
    const std::string _name;
153
    std::atomic<bool> _ready;
154
155
    BasicSharedState* _shared_state = nullptr;
156
    MonotonicStopWatch _watcher;
157
158
    std::mutex _task_lock;
159
    std::vector<PipelineTask*> _blocked_task;
160
161
    // If `_always_ready` is true, `block()` will never block tasks.
162
    std::atomic<bool> _always_ready = false;
163
    std::mutex _always_ready_lock;
164
};
165
166
struct FakeSharedState final : public BasicSharedState {
167
    ENABLE_FACTORY_CREATOR(FakeSharedState)
168
};
169
170
class CountedFinishDependency final : public Dependency {
171
public:
172
    using SharedState = FakeSharedState;
173
    CountedFinishDependency(int id, int node_id, std::string name)
174
17
            : Dependency(id, node_id, std::move(name), true) {}
175
176
0
    void add() {
177
0
        std::unique_lock<std::mutex> l(_mtx);
178
0
        if (!_counter) {
179
0
            block();
180
0
        }
181
0
        _counter++;
182
0
    }
183
184
0
    void sub() {
185
0
        std::unique_lock<std::mutex> l(_mtx);
186
0
        _counter--;
187
0
        if (!_counter) {
188
0
            set_ready();
189
0
        }
190
0
    }
191
192
    std::string debug_string(int indentation_level = 0) override;
193
194
private:
195
    std::mutex _mtx;
196
    uint32_t _counter = 0;
197
};
198
199
class RuntimeFilterDependency;
200
struct RuntimeFilterTimerQueue;
201
class RuntimeFilterTimer {
202
public:
203
    RuntimeFilterTimer(int64_t registration_time, int32_t wait_time_ms,
204
                       std::shared_ptr<RuntimeFilterDependency> parent)
205
            : _parent(std::move(parent)),
206
              _registration_time(registration_time),
207
0
              _wait_time_ms(wait_time_ms) {}
208
209
    // Called by runtime filter producer.
210
    void call_ready();
211
212
    // Called by RuntimeFilterTimerQueue which is responsible for checking if this rf is timeout.
213
    void call_timeout();
214
215
0
    int64_t registration_time() const { return _registration_time; }
216
0
    int32_t wait_time_ms() const { return _wait_time_ms; }
217
218
    void set_local_runtime_filter_dependencies(
219
0
            const std::vector<std::shared_ptr<RuntimeFilterDependency>>& deps) {
220
0
        _local_runtime_filter_dependencies = deps;
221
0
    }
222
223
    bool should_be_check_timeout();
224
225
private:
226
    friend struct RuntimeFilterTimerQueue;
227
    std::shared_ptr<RuntimeFilterDependency> _parent = nullptr;
228
    std::vector<std::shared_ptr<RuntimeFilterDependency>> _local_runtime_filter_dependencies;
229
    std::mutex _lock;
230
    int64_t _registration_time;
231
    const int32_t _wait_time_ms;
232
};
233
234
struct RuntimeFilterTimerQueue {
235
    constexpr static int64_t interval = 10;
236
0
    void run() { _thread.detach(); }
237
    void start();
238
239
0
    void stop() {
240
0
        _stop = true;
241
0
        cv.notify_all();
242
0
        wait_for_shutdown();
243
0
    }
244
245
0
    void wait_for_shutdown() const {
246
0
        while (!_shutdown) {
247
0
            std::this_thread::sleep_for(std::chrono::milliseconds(interval));
248
0
        }
249
0
    }
250
251
0
    ~RuntimeFilterTimerQueue() = default;
252
0
    RuntimeFilterTimerQueue() { _thread = std::thread(&RuntimeFilterTimerQueue::start, this); }
253
0
    void push_filter_timer(std::vector<std::shared_ptr<pipeline::RuntimeFilterTimer>>&& filter) {
254
0
        std::unique_lock<std::mutex> lc(_que_lock);
255
0
        _que.insert(_que.end(), filter.begin(), filter.end());
256
0
        cv.notify_all();
257
0
    }
258
259
    std::thread _thread;
260
    std::condition_variable cv;
261
    std::mutex cv_m;
262
    std::mutex _que_lock;
263
    std::atomic_bool _stop = false;
264
    std::atomic_bool _shutdown = false;
265
    std::list<std::shared_ptr<pipeline::RuntimeFilterTimer>> _que;
266
};
267
268
class RuntimeFilterDependency final : public Dependency {
269
public:
270
    RuntimeFilterDependency(int id, int node_id, std::string name, IRuntimeFilter* runtime_filter)
271
0
            : Dependency(id, node_id, std::move(name)), _runtime_filter(runtime_filter) {}
272
    std::string debug_string(int indentation_level = 0) override;
273
274
private:
275
    const IRuntimeFilter* _runtime_filter = nullptr;
276
};
277
278
struct AggSharedState : public BasicSharedState {
279
    ENABLE_FACTORY_CREATOR(AggSharedState)
280
public:
281
10
    AggSharedState() {
282
10
        agg_data = std::make_unique<AggregatedDataVariants>();
283
10
        agg_arena_pool = std::make_unique<vectorized::Arena>();
284
10
    }
285
10
    ~AggSharedState() override {
286
10
        if (!probe_expr_ctxs.empty()) {
287
4
            _close_with_serialized_key();
288
6
        } else {
289
6
            _close_without_key();
290
6
        }
291
10
    }
292
293
    Status reset_hash_table();
294
295
    bool do_limit_filter(vectorized::Block* block, size_t num_rows,
296
                         const std::vector<int>* key_locs = nullptr);
297
    void build_limit_heap(size_t hash_table_size);
298
299
    // We should call this function only at 1st phase.
300
    // 1st phase: is_merge=true, only have one SlotRef.
301
    // 2nd phase: is_merge=false, maybe have multiple exprs.
302
    static int get_slot_column_id(const vectorized::AggFnEvaluator* evaluator);
303
304
    AggregatedDataVariantsUPtr agg_data = nullptr;
305
    std::unique_ptr<AggregateDataContainer> aggregate_data_container;
306
    ArenaUPtr agg_arena_pool;
307
    std::vector<vectorized::AggFnEvaluator*> aggregate_evaluators;
308
    // group by k1,k2
309
    vectorized::VExprContextSPtrs probe_expr_ctxs;
310
    size_t input_num_rows = 0;
311
    std::vector<vectorized::AggregateDataPtr> values;
312
    /// The total size of the row from the aggregate functions.
313
    size_t total_size_of_aggregate_states = 0;
314
    size_t align_aggregate_states = 1;
315
    /// The offset to the n-th aggregate function in a row of aggregate functions.
316
    vectorized::Sizes offsets_of_aggregate_states;
317
    std::vector<size_t> make_nullable_keys;
318
319
    bool agg_data_created_without_key = false;
320
    bool enable_spill = false;
321
    bool reach_limit = false;
322
323
    int64_t limit = -1;
324
    bool do_sort_limit = false;
325
    vectorized::MutableColumns limit_columns;
326
    int limit_columns_min = -1;
327
    vectorized::PaddedPODArray<uint8_t> need_computes;
328
    std::vector<uint8_t> cmp_res;
329
    std::vector<int> order_directions;
330
    std::vector<int> null_directions;
331
332
    struct HeapLimitCursor {
333
        HeapLimitCursor(int row_id, vectorized::MutableColumns& limit_columns,
334
                        std::vector<int>& order_directions, std::vector<int>& null_directions)
335
                : _row_id(row_id),
336
                  _limit_columns(limit_columns),
337
                  _order_directions(order_directions),
338
6
                  _null_directions(null_directions) {}
339
340
        HeapLimitCursor(const HeapLimitCursor& other) = default;
341
342
        HeapLimitCursor(HeapLimitCursor&& other) noexcept
343
                : _row_id(other._row_id),
344
                  _limit_columns(other._limit_columns),
345
                  _order_directions(other._order_directions),
346
30
                  _null_directions(other._null_directions) {}
347
348
0
        HeapLimitCursor& operator=(const HeapLimitCursor& other) noexcept {
349
0
            _row_id = other._row_id;
350
0
            return *this;
351
0
        }
352
353
23
        HeapLimitCursor& operator=(HeapLimitCursor&& other) noexcept {
354
23
            _row_id = other._row_id;
355
23
            return *this;
356
23
        }
357
358
14
        bool operator<(const HeapLimitCursor& rhs) const {
359
14
            for (int i = 0; i < _limit_columns.size(); ++i) {
360
14
                const auto& _limit_column = _limit_columns[i];
361
14
                auto res = _limit_column->compare_at(_row_id, rhs._row_id, *_limit_column,
362
14
                                                     _null_directions[i]) *
363
14
                           _order_directions[i];
364
14
                if (res < 0) {
365
5
                    return true;
366
9
                } else if (res > 0) {
367
9
                    return false;
368
9
                }
369
14
            }
370
0
            return false;
371
14
        }
372
373
        int _row_id;
374
        vectorized::MutableColumns& _limit_columns;
375
        std::vector<int>& _order_directions;
376
        std::vector<int>& _null_directions;
377
    };
378
379
    std::priority_queue<HeapLimitCursor> limit_heap;
380
381
    // Refresh the top limit heap with a new row
382
    void refresh_top_limit(size_t row_id, const vectorized::ColumnRawPtrs& key_columns);
383
384
private:
385
    vectorized::MutableColumns _get_keys_hash_table();
386
387
4
    void _close_with_serialized_key() {
388
4
        std::visit(vectorized::Overload {[&](std::monostate& arg) -> void {
389
                                             // Do nothing
390
0
                                         },
391
4
                                         [&](auto& agg_method) -> void {
392
4
                                             auto& data = *agg_method.hash_table;
393
12
                                             data.for_each_mapped([&](auto& mapped) {
394
12
                                                 if (mapped) {
395
12
                                                     static_cast<void>(_destroy_agg_status(mapped));
396
12
                                                     mapped = nullptr;
397
12
                                                 }
398
12
                                             });
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS3_ENKUlS3_E_clISA_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized19MethodStringNoCacheINS_13StringHashMapIPc9AllocatorILb1ELb1ELb0E22DefaultMemoryAllocatorEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
_ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Line
Count
Source
393
12
                                             data.for_each_mapped([&](auto& mapped) {
394
12
                                                 if (mapped) {
395
12
                                                     static_cast<void>(_destroy_agg_status(mapped));
396
12
                                                     mapped = nullptr;
397
12
                                                 }
398
12
                                             });
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIhNS6_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberItNS6_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm128EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ENKUlS3_E_clISE_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm256EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ENKUlS3_E_clISE_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_19MethodStringNoCacheINS6_15DataWithNullKeyINS_13StringHashMapIPc9AllocatorILb1ELb1ELb0E22DefaultMemoryAllocatorEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapINS6_7UInt136EPc9HashCRC32IS9_EEEEEEvS3_ENKUlS3_E_clISA_EEDaS3_
399
4
                                             if (data.has_null_key_data()) {
400
0
                                                 auto st = _destroy_agg_status(
401
0
                                                         data.template get_null_key_data<
402
0
                                                                 vectorized::AggregateDataPtr>());
403
0
                                                 if (!st) {
404
0
                                                     throw Exception(st.code(), st.to_string());
405
0
                                                 }
406
0
                                             }
407
4
                                         }},
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized19MethodStringNoCacheINS_13StringHashMapIPc9AllocatorILb1ELb1ELb0E22DefaultMemoryAllocatorEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS3_
_ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS3_
Line
Count
Source
391
4
                                         [&](auto& agg_method) -> void {
392
4
                                             auto& data = *agg_method.hash_table;
393
4
                                             data.for_each_mapped([&](auto& mapped) {
394
4
                                                 if (mapped) {
395
4
                                                     static_cast<void>(_destroy_agg_status(mapped));
396
4
                                                     mapped = nullptr;
397
4
                                                 }
398
4
                                             });
399
4
                                             if (data.has_null_key_data()) {
400
0
                                                 auto st = _destroy_agg_status(
401
0
                                                         data.template get_null_key_data<
402
0
                                                                 vectorized::AggregateDataPtr>());
403
0
                                                 if (!st) {
404
0
                                                     throw Exception(st.code(), st.to_string());
405
0
                                                 }
406
0
                                             }
407
4
                                         }},
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIhNS6_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberItNS6_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm128EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm256EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_19MethodStringNoCacheINS6_15DataWithNullKeyINS_13StringHashMapIPc9AllocatorILb1ELb1ELb0E22DefaultMemoryAllocatorEEEEEEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS3_
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapINS6_7UInt136EPc9HashCRC32IS9_EEEEEEvS3_
408
4
                   agg_data->method_variant);
409
4
    }
410
411
6
    void _close_without_key() {
412
        //because prepare maybe failed, and couldn't create agg data.
413
        //but finally call close to destory agg data, if agg data has bitmapValue
414
        //will be core dump, it's not initialized
415
6
        if (agg_data_created_without_key) {
416
5
            static_cast<void>(_destroy_agg_status(agg_data->without_key));
417
5
            agg_data_created_without_key = false;
418
5
        }
419
6
    }
420
    Status _destroy_agg_status(vectorized::AggregateDataPtr data);
421
};
422
423
struct BasicSpillSharedState {
424
31
    virtual ~BasicSpillSharedState() = default;
425
426
    // These two counters are shared to spill source operators as the initial value
427
    // of 'SpillWriteFileCurrentBytes' and 'SpillWriteFileCurrentCount'.
428
    // Total bytes of spill data written to disk file(after serialized)
429
    RuntimeProfile::Counter* _spill_write_file_total_size = nullptr;
430
    RuntimeProfile::Counter* _spill_file_total_count = nullptr;
431
432
5
    void setup_shared_profile(RuntimeProfile* sink_profile) {
433
5
        _spill_file_total_count =
434
5
                ADD_COUNTER_WITH_LEVEL(sink_profile, "SpillWriteFileTotalCount", TUnit::UNIT, 1);
435
5
        _spill_write_file_total_size =
436
5
                ADD_COUNTER_WITH_LEVEL(sink_profile, "SpillWriteFileBytes", TUnit::BYTES, 1);
437
5
    }
438
439
    virtual void update_spill_stream_profiles(RuntimeProfile* source_profile) = 0;
440
};
441
442
struct AggSpillPartition;
443
struct PartitionedAggSharedState : public BasicSharedState,
444
                                   public BasicSpillSharedState,
445
                                   public std::enable_shared_from_this<PartitionedAggSharedState> {
446
    ENABLE_FACTORY_CREATOR(PartitionedAggSharedState)
447
448
0
    PartitionedAggSharedState() = default;
449
0
    ~PartitionedAggSharedState() override = default;
450
451
    void update_spill_stream_profiles(RuntimeProfile* source_profile) override;
452
453
    void init_spill_params(size_t spill_partition_count);
454
455
    void close();
456
457
    AggSharedState* in_mem_shared_state = nullptr;
458
    std::shared_ptr<BasicSharedState> in_mem_shared_state_sptr;
459
460
    size_t partition_count;
461
    size_t max_partition_index;
462
    bool is_spilled = false;
463
    std::atomic_bool is_closed = false;
464
    std::deque<std::shared_ptr<AggSpillPartition>> spill_partitions;
465
466
0
    size_t get_partition_index(size_t hash_value) const { return hash_value % partition_count; }
467
};
468
469
struct AggSpillPartition {
470
    static constexpr int64_t AGG_SPILL_FILE_SIZE = 1024 * 1024 * 1024; // 1G
471
472
0
    AggSpillPartition() = default;
473
474
    void close();
475
476
    Status get_spill_stream(RuntimeState* state, int node_id, RuntimeProfile* profile,
477
                            vectorized::SpillStreamSPtr& spilling_stream);
478
479
0
    Status flush_if_full() {
480
0
        DCHECK(spilling_stream_);
481
0
        Status status;
482
        // avoid small spill files
483
0
        if (spilling_stream_->get_written_bytes() >= AGG_SPILL_FILE_SIZE) {
484
0
            status = spilling_stream_->spill_eof();
485
0
            spilling_stream_.reset();
486
0
        }
487
0
        return status;
488
0
    }
489
490
0
    Status finish_current_spilling(bool eos = false) {
491
0
        if (spilling_stream_) {
492
0
            if (eos || spilling_stream_->get_written_bytes() >= AGG_SPILL_FILE_SIZE) {
493
0
                auto status = spilling_stream_->spill_eof();
494
0
                spilling_stream_.reset();
495
0
                return status;
496
0
            }
497
0
        }
498
0
        return Status::OK();
499
0
    }
500
501
    std::deque<vectorized::SpillStreamSPtr> spill_streams_;
502
    vectorized::SpillStreamSPtr spilling_stream_;
503
};
504
using AggSpillPartitionSPtr = std::shared_ptr<AggSpillPartition>;
505
struct SortSharedState : public BasicSharedState {
506
    ENABLE_FACTORY_CREATOR(SortSharedState)
507
public:
508
    std::shared_ptr<vectorized::Sorter> sorter;
509
};
510
511
struct SpillSortSharedState : public BasicSharedState,
512
                              public BasicSpillSharedState,
513
                              public std::enable_shared_from_this<SpillSortSharedState> {
514
    ENABLE_FACTORY_CREATOR(SpillSortSharedState)
515
516
0
    SpillSortSharedState() = default;
517
0
    ~SpillSortSharedState() override = default;
518
519
0
    void update_spill_block_batch_row_count(RuntimeState* state, const vectorized::Block* block) {
520
0
        auto rows = block->rows();
521
0
        if (rows > 0 && 0 == avg_row_bytes) {
522
0
            avg_row_bytes = std::max((std::size_t)1, block->bytes() / rows);
523
0
            spill_block_batch_row_count =
524
0
                    (state->spill_sort_batch_bytes() + avg_row_bytes - 1) / avg_row_bytes;
525
0
            LOG(INFO) << "spill sort block batch row count: " << spill_block_batch_row_count;
526
0
        }
527
0
    }
528
529
    void update_spill_stream_profiles(RuntimeProfile* source_profile) override;
530
531
    void close();
532
533
    SortSharedState* in_mem_shared_state = nullptr;
534
    bool enable_spill = false;
535
    bool is_spilled = false;
536
    std::atomic_bool is_closed = false;
537
    std::shared_ptr<BasicSharedState> in_mem_shared_state_sptr;
538
539
    std::deque<vectorized::SpillStreamSPtr> sorted_streams;
540
    size_t avg_row_bytes = 0;
541
    size_t spill_block_batch_row_count;
542
};
543
544
struct UnionSharedState : public BasicSharedState {
545
    ENABLE_FACTORY_CREATOR(UnionSharedState)
546
547
public:
548
0
    UnionSharedState(int child_count = 1) : data_queue(child_count), _child_count(child_count) {};
549
0
    int child_count() const { return _child_count; }
550
    DataQueue data_queue;
551
    const int _child_count;
552
};
553
554
struct CacheSharedState : public BasicSharedState {
555
    ENABLE_FACTORY_CREATOR(CacheSharedState)
556
public:
557
    DataQueue data_queue;
558
};
559
560
class MultiCastDataStreamer;
561
562
struct MultiCastSharedState : public BasicSharedState,
563
                              public BasicSpillSharedState,
564
                              public std::enable_shared_from_this<MultiCastSharedState> {
565
    MultiCastSharedState(ObjectPool* pool, int cast_sender_count, int node_id);
566
    std::unique_ptr<pipeline::MultiCastDataStreamer> multi_cast_data_streamer;
567
568
    void update_spill_stream_profiles(RuntimeProfile* source_profile) override;
569
};
570
571
struct AnalyticSharedState : public BasicSharedState {
572
    ENABLE_FACTORY_CREATOR(AnalyticSharedState)
573
574
public:
575
0
    AnalyticSharedState() = default;
576
    std::queue<vectorized::Block> blocks_buffer;
577
    std::mutex buffer_mutex;
578
    bool sink_eos = false;
579
    std::mutex sink_eos_lock;
580
};
581
582
struct JoinSharedState : public BasicSharedState {
583
    // For some join case, we can apply a short circuit strategy
584
    // 1. _has_null_in_build_side = true
585
    // 2. build side rows is empty, Join op is: inner join/right outer join/left semi/right semi/right anti
586
    bool _has_null_in_build_side = false;
587
    bool short_circuit_for_probe = false;
588
    // for some join, when build side rows is empty, we could return directly by add some additional null data in probe table.
589
    bool empty_right_table_need_probe_dispose = false;
590
    JoinOpVariants join_op_variants;
591
};
592
593
struct HashJoinSharedState : public JoinSharedState {
594
    ENABLE_FACTORY_CREATOR(HashJoinSharedState)
595
    // mark the join column whether support null eq
596
    std::vector<bool> is_null_safe_eq_join;
597
598
    // mark the build hash table whether it needs to store null value
599
    std::vector<bool> serialize_null_into_key;
600
    std::shared_ptr<vectorized::Arena> arena = std::make_shared<vectorized::Arena>();
601
602
    // maybe share hash table with other fragment instances
603
    std::shared_ptr<JoinDataVariants> hash_table_variants = std::make_shared<JoinDataVariants>();
604
    const std::vector<TupleDescriptor*> build_side_child_desc;
605
    size_t build_exprs_size = 0;
606
    std::shared_ptr<vectorized::Block> build_block;
607
    std::shared_ptr<std::vector<uint32_t>> build_indexes_null;
608
};
609
610
struct PartitionedHashJoinSharedState
611
        : public HashJoinSharedState,
612
          public BasicSpillSharedState,
613
          public std::enable_shared_from_this<PartitionedHashJoinSharedState> {
614
    ENABLE_FACTORY_CREATOR(PartitionedHashJoinSharedState)
615
616
0
    void update_spill_stream_profiles(RuntimeProfile* source_profile) override {
617
0
        for (auto& stream : spilled_streams) {
618
0
            if (stream) {
619
0
                stream->update_shared_profiles(source_profile);
620
0
            }
621
0
        }
622
0
    }
623
624
    std::unique_ptr<RuntimeState> inner_runtime_state;
625
    std::shared_ptr<HashJoinSharedState> inner_shared_state;
626
    std::vector<std::unique_ptr<vectorized::MutableBlock>> partitioned_build_blocks;
627
    std::vector<vectorized::SpillStreamSPtr> spilled_streams;
628
    bool need_to_spill = false;
629
};
630
631
struct NestedLoopJoinSharedState : public JoinSharedState {
632
    ENABLE_FACTORY_CREATOR(NestedLoopJoinSharedState)
633
    // if true, left child has no more rows to process
634
    bool left_side_eos = false;
635
    // Visited flags for each row in build side.
636
    vectorized::MutableColumns build_side_visited_flags;
637
    // List of build blocks, constructed in prepare()
638
    vectorized::Blocks build_blocks;
639
};
640
641
struct PartitionSortNodeSharedState : public BasicSharedState {
642
    ENABLE_FACTORY_CREATOR(PartitionSortNodeSharedState)
643
public:
644
    std::queue<vectorized::Block> blocks_buffer;
645
    std::mutex buffer_mutex;
646
    std::vector<std::unique_ptr<vectorized::PartitionSorter>> partition_sorts;
647
    bool sink_eos = false;
648
    std::mutex sink_eos_lock;
649
};
650
651
struct SetSharedState : public BasicSharedState {
652
    ENABLE_FACTORY_CREATOR(SetSharedState)
653
public:
654
    /// default init
655
    vectorized::Block build_block; // build to source
656
    //record element size in hashtable
657
    int64_t valid_element_in_hash_tbl = 0;
658
    //first: idx mapped to column types
659
    //second: column_id, could point to origin column or cast column
660
    std::unordered_map<int, int> build_col_idx;
661
662
    //// shared static states (shared, decided in prepare/open...)
663
664
    /// init in setup_local_state
665
    std::unique_ptr<SetDataVariants> hash_table_variants =
666
            std::make_unique<SetDataVariants>(); // the real data HERE.
667
    std::vector<bool> build_not_ignore_null;
668
669
    // The SET operator's child might have different nullable attributes.
670
    // If a calculation involves both nullable and non-nullable columns, the final output should be a nullable column
671
    Status update_build_not_ignore_null(const vectorized::VExprContextSPtrs& ctxs);
672
673
    /// init in both upstream side.
674
    //The i-th result expr list refers to the i-th child.
675
    std::vector<vectorized::VExprContextSPtrs> child_exprs_lists;
676
677
    /// init in build side
678
    size_t child_quantity;
679
    vectorized::VExprContextSPtrs build_child_exprs;
680
    std::vector<Dependency*> probe_finished_children_dependency;
681
682
    /// init in probe side
683
    std::vector<vectorized::VExprContextSPtrs> probe_child_exprs_lists;
684
685
    std::atomic<bool> ready_for_read = false;
686
687
    /// called in setup_local_state
688
    Status hash_table_init();
689
};
690
691
enum class ExchangeType : uint8_t {
692
    NOOP = 0,
693
    // Shuffle data by Crc32HashPartitioner<LocalExchangeChannelIds>.
694
    HASH_SHUFFLE = 1,
695
    // Round-robin passthrough data blocks.
696
    PASSTHROUGH = 2,
697
    // Shuffle data by Crc32HashPartitioner<ShuffleChannelIds> (e.g. same as storage engine).
698
    BUCKET_HASH_SHUFFLE = 3,
699
    // Passthrough data blocks to all channels.
700
    BROADCAST = 4,
701
    // Passthrough data to channels evenly in an adaptive way.
702
    ADAPTIVE_PASSTHROUGH = 5,
703
    // Send all data to the first channel.
704
    PASS_TO_ONE = 6,
705
};
706
707
1
inline std::string get_exchange_type_name(ExchangeType idx) {
708
1
    switch (idx) {
709
0
    case ExchangeType::NOOP:
710
0
        return "NOOP";
711
1
    case ExchangeType::HASH_SHUFFLE:
712
1
        return "HASH_SHUFFLE";
713
0
    case ExchangeType::PASSTHROUGH:
714
0
        return "PASSTHROUGH";
715
0
    case ExchangeType::BUCKET_HASH_SHUFFLE:
716
0
        return "BUCKET_HASH_SHUFFLE";
717
0
    case ExchangeType::BROADCAST:
718
0
        return "BROADCAST";
719
0
    case ExchangeType::ADAPTIVE_PASSTHROUGH:
720
0
        return "ADAPTIVE_PASSTHROUGH";
721
0
    case ExchangeType::PASS_TO_ONE:
722
0
        return "PASS_TO_ONE";
723
1
    }
724
0
    throw Exception(Status::FatalError("__builtin_unreachable"));
725
1
}
726
727
struct DataDistribution {
728
75
    DataDistribution(ExchangeType type) : distribution_type(type) {}
729
    DataDistribution(ExchangeType type, const std::vector<TExpr>& partition_exprs_)
730
4
            : distribution_type(type), partition_exprs(partition_exprs_) {}
731
0
    DataDistribution(const DataDistribution& other) = default;
732
5
    bool need_local_exchange() const { return distribution_type != ExchangeType::NOOP; }
733
5
    DataDistribution& operator=(const DataDistribution& other) = default;
734
    ExchangeType distribution_type;
735
    std::vector<TExpr> partition_exprs;
736
};
737
738
class ExchangerBase;
739
740
struct LocalExchangeSharedState : public BasicSharedState {
741
public:
742
    ENABLE_FACTORY_CREATOR(LocalExchangeSharedState);
743
    LocalExchangeSharedState(int num_instances);
744
    ~LocalExchangeSharedState() override;
745
    std::unique_ptr<ExchangerBase> exchanger {};
746
    std::vector<RuntimeProfile::Counter*> mem_counters;
747
    std::atomic<int64_t> mem_usage = 0;
748
    std::atomic<size_t> _buffer_mem_limit = config::local_exchange_buffer_mem_limit;
749
    // We need to make sure to add mem_usage first and then enqueue, otherwise sub mem_usage may cause negative mem_usage during concurrent dequeue.
750
    std::mutex le_lock;
751
6
    void create_dependencies(int local_exchange_id) {
752
24
        for (auto& source_dep : source_deps) {
753
24
            source_dep = std::make_shared<Dependency>(local_exchange_id, local_exchange_id,
754
24
                                                      "LOCAL_EXCHANGE_OPERATOR_DEPENDENCY");
755
24
            source_dep->set_shared_state(this);
756
24
        }
757
6
    }
758
    void sub_running_sink_operators();
759
    void sub_running_source_operators();
760
10
    void _set_always_ready() {
761
40
        for (auto& dep : source_deps) {
762
40
            DCHECK(dep);
763
40
            dep->set_always_ready();
764
40
        }
765
10
        for (auto& dep : sink_deps) {
766
10
            DCHECK(dep);
767
10
            dep->set_always_ready();
768
10
        }
769
10
    }
770
771
24
    std::vector<DependencySPtr> get_dep_by_channel_id(int channel_id) {
772
24
        return {source_deps[channel_id]};
773
24
    }
774
0
    Dependency* get_sink_dep_by_channel_id(int channel_id) { return nullptr; }
775
776
129
    void set_ready_to_read(int channel_id) {
777
129
        auto& dep = source_deps[channel_id];
778
129
        DCHECK(dep) << channel_id;
779
129
        dep->set_ready();
780
129
    }
781
782
161
    void add_mem_usage(int channel_id, size_t delta) { mem_counters[channel_id]->update(delta); }
783
784
125
    void sub_mem_usage(int channel_id, size_t delta) {
785
125
        mem_counters[channel_id]->update(-(int64_t)delta);
786
125
    }
787
788
114
    void add_total_mem_usage(size_t delta) {
789
114
        if (cast_set<int64_t>(mem_usage.fetch_add(delta) + delta) > _buffer_mem_limit) {
790
15
            sink_deps.front()->block();
791
15
        }
792
114
    }
793
794
114
    void sub_total_mem_usage(size_t delta) {
795
114
        auto prev_usage = mem_usage.fetch_sub(delta);
796
114
        DCHECK_GE(prev_usage - delta, 0) << "prev_usage: " << prev_usage << " delta: " << delta;
797
114
        if (cast_set<int64_t>(prev_usage - delta) <= _buffer_mem_limit) {
798
102
            sink_deps.front()->set_ready();
799
102
        }
800
114
    }
801
802
0
    void set_low_memory_mode(RuntimeState* state) {
803
0
        _buffer_mem_limit = std::min<int64_t>(config::local_exchange_buffer_mem_limit,
804
0
                                              state->low_memory_mode_buffer_limit());
805
0
    }
806
};
807
808
class QueryGlobalDependency final : public Dependency {
809
    ENABLE_FACTORY_CREATOR(QueryGlobalDependency);
810
344
    QueryGlobalDependency(std::string name, bool ready = false) : Dependency(-1, -1, name, ready) {}
811
344
    ~QueryGlobalDependency() override = default;
812
    Dependency* is_blocked_by(PipelineTask* task = nullptr) override;
813
};
814
#include "common/compile_check_end.h"
815
} // namespace doris::pipeline