Coverage Report

Created: 2026-06-26 13:27

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 "core/data_type/data_type_number.h"
42
#include "exec/common/variant_util.h"
43
#include "exec/operator/olap_scan_operator.h"
44
#include "exec/scan/predicate_lm_utils.h"
45
#include "exec/scan/scan_node.h"
46
#include "exprs/function_filter.h"
47
#include "exprs/vexpr.h"
48
#include "exprs/vexpr_context.h"
49
#include "io/cache/block_file_cache_profile.h"
50
#include "io/io_common.h"
51
#include "runtime/descriptors.h"
52
#include "runtime/exec_env.h"
53
#include "runtime/runtime_profile.h"
54
#include "runtime/runtime_state.h"
55
#include "service/backend_options.h"
56
#include "storage/binlog.h"
57
#include "storage/id_manager.h"
58
#include "storage/index/inverted/inverted_index_profile.h"
59
#include "storage/iterator/block_reader.h"
60
#include "storage/olap_common.h"
61
#include "storage/olap_tuple.h"
62
#include "storage/olap_utils.h"
63
#include "storage/predicate/predicate_creator.h"
64
#include "storage/storage_engine.h"
65
#include "storage/tablet/tablet_schema.h"
66
#ifndef NDEBUG
67
#include "util/debug_points.h"
68
#endif
69
#include "util/json/path_in_data.h"
70
#include "util/string_util.h"
71
72
namespace doris {
73
#include "common/compile_check_avoid_begin.h"
74
75
using ReadSource = TabletReadSource;
76
77
OlapScanner::OlapScanner(ScanLocalStateBase* parent, OlapScanner::Params&& params)
78
1.22M
        : Scanner(params.state, parent, params.limit, params.profile),
79
1.22M
          _key_ranges(std::move(params.key_ranges)),
80
1.22M
          _tablet_reader_params({.tablet = std::move(params.tablet),
81
1.22M
                                 .tablet_schema {},
82
1.22M
                                 .reader_type = params.read_row_binlog ? ReaderType::READER_BINLOG
83
1.22M
                                                                       : ReaderType::READER_QUERY,
84
1.22M
                                 .aggregation = params.aggregation,
85
1.22M
                                 .version = {0, params.version},
86
1.22M
                                 .start_key {},
87
1.22M
                                 .end_key {},
88
1.22M
                                 .predicates {},
89
1.22M
                                 .function_filters {},
90
1.22M
                                 .delete_predicates {},
91
1.22M
                                 .target_cast_type_for_variants {},
92
1.22M
                                 .all_access_paths {},
93
1.22M
                                 .predicate_access_paths {},
94
1.22M
                                 .rs_splits {},
95
1.22M
                                 .return_columns {},
96
1.22M
                                 .output_columns {},
97
1.22M
                                 .common_expr_ctxs_push_down {},
98
1.22M
                                 .topn_filter_source_node_ids {},
99
1.22M
                                 .key_group_cluster_key_idxes {},
100
1.22M
                                 .virtual_column_exprs {},
101
1.22M
                                 .vir_cid_to_idx_in_block {},
102
1.22M
                                 .vir_col_idx_to_type {},
103
1.22M
                                 .score_runtime {},
104
1.22M
                                 .collection_statistics {},
105
1.22M
                                 .ann_topn_runtime {},
106
1.22M
                                 .condition_cache_digest = parent->get_condition_cache_digest(),
107
1.22M
                                 .predicate_lm_stage1_column_ids {},
108
1.22M
                                 .binlog_scan_type = params.binlog_scan_type}),
109
1.22M
          _start_tso(params.start_tso),
110
1.22M
          _end_tso(params.end_tso) {
111
1.22M
    _tablet_reader_params.set_read_source(std::move(params.read_source),
112
1.22M
                                          _state->skip_delete_bitmap());
113
1.22M
    _has_prepared = false;
114
1.22M
    _vector_search_params = params.state->get_vector_search_params();
115
1.22M
}
116
117
static std::string read_columns_to_string(TabletSchemaSPtr tablet_schema,
118
4.66k
                                          const std::vector<uint32_t>& read_columns) {
119
    // avoid too long for one line,
120
    // it is hard to display in `show profile` stmt if one line is too long.
121
4.66k
    const int col_per_line = 10;
122
4.66k
    int i = 0;
123
4.66k
    std::string read_columns_string;
124
4.66k
    read_columns_string += "[";
125
19.5k
    for (auto it = read_columns.cbegin(); it != read_columns.cend(); it++) {
126
14.8k
        if (it != read_columns.cbegin()) {
127
10.3k
            read_columns_string += ", ";
128
10.3k
        }
129
14.8k
        read_columns_string += tablet_schema->columns().at(*it)->name();
130
14.8k
        if (i >= col_per_line) {
131
13
            read_columns_string += "\n";
132
13
            i = 0;
133
14.8k
        } else {
134
14.8k
            ++i;
135
14.8k
        }
136
14.8k
    }
137
4.66k
    read_columns_string += "]";
138
4.66k
    return read_columns_string;
139
4.66k
}
140
141
2.50M
static bool has_file_cache_statistics(const io::FileCacheStatistics& stats) {
142
2.50M
    return stats.num_local_io_total != 0 || stats.num_remote_io_total != 0 ||
143
2.50M
           stats.num_peer_io_total != 0 || stats.local_io_timer != 0 ||
144
2.50M
           stats.bytes_read_from_local != 0 || stats.bytes_read_from_remote != 0 ||
145
2.50M
           stats.bytes_read_from_peer != 0 || stats.remote_io_timer != 0 ||
146
2.50M
           stats.peer_io_timer != 0 || stats.remote_wait_timer != 0 ||
147
2.50M
           stats.write_cache_io_timer != 0 || stats.bytes_write_into_cache != 0 ||
148
2.50M
           stats.num_skip_cache_io_total != 0 || stats.read_cache_file_directly_timer != 0 ||
149
2.50M
           stats.cache_get_or_set_timer != 0 || stats.lock_wait_timer != 0 ||
150
2.50M
           stats.get_timer != 0 || stats.set_timer != 0 ||
151
2.50M
           stats.inverted_index_num_local_io_total != 0 ||
152
2.50M
           stats.inverted_index_num_remote_io_total != 0 ||
153
2.50M
           stats.inverted_index_num_peer_io_total != 0 ||
154
2.50M
           stats.inverted_index_bytes_read_from_local != 0 ||
155
2.50M
           stats.inverted_index_bytes_read_from_remote != 0 ||
156
2.50M
           stats.inverted_index_bytes_read_from_peer != 0 ||
157
2.50M
           stats.inverted_index_local_io_timer != 0 || stats.inverted_index_remote_io_timer != 0 ||
158
2.50M
           stats.inverted_index_peer_io_timer != 0 || stats.inverted_index_io_timer != 0;
159
2.50M
}
160
161
1.22M
Status OlapScanner::_prepare_impl() {
162
1.22M
    auto* local_state = static_cast<OlapScanLocalState*>(_local_state);
163
1.22M
    auto& tablet = _tablet_reader_params.tablet;
164
1.22M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
165
1.22M
    DBUG_EXECUTE_IF("CloudTablet.capture_rs_readers.return.e-230", {
166
1.22M
        LOG_WARNING("CloudTablet.capture_rs_readers.return e-230 init")
167
1.22M
                .tag("tablet_id", tablet->tablet_id());
168
1.22M
        return Status::Error<false>(-230, "injected error");
169
1.22M
    });
170
171
1.22M
    for (auto& ctx : local_state->_common_expr_ctxs_push_down) {
172
22.3k
        VExprContextSPtr context;
173
22.3k
        RETURN_IF_ERROR(ctx->clone(_state, context));
174
22.3k
        _common_expr_ctxs_push_down.emplace_back(context);
175
22.3k
        context->prepare_ann_range_search(_vector_search_params);
176
22.3k
    }
177
178
1.22M
    for (auto pair : local_state->_slot_id_to_virtual_column_expr) {
179
        // Scanner will be executed in a different thread, so we need to clone the context.
180
385
        VExprContextSPtr context;
181
385
        RETURN_IF_ERROR(pair.second->clone(_state, context));
182
385
        _slot_id_to_virtual_column_expr[pair.first] = context;
183
385
    }
184
185
1.22M
    _slot_id_to_index_in_block = local_state->_slot_id_to_index_in_block;
186
1.22M
    _slot_id_to_col_type = local_state->_slot_id_to_col_type;
187
1.22M
    _score_runtime = local_state->_score_runtime;
188
    // All scanners share the same ann_topn_runtime.
189
1.22M
    _ann_topn_runtime = local_state->_ann_topn_runtime;
190
191
    // set limit to reduce end of rowset and segment mem use
192
1.22M
    _tablet_reader = std::make_unique<BlockReader>();
193
    // batch size is passed down to segment iterator, use _state->batch_size()
194
    // instead of _parent->limit(), because if _parent->limit() is a very small
195
    // value (e.g. select a from t where a .. and b ... limit 1),
196
    // it will be very slow when reading data in segment iterator
197
1.22M
    _tablet_reader->set_batch_size(_state->batch_size());
198
    // Adaptive batch size: pass byte-budget settings to the storage reader.
199
    // The reader still uses batch_size() as the row ceiling.
200
1.22M
    _tablet_reader->set_preferred_block_size_bytes(_state->preferred_block_size_bytes());
201
1.22M
    {
202
1.22M
        TOlapScanNode& olap_scan_node = local_state->olap_scan_node();
203
1.22M
        TabletSchemaSPtr source_tablet_schema =
204
1.22M
                _tablet_reader_params.reader_type == ReaderType::READER_BINLOG
205
1.22M
                        ? tablet->row_binlog_tablet_schema()
206
1.22M
                        : tablet->tablet_schema();
207
208
1.22M
        tablet_schema = std::make_shared<TabletSchema>();
209
1.22M
        tablet_schema->copy_from(*source_tablet_schema);
210
1.22M
        if (olap_scan_node.__isset.columns_desc && !olap_scan_node.columns_desc.empty() &&
211
1.22M
            olap_scan_node.columns_desc[0].col_unique_id >= 0) {
212
1.22M
            tablet_schema->clear_columns();
213
19.3M
            for (const auto& column_desc : olap_scan_node.columns_desc) {
214
19.3M
                tablet_schema->append_column(TabletColumn(column_desc));
215
19.3M
            }
216
1.22M
            if (olap_scan_node.__isset.schema_version) {
217
1.22M
                tablet_schema->set_schema_version(olap_scan_node.schema_version);
218
1.22M
            }
219
1.22M
        }
220
1.22M
        if (olap_scan_node.__isset.indexes_desc) {
221
1.22M
            tablet_schema->update_indexes_from_thrift(olap_scan_node.indexes_desc);
222
1.22M
        }
223
224
1.22M
        const auto& qopts = _state->query_options();
225
226
        // Map FE session variable enable_multi_stage_predicate_lm -> storage read option flag.
227
        // This flag is what SegmentIterator actually checks.
228
1.22M
        if (qopts.__isset.enable_multi_stage_predicate_lm) {
229
1.22M
            _tablet_reader_params.enable_multi_stage_predicate_lazy_materialization =
230
1.22M
                    qopts.enable_multi_stage_predicate_lm;
231
1.22M
        }
232
233
        // Map FE session variable predicate_lm_stage1_survival_ratio_threshold -> stage2 strategy threshold.
234
1.22M
        if (qopts.__isset.predicate_lm_stage1_survival_ratio_threshold) {
235
1.22M
            _tablet_reader_params.predicate_lm_stage1_survival_ratio_threshold =
236
1.22M
                    qopts.predicate_lm_stage1_survival_ratio_threshold;
237
1.22M
        }
238
239
        // Map FE session variable predicate_lm_stage1_cols -> ColumnId list
240
        // NOTE: only meaningful when multi-stage predicate LM is enabled.
241
1.22M
        if (_tablet_reader_params.enable_multi_stage_predicate_lazy_materialization) {
242
12
            if (qopts.__isset.predicate_lm_stage1_cols && !qopts.predicate_lm_stage1_cols.empty()) {
243
12
                std::vector<ColumnId> stage1_column_ids;
244
12
                RETURN_IF_ERROR(parse_predicate_lm_stage1_cols_to_column_ids(
245
12
                        qopts.predicate_lm_stage1_cols, tablet_schema,
246
12
                        olap_scan_node.__isset.db_name ? std::string_view(olap_scan_node.db_name)
247
12
                                                       : std::string_view(),
248
12
                        olap_scan_node.table_name, &stage1_column_ids));
249
12
                _tablet_reader_params.predicate_lm_stage1_column_ids = std::move(stage1_column_ids);
250
12
            }
251
12
        }
252
253
1.22M
        if (_tablet_reader_params.rs_splits.empty()) {
254
            // Non-pipeline mode, Tablet : Scanner = 1 : 1
255
            // acquire tablet rowset readers at the beginning of the scan node
256
            // to prevent this case: when there are lots of olap scanners to run for example 10000
257
            // the rowsets maybe compacted when the last olap scanner starts
258
0
            ReadSource read_source;
259
260
0
            if (config::is_cloud_mode()) {
261
                // FIXME(plat1ko): Avoid pointer cast
262
0
                ExecEnv::GetInstance()->storage_engine().to_cloud().tablet_hotspot().count(*tablet);
263
0
            }
264
265
0
            auto maybe_read_source = tablet->capture_read_source(
266
0
                    _tablet_reader_params.version,
267
0
                    {
268
0
                            .skip_missing_versions = _state->skip_missing_version(),
269
0
                            .enable_fetch_rowsets_from_peers =
270
0
                                    config::enable_fetch_rowsets_from_peer_replicas,
271
0
                            .capture_row_binlog =
272
0
                                    _tablet_reader_params.reader_type == ReaderType::READER_BINLOG,
273
0
                            .enable_prefer_cached_rowset =
274
0
                                    config::is_cloud_mode() ? _state->enable_prefer_cached_rowset()
275
0
                                                            : false,
276
0
                            .query_freshness_tolerance_ms =
277
0
                                    config::is_cloud_mode() ? _state->query_freshness_tolerance_ms()
278
0
                                                            : -1,
279
0
                    });
280
0
            if (!maybe_read_source) {
281
0
                LOG(WARNING) << "fail to init reader. res=" << maybe_read_source.error();
282
0
                return maybe_read_source.error();
283
0
            }
284
0
            read_source = std::move(maybe_read_source.value());
285
286
0
            if (config::enable_mow_verbose_log && tablet->enable_unique_key_merge_on_write()) {
287
0
                LOG_INFO("finish capture_rs_readers for tablet={}, query_id={}",
288
0
                         tablet->tablet_id(), print_id(_state->query_id()));
289
0
            }
290
291
0
            if (!_state->skip_delete_predicate()) {
292
0
                read_source.fill_delete_predicates();
293
0
            }
294
0
            _tablet_reader_params.set_read_source(std::move(read_source));
295
0
        }
296
297
        // Initialize tablet_reader_params
298
1.22M
        RETURN_IF_ERROR(_init_tablet_reader_params(
299
1.22M
                local_state->_parent->cast<OlapScanOperatorX>()._slot_id_to_slot_desc, _key_ranges,
300
1.22M
                local_state->_slot_id_to_predicates, local_state->_push_down_functions));
301
1.22M
    }
302
303
    // add read columns in profile
304
1.22M
    if (_state->enable_profile()) {
305
4.64k
        _profile->add_info_string("ReadColumns",
306
4.64k
                                  read_columns_to_string(tablet_schema, _return_columns));
307
4.64k
    }
308
309
1.22M
    if (_tablet_reader_params.score_runtime) {
310
14
        SCOPED_TIMER(local_state->_statistics_collect_timer);
311
14
        _tablet_reader_params.collection_statistics = std::make_shared<CollectionStatistics>();
312
313
14
        io::IOContext io_ctx {
314
14
                .reader_type = _tablet_reader_params.reader_type,
315
14
                .expiration_time = tablet->ttl_seconds(),
316
14
                .query_id = &_state->query_id(),
317
14
                .file_cache_stats = &_tablet_reader->mutable_stats()->file_cache_stats,
318
14
                .is_inverted_index = true,
319
14
        };
320
321
14
        RETURN_IF_ERROR(_tablet_reader_params.collection_statistics->collect(
322
14
                _state, _tablet_reader_params.rs_splits, _tablet_reader_params.tablet_schema,
323
14
                _tablet_reader_params.common_expr_ctxs_push_down, &io_ctx));
324
14
    }
325
326
1.22M
    _has_prepared = true;
327
1.22M
    return Status::OK();
328
1.22M
}
329
330
1.22M
Status OlapScanner::_open_impl(RuntimeState* state) {
331
1.22M
    RETURN_IF_ERROR(Scanner::_open_impl(state));
332
1.22M
    SCOPED_TIMER(_local_state->cast<OlapScanLocalState>()._reader_init_timer);
333
334
1.22M
    auto res = _tablet_reader->init(_tablet_reader_params);
335
1.22M
    if (!res.ok()) {
336
        // init() also runs the eager first-row read that evaluates pushed-down expressions,
337
        // so res may be a data/expression error rather than a storage failure. Keep its own
338
        // message and only append the tablet/backend, without a misleading storage wording.
339
40
        res.append(". tablet=" + std::to_string(_tablet_reader_params.tablet->tablet_id()) +
340
40
                   ", backend=" + BackendOptions::get_localhost());
341
40
        return res;
342
40
    }
343
344
    // Do not hold rs_splits any more to release memory.
345
1.22M
    _tablet_reader_params.rs_splits.clear();
346
347
1.22M
    return Status::OK();
348
1.22M
}
349
350
// For binlog partition-based incremental read. Pushes down [start_tso, end_tso] range
351
// predicates onto the binlog timestamp column for row-binlog scans. Also ensures the
352
// timestamp column is part of return_columns so the predicates can be evaluated by the
353
// storage layer.
354
1.22M
Status OlapScanner::_init_row_binlog_tso_predicates() {
355
1.22M
    if (_tablet_reader_params.reader_type != ReaderType::READER_BINLOG) {
356
1.22M
        return Status::OK();
357
1.22M
    }
358
359
18.4E
    if (!_start_tso.has_value() && !_end_tso.has_value()) {
360
0
        return Status::OK();
361
0
    }
362
363
18.4E
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
364
18.4E
    int32_t tso_index = tablet_schema->binlog_timestamp_col_idx();
365
18.4E
    if (tso_index < 0) {
366
0
        return Status::InternalError("Column {} not found in tablet schema after append",
367
0
                                     BINLOG_TIMESTAMP_COL);
368
0
    }
369
370
18.4E
    auto data_type = std::make_shared<DataTypeInt64>();
371
18.4E
    if (_start_tso.has_value()) {
372
0
        Field start_value = Field::create_field<TYPE_BIGINT>(*_start_tso);
373
0
        _tablet_reader_params.predicates.push_back(create_comparison_predicate<PredicateType::GT>(
374
0
                tso_index, std::string(kRowBinlogTimestampColName), data_type, start_value, false));
375
0
    }
376
18.4E
    if (_end_tso.has_value()) {
377
0
        Field end_value = Field::create_field<TYPE_BIGINT>(*_end_tso);
378
0
        _tablet_reader_params.predicates.push_back(create_comparison_predicate<PredicateType::LE>(
379
0
                tso_index, std::string(kRowBinlogTimestampColName), data_type, end_value, false));
380
0
    }
381
382
18.4E
    if (std::find(_tablet_reader_params.return_columns.begin(),
383
18.4E
                  _tablet_reader_params.return_columns.end(),
384
18.4E
                  tso_index) == _tablet_reader_params.return_columns.end()) {
385
0
        _tablet_reader_params.return_columns.push_back(tso_index);
386
0
    }
387
388
18.4E
    return Status::OK();
389
18.4E
}
390
391
// it will be called under tablet read lock because capture rs readers need
392
Status OlapScanner::_init_tablet_reader_params(
393
        const phmap::flat_hash_map<int, SlotDescriptor*>& slot_id_to_slot_desc,
394
        const std::vector<OlapScanRange*>& key_ranges,
395
        const phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
396
                slot_to_predicates,
397
1.22M
        const std::vector<FunctionFilter>& function_filters) {
398
    // if the table with rowset [0-x] or [0-1] [2-y], and [0-1] is empty
399
1.22M
    const bool single_version = _tablet_reader_params.has_single_version();
400
401
1.22M
    auto* olap_local_state = static_cast<OlapScanLocalState*>(_local_state);
402
1.22M
    bool read_mor_as_dup = olap_local_state->olap_scan_node().__isset.read_mor_as_dup &&
403
1.22M
                           olap_local_state->olap_scan_node().read_mor_as_dup;
404
1.22M
    if (_state->skip_storage_engine_merge() || read_mor_as_dup) {
405
49
        _tablet_reader_params.direct_mode = true;
406
49
        _tablet_reader_params.aggregation = true;
407
1.22M
    } else {
408
1.22M
        auto push_down_agg_type = _local_state->get_push_down_agg_type();
409
1.22M
        _tablet_reader_params.direct_mode = _tablet_reader_params.aggregation || single_version ||
410
1.22M
                                            (push_down_agg_type != TPushAggOp::NONE &&
411
10.5k
                                             push_down_agg_type != TPushAggOp::COUNT_ON_INDEX);
412
1.22M
    }
413
414
1.22M
    RETURN_IF_ERROR(_init_variant_columns());
415
1.22M
    RETURN_IF_ERROR(_init_return_columns());
416
417
1.22M
    _tablet_reader_params.push_down_agg_type_opt = _local_state->get_push_down_agg_type();
418
419
    // Binlog DETAIL/MIN_DELTA scans widen `return_columns` with key/op/lsn/before
420
    // columns to drive the row-level merge in BlockReader. The storage-layer
421
    // statistics fast path (VStatisticsIterator, picked when push_down_agg_type
422
    // is COUNT/MINMAX) bypasses SegmentIterator entirely, returning raw segment
423
    // row counts without binlog op filtering and with a schema that does not
424
    // match the widened read schema. The result is both wrong (raw segment
425
    // count != binlog row count) and unsafe (column-count DCHECK fires inside
426
    // VStatisticsIterator::next_batch). Disable the fast path for these scans.
427
1.22M
    if (_tablet_reader_params.binlog_scan_type == TBinlogScanType::DETAIL ||
428
1.22M
        _tablet_reader_params.binlog_scan_type == TBinlogScanType::MIN_DELTA) {
429
0
        _tablet_reader_params.push_down_agg_type_opt = TPushAggOp::NONE;
430
0
    }
431
432
1.22M
    _tablet_reader_params.common_expr_ctxs_push_down = _common_expr_ctxs_push_down;
433
1.22M
    _tablet_reader_params.virtual_column_exprs = _virtual_column_exprs;
434
1.22M
    _tablet_reader_params.vir_cid_to_idx_in_block = _vir_cid_to_idx_in_block;
435
1.22M
    _tablet_reader_params.vir_col_idx_to_type = _vir_col_idx_to_type;
436
1.22M
    _tablet_reader_params.score_runtime = _score_runtime;
437
1.22M
    _tablet_reader_params.output_columns = ((OlapScanLocalState*)_local_state)->_output_column_ids;
438
1.22M
    _tablet_reader_params.ann_topn_runtime = _ann_topn_runtime;
439
1.22M
    for (const auto& ele : ((OlapScanLocalState*)_local_state)->_cast_types_for_variants) {
440
1.67k
        _tablet_reader_params.target_cast_type_for_variants[ele.first] = ele.second;
441
1.67k
    };
442
1.22M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
443
9.83M
    for (auto& predicates : slot_to_predicates) {
444
9.83M
        const int sid = predicates.first;
445
9.83M
        DCHECK(slot_id_to_slot_desc.contains(sid));
446
9.83M
        int32_t index =
447
9.83M
                tablet_schema->field_index(slot_id_to_slot_desc.find(sid)->second->col_name());
448
9.83M
        if (index < 0) {
449
0
            throw Exception(
450
0
                    Status::InternalError("Column {} not found in tablet schema",
451
0
                                          slot_id_to_slot_desc.find(sid)->second->col_name()));
452
0
        }
453
9.83M
        for (auto& predicate : predicates.second) {
454
992k
            _tablet_reader_params.predicates.push_back(predicate->clone(index));
455
992k
        }
456
9.83M
    }
457
458
1.22M
    std::copy(function_filters.cbegin(), function_filters.cend(),
459
1.22M
              std::inserter(_tablet_reader_params.function_filters,
460
1.22M
                            _tablet_reader_params.function_filters.begin()));
461
462
    // Merge the columns in delete predicate that not in latest schema in to current tablet schema
463
1.22M
    for (auto& del_pred : _tablet_reader_params.delete_predicates) {
464
6.80k
        tablet_schema->merge_dropped_columns(*del_pred->tablet_schema());
465
6.80k
    }
466
467
    // Push key ranges to the tablet reader.
468
    // Skip the "full scan" placeholder (has_lower_bound == false) — when no key
469
    // predicates exist, start_key/end_key remain empty and the reader does a full scan.
470
1.78M
    for (auto* key_range : key_ranges) {
471
1.78M
        if (!key_range->has_lower_bound) {
472
188k
            continue;
473
188k
        }
474
475
1.59M
        _tablet_reader_params.start_key_include = key_range->begin_include;
476
1.59M
        _tablet_reader_params.end_key_include = key_range->end_include;
477
478
1.59M
        _tablet_reader_params.start_key.push_back(key_range->begin_scan_range);
479
1.59M
        _tablet_reader_params.end_key.push_back(key_range->end_scan_range);
480
1.59M
    }
481
482
1.22M
    _tablet_reader_params.profile = _local_state->custom_profile();
483
1.22M
    _tablet_reader_params.runtime_state = _state;
484
485
1.22M
    _tablet_reader_params.origin_return_columns = &_return_columns;
486
1.22M
    _tablet_reader_params.tablet_columns_convert_to_null_set = &_tablet_columns_convert_to_null_set;
487
488
1.22M
    auto add_return_column_if_absent = [&](uint32_t cid) {
489
0
        if (std::find(_tablet_reader_params.return_columns.begin(),
490
0
                      _tablet_reader_params.return_columns.end(),
491
0
                      cid) == _tablet_reader_params.return_columns.end()) {
492
0
            _tablet_reader_params.return_columns.push_back(cid);
493
0
        }
494
0
    };
495
496
    // For row-binlog scans that emit BEFORE/AFTER pairs (MIN_DELTA / DETAIL), we must read
497
    // every key column, every requested value column, the binlog meta columns (op / lsn /
498
    // tso) and their __BEFORE__ mirrors, so the BlockReader can reconstruct change rows.
499
1.22M
    const bool need_before_columns =
500
1.22M
            _tablet_reader_params.binlog_scan_type == TBinlogScanType::MIN_DELTA ||
501
1.22M
            _tablet_reader_params.binlog_scan_type == TBinlogScanType::DETAIL;
502
1.22M
    if (need_before_columns) {
503
0
        for (size_t i = 0; i < tablet_schema->num_key_columns(); ++i) {
504
0
            add_return_column_if_absent(static_cast<uint32_t>(i));
505
0
        }
506
0
        for (auto cid : _return_columns) {
507
0
            add_return_column_if_absent(cid);
508
0
        }
509
510
0
        if (int32_t op_idx = tablet_schema->field_index(std::string(kRowBinlogOpColName));
511
0
            op_idx >= 0) {
512
0
            add_return_column_if_absent(static_cast<uint32_t>(op_idx));
513
0
        }
514
0
        if (int32_t lsn_idx = tablet_schema->binlog_lsn_col_idx(); lsn_idx >= 0) {
515
0
            add_return_column_if_absent(static_cast<uint32_t>(lsn_idx));
516
0
        }
517
518
0
        for (auto cid : _return_columns) {
519
0
            if (cid >= tablet_schema->num_key_columns()) {
520
0
                const auto& col_name = tablet_schema->column(cid).name();
521
0
                std::string before_col_name;
522
0
                before_col_name.append("__BEFORE__");
523
0
                before_col_name.append(col_name);
524
0
                before_col_name.append("__");
525
0
                if (int32_t before_idx = tablet_schema->field_index(before_col_name);
526
0
                    before_idx >= 0) {
527
0
                    add_return_column_if_absent(static_cast<uint32_t>(before_idx));
528
0
                }
529
0
            }
530
0
        }
531
1.22M
    } else if (_tablet_reader_params.direct_mode) {
532
1.21M
        _tablet_reader_params.return_columns = _return_columns;
533
1.21M
    } else {
534
        // we need to fetch all key columns to do the right aggregation on storage engine side.
535
39.9k
        for (size_t i = 0; i < tablet_schema->num_key_columns(); ++i) {
536
27.3k
            _tablet_reader_params.return_columns.push_back(i);
537
27.3k
        }
538
49.6k
        for (auto index : _return_columns) {
539
49.6k
            if (tablet_schema->column(index).is_key()) {
540
18.4k
                continue;
541
18.4k
            }
542
31.1k
            _tablet_reader_params.return_columns.push_back(index);
543
31.1k
        }
544
        // expand the sequence column
545
12.6k
        if (tablet_schema->has_sequence_col() || tablet_schema->has_seq_map()) {
546
40
            bool has_replace_col = false;
547
91
            for (auto col : _return_columns) {
548
91
                if (tablet_schema->column(col).aggregation() ==
549
91
                    FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE) {
550
40
                    has_replace_col = true;
551
40
                    break;
552
40
                }
553
91
            }
554
40
            if (auto sequence_col_idx = tablet_schema->sequence_col_idx();
555
40
                has_replace_col && tablet_schema->has_sequence_col() &&
556
40
                std::find(_return_columns.begin(), _return_columns.end(), sequence_col_idx) ==
557
28
                        _return_columns.end()) {
558
16
                _tablet_reader_params.return_columns.push_back(sequence_col_idx);
559
16
            }
560
40
            if (has_replace_col) {
561
40
                const auto& val_to_seq = tablet_schema->value_col_idx_to_seq_col_idx();
562
40
                std::set<uint32_t> return_seq_columns;
563
564
235
                for (auto col : _tablet_reader_params.return_columns) {
565
                    // we need to add the necessary sequence column in _return_columns, and
566
                    // Avoid adding the same seq column twice
567
235
                    const auto val_iter = val_to_seq.find(col);
568
235
                    if (val_iter != val_to_seq.end()) {
569
42
                        auto seq = val_iter->second;
570
42
                        if (std::find(_tablet_reader_params.return_columns.begin(),
571
42
                                      _tablet_reader_params.return_columns.end(),
572
42
                                      seq) == _tablet_reader_params.return_columns.end()) {
573
4
                            return_seq_columns.insert(seq);
574
4
                        }
575
42
                    }
576
235
                }
577
40
                _tablet_reader_params.return_columns.insert(
578
40
                        std::end(_tablet_reader_params.return_columns),
579
40
                        std::begin(return_seq_columns), std::end(return_seq_columns));
580
40
            }
581
40
        }
582
12.6k
    }
583
584
1.22M
    RETURN_IF_ERROR(_init_row_binlog_tso_predicates());
585
586
    // For any row-binlog scan, force the storage layer to deliver rows strictly in primary-key
587
    // order so the BlockReader can group consecutive same-key changes (MIN_DELTA) or emit
588
    // BEFORE/AFTER pairs in deterministic order (DETAIL). Disable ORDER BY / TopN pushdowns
589
    // and reset their related params, since they would otherwise re-order the stream.
590
1.22M
    if (_tablet_reader_params.binlog_scan_type != TBinlogScanType::NONE) {
591
0
        _tablet_reader_params.read_orderby_key = true;
592
0
        _tablet_reader_params.read_orderby_key_reverse = false;
593
0
        _tablet_reader_params.read_orderby_key_num_prefix_columns = 0;
594
0
        _tablet_reader_params.read_orderby_key_limit = 0;
595
0
        _tablet_reader_params.force_key_ordered_read = true;
596
0
        _tablet_reader_params.topn_filter_source_node_ids.clear();
597
0
    }
598
599
1.22M
    _tablet_reader_params.use_page_cache = _state->enable_page_cache();
600
601
1.22M
    DBUG_EXECUTE_IF("NewOlapScanner::_init_tablet_reader_params.block", DBUG_BLOCK);
602
603
1.22M
    if (!_state->skip_storage_engine_merge()) {
604
1.22M
        auto* olap_scan_local_state = (OlapScanLocalState*)_local_state;
605
1.22M
        TOlapScanNode& olap_scan_node = olap_scan_local_state->olap_scan_node();
606
607
        // Set MOR value predicate pushdown flag
608
1.22M
        if (olap_scan_node.__isset.enable_mor_value_predicate_pushdown &&
609
1.22M
            olap_scan_node.enable_mor_value_predicate_pushdown) {
610
25
            _tablet_reader_params.enable_mor_value_predicate_pushdown = true;
611
25
        }
612
613
1.22M
        const bool has_key_topn =
614
1.22M
                olap_scan_node.__isset.sort_info && !olap_scan_node.sort_info.is_asc_order.empty();
615
1.22M
        if (has_key_topn) {
616
2.33k
            _limit = _local_state->limit_per_scanner();
617
2.33k
        }
618
619
1.22M
        const bool no_runtime_filters = _total_rf_num == 0;
620
1.22M
        const bool segment_limit_enabled = _state->enable_segment_limit_pushdown();
621
1.22M
        const bool storage_no_merge = olap_scan_local_state->_storage_no_merge();
622
623
1.22M
        if (_limit > 0 && no_runtime_filters && segment_limit_enabled && storage_no_merge) {
624
1.91k
            for (const auto& conjunct : _conjuncts) {
625
0
                DORIS_CHECK(!olap_scan_local_state->_check_expr_storage_filter(
626
0
                        conjunct->root(), OlapScanLocalState::ExprStorageFilterCheckMode::
627
0
                                                  HAS_SEGMENT_EVALUABLE_EXPR));
628
0
            }
629
1.91k
        }
630
631
        // Segment LIMIT has only two legal states: completely disabled, or enabled after every
632
        // row-filtering conjunct has become a storage predicate or SegmentIterator common expr.
633
1.22M
        const bool can_push_down_segment_limit = _limit > 0 && no_runtime_filters &&
634
1.22M
                                                 _conjuncts.empty() && segment_limit_enabled &&
635
1.22M
                                                 storage_no_merge;
636
1.22M
        if (can_push_down_segment_limit) {
637
1.92k
            if (has_key_topn) {
638
1.45k
                _tablet_reader_params.read_orderby_key = true;
639
1.45k
                if (!olap_scan_node.sort_info.is_asc_order[0]) {
640
230
                    _tablet_reader_params.read_orderby_key_reverse = true;
641
230
                }
642
1.45k
                _tablet_reader_params.read_orderby_key_num_prefix_columns =
643
1.45k
                        olap_scan_node.sort_info.is_asc_order.size();
644
1.45k
                _tablet_reader_params.read_orderby_key_limit = _limit;
645
1.45k
            } else {
646
463
                _tablet_reader_params.general_read_limit = _limit;
647
463
            }
648
1.92k
        }
649
650
1.22M
        if (_tablet_reader_params.read_orderby_key_limit > 0 ||
651
1.22M
            _tablet_reader_params.general_read_limit > 0) {
652
1.92k
            DORIS_CHECK(can_push_down_segment_limit);
653
1.92k
            DORIS_CHECK(_conjuncts.empty());
654
1.92k
        }
655
656
        // A key TopN scan cannot share the plain LIMIT early-stop counter. If
657
        // storage TopN is pushed down, each scanner must produce its full local
658
        // candidates. If it is not pushed down for any reason, the upper TopN
659
        // still needs all rows from the scan.
660
1.22M
        if (has_key_topn) {
661
2.33k
            _shared_scan_limit = nullptr;
662
2.33k
            if (_tablet_reader_params.read_orderby_key_limit == 0) {
663
875
                _limit = -1;
664
875
            }
665
2.33k
        }
666
        // Note: _shared_scan_limit is intentionally not pushed into the
667
        // storage layer. SegmentIterator's _process_eof() is irreversible,
668
        // so a concurrently-decremented atomic could reach 0 while a segment
669
        // still has data needed by other scanners.
670
671
        // set push down topn filter
672
1.22M
        _tablet_reader_params.topn_filter_source_node_ids =
673
1.22M
                olap_scan_local_state->get_topn_filter_source_node_ids(_state, true);
674
1.22M
        if (!_tablet_reader_params.topn_filter_source_node_ids.empty()) {
675
5.58k
            _tablet_reader_params.topn_filter_target_node_id =
676
5.58k
                    olap_scan_local_state->parent()->node_id();
677
5.58k
        }
678
1.22M
    }
679
680
    // If this is a Two-Phase read query, and we need to delay the release of Rowset
681
    // by rowset->update_delayed_expired_timestamp().This could expand the lifespan of Rowset
682
1.22M
    if (tablet_schema->field_index(BeConsts::ROWID_COL) >= 0) {
683
0
        constexpr static int delayed_s = 60;
684
0
        for (auto rs_reader : _tablet_reader_params.rs_splits) {
685
0
            uint64_t delayed_expired_timestamp =
686
0
                    UnixSeconds() + _tablet_reader_params.runtime_state->execution_timeout() +
687
0
                    delayed_s;
688
0
            rs_reader.rs_reader->rowset()->update_delayed_expired_timestamp(
689
0
                    delayed_expired_timestamp);
690
0
            ExecEnv::GetInstance()->storage_engine().add_quering_rowset(
691
0
                    rs_reader.rs_reader->rowset());
692
0
        }
693
0
    }
694
695
1.22M
    if (tablet_schema->has_global_row_id()) {
696
7.86k
        auto& id_file_map = _state->get_id_file_map();
697
14.6k
        for (auto rs_reader : _tablet_reader_params.rs_splits) {
698
14.6k
            id_file_map->add_temp_rowset(rs_reader.rs_reader->rowset());
699
14.6k
        }
700
7.86k
    }
701
702
1.22M
    return Status::OK();
703
1.22M
}
704
705
1.22M
Status OlapScanner::_init_variant_columns() {
706
1.22M
    auto& tablet_schema = _tablet_reader_params.tablet_schema;
707
1.22M
    if (tablet_schema->num_variant_columns() == 0) {
708
1.22M
        return Status::OK();
709
1.22M
    }
710
    // Parent column has path info to distinction from each other
711
13.9k
    for (auto* slot : _output_tuple_desc->slots()) {
712
13.9k
        if (slot->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
713
            // Such columns are not exist in frontend schema info, so we need to
714
            // add them into tablet_schema for later column indexing.
715
7.16k
            const auto& dt_variant =
716
7.16k
                    assert_cast<const DataTypeVariant&>(*remove_nullable(slot->type()));
717
7.16k
            TabletColumn subcol = TabletColumn::create_materialized_variant_column(
718
7.16k
                    tablet_schema->column_by_uid(slot->col_unique_id()).name_lower_case(),
719
7.16k
                    slot->column_paths(), slot->col_unique_id(),
720
7.16k
                    dt_variant.variant_max_subcolumns_count(), dt_variant.enable_doc_mode());
721
7.16k
            if (tablet_schema->field_index(*subcol.path_info_ptr()) < 0) {
722
5.56k
                tablet_schema->append_column(subcol, TabletSchema::ColumnType::VARIANT);
723
5.56k
            }
724
7.16k
        }
725
13.9k
    }
726
4.92k
    variant_util::inherit_column_attributes(tablet_schema);
727
4.92k
    return Status::OK();
728
1.22M
}
729
730
1.22M
Status OlapScanner::_init_return_columns() {
731
12.9M
    for (auto* slot : _output_tuple_desc->slots()) {
732
        // variant column using path to index a column
733
12.9M
        int32_t index = 0;
734
12.9M
        auto& tablet_schema = _tablet_reader_params.tablet_schema;
735
12.9M
        if (slot->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
736
7.17k
            index = tablet_schema->field_index(PathInData(
737
7.17k
                    tablet_schema->column_by_uid(slot->col_unique_id()).name_lower_case(),
738
7.17k
                    slot->column_paths()));
739
12.9M
        } else {
740
12.9M
            index = slot->col_unique_id() >= 0 ? tablet_schema->field_index(slot->col_unique_id())
741
18.4E
                                               : tablet_schema->field_index(slot->col_name());
742
12.9M
        }
743
744
12.9M
        if (index < 0) {
745
0
            return Status::InternalError(
746
0
                    "field name is invalid. field={}, field_name_to_index={}, col_unique_id={}",
747
0
                    slot->col_name(), tablet_schema->get_all_field_names(), slot->col_unique_id());
748
0
        }
749
750
12.9M
        if (slot->get_virtual_column_expr()) {
751
380
            ColumnId virtual_column_cid = index;
752
380
            _virtual_column_exprs[virtual_column_cid] = _slot_id_to_virtual_column_expr[slot->id()];
753
380
            size_t idx_in_block = _slot_id_to_index_in_block[slot->id()];
754
380
            _vir_cid_to_idx_in_block[virtual_column_cid] = idx_in_block;
755
380
            _vir_col_idx_to_type[idx_in_block] = _slot_id_to_col_type[slot->id()];
756
757
380
            VLOG_DEBUG << fmt::format(
758
8
                    "Virtual column, slot id: {}, cid {}, column index: {}, type: {}", slot->id(),
759
8
                    virtual_column_cid, _vir_cid_to_idx_in_block[virtual_column_cid],
760
8
                    _vir_col_idx_to_type[idx_in_block]->get_name());
761
380
        }
762
763
12.9M
        const auto& column = tablet_schema->column(index);
764
12.9M
        int32_t unique_id =
765
18.4E
                column.unique_id() >= 0 ? column.unique_id() : column.parent_unique_id();
766
12.9M
        if (!slot->all_access_paths().empty()) {
767
76.8k
            _tablet_reader_params.all_access_paths.insert({unique_id, slot->all_access_paths()});
768
76.8k
        }
769
770
12.9M
        if (!slot->predicate_access_paths().empty()) {
771
6.28k
            _tablet_reader_params.predicate_access_paths.insert(
772
6.28k
                    {unique_id, slot->predicate_access_paths()});
773
6.28k
        }
774
775
12.9M
        if ((slot->type()->get_primitive_type() == PrimitiveType::TYPE_STRUCT ||
776
12.9M
             slot->type()->get_primitive_type() == PrimitiveType::TYPE_MAP ||
777
12.9M
             slot->type()->get_primitive_type() == PrimitiveType::TYPE_ARRAY) &&
778
12.9M
            !slot->all_access_paths().empty()) {
779
69.8k
            tablet_schema->add_pruned_columns_data_type(column.unique_id(), slot->type());
780
69.8k
        }
781
782
12.9M
        _return_columns.push_back(index);
783
12.9M
        if (slot->is_nullable() && !tablet_schema->column(index).is_nullable()) {
784
0
            _tablet_columns_convert_to_null_set.emplace(index);
785
12.9M
        } else if (!slot->is_nullable() && tablet_schema->column(index).is_nullable()) {
786
0
            return Status::Error<ErrorCode::INVALID_SCHEMA>(
787
0
                    "slot(id: {}, name: {})'s nullable does not match "
788
0
                    "column(tablet id: {}, index: {}, name: {}) ",
789
0
                    slot->id(), slot->col_name(), tablet_schema->table_id(), index,
790
0
                    tablet_schema->column(index).name());
791
0
        }
792
12.9M
    }
793
794
1.22M
    if (_return_columns.empty()) {
795
0
        return Status::InternalError("failed to build storage scanner, no materialized slot!");
796
0
    }
797
798
1.22M
    return Status::OK();
799
1.22M
}
800
801
2.55M
bool OlapScanner::check_partition_pruned() const {
802
2.55M
    if (!_local_state) {
803
0
        return false;
804
0
    }
805
2.55M
    return _local_state->is_partition_pruned(_tablet_reader_params.tablet->partition_id());
806
2.55M
}
807
808
1.27M
doris::TabletStorageType OlapScanner::get_storage_type() {
809
1.27M
    if (config::is_cloud_mode()) {
810
        // we don't have cold storage in cloud mode, all storage is treated as local
811
904k
        return doris::TabletStorageType::STORAGE_TYPE_LOCAL;
812
904k
    }
813
374k
    int local_reader = 0;
814
693k
    for (const auto& reader : _tablet_reader_params.rs_splits) {
815
693k
        local_reader += reader.rs_reader->rowset()->is_local();
816
693k
    }
817
374k
    int total_reader = _tablet_reader_params.rs_splits.size();
818
819
374k
    if (local_reader == total_reader) {
820
374k
        return doris::TabletStorageType::STORAGE_TYPE_LOCAL;
821
374k
    } else if (local_reader == 0) {
822
0
        return doris::TabletStorageType::STORAGE_TYPE_REMOTE;
823
0
    }
824
1
    return doris::TabletStorageType::STORAGE_TYPE_REMOTE_AND_LOCAL;
825
374k
}
826
827
1.56M
Status OlapScanner::_get_block_impl(RuntimeState* state, Block* block, bool* eof) {
828
    // Read one block from block reader
829
    // ATTN: Here we need to let the _get_block_impl method guarantee the semantics of the interface,
830
    // that is, eof can be set to true only when the returned block is empty.
831
1.56M
    RETURN_IF_ERROR(_tablet_reader->next_block_with_aggregation(block, eof));
832
1.56M
    if (block->rows() > 0) {
833
335k
        _tablet_reader_params.tablet->read_block_count.fetch_add(1, std::memory_order_relaxed);
834
335k
        *eof = false;
835
335k
    }
836
1.56M
#ifndef NDEBUG
837
1.56M
    RETURN_IF_ERROR(_check_ann_cache_hit_debug_points(_tablet_reader->stats()));
838
1.56M
#endif
839
1.56M
    return Status::OK();
840
1.56M
}
841
842
1.23M
Status OlapScanner::close(RuntimeState* state) {
843
1.23M
    if (!_try_close()) {
844
150
        return Status::OK();
845
150
    }
846
1.23M
    RETURN_IF_ERROR(Scanner::close(state));
847
1.23M
    return Status::OK();
848
1.23M
}
849
850
1.27M
void OlapScanner::update_realtime_counters() {
851
1.27M
    if (!_has_prepared) {
852
        // Counter update need prepare successfully, or it maybe core. For example, olap scanner
853
        // will open tablet reader during prepare, if not prepare successfully, tablet reader == nullptr.
854
0
        return;
855
0
    }
856
1.27M
    OlapScanLocalState* local_state = static_cast<OlapScanLocalState*>(_local_state);
857
1.27M
    const OlapReaderStatistics& stats = _tablet_reader->stats();
858
1.27M
    COUNTER_UPDATE(local_state->_read_compressed_counter, stats.compressed_bytes_read);
859
1.27M
    COUNTER_UPDATE(local_state->_read_uncompressed_counter, stats.uncompressed_bytes_read);
860
1.27M
    COUNTER_UPDATE(local_state->_scan_bytes, stats.uncompressed_bytes_read);
861
1.27M
    COUNTER_UPDATE(local_state->_scan_rows, stats.raw_rows_read);
862
863
    // Make sure the scan bytes and scan rows counter in audit log is the same as the counter in
864
    // doris metrics.
865
    // ScanBytes is the uncompressed bytes read from local + remote
866
    // bytes_read_from_local is the compressed bytes read from local
867
    // bytes_read_from_remote is the compressed bytes read from remote
868
    // scan bytes > bytes_read_from_local + bytes_read_from_remote
869
1.27M
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(stats.raw_rows_read);
870
1.27M
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(
871
1.27M
            stats.uncompressed_bytes_read);
872
873
    // In case of no cache, we still need to update the IO stats. uncompressed bytes read == local + remote
874
1.27M
    if (stats.file_cache_stats.bytes_read_from_local == 0 &&
875
1.27M
        stats.file_cache_stats.bytes_read_from_remote == 0) {
876
1.04M
        _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
877
1.04M
                stats.compressed_bytes_read);
878
1.04M
        DorisMetrics::instance()->query_scan_bytes_from_local->increment(
879
1.04M
                stats.compressed_bytes_read);
880
1.04M
    } else {
881
225k
        _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
882
225k
                stats.file_cache_stats.bytes_read_from_local);
883
225k
        _state->get_query_ctx()
884
225k
                ->resource_ctx()
885
225k
                ->io_context()
886
225k
                ->update_scan_bytes_from_remote_storage(
887
225k
                        stats.file_cache_stats.bytes_read_from_remote);
888
889
225k
        DorisMetrics::instance()->query_scan_bytes_from_local->increment(
890
225k
                stats.file_cache_stats.bytes_read_from_local);
891
225k
        DorisMetrics::instance()->query_scan_bytes_from_remote->increment(
892
225k
                stats.file_cache_stats.bytes_read_from_remote);
893
225k
    }
