Coverage Report

Created: 2026-05-08 23:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/pipeline/dependency.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#ifdef __APPLE__
21
#include <netinet/in.h>
22
#include <sys/_types/_u_int.h>
23
#endif
24
25
#include <concurrentqueue.h>
26
#include <gen_cpp/internal_service.pb.h>
27
#include <sqltypes.h>
28
29
#include <atomic>
30
#include <functional>
31
#include <memory>
32
#include <mutex>
33
#include <thread>
34
#include <utility>
35
36
#include "common/config.h"
37
#include "common/logging.h"
38
#include "core/block/block.h"
39
#include "core/types.h"
40
#include "exec/common/agg_utils.h"
41
#include "exec/common/join_utils.h"
42
#include "exec/common/set_utils.h"
43
#include "exec/operator/data_queue.h"
44
#include "exec/operator/join/process_hash_table_probe.h"
45
#include "exec/sort/partition_sorter.h"
46
#include "exec/sort/sorter.h"
47
#include "exec/spill/spill_file.h"
48
#include "runtime/runtime_profile_counter_names.h"
49
#include "util/brpc_closure.h"
50
#include "util/stack_util.h"
51
52
namespace doris {
53
class AggFnEvaluator;
54
class VSlotRef;
55
} // namespace doris
56
57
namespace doris {
58
class Dependency;
59
class PipelineTask;
60
struct BasicSharedState;
61
using DependencySPtr = std::shared_ptr<Dependency>;
62
class LocalExchangeSourceLocalState;
63
64
static constexpr auto SLOW_DEPENDENCY_THRESHOLD = 60 * 1000L * 1000L * 1000L;
65
static constexpr auto TIME_UNIT_DEPENDENCY_LOG = 30 * 1000L * 1000L * 1000L;
66
static_assert(TIME_UNIT_DEPENDENCY_LOG < SLOW_DEPENDENCY_THRESHOLD);
67
68
struct BasicSharedState {
69
    ENABLE_FACTORY_CREATOR(BasicSharedState)
70
71
    template <class TARGET>
72
96.5k
    TARGET* cast() {
73
96.5k
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
96.5k
        return reinterpret_cast<TARGET*>(this);
77
96.5k
    }
_ZN5doris16BasicSharedState4castINS_19HashJoinSharedStateEEEPT_v
Line
Count
Source
72
96.0k
    TARGET* cast() {
73
96.0k
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
96.0k
        return reinterpret_cast<TARGET*>(this);
77
96.0k
    }
_ZN5doris16BasicSharedState4castINS_30PartitionedHashJoinSharedStateEEEPT_v
Line
Count
Source
72
3
    TARGET* cast() {
73
3
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
3
        return reinterpret_cast<TARGET*>(this);
77
3
    }
_ZN5doris16BasicSharedState4castINS_15SortSharedStateEEEPT_v
Line
Count
Source
72
49
    TARGET* cast() {
73
49
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
49
        return reinterpret_cast<TARGET*>(this);
77
49
    }
_ZN5doris16BasicSharedState4castINS_20SpillSortSharedStateEEEPT_v
Line
Count
Source
72
28
    TARGET* cast() {
73
28
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
28
        return reinterpret_cast<TARGET*>(this);
77
28
    }
Unexecuted instantiation: _ZN5doris16BasicSharedState4castINS_25NestedLoopJoinSharedStateEEEPT_v
_ZN5doris16BasicSharedState4castINS_19AnalyticSharedStateEEEPT_v
Line
Count
Source
72
18
    TARGET* cast() {
73
18
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
18
        return reinterpret_cast<TARGET*>(this);
77
18
    }
_ZN5doris16BasicSharedState4castINS_14AggSharedStateEEEPT_v
Line
Count
Source
72
84
    TARGET* cast() {
73
84
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
84
        return reinterpret_cast<TARGET*>(this);
77
84
    }
Unexecuted instantiation: _ZN5doris16BasicSharedState4castINS_22BucketedAggSharedStateEEEPT_v
_ZN5doris16BasicSharedState4castINS_25PartitionedAggSharedStateEEEPT_v
Line
Count
Source
72
30
    TARGET* cast() {
73
30
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
30
        return reinterpret_cast<TARGET*>(this);
77
30
    }
_ZN5doris16BasicSharedState4castINS_16UnionSharedStateEEEPT_v
Line
Count
Source
72
4
    TARGET* cast() {
73
4
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
4
        return reinterpret_cast<TARGET*>(this);
77
4
    }
_ZN5doris16BasicSharedState4castINS_28PartitionSortNodeSharedStateEEEPT_v
Line
Count
Source
72
204
    TARGET* cast() {
73
204
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
204
        return reinterpret_cast<TARGET*>(this);
77
204
    }
Unexecuted instantiation: _ZN5doris16BasicSharedState4castINS_20MultiCastSharedStateEEEPT_v
_ZN5doris16BasicSharedState4castINS_14SetSharedStateEEEPT_v
Line
Count
Source
72
39
    TARGET* cast() {
73
39
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
39
        return reinterpret_cast<TARGET*>(this);
77
39
    }
Unexecuted instantiation: _ZN5doris16BasicSharedState4castINS_24LocalExchangeSharedStateEEEPT_v
_ZN5doris16BasicSharedState4castIS0_EEPT_v
Line
Count
Source
72
17
    TARGET* cast() {
73
17
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
17
        return reinterpret_cast<TARGET*>(this);
77
17
    }
_ZN5doris16BasicSharedState4castINS_20DataQueueSharedStateEEEPT_v
Line
Count
Source
72
6
    TARGET* cast() {
73
6
        DCHECK(dynamic_cast<TARGET*>(this))
74
0
                << " Mismatch type! Current type is " << typeid(*this).name()
75
0
                << " and expect type is" << typeid(TARGET).name();
76
6
        return reinterpret_cast<TARGET*>(this);
77
6
    }
Unexecuted instantiation: _ZN5doris16BasicSharedState4castINS_17RecCTESharedStateEEEPT_v
78
    template <class TARGET>
79
    const TARGET* cast() const {
80
        DCHECK(dynamic_cast<const TARGET*>(this))
81
                << " Mismatch type! Current type is " << typeid(*this).name()
82
                << " and expect type is" << typeid(TARGET).name();
83
        return reinterpret_cast<const TARGET*>(this);
84
    }
85
    std::vector<DependencySPtr> source_deps;
86
    std::vector<DependencySPtr> sink_deps;
87
    int id = 0;
88
    std::set<int> related_op_ids;
89
90
72.4k
    virtual ~BasicSharedState() = default;
91
92
    void create_source_dependencies(int num_sources, int operator_id, int node_id,
93
                                    const std::string& name);
94
    Dependency* create_source_dependency(int operator_id, int node_id, const std::string& name);
95
96
    Dependency* create_sink_dependency(int dest_id, int node_id, const std::string& name);
97
24
    std::vector<DependencySPtr> get_dep_by_channel_id(int channel_id) {
98
24
        DCHECK_LT(channel_id, source_deps.size());
99
24
        return {source_deps[channel_id]};
100
24
    }
101
};
102
103
class Dependency : public std::enable_shared_from_this<Dependency> {
104
public:
105
    ENABLE_FACTORY_CREATOR(Dependency);
106
    Dependency(int id, int node_id, std::string name, bool ready = false)
107
485k
            : _id(id), _node_id(node_id), _name(std::move(name)), _ready(ready) {}
108
485k
    virtual ~Dependency() = default;
109
110
0
    [[nodiscard]] int id() const { return _id; }
111
96.5k
    [[nodiscard]] virtual std::string name() const { return _name; }
112
4
    BasicSharedState* shared_state() { return _shared_state; }
113
144k
    void set_shared_state(BasicSharedState* shared_state) { _shared_state = shared_state; }
114
    virtual std::string debug_string(int indentation_level = 0);
115
870M
    bool ready() const { return _ready; }
116
117
    // Start the watcher. We use it to count how long this dependency block the current pipeline task.
118
26
    void start_watcher() { _watcher.start(); }
119
96.1k
    [[nodiscard]] int64_t watcher_elapse_time() { return _watcher.elapsed_time(); }
120
121
    // Which dependency current pipeline task is blocked by. `nullptr` if this dependency is ready.
122
    [[nodiscard]] Dependency* is_blocked_by(std::shared_ptr<PipelineTask> task = nullptr);
123
    // Notify downstream pipeline tasks this dependency is ready.
124
    void set_ready();
125
48.6k
    void set_ready_to_read(int channel_id = 0) {
126
48.6k
        DCHECK_LT(channel_id, _shared_state->source_deps.size()) << debug_string();
127
48.6k
        _shared_state->source_deps[channel_id]->set_ready();
128
48.6k
    }
129
0
    void set_ready_to_write() {
130
0
        DCHECK_EQ(_shared_state->sink_deps.size(), 1) << debug_string();
131
0
        _shared_state->sink_deps.front()->set_ready();
132
0
    }
133
134
    // Notify downstream pipeline tasks this dependency is blocked.
135
1.94k
    void block() {
136
1.94k
        if (_always_ready) {
137
13
            return;
138
13
        }
139
1.92k
        std::unique_lock<std::mutex> lc(_always_ready_lock);
140
1.92k
        if (_always_ready) {
141
0
            return;
142
0
        }
143
1.92k
        _ready = false;
144
1.92k
    }
145
146
154
    void set_always_ready() {
147
154
        if (_always_ready) {
148
31
            return;
149
31
        }
150
123
        std::unique_lock<std::mutex> lc(_always_ready_lock);
151
123
        if (_always_ready) {
152
0
            return;
153
0
        }
154
123
        _always_ready = true;
155
123
        set_ready();
156
123
    }
157
158
protected:
159
    void _add_block_task(std::shared_ptr<PipelineTask> task);
160
161
    const int _id;
162
    const int _node_id;
163
    const std::string _name;
164
    std::atomic<bool> _ready;
165
166
    BasicSharedState* _shared_state = nullptr;
167
    MonotonicStopWatch _watcher;
168
169
    std::mutex _task_lock;
170
    std::vector<std::weak_ptr<PipelineTask>> _blocked_task;
171
172
    // If `_always_ready` is true, `block()` will never block tasks.
173
    std::atomic<bool> _always_ready = false;
174
    std::mutex _always_ready_lock;
175
};
176
177
struct FakeSharedState final : public BasicSharedState {
178
    ENABLE_FACTORY_CREATOR(FakeSharedState)
179
};
180
181
class CountedFinishDependency final : public Dependency {
182
public:
183
    using SharedState = FakeSharedState;
184
    CountedFinishDependency(int id, int node_id, std::string name)
185
96.0k
            : Dependency(id, node_id, std::move(name), true) {}
186
187
18
    void add(uint32_t count = 1) {
188
18
        std::unique_lock<std::mutex> l(_mtx);
189
18
        if (!_counter) {
190
17
            block();
191
17
        }
192
18
        _counter += count;
193
18
    }
194
195
19
    void sub() {
196
19
        std::unique_lock<std::mutex> l(_mtx);
197
        // _counter is unsigned: a stray sub() when counter is already 0 would
198
        // underflow to UINT32_MAX and the dependency would never become ready,
199
        // hanging the query forever. Fail loudly instead.
200
19
        if (_counter == 0) [[unlikely]] {
201
2
            throw Exception(ErrorCode::INTERNAL_ERROR,
202
2
                            "CountedFinishDependency::sub() underflow on {}", debug_string());
203
2
        }
204
17
        _counter--;
205
17
        if (!_counter) {
206
14
            set_ready();
207
14
        }
208
17
    }
209
210
    std::string debug_string(int indentation_level = 0) override;
211
212
private:
213
    std::mutex _mtx;
214
    uint32_t _counter = 0;
215
};
216
217
struct RuntimeFilterTimerQueue;
218
class RuntimeFilterTimer {
219
public:
220
    RuntimeFilterTimer(int64_t registration_time, int32_t wait_time_ms,
221
                       std::shared_ptr<Dependency> parent, bool force_wait_timeout = false)
222
4
            : _parent(std::move(parent)),
223
4
              _registration_time(registration_time),
224
4
              _wait_time_ms(wait_time_ms),
225
4
              _force_wait_timeout(force_wait_timeout) {}
226
227
    // Called by runtime filter producer.
228
    void call_ready();
229
230
    // Called by RuntimeFilterTimerQueue which is responsible for checking if this rf is timeout.
231
    void call_timeout();
232
233
4
    int64_t registration_time() const { return _registration_time; }
234
4
    int32_t wait_time_ms() const { return _wait_time_ms; }
235
236
    void set_local_runtime_filter_dependencies(
237
0
            const std::vector<std::shared_ptr<Dependency>>& deps) {
238
0
        _local_runtime_filter_dependencies = deps;
239
0
    }
240
241
    bool should_be_check_timeout();
242
243
4
    bool force_wait_timeout() { return _force_wait_timeout; }
244
245
private:
246
    friend struct RuntimeFilterTimerQueue;
247
    std::shared_ptr<Dependency> _parent = nullptr;
248
    std::vector<std::shared_ptr<Dependency>> _local_runtime_filter_dependencies;
249
    std::mutex _lock;
250
    int64_t _registration_time;
251
    const int32_t _wait_time_ms;
252
    // true only for group_commit_scan_operator
253
    bool _force_wait_timeout;
254
};
255
256
struct RuntimeFilterTimerQueue {
257
    constexpr static int64_t interval = 10;
258
2
    void run() { _thread.detach(); }
259
    void start();
260
261
0
    void stop() {
262
0
        _stop = true;
263
0
        cv.notify_all();
264
0
        wait_for_shutdown();
265
0
    }
266
267
0
    void wait_for_shutdown() const {
268
0
        while (!_shutdown) {
269
0
            std::this_thread::sleep_for(std::chrono::milliseconds(interval));
270
0
        }
271
0
    }
272
273
0
    ~RuntimeFilterTimerQueue() = default;
274
2
    RuntimeFilterTimerQueue() { _thread = std::thread(&RuntimeFilterTimerQueue::start, this); }
275
2
    void push_filter_timer(std::vector<std::shared_ptr<RuntimeFilterTimer>>&& filter) {
276
2
        std::unique_lock<std::mutex> lc(_que_lock);
277
2
        _que.insert(_que.end(), filter.begin(), filter.end());
278
2
        cv.notify_all();
279
2
    }
280
281
    std::thread _thread;
282
    std::condition_variable cv;
283
    std::mutex cv_m;
284
    std::mutex _que_lock;
285
    std::atomic_bool _stop = false;
286
    std::atomic_bool _shutdown = false;
287
    std::list<std::shared_ptr<RuntimeFilterTimer>> _que;
288
};
289
290
struct AggSharedState : public BasicSharedState {
291
    ENABLE_FACTORY_CREATOR(AggSharedState)
292
public:
293
55
    AggSharedState() { agg_data = std::make_unique<AggregatedDataVariants>(); }
294
55
    ~AggSharedState() override {
295
55
        if (!probe_expr_ctxs.empty()) {
296
39
            _close_with_serialized_key();
297
39
        } else {
298
16
            _close_without_key();
299
16
        }
300
55
    }
301
302
    Status reset_hash_table();
303
304
    bool do_limit_filter(Block* block, size_t num_rows, const std::vector<int>* key_locs = nullptr);
305
    void build_limit_heap(size_t hash_table_size);
306
307
    // We should call this function only at 1st phase.
308
    // 1st phase: is_merge=true, only have one SlotRef.
309
    // 2nd phase: is_merge=false, maybe have multiple exprs.
310
    static int get_slot_column_id(const AggFnEvaluator* evaluator);
311
312
    AggregatedDataVariantsUPtr agg_data = nullptr;
313
    std::unique_ptr<AggregateDataContainer> aggregate_data_container;
314
    std::vector<AggFnEvaluator*> aggregate_evaluators;
315
    // group by k1,k2
316
    VExprContextSPtrs probe_expr_ctxs;
317
    size_t input_num_rows = 0;
318
    std::vector<AggregateDataPtr> values;
319
    /// The total size of the row from the aggregate functions.
320
    size_t total_size_of_aggregate_states = 0;
321
    size_t align_aggregate_states = 1;
322
    /// The offset to the n-th aggregate function in a row of aggregate functions.
323
    Sizes offsets_of_aggregate_states;
324
    std::vector<size_t> make_nullable_keys;
325
326
    bool agg_data_created_without_key = false;
327
    bool enable_spill = false;
328
    bool reach_limit = false;
329
330
    bool use_simple_count = false;
331
    int64_t limit = -1;
332
    bool do_sort_limit = false;
333
    MutableColumns limit_columns;
334
    int limit_columns_min = -1;
335
    PaddedPODArray<uint8_t> need_computes;
336
    std::vector<uint8_t> cmp_res;
337
    std::vector<int> order_directions;
338
    std::vector<int> null_directions;
339
340
    struct HeapLimitCursor {
341
        HeapLimitCursor(int row_id, MutableColumns& limit_columns,
342
                        std::vector<int>& order_directions, std::vector<int>& null_directions)
343
32
                : _row_id(row_id),
344
32
                  _limit_columns(limit_columns),
345
32
                  _order_directions(order_directions),
346
32
                  _null_directions(null_directions) {}
347
348
        HeapLimitCursor(const HeapLimitCursor& other) = default;
349
350
        HeapLimitCursor(HeapLimitCursor&& other) noexcept
351
152
                : _row_id(other._row_id),
352
152
                  _limit_columns(other._limit_columns),
353
152
                  _order_directions(other._order_directions),
354
152
                  _null_directions(other._null_directions) {}
355
356
0
        HeapLimitCursor& operator=(const HeapLimitCursor& other) noexcept {
357
0
            _row_id = other._row_id;
358
0
            return *this;
359
0
        }
360
361
129
        HeapLimitCursor& operator=(HeapLimitCursor&& other) noexcept {
362
129
            _row_id = other._row_id;
363
129
            return *this;
364
129
        }
365
366
79
        bool operator<(const HeapLimitCursor& rhs) const {
367
85
            for (int i = 0; i < _limit_columns.size(); ++i) {
368
79
                const auto& _limit_column = _limit_columns[i];
369
79
                auto res = _limit_column->compare_at(_row_id, rhs._row_id, *_limit_column,
370
79
                                                     _null_directions[i]) *
371
79
                           _order_directions[i];
372
79
                if (res < 0) {
373
46
                    return true;
374
46
                } else if (res > 0) {
375
27
                    return false;
376
27
                }
377
79
            }
378
6
            return false;
379
79
        }
380
381
        int _row_id;
382
        MutableColumns& _limit_columns;
383
        std::vector<int>& _order_directions;
384
        std::vector<int>& _null_directions;
385
    };
386
387
    std::priority_queue<HeapLimitCursor> limit_heap;
388
389
    // Refresh the top limit heap with a new row
390
    void refresh_top_limit(size_t row_id, const ColumnRawPtrs& key_columns);
391
392
    Arena agg_arena_pool;
393
    Arena agg_profile_arena;
394
395
private:
396
    MutableColumns _get_keys_hash_table();
397
398
39
    void _close_with_serialized_key() {
399
39
        std::visit(Overload {[&](std::monostate& arg) -> void {
400
                                 // Do nothing
401
0
                             },
402
39
                             [&](auto& agg_method) -> void {
403
39
                                 if (use_simple_count) {
404
                                     // Inline count: mapped slots hold UInt64,
405
                                     // not real agg state pointers. Skip destroy.
406
0
                                     return;
407
0
                                 }
408
39
                                 auto& data = *agg_method.hash_table;
409
91
                                 data.for_each_mapped([&](auto& mapped) {
410
91
                                     if (mapped) {
411
91
                                         _destroy_agg_status(mapped);
412
91
                                         mapped = nullptr;
413
91
                                     }
414
91
                                 });
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32IS9_EEEEEEvS2_ENKUlS2_E_clISA_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt136EPc9HashCRC32IS7_EEEEEEvS2_ENKUlS2_E_clIS8_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32IS9_EEEEEEvS2_ENKUlS2_E_clISA_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt104EPc9HashCRC32IS7_EEEEEEvS2_ENKUlS2_E_clIS8_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt96EPc9HashCRC32IS7_EEEEEEvS2_ENKUlS2_E_clIS8_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt72EPc9HashCRC32IS7_EEEEEEvS2_ENKUlS2_E_clIS8_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_19MethodStringNoCacheINS_15DataWithNullKeyINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm256EjEENS_15DataWithNullKeyI9PHHashMapIS9_Pc9HashCRC32IS9_EEEEEEEEEEvS2_ENKUlS2_E_clISC_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm128EjEENS_15DataWithNullKeyI9PHHashMapIS9_Pc9HashCRC32IS9_EEEEEEEEEEvS2_ENKUlS2_E_clISC_EEDaS2_
_ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Line
Count
Source
409
20
                                 data.for_each_mapped([&](auto& mapped) {
410
20
                                     if (mapped) {
411
20
                                         _destroy_agg_status(mapped);
412
20
                                         mapped = nullptr;
413
20
                                     }
414
20
                                 });
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberItNS_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIhNS_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS2_ENKUlS2_E_clIS9_EEDaS2_
_ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Line
Count
Source
409
55
                                 data.for_each_mapped([&](auto& mapped) {
410
55
                                     if (mapped) {
411
55
                                         _destroy_agg_status(mapped);
412
55
                                         mapped = nullptr;
413
55
                                     }
414
55
                                 });
_ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Line
Count
Source
409
16
                                 data.for_each_mapped([&](auto& mapped) {
410
16
                                     if (mapped) {
411
16
                                         _destroy_agg_status(mapped);
412
16
                                         mapped = nullptr;
413
16
                                     }
414
16
                                 });
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapIS8_Pc9HashCRC32IS8_EEEEEEvS2_ENKUlS2_E_clISA_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapIS8_Pc9HashCRC32IS8_EEEEEEvS2_ENKUlS2_E_clISA_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_19MethodStringNoCacheINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS2_ENKUlS2_E_clIS7_EEDaS2_
Unexecuted instantiation: _ZZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS7_vEEEEEEvS2_ENKUlS2_E_clIS8_EEDaS2_
415
39
                                 if (data.has_null_key_data()) {
416
5
                                     _destroy_agg_status(
417
5
                                             data.template get_null_key_data<AggregateDataPtr>());
418
5
                                 }
419
39
                             }},
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32IS9_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt136EPc9HashCRC32IS7_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32IS9_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt104EPc9HashCRC32IS7_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt96EPc9HashCRC32IS7_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt72EPc9HashCRC32IS7_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_19MethodStringNoCacheINS_15DataWithNullKeyINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm256EjEENS_15DataWithNullKeyI9PHHashMapIS9_Pc9HashCRC32IS9_EEEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm128EjEENS_15DataWithNullKeyI9PHHashMapIS9_Pc9HashCRC32IS9_EEEEEEEEEEvS2_
_ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS2_
Line
Count
Source
402
5
                             [&](auto& agg_method) -> void {
403
5
                                 if (use_simple_count) {
404
                                     // Inline count: mapped slots hold UInt64,
405
                                     // not real agg state pointers. Skip destroy.
406
0
                                     return;
407
0
                                 }
408
5
                                 auto& data = *agg_method.hash_table;
409
5
                                 data.for_each_mapped([&](auto& mapped) {
410
5
                                     if (mapped) {
411
5
                                         _destroy_agg_status(mapped);
412
5
                                         mapped = nullptr;
413
5
                                     }
414
5
                                 });
415
5
                                 if (data.has_null_key_data()) {
416
5
                                     _destroy_agg_status(
417
5
                                             data.template get_null_key_data<AggregateDataPtr>());
418
5
                                 }
419
5
                             }},
_ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS2_
Line
Count
Source
402
1
                             [&](auto& agg_method) -> void {
403
1
                                 if (use_simple_count) {
404
                                     // Inline count: mapped slots hold UInt64,
405
                                     // not real agg state pointers. Skip destroy.
406
0
                                     return;
407
0
                                 }
408
1
                                 auto& data = *agg_method.hash_table;
409
1
                                 data.for_each_mapped([&](auto& mapped) {
410
1
                                     if (mapped) {
411
1
                                         _destroy_agg_status(mapped);
412
1
                                         mapped = nullptr;
413
1
                                     }
414
1
                                 });
415
1
                                 if (data.has_null_key_data()) {
416
0
                                     _destroy_agg_status(
417
0
                                             data.template get_null_key_data<AggregateDataPtr>());
418
0
                                 }
419
1
                             }},
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberItNS_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIhNS_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS2_
_ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS2_
Line
Count
Source
402
14
                             [&](auto& agg_method) -> void {
403
14
                                 if (use_simple_count) {
404
                                     // Inline count: mapped slots hold UInt64,
405
                                     // not real agg state pointers. Skip destroy.
406
0
                                     return;
407
0
                                 }
408
14
                                 auto& data = *agg_method.hash_table;
409
14
                                 data.for_each_mapped([&](auto& mapped) {
410
14
                                     if (mapped) {
411
14
                                         _destroy_agg_status(mapped);
412
14
                                         mapped = nullptr;
413
14
                                     }
414
14
                                 });
415
14
                                 if (data.has_null_key_data()) {
416
0
                                     _destroy_agg_status(
417
0
                                             data.template get_null_key_data<AggregateDataPtr>());
418
0
                                 }
419
14
                             }},
_ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS2_
Line
Count
Source
402
19
                             [&](auto& agg_method) -> void {
403
19
                                 if (use_simple_count) {
404
                                     // Inline count: mapped slots hold UInt64,
405
                                     // not real agg state pointers. Skip destroy.
406
0
                                     return;
407
0
                                 }
408
19
                                 auto& data = *agg_method.hash_table;
409
19
                                 data.for_each_mapped([&](auto& mapped) {
410
19
                                     if (mapped) {
411
19
                                         _destroy_agg_status(mapped);
412
19
                                         mapped = nullptr;
413
19
                                     }
414
19
                                 });
415
19
                                 if (data.has_null_key_data()) {
416
0
                                     _destroy_agg_status(
417
0
                                             data.template get_null_key_data<AggregateDataPtr>());
418
0
                                 }
419
19
                             }},
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapIS8_Pc9HashCRC32IS8_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapIS8_Pc9HashCRC32IS8_EEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_19MethodStringNoCacheINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS2_
Unexecuted instantiation: _ZZN5doris14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS7_vEEEEEEvS2_
420
39
                   agg_data->method_variant);
