Coverage Report

Created: 2026-07-09 21:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/file_scanner_v2.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/file_scanner_v2.h"
19
20
#include <gen_cpp/Exprs_types.h>
21
#include <gen_cpp/PlanNodes_types.h>
22
23
#include <algorithm>
24
#include <map>
25
#include <memory>
26
#include <optional>
27
#include <string>
28
#include <utility>
29
30
#include "common/cast_set.h"
31
#include "common/config.h"
32
#include "common/consts.h"
33
#include "common/metrics/doris_metrics.h"
34
#include "common/status.h"
35
#include "core/assert_cast.h"
36
#include "core/block/column_with_type_and_name.h"
37
#include "core/column/column.h"
38
#include "core/data_type/data_type.h"
39
#include "core/data_type/data_type_nullable.h"
40
#include "core/data_type_serde/data_type_serde.h"
41
#include "core/string_ref.h"
42
#include "exec/common/util.hpp"
43
#include "exec/operator/scan_operator.h"
44
#include "exec/scan/access_path_parser.h"
45
#include "exprs/runtime_filter_expr.h"
46
#include "exprs/vexpr.h"
47
#include "exprs/vexpr_context.h"
48
#include "exprs/vslot_ref.h"
49
#include "format/format_common.h"
50
#include "format_v2/column_mapper.h"
51
#include "format_v2/jni/iceberg_sys_table_reader.h"
52
#include "format_v2/jni/jdbc_reader.h"
53
#include "format_v2/jni/max_compute_jni_reader.h"
54
#include "format_v2/jni/trino_connector_jni_reader.h"
55
#include "format_v2/table/hive_reader.h"
56
#include "format_v2/table/hudi_reader.h"
57
#include "format_v2/table/iceberg_reader.h"
58
#include "format_v2/table/paimon_reader.h"
59
#include "format_v2/table/remote_doris_reader.h"
60
#include "format_v2/table_reader.h"
61
#include "io/fs/file_meta_cache.h"
62
#include "io/io_common.h"
63
#include "runtime/descriptors.h"
64
#include "runtime/exec_env.h"
65
#include "runtime/runtime_state.h"
66
#include "service/backend_options.h"
67
#include "storage/id_manager.h"
68
69
namespace doris {
70
namespace {
71
72
160k
std::string table_format_name(const TFileRangeDesc& range) {
73
160k
    return range.__isset.table_format_params ? range.table_format_params.table_format_type
74
160k
                                             : "NotSet";
75
160k
}
76
77
TFileFormatType::type get_range_format_type(const TFileScanRangeParams& params,
78
425k
                                            const TFileRangeDesc& range) {
79
18.4E
    return range.__isset.format_type ? range.format_type : params.format_type;
80
425k
}
81
82
106k
bool is_supported_table_format(const TFileRangeDesc& range) {
83
106k
    const auto table_format = table_format_name(range);
84
106k
    if (table_format == "hudi" && range.__isset.table_format_params &&
85
106k
        range.table_format_params.__isset.hudi_params &&
86
106k
        range.table_format_params.hudi_params.__isset.delta_logs &&
87
106k
        !range.table_format_params.hudi_params.delta_logs.empty()) {
88
        // Hudi MOR splits need log-file merge semantics and must stay on the existing JNI path.
89
        // FileScannerV2 currently supports native Parquet data files only.
90
1
        return false;
91
1
    }
92
113k
    return table_format == "NotSet" || table_format == "tvf" || table_format == "hive" ||
93
106k
           table_format == "iceberg" || table_format == "paimon" || table_format == "hudi";
94
106k
}
95
96
101
bool is_supported_arrow_table_format(const TFileRangeDesc& range) {
97
101
    return table_format_name(range) == "remote_doris";
98
101
}
99
100
5.33k
bool is_supported_jni_table_format(const TFileRangeDesc& range) {
101
5.33k
    const auto table_format = table_format_name(range);
102
5.33k
    if (table_format == "paimon") {
103
2.00k
        return range.__isset.table_format_params &&
104
2.00k
               range.table_format_params.__isset.paimon_params &&
105
2.00k
               range.table_format_params.paimon_params.__isset.reader_type &&
106
2.00k
               range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI;
107
2.00k
    }
108
3.33k
    return table_format == "jdbc" || table_format == "iceberg" || table_format == "hudi" ||
109
3.33k
           table_format == "max_compute" || table_format == "trino_connector";
110
5.33k
}
111
112
6.76k
bool is_csv_format(TFileFormatType::type format_type) {
113
6.76k
    switch (format_type) {
114
952
    case TFileFormatType::FORMAT_CSV_PLAIN:
115
953
    case TFileFormatType::FORMAT_CSV_GZ:
116
954
    case TFileFormatType::FORMAT_CSV_BZ2:
117
955
    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
118
956
    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
119
957
    case TFileFormatType::FORMAT_CSV_LZOP:
120
958
    case TFileFormatType::FORMAT_CSV_DEFLATE:
121
959
    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
122
960
    case TFileFormatType::FORMAT_PROTO:
123
960
        return true;
124
5.79k
    default:
125
5.79k
        return false;
126
6.76k
    }
127
6.76k
}
128
129
5.77k
bool is_text_format(TFileFormatType::type format_type) {
130
5.77k
    return format_type == TFileFormatType::FORMAT_TEXT;
131
5.77k
}
132
133
1.22k
bool is_json_format(TFileFormatType::type format_type) {
134
1.22k
    return format_type == TFileFormatType::FORMAT_JSON;
135
1.22k
}
136
137
645
bool is_native_format(TFileFormatType::type format_type) {
138
645
    return format_type == TFileFormatType::FORMAT_NATIVE;
139
645
}
140
141
280k
bool is_partition_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) {
142
280k
    if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) ||
143
280k
        column_name == BeConsts::ICEBERG_ROWID_COL) {
144
4.47k
        return false;
145
4.47k
    }
146
276k
    return slot_info.__isset.category ? slot_info.category == TColumnCategory::PARTITION_KEY
147
276k
                                      : !slot_info.is_file_slot;
148
280k
}
149
150
266k
bool is_data_file_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) {
151
266k
    if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) ||
152
266k
        column_name == BeConsts::ICEBERG_ROWID_COL) {
153
4.47k
        return false;
154
4.47k
    }
