Coverage Report

Created: 2026-05-09 12:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/olap_scanner.cpp
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
18
#include "exec/scan/olap_scanner.h"
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/PlanNodes_types.h>
22
#include <gen_cpp/Types_types.h>
23
#include <glog/logging.h>
24
#include <stdlib.h>
25
#include <thrift/protocol/TDebugProtocol.h>
26
27
#include <algorithm>
28
#include <atomic>
29
#include <iterator>
30
#include <ostream>
31
#include <set>
32
33
#include "cloud/cloud_storage_engine.h"
34
#include "cloud/cloud_tablet_hotspot.h"
35
#include "cloud/config.h"
36
#include "common/config.h"
37
#include "common/consts.h"
38
#include "common/logging.h"
39
#include "common/metrics/doris_metrics.h"
40
#include "core/block/block.h"
41
#include "exec/common/variant_util.h"
42
#include "exec/operator/olap_scan_operator.h"
43
#include "exec/scan/scan_node.h"
44
#include "exprs/function_filter.h"
45
#include "exprs/vexpr.h"
46
#include "exprs/vexpr_context.h"
47
#include "io/cache/block_file_cache_profile.h"
48
#include "io/io_common.h"
49
#include "runtime/descriptors.h"
50
#include "runtime/exec_env.h"
51
#include "runtime/runtime_profile.h"
52
#include "runtime/runtime_state.h"
53
#include "service/backend_options.h"
54
#include "storage/id_manager.h"
55
#include "storage/index/inverted/inverted_index_profile.h"
56
#include "storage/iterator/block_reader.h"
57
#include "storage/olap_common.h"
58
#include "storage/olap_tuple.h"
59
#include "storage/olap_utils.h"
60
#include "storage/storage_engine.h"
61
#include "storage/tablet/tablet_schema.h"
62
#include "util/json/path_in_data.h"
63
64
namespace doris {
65
#include "common/compile_check_avoid_begin.h"
66
67
using ReadSource = TabletReadSource;
68
69
OlapScanner::OlapScanner(ScanLocalStateBase* parent, OlapScanner::Params&& params)
70
1.04M
        : Scanner(params.state, parent, params.limit, params.profile),
71
1.04M
          _key_ranges(std::move(params.key_ranges)),
72
1.04M
          _tablet_reader_params({.tablet = std::move(params.tablet),
73
1.04M
                                 .tablet_schema {},
74
1.04M
                                 .aggregation = params.aggregation,
75
1.04M
                                 .version = {0, params.version},
76
1.04M
                                 .start_key {},
77
1.04M
                                 .end_key {},
78
1.04M
                                 .predicates {},
79
1.04M
                                 .function_filters {},
80
1.04M
                                 .delete_predicates {},
81
1.04M
                                 .target_cast_type_for_variants {},
82
1.04M
                                 .all_access_paths {},
83
1.04M
                                 .predicate_access_paths {},
84
1.04M
                                 .rs_splits {},
85
1.04M
                                 .return_columns {},
86
1.04M
                                 .output_columns {},
87
1.04M
                                 .remaining_conjunct_roots {},
88
1.04M
                                 .common_expr_ctxs_push_down {},
89
1.04M
                                 .topn_filter_source_node_ids {},
90
1.04M
                                 .filter_block_conjuncts {},
91
1.04M
                                 .key_group_cluster_key_idxes {},
92
1.04M
                                 .virtual_column_exprs {},
93
1.04M
                                 .vir_cid_to_idx_in_block {},
94
1.04M
                                 .vir_col_idx_to_type {},
95
1.04M
                                 .score_runtime {},
96
1.04M
                                 .collection_statistics {},
97
1.04M
                                 .ann_topn_runtime {},
98
1.04M
                                 .condition_cache_digest = parent->get_condition_cache_digest()}) {
99
1.04M
    _tablet_reader_params.set_read_source(std::move(params.read_source),
100
1.04M
                                          _state->skip_delete_bitmap());
101
1.04M
    _has_prepared = false;
102
1.04M
    _vector_search_params = params.state->get_vector_search_params();
103
1.04M
}
104
105
static std::string read_columns_to_string(TabletSchemaSPtr tablet_schema,
106
2.97k
                                          const std::vector<uint32_t>& read_columns) {
107
    // avoid too long for one line,
108
    // it is hard to display in `show profile` stmt if one line is too long.
109
2.97k
    const int col_per_line = 10;
110
2.97k
    int i = 0;
111
2.97k
    std::string read_columns_string;
112
2.97k
    read_columns_string += "[";
113
16.2k
    for (auto it = read_columns.cbegin(); it != read_columns.cend(); it++) {
114
13.2k
        if (it != read_columns.cbegin()) {
115
10.3k
            read_columns_string += ", ";
116
10.3k
        }
117
13.2k
        read_columns_string += tablet_schema->columns().at(*it)->name();
118
13.2k
        if (i >= col_per_line) {
119
13
            read_columns_string += "\n";
120
13
            i = 0;
121
13.2k
        } else {
122
13.2k
            ++i;
123
13.2k
        }
124
13.2k
    }
125
2.97k
    read_columns_string += "]";
126
2.97k
    return read_columns_string;
127
2.97k
}
128
129
1.04M
Status OlapScanner::_prepare_impl() {
130
1.04M
    auto* local_state = static_cast<OlapScanLocalState*>(_local_state);
131
1.04M
    auto& tablet = _tablet_reader_params.tablet;
132
1.04M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
133
1.04M
    DBUG_EXECUTE_IF("CloudTablet.capture_rs_readers.return.e-230", {
134
1.04M
        LOG_WARNING("CloudTablet.capture_rs_readers.return e-230 init")
135
1.04M
                .tag("tablet_id", tablet->tablet_id());
136
1.04M
        return Status::Error<false>(-230, "injected error");
137
1.04M
    });
138
139
1.04M
    for (auto& ctx : local_state->_common_expr_ctxs_push_down) {
140
15.6k
        VExprContextSPtr context;
141
15.6k
        RETURN_IF_ERROR(ctx->clone(_state, context));
142
15.6k
        _common_expr_ctxs_push_down.emplace_back(context);
143
15.6k
        context->prepare_ann_range_search(_vector_search_params);
144
15.6k
    }
145
146
1.04M
    for (auto pair : local_state->_slot_id_to_virtual_column_expr) {
147
        // Scanner will be executed in a different thread, so we need to clone the context.
148
414
        VExprContextSPtr context;
149
414
        RETURN_IF_ERROR(pair.second->clone(_state, context));
150
414
        _slot_id_to_virtual_column_expr[pair.first] = context;
151
414
    }
152
153
1.04M
    _slot_id_to_index_in_block = local_state->_slot_id_to_index_in_block;
154
1.04M
    _slot_id_to_col_type = local_state->_slot_id_to_col_type;
155
1.04M
    _score_runtime = local_state->_score_runtime;
156
    // All scanners share the same ann_topn_runtime.
157
1.04M
    _ann_topn_runtime = local_state->_ann_topn_runtime;
158
159
    // set limit to reduce end of rowset and segment mem use
160
1.04M
    _tablet_reader = std::make_unique<BlockReader>();
161
    // batch size is passed down to segment iterator, use _state->batch_size()
162
    // instead of _parent->limit(), because if _parent->limit() is a very small
163
    // value (e.g. select a from t where a .. and b ... limit 1),
164
    // it will be very slow when reading data in segment iterator
165
1.04M
    _tablet_reader->set_batch_size(_state->batch_size());
166
    // Adaptive batch size: pass byte-budget settings to the storage reader.
167
    // The reader still uses batch_size() as the row ceiling.
168
1.04M
    _tablet_reader->set_preferred_block_size_bytes(_state->preferred_block_size_bytes());
169
1.04M
    {
170
1.04M
        TOlapScanNode& olap_scan_node = local_state->olap_scan_node();
171
172
        // Each scanner builds its own TabletSchema to avoid concurrent modification.
173
1.04M
        tablet_schema = std::make_shared<TabletSchema>();
174
1.04M
        tablet_schema->copy_from(*tablet->tablet_schema());
175
1.04M
        if (olap_scan_node.__isset.columns_desc && !olap_scan_node.columns_desc.empty() &&
176
1.04M
            olap_scan_node.columns_desc[0].col_unique_id >= 0) {
177
1.04M
            tablet_schema->clear_columns();
178
16.5M
            for (const auto& column_desc : olap_scan_node.columns_desc) {
179
16.5M
                tablet_schema->append_column(TabletColumn(column_desc));
180
16.5M
            }
181
1.04M
            if (olap_scan_node.__isset.schema_version) {
182
1.04M
                tablet_schema->set_schema_version(olap_scan_node.schema_version);
183
1.04M
            }
184
1.04M
        }
185
1.04M
        if (olap_scan_node.__isset.indexes_desc) {
186
1.04M
            tablet_schema->update_indexes_from_thrift(olap_scan_node.indexes_desc);
187
1.04M
        }
188
189
1.04M
        if (_tablet_reader_params.rs_splits.empty()) {
190
            // Non-pipeline mode, Tablet : Scanner = 1 : 1
191
            // acquire tablet rowset readers at the beginning of the scan node
192
            // to prevent this case: when there are lots of olap scanners to run for example 10000
193
            // the rowsets maybe compacted when the last olap scanner starts
194
0
            ReadSource read_source;
195
196
0
            if (config::is_cloud_mode()) {
197
                // FIXME(plat1ko): Avoid pointer cast
198
0
                ExecEnv::GetInstance()->storage_engine().to_cloud().tablet_hotspot().count(*tablet);
199
0
            }
200
201
0
            auto maybe_read_source = tablet->capture_read_source(
202
0
                    _tablet_reader_params.version,
203
0
                    {
204
0
                            .skip_missing_versions = _state->skip_missing_version(),
205
0
                            .enable_fetch_rowsets_from_peers =
206
0
                                    config::enable_fetch_rowsets_from_peer_replicas,
207
0
                            .enable_prefer_cached_rowset =
208
0
                                    config::is_cloud_mode() ? _state->enable_prefer_cached_rowset()
209
0
                                                            : false,
210
0
                            .query_freshness_tolerance_ms =
211
0
                                    config::is_cloud_mode() ? _state->query_freshness_tolerance_ms()
212
0
                                                            : -1,
213
0
                    });
214
0
            if (!maybe_read_source) {
215
0
                LOG(WARNING) << "fail to init reader. res=" << maybe_read_source.error();
216
0
                return maybe_read_source.error();
217
0
            }
218
219
0
            read_source = std::move(maybe_read_source.value());
220
221
0
            if (config::enable_mow_verbose_log && tablet->enable_unique_key_merge_on_write()) {
222
0
                LOG_INFO("finish capture_rs_readers for tablet={}, query_id={}",
223
0
                         tablet->tablet_id(), print_id(_state->query_id()));
224
0
            }
225
226
0
            if (!_state->skip_delete_predicate()) {
227
0
                read_source.fill_delete_predicates();
228
0
            }
229
0
            _tablet_reader_params.set_read_source(std::move(read_source));
230
0
        }
231
232
        // Initialize tablet_reader_params
233
1.04M
        RETURN_IF_ERROR(_init_tablet_reader_params(
234
1.04M
                local_state->_parent->cast<OlapScanOperatorX>()._slot_id_to_slot_desc, _key_ranges,
235
1.04M
                local_state->_slot_id_to_predicates, local_state->_push_down_functions));
236
1.04M
    }
237
238
    // add read columns in profile
239
1.04M
    if (_state->enable_profile()) {
240
2.97k
        _profile->add_info_string("ReadColumns",
241
2.97k
                                  read_columns_to_string(tablet_schema, _return_columns));
242
2.97k
    }
243
244
1.04M
    if (_tablet_reader_params.score_runtime) {
245
14
        SCOPED_TIMER(local_state->_statistics_collect_timer);
246
14
        _tablet_reader_params.collection_statistics = std::make_shared<CollectionStatistics>();
247
248
14
        io::IOContext io_ctx {
249
14
                .reader_type = ReaderType::READER_QUERY,
250
14
                .expiration_time = tablet->ttl_seconds(),
251
14
                .query_id = &_state->query_id(),
252
14
                .file_cache_stats = &_tablet_reader->mutable_stats()->file_cache_stats,
253
14
                .is_inverted_index = true,
254
14
        };
255
256
14
        RETURN_IF_ERROR(_tablet_reader_params.collection_statistics->collect(
257
14
                _state, _tablet_reader_params.rs_splits, _tablet_reader_params.tablet_schema,
258
14
                _tablet_reader_params.common_expr_ctxs_push_down, &io_ctx));
259
14
    }
260
261
1.04M
    _has_prepared = true;
262
1.04M
    return Status::OK();
263
1.04M
}
264
265
1.03M
Status OlapScanner::_open_impl(RuntimeState* state) {
266
1.03M
    RETURN_IF_ERROR(Scanner::_open_impl(state));
267
1.03M
    SCOPED_TIMER(_local_state->cast<OlapScanLocalState>()._reader_init_timer);
268
269
1.03M
    auto res = _tablet_reader->init(_tablet_reader_params);
270
1.03M
    if (!res.ok()) {
271
39
        res.append("failed to initialize storage reader. tablet=" +
272
39
                   std::to_string(_tablet_reader_params.tablet->tablet_id()) +
273
39
                   ", backend=" + BackendOptions::get_localhost());
274
39
        return res;
275
39
    }
276
277
    // Do not hold rs_splits any more to release memory.
278
1.03M
    _tablet_reader_params.rs_splits.clear();
279
280
1.03M
    return Status::OK();
281
1.03M
}
282
283
// it will be called under tablet read lock because capture rs readers need
284
Status OlapScanner::_init_tablet_reader_params(
285
        const phmap::flat_hash_map<int, SlotDescriptor*>& slot_id_to_slot_desc,
286
        const std::vector<OlapScanRange*>& key_ranges,
287
        const phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
288
                slot_to_predicates,
289
1.04M
        const std::vector<FunctionFilter>& function_filters) {
290
    // if the table with rowset [0-x] or [0-1] [2-y], and [0-1] is empty
291
1.04M
    const bool single_version = _tablet_reader_params.has_single_version();
292
293
1.04M
    auto* olap_local_state = static_cast<OlapScanLocalState*>(_local_state);
294
1.04M
    bool read_mor_as_dup = olap_local_state->olap_scan_node().__isset.read_mor_as_dup &&
295
1.04M
                           olap_local_state->olap_scan_node().read_mor_as_dup;
296
1.04M
    if (_state->skip_storage_engine_merge() || read_mor_as_dup) {
297
49
        _tablet_reader_params.direct_mode = true;
298
49
        _tablet_reader_params.aggregation = true;
299
1.04M
    } else {
300
1.04M
        auto push_down_agg_type = _local_state->get_push_down_agg_type();
301
1.04M
        _tablet_reader_params.direct_mode = _tablet_reader_params.aggregation || single_version ||
302
1.04M
                                            (push_down_agg_type != TPushAggOp::NONE &&
303
10.6k
                                             push_down_agg_type != TPushAggOp::COUNT_ON_INDEX);
304
1.04M
    }
305
306
1.04M
    RETURN_IF_ERROR(_init_variant_columns());
307
1.04M
    RETURN_IF_ERROR(_init_return_columns());
308
309
1.04M
    _tablet_reader_params.reader_type = ReaderType::READER_QUERY;
310
1.04M
    _tablet_reader_params.push_down_agg_type_opt = _local_state->get_push_down_agg_type();
311
312
    // TODO: If a new runtime filter arrives after `_conjuncts` move to `_common_expr_ctxs_push_down`,
313
1.04M
    if (_common_expr_ctxs_push_down.empty()) {
314
1.02M
        for (auto& conjunct : _conjuncts) {
315
10.7k
            _tablet_reader_params.remaining_conjunct_roots.emplace_back(conjunct->root());
316
10.7k
        }
317
1.02M
    } else {
318
15.5k
        for (auto& ctx : _common_expr_ctxs_push_down) {
319
15.5k
            _tablet_reader_params.remaining_conjunct_roots.emplace_back(ctx->root());
320
15.5k
        }
321
14.1k
    }
322
323
1.04M
    _tablet_reader_params.common_expr_ctxs_push_down = _common_expr_ctxs_push_down;
324
1.04M
    _tablet_reader_params.virtual_column_exprs = _virtual_column_exprs;
325
1.04M
    _tablet_reader_params.vir_cid_to_idx_in_block = _vir_cid_to_idx_in_block;
326
1.04M
    _tablet_reader_params.vir_col_idx_to_type = _vir_col_idx_to_type;
327
1.04M
    _tablet_reader_params.score_runtime = _score_runtime;
328
1.04M
    _tablet_reader_params.output_columns = ((OlapScanLocalState*)_local_state)->_output_column_ids;
329
1.04M
    _tablet_reader_params.ann_topn_runtime = _ann_topn_runtime;
330
1.04M
    for (const auto& ele : ((OlapScanLocalState*)_local_state)->_cast_types_for_variants) {
331
1.83k
        _tablet_reader_params.target_cast_type_for_variants[ele.first] = ele.second;
332
1.83k
    };
333
1.04M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
334
7.94M
    for (auto& predicates : slot_to_predicates) {
335
7.94M
        const int sid = predicates.first;
336
7.94M
        DCHECK(slot_id_to_slot_desc.contains(sid));
337
7.94M
        int32_t index =
338
7.94M
                tablet_schema->field_index(slot_id_to_slot_desc.find(sid)->second->col_name());
339
7.94M
        if (index < 0) {
340
0
            throw Exception(
341
0
                    Status::InternalError("Column {} not found in tablet schema",
342
0
                                          slot_id_to_slot_desc.find(sid)->second->col_name()));
343
0
        }
344
7.94M
        for (auto& predicate : predicates.second) {
345
832k
            _tablet_reader_params.predicates.push_back(predicate->clone(index));
346
832k
        }
347
7.94M
    }
348
349
1.04M
    std::copy(function_filters.cbegin(), function_filters.cend(),
350
1.04M
              std::inserter(_tablet_reader_params.function_filters,
351
1.04M
                            _tablet_reader_params.function_filters.begin()));
352
353
    // Merge the columns in delete predicate that not in latest schema in to current tablet schema
354
1.04M
    for (auto& del_pred : _tablet_reader_params.delete_predicates) {
355
7.27k
        tablet_schema->merge_dropped_columns(*del_pred->tablet_schema());
356
7.27k
    }
357
358
    // Push key ranges to the tablet reader.
359
    // Skip the "full scan" placeholder (has_lower_bound == false) — when no key
360
    // predicates exist, start_key/end_key remain empty and the reader does a full scan.
361
1.76M
    for (auto* key_range : key_ranges) {
362
1.76M
        if (!key_range->has_lower_bound) {
363
171k
            continue;
364
171k
        }
365
366
1.59M
        _tablet_reader_params.start_key_include = key_range->begin_include;
367
1.59M
        _tablet_reader_params.end_key_include = key_range->end_include;
368
369
1.59M
        _tablet_reader_params.start_key.push_back(key_range->begin_scan_range);
370
1.59M
        _tablet_reader_params.end_key.push_back(key_range->end_scan_range);
371
1.59M
    }
372
373
1.04M
    _tablet_reader_params.profile = _local_state->custom_profile();
374
1.04M
    _tablet_reader_params.runtime_state = _state;
375
376
1.04M
    _tablet_reader_params.origin_return_columns = &_return_columns;
377
1.04M
    _tablet_reader_params.tablet_columns_convert_to_null_set = &_tablet_columns_convert_to_null_set;
378
379
1.04M
    if (_tablet_reader_params.direct_mode) {
380
1.02M
        _tablet_reader_params.return_columns = _return_columns;
381
1.02M
    } else {
382
        // we need to fetch all key columns to do the right aggregation on storage engine side.
383
40.2k
        for (size_t i = 0; i < tablet_schema->num_key_columns(); ++i) {
384
28.5k
            _tablet_reader_params.return_columns.push_back(i);
385
28.5k
        }
386
51.2k
        for (auto index : _return_columns) {
387
51.2k
            if (tablet_schema->column(index).is_key()) {
388
19.4k
                continue;
389
19.4k
            }
390
31.7k
            _tablet_reader_params.return_columns.push_back(index);
391
31.7k
        }
392
        // expand the sequence column
393
11.7k
        if (tablet_schema->has_sequence_col() || tablet_schema->has_seq_map()) {
394
40
            bool has_replace_col = false;
395
91
            for (auto col : _return_columns) {
396
91
                if (tablet_schema->column(col).aggregation() ==
397
91
                    FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE) {
398
40
                    has_replace_col = true;
399
40
                    break;
400
40
                }
401
91
            }
402
40
            if (auto sequence_col_idx = tablet_schema->sequence_col_idx();
403
40
                has_replace_col && tablet_schema->has_sequence_col() &&
404
40
                std::find(_return_columns.begin(), _return_columns.end(), sequence_col_idx) ==
405
28
                        _return_columns.end()) {
406
16
                _tablet_reader_params.return_columns.push_back(sequence_col_idx);
407
16
            }
408
40
            if (has_replace_col) {
409
40
                const auto& val_to_seq = tablet_schema->value_col_idx_to_seq_col_idx();
410
40
                std::set<uint32_t> return_seq_columns;
411
412
243
                for (auto col : _tablet_reader_params.return_columns) {
413
                    // we need to add the necessary sequence column in _return_columns, and
414
                    // Avoid adding the same seq column twice
415
243
                    const auto val_iter = val_to_seq.find(col);
416
243
                    if (val_iter != val_to_seq.end()) {
417
42
                        auto seq = val_iter->second;
418
42
                        if (std::find(_tablet_reader_params.return_columns.begin(),
419
42
                                      _tablet_reader_params.return_columns.end(),
420
42
                                      seq) == _tablet_reader_params.return_columns.end()) {
421
4
                            return_seq_columns.insert(seq);
422
4
                        }
423
42
                    }
424
243
                }
425
40
                _tablet_reader_params.return_columns.insert(
426
40
                        std::end(_tablet_reader_params.return_columns),
427
40
                        std::begin(return_seq_columns), std::end(return_seq_columns));
428
40
            }
429
40
        }
430
11.7k
    }
431
432
1.04M
    _tablet_reader_params.use_page_cache = _state->enable_page_cache();
433
434
1.04M
    DBUG_EXECUTE_IF("NewOlapScanner::_init_tablet_reader_params.block", DBUG_BLOCK);
435
436
1.04M
    if (!_state->skip_storage_engine_merge()) {
437
1.03M
        auto* olap_scan_local_state = (OlapScanLocalState*)_local_state;
438
1.03M
        TOlapScanNode& olap_scan_node = olap_scan_local_state->olap_scan_node();
439
440
        // Set MOR value predicate pushdown flag
441
1.03M
        if (olap_scan_node.__isset.enable_mor_value_predicate_pushdown &&
442
1.03M
            olap_scan_node.enable_mor_value_predicate_pushdown) {
443
25
            _tablet_reader_params.enable_mor_value_predicate_pushdown = true;
444
25
        }
445
446
        // Skip topn / general-limit storage-layer optimizations when runtime
447
        // filters exist.  Late-arriving filters would re-populate _conjuncts
448
        // at the scanner level while the storage layer has already committed
449
        // to a row budget counted before those filters, causing the scan to
450
        // return fewer rows than the limit requires.
451
1.03M
        if (_total_rf_num == 0) {
452
            // order by table keys optimization for topn
453
            // will only read head/tail of data file since it's already sorted by keys
454
1.01M
            if (olap_scan_node.__isset.sort_info &&
455
1.01M
                !olap_scan_node.sort_info.is_asc_order.empty()) {
456
2.68k
                _limit = _local_state->limit_per_scanner();
457
2.68k
                _tablet_reader_params.read_orderby_key = true;
458
2.68k
                if (!olap_scan_node.sort_info.is_asc_order[0]) {
459
350
                    _tablet_reader_params.read_orderby_key_reverse = true;
460
350
                }
461
2.68k
                _tablet_reader_params.read_orderby_key_num_prefix_columns =
462
2.68k
                        olap_scan_node.sort_info.is_asc_order.size();
463
2.68k
                _tablet_reader_params.read_orderby_key_limit = _limit;
464
465
2.68k
                if (_tablet_reader_params.read_orderby_key_limit > 0 &&
466
2.69k
                    olap_scan_local_state->_storage_no_merge()) {
467
2.57k
                    _tablet_reader_params.filter_block_conjuncts = _conjuncts;
468
2.57k
                    _conjuncts.clear();
469
2.57k
                }
470
1.00M
            } else if (_limit > 0 && olap_scan_local_state->_storage_no_merge()) {
471
                // General limit pushdown for DUP_KEYS and UNIQUE_KEYS with MOW
472
                // (non-merge path). Only when topn optimization is NOT active.
473
                // NOTE: _limit is the global query limit (TPlanNode.limit), not a
474
                // per-scanner budget. With N scanners each scanner may read up to
475
                // _limit rows, so up to N * _limit rows are read in total before
476
                // the _shared_scan_limit coordinator stops them. This is
477
                // acceptable because _shared_scan_limit guarantees correctness,
478
                // and the over-read is bounded by (N-1) * _limit which is small
479
                // for typical LIMIT values.
480
1.89k
                _tablet_reader_params.general_read_limit = _limit;
481
1.89k
                _tablet_reader_params.filter_block_conjuncts = _conjuncts;
482
1.89k
                _conjuncts.clear();
483
1.89k
            }
484
1.01M
        }
485
486
        // set push down topn filter
487
1.03M
        _tablet_reader_params.topn_filter_source_node_ids =
488
1.03M
                olap_scan_local_state->get_topn_filter_source_node_ids(_state, true);
489
1.03M
        if (!_tablet_reader_params.topn_filter_source_node_ids.empty()) {
490
5.26k
            _tablet_reader_params.topn_filter_target_node_id =
491
5.26k
                    olap_scan_local_state->parent()->node_id();
492
5.26k
        }
493
1.03M
    }
494
495
    // If this is a Two-Phase read query, and we need to delay the release of Rowset
496
    // by rowset->update_delayed_expired_timestamp().This could expand the lifespan of Rowset
497
1.04M
    if (tablet_schema->field_index(BeConsts::ROWID_COL) >= 0) {
498
0
        constexpr static int delayed_s = 60;
499
0
        for (auto rs_reader : _tablet_reader_params.rs_splits) {
500
0
            uint64_t delayed_expired_timestamp =
501
0
                    UnixSeconds() + _tablet_reader_params.runtime_state->execution_timeout() +
502
0
                    delayed_s;
503
0
            rs_reader.rs_reader->rowset()->update_delayed_expired_timestamp(
504
0
                    delayed_expired_timestamp);
505
0
            ExecEnv::GetInstance()->storage_engine().add_quering_rowset(
506
0
                    rs_reader.rs_reader->rowset());
507
0
        }
508
0
    }
509
510
1.04M
    if (tablet_schema->has_global_row_id()) {
511
7.07k
        auto& id_file_map = _state->get_id_file_map();
512
12.8k
        for (auto rs_reader : _tablet_reader_params.rs_splits) {
513
12.8k
            id_file_map->add_temp_rowset(rs_reader.rs_reader->rowset());
514
12.8k
        }
515
7.07k
    }
516
517
1.04M
    return Status::OK();
518
1.04M
}
519
520
1.03M
Status OlapScanner::_init_variant_columns() {
521
1.03M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
522
1.03M
    if (tablet_schema->num_variant_columns() == 0) {
523
1.03M
        return Status::OK();
524
1.03M
    }
525
    // Parent column has path info to distinction from each other
526
16.0k
    for (auto* slot : _output_tuple_desc->slots()) {
527
16.0k
        if (slot->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
528
            // Such columns are not exist in frontend schema info, so we need to
529
            // add them into tablet_schema for later column indexing.
530
8.14k
            const auto& dt_variant =
531
8.14k
                    assert_cast<const DataTypeVariant&>(*remove_nullable(slot->type()));
532
8.14k
            TabletColumn subcol = TabletColumn::create_materialized_variant_column(
533
8.14k
                    tablet_schema->column_by_uid(slot->col_unique_id()).name_lower_case(),
534
8.14k
                    slot->column_paths(), slot->col_unique_id(),
535
8.14k
                    dt_variant.variant_max_subcolumns_count(), dt_variant.enable_doc_mode());
536
8.14k
            if (tablet_schema->field_index(*subcol.path_info_ptr()) < 0) {
537
6.44k
                tablet_schema->append_column(subcol, TabletSchema::ColumnType::VARIANT);
538
6.44k
            }
539
8.14k
        }
540
16.0k
    }
541
5.86k
    variant_util::inherit_column_attributes(tablet_schema);
542
5.86k
    return Status::OK();
543
1.03M
}
544
545
1.03M
Status OlapScanner::_init_return_columns() {
546
10.4M
    for (auto* slot : _output_tuple_desc->slots()) {
547
        // variant column using path to index a column
548
10.4M
        int32_t index = 0;
549
10.4M
        auto& tablet_schema = _tablet_reader_params.tablet_schema;
550
10.4M
        if (slot->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
551
8.13k
            index = tablet_schema->field_index(PathInData(
552
8.13k
                    tablet_schema->column_by_uid(slot->col_unique_id()).name_lower_case(),
553
8.13k
                    slot->column_paths()));
554
10.3M
        } else {
555
10.4M
            index = slot->col_unique_id() >= 0 ? tablet_schema->field_index(slot->col_unique_id())
556
18.4E
                                               : tablet_schema->field_index(slot->col_name());
557
10.3M
        }
558
559
10.4M
        if (index < 0) {
560
0
            return Status::InternalError(
561
0
                    "field name is invalid. field={}, field_name_to_index={}, col_unique_id={}",
562
0
                    slot->col_name(), tablet_schema->get_all_field_names(), slot->col_unique_id());
563
0
        }
564
565
10.4M
        if (slot->get_virtual_column_expr()) {
566
414
            ColumnId virtual_column_cid = index;
567
414
            _virtual_column_exprs[virtual_column_cid] = _slot_id_to_virtual_column_expr[slot->id()];
568
414
            size_t idx_in_block = _slot_id_to_index_in_block[slot->id()];
569
414
            _vir_cid_to_idx_in_block[virtual_column_cid] = idx_in_block;
570
414
            _vir_col_idx_to_type[idx_in_block] = _slot_id_to_col_type[slot->id()];
571
572
414
            VLOG_DEBUG << fmt::format(
573
4
                    "Virtual column, slot id: {}, cid {}, column index: {}, type: {}", slot->id(),
574
4
                    virtual_column_cid, _vir_cid_to_idx_in_block[virtual_column_cid],
575
4
                    _vir_col_idx_to_type[idx_in_block]->get_name());
576
414
        }
577
578
10.4M
        const auto& column = tablet_schema->column(index);
579
10.4M
        int32_t unique_id =
580
10.4M
                column.unique_id() >= 0 ? column.unique_id() : column.parent_unique_id();
581
10.4M
        if (!slot->all_access_paths().empty()) {
582
75.7k
            _tablet_reader_params.all_access_paths.insert({unique_id, slot->all_access_paths()});
583
75.7k
        }
584
585
10.4M
        if (!slot->predicate_access_paths().empty()) {
586
5.68k
            _tablet_reader_params.predicate_access_paths.insert(
587
5.68k
                    {unique_id, slot->predicate_access_paths()});
588
5.68k
        }
589
590
10.4M
        if ((slot->type()->get_primitive_type() == PrimitiveType::TYPE_STRUCT ||
591
10.4M
             slot->type()->get_primitive_type() == PrimitiveType::TYPE_MAP ||
592
10.4M
             slot->type()->get_primitive_type() == PrimitiveType::TYPE_ARRAY) &&
593
10.4M
            !slot->all_access_paths().empty()) {
594
69.1k
            tablet_schema->add_pruned_columns_data_type(column.unique_id(), slot->type());
595
69.1k
        }
596
597
10.4M
        _return_columns.push_back(index);
598
10.4M
        if (slot->is_nullable() && !tablet_schema->column(index).is_nullable()) {
599
0
            _tablet_columns_convert_to_null_set.emplace(index);
600
10.4M
        } else if (!slot->is_nullable() && tablet_schema->column(index).is_nullable()) {
601
0
            return Status::Error<ErrorCode::INVALID_SCHEMA>(
602
0
                    "slot(id: {}, name: {})'s nullable does not match "
603
0
                    "column(tablet id: {}, index: {}, name: {}) ",
604
0
                    slot->id(), slot->col_name(), tablet_schema->table_id(), index,
605
0
                    tablet_schema->column(index).name());
606
0
        }
607
10.4M
    }
608
609
1.03M
    if (_return_columns.empty()) {
610
0
        return Status::InternalError("failed to build storage scanner, no materialized slot!");
611
0
    }
612
613
1.03M
    return Status::OK();
614
1.03M
}
615
616
1.08M
doris::TabletStorageType OlapScanner::get_storage_type() {
617
1.08M
    if (config::is_cloud_mode()) {
618
        // we don't have cold storage in cloud mode, all storage is treated as local
619
913k
        return doris::TabletStorageType::STORAGE_TYPE_LOCAL;
620
913k
    }
621
172k
    int local_reader = 0;
622
308k
    for (const auto& reader : _tablet_reader_params.rs_splits) {
623
308k
        local_reader += reader.rs_reader->rowset()->is_local();
624
308k
    }
625
172k
    int total_reader = _tablet_reader_params.rs_splits.size();
626
627
172k
    if (local_reader == total_reader) {
628
172k
        return doris::TabletStorageType::STORAGE_TYPE_LOCAL;
629
172k
    } else if (local_reader == 0) {
630
0
        return doris::TabletStorageType::STORAGE_TYPE_REMOTE;
631
0
    }
632
0
    return doris::TabletStorageType::STORAGE_TYPE_REMOTE_AND_LOCAL;
633
172k
}
634
635
1.29M
Status OlapScanner::_get_block_impl(RuntimeState* state, Block* block, bool* eof) {
636
    // Read one block from block reader
637
    // ATTN: Here we need to let the _get_block_impl method guarantee the semantics of the interface,
638
    // that is, eof can be set to true only when the returned block is empty.
639
1.29M
    RETURN_IF_ERROR(_tablet_reader->next_block_with_aggregation(block, eof));
640
1.29M
    if (block->rows() > 0) {
641
260k
        _tablet_reader_params.tablet->read_block_count.fetch_add(1, std::memory_order_relaxed);
642
260k
        *eof = false;
643
260k
    }
644
1.29M
    return Status::OK();
645
1.29M
}
646
647
1.04M
Status OlapScanner::close(RuntimeState* state) {
648
1.04M
    if (!_try_close()) {
649
150
        return Status::OK();
650
150
    }
651
1.04M
    RETURN_IF_ERROR(Scanner::close(state));
652
1.04M
    return Status::OK();
653
1.04M
}
654
655
1.08M
void OlapScanner::update_realtime_counters() {
656
1.08M
    if (!_has_prepared) {
657
        // Counter update need prepare successfully, or it maybe core. For example, olap scanner
658
        // will open tablet reader during prepare, if not prepare successfully, tablet reader == nullptr.
659
0
        return;
660
0
    }
661
1.08M
    OlapScanLocalState* local_state = static_cast<OlapScanLocalState*>(_local_state);
662
1.08M
    const OlapReaderStatistics& stats = _tablet_reader->stats();
663
1.08M
    COUNTER_UPDATE(local_state->_read_compressed_counter, stats.compressed_bytes_read);
664
1.08M
    COUNTER_UPDATE(local_state->_read_uncompressed_counter, stats.uncompressed_bytes_read);
665
1.08M
    COUNTER_UPDATE(local_state->_scan_bytes, stats.uncompressed_bytes_read);
666
1.08M
    COUNTER_UPDATE(local_state->_scan_rows, stats.raw_rows_read);
667
668
    // Make sure the scan bytes and scan rows counter in audit log is the same as the counter in
669
    // doris metrics.
670
    // ScanBytes is the uncompressed bytes read from local + remote
671
    // bytes_read_from_local is the compressed bytes read from local
672
    // bytes_read_from_remote is the compressed bytes read from remote
673
    // scan bytes > bytes_read_from_local + bytes_read_from_remote
674
1.08M
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(stats.raw_rows_read);
675
1.08M
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(
676
1.08M
            stats.uncompressed_bytes_read);
677
678
    // In case of no cache, we still need to update the IO stats. uncompressed bytes read == local + remote
679
1.08M
    if (stats.file_cache_stats.bytes_read_from_local == 0 &&
680
1.08M
        stats.file_cache_stats.bytes_read_from_remote == 0) {
681
833k
        _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
682
833k
                stats.compressed_bytes_read);
683
833k
        DorisMetrics::instance()->query_scan_bytes_from_local->increment(
684
833k
                stats.compressed_bytes_read);
685
833k
    } else {
686
251k
        _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
687
251k
                stats.file_cache_stats.bytes_read_from_local - _bytes_read_from_local);
688
251k
        _state->get_query_ctx()
689
251k
                ->resource_ctx()
690
251k
                ->io_context()
691
251k
                ->update_scan_bytes_from_remote_storage(
692
251k
                        stats.file_cache_stats.bytes_read_from_remote - _bytes_read_from_remote);
693
694
251k
        DorisMetrics::instance()->query_scan_bytes_from_local->increment(
695
251k
                stats.file_cache_stats.bytes_read_from_local - _bytes_read_from_local);
696
251k
        DorisMetrics::instance()->query_scan_bytes_from_remote->increment(
697
251k
                stats.file_cache_stats.bytes_read_from_remote - _bytes_read_from_remote);
698
251k
    }
