Coverage Report

Created: 2026-07-29 15:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/runtime_state.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/runtime-state.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/PaloInternalService_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/segment_v2.pb.h>
26
#include <stdint.h>
27
28
#include <atomic>
29
#include <cstdint>
30
#include <fstream>
31
#include <functional>
32
#include <memory>
33
#include <mutex>
34
#include <shared_mutex>
35
#include <string>
36
#include <vector>
37
38
#include "agent/be_exec_version_manager.h"
39
#include "cctz/time_zone.h"
40
#include "common/be_mock_util.h"
41
#include "common/compiler_util.h" // IWYU pragma: keep
42
#include "common/config.h"
43
#include "common/factory_creator.h"
44
#include "common/status.h"
45
#include "exec/scan/vector_search_user_params.h"
46
#include "io/fs/s3_file_system.h"
47
#include "runtime/runtime_profile.h"
48
#include "runtime/task_execution_context.h"
49
#include "runtime/workload_group/workload_group.h"
50
#include "util/debug_util.h"
51
#include "util/timezone_utils.h"
52
53
namespace doris {
54
class RuntimeFilter;
55
56
3.89M
inline int32_t get_execution_rpc_timeout_ms(int32_t execution_timeout_sec) {
57
3.89M
    return std::min(config::execution_max_rpc_timeout_sec, execution_timeout_sec) * 1000;
58
3.89M
}
59
60
class PipelineXLocalStateBase;
61
class PipelineXSinkLocalStateBase;
62
class PipelineFragmentContext;
63
class PipelineTask;
64
class Dependency;
65
66
class DescriptorTbl;
67
class ObjectPool;
68
class ExecEnv;
69
class IdFileMap;
70
class RuntimeFilterMgr;
71
class MemTrackerLimiter;
72
class QueryContext;
73
class RuntimeFilterConsumer;
74
class RuntimeFilterProducer;
75
class TaskExecutionContext;
76
77
// A collection of items that are part of the global state of a
78
// query and shared across all execution nodes of that query.
79
class RuntimeState {
80
    ENABLE_FACTORY_CREATOR(RuntimeState);
81
82
public:
83
    RuntimeState(const TPlanFragmentExecParams& fragment_exec_params,
84
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
85
                 ExecEnv* exec_env, QueryContext* ctx,
86
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker = nullptr);
87
88
    RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id, int32_t fragment_id,
89
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
90
                 ExecEnv* exec_env, QueryContext* ctx);
91
92
    // Used by pipeline. This runtime state is only used for setup.
93
    RuntimeState(const TUniqueId& query_id, int32_t fragment_id, const TQueryOptions& query_options,
94
                 const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx);
95
96
    // Only used in the materialization phase of delayed materialization,
97
    // when there may be no corresponding QueryContext.
98
    RuntimeState(const TUniqueId& query_id, int32_t fragment_id, const TQueryOptions& query_options,
99
                 const TQueryGlobals& query_globals, ExecEnv* exec_env,
100
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker);
101
102
    // RuntimeState for executing expr in fe-support.
103
    RuntimeState(const TQueryOptions& query_options, const TQueryGlobals& query_globals);
104
105
    // for job task only
106
    RuntimeState();
107
108
    // Empty d'tor to avoid issues with unique_ptr.
109
    MOCK_DEFINE(virtual) ~RuntimeState();
110
111
    // Set per-query state.
112
    Status init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
113
                const TQueryGlobals& query_globals, ExecEnv* exec_env);
114
115
    // for ut and non-query.
116
    void set_exec_env(ExecEnv* exec_env) { _exec_env = exec_env; }
117
118
    // for ut and non-query.
119
    void init_mem_trackers(const std::string& name = "ut", const TUniqueId& id = TUniqueId());
120
121
56.2M
    const TQueryOptions& query_options() const { return _query_options; }
122
316k
    int64_t scan_queue_mem_limit() const {
123
316k
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
124
316k
                                                           : _query_options.mem_limit / 20;
125
316k
    }
126
127
921k
    bool enable_adjust_conjunct_order_by_cost() const {
128
921k
        return _query_options.__isset.enable_adjust_conjunct_order_by_cost &&
129
921k
               _query_options.enable_adjust_conjunct_order_by_cost;
130
921k
    }
131
132
316k
    int32_t max_column_reader_num() const {
133
316k
        return _query_options.__isset.max_column_reader_num ? _query_options.max_column_reader_num
134
316k
                                                            : 20000;
135
316k
    }
136
137
7.77M
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
138
139
7.41M
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
140
2.82M
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
141
142
    // Row-count limit for output blocks. Clamp to [1, 65535].
143
    // Adaptive byte budgeting still uses this as the hard row ceiling.
144
18.4M
    MOCK_FUNCTION int batch_size() const {
145
18.4M
        static constexpr int kMax = 65535;
146
18.4M
        auto v = _query_options.batch_size;
147
18.4M
        return std::min(std::max(1, v), kMax);
148
18.4M
    }
149
150
    // Target byte budget per output block (default 8MB when adaptive is enabled).
151
    // The public FE/session contract is [1MB, 512MB]; this accessor still clamps any direct
152
    // thrift or mixed-version out-of-range value into that range. Returns `kMax` when adaptive
153
    // is disabled by BE config so the value is always a legal byte budget; callers that need
154
    // to know whether adaptive batch size is active should test