155
    // CSV and other non-self-describing formats need FE slot descriptors for only the columns that
156
    // are physically read from the file. Partition/default/virtual columns stay in TableReader's
157
    // mapping layer and are materialized after the file-local block is read. New FE provides an
158
    // explicit category; old FE falls back to `is_file_slot`.
159
262k
    if (slot_info.__isset.category) {
160
262k
        return slot_info.category == TColumnCategory::REGULAR ||
161
262k
               slot_info.category == TColumnCategory::GENERATED;
162
262k
    }
163
18.4E
    return slot_info.is_file_slot;
164
262k
}
165
166
Status rewrite_slot_refs_to_global_index(
167
        VExprSPtr* expr,
168
110k
        const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) {
169
110k
    DORIS_CHECK(expr != nullptr);
170
110k
    if (*expr == nullptr) {
171
0
        return Status::OK();
172
0
    }
173
110k
    if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr->get());
174
110k
        runtime_filter != nullptr) {
175
7.33k
        auto impl = runtime_filter->get_impl();
176
7.33k
        DORIS_CHECK(impl != nullptr);
177
7.33k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&impl, slot_id_to_global_index));
178
7.33k
        runtime_filter->set_impl(std::move(impl));
179
7.33k
        return Status::OK();
180
7.33k
    }
181
102k
    if ((*expr)->is_slot_ref()) {
182
33.5k
        const auto* slot_ref = assert_cast<const VSlotRef*>(expr->get());
183
33.5k
        const auto global_index_it = slot_id_to_global_index.find(slot_ref->slot_id());
184
33.5k
        if (global_index_it == slot_id_to_global_index.end()) {
185
1
            DORIS_CHECK(slot_ref->slot_id() >= 0);
186
1
            const auto global_index = format::GlobalIndex(cast_set<size_t>(slot_ref->slot_id()));
187
1
            *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()),
188
1
                                            cast_set<int>(global_index.value()), -1,
189
1
                                            slot_ref->data_type(), slot_ref->column_name());
190
1
            RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr));
191
1
            return Status::OK();
192
1
        }
193
33.5k
        const auto global_index = global_index_it->second;
194
33.5k
        *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()),
195
33.5k
                                        cast_set<int>(global_index.value()), -1,
196
33.5k
                                        slot_ref->data_type(), slot_ref->column_name());
197
33.5k
        RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr));
198
33.5k
        return Status::OK();
199
33.5k
    }
200
69.2k
    auto children = (*expr)->children();
201
70.4k
    for (auto& child : children) {
202
70.4k
        if (child == nullptr) {
203
0
            continue;
204
0
        }
205
70.4k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&child, slot_id_to_global_index));
206
70.4k
    }
207
69.2k
    (*expr)->set_children(std::move(children));
208
69.2k
    return Status::OK();
209
69.2k
}
210
211
} // namespace
212
213
#ifdef BE_TEST
214
Status FileScannerV2::TEST_to_file_format(TFileFormatType::type format_type,
215
                                          format::FileFormat* file_format) {
216
    return _to_file_format(format_type, file_format);
217
}
218
219
bool FileScannerV2::TEST_is_partition_slot(const TFileScanSlotInfo& slot_info,
220
                                           const std::string& column_name) {
221
    return is_partition_slot(slot_info, column_name);
222
}
223
224
bool FileScannerV2::TEST_is_data_file_slot(const TFileScanSlotInfo& slot_info,
225
                                           const std::string& column_name) {
226
    return is_data_file_slot(slot_info, column_name);
227
}
228
229
Status FileScannerV2::TEST_rewrite_slot_refs_to_global_index(
230
        VExprSPtr* expr,
231
        const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) {
232
    return rewrite_slot_refs_to_global_index(expr, slot_id_to_global_index);
233
}
234
235
FileScannerV2::RealtimeCounterDeltas FileScannerV2::TEST_collect_realtime_counter_deltas(
236
        const io::FileReaderStats& file_reader_stats,
237
        const io::FileCacheStatistics& file_cache_statistics,
238
        UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes,
239
        int64_t* last_read_rows, int64_t* last_bytes_read_from_local,
240
        int64_t* last_bytes_read_from_remote) {
241
    return _collect_realtime_counter_deltas(file_reader_stats, file_cache_statistics,
242
                                            uncached_reader_bytes_storage, last_read_bytes,
243
                                            last_read_rows, last_bytes_read_from_local,
244
                                            last_bytes_read_from_remote);
245
}
246
#endif
247
248
114k
bool FileScannerV2::is_supported(const TFileScanRangeParams& params, const TFileRangeDesc& range) {
249
114k
    const auto format_type = get_range_format_type(params, range);
250
114k
    if (format_type == TFileFormatType::FORMAT_PARQUET ||
251
114k
        format_type == TFileFormatType::FORMAT_ORC) {
252
102k
        return is_supported_table_format(range);
253
102k
    } else if (format_type == TFileFormatType::FORMAT_ARROW) {
254
101
        return is_supported_arrow_table_format(range);
255
12.1k
    } else if (format_type == TFileFormatType::FORMAT_JNI) {
256
5.33k
        return is_supported_jni_table_format(range);
257
6.81k
    } else if (is_csv_format(format_type) || is_text_format(format_type) ||
258
6.81k
               is_json_format(format_type) || is_native_format(format_type)) {
259
6.08k
        return is_supported_table_format(range);
260
6.08k
    } else {
261
734
        LOG(WARNING) << "Unsupported file format type " << format_type << " for file scanner v2";
262
734
        return false;
263
734
    }
264
114k
}
265
266
FileScannerV2::FileScannerV2(RuntimeState* state, FileScanLocalState* local_state, int64_t limit,
267
                             std::shared_ptr<SplitSourceConnector> split_source,
268
                             RuntimeProfile* profile, ShardedKVCache* kv_cache,
269
                             const std::unordered_map<std::string, int>* colname_to_slot_id)
270
67.7k
        : Scanner(state, local_state, limit, profile),
271
67.7k
          _split_source(std::move(split_source)),
