Coverage Report

Created: 2025-03-13 11:28

/root/doris/be/src/runtime/runtime_state.h
Line
Count
Source (jump to first uncovered line)
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 <fstream>
30
#include <functional>
31
#include <memory>
32
#include <mutex>
33
#include <string>
34
#include <utility>
35
#include <vector>
36
37
#include "cctz/time_zone.h"
38
#include "common/compiler_util.h" // IWYU pragma: keep
39
#include "common/factory_creator.h"
40
#include "common/status.h"
41
#include "gutil/integral_types.h"
42
#include "runtime/task_execution_context.h"
43
#include "util/debug_util.h"
44
#include "util/runtime_profile.h"
45
#include "vec/columns/columns_number.h"
46
47
namespace doris {
48
class IRuntimeFilter;
49
50
namespace pipeline {
51
class PipelineXLocalStateBase;
52
class PipelineXSinkLocalStateBase;
53
class PipelineXFragmentContext;
54
class PipelineXTask;
55
} // namespace pipeline
56
57
class DescriptorTbl;
58
class ObjectPool;
59
class ExecEnv;
60
class RuntimeFilterMgr;
61
class MemTrackerLimiter;
62
class QueryContext;
63
64
// A collection of items that are part of the global state of a
65
// query and shared across all execution nodes of that query.
66
class RuntimeState {
67
    ENABLE_FACTORY_CREATOR(RuntimeState);
68
69
public:
70
    RuntimeState(const TPlanFragmentExecParams& fragment_exec_params,
71
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
72
                 ExecEnv* exec_env, QueryContext* ctx,
73
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker = nullptr);
74
75
    RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id, int32 fragment_id,
76
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
77
                 ExecEnv* exec_env, QueryContext* ctx);
78
79
    // for only use in pipelineX
80
    RuntimeState(pipeline::PipelineXFragmentContext*, const TUniqueId& instance_id,
81
                 const TUniqueId& query_id, int32 fragment_id, const TQueryOptions& query_options,
82
                 const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx);
83
84
    // Used by pipelineX. This runtime state is only used for setup.
85
    RuntimeState(const TUniqueId& query_id, int32 fragment_id, const TQueryOptions& query_options,
86
                 const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx);
87
88
    // RuntimeState for executing expr in fe-support.
89
    RuntimeState(const TQueryGlobals& query_globals);
90
91
    // for job task only
92
    RuntimeState();
93
94
    // Empty d'tor to avoid issues with unique_ptr.
95
    ~RuntimeState();
96
97
    // Set per-query state.
98
    Status init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
99
                const TQueryGlobals& query_globals, ExecEnv* exec_env);
100
101
    // for ut and non-query.
102
0
    void set_exec_env(ExecEnv* exec_env) { _exec_env = exec_env; }
103
104
    // for ut and non-query.
105
    void init_mem_trackers(const std::string& name = "ut", const TUniqueId& id = TUniqueId());
106
107
46
    const TQueryOptions& query_options() const { return _query_options; }
108
0
    int64_t scan_queue_mem_limit() const {
109
0
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
110
0
                                                           : _query_options.mem_limit / 20;
111
0
    }
112
0
    int64_t query_mem_limit() const {
113
0
        if (_query_options.__isset.mem_limit && (_query_options.mem_limit > 0)) {
114
0
            return _query_options.mem_limit;
115
0
        }
116
0
        return 0;
117
0
    }
118
119
10
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
120
121
29
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
122
13
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
123
25
    int batch_size() const { return _query_options.batch_size; }
124
0
    int wait_full_block_schedule_times() const {
125
0
        return _query_options.wait_full_block_schedule_times;
126
0
    }
127
0
    bool abort_on_error() const { return _query_options.abort_on_error; }
128
0
    bool abort_on_default_limit_exceeded() const {
129
0
        return _query_options.abort_on_default_limit_exceeded;
130
0
    }
131
0
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
132
0
    int max_errors() const { return _query_options.max_errors; }
