ClickHouse/src/Dictionaries/FileDictionarySource.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
3.2 KiB
C++
Raw Normal View History

#include "FileDictionarySource.h"
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/filesystemHelpers.h>
#include <IO/ReadBufferFromFile.h>
#include <Interpreters/Context.h>
#include <Processors/Formats/IInputFormat.h>
#include "DictionarySourceFactory.h"
#include "DictionaryStructure.h"
2019-12-15 06:34:43 +00:00
#include "registerDictionaries.h"
#include "DictionarySourceHelpers.h"
2016-12-08 02:49:04 +00:00
namespace DB
{
2019-02-10 16:55:12 +00:00
static const UInt64 max_block_size = 8192;
2016-12-08 02:49:04 +00:00
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int LOGICAL_ERROR;
extern const int PATH_ACCESS_DENIED;
}
2016-12-08 02:49:04 +00:00
FileDictionarySource::FileDictionarySource(
const std::string & filepath_, const std::string & format_,
2021-06-01 12:20:52 +00:00
Block & sample_block_, ContextPtr context_, bool created_from_ddl)
: filepath{filepath_}
, format{format_}
, sample_block{sample_block_}
, context(context_)
{
2021-08-27 10:46:59 +00:00
auto user_files_path = context->getUserFilesPath();
2021-10-17 08:42:36 +00:00
if (created_from_ddl && !fileOrSymlinkPathStartsWith(filepath, user_files_path))
2021-08-27 10:46:59 +00:00
throw Exception(ErrorCodes::PATH_ACCESS_DENIED, "File path {} is not inside {}", filepath, user_files_path);
}
2016-12-08 02:49:04 +00:00
FileDictionarySource::FileDictionarySource(const FileDictionarySource & other)
: filepath{other.filepath}
, format{other.format}
, sample_block{other.sample_block}
, context(Context::createCopy(other.context))
, last_modification{other.last_modification}
{
}
2016-12-08 02:49:04 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline FileDictionarySource::loadAll()
2016-12-08 02:49:04 +00:00
{
2024-01-23 17:04:50 +00:00
LOG_TRACE(getLogger("FileDictionary"), "loadAll {}", toString());
auto in_ptr = std::make_unique<ReadBufferFromFile>(filepath);
2021-10-11 16:11:50 +00:00
auto source = context->getInputFormat(format, *in_ptr, sample_block, max_block_size);
source->addBuffer(std::move(in_ptr));
2016-12-08 02:49:04 +00:00
last_modification = getLastModification();
2022-05-20 19:49:31 +00:00
return QueryPipeline(std::move(source));
2016-12-08 02:49:04 +00:00
}
std::string FileDictionarySource::toString() const
{
2020-08-27 18:51:19 +00:00
return fmt::format("File: {}, {}", filepath, format);
2016-12-08 02:49:04 +00:00
}
Poco::Timestamp FileDictionarySource::getLastModification() const
{
2021-05-28 18:17:16 +00:00
return FS::getModificationTimestamp(filepath);
2016-12-08 02:49:04 +00:00
}
2021-04-30 20:35:44 +00:00
void registerDictionarySourceFile(DictionarySourceFactory & factory)
{
auto create_table_source = [=](const DictionaryStructure & dict_struct,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
Block & sample_block,
2021-08-12 15:16:55 +00:00
ContextPtr global_context,
2020-08-15 03:10:57 +00:00
const std::string & /* default_database */,
bool created_from_ddl) -> DictionarySourcePtr
{
if (dict_struct.has_expressions)
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::LOGICAL_ERROR, "Dictionary source of type `file` does not support attribute expressions");
const auto filepath = config.getString(config_prefix + ".file.path");
const auto format = config.getString(config_prefix + ".file.format");
2021-08-12 15:16:55 +00:00
const auto context = copyContextAndApplySettingsFromDictionaryConfig(global_context, config, config_prefix);
2021-08-12 15:16:55 +00:00
return std::make_unique<FileDictionarySource>(filepath, format, sample_block, context, created_from_ddl);
};
factory.registerSource("file", create_table_source);
}
}