Coverage Report

Created: 2026-07-08 16:37

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
110k
std::string table_format_name(const TFileRangeDesc& range) {
73
110k
    return range.__isset.table_format_params ? range.table_format_params.table_format_type
74
110k
                                             : "NotSet";
75
110k
}
76
77
TFileFormatType::type get_range_format_type(const TFileScanRangeParams& params,
78
322k
                                            const TFileRangeDesc& range) {
79
18.4E
    return range.__isset.format_type ? range.format_type : params.format_type;
80
322k
}
81
82
71.7k
bool is_supported_table_format(const TFileRangeDesc& range) {
83
71.7k
    const auto table_format = table_format_name(range);
84
71.7k
    if (table_format == "hudi" && range.__isset.table_format_params &&
85
71.7k
        range.table_format_params.__isset.hudi_params &&
86
71.7k
        range.table_format_params.hudi_params.__isset.delta_logs &&
87
71.7k
        !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
83.1k
    return table_format == "NotSet" || table_format == "tvf" || table_format == "hive" ||
93
71.7k
           table_format == "iceberg" || table_format == "paimon" || table_format == "hudi";
94
71.7k
}
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.19k
bool is_supported_jni_table_format(const TFileRangeDesc& range) {
101
5.19k
    const auto table_format = table_format_name(range);
102
5.19k
    if (table_format == "paimon") {
103
1.86k
        return range.__isset.table_format_params &&
104
1.86k
               range.table_format_params.__isset.paimon_params &&
105
1.86k
               range.table_format_params.paimon_params.__isset.reader_type &&
106
1.86k
               range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI;
107
1.86k
    }
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.19k
}
111
112
16.5k
bool is_csv_format(TFileFormatType::type format_type) {
113
16.5k
    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
15.5k
    default:
125
15.5k
        return false;
126
16.5k
    }
127
16.5k
}
128
129
15.5k
bool is_text_format(TFileFormatType::type format_type) {
130
15.5k
    return format_type == TFileFormatType::FORMAT_TEXT;
131
15.5k
}
132
133
10.8k
bool is_json_format(TFileFormatType::type format_type) {
134
10.8k
    return format_type == TFileFormatType::FORMAT_JSON;
135
10.8k
}
136
137
9.73k
bool is_native_format(TFileFormatType::type format_type) {
138
9.73k
    return format_type == TFileFormatType::FORMAT_NATIVE;
139
9.73k
}
140
141
214k
bool is_partition_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) {
142
214k
    if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) ||
143
214k
        column_name == BeConsts::ICEBERG_ROWID_COL) {
144
2.01k
        return false;
145
2.01k
    }
146
212k
    return slot_info.__isset.category ? slot_info.category == TColumnCategory::PARTITION_KEY
147
212k
                                      : !slot_info.is_file_slot;
148
214k
}
149
150
204k
bool is_data_file_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) {
151
204k
    if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) ||
152
204k
        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
202k
    if (slot_info.__isset.category) {
160
202k
        return slot_info.category == TColumnCategory::REGULAR ||
161
202k
               slot_info.category == TColumnCategory::GENERATED;
162
202k
    }
163
2
    return slot_info.is_file_slot;
164
202k
}
165
166
Status rewrite_slot_refs_to_global_index(
167
        VExprSPtr* expr,
168
71.1k
        const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) {
169
71.1k
    DORIS_CHECK(expr != nullptr);
170
71.1k
    if (*expr == nullptr) {
171
0
        return Status::OK();
172
0
    }
173
71.1k
    if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr->get());
174
71.1k
        runtime_filter != nullptr) {
175
6.72k
        auto impl = runtime_filter->get_impl();
176
6.72k
        DORIS_CHECK(impl != nullptr);
177
6.72k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&impl, slot_id_to_global_index));
178
6.72k
        runtime_filter->set_impl(std::move(impl));
179
6.72k
        return Status::OK();
180
6.72k
    }
181
64.3k
    if ((*expr)->is_slot_ref()) {
182
21.7k
        const auto* slot_ref = assert_cast<const VSlotRef*>(expr->get());
183
21.7k
        const auto global_index_it = slot_id_to_global_index.find(slot_ref->slot_id());
184
21.7k
        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.7k
        const auto global_index = global_index_it->second;
194
21.7k
        *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()),
