Coverage Report

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