699
700
1.08M
    _tablet_reader->mutable_stats()->compressed_bytes_read = 0;
701
1.08M
    _tablet_reader->mutable_stats()->uncompressed_bytes_read = 0;
702
1.08M
    _tablet_reader->mutable_stats()->raw_rows_read = 0;
703
704
1.08M
    _bytes_read_from_local = _tablet_reader->stats().file_cache_stats.bytes_read_from_local;
705
1.08M
    _bytes_read_from_remote = _tablet_reader->stats().file_cache_stats.bytes_read_from_remote;
706
1.08M
}
707
708
1.03M
void OlapScanner::_collect_profile_before_close() {
709
    //  Please don't directly enable the profile here, we need to set QueryStatistics using the counter inside.
710
1.03M
    if (_has_updated_counter) {
711
0
        return;
712
0
    }
713
1.03M
    _has_updated_counter = true;
714
1.03M
    _tablet_reader->update_profile(_profile);
715
716
1.03M
    Scanner::_collect_profile_before_close();
717
718
    // Update counters for OlapScanner
719
    // Update counters from tablet reader's stats
720
1.03M
    auto& stats = _tablet_reader->stats();
721
1.03M
    auto* local_state = (OlapScanLocalState*)_local_state;
722
1.03M
    COUNTER_UPDATE(local_state->_io_timer, stats.io_ns);
723
1.03M
    COUNTER_UPDATE(local_state->_read_compressed_counter, stats.compressed_bytes_read);
724
1.03M
    COUNTER_UPDATE(local_state->_scan_bytes, stats.uncompressed_bytes_read);
725
1.03M
    COUNTER_UPDATE(local_state->_decompressor_timer, stats.decompress_ns);
726
1.03M
    COUNTER_UPDATE(local_state->_read_uncompressed_counter, stats.uncompressed_bytes_read);
727
1.03M
    COUNTER_UPDATE(local_state->_block_load_timer, stats.block_load_ns);
728
1.03M
    COUNTER_UPDATE(local_state->_block_load_counter, stats.blocks_load);
729
1.03M
    COUNTER_UPDATE(local_state->_block_fetch_timer, stats.block_fetch_ns);
730
1.03M
    COUNTER_UPDATE(local_state->_delete_bitmap_get_agg_timer, stats.delete_bitmap_get_agg_ns);
731
1.03M
    COUNTER_UPDATE(local_state->_scan_rows, stats.raw_rows_read);
732
1.03M
    COUNTER_UPDATE(local_state->_vec_cond_timer, stats.vec_cond_ns);
733
1.03M
    COUNTER_UPDATE(local_state->_short_cond_timer, stats.short_cond_ns);
734
1.03M
    COUNTER_UPDATE(local_state->_expr_filter_timer, stats.expr_filter_ns);
735
1.03M
    COUNTER_UPDATE(local_state->_block_init_timer, stats.block_init_ns);
736
1.03M
    COUNTER_UPDATE(local_state->_block_init_seek_timer, stats.block_init_seek_ns);
737
1.03M
    COUNTER_UPDATE(local_state->_block_init_seek_counter, stats.block_init_seek_num);
738
1.03M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_keys_timer,
739
1.03M
                   stats.generate_row_ranges_by_keys_ns);
