ClickHouse/src/Databases/DatabaseDictionary.cpp

138 lines
5.1 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 <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;
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_)
, log(&Logger::get("DatabaseDictionary(" + database_name + ")"))
, 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;
2019-10-17 13:05:12 +00:00
ExternalLoader::LoadResults load_results;
if (filter_by_name)
{
/// If `filter_by_name` is set, we iterate through all dictionaries with such names. That's why we need to load all of them.
2020-04-01 22:41:29 +00:00
load_results = global_context.getExternalDictionariesLoader().tryLoad<ExternalLoader::LoadResults>(filter_by_name);
}
else
2017-06-22 15:44:19 +00:00
{
/// If `filter_by_name` isn't set, we iterate through only already loaded dictionaries. We don't try to load all dictionaries in this case.
2020-04-01 22:41:29 +00:00
load_results = global_context.getExternalDictionariesLoader().getCurrentLoadResults();
2017-06-22 15:44:19 +00:00
}
for (const auto & load_result: load_results)
{
2019-10-17 13:05:12 +00:00
/// Load tables only from XML dictionaries, don't touch other
if (load_result.object && load_result.repository_name.empty())
2019-10-17 13:05:12 +00:00
{
auto dict_ptr = std::static_pointer_cast<const IDictionaryBase>(load_result.object);
2019-10-17 13:05:12 +00:00
auto dict_name = dict_ptr->getName();
const DictionaryStructure & dictionary_structure = dict_ptr->getStructure();
auto columns = StorageDictionary::getNamesAndTypes(dictionary_structure);
2020-04-01 22:41:29 +00:00
tables[dict_name] = StorageDictionary::create(StorageID(getDatabaseName(), dict_name), ColumnsDescription{columns}, global_context, true, dict_name);
2019-10-17 13:05:12 +00:00
}
}
return tables;
2017-06-22 15:44:19 +00:00
}
bool DatabaseDictionary::isTableExist(
const Context & context,
const String & table_name) const
2017-06-22 15:44:19 +00:00
{
return context.getExternalDictionariesLoader().getCurrentStatus(table_name) != ExternalLoader::Status::NOT_EXIST;
2017-06-22 15:44:19 +00:00
}
StoragePtr DatabaseDictionary::tryGetTable(
const Context & context,
const String & table_name) const
2017-06-22 15:44:19 +00:00
{
auto dict_ptr = context.getExternalDictionariesLoader().tryGetDictionary(table_name, true /*load*/);
if (dict_ptr)
{
const DictionaryStructure & dictionary_structure = dict_ptr->getStructure();
auto columns = StorageDictionary::getNamesAndTypes(dictionary_structure);
2019-12-04 16:06:55 +00:00
return StorageDictionary::create(StorageID(getDatabaseName(), table_name), ColumnsDescription{columns}, context, true, table_name);
}
return {};
2017-06-22 15:44:19 +00:00
}
2020-04-01 22:41:29 +00:00
DatabaseTablesIteratorPtr DatabaseDictionary::getTablesIterator(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 Context & context) const
2017-06-22 15:44:19 +00:00
{
return !context.getExternalDictionariesLoader().hasCurrentlyLoadedObjects();
2017-06-22 15:44:19 +00:00
}
2018-03-23 19:56:24 +00:00
ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const Context & context,
const String & table_name, bool throw_on_error) const
2017-06-22 15:44:19 +00:00
{
String query;
{
WriteBufferFromString buffer(query);
const auto & dictionaries = context.getExternalDictionariesLoader();
2018-03-23 19:56:24 +00:00
auto dictionary = throw_on_error ? dictionaries.getDictionary(table_name)
: dictionaries.tryGetDictionary(table_name, true /*load*/);
2019-11-01 12:47:55 +00:00
if (!dictionary)
return {};
2018-03-23 19:56:24 +00:00
auto names_and_types = StorageDictionary::getNamesAndTypes(dictionary->getStructure());
2019-11-01 12:47:55 +00:00
buffer << "CREATE TABLE " << backQuoteIfNeed(database_name) << '.' << backQuoteIfNeed(table_name) << " (";
buffer << StorageDictionary::generateNamesAndTypesDescription(names_and_types.begin(), names_and_types.end());
buffer << ") Engine = Dictionary(" << backQuoteIfNeed(table_name) << ")";
}
auto settings = 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 Context & context) 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 = 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()
{
}
}