272
67.7k
          _kv_cache(kv_cache) {
273
67.7k
    (void)colname_to_slot_id;
274
67.7k
    if (state->get_query_ctx() != nullptr &&
275
67.8k
        state->get_query_ctx()->file_scan_range_params_map.count(local_state->parent_id()) > 0) {
276
67.7k
        _params = &(state->get_query_ctx()->file_scan_range_params_map[local_state->parent_id()]);
277
67.7k
    } else {
278
33
        _params = _split_source->get_params();
279
33
    }
280
67.7k
}
281
282
67.4k
Status FileScannerV2::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
283
67.4k
    RETURN_IF_ERROR(Scanner::init(state, conjuncts));
284
67.4k
    _get_block_timer =
285
67.4k
            ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerV2GetBlockTime", 1);
286
67.4k
    _file_counter =
287
67.4k
            ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "FileNumber", TUnit::UNIT, 1);
288
67.4k
    _file_read_bytes_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(),
289
67.4k
                                                      "FileReadBytes", TUnit::BYTES, 1);
290
67.4k
    _file_read_calls_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(),
291
67.4k
                                                      "FileReadCalls", TUnit::UNIT, 1);
292
67.4k
    _file_read_time_counter =
293
67.4k
            ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileReadTime", 1);
294
67.4k
    _adaptive_batch_predicted_rows_counter = ADD_COUNTER_WITH_LEVEL(
295
67.4k
            _local_state->scanner_profile(), "AdaptiveBatchPredictedRows", TUnit::UNIT, 1);
296
67.4k
    _adaptive_batch_actual_bytes_counter = ADD_COUNTER_WITH_LEVEL(
297
67.4k
            _local_state->scanner_profile(), "AdaptiveBatchActualBytes", TUnit::BYTES, 1);
298
67.4k
    _adaptive_batch_probe_count_counter = ADD_COUNTER_WITH_LEVEL(
299
67.4k
            _local_state->scanner_profile(), "AdaptiveBatchProbeCount", TUnit::UNIT, 1);
300
67.4k
    _file_cache_statistics = std::make_unique<io::FileCacheStatistics>();
301
67.4k
    _file_reader_stats = std::make_unique<io::FileReaderStats>();
302
67.4k
    RETURN_IF_ERROR(_init_io_ctx());
303
67.4k
    _io_ctx->file_cache_stats = _file_cache_statistics.get();
304
67.4k
    _io_ctx->file_reader_stats = _file_reader_stats.get();
305
67.4k
    _io_ctx->is_disposable = _state->query_options().disable_file_cache;
306
67.4k
    return Status::OK();