195
21.7k
                                        cast_set<int>(global_index.value()), -1,
196
21.7k
                                        slot_ref->data_type(), slot_ref->column_name());
197
21.7k
        RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr));
198
21.7k
        return Status::OK();
199
21.7k
    }
200
42.6k
    auto children = (*expr)->children();
201
43.3k
    for (auto& child : children) {
202
43.3k
        if (child == nullptr) {
203
0
            continue;
204
0
        }
205
43.3k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&child, slot_id_to_global_index));
206
43.3k
    }
207
42.6k
    (*expr)->set_children(std::move(children));
208
42.6k
    return Status::OK();
209
42.6k
}
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
92.7k
bool FileScannerV2::is_supported(const TFileScanRangeParams& params, const TFileRangeDesc& range) {
249
92.7k
    const auto format_type = get_range_format_type(params, range);
250
92.7k
    if (format_type == TFileFormatType::FORMAT_PARQUET) {
251
67.9k
        return is_supported_table_format(range);
252
67.9k
    } else if (format_type == TFileFormatType::FORMAT_ARROW) {
253
101
        return is_supported_arrow_table_format(range);
254
24.6k
    } else if (format_type == TFileFormatType::FORMAT_JNI) {
255
5.19k
        return is_supported_jni_table_format(range);
256
19.4k
    } else if (is_csv_format(format_type) || is_text_format(format_type) ||
257
19.4k
               is_json_format(format_type) || is_native_format(format_type)) {
258
6.80k
        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
92.7k
}
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
44.2k
        : Scanner(state, local_state, limit, profile),
270
44.2k
          _split_source(std::move(split_source)),
271
44.2k
          _kv_cache(kv_cache) {
272
44.2k
    (void)colname_to_slot_id;
273
44.2k
    if (state->get_query_ctx() != nullptr &&
274
44.3k
        state->get_query_ctx()->file_scan_range_params_map.count(local_state->parent_id()) > 0) {
275
44.2k
        _params = &(state->get_query_ctx()->file_scan_range_params_map[local_state->parent_id()]);
276
18.4E
    } else {
277
18.4E
        _params = _split_source->get_params();
278
18.4E
    }
279
44.2k
}
280
281
44.2k
Status FileScannerV2::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
282
44.2k
    RETURN_IF_ERROR(Scanner::init(state, conjuncts));
283
44.2k
    _get_block_timer =
284
44.2k
            ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerV2GetBlockTime", 1);
285
44.2k
    _file_counter =
286
44.2k
            ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "FileNumber", TUnit::UNIT, 1);
287
44.2k
    _file_read_bytes_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(),
288
44.2k
                                                      "FileReadBytes", TUnit::BYTES, 1);
289
44.2k
    _file_read_calls_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(),
290
44.2k
                                                      "FileReadCalls", TUnit::UNIT, 1);
291
44.2k
    _file_read_time_counter =
292
44.2k
            ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileReadTime", 1);
293
44.2k
    _adaptive_batch_predicted_rows_counter = ADD_COUNTER_WITH_LEVEL(
294
44.2k
            _local_state->scanner_profile(), "AdaptiveBatchPredictedRows", TUnit::UNIT, 1);
295
44.2k
    _adaptive_batch_actual_bytes_counter = ADD_COUNTER_WITH_LEVEL(
296
44.2k
            _local_state->scanner_profile(), "AdaptiveBatchActualBytes", TUnit::BYTES, 1);
297
44.2k
    _adaptive_batch_probe_count_counter = ADD_COUNTER_WITH_LEVEL(
298
44.2k
            _local_state->scanner_profile(), "AdaptiveBatchProbeCount", TUnit::UNIT, 1);
299
44.2k
    _file_cache_statistics = std::make_unique<io::FileCacheStatistics>();
300
44.2k
    _file_reader_stats = std::make_unique<io::FileReaderStats>();
301
44.2k
    RETURN_IF_ERROR(_init_io_ctx());
