ClickHouse/src/Databases/DatabaseDictionary.cpp

134 lines
4.6 KiB
C++
Raw Normal View History

2017-06-22 15:44:19 +00:00
#include <Databases/DatabaseDictionary.h>
#include <Interpreters/Context.h>
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Dictionaries/DictionaryStructure.h>
#include <Storages/StorageDictionary.h>
#include <common/logger_useful.h>
#include <IO/WriteBufferFromString.h>
#include <IO/Operators.h>
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/parseQuery.h>
2019-02-10 17:40:52 +00:00
#include <Parsers/IAST.h>
2017-06-22 15:44:19 +00:00
namespace DB
{
namespace ErrorCodes
{
2018-03-23 19:56:24 +00:00
extern const int SYNTAX_ERROR;
extern const int CANNOT_GET_CREATE_DICTIONARY_QUERY;
}
namespace
{
StoragePtr createStorageDictionary(const String & database_name, const ExternalLoader::LoadResult & load_result)
{
try
{
if (!load_result.config)
return nullptr;
DictionaryStructure dictionary_structure = ExternalDictionariesLoader::getDictionaryStructure(*load_result.config);
return StorageDictionary::create(StorageID(database_name, load_result.name), load_result.name, dictionary_structure);
}
catch (Exception & e)
{
throw Exception(
fmt::format("Error while loading dictionary '{}.{}': {}",
database_name, load_result.name, e.displayText()),
e.code());
}
}
2017-06-22 15:44:19 +00:00
}
2020-04-01 22:41:29 +00:00
DatabaseDictionary::DatabaseDictionary(const String & name_, const Context & global_context_)
: IDatabase(name_)
2020-05-30 21:57:37 +00:00
, log(&Poco::Logger::get("DatabaseDictionary(" + database_name + ")"))
2020-04-01 22:41:29 +00:00
, global_context(global_context_.getGlobalContext())
2017-06-22 15:44:19 +00:00
{
}
2017-06-22 15:44:19 +00:00
2020-04-01 22:41:29 +00:00
Tables DatabaseDictionary::listTables(const FilterByNameFunction & filter_by_name)
{
Tables tables;
auto load_results = global_context.getExternalDictionariesLoader().getLoadResults(filter_by_name);
for (auto & load_result : load_results)
{
auto storage = createStorageDictionary(getDatabaseName(), load_result);
if (storage)
tables.emplace(storage->getStorageID().table_name, storage);
}
return tables;
2017-06-22 15:44:19 +00:00
}
2020-05-28 20:10:45 +00:00
bool DatabaseDictionary::isTableExist(const String & table_name, const Context &) const
2017-06-22 15:44:19 +00:00
{
return global_context.getExternalDictionariesLoader().getCurrentStatus(table_name) != ExternalLoader::Status::NOT_EXIST;
2017-06-22 15:44:19 +00:00
}
2020-05-28 20:10:45 +00:00
StoragePtr DatabaseDictionary::tryGetTable(const String & table_name, const Context &) const
2017-06-22 15:44:19 +00:00
{
auto load_result = global_context.getExternalDictionariesLoader().getLoadResult(table_name);
return createStorageDictionary(getDatabaseName(), load_result);
2017-06-22 15:44:19 +00:00
}
2020-05-28 20:10:45 +00:00
DatabaseTablesIteratorPtr DatabaseDictionary::getTablesIterator(const Context &, const FilterByNameFunction & filter_by_table_name)
2017-06-22 15:44:19 +00:00
{
2020-04-01 22:41:29 +00:00
return std::make_unique<DatabaseTablesSnapshotIterator>(listTables(filter_by_table_name));
2017-06-22 15:44:19 +00:00
}
bool DatabaseDictionary::empty() const
2017-06-22 15:44:19 +00:00
{
return !global_context.getExternalDictionariesLoader().hasObjects();
2017-06-22 15:44:19 +00:00
}
2020-05-28 20:10:45 +00:00
ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const String & table_name, const Context &, bool throw_on_error) const
2017-06-22 15:44:19 +00:00
{
String query;
{
WriteBufferFromString buffer(query);
auto load_result = global_context.getExternalDictionariesLoader().getLoadResult(table_name);
if (!load_result.config)
{
if (throw_on_error)
throw Exception{"Dictionary " + backQuote(table_name) + " doesn't exist", ErrorCodes::CANNOT_GET_CREATE_DICTIONARY_QUERY};
2019-11-01 12:47:55 +00:00
return {};
}
2018-03-23 19:56:24 +00:00
auto names_and_types = StorageDictionary::getNamesAndTypes(ExternalDictionariesLoader::getDictionaryStructure(*load_result.config));
2019-11-01 12:47:55 +00:00
buffer << "CREATE TABLE " << backQuoteIfNeed(database_name) << '.' << backQuoteIfNeed(table_name) << " (";
buffer << StorageDictionary::generateNamesAndTypesDescription(names_and_types);
buffer << ") Engine = Dictionary(" << backQuoteIfNeed(table_name) << ")";
}
auto settings = global_context.getSettingsRef();
ParserCreateQuery parser;
2018-03-23 19:56:24 +00:00
const char * pos = query.data();
std::string error_message;
auto ast = tryParseQuery(parser, pos, pos + query.size(), error_message,
/* hilite = */ false, "", /* allow_multi_statements = */ false, 0, settings.max_parser_depth);
2018-03-23 19:56:24 +00:00
if (!ast && throw_on_error)
throw Exception(error_message, ErrorCodes::SYNTAX_ERROR);
return ast;
}
ASTPtr DatabaseDictionary::getCreateDatabaseQuery() const
2017-06-22 15:44:19 +00:00
{
String query;
{
WriteBufferFromString buffer(query);
2019-11-01 12:47:55 +00:00
buffer << "CREATE DATABASE " << backQuoteIfNeed(database_name) << " ENGINE = Dictionary";
}
auto settings = global_context.getSettingsRef();
ParserCreateQuery parser;
return parseQuery(parser, query.data(), query.data() + query.size(), "", 0, settings.max_parser_depth);
2017-06-22 15:44:19 +00:00
}
void DatabaseDictionary::shutdown()
{
}
}