Coverage Report

Created: 2026-04-14 07:58

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
4.70M
inline int32_t get_execution_rpc_timeout_ms(int32_t execution_timeout_sec) {
57
4.70M
    return std::min(config::execution_max_rpc_timeout_sec, execution_timeout_sec) * 1000;
58
4.70M
}
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.7M
    const TQueryOptions& query_options() const { return _query_options; }
122
297k
    int64_t scan_queue_mem_limit() const {
123
297k
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
124
297k
                                                           : _query_options.mem_limit / 20;
125
297k
    }
126
127
804k
    bool enable_adjust_conjunct_order_by_cost() const {
128
804k
        return _query_options.__isset.enable_adjust_conjunct_order_by_cost &&
129
804k
               _query_options.enable_adjust_conjunct_order_by_cost;
130
804k
    }
131
132
297k
    int32_t max_column_reader_num() const {
133
297k
        return _query_options.__isset.max_column_reader_num ? _query_options.max_column_reader_num
134
18.4E
                                                            : 20000;
135
297k
    }
136
137
7.88M
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
138
139
7.20M
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
140
2.79M
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
141
17.7M
    MOCK_FUNCTION int batch_size() const { return _query_options.batch_size; }
142
1.30M
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
143
0
    int max_errors() const { return _query_options.max_errors; }
144
5.10M
    int execution_timeout() const {
145
5.11M
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
146
18.4E
                                                        : _query_options.query_timeout;
147
5.10M
    }
148
522k
    int max_scanners_concurrency() const {
149
522k
        return _query_options.__isset.max_scanners_concurrency
150
523k
                       ? _query_options.max_scanners_concurrency
151
18.4E
                       : 0;
152
522k
    }
153
111k
    int max_file_scanners_concurrency() const {
154
111k
        return _query_options.__isset.max_file_scanners_concurrency
155
111k
                       ? _query_options.max_file_scanners_concurrency
156
18.4E
                       : max_scanners_concurrency();
157
111k
    }
158
159
522k
    int min_scanners_concurrency() const {
160
522k
        return _query_options.__isset.min_scanners_concurrency
161
523k
                       ? _query_options.min_scanners_concurrency
162
18.4E
                       : 1;
163
522k
    }
164
165
55.2k
    int min_file_scanners_concurrency() const {
166
55.2k
        return _query_options.__isset.min_file_scanners_concurrency
167
55.2k
                       ? _query_options.min_file_scanners_concurrency
168
18.4E
                       : 1;
169
55.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
1.45M
    TQueryType::type query_type() const { return _query_options.query_type; }
178
59.9k
    int64_t timestamp_ms() const { return _timestamp_ms; }
179
59.7k
    int32_t nano_seconds() const { return _nano_seconds; }
180
    // if possible, use timezone_obj() rather than timezone()
181
379k
    const std::string& timezone() const { return _timezone; }
182
296k
    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.1M
    const TUniqueId& query_id() const { return _query_id; }
190
2.56M
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
191
    // should only be called in pipeline engine
192
594
    int32_t fragment_id() const { return _fragment_id; }
193
5.88M
    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
50.0k
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
199
200
515k
    bool enable_function_pushdown() const {
201
515k
        return _query_options.__isset.enable_function_pushdown &&
202
515k
               _query_options.enable_function_pushdown;
203
515k
    }
204
205
831k
    bool check_overflow_for_decimal() const {
206
831k
        return _query_options.__isset.check_overflow_for_decimal &&
207
831k
               _query_options.check_overflow_for_decimal;
208
831k
    }
209
210
831k
    bool enable_strict_mode() const {
211
832k
        return _query_options.__isset.enable_strict_cast && _query_options.enable_strict_cast;
212
831k
    }
213
214
82
    bool enable_insert_strict() const {
215
82
        return _query_options.__isset.enable_insert_strict && _query_options.enable_insert_strict;
216
82
    }
217
218
11.0k
    bool enable_common_expr_pushdown() const {
219
11.0k
        return _query_options.__isset.enable_common_expr_pushdown &&
220
11.1k
               _query_options.enable_common_expr_pushdown;
221
11.0k
    }
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
474k
    bool mysql_row_binary_format() const {
230
474k
        return _query_options.__isset.mysql_row_binary_format &&
231
474k
               _query_options.mysql_row_binary_format;
232
474k
    }
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.52M
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
270
5.47k
    int64_t backend_id() const { return _backend_id; }
271
272
2.09M
    void set_be_number(int be_number) { _be_number = be_number; }
273
6.84M
    int be_number(void) const { return _be_number; }
274
275
4.63k
    std::vector<std::string>& output_files() { return _output_files; }
276
277
802
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
278
279
226k
    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
379
    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
178k
    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
173
    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
59.6k
    int64_t load_job_id() const { return _load_job_id; }
302
303
    std::string get_error_log_file_path();
304
305
306k
    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
81.6k
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
314
315
229k
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
316
317
347k
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
318
319
330k
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
320
321
137k
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
322
323
108k
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
324
108k
        return _num_rows_filtered_in_strict_mode_partial_update;
325
108k
    }