155
    // `config::enable_adaptive_batch_size` explicitly.
156
5.38M
    MOCK_FUNCTION size_t preferred_block_size_bytes() const {
157
5.38M
        static constexpr int64_t kDefault = 8388608L; // 8MB
158
5.38M
        static constexpr int64_t kMax = 536870912L;   // 512MB
159
5.38M
        static constexpr int64_t kMin = 1048576L;     // 1MB
160
5.38M
        if (!config::enable_adaptive_batch_size) [[unlikely]] {
161
7.78k
            return kMax;
162
7.78k
        }
163
5.37M
        if (_query_options.__isset.preferred_block_size_bytes) [[likely]] {
164
5.37M
            return std::max<int64_t>(
165
5.37M
                    kMin, std::min<int64_t>(_query_options.preferred_block_size_bytes, kMax));
166
5.37M
        }
167
18.4E
        return kDefault;
168
5.37M
    }
169
170
1.40M
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
171
0
    int max_errors() const { return _query_options.max_errors; }
172
4.31M
    int execution_timeout() const {
173
4.31M
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
174
18.4E
                                                        : _query_options.query_timeout;
175
4.31M
    }
176
542k
    int max_scanners_concurrency() const {
177
542k
        return _query_options.__isset.max_scanners_concurrency
178
544k
                       ? _query_options.max_scanners_concurrency
179
18.4E
                       : 0;
180
542k
    }
181
131k
    int max_file_scanners_concurrency() const {
182
131k
        return _query_options.__isset.max_file_scanners_concurrency
183
131k
                       ? _query_options.max_file_scanners_concurrency
184
18.4E
                       : max_scanners_concurrency();
185
131k
    }
186
187
545k
    int min_scanners_concurrency() const {
188
545k
        return _query_options.__isset.min_scanners_concurrency
189
545k
                       ? _query_options.min_scanners_concurrency
190
18.4E
                       : 1;
191
545k
    }
192
193
65.2k
    int min_file_scanners_concurrency() const {
194
65.2k
        return _query_options.__isset.min_file_scanners_concurrency
195
65.2k
                       ? _query_options.min_file_scanners_concurrency
196
18.4E
                       : 1;
197
65.2k
    }
198
199
    // Support extended regex
200
    // like look-around zero-width assertions(`?=`, `?!`, `?<=`, `?<!`)
201
307
    bool enable_extended_regex() const {
202
307
        return _query_options.__isset.enable_extended_regex && _query_options.enable_extended_regex;
203
307
    }
204
205
125k
    TQueryType::type query_type() const { return _query_options.query_type; }
206
57.0k
    int64_t timestamp_ms() const { return _timestamp_ms; }
207
56.9k
    int32_t nano_seconds() const { return _nano_seconds; }
208
    // if possible, use timezone_obj() rather than timezone()
209
407k
    const std::string& timezone() const { return _timezone; }
210
822k
    const cctz::time_zone& timezone_obj() const { return _timezone_obj; }
211
267
    void set_timezone(const std::string& timezone) {
212
267
        _timezone = timezone;
213
267
        TimezoneUtils::find_cctz_time_zone(_timezone, _timezone_obj);
214
267
    }
215
160
    const std::string& lc_time_names() const { return _lc_time_names; }
216
18.7M
    const TUniqueId& query_id() const { return _query_id; }
217
2.48M
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
218
    // should only be called in pipeline engine
219
665
    int32_t fragment_id() const { return _fragment_id; }
220
5.22M
    ExecEnv* exec_env() { return _exec_env; }
221
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const;
222
223
    // Returns runtime state profile
224
759
    RuntimeProfile* runtime_profile() { return &_profile; }
225
63.7k
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
226
227
529k
    bool enable_function_pushdown() const {
228
529k
        return _query_options.__isset.enable_function_pushdown &&
229
529k
               _query_options.enable_function_pushdown;
230
529k
    }
231
232
1.03M
    bool check_overflow_for_decimal() const {
233
1.03M
        return _query_options.__isset.check_overflow_for_decimal &&
234
1.03M
               _query_options.check_overflow_for_decimal;
235
1.03M
    }
236
237
1.24M
    bool enable_strict_mode() const {
238
1.24M
        return _query_options.__isset.enable_strict_cast && _query_options.enable_strict_cast;
239
1.24M
    }
240
241
130
    bool enable_insert_strict() const {
242
130
        return _query_options.__isset.enable_insert_strict && _query_options.enable_insert_strict;
243
130
    }
244
245
1.26M
    bool enable_segment_limit_pushdown() const {
246
1.26M
        return !_query_options.__isset.enable_segment_limit_pushdown ||
247
1.26M
               _query_options.enable_segment_limit_pushdown;
248
1.26M
    }
249
250
478k
    bool mysql_row_binary_format() const {
251
478k
        return _query_options.__isset.mysql_row_binary_format &&
252
479k
               _query_options.mysql_row_binary_format;
253
478k
    }
254
255
10
    bool enable_short_circuit_query_access_column_store() const {
256
10
        return _query_options.__isset.enable_short_circuit_query_access_column_store &&
257
10
               _query_options.enable_short_circuit_query_access_column_store;
258
10
    }
259
260
    // Appends error to the _error_log if there is space
261
    bool log_error(const std::string& error);
262
263
    // Returns true if the error log has not reached _max_errors.
