Coverage Report

Created: 2026-03-28 18:46

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
5.56M
inline int32_t get_execution_rpc_timeout_ms(int32_t execution_timeout_sec) {
57
5.56M
    return std::min(config::execution_max_rpc_timeout_sec, execution_timeout_sec) * 1000;
58
5.56M
}
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
41.9M
    const TQueryOptions& query_options() const { return _query_options; }
122
321k
    int64_t scan_queue_mem_limit() const {
123
321k
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
124
321k
                                                           : _query_options.mem_limit / 20;
125
321k
    }
126
127
841k
    bool enable_adjust_conjunct_order_by_cost() const {
128
841k
        return _query_options.__isset.enable_adjust_conjunct_order_by_cost &&
129
841k
               _query_options.enable_adjust_conjunct_order_by_cost;
130
841k
    }
131
132
321k
    int32_t max_column_reader_num() const {
133
321k
        return _query_options.__isset.max_column_reader_num ? _query_options.max_column_reader_num
134
18.4E
                                                            : 20000;
135
321k
    }
136
137
8.13M
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
138
139
7.26M
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
140
2.87M
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
141
88.8M
    MOCK_FUNCTION int batch_size() const { return _query_options.batch_size; }
142
1.16M
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
143
0
    int max_errors() const { return _query_options.max_errors; }
144
5.97M
    int execution_timeout() const {
145
5.98M
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
146
18.4E
                                                        : _query_options.query_timeout;
147
5.97M
    }
148
575k
    int max_scanners_concurrency() const {
149
575k
        return _query_options.__isset.max_scanners_concurrency
150
577k
                       ? _query_options.max_scanners_concurrency
151
18.4E
                       : 0;
152
575k
    }
153
107k
    int max_file_scanners_concurrency() const {
154
107k
        return _query_options.__isset.max_file_scanners_concurrency
155
107k
                       ? _query_options.max_file_scanners_concurrency
156
18.4E
                       : max_scanners_concurrency();
157
107k
    }
158
159
574k
    int min_scanners_concurrency() const {
160
574k
        return _query_options.__isset.min_scanners_concurrency
161
575k
                       ? _query_options.min_scanners_concurrency
162
18.4E
                       : 1;
163
574k
    }
164
165
53.1k
    int min_file_scanners_concurrency() const {
166
53.1k
        return _query_options.__isset.min_file_scanners_concurrency
167
53.1k
                       ? _query_options.min_file_scanners_concurrency
168
18.4E
                       : 1;
169
53.1k
    }
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
109k
    TQueryType::type query_type() const { return _query_options.query_type; }
178
73.2k
    int64_t timestamp_ms() const { return _timestamp_ms; }
179
73.0k
    int32_t nano_seconds() const { return _nano_seconds; }
180
    // if possible, use timezone_obj() rather than timezone()
181
393k
    const std::string& timezone() const { return _timezone; }
182
305k
    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.3M
    const TUniqueId& query_id() const { return _query_id; }
190
2.51M
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
191
    // should only be called in pipeline engine
192
1.66k
    int32_t fragment_id() const { return _fragment_id; }
193
6.72M
    ExecEnv* exec_env() { return _exec_env; }
194
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const;
195
196
    // Returns runtime state profile
197
1.28k
    RuntimeProfile* runtime_profile() { return &_profile; }
198
45.5k
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
199
200
553k
    bool enable_function_pushdown() const {
201
553k
        return _query_options.__isset.enable_function_pushdown &&
202
554k
               _query_options.enable_function_pushdown;
203
553k
    }
204
205
870k
    bool check_overflow_for_decimal() const {
206
870k
        return _query_options.__isset.check_overflow_for_decimal &&
207
870k
               _query_options.check_overflow_for_decimal;
208
870k
    }
209
210
869k
    bool enable_strict_mode() const {
211
869k
        return _query_options.__isset.enable_strict_cast && _query_options.enable_strict_cast;
212
869k
    }