326
327
38.4k
    int64_t num_rows_load_success() {
328
38.4k
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
329
38.4k
    }
330
331
40.1k
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
332
333
60.2k
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
334
335
39.9k
    void update_num_bytes_load_total(int64_t bytes_load) {
336
39.9k
        _num_bytes_load_total.fetch_add(bytes_load);
337
39.9k
    }
338
339
70.7k
    void update_num_finished_scan_range(int64_t finished_range) {
340
70.7k
        _num_finished_scan_range.fetch_add(finished_range);
341
70.7k
    }
342
343
1.34M
    void update_num_rows_load_filtered(int64_t num_rows) {
344
1.34M
        _num_rows_load_filtered.fetch_add(num_rows);
345
1.34M
    }
346
347
1.34M
    void update_num_rows_load_unselected(int64_t num_rows) {
348
1.34M
        _num_rows_load_unselected.fetch_add(num_rows);
349
1.34M
    }
350
351
77
    void set_num_rows_filtered_in_strict_mode_partial_update(int64_t num_rows) {
352
77
        _num_rows_filtered_in_strict_mode_partial_update = num_rows;
353
77
    }
354
355
2.09M
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
356
357
278k
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
358
359
2.52M
    void set_num_per_fragment_instances(int num_instances) {
360
2.52M
        _num_per_fragment_instances = num_instances;
361
2.52M
    }
362
363
59.6k
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
364
365
2.52M
    void set_load_stream_per_node(int load_stream_per_node) {
366
2.52M
        _load_stream_per_node = load_stream_per_node;
367
2.52M
    }
368
369
5.46k
    int load_stream_per_node() const { return _load_stream_per_node; }
370
371
2.52M
    void set_total_load_streams(int total_load_streams) {
372
2.52M
        _total_load_streams = total_load_streams;
373
2.52M
    }
374
375
5.47k
    int total_load_streams() const { return _total_load_streams; }
376
377
2.52M
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
378
379
5.46k
    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
29.6M
    int be_exec_version() const {
392
29.6M
        DCHECK(_query_options.__isset.be_exec_version &&
393
29.6M
               BeExecVersionManager::check_be_exec_version(_query_options.be_exec_version));
394
29.6M
        return _query_options.be_exec_version;
395
29.6M
    }
396
432k
    bool enable_local_shuffle() const {
397
432k
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle;
398
432k
    }
399
400
4.68M
    MOCK_FUNCTION bool enable_local_exchange() const {
401
4.69M
        return _query_options.__isset.enable_local_exchange && _query_options.enable_local_exchange;
402
4.68M
    }
403
404
2.77k
    bool trim_tailing_spaces_for_external_table_query() const {
405
2.77k
        return _query_options.trim_tailing_spaces_for_external_table_query;
406
2.77k
    }
