2018-03-13 23:44:23 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Macros.h>
|
|
|
|
#include <Common/Exception.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2023-04-09 01:04:26 +00:00
|
|
|
#include <Core/ServerUUID.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2018-03-13 23:44:23 +00:00
|
|
|
|
2014-08-11 15:59:01 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
2023-04-09 01:25:38 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int NO_ELEMENTS_IN_CONFIG;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:18:28 +00:00
|
|
|
Macros::Macros(const Poco::Util::AbstractConfiguration & config, const String & root_key, Poco::Logger * log)
|
2014-08-11 15:59:01 +00:00
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(root_key, keys);
|
|
|
|
for (const String & key : keys)
|
|
|
|
{
|
|
|
|
macros[key] = config.getString(root_key + "." + key);
|
2020-09-26 19:18:28 +00:00
|
|
|
if (key == "database" || key == "table" || key == "uuid")
|
|
|
|
{
|
2020-09-27 11:10:45 +00:00
|
|
|
if (log)
|
|
|
|
LOG_WARNING(log,
|
|
|
|
"Config file contains '{}' macro. This macro has special meaning "
|
|
|
|
"and it's explicit definition is not recommended. Implicit unfolding for "
|
|
|
|
"'database', 'table' and 'uuid' macros will be disabled.",
|
|
|
|
key);
|
2020-09-26 19:18:28 +00:00
|
|
|
enable_special_macros = false;
|
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 09:19:32 +00:00
|
|
|
String Macros::expand(const String & s,
|
2020-09-23 18:28:59 +00:00
|
|
|
MacroExpansionInfo & info) const
|
2014-08-11 15:59:01 +00:00
|
|
|
{
|
2020-09-26 19:18:28 +00:00
|
|
|
/// Do not allow recursion if we expand only special macros, because it will be infinite recursion
|
|
|
|
assert(info.level == 0 || !info.expand_special_macros_only);
|
|
|
|
|
2014-08-11 15:59:01 +00:00
|
|
|
if (s.find('{') == String::npos)
|
|
|
|
return s;
|
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
if (info.level && s.size() > 65536)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Too long string while expanding macros");
|
2014-10-15 19:38:46 +00:00
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
if (info.level >= 10)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Too deep recursion while expanding macros: '{}'", s);
|
2014-10-15 19:38:46 +00:00
|
|
|
|
2020-09-26 19:18:28 +00:00
|
|
|
/// If config file contains explicit special macro, then we do not expand it in this mode.
|
|
|
|
if (!enable_special_macros && info.expand_special_macros_only)
|
|
|
|
return s;
|
|
|
|
|
2014-08-11 15:59:01 +00:00
|
|
|
String res;
|
|
|
|
size_t pos = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t begin = s.find('{', pos);
|
|
|
|
|
|
|
|
if (begin == String::npos)
|
|
|
|
{
|
|
|
|
res.append(s, pos, String::npos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.append(s, pos, begin - pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
++begin;
|
|
|
|
size_t end = s.find('}', begin);
|
|
|
|
if (end == String::npos)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unbalanced {{ and }} in string with macros: '{}'", s);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
|
|
|
String macro_name = s.substr(begin, end - begin);
|
2018-10-01 09:01:50 +00:00
|
|
|
auto it = macros.find(macro_name);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2018-10-01 09:01:50 +00:00
|
|
|
/// Prefer explicit macros over implicit.
|
2020-09-26 19:18:28 +00:00
|
|
|
if (it != macros.end() && !info.expand_special_macros_only)
|
2021-03-16 20:01:20 +00:00
|
|
|
{
|
2018-10-01 09:01:50 +00:00
|
|
|
res += it->second;
|
2021-03-16 20:01:20 +00:00
|
|
|
info.expanded_other = true;
|
|
|
|
}
|
2020-09-26 19:18:28 +00:00
|
|
|
else if (macro_name == "database" && !info.table_id.database_name.empty())
|
|
|
|
{
|
|
|
|
res += info.table_id.database_name;
|
|
|
|
info.expanded_database = true;
|
|
|
|
}
|
|
|
|
else if (macro_name == "table" && !info.table_id.table_name.empty())
|
|
|
|
{
|
|
|
|
res += info.table_id.table_name;
|
|
|
|
info.expanded_table = true;
|
|
|
|
}
|
2022-04-13 14:51:59 +00:00
|
|
|
else if (macro_name == "uuid" && !info.expand_special_macros_only)
|
2020-09-10 14:50:10 +00:00
|
|
|
{
|
2020-09-26 19:18:28 +00:00
|
|
|
if (info.table_id.uuid == UUIDHelpers::Nil)
|
2023-04-09 01:25:38 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Macro 'uuid' and empty arguments of ReplicatedMergeTree "
|
2023-01-23 21:13:58 +00:00
|
|
|
"are supported only for ON CLUSTER queries with Atomic database engine");
|
2020-09-23 18:28:59 +00:00
|
|
|
/// For ON CLUSTER queries we don't want to require all macros definitions in initiator's config.
|
|
|
|
/// However, initiator must check that for cross-replication cluster zookeeper_path does not contain {uuid} macro.
|
|
|
|
/// It becomes impossible to check if {uuid} is contained inside some unknown macro.
|
|
|
|
if (info.level)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Macro 'uuid' should not be inside another macro");
|
2020-09-26 19:18:28 +00:00
|
|
|
res += toString(info.table_id.uuid);
|
2020-09-23 18:28:59 +00:00
|
|
|
info.expanded_uuid = true;
|
|
|
|
}
|
2023-04-09 01:04:26 +00:00
|
|
|
else if (macro_name == "server_uuid")
|
|
|
|
{
|
|
|
|
auto uuid = ServerUUID::get();
|
|
|
|
if (UUIDHelpers::Nil == uuid)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Macro {server_uuid} expanded to zero, which means the UUID is not initialized (most likely it's not a server application)");
|
|
|
|
res += toString(uuid);
|
|
|
|
info.expanded_other = true;
|
|
|
|
}
|
2021-11-23 09:41:54 +00:00
|
|
|
else if (info.shard && macro_name == "shard")
|
|
|
|
{
|
|
|
|
res += *info.shard;
|
2022-04-13 14:51:59 +00:00
|
|
|
info.expanded_other = true;
|
2021-11-23 09:41:54 +00:00
|
|
|
}
|
|
|
|
else if (info.replica && macro_name == "replica")
|
|
|
|
{
|
|
|
|
res += *info.replica;
|
2022-04-13 14:51:59 +00:00
|
|
|
info.expanded_other = true;
|
2021-11-23 09:41:54 +00:00
|
|
|
}
|
2020-09-26 19:18:28 +00:00
|
|
|
else if (info.ignore_unknown || info.expand_special_macros_only)
|
2020-09-23 18:28:59 +00:00
|
|
|
{
|
2020-09-26 19:18:28 +00:00
|
|
|
if (info.expand_special_macros_only)
|
|
|
|
res += '{';
|
2020-09-23 18:28:59 +00:00
|
|
|
res += macro_name;
|
2020-09-26 19:18:28 +00:00
|
|
|
if (info.expand_special_macros_only)
|
|
|
|
res += '}';
|
2020-09-23 18:28:59 +00:00
|
|
|
info.has_unknown = true;
|
2020-09-10 14:50:10 +00:00
|
|
|
}
|
2018-09-27 10:01:10 +00:00
|
|
|
else
|
2023-04-09 01:25:38 +00:00
|
|
|
throw Exception(ErrorCodes::NO_ELEMENTS_IN_CONFIG, "No macro '{}' in config while processing substitutions in "
|
2023-01-23 21:13:58 +00:00
|
|
|
"'{}' at '{}' or macro is not supported here", macro_name, s, toString(begin));
|
2014-08-11 15:59:01 +00:00
|
|
|
|
|
|
|
pos = end + 1;
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
++info.level;
|
2020-09-26 19:18:28 +00:00
|
|
|
if (info.expand_special_macros_only)
|
|
|
|
return res;
|
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
return expand(res, info);
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:14:57 +00:00
|
|
|
String Macros::getValue(const String & key) const
|
|
|
|
{
|
|
|
|
if (auto it = macros.find(key); it != macros.end())
|
|
|
|
return it->second;
|
2023-04-09 01:25:38 +00:00
|
|
|
throw Exception(ErrorCodes::NO_ELEMENTS_IN_CONFIG, "No macro {} in config", key);
|
2019-10-09 01:14:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
|
|
|
|
String Macros::expand(const String & s) const
|
|
|
|
{
|
|
|
|
MacroExpansionInfo info;
|
|
|
|
return expand(s, info);
|
|
|
|
}
|
|
|
|
|
2020-07-10 09:19:32 +00:00
|
|
|
String Macros::expand(const String & s, const StorageID & table_id, bool allow_uuid) const
|
2018-10-01 09:01:50 +00:00
|
|
|
{
|
2020-09-23 18:28:59 +00:00
|
|
|
MacroExpansionInfo info;
|
2020-09-26 19:18:28 +00:00
|
|
|
info.table_id = table_id;
|
|
|
|
if (!allow_uuid)
|
|
|
|
info.table_id.uuid = UUIDHelpers::Nil;
|
2020-09-23 18:28:59 +00:00
|
|
|
return expand(s, info);
|
2018-10-01 09:01:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 16:08:27 +00:00
|
|
|
Names Macros::expand(const Names & source_names, size_t level) const
|
2018-04-20 13:48:42 +00:00
|
|
|
{
|
2018-04-20 16:08:27 +00:00
|
|
|
Names result_names;
|
|
|
|
result_names.reserve(source_names.size());
|
2018-04-20 13:48:42 +00:00
|
|
|
|
2020-09-23 18:28:59 +00:00
|
|
|
MacroExpansionInfo info;
|
2018-04-20 16:08:27 +00:00
|
|
|
for (const String & name : source_names)
|
2020-09-23 18:28:59 +00:00
|
|
|
{
|
|
|
|
info.level = level;
|
|
|
|
result_names.push_back(expand(name, info));
|
|
|
|
}
|
2018-05-07 02:01:11 +00:00
|
|
|
|
2018-04-20 16:08:27 +00:00
|
|
|
return result_names;
|
2018-04-20 13:48:42 +00:00
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|