ClickHouse/src/Storages/ExternalDataSourceConfiguration.cpp

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

289 lines
13 KiB
C++
Raw Normal View History

2021-09-01 17:59:11 +00:00
#include "ExternalDataSourceConfiguration.h"
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
2021-09-01 17:59:11 +00:00
#include <Poco/Util/AbstractConfiguration.h>
2021-09-01 23:17:15 +00:00
#include <IO/WriteBufferFromString.h>
2021-09-01 17:59:11 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
2021-12-27 14:41:37 +00:00
IMPLEMENT_SETTINGS_TRAITS(EmptySettingsTraits, EMPTY_SETTINGS)
static const std::unordered_set<std::string_view> dictionary_allowed_keys = {
2022-08-03 19:44:08 +00:00
"host", "port", "user", "password", "quota_key", "db",
"database", "table", "schema", "replica",
"update_field", "update_lag", "invalidate_query", "query",
"where", "name", "secure", "uri", "collection"};
2022-01-10 11:00:03 +00:00
template<typename T>
SettingsChanges getSettingsChangesFromConfig(
const BaseSettings<T> & settings, const Poco::Util::AbstractConfiguration & config, const String & config_prefix)
{
SettingsChanges config_settings;
for (const auto & setting : settings.all())
{
const auto & setting_name = setting.getName();
auto setting_value = config.getString(config_prefix + '.' + setting_name, "");
if (!setting_value.empty())
config_settings.emplace_back(setting_name, setting_value);
}
return config_settings;
}
2021-09-01 23:17:15 +00:00
String ExternalDataSourceConfiguration::toString() const
{
WriteBufferFromOwnString configuration_info;
2021-09-07 11:17:25 +00:00
configuration_info << "username: " << username << "\t";
if (addresses.empty())
{
configuration_info << "host: " << host << "\t";
configuration_info << "port: " << port << "\t";
}
else
{
for (const auto & [replica_host, replica_port] : addresses)
{
configuration_info << "host: " << replica_host << "\t";
configuration_info << "port: " << replica_port << "\t";
}
}
2021-09-01 23:17:15 +00:00
return configuration_info.str();
}
2021-09-15 22:45:43 +00:00
void ExternalDataSourceConfiguration::set(const ExternalDataSourceConfiguration & conf)
{
host = conf.host;
port = conf.port;
username = conf.username;
2021-09-22 15:10:25 +00:00
password = conf.password;
2022-08-03 19:44:08 +00:00
quota_key = conf.quota_key;
2021-09-15 22:45:43 +00:00
database = conf.database;
table = conf.table;
schema = conf.schema;
2021-12-13 22:06:46 +00:00
addresses = conf.addresses;
addresses_expr = conf.addresses_expr;
2021-09-15 22:45:43 +00:00
}
static void validateConfigKeys(
2021-12-29 10:02:18 +00:00
const Poco::Util::AbstractConfiguration & dict_config, const String & config_prefix, HasConfigKeyFunc has_config_key_func)
{
Poco::Util::AbstractConfiguration::Keys config_keys;
dict_config.keys(config_prefix, config_keys);
for (const auto & config_key : config_keys)
{
2021-12-29 10:02:18 +00:00
if (!has_config_key_func(config_key))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unexpected key `{}` in dictionary source configuration", config_key);
}
}
2021-09-01 23:17:15 +00:00
2022-01-10 11:00:03 +00:00
template <typename T>
std::optional<ExternalDataSourceInfo> getExternalDataSourceConfiguration(
2021-12-29 10:02:18 +00:00
const Poco::Util::AbstractConfiguration & dict_config, const String & dict_config_prefix,
2022-01-10 11:00:03 +00:00
ContextPtr context, HasConfigKeyFunc has_config_key, const BaseSettings<T> & settings)
2021-09-01 23:17:15 +00:00
{
2021-12-29 10:02:18 +00:00
validateConfigKeys(dict_config, dict_config_prefix, has_config_key);
2021-09-01 23:17:15 +00:00
ExternalDataSourceConfiguration configuration;
auto collection_name = dict_config.getString(dict_config_prefix + ".name", "");
if (!collection_name.empty())
{
const auto & config = context->getConfigRef();
const auto & collection_prefix = fmt::format("named_collections.{}", collection_name);
2021-12-29 10:02:18 +00:00
validateConfigKeys(dict_config, collection_prefix, has_config_key);
2022-01-10 11:00:03 +00:00
auto config_settings = getSettingsChangesFromConfig(settings, config, collection_prefix);
auto dict_settings = getSettingsChangesFromConfig(settings, dict_config, dict_config_prefix);
/// dictionary config settings override collection settings.
config_settings.insert(config_settings.end(), dict_settings.begin(), dict_settings.end());
2021-09-01 23:17:15 +00:00
if (!config.has(collection_prefix))
2021-09-01 23:17:15 +00:00
throw Exception(ErrorCodes::BAD_ARGUMENTS, "There is no collection named `{}` in config", collection_name);
configuration.host = dict_config.getString(dict_config_prefix + ".host", config.getString(collection_prefix + ".host", ""));
configuration.port = dict_config.getInt(dict_config_prefix + ".port", config.getUInt(collection_prefix + ".port", 0));
configuration.username = dict_config.getString(dict_config_prefix + ".user", config.getString(collection_prefix + ".user", ""));
configuration.password = dict_config.getString(dict_config_prefix + ".password", config.getString(collection_prefix + ".password", ""));
2022-08-03 19:44:08 +00:00
configuration.quota_key = dict_config.getString(dict_config_prefix + ".quota_key", config.getString(collection_prefix + ".quota_key", ""));
configuration.database = dict_config.getString(dict_config_prefix + ".db", config.getString(dict_config_prefix + ".database",
config.getString(collection_prefix + ".db", config.getString(collection_prefix + ".database", ""))));
configuration.table = dict_config.getString(dict_config_prefix + ".table", config.getString(collection_prefix + ".table", ""));
configuration.schema = dict_config.getString(dict_config_prefix + ".schema", config.getString(collection_prefix + ".schema", ""));
2021-09-01 23:17:15 +00:00
if (configuration.host.empty() || configuration.port == 0 || configuration.username.empty() || configuration.table.empty())
2021-09-01 23:17:15 +00:00
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Named collection of connection parameters is missing some "
"of the parameters and dictionary parameters are not added");
2021-09-01 23:17:15 +00:00
}
return ExternalDataSourceInfo{.configuration = configuration, .settings_changes = config_settings};
2021-09-01 23:17:15 +00:00
}
2021-09-25 14:46:03 +00:00
return std::nullopt;
2021-09-01 23:17:15 +00:00
}
std::optional<URLBasedDataSourceConfig> getURLBasedDataSourceConfiguration(
const Poco::Util::AbstractConfiguration & dict_config, const String & dict_config_prefix, ContextPtr context)
{
URLBasedDataSourceConfiguration configuration;
auto collection_name = dict_config.getString(dict_config_prefix + ".name", "");
if (!collection_name.empty())
{
const auto & config = context->getConfigRef();
const auto & collection_prefix = fmt::format("named_collections.{}", collection_name);
if (!config.has(collection_prefix))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "There is no collection named `{}` in config", collection_name);
configuration.url =
dict_config.getString(dict_config_prefix + ".url", config.getString(collection_prefix + ".url", ""));
2022-05-27 17:43:34 +00:00
configuration.endpoint =
dict_config.getString(dict_config_prefix + ".endpoint", config.getString(collection_prefix + ".endpoint", ""));
configuration.format =
dict_config.getString(dict_config_prefix + ".format", config.getString(collection_prefix + ".format", ""));
configuration.compression_method =
dict_config.getString(dict_config_prefix + ".compression", config.getString(collection_prefix + ".compression_method", ""));
configuration.structure =
dict_config.getString(dict_config_prefix + ".structure", config.getString(collection_prefix + ".structure", ""));
configuration.user =
dict_config.getString(dict_config_prefix + ".credentials.user", config.getString(collection_prefix + ".credentials.user", ""));
configuration.password =
dict_config.getString(dict_config_prefix + ".credentials.password", config.getString(collection_prefix + ".credentials.password", ""));
String headers_prefix;
const Poco::Util::AbstractConfiguration *headers_config = nullptr;
if (dict_config.has(dict_config_prefix + ".headers"))
{
headers_prefix = dict_config_prefix + ".headers";
headers_config = &dict_config;
}
else
{
headers_prefix = collection_prefix + ".headers";
headers_config = &config;
}
if (headers_config)
{
Poco::Util::AbstractConfiguration::Keys header_keys;
headers_config->keys(headers_prefix, header_keys);
headers_prefix += ".";
for (const auto & header : header_keys)
{
const auto header_prefix = headers_prefix + header;
configuration.headers.emplace_back(
2022-09-13 13:07:43 +00:00
headers_config->getString(header_prefix + ".name"),
headers_config->getString(header_prefix + ".value"));
}
}
return URLBasedDataSourceConfig{ .configuration = configuration };
}
return std::nullopt;
}
2021-09-01 23:17:15 +00:00
2021-09-03 11:16:32 +00:00
ExternalDataSourcesByPriority getExternalDataSourceConfigurationByPriority(
2021-12-29 10:02:18 +00:00
const Poco::Util::AbstractConfiguration & dict_config, const String & dict_config_prefix, ContextPtr context, HasConfigKeyFunc has_config_key)
2021-09-01 23:17:15 +00:00
{
2021-12-29 10:02:18 +00:00
validateConfigKeys(dict_config, dict_config_prefix, has_config_key);
2021-09-25 14:46:03 +00:00
ExternalDataSourceConfiguration common_configuration;
2021-12-29 10:02:18 +00:00
auto named_collection = getExternalDataSourceConfiguration(dict_config, dict_config_prefix, context, has_config_key);
2021-09-25 14:46:03 +00:00
if (named_collection)
{
2022-01-10 11:00:03 +00:00
common_configuration = named_collection->configuration;
2021-09-25 14:46:03 +00:00
}
else
{
common_configuration.host = dict_config.getString(dict_config_prefix + ".host", "");
common_configuration.port = dict_config.getUInt(dict_config_prefix + ".port", 0);
common_configuration.username = dict_config.getString(dict_config_prefix + ".user", "");
common_configuration.password = dict_config.getString(dict_config_prefix + ".password", "");
2022-08-03 19:44:08 +00:00
common_configuration.quota_key = dict_config.getString(dict_config_prefix + ".quota_key", "");
common_configuration.database = dict_config.getString(dict_config_prefix + ".db", dict_config.getString(dict_config_prefix + ".database", ""));
2021-09-25 14:46:03 +00:00
common_configuration.table = dict_config.getString(fmt::format("{}.table", dict_config_prefix), "");
common_configuration.schema = dict_config.getString(fmt::format("{}.schema", dict_config_prefix), "");
}
2021-09-02 13:01:26 +00:00
ExternalDataSourcesByPriority configuration
{
.database = common_configuration.database,
.table = common_configuration.table,
2021-09-10 11:11:52 +00:00
.schema = common_configuration.schema,
.replicas_configurations = {}
2021-09-02 13:01:26 +00:00
};
2021-09-01 23:17:15 +00:00
if (dict_config.has(dict_config_prefix + ".replica"))
{
Poco::Util::AbstractConfiguration::Keys config_keys;
dict_config.keys(dict_config_prefix, config_keys);
for (const auto & config_key : config_keys)
{
if (config_key.starts_with("replica"))
{
ExternalDataSourceConfiguration replica_configuration(common_configuration);
String replica_name = dict_config_prefix + "." + config_key;
2021-12-29 10:02:18 +00:00
validateConfigKeys(dict_config, replica_name, has_config_key);
2021-09-01 23:17:15 +00:00
size_t priority = dict_config.getInt(replica_name + ".priority", 0);
2021-09-01 23:17:15 +00:00
replica_configuration.host = dict_config.getString(replica_name + ".host", common_configuration.host);
replica_configuration.port = dict_config.getUInt(replica_name + ".port", common_configuration.port);
replica_configuration.username = dict_config.getString(replica_name + ".user", common_configuration.username);
replica_configuration.password = dict_config.getString(replica_name + ".password", common_configuration.password);
2022-08-03 19:44:08 +00:00
replica_configuration.quota_key = dict_config.getString(replica_name + ".quota_key", common_configuration.quota_key);
2021-09-01 23:17:15 +00:00
if (replica_configuration.host.empty() || replica_configuration.port == 0
|| replica_configuration.username.empty() || replica_configuration.password.empty())
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Named collection of connection parameters is missing some "
"of the parameters and no other dictionary parameters are added");
2021-09-01 23:17:15 +00:00
}
2021-09-02 13:01:26 +00:00
configuration.replicas_configurations[priority].emplace_back(replica_configuration);
2021-09-01 23:17:15 +00:00
}
}
}
else
{
2021-09-02 13:01:26 +00:00
configuration.replicas_configurations[0].emplace_back(common_configuration);
2021-09-01 23:17:15 +00:00
}
2021-09-02 13:01:26 +00:00
return configuration;
2021-09-01 23:17:15 +00:00
}
2021-09-07 11:17:25 +00:00
2021-09-15 22:45:43 +00:00
void URLBasedDataSourceConfiguration::set(const URLBasedDataSourceConfiguration & conf)
{
url = conf.url;
format = conf.format;
compression_method = conf.compression_method;
structure = conf.structure;
2021-10-28 12:44:12 +00:00
http_method = conf.http_method;
2022-03-24 18:09:13 +00:00
headers = conf.headers;
2021-09-15 22:45:43 +00:00
}
2022-01-10 11:00:03 +00:00
template
std::optional<ExternalDataSourceInfo> getExternalDataSourceConfiguration(
const Poco::Util::AbstractConfiguration & dict_config, const String & dict_config_prefix,
ContextPtr context, HasConfigKeyFunc has_config_key, const BaseSettings<EmptySettingsTraits> & settings);
template
SettingsChanges getSettingsChangesFromConfig(
const BaseSettings<EmptySettingsTraits> & settings, const Poco::Util::AbstractConfiguration & config, const String & config_prefix);
2021-09-01 17:59:11 +00:00
}