307
67.4k
}
308
309
67.9k
Status FileScannerV2::_open_impl(RuntimeState* state) {
310
67.9k
    RETURN_IF_CANCELLED(state);
311
67.9k
    RETURN_IF_ERROR(Scanner::_open_impl(state));
312
67.9k
    RETURN_IF_ERROR(_split_source->get_next(&_first_scan_range, &_current_range));
313
67.9k
    if (_first_scan_range) {
314
48.5k
        RETURN_IF_ERROR(_create_table_reader_for_format(_current_range, &_table_reader));
315
48.5k
        DORIS_CHECK(_table_reader != nullptr);
316
48.5k
        RETURN_IF_ERROR(_init_expr_ctxes());
317
48.5k
        RETURN_IF_ERROR(_init_table_reader(_current_range));
318
48.5k
    }
319
67.9k
    return Status::OK();
320
67.9k
}
321
322
193k
Status FileScannerV2::_get_block_impl(RuntimeState* state, Block* block, bool* eof) {
323
316k
    while (true) {
324
316k
        RETURN_IF_CANCELLED(state);
325
316k
        if (!_has_prepared_split) {
326
191k
            RETURN_IF_ERROR(_prepare_next_split(eof));
327
191k
            if (*eof) {
328
67.3k
                return Status::OK();
329
67.3k
            }
330
191k
        }
331
332
249k
        {
333
249k
            SCOPED_TIMER(_get_block_timer);
334
249k
            if (_should_run_adaptive_batch_size()) {
335
243k
                _table_reader->set_batch_size(_predict_reader_batch_rows());
336
243k
            }
337
249k
            RETURN_IF_ERROR(_table_reader->get_block(block, eof));
338
249k
        }
339
249k
        if (*eof) {
340
123k
            _state->update_num_finished_scan_range(1);
341
123k
            _has_prepared_split = false;
342
123k
            *eof = false;
343
123k
            continue;
344
123k
        }
345
126k
        _update_adaptive_batch_size(*block);
346
126k
        return Status::OK();
347
249k
    }
348
193k
}
349
350
191k
Status FileScannerV2::_prepare_next_split(bool* eos) {
351
191k
    bool has_next = _first_scan_range;
352
191k
    if (!_first_scan_range) {
353
142k
        RETURN_IF_ERROR(_split_source->get_next(&has_next, &_current_range));
354
142k
    }
355
191k
    _first_scan_range = false;
356
191k
    if (!has_next || _should_stop) {
357
67.3k
        *eos = true;
358
67.3k
        return Status::OK();
359
67.3k
    }
360
123k
    DORIS_CHECK(_table_reader != nullptr);
361
123k
    _current_range_path = _current_range.path;
362
123k
    _init_adaptive_batch_size_state(get_range_format_type(*_params, _current_range));
363
123k
    RETURN_IF_ERROR(_prepare_table_reader_split(_current_range));
364
123k
    COUNTER_UPDATE(_file_counter, 1);
365
123k
    _has_prepared_split = true;
366
123k
    *eos = false;
367
123k
    return Status::OK();
368
123k
}
369
370
48.5k
Status FileScannerV2::_init_table_reader(const TFileRangeDesc& range) {
371
48.5k
    const auto format_type = get_range_format_type(*_params, range);
372
48.5k
    format::FileFormat file_format;
373
48.5k
    RETURN_IF_ERROR(_to_file_format(format_type, &file_format));
374
48.5k
    DORIS_CHECK(_table_reader != nullptr);
375
376
48.5k
    VExprContextSPtrs table_conjuncts;
377
48.5k
    RETURN_IF_ERROR(_build_table_conjuncts(&table_conjuncts));
378
48.5k
    RETURN_IF_ERROR(_table_reader->init({
379
48.5k
            .projected_columns = _projected_columns,
380
48.5k
            .conjuncts = std::move(table_conjuncts),
381
48.5k
            .format = file_format,
382
48.5k
            .scan_params = const_cast<TFileScanRangeParams*>(_params),
383
48.5k
            .io_ctx = _io_ctx,
384
48.5k
            .runtime_state = _state,
385
48.5k
            .scanner_profile = _local_state->scanner_profile(),
386
48.5k
            .file_slot_descs = &_file_slot_descs,
387
48.5k
            .push_down_agg_type = _local_state->get_push_down_agg_type(),
388
48.5k
            .condition_cache_digest = _local_state->get_condition_cache_digest(),
389
48.5k
    }));
390
48.5k
    return Status::OK();
391
48.5k
}
392
393
Status FileScannerV2::_create_table_reader_for_format(
394
48.5k
        const TFileRangeDesc& range, std::unique_ptr<format::TableReader>* reader) const {
395
48.5k
    DORIS_CHECK(reader != nullptr);
396
48.5k
    const auto table_format = table_format_name(range);
397
48.5k
    if (table_format == "NotSet" || table_format == "tvf") {
398
4.28k
        *reader = std::make_unique<format::TableReader>();
399
44.2k
    } else if (table_format == "hive") {
400
19.8k
        *reader = format::hive::HiveReader::create_unique();
401
24.3k
    } else if (table_format == "iceberg") {
402
16.3k
        if (get_range_format_type(*_params, range) == TFileFormatType::FORMAT_JNI) {
403
1.26k
            *reader = std::make_unique<format::iceberg::IcebergSysTableJniReader>();
404
15.0k
        } else {
405
15.0k
            *reader = std::make_unique<format::iceberg::IcebergTableReader>();
406
15.0k
        }
407
16.3k
    } else if (table_format == "paimon") {
408
6.04k
        *reader = std::make_unique<format::paimon::PaimonHybridReader>();
409
6.04k
    } else if (table_format == "hudi") {
410
0
        *reader = std::make_unique<format::hudi::HudiHybridReader>();
411
1.98k
    } else if (table_format == "jdbc") {
412
1.24k
        *reader = std::make_unique<format::jdbc::JdbcJniReader>();
413
1.24k
    } else if (table_format == "max_compute") {
414
0
        const auto* mc_desc =
415
0
                static_cast<const MaxComputeTableDescriptor*>(_output_tuple_desc->table_desc());
416
0
        RETURN_IF_ERROR(mc_desc->init_status());
417
0
        *reader = std::make_unique<format::max_compute::MaxComputeJniReader>(mc_desc);
418
734
    } else if (table_format == "trino_connector") {
419
636
        *reader = std::make_unique<format::trino_connector::TrinoConnectorJniReader>();
420
636
    } else if (table_format == "remote_doris") {
421
98
        *reader = std::make_unique<format::remote_doris::RemoteDorisReader>();
422
98
    } else {
423
0
        return Status::NotSupported("FileScannerV2 does not support table format {}", table_format);
424
0
    }
425
48.5k
    return Status::OK();
426
48.5k
}
427
428
123k
Status FileScannerV2::_prepare_table_reader_split(const TFileRangeDesc& range) {
429
123k
    std::map<std::string, Field> partition_values;
430
123k
    RETURN_IF_ERROR(_generate_partition_values(range, &partition_values));
431
123k
    format::FileFormat current_split_format;
432
123k
    RETURN_IF_ERROR(_to_file_format(get_range_format_type(*_params, range), &current_split_format));
433
123k
    RETURN_IF_ERROR(_table_reader->prepare_split({
434
123k
            .partition_values = std::move(partition_values),
435
123k
            .cache = _kv_cache,
436
123k
            .current_range = range,
437
123k
            .current_split_format = current_split_format,
438
123k
            .global_rowid_context = _create_global_rowid_context(range),
439
123k
    }));
440
123k
    return Status::OK();
441
123k
}
442
443
7.54k
bool FileScannerV2::_should_enable_file_meta_cache() const {
444
7.54k
    return ExecEnv::GetInstance()->file_meta_cache()->enabled() &&
445
7.55k
           _split_source->num_scan_ranges() < config::max_external_file_meta_cache_num / 3;
446
7.54k
}
447
448
std::optional<format::GlobalRowIdContext> FileScannerV2::_create_global_rowid_context(
449
123k
        const TFileRangeDesc& range) const {
450
123k
    if (!_need_global_rowid_column) {
451
116k
        return std::nullopt;
452
116k
    }
453
7.54k
    auto& id_file_map = _state->get_id_file_map();
454
7.54k
    DORIS_CHECK(id_file_map != nullptr);
455
7.54k
    const auto file_id = id_file_map->get_file_mapping_id(
456
7.54k
            std::make_shared<FileMapping>(_local_state->cast<FileScanLocalState>().parent_id(),
457
7.54k
                                          range, _should_enable_file_meta_cache()));
458
7.54k
    return format::GlobalRowIdContext {
459
7.54k
            .version = IdManager::ID_VERSION,
460
7.54k
            .backend_id = BackendOptions::get_backend_id(),
461
7.54k
            .file_id = file_id,
462
7.54k
    };
463
123k
}
464
465
Status FileScannerV2::_generate_partition_values(
466
123k
        const TFileRangeDesc& range, std::map<std::string, Field>* partition_values) const {
467
123k
    DORIS_CHECK(partition_values != nullptr);
468
123k
    partition_values->clear();
469
123k
    if (!range.__isset.columns_from_path_keys || !range.__isset.columns_from_path) {
470
106k
        return Status::OK();
471
106k
    }
472
17.8k
    DORIS_CHECK(range.columns_from_path_keys.size() == range.columns_from_path.size());
473
47.1k
    for (size_t idx = 0; idx < range.columns_from_path_keys.size(); ++idx) {
474
29.3k
        const auto& key = range.columns_from_path_keys[idx];
475
29.3k
        const auto it = _partition_slot_descs.find(key);
476
29.3k
        if (it == _partition_slot_descs.end()) {
477
14.0k
            continue;
478
14.0k
        }
479
15.2k
        const auto& value = range.columns_from_path[idx];
480
15.2k
        const bool is_null = range.__isset.columns_from_path_is_null &&
481
15.3k
                             idx < range.columns_from_path_is_null.size() &&
482
15.2k
                             range.columns_from_path_is_null[idx];
483
15.2k
        Field field;
484
15.2k
        DORIS_CHECK(it->second.slot_desc != nullptr);
485
15.2k
        RETURN_IF_ERROR(_parse_partition_value(it->second.slot_desc, value, is_null, &field));
486
15.2k
        partition_values->emplace(it->second.canonical_name, std::move(field));
487
15.2k
    }
488
17.8k
    return Status::OK();
489
17.8k
}
490
491
Status FileScannerV2::_parse_partition_value(const SlotDescriptor* slot_desc,
492
                                             const std::string& value, bool is_null,
493
15.3k
                                             Field* field) const {
494
15.3k
    DORIS_CHECK(slot_desc != nullptr);
495
15.3k
    DORIS_CHECK(field != nullptr);
496
15.3k
    if (is_null) {
497
1.07k
        *field = Field::create_field<TYPE_NULL>(Null());
498
1.07k
        return Status::OK();
499
1.07k
    }
500
14.2k
    const auto data_type = remove_nullable(slot_desc->get_data_type_ptr());
501
14.2k
    auto column = data_type->create_column();
502
14.2k
    auto serde = data_type->get_serde();
503
14.2k
    DataTypeSerDe::FormatOptions options;
504
14.2k
    options.converted_from_string = true;
505
14.2k
    StringRef ref(value.data(), value.size());
506
14.2k
    RETURN_IF_ERROR(serde->from_string(ref, *column, options));
507
14.2k
    DORIS_CHECK(column->size() == 1);
508
14.2k
    *field = (*column)[0];
509
14.2k
    return Status::OK();
510
14.2k
}
511
512
48.5k
Status FileScannerV2::_init_expr_ctxes() {
513
48.5k
    _slot_id_to_desc.clear();
514
48.5k
    _slot_id_to_global_index.clear();
515
48.5k
    _partition_slot_descs.clear();
516
48.5k
    _file_slot_descs.clear();
517
279k
    for (const auto* slot_desc : _output_tuple_desc->slots()) {
518
279k
        _slot_id_to_desc.emplace(slot_desc->id(), slot_desc);
519
279k
    }
520
48.5k
    DORIS_CHECK(_table_reader != nullptr);
521
48.5k
    RETURN_IF_ERROR(_build_projected_columns(*_table_reader));
522
48.5k
    return Status::OK();
523
48.5k
}
524
525
48.5k
Status FileScannerV2::_build_projected_columns(const format::TableReader& table_reader) {
526
48.5k
    _projected_columns.clear();
527
48.5k
    _projected_columns.reserve(_params->required_slots.size());
528
48.5k
    _need_global_rowid_column = false;
529
48.5k
    format::ProjectedColumnBuildContext build_context {
530
48.5k
            .scan_params = _params,
531
48.5k
            .range = &_current_range,
532
48.5k
            .runtime_state = _state,
533
48.5k
    };
534
535
329k
    for (size_t slot_idx = 0; slot_idx < _params->required_slots.size(); ++slot_idx) {
536
280k
        const auto& slot_info = _params->required_slots[slot_idx];
537
280k
        const auto it = _slot_id_to_desc.find(slot_info.slot_id);
538
280k
        if (it == _slot_id_to_desc.end()) {
539
0
            return Status::InternalError("Unknown source slot descriptor, slot_id={}",
540
0
                                         slot_info.slot_id);
541
0
        }
542
280k
        auto column = _build_table_column(it->second);
543
280k
        if (column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
544
4.27k
            _need_global_rowid_column = true;
545
4.27k
        }
546
280k
        RETURN_IF_ERROR(_build_default_expr(slot_info, &column.default_expr));
547
280k
        build_context.schema_column.reset();
548
280k
        RETURN_IF_ERROR(table_reader.annotate_projected_column(slot_info, &build_context, &column));
549
        // Build nested children from access paths generated by the slot's access-path
550
        // expressions. A projected column can therefore contain only a subset of the schema
551
        // column's nested children.
552
280k
        RETURN_IF_ERROR(AccessPathParser::build_nested_children(
553
280k
                &column, it->second,
554
280k
                build_context.schema_column.has_value() ? &*build_context.schema_column : nullptr));
555
280k
        if (is_partition_slot(slot_info, column.name)) {
556
14.1k
            column.is_partition_key = true;
557
14.1k
            _partition_slot_descs.emplace(
558
14.1k
                    column.name,
559
14.1k
                    PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name});
560
14.1k
            for (const auto& alias : column.name_mapping) {
561
0
                _partition_slot_descs.emplace(
562
0
                        alias,
563
0
                        PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name});
564
0
            }
565
266k
        } else if (is_data_file_slot(slot_info, column.name)) {
566
262k
            _file_slot_descs.push_back(const_cast<SlotDescriptor*>(it->second));
567
262k
        }