740
1.03M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_column_conditions_timer,
741
1.03M
                   stats.generate_row_ranges_by_column_conditions_ns);
742
1.03M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_bf_timer,
743
1.03M
                   stats.generate_row_ranges_by_bf_ns);
744
1.03M
    COUNTER_UPDATE(local_state->_collect_iterator_merge_next_timer,
745
1.03M
                   stats.collect_iterator_merge_next_timer);
746
1.03M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_zonemap_timer,
747
1.03M
                   stats.generate_row_ranges_by_zonemap_ns);
748
1.03M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_dict_timer,
749
1.03M
                   stats.generate_row_ranges_by_dict_ns);
750
1.03M
    COUNTER_UPDATE(local_state->_predicate_column_read_timer, stats.predicate_column_read_ns);
751
1.03M
    COUNTER_UPDATE(local_state->_non_predicate_column_read_timer, stats.non_predicate_read_ns);
752
1.03M
    COUNTER_UPDATE(local_state->_predicate_column_read_seek_timer,
753
1.03M
                   stats.predicate_column_read_seek_ns);
754
1.03M
    COUNTER_UPDATE(local_state->_predicate_column_read_seek_counter,
755
1.03M
                   stats.predicate_column_read_seek_num);
756
1.03M
    COUNTER_UPDATE(local_state->_lazy_read_timer, stats.lazy_read_ns);