133
20
    int execution_timeout() const {
134
20
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
135
20
                                                        : _query_options.query_timeout;
136
20
    }
137
0
    int max_io_buffers() const { return _query_options.max_io_buffers; }
138
0
    int num_scanner_threads() const {
139
0
        return _query_options.__isset.num_scanner_threads ? _query_options.num_scanner_threads : 0;
140
0
    }
141
0
    double scanner_scale_up_ratio() const {
142
0
        return _query_options.__isset.scanner_scale_up_ratio ? _query_options.scanner_scale_up_ratio
143
0
                                                             : 0;
144
0
    }
145
3
    TQueryType::type query_type() const { return _query_options.query_type; }
146
5
    int64_t timestamp_ms() const { return _timestamp_ms; }
147
5
    int32_t nano_seconds() const { return _nano_seconds; }
148
    // if possible, use timezone_obj() rather than timezone()
149
5
    const std::string& timezone() const { return _timezone; }
150
2
    const cctz::time_zone& timezone_obj() const { return _timezone_obj; }
151
0
    const std::string& user() const { return _user; }
152
5
    const TUniqueId& query_id() const { return _query_id; }
153
2
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
154
    // should only be called in pipeline engine
155
0
    int32_t fragment_id() const { return _fragment_id; }
156
23
    ExecEnv* exec_env() { return _exec_env; }
157
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const;
158
159
    // Returns runtime state profile
160
9
    RuntimeProfile* runtime_profile() { return &_profile; }
161
0
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
162
163
0
    bool enable_function_pushdown() const {
164
0
        return _query_options.__isset.enable_function_pushdown &&
165
0
               _query_options.enable_function_pushdown;
166
0
    }
167
168
2
    bool check_overflow_for_decimal() const {
169
2
        return _query_options.__isset.check_overflow_for_decimal &&
170
2
               _query_options.check_overflow_for_decimal;
171
2
    }
172
173
13
    bool enable_decimal256() const {
174
13
        return _query_options.__isset.enable_decimal256 && _query_options.enable_decimal256;
175
13
    }
176
177
2
    bool new_is_ip_address_in_range() const {
178
2
        return _query_options.__isset.new_is_ip_address_in_range &&
179
2
               _query_options.new_is_ip_address_in_range;
180
2
    }
181
182
0
    bool enable_common_expr_pushdown() const {
183
0
        return _query_options.__isset.enable_common_expr_pushdown &&
184
0
               _query_options.enable_common_expr_pushdown;
185
0
    }
186
187
0
    bool mysql_row_binary_format() const {
188
0
        return _query_options.__isset.mysql_row_binary_format &&
189
0
               _query_options.mysql_row_binary_format;
190
0
    }
191
    Status query_status();
192
193
    // Appends error to the _error_log if there is space
194
    bool log_error(const std::string& error);
195
196
    // Returns true if the error log has not reached _max_errors.
197
0
    bool log_has_space() {
198
0
        std::lock_guard<std::mutex> l(_error_log_lock);
199
0
        return _error_log.size() < _query_options.max_errors;
200
0
    }
201
202
    // Append all _error_log[_unreported_error_idx+] to new_errors and set
203
    // _unreported_error_idx to _errors_log.size()
204
    void get_unreported_errors(std::vector<std::string>* new_errors);
205
206
    [[nodiscard]] bool is_cancelled() const;
207
    std::string cancel_reason() const;
208
0
    int codegen_level() const { return _query_options.codegen_level; }
209
0
    void set_is_cancelled(std::string msg) {
210
0
        if (!_is_cancelled.exchange(true)) {
211
0
            _cancel_reason = msg;
212
            // Create a error status, so that we could print error stack, and
213
            // we could know which path call cancel.
214
0
            LOG(WARNING) << "Task is cancelled, instance: "
215
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
216
0
                         << ", st = " << Status::Error<ErrorCode::CANCELLED>(msg);
217
0
        } else {
218
0
            LOG(WARNING) << "Task is already cancelled, instance: "
219
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
220
0
                         << ", original cancel msg: " << _cancel_reason
221
0
                         << ", new cancel msg: " << Status::Error<ErrorCode::CANCELLED>(msg);
222
0
        }
223
0
    }
