This commit is contained in:
Amos Bird 2023-06-04 20:06:27 +08:00
parent 92b2200c55
commit e24c9267bc
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
2 changed files with 47 additions and 60 deletions

View File

@ -532,55 +532,12 @@ void MutationsInterpreter::prepare(bool dry_run)
validateUpdateColumns(source, metadata_snapshot, updated_columns, column_to_affected_materialized);
}
for (const auto & [_, names] : column_to_affected_materialized)
updated_columns.insert(names.begin(), names.end());
std::function<bool(const String & file_name)> has_index_or_projection
= [&](const String & file_name) { return source.hasIndexOrProjection(file_name); };
if (settings.recalculate_dependencies_of_updated_columns)
dependencies = getAllColumnDependencies(metadata_snapshot, updated_columns, has_index_or_projection);
for (const auto & index : metadata_snapshot->getSecondaryIndices())
{
if (source.hasIndexOrProjection("skp_idx_" + index.name + ".idx") || source.hasIndexOrProjection("skp_idx_" + index.name + ".idx2"))
{
// If some dependent columns gets mutated
bool mutate = false;
const auto & index_cols = index.expression->getRequiredColumns();
for (const auto & col : index_cols)
{
if (updated_columns.contains(col))
{
mutate = true;
break;
}
}
if (mutate)
materialized_indices.insert(index.name);
}
}
for (const auto & projection : metadata_snapshot->getProjections())
{
if (source.hasIndexOrProjection(projection.getDirectoryName()))
{
// If some dependent columns gets mutated
bool mutate = false;
const auto & projection_cols = projection.required_columns;
for (const auto & col : projection_cols)
{
if (updated_columns.contains(col))
{
mutate = true;
break;
}
}
if (mutate)
materialized_projections.insert(projection.name);
}
}
std::vector<String> read_columns;
/// First, break a sequence of commands into stages.
for (auto & command : commands)
@ -869,6 +826,31 @@ void MutationsInterpreter::prepare(bool dry_run)
for (const auto & column : changed_columns)
stages.back().column_to_updated.emplace(
column, std::make_shared<ASTIdentifier>(column));
for (const auto & index : metadata_snapshot->getSecondaryIndices())
{
if (source.hasIndexOrProjection("skp_idx_" + index.name + ".idx")
|| source.hasIndexOrProjection("skp_idx_" + index.name + ".idx2"))
{
const auto & index_cols = index.expression->getRequiredColumns();
bool changed = std::any_of(
index_cols.begin(), index_cols.end(), [&](const auto & col) { return changed_columns.contains(col); });
if (changed)
materialized_indices.insert(index.name);
}
}
for (const auto & projection : metadata_snapshot->getProjections())
{
if (source.hasIndexOrProjection(projection.getDirectoryName()))
{
const auto & projection_cols = projection.required_columns;
bool changed = std::any_of(
projection_cols.begin(), projection_cols.end(), [&](const auto & col) { return changed_columns.contains(col); });
if (changed)
materialized_projections.insert(projection.name);
}
}
}
if (!unchanged_columns.empty())

View File

@ -1269,22 +1269,23 @@ private:
continue;
if (ctx->materialized_indices.contains(idx.name))
skip_indices.push_back(MergeTreeIndexFactory::instance().get(idx));
auto hardlink_index = [&](const String & idx_name)
{
if (ctx->source_part->checksums.has(idx_name))
skip_indices.push_back(MergeTreeIndexFactory::instance().get(idx));
}
else
{
auto prefix = fmt::format("{}{}.", INDEX_FILE_PREFIX, idx.name);
auto it = ctx->source_part->checksums.files.upper_bound(prefix);
while (it != ctx->source_part->checksums.files.end())
{
auto it = ctx->source_part->checksums.files.find(idx_name);
if (it != ctx->source_part->checksums.files.end())
{
entries_to_hardlink.insert(idx_name);
ctx->existing_indices_checksums.addFile(idx_name, it->second.file_size, it->second.file_hash);
}
if (!startsWith(it->first, prefix))
break;
entries_to_hardlink.insert(it->first);
ctx->existing_indices_checksums.addFile(it->first, it->second.file_size, it->second.file_hash);
++it;
}
};
hardlink_index(INDEX_FILE_PREFIX + idx.name + ".idx");
hardlink_index(INDEX_FILE_PREFIX + idx.name + ".idx2");
}
}
NameSet removed_projections;
@ -1301,10 +1302,14 @@ private:
continue;
if (ctx->materialized_projections.contains(projection.name))
{
ctx->projections_to_build.push_back(&projection);
if (ctx->source_part->checksums.has(projection.getDirectoryName()))
entries_to_hardlink.insert(projection.getDirectoryName());
}
else
{
if (ctx->source_part->checksums.has(projection.getDirectoryName()))
entries_to_hardlink.insert(projection.getDirectoryName());
}
}
NameSet hardlinked_files;
@ -1354,7 +1359,7 @@ private:
if (ctx->metadata_snapshot->hasPrimaryKey() || ctx->metadata_snapshot->hasSecondaryIndices())
{
builder.addTransform(std::make_shared<ExpressionTransform>(
builder.getHeader(), ctx->data->getPrimaryKeyAndSkipIndicesExpression(ctx->metadata_snapshot, skip_indices)));
builder.getHeader(), ctx->data->getPrimaryKeyAndSkipIndicesExpression(ctx->metadata_snapshot, skip_indices)));
builder.addTransform(std::make_shared<MaterializingTransform>(builder.getHeader()));
}