894
895
1.27M
    if (has_file_cache_statistics(stats.file_cache_stats)) {
896
228k
        io::FileCacheProfileReporter cache_profile(local_state->_segment_profile.get());
897
228k
        cache_profile.update(&stats.file_cache_stats);
898
228k
        _state->get_query_ctx()->resource_ctx()->io_context()->update_bytes_write_into_cache(
899
228k
                stats.file_cache_stats.bytes_write_into_cache);
900
228k
    }
901
902
1.27M
    _tablet_reader->mutable_stats()->compressed_bytes_read = 0;
903
1.27M
    _tablet_reader->mutable_stats()->uncompressed_bytes_read = 0;
904
1.27M
    _tablet_reader->mutable_stats()->raw_rows_read = 0;
905
1.27M
    _tablet_reader->mutable_stats()->file_cache_stats = {};
906
1.27M
}
907
908
1.22M
void OlapScanner::_collect_profile_before_close() {
909
    //  Please don't directly enable the profile here, we need to set QueryStatistics using the counter inside.
910
1.22M
    if (_has_updated_counter) {
911
0
        return;
912
0
    }
913
1.22M
    _has_updated_counter = true;
914
1.22M
    _tablet_reader->update_profile(_profile);
915
916
1.22M
    Scanner::_collect_profile_before_close();
917
918
    // Update counters for OlapScanner
919
    // Update counters from tablet reader's stats
920
1.22M
    auto& stats = _tablet_reader->stats();
921
1.22M
    auto* local_state = (OlapScanLocalState*)_local_state;
922
1.22M
    COUNTER_UPDATE(local_state->_io_timer, stats.io_ns);
923
1.22M
    COUNTER_UPDATE(local_state->_read_compressed_counter, stats.compressed_bytes_read);
924
1.22M
    COUNTER_UPDATE(local_state->_scan_bytes, stats.uncompressed_bytes_read);
925
1.22M
    COUNTER_UPDATE(local_state->_decompressor_timer, stats.decompress_ns);
926
1.22M
    COUNTER_UPDATE(local_state->_read_uncompressed_counter, stats.uncompressed_bytes_read);
927
1.22M
    COUNTER_UPDATE(local_state->_block_load_timer, stats.block_load_ns);
928
1.22M
    COUNTER_UPDATE(local_state->_block_load_counter, stats.blocks_load);
929
1.22M
    COUNTER_UPDATE(local_state->_block_fetch_timer, stats.block_fetch_ns);
930
1.22M
    COUNTER_UPDATE(local_state->_delete_bitmap_get_agg_timer, stats.delete_bitmap_get_agg_ns);
931
1.22M
    COUNTER_UPDATE(local_state->_scan_rows, stats.raw_rows_read);
932
1.22M
    COUNTER_UPDATE(local_state->_vec_cond_timer, stats.vec_cond_ns);
933
1.22M
    COUNTER_UPDATE(local_state->_short_cond_timer, stats.short_cond_ns);
934
1.22M
    COUNTER_UPDATE(local_state->_expr_filter_timer, stats.expr_filter_ns);
935
1.22M
    COUNTER_UPDATE(local_state->_block_init_timer, stats.block_init_ns);
936
1.22M
    COUNTER_UPDATE(local_state->_block_init_seek_timer, stats.block_init_seek_ns);
937
1.22M
    COUNTER_UPDATE(local_state->_block_init_seek_counter, stats.block_init_seek_num);
938
1.22M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_keys_timer,
939
1.22M
                   stats.generate_row_ranges_by_keys_ns);