757
1.03M
    COUNTER_UPDATE(local_state->_lazy_read_seek_timer, stats.block_lazy_read_seek_ns);
758
1.03M
    COUNTER_UPDATE(local_state->_lazy_read_seek_counter, stats.block_lazy_read_seek_num);
759
1.03M
    COUNTER_UPDATE(local_state->_output_col_timer, stats.output_col_ns);
760
1.03M
    COUNTER_UPDATE(local_state->_rows_vec_cond_filtered_counter, stats.rows_vec_cond_filtered);
761
1.03M
    COUNTER_UPDATE(local_state->_rows_short_circuit_cond_filtered_counter,
762
1.03M
                   stats.rows_short_circuit_cond_filtered);
763
1.03M
    COUNTER_UPDATE(local_state->_rows_expr_cond_filtered_counter, stats.rows_expr_cond_filtered);
764
1.03M
    COUNTER_UPDATE(local_state->_rows_vec_cond_input_counter, stats.vec_cond_input_rows);
765
1.03M
    COUNTER_UPDATE(local_state->_rows_short_circuit_cond_input_counter,
766
1.03M
                   stats.short_circuit_cond_input_rows);
767
1.03M
    COUNTER_UPDATE(local_state->_rows_expr_cond_input_counter, stats.expr_cond_input_rows);