407
408
473k
    bool return_object_data_as_binary() const {
409
473k
        return _query_options.return_object_data_as_binary;
410
473k
    }
411
412
7.55M
    bool enable_fuzzy_blockable_task() const {
413
7.55M
        return _query_options.__isset.enable_fuzzy_blockable_task &&
414
7.55M
               _query_options.enable_fuzzy_blockable_task;
415
7.55M
    }
416
417
434k
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
418
434k
        if (_query_options.__isset.fragment_transmission_compression_codec) {
419
431k
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
420
142k
                return segment_v2::CompressionTypePB::LZ4;
421
288k
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
422
93.9k
                return segment_v2::CompressionTypePB::SNAPPY;
423
194k
            } else {
424
194k
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
425
194k
            }
426
431k
        }
427
3.24k
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
428
434k
    }
429
430
2.37M
    bool skip_storage_engine_merge() const {
431
2.37M
        return _query_options.__isset.skip_storage_engine_merge &&
432
2.37M
               _query_options.skip_storage_engine_merge;
433
2.37M
    }
434
435
1.35M
    bool skip_delete_predicate() const {
436
1.35M
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
437
1.35M
    }
438
439
1.18M
    bool skip_delete_bitmap() const {
440
1.18M
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
441
1.18M
    }
442
443
1.35M
    bool skip_missing_version() const {
444
1.35M
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
445
1.35M
    }
446
447
8.60k
    int64_t data_queue_max_blocks() const {
448
8.60k
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
449
18.4E
                                                            : 1;
450
8.60k
    }
451
452
    bool enable_page_cache() const;
453
454
1.00M
    bool enable_prefer_cached_rowset() const {
455
1.00M
        return _query_options.__isset.enable_prefer_cached_rowset &&
456
1.00M
               _query_options.enable_prefer_cached_rowset;
457
1.00M
    }
458
459
1.00M
    int64_t query_freshness_tolerance_ms() const {
460
1.00M
        return _query_options.query_freshness_tolerance_ms;
461
1.00M
    }
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
228k
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
469
228k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
470
228k
        return _tablet_commit_infos;
471
228k
    }
472
473
57.4k
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
474
57.4k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
475
57.4k
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
476
57.4k
                                    std::make_move_iterator(commit_infos.begin()),
477
57.4k
                                    std::make_move_iterator(commit_infos.end()));
478
57.4k
    }
479
480
226k
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
481
226k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
482
226k
        return _error_tablet_infos;
483
226k
    }
484
485
55.4k
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
486
55.4k
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
487
55.4k
        _error_tablet_infos.insert(_error_tablet_infos.end(),
488
55.4k
                                   std::make_move_iterator(tablet_infos.begin()),
489
55.4k
                                   std::make_move_iterator(tablet_infos.end()));
490
55.4k
    }
491
492
226k
    std::vector<THivePartitionUpdate> hive_partition_updates() const {
493
226k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
494
226k
        return _hive_partition_updates;
495
226k
    }
496
497
2.14k
    void add_hive_partition_updates(const THivePartitionUpdate& hive_partition_update) {
498
2.14k
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
499
2.14k
        _hive_partition_updates.emplace_back(hive_partition_update);
500
2.14k
    }
501
502
226k
    std::vector<TIcebergCommitData> iceberg_commit_datas() const {
503
226k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
504
226k
        return _iceberg_commit_datas;
505
226k
    }
506
507
2.47k
    void add_iceberg_commit_datas(const TIcebergCommitData& iceberg_commit_data) {
508
2.47k
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
509
2.47k
        _iceberg_commit_datas.emplace_back(iceberg_commit_data);
510
2.47k
    }
511
512
226k
    std::vector<TMCCommitData> mc_commit_datas() const {
513
226k
        std::lock_guard<std::mutex> lock(_mc_commit_datas_mutex);
514
226k
        return _mc_commit_datas;
515
226k
    }
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
173k
    RuntimeFilterMgr* local_runtime_filter_mgr() { return _runtime_filter_mgr; }