213
214
84
    bool enable_insert_strict() const {
215
84
        return _query_options.__isset.enable_insert_strict && _query_options.enable_insert_strict;
216
84
    }
217
218
10.9k
    bool enable_common_expr_pushdown() const {
219
10.9k
        return _query_options.__isset.enable_common_expr_pushdown &&
220
10.9k
               _query_options.enable_common_expr_pushdown;
221
10.9k
    }
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
428k
    bool mysql_row_binary_format() const {
230
428k
        return _query_options.__isset.mysql_row_binary_format &&
231
428k
               _query_options.mysql_row_binary_format;
232
428k
    }
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.60M
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
270
5.34k
    int64_t backend_id() const { return _backend_id; }
271
272
2.16M
    void set_be_number(int be_number) { _be_number = be_number; }
273
9.18M
    int be_number(void) const { return _be_number; }
274
275
4.81k
    std::vector<std::string>& output_files() { return _output_files; }
276
277
806
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
278
279
284k
    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
383
    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
234k
    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
174
    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
72.9k
    int64_t load_job_id() const { return _load_job_id; }
302
303
    std::string get_error_log_file_path();
304
305
343k
    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
84.6k
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
314
315
287k
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
316
317
408k
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
318
319
406k
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
320
321
154k
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
322
323
135k
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
324
135k
        return _num_rows_filtered_in_strict_mode_partial_update;
325
135k
    }
326
327
38.5k
    int64_t num_rows_load_success() {
328
38.5k
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
329
38.5k
    }
330
331
40.8k
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
332
333
76.9k
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
334
335
40.6k
    void update_num_bytes_load_total(int64_t bytes_load) {
336
40.6k
        _num_bytes_load_total.fetch_add(bytes_load);
337
40.6k
    }
338
339
68.8k
    void update_num_finished_scan_range(int64_t finished_range) {
340
68.8k
        _num_finished_scan_range.fetch_add(finished_range);
341
68.8k
    }
342
343
1.44M
    void update_num_rows_load_filtered(int64_t num_rows) {
344
1.44M
        _num_rows_load_filtered.fetch_add(num_rows);
345
1.44M
    }
346
347
1.44M
    void update_num_rows_load_unselected(int64_t num_rows) {
348
1.44M
        _num_rows_load_unselected.fetch_add(num_rows);
349
1.44M
    }
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.15M
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
356
357
292k
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
358
359
2.60M
    void set_num_per_fragment_instances(int num_instances) {
360
2.60M
        _num_per_fragment_instances = num_instances;
361
2.60M
    }
362
363
72.9k
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
364
365
2.60M
    void set_load_stream_per_node(int load_stream_per_node) {
366
2.60M
        _load_stream_per_node = load_stream_per_node;
367
2.60M
    }
368
369
5.32k
    int load_stream_per_node() const { return _load_stream_per_node; }
370
371
2.60M
    void set_total_load_streams(int total_load_streams) {
372
2.60M
        _total_load_streams = total_load_streams;
373
2.60M
    }
374
375
5.32k
    int total_load_streams() const { return _total_load_streams; }
376
377
2.60M
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
378
379
5.32k
    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
30.8M
    int be_exec_version() const {
392
30.8M
        DCHECK(_query_options.__isset.be_exec_version &&
393
30.8M
               BeExecVersionManager::check_be_exec_version(_query_options.be_exec_version));
394
30.8M
        return _query_options.be_exec_version;
395
30.8M
    }
396
445k
    bool enable_local_shuffle() const {
397
445k
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle;
398
445k
    }
399
400
5.52M
    MOCK_FUNCTION bool enable_local_exchange() const {
401
5.53M
        return _query_options.__isset.enable_local_exchange && _query_options.enable_local_exchange;
402
5.52M
    }
403
404
2.81k
    bool trim_tailing_spaces_for_external_table_query() const {
405
2.81k
        return _query_options.trim_tailing_spaces_for_external_table_query;
406
2.81k
    }
407
408
428k
    bool return_object_data_as_binary() const {
409
428k
        return _query_options.return_object_data_as_binary;
410
428k
    }
