2011-11-05 23:31:19 +00:00
|
|
|
#include <Poco/File.h>
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Databases/IDatabase.h>
|
2017-05-23 18:01:50 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/DDLWorker.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterDropQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTDropQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int TABLE_WAS_NOT_DROPPED;
|
|
|
|
extern const int DATABASE_NOT_EMPTY;
|
|
|
|
extern const int UNKNOWN_DATABASE;
|
2017-12-20 07:39:52 +00:00
|
|
|
extern const int READONLY;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
InterpreterDropQuery::InterpreterDropQuery(const ASTPtr & query_ptr_, Context & context_) : query_ptr(query_ptr_), context(context_) {}
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO InterpreterDropQuery::execute()
|
2017-04-13 16:12:56 +00:00
|
|
|
{
|
|
|
|
ASTDropQuery & drop = typeid_cast<ASTDropQuery &>(*query_ptr);
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
checkAccess(drop);
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
if (!drop.cluster.empty())
|
2017-04-25 15:21:03 +00:00
|
|
|
return executeDDLQueryOnCluster(query_ptr, context);
|
2017-04-13 16:12:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String path = context.getPath();
|
|
|
|
String current_database = context.getCurrentDatabase();
|
|
|
|
|
|
|
|
bool drop_database = drop.table.empty() && !drop.database.empty();
|
|
|
|
|
|
|
|
if (drop_database && drop.detach)
|
|
|
|
{
|
|
|
|
context.detachDatabase(drop.database);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-08-15 12:34:28 +00:00
|
|
|
/// Drop temporary table.
|
2018-02-02 13:17:45 +00:00
|
|
|
if (drop.database.empty() || drop.temporary)
|
2017-08-15 12:34:28 +00:00
|
|
|
{
|
2017-08-15 17:00:18 +00:00
|
|
|
StoragePtr table = (context.hasSessionContext() ? context.getSessionContext() : context).tryRemoveExternalTable(drop.table);
|
|
|
|
if (table)
|
|
|
|
{
|
2018-02-02 13:17:45 +00:00
|
|
|
if (drop.database.empty() && !drop.temporary)
|
|
|
|
{
|
|
|
|
LOG_WARNING((&Logger::get("InterpreterDropQuery")),
|
2018-02-08 19:14:22 +00:00
|
|
|
"It is recommended to use `DROP TEMPORARY TABLE` to delete temporary tables");
|
2018-02-02 13:17:45 +00:00
|
|
|
}
|
2017-08-15 17:00:18 +00:00
|
|
|
table->shutdown();
|
|
|
|
/// If table was already dropped by anyone, an exception will be thrown
|
2017-09-01 15:05:23 +00:00
|
|
|
auto table_lock = table->lockForAlter(__PRETTY_FUNCTION__);
|
2017-08-15 17:00:18 +00:00
|
|
|
/// Delete table data
|
|
|
|
table->drop();
|
|
|
|
table->is_dropped = true;
|
|
|
|
return {};
|
|
|
|
}
|
2017-08-15 12:34:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String database_name = drop.database.empty() ? current_database : drop.database;
|
|
|
|
String database_name_escaped = escapeForFileName(database_name);
|
|
|
|
|
|
|
|
String metadata_path = path + "metadata/" + database_name_escaped + "/";
|
2018-01-25 19:01:11 +00:00
|
|
|
String database_metadata_path = path + "metadata/" + database_name_escaped + ".sql";
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto database = context.tryGetDatabase(database_name);
|
|
|
|
if (!database && !drop.if_exists)
|
|
|
|
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
std::vector<std::pair<StoragePtr, std::unique_ptr<DDLGuard>>> tables_to_drop;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!drop_database)
|
|
|
|
{
|
|
|
|
StoragePtr table;
|
|
|
|
|
|
|
|
if (drop.if_exists)
|
|
|
|
table = context.tryGetTable(database_name, drop.table);
|
|
|
|
else
|
|
|
|
table = context.getTable(database_name, drop.table);
|
|
|
|
|
|
|
|
if (table)
|
2017-12-20 07:39:52 +00:00
|
|
|
tables_to_drop.emplace_back(table,
|
|
|
|
context.getDDLGuard(
|
|
|
|
database_name, drop.table, "Table " + database_name + "." + drop.table + " is dropping or detaching right now"));
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!database)
|
|
|
|
{
|
|
|
|
if (!drop.if_exists)
|
|
|
|
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-09-11 12:39:01 +00:00
|
|
|
for (auto iterator = database->getIterator(context); iterator->isValid(); iterator->next())
|
2017-12-20 07:39:52 +00:00
|
|
|
tables_to_drop.emplace_back(iterator->table(),
|
|
|
|
context.getDDLGuard(database_name,
|
|
|
|
iterator->name(),
|
|
|
|
"Table " + database_name + "." + iterator->name() + " is dropping or detaching right now"));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & table : tables_to_drop)
|
|
|
|
{
|
2017-06-13 16:36:15 +00:00
|
|
|
if (!drop.detach)
|
|
|
|
{
|
|
|
|
if (!table.first->checkTableCanBeDropped())
|
2017-12-20 07:39:52 +00:00
|
|
|
throw Exception("Table " + database_name + "." + table.first->getTableName() + " couldn't be dropped due to failed pre-drop check",
|
|
|
|
ErrorCodes::TABLE_WAS_NOT_DROPPED);
|
2017-06-13 16:36:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
table.first->shutdown();
|
|
|
|
|
|
|
|
/// If table was already dropped by anyone, an exception will be thrown
|
2017-09-01 15:05:23 +00:00
|
|
|
auto table_lock = table.first->lockForAlter(__PRETTY_FUNCTION__);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String current_table_name = table.first->getTableName();
|
|
|
|
|
|
|
|
if (drop.detach)
|
|
|
|
{
|
|
|
|
/// Drop table from memory, don't touch data and metadata
|
|
|
|
database->detachTable(current_table_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Delete table metdata and table itself from memory
|
2017-09-11 12:39:01 +00:00
|
|
|
database->removeTable(context, current_table_name);
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Delete table data
|
|
|
|
table.first->drop();
|
|
|
|
|
|
|
|
table.first->is_dropped = true;
|
|
|
|
|
2018-02-21 19:26:59 +00:00
|
|
|
String database_data_path = database->getDataPath();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-08 14:14:08 +00:00
|
|
|
/// If it is not virtual database like Dictionary then drop remaining data dir
|
|
|
|
if (!database_data_path.empty())
|
|
|
|
{
|
|
|
|
String table_data_path = database_data_path + "/" + escapeForFileName(current_table_name);
|
|
|
|
|
|
|
|
if (Poco::File(table_data_path).exists())
|
|
|
|
Poco::File(table_data_path).remove(true);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drop_database)
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Delete the database. The tables in it have already been deleted.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto lock = context.getLock();
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Someone could have time to delete the database before us.
|
2017-04-01 07:20:54 +00:00
|
|
|
context.assertDatabaseExists(database_name);
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Someone could have time to create a table in the database to be deleted while we deleted the tables without the context lock.
|
2017-09-11 12:39:01 +00:00
|
|
|
if (!context.getDatabase(database_name)->empty(context))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("New table appeared in database being dropped. Try dropping it again.", ErrorCodes::DATABASE_NOT_EMPTY);
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Delete database information from the RAM
|
2017-04-01 07:20:54 +00:00
|
|
|
auto database = context.detachDatabase(database_name);
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Delete the database.
|
2017-04-01 07:20:54 +00:00
|
|
|
database->drop();
|
|
|
|
|
2018-02-08 14:14:08 +00:00
|
|
|
/// Remove data directory if it is not virtual database. TODO: should IDatabase::drop() do that?
|
2018-02-21 19:26:59 +00:00
|
|
|
String database_data_path = database->getDataPath();
|
2018-02-08 14:14:08 +00:00
|
|
|
if (!database_data_path.empty())
|
|
|
|
Poco::File(database_data_path).remove(false);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::File(metadata_path).remove(false);
|
2018-01-25 19:01:11 +00:00
|
|
|
|
|
|
|
/// Old ClickHouse versions did not store database.sql files
|
|
|
|
Poco::File database_metadata_file(database_metadata_path);
|
|
|
|
if (database_metadata_file.exists())
|
|
|
|
database_metadata_file.remove(false);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2014-03-20 10:59:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
void InterpreterDropQuery::checkAccess(const ASTDropQuery & drop)
|
|
|
|
{
|
|
|
|
const Settings & settings = context.getSettingsRef();
|
2018-03-11 00:15:26 +00:00
|
|
|
auto readonly = settings.readonly;
|
2015-12-13 12:02:10 +00:00
|
|
|
|
2017-12-22 19:20:18 +00:00
|
|
|
/// It's allowed to drop temporary tables.
|
2017-12-20 07:39:52 +00:00
|
|
|
if (!readonly || (drop.database.empty() && context.tryGetExternalTable(drop.table) && readonly >= 2))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception("Cannot drop table in readonly mode", ErrorCodes::READONLY);
|
|
|
|
}
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|