526
527
    RuntimeFilterMgr* global_runtime_filter_mgr();
528
529
2.09M
    void set_runtime_filter_mgr(RuntimeFilterMgr* runtime_filter_mgr) {
530
2.09M
        _runtime_filter_mgr = runtime_filter_mgr;
531
2.09M
    }
532
533
49.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.09M
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
541
2.09M
        _query_mem_tracker = tracker;
542
2.09M
    }
543
544
4.29k
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
545
546
3.42M
    bool enable_profile() const {
547
3.42M
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
548
3.42M
    }
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
495
    bool enable_streaming_agg_hash_join_force_passthrough() const {
557
495
        return _query_options.__isset.enable_streaming_agg_hash_join_force_passthrough &&
558
495
               _query_options.enable_streaming_agg_hash_join_force_passthrough;
559
495
    }
560
561
146k
    bool enable_local_exchange_before_agg() const {
562
146k
        return _query_options.__isset.enable_local_exchange_before_agg &&
563
146k
               _query_options.enable_local_exchange_before_agg;
564
146k
    }
565
566
37.8k
    bool enable_distinct_streaming_agg_force_passthrough() const {
567
37.8k
        return _query_options.__isset.enable_distinct_streaming_agg_force_passthrough &&
568
37.8k
               _query_options.enable_distinct_streaming_agg_force_passthrough;
569
37.8k
    }
570
571
19.7k
    bool enable_broadcast_join_force_passthrough() const {
572
19.7k
        return _query_options.__isset.enable_broadcast_join_force_passthrough &&
573
19.7k
               _query_options.enable_broadcast_join_force_passthrough;
574
19.7k
    }
575
576
303k
    int rpc_verbose_profile_max_instance_count() const {
577
303k
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
578
303k
                       ? _query_options.rpc_verbose_profile_max_instance_count
579
303k
                       : 0;
580
303k
    }
581
582
17.1k
    MOCK_FUNCTION bool enable_share_hash_table_for_broadcast_join() const {
583
17.1k
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
584
17.1k
               _query_options.enable_share_hash_table_for_broadcast_join;
585
17.1k
    }
586
587
289k
    bool enable_parallel_scan() const {
588
289k
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
589
289k
    }
590
591
185k
    bool enable_aggregate_function_null_v2() const {
592
185k
        return _query_options.__isset.enable_aggregate_function_null_v2 &&
593
185k
               _query_options.enable_aggregate_function_null_v2;
594
185k
    }
595
596
50
    bool is_read_csv_empty_line_as_null() const {
597
50
        return _query_options.__isset.read_csv_empty_line_as_null &&
598
50
               _query_options.read_csv_empty_line_as_null;
599
50
    }
600
601
153k
    int parallel_scan_max_scanners_count() const {
602
153k
        return _query_options.__isset.parallel_scan_max_scanners_count
603
153k
                       ? _query_options.parallel_scan_max_scanners_count
604
153k
                       : 0;
605
153k
    }
606
607
1.80k
    int partition_topn_max_partitions() const {
608
1.80k
        return _query_options.__isset.partition_topn_max_partitions
609
1.80k
                       ? _query_options.partition_topn_max_partitions
610
1.80k
                       : 1024;
611
1.80k
    }
612
613
0
    int partition_topn_per_partition_rows() const {
614
0
        return _query_options.__isset.partition_topn_pre_partition_rows
615
0
                       ? _query_options.partition_topn_pre_partition_rows
616
0
                       : 1000;
617
0
    }
618
619
153k
    int64_t parallel_scan_min_rows_per_scanner() const {
620
153k
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
621
153k
                       ? _query_options.parallel_scan_min_rows_per_scanner
622
153k
                       : 0;
623
153k
    }
624
625
3.29k
    void set_be_exec_version(int32_t version) noexcept { _query_options.be_exec_version = version; }