411
412
7.52M
    bool enable_fuzzy_blockable_task() const {
413
7.52M
        return _query_options.__isset.enable_fuzzy_blockable_task &&
414
7.52M
               _query_options.enable_fuzzy_blockable_task;
415
7.52M
    }
416
417
448k
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
418
448k
        if (_query_options.__isset.fragment_transmission_compression_codec) {
419
445k
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
420
120k
                return segment_v2::CompressionTypePB::LZ4;
421
324k
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
422
116k
                return segment_v2::CompressionTypePB::SNAPPY;
423
208k
            } else {
424
208k
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
425
208k
            }
426
445k
        }
427
2.92k
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
428
448k
    }
429
430
2.55M
    bool skip_storage_engine_merge() const {
431
2.55M
        return _query_options.__isset.skip_storage_engine_merge &&
432
2.55M
               _query_options.skip_storage_engine_merge;
433
2.55M
    }
434
435
1.57M
    bool skip_delete_predicate() const {
436
1.57M
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
437
1.57M
    }
438
439
1.27M
    bool skip_delete_bitmap() const {
440
1.27M
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
441
1.27M
    }
442
443
1.57M
    bool skip_missing_version() const {
444
1.57M
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
445
1.57M
    }
446
447
10.2k
    int64_t data_queue_max_blocks() const {
448
10.2k
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
449
18.4E
                                                            : 1;
450
10.2k
    }
451
452
    bool enable_page_cache() const;
453
454
1.27M
    bool enable_prefer_cached_rowset() const {
455
1.27M
        return _query_options.__isset.enable_prefer_cached_rowset &&
456
1.27M
               _query_options.enable_prefer_cached_rowset;
457
1.27M
    }
458
459
1.27M
    int64_t query_freshness_tolerance_ms() const {
460
1.27M
        return _query_options.query_freshness_tolerance_ms;
461
1.27M
    }
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
287k
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
469
287k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
470
287k
        return _tablet_commit_infos;
471
287k
    }
472
473
70.6k
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
474
70.6k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
475
70.6k
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
476
70.6k
                                    std::make_move_iterator(commit_infos.begin()),
477
70.6k
                                    std::make_move_iterator(commit_infos.end()));
478
70.6k
    }
479
480
284k
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
481
284k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
482
284k
        return _error_tablet_infos;
483
284k
    }
484
485
68.5k
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
486
68.5k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
487
68.5k
        _error_tablet_infos.insert(_error_tablet_infos.end(),
488
68.5k
                                   std::make_move_iterator(tablet_infos.begin()),
489
68.5k
                                   std::make_move_iterator(tablet_infos.end()));
490
68.5k
    }
491
492
284k
    std::vector<THivePartitionUpdate> hive_partition_updates() const {
493
284k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
494
284k
        return _hive_partition_updates;
495
284k
    }
496
497
2.15k
    void add_hive_partition_updates(const THivePartitionUpdate& hive_partition_update) {
498
2.15k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
499
2.15k
        _hive_partition_updates.emplace_back(hive_partition_update);
500
2.15k
    }
501
502
284k
    std::vector<TIcebergCommitData> iceberg_commit_datas() const {
503
284k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
504
284k
        return _iceberg_commit_datas;
505
284k
    }
506
507
2.31k
    void add_iceberg_commit_datas(const TIcebergCommitData& iceberg_commit_data) {
508
2.31k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
509
2.31k
        _iceberg_commit_datas.emplace_back(iceberg_commit_data);
510
2.31k
    }
511
512
284k
    std::vector<TMCCommitData> mc_commit_datas() const {
513
284k
        std::lock_guard<std::mutex> lock(_mc_commit_datas_mutex);
514
284k
        return _mc_commit_datas;
515
284k
    }
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
302k
    RuntimeFilterMgr* local_runtime_filter_mgr() { return _runtime_filter_mgr; }
526
527
    RuntimeFilterMgr* global_runtime_filter_mgr();