768
1.03M
    COUNTER_UPDATE(local_state->_stats_filtered_counter, stats.rows_stats_filtered);
769
1.03M
    COUNTER_UPDATE(local_state->_stats_rp_filtered_counter, stats.rows_stats_rp_filtered);
770
1.03M
    COUNTER_UPDATE(local_state->_dict_filtered_counter, stats.segment_dict_filtered);
771
1.03M
    COUNTER_UPDATE(local_state->_bf_filtered_counter, stats.rows_bf_filtered);
772
1.03M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_del_filtered);
773
1.03M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_del_by_bitmap);
774
1.03M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_vec_del_cond_filtered);
775
1.03M
    COUNTER_UPDATE(local_state->_conditions_filtered_counter, stats.rows_conditions_filtered);
776
1.03M
    COUNTER_UPDATE(local_state->_key_range_filtered_counter, stats.rows_key_range_filtered);
777
1.03M
    COUNTER_UPDATE(local_state->_total_pages_num_counter, stats.total_pages_num);
778
1.03M
    COUNTER_UPDATE(local_state->_cached_pages_num_counter, stats.cached_pages_num);
779
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_filter_counter, stats.rows_inverted_index_filtered);
780
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_filter_timer, stats.inverted_index_filter_timer);
781
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_query_cache_hit_counter,
782
1.03M
                   stats.inverted_index_query_cache_hit);