302
44.2k
    _io_ctx->file_cache_stats = _file_cache_statistics.get();
303
44.2k
    _io_ctx->file_reader_stats = _file_reader_stats.get();
304
44.2k
    _io_ctx->is_disposable = _state->query_options().disable_file_cache;
305
44.2k
    return Status::OK();
306
44.2k
}
307
308
44.3k
Status FileScannerV2::_open_impl(RuntimeState* state) {
309
44.3k
    RETURN_IF_CANCELLED(state);
310
44.3k
    RETURN_IF_ERROR(Scanner::_open_impl(state));
311
44.3k
    RETURN_IF_ERROR(_split_source->get_next(&_first_scan_range, &_current_range));
312
44.3k
    if (_first_scan_range) {
313
34.0k
        RETURN_IF_ERROR(_create_table_reader_for_format(_current_range, &_table_reader));
314
34.0k
        DORIS_CHECK(_table_reader != nullptr);
315
34.0k
        RETURN_IF_ERROR(_init_expr_ctxes());
316
34.0k
        RETURN_IF_ERROR(_init_table_reader(_current_range));
317
34.0k
    }
318
44.3k
    return Status::OK();
319
44.3k
}
320
321
132k
Status FileScannerV2::_get_block_impl(RuntimeState* state, Block* block, bool* eof) {
322
226k
    while (true) {
323
226k
        RETURN_IF_CANCELLED(state);
324
226k
        if (!_has_prepared_split) {
325
138k
            RETURN_IF_ERROR(_prepare_next_split(eof));
326
138k
            if (*eof) {
327
43.8k
                return Status::OK();
328
43.8k
            }
329
138k
        }
330
331
182k
        {
332
182k
            SCOPED_TIMER(_get_block_timer);
333
182k
            if (_should_run_adaptive_batch_size()) {
334
177k
                _table_reader->set_batch_size(_predict_reader_batch_rows());
335
177k
            }
336
182k
            RETURN_IF_ERROR(_table_reader->get_block(block, eof));
337
182k
        }
338
182k
        if (*eof) {
339
93.6k
            _state->update_num_finished_scan_range(1);
340
93.6k
            _has_prepared_split = false;
341
93.6k
            *eof = false;
342
93.6k
            continue;
343
93.6k
        }
344
88.5k
        _update_adaptive_batch_size(*block);
345
88.5k
        return Status::OK();
346
182k
    }
347
132k
}
348
349
137k
Status FileScannerV2::_prepare_next_split(bool* eos) {
350
137k
    bool has_next = _first_scan_range;
351
137k
    if (!_first_scan_range) {
352
103k
        RETURN_IF_ERROR(_split_source->get_next(&has_next, &_current_range));
353
103k
    }
354
137k
    _first_scan_range = false;
355
137k
    if (!has_next || _should_stop) {
356
43.8k
        *eos = true;
357
43.8k
        return Status::OK();
358
43.8k
    }
359
94.0k
    DORIS_CHECK(_table_reader != nullptr);
360
94.0k
    _current_range_path = _current_range.path;
361
94.0k
    _init_adaptive_batch_size_state(get_range_format_type(*_params, _current_range));
362
94.0k
    RETURN_IF_ERROR(_prepare_table_reader_split(_current_range));
363
94.0k
    COUNTER_UPDATE(_file_counter, 1);
364
94.0k
    _has_prepared_split = true;
365
94.0k
    *eos = false;
366
94.0k
    return Status::OK();
367
94.0k
}
368
369
34.0k
Status FileScannerV2::_init_table_reader(const TFileRangeDesc& range) {
370
34.0k
    const auto format_type = get_range_format_type(*_params, range);
371
34.0k
    format::FileFormat file_format;
372
34.0k
    RETURN_IF_ERROR(_to_file_format(format_type, &file_format));
373
34.0k
    DORIS_CHECK(_table_reader != nullptr);
374
375
34.0k
    VExprContextSPtrs table_conjuncts;
376
34.0k
    RETURN_IF_ERROR(_build_table_conjuncts(&table_conjuncts));
377
34.0k
    RETURN_IF_ERROR(_table_reader->init({
378
34.0k
            .projected_columns = _projected_columns,
379
34.0k
            .conjuncts = std::move(table_conjuncts),
380
34.0k
            .format = file_format,
381
34.0k
            .scan_params = const_cast<TFileScanRangeParams*>(_params),
382
34.0k
            .io_ctx = _io_ctx,
383
34.0k
            .runtime_state = _state,
384
34.0k
            .scanner_profile = _local_state->scanner_profile(),
385
34.0k
            .file_slot_descs = &_file_slot_descs,
386
34.0k
            .push_down_agg_type = _local_state->get_push_down_agg_type(),
387
34.0k
            .condition_cache_digest = _local_state->get_condition_cache_digest(),
388
34.0k
    }));
389
34.0k
    return Status::OK();
390
34.0k
}
391
392
Status FileScannerV2::_create_table_reader_for_format(
393
34.0k
        const TFileRangeDesc& range, std::unique_ptr<format::TableReader>* reader) const {
394
34.0k
    DORIS_CHECK(reader != nullptr);
395
34.0k
    const auto table_format = table_format_name(range);
396
34.0k
    if (table_format == "NotSet" || table_format == "tvf") {
397
4.19k
        *reader = std::make_unique<format::TableReader>();
398
29.9k
    } else if (table_format == "hive") {
399
11.3k
        *reader = format::hive::HiveReader::create_unique();
400
18.6k
    } else if (table_format == "iceberg") {
401
12.4k
        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.4k
    } 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
34.0k
    return Status::OK();
425
34.0k
}
426
427
94.0k
Status FileScannerV2::_prepare_table_reader_split(const TFileRangeDesc& range) {
428
94.0k
    std::map<std::string, Field> partition_values;
429
94.0k
    RETURN_IF_ERROR(_generate_partition_values(range, &partition_values));
430
94.0k
    format::FileFormat current_split_format;
431
94.0k
    RETURN_IF_ERROR(_to_file_format(get_range_format_type(*_params, range), &current_split_format));
432
94.0k
    RETURN_IF_ERROR(_table_reader->prepare_split({
433
94.0k
            .partition_values = std::move(partition_values),
434
94.0k
            .cache = _kv_cache,
435
94.0k
            .current_range = range,
436
94.0k
            .current_split_format = current_split_format,
437
94.0k
            .global_rowid_context = _create_global_rowid_context(range),
438
94.0k
    }));
439
94.0k
    return Status::OK();
440
94.0k
}
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.9k
        const TFileRangeDesc& range) const {
449
93.9k
    if (!_need_global_rowid_column) {
450
91.8k
        return std::nullopt;
451
91.8k
    }
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.9k
}
463
464
Status FileScannerV2::_generate_partition_values(
465
94.0k
        const TFileRangeDesc& range, std::map<std::string, Field>* partition_values) const {
466
94.0k
    DORIS_CHECK(partition_values != nullptr);
467
94.0k
    partition_values->clear();
468
94.0k
    if (!range.__isset.columns_from_path_keys || !range.__isset.columns_from_path) {
469
83.6k
        return Status::OK();
470
83.6k
    }
471
10.3k
    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.3k
    return Status::OK();
488
10.3k
}
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.1k
    const auto data_type = remove_nullable(slot_desc->get_data_type_ptr());
