Create dictionary with comment

This commit is contained in:
Vasily Nemkov 2021-10-08 19:56:25 +03:00
parent 199eda4ee3
commit 2c55807b2a
6 changed files with 43 additions and 3 deletions

View File

@ -55,7 +55,11 @@ DictionaryPtr DictionaryFactory::create(
if (found != registered_layouts.end()) if (found != registered_layouts.end())
{ {
const auto & layout_creator = found->second.layout_create_function; const auto & layout_creator = found->second.layout_create_function;
return layout_creator(name, dict_struct, config, config_prefix, std::move(source_ptr), global_context, created_from_ddl); auto result = layout_creator(name, dict_struct, config, config_prefix, std::move(source_ptr), global_context, created_from_ddl);
if (config.hasProperty(config_prefix + ".comment"))
result->setDictionaryComment(config.getString(config_prefix + ".comment"));
return result;
} }
} }

View File

@ -216,12 +216,25 @@ struct IDictionary : public IExternalLoadable
return std::static_pointer_cast<const IDictionary>(IExternalLoadable::shared_from_this()); return std::static_pointer_cast<const IDictionary>(IExternalLoadable::shared_from_this());
} }
void setDictionaryComment(String new_comment)
{
std::lock_guard lock{name_mutex};
dictionary_comment = std::move(new_comment);
}
String getDictionaryComment() const
{
std::lock_guard lock{name_mutex};
return dictionary_comment;
}
private: private:
mutable std::mutex name_mutex; mutable std::mutex name_mutex;
mutable StorageID dictionary_id; mutable StorageID dictionary_id;
protected: protected:
const String full_name; const String full_name;
String dictionary_comment;
}; };
} }

View File

@ -573,6 +573,15 @@ getDictionaryConfigurationFromAST(const ASTCreateQuery & query, ContextPtr conte
if (query.dictionary->range) if (query.dictionary->range)
buildRangeConfiguration(xml_document, structure_element, query.dictionary->range, all_attr_names_and_types); buildRangeConfiguration(xml_document, structure_element, query.dictionary->range, all_attr_names_and_types);
if (query.comment)
{
AutoPtr<Element> comment_element(xml_document->createElement("comment"));
current_dictionary->appendChild(comment_element);
AutoPtr<Text> comment_value(xml_document->createTextNode(query.comment->as<ASTLiteral>()->value.safeGet<String>()));
comment_element->appendChild(comment_value);
}
conf->load(xml_document); conf->load(xml_document);
return conf; return conf;
} }

View File

@ -47,7 +47,8 @@ NamesAndTypesList StorageSystemDictionaries::getNamesAndTypes()
{"last_successful_update_time", std::make_shared<DataTypeDateTime>()}, {"last_successful_update_time", std::make_shared<DataTypeDateTime>()},
{"loading_duration", std::make_shared<DataTypeFloat32>()}, {"loading_duration", std::make_shared<DataTypeFloat32>()},
//{ "creation_time", std::make_shared<DataTypeDateTime>() }, //{ "creation_time", std::make_shared<DataTypeDateTime>() },
{"last_exception", std::make_shared<DataTypeString>()} {"last_exception", std::make_shared<DataTypeString>()},
{"comment", std::make_shared<DataTypeString>()}
}; };
} }
@ -140,6 +141,16 @@ void StorageSystemDictionaries::fillData(MutableColumns & res_columns, ContextPt
else else
res_columns[i++]->insertDefault(); res_columns[i++]->insertDefault();
if (dict_ptr)
res_columns[i++]->insert(dict_ptr->getDictionaryComment());
else
{
if (load_result.config && load_result.config->config->has("dictionary.comment"))
res_columns[i++]->insert(load_result.config->config->getString("dictionary.comment"));
else
res_columns[i++]->insertDefault();
}
/// Start fill virtual columns /// Start fill virtual columns
res_columns[i++]->insert(dictionary_structure.getKeyDescription()); res_columns[i++]->insert(dictionary_structure.getKeyDescription());

View File

@ -0,0 +1,2 @@
CREATE DICTIONARY default.`2024_dictionary_with_comment`\n(\n `id` UInt64,\n `value` String\n)\nPRIMARY KEY id\nSOURCE(CLICKHOUSE(HOST \'localhost\' PORT tcpPort() TABLE \'source_table\'))\nLIFETIME(MIN 0 MAX 1000)\nLAYOUT(FLAT())\nCOMMENT \'Test dictionary with comment\'
Test dictionary with comment

View File

@ -28,5 +28,6 @@ LIFETIME(MIN 0 MAX 1000)
COMMENT 'Test dictionary with comment'; COMMENT 'Test dictionary with comment';
SHOW CREATE DICTIONARY 2024_dictionary_with_comment; SHOW CREATE DICTIONARY 2024_dictionary_with_comment;
SELECT comment FROM system.dictionaries WHERE name == '2024_dictionary_with_comment' AND database == currentDatabase();
SELECT comment FROM system.dictionaries WHERE name == '2024_dictionary_with_comment'; DROP DICTIONARY IF EXISTS 2024_dictionary_with_comment;