783
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_query_cache_miss_counter,
784
1.03M
                   stats.inverted_index_query_cache_miss);
785
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_query_timer, stats.inverted_index_query_timer);
786
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_query_null_bitmap_timer,
787
1.03M
                   stats.inverted_index_query_null_bitmap_timer);
788
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_query_bitmap_copy_timer,
789
1.03M
                   stats.inverted_index_query_bitmap_copy_timer);
790
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_open_timer,
791
1.03M
                   stats.inverted_index_searcher_open_timer);
792
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_timer,
793
1.03M
                   stats.inverted_index_searcher_search_timer);
794
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_init_timer,
795
1.03M
                   stats.inverted_index_searcher_search_init_timer);
796
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_exec_timer,
797
1.03M
                   stats.inverted_index_searcher_search_exec_timer);
798
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_cache_hit_counter,
799
1.03M
                   stats.inverted_index_searcher_cache_hit);
800
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_cache_miss_counter,
801
1.03M
                   stats.inverted_index_searcher_cache_miss);
802
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_downgrade_count_counter,
803
1.03M
                   stats.inverted_index_downgrade_count);
804
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_analyzer_timer,
805
1.03M
                   stats.inverted_index_analyzer_timer);
806
1.03M
    COUNTER_UPDATE(local_state->_inverted_index_lookup_timer, stats.inverted_index_lookup_timer);