626
627
    using LocalState = doris::PipelineXLocalStateBase;
628
    using SinkLocalState = doris::PipelineXSinkLocalStateBase;
629
    // get result can return an error message, and we will only call it during the prepare.
630
    void emplace_local_state(int id, std::unique_ptr<LocalState> state);
631
632
    LocalState* get_local_state(int id);
633
    Result<LocalState*> get_local_state_result(int id);
634
635
    void emplace_sink_local_state(int id, std::unique_ptr<SinkLocalState> state);
636
637
    SinkLocalState* get_sink_local_state();
638
639
    Result<SinkLocalState*> get_sink_local_state_result();
640
641
    void resize_op_id_to_local_state(int operator_size);
642
643
    std::vector<std::shared_ptr<RuntimeProfile>> pipeline_id_to_profile();
644
645
    std::vector<std::shared_ptr<RuntimeProfile>> build_pipeline_profile(std::size_t pipeline_size);
646
647
2.59M
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
648
2.59M
        _task_execution_context_inited = true;
649
2.59M
        _task_execution_context = context;
650
2.59M
    }
651
652
112k
    bool task_execution_context_inited() const { return _task_execution_context_inited; }
653
654
2.35M
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
655
18.4E
        CHECK(_task_execution_context_inited)
656
18.4E
                << "_task_execution_context_inited == false, the ctx is not inited";
657
2.35M
        return _task_execution_context;
658
2.35M
    }
659
660
    Status register_producer_runtime_filter(
661
            const doris::TRuntimeFilterDesc& desc,
662
            std::shared_ptr<RuntimeFilterProducer>* producer_filter);
663
664
    Status register_consumer_runtime_filter(
665
            const doris::TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
666
            std::shared_ptr<RuntimeFilterConsumer>* consumer_filter);
667
668
    bool is_nereids() const;
669
670
8.13M
    bool enable_spill() const {
671
8.13M
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
672
8.13M
               (_query_options.__isset.enable_spill && _query_options.enable_spill);
673
8.13M
    }
674
675
15.8M
    bool enable_force_spill() const {
676
15.8M
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
677
15.8M
    }
678
679
5.23k
    int64_t spill_min_revocable_mem() const {
680
5.23k
        if (_query_options.__isset.min_revocable_mem) {
681
5.23k
            return std::max(_query_options.min_revocable_mem, (int64_t)1 << 20);
682
5.23k
        }
683
0
        return 32 << 20;
684
5.23k
    }
685
686
157
    int spill_aggregation_partition_count() const {
687
157
        if (_query_options.__isset.spill_aggregation_partition_count) {
688
157
            return std::min(std::max(2, _query_options.spill_aggregation_partition_count), 32);
689
157
        }
690
0
        return 8;
691
157
    }
692
693
9
    int spill_hash_join_partition_count() const {
694
9
        if (_query_options.__isset.spill_hash_join_partition_count) {
695
9
            return std::min(std::max(2, _query_options.spill_hash_join_partition_count), 32);
696
9
        }
697
0
        return 8;
698
9
    }
699
700
82
    int spill_repartition_max_depth() const {
701
82
        if (_query_options.__isset.spill_repartition_max_depth) {
702
            // Clamp to a reasonable range: [1, 128]
703
82
            return std::max(1, std::min(_query_options.spill_repartition_max_depth, 128));
704
82
        }
705
0
        return 8;
706
82
    }
707
708
80
    int64_t spill_buffer_size_bytes() const {
709
        // clamp to [1MB, 256MB]
710
80
        constexpr int64_t kMin = 1LL * 1024 * 1024;
711
80
        constexpr int64_t kMax = 256LL * 1024 * 1024;
712
80
        if (_query_options.__isset.spill_buffer_size_bytes) {
713
80
            int64_t v = _query_options.spill_buffer_size_bytes;
714
80
            if (v < kMin) return kMin;
715
80
            if (v > kMax) return kMax;
716
80
            return v;
717
80
        }
718
0
        return 8LL * 1024 * 1024;
719
80
    }
