Coverage Report

Created: 2026-07-15 10:06

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