Fix primary key update

This commit is contained in:
alesapin 2020-06-09 20:21:38 +03:00
parent 6f673b0981
commit 4c2e0561f5
2 changed files with 15 additions and 15 deletions

View File

@ -275,22 +275,22 @@ static void checkKeyExpression(const ExpressionActions & expr, const Block & sam
} }
} }
void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool only_check, bool attach) void MergeTreeData::setProperties(const StorageInMemoryMetadata & new_metadata, bool only_check, bool attach)
{ {
KeyDescription new_primary_key = metadata.primary_key; KeyDescription new_primary_key = new_metadata.primary_key;
if (!metadata.sorting_key.definition_ast) if (!new_metadata.sorting_key.definition_ast)
throw Exception("ORDER BY cannot be empty", ErrorCodes::BAD_ARGUMENTS); throw Exception("ORDER BY cannot be empty", ErrorCodes::BAD_ARGUMENTS);
KeyDescription new_sorting_key; KeyDescription new_sorting_key;
if (merging_params.mode == MergeTreeData::MergingParams::VersionedCollapsing) if (merging_params.mode == MergeTreeData::MergingParams::VersionedCollapsing)
new_sorting_key = KeyDescription::getKeyFromAST( new_sorting_key = KeyDescription::getKeyFromAST(
metadata.sorting_key.definition_ast, new_metadata.sorting_key.definition_ast,
metadata.columns, new_metadata.columns,
global_context, global_context,
std::make_shared<ASTIdentifier>(merging_params.version_column)); std::make_shared<ASTIdentifier>(merging_params.version_column));
else else
new_sorting_key = metadata.sorting_key; new_sorting_key = new_metadata.sorting_key;
/// Primary key not defined at all /// Primary key not defined at all
if (new_primary_key.definition_ast == nullptr) if (new_primary_key.definition_ast == nullptr)
@ -299,7 +299,7 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool
/// because in merge tree code we sometimes chech, that our primary key /// because in merge tree code we sometimes chech, that our primary key
/// is fake (copied from sorting key, i.e. isPrimaryKeyDefined() == /// is fake (copied from sorting key, i.e. isPrimaryKeyDefined() ==
/// false, but hasSortingKey() == true) /// false, but hasSortingKey() == true)
new_primary_key = metadata.sorting_key; new_primary_key = new_metadata.sorting_key;
new_primary_key.definition_ast = nullptr; new_primary_key.definition_ast = nullptr;
} }
@ -330,7 +330,7 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool
} }
} }
auto all_columns = metadata.columns.getAllPhysical(); auto all_columns = new_metadata.columns.getAllPhysical();
/// Order by check AST /// Order by check AST
if (hasSortingKey() && only_check) if (hasSortingKey() && only_check)
@ -372,7 +372,7 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool
"added to the sorting key. You can add expressions that use only the newly added columns", "added to the sorting key. You can add expressions that use only the newly added columns",
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
if (metadata.columns.getDefaults().count(col)) if (new_metadata.columns.getDefaults().count(col))
throw Exception("Newly added column " + col + " has a default expression, so adding " throw Exception("Newly added column " + col + " has a default expression, so adding "
"expressions that use it to the sorting key is forbidden", "expressions that use it to the sorting key is forbidden",
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
@ -380,11 +380,11 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool
} }
} }
if (!metadata.secondary_indices.empty()) if (!new_metadata.secondary_indices.empty())
{ {
std::unordered_set<String> indices_names; std::unordered_set<String> indices_names;
for (const auto & index : metadata.secondary_indices) for (const auto & index : new_metadata.secondary_indices)
{ {
MergeTreeIndexFactory::instance().validate(index, attach); MergeTreeIndexFactory::instance().validate(index, attach);
@ -403,9 +403,9 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool
if (!only_check) if (!only_check)
{ {
/// Other parts of metadata initialized is separate methods /// Other parts of metadata initialized is separate methods
setColumns(std::move(metadata.columns)); setColumns(std::move(new_metadata.columns));
setSecondaryIndices(std::move(metadata.secondary_indices)); setSecondaryIndices(std::move(new_metadata.secondary_indices));
setConstraints(std::move(metadata.constraints)); setConstraints(std::move(new_metadata.constraints));
setSortingKey(std::move(new_sorting_key)); setSortingKey(std::move(new_sorting_key));
setPrimaryKey(std::move(new_primary_key)); setPrimaryKey(std::move(new_primary_key));
} }

View File

@ -780,7 +780,7 @@ protected:
/// The same for clearOldTemporaryDirectories. /// The same for clearOldTemporaryDirectories.
std::mutex clear_old_temporary_directories_mutex; std::mutex clear_old_temporary_directories_mutex;
void setProperties(const StorageInMemoryMetadata & metadata, bool only_check = false, bool attach = false); void setProperties(const StorageInMemoryMetadata & new_metadata, bool only_check = false, bool attach = false);
void initPartitionKey(const KeyDescription & new_partition_key); void initPartitionKey(const KeyDescription & new_partition_key);