500
10.1k
    auto column = data_type->create_column();
501
10.1k
    auto serde = data_type->get_serde();
502
10.1k
    DataTypeSerDe::FormatOptions options;
503
10.1k
    options.converted_from_string = true;
504
10.1k
    StringRef ref(value.data(), value.size());
505
10.1k
    RETURN_IF_ERROR(serde->from_string(ref, *column, options));
506
10.1k
    DORIS_CHECK(column->size() == 1);
507
10.1k
    *field = (*column)[0];
508
10.1k
    return Status::OK();
509
10.1k
}
510
511
34.0k
Status FileScannerV2::_init_expr_ctxes() {
512
34.0k
    _slot_id_to_desc.clear();
513
34.0k
    _slot_id_to_global_index.clear();
514
34.0k
    _partition_slot_descs.clear();
515
34.0k
    _file_slot_descs.clear();
516
214k
    for (const auto* slot_desc : _output_tuple_desc->slots()) {
517
214k
        _slot_id_to_desc.emplace(slot_desc->id(), slot_desc);
518
214k
    }
519
34.0k
    DORIS_CHECK(_table_reader != nullptr);
520
34.0k
    RETURN_IF_ERROR(_build_projected_columns(*_table_reader));
521
34.0k
    return Status::OK();
522
34.0k
}
523
524
34.0k
Status FileScannerV2::_build_projected_columns(const format::TableReader& table_reader) {
525
34.0k
    _projected_columns.clear();
526
34.0k
    _projected_columns.reserve(_params->required_slots.size());
527
34.0k
    _need_global_rowid_column = false;
528
34.0k
    format::ProjectedColumnBuildContext build_context {
529
34.0k
            .scan_params = _params,
530
34.0k
            .range = &_current_range,
531
34.0k
            .runtime_state = _state,
532
34.0k
    };
533
534
248k
    for (size_t slot_idx = 0; slot_idx < _params->required_slots.size(); ++slot_idx) {
535
214k
        const auto& slot_info = _params->required_slots[slot_idx];
536
214k
        const auto it = _slot_id_to_desc.find(slot_info.slot_id);
537
214k
        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
214k
        auto column = _build_table_column(it->second);
542
214k
        if (column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
543
1.91k
            _need_global_rowid_column = true;
544
1.91k
        }
545
214k
        RETURN_IF_ERROR(_build_default_expr(slot_info, &column.default_expr));
546
214k
        build_context.schema_column.reset();
547
214k
        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
214k
        RETURN_IF_ERROR(AccessPathParser::build_nested_children(
552
214k
                &column, it->second,
553
214k
                build_context.schema_column.has_value() ? &*build_context.schema_column : nullptr));
554
214k
        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
204k
        } else if (is_data_file_slot(slot_info, column.name)) {
565
202k
            _file_slot_descs.push_back(const_cast<SlotDescriptor*>(it->second));
566
202k
        }
567
214k
        const auto global_index = format::GlobalIndex(slot_idx);
568
214k
        _slot_id_to_global_index.emplace(slot_info.slot_id, global_index);
569
214k
        _projected_columns.push_back(std::move(column));
570
214k
    }
