Coverage Report

Created: 2026-03-15 15:59

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