264
0
    bool log_has_space() {
265
0
        std::lock_guard<std::mutex> l(_error_log_lock);
266
0
        return _error_log.size() < _query_options.max_errors;
267
0
    }
268
269
    // Append all _error_log[_unreported_error_idx+] to new_errors and set
270
    // _unreported_error_idx to _errors_log.size()
271
    void get_unreported_errors(std::vector<std::string>* new_errors);
272
273
    [[nodiscard]] MOCK_FUNCTION bool is_cancelled() const;
274
    MOCK_FUNCTION Status cancel_reason() const;
275
20
    void cancel(const Status& reason) {
276
20
        if (_exec_status.update(reason)) {
277
            // Create a error status, so that we could print error stack, and
278
            // we could know which path call cancel.
279
20
            LOG(WARNING) << "Task is cancelled, instance: "
280
20
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
281
20
                         << ", st = " << reason;
282
20
        } else {
283
0
            LOG(WARNING) << "Task is already cancelled, instance: "
284
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
285
0
                         << ", original cancel msg: " << _exec_status.status()
286
0
                         << ", new cancel msg: " << reason;
287
0
        }
288
20
    }
289
290
2.54M
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
291
9.48k
    int64_t backend_id() const { return _backend_id; }
292
293
2.08M
    void set_be_number(int be_number) { _be_number = be_number; }
294
6.20M
    int be_number(void) const { return _be_number; }
295
296
5.63k
    std::vector<std::string>& output_files() { return _output_files; }
297
298
1.76k
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
299
300
223k
    const std::vector<std::string>& export_output_files() const { return _export_output_files; }
301
302
0
    void add_export_output_file(const std::string& file) { _export_output_files.push_back(file); }
303
304
533
    void set_db_name(const std::string& db_name) { _db_name = db_name; }
305
306
0
    const std::string& db_name() { return _db_name; }
307
308
1.24k
    void set_wal_id(int64_t wal_id) { _wal_id = wal_id; }
309
310
171k
    int64_t wal_id() const { return _wal_id; }
311
312
429
    void set_content_length(size_t content_length) { _content_length = content_length; }
313
314
4
    size_t content_length() const { return _content_length; }
315
316
579
    const std::string& import_label() { return _import_label; }
317
318
0
    const std::string& load_dir() const { return _load_dir; }
319
320
0
    void set_load_job_id(int64_t job_id) { _load_job_id = job_id; }
321
322
56.5k
    int64_t load_job_id() const { return _load_job_id; }
323
324
    std::string get_error_log_file_path();
325
326
405k
    std::string get_first_error_msg() const { return _first_error_msg; }
327
328
    // append error msg and error line to file when loading data.
329
    // is_summary is true, means we are going to write the summary line
330
    // If we need to stop the processing, set stop_processing to true
331
    Status append_error_msg_to_file(std::function<std::string()> line,
332
                                    std::function<std::string()> error_msg);
333
334
90.5k
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
335
336
227k
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
337
338
359k
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
339
340
337k
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
341
342
148k
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
343
344
94.9k
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
345
94.9k
        return _num_rows_filtered_in_strict_mode_partial_update;
346
94.9k
    }
347
348
44.2k
    int64_t num_rows_load_success() {
349
44.2k
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
350
44.2k
    }
351
352
46.4k
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
353
354
59.3k
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
355
356
46.3k
    void update_num_bytes_load_total(int64_t bytes_load) {
357
46.3k
        _num_bytes_load_total.fetch_add(bytes_load);
358
46.3k
    }
359
360
134k
    void update_num_finished_scan_range(int64_t finished_range) {
361
134k
        _num_finished_scan_range.fetch_add(finished_range);
362
134k
    }
363
364
64.9k
    void update_num_rows_load_filtered(int64_t num_rows) {
365
64.9k
        _num_rows_load_filtered.fetch_add(num_rows);
366
64.9k
    }
367
368
64.4k
    void update_num_rows_load_unselected(int64_t num_rows) {
369
64.4k
        _num_rows_load_unselected.fetch_add(num_rows);
370
64.4k
    }
371
372
78
    void set_num_rows_filtered_in_strict_mode_partial_update(int64_t num_rows) {
373
78
        _num_rows_filtered_in_strict_mode_partial_update = num_rows;
374
78
    }
375
376
2.08M
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
377
378
280k
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
379
380
2.55M
    void set_num_per_fragment_instances(int num_instances) {
381
2.55M
        _num_per_fragment_instances = num_instances;
382
2.55M
    }
383
384
56.6k
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
385
386
2.55M
    void set_load_stream_per_node(int load_stream_per_node) {
387
2.55M
        _load_stream_per_node = load_stream_per_node;
388
2.55M
    }
389
390
9.49k
    int load_stream_per_node() const { return _load_stream_per_node; }
391
392
2.55M
    void set_total_load_streams(int total_load_streams) {
393
2.55M
        _total_load_streams = total_load_streams;
394
2.55M
    }
395
396
9.49k
    int total_load_streams() const { return _total_load_streams; }
397
398
2.55M
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
399
400
9.49k
    int num_local_sink() const { return _num_local_sink; }
401
402
0
    bool disable_stream_preaggregations() const {
403
0
        return _query_options.disable_stream_preaggregations;
404
0
    }
405
406
0
    int32_t runtime_filter_wait_time_ms() const {
407
0
        return _query_options.runtime_filter_wait_time_ms;
408
0
    }
