Fix Distributed engine with virtual columns of the underlying table in WHERE

Before storage has not been passed to the SyntaxAnalyzer and hence
it cannot check if there is such column and throw for virtual columns
like _part (MergeTree) and others.

Follow-up-for: #8846
This commit is contained in:
Azat Khuzhin 2020-03-24 21:06:55 +03:00
parent dcf4d5198f
commit 5039b6ab73
2 changed files with 11 additions and 4 deletions

View File

@ -227,16 +227,16 @@ public:
}
};
void replaceConstantExpressions(ASTPtr & node, const Context & context, const NamesAndTypesList & columns)
void replaceConstantExpressions(ASTPtr & node, const Context & context, const NamesAndTypesList & columns, ConstStoragePtr storage)
{
auto syntax_result = SyntaxAnalyzer(context).analyze(node, columns);
auto syntax_result = SyntaxAnalyzer(context).analyze(node, columns, storage);
Block block_with_constants = KeyCondition::getBlockWithConstants(node, syntax_result, context);
InDepthNodeVisitor<ReplacingConstantExpressionsMatcher, true> visitor(block_with_constants);
visitor.visit(node);
}
}
} // \anonymous
/// For destruction of std::unique_ptr of type that is incomplete in class definition.
@ -662,7 +662,7 @@ ClusterPtr StorageDistributed::skipUnusedShards(ClusterPtr cluster, const Select
condition_ast = select.prewhere() ? select.prewhere()->clone() : select.where()->clone();
}
replaceConstantExpressions(condition_ast, context, getColumns().getAllPhysical() /** TODO: sharding_key_column_name */);
replaceConstantExpressions(condition_ast, context, getColumns().getAll(), shared_from_this());
const auto blocks = evaluateExpressionOverConstantCondition(condition_ast, sharding_key_expr);
// Can't get definite answer if we can skip any shards

View File

@ -45,5 +45,12 @@ select * from dist_01072 where key=toInt32(value); -- { serverError 507; }
select * from dist_01072 where key=value settings force_optimize_skip_unused_shards=0;
select * from dist_01072 where key=toInt32(value) settings force_optimize_skip_unused_shards=0;
-- check virtual columns
drop table data_01072;
drop table dist_01072;
create table data_01072 (key Int) Engine=MergeTree() ORDER BY key;
create table dist_01072 (key Int) Engine=Distributed(test_cluster_two_shards, currentDatabase(), data_01072, key);
select * from dist_01072 where key=0 and _part='0' settings force_optimize_skip_unused_shards=2;
drop table data_01072;
drop table dist_01072;