Coverage Report

Created: 2026-07-09 13:10

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