409
410
0
    int32_t runtime_filter_max_in_num() const { return _query_options.runtime_filter_max_in_num; }
411
412
31.8M
    int be_exec_version() const {
413
31.8M
        DCHECK(_query_options.__isset.be_exec_version &&
414
31.8M
               BeExecVersionManager::check_be_exec_version(_query_options.be_exec_version));
415
31.8M
        return _query_options.be_exec_version;
416
31.8M
    }
417
469k
    bool plan_local_shuffle() const {
418
        // If local shuffle is enabled and not planned by local shuffle planner, we should plan local shuffle in BE.
419
469k
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle &&
420
469k
               (!_query_options.__isset.enable_local_shuffle_planner ||
421
463k
                !_query_options.enable_local_shuffle_planner);
422
469k
    }
423
424
4.10M
    MOCK_FUNCTION bool enable_local_exchange() const {
425
4.10M
        return _query_options.__isset.enable_local_exchange && _query_options.enable_local_exchange;
426
4.10M
    }
427
428
3.29k
    bool trim_tailing_spaces_for_external_table_query() const {
429
3.29k
        return _query_options.trim_tailing_spaces_for_external_table_query;
430
3.29k
    }
431
432
477k
    bool return_object_data_as_binary() const {
433
477k
        return _query_options.return_object_data_as_binary;
434
477k
    }
435
436
7.65M
    bool enable_fuzzy_blockable_task() const {
437
7.65M
        return _query_options.__isset.enable_fuzzy_blockable_task &&
438
7.65M
               _query_options.enable_fuzzy_blockable_task;
439
7.65M
    }
440
441
470k
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
442
470k
        if (_query_options.__isset.fragment_transmission_compression_codec) {
443
465k
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
444
117k
                return segment_v2::CompressionTypePB::LZ4;
445
348k
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
446
113k
                return segment_v2::CompressionTypePB::SNAPPY;
447
234k
            } else {
448
234k
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
449
234k
            }
450
465k
        }
451
5.56k
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
452
470k
    }
453
454
2.53M
    bool skip_storage_engine_merge() const {
455
2.53M
        return _query_options.__isset.skip_storage_engine_merge &&
456
2.53M
               _query_options.skip_storage_engine_merge;
457
2.53M
    }
458
459
1.47M
    bool skip_delete_predicate() const {
460
1.48M
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
461
1.47M
    }
462
463
1.26M
    bool skip_delete_bitmap() const {
464
1.27M
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
465
1.26M
    }
466
467
1.48M
    bool skip_missing_version() const {
468
1.48M
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
469
1.48M
    }
470
471
7.17k
    int64_t data_queue_max_blocks() const {
472
7.18k
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
473
18.4E
                                                            : 1;
474
7.17k
    }
475
476
    bool enable_page_cache() const;
477
478
1.01M
    bool enable_prefer_cached_rowset() const {
479
1.01M
        return _query_options.__isset.enable_prefer_cached_rowset &&
480
1.01M
               _query_options.enable_prefer_cached_rowset;
481
1.01M
    }
482
483
1.01M
    int64_t query_freshness_tolerance_ms() const {
484
1.01M
        return _query_options.query_freshness_tolerance_ms;
485
1.01M
    }
486
487
0
    bool enable_query_freshness_tolerance() const {
488
0
        return _query_options.__isset.query_freshness_tolerance_ms &&
489
0
               _query_options.query_freshness_tolerance_ms > 0;
490
0
    }
491
492
228k
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
493
228k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
494
228k
        return _tablet_commit_infos;
495
228k
    }
496
497
51.7k
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
498
51.7k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
499
51.7k
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
500
51.7k
                                    std::make_move_iterator(commit_infos.begin()),
501
51.7k
                                    std::make_move_iterator(commit_infos.end()));
502
51.7k
    }
503
504
223k
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
505
223k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
506
223k
        return _error_tablet_infos;
507
223k
    }
508
509
48.6k
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
510
48.6k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
511
48.6k
        _error_tablet_infos.insert(_error_tablet_infos.end(),
512
48.6k
                                   std::make_move_iterator(tablet_infos.begin()),
513
48.6k
                                   std::make_move_iterator(tablet_infos.end()));
514
48.6k
    }
515
516
223k
    std::vector<THivePartitionUpdate> hive_partition_updates() const {
517
223k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
518
223k
        return _hive_partition_updates;
519
223k
    }
520
521
1.95k
    void add_hive_partition_updates(const THivePartitionUpdate& hive_partition_update) {
522
1.95k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
523
1.95k
        _hive_partition_updates.emplace_back(hive_partition_update);
524
1.95k
    }
525
526
223k
    std::vector<TIcebergCommitData> iceberg_commit_datas() const {
527
223k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
528
223k
        return _iceberg_commit_datas;
529
223k
    }
530
531
4.05k
    void add_iceberg_commit_datas(const TIcebergCommitData& iceberg_commit_data) {
532
4.05k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
533
4.05k
        _iceberg_commit_datas.emplace_back(iceberg_commit_data);
534
4.05k
    }
535
536
223k
    std::vector<TMCCommitData> mc_commit_datas() const {
537
223k
        std::lock_guard<std::mutex> lock(_mc_commit_datas_mutex);
538
223k
        return _mc_commit_datas;
539
223k
    }