568
280k
        const auto global_index = format::GlobalIndex(slot_idx);
569
280k
        _slot_id_to_global_index.emplace(slot_info.slot_id, global_index);
570
280k
        _projected_columns.push_back(std::move(column));
571
280k
    }
572
48.5k
    RETURN_IF_ERROR(table_reader.validate_projected_columns(build_context));
573
48.5k
    return Status::OK();
574
48.5k
}
575
576
Status FileScannerV2::_build_default_expr(const TFileScanSlotInfo& slot_info,
577
280k
                                          VExprContextSPtr* ctx) const {
578
280k
    DORIS_CHECK(ctx != nullptr);
579
280k
    if (slot_info.__isset.default_value_expr && !slot_info.default_value_expr.nodes.empty()) {
580
274k
        return VExpr::create_expr_tree(slot_info.default_value_expr, *ctx);
581
274k
    }
582
583
6.18k
    if (_params->__isset.default_value_of_src_slot) {
584
6.18k
        const auto it = _params->default_value_of_src_slot.find(slot_info.slot_id);
585
6.18k
        if (it != _params->default_value_of_src_slot.end() && !it->second.nodes.empty()) {
586
0
            return VExpr::create_expr_tree(it->second, *ctx);
587
0
        }
588
6.18k
    }
589
6.17k
    return Status::OK();
590
6.17k
}
591
592
280k
format::ColumnDefinition FileScannerV2::_build_table_column(const SlotDescriptor* slot_desc) {
593
280k
    DORIS_CHECK(slot_desc != nullptr);
594
280k
    format::ColumnDefinition column;
595
    // TODO(gabriel): why always BY_NAME here?
596
280k
    column.identifier = Field::create_field<TYPE_STRING>(slot_desc->col_name());
597
280k
    column.name = slot_desc->col_name();
598
280k
    column.type = slot_desc->get_data_type_ptr();
599
280k
    return column;
600
280k
}
601
602
48.5k
Status FileScannerV2::_build_table_conjuncts(VExprContextSPtrs* conjuncts) const {
603
48.5k
    DORIS_CHECK(conjuncts != nullptr);
604
48.5k
    conjuncts->clear();
605
48.5k
    conjuncts->reserve(_conjuncts.size());
606
48.5k
    for (const auto& conjunct : _conjuncts) {
607
32.4k
        VExprSPtr root;
608
32.4k
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
609
32.4k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&root, _slot_id_to_global_index));
610
32.4k
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
611
32.4k
    }