528
529
2.16M
    void set_runtime_filter_mgr(RuntimeFilterMgr* runtime_filter_mgr) {
530
2.16M
        _runtime_filter_mgr = runtime_filter_mgr;
531
2.16M
    }
532
533
84.7M
    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.16M
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
541
2.16M
        _query_mem_tracker = tracker;
542
2.16M
    }
543
544
4.29k
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
545
546
3.34M
    bool enable_profile() const {
547
3.34M
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
548
3.34M
    }
549
550
151
    int cte_max_recursion_depth() const {
551
151
        return _query_options.__isset.cte_max_recursion_depth
552
151
                       ? _query_options.cte_max_recursion_depth
553
151
                       : 0;
554
151
    }
555
556
360
    bool enable_streaming_agg_hash_join_force_passthrough() const {
557
360
        return _query_options.__isset.enable_streaming_agg_hash_join_force_passthrough &&
558
360
               _query_options.enable_streaming_agg_hash_join_force_passthrough;
559
360
    }
560
561
74.4k
    bool enable_distinct_streaming_agg_force_passthrough() const {
562
74.4k
        return _query_options.__isset.enable_distinct_streaming_agg_force_passthrough &&
563
74.4k
               _query_options.enable_distinct_streaming_agg_force_passthrough;
564
74.4k
    }
565
566
14.2k
    bool enable_broadcast_join_force_passthrough() const {
567
14.2k
        return _query_options.__isset.enable_broadcast_join_force_passthrough &&
568
14.2k
               _query_options.enable_broadcast_join_force_passthrough;
569
14.2k
    }
570
571
307k
    int rpc_verbose_profile_max_instance_count() const {
572
307k
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
573
307k
                       ? _query_options.rpc_verbose_profile_max_instance_count
574
307k
                       : 0;
575
307k
    }
576
577
13.0k
    MOCK_FUNCTION bool enable_share_hash_table_for_broadcast_join() const {
578
13.0k
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
579
13.0k
               _query_options.enable_share_hash_table_for_broadcast_join;
580
13.0k
    }
581
582
327k
    bool enable_parallel_scan() const {
583
328k
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
584
327k
    }
585
586
220k
    bool enable_aggregate_function_null_v2() const {
587
220k
        return _query_options.__isset.enable_aggregate_function_null_v2 &&
588
220k
               _query_options.enable_aggregate_function_null_v2;
589
220k
    }
590
591
50
    bool is_read_csv_empty_line_as_null() const {
592
50
        return _query_options.__isset.read_csv_empty_line_as_null &&
593
50
               _query_options.read_csv_empty_line_as_null;
594
50
    }
595
596
162k
    int parallel_scan_max_scanners_count() const {
597
162k
        return _query_options.__isset.parallel_scan_max_scanners_count
598
162k
                       ? _query_options.parallel_scan_max_scanners_count
599
18.4E
                       : 0;
600
162k
    }
601
602
1.81k
    int partition_topn_max_partitions() const {
603
1.81k
        return _query_options.__isset.partition_topn_max_partitions
604
1.81k
                       ? _query_options.partition_topn_max_partitions
605
1.81k
                       : 1024;
606
1.81k
    }
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
162k
    int64_t parallel_scan_min_rows_per_scanner() const {
615
162k
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
616
162k
                       ? _query_options.parallel_scan_min_rows_per_scanner
617
18.4E
                       : 0;
618
162k
    }
619
620
3.24k
    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.68M
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
643
2.68M
        _task_execution_context_inited = true;
644
2.68M
        _task_execution_context = context;
645
2.68M
    }
646
647
185k
    bool task_execution_context_inited() const { return _task_execution_context_inited; }
648
649
2.75M
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
650
18.4E
        CHECK(_task_execution_context_inited)
651
18.4E
                << "_task_execution_context_inited == false, the ctx is not inited";
652
2.75M
        return _task_execution_context;
653
2.75M
    }
654
655
    Status register_producer_runtime_filter(
656
            const doris::TRuntimeFilterDesc& desc,
657
            std::shared_ptr<RuntimeFilterProducer>* producer_filter);