540
541
0
    void add_mc_commit_datas(const TMCCommitData& mc_commit_data) {
542
0
        std::lock_guard<std::mutex> lock(_mc_commit_datas_mutex);
543
0
        _mc_commit_datas.emplace_back(mc_commit_data);
544
0
    }
545
546
    // local runtime filter mgr, the runtime filter do not have remote target or
547
    // not need local merge should regist here. the instance exec finish, the local
548
    // runtime filter mgr can release the memory of local runtime filter
549
99.7k
    RuntimeFilterMgr* local_runtime_filter_mgr() { return _runtime_filter_mgr; }
550
551
    RuntimeFilterMgr* global_runtime_filter_mgr();
552
553
2.08M
    void set_runtime_filter_mgr(RuntimeFilterMgr* runtime_filter_mgr) {
554
2.08M
        _runtime_filter_mgr = runtime_filter_mgr;
555
2.08M
    }
556
557
85.8M
    QueryContext* get_query_ctx() const { return _query_ctx; }
558
559
    [[nodiscard]] bool low_memory_mode() const;
560
561
    std::weak_ptr<QueryContext> get_query_ctx_weak();
562
    MOCK_FUNCTION WorkloadGroupPtr workload_group();
563
564
2.08M
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
565
2.08M
        _query_mem_tracker = tracker;
566
2.08M
    }
567
568
4.37k
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
569
570
3.63M
    bool enable_profile() const {
571
3.63M
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
572
3.63M
    }
573
574
164
    int cte_max_recursion_depth() const {
575
164
        return _query_options.__isset.cte_max_recursion_depth
576
164
                       ? _query_options.cte_max_recursion_depth
577
164
                       : 0;
578
164
    }
579
580
51
    bool enable_streaming_agg_hash_join_force_passthrough() const {
581
51
        return _query_options.__isset.enable_streaming_agg_hash_join_force_passthrough &&
582
51
               _query_options.enable_streaming_agg_hash_join_force_passthrough;
583
51
    }
584
585
41.5k
    bool enable_local_exchange_before_agg() const {
586
41.5k
        return _query_options.__isset.enable_local_exchange_before_agg &&
587
41.5k
               _query_options.enable_local_exchange_before_agg;
588
41.5k
    }
589
590
2.25k
    bool enable_local_exchange_before_streaming_agg() const {
591
2.25k
        return _query_options.__isset.enable_local_exchange_before_streaming_agg &&
592
2.25k
               _query_options.enable_local_exchange_before_streaming_agg;
593
2.25k
    }
594
595
10.2k
    bool enable_distinct_streaming_agg_force_passthrough() const {
596
10.2k
        return _query_options.__isset.enable_distinct_streaming_agg_force_passthrough &&
597
10.2k
               _query_options.enable_distinct_streaming_agg_force_passthrough;
598
10.2k
    }
599
600
9.59k
    bool enable_broadcast_join_force_passthrough() const {
601
9.59k
        return _query_options.__isset.enable_broadcast_join_force_passthrough &&
602
9.59k
               _query_options.enable_broadcast_join_force_passthrough;
603
9.59k
    }
604
605
301k
    int rpc_verbose_profile_max_instance_count() const {
606
301k
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
607
301k
                       ? _query_options.rpc_verbose_profile_max_instance_count
608
301k
                       : 0;
609
301k
    }
610
611
17.9k
    MOCK_FUNCTION bool enable_share_hash_table_for_broadcast_join() const {
612
17.9k
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
613
17.9k
               _query_options.enable_share_hash_table_for_broadcast_join;
614
17.9k
    }
615
616
304k
    bool enable_parallel_scan() const {
617
304k
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
618
304k
    }
619
620
131k
    bool enable_aggregate_function_null_v2() const {
621
131k
        return _query_options.__isset.enable_aggregate_function_null_v2 &&
622
131k
               _query_options.enable_aggregate_function_null_v2;
623
131k
    }
624
625
2.00M
    bool enable_prune_nested_column() const {
626
2.00M
        return _query_options.__isset.enable_prune_nested_column &&
627
2.01M
               _query_options.enable_prune_nested_column;
628
2.00M
    }
629
630
54
    bool is_read_csv_empty_line_as_null() const {
631
54
        return _query_options.__isset.read_csv_empty_line_as_null &&
632
54
               _query_options.read_csv_empty_line_as_null;
633
54
    }
634
635
187k
    int parallel_scan_max_scanners_count() const {
636
187k
        return _query_options.__isset.parallel_scan_max_scanners_count
637
187k
                       ? _query_options.parallel_scan_max_scanners_count
638
18.4E
                       : 0;
639
187k
    }
640
641
5.54k
    int partition_topn_max_partitions() const {
642
5.54k
        return _query_options.__isset.partition_topn_max_partitions
643
5.54k
                       ? _query_options.partition_topn_max_partitions
644
5.54k
                       : 1024;
645
5.54k
    }
646
647
0
    int partition_topn_per_partition_rows() const {
648
0
        return _query_options.__isset.partition_topn_pre_partition_rows
649
0
                       ? _query_options.partition_topn_pre_partition_rows
650
0
                       : 1000;
651
0
    }
652
653
187k
    int64_t parallel_scan_min_rows_per_scanner() const {
654
187k
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
655
187k
                       ? _query_options.parallel_scan_min_rows_per_scanner
656
18.4E
                       : 0;
657
187k
    }
