2017-06-22 15:44:19 +00:00
|
|
|
#include <Databases/DatabaseDictionary.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2019-09-26 10:41:33 +00:00
|
|
|
#include <Interpreters/ExternalDictionariesLoader.h>
|
2017-06-23 15:55:45 +00:00
|
|
|
#include <Storages/StorageDictionary.h>
|
|
|
|
#include <common/logger_useful.h>
|
2018-03-13 13:28:32 +00:00
|
|
|
#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-23 15:55:45 +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)
|
2017-06-23 15:55:45 +00:00
|
|
|
{
|
|
|
|
Tables tables;
|
2019-10-17 13:05:12 +00:00
|
|
|
ExternalLoader::LoadResults load_results;
|
2019-06-02 12:11:01 +00:00
|
|
|
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);
|
2019-06-02 12:11:01 +00:00
|
|
|
}
|
|
|
|
else
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2019-06-02 12:11:01 +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
|
|
|
}
|
|
|
|
|
2019-12-12 18:33:43 +00:00
|
|
|
for (const auto & load_result: load_results)
|
2019-06-02 12:11:01 +00:00
|
|
|
{
|
2019-10-17 13:05:12 +00:00
|
|
|
/// Load tables only from XML dictionaries, don't touch other
|
2019-12-12 18:33:43 +00:00
|
|
|
if (load_result.object && load_result.repository_name.empty())
|
2019-10-17 13:05:12 +00:00
|
|
|
{
|
2019-12-12 18:33:43 +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
|
|
|
}
|
2019-06-02 12:11:01 +00:00
|
|
|
}
|
2017-06-23 15:55:45 +00:00
|
|
|
return tables;
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
bool DatabaseDictionary::isTableExist(
|
2019-02-04 19:45:22 +00:00
|
|
|
const Context & context,
|
2017-09-11 12:39:01 +00:00
|
|
|
const String & table_name) const
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2019-09-26 10:41:33 +00:00
|
|
|
return context.getExternalDictionariesLoader().getCurrentStatus(table_name) != ExternalLoader::Status::NOT_EXIST;
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
StoragePtr DatabaseDictionary::tryGetTable(
|
2019-02-04 19:45:22 +00:00
|
|
|
const Context & context,
|
2018-01-30 17:47:04 +00:00
|
|
|
const String & table_name) const
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2020-04-09 22:32:59 +00:00
|
|
|
auto dict_ptr = context.getExternalDictionariesLoader().tryGetDictionary(table_name, true /*load*/);
|
2019-04-10 18:36:52 +00:00
|
|
|
if (dict_ptr)
|
2017-06-23 15:55:45 +00:00
|
|
|
{
|
2019-04-10 18:36:52 +00:00
|
|
|
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);
|
2017-06-23 15:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-04 19:45:22 +00:00
|
|
|
bool DatabaseDictionary::empty(const Context & context) const
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2019-09-26 10:41:33 +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
|
|
|
{
|
2018-03-13 13:28:32 +00:00
|
|
|
String query;
|
|
|
|
{
|
|
|
|
WriteBufferFromString buffer(query);
|
|
|
|
|
2019-09-26 10:41:33 +00:00
|
|
|
const auto & dictionaries = context.getExternalDictionariesLoader();
|
2018-03-23 19:56:24 +00:00
|
|
|
auto dictionary = throw_on_error ? dictionaries.getDictionary(table_name)
|
2020-04-09 22:32:59 +00:00
|
|
|
: 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
|
|
|
|
2018-03-13 13:28:32 +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) << " (";
|
2018-03-13 13:28:32 +00:00
|
|
|
buffer << StorageDictionary::generateNamesAndTypesDescription(names_and_types.begin(), names_and_types.end());
|
|
|
|
buffer << ") Engine = Dictionary(" << backQuoteIfNeed(table_name) << ")";
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:11:01 +00:00
|
|
|
auto settings = context.getSettingsRef();
|
2018-03-13 13:28:32 +00:00
|
|
|
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,
|
2020-01-14 11:11:01 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:11:01 +00:00
|
|
|
ASTPtr DatabaseDictionary::getCreateDatabaseQuery(const Context & context) const
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2018-03-13 13:28:32 +00:00
|
|
|
String query;
|
|
|
|
{
|
|
|
|
WriteBufferFromString buffer(query);
|
2019-11-01 12:47:55 +00:00
|
|
|
buffer << "CREATE DATABASE " << backQuoteIfNeed(database_name) << " ENGINE = Dictionary";
|
2018-03-13 13:28:32 +00:00
|
|
|
}
|
2020-01-14 11:11:01 +00:00
|
|
|
auto settings = context.getSettingsRef();
|
2018-03-13 13:28:32 +00:00
|
|
|
ParserCreateQuery parser;
|
2020-01-14 11:11:01 +00:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|