720
721
    // Per-sink memory limits: after spill is triggered, the sink proactively
722
    // spills when its revocable memory exceeds this threshold.
723
    // Clamped to [1MB, 4GB], default 64MB.
724
2
    int64_t spill_join_build_sink_mem_limit_bytes() const {
725
2
        constexpr int64_t kMin = 1LL * 1024 * 1024;
726
2
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
727
2
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
728
2
        if (_query_options.__isset.spill_join_build_sink_mem_limit_bytes) {
729
2
            int64_t v = _query_options.spill_join_build_sink_mem_limit_bytes;
730
2
            return std::min(std::max(v, kMin), kMax);
731
2
        }
732
0
        return kDefault;
733
2
    }
734
735
16
    int64_t spill_aggregation_sink_mem_limit_bytes() const {
736
16
        constexpr int64_t kMin = 1LL * 1024 * 1024;
737
16
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
738
16
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
739
16
        if (_query_options.__isset.spill_aggregation_sink_mem_limit_bytes) {
740
16
            int64_t v = _query_options.spill_aggregation_sink_mem_limit_bytes;
741
16
            return std::min(std::max(v, kMin), kMax);
742
16
        }
743
0
        return kDefault;
744
16
    }
745
746
8
    int64_t spill_sort_sink_mem_limit_bytes() const {
747
8
        constexpr int64_t kMin = 1LL * 1024 * 1024;
748
8
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
749
8
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
750
8
        if (_query_options.__isset.spill_sort_sink_mem_limit_bytes) {
751
8
            int64_t v = _query_options.spill_sort_sink_mem_limit_bytes;
752
8
            return std::min(std::max(v, kMin), kMax);
753
8
        }
754
0
        return kDefault;
755
8
    }
756
757
10
    int64_t spill_sort_merge_mem_limit_bytes() const {
758
10
        constexpr int64_t kMin = 1LL * 1024 * 1024;
759
10
        constexpr int64_t kMax = 4LL * 1024 * 1024 * 1024;
760
10
        constexpr int64_t kDefault = 64LL * 1024 * 1024;
761
10
        if (_query_options.__isset.spill_sort_merge_mem_limit_bytes) {
762
10
            int64_t v = _query_options.spill_sort_merge_mem_limit_bytes;
763
10
            return std::min(std::max(v, kMin), kMax);
764
10
        }
765
0
        return kDefault;
766
10
    }
767
768
0
    int64_t low_memory_mode_buffer_limit() const {
769
0
        if (_query_options.__isset.low_memory_mode_buffer_limit) {
770
0
            return std::max(_query_options.low_memory_mode_buffer_limit, (int64_t)1);
771
0
        }
772
0
        return 32L * 1024 * 1024;
773
0
    }
774
775
0
    int spill_revocable_memory_high_watermark_percent() const {
776
0
        if (_query_options.__isset.revocable_memory_high_watermark_percent) {
777
0
            return _query_options.revocable_memory_high_watermark_percent;
778
0
        }
779
0
        return -1;
780
0
    }
781
782
205k
    MOCK_FUNCTION bool enable_shared_exchange_sink_buffer() const {
783
205k
        return _query_options.__isset.enable_shared_exchange_sink_buffer &&
784
205k
               _query_options.enable_shared_exchange_sink_buffer;
785
205k
    }
786
787
12.1M
    size_t minimum_operator_memory_required_bytes() const {
788
12.1M
        if (_query_options.__isset.minimum_operator_memory_required_kb) {
789
12.1M
            return _query_options.minimum_operator_memory_required_kb * 1024;
790
18.4E
        } else {
791
            // refer other database
792
18.4E
            return 4 * 1024 * 1024;
793
18.4E
        }
794
12.1M
    }
