rebuild indexes and projections when UPDATE compact parts with index_granularity_bytes > 0

Signed-off-by: Duc Canh Le <duccanh.le@ahrefs.com>
This commit is contained in:
Duc Canh Le 2023-10-02 07:36:57 +00:00
parent 9200b121ac
commit 0064d3637f
2 changed files with 14 additions and 4 deletions

View File

@ -40,6 +40,7 @@
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Parsers/makeASTForLogicalFunction.h>
#include <Common/logger_useful.h>
#include <Storages/MergeTree/MergeTreeDataPartType.h>
namespace DB
{
@ -304,6 +305,11 @@ bool MutationsInterpreter::Source::hasProjection(const String & name) const
return part && part->hasProjection(name);
}
bool MutationsInterpreter::Source::isCompactPart() const
{
return part && part->getType() == MergeTreeDataPartType::Compact;
}
static Names getAvailableColumnsWithVirtuals(StorageMetadataPtr metadata_snapshot, const IStorage & storage)
{
auto all_columns = metadata_snapshot->getColumns().getNamesOfPhysical();
@ -562,7 +568,7 @@ void MutationsInterpreter::prepare(bool dry_run)
if (settings.recalculate_dependencies_of_updated_columns)
dependencies = getAllColumnDependencies(metadata_snapshot, updated_columns, has_dependency);
bool has_alter_delete = false;
bool need_rebuild_indexes_projections = false;
std::vector<String> read_columns;
/// First, break a sequence of commands into stages.
@ -583,7 +589,7 @@ void MutationsInterpreter::prepare(bool dry_run)
predicate = makeASTFunction("isZeroOrNull", predicate);
stages.back().filters.push_back(predicate);
has_alter_delete = true;
need_rebuild_indexes_projections = true;
}
else if (command.type == MutationCommand::UPDATE)
{
@ -687,6 +693,9 @@ void MutationsInterpreter::prepare(bool dry_run)
}
}
}
if (source.isCompactPart() && source.getMergeTreeData() && source.getMergeTreeData()->getSettings()->index_granularity_bytes > 0)
need_rebuild_indexes_projections = true;
}
else if (command.type == MutationCommand::MATERIALIZE_COLUMN)
{
@ -892,7 +901,7 @@ void MutationsInterpreter::prepare(bool dry_run)
if (!source.hasSecondaryIndex(index.name))
continue;
if (has_alter_delete)
if (need_rebuild_indexes_projections)
{
materialized_indices.insert(index.name);
continue;
@ -913,7 +922,7 @@ void MutationsInterpreter::prepare(bool dry_run)
if (!source.hasProjection(projection.name))
continue;
if (has_alter_delete)
if (need_rebuild_indexes_projections)
{
materialized_projections.insert(projection.name);
continue;

View File

@ -122,6 +122,7 @@ public:
bool materializeTTLRecalculateOnly() const;
bool hasSecondaryIndex(const String & name) const;
bool hasProjection(const String & name) const;
bool isCompactPart() const;
void read(
Stage & first_stage,