Skip to content

Commit

Permalink
Merge pull request #56331 from ucasfl/alter
Browse files Browse the repository at this point in the history
Support create and materialized index in the same alter query
  • Loading branch information
alexey-milovidov committed Nov 18, 2023
2 parents 4353507 + 468d549 commit 2efa5ab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/Interpreters/InterpreterAlterQuery.cpp
Expand Up @@ -114,7 +114,6 @@ BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
if (table->isStaticStorage())
throw Exception(ErrorCodes::TABLE_IS_READ_ONLY, "Table is read-only");
auto table_lock = table->lockForShare(getContext()->getCurrentQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
auto metadata_snapshot = table->getInMemoryMetadataPtr();

/// Add default database to table identifiers that we can encounter in e.g. default expressions, mutation expression, etc.
AddDefaultDatabaseVisitor visitor(getContext(), table_id.getDatabaseName());
Expand All @@ -137,10 +136,6 @@ BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
}
else if (auto mut_command = MutationCommand::parse(command_ast))
{
if (mut_command->type == MutationCommand::MATERIALIZE_TTL && !metadata_snapshot->hasAnyTTL())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Cannot MATERIALIZE TTL as there is no TTL set for table {}",
table->getStorageID().getNameForLogs());

if (mut_command->type == MutationCommand::UPDATE || mut_command->type == MutationCommand::DELETE)
{
/// TODO: add a check for result query size.
Expand Down Expand Up @@ -171,8 +166,30 @@ BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
"to execute ALTERs of different types (replicated and non replicated) in single query");
}

if (!alter_commands.empty())
{
auto alter_lock = table->lockForAlter(getContext()->getSettingsRef().lock_acquire_timeout);
StorageInMemoryMetadata metadata = table->getInMemoryMetadata();
alter_commands.validate(table, getContext());
alter_commands.prepare(metadata);
table->checkAlterIsPossible(alter_commands, getContext());
table->alter(alter_commands, getContext(), alter_lock);
}

/// Get newest metadata_snapshot after execute ALTER command, in order to
/// support like materialize index in the same ALTER query that creates it.
auto metadata_snapshot = table->getInMemoryMetadataPtr();

if (mutation_commands.hasNonEmptyMutationCommands())
{
for (const auto & command : mutation_commands)
{
/// Check it after alter finished, so we can add TTL and materialize TTL in the same ALTER query.
if (command.type == MutationCommand::MATERIALIZE_TTL && !metadata_snapshot->hasAnyTTL())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Cannot MATERIALIZE TTL as there is no TTL set for table {}",
table->getStorageID().getNameForLogs());

}
table->checkMutationIsPossible(mutation_commands, getContext()->getSettingsRef());
MutationsInterpreter::Settings settings(false);
MutationsInterpreter(table, metadata_snapshot, mutation_commands, getContext(), settings).validate();
Expand All @@ -187,16 +204,6 @@ BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
res.pipeline = QueryPipeline(std::move(partition_commands_pipe));
}

if (!alter_commands.empty())
{
auto alter_lock = table->lockForAlter(getContext()->getSettingsRef().lock_acquire_timeout);
StorageInMemoryMetadata metadata = table->getInMemoryMetadata();
alter_commands.validate(table, getContext());
alter_commands.prepare(metadata);
table->checkAlterIsPossible(alter_commands, getContext());
table->alter(alter_commands, getContext(), alter_lock);
}

return res;
}

Expand Down
Empty file.
@@ -0,0 +1,18 @@
-- Tags: no-replicated-database

DROP TABLE IF EXISTS index_test;

CREATE TABLE index_test
(
x UInt32,
y UInt32,
z UInt32
) ENGINE = MergeTree order by x;

ALTER TABLE index_test
ADD INDEX i_x mortonDecode(2, z).1 TYPE minmax GRANULARITY 1,
ADD INDEX i_y mortonDecode(2, z).2 TYPE minmax GRANULARITY 1,
MATERIALIZE INDEX i_x,
MATERIALIZE INDEX i_y;

drop table index_test;

0 comments on commit 2efa5ab

Please sign in to comment.