提交 ed77e40d 编写于 作者: C CurtizJ

fix implicit macros

上级 d4de7634
......@@ -56,19 +56,17 @@ String Macros::expand(const String & s, size_t level, const String & database_na
throw Exception("Unbalanced { and } in string with macros: '" + s + "'", ErrorCodes::SYNTAX_ERROR);
String macro_name = s.substr(begin, end - begin);
auto it = macros.find(macro_name);
if (macro_name == "database")
/// Prefer explicit macros over implicit.
if (it != macros.end())
res += it->second;
else if (macro_name == "database" && !database_name.empty())
res += database_name;
else if (macro_name == "table")
else if (macro_name == "table" && !table_name.empty())
res += table_name;
else
{
auto it = macros.find(macro_name);
if (it == macros.end())
throw Exception("No macro " + macro_name + " in config", ErrorCodes::SYNTAX_ERROR);
res += it->second;
}
throw Exception("No macro " + macro_name + " in config", ErrorCodes::SYNTAX_ERROR);
pos = end + 1;
}
......@@ -76,6 +74,11 @@ String Macros::expand(const String & s, size_t level, const String & database_na
return expand(res, level + 1, database_name, table_name);
}
String Macros::expand(const String & s, const String & database_name, const String & table_name) const
{
return expand(s, 0, database_name, table_name);
}
Names Macros::expand(const Names & source_names, size_t level) const
{
Names result_names;
......
......@@ -28,10 +28,14 @@ public:
Macros(const Poco::Util::AbstractConfiguration & config, const String & key);
/** Replace the substring of the form {macro_name} with the value for macro_name, obtained from the config file.
* If {database} and {table} macros aren`t defined explicitly, expand them as database_name and table_name respectively.
* level - the level of recursion.
*/
String expand(const String & s, size_t level = 0, const String & database_name = "", const String & table_name = "") const;
String expand(const String & s, const String & database_name, const String & table_name) const;
/** Apply expand for the list.
*/
Names expand(const Names & source_names, size_t level = 0) const;
......
......@@ -207,8 +207,8 @@ StorageReplicatedMergeTree::StorageReplicatedMergeTree(
: context(context_),
database_name(database_name_),
table_name(name_), full_path(path_ + escapeForFileName(table_name) + '/'),
zookeeper_path(context.getMacros()->expand(zookeeper_path_, 0, database_name, table_name)),
replica_name(context.getMacros()->expand(replica_name_)),
zookeeper_path(context.getMacros()->expand(zookeeper_path_, database_name, table_name)),
replica_name(context.getMacros()->expand(replica_name_, database_name, table_name)),
data(database_name, table_name,
full_path, columns_,
context_, primary_expr_ast_, secondary_sorting_expr_list_, date_column_name, partition_expr_ast_,
......
......@@ -317,11 +317,14 @@ def test_macro(started_cluster):
ddl_check_query(instance, "DROP TABLE IF EXISTS distr ON CLUSTER '{cluster}'")
ddl_check_query(instance, "DROP TABLE IF EXISTS tab ON CLUSTER '{cluster}'")
def test_implicit_macros(started_cluster):
# Temporarily disable random ZK packet drops, they might broke creation if ReplicatedMergeTree replicas
firewall_drops_rules = cluster.pm_random_drops.pop_rules()
instance = cluster.instances['ch2']
ddl_check_query(instance, "DROP DATABASE IF EXISTS test_db ON CLUSTER '{cluster}'")
ddl_check_query(instance, "DROP TABLE IF EXISTS test_db.test_macro ON CLUSTER '{cluster}'")
ddl_check_query(instance, "CREATE DATABASE IF NOT EXISTS test_db ON CLUSTER '{cluster}'")
ddl_check_query(instance, """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册