Coverage Report

Created: 2025-04-16 14:36

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