224
225
0
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
226
0
    int64_t backend_id() const { return _backend_id; }
227
228
0
    void set_be_number(int be_number) { _be_number = be_number; }
229
0
    int be_number(void) const { return _be_number; }
230
231
    // Sets _process_status with err_msg if no error has been set yet.
232
0
    void set_process_status(const std::string& err_msg) {
233
0
        std::lock_guard<std::mutex> l(_process_status_lock);
234
0
        if (!_process_status.ok()) {
235
0
            return;
236
0
        }
237
0
        _process_status = Status::InternalError(err_msg);
238
0
    }
239
240
0
    void set_process_status(const Status& status) {
241
0
        if (status.ok()) {
242
0
            return;
243
0
        }
244
0
        std::lock_guard<std::mutex> l(_process_status_lock);
245
0
        if (!_process_status.ok()) {
246
0
            return;
247
0
        }
248
0
        _process_status = status;
249
0
    }
250
251
    // Sets _process_status to MEM_LIMIT_EXCEEDED.
252
    // Subsequent calls to this will be no-ops. Returns _process_status.
253
    // If 'msg' is non-nullptr, it will be appended to query_status_ in addition to the
254
    // generic "Memory limit exceeded" error.
255
    Status set_mem_limit_exceeded(const std::string& msg = "Memory limit exceeded");
256
257
0
    std::vector<std::string>& output_files() { return _output_files; }
258
259
0
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
260
261
0
    const std::vector<std::string>& export_output_files() const { return _export_output_files; }
262
263
0
    void add_export_output_file(const std::string& file) { _export_output_files.push_back(file); }
264
265
0
    void set_db_name(const std::string& db_name) { _db_name = db_name; }
266
267
0
    const std::string& db_name() { return _db_name; }
268
269
0
    void set_wal_id(int64_t wal_id) { _wal_id = wal_id; }
270
271
3
    int64_t wal_id() const { return _wal_id; }
272
273
0
    void set_content_length(size_t content_length) { _content_length = content_length; }
274
275
0
    size_t content_length() const { return _content_length; }
276
277
0
    const std::string& import_label() { return _import_label; }
278
279
0
    const std::string& load_dir() const { return _load_dir; }
280
281
0
    void set_load_job_id(int64_t job_id) { _load_job_id = job_id; }
282
283
5
    int64_t load_job_id() const { return _load_job_id; }
284
285
0
    const std::string get_error_log_file_path() const { return _error_log_file_path; }
286
287
    // append error msg and error line to file when loading data.
288
    // is_summary is true, means we are going to write the summary line
289
    // If we need to stop the processing, set stop_processing to true
290
    Status append_error_msg_to_file(std::function<std::string()> line,
291
                                    std::function<std::string()> error_msg, bool* stop_processing,
292
                                    bool is_summary = false);
293
294
0
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
295
296
0
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
297
298
0
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
299
300
7
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
301
302
4
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
303
304
8
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
305
8
        return _num_rows_filtered_in_strict_mode_partial_update;
306
8
    }
307
308
0
    int64_t num_rows_load_success() {
309
0
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
310
0
    }
311
312
7
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
313
314
4
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
315
316
7
    void update_num_bytes_load_total(int64_t bytes_load) {
317
7
        _num_bytes_load_total.fetch_add(bytes_load);
318
7
    }
319
320
2
    void update_num_finished_scan_range(int64_t finished_range) {
321
2
        _num_finished_scan_range.fetch_add(finished_range);
322
2
    }
323
324
4
    void update_num_rows_load_filtered(int64_t num_rows) {
325
4
        _num_rows_load_filtered.fetch_add(num_rows);
326
4
    }