795
796
261k
    MOCK_FUNCTION bool enable_use_hybrid_sort() const {
797
261k
        return _query_options.__isset.enable_use_hybrid_sort &&
798
261k
               _query_options.enable_use_hybrid_sort;
799
261k
    }
800
801
2.16M
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
802
803
95
    int max_operator_id() const { return _max_operator_id; }
804
805
2.09M
    void set_task_id(int id) { _task_id = id; }
806
807
290
    int task_id() const { return _task_id; }
808
809
2.09M
    void set_task_num(int task_num) { _task_num = task_num; }
810
811
7.32k
    int task_num() const { return _task_num; }
812
813
328k
    int profile_level() const { return _profile_level; }
814
815
52.4k
    std::shared_ptr<IdFileMap>& get_id_file_map() { return _id_file_map; }
816
817
    void set_id_file_map();
818
1.18M
    VectorSearchUserParams get_vector_search_params() const {
819
1.18M
        VectorSearchUserParams params;
820
1.18M
        params.hnsw_ef_search = _query_options.hnsw_ef_search;
821
1.18M
        params.hnsw_check_relative_distance = _query_options.hnsw_check_relative_distance;
822
1.18M
        params.hnsw_bounded_queue = _query_options.hnsw_bounded_queue;
823
1.18M
        params.ivf_nprobe = _query_options.ivf_nprobe;
824
1.18M
        return params;
825
1.18M
    }
826
827
39.1k
    bool runtime_filter_wait_infinitely() const {
828
39.1k
        return _query_options.__isset.runtime_filter_wait_infinitely &&
829
39.2k
               _query_options.runtime_filter_wait_infinitely;
830
39.1k
    }
831
832
    const std::set<int>& get_deregister_runtime_filter() const;
833
834
    void merge_register_runtime_filter(const std::set<int>& runtime_filter_ids);
835
836
private:
837
    Status create_error_log_file();
838
839
    static const int DEFAULT_BATCH_SIZE = 4062;
840
841
    std::shared_ptr<MemTrackerLimiter> _query_mem_tracker = nullptr;
842
843
    // Could not find a better way to record if the weak ptr is inited, use a bool to record
844
    // it. In some unit test cases, the runtime state's task ctx is not inited, then the test
845
    // hang, it is very hard to debug.
846
    bool _task_execution_context_inited = false;
847
    // Hold execution context for other threads
848
    std::weak_ptr<TaskExecutionContext> _task_execution_context;
849
850
    // put runtime state before _obj_pool, so that it will be deconstructed after
851
    // _obj_pool. Because some of object in _obj_pool will use profile when deconstructing.
852
    RuntimeProfile _profile;
853
    RuntimeProfile _load_channel_profile;
854
    // Why 2?
855
    // During cluster upgrade, fe will not pass profile_level to be, so we need to set it to 2
856
    // to make sure user can see all profile counters like before.
857
    int _profile_level = 2;
858
859
    const DescriptorTbl* _desc_tbl = nullptr;
860
    std::shared_ptr<ObjectPool> _obj_pool;
861
862
    // owned by PipelineFragmentContext
863
    RuntimeFilterMgr* _runtime_filter_mgr = nullptr;
864
865
    // Lock protecting _error_log and _unreported_error_idx
866
    std::mutex _error_log_lock;
867
868
    // Logs error messages.
869
    std::vector<std::string> _error_log;
870
871
    // _error_log[_unreported_error_idx+] has been not reported to the coordinator.
872
    int _unreported_error_idx;
873
874
    // Username of user that is executing the query to which this RuntimeState belongs.
875
    std::string _user;
876
877
    //Query-global timestamp_ms
878
    int64_t _timestamp_ms;
879
    int32_t _nano_seconds;
880
    std::string _timezone;
881
    cctz::time_zone _timezone_obj;
882
    std::string _lc_time_names;
883
884
    TUniqueId _query_id;
885
    // fragment id for each TPipelineFragmentParams
886
    int32_t _fragment_id;