571
34.0k
    RETURN_IF_ERROR(table_reader.validate_projected_columns(build_context));
572
34.0k
    return Status::OK();
573
34.0k
}
574
575
Status FileScannerV2::_build_default_expr(const TFileScanSlotInfo& slot_info,
576
214k
                                          VExprContextSPtr* ctx) const {
577
214k
    DORIS_CHECK(ctx != nullptr);
578
214k
    if (slot_info.__isset.default_value_expr && !slot_info.default_value_expr.nodes.empty()) {
579
210k
        return VExpr::create_expr_tree(slot_info.default_value_expr, *ctx);
580
210k
    }
581
582
4.29k
    if (_params->__isset.default_value_of_src_slot) {
583
4.29k
        const auto it = _params->default_value_of_src_slot.find(slot_info.slot_id);
584
4.29k
        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
4.29k
    }
588
4.28k
    return Status::OK();
589
4.28k
}
590
591
214k
format::ColumnDefinition FileScannerV2::_build_table_column(const SlotDescriptor* slot_desc) {
592
214k
    DORIS_CHECK(slot_desc != nullptr);
593
214k
    format::ColumnDefinition column;
594
    // TODO(gabriel): why always BY_NAME here?
595
214k
    column.identifier = Field::create_field<TYPE_STRING>(slot_desc->col_name());
596
214k
    column.name = slot_desc->col_name();
597
214k
    column.type = slot_desc->get_data_type_ptr();
598
214k
    return column;
599
214k
}
600
601
34.0k
Status FileScannerV2::_build_table_conjuncts(VExprContextSPtrs* conjuncts) const {
602
34.0k
    DORIS_CHECK(conjuncts != nullptr);
603
34.0k
    conjuncts->clear();
604
34.0k
    conjuncts->reserve(_conjuncts.size());
605
34.0k
    for (const auto& conjunct : _conjuncts) {
606
21.0k
        VExprSPtr root;
607
21.0k
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
608
21.0k
        RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&root, _slot_id_to_global_index));
