Coverage Report

Created: 2026-03-20 13:28

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