658
659
3.30k
    void set_be_exec_version(int32_t version) noexcept { _query_options.be_exec_version = version; }
660
661
    using LocalState = doris::PipelineXLocalStateBase;
662
    using SinkLocalState = doris::PipelineXSinkLocalStateBase;
663
    // get result can return an error message, and we will only call it during the prepare.
664
    void emplace_local_state(int id, std::unique_ptr<LocalState> state);
665
666
    LocalState* get_local_state(int id);
667
    Result<LocalState*> get_local_state_result(int id);
668
669
    void emplace_sink_local_state(int id, std::unique_ptr<SinkLocalState> state);
670
671
    SinkLocalState* get_sink_local_state();
672
673
    Result<SinkLocalState*> get_sink_local_state_result();
674
675
    void resize_op_id_to_local_state(int operator_size);
676
677
    std::vector<std::shared_ptr<RuntimeProfile>> pipeline_id_to_profile();
678
679
    std::vector<std::shared_ptr<RuntimeProfile>> build_pipeline_profile(std::size_t pipeline_size);
680
681
2.62M
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
682
2.62M
        _task_execution_context_inited = true;
683
2.62M
        _task_execution_context = context;
684
2.62M
    }
685
686
64.8k
    bool task_execution_context_inited() const { return _task_execution_context_inited; }
687
688
2.27M
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
689
18.4E
        CHECK(_task_execution_context_inited)
690
18.4E
                << "_task_execution_context_inited == false, the ctx is not inited";
691
2.27M
        return _task_execution_context;
692
2.27M
    }
693
694
    Status register_producer_runtime_filter(
695
            const doris::TRuntimeFilterDesc& desc,
696
            std::shared_ptr<RuntimeFilterProducer>* producer_filter);
697
698
    Status register_consumer_runtime_filter(
699
            const doris::TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
700
            std::shared_ptr<RuntimeFilterConsumer>* consumer_filter);
701
702
    bool is_nereids() const;
703
704
7.19M
    bool enable_spill() const {
705
7.19M
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
706
7.19M
               (_query_options.__isset.enable_spill && _query_options.enable_spill);
707
7.19M
    }
708
709
13.7M
    bool enable_force_spill() const {
710
13.7M
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
711
13.7M
    }
712
713
17.4k
    int64_t spill_min_revocable_mem() const {
714
17.4k
        if (_query_options.__isset.min_revocable_mem) {
715
17.4k
            return std::max(_query_options.min_revocable_mem, (int64_t)1 << 20);
716
17.4k
        }
717
0
        return 32 << 20;
718
17.4k
    }
719
720
215
    int spill_aggregation_partition_count() const {
721
215
        if (_query_options.__isset.spill_aggregation_partition_count) {
722
215
            return std::min(std::max(2, _query_options.spill_aggregation_partition_count), 32);
723
215
        }
724
0
        return 8;
725
215
    }
726
727
9
    int spill_hash_join_partition_count() const {
728
9
        if (_query_options.__isset.spill_hash_join_partition_count) {
729
9
            return std::min(std::max(2, _query_options.spill_hash_join_partition_count), 32);
730
9
        }
731
0
        return 8;
732
9
    }
733
734
111
    int spill_repartition_max_depth() const {
735
111
        if (_query_options.__isset.spill_repartition_max_depth) {
736
            // Clamp to a reasonable range: [1, 128]
737
111
            return std::max(1, std::min(_query_options.spill_repartition_max_depth, 128));
738
111
        }
739
0
        return 8;
740
111
    }
741
742
114
    int64_t spill_buffer_size_bytes() const {
743
        // clamp to [1MB, 256MB]
744
114
        constexpr int64_t kMin = 1LL * 1024 * 1024;
745
114
        constexpr int64_t kMax = 256LL * 1024 * 1024;
746
114
        if (_query_options.__isset.spill_buffer_size_bytes) {
747
114
            int64_t v = _query_options.spill_buffer_size_bytes;
748
114
            if (v < kMin) return kMin;
749
114
            if (v > kMax) return kMax;
750
114
            return v;
751
114
        }
752
0
        return 8LL * 1024 * 1024;
753
114
    }
754
755
    // Per-sink memory limits: after spill is triggered, the sink proactively
756
    // spills when its revocable memory exceeds this threshold.
757
    // Clamped to [1MB, 4GB], default 64MB.
758
2
    int64_t spill_join_build_sink_mem_limit_bytes() const {
759
2
        constexpr int64_t kMin = 1LL * 1024 * 1024;
760
2
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
761
2
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
762
2
        if (_query_options.__isset.spill_join_build_sink_mem_limit_bytes) {
763
2
            int64_t v = _query_options.spill_join_build_sink_mem_limit_bytes;
764
2
            return std::min(std::max(v, kMin), kMax);
765
2
        }
766
0
        return kDefault;
767
2
    }
768
769
16
    int64_t spill_aggregation_sink_mem_limit_bytes() const {
770
16
        constexpr int64_t kMin = 1LL * 1024 * 1024;
771
16
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
772
16
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
773
16
        if (_query_options.__isset.spill_aggregation_sink_mem_limit_bytes) {
774
16
            int64_t v = _query_options.spill_aggregation_sink_mem_limit_bytes;
775
16
            return std::min(std::max(v, kMin), kMax);
776
16
        }
777
0
        return kDefault;
778
16
    }