327
328
4
    void update_num_rows_load_unselected(int64_t num_rows) {
329
4
        _num_rows_load_unselected.fetch_add(num_rows);
330
4
    }
331
332
0
    void set_num_rows_filtered_in_strict_mode_partial_update(int64_t num_rows) {
333
0
        _num_rows_filtered_in_strict_mode_partial_update = num_rows;
334
0
    }
335
336
0
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
337
338
5
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
339
340
0
    void set_num_per_fragment_instances(int num_instances) {
341
0
        _num_per_fragment_instances = num_instances;
342
0
    }
343
344
5
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
345
346
0
    void set_load_stream_per_node(int load_stream_per_node) {
347
0
        _load_stream_per_node = load_stream_per_node;
348
0
    }
349
350
0
    int load_stream_per_node() const { return _load_stream_per_node; }
351
352
0
    void set_total_load_streams(int total_load_streams) {
353
0
        _total_load_streams = total_load_streams;
354
0
    }
355
356
0
    int total_load_streams() const { return _total_load_streams; }
357
358
0
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
359
360
0
    int num_local_sink() const { return _num_local_sink; }
361
362
0
    bool disable_stream_preaggregations() const {
363
0
        return _query_options.disable_stream_preaggregations;
364
0
    }
365
366
0
    bool enable_spill() const { return _query_options.enable_spilling; }
367
368
0
    int32_t runtime_filter_wait_time_ms() const {
369
0
        return _query_options.runtime_filter_wait_time_ms;
370
0
    }
371
372
0
    bool runtime_filter_wait_infinitely() const {
373
0
        return _query_options.__isset.runtime_filter_wait_infinitely &&
374
0
               _query_options.runtime_filter_wait_infinitely;
375
0
    }
376
377
0
    int32_t runtime_filter_max_in_num() const { return _query_options.runtime_filter_max_in_num; }
378
379
12
    int be_exec_version() const {
380
12
        if (!_query_options.__isset.be_exec_version) {
381
0
            return 0;
382
0
        }
383
12
        return _query_options.be_exec_version;
384
12
    }
385
12
    bool enable_pipeline_exec() const {
386
12
        return _query_options.__isset.enable_pipeline_engine &&
387
12
               _query_options.enable_pipeline_engine;
388
12
    }
389
0
    bool enable_pipeline_x_exec() const {
390
0
        return _query_options.__isset.enable_pipeline_x_engine &&
391
0
               _query_options.enable_pipeline_x_engine;
392
0
    }
393
0
    bool enable_local_shuffle() const {
394
0
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle;
395
0
    }
396
397
0
    bool trim_tailing_spaces_for_external_table_query() const {
398
0
        return _query_options.trim_tailing_spaces_for_external_table_query;
399
0
    }
400
401
0
    bool return_object_data_as_binary() const {
402
0
        return _query_options.return_object_data_as_binary;
403
0
    }
404
405
0
    bool enable_exchange_node_parallel_merge() const {
406
0
        return _query_options.enable_enable_exchange_node_parallel_merge;
407
0
    }
408
409
10
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
410
10
        if (_query_options.__isset.fragment_transmission_compression_codec) {
411
0
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
412
0
                return segment_v2::CompressionTypePB::LZ4;
413
0
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
414
0
                return segment_v2::CompressionTypePB::SNAPPY;
415
0
            } else {
416
0
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
417
0
            }
418
0
        }
419
10
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
420
10
    }
421
422
0
    bool skip_storage_engine_merge() const {
423
0
        return _query_options.__isset.skip_storage_engine_merge &&
424
0
               _query_options.skip_storage_engine_merge;
425
0
    }
426
427
0
    bool skip_delete_predicate() const {
428
0
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
429
0
    }
430
431
0
    bool skip_delete_bitmap() const {
432
0
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
433
0
    }
434
435
0
    bool skip_missing_version() const {
436
0
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
437
0
    }
438
439
0
    int64_t data_queue_max_blocks() const {
440
0
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
441
0
                                                            : 1;
442
0
    }
