2022-06-07 05:20:46 +00:00
|
|
|
#include <Interpreters/InterpreterDeleteQuery.h>
|
|
|
|
|
|
|
|
#include <Access/ContextAccess.h>
|
|
|
|
#include <Databases/DatabaseReplicated.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/FunctionNameNormalizer.h>
|
2023-01-30 14:46:17 +00:00
|
|
|
#include <Interpreters/InterpreterAlterQuery.h>
|
2022-06-07 05:20:46 +00:00
|
|
|
#include <Interpreters/MutationsInterpreter.h>
|
2023-01-30 14:46:17 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/ParserAlterQuery.h>
|
2022-06-07 05:20:46 +00:00
|
|
|
#include <Parsers/ASTDeleteQuery.h>
|
|
|
|
#include <Storages/AlterCommands.h>
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Storages/MutationCommands.h>
|
2022-07-21 19:50:19 +00:00
|
|
|
#include <Storages/LightweightDeleteDescription.h>
|
2022-06-07 05:20:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int TABLE_IS_READ_ONLY;
|
2022-07-06 10:29:29 +00:00
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
2022-12-29 11:24:48 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2022-06-07 05:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InterpreterDeleteQuery::InterpreterDeleteQuery(const ASTPtr & query_ptr_, ContextPtr context_) : WithContext(context_), query_ptr(query_ptr_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterDeleteQuery::execute()
|
|
|
|
{
|
|
|
|
FunctionNameNormalizer().visit(query_ptr.get());
|
|
|
|
const ASTDeleteQuery & delete_query = query_ptr->as<ASTDeleteQuery &>();
|
|
|
|
auto table_id = getContext()->resolveStorageID(delete_query, Context::ResolveOrdinary);
|
|
|
|
|
|
|
|
getContext()->checkAccess(AccessType::ALTER_DELETE, table_id);
|
|
|
|
|
|
|
|
query_ptr->as<ASTDeleteQuery &>().setDatabase(table_id.database_name);
|
|
|
|
|
|
|
|
/// First check table storage for validations.
|
|
|
|
StoragePtr table = DatabaseCatalog::instance().getTable(table_id, getContext());
|
|
|
|
checkStorageSupportsTransactionsIfNeeded(table, getContext());
|
|
|
|
if (table->isStaticStorage())
|
|
|
|
throw Exception(ErrorCodes::TABLE_IS_READ_ONLY, "Table is read-only");
|
|
|
|
|
|
|
|
DatabasePtr database = DatabaseCatalog::instance().getDatabase(table_id.database_name);
|
2022-09-15 19:15:57 +00:00
|
|
|
if (database->shouldReplicateQuery(getContext(), query_ptr))
|
2022-06-07 05:20:46 +00:00
|
|
|
{
|
|
|
|
auto guard = DatabaseCatalog::instance().getDDLGuard(table_id.database_name, table_id.table_name);
|
|
|
|
guard->releaseTableLock();
|
2022-09-16 14:25:32 +00:00
|
|
|
return database->tryEnqueueReplicatedDDL(query_ptr, getContext());
|
2022-06-07 05:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto table_lock = table->lockForShare(getContext()->getCurrentQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
|
|
|
|
auto metadata_snapshot = table->getInMemoryMetadataPtr();
|
|
|
|
|
2022-12-29 11:24:48 +00:00
|
|
|
if (table->supportsDelete())
|
2022-08-31 13:08:27 +00:00
|
|
|
{
|
|
|
|
/// Convert to MutationCommand
|
|
|
|
MutationCommands mutation_commands;
|
|
|
|
MutationCommand mut_command;
|
|
|
|
|
|
|
|
mut_command.type = MutationCommand::Type::DELETE;
|
|
|
|
mut_command.predicate = delete_query.predicate;
|
|
|
|
|
|
|
|
mutation_commands.emplace_back(mut_command);
|
|
|
|
|
|
|
|
table->checkMutationIsPossible(mutation_commands, getContext()->getSettingsRef());
|
|
|
|
MutationsInterpreter(table, metadata_snapshot, mutation_commands, getContext(), false).validate();
|
2023-01-30 17:38:28 +00:00
|
|
|
table->mutate(mutation_commands, getContext());
|
2022-08-31 13:08:27 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-12-29 11:24:48 +00:00
|
|
|
else if (table->supportsLightweightDelete())
|
|
|
|
{
|
2023-03-01 18:35:55 +00:00
|
|
|
if (!getContext()->getSettingsRef().enable_lightweight_delete)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
|
2023-03-01 18:35:55 +00:00
|
|
|
"Lightweight delete mutate is disabled. "
|
|
|
|
"Set `enable_lightweight_delete` setting to enable it");
|
2022-12-29 11:24:48 +00:00
|
|
|
|
2023-01-30 14:46:17 +00:00
|
|
|
/// Build "ALTER ... UPDATE _row_exists = 0 WHERE predicate" query
|
|
|
|
String alter_query =
|
|
|
|
"ALTER TABLE " + table->getStorageID().getFullTableName()
|
2023-01-30 15:42:52 +00:00
|
|
|
+ (delete_query.cluster.empty() ? "" : " ON CLUSTER " + backQuoteIfNeed(delete_query.cluster))
|
2023-01-30 14:46:17 +00:00
|
|
|
+ " UPDATE `_row_exists` = 0 WHERE " + serializeAST(*delete_query.predicate);
|
|
|
|
|
|
|
|
ParserAlterQuery parser;
|
|
|
|
ASTPtr alter_ast = parseQuery(
|
|
|
|
parser,
|
|
|
|
alter_query.data(),
|
|
|
|
alter_query.data() + alter_query.size(),
|
|
|
|
"ALTER query",
|
|
|
|
0,
|
|
|
|
DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
|
|
|
|
|
|
|
auto context = Context::createCopy(getContext());
|
|
|
|
context->setSetting("mutations_sync", 2); /// Lightweight delete is always synchronous
|
|
|
|
InterpreterAlterQuery alter_interpreter(alter_ast, context);
|
|
|
|
return alter_interpreter.execute();
|
2022-12-29 11:24:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-31 00:40:50 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "DELETE query is not supported for table {}", table->getStorageID().getFullTableName());
|
2022-12-29 11:24:48 +00:00
|
|
|
}
|
2022-06-07 05:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|