658
659
    Status register_consumer_runtime_filter(
660
            const doris::TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
661
            std::shared_ptr<RuntimeFilterConsumer>* consumer_filter);
662
663
    bool is_nereids() const;
664
665
42.8M
    bool enable_spill() const {
666
42.8M
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
667
42.8M
               (_query_options.__isset.enable_spill && _query_options.enable_spill);
668
42.8M
    }
669
670
85.2M
    bool enable_force_spill() const {
671
85.2M
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
672
85.2M
    }
673
674
18.5k
    int64_t spill_min_revocable_mem() const {
675
18.5k
        if (_query_options.__isset.min_revocable_mem) {
676
18.5k
            return std::max(_query_options.min_revocable_mem, (int64_t)1 << 20);
677
18.5k
        }
678
0
        return 32 << 20;
679
18.5k
    }
680
681
1.20k
    int spill_aggregation_partition_count() const {
682
1.20k
        if (_query_options.__isset.spill_aggregation_partition_count) {
683
1.20k
            return std::min(std::max(2, _query_options.spill_aggregation_partition_count), 32);
684
1.20k
        }
685
0
        return 8;
686
1.20k
    }
687
688
9
    int spill_hash_join_partition_count() const {
689
9
        if (_query_options.__isset.spill_hash_join_partition_count) {
690
9
            return std::min(std::max(2, _query_options.spill_hash_join_partition_count), 32);
691
9
        }
692
0
        return 8;
693
9
    }
694
695
604
    int spill_repartition_max_depth() const {
696
604
        if (_query_options.__isset.spill_repartition_max_depth) {
697
            // Clamp to a reasonable range: [1, 128]
698
604
            return std::max(1, std::min(_query_options.spill_repartition_max_depth, 128));
699
604
        }
700
0
        return 8;
701
604
    }
702
703
84
    int64_t spill_buffer_size_bytes() const {
704
        // clamp to [1MB, 256MB]
705
84
        constexpr int64_t kMin = 1LL * 1024 * 1024;
706
84
        constexpr int64_t kMax = 256LL * 1024 * 1024;
707
84
        if (_query_options.__isset.spill_buffer_size_bytes) {
708
84
            int64_t v = _query_options.spill_buffer_size_bytes;
709
84
            if (v < kMin) return kMin;
710
84
            if (v > kMax) return kMax;
711
84
            return v;
712
84
        }
713
0
        return 8LL * 1024 * 1024;
714
84
    }
715
716
    // Per-sink memory limits: after spill is triggered, the sink proactively
717
    // spills when its revocable memory exceeds this threshold.
718
    // Clamped to [1MB, 4GB], default 64MB.
719
2
    int64_t spill_join_build_sink_mem_limit_bytes() const {
720
2
        constexpr int64_t kMin = 1LL * 1024 * 1024;
721
2
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
722
2
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
723
2
        if (_query_options.__isset.spill_join_build_sink_mem_limit_bytes) {
724
2
            int64_t v = _query_options.spill_join_build_sink_mem_limit_bytes;
725
2
            return std::min(std::max(v, kMin), kMax);
726
2
        }
727
0
        return kDefault;
728
2
    }
729
730
16
    int64_t spill_aggregation_sink_mem_limit_bytes() const {
731
16
        constexpr int64_t kMin = 1LL * 1024 * 1024;
732
16
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
733
16
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
734
16
        if (_query_options.__isset.spill_aggregation_sink_mem_limit_bytes) {
735
16
            int64_t v = _query_options.spill_aggregation_sink_mem_limit_bytes;
736
16
            return std::min(std::max(v, kMin), kMax);
737
16
        }
738
0
        return kDefault;
739
16
    }