940
1.22M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_column_conditions_timer,
941
1.22M
                   stats.generate_row_ranges_by_column_conditions_ns);
942
1.22M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_bf_timer,
943
1.22M
                   stats.generate_row_ranges_by_bf_ns);
944
1.22M
    COUNTER_UPDATE(local_state->_collect_iterator_merge_next_timer,
945
1.22M
                   stats.collect_iterator_merge_next_timer);
946
1.22M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_zonemap_timer,
947
1.22M
                   stats.generate_row_ranges_by_zonemap_ns);
948
1.22M
    COUNTER_UPDATE(local_state->_segment_generate_row_range_by_dict_timer,
949
1.22M
                   stats.generate_row_ranges_by_dict_ns);
950
1.22M
    COUNTER_UPDATE(local_state->_predicate_column_read_timer, stats.predicate_column_read_ns);
951
1.22M
    COUNTER_UPDATE(local_state->_non_predicate_column_read_timer, stats.non_predicate_read_ns);
952
1.22M
    COUNTER_UPDATE(local_state->_predicate_column_read_seek_timer,
953
1.22M
                   stats.predicate_column_read_seek_ns);
954
1.22M
    COUNTER_UPDATE(local_state->_predicate_column_read_seek_counter,
955
1.22M
                   stats.predicate_column_read_seek_num);