779
780
8
    int64_t spill_sort_sink_mem_limit_bytes() const {
781
8
        constexpr int64_t kMin = 1LL * 1024 * 1024;
782
8
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
783
8
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
784
8
        if (_query_options.__isset.spill_sort_sink_mem_limit_bytes) {
785
8
            int64_t v = _query_options.spill_sort_sink_mem_limit_bytes;
786
8
            return std::min(std::max(v, kMin), kMax);
787
8
        }
788
0
        return kDefault;
789
8
    }
790
791
10
    int64_t spill_sort_merge_mem_limit_bytes() const {
792
10
        constexpr int64_t kMin = 1LL * 1024 * 1024;
793
10
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
794
10
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
795
10
        if (_query_options.__isset.spill_sort_merge_mem_limit_bytes) {
796
10
            int64_t v = _query_options.spill_sort_merge_mem_limit_bytes;
797
10
            return std::min(std::max(v, kMin), kMax);
798
10
        }
799
0
        return kDefault;
800
10
    }
801
802
0
    int64_t low_memory_mode_buffer_limit() const {
803
0
        if (_query_options.__isset.low_memory_mode_buffer_limit) {
804
0
            return std::max(_query_options.low_memory_mode_buffer_limit, (int64_t)1);
805
0
        }
806
0
        return 32L * 1024 * 1024;
807
0
    }
808
809
0
    int spill_revocable_memory_high_watermark_percent() const {
810
0
        if (_query_options.__isset.revocable_memory_high_watermark_percent) {
811
0
            return _query_options.revocable_memory_high_watermark_percent;
812
0
        }
813
0
        return -1;
814
0
    }
815
816
222k
    MOCK_FUNCTION bool enable_shared_exchange_sink_buffer() const {
817
222k
        return _query_options.__isset.enable_shared_exchange_sink_buffer &&
818
222k
               _query_options.enable_shared_exchange_sink_buffer;
819
222k
    }
820
821
11.5M
    size_t minimum_operator_memory_required_bytes() const {
822
11.5M
        if (_query_options.__isset.minimum_operator_memory_required_kb) {
823
11.5M
            return _query_options.minimum_operator_memory_required_kb * 1024;
824
18.4E
        } else {
825
            // refer other database
826
18.4E
            return 4 * 1024 * 1024;
827
18.4E
        }
828
11.5M
    }
829
830
257k
    MOCK_FUNCTION bool enable_use_hybrid_sort() const {
831
257k
        return _query_options.__isset.enable_use_hybrid_sort &&
832
257k
               _query_options.enable_use_hybrid_sort;
833
257k
    }
834
835
2.15M
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
836
837
123
    int max_operator_id() const { return _max_operator_id; }
838
839
2.08M
    void set_task_id(int id) { _task_id = id; }
840
841
318
    int task_id() const { return _task_id; }
842
843
2.08M
    void set_task_num(int task_num) { _task_num = task_num; }
844
845
8.45k
    int task_num() const { return _task_num; }
846
847
334k
    int profile_level() const { return _profile_level; }
848
849
63.4k
    std::shared_ptr<IdFileMap>& get_id_file_map() { return _id_file_map; }
850
851
    void set_id_file_map();
852
1.27M
    VectorSearchUserParams get_vector_search_params() const {
853
1.27M
        VectorSearchUserParams params;
854
1.27M
        params.hnsw_ef_search = _query_options.hnsw_ef_search;
855
1.27M
        params.hnsw_check_relative_distance = _query_options.hnsw_check_relative_distance;
856
1.27M
        params.hnsw_bounded_queue = _query_options.hnsw_bounded_queue;
857
1.27M
        params.ivf_nprobe = _query_options.ivf_nprobe;
858
1.27M
        params.ann_index_candidate_rows_threshold =
859
1.27M
                _query_options.__isset.ann_index_candidate_rows_threshold
860
1.27M
                        ? _query_options.ann_index_candidate_rows_threshold
861
18.4E
                        : 0;
862
1.27M
        params.ann_index_candidate_rows_percent_threshold =
863
1.27M
                _query_options.__isset.ann_index_candidate_rows_percent_threshold
864
1.27M
                        ? _query_options.ann_index_candidate_rows_percent_threshold
865
18.4E
                        : 0.3;
866
1.27M
        return params;
867
1.27M
    }
868
869
119
    bool runtime_filter_wait_infinitely() const {
870
119
        return _query_options.__isset.runtime_filter_wait_infinitely &&
871
119
               _query_options.runtime_filter_wait_infinitely;
872
119
    }
873
874
    const std::set<int>& get_deregister_runtime_filter() const;
875
876
    void merge_register_runtime_filter(const std::set<int>& runtime_filter_ids);
877
878
private:
879
    Status create_error_log_file();
880
881
    static const int DEFAULT_BATCH_SIZE = 4062;
882
883
    std::shared_ptr<MemTrackerLimiter> _query_mem_tracker = nullptr;
884
885
    // Could not find a better way to record if the weak ptr is inited, use a bool to record
886
    // it. In some unit test cases, the runtime state's task ctx is not inited, then the test
887
    // hang, it is very hard to debug.
888
    bool _task_execution_context_inited = false;
889
    // Hold execution context for other threads
890
    std::weak_ptr<TaskExecutionContext> _task_execution_context;
