2017-06-22 15:44:19 +00:00
|
|
|
#include <Databases/DatabaseDictionary.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ExternalDictionaries.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 TABLE_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_TABLE;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
|
|
|
|
extern const int SYNTAX_ERROR;
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 19:45:22 +00:00
|
|
|
DatabaseDictionary::DatabaseDictionary(const String & name_)
|
2017-06-23 15:55:45 +00:00
|
|
|
: name(name_),
|
|
|
|
log(&Logger::get("DatabaseDictionary(" + name + ")"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-01 20:21:35 +00:00
|
|
|
void DatabaseDictionary::loadTables(Context &, ThreadPool *, bool)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2017-06-23 15:55:45 +00:00
|
|
|
}
|
2017-06-22 15:44:19 +00:00
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
Tables DatabaseDictionary::listTables(const Context & context, const FilterByNameFunction & filter_by_name)
|
2017-06-23 15:55:45 +00:00
|
|
|
{
|
|
|
|
Tables tables;
|
2019-06-02 12:11:01 +00:00
|
|
|
ExternalLoader::Loadables loadables;
|
|
|
|
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.
|
|
|
|
loadables = context.getExternalDictionaries().loadAndGet(filter_by_name);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
loadables = context.getExternalDictionaries().getCurrentlyLoadedObjects();
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
for (const auto & loadable : loadables)
|
|
|
|
{
|
|
|
|
auto dict_ptr = std::static_pointer_cast<const IDictionaryBase>(loadable);
|
|
|
|
auto dict_name = dict_ptr->getName();
|
|
|
|
const DictionaryStructure & dictionary_structure = dict_ptr->getStructure();
|
|
|
|
auto columns = StorageDictionary::getNamesAndTypes(dictionary_structure);
|
|
|
|
tables[dict_name] = StorageDictionary::create(dict_name, ColumnsDescription{columns}, context, true, dict_name);
|
|
|
|
}
|
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-06-02 12:11:01 +00:00
|
|
|
return context.getExternalDictionaries().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
|
|
|
{
|
2019-04-10 18:36:52 +00:00
|
|
|
auto dict_ptr = context.getExternalDictionaries().tryGetDictionary(table_name);
|
|
|
|
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);
|
|
|
|
return StorageDictionary::create(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
|
|
|
}
|
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
DatabaseIteratorPtr DatabaseDictionary::getIterator(const Context & context, const FilterByNameFunction & filter_by_name)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2019-06-02 12:11:01 +00:00
|
|
|
return std::make_unique<DatabaseSnapshotIterator>(listTables(context, filter_by_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-06-29 17:27:32 +00:00
|
|
|
return !context.getExternalDictionaries().hasCurrentlyLoadedObjects();
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 20:21:35 +00:00
|
|
|
StoragePtr DatabaseDictionary::detachTable(const String & /*table_name*/)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
|
|
|
throw Exception("DatabaseDictionary: detachTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2017-12-01 20:21:35 +00:00
|
|
|
void DatabaseDictionary::attachTable(const String & /*table_name*/, const StoragePtr & /*table*/)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
|
|
|
throw Exception("DatabaseDictionary: attachTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
void DatabaseDictionary::createTable(
|
2019-02-04 19:45:22 +00:00
|
|
|
const Context &,
|
|
|
|
const String &,
|
|
|
|
const StoragePtr &,
|
|
|
|
const ASTPtr &)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2017-06-23 15:55:45 +00:00
|
|
|
throw Exception("DatabaseDictionary: createTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
void DatabaseDictionary::removeTable(
|
2019-02-04 19:45:22 +00:00
|
|
|
const Context &,
|
|
|
|
const String &)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2019-02-04 19:45:22 +00:00
|
|
|
throw Exception("DatabaseDictionary: removeTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
void DatabaseDictionary::renameTable(
|
2017-12-01 20:21:35 +00:00
|
|
|
const Context &,
|
|
|
|
const String &,
|
|
|
|
IDatabase &,
|
|
|
|
const String &)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
|
|
|
throw Exception("DatabaseDictionary: renameTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
void DatabaseDictionary::alterTable(
|
2017-12-01 20:21:35 +00:00
|
|
|
const Context &,
|
|
|
|
const String &,
|
2018-03-06 20:18:34 +00:00
|
|
|
const ColumnsDescription &,
|
2019-02-05 14:50:25 +00:00
|
|
|
const IndicesDescription &,
|
2017-12-01 20:21:35 +00:00
|
|
|
const ASTModifier &)
|
2017-09-11 12:39:01 +00:00
|
|
|
{
|
|
|
|
throw Exception("DatabaseDictionary: alterTable() is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t DatabaseDictionary::getTableMetadataModificationTime(
|
2017-12-01 20:21:35 +00:00
|
|
|
const Context &,
|
|
|
|
const String &)
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
|
|
|
return static_cast<time_t>(0);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2018-03-23 19:56:24 +00:00
|
|
|
const auto & dictionaries = context.getExternalDictionaries();
|
|
|
|
auto dictionary = throw_on_error ? dictionaries.getDictionary(table_name)
|
|
|
|
: dictionaries.tryGetDictionary(table_name);
|
|
|
|
|
2018-03-13 13:28:32 +00:00
|
|
|
auto names_and_types = StorageDictionary::getNamesAndTypes(dictionary->getStructure());
|
|
|
|
buffer << "CREATE TABLE " << backQuoteIfNeed(name) << '.' << backQuoteIfNeed(table_name) << " (";
|
|
|
|
buffer << StorageDictionary::generateNamesAndTypesDescription(names_and_types.begin(), names_and_types.end());
|
|
|
|
buffer << ") Engine = Dictionary(" << backQuoteIfNeed(table_name) << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2018-04-16 15:39:12 +00:00
|
|
|
/* hilite = */ false, "", /* allow_multi_statements = */ false, 0);
|
2018-03-23 19:56:24 +00:00
|
|
|
|
|
|
|
if (!ast && throw_on_error)
|
|
|
|
throw Exception(error_message, ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr DatabaseDictionary::getCreateTableQuery(const Context & context, const String & table_name) const
|
|
|
|
{
|
|
|
|
return getCreateTableQueryImpl(context, table_name, true);
|
2018-03-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 19:56:44 +00:00
|
|
|
ASTPtr DatabaseDictionary::tryGetCreateTableQuery(const Context & context, const String & table_name) const
|
|
|
|
{
|
2018-03-23 19:56:24 +00:00
|
|
|
return getCreateTableQueryImpl(context, table_name, false);
|
2018-03-14 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 13:28:32 +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);
|
|
|
|
buffer << "CREATE DATABASE " << backQuoteIfNeed(name) << " ENGINE = Dictionary";
|
|
|
|
}
|
|
|
|
ParserCreateQuery parser;
|
2018-04-16 15:39:12 +00:00
|
|
|
return parseQuery(parser, query.data(), query.data() + query.size(), "", 0);
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseDictionary::shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:56:38 +00:00
|
|
|
String DatabaseDictionary::getDatabaseName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|