612
48.5k
    return Status::OK();
613
48.5k
}
614
615
0
TFileFormatType::type FileScannerV2::_get_current_format_type() const {
616
0
    return get_range_format_type(*_params, _current_range);
617
0
}
618
619
Status FileScannerV2::_to_file_format(TFileFormatType::type format_type,
620
172k
                                      format::FileFormat* file_format) {
621
172k
    DORIS_CHECK(file_format != nullptr);
622
172k
    switch (format_type) {
623
106k
    case TFileFormatType::FORMAT_PARQUET:
624
106k
        *file_format = format::FileFormat::PARQUET;
625
106k
        return Status::OK();
626
45.1k
    case TFileFormatType::FORMAT_ORC:
627
45.1k
        *file_format = format::FileFormat::ORC;
628
45.1k
        return Status::OK();
629
10.2k
    case TFileFormatType::FORMAT_JNI:
630
10.2k
        *file_format = format::FileFormat::JNI;
631
10.2k
        return Status::OK();
632
1.79k
    case TFileFormatType::FORMAT_CSV_PLAIN:
633
1.79k
    case TFileFormatType::FORMAT_CSV_GZ:
634
1.80k
    case TFileFormatType::FORMAT_CSV_BZ2:
635
1.80k
    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
636
1.80k
    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
637
1.80k
    case TFileFormatType::FORMAT_CSV_LZOP:
638
1.80k
    case TFileFormatType::FORMAT_CSV_DEFLATE:
639
1.80k
    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
640
1.80k
    case TFileFormatType::FORMAT_PROTO:
641
1.80k
        *file_format = format::FileFormat::CSV;
642
1.80k
        return Status::OK();
643
6.99k
    case TFileFormatType::FORMAT_TEXT:
644
6.99k
        *file_format = format::FileFormat::TEXT;
645
6.99k
        return Status::OK();
646
1.15k
    case TFileFormatType::FORMAT_JSON:
647
1.15k
        *file_format = format::FileFormat::JSON;
648
1.15k
        return Status::OK();
649
5
    case TFileFormatType::FORMAT_NATIVE:
650
5
        *file_format = format::FileFormat::NATIVE;
651
5
        return Status::OK();
652
197
    case TFileFormatType::FORMAT_ARROW:
653
197
        *file_format = format::FileFormat::ARROW;
654
197
        return Status::OK();
655
0
    default:
656
0
        return Status::NotSupported("FileScannerV2 does not support file format {}",
657
0
                                    to_string(format_type));
658
172k
    }
659
172k
}
660
661
67.7k
Status FileScannerV2::_init_io_ctx() {
662
67.7k
    _io_ctx = std::make_shared<io::IOContext>();
663
67.7k
    _io_ctx->query_id = &_state->query_id();
664
67.7k
    return Status::OK();
665
67.7k
}
666
667
123k
void FileScannerV2::_reset_adaptive_batch_size_state() {
668
123k
    _block_size_predictor.reset();
669
123k
    COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0));
670
123k
    COUNTER_SET(_adaptive_batch_actual_bytes_counter, int64_t(0));
671
123k
}
672
673
123k
void FileScannerV2::_init_adaptive_batch_size_state(TFileFormatType::type format_type) {
674
123k
    _reset_adaptive_batch_size_state();
675
123k
    if (!_should_enable_adaptive_batch_size(format_type)) {
676
100
        return;
677
100
    }
678
679
    // V2 native file readers do not have reliable row-width hints before the first batch. Start
680
    // every split with a small probe, then learn bytes-per-row from the materialized table block
681
    // and keep later batches close to RuntimeState::preferred_block_size_bytes().
682
123k
    _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>(
683
123k
            _state->preferred_block_size_bytes(), 0.0, ADAPTIVE_BATCH_INITIAL_PROBE_ROWS,
684
123k
            _state->batch_size());
685
123k
}
686
687
123k
bool FileScannerV2::_should_enable_adaptive_batch_size(TFileFormatType::type format_type) const {
688
123k
    if (!config::enable_adaptive_batch_size) {
689
0
        return false;
690
0
    }
691
123k
    switch (format_type) {
692
82.1k
    case TFileFormatType::FORMAT_PARQUET:
693
112k
    case TFileFormatType::FORMAT_ORC:
694
113k
    case TFileFormatType::FORMAT_CSV_PLAIN:
695
113k
    case TFileFormatType::FORMAT_CSV_GZ:
696
113k
    case TFileFormatType::FORMAT_CSV_BZ2:
697
113k
    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
698
113k
    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
699
113k
    case TFileFormatType::FORMAT_CSV_LZOP:
700
113k
    case TFileFormatType::FORMAT_CSV_DEFLATE:
701
113k
    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
702
113k
    case TFileFormatType::FORMAT_PROTO:
703
117k
    case TFileFormatType::FORMAT_TEXT:
704
118k
    case TFileFormatType::FORMAT_JSON:
705
123k
    case TFileFormatType::FORMAT_JNI:
706
123k
        return true;
707
100
    default:
708
100
        return false;
709
123k
    }
710
123k
}
711
712
375k
bool FileScannerV2::_should_run_adaptive_batch_size() const {
713
    // COUNT pushdown emits synthetic rows from file metadata and does not materialize file columns,
714
    // so there is no useful row-width sample to learn from.
715
375k
    return _block_size_predictor != nullptr &&
716
375k
           _local_state->get_push_down_agg_type() != TPushAggOp::type::COUNT;
717
375k
}
718
719
243k
size_t FileScannerV2::_predict_reader_batch_rows() {
720
243k
    DORIS_CHECK(_block_size_predictor != nullptr);
721
    // Before history exists this returns the probe row count; after update(), it returns roughly
722
    // preferred_block_size_bytes / EWMA(bytes_per_row), capped by RuntimeState::batch_size().
723
243k
    const size_t predicted_rows = _block_size_predictor->predict_next_rows();
724
243k
    COUNTER_SET(_adaptive_batch_predicted_rows_counter, static_cast<int64_t>(predicted_rows));
725
243k
    return predicted_rows;
726
243k
}
727
728
126k
void FileScannerV2::_update_adaptive_batch_size(const Block& block) {
729
126k
    if (!_should_run_adaptive_batch_size()) {
730
2.68k
        return;
731
2.68k
    }
732
123k
    COUNTER_SET(_adaptive_batch_actual_bytes_counter, static_cast<int64_t>(block.bytes()));
733
123k
    if (block.rows() == 0) {
734
0
        return;
735
0
    }
736
    // The sample is taken after TableReader has finalized file-local columns to table columns.
737
    // This matches the memory shape seen by upstream operators and catches very wide nested
738
    // columns, such as map/string payloads, after the first probe batch.
739
123k
    if (!_block_size_predictor->has_history()) {
740
54.9k
        COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1);
741
54.9k
    }