443
444
    bool enable_page_cache() const;
445
446
0
    int partitioned_hash_join_rows_threshold() const {
447
0
        if (!_query_options.__isset.partitioned_hash_join_rows_threshold) {
448
0
            return 0;
449
0
        }
450
0
        return _query_options.partitioned_hash_join_rows_threshold;
451
0
    }
452
453
0
    int partitioned_hash_agg_rows_threshold() const {
454
0
        if (!_query_options.__isset.partitioned_hash_agg_rows_threshold) {
455
0
            return 0;
456
0
        }
457
0
        return _query_options.partitioned_hash_agg_rows_threshold;
458
0
    }
459
460
0
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
461
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
462
0
        return _tablet_commit_infos;
463
0
    }
464
465
8
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
466
8
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
467
8
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
468
8
                                    std::make_move_iterator(commit_infos.begin()),
469
8
                                    std::make_move_iterator(commit_infos.end()));
470
8
    }
471
472
0
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
473
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
474
0
        return _error_tablet_infos;
475
0
    }
476
477
8
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
478
8
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
479
8
        _error_tablet_infos.insert(_error_tablet_infos.end(),
480
8
                                   std::make_move_iterator(tablet_infos.begin()),
481
8
                                   std::make_move_iterator(tablet_infos.end()));
482
8
    }
483
484
0
    std::vector<THivePartitionUpdate>& hive_partition_updates() { return _hive_partition_updates; }
485
486
0
    std::vector<TIcebergCommitData>& iceberg_commit_datas() { return _iceberg_commit_datas; }
487
488
    // local runtime filter mgr, the runtime filter do not have remote target or
489
    // not need local merge should regist here. the instance exec finish, the local
490
    // runtime filter mgr can release the memory of local runtime filter
491
0
    RuntimeFilterMgr* local_runtime_filter_mgr() {
492
0
        if (_pipeline_x_runtime_filter_mgr) {
493
0
            return _pipeline_x_runtime_filter_mgr;
494
0
        } else {
495
0
            return _runtime_filter_mgr.get();
496
0
        }
497
0
    }
498
499
    RuntimeFilterMgr* global_runtime_filter_mgr();
500
501
0
    void set_pipeline_x_runtime_filter_mgr(RuntimeFilterMgr* pipeline_x_runtime_filter_mgr) {
502
0
        _pipeline_x_runtime_filter_mgr = pipeline_x_runtime_filter_mgr;
503
0
    }
504
505
5
    QueryContext* get_query_ctx() { return _query_ctx; }
506
507
0
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
508
0
        _query_mem_tracker = tracker;
509
0
    }
510
511
9
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
512
513
15
    bool enable_profile() const {
514
15
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
515
15
    }
516
517
0
    bool enable_verbose_profile() const {
518
0
        return enable_profile() && _query_options.__isset.enable_verbose_profile &&
519
0
               _query_options.enable_verbose_profile;
520
0
    }
521
522
0
    int rpc_verbose_profile_max_instance_count() const {
523
0
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
524
0
                       ? _query_options.rpc_verbose_profile_max_instance_count
525
0
                       : 0;
526
0
    }
527
528
0
    bool enable_scan_node_run_serial() const {
529
0
        return _query_options.__isset.enable_scan_node_run_serial &&
530
0
               _query_options.enable_scan_node_run_serial;
531
0
    }
532
533
0
    bool enable_share_hash_table_for_broadcast_join() const {
534
0
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
535
0
               _query_options.enable_share_hash_table_for_broadcast_join;
536
0
    }
537
538
0
    bool enable_hash_join_early_start_probe() const {
539
0
        return _query_options.__isset.enable_hash_join_early_start_probe &&
540
0
               _query_options.enable_hash_join_early_start_probe;
541
0
    }
542
543
0
    bool enable_parallel_scan() const {
544
0
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
545
0
    }