740
741
34
    int64_t spill_sort_sink_mem_limit_bytes() const {
742
34
        constexpr int64_t kMin = 1LL * 1024 * 1024;
743
34
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
744
34
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
745
34
        if (_query_options.__isset.spill_sort_sink_mem_limit_bytes) {
746
34
            int64_t v = _query_options.spill_sort_sink_mem_limit_bytes;
747
34
            return std::min(std::max(v, kMin), kMax);
748
34
        }
749
0
        return kDefault;
750
34
    }
751
752
12
    int64_t spill_sort_merge_mem_limit_bytes() const {
753
12
        constexpr int64_t kMin = 1LL * 1024 * 1024;
754
12
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
755
12
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
756
12
        if (_query_options.__isset.spill_sort_merge_mem_limit_bytes) {
757
12
            int64_t v = _query_options.spill_sort_merge_mem_limit_bytes;
758
12
            return std::min(std::max(v, kMin), kMax);
759
12
        }
760
0
        return kDefault;
761
12
    }
762
763
0
    int64_t low_memory_mode_buffer_limit() const {
764
0
        if (_query_options.__isset.low_memory_mode_buffer_limit) {
765
0
            return std::max(_query_options.low_memory_mode_buffer_limit, (int64_t)1);
766
0
        }
767
0
        return 32L * 1024 * 1024;
768
0
    }
769
770
0
    int spill_revocable_memory_high_watermark_percent() const {
771
0
        if (_query_options.__isset.revocable_memory_high_watermark_percent) {
772
0
            return _query_options.revocable_memory_high_watermark_percent;
773
0
        }
774
0
        return -1;
775
0
    }
776
777
225k
    MOCK_FUNCTION bool enable_shared_exchange_sink_buffer() const {
778
225k
        return _query_options.__isset.enable_shared_exchange_sink_buffer &&
779
225k
               _query_options.enable_shared_exchange_sink_buffer;
780
225k
    }
781
782
79.3M
    size_t minimum_operator_memory_required_bytes() const {
783
79.3M
        if (_query_options.__isset.minimum_operator_memory_required_kb) {
784
79.3M
            return _query_options.minimum_operator_memory_required_kb * 1024;
785
18.4E
        } else {
786
            // refer other database
787
18.4E
            return 4 * 1024 * 1024;
788
18.4E
        }
789
79.3M
    }
790
791
267k
    MOCK_FUNCTION bool enable_use_hybrid_sort() const {
792
267k
        return _query_options.__isset.enable_use_hybrid_sort &&
793
267k
               _query_options.enable_use_hybrid_sort;
794
267k
    }
795
796
2.23M
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
797
798
628
    int max_operator_id() const { return _max_operator_id; }
799
800
2.16M
    void set_task_id(int id) { _task_id = id; }
801
802
827
    int task_id() const { return _task_id; }
803
804
2.16M
    void set_task_num(int task_num) { _task_num = task_num; }
805
806
7.23k
    int task_num() const { return _task_num; }
807
808
331k
    int profile_level() const { return _profile_level; }
809
810
51.3k
    std::shared_ptr<IdFileMap>& get_id_file_map() { return _id_file_map; }
811
812
    void set_id_file_map();
813
1.28M
    VectorSearchUserParams get_vector_search_params() const {
814
1.28M
        return VectorSearchUserParams(_query_options.hnsw_ef_search,
815
1.28M
                                      _query_options.hnsw_check_relative_distance,
816
1.28M
                                      _query_options.hnsw_bounded_queue, _query_options.ivf_nprobe);
817
1.28M
    }
818
819
75.0k
    bool runtime_filter_wait_infinitely() const {
820
75.0k
        return _query_options.__isset.runtime_filter_wait_infinitely &&
821
75.1k
               _query_options.runtime_filter_wait_infinitely;
822
75.0k
    }
823
824
    const std::set<int>& get_deregister_runtime_filter() const;
825
826
    void merge_register_runtime_filter(const std::set<int>& runtime_filter_ids);
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
1.11M
    do {                                         \
963
1.11M
        if (UNLIKELY((state)->is_cancelled())) { \
964
2
            return (state)->cancel_reason();     \
965
2
        }                                        \
966
1.11M
    } while (false)
967
968
} // namespace doris