956
1.22M
    COUNTER_UPDATE(local_state->_lazy_read_timer, stats.lazy_read_ns);
957
1.22M
    COUNTER_UPDATE(local_state->_lazy_read_seek_timer, stats.block_lazy_read_seek_ns);
958
1.22M
    COUNTER_UPDATE(local_state->_lazy_read_seek_counter, stats.block_lazy_read_seek_num);
959
1.22M
    COUNTER_UPDATE(local_state->_output_col_timer, stats.output_col_ns);
960
1.22M
    COUNTER_UPDATE(local_state->_rows_vec_cond_filtered_counter, stats.rows_vec_cond_filtered);
961
1.22M
    COUNTER_UPDATE(local_state->_rows_short_circuit_cond_filtered_counter,
962
1.22M
                   stats.rows_short_circuit_cond_filtered);
963
1.22M
    COUNTER_UPDATE(local_state->_rows_expr_cond_filtered_counter, stats.rows_expr_cond_filtered);
964
1.22M
    COUNTER_UPDATE(local_state->_rows_vec_cond_input_counter, stats.vec_cond_input_rows);
965
1.22M
    COUNTER_UPDATE(local_state->_rows_short_circuit_cond_input_counter,
966
1.22M
                   stats.short_circuit_cond_input_rows);