421
39
    }
422
423
16
    void _close_without_key() {
424
        //because prepare maybe failed, and couldn't create agg data.
425
        //but finally call close to destory agg data, if agg data has bitmapValue
426
        //will be core dump, it's not initialized
427
16
        if (agg_data_created_without_key) {
428
8
            _destroy_agg_status(agg_data->without_key);
429
8
            agg_data_created_without_key = false;
430
8
        }
431
16
    }
432
    void _destroy_agg_status(AggregateDataPtr data);
433
};
434
435
static constexpr int BUCKETED_AGG_NUM_BUCKETS = 256;
436
437
/// Shared state for BucketedAggSinkOperatorX / BucketedAggSourceOperatorX.
438
///
439
/// Each sink pipeline instance owns 256 per-bucket hash tables (two-level hash table
440
/// approach, inspired by ClickHouse). During sink, each row is routed to bucket
441
/// (hash >> 24) & 0xFF.
442
///
443
/// Source-side merge is pipelined with sink completion: as each sink instance finishes,
444
/// it unblocks all source dependencies. Source instances scan buckets and merge data
445
/// from finished sink instances into the merge target (the first sink to finish).
446
/// Each bucket has a CAS lock so only one source works on a bucket at a time.
447
/// After all sinks finish and all buckets are merged + output, one source handles
448
/// null key merge and the pipeline completes.
449
///
450
/// Thread safety model:
451
///  - Sink phase: each instance writes only to its own per_instance_data[task_idx]. No locking.
452
///  - Source phase: per-bucket CAS lock (merge_in_progress). Under the lock, a source
453
///    scans all finished sink instances and merges their bucket data into the merge
454
///    target's bucket. Already-merged entries are nulled out to prevent re-processing.
455
///    Output is only done when all sinks have finished and the bucket is fully merged.
456
struct BucketedAggSharedState : public BasicSharedState {
457
    ENABLE_FACTORY_CREATOR(BucketedAggSharedState)
458
public:
459
0
    BucketedAggSharedState() = default;
460
0
    ~BucketedAggSharedState() override { _close(); }
461
462
    /// Per-instance data. One per sink pipeline instance.
463
    /// Each instance has 256 bucket hash tables + 1 shared arena.
464
    struct PerInstanceData {
465
        /// 256 per-bucket hash tables. Each bucket has its own BucketedAggDataVariants.
466
        /// Uses PHHashMap<StringRef> for string keys instead of StringHashMap.
467
        std::vector<BucketedAggDataVariantsUPtr> bucket_agg_data;
468
        ArenaUPtr arena;
469
470
0
        PerInstanceData() : arena(std::make_unique<Arena>()) {
471
0
            bucket_agg_data.resize(BUCKETED_AGG_NUM_BUCKETS);
472
0
            for (auto& p : bucket_agg_data) {
473
0
                p = std::make_unique<BucketedAggDataVariants>();
474
0
            }
475
0
        }
476
    };
477
478
    /// Per-bucket merge state for pipelined source-side processing.
479
    struct BucketMergeState {
480
        /// CAS lock: only one source instance can merge/output this bucket at a time.
481
        std::atomic<bool> merge_in_progress {false};
482
        /// Set to true once the bucket is fully merged and all rows have been output.
483
        std::atomic<bool> output_done {false};
484
        /// Tracks which sink instances have been merged into the merge target
485
        /// for this bucket. Accessed only under merge_in_progress CAS lock.
486
        /// Element i is true when instance i's data for this bucket has been merged.
487
        /// Sized to num_sink_instances in init_instances().
488
        std::vector<bool> merged_instances;
489
    };
490
491
    std::vector<PerInstanceData> per_instance_data;
492
    int num_sink_instances = 0;
493
494
    /// Tracks how many sinks have finished. Incremented by each sink on EOS.
495
    std::atomic<int> num_sinks_finished = 0;
496
497
    /// Per-sink completion flags. Set to true when each sink instance finishes.
498
    /// Source instances read these to know which sinks' data is safe to merge.
499
    std::unique_ptr<std::atomic<bool>[]> sink_finished;
500
501
    /// Index of the first sink instance to finish. Its bucket hash tables serve
502
    /// as the merge target — all other sinks' data is merged into it.
503
    /// Initialized to -1; the first sink to finish CAS-sets it to its instance idx.
504
    std::atomic<int> merge_target_instance = -1;
505
506
    /// Per-bucket merge state. Indexed by bucket id [0, 256).
507
    std::array<BucketMergeState, BUCKETED_AGG_NUM_BUCKETS> bucket_states;
508
509
    // Aggregate function metadata (shared, read-only after init).
510
    std::vector<AggFnEvaluator*> aggregate_evaluators;
511
    VExprContextSPtrs probe_expr_ctxs;
512
    size_t total_size_of_aggregate_states = 0;
513
    size_t align_aggregate_states = 1;
514
    Sizes offsets_of_aggregate_states;
515
    std::vector<size_t> make_nullable_keys;
516
517
    std::atomic<size_t> input_num_rows {0};
518
519
    /// When true, the aggregate has exactly one COUNT(*) function with no args.
520
    /// In this case, mapped values in the hash table store a UInt64 counter
521
    /// directly (reinterpret_cast<AggregateDataPtr>) instead of a pointer to
522
    /// allocated aggregate state. This eliminates create/merge/destroy overhead.
523
    bool use_simple_count = false;
524
525
    // ---- Source-side fields ----
526
527
    // Null key handling: null keys are stored separately (not in any bucket).
528
    // After all buckets are processed, one source instance merges and outputs
529
    // all null key data. This atomic ensures exactly one source instance does it.
530
    std::atomic<bool> null_key_output_claimed {false};
531
532
    /// Monotonically increasing counter bumped on every state change (bucket lock
533
    /// release, sink finish). Used by source instances to detect missed wakeups:
534
    /// if the generation changed between scan start and post-block() re-check,
535
    /// something happened and the source should unblock immediately.
536
    std::atomic<uint64_t> state_generation {0};
537
538
    /// Initialize per-instance data and optionally run a metadata init callback.
539
    /// The callback runs exactly once (under std::call_once), must return Status,
540
    /// and should populate shared metadata like probe_expr_ctxs, aggregate_evaluators, etc.
541
    /// All threads observe the same init status via _init_status.
542
    template <typename Func>
543
0
    Status init_instances(int num_instances, Func&& metadata_init) {
544
0
        std::call_once(_init_once, [&]() {
545
0
            num_sink_instances = num_instances;
546
0
            per_instance_data.resize(num_instances);
547
0
            sink_finished = std::make_unique<std::atomic<bool>[]>(num_instances);
548
0
            for (int i = 0; i < num_instances; ++i) {
549
0
                sink_finished[i].store(false, std::memory_order_relaxed);
550
0
            }
551
0
            for (auto& bs : bucket_states) {
552
0
                bs.merged_instances.resize(num_instances, false);
553
0
            }
554
0
            _init_status = std::forward<Func>(metadata_init)();
555
0
        });
556
0
        return _init_status;
557
0
    }
558
559
private:
560
    std::once_flag _init_once;
561
    Status _init_status;
562
563
0
    void _close() {
564
0
        for (auto& inst : per_instance_data) {
565
0
            for (auto& bucket_data : inst.bucket_agg_data) {
566
0
                _close_one_agg_data(*bucket_data);
567
0
            }
568
0
        }
569
0
    }
570
571
0
    void _close_one_agg_data(BucketedAggDataVariants& agg_data) {
572
0
        std::visit(
573
0
                Overload {[&](std::monostate& arg) -> void {
574
                              // Do nothing
575
0
                          },
576
0
                          [&](auto& agg_method) -> void {
577
0
                              if (use_simple_count) {
578
                                  // simple_count: mapped slots hold UInt64 counters,
579
                                  // not real agg state pointers. Skip destroy.
580
0
                                  return;
581
0
                              }
582
0
                              auto& data = *agg_method.hash_table;
583
0
                              data.for_each_mapped([&](auto& mapped) {
584
0
                                  if (mapped) {
585
0
                                      _destroy_agg_status(mapped);
586
0
                                      mapped = nullptr;
587
0
                                  }
588
0
                              });
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS4_ENKUlS4_E_clISC_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt136EPc9HashCRC32IS9_EEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS4_ENKUlS4_E_clISC_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt104EPc9HashCRC32IS9_EEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt96EPc9HashCRC32IS9_EEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt72EPc9HashCRC32IS9_EEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_19MethodStringNoCacheINS_15DataWithNullKeyI9PHHashMapINS_9StringRefEPc11DefaultHashISB_vEEEEEEEEEEvS4_ENKUlS4_E_clISC_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm256EjEENS_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS4_ENKUlS4_E_clISE_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm128EjEENS_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS4_ENKUlS4_E_clISE_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberItNS_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIhNS_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS4_ENKUlS4_E_clISB_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS4_ENKUlS4_E_clISC_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS4_ENKUlS4_E_clISC_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_19MethodStringNoCacheI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS4_ENKUlS4_E_clIS9_EEDaS4_
Unexecuted instantiation: _ZZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS4_ENKUlS4_E_clISA_EEDaS4_
589
                              if constexpr (std::is_assignable_v<decltype(data.has_null_key_data()),
590
0
                                                                 bool>) {
591
0
                                  if (data.has_null_key_data()) {
592
0
                                      _destroy_agg_status(
593
0
                                              data.template get_null_key_data<AggregateDataPtr>());
594
0
                                  }
595
0
                              }
596
0
                          }},
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt136EPc9HashCRC32IS9_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_7UInt104EPc9HashCRC32IS9_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt96EPc9HashCRC32IS9_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapINS_6UInt72EPc9HashCRC32IS9_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_19MethodStringNoCacheINS_15DataWithNullKeyI9PHHashMapINS_9StringRefEPc11DefaultHashISB_vEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm256EjEENS_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIN4wide7integerILm128EjEENS_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberImNS_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIjNS_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberItNS_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_26MethodSingleNullableColumnINS_15MethodOneNumberIhNS_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_19MethodStringNoCacheI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS4_
Unexecuted instantiation: _ZZN5doris22BucketedAggSharedState19_close_one_agg_dataERNS_23BucketedAggDataVariantsEENKUlRT_E_clINS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS4_
597
0
                agg_data.method_variant);
598
0
    }
599
600
    void _destroy_agg_status(AggregateDataPtr data);
601
};
602
603
struct PartitionedAggSharedState : public BasicSharedState,
604
                                   public std::enable_shared_from_this<PartitionedAggSharedState> {
605
    ENABLE_FACTORY_CREATOR(PartitionedAggSharedState)
606
607
40
    PartitionedAggSharedState() = default;
608
40
    ~PartitionedAggSharedState() override = default;
609
610
    void close();
611
612
    AggSharedState* _in_mem_shared_state = nullptr;
613
    std::shared_ptr<BasicSharedState> _in_mem_shared_state_sptr;
614
615
    // partition count is no longer stored in shared state; operators maintain their own
616
    std::atomic<bool> _is_spilled = false;
617
    std::deque<SpillFileSPtr> _spill_partitions;
618
};
619
620
struct SortSharedState : public BasicSharedState {
621
    ENABLE_FACTORY_CREATOR(SortSharedState)
622
public:
623
    std::shared_ptr<Sorter> sorter;
624
};
625
626
struct SpillSortSharedState : public BasicSharedState,
627
                              public std::enable_shared_from_this<SpillSortSharedState> {
628
    ENABLE_FACTORY_CREATOR(SpillSortSharedState)
629
630
19
    SpillSortSharedState() = default;
631
19
    ~SpillSortSharedState() override = default;
632
633
11
    void update_spill_block_batch_row_count(RuntimeState* state, const Block* block) {
634
11
        auto rows = block->rows();
635
11
        if (rows > 0 && 0 == avg_row_bytes) {
636
7
            avg_row_bytes = std::max((std::size_t)1, block->bytes() / rows);
637
7
            spill_block_batch_row_count =
638
7
                    (state->spill_buffer_size_bytes() + avg_row_bytes - 1) / avg_row_bytes;
639
7
            LOG(INFO) << "spill sort block batch row count: " << spill_block_batch_row_count;
640
7
        }
641
11
    }
642
643
    void close();
644
645
    SortSharedState* in_mem_shared_state = nullptr;
646
    bool enable_spill = false;
647
    bool is_spilled = false;
648
    int64_t limit = -1;
649
    int64_t offset = 0;
650
    std::atomic_bool is_closed = false;
651
    std::shared_ptr<BasicSharedState> in_mem_shared_state_sptr;
652
653
    std::deque<SpillFileSPtr> sorted_spill_groups;
654
    size_t avg_row_bytes = 0;
655
    size_t spill_block_batch_row_count;
656
};
657
658
struct UnionSharedState : public BasicSharedState {
659
    ENABLE_FACTORY_CREATOR(UnionSharedState)
660
661
public:
662
1
    UnionSharedState(int child_count = 1) : data_queue(child_count), _child_count(child_count) {};
663
0
    int child_count() const { return _child_count; }
664
    DataQueue data_queue;
665
    const int _child_count;
666
};
667
668
struct DataQueueSharedState : public BasicSharedState {
669
    ENABLE_FACTORY_CREATOR(DataQueueSharedState)
670
public:
671
    DataQueue data_queue;
672
};
673
674
class MultiCastDataStreamer;
675
676
struct MultiCastSharedState : public BasicSharedState,
677
                              public std::enable_shared_from_this<MultiCastSharedState> {
678
    MultiCastSharedState(ObjectPool* pool, int cast_sender_count, int node_id);
679
680
    std::unique_ptr<MultiCastDataStreamer> multi_cast_data_streamer;
681
};
682
683
struct AnalyticSharedState : public BasicSharedState {
684
    ENABLE_FACTORY_CREATOR(AnalyticSharedState)
685
686
public:
687
9
    AnalyticSharedState() = default;
688
    std::queue<Block> blocks_buffer;
689
    std::mutex buffer_mutex;
690
    bool sink_eos = false;
691
    std::mutex sink_eos_lock;
692
    Arena agg_arena_pool;
693
};
694
695
struct JoinSharedState : public BasicSharedState {
696
    // For some join case, we can apply a short circuit strategy
697
    // 1. _has_null_in_build_side = true
698
    // 2. build side rows is empty, Join op is: inner join/right outer join/left semi/right semi/right anti
699
    bool _has_null_in_build_side = false;
700
    bool short_circuit_for_probe = false;
701
    // for some join, when build side rows is empty, we could return directly by add some additional null data in probe table.
702
    bool empty_right_table_need_probe_dispose = false;
703
    JoinOpVariants join_op_variants;
704
};
705
706
struct HashJoinSharedState : public JoinSharedState {
707
    ENABLE_FACTORY_CREATOR(HashJoinSharedState)
708
72.1k
    HashJoinSharedState() {
709
72.1k
        hash_table_variant_vector.push_back(std::make_shared<JoinDataVariants>());
710
72.1k
    }
711
4
    HashJoinSharedState(int num_instances) {
712
4
        source_deps.resize(num_instances, nullptr);
713
4
        hash_table_variant_vector.resize(num_instances, nullptr);
714
20
        for (int i = 0; i < num_instances; i++) {
715
16
            hash_table_variant_vector[i] = std::make_shared<JoinDataVariants>();
716
16
        }
717
4
    }
718
    std::shared_ptr<Arena> arena = std::make_shared<Arena>();
719
720
    const std::vector<TupleDescriptor*> build_side_child_desc;
721
    size_t build_exprs_size = 0;
722
    std::shared_ptr<Block> build_block;
723
    std::shared_ptr<std::vector<uint32_t>> build_indexes_null;
724
725
    // Used by shared hash table
726
    // For probe operator, hash table in _hash_table_variants is read-only if visited flags is not
727
    // used. (visited flags will be used only in right / full outer join).
728
    //
729
    // For broadcast join, although hash table is read-only, some states in `_hash_table_variants`
730
    // are still could be written. For example, serialized keys will be written in a continuous
731
    // memory in `_hash_table_variants`. So before execution, we should use a local _hash_table_variants
732
    // which has a shared hash table in it.
733
    std::vector<std::shared_ptr<JoinDataVariants>> hash_table_variant_vector;
734
735
    // whether left semi join could directly return
736
    // if runtime filters contains local in filter, we can make sure all input rows are matched
737
    // local filter will always be applied, and in filter could guarantee precise filtering
738
    // ATTN: we should disable always_true logic for in filter when we set this flag
739
    bool left_semi_direct_return = false;
740
741
    // ASOF JOIN specific fields
742
    // Whether the inequality is >= or > (true) vs <= or < (false)
743
    bool asof_inequality_is_greater = true;
744
    // Whether the inequality is strict (> or <) vs non-strict (>= or <=)
745
    bool asof_inequality_is_strict = false;
746
747
    // ASOF JOIN pre-sorted index with inline values for O(log K) branchless lookup
748
    // Typed AsofIndexGroups stored in a variant (uint32_t for DateV2, uint64_t for DateTimeV2/TimestampTZ)
749
    AsofIndexVariant asof_index_groups;
750
    // build_row_index -> bucket_id for O(1) reverse lookup
751
    std::vector<uint32_t> asof_build_row_to_bucket;
752
};
753
754
struct PartitionedHashJoinSharedState
755
        : public HashJoinSharedState,