546
547
0
    bool is_read_csv_empty_line_as_null() const {
548
0
        return _query_options.__isset.read_csv_empty_line_as_null &&
549
0
               _query_options.read_csv_empty_line_as_null;
550
0
    }
551
552
0
    int parallel_scan_max_scanners_count() const {
553
0
        return _query_options.__isset.parallel_scan_max_scanners_count
554
0
                       ? _query_options.parallel_scan_max_scanners_count
555
0
                       : 0;
556
0
    }
557
558
0
    int partition_topn_max_partitions() const {
559
0
        return _query_options.__isset.partition_topn_max_partitions
560
0
                       ? _query_options.partition_topn_max_partitions
561
0
                       : 1024;
562
0
    }
563
564
0
    int partition_topn_per_partition_rows() const {
565
0
        return _query_options.__isset.partition_topn_pre_partition_rows
566
0
                       ? _query_options.partition_topn_pre_partition_rows
567
0
                       : 1000;
568
0
    }
569
570
0
    int64_t parallel_scan_min_rows_per_scanner() const {
571
0
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
572
0
                       ? _query_options.parallel_scan_min_rows_per_scanner
573
0
                       : 0;
574
0
    }
575
576
12
    int repeat_max_num() const {
577
#ifndef BE_TEST
578
        if (!_query_options.__isset.repeat_max_num) {
579
            return 10000;
580
        }
581
        return _query_options.repeat_max_num;
582
#else
583
12
        return 10;
584
12
#endif
585
12
    }
586
587
0
    int64_t external_sort_bytes_threshold() const {
588
0
        if (_query_options.__isset.external_sort_bytes_threshold) {
589
0
            return _query_options.external_sort_bytes_threshold;
590
0
        }
591
0
        return 0;
592
0
    }
593
594
0
    void set_be_exec_version(int32_t version) noexcept { _query_options.be_exec_version = version; }
595
596
0
    int64_t external_agg_bytes_threshold() const {
597
0
        return _query_options.__isset.external_agg_bytes_threshold
598
0
                       ? _query_options.external_agg_bytes_threshold
599
0
                       : 0;
600
0
    }
601
602
0
    void set_task(pipeline::PipelineXTask* task) { _task = task; }
603
604
0
    pipeline::PipelineXTask* get_task() const { return _task; }
605
606
0
    inline bool enable_delete_sub_pred_v2() const {
607
0
        return _query_options.__isset.enable_delete_sub_predicate_v2 &&
608
0
               _query_options.enable_delete_sub_predicate_v2;
609
0
    }
610
611
    using LocalState = doris::pipeline::PipelineXLocalStateBase;
612
    using SinkLocalState = doris::pipeline::PipelineXSinkLocalStateBase;
613
    // get result can return an error message, and we will only call it during the prepare.
614
    void emplace_local_state(int id, std::unique_ptr<LocalState> state);
615
616
    LocalState* get_local_state(int id);
617
    Result<LocalState*> get_local_state_result(int id);
618
619
    void emplace_sink_local_state(int id, std::unique_ptr<SinkLocalState> state);
620
621
    SinkLocalState* get_sink_local_state();
622
623
    Result<SinkLocalState*> get_sink_local_state_result();
624
625
    void resize_op_id_to_local_state(int operator_size);
626
627
0
    auto& pipeline_id_to_profile() { return _pipeline_id_to_profile; }
628
629
5
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
630
5
        _task_execution_context_inited = true;
631
5
        _task_execution_context = context;
632
5
    }
633
634
15
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
635
15
        CHECK(_task_execution_context_inited)
636
0
                << "_task_execution_context_inited == false, the ctx is not inited";
637
15
        return _task_execution_context;
638
15
    }
639
640
    Status register_producer_runtime_filter(const doris::TRuntimeFilterDesc& desc,
641
                                            bool need_local_merge,
642
                                            doris::IRuntimeFilter** producer_filter,
643
                                            bool build_bf_exactly);