807
1.03M
    COUNTER_UPDATE(local_state->_variant_scan_sparse_column_timer,
808
1.03M
                   stats.variant_scan_sparse_column_timer_ns);
809
1.03M
    COUNTER_UPDATE(local_state->_variant_scan_sparse_column_bytes,
810
1.03M
                   stats.variant_scan_sparse_column_bytes);
811
1.03M
    COUNTER_UPDATE(local_state->_variant_fill_path_from_sparse_column_timer,
812
1.03M
                   stats.variant_fill_path_from_sparse_column_timer_ns);
813
1.03M
    COUNTER_UPDATE(local_state->_variant_subtree_default_iter_count,
814
1.03M
                   stats.variant_subtree_default_iter_count);
815
1.03M
    COUNTER_UPDATE(local_state->_variant_subtree_leaf_iter_count,
816
1.03M
                   stats.variant_subtree_leaf_iter_count);
817
1.03M
    COUNTER_UPDATE(local_state->_variant_subtree_hierarchical_iter_count,
818
1.03M
                   stats.variant_subtree_hierarchical_iter_count);
819
1.03M
    COUNTER_UPDATE(local_state->_variant_subtree_sparse_iter_count,
820
1.03M
                   stats.variant_subtree_sparse_iter_count);
821
1.03M
    COUNTER_UPDATE(local_state->_variant_doc_value_column_iter_count,
822
1.03M
                   stats.variant_doc_value_column_iter_count);
823
824
1.03M
    if (stats.adaptive_batch_size_predict_max_rows > 0) {
825
767k
        local_state->_adaptive_batch_predict_min_rows_counter->set(
826
767k
                stats.adaptive_batch_size_predict_min_rows);
827
767k
        local_state->_adaptive_batch_predict_max_rows_counter->set(
828
767k
                stats.adaptive_batch_size_predict_max_rows);
829
767k
    }
830
831
1.03M
    InvertedIndexProfileReporter inverted_index_profile;
832
1.03M
    inverted_index_profile.update(local_state->_index_filter_profile.get(),
833
1.03M
                                  &stats.inverted_index_stats);
834
835
    // only cloud deploy mode will use file cache.
836
1.03M
    if (config::is_cloud_mode() && config::enable_file_cache) {
837
869k
        io::FileCacheProfileReporter cache_profile(local_state->_segment_profile.get());
838
869k
        cache_profile.update(&stats.file_cache_stats);
839
869k
        _state->get_query_ctx()->resource_ctx()->io_context()->update_bytes_write_into_cache(
840
869k
                stats.file_cache_stats.bytes_write_into_cache);
841
869k
    }
842
1.03M
    COUNTER_UPDATE(local_state->_output_index_result_column_timer,
843
1.03M
                   stats.output_index_result_column_timer);
844
1.03M
    COUNTER_UPDATE(local_state->_filtered_segment_counter, stats.filtered_segment_number);
845
1.03M
    COUNTER_UPDATE(local_state->_total_segment_counter, stats.total_segment_number);
846
1.03M
    COUNTER_UPDATE(local_state->_condition_cache_hit_counter, stats.condition_cache_hit_seg_nums);
847
1.03M
    COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter,
848
1.03M
                   stats.condition_cache_filtered_rows);
849
850
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_timer, stats.tablet_reader_init_timer_ns);
851
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_capture_rs_readers_timer,
852
1.03M
                   stats.tablet_reader_capture_rs_readers_timer_ns);
