Coverage Report

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