644
645
    Status register_consumer_runtime_filter(const doris::TRuntimeFilterDesc& desc,
646
                                            bool need_local_merge, int node_id,
647
                                            doris::IRuntimeFilter** producer_filter);
648
    bool is_nereids() const;
649
650
0
    bool enable_join_spill() const {
651
0
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
652
0
               (_query_options.__isset.enable_join_spill && _query_options.enable_join_spill);
653
0
    }
654
655
0
    bool enable_sort_spill() const {
656
0
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
657
0
               (_query_options.__isset.enable_sort_spill && _query_options.enable_sort_spill);
658
0
    }
659
660
0
    bool enable_agg_spill() const {
661
0
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
662
0
               (_query_options.__isset.enable_agg_spill && _query_options.enable_agg_spill);
663
0
    }
664
665
0
    bool enable_force_spill() const {
666
0
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
667
0
    }
668
669
0
    bool fuzzy_disable_runtime_filter_in_be() const {
670
0
        return _query_options.__isset.fuzzy_disable_runtime_filter_in_be &&
671
0
               _query_options.fuzzy_disable_runtime_filter_in_be;
672
0
    }
673
674
0
    int64_t min_revocable_mem() const {
675
0
        if (_query_options.__isset.min_revocable_mem) {
676
0
            return _query_options.min_revocable_mem;
677
0
        }
678
0
        return 0;
679
0
    }
680
681
0
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
682
683
0
    int max_operator_id() const { return _max_operator_id; }
684
685
0
    void set_task_id(int id) { _task_id = id; }
686
687
0
    int task_id() const { return _task_id; }
688
689
0
    void set_task_num(int task_num) { _task_num = task_num; }
690
691
0
    int task_num() const { return _task_num; }
692
693
0
    vectorized::ColumnInt64* partial_update_auto_inc_column() {
694
0
        return _partial_update_auto_inc_column;
695
0
    };
696
697
private:
698
    Status create_error_log_file();
699
700
    static const int DEFAULT_BATCH_SIZE = 4062;
701
702
    std::shared_ptr<MemTrackerLimiter> _query_mem_tracker = nullptr;
703
704
    // Could not find a better way to record if the weak ptr is inited, use a bool to record
705
    // it. In some unit test cases, the runtime state's task ctx is not inited, then the test
706
    // hang, it is very hard to debug.
707
    bool _task_execution_context_inited = false;
708
    // Hold execution context for other threads
709
    std::weak_ptr<TaskExecutionContext> _task_execution_context;
710
711
    // put runtime state before _obj_pool, so that it will be deconstructed after
712
    // _obj_pool. Because some of object in _obj_pool will use profile when deconstructing.
713
    RuntimeProfile _profile;
714
    RuntimeProfile _load_channel_profile;
715
716
    const DescriptorTbl* _desc_tbl = nullptr;
717
    std::shared_ptr<ObjectPool> _obj_pool;
718
719
    // runtime filter
720
    std::unique_ptr<RuntimeFilterMgr> _runtime_filter_mgr;
721
722
    // owned by PipelineXFragmentContext
723
    RuntimeFilterMgr* _pipeline_x_runtime_filter_mgr = nullptr;
724
725
    // Data stream receivers created by a plan fragment are gathered here to make sure
726
    // they are destroyed before _obj_pool (class members are destroyed in reverse order).
727
    // Receivers depend on the descriptor table and we need to guarantee that their control
728
    // blocks are removed from the data stream manager before the objects in the
729
    // descriptor table are destroyed.
730
    std::unique_ptr<ObjectPool> _data_stream_recvrs_pool;
731
732
    // Lock protecting _error_log and _unreported_error_idx
733
    std::mutex _error_log_lock;
734
735
    // Logs error messages.
736
    std::vector<std::string> _error_log;
737
738
    // _error_log[_unreported_error_idx+] has been not reported to the coordinator.
739
    int _unreported_error_idx;
740
741
    // Username of user that is executing the query to which this RuntimeState belongs.
742
    std::string _user;
