2018-09-03 13:36:58 +00:00
|
|
|
#include <Interpreters/MutationsInterpreter.h>
|
2018-11-08 15:43:14 +00:00
|
|
|
#include <Interpreters/SyntaxAnalyzer.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2018-09-07 15:44:51 +00:00
|
|
|
#include <Storages/StorageMergeTree.h>
|
|
|
|
#include <Storages/StorageReplicatedMergeTree.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <DataStreams/FilterBlockInputStream.h>
|
|
|
|
#include <DataStreams/ExpressionBlockInputStream.h>
|
2018-09-04 11:38:41 +00:00
|
|
|
#include <DataStreams/CreatingSetsBlockInputStream.h>
|
2018-09-03 13:36:58 +00:00
|
|
|
#include <DataStreams/MaterializingBlockInputStream.h>
|
2018-09-04 13:45:39 +00:00
|
|
|
#include <DataStreams/NullBlockInputStream.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>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
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;
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MutationsInterpreter::isStorageTouchedByMutations() const
|
|
|
|
{
|
|
|
|
if (commands.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const MutationCommand & command : commands)
|
|
|
|
{
|
|
|
|
if (!command.predicate) /// The command touches all rows.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute `SELECT count() FROM storage WHERE predicate1 OR predicate2 OR ...` query.
|
|
|
|
/// The result can differ from tne number of affected rows (e.g. if there is an UPDATE command that
|
|
|
|
/// 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->select_expression_list = std::make_shared<ASTExpressionList>();
|
|
|
|
select->children.push_back(select->select_expression_list);
|
|
|
|
auto count_func = std::make_shared<ASTFunction>();
|
|
|
|
count_func->name = "count";
|
|
|
|
count_func->arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
select->select_expression_list->children.push_back(count_func);
|
|
|
|
|
|
|
|
if (commands.size() == 1)
|
2019-01-23 13:27:01 +00:00
|
|
|
select->where_expression = commands[0].predicate->clone();
|
2018-09-03 13:36:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto coalesced_predicates = std::make_shared<ASTFunction>();
|
|
|
|
coalesced_predicates->name = "or";
|
|
|
|
coalesced_predicates->arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
coalesced_predicates->children.push_back(coalesced_predicates->arguments);
|
|
|
|
|
|
|
|
for (const MutationCommand & command : commands)
|
2019-01-23 13:27:01 +00:00
|
|
|
coalesced_predicates->arguments->children.push_back(command.predicate->clone());
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
select->where_expression = std::move(coalesced_predicates);
|
|
|
|
}
|
|
|
|
select->children.push_back(select->where_expression);
|
|
|
|
|
|
|
|
auto context_copy = context;
|
|
|
|
context_copy.getSettingsRef().merge_tree_uniform_read_distribution = 0;
|
|
|
|
context_copy.getSettingsRef().max_threads = 1;
|
|
|
|
|
2019-03-15 13:49:58 +00:00
|
|
|
BlockInputStreamPtr in = InterpreterSelectQuery(select, context_copy, storage).execute().in;
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
Block block = in->read();
|
|
|
|
if (!block.rows())
|
|
|
|
return false;
|
|
|
|
else if (block.rows() != 1)
|
|
|
|
throw Exception("count() expression returned " + toString(block.rows()) + " rows, not 1",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
auto count = (*block.getByName("count()").column)[0].get<UInt64>();
|
|
|
|
return count != 0;
|
|
|
|
}
|
|
|
|
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
static NameSet getKeyColumns(const StoragePtr & storage)
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
const MergeTreeData * merge_tree_data = nullptr;
|
|
|
|
if (auto merge_tree = dynamic_cast<StorageMergeTree *>(storage.get()))
|
|
|
|
merge_tree_data = &merge_tree->getData();
|
|
|
|
else if (auto replicated_merge_tree = dynamic_cast<StorageReplicatedMergeTree *>(storage.get()))
|
|
|
|
merge_tree_data = &replicated_merge_tree->getData();
|
|
|
|
else
|
|
|
|
return {};
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
NameSet key_columns;
|
|
|
|
|
2018-11-06 18:25:36 +00:00
|
|
|
if (merge_tree_data->partition_key_expr)
|
|
|
|
for (const String & col : merge_tree_data->partition_key_expr->getRequiredColumns())
|
2018-09-07 15:44:51 +00:00
|
|
|
key_columns.insert(col);
|
|
|
|
|
2018-11-09 19:01:39 +00:00
|
|
|
auto sorting_key_expr = merge_tree_data->sorting_key_expr;
|
2018-10-12 19:00:43 +00:00
|
|
|
if (sorting_key_expr)
|
|
|
|
for (const String & col : sorting_key_expr->getRequiredColumns())
|
2018-09-07 15:44:51 +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(
|
|
|
|
const StoragePtr & storage, const NameSet & updated_columns,
|
|
|
|
const std::unordered_map<String, Names> & column_to_affected_materialized)
|
|
|
|
{
|
|
|
|
NameSet key_columns = getKeyColumns(storage);
|
|
|
|
|
|
|
|
for (const String & column_name : updated_columns)
|
|
|
|
{
|
|
|
|
auto found = false;
|
2019-03-14 15:20:51 +00:00
|
|
|
for (const auto & col : storage->getColumns().getOrdinary())
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
if (col.name == column_name)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
for (const auto & col : storage->getColumns().getMaterialized())
|
2018-09-04 13:45:39 +00:00
|
|
|
{
|
|
|
|
if (col.name == column_name)
|
2018-09-07 15:44:51 +00:00
|
|
|
throw Exception("Cannot UPDATE materialized column `" + column_name + "`", ErrorCodes::CANNOT_UPDATE_COLUMN);
|
2018-09-04 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
throw Exception("There is no column `" + column_name + "` in table", ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
|
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (key_columns.count(column_name))
|
|
|
|
throw Exception("Cannot UPDATE key column `" + column_name + "`", ErrorCodes::CANNOT_UPDATE_COLUMN);
|
|
|
|
|
|
|
|
auto materialized_it = column_to_affected_materialized.find(column_name);
|
|
|
|
if (materialized_it != column_to_affected_materialized.end())
|
|
|
|
{
|
|
|
|
for (const String & materialized : materialized_it->second)
|
|
|
|
{
|
|
|
|
if (key_columns.count(materialized))
|
|
|
|
throw Exception("Updated column `" + column_name + "` affects MATERIALIZED column `"
|
|
|
|
+ materialized + "`, which is a key column. Cannot UPDATE it.",
|
|
|
|
ErrorCodes::CANNOT_UPDATE_COLUMN);
|
2018-09-04 13:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MutationsInterpreter::prepare(bool dry_run)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
if (is_prepared)
|
2018-09-04 13:45:39 +00:00
|
|
|
throw Exception("MutationsInterpreter is already prepared. It is a bug.", ErrorCodes::LOGICAL_ERROR);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
if (commands.empty())
|
|
|
|
throw Exception("Empty mutation commands list", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
const ColumnsDescription & columns_desc = storage->getColumns();
|
2019-04-17 17:07:07 +00:00
|
|
|
const IndicesDescription & indices_desc = storage->getIndicesDescription();
|
2018-09-07 15:44:51 +00:00
|
|
|
NamesAndTypesList all_columns = columns_desc.getAllPhysical();
|
|
|
|
|
|
|
|
NameSet updated_columns;
|
|
|
|
for (const MutationCommand & command : commands)
|
|
|
|
{
|
|
|
|
for (const auto & kv : command.column_to_update_expression)
|
|
|
|
updated_columns.insert(kv.first);
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:07:07 +00:00
|
|
|
/// We need to know which columns affect which MATERIALIZED columns and data skipping indices
|
|
|
|
/// to recalculate them if dependencies are updated.
|
2018-09-07 15:44:51 +00:00
|
|
|
std::unordered_map<String, Names> column_to_affected_materialized;
|
2019-04-17 17:07:07 +00:00
|
|
|
NameSet affected_indices_columns;
|
2018-09-07 15:44:51 +00:00
|
|
|
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
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.default_desc.kind == ColumnDefaultKind::Materialized)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
auto query = column.default_desc.expression->clone();
|
2019-01-09 16:16:59 +00:00
|
|
|
auto syntax_result = SyntaxAnalyzer(context).analyze(query, all_columns);
|
2018-11-08 17:28:52 +00:00
|
|
|
ExpressionAnalyzer analyzer(query, syntax_result, context);
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const String & dependency : analyzer.getRequiredSourceColumns())
|
|
|
|
{
|
|
|
|
if (updated_columns.count(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 17:07:07 +00:00
|
|
|
for (const auto & index : indices_desc.indices)
|
|
|
|
{
|
|
|
|
auto query = index->expr->clone();
|
|
|
|
auto syntax_result = SyntaxAnalyzer(context).analyze(query, all_columns);
|
|
|
|
ExpressionAnalyzer analyzer(query, syntax_result, context);
|
|
|
|
const auto required_columns = analyzer.getRequiredSourceColumns();
|
|
|
|
|
|
|
|
for (const String & dependency : required_columns)
|
|
|
|
{
|
|
|
|
if (updated_columns.count(dependency))
|
|
|
|
{
|
|
|
|
affected_indices_columns.insert(std::cbegin(required_columns), std::cend(required_columns));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 13:45:39 +00:00
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
validateUpdateColumns(storage, updated_columns, column_to_affected_materialized);
|
2019-01-09 15:44:20 +00:00
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
/// First, break a sequence of commands into stages.
|
2018-09-07 19:14:05 +00:00
|
|
|
stages.emplace_back(context);
|
2018-09-03 13:36:58 +00:00
|
|
|
for (const auto & command : commands)
|
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
if (!stages.back().column_to_updated.empty())
|
2018-09-07 19:14:05 +00:00
|
|
|
stages.emplace_back(context);
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
if (command.type == MutationCommand::DELETE)
|
2018-09-07 15:44:51 +00:00
|
|
|
{
|
|
|
|
auto negated_predicate = makeASTFunction("not", command.predicate->clone());
|
|
|
|
stages.back().filters.push_back(negated_predicate);
|
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
else if (command.type == MutationCommand::UPDATE)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
for (const auto & kv : command.column_to_update_expression)
|
|
|
|
{
|
|
|
|
const String & column = kv.first;
|
|
|
|
|
|
|
|
auto materialized_it = column_to_affected_materialized.find(column);
|
|
|
|
if (materialized_it != column_to_affected_materialized.end())
|
|
|
|
{
|
|
|
|
for (const String & mat_column : materialized_it->second)
|
|
|
|
affected_materialized.emplace(mat_column);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & update_expr = kv.second;
|
|
|
|
auto updated_column = makeASTFunction("CAST",
|
|
|
|
makeASTFunction("if",
|
|
|
|
command.predicate->clone(),
|
|
|
|
update_expr->clone(),
|
|
|
|
std::make_shared<ASTIdentifier>(column)),
|
|
|
|
std::make_shared<ASTLiteral>(columns_desc.getPhysical(column).type->getName()));
|
|
|
|
stages.back().column_to_updated.emplace(column, updated_column);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
stages.back().column_to_updated.emplace(
|
|
|
|
column.name,
|
|
|
|
column.default_desc.expression->clone());
|
|
|
|
}
|
2018-09-07 15:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Unknown mutation command type: " + DB::toString<int>(command.type), ErrorCodes::UNKNOWN_MUTATION_COMMAND);
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:07:07 +00:00
|
|
|
/// Special step to recalculate affected indices.
|
|
|
|
if (!affected_indices_columns.empty())
|
|
|
|
{
|
|
|
|
stages.emplace_back(context);
|
|
|
|
for (const auto & column : affected_indices_columns)
|
|
|
|
{
|
|
|
|
stages.back().column_to_updated.emplace(
|
|
|
|
column,
|
|
|
|
std::make_shared<ASTLiteral>(
|
|
|
|
columns_desc.getPhysical(column).type->getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
/// Next, for each stage calculate columns changed by this and previous stages.
|
|
|
|
for (size_t i = 0; i < stages.size(); ++i)
|
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
if (!stages[i].filters.empty())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
for (const auto & column : all_columns)
|
|
|
|
stages[i].output_columns.insert(column.name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
stages[i].output_columns = stages[i - 1].output_columns;
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (stages[i].output_columns.size() < all_columns.size())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & kv : stages[i].column_to_updated)
|
2018-09-03 13:36:58 +00:00
|
|
|
stages[i].output_columns.insert(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
for (size_t i = stages.size() - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
auto & stage = stages[i];
|
|
|
|
|
|
|
|
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.
|
|
|
|
for (const String & column : stage.output_columns)
|
|
|
|
all_asts->children.push_back(std::make_shared<ASTIdentifier>(column));
|
|
|
|
|
2019-01-09 16:16:59 +00:00
|
|
|
auto syntax_result = SyntaxAnalyzer(context).analyze(all_asts, all_columns);
|
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;
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & ast : stage.filters)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
if (!actions_chain.steps.empty())
|
|
|
|
actions_chain.addStep();
|
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
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
for (const auto & kv : stage.column_to_updated)
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
actions_chain.getLastActions()->add(ExpressionAction::copyColumn(
|
|
|
|
kv.second->getColumnName(), kv.first, /* can_replace = */ true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove all intermediate columns.
|
|
|
|
actions_chain.addStep();
|
|
|
|
actions_chain.getLastStep().required_output.assign(stage.output_columns.begin(), stage.output_columns.end());
|
|
|
|
|
|
|
|
actions_chain.finalize();
|
|
|
|
|
|
|
|
/// Propagate information about columns needed as input.
|
|
|
|
for (const auto & column : actions_chain.steps.front().actions->getRequiredColumnsWithTypes())
|
|
|
|
stages[i - 1].output_columns.insert(column.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute first stage as a SELECT statement.
|
|
|
|
|
|
|
|
auto select = std::make_shared<ASTSelectQuery>();
|
|
|
|
|
|
|
|
select->select_expression_list = std::make_shared<ASTExpressionList>();
|
|
|
|
select->children.push_back(select->select_expression_list);
|
|
|
|
for (const auto & column_name : stages[0].output_columns)
|
|
|
|
select->select_expression_list->children.push_back(std::make_shared<ASTIdentifier>(column_name));
|
|
|
|
|
2018-09-07 15:44:51 +00:00
|
|
|
if (!stages[0].filters.empty())
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
ASTPtr where_expression;
|
2018-09-07 15:44:51 +00:00
|
|
|
if (stages[0].filters.size() == 1)
|
|
|
|
where_expression = stages[0].filters[0];
|
2018-09-03 13:36:58 +00:00
|
|
|
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);
|
2018-09-07 15:44:51 +00:00
|
|
|
coalesced_predicates->arguments->children = stages[0].filters;
|
2018-09-03 13:36:58 +00:00
|
|
|
where_expression = std::move(coalesced_predicates);
|
|
|
|
}
|
|
|
|
select->where_expression = where_expression;
|
|
|
|
select->children.push_back(where_expression);
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:05:51 +00:00
|
|
|
interpreter_select = std::make_unique<InterpreterSelectQuery>(select, context, storage, SelectQueryOptions().analyze(dry_run));
|
2018-09-03 13:36:58 +00:00
|
|
|
|
|
|
|
is_prepared = true;
|
|
|
|
}
|
|
|
|
|
2018-09-04 13:45:39 +00:00
|
|
|
BlockInputStreamPtr MutationsInterpreter::addStreamsForLaterStages(BlockInputStreamPtr in) const
|
2018-09-03 13:36:58 +00:00
|
|
|
{
|
|
|
|
for (size_t i_stage = 1; i_stage < stages.size(); ++i_stage)
|
|
|
|
{
|
|
|
|
const Stage & stage = stages[i_stage];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < stage.expressions_chain.steps.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto & step = stage.expressions_chain.steps[i];
|
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.
|
|
|
|
in = std::make_shared<FilterBlockInputStream>(in, step.actions, stage.filter_column_names[i]);
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Execute UPDATE or final projection.
|
|
|
|
in = std::make_shared<ExpressionBlockInputStream>(in, step.actions);
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 11:38:41 +00:00
|
|
|
|
|
|
|
const SubqueriesForSets & subqueries_for_sets = stage.analyzer->getSubqueriesForSets();
|
|
|
|
if (!subqueries_for_sets.empty())
|
2019-02-27 18:26:24 +00:00
|
|
|
in = std::make_shared<CreatingSetsBlockInputStream>(in, subqueries_for_sets, context);
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in = std::make_shared<MaterializingBlockInputStream>(in);
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2018-09-04 13:45:39 +00:00
|
|
|
void MutationsInterpreter::validate()
|
|
|
|
{
|
|
|
|
prepare(/* dry_run = */ true);
|
|
|
|
Block first_stage_header = interpreter_select->getSampleBlock();
|
|
|
|
BlockInputStreamPtr in = std::make_shared<NullBlockInputStream>(first_stage_header);
|
|
|
|
addStreamsForLaterStages(in)->getHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr MutationsInterpreter::execute()
|
|
|
|
{
|
|
|
|
prepare(/* dry_run = */ false);
|
|
|
|
BlockInputStreamPtr in = interpreter_select->execute().in;
|
|
|
|
return addStreamsForLaterStages(in);
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:36:58 +00:00
|
|
|
}
|