742
123k
    _block_size_predictor->update(block);
743
123k
}
744
745
68.0k
Status FileScannerV2::close(RuntimeState* state) {
746
68.0k
    if (!_try_close()) {
747
0
        return Status::OK();
748
0
    }
749
68.0k
    if (_table_reader != nullptr) {
750
48.5k
        RETURN_IF_ERROR(_table_reader->close());
751
48.5k
        _report_condition_cache_profile();
752
48.5k
        _table_reader.reset();
753
48.5k
    }
754
68.0k
    return Scanner::close(state);
755
68.0k
}
756
757
68.0k
void FileScannerV2::try_stop() {
758
68.0k
    Scanner::try_stop();
759
68.0k
    if (_io_ctx) {
760
68.0k
        _io_ctx->should_stop = true;
761
68.0k
    }
762
68.0k
}
763
764
124k
void FileScannerV2::update_realtime_counters() {
765
124k
    if (_file_reader_stats == nullptr) {
766
0
        return;
767
0
    }
768
124k
    DORIS_CHECK(_file_cache_statistics != nullptr);
769
124k
    const int64_t bytes_read = cast_set<int64_t>(_file_reader_stats->read_bytes);
770
124k
    auto* local_state = static_cast<FileScanLocalState*>(_local_state);
771
124k
    const auto file_type =
772
124k
            _current_range.__isset.file_type
773
124k
                    ? _current_range.file_type
774
124k
                    : (_params != nullptr && _params->__isset.file_type ? _params->file_type
775
28.5k
                                                                        : TFileType::FILE_LOCAL);
776
124k
    const auto deltas = _collect_realtime_counter_deltas(
777
124k
            *_file_reader_stats, *_file_cache_statistics, _uncached_reader_bytes_storage(file_type),
778
124k
            &_last_read_bytes, &_last_read_rows, &_last_bytes_read_from_local,
779
124k
            &_last_bytes_read_from_remote);
780
781
124k
    COUNTER_UPDATE(local_state->_scan_bytes, deltas.scan_bytes);
782
124k
    COUNTER_UPDATE(local_state->_scan_rows, deltas.scan_rows);
783
784
124k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(deltas.scan_rows);
785
124k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(deltas.scan_bytes);
786
124k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
787
124k
            deltas.scan_bytes_from_local_storage);
788
124k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_remote_storage(
789
124k
            deltas.scan_bytes_from_remote_storage);
790
791
124k
    COUNTER_SET(_file_read_bytes_counter, bytes_read);
792
124k
    COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls));
793
124k
    COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns));
794
795
124k
    DorisMetrics::instance()->query_scan_bytes->increment(deltas.scan_bytes);
796
124k
    DorisMetrics::instance()->query_scan_rows->increment(deltas.scan_rows);
797
124k
    DorisMetrics::instance()->query_scan_bytes_from_local->increment(
798
124k
            deltas.scan_bytes_from_local_storage);
799
124k
    DorisMetrics::instance()->query_scan_bytes_from_remote->increment(
800
124k
            deltas.scan_bytes_from_remote_storage);