853
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_return_columns_timer,
854
1.03M
                   stats.tablet_reader_init_return_columns_timer_ns);
855
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_keys_param_timer,
856
1.03M
                   stats.tablet_reader_init_keys_param_timer_ns);
857
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_orderby_keys_param_timer,
858
1.03M
                   stats.tablet_reader_init_orderby_keys_param_timer_ns);
859
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_conditions_param_timer,
860
1.03M
                   stats.tablet_reader_init_conditions_param_timer_ns);
861
1.03M
    COUNTER_UPDATE(local_state->_tablet_reader_init_delete_condition_param_timer,
862
1.03M
                   stats.tablet_reader_init_delete_condition_param_timer_ns);
863
1.03M
    COUNTER_UPDATE(local_state->_block_reader_vcollect_iter_init_timer,
864
1.03M
                   stats.block_reader_vcollect_iter_init_timer_ns);
865
1.03M
    COUNTER_UPDATE(local_state->_block_reader_rs_readers_init_timer,
866
1.03M
                   stats.block_reader_rs_readers_init_timer_ns);
867
1.03M
    COUNTER_UPDATE(local_state->_block_reader_build_heap_init_timer,
868
1.03M
                   stats.block_reader_build_heap_init_timer_ns);
869
870
1.03M
    COUNTER_UPDATE(local_state->_rowset_reader_get_segment_iterators_timer,
871
1.03M
                   stats.rowset_reader_get_segment_iterators_timer_ns);
872
1.03M
    COUNTER_UPDATE(local_state->_rowset_reader_create_iterators_timer,
873
1.03M
                   stats.rowset_reader_create_iterators_timer_ns);
874
1.03M
    COUNTER_UPDATE(local_state->_rowset_reader_init_iterators_timer,
875
1.03M
                   stats.rowset_reader_init_iterators_timer_ns);
876
1.03M
    COUNTER_UPDATE(local_state->_rowset_reader_load_segments_timer,
877
1.03M
                   stats.rowset_reader_load_segments_timer_ns);
878
879
1.03M
    COUNTER_UPDATE(local_state->_segment_iterator_init_timer, stats.segment_iterator_init_timer_ns);
880
1.03M
    COUNTER_UPDATE(local_state->_segment_iterator_init_return_column_iterators_timer,
881
1.03M
                   stats.segment_iterator_init_return_column_iterators_timer_ns);
882
1.03M
    COUNTER_UPDATE(local_state->_segment_iterator_init_index_iterators_timer,
883
1.03M
                   stats.segment_iterator_init_index_iterators_timer_ns);
884
1.03M
    COUNTER_UPDATE(local_state->_segment_iterator_init_segment_prefetchers_timer,
885
1.03M
                   stats.segment_iterator_init_segment_prefetchers_timer_ns);
886
887
1.03M
    COUNTER_UPDATE(local_state->_segment_create_column_readers_timer,
888
1.03M
                   stats.segment_create_column_readers_timer_ns);
889
1.03M
    COUNTER_UPDATE(local_state->_segment_load_index_timer, stats.segment_load_index_timer_ns);
890
891
    // Update metrics
892
1.03M
    DorisMetrics::instance()->query_scan_bytes->increment(
893
1.03M
            local_state->_read_uncompressed_counter->value());
894
1.03M
    DorisMetrics::instance()->query_scan_rows->increment(local_state->_scan_rows->value());
895
1.03M
    auto& tablet = _tablet_reader_params.tablet;
896
1.03M
    tablet->query_scan_bytes->increment(local_state->_read_uncompressed_counter->value());
897
1.03M
    tablet->query_scan_rows->increment(local_state->_scan_rows->value());
898
1.03M
    tablet->query_scan_count->increment(1);
899
900
1.03M
    COUNTER_UPDATE(local_state->_ann_range_search_filter_counter,
901
1.03M
                   stats.rows_ann_index_range_filtered);
902
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_filter_counter, stats.rows_ann_index_topn_filtered);
903
1.03M
    COUNTER_UPDATE(local_state->_ann_index_load_costs, stats.ann_index_load_ns);
904
1.03M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_load_costs, stats.ann_ivf_on_disk_load_ns);
905
1.03M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_cache_hit_cnt,
906
1.03M
                   stats.ann_ivf_on_disk_cache_hit_cnt);
907
1.03M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_cache_miss_cnt,
908
1.03M
                   stats.ann_ivf_on_disk_cache_miss_cnt);
909
1.03M
    COUNTER_UPDATE(local_state->_ann_range_search_costs, stats.ann_index_range_search_ns);
910
1.03M
    COUNTER_UPDATE(local_state->_ann_range_search_cnt, stats.ann_index_range_search_cnt);
911
1.03M
    COUNTER_UPDATE(local_state->_ann_range_engine_search_costs, stats.ann_range_engine_search_ns);
912
    // Engine prepare before search
913
1.03M
    COUNTER_UPDATE(local_state->_ann_range_pre_process_costs, stats.ann_range_pre_process_ns);
914
    // Post process parent: Doris result process + engine convert
915
1.03M
    COUNTER_UPDATE(local_state->_ann_range_post_process_costs,
916
1.03M
                   stats.ann_range_result_convert_ns + stats.ann_range_engine_convert_ns);
917
    // Engine convert (child under post-process)
918
1.03M
    COUNTER_UPDATE(local_state->_ann_range_engine_convert_costs, stats.ann_range_engine_convert_ns);
919
    // Doris-side result convert (child under post-process)
920
1.03M
    COUNTER_UPDATE(local_state->_ann_range_result_convert_costs, stats.ann_range_result_convert_ns);
921
922
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_search_costs, stats.ann_topn_search_ns);
923
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_search_cnt, stats.ann_index_topn_search_cnt);
924
925
    // Detailed ANN timers
926
    // ANN TopN timers with hierarchy
927
    // Engine search time (FAISS)
928
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_engine_search_costs,
929
1.03M
                   stats.ann_index_topn_engine_search_ns);
930
    // Engine prepare time (allocations/buffer setup before search)
931
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_pre_process_costs,
932
1.03M
                   stats.ann_index_topn_engine_prepare_ns);
933
    // Post process parent includes Doris result processing + engine convert
934
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_post_process_costs,
935
1.03M
                   stats.ann_index_topn_result_process_ns + stats.ann_index_topn_engine_convert_ns);
936
    // Engine-side conversion time inside FAISS wrappers (child under post-process)
937
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_engine_convert_costs,
938
1.03M
                   stats.ann_index_topn_engine_convert_ns);
939
940
    // Doris-side result convert costs (show separately as another child counter); use pure process time
941
1.03M
    COUNTER_UPDATE(local_state->_ann_topn_result_convert_costs,
942
1.03M
                   stats.ann_index_topn_result_process_ns);
943
944
1.03M
    COUNTER_UPDATE(local_state->_ann_fallback_brute_force_cnt, stats.ann_fall_back_brute_force_cnt);
945
946
    // Overhead counter removed; precise instrumentation is reported via engine_prepare above.
947
1.03M
}
948
949
#include "common/compile_check_avoid_end.h"
950
} // namespace doris