891
892
    // put runtime state before _obj_pool, so that it will be deconstructed after
893
    // _obj_pool. Because some of object in _obj_pool will use profile when deconstructing.
894
    RuntimeProfile _profile;
895
    RuntimeProfile _load_channel_profile;
896
    // Why 2?
897
    // During cluster upgrade, fe will not pass profile_level to be, so we need to set it to 2
898
    // to make sure user can see all profile counters like before.
899
    int _profile_level = 2;
900
901
    const DescriptorTbl* _desc_tbl = nullptr;
902
    std::shared_ptr<ObjectPool> _obj_pool;
903
904
    // owned by PipelineFragmentContext
905
    RuntimeFilterMgr* _runtime_filter_mgr = nullptr;
906
907
    // Lock protecting _error_log and _unreported_error_idx
908
    std::mutex _error_log_lock;
909
910
    // Logs error messages.
911
    std::vector<std::string> _error_log;
912
913
    // _error_log[_unreported_error_idx+] has been not reported to the coordinator.
914
    int _unreported_error_idx;
915
916
    //Query-global timestamp_ms
917
    int64_t _timestamp_ms;
918
    int32_t _nano_seconds;
919
    std::string _timezone;
920
    cctz::time_zone _timezone_obj;
921
    std::string _lc_time_names;
922
923
    TUniqueId _query_id;
924
    // fragment id for each TPipelineFragmentParams
925
    int32_t _fragment_id;
926
    TUniqueId _fragment_instance_id;
927
    TQueryOptions _query_options;
928
    ExecEnv* _exec_env = nullptr;
929
930
    AtomicStatus _exec_status;
931
932
    int _per_fragment_instance_idx;
933
    int _num_per_fragment_instances = 0;
934
    int _load_stream_per_node = 0;
935
    int _total_load_streams = 0;
936
    int _num_local_sink = 0;
937
938
    // The backend id on which this fragment instance runs
939
    int64_t _backend_id = -1;
940
941
    // used as send id
942
    int _be_number;
943
944
    // put here to collect files??
945
    std::vector<std::string> _output_files;
946
    std::atomic<int64_t> _num_rows_load_total;      // total rows read from source
947
    std::atomic<int64_t> _num_rows_load_filtered;   // unqualified rows
948
    std::atomic<int64_t> _num_rows_load_unselected; // rows filtered by predicates
949
    std::atomic<int64_t> _num_rows_filtered_in_strict_mode_partial_update;
950
    std::atomic<int64_t> _num_print_error_rows;
951
952
    std::atomic<int64_t> _num_bytes_load_total; // total bytes read from source
953
    std::atomic<int64_t> _num_finished_scan_range;
954
955
    std::vector<std::string> _export_output_files;
956
    std::string _import_label;
957
    std::string _db_name;
958
    std::string _load_dir;
959
    int64_t _load_job_id;
960
    int64_t _wal_id = -1;
961
    size_t _content_length = 0;
962
963
    // mini load
964
    std::string _error_log_file_path;
965
    std::unique_ptr<std::ofstream> _error_log_file; // error file path, absolute path
966
    std::string _first_error_msg = "";
967
    mutable std::mutex _tablet_infos_mutex;
968
    std::vector<TTabletCommitInfo> _tablet_commit_infos;
969
    std::vector<TErrorTabletInfo> _error_tablet_infos;
970
    int _max_operator_id = 0;
971
    int _task_id = -1;
972
    int _task_num = 0;
973
974
    mutable std::mutex _hive_partition_updates_mutex;
975
    std::vector<THivePartitionUpdate> _hive_partition_updates;
976
977
    mutable std::mutex _iceberg_commit_datas_mutex;
978
    std::vector<TIcebergCommitData> _iceberg_commit_datas;
979
980
    mutable std::mutex _mc_commit_datas_mutex;
981
    std::vector<TMCCommitData> _mc_commit_datas;
982
983
    std::vector<std::unique_ptr<doris::PipelineXLocalStateBase>> _op_id_to_local_state;
984
985
    std::unique_ptr<doris::PipelineXSinkLocalStateBase> _sink_local_state;
986
987
    QueryContext* _query_ctx = nullptr;
988
989
    // true if max_filter_ratio is 0
990
    bool _load_zero_tolerance = false;
991
992
    // only to lock _pipeline_id_to_profile
993
    std::shared_mutex _pipeline_profile_lock;
994
    std::vector<std::shared_ptr<RuntimeProfile>> _pipeline_id_to_profile;
995
996
    // save error log to s3
997
    std::shared_ptr<io::S3FileSystem> _s3_error_fs;
998
    // error file path on s3, ${bucket}/${prefix}/error_log/${label}_${fragment_instance_id}
999
    std::string _s3_error_log_file_path;
1000
    std::mutex _s3_error_log_file_lock;
1001
1002
    // used for encoding the global lazy materialize
1003
    std::shared_ptr<IdFileMap> _id_file_map = nullptr;
1004
1005
    std::set<int> _registered_runtime_filter_ids;
1006
};
1007
1008
#define RETURN_IF_CANCELLED(state)               \
1009
1.32M
    do {                                         \
1010
1.32M
        if (UNLIKELY((state)->is_cancelled())) { \
1011
22
            return (state)->cancel_reason();     \
1012
22
        }                                        \
1013
1.32M
    } while (false)
1014
1015
} // namespace doris