Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor alter addition #8456

Merged
merged 3 commits into from Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions dbms/src/Interpreters/InterpreterAlterQuery.cpp
Expand Up @@ -106,6 +106,7 @@ BlockIO InterpreterAlterQuery::execute()
StorageInMemoryMetadata metadata = table->getInMemoryMetadata();
alter_commands.validate(metadata, context);
alter_commands.prepare(metadata, context);
table->checkAlterIsPossible(alter_commands, context.getSettingsRef());
table->alter(alter_commands, context, table_lock_holder);
}

Expand Down
10 changes: 3 additions & 7 deletions dbms/src/Storages/AlterCommands.cpp
Expand Up @@ -392,8 +392,9 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata) const
for (const auto & change : settings_changes)
{
auto finder = [&change](const SettingChange & c) { return c.name == change.name; };
if (auto it = std::find_if(settings_from_storage.begin(), settings_from_storage.end(), finder);
it != settings_from_storage.end())
auto it = std::find_if(settings_from_storage.begin(), settings_from_storage.end(), finder);

if (it != settings_from_storage.end())
it->value = change.value;
else
settings_from_storage.push_back(change);
Expand Down Expand Up @@ -644,11 +645,6 @@ void AlterCommands::prepare(const StorageInMemoryMetadata & metadata, const Cont

void AlterCommands::validate(const StorageInMemoryMetadata & metadata, const Context & context) const
{
/// We will save ALTER ADD/MODIFY command indices (only the last for each column) for possible modification
/// (we might need to add deduced types or modify default expressions).
/// Saving indices because we can add new commands later and thus cause vector resize.
std::unordered_map<String, size_t> column_to_command_idx;

for (size_t i = 0; i < size(); ++i)
{
auto & command = (*this)[i];
Expand Down
21 changes: 17 additions & 4 deletions dbms/src/Storages/AlterCommands.h
Expand Up @@ -96,32 +96,45 @@ struct AlterCommand
/// in each part on disk (it's not lightweight alter).
bool isModifyingData() const;

/// checks that only settings changed by alter
/// Checks that only settings changed by alter
bool isSettingsAlter() const;

/// Checks that only comment changed by alter
bool isCommentAlter() const;
};

/// Return string representation of AlterCommand::Type
String alterTypeToString(const AlterCommand::Type type);

class Context;

/// Vector of AlterCommand with several additional functions
class AlterCommands : public std::vector<AlterCommand>
{
private:
bool prepared = false;
public:
void apply(StorageInMemoryMetadata & metadata) const;

/// Validate that commands can be applied to metadata.
/// Checks that all columns exist and dependecies between them.
/// This check is lightweight and base only on metadata.
/// More accurate check have to be performed with storage->checkAlterIsPossible.
void validate(const StorageInMemoryMetadata & metadata, const Context & context) const;

/// Prepare alter commands. Set ignore flag to some of them
/// and additional commands for dependent columns.
void prepare(const StorageInMemoryMetadata & metadata, const Context & context);

void validate(const StorageInMemoryMetadata & metadata, const Context & context) const;
/// Apply all alter command in sequential order to storage metadata.
/// Commands have to be prepared before apply.
void apply(StorageInMemoryMetadata & metadata) const;

/// At least one command modify data on disk.
bool isModifyingData() const;

/// At least one command modify settings.
bool isSettingsAlter() const;

/// At least one command modify comments.
bool isCommentAlter() const;
};

Expand Down
7 changes: 4 additions & 3 deletions dbms/src/Storages/IStorage.cpp
Expand Up @@ -381,12 +381,13 @@ StorageInMemoryMetadata IStorage::getInMemoryMetadata() const
void IStorage::alter(
const AlterCommands & params,
const Context & context,
TableStructureWriteLockHolder & /*table_lock_holder*/)
TableStructureWriteLockHolder & table_lock_holder)
{
checkAlterIsPossible(params, context.getSettingsRef());

const String database_name = getDatabaseName();
const String table_name = getTableName();

lockStructureExclusively(table_lock_holder, context.getCurrentQueryId());

StorageInMemoryMetadata metadata = getInMemoryMetadata();
params.apply(metadata);
context.getDatabase(database_name)->alterTable(context, table_name, metadata);
Expand Down
2 changes: 0 additions & 2 deletions dbms/src/Storages/StorageBuffer.cpp
Expand Up @@ -716,8 +716,6 @@ void StorageBuffer::alter(const AlterCommands & params, const Context & context,
{
lockStructureExclusively(table_lock_holder, context.getCurrentQueryId());

checkAlterIsPossible(params, context.getSettingsRef());

const String database_name_ = getDatabaseName();
const String table_name_ = getTableName();

Expand Down
2 changes: 0 additions & 2 deletions dbms/src/Storages/StorageDistributed.cpp
Expand Up @@ -411,8 +411,6 @@ void StorageDistributed::alter(const AlterCommands & params, const Context & con
{
lockStructureExclusively(table_lock_holder, context.getCurrentQueryId());

checkAlterIsPossible(params, context.getSettingsRef());

const String current_database_name = getDatabaseName();
const String current_table_name = getTableName();

Expand Down
2 changes: 0 additions & 2 deletions dbms/src/Storages/StorageMergeTree.cpp
Expand Up @@ -252,8 +252,6 @@ void StorageMergeTree::alter(

lockNewDataStructureExclusively(table_lock_holder, context.getCurrentQueryId());

checkAlterIsPossible(params, context.getSettingsRef());

StorageInMemoryMetadata metadata = getInMemoryMetadata();

params.apply(metadata);
Expand Down
3 changes: 0 additions & 3 deletions dbms/src/Storages/StorageReplicatedMergeTree.cpp
Expand Up @@ -3198,9 +3198,6 @@ void StorageReplicatedMergeTree::alter(
const String current_database_name = getDatabaseName();
const String current_table_name = getTableName();


checkAlterIsPossible(params, query_context.getSettingsRef());

/// We cannot check this alter commands with method isModifyingData()
/// because ReplicatedMergeTree stores both columns and metadata for
/// each replica. So we have to wait AlterThread even with lightweight
Expand Down