2019-05-17 14:34:25 +00:00
|
|
|
#include <Databases/DatabasesCommon.h>
|
2017-05-23 18:33:48 +00:00
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
2016-03-26 03:03:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-10-27 21:18:06 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int EMPTY_LIST_OF_COLUMNS_PASSED;
|
2018-03-23 20:46:43 +00:00
|
|
|
extern const int TABLE_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_TABLE;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2017-10-27 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:03:50 +00:00
|
|
|
|
|
|
|
String getTableDefinitionFromCreateQuery(const ASTPtr & query)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr query_clone = query->clone();
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & create = query_clone->as<ASTCreateQuery &>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// We remove everything that is not needed for ATTACH from the query.
|
2019-03-15 16:14:13 +00:00
|
|
|
create.attach = true;
|
|
|
|
create.database.clear();
|
|
|
|
create.as_database.clear();
|
|
|
|
create.as_table.clear();
|
|
|
|
create.if_not_exists = false;
|
|
|
|
create.is_populate = false;
|
|
|
|
create.replace_view = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
/// For views it is necessary to save the SELECT query itself, for the rest - on the contrary
|
2019-03-15 16:14:13 +00:00
|
|
|
if (!create.is_view && !create.is_materialized_view)
|
|
|
|
create.select = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
create.format = nullptr;
|
|
|
|
create.out_file = nullptr;
|
2017-11-21 14:45:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ostringstream statement_stream;
|
2019-03-15 16:14:13 +00:00
|
|
|
formatAST(create, statement_stream, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
statement_stream << '\n';
|
|
|
|
return statement_stream.str();
|
2016-03-26 03:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<String, StoragePtr> createTableFromDefinition(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & definition,
|
|
|
|
const String & database_name,
|
|
|
|
const String & database_data_path,
|
|
|
|
Context & context,
|
|
|
|
bool has_force_restore_data_flag,
|
|
|
|
const String & description_for_error_message)
|
2016-03-26 03:03:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserCreateQuery parser;
|
2018-04-16 15:11:13 +00:00
|
|
|
ASTPtr ast = parseQuery(parser, definition.data(), definition.data() + definition.size(), description_for_error_message, 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & ast_create_query = ast->as<ASTCreateQuery &>();
|
|
|
|
ast_create_query.attach = true;
|
|
|
|
ast_create_query.database = database_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// We do not directly use `InterpreterCreateQuery::execute`, because
|
|
|
|
/// - the database has not been created yet;
|
|
|
|
/// - the code is simpler, since the query is already brought to a suitable form.
|
2019-03-15 16:14:13 +00:00
|
|
|
if (!ast_create_query.columns_list || !ast_create_query.columns_list->columns)
|
2017-10-25 00:55:31 +00:00
|
|
|
throw Exception("Missing definition of columns.", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
ColumnsDescription columns = InterpreterCreateQuery::getColumnsDescription(*ast_create_query.columns_list->columns, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
ast_create_query.table,
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageFactory::instance().get(
|
2019-03-15 16:14:13 +00:00
|
|
|
ast_create_query,
|
|
|
|
database_data_path, ast_create_query.table, database_name, context, context.getGlobalContext(),
|
2018-03-06 20:18:34 +00:00
|
|
|
columns,
|
2017-04-01 07:20:54 +00:00
|
|
|
true, has_force_restore_data_flag)
|
|
|
|
};
|
2016-03-26 03:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:46:43 +00:00
|
|
|
|
|
|
|
bool DatabaseWithOwnTablesBase::isTableExist(
|
|
|
|
const Context & /*context*/,
|
|
|
|
const String & table_name) const
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
return tables.find(table_name) != tables.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr DatabaseWithOwnTablesBase::tryGetTable(
|
|
|
|
const Context & /*context*/,
|
|
|
|
const String & table_name) const
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
auto it = tables.find(table_name);
|
|
|
|
if (it == tables.end())
|
|
|
|
return {};
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
DatabaseIteratorPtr DatabaseWithOwnTablesBase::getIterator(const Context & /*context*/, const FilterByNameFunction & filter_by_table_name)
|
2018-03-23 20:46:43 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2019-06-02 12:11:01 +00:00
|
|
|
if (!filter_by_table_name)
|
|
|
|
return std::make_unique<DatabaseSnapshotIterator>(tables);
|
|
|
|
Tables filtered_tables;
|
|
|
|
for (const auto & [table_name, storage] : tables)
|
|
|
|
if (filter_by_table_name(table_name))
|
|
|
|
filtered_tables.emplace(table_name, storage);
|
|
|
|
return std::make_unique<DatabaseSnapshotIterator>(std::move(filtered_tables));
|
2018-03-23 20:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DatabaseWithOwnTablesBase::empty(const Context & /*context*/) const
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
return tables.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr DatabaseWithOwnTablesBase::detachTable(const String & table_name)
|
|
|
|
{
|
|
|
|
StoragePtr res;
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
auto it = tables.find(table_name);
|
|
|
|
if (it == tables.end())
|
|
|
|
throw Exception("Table " + name + "." + table_name + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
|
|
|
|
res = it->second;
|
|
|
|
tables.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseWithOwnTablesBase::attachTable(const String & table_name, const StoragePtr & table)
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
if (!tables.emplace(table_name, table).second)
|
|
|
|
throw Exception("Table " + name + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseWithOwnTablesBase::shutdown()
|
|
|
|
{
|
|
|
|
/// You can not hold a lock during shutdown.
|
|
|
|
/// Because inside `shutdown` function tables can work with database, and mutex is not recursive.
|
|
|
|
|
|
|
|
Tables tables_snapshot;
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
tables_snapshot = tables;
|
|
|
|
}
|
|
|
|
|
2018-06-09 15:48:22 +00:00
|
|
|
for (const auto & kv : tables_snapshot)
|
2018-03-23 20:46:43 +00:00
|
|
|
{
|
|
|
|
kv.second->shutdown();
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-03-23 20:46:43 +00:00
|
|
|
tables.clear();
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:56:45 +00:00
|
|
|
DatabaseWithOwnTablesBase::~DatabaseWithOwnTablesBase()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
shutdown();
|
|
|
|
}
|
2018-11-23 18:52:00 +00:00
|
|
|
catch (...)
|
2018-03-23 20:56:45 +00:00
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 03:03:50 +00:00
|
|
|
}
|