967
1.22M
    COUNTER_UPDATE(local_state->_rows_expr_cond_input_counter, stats.expr_cond_input_rows);
968
969
1.22M
    COUNTER_UPDATE(local_state->_predicate_lm_stage1_input_rows_counter,
970
1.22M
                   stats.predicate_lm_stage1_input_rows);
971
1.22M
    COUNTER_UPDATE(local_state->_predicate_lm_stage1_output_rows_counter,
972
1.22M
                   stats.predicate_lm_stage1_output_rows);
973
1.22M
    COUNTER_UPDATE(local_state->_predicate_lm_stage2_by_rowids_batches_counter,
974
1.22M
                   stats.predicate_lm_stage2_by_rowids_batches);
975
1.22M
    COUNTER_UPDATE(local_state->_predicate_lm_stage2_by_all_rows_batches_counter,
976
1.22M
                   stats.predicate_lm_stage2_by_all_rows_batches);
977
1.22M
    COUNTER_UPDATE(local_state->_predicate_lm_stage2_rows_read_counter,
978
1.22M
                   stats.predicate_lm_stage2_rows_read);
979
980
1.22M
    COUNTER_UPDATE(local_state->_stats_filtered_counter, stats.rows_stats_filtered);
981
1.22M
    COUNTER_UPDATE(local_state->_stats_rp_filtered_counter, stats.rows_stats_rp_filtered);