801
124k
}
802
803
FileScannerV2::RealtimeCounterDeltas FileScannerV2::_collect_realtime_counter_deltas(
804
        const io::FileReaderStats& file_reader_stats,
805
        const io::FileCacheStatistics& file_cache_statistics,
806
        UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes,
807
        int64_t* last_read_rows, int64_t* last_bytes_read_from_local,
808
124k
        int64_t* last_bytes_read_from_remote) {
809
124k
    DORIS_CHECK(last_read_bytes != nullptr);
810
124k
    DORIS_CHECK(last_read_rows != nullptr);
811
124k
    DORIS_CHECK(last_bytes_read_from_local != nullptr);
812
124k
    DORIS_CHECK(last_bytes_read_from_remote != nullptr);
813
814
124k
    const int64_t read_bytes = cast_set<int64_t>(file_reader_stats.read_bytes);
815
124k
    const int64_t read_rows = cast_set<int64_t>(file_reader_stats.read_rows);
816
124k
    const int64_t bytes_read_from_local = file_cache_statistics.bytes_read_from_local;
817
124k
    const int64_t bytes_read_from_remote = file_cache_statistics.bytes_read_from_remote;
818
124k
    DORIS_CHECK(read_bytes >= *last_read_bytes);
819
124k
    DORIS_CHECK(read_rows >= *last_read_rows);
820
124k
    DORIS_CHECK(bytes_read_from_local >= *last_bytes_read_from_local);
821
124k
    DORIS_CHECK(bytes_read_from_remote >= *last_bytes_read_from_remote);
822
823
124k
    RealtimeCounterDeltas deltas;
824
124k
    deltas.scan_rows = read_rows - *last_read_rows;
825
124k
    deltas.scan_bytes = read_bytes - *last_read_bytes;
826
    // Peer cache is a known cache source, but it is not remote object storage.
827
124k
    const bool has_cache_source_stats = file_cache_statistics.num_local_io_total != 0 ||
828
124k
                                        file_cache_statistics.num_remote_io_total != 0 ||
829
124k
                                        file_cache_statistics.num_peer_io_total != 0 ||
830
124k
                                        bytes_read_from_local != 0 || bytes_read_from_remote != 0 ||
831
124k
                                        file_cache_statistics.bytes_read_from_peer != 0;
832
124k
    if (!has_cache_source_stats) {
833
110k
        switch (uncached_reader_bytes_storage) {
834
8.56k
        case UncachedReaderBytesStorage::LOCAL:
835
8.56k
            deltas.scan_bytes_from_local_storage = deltas.scan_bytes;
836
8.56k
            break;
837
101k
        case UncachedReaderBytesStorage::REMOTE:
838
101k
            deltas.scan_bytes_from_remote_storage = deltas.scan_bytes;
839
101k
            break;
840
262
        case UncachedReaderBytesStorage::NONE:
841
262
            break;
842
110k
        }
843
110k
    } else {
844
14.1k
        deltas.scan_bytes_from_local_storage = bytes_read_from_local - *last_bytes_read_from_local;
845
14.1k
        deltas.scan_bytes_from_remote_storage =
846
14.1k
                bytes_read_from_remote - *last_bytes_read_from_remote;
847
14.1k
    }
848
849
124k
    *last_read_bytes = read_bytes;
850
124k
    *last_read_rows = read_rows;
851
124k
    *last_bytes_read_from_local = bytes_read_from_local;
852
124k
    *last_bytes_read_from_remote = bytes_read_from_remote;
853
124k
    return deltas;
854
124k
}
855
856
FileScannerV2::UncachedReaderBytesStorage FileScannerV2::_uncached_reader_bytes_storage(
857
124k
        TFileType::type file_type) {
858
124k
    switch (file_type) {
859
8.56k
    case TFileType::FILE_LOCAL:
860
8.56k
        return UncachedReaderBytesStorage::LOCAL;
861
262
    case TFileType::FILE_STREAM:
862
262
        return UncachedReaderBytesStorage::NONE;
863
0
    case TFileType::FILE_BROKER:
864
34.6k
    case TFileType::FILE_S3:
865
115k
    case TFileType::FILE_HDFS:
866
115k
    case TFileType::FILE_NET:
867
115k
    case TFileType::FILE_HTTP:
868
115k
        return UncachedReaderBytesStorage::REMOTE;
869
124k
    }
870
0
    DORIS_CHECK(false) << "unknown file type: " << file_type;
871
0
    return UncachedReaderBytesStorage::NONE;
872
124k
}
873
874
67.9k
void FileScannerV2::_collect_profile_before_close() {
875
67.9k
    _report_file_reader_predicate_filtered_rows();
876
67.9k
    Scanner::_collect_profile_before_close();
877
67.9k
    if (_file_reader_stats != nullptr) {
878
67.9k
        COUNTER_SET(_file_read_bytes_counter, cast_set<int64_t>(_file_reader_stats->read_bytes));
879
67.9k
        COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls));
880
67.9k
        COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns));
881
67.9k
    }
882
    // Query profiles can be collected before Scanner::close() runs. Publish condition-cache
883
    // counters here as well, using deltas so this method and close() cannot double count.
884
67.9k
    _report_condition_cache_profile();
885
67.9k
}
886
887
68.0k
bool FileScannerV2::_should_update_load_counters() const {
888
68.0k
    if (_is_load) {
889
0
        return true;
890
0
    }
891
    // TVF based loads (e.g. http_stream, group commit relay) plan the load source as a
892
    // tvf query scan without src tuple desc, so _is_load is false. But rows filtered by
893
    // the load's WHERE clause still need to be reported as unselected rows. FILE_STREAM
894
    // is only reachable from such load entries, never from normal queries, so use it to
895
    // identify these scanners.
896
68.0k
    return (_params != nullptr && _params->__isset.file_type &&
897
68.0k
            _params->file_type == TFileType::FILE_STREAM) ||
898
68.0k
           (_current_range.__isset.file_type && _current_range.file_type == TFileType::FILE_STREAM);
899
68.0k
}
900
901
67.9k
void FileScannerV2::_report_file_reader_predicate_filtered_rows() {
902
67.9k
    const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->predicate_filtered_rows : 0;
903
67.9k
    const int64_t filtered_delta = filtered_rows - _reported_predicate_filtered_rows;
904
67.9k
    if (filtered_delta > 0) {
905
        // File readers can evaluate localized conjuncts before a block reaches Scanner. Count
906
        // those rows as scanner-level unselected rows so load statistics stay identical no matter
907
        // whether a predicate is pushed down or evaluated by Scanner::_filter_output_block().
908
2.89k
        _counter.num_rows_unselected += filtered_delta;
909
2.89k
        _reported_predicate_filtered_rows = filtered_rows;
910
2.89k
    }
911
67.9k
}
912
913
116k
void FileScannerV2::_report_condition_cache_profile() {
914
116k
    auto* local_state = static_cast<FileScanLocalState*>(_local_state);
915
116k
    const int64_t hit_count =
916
116k
            _table_reader != nullptr ? _table_reader->condition_cache_hit_count() : 0;
917
116k
    const int64_t hit_delta = hit_count - _reported_condition_cache_hit_count;
918
116k
    if (hit_delta > 0) {
919
2.73k
        COUNTER_UPDATE(local_state->_condition_cache_hit_counter, hit_delta);
920
2.73k
        _reported_condition_cache_hit_count = hit_count;
921
2.73k
    }
922
116k
    const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->condition_cache_filtered_rows : 0;
923
116k
    const int64_t filtered_delta = filtered_rows - _reported_condition_cache_filtered_rows;
924
116k
    if (filtered_delta > 0) {
925
562
        COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter, filtered_delta);
926
562
        _reported_condition_cache_filtered_rows = filtered_rows;
927
562
    }
928
116k
}
929
930
} // namespace doris