Coverage Report

Created: 2026-10-23 15:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/file_factory.h
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
#pragma once
18
19
#include <gen_cpp/PaloInternalService_types.h>
20
#include <gen_cpp/PlanNodes_types.h>
21
#include <gen_cpp/Types_types.h>
22
#include <glog/logging.h>
23
#include <stdint.h>
24
25
#include <map>
26
#include <memory>
27
#include <optional>
28
#include <string>
29
30
#include "common/factory_creator.h"
31
#include "common/status.h"
32
#include "io/fs/file_reader.h"
33
#include "io/fs/file_reader_writer_fwd.h"
34
#include "io/fs/file_system.h"
35
36
namespace doris {
37
namespace io {
38
class FileSystem;
39
class FileWriter;
40
41
struct FileSystemProperties {
42
    TFileType::type system_type;
43
    std::map<std::string, std::string> properties;
44
    THdfsParams hdfs_params;
45
    std::vector<TNetworkAddress> broker_addresses;
46
    // Typed native Azure access for FILE_S3, copied from TFileScanRangeParams. When set,
47
    // FileFactory builds the Azure client from it and ignores the S3-style Azure keys in
48
    // `properties`.
49
    std::optional<TAzureClientConfig> azure_client_config {};
50
};
51
52
// The typed native Azure access a scan carries, if FE sent one
53
// (TFileScanRangeParams.azure_client_config); every reader that builds FileSystemProperties from
54
// scan parameters copies it through this.
55
inline std::optional<TAzureClientConfig> azure_client_config_of(
56
6.53k
        const TFileScanRangeParams& params) {
57
6.53k
    return params.__isset.azure_client_config
58
6.53k
                   ? std::optional<TAzureClientConfig>(params.azure_client_config)
59
6.53k
                   : std::nullopt;
60
6.53k
}
61
62
struct FSPropertiesRef {
63
    TFileType::type type;
64
    const std::map<std::string, std::string>* properties {nullptr};
65
    const THdfsParams* hdfs_params {nullptr};
66
    const std::vector<TNetworkAddress>* broker_addresses {nullptr};
67
    // Typed native Azure access for FILE_S3; see FileSystemProperties::azure_client_config.
68
    const TAzureClientConfig* azure_client_config {nullptr};
69
70
9
    FSPropertiesRef(TFileType::type type_) : type(type_) {}
71
72
    FSPropertiesRef(const FileSystemProperties& fs_properties)
73
            : type(fs_properties.system_type),
74
              properties(&fs_properties.properties),
75
              hdfs_params(&fs_properties.hdfs_params),
76
              broker_addresses(&fs_properties.broker_addresses),
77
              azure_client_config(fs_properties.azure_client_config.has_value()
78
                                          ? &*fs_properties.azure_client_config
79
                                          : nullptr) {}
80
};
81
82
struct FileDescription {
83
    std::string path;
84
    // length of the file in bytes.
85
    // -1 means unset.
86
    // If the file length is not set, the file length will be fetched from the file system.
87
    int64_t file_size = -1;
88
    int64_t range_start_offset = 0;
89
    int64_t range_size = -1;
90
    // modification time of this file.
91
    // 0 means unset.
92
    int64_t mtime = 0;
93
    // A correctness promise that the bytes at this path never change. This is only used when
94
    // mtime is unavailable: immutable table formats may still key process-global caches by
95
    // fs/path/size, while mutable files must not do so because the same path and size can refer to
96
    // different contents after an overwrite. Generic Hive/local files must keep the default false.
97
    bool is_immutable = false;
98
    // for hdfs, eg: hdfs://nameservices1/
99
    // because for a hive table, differenet partitions may have different
100
    // locations(or fs), so different files may have different fs.
101
    std::string fs_name;
102
    bool file_cache_admission = true;
103
};
104
105
} // namespace io
106
class ExecEnv;
107
class RuntimeProfile;
108
class RuntimeState;
109
110
class FileFactory {
111
    ENABLE_FACTORY_CREATOR(FileFactory);
112
113
public:
114
    static io::FileReaderOptions get_reader_options(const TQueryOptions& option,
115
                                                    const io::FileDescription& fd);
116
117
    /// Create a temporary FileSystem for accessing file corresponding to `file_description`
118
    /// FIXME(plat1ko): Declare the path formats supported by each file system
119
    static Result<io::FileSystemSPtr> create_fs(const io::FSPropertiesRef& fs_properties,
120
                                                const io::FileDescription& file_description);
121
122
    /// Create FileWriter without FS. A non-null azure_client_config selects typed Azure access
123
    /// for FILE_S3, as in create_fs.
124
    static Result<io::FileWriterPtr> create_file_writer(
125
            TFileType::type type, ExecEnv* env,
126
            const std::vector<TNetworkAddress>& broker_addresses,
127
            const std::map<std::string, std::string>& properties, const std::string& path,
128
            const io::FileWriterOptions& options,
129
            const TAzureClientConfig* azure_client_config = nullptr);
130
131
    /// Create FileReader without FS
132
    static Result<io::FileReaderSPtr> create_file_reader(
133
            const io::FileSystemProperties& system_properties,
134
            const io::FileDescription& file_description,
135
            const io::FileReaderOptions& reader_options, RuntimeProfile* profile = nullptr);
136
137
    // Create FileReader for stream load pipe
138
    static Status create_pipe_reader(const TUniqueId& load_id, io::FileReaderSPtr* file_reader,
139
                                     RuntimeState* runtime_state, bool need_schema);
140
141
386
    static Result<TFileType::type> convert_storage_type(TStorageBackendType::type type) {
142
386
        switch (type) {
143
79
        case TStorageBackendType::LOCAL:
144
79
            return TFileType::FILE_LOCAL;
145
307
        case TStorageBackendType::S3:
146
307
            return TFileType::FILE_S3;
147
0
        case TStorageBackendType::AZURE:
148
0
            return TFileType::FILE_S3;
149
0
        case TStorageBackendType::BROKER:
150
0
            return TFileType::FILE_BROKER;
151
0
        case TStorageBackendType::HDFS:
152
0
        case TStorageBackendType::JFS:
153
0
            return TFileType::FILE_HDFS;
154
0
        default:
155
0
            return ResultError(Status::FatalError("not match type to convert, from type:{}", type));
156
386
        }
157
386
    }
158
159
private:
160
    static std::string _get_fs_name(const io::FileDescription& file_description);
161
162
    /// Create FileReader without FS
163
    static Result<io::FileReaderSPtr> _create_file_reader_internal(
164
            const io::FileSystemProperties& system_properties,
165
            const io::FileDescription& file_description,
166
            const io::FileReaderOptions& reader_options, RuntimeProfile* profile = nullptr);
167
};
168
169
} // namespace doris