887
    TUniqueId _fragment_instance_id;
888
    TQueryOptions _query_options;
889
    ExecEnv* _exec_env = nullptr;
890
891
    AtomicStatus _exec_status;
892
893
    int _per_fragment_instance_idx;
894
    int _num_per_fragment_instances = 0;
895
    int _load_stream_per_node = 0;
896
    int _total_load_streams = 0;
897
    int _num_local_sink = 0;
898
899
    // The backend id on which this fragment instance runs
900
    int64_t _backend_id = -1;
901
902
    // used as send id
903
    int _be_number;
904
905
    // put here to collect files??
906
    std::vector<std::string> _output_files;
907
    std::atomic<int64_t> _num_rows_load_total;      // total rows read from source
908
    std::atomic<int64_t> _num_rows_load_filtered;   // unqualified rows
909
    std::atomic<int64_t> _num_rows_load_unselected; // rows filtered by predicates
910
    std::atomic<int64_t> _num_rows_filtered_in_strict_mode_partial_update;
911
    std::atomic<int64_t> _num_print_error_rows;
912
913
    std::atomic<int64_t> _num_bytes_load_total; // total bytes read from source
914
    std::atomic<int64_t> _num_finished_scan_range;
915
916
    std::vector<std::string> _export_output_files;
917
    std::string _import_label;
918
    std::string _db_name;
919
    std::string _load_dir;
920
    int64_t _load_job_id;
921
    int64_t _wal_id = -1;
922
    size_t _content_length = 0;
923
924
    // mini load
925
    std::string _error_log_file_path;
926
    std::unique_ptr<std::ofstream> _error_log_file; // error file path, absolute path
927
    std::string _first_error_msg = "";
928
    mutable std::mutex _tablet_infos_mutex;
929
    std::vector<TTabletCommitInfo> _tablet_commit_infos;
930
    std::vector<TErrorTabletInfo> _error_tablet_infos;
931
    int _max_operator_id = 0;
932
    int _task_id = -1;
933
    int _task_num = 0;
934
935
    mutable std::mutex _hive_partition_updates_mutex;
936
    std::vector<THivePartitionUpdate> _hive_partition_updates;
937
938
    mutable std::mutex _iceberg_commit_datas_mutex;
939
    std::vector<TIcebergCommitData> _iceberg_commit_datas;
940
941
    mutable std::mutex _mc_commit_datas_mutex;
942
    std::vector<TMCCommitData> _mc_commit_datas;
943
944
    std::vector<std::unique_ptr<doris::PipelineXLocalStateBase>> _op_id_to_local_state;
945
946
    std::unique_ptr<doris::PipelineXSinkLocalStateBase> _sink_local_state;
947
948
    QueryContext* _query_ctx = nullptr;
949
950
    // true if max_filter_ratio is 0
951
    bool _load_zero_tolerance = false;
952
953
    // only to lock _pipeline_id_to_profile
954
    std::shared_mutex _pipeline_profile_lock;
955
    std::vector<std::shared_ptr<RuntimeProfile>> _pipeline_id_to_profile;
956
957
    // save error log to s3
958
    std::shared_ptr<io::S3FileSystem> _s3_error_fs;
959
    // error file path on s3, ${bucket}/${prefix}/error_log/${label}_${fragment_instance_id}
960
    std::string _s3_error_log_file_path;
961
    std::mutex _s3_error_log_file_lock;
962
963
    // used for encoding the global lazy materialize
964
    std::shared_ptr<IdFileMap> _id_file_map = nullptr;
965
966
    std::set<int> _registered_runtime_filter_ids;
967
};
968
969
#define RETURN_IF_CANCELLED(state)               \
970
1.12M
    do {                                         \
971
1.12M
        if (UNLIKELY((state)->is_cancelled())) { \
972
1
            return (state)->cancel_reason();     \
973
1
        }                                        \
974
1.12M
    } while (false)
975
976
} // namespace doris