609
21.0k
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
610
21.0k
    }
611
34.0k
    return Status::OK();
612
34.0k
}
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
128k
                                      format::FileFormat* file_format) {
620
128k
    DORIS_CHECK(file_format != nullptr);
621
128k
    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.80k
    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
7.09k
    case TFileFormatType::FORMAT_TEXT:
640
7.09k
        *file_format = format::FileFormat::TEXT;
641
7.09k
        return Status::OK();
642
2.30k
    case TFileFormatType::FORMAT_JSON:
643
2.30k
        *file_format = format::FileFormat::JSON;
644
2.30k
        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
128k
    }
655
128k
}
656
657
44.2k
Status FileScannerV2::_init_io_ctx() {
658
44.2k
    _io_ctx = std::make_shared<io::IOContext>();
659
44.2k
    _io_ctx->query_id = &_state->query_id();
660
44.2k
    return Status::OK();
661
44.2k
}
662
663
94.0k
void FileScannerV2::_reset_adaptive_batch_size_state() {
664
94.0k
    _block_size_predictor.reset();
665
94.0k
    COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0));
666
94.0k
    COUNTER_SET(_adaptive_batch_actual_bytes_counter, int64_t(0));
667
94.0k
}
668
669
94.0k
void FileScannerV2::_init_adaptive_batch_size_state(TFileFormatType::type format_type) {
670
94.0k
    _reset_adaptive_batch_size_state();
671
94.0k
    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.9k
    _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>(
679
93.9k
            _state->preferred_block_size_bytes(), 0.0, ADAPTIVE_BATCH_INITIAL_PROBE_ROWS,
680
93.9k
            _state->batch_size());
681
93.9k
}
682
683
94.1k
bool FileScannerV2::_should_enable_adaptive_batch_size(TFileFormatType::type format_type) const {
684
94.1k
    if (!config::enable_adaptive_batch_size) {
685
0
        return false;
686
0
    }
687
94.1k
    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.7k
    case TFileFormatType::FORMAT_TEXT:
700
88.8k
    case TFileFormatType::FORMAT_JSON:
701
94.0k
    case TFileFormatType::FORMAT_JNI:
702
94.0k
        return true;
703
100
    default:
704
100
        return false;
705
94.1k
    }
706
94.1k
}
707
708
270k
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
270k
    return _block_size_predictor != nullptr &&
712
270k
           _local_state->get_push_down_agg_type() != TPushAggOp::type::COUNT;
713
270k
}
714
715
177k
size_t FileScannerV2::_predict_reader_batch_rows() {
716
177k
    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
177k
    const size_t predicted_rows = _block_size_predictor->predict_next_rows();
720
177k
    COUNTER_SET(_adaptive_batch_predicted_rows_counter, static_cast<int64_t>(predicted_rows));
721
177k
    return predicted_rows;
722
177k
}
723
724
88.5k
void FileScannerV2::_update_adaptive_batch_size(const Block& block) {
725
88.5k
    if (!_should_run_adaptive_batch_size()) {
726
2.26k
        return;
727
2.26k
    }
728
86.3k
    COUNTER_SET(_adaptive_batch_actual_bytes_counter, static_cast<int64_t>(block.bytes()));
729
86.3k
    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.3k
    if (!_block_size_predictor->has_history()) {
736
37.5k
        COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1);
737
37.5k
    }
738
86.3k
    _block_size_predictor->update(block);