982
1.22M
    COUNTER_UPDATE(local_state->_dict_filtered_counter, stats.segment_dict_filtered);
983
1.22M
    COUNTER_UPDATE(local_state->_bf_filtered_counter, stats.rows_bf_filtered);
984
1.22M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_del_filtered);
985
1.22M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_del_by_bitmap);
986
1.22M
    COUNTER_UPDATE(local_state->_del_filtered_counter, stats.rows_vec_del_cond_filtered);
987
1.22M
    COUNTER_UPDATE(local_state->_conditions_filtered_counter, stats.rows_conditions_filtered);
988
1.22M
    COUNTER_UPDATE(local_state->_key_range_filtered_counter, stats.rows_key_range_filtered);
989
1.22M
    COUNTER_UPDATE(local_state->_total_pages_num_counter, stats.total_pages_num);
990
1.22M
    COUNTER_UPDATE(local_state->_cached_pages_num_counter, stats.cached_pages_num);
991
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_filter_counter, stats.rows_inverted_index_filtered);
992
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_filter_timer, stats.inverted_index_filter_timer);
993
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_query_cache_hit_counter,
994
1.22M
                   stats.inverted_index_query_cache_hit);
995
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_query_cache_miss_counter,
996
1.22M
                   stats.inverted_index_query_cache_miss);
