ClickHouse/src/Storages/StorageFile.h

133 lines
4.2 KiB
C++
Raw Normal View History

#pragma once
#include <Storages/IStorage.h>
2021-10-02 07:13:14 +00:00
#include <base/logger_useful.h>
2017-04-08 01:32:05 +00:00
#include <atomic>
#include <shared_mutex>
2021-10-02 07:13:14 +00:00
#include <base/shared_ptr_helper.h>
2017-04-08 01:32:05 +00:00
namespace DB
{
class StorageFileBlockInputStream;
class StorageFileBlockOutputStream;
2021-06-15 19:55:21 +00:00
class StorageFile final : public shared_ptr_helper<StorageFile>, public IStorage
{
2021-10-25 16:23:44 +00:00
friend struct shared_ptr_helper<StorageFile>;
friend class PartitionedStorageFileSink;
public:
std::string getName() const override { return "File"; }
2020-08-03 13:54:14 +00:00
Pipe read(
const Names & column_names,
const StorageMetadataPtr & /*metadata_snapshot*/,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
2017-06-02 15:54:39 +00:00
unsigned num_streams) override;
2021-07-23 14:25:35 +00:00
SinkToStoragePtr write(
2017-05-21 22:25:25 +00:00
const ASTPtr & query,
const StorageMetadataPtr & /*metadata_snapshot*/,
ContextPtr context) override;
2020-06-18 10:29:13 +00:00
void truncate(
const ASTPtr & /*query*/,
const StorageMetadataPtr & /* metadata_snapshot */,
ContextPtr /* context */,
2020-06-18 16:10:47 +00:00
TableExclusiveLockHolder &) override;
2020-01-05 02:57:09 +00:00
2020-04-07 14:05:51 +00:00
void rename(const String & new_path_to_table_data, const StorageID & new_table_id) override;
2019-11-11 14:28:28 +00:00
2020-11-01 17:38:43 +00:00
bool storesDataOnDisk() const override;
2019-09-06 08:53:32 +00:00
Strings getDataPaths() const override;
struct CommonArguments : public WithContext
2019-10-30 14:17:55 +00:00
{
StorageID table_id;
std::string format_name;
std::optional<FormatSettings> format_settings;
std::string compression_method;
2019-10-30 14:17:55 +00:00
const ColumnsDescription & columns;
const ConstraintsDescription & constraints;
2021-04-23 12:18:23 +00:00
const String & comment;
2019-10-30 14:17:55 +00:00
};
NamesAndTypesList getVirtuals() const override;
2020-04-27 13:55:30 +00:00
2021-04-26 13:34:44 +00:00
static Strings getPathsList(const String & table_path, const String & user_files_path, ContextPtr context, size_t & total_bytes_to_read);
2021-03-31 14:21:19 +00:00
/// Check if the format is column-oriented.
/// Is is useful because column oriented formats could effectively skip unknown columns
/// So we can create a header of only required columns in read method and ask
/// format to read only them. Note: this hack cannot be done with ordinary formats like TSV.
bool isColumnOriented() const;
2021-10-25 16:23:44 +00:00
bool supportsPartitionBy() const override { return true; }
ColumnsDescription getTableStructureFromFileDescriptor(ContextPtr context);
static ColumnsDescription getTableStructureFromFile(
const String & format,
const std::vector<String> & paths,
const String & compression_method,
const std::optional<FormatSettings> & format_settings,
ContextPtr context);
protected:
friend class StorageFileSource;
2021-07-23 14:25:35 +00:00
friend class StorageFileSink;
2019-10-30 14:17:55 +00:00
/// From file descriptor
StorageFile(int table_fd_, CommonArguments args);
/// From user's file
StorageFile(const std::string & table_path_, const std::string & user_files_path, CommonArguments args);
2019-10-30 14:17:55 +00:00
/// From table in database
StorageFile(const std::string & relative_table_dir_path, CommonArguments args);
private:
2019-10-30 14:17:55 +00:00
explicit StorageFile(CommonArguments args);
2020-04-06 23:22:44 +00:00
void setStorageMetadata(CommonArguments args);
std::string format_name;
// We use format settings from global context + CREATE query for File table
// function -- in this case, format_settings is set.
// For `file` table function, we use format settings from current user context,
// in this case, format_settings is not set.
std::optional<FormatSettings> format_settings;
int table_fd = -1;
String compression_method;
std::string base_path;
2019-09-06 18:29:41 +00:00
std::vector<std::string> paths;
2019-07-21 13:15:04 +00:00
bool is_db_table = true; /// Table is stored in real database, not user's file
bool use_table_fd = false; /// Use table_fd instead of path
mutable std::shared_timed_mutex rwlock;
2020-05-30 21:57:37 +00:00
Poco::Logger * log = &Poco::Logger::get("StorageFile");
2021-04-26 13:34:44 +00:00
/// Total number of bytes to read (sums for multiple files in case of globs). Needed for progress bar.
2021-04-26 13:34:44 +00:00
size_t total_bytes_to_read = 0;
2021-10-28 13:56:45 +00:00
String path_for_partitioned_write;
/// These buffers are needed for schema inference when data source
/// is file descriptor. See getTableStructureFromFileDescriptor.
std::unique_ptr<ReadBuffer> read_buffer_from_fd;
std::unique_ptr<ReadBuffer> peekable_read_buffer_from_fd;
};
}