739
86.3k
}
740
741
44.4k
Status FileScannerV2::close(RuntimeState* state) {
742
44.4k
    if (!_try_close()) {
743
0
        return Status::OK();
744
0
    }
745
44.4k
    if (_table_reader != nullptr) {
746
34.0k
        RETURN_IF_ERROR(_table_reader->close());
747
34.0k
        _report_condition_cache_profile();
748
34.0k
        _table_reader.reset();
749
34.0k
    }
750
44.4k
    return Scanner::close(state);
751
44.4k
}
752
753
44.4k
void FileScannerV2::try_stop() {
754
44.4k
    Scanner::try_stop();
755
44.4k
    if (_io_ctx) {
756
44.4k
        _io_ctx->should_stop = true;
757
44.4k
    }
758
44.4k
}
759
760
84.8k
void FileScannerV2::update_realtime_counters() {
761
84.8k
    if (_file_reader_stats == nullptr) {
762
0
        return;
763
0
    }
764
84.8k
    DORIS_CHECK(_file_cache_statistics != nullptr);
765
84.8k
    const int64_t bytes_read = cast_set<int64_t>(_file_reader_stats->read_bytes);
766
84.8k
    auto* local_state = static_cast<FileScanLocalState*>(_local_state);
767
84.8k
    const auto file_type =
768
84.8k
            _current_range.__isset.file_type
769
84.8k
                    ? _current_range.file_type
770
84.8k
                    : (_params != nullptr && _params->__isset.file_type ? _params->file_type
771
19.1k
                                                                        : TFileType::FILE_LOCAL);
772
84.8k
    const auto deltas = _collect_realtime_counter_deltas(
773
84.8k
            *_file_reader_stats, *_file_cache_statistics, _uncached_reader_bytes_storage(file_type),
774
84.8k
            &_last_read_bytes, &_last_read_rows, &_last_bytes_read_from_local,
775
84.8k
            &_last_bytes_read_from_remote);
776
777
84.8k
    COUNTER_UPDATE(local_state->_scan_bytes, deltas.scan_bytes);
778
84.8k
    COUNTER_UPDATE(local_state->_scan_rows, deltas.scan_rows);
779
780
84.8k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(deltas.scan_rows);
781
84.8k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(deltas.scan_bytes);
782
84.8k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage(
783
84.8k
            deltas.scan_bytes_from_local_storage);
784
84.8k
    _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_remote_storage(
785
84.8k
            deltas.scan_bytes_from_remote_storage);
786
787
84.8k
    COUNTER_SET(_file_read_bytes_counter, bytes_read);
788
84.8k
    COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls));
789
84.8k
    COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns));
790
791
84.8k
    DorisMetrics::instance()->query_scan_bytes->increment(deltas.scan_bytes);
792
84.8k
    DorisMetrics::instance()->query_scan_rows->increment(deltas.scan_rows);
793
84.8k
    DorisMetrics::instance()->query_scan_bytes_from_local->increment(
794
84.8k
            deltas.scan_bytes_from_local_storage);
795
84.8k
    DorisMetrics::instance()->query_scan_bytes_from_remote->increment(
796
84.8k
            deltas.scan_bytes_from_remote_storage);
797
84.8k
}
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.8k
        int64_t* last_bytes_read_from_remote) {
805
84.8k
    DORIS_CHECK(last_read_bytes != nullptr);
806
84.8k
    DORIS_CHECK(last_read_rows != nullptr);
807
84.8k
    DORIS_CHECK(last_bytes_read_from_local != nullptr);
808
84.8k
    DORIS_CHECK(last_bytes_read_from_remote != nullptr);
809
810
84.8k
    const int64_t read_bytes = cast_set<int64_t>(file_reader_stats.read_bytes);
811
84.8k
    const int64_t read_rows = cast_set<int64_t>(file_reader_stats.read_rows);
812
84.8k
    const int64_t bytes_read_from_local = file_cache_statistics.bytes_read_from_local;
813
84.8k
    const int64_t bytes_read_from_remote = file_cache_statistics.bytes_read_from_remote;
814
84.8k
    DORIS_CHECK(read_bytes >= *last_read_bytes);
815
84.8k
    DORIS_CHECK(read_rows >= *last_read_rows);
816
84.8k
    DORIS_CHECK(bytes_read_from_local >= *last_bytes_read_from_local);
817
84.8k
    DORIS_CHECK(bytes_read_from_remote >= *last_bytes_read_from_remote);
818
819
84.8k
    RealtimeCounterDeltas deltas;
820
84.8k
    deltas.scan_rows = read_rows - *last_read_rows;
821
84.8k
    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.8k
    const bool has_cache_source_stats = file_cache_statistics.num_local_io_total != 0 ||
824
84.8k
                                        file_cache_statistics.num_remote_io_total != 0 ||
825
84.8k
                                        file_cache_statistics.num_peer_io_total != 0 ||
826
84.8k
                                        bytes_read_from_local != 0 || bytes_read_from_remote != 0 ||
827
84.8k
                                        file_cache_statistics.bytes_read_from_peer != 0;
828
84.8k
    if (!has_cache_source_stats) {
829
71.2k
        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.6k
        case UncachedReaderBytesStorage::REMOTE:
834
62.6k
            deltas.scan_bytes_from_remote_storage = deltas.scan_bytes;
835
62.6k
            break;
836
262
        case UncachedReaderBytesStorage::NONE:
837
262
            break;
838
71.2k
        }
839
71.2k
    } 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.8k
    *last_read_bytes = read_bytes;