997
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_query_timer, stats.inverted_index_query_timer);
998
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_query_null_bitmap_timer,
999
1.22M
                   stats.inverted_index_query_null_bitmap_timer);
1000
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_query_bitmap_copy_timer,
1001
1.22M
                   stats.inverted_index_query_bitmap_copy_timer);
1002
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_open_timer,
1003
1.22M
                   stats.inverted_index_searcher_open_timer);
1004
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_timer,
1005
1.22M
                   stats.inverted_index_searcher_search_timer);
1006
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_init_timer,
1007
1.22M
                   stats.inverted_index_searcher_search_init_timer);
1008
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_search_exec_timer,
1009
1.22M
                   stats.inverted_index_searcher_search_exec_timer);
1010
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_cache_hit_counter,
1011
1.22M
                   stats.inverted_index_searcher_cache_hit);
1012
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_searcher_cache_miss_counter,
1013
1.22M
                   stats.inverted_index_searcher_cache_miss);
1014
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_downgrade_count_counter,
1015
1.22M
                   stats.inverted_index_downgrade_count);
1016
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_analyzer_timer,
1017
1.22M
                   stats.inverted_index_analyzer_timer);
1018
1.22M
    COUNTER_UPDATE(local_state->_inverted_index_lookup_timer, stats.inverted_index_lookup_timer);
1019
1.22M
    COUNTER_UPDATE(local_state->_variant_scan_sparse_column_timer,
1020
1.22M
                   stats.variant_scan_sparse_column_timer_ns);
1021
1.22M
    COUNTER_UPDATE(local_state->_variant_scan_sparse_column_bytes,
1022
1.22M
                   stats.variant_scan_sparse_column_bytes);
1023
1.22M
    COUNTER_UPDATE(local_state->_variant_fill_path_from_sparse_column_timer,
1024
1.22M
                   stats.variant_fill_path_from_sparse_column_timer_ns);
1025
1.22M
    COUNTER_UPDATE(local_state->_variant_subtree_default_iter_count,
1026
1.22M
                   stats.variant_subtree_default_iter_count);
1027
1.22M
    COUNTER_UPDATE(local_state->_variant_subtree_leaf_iter_count,
1028
1.22M
                   stats.variant_subtree_leaf_iter_count);
1029
1.22M
    COUNTER_UPDATE(local_state->_variant_subtree_hierarchical_iter_count,
1030
1.22M
                   stats.variant_subtree_hierarchical_iter_count);
1031
1.22M
    COUNTER_UPDATE(local_state->_variant_subtree_sparse_iter_count,
1032
1.22M
                   stats.variant_subtree_sparse_iter_count);
1033
1.22M
    COUNTER_UPDATE(local_state->_variant_doc_value_column_iter_count,
1034
1.22M
                   stats.variant_doc_value_column_iter_count);
1035
1036
1.22M
    if (stats.adaptive_batch_size_predict_max_rows > 0) {
1037
957k
        local_state->_adaptive_batch_predict_min_rows_counter->set(
1038
957k
                stats.adaptive_batch_size_predict_min_rows);
1039
957k
        local_state->_adaptive_batch_predict_max_rows_counter->set(
1040
957k
                stats.adaptive_batch_size_predict_max_rows);
1041
957k
    }
1042
1043
1.22M
    InvertedIndexProfileReporter inverted_index_profile;
1044
1.22M
    inverted_index_profile.update(local_state->_index_filter_profile.get(),
1045
1.22M
                                  &stats.inverted_index_stats);
1046
1047
1.22M
    if (has_file_cache_statistics(stats.file_cache_stats)) {
1048
0
        io::FileCacheProfileReporter cache_profile(local_state->_segment_profile.get());
1049
0
        cache_profile.update(&stats.file_cache_stats);
1050
0
        _state->get_query_ctx()->resource_ctx()->io_context()->update_bytes_write_into_cache(
1051
0
                stats.file_cache_stats.bytes_write_into_cache);
1052
0
    }
1053
1.22M
    COUNTER_UPDATE(local_state->_output_index_result_column_timer,
1054
1.22M
                   stats.output_index_result_column_timer);
1055
1.22M
    COUNTER_UPDATE(local_state->_filtered_segment_counter, stats.filtered_segment_number);
1056
1.22M
    COUNTER_UPDATE(local_state->_total_segment_counter, stats.total_segment_number);
1057
1.22M
    COUNTER_UPDATE(local_state->_condition_cache_hit_counter, stats.condition_cache_hit_seg_nums);
1058
1.22M
    COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter,
1059
1.22M
                   stats.condition_cache_filtered_rows);
1060
1061
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_timer, stats.tablet_reader_init_timer_ns);
1062
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_capture_rs_readers_timer,
1063
1.22M
                   stats.tablet_reader_capture_rs_readers_timer_ns);
1064
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_return_columns_timer,
1065
1.22M
                   stats.tablet_reader_init_return_columns_timer_ns);
1066
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_keys_param_timer,
1067
1.22M
                   stats.tablet_reader_init_keys_param_timer_ns);
1068
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_orderby_keys_param_timer,
1069
1.22M
                   stats.tablet_reader_init_orderby_keys_param_timer_ns);
1070
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_conditions_param_timer,
1071
1.22M
                   stats.tablet_reader_init_conditions_param_timer_ns);
1072
1.22M
    COUNTER_UPDATE(local_state->_tablet_reader_init_delete_condition_param_timer,
1073
1.22M
                   stats.tablet_reader_init_delete_condition_param_timer_ns);
1074
1.22M
    COUNTER_UPDATE(local_state->_block_reader_vcollect_iter_init_timer,
1075
1.22M
                   stats.block_reader_vcollect_iter_init_timer_ns);
1076
1.22M
    COUNTER_UPDATE(local_state->_block_reader_rs_readers_init_timer,
1077
1.22M
                   stats.block_reader_rs_readers_init_timer_ns);
1078
1.22M
    COUNTER_UPDATE(local_state->_block_reader_build_heap_init_timer,
1079
1.22M
                   stats.block_reader_build_heap_init_timer_ns);
1080
1081
1.22M
    COUNTER_UPDATE(local_state->_rowset_reader_get_segment_iterators_timer,
1082
1.22M
                   stats.rowset_reader_get_segment_iterators_timer_ns);
1083
1.22M
    COUNTER_UPDATE(local_state->_rowset_reader_create_iterators_timer,
1084
1.22M
                   stats.rowset_reader_create_iterators_timer_ns);
1085
1.22M
    COUNTER_UPDATE(local_state->_rowset_reader_init_iterators_timer,
1086
1.22M
                   stats.rowset_reader_init_iterators_timer_ns);
1087
1.22M
    COUNTER_UPDATE(local_state->_rowset_reader_load_segments_timer,
1088
1.22M
                   stats.rowset_reader_load_segments_timer_ns);
1089
1090
1.22M
    COUNTER_UPDATE(local_state->_segment_iterator_init_timer, stats.segment_iterator_init_timer_ns);
1091
1.22M
    COUNTER_UPDATE(local_state->_segment_iterator_init_return_column_iterators_timer,
1092
1.22M
                   stats.segment_iterator_init_return_column_iterators_timer_ns);
1093
1.22M
    COUNTER_UPDATE(local_state->_segment_iterator_init_index_iterators_timer,
1094
1.22M
                   stats.segment_iterator_init_index_iterators_timer_ns);
1095
1.22M
    COUNTER_UPDATE(local_state->_segment_iterator_init_segment_prefetchers_timer,
1096
1.22M
                   stats.segment_iterator_init_segment_prefetchers_timer_ns);
1097
1098
1.22M
    COUNTER_UPDATE(local_state->_segment_create_column_readers_timer,
1099
1.22M
                   stats.segment_create_column_readers_timer_ns);
1100
1.22M
    COUNTER_UPDATE(local_state->_segment_load_index_timer, stats.segment_load_index_timer_ns);
1101
1102
    // Update metrics
1103
1.22M
    DorisMetrics::instance()->query_scan_bytes->increment(
1104
1.22M
            local_state->_read_uncompressed_counter->value());
1105
1.22M
    DorisMetrics::instance()->query_scan_rows->increment(local_state->_scan_rows->value());
1106
1.22M
    auto& tablet = _tablet_reader_params.tablet;
1107
1.22M
    tablet->query_scan_bytes->increment(local_state->_read_uncompressed_counter->value());
1108
1.22M
    tablet->query_scan_rows->increment(local_state->_scan_rows->value());
1109
1.22M
    tablet->query_scan_count->increment(1);
1110
1111
1.22M
    COUNTER_UPDATE(local_state->_ann_range_search_filter_counter,
1112
1.22M
                   stats.rows_ann_index_range_filtered);
1113
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_filter_counter, stats.rows_ann_index_topn_filtered);
1114
1.22M
    COUNTER_UPDATE(local_state->_ann_index_load_costs, stats.ann_index_load_ns);
1115
1.22M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_load_costs, stats.ann_ivf_on_disk_load_ns);
1116
1.22M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_cache_hit_cnt,
1117
1.22M
                   stats.ann_ivf_on_disk_cache_hit_cnt);
1118
1.22M
    COUNTER_UPDATE(local_state->_ann_ivf_on_disk_cache_miss_cnt,
1119
1.22M
                   stats.ann_ivf_on_disk_cache_miss_cnt);
1120
1.22M
    COUNTER_UPDATE(local_state->_ann_range_search_costs, stats.ann_index_range_search_ns);
1121
1.22M
    COUNTER_UPDATE(local_state->_ann_range_search_cnt, stats.ann_index_range_search_cnt);
1122
1.22M
    COUNTER_UPDATE(local_state->_ann_range_engine_search_costs, stats.ann_range_engine_search_ns);
1123
    // Engine prepare before search
1124
1.22M
    COUNTER_UPDATE(local_state->_ann_range_pre_process_costs, stats.ann_range_pre_process_ns);
1125
    // Post process parent: Doris result process + engine convert
1126
1.22M
    COUNTER_UPDATE(local_state->_ann_range_post_process_costs,
1127
1.22M
                   stats.ann_range_result_convert_ns + stats.ann_range_engine_convert_ns);
1128
    // Engine convert (child under post-process)
1129
1.22M
    COUNTER_UPDATE(local_state->_ann_range_engine_convert_costs, stats.ann_range_engine_convert_ns);
1130
    // Doris-side result convert (child under post-process)
1131
1.22M
    COUNTER_UPDATE(local_state->_ann_range_result_convert_costs, stats.ann_range_result_convert_ns);
1132
1133
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_search_costs, stats.ann_topn_search_ns);
1134
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_search_cnt, stats.ann_index_topn_search_cnt);
1135
1.22M
    COUNTER_UPDATE(local_state->_ann_cache_hit_cnt, stats.ann_index_cache_hits);
1136
1.22M
    COUNTER_UPDATE(local_state->_ann_range_cache_hit_cnt, stats.ann_index_range_cache_hits);
1137
1138
    // Detailed ANN timers
1139
    // ANN TopN timers with hierarchy
1140
    // Engine search time (FAISS)
1141
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_engine_search_costs,
1142
1.22M
                   stats.ann_index_topn_engine_search_ns);
1143
    // Engine prepare time (allocations/buffer setup before search)
1144
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_pre_process_costs,
1145
1.22M
                   stats.ann_index_topn_engine_prepare_ns);
1146
    // Post process parent includes Doris result processing + engine convert
1147
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_post_process_costs,
1148
1.22M
                   stats.ann_index_topn_result_process_ns + stats.ann_index_topn_engine_convert_ns);
1149
    // Engine-side conversion time inside FAISS wrappers (child under post-process)
1150
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_engine_convert_costs,
1151
1.22M
                   stats.ann_index_topn_engine_convert_ns);
1152
1153
    // Doris-side result convert costs (show separately as another child counter); use pure process time
1154
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_result_convert_costs,
1155
1.22M
                   stats.ann_index_topn_result_process_ns);
1156
1157
1.22M
    COUNTER_UPDATE(local_state->_ann_fallback_brute_force_cnt, stats.ann_fall_back_brute_force_cnt);
1158
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_fallback_by_small_candidate_cnt,
1159
1.22M
                   stats.ann_topn_fallback_by_small_candidate_cnt);
1160
1.22M
    COUNTER_UPDATE(local_state->_ann_topn_fallback_small_candidate_rows,
1161
1.22M
                   stats.ann_topn_fallback_small_candidate_rows);
1162
1.22M
    COUNTER_UPDATE(local_state->_ann_range_fallback_by_small_candidate_cnt,
1163
1.22M
                   stats.ann_range_fallback_by_small_candidate_cnt);
1164
1.22M
    COUNTER_UPDATE(local_state->_ann_range_fallback_small_candidate_rows,
1165
1.22M
                   stats.ann_range_fallback_small_candidate_rows);
1166
1167
    // Overhead counter removed; precise instrumentation is reported via engine_prepare above.
1168
1.22M
}
1169
1170
#ifndef NDEBUG
1171
1.56M
Status OlapScanner::_check_ann_cache_hit_debug_points(const OlapReaderStatistics& stats) {
1172
1.56M
    DBUG_EXECUTE_IF("olap_scanner.ann_topn_cache_hits", {
1173
1.56M
        auto expected_hits = dp->param<int32_t>("expected_hits", -1);
1174
1.56M
        auto min_hits = dp->param<int32_t>("min_hits", -1);
1175
1.56M
        if (expected_hits >= 0 && stats.ann_index_cache_hits != expected_hits) {
1176
1.56M
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
1177
1.56M
                    "ann_index_cache_hits: {} not equal to expected: {}",
1178
1.56M
                    stats.ann_index_cache_hits, expected_hits);
1179
1.56M
        }
1180
1.56M
        if (min_hits >= 0 && stats.ann_index_cache_hits < min_hits) {
1181
1.56M
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
1182
1.56M
                    "ann_index_cache_hits: {} less than expected min: {}",
1183
1.56M
                    stats.ann_index_cache_hits, min_hits);
1184
1.56M
        }
1185
1.56M
    })
1186
1.56M
    DBUG_EXECUTE_IF("olap_scanner.ann_range_cache_hits", {
1187
1.56M
        auto expected_hits = dp->param<int32_t>("expected_hits", -1);
1188
1.56M
        auto min_hits = dp->param<int32_t>("min_hits", -1);
1189
1.56M
        if (expected_hits >= 0 && stats.ann_index_range_cache_hits != expected_hits) {
1190
1.56M
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
1191
1.56M
                    "ann_index_range_cache_hits: {} not equal to expected: {}",
1192
1.56M
                    stats.ann_index_range_cache_hits, expected_hits);
1193
1.56M
        }
1194
1.56M
        if (min_hits >= 0 && stats.ann_index_range_cache_hits < min_hits) {
1195
1.56M
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
1196
1.56M
                    "ann_index_range_cache_hits: {} less than expected min: {}",
1197
1.56M
                    stats.ann_index_range_cache_hits, min_hits);
1198
1.56M
        }
1199
1.56M
    })
1200
1.56M
    return Status::OK();
1201
1.56M
}
1202
#endif
1203
1204
#include "common/compile_check_avoid_end.h"
1205
} // namespace doris