743
744
    //Query-global timestamp_ms
745
    int64_t _timestamp_ms;
746
    int32_t _nano_seconds;
747
    std::string _timezone;
748
    cctz::time_zone _timezone_obj;
749
750
    TUniqueId _query_id;
751
    // fragment id for each TPipelineFragmentParams
752
    int32_t _fragment_id;
753
    TUniqueId _fragment_instance_id;
754
    TQueryOptions _query_options;
755
    ExecEnv* _exec_env = nullptr;
756
757
    // if true, execution should stop with a CANCELLED status
758
    std::atomic<bool> _is_cancelled;
759
    std::string _cancel_reason;
760
761
    int _per_fragment_instance_idx;
762
    int _num_per_fragment_instances = 0;
763
    int _load_stream_per_node = 0;
764
    int _total_load_streams = 0;
765
    int _num_local_sink = 0;
766
767
    // The backend id on which this fragment instance runs
768
    int64_t _backend_id = -1;
769
770
    // used as send id
771
    int _be_number;
772
773
    // Non-OK if an error has occurred and query execution should abort. Used only for
774
    // asynchronously reporting such errors (e.g., when a UDF reports an error), so this
775
    // will not necessarily be set in all error cases.
776
    std::mutex _process_status_lock;
777
    Status _process_status;
778
779
    // put here to collect files??
780
    std::vector<std::string> _output_files;
781
    std::atomic<int64_t> _num_rows_load_total;      // total rows read from source
782
    std::atomic<int64_t> _num_rows_load_filtered;   // unqualified rows
783
    std::atomic<int64_t> _num_rows_load_unselected; // rows filtered by predicates
784
    std::atomic<int64_t> _num_rows_filtered_in_strict_mode_partial_update;
785
    std::atomic<int64_t> _num_print_error_rows;
786
787
    std::atomic<int64_t> _num_bytes_load_total; // total bytes read from source
788
    std::atomic<int64_t> _num_finished_scan_range;
789
790
    std::vector<std::string> _export_output_files;
791
    std::string _import_label;
792
    std::string _db_name;
793
    std::string _load_dir;
794
    int64_t _load_job_id;
795
    int64_t _wal_id = -1;
796
    size_t _content_length = 0;
797
798
    // mini load
799
    int64_t _normal_row_number;
800
    int64_t _error_row_number;
801
    std::string _error_log_file_path;
802
    std::ofstream* _error_log_file = nullptr; // error file path, absolute path
803
    mutable std::mutex _tablet_infos_mutex;
804
    std::vector<TTabletCommitInfo> _tablet_commit_infos;
805
    std::vector<TErrorTabletInfo> _error_tablet_infos;
806
    int _max_operator_id = 0;
807
    int _task_id = -1;
808
    int _task_num = 0;
809
810
    std::vector<THivePartitionUpdate> _hive_partition_updates;
811
812
    std::vector<TIcebergCommitData> _iceberg_commit_datas;
813
814
    std::vector<std::unique_ptr<doris::pipeline::PipelineXLocalStateBase>> _op_id_to_local_state;
815
816
    std::unique_ptr<doris::pipeline::PipelineXSinkLocalStateBase> _sink_local_state;
817
818
    QueryContext* _query_ctx = nullptr;
819
820
    // true if max_filter_ratio is 0
821
    bool _load_zero_tolerance = false;
822
823
    std::vector<std::unique_ptr<RuntimeProfile>> _pipeline_id_to_profile;
824
825
    // prohibit copies
826
    RuntimeState(const RuntimeState&);
827
828
    pipeline::PipelineXTask* _task;
829
    vectorized::ColumnInt64* _partial_update_auto_inc_column;
830
};
831
832
#define RETURN_IF_CANCELLED(state)                                                    \
833
7
    do {                                                                              \
834
7
        if (UNLIKELY((state)->is_cancelled())) return Status::Cancelled("Cancelled"); \
835
7
    } while (false)
836
837
} // namespace doris