756
          public std::enable_shared_from_this<PartitionedHashJoinSharedState> {
757
    ENABLE_FACTORY_CREATOR(PartitionedHashJoinSharedState)
758
759
    std::unique_ptr<RuntimeState> _inner_runtime_state;
760
    std::shared_ptr<HashJoinSharedState> _inner_shared_state;
761
    std::vector<std::unique_ptr<MutableBlock>> _partitioned_build_blocks;
762
    std::vector<SpillFileSPtr> _spilled_build_groups;
763
    std::atomic<bool> _is_spilled = false;
764
};
765
766
struct NestedLoopJoinSharedState : public JoinSharedState {
767
    ENABLE_FACTORY_CREATOR(NestedLoopJoinSharedState)
768
    // if true, probe child has no more rows to process
769
    bool probe_side_eos = false;
770
    // Visited flags for each row in build side.
771
    MutableColumns build_side_visited_flags;
772
    // List of build blocks, constructed in prepare()
773
    Blocks build_blocks;
774
};
775
776
struct PartitionSortNodeSharedState : public BasicSharedState {
777
    ENABLE_FACTORY_CREATOR(PartitionSortNodeSharedState)
778
public:
779
    std::queue<Block> blocks_buffer;
780
    std::mutex buffer_mutex;
781
    std::vector<std::unique_ptr<PartitionSorter>> partition_sorts;
782
    bool sink_eos = false;
783
    std::mutex sink_eos_lock;
784
    std::mutex prepared_finish_lock;
785
};
786
787
struct SetSharedState : public BasicSharedState {
788
    ENABLE_FACTORY_CREATOR(SetSharedState)
789
public:
790
    /// default init
791
    Block build_block; // build to source
792
    //record element size in hashtable
793
    int64_t valid_element_in_hash_tbl = 0;
794
    //first: idx mapped to column types
795
    //second: column_id, could point to origin column or cast column
796
    std::unordered_map<int, int> build_col_idx;
797
798
    //// shared static states (shared, decided in prepare/open...)
799
800
    /// init in setup_local_state
801
    std::unique_ptr<SetDataVariants> hash_table_variants =
802
            std::make_unique<SetDataVariants>(); // the real data HERE.
803
    std::vector<bool> build_not_ignore_null;
804
805
    // The SET operator's child might have different nullable attributes.
806
    // If a calculation involves both nullable and non-nullable columns, the final output should be a nullable column
807
    Status update_build_not_ignore_null(const VExprContextSPtrs& ctxs);
808
809
    size_t get_hash_table_size() const;
810
    /// init in both upstream side.
811
    //The i-th result expr list refers to the i-th child.
812
    std::vector<VExprContextSPtrs> child_exprs_lists;
813
814
    /// init in build side
815
    size_t child_quantity;
816
    VExprContextSPtrs build_child_exprs;
817
    std::vector<Dependency*> probe_finished_children_dependency;
818
819
    /// init in probe side
820
    std::vector<VExprContextSPtrs> probe_child_exprs_lists;
821
822
    std::atomic<bool> ready_for_read = false;
823
824
    Arena arena;
825
826
    /// called in setup_local_state
827
    Status hash_table_init();
828
};
829
830
enum class ExchangeType : uint8_t {
831
    NOOP = 0,
832
    // Shuffle data by Crc32CHashPartitioner
833
    HASH_SHUFFLE = 1,
834
    // Round-robin passthrough data blocks.
835
    PASSTHROUGH = 2,
836
    // Shuffle data by Crc32HashPartitioner<ShuffleChannelIds> (e.g. same as storage engine).
837
    BUCKET_HASH_SHUFFLE = 3,
838
    // Passthrough data blocks to all channels.
839
    BROADCAST = 4,
840
    // Passthrough data to channels evenly in an adaptive way.
841
    ADAPTIVE_PASSTHROUGH = 5,
842
    // Send all data to the first channel.
843
    PASS_TO_ONE = 6,
844
};
845
846
63
inline std::string get_exchange_type_name(ExchangeType idx) {
847
63
    switch (idx) {
848
14
    case ExchangeType::NOOP:
849
14
        return "NOOP";
850
49
    case ExchangeType::HASH_SHUFFLE:
851
49
        return "HASH_SHUFFLE";
852
0
    case ExchangeType::PASSTHROUGH:
853
0
        return "PASSTHROUGH";
854
0
    case ExchangeType::BUCKET_HASH_SHUFFLE:
855
0
        return "BUCKET_HASH_SHUFFLE";
856
0
    case ExchangeType::BROADCAST:
857
0
        return "BROADCAST";
858
0
    case ExchangeType::ADAPTIVE_PASSTHROUGH:
859
0
        return "ADAPTIVE_PASSTHROUGH";
860
0
    case ExchangeType::PASS_TO_ONE:
861
0
        return "PASS_TO_ONE";
862
63
    }
863
0
    throw Exception(Status::FatalError("__builtin_unreachable"));
864
63
}
865
866
struct DataDistribution {
867
144k
    DataDistribution(ExchangeType type) : distribution_type(type) {}
868
    DataDistribution(ExchangeType type, const std::vector<TExpr>& partition_exprs_)
869
52
            : distribution_type(type), partition_exprs(partition_exprs_) {}
870
0
    DataDistribution(const DataDistribution& other) = default;
871
5
    bool need_local_exchange() const { return distribution_type != ExchangeType::NOOP; }
872
5
    DataDistribution& operator=(const DataDistribution& other) = default;
873
    ExchangeType distribution_type;
874
    std::vector<TExpr> partition_exprs;
875
};
876
877
class ExchangerBase;
878
879
struct LocalExchangeSharedState : public BasicSharedState {
880
public:
881
    ENABLE_FACTORY_CREATOR(LocalExchangeSharedState);
882
    LocalExchangeSharedState(int num_instances);
883
    ~LocalExchangeSharedState() override;
884
    std::unique_ptr<ExchangerBase> exchanger {};
885
    std::vector<RuntimeProfile::Counter*> mem_counters;
886
    std::atomic<int64_t> mem_usage = 0;
887
    std::atomic<size_t> _buffer_mem_limit = config::local_exchange_buffer_mem_limit;
888
    // 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.
889
    std::mutex le_lock;
890
    void sub_running_sink_operators();
891
    void sub_running_source_operators();
892
10
    void _set_always_ready() {
893
40
        for (auto& dep : source_deps) {
894
40
            DCHECK(dep);
895
40
            dep->set_always_ready();
896
40
        }
897
10
        for (auto& dep : sink_deps) {
898
10
            DCHECK(dep);
899
10
            dep->set_always_ready();
900
10
        }
901
10
    }
902
903
0
    Dependency* get_sink_dep_by_channel_id(int channel_id) { return nullptr; }
904
905
129
    void set_ready_to_read(int channel_id) {
906
129
        auto& dep = source_deps[channel_id];
907
129
        DCHECK(dep) << channel_id;
908
129
        dep->set_ready();
909
129
    }
910
911
161
    void add_mem_usage(int channel_id, size_t delta) { mem_counters[channel_id]->update(delta); }
912
913
125
    void sub_mem_usage(int channel_id, size_t delta) {
914
125
        mem_counters[channel_id]->update(-(int64_t)delta);
915
125
    }
916
917
102
    void add_total_mem_usage(size_t delta) {
918
102
        if (cast_set<int64_t>(mem_usage.fetch_add(delta) + delta) > _buffer_mem_limit) {
919
15
            sink_deps.front()->block();
920
15
        }
921
102
    }
922
923
102
    void sub_total_mem_usage(size_t delta) {
924
102
        auto prev_usage = mem_usage.fetch_sub(delta);
925
102
        DCHECK_GE(prev_usage - delta, 0) << "prev_usage: " << prev_usage << " delta: " << delta;
926
102
        if (cast_set<int64_t>(prev_usage - delta) <= _buffer_mem_limit) {
927
92
            sink_deps.front()->set_ready();
928
92
        }
929
102
    }
930
931
0
    void set_low_memory_mode(RuntimeState* state) {
932
0
        _buffer_mem_limit = std::min<int64_t>(config::local_exchange_buffer_mem_limit,
933
0
                                              state->low_memory_mode_buffer_limit());
934
0
    }
935
};
936
937
} // namespace doris