2019-10-10 14:38:08 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <Interpreters/MutationsInterpreter.h>
|
2020-07-22 17:13:05 +00:00
|
|
|
#include <Interpreters/TreeRewriter.h>
|
2023-05-25 22:54:54 +00:00
|
|
|
#include <Interpreters/MutationsNonDeterministicHelpers.h>
|
2019-05-03 02:00:57 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2020-11-10 10:23:46 +00:00
|
|
|
#include <Storages/MergeTree/StorageFromMergeTreeDataPart.h>
|
2021-10-21 15:03:16 +00:00
|
|
|
#include <Storages/StorageMergeTree.h>
|
2024-03-05 16:03:02 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeVirtualColumns.h>
|
2020-08-31 12:45:20 +00:00
|
|
|
#include <Processors/Transforms/FilterTransform.h>
|
|
|
|
#include <Processors/Transforms/ExpressionTransform.h>
|
|
|
|
#include <Processors/Transforms/CreatingSetsTransform.h>
|
|
|
|
#include <Processors/Transforms/MaterializingTransform.h>
|
|
|
|
#include <Processors/Sources/NullSource.h>
|
2021-10-16 14:03:50 +00:00
|
|
|
#include <QueryPipeline/QueryPipelineBuilder.h>
|
2020-09-15 17:13:13 +00:00
|
|
|
#include <Processors/QueryPlan/QueryPlan.h>
|
|
|
|
#include <Processors/QueryPlan/ExpressionStep.h>
|
|
|
|
#include <Processors/QueryPlan/FilterStep.h>
|
|
|
|
#include <Processors/QueryPlan/ReadFromPreparedSource.h>
|
2023-01-30 22:48:52 +00:00
|
|
|
#include <Processors/Executors/PullingAsyncPipelineExecutor.h>
|
2021-07-22 16:05:52 +00:00
|
|
|
#include <Processors/Transforms/CheckSortedTransform.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
2023-12-01 19:12:05 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2020-09-15 17:13:13 +00:00
|
|
|
#include <Processors/QueryPlan/CreatingSetsStep.h>
|
2021-04-02 11:31:33 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2022-07-18 15:53:30 +00:00
|
|
|
#include <Interpreters/PreparedSets.h>
|
2023-01-10 15:31:01 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeSequentialSource.h>
|
|
|
|
#include <Processors/Sources/ThrowingExceptionSource.h>
|
2023-01-21 10:20:25 +00:00
|
|
|
#include <Analyzer/QueryTreeBuilder.h>
|
|
|
|
#include <Analyzer/QueryTreePassManager.h>
|
|
|
|
#include <Analyzer/QueryNode.h>
|
|
|
|
#include <Analyzer/TableNode.h>
|
|
|
|
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
|
2023-04-06 19:07:53 +00:00
|
|
|
#include <Parsers/makeASTForLogicalFunction.h>
|
2023-06-04 17:48:13 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2023-10-02 07:36:57 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartType.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-11-10 10:23:46 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2018-09-03 13:36:58 +00:00
|
|
|
extern const int UNKNOWN_MUTATION_COMMAND;
|
2018-09-04 13:45:39 +00:00
|
|
|
extern const int NO_SUCH_COLUMN_IN_TABLE;
|
|
|
|
extern const int CANNOT_UPDATE_COLUMN;
|
2023-01-04 08:56:44 +00:00
|
|
|
extern const int UNEXPECTED_EXPRESSION;
|
2023-05-25 22:54:54 +00:00
|
|
|
extern const int THERE_IS_NO_COLUMN;
|
2023-09-26 17:16:01 +00:00
|
|
|
extern const int ILLEGAL_STATISTIC;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:12 +00:00
|
|
|
|
2019-10-10 14:38:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-07-06 02:07:55 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ASTPtr prepareQueryAffectedAST(const std::vector<MutationCommand> & commands, const StoragePtr & storage, ContextPtr context)
|
2019-11-06 17:28:03 +00:00
|
|
|
{
|
2019-11-07 08:40:36 +00:00
|
|
|
/// Execute `SELECT count() FROM storage WHERE predicate1 OR predicate2 OR ...` query.
|
2020-08-15 14:50:56 +00:00
|
|
|
/// The result can differ from the number of affected rows (e.g. if there is an UPDATE command that
|
2019-11-07 08:40:36 +00:00
|
|
|
/// changes how many rows satisfy the predicates of the subsequent commands).
|
|
|
|
/// But we can be sure that if count = 0, then no rows will be touched.
|
|
|
|
|
|
|
|
auto select = std::make_shared<ASTSelectQuery>();
|
|
|
|
|
|
|
|
select->setExpression(ASTSelectQuery::Expression::SELECT, std::make_shared<ASTExpressionList>());
|
|
|
|
auto count_func = std::make_shared<ASTFunction>();
|
|
|
|
count_func->name = "count";
|
|
|
|
count_func->arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
select->select()->children.push_back(count_func);
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
ASTs conditions;
|
|
|
|
for (const MutationCommand & command : commands)
|
2019-11-07 08:40:36 +00:00
|
|
|
{
|
2020-11-10 10:23:46 +00:00
|
|
|
if (ASTPtr condition = getPartitionAndPredicateExpressionForMutationCommand(command, storage, context))
|
|
|
|
conditions.push_back(std::move(condition));
|
|
|
|
}
|
2019-11-07 08:40:36 +00:00
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
if (conditions.size() > 1)
|
|
|
|
{
|
|
|
|
auto coalesced_predicates = makeASTFunction("or");
|
|
|
|
coalesced_predicates->arguments->children = std::move(conditions);
|
2019-11-07 08:40:36 +00:00
|
|
|
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(coalesced_predicates));
|
|
|
|
}
|
2020-11-10 10:23:46 +00:00
|
|
|
else if (conditions.size() == 1)
|
|
|
|
{
|
|
|
|
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(conditions.front()));
|
|
|
|
}
|
2019-11-07 08:40:36 +00:00
|
|
|
|
|
|
|
return select;
|
2019-11-06 17:28:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 12:45:34 +00:00
|
|
|
QueryTreeNodePtr prepareQueryAffectedQueryTree(const std::vector<MutationCommand> & commands, const StoragePtr & storage, ContextPtr context)
|
|
|
|
{
|
|
|
|
auto ast = prepareQueryAffectedAST(commands, storage, context);
|
|
|
|
auto query_tree = buildQueryTree(ast, context);
|
|
|
|
|
|
|
|
auto & query_node = query_tree->as<QueryNode &>();
|
|
|
|
query_node.getJoinTree() = std::make_shared<TableNode>(storage, context);
|
|
|
|
|
|
|
|
QueryTreePassManager query_tree_pass_manager(context);
|
|
|
|
addQueryTreePasses(query_tree_pass_manager);
|
|
|
|
query_tree_pass_manager.run(query_tree);
|
|
|
|
|
|
|
|
return query_tree;
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:25:59 +00:00
|
|
|
ColumnDependencies getAllColumnDependencies(
|
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
|
|
|
const NameSet & updated_columns,
|
2023-07-24 14:12:01 +00:00
|
|
|
const StorageInMemoryMetadata::HasDependencyCallback & has_dependency)
|
2020-02-17 20:39:24 +00:00
|
|
|
{
|
|
|
|
NameSet new_updated_columns = updated_columns;
|
|
|
|
ColumnDependencies dependencies;
|
2023-07-24 14:12:01 +00:00
|
|
|
|
2020-02-17 20:39:24 +00:00
|
|
|
while (!new_updated_columns.empty())
|
|
|
|
{
|
2023-07-24 14:12:01 +00:00
|
|
|
auto new_dependencies = metadata_snapshot->getColumnDependencies(new_updated_columns, true, has_dependency);
|
2020-02-17 20:39:24 +00:00
|
|
|
new_updated_columns.clear();
|
|
|
|
for (const auto & dependency : new_dependencies)
|
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!dependencies.contains(dependency))
|
2020-02-17 20:39:24 +00:00
|
|
|
{
|
|
|
|
dependencies.insert(dependency);
|
|
|
|
if (!dependency.isReadOnly())
|
|
|
|
new_updated_columns.insert(dependency.column_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dependencies;
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:45:20 +00:00
|
|
|
}
|
2019-10-10 14:38:08 +00:00
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
|
2019-11-07 08:40:36 +00:00
|
|
|
bool isStorageTouchedByMutations(
|
2023-01-10 15:31:01 +00:00
|
|
|
MergeTreeData & storage,
|
|
|
|
MergeTreeData::DataPartPtr source_part,
|
2020-06-17 11:52:19 +00:00
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
2019-11-07 08:40:36 +00:00
|
|
|
const std::vector<MutationCommand> & commands,
|
2023-01-30 18:34:24 +00:00
|
|
|
ContextPtr context)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
if (commands.empty())
|
|
|
|
return false;
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
bool all_commands_can_be_skipped = true;
|
2023-12-01 19:12:05 +00:00
|
|
|
for (const auto & command : commands)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2023-12-01 19:12:05 +00:00
|
|
|
if (command.type == MutationCommand::APPLY_DELETED_MASK)
|
2020-11-10 10:23:46 +00:00
|
|
|
{
|
2023-12-01 19:12:05 +00:00
|
|
|
if (source_part->hasLightweightDelete())
|
|
|
|
return true;
|
2020-11-10 10:23:46 +00:00
|
|
|
}
|
|
|
|
else
|
2023-12-01 19:12:05 +00:00
|
|
|
{
|
|
|
|
if (!command.predicate) /// The command touches all rows.
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (command.partition)
|
|
|
|
{
|
|
|
|
const String partition_id = storage.getPartitionIDFromQuery(command.partition, context);
|
|
|
|
if (partition_id == source_part->info.partition_id)
|
|
|
|
all_commands_can_be_skipped = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
all_commands_can_be_skipped = false;
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
if (all_commands_can_be_skipped)
|
|
|
|
return false;
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
auto storage_from_part = std::make_shared<StorageFromMergeTreeDataPart>(source_part);
|
2019-11-06 17:28:03 +00:00
|
|
|
|
2023-01-21 10:20:25 +00:00
|
|
|
std::optional<InterpreterSelectQuery> interpreter_select_query;
|
|
|
|
BlockIO io;
|
|
|
|
|
|
|
|
if (context->getSettingsRef().allow_experimental_analyzer)
|
|
|
|
{
|
2023-02-19 12:45:34 +00:00
|
|
|
auto select_query_tree = prepareQueryAffectedQueryTree(commands, storage.shared_from_this(), context);
|
2023-10-12 23:25:20 +00:00
|
|
|
InterpreterSelectQueryAnalyzer interpreter(select_query_tree, context, SelectQueryOptions().ignoreLimits());
|
2023-01-21 10:20:25 +00:00
|
|
|
io = interpreter.execute();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-19 12:45:34 +00:00
|
|
|
ASTPtr select_query = prepareQueryAffectedAST(commands, storage.shared_from_this(), context);
|
2023-01-21 10:20:25 +00:00
|
|
|
/// Interpreter must be alive, when we use result of execute() method.
|
|
|
|
/// For some reason it may copy context and give it into ExpressionTransform
|
|
|
|
/// after that we will use context from destroyed stack frame in our stream.
|
|
|
|
interpreter_select_query.emplace(
|
2023-10-12 23:25:20 +00:00
|
|
|
select_query, context, storage_from_part, metadata_snapshot, SelectQueryOptions().ignoreLimits());
|
2023-01-21 10:20:25 +00:00
|
|
|
|
|
|
|
io = interpreter_select_query->execute();
|
|
|
|
}
|
|
|
|
|
2023-01-30 22:48:52 +00:00
|
|
|
PullingAsyncPipelineExecutor executor(io.pipeline);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2021-09-16 17:40:42 +00:00
|
|
|
Block block;
|
2023-01-31 14:49:56 +00:00
|
|
|
while (block.rows() == 0 && executor.pull(block));
|
2021-12-07 16:50:57 +00:00
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
if (!block.rows())
|
|
|
|
return false;
|
|
|
|
else if (block.rows() != 1)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "count() expression returned {} rows, not 1", block.rows());
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2021-09-19 20:15:10 +00:00
|
|
|
Block tmp_block;
|
|
|
|
while (executor.pull(tmp_block));
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
auto count = (*block.getByName("count()").column)[0].get<UInt64>();
|
|
|
|
return count != 0;
|
2020-11-10 10:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr getPartitionAndPredicateExpressionForMutationCommand(
|
|
|
|
const MutationCommand & command,
|
|
|
|
const StoragePtr & storage,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context
|
2020-11-10 10:23:46 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ASTPtr partition_predicate_as_ast_func;
|
|
|
|
if (command.partition)
|
|
|
|
{
|
|
|
|
String partition_id;
|
|
|
|
|
|
|
|
auto storage_merge_tree = std::dynamic_pointer_cast<MergeTreeData>(storage);
|
|
|
|
auto storage_from_merge_tree_data_part = std::dynamic_pointer_cast<StorageFromMergeTreeDataPart>(storage);
|
|
|
|
if (storage_merge_tree)
|
|
|
|
partition_id = storage_merge_tree->getPartitionIDFromQuery(command.partition, context);
|
|
|
|
else if (storage_from_merge_tree_data_part)
|
|
|
|
partition_id = storage_from_merge_tree_data_part->getPartitionIDFromQuery(command.partition, context);
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "ALTER UPDATE/DELETE ... IN PARTITION is not supported for non-MergeTree tables");
|
2019-11-07 08:40:36 +00:00
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
partition_predicate_as_ast_func = makeASTFunction("equals",
|
|
|
|
std::make_shared<ASTIdentifier>("_partition_id"),
|
|
|
|
std::make_shared<ASTLiteral>(partition_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command.predicate && command.partition)
|
|
|
|
return makeASTFunction("and", command.predicate->clone(), std::move(partition_predicate_as_ast_func));
|
|
|
|
else
|
|
|
|
return command.predicate ? command.predicate->clone() : partition_predicate_as_ast_func;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
|
|
|
|
MutationCommand createCommandToApplyDeletedMask(const MutationCommand & command)
|
|
|
|
{
|
|
|
|
if (command.type != MutationCommand::APPLY_DELETED_MASK)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected APPLY_DELETED_MASK mutation command, got: {}", magic_enum::enum_name(command.type));
|
|
|
|
|
|
|
|
auto alter_command = std::make_shared<ASTAlterCommand>();
|
|
|
|
alter_command->type = ASTAlterCommand::DELETE;
|
2023-12-27 17:13:16 +00:00
|
|
|
alter_command->partition = alter_command->children.emplace_back(command.partition).get();
|
2023-12-01 19:12:05 +00:00
|
|
|
|
|
|
|
auto row_exists_predicate = makeASTFunction("equals",
|
2024-02-28 19:58:55 +00:00
|
|
|
std::make_shared<ASTIdentifier>(RowExistsColumn::name),
|
2023-12-01 19:12:05 +00:00
|
|
|
std::make_shared<ASTLiteral>(Field(0)));
|
|
|
|
|
|
|
|
if (command.predicate)
|
2023-12-13 17:27:23 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Mutation command APPLY DELETED MASK does not support WHERE clause");
|
|
|
|
|
2023-12-27 17:13:16 +00:00
|
|
|
alter_command->predicate = alter_command->children.emplace_back(std::move(row_exists_predicate)).get();
|
2023-12-01 19:12:05 +00:00
|
|
|
|
|
|
|
auto mutation_command = MutationCommand::parse(alter_command.get());
|
|
|
|
if (!mutation_command)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Failed to parse command {}. It's a bug", queryToString(alter_command));
|
|
|
|
|
|
|
|
return *mutation_command;
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
MutationsInterpreter::Source::Source(StoragePtr storage_) : storage(std::move(storage_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MutationsInterpreter::Source::Source(MergeTreeData & storage_, MergeTreeData::DataPartPtr source_part_)
|
|
|
|
: data(&storage_), part(std::move(source_part_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageSnapshotPtr MutationsInterpreter::Source::getStorageSnapshot(const StorageMetadataPtr & snapshot_, const ContextPtr & context_) const
|
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
if (const auto * merge_tree = getMergeTreeData())
|
|
|
|
return merge_tree->getStorageSnapshotWithoutData(snapshot_, context_);
|
2023-01-10 15:31:01 +00:00
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
return storage->getStorageSnapshotWithoutData(snapshot_, context_);
|
2023-01-10 15:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr MutationsInterpreter::Source::getStorage() const
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
return data->shared_from_this();
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MergeTreeData * MutationsInterpreter::Source::getMergeTreeData() const
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
return dynamic_cast<const MergeTreeData *>(storage.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutationsInterpreter::Source::supportsLightweightDelete() const
|
|
|
|
{
|
|
|
|
if (part)
|
|
|
|
return part->supportLightweightDeleteMutate();
|
|
|
|
|
|
|
|
return storage->supportsLightweightDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MutationsInterpreter::Source::hasLightweightDeleteMask() const
|
|
|
|
{
|
|
|
|
return part && part->hasLightweightDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutationsInterpreter::Source::materializeTTLRecalculateOnly() const
|
|
|
|
{
|
|
|
|
return data && data->getSettings()->materialize_ttl_recalculate_only;
|
|
|
|
}
|
2020-11-10 10:23:46 +00:00
|
|
|
|
2023-07-24 14:12:01 +00:00
|
|
|
bool MutationsInterpreter::Source::hasSecondaryIndex(const String & name) const
|
|
|
|
{
|
|
|
|
return part && part->hasSecondaryIndex(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutationsInterpreter::Source::hasProjection(const String & name) const
|
2023-05-22 15:25:59 +00:00
|
|
|
{
|
2023-07-24 14:12:01 +00:00
|
|
|
return part && part->hasProjection(name);
|
2023-05-22 15:25:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 15:06:14 +00:00
|
|
|
bool MutationsInterpreter::Source::hasBrokenProjection(const String & name) const
|
|
|
|
{
|
|
|
|
return part && part->hasBrokenProjection(name);
|
|
|
|
}
|
|
|
|
|
2023-10-02 07:36:57 +00:00
|
|
|
bool MutationsInterpreter::Source::isCompactPart() const
|
|
|
|
{
|
|
|
|
return part && part->getType() == MergeTreeDataPartType::Compact;
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
static Names getAvailableColumnsWithVirtuals(StorageMetadataPtr metadata_snapshot, const IStorage & storage)
|
|
|
|
{
|
|
|
|
auto all_columns = metadata_snapshot->getColumns().getNamesOfPhysical();
|
2024-03-01 22:29:56 +00:00
|
|
|
auto virtuals = storage.getVirtualsPtr();
|
|
|
|
for (const auto & column : *virtuals)
|
2023-05-25 22:54:54 +00:00
|
|
|
all_columns.push_back(column.name);
|
|
|
|
return all_columns;
|
|
|
|
}
|
|
|
|
|
2019-11-07 08:40:36 +00:00
|
|
|
MutationsInterpreter::MutationsInterpreter(
|
|
|
|
StoragePtr storage_,
|
2023-05-25 22:54:54 +00:00
|
|
|
StorageMetadataPtr metadata_snapshot_,
|
2019-12-19 13:10:57 +00:00
|
|
|
MutationCommands commands_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context_,
|
2023-05-25 22:54:54 +00:00
|
|
|
Settings settings_)
|
2023-01-10 15:31:01 +00:00
|
|
|
: MutationsInterpreter(
|
2023-05-25 22:54:54 +00:00
|
|
|
Source(storage_),
|
|
|
|
metadata_snapshot_, std::move(commands_),
|
|
|
|
getAvailableColumnsWithVirtuals(metadata_snapshot_, *storage_),
|
|
|
|
std::move(context_), std::move(settings_))
|
2023-01-10 15:31:01 +00:00
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
if (settings.can_execute && dynamic_cast<const MergeTreeData *>(source.getStorage().get()))
|
2023-01-10 15:31:01 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"Cannot execute mutation for {}. Mutation should be applied to every part separately.",
|
|
|
|
source.getStorage()->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MutationsInterpreter::MutationsInterpreter(
|
|
|
|
MergeTreeData & storage_,
|
|
|
|
MergeTreeData::DataPartPtr source_part_,
|
2023-05-25 22:54:54 +00:00
|
|
|
StorageMetadataPtr metadata_snapshot_,
|
2023-01-10 15:31:01 +00:00
|
|
|
MutationCommands commands_,
|
2023-05-25 22:54:54 +00:00
|
|
|
Names available_columns_,
|
2023-01-10 15:31:01 +00:00
|
|
|
ContextPtr context_,
|
2023-05-25 22:54:54 +00:00
|
|
|
Settings settings_)
|
2023-01-10 15:31:01 +00:00
|
|
|
: MutationsInterpreter(
|
|
|
|
Source(storage_, std::move(source_part_)),
|
2023-05-25 22:54:54 +00:00
|
|
|
std::move(metadata_snapshot_), std::move(commands_),
|
|
|
|
std::move(available_columns_), std::move(context_), std::move(settings_))
|
2023-01-10 15:31:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MutationsInterpreter::MutationsInterpreter(
|
|
|
|
Source source_,
|
2023-05-25 22:54:54 +00:00
|
|
|
StorageMetadataPtr metadata_snapshot_,
|
2023-01-10 15:31:01 +00:00
|
|
|
MutationCommands commands_,
|
2023-05-25 22:54:54 +00:00
|
|
|
Names available_columns_,
|
2023-01-10 15:31:01 +00:00
|
|
|
ContextPtr context_,
|
2023-05-25 22:54:54 +00:00
|
|
|
Settings settings_)
|
2023-01-10 15:31:01 +00:00
|
|
|
: source(std::move(source_))
|
2020-06-17 11:52:19 +00:00
|
|
|
, metadata_snapshot(metadata_snapshot_)
|
2019-11-07 08:40:36 +00:00
|
|
|
, commands(std::move(commands_))
|
2023-05-25 22:54:54 +00:00
|
|
|
, available_columns(std::move(available_columns_))
|
|
|
|
, settings(std::move(settings_))
|
2023-10-12 23:25:20 +00:00
|
|
|
, select_limits(SelectQueryOptions().analyze(!settings.can_execute).ignoreLimits())
|
2024-03-26 20:59:11 +00:00
|
|
|
, logger(getLogger("MutationsInterpreter(" + source.getStorage()->getStorageID().getFullTableName() + ")"))
|
2019-11-07 08:40:36 +00:00
|
|
|
{
|
2024-03-18 14:50:58 +00:00
|
|
|
auto new_context = Context::createCopy(context_);
|
|
|
|
if (new_context->getSettingsRef().allow_experimental_analyzer)
|
|
|
|
{
|
|
|
|
new_context->setSetting("allow_experimental_analyzer", false);
|
2024-03-26 12:26:42 +00:00
|
|
|
LOG_DEBUG(logger, "Will use old analyzer to prepare mutation");
|
2024-03-18 14:50:58 +00:00
|
|
|
}
|
|
|
|
context = std::move(new_context);
|
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
prepare(!settings.can_execute);
|
2019-11-07 08:40:36 +00:00
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
static NameSet getKeyColumns(const MutationsInterpreter::Source & source, const StorageMetadataPtr & metadata_snapshot)
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
2023-01-10 15:31:01 +00:00
|
|
|
const MergeTreeData * merge_tree_data = source.getMergeTreeData();
|
2019-05-03 02:00:57 +00:00
|
|
|
if (!merge_tree_data)
|
2018-09-07 15:44:51 +00:00
|
|
|
return {};
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
NameSet key_columns;
|
|
|
|
|
2020-06-17 10:34:23 +00:00
|
|
|
for (const String & col : metadata_snapshot->getColumnsRequiredForPartitionKey())
|
2020-05-20 12:16:55 +00:00
|
|
|
key_columns.insert(col);
|
2018-09-07 15:44:51 +00:00
|
|
|
|
2020-06-17 11:05:11 +00:00
|
|
|
for (const String & col : metadata_snapshot->getColumnsRequiredForSortingKey())
|
2020-05-20 18:11:38 +00:00
|
|
|
key_columns.insert(col);
|
2018-11-09 19:01:39 +00:00
|
|
|
/// We don't process sample_by_ast separately because it must be among the primary key columns.
|
2018-09-07 15:44:51 +00:00
|
|
|
|
|
|
|
if (!merge_tree_data->merging_params.sign_column.empty())
|
|
|
|
key_columns.insert(merge_tree_data->merging_params.sign_column);
|
|
|
|
|
2018-10-11 14:53:23 +00:00
|
|
|
if (!merge_tree_data->merging_params.version_column.empty())
|
|
|
|
key_columns.insert(merge_tree_data->merging_params.version_column);
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
return key_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void validateUpdateColumns(
|
2023-01-10 15:31:01 +00:00
|
|
|
const MutationsInterpreter::Source & source,
|
2024-02-28 19:58:55 +00:00
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
|
|
|
const NameSet & updated_columns,
|
|
|
|
const std::unordered_map<String, Names> & column_to_affected_materialized,
|
|
|
|
const ContextPtr & context)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
auto storage_snapshot = source.getStorageSnapshot(metadata_snapshot, context);
|
2023-01-10 15:31:01 +00:00
|
|
|
NameSet key_columns = getKeyColumns(source, metadata_snapshot);
|
2018-09-07 15:44:51 +00:00
|
|
|
|
2024-02-28 19:58:55 +00:00
|
|
|
const auto & storage_columns = storage_snapshot->metadata->getColumns();
|
|
|
|
const auto & virtual_columns = *storage_snapshot->virtual_columns;
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2024-02-28 19:58:55 +00:00
|
|
|
for (const auto & column_name : updated_columns)
|
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
if (key_columns.contains(column_name))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_UPDATE_COLUMN, "Cannot UPDATE key column {}", backQuote(column_name));
|
2018-09-07 15:44:51 +00:00
|
|
|
|
2024-02-28 19:58:55 +00:00
|
|
|
if (storage_columns.tryGetColumn(GetColumnsOptions::Materialized, column_name))
|
|
|
|
throw Exception(ErrorCodes::CANNOT_UPDATE_COLUMN, "Cannot UPDATE materialized column {}", backQuote(column_name));
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
auto materialized_it = column_to_affected_materialized.find(column_name);
|
|
|
|
if (materialized_it != column_to_affected_materialized.end())
|
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
for (const auto & materialized : materialized_it->second)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
if (key_columns.contains(materialized))
|
2024-02-28 19:58:55 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_UPDATE_COLUMN,
|
|
|
|
"Updated column {} affects MATERIALIZED column {}, which is a key column. "
|
|
|
|
"Cannot UPDATE it.", backQuote(column_name), backQuote(materialized));
|
2024-02-28 19:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!storage_columns.tryGetColumn(GetColumnsOptions::Ordinary, column_name))
|
|
|
|
{
|
|
|
|
/// Allow to override value of lightweight delete filter virtual column
|
|
|
|
if (column_name == RowExistsColumn::name)
|
|
|
|
{
|
|
|
|
if (!source.supportsLightweightDelete())
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Lightweight delete is not supported for table");
|
|
|
|
}
|
|
|
|
else if (virtual_columns.tryGet(column_name))
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Update is not supported for virtual column {} ", backQuote(column_name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::NO_SUCH_COLUMN_IN_TABLE, "There is no column {} in table", backQuote(column_name));
|
2018-09-04 13:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:19:22 +00:00
|
|
|
/// Returns ASTs of updated nested subcolumns, if all of subcolumns were updated.
|
|
|
|
/// They are used to validate sizes of nested arrays.
|
|
|
|
/// If some of subcolumns were updated and some weren't,
|
|
|
|
/// it makes sense to validate only updated columns with their old versions,
|
|
|
|
/// because their sizes couldn't change, since sizes of all nested subcolumns must be consistent.
|
|
|
|
static std::optional<std::vector<ASTPtr>> getExpressionsOfUpdatedNestedSubcolumns(
|
|
|
|
const String & column_name,
|
2024-04-18 21:40:27 +00:00
|
|
|
NameSet affected_materialized,
|
2021-04-16 14:19:22 +00:00
|
|
|
const NamesAndTypesList & all_columns,
|
|
|
|
const std::unordered_map<String, ASTPtr> & column_to_update_expression)
|
2021-04-02 11:31:33 +00:00
|
|
|
{
|
|
|
|
std::vector<ASTPtr> res;
|
2021-04-16 14:19:22 +00:00
|
|
|
auto source_name = Nested::splitName(column_name).first;
|
2021-04-02 11:31:33 +00:00
|
|
|
|
2021-04-13 09:31:49 +00:00
|
|
|
/// Check this nested subcolumn
|
2021-04-16 14:19:22 +00:00
|
|
|
for (const auto & column : all_columns)
|
2021-04-13 09:31:49 +00:00
|
|
|
{
|
2021-04-16 14:19:22 +00:00
|
|
|
auto split = Nested::splitName(column.name);
|
|
|
|
if (isArray(column.type) && split.first == source_name && !split.second.empty())
|
2021-04-02 11:31:33 +00:00
|
|
|
{
|
2024-04-18 21:40:27 +00:00
|
|
|
// Materialized nested columns shall never be part of the update expression
|
|
|
|
if (affected_materialized.contains(column.name))
|
2024-04-26 11:51:25 +00:00
|
|
|
continue;
|
2024-04-18 21:40:27 +00:00
|
|
|
|
2021-04-16 14:19:22 +00:00
|
|
|
auto it = column_to_update_expression.find(column.name);
|
|
|
|
if (it == column_to_update_expression.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
res.push_back(it->second);
|
2021-04-02 11:31:33 +00:00
|
|
|
}
|
2021-04-13 09:31:49 +00:00
|
|
|
}
|
2021-04-02 11:31:33 +00:00
|
|
|
|
2021-04-16 14:19:22 +00:00
|
|
|
return res;
|
2021-04-02 11:31:33 +00:00
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
void MutationsInterpreter::prepare(bool dry_run)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
if (is_prepared)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "MutationsInterpreter is already prepared. It is a bug.");
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
if (commands.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Empty mutation commands list");
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2023-05-15 16:28:12 +00:00
|
|
|
/// TODO Should we get columns, indices and projections from the part itself? Table metadata may be different
|
2020-06-17 09:38:47 +00:00
|
|
|
const ColumnsDescription & columns_desc = metadata_snapshot->getColumns();
|
|
|
|
const IndicesDescription & indices_desc = metadata_snapshot->getSecondaryIndices();
|
2021-02-10 14:12:49 +00:00
|
|
|
const ProjectionsDescription & projections_desc = metadata_snapshot->getProjections();
|
2023-05-25 22:54:54 +00:00
|
|
|
|
|
|
|
auto storage_snapshot = std::make_shared<StorageSnapshot>(*source.getStorage(), metadata_snapshot);
|
|
|
|
auto options = GetColumnsOptions(GetColumnsOptions::AllPhysical).withVirtuals();
|
|
|
|
|
|
|
|
auto all_columns = storage_snapshot->getColumnsByNames(options, available_columns);
|
|
|
|
NameSet available_columns_set(available_columns.begin(), available_columns.end());
|
2018-09-07 15:44:51 +00:00
|
|
|
|
2023-01-09 11:06:58 +00:00
|
|
|
/// Add _row_exists column if it is physically present in the part
|
2023-01-10 16:40:10 +00:00
|
|
|
if (source.hasLightweightDeleteMask())
|
2023-03-31 13:29:20 +00:00
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
all_columns.emplace_back(RowExistsColumn::name, RowExistsColumn::type);
|
|
|
|
available_columns_set.insert(RowExistsColumn::name);
|
2023-03-31 13:29:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
NameSet updated_columns;
|
2023-01-10 15:31:01 +00:00
|
|
|
bool materialize_ttl_recalculate_only = source.materializeTTLRecalculateOnly();
|
2021-12-13 14:21:05 +00:00
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
for (auto & command : commands)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2023-12-01 19:12:05 +00:00
|
|
|
if (command.type == MutationCommand::Type::APPLY_DELETED_MASK)
|
|
|
|
command = createCommandToApplyDeletedMask(command);
|
|
|
|
|
|
|
|
if (command.type == MutationCommand::Type::UPDATE || command.type == MutationCommand::Type::DELETE)
|
2021-08-04 14:16:13 +00:00
|
|
|
materialize_ttl_recalculate_only = false;
|
2021-08-05 07:33:02 +00:00
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
for (const auto & [name, _] : command.column_to_update_expression)
|
2020-01-15 13:00:08 +00:00
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
if (!available_columns_set.contains(name) && name != RowExistsColumn::name)
|
2023-05-25 22:54:54 +00:00
|
|
|
throw Exception(ErrorCodes::THERE_IS_NO_COLUMN,
|
|
|
|
"Column {} is updated but not requested to read", name);
|
|
|
|
|
|
|
|
updated_columns.insert(name);
|
2020-01-15 13:00:08 +00:00
|
|
|
}
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/// We need to know which columns affect which MATERIALIZED columns, data skipping indices
|
|
|
|
/// and projections to recalculate them if dependencies are updated.
|
2018-09-07 15:44:51 +00:00
|
|
|
std::unordered_map<String, Names> column_to_affected_materialized;
|
|
|
|
if (!updated_columns.empty())
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
for (const auto & column : columns_desc)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
if (column.default_desc.kind == ColumnDefaultKind::Materialized && available_columns_set.contains(column.name))
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
auto query = column.default_desc.expression->clone();
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(query, all_columns);
|
2023-05-25 22:54:54 +00:00
|
|
|
for (const auto & dependency : syntax_result->requiredSourceColumns())
|
2022-04-18 10:18:43 +00:00
|
|
|
if (updated_columns.contains(dependency))
|
2019-03-14 15:20:51 +00:00
|
|
|
column_to_affected_materialized[dependency].push_back(column.name);
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2024-02-28 19:58:55 +00:00
|
|
|
validateUpdateColumns(source, metadata_snapshot, updated_columns, column_to_affected_materialized, context);
|
2019-01-09 15:44:20 +00:00
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2023-07-24 14:12:01 +00:00
|
|
|
StorageInMemoryMetadata::HasDependencyCallback has_dependency =
|
|
|
|
[&](const String & name, ColumnDependency::Kind kind)
|
|
|
|
{
|
|
|
|
if (kind == ColumnDependency::PROJECTION)
|
|
|
|
return source.hasProjection(name);
|
|
|
|
|
|
|
|
if (kind == ColumnDependency::SKIP_INDEX)
|
|
|
|
return source.hasSecondaryIndex(name);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2023-05-22 15:25:59 +00:00
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
if (settings.recalculate_dependencies_of_updated_columns)
|
2023-07-24 14:12:01 +00:00
|
|
|
dependencies = getAllColumnDependencies(metadata_snapshot, updated_columns, has_dependency);
|
2023-05-22 15:25:59 +00:00
|
|
|
|
2023-10-03 01:27:45 +00:00
|
|
|
bool need_rebuild_indexes = false;
|
|
|
|
bool need_rebuild_projections = false;
|
2022-09-09 02:22:35 +00:00
|
|
|
std::vector<String> read_columns;
|
2023-07-24 14:12:01 +00:00
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
/// First, break a sequence of commands into stages.
|
2023-12-01 19:12:05 +00:00
|
|
|
for (const auto & command : commands)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2022-08-31 14:33:53 +00:00
|
|
|
// we can return deleted rows only if it's the only present command
|
2023-05-25 22:54:54 +00:00
|
|
|
assert(command.type == MutationCommand::DELETE || command.type == MutationCommand::UPDATE || !settings.return_mutated_rows);
|
2022-08-31 14:33:53 +00:00
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
if (command.type == MutationCommand::DELETE)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2021-02-10 14:12:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_OTHER);
|
2019-08-05 18:06:05 +00:00
|
|
|
if (stages.empty() || !stages.back().column_to_updated.empty())
|
|
|
|
stages.emplace_back(context);
|
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
auto predicate = getPartitionAndPredicateExpressionForMutationCommand(command);
|
2022-08-31 13:08:27 +00:00
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
if (!settings.return_mutated_rows)
|
2022-08-31 13:08:27 +00:00
|
|
|
predicate = makeASTFunction("isZeroOrNull", predicate);
|
|
|
|
|
|
|
|
stages.back().filters.push_back(predicate);
|
2023-10-05 07:49:17 +00:00
|
|
|
/// ALTER DELETE can changes number of rows in the part, so we need to rebuild indexes and projection
|
2023-10-03 01:27:45 +00:00
|
|
|
need_rebuild_indexes = true;
|
|
|
|
need_rebuild_projections = true;
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
else if (command.type == MutationCommand::UPDATE)
|
|
|
|
{
|
2021-02-10 14:12:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_OTHER);
|
2019-08-05 18:06:05 +00:00
|
|
|
if (stages.empty() || !stages.back().column_to_updated.empty())
|
|
|
|
stages.emplace_back(context);
|
2018-09-07 15:44:51 +00:00
|
|
|
if (stages.size() == 1) /// First stage only supports filtering and can't update columns.
|
2018-09-07 19:14:05 +00:00
|
|
|
stages.emplace_back(context);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
NameSet affected_materialized;
|
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
for (const auto & [column_name, update_expr] : command.column_to_update_expression)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2023-12-01 19:12:05 +00:00
|
|
|
auto materialized_it = column_to_affected_materialized.find(column_name);
|
2018-09-07 15:44:51 +00:00
|
|
|
if (materialized_it != column_to_affected_materialized.end())
|
2023-12-01 19:12:05 +00:00
|
|
|
for (const auto & mat_column : materialized_it->second)
|
2018-09-07 15:44:51 +00:00
|
|
|
affected_materialized.emplace(mat_column);
|
2024-04-18 21:40:27 +00:00
|
|
|
}
|
2018-09-07 15:44:51 +00:00
|
|
|
|
2024-04-18 21:40:27 +00:00
|
|
|
for (const auto & [column_name, update_expr] : command.column_to_update_expression)
|
|
|
|
{
|
2020-07-21 13:31:59 +00:00
|
|
|
/// When doing UPDATE column = expression WHERE condition
|
|
|
|
/// we will replace column to the result of the following expression:
|
|
|
|
///
|
|
|
|
/// CAST(if(condition, CAST(expression, type), column), type)
|
|
|
|
///
|
|
|
|
/// Inner CAST is needed to make 'if' work when branches have no common type,
|
|
|
|
/// example: type is UInt64, UPDATE x = -1 or UPDATE x = x - 1.
|
|
|
|
///
|
|
|
|
/// Outer CAST is added just in case if we don't trust the returning type of 'if'.
|
|
|
|
|
2022-07-17 18:41:17 +00:00
|
|
|
DataTypePtr type;
|
2023-12-01 19:12:05 +00:00
|
|
|
if (auto physical_column = columns_desc.tryGetPhysical(column_name))
|
|
|
|
{
|
2022-07-17 18:41:17 +00:00
|
|
|
type = physical_column->type;
|
2023-12-01 19:12:05 +00:00
|
|
|
}
|
2024-02-28 19:58:55 +00:00
|
|
|
else if (column_name == RowExistsColumn::name)
|
2023-12-01 19:12:05 +00:00
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
type = RowExistsColumn::type;
|
2023-12-01 19:12:05 +00:00
|
|
|
deleted_mask_updated = true;
|
|
|
|
}
|
2022-07-17 18:41:17 +00:00
|
|
|
else
|
2023-12-01 19:12:05 +00:00
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown column {}", column_name);
|
|
|
|
}
|
2022-07-17 18:41:17 +00:00
|
|
|
|
2021-04-16 14:19:22 +00:00
|
|
|
auto type_literal = std::make_shared<ASTLiteral>(type->getName());
|
2021-04-02 11:31:33 +00:00
|
|
|
ASTPtr condition = getPartitionAndPredicateExpressionForMutationCommand(command);
|
|
|
|
|
|
|
|
/// And new check validateNestedArraySizes for Nested subcolumns
|
2023-12-01 19:12:05 +00:00
|
|
|
if (isArray(type) && !Nested::splitName(column_name).second.empty())
|
2021-04-02 11:31:33 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<ASTFunction> function = nullptr;
|
|
|
|
|
2024-04-18 21:40:27 +00:00
|
|
|
auto nested_update_exprs = getExpressionsOfUpdatedNestedSubcolumns(column_name, affected_materialized, all_columns, command.column_to_update_expression);
|
2021-04-16 14:19:22 +00:00
|
|
|
if (!nested_update_exprs)
|
2021-04-02 11:31:33 +00:00
|
|
|
{
|
|
|
|
function = makeASTFunction("validateNestedArraySizes",
|
|
|
|
condition,
|
|
|
|
update_expr->clone(),
|
2023-12-01 19:12:05 +00:00
|
|
|
std::make_shared<ASTIdentifier>(column_name));
|
2021-04-07 02:39:01 +00:00
|
|
|
condition = makeASTFunction("and", condition, function);
|
2021-04-02 11:31:33 +00:00
|
|
|
}
|
2021-04-16 14:19:22 +00:00
|
|
|
else if (nested_update_exprs->size() > 1)
|
2021-04-02 11:31:33 +00:00
|
|
|
{
|
|
|
|
function = std::make_shared<ASTFunction>();
|
|
|
|
function->name = "validateNestedArraySizes";
|
|
|
|
function->arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
function->children.push_back(function->arguments);
|
|
|
|
function->arguments->children.push_back(condition);
|
2021-04-16 14:19:22 +00:00
|
|
|
for (const auto & it : *nested_update_exprs)
|
2021-04-02 11:31:33 +00:00
|
|
|
function->arguments->children.push_back(it->clone());
|
2021-04-07 02:39:01 +00:00
|
|
|
condition = makeASTFunction("and", condition, function);
|
2021-04-02 11:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:11:40 +00:00
|
|
|
auto updated_column = makeASTFunction("_CAST",
|
2018-09-07 15:44:51 +00:00
|
|
|
makeASTFunction("if",
|
2021-04-02 11:31:33 +00:00
|
|
|
condition,
|
2021-08-07 08:11:40 +00:00
|
|
|
makeASTFunction("_CAST",
|
2020-07-21 13:31:59 +00:00
|
|
|
update_expr->clone(),
|
|
|
|
type_literal),
|
2023-12-01 19:12:05 +00:00
|
|
|
std::make_shared<ASTIdentifier>(column_name)),
|
2020-07-21 13:31:59 +00:00
|
|
|
type_literal);
|
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
stages.back().column_to_updated.emplace(column_name, updated_column);
|
2023-04-05 16:07:59 +00:00
|
|
|
|
2023-05-25 22:54:54 +00:00
|
|
|
if (condition && settings.return_mutated_rows)
|
2023-04-05 16:07:59 +00:00
|
|
|
stages.back().filters.push_back(condition);
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!affected_materialized.empty())
|
|
|
|
{
|
2018-09-07 19:14:05 +00:00
|
|
|
stages.emplace_back(context);
|
2019-03-14 15:20:51 +00:00
|
|
|
for (const auto & column : columns_desc)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.default_desc.kind == ColumnDefaultKind::Materialized)
|
|
|
|
{
|
2023-11-23 14:51:36 +00:00
|
|
|
auto type_literal = std::make_shared<ASTLiteral>(column.type->getName());
|
|
|
|
|
|
|
|
auto materialized_column = makeASTFunction("_CAST",
|
|
|
|
column.default_desc.expression->clone(),
|
|
|
|
type_literal);
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
stages.back().column_to_updated.emplace(
|
|
|
|
column.name,
|
2023-11-23 14:51:36 +00:00
|
|
|
materialized_column);
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 07:36:57 +00:00
|
|
|
|
2023-10-05 07:49:17 +00:00
|
|
|
/// If the part is compact and adaptive index granularity is enabled, modify data in one column via ALTER UPDATE can change
|
|
|
|
/// the part granularity, so we need to rebuild indexes
|
2023-10-02 07:36:57 +00:00
|
|
|
if (source.isCompactPart() && source.getMergeTreeData() && source.getMergeTreeData()->getSettings()->index_granularity_bytes > 0)
|
2023-10-03 01:27:45 +00:00
|
|
|
need_rebuild_indexes = true;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
2021-07-31 18:17:06 +00:00
|
|
|
else if (command.type == MutationCommand::MATERIALIZE_COLUMN)
|
|
|
|
{
|
2021-07-31 19:36:43 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_OTHER);
|
|
|
|
if (stages.empty() || !stages.back().column_to_updated.empty())
|
|
|
|
stages.emplace_back(context);
|
|
|
|
if (stages.size() == 1) /// First stage only supports filtering and can't update columns.
|
|
|
|
stages.emplace_back(context);
|
|
|
|
|
|
|
|
const auto & column = columns_desc.get(command.column_name);
|
2021-12-09 15:58:28 +00:00
|
|
|
|
|
|
|
if (!column.default_desc.expression)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Cannot materialize column `{}` because it doesn't have default expression", column.name);
|
|
|
|
|
2021-12-07 13:42:16 +00:00
|
|
|
auto materialized_column = makeASTFunction(
|
|
|
|
"_CAST", column.default_desc.expression->clone(), std::make_shared<ASTLiteral>(column.type->getName()));
|
|
|
|
|
|
|
|
stages.back().column_to_updated.emplace(column.name, materialized_column);
|
2021-07-31 18:17:06 +00:00
|
|
|
}
|
2019-05-05 17:01:54 +00:00
|
|
|
else if (command.type == MutationCommand::MATERIALIZE_INDEX)
|
|
|
|
{
|
2023-08-09 20:57:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2019-05-05 17:01:54 +00:00
|
|
|
auto it = std::find_if(
|
2020-05-28 12:47:17 +00:00
|
|
|
std::cbegin(indices_desc), std::end(indices_desc),
|
2020-05-28 13:09:03 +00:00
|
|
|
[&](const IndexDescription & index)
|
2019-05-05 17:01:54 +00:00
|
|
|
{
|
2020-05-28 12:37:05 +00:00
|
|
|
return index.name == command.index_name;
|
2019-05-05 17:01:54 +00:00
|
|
|
});
|
2020-05-28 12:47:17 +00:00
|
|
|
if (it == std::cend(indices_desc))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unknown index: {}", command.index_name);
|
2019-05-05 17:01:54 +00:00
|
|
|
|
2023-07-24 14:12:01 +00:00
|
|
|
if (!source.hasSecondaryIndex(it->name))
|
2023-05-22 15:25:59 +00:00
|
|
|
{
|
|
|
|
auto query = (*it).expression_list_ast->clone();
|
|
|
|
auto syntax_result = TreeRewriter(context).analyze(query, all_columns);
|
|
|
|
const auto required_columns = syntax_result->requiredSourceColumns();
|
|
|
|
for (const auto & column : required_columns)
|
|
|
|
dependencies.emplace(column, ColumnDependency::SKIP_INDEX);
|
|
|
|
materialized_indices.emplace(command.index_name);
|
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
}
|
2023-08-09 20:57:49 +00:00
|
|
|
else if (command.type == MutationCommand::MATERIALIZE_STATISTIC)
|
|
|
|
{
|
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2023-09-08 00:27:17 +00:00
|
|
|
for (const auto & stat_column_name: command.statistic_columns)
|
|
|
|
{
|
2023-09-26 17:16:01 +00:00
|
|
|
if (!columns_desc.has(stat_column_name) || !columns_desc.get(stat_column_name).stat)
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_STATISTIC, "Unknown statistic column: {}", stat_column_name);
|
|
|
|
dependencies.emplace(stat_column_name, ColumnDependency::STATISTIC);
|
2023-09-08 00:27:17 +00:00
|
|
|
materialized_statistics.emplace(stat_column_name);
|
|
|
|
}
|
2023-08-09 20:57:49 +00:00
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
else if (command.type == MutationCommand::MATERIALIZE_PROJECTION)
|
|
|
|
{
|
2023-08-09 20:57:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2021-02-10 14:12:49 +00:00
|
|
|
const auto & projection = projections_desc.get(command.projection_name);
|
2024-02-27 15:06:14 +00:00
|
|
|
if (!source.hasProjection(projection.name) || source.hasBrokenProjection(projection.name))
|
2023-05-22 15:25:59 +00:00
|
|
|
{
|
|
|
|
for (const auto & column : projection.required_columns)
|
|
|
|
dependencies.emplace(column, ColumnDependency::PROJECTION);
|
|
|
|
materialized_projections.emplace(command.projection_name);
|
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
}
|
|
|
|
else if (command.type == MutationCommand::DROP_INDEX)
|
|
|
|
{
|
2023-08-09 20:57:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2021-02-10 14:12:49 +00:00
|
|
|
materialized_indices.erase(command.index_name);
|
|
|
|
}
|
2023-08-09 20:57:49 +00:00
|
|
|
else if (command.type == MutationCommand::DROP_STATISTIC)
|
|
|
|
{
|
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2023-09-08 00:27:17 +00:00
|
|
|
for (const auto & stat_column_name: command.statistic_columns)
|
|
|
|
materialized_statistics.erase(stat_column_name);
|
2023-08-09 20:57:49 +00:00
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
else if (command.type == MutationCommand::DROP_PROJECTION)
|
|
|
|
{
|
2023-08-09 20:57:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION);
|
2021-02-10 14:12:49 +00:00
|
|
|
materialized_projections.erase(command.projection_name);
|
2019-05-05 17:01:54 +00:00
|
|
|
}
|
2020-01-22 13:24:20 +00:00
|
|
|
else if (command.type == MutationCommand::MATERIALIZE_TTL)
|
|
|
|
{
|
2021-02-10 14:12:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_OTHER);
|
2021-08-04 14:16:13 +00:00
|
|
|
if (materialize_ttl_recalculate_only)
|
|
|
|
{
|
|
|
|
// just recalculate ttl_infos without remove expired data
|
|
|
|
auto all_columns_vec = all_columns.getNames();
|
2023-08-08 14:04:46 +00:00
|
|
|
auto all_columns_set = NameSet(all_columns_vec.begin(), all_columns_vec.end());
|
|
|
|
auto new_dependencies = metadata_snapshot->getColumnDependencies(all_columns_set, false, has_dependency);
|
|
|
|
|
2021-08-04 14:16:13 +00:00
|
|
|
for (const auto & dependency : new_dependencies)
|
|
|
|
{
|
|
|
|
if (dependency.kind == ColumnDependency::TTL_EXPRESSION)
|
|
|
|
dependencies.insert(dependency);
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 14:21:05 +00:00
|
|
|
else if (metadata_snapshot->hasRowsTTL()
|
|
|
|
|| metadata_snapshot->hasAnyRowsWhereTTL()
|
|
|
|
|| metadata_snapshot->hasAnyGroupByTTL())
|
2020-02-18 19:03:40 +00:00
|
|
|
{
|
|
|
|
for (const auto & column : all_columns)
|
|
|
|
dependencies.emplace(column.name, ColumnDependency::TTL_TARGET);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NameSet new_updated_columns;
|
2020-06-17 16:39:58 +00:00
|
|
|
auto column_ttls = metadata_snapshot->getColumns().getColumnTTLs();
|
2020-02-18 19:03:40 +00:00
|
|
|
for (const auto & elem : column_ttls)
|
|
|
|
{
|
|
|
|
dependencies.emplace(elem.first, ColumnDependency::TTL_TARGET);
|
|
|
|
new_updated_columns.insert(elem.first);
|
|
|
|
}
|
2020-01-22 13:24:20 +00:00
|
|
|
|
2020-02-18 19:03:40 +00:00
|
|
|
auto all_columns_vec = all_columns.getNames();
|
2023-08-08 14:04:46 +00:00
|
|
|
auto all_columns_set = NameSet(all_columns_vec.begin(), all_columns_vec.end());
|
|
|
|
auto all_dependencies = getAllColumnDependencies(metadata_snapshot, all_columns_set, has_dependency);
|
2020-01-22 13:24:20 +00:00
|
|
|
|
2020-02-18 19:03:40 +00:00
|
|
|
for (const auto & dependency : all_dependencies)
|
|
|
|
{
|
|
|
|
if (dependency.kind == ColumnDependency::TTL_EXPRESSION)
|
|
|
|
dependencies.insert(dependency);
|
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/// Recalc only skip indices and projections of columns which could be updated by TTL.
|
2023-07-24 14:12:01 +00:00
|
|
|
auto new_dependencies = metadata_snapshot->getColumnDependencies(new_updated_columns, true, has_dependency);
|
2020-02-18 19:03:40 +00:00
|
|
|
for (const auto & dependency : new_dependencies)
|
|
|
|
{
|
2023-08-09 20:57:49 +00:00
|
|
|
if (dependency.kind == ColumnDependency::SKIP_INDEX
|
|
|
|
|| dependency.kind == ColumnDependency::PROJECTION
|
|
|
|
|| dependency.kind == ColumnDependency::STATISTIC)
|
2020-02-18 19:03:40 +00:00
|
|
|
dependencies.insert(dependency);
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 17:18:12 +00:00
|
|
|
|
2021-07-30 04:27:15 +00:00
|
|
|
if (dependencies.empty())
|
|
|
|
{
|
|
|
|
/// Very rare case. It can happen if we have only one MOVE TTL with constant expression.
|
|
|
|
/// But we still have to read at least one column.
|
|
|
|
dependencies.emplace(all_columns.front().name, ColumnDependency::TTL_EXPRESSION);
|
2020-02-18 19:03:40 +00:00
|
|
|
}
|
2019-05-05 17:01:54 +00:00
|
|
|
}
|
2020-01-17 13:54:22 +00:00
|
|
|
else if (command.type == MutationCommand::READ_COLUMN)
|
2020-01-13 16:39:20 +00:00
|
|
|
{
|
2021-02-10 14:12:49 +00:00
|
|
|
mutation_kind.set(MutationKind::MUTATE_OTHER);
|
2022-09-09 02:22:35 +00:00
|
|
|
read_columns.emplace_back(command.column_name);
|
2020-01-13 16:39:20 +00:00
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_MUTATION_COMMAND, "Unknown mutation command type: {}", DB::toString<int>(command.type));
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:22:35 +00:00
|
|
|
if (!read_columns.empty())
|
|
|
|
{
|
|
|
|
if (stages.empty() || !stages.back().column_to_updated.empty())
|
|
|
|
stages.emplace_back(context);
|
|
|
|
if (stages.size() == 1) /// First stage only supports filtering and can't update columns.
|
|
|
|
stages.emplace_back(context);
|
|
|
|
|
|
|
|
for (auto & column_name : read_columns)
|
|
|
|
stages.back().column_to_updated.emplace(column_name, std::make_shared<ASTIdentifier>(column_name));
|
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/// We care about affected indices and projections because we also need to rewrite them
|
2020-02-17 20:39:24 +00:00
|
|
|
/// when one of index columns updated or filtered with delete.
|
2020-08-08 01:01:47 +00:00
|
|
|
/// The same about columns, that are needed for calculation of TTL expressions.
|
2023-06-04 17:48:13 +00:00
|
|
|
NameSet changed_columns;
|
|
|
|
NameSet unchanged_columns;
|
2020-02-17 20:39:24 +00:00
|
|
|
if (!dependencies.empty())
|
2019-04-17 17:07:07 +00:00
|
|
|
{
|
2020-02-18 19:03:40 +00:00
|
|
|
for (const auto & dependency : dependencies)
|
2019-08-05 18:06:05 +00:00
|
|
|
{
|
2020-02-18 19:03:40 +00:00
|
|
|
if (dependency.isReadOnly())
|
|
|
|
unchanged_columns.insert(dependency.column_name);
|
|
|
|
else
|
|
|
|
changed_columns.insert(dependency.column_name);
|
|
|
|
}
|
2019-09-03 05:48:02 +00:00
|
|
|
|
2020-02-18 19:03:40 +00:00
|
|
|
if (!changed_columns.empty())
|
|
|
|
{
|
|
|
|
if (stages.empty() || !stages.back().column_to_updated.empty())
|
|
|
|
stages.emplace_back(context);
|
|
|
|
if (stages.size() == 1) /// First stage only supports filtering and can't update columns.
|
|
|
|
stages.emplace_back(context);
|
2019-09-03 05:48:02 +00:00
|
|
|
|
2020-02-18 19:03:40 +00:00
|
|
|
for (const auto & column : changed_columns)
|
|
|
|
stages.back().column_to_updated.emplace(
|
|
|
|
column, std::make_shared<ASTIdentifier>(column));
|
2019-08-05 18:06:05 +00:00
|
|
|
}
|
2020-02-18 19:03:40 +00:00
|
|
|
|
|
|
|
if (!unchanged_columns.empty())
|
2020-01-17 14:39:18 +00:00
|
|
|
{
|
2020-02-18 19:03:40 +00:00
|
|
|
if (!stages.empty())
|
|
|
|
{
|
|
|
|
std::vector<Stage> stages_copy;
|
|
|
|
/// Copy all filled stages except index calculation stage.
|
|
|
|
for (const auto & stage : stages)
|
|
|
|
{
|
|
|
|
stages_copy.emplace_back(context);
|
|
|
|
stages_copy.back().column_to_updated = stage.column_to_updated;
|
|
|
|
stages_copy.back().output_columns = stage.output_columns;
|
|
|
|
stages_copy.back().filters = stage.filters;
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
prepareMutationStages(stages_copy, true);
|
2020-02-18 19:03:40 +00:00
|
|
|
|
2020-09-15 17:13:13 +00:00
|
|
|
QueryPlan plan;
|
2023-01-10 15:31:01 +00:00
|
|
|
initQueryPlan(stages_copy.front(), plan);
|
2020-09-15 17:13:13 +00:00
|
|
|
auto pipeline = addStreamsForLaterStages(stages_copy, plan);
|
2022-05-23 13:46:57 +00:00
|
|
|
updated_header = std::make_unique<Block>(pipeline.getHeader());
|
2020-02-18 19:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
/// Special step to recalculate affected indices, projections and TTL expressions.
|
2020-02-18 19:03:40 +00:00
|
|
|
stages.emplace_back(context);
|
|
|
|
for (const auto & column : unchanged_columns)
|
2020-02-17 20:39:24 +00:00
|
|
|
stages.back().column_to_updated.emplace(
|
2019-04-18 15:41:07 +00:00
|
|
|
column, std::make_shared<ASTIdentifier>(column));
|
2020-01-17 14:39:18 +00:00
|
|
|
}
|
2019-04-17 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 17:48:13 +00:00
|
|
|
for (const auto & index : metadata_snapshot->getSecondaryIndices())
|
|
|
|
{
|
2023-07-24 14:12:01 +00:00
|
|
|
if (!source.hasSecondaryIndex(index.name))
|
|
|
|
continue;
|
|
|
|
|
2023-10-03 01:27:45 +00:00
|
|
|
if (need_rebuild_indexes)
|
2023-06-04 17:48:13 +00:00
|
|
|
{
|
2023-07-24 14:12:01 +00:00
|
|
|
materialized_indices.insert(index.name);
|
|
|
|
continue;
|
2023-06-04 17:48:13 +00:00
|
|
|
}
|
2023-07-24 14:12:01 +00:00
|
|
|
|
|
|
|
const auto & index_cols = index.expression->getRequiredColumns();
|
|
|
|
bool changed = std::any_of(
|
|
|
|
index_cols.begin(),
|
|
|
|
index_cols.end(),
|
|
|
|
[&](const auto & col) { return updated_columns.contains(col) || changed_columns.contains(col); });
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
materialized_indices.insert(index.name);
|
2023-06-04 17:48:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:35:54 +00:00
|
|
|
for (const auto & projection : metadata_snapshot->getProjections())
|
|
|
|
{
|
2023-11-16 15:43:34 +00:00
|
|
|
if (!source.hasProjection(projection.name))
|
2023-11-14 10:35:54 +00:00
|
|
|
continue;
|
|
|
|
|
2024-02-27 15:06:14 +00:00
|
|
|
/// Always rebuild broken projections.
|
|
|
|
if (source.hasBrokenProjection(projection.name))
|
|
|
|
{
|
2024-03-26 12:26:42 +00:00
|
|
|
LOG_DEBUG(logger, "Will rebuild broken projection {}", projection.name);
|
2024-02-27 15:06:14 +00:00
|
|
|
materialized_projections.insert(projection.name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-03 01:27:45 +00:00
|
|
|
if (need_rebuild_projections)
|
2023-06-04 17:48:13 +00:00
|
|
|
{
|
2023-07-24 14:12:01 +00:00
|
|
|
materialized_projections.insert(projection.name);
|
|
|
|
continue;
|
2023-06-04 17:48:13 +00:00
|
|
|
}
|
2023-07-24 14:12:01 +00:00
|
|
|
|
|
|
|
const auto & projection_cols = projection.required_columns;
|
|
|
|
bool changed = std::any_of(
|
|
|
|
projection_cols.begin(),
|
|
|
|
projection_cols.end(),
|
|
|
|
[&](const auto & col) { return updated_columns.contains(col) || changed_columns.contains(col); });
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
materialized_projections.insert(projection.name);
|
2023-06-04 17:48:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 12:46:50 +00:00
|
|
|
/// Stages might be empty when we materialize skip indices or projections which don't add any
|
|
|
|
/// column dependencies.
|
|
|
|
if (stages.empty())
|
|
|
|
stages.emplace_back(context);
|
|
|
|
|
2019-08-05 18:06:05 +00:00
|
|
|
is_prepared = true;
|
2023-01-10 15:31:01 +00:00
|
|
|
prepareMutationStages(stages, dry_run);
|
2019-08-05 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
void MutationsInterpreter::prepareMutationStages(std::vector<Stage> & prepared_stages, bool dry_run)
|
2019-08-05 18:06:05 +00:00
|
|
|
{
|
2023-01-10 15:31:01 +00:00
|
|
|
auto storage_snapshot = source.getStorageSnapshot(metadata_snapshot, context);
|
2023-05-25 22:54:54 +00:00
|
|
|
auto options = GetColumnsOptions(GetColumnsOptions::AllPhysical).withExtendedObjects().withVirtuals();
|
|
|
|
|
|
|
|
auto all_columns = storage_snapshot->getColumnsByNames(options, available_columns);
|
2020-01-15 13:00:08 +00:00
|
|
|
|
2022-07-21 19:50:19 +00:00
|
|
|
/// Add _row_exists column if it is present in the part
|
2023-12-05 13:38:25 +00:00
|
|
|
if (source.hasLightweightDeleteMask() || deleted_mask_updated)
|
2024-02-28 19:58:55 +00:00
|
|
|
all_columns.emplace_back(RowExistsColumn::name, RowExistsColumn::type);
|
2022-07-21 07:32:29 +00:00
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
bool has_filters = false;
|
2018-09-03 13:36:58 +00:00
|
|
|
/// Next, for each stage calculate columns changed by this and previous stages.
|
2019-08-05 18:22:44 +00:00
|
|
|
for (size_t i = 0; i < prepared_stages.size(); ++i)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
if (settings.return_all_columns || !prepared_stages[i].filters.empty())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
for (const auto & column : all_columns)
|
2023-12-01 19:12:05 +00:00
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
if (column.name == RowExistsColumn::name && !deleted_mask_updated)
|
2023-12-01 19:12:05 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-05 18:22:44 +00:00
|
|
|
prepared_stages[i].output_columns.insert(column.name);
|
2023-12-01 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
has_filters = true;
|
2023-12-04 13:00:50 +00:00
|
|
|
settings.apply_deleted_mask = true;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
2023-12-01 19:12:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
prepared_stages[i].output_columns = prepared_stages[i - 1].output_columns;
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
/// Make sure that all updated columns are included into output_columns set.
|
|
|
|
/// This is important for a "hidden" column like _row_exists gets because it is a virtual column
|
|
|
|
/// and so it is not in the list of AllPhysical columns.
|
|
|
|
for (const auto & [column_name, _] : prepared_stages[i].column_to_updated)
|
|
|
|
{
|
2024-02-28 19:58:55 +00:00
|
|
|
if (column_name == RowExistsColumn::name && has_filters && !deleted_mask_updated)
|
2023-12-01 19:12:05 +00:00
|
|
|
continue;
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2023-12-01 19:12:05 +00:00
|
|
|
prepared_stages[i].output_columns.insert(column_name);
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, calculate `expressions_chain` for each stage except the first.
|
|
|
|
/// Do it backwards to propagate information about columns required as input for a stage to the previous stage.
|
2023-01-10 15:31:01 +00:00
|
|
|
for (int64_t i = prepared_stages.size() - 1; i >= 0; --i)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2019-08-05 18:22:44 +00:00
|
|
|
auto & stage = prepared_stages[i];
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
ASTPtr all_asts = std::make_shared<ASTExpressionList>();
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & ast : stage.filters)
|
|
|
|
all_asts->children.push_back(ast);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & kv : stage.column_to_updated)
|
|
|
|
all_asts->children.push_back(kv.second);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
/// Add all output columns to prevent ExpressionAnalyzer from deleting them from source columns.
|
2023-12-01 19:12:05 +00:00
|
|
|
for (const auto & column : stage.output_columns)
|
2018-09-03 13:36:58 +00:00
|
|
|
all_asts->children.push_back(std::make_shared<ASTIdentifier>(column));
|
|
|
|
|
2021-10-22 09:47:28 +00:00
|
|
|
/// Executing scalar subquery on that stage can lead to deadlock
|
|
|
|
/// e.g. ALTER referencing the same table in scalar subquery
|
|
|
|
bool execute_scalar_subqueries = !dry_run;
|
2021-10-21 15:03:16 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(
|
2023-01-10 15:31:01 +00:00
|
|
|
all_asts, all_columns, source.getStorage(), storage_snapshot,
|
2021-11-09 12:36:25 +00:00
|
|
|
false, true, execute_scalar_subqueries);
|
|
|
|
|
2018-11-08 17:28:52 +00:00
|
|
|
stage.analyzer = std::make_unique<ExpressionAnalyzer>(all_asts, syntax_result, context);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
ExpressionActionsChain & actions_chain = stage.expressions_chain;
|
|
|
|
|
2023-04-06 19:07:53 +00:00
|
|
|
if (!stage.filters.empty())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2023-04-06 19:07:53 +00:00
|
|
|
auto ast = stage.filters.front();
|
|
|
|
if (stage.filters.size() > 1)
|
|
|
|
ast = makeASTForLogicalAnd(std::move(stage.filters));
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
if (!actions_chain.steps.empty())
|
|
|
|
actions_chain.addStep();
|
2023-04-06 19:07:53 +00:00
|
|
|
|
2018-09-04 13:45:39 +00:00
|
|
|
stage.analyzer->appendExpression(actions_chain, ast, dry_run);
|
2018-09-07 15:44:51 +00:00
|
|
|
stage.filter_column_names.push_back(ast->getColumnName());
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (!stage.column_to_updated.empty())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
if (!actions_chain.steps.empty())
|
|
|
|
actions_chain.addStep();
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & kv : stage.column_to_updated)
|
2018-09-04 13:45:39 +00:00
|
|
|
stage.analyzer->appendExpression(actions_chain, kv.second, dry_run);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2021-03-10 08:41:24 +00:00
|
|
|
auto & actions = actions_chain.getLastStep().actions();
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & kv : stage.column_to_updated)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2021-03-03 20:01:07 +00:00
|
|
|
auto column_name = kv.second->getColumnName();
|
2022-08-08 15:54:51 +00:00
|
|
|
const auto & dag_node = actions->findInOutputs(column_name);
|
2021-03-11 17:03:39 +00:00
|
|
|
const auto & alias = actions->addAlias(dag_node, kv.first);
|
2022-08-08 15:54:51 +00:00
|
|
|
actions->addOrReplaceInOutputs(alias);
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
if (i == 0 && actions_chain.steps.empty())
|
|
|
|
actions_chain.lastStep(syntax_result->required_source_columns);
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
/// Remove all intermediate columns.
|
|
|
|
actions_chain.addStep();
|
2021-03-10 08:41:24 +00:00
|
|
|
actions_chain.getLastStep().required_output.clear();
|
|
|
|
ActionsDAG::NodeRawConstPtrs new_index;
|
2021-03-05 13:56:44 +00:00
|
|
|
for (const auto & name : stage.output_columns)
|
|
|
|
actions_chain.getLastStep().addRequiredOutput(name);
|
2021-03-10 08:41:24 +00:00
|
|
|
|
2020-09-13 13:51:31 +00:00
|
|
|
actions_chain.getLastActions();
|
2018-09-03 13:36:58 +00:00
|
|
|
actions_chain.finalize();
|
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
/// Propagate information about columns needed as input.
|
|
|
|
for (const auto & column : actions_chain.steps.front()->getRequiredColumns())
|
|
|
|
prepared_stages[i - 1].output_columns.insert(column.name);
|
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
2023-01-10 15:31:01 +00:00
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
void MutationsInterpreter::Source::read(
|
|
|
|
Stage & first_stage,
|
|
|
|
QueryPlan & plan,
|
|
|
|
const StorageMetadataPtr & snapshot_,
|
|
|
|
const ContextPtr & context_,
|
|
|
|
bool apply_deleted_mask_,
|
|
|
|
bool can_execute_) const
|
|
|
|
{
|
|
|
|
auto required_columns = first_stage.expressions_chain.steps.front()->getRequiredColumns().getNames();
|
|
|
|
auto storage_snapshot = getStorageSnapshot(snapshot_, context_);
|
|
|
|
|
|
|
|
if (!can_execute_)
|
|
|
|
{
|
|
|
|
auto header = storage_snapshot->getSampleBlockForColumns(required_columns);
|
|
|
|
auto callback = []()
|
|
|
|
{
|
|
|
|
return DB::Exception(ErrorCodes::LOGICAL_ERROR, "Cannot execute a mutation because can_execute flag set to false");
|
|
|
|
};
|
|
|
|
|
|
|
|
Pipe pipe(std::make_shared<ThrowingExceptionSource>(header, callback));
|
|
|
|
|
|
|
|
auto read_from_pipe = std::make_unique<ReadFromPreparedSource>(std::move(pipe));
|
|
|
|
plan.addStep(std::move(read_from_pipe));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
const auto & steps = first_stage.expressions_chain.steps;
|
|
|
|
const auto & names = first_stage.filter_column_names;
|
|
|
|
size_t num_filters = names.size();
|
|
|
|
|
|
|
|
ActionsDAGPtr filter;
|
|
|
|
if (!first_stage.filter_column_names.empty())
|
|
|
|
{
|
|
|
|
ActionsDAG::NodeRawConstPtrs nodes(num_filters);
|
|
|
|
for (size_t i = 0; i < num_filters; ++i)
|
|
|
|
nodes[i] = &steps[i]->actions()->findInOutputs(names[i]);
|
|
|
|
|
2024-01-25 15:22:49 +00:00
|
|
|
filter = ActionsDAG::buildFilterActionsDAG(nodes);
|
2023-01-10 15:31:01 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 13:00:50 +00:00
|
|
|
createReadFromPartStep(
|
2023-12-14 17:30:11 +00:00
|
|
|
MergeTreeSequentialSourceType::Mutation,
|
2024-02-28 19:58:55 +00:00
|
|
|
plan, *data, storage_snapshot,
|
|
|
|
part, required_columns,
|
2023-05-25 22:54:54 +00:00
|
|
|
apply_deleted_mask_, filter, context_,
|
2024-01-23 17:04:50 +00:00
|
|
|
getLogger("MutationsInterpreter"));
|
2023-01-10 15:31:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto select = std::make_shared<ASTSelectQuery>();
|
|
|
|
|
|
|
|
select->setExpression(ASTSelectQuery::Expression::SELECT, std::make_shared<ASTExpressionList>());
|
|
|
|
for (const auto & column_name : first_stage.output_columns)
|
|
|
|
select->select()->children.push_back(std::make_shared<ASTIdentifier>(column_name));
|
|
|
|
|
|
|
|
/// Don't let select list be empty.
|
|
|
|
if (select->select()->children.empty())
|
|
|
|
select->select()->children.push_back(std::make_shared<ASTLiteral>(Field(0)));
|
|
|
|
|
|
|
|
if (!first_stage.filters.empty())
|
|
|
|
{
|
|
|
|
ASTPtr where_expression;
|
|
|
|
if (first_stage.filters.size() == 1)
|
|
|
|
where_expression = first_stage.filters[0];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto coalesced_predicates = std::make_shared<ASTFunction>();
|
|
|
|
coalesced_predicates->name = "and";
|
|
|
|
coalesced_predicates->arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
coalesced_predicates->children.push_back(coalesced_predicates->arguments);
|
|
|
|
coalesced_predicates->arguments->children = first_stage.filters;
|
|
|
|
where_expression = std::move(coalesced_predicates);
|
|
|
|
}
|
|
|
|
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(where_expression));
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectQueryInfo query_info;
|
|
|
|
query_info.query = std::move(select);
|
|
|
|
|
|
|
|
size_t max_block_size = context_->getSettingsRef().max_block_size;
|
|
|
|
size_t max_streams = 1;
|
|
|
|
storage->read(plan, required_columns, storage_snapshot, query_info, context_, QueryProcessingStage::FetchColumns, max_block_size, max_streams);
|
|
|
|
|
|
|
|
if (!plan.isInitialized())
|
|
|
|
{
|
|
|
|
/// It may be possible when there is nothing to read from storage.
|
|
|
|
auto header = storage_snapshot->getSampleBlockForColumns(required_columns);
|
|
|
|
auto read_from_pipe = std::make_unique<ReadFromPreparedSource>(Pipe(std::make_shared<NullSource>(header)));
|
|
|
|
plan.addStep(std::move(read_from_pipe));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutationsInterpreter::initQueryPlan(Stage & first_stage, QueryPlan & plan)
|
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
source.read(first_stage, plan, metadata_snapshot, context, settings.apply_deleted_mask, settings.can_execute);
|
2023-01-10 15:31:01 +00:00
|
|
|
addCreatingSetsStep(plan, first_stage.analyzer->getPreparedSets(), context);
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 13:46:57 +00:00
|
|
|
QueryPipelineBuilder MutationsInterpreter::addStreamsForLaterStages(const std::vector<Stage> & prepared_stages, QueryPlan & plan) const
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2023-01-10 15:31:01 +00:00
|
|
|
for (const Stage & stage : prepared_stages)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < stage.expressions_chain.steps.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto & step = stage.expressions_chain.steps[i];
|
2023-01-04 08:56:44 +00:00
|
|
|
if (step->actions()->hasArrayJoin())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_EXPRESSION, "arrayJoin is not allowed in mutations");
|
2023-05-25 22:54:54 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (i < stage.filter_column_names.size())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
/// Execute DELETEs.
|
2021-03-04 17:38:12 +00:00
|
|
|
plan.addStep(std::make_unique<FilterStep>(plan.getCurrentDataStream(), step->actions(), stage.filter_column_names[i], false));
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Execute UPDATE or final projection.
|
2021-03-04 17:38:12 +00:00
|
|
|
plan.addStep(std::make_unique<ExpressionStep>(plan.getCurrentDataStream(), step->actions()));
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 11:38:41 +00:00
|
|
|
|
2022-07-27 11:22:16 +00:00
|
|
|
addCreatingSetsStep(plan, stage.analyzer->getPreparedSets(), context);
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:48:12 +00:00
|
|
|
QueryPlanOptimizationSettings do_not_optimize_plan;
|
|
|
|
do_not_optimize_plan.optimize_plan = false;
|
|
|
|
|
2022-05-23 13:46:57 +00:00
|
|
|
auto pipeline = std::move(*plan.buildQueryPipeline(
|
2021-06-11 08:48:12 +00:00
|
|
|
do_not_optimize_plan,
|
2022-05-23 13:46:57 +00:00
|
|
|
BuildQueryPipelineSettings::fromContext(context)));
|
2021-03-04 17:38:12 +00:00
|
|
|
|
2022-05-23 13:46:57 +00:00
|
|
|
pipeline.addSimpleTransform([&](const Block & header)
|
2020-08-31 12:45:20 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<MaterializingTransform>(header);
|
|
|
|
});
|
2020-09-15 17:13:13 +00:00
|
|
|
|
|
|
|
return pipeline;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:10:47 +00:00
|
|
|
void MutationsInterpreter::validate()
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
2019-10-10 14:38:08 +00:00
|
|
|
/// For Replicated* storages mutations cannot employ non-deterministic functions
|
|
|
|
/// because that produces inconsistencies between replicas
|
2023-05-25 22:54:54 +00:00
|
|
|
if (startsWith(source.getStorage()->getName(), "Replicated") && !context->getSettingsRef().allow_nondeterministic_mutations)
|
2019-10-10 14:38:08 +00:00
|
|
|
{
|
|
|
|
for (const auto & command : commands)
|
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
const auto nondeterministic_func_data = findFirstNonDeterministicFunction(command, context);
|
2021-10-07 12:04:54 +00:00
|
|
|
if (nondeterministic_func_data.subquery)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "ALTER UPDATE/ALTER DELETE statement with subquery may be nondeterministic, "
|
|
|
|
"see allow_nondeterministic_mutations setting");
|
|
|
|
|
|
|
|
if (nondeterministic_func_data.nondeterministic_function_name)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
2024-04-24 09:04:56 +00:00
|
|
|
"The source storage is replicated so ALTER UPDATE/ALTER DELETE statements must use only deterministic functions. "
|
2021-10-07 12:04:54 +00:00
|
|
|
"Function '{}' is non-deterministic", *nondeterministic_func_data.nondeterministic_function_name);
|
2019-10-10 14:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:13:13 +00:00
|
|
|
QueryPlan plan;
|
2023-03-31 13:29:20 +00:00
|
|
|
|
2023-01-10 15:31:01 +00:00
|
|
|
initQueryPlan(stages.front(), plan);
|
2020-09-16 08:58:27 +00:00
|
|
|
auto pipeline = addStreamsForLaterStages(stages, plan);
|
2018-09-04 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 20:06:08 +00:00
|
|
|
QueryPipelineBuilder MutationsInterpreter::execute()
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
2023-05-25 22:54:54 +00:00
|
|
|
if (!settings.can_execute)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot execute mutations interpreter because can_execute flag set to false");
|
2019-11-06 17:28:03 +00:00
|
|
|
|
2020-09-15 17:13:13 +00:00
|
|
|
QueryPlan plan;
|
2023-01-10 15:31:01 +00:00
|
|
|
initQueryPlan(stages.front(), plan);
|
2021-09-16 17:40:42 +00:00
|
|
|
auto builder = addStreamsForLaterStages(stages, plan);
|
2020-03-26 15:59:09 +00:00
|
|
|
|
|
|
|
/// Sometimes we update just part of columns (for example UPDATE mutation)
|
|
|
|
/// in this case we don't read sorting key, so just we don't check anything.
|
2022-05-23 13:46:57 +00:00
|
|
|
if (auto sort_desc = getStorageSortDescriptionIfPossible(builder.getHeader()))
|
2021-07-22 16:05:52 +00:00
|
|
|
{
|
2022-05-23 13:46:57 +00:00
|
|
|
builder.addSimpleTransform([&](const Block & header)
|
2021-07-22 16:05:52 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<CheckSortedTransform>(header, *sort_desc);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:06:05 +00:00
|
|
|
if (!updated_header)
|
2022-05-24 20:06:08 +00:00
|
|
|
updated_header = std::make_unique<Block>(builder.getHeader());
|
2020-03-26 15:59:09 +00:00
|
|
|
|
2022-05-24 20:06:08 +00:00
|
|
|
return builder;
|
2019-08-05 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 13:47:27 +00:00
|
|
|
Block MutationsInterpreter::getUpdatedHeader() const
|
2019-08-05 18:06:05 +00:00
|
|
|
{
|
2021-08-10 13:47:27 +00:00
|
|
|
// If it's an index/projection materialization, we don't write any data columns, thus empty header is used
|
2023-08-09 20:57:49 +00:00
|
|
|
return mutation_kind.mutation_kind == MutationKind::MUTATE_INDEX_STATISTIC_PROJECTION ? Block{} : *updated_header;
|
2018-09-04 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 15:20:18 +00:00
|
|
|
const ColumnDependencies & MutationsInterpreter::getColumnDependencies() const
|
|
|
|
{
|
|
|
|
return dependencies;
|
|
|
|
}
|
2019-08-05 08:36:41 +00:00
|
|
|
|
|
|
|
size_t MutationsInterpreter::evaluateCommandsSize()
|
|
|
|
{
|
2023-01-10 15:31:01 +00:00
|
|
|
return prepareQueryAffectedAST(commands, source.getStorage(), context)->size();
|
2019-08-05 03:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 15:59:09 +00:00
|
|
|
std::optional<SortDescription> MutationsInterpreter::getStorageSortDescriptionIfPossible(const Block & header) const
|
|
|
|
{
|
2020-06-17 11:05:11 +00:00
|
|
|
Names sort_columns = metadata_snapshot->getSortingKeyColumns();
|
2020-03-26 15:59:09 +00:00
|
|
|
SortDescription sort_description;
|
|
|
|
size_t sort_columns_size = sort_columns.size();
|
|
|
|
sort_description.reserve(sort_columns_size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sort_columns_size; ++i)
|
|
|
|
{
|
|
|
|
if (header.has(sort_columns[i]))
|
2022-04-04 12:17:15 +00:00
|
|
|
sort_description.emplace_back(sort_columns[i], 1, 1);
|
2020-03-26 15:59:09 +00:00
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return sort_description;
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
ASTPtr MutationsInterpreter::getPartitionAndPredicateExpressionForMutationCommand(const MutationCommand & command) const
|
|
|
|
{
|
2023-01-10 15:31:01 +00:00
|
|
|
return DB::getPartitionAndPredicateExpressionForMutationCommand(command, source.getStorage(), context);
|
2020-11-10 10:23:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 14:21:57 +00:00
|
|
|
bool MutationsInterpreter::Stage::isAffectingAllColumns(const Names & storage_columns) const
|
|
|
|
{
|
|
|
|
/// is subset
|
|
|
|
for (const auto & storage_column : storage_columns)
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!output_columns.contains(storage_column))
|
2020-07-26 14:21:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutationsInterpreter::isAffectingAllColumns() const
|
|
|
|
{
|
|
|
|
auto storage_columns = metadata_snapshot->getColumns().getNamesOfPhysical();
|
2020-07-27 09:42:37 +00:00
|
|
|
if (stages.empty())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Mutation interpreter has no stages");
|
|
|
|
|
|
|
|
return stages.back().isAffectingAllColumns(storage_columns);
|
2020-07-26 14:21:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
void MutationsInterpreter::MutationKind::set(const MutationKindEnum & kind)
|
|
|
|
{
|
2024-05-10 02:53:29 +00:00
|
|
|
mutation_kind = std::max(mutation_kind, kind);
|
2021-02-10 14:12:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|