2022-06-16 09:58:14 +00:00
|
|
|
#include <Interpreters/InterpreterCreateIndexQuery.h>
|
|
|
|
|
|
|
|
#include <Access/ContextAccess.h>
|
|
|
|
#include <Databases/DatabaseReplicated.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/executeDDLQueryOnCluster.h>
|
|
|
|
#include <Parsers/ASTCreateIndexQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTIndexDeclaration.h>
|
|
|
|
#include <Storages/AlterCommands.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int TABLE_IS_READ_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterCreateIndexQuery::execute()
|
|
|
|
{
|
2022-06-29 11:10:30 +00:00
|
|
|
auto current_context = getContext();
|
2022-06-16 09:58:14 +00:00
|
|
|
const auto & create_index = query_ptr->as<ASTCreateIndexQuery &>();
|
|
|
|
|
|
|
|
AccessRightsElements required_access;
|
|
|
|
required_access.emplace_back(AccessType::ALTER_ADD_INDEX, create_index.getDatabase(), create_index.getTable());
|
|
|
|
|
|
|
|
if (!create_index.cluster.empty())
|
|
|
|
{
|
|
|
|
DDLQueryOnClusterParams params;
|
|
|
|
params.access_to_check = std::move(required_access);
|
2022-06-29 11:10:30 +00:00
|
|
|
return executeDDLQueryOnCluster(query_ptr, current_context, params);
|
2022-06-16 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:10:30 +00:00
|
|
|
current_context->checkAccess(required_access);
|
|
|
|
auto table_id = current_context->resolveStorageID(create_index, Context::ResolveOrdinary);
|
2022-06-16 09:58:14 +00:00
|
|
|
query_ptr->as<ASTCreateIndexQuery &>().setDatabase(table_id.database_name);
|
|
|
|
|
|
|
|
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-16 09:58:14 +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, current_context);
|
2022-06-16 09:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:10:30 +00:00
|
|
|
StoragePtr table = DatabaseCatalog::instance().getTable(table_id, current_context);
|
2022-06-16 09:58:14 +00:00
|
|
|
if (table->isStaticStorage())
|
|
|
|
throw Exception(ErrorCodes::TABLE_IS_READ_ONLY, "Table is read-only");
|
|
|
|
|
|
|
|
/// Convert ASTCreateIndexQuery to AlterCommand.
|
|
|
|
AlterCommands alter_commands;
|
|
|
|
|
|
|
|
AlterCommand command;
|
2022-06-29 11:10:30 +00:00
|
|
|
command.ast = create_index.convertToASTAlterCommand();
|
2022-06-16 09:58:14 +00:00
|
|
|
command.index_decl = create_index.index_decl;
|
|
|
|
command.type = AlterCommand::ADD_INDEX;
|
|
|
|
command.index_name = create_index.index_name->as<ASTIdentifier &>().name();
|
|
|
|
command.if_not_exists = create_index.if_not_exists;
|
|
|
|
|
|
|
|
alter_commands.emplace_back(std::move(command));
|
|
|
|
|
2022-06-29 11:10:30 +00:00
|
|
|
auto alter_lock = table->lockForAlter(current_context->getSettingsRef().lock_acquire_timeout);
|
2022-06-16 09:58:14 +00:00
|
|
|
StorageInMemoryMetadata metadata = table->getInMemoryMetadata();
|
2022-06-29 11:10:30 +00:00
|
|
|
alter_commands.validate(table, current_context);
|
2022-06-16 09:58:14 +00:00
|
|
|
alter_commands.prepare(metadata);
|
2022-06-29 11:10:30 +00:00
|
|
|
table->checkAlterIsPossible(alter_commands, current_context);
|
|
|
|
table->alter(alter_commands, current_context, alter_lock);
|
2022-06-16 09:58:14 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|