846
84.8k
    *last_read_rows = read_rows;
847
84.8k
    *last_bytes_read_from_local = bytes_read_from_local;
848
84.8k
    *last_bytes_read_from_remote = bytes_read_from_remote;
849
84.8k
    return deltas;
850
84.8k
}
851
852
FileScannerV2::UncachedReaderBytesStorage FileScannerV2::_uncached_reader_bytes_storage(
853
84.8k
        TFileType::type file_type) {
854
84.8k
    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.4k
    case TFileType::FILE_S3:
861
75.1k
    case TFileType::FILE_HDFS:
862
75.1k
    case TFileType::FILE_NET:
863
76.1k
    case TFileType::FILE_HTTP:
864
76.1k
        return UncachedReaderBytesStorage::REMOTE;
865
84.8k
    }
866
0
    DORIS_CHECK(false) << "unknown file type: " << file_type;
867
0
    return UncachedReaderBytesStorage::NONE;
868
84.8k
}
869
870
44.4k
void FileScannerV2::_collect_profile_before_close() {
871
44.4k
    _report_file_reader_predicate_filtered_rows();
872
44.4k
    Scanner::_collect_profile_before_close();
873
44.4k
    if (_file_reader_stats != nullptr) {
874
44.4k
        COUNTER_SET(_file_read_bytes_counter, cast_set<int64_t>(_file_reader_stats->read_bytes));
875
44.4k
        COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls));
876
44.4k
        COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns));
877
44.4k
    }
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
44.4k
    _report_condition_cache_profile();
881
44.4k
}
882
883
44.4k
bool FileScannerV2::_should_update_load_counters() const {
884
44.4k
    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
44.4k
    return (_params != nullptr && _params->__isset.file_type &&
893
44.4k
            _params->file_type == TFileType::FILE_STREAM) ||
894
44.4k
           (_current_range.__isset.file_type && _current_range.file_type == TFileType::FILE_STREAM);
895
44.4k
}
896
897
44.4k
void FileScannerV2::_report_file_reader_predicate_filtered_rows() {
898
44.4k
    const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->predicate_filtered_rows : 0;
899
44.4k
    const int64_t filtered_delta = filtered_rows - _reported_predicate_filtered_rows;
900
44.4k
    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.89k
        _counter.num_rows_unselected += filtered_delta;
905
2.89k
        _reported_predicate_filtered_rows = filtered_rows;
906
2.89k
    }
907
44.4k
}
908
909
78.5k
void FileScannerV2::_report_condition_cache_profile() {
910
78.5k
    auto* local_state = static_cast<FileScanLocalState*>(_local_state);
911
78.5k
    const int64_t hit_count =
912
78.5k
            _table_reader != nullptr ? _table_reader->condition_cache_hit_count() : 0;
913
78.5k
    const int64_t hit_delta = hit_count - _reported_condition_cache_hit_count;
914
78.5k
    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
78.5k
    const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->condition_cache_filtered_rows : 0;
919
78.5k
    const int64_t filtered_delta = filtered_rows - _reported_condition_cache_filtered_rows;
920
78.5k
    if (filtered_delta > 0) {
921
180
        COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter, filtered_delta);
922
180
        _reported_condition_cache_filtered_rows = filtered_rows;
923
180
    }
924
78.5k
}
925
926
} // namespace doris