Coverage Report

Created: 2026-07-24 18:25

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