Merge pull request #3090 from yandex/fix_prewhere_with_sampling_2

Fix prewhere with sampling 2
This commit is contained in:
alexey-milovidov 2018-09-10 13:43:07 +03:00 committed by GitHub
commit 1d28a9c510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -2678,7 +2678,7 @@ bool ExpressionAnalyzer::appendPrewhere(ExpressionActionsChain & chain, bool onl
Names required_sample_columns;
if (sampling_expression)
required_sample_columns = ExpressionAnalyzer(sampling_expression, context, nullptr, source_columns).getRequiredSourceColumns();
required_sample_columns = ExpressionAnalyzer(sampling_expression, context, storage).getRequiredSourceColumns();
initChain(chain, source_columns);
auto & step = chain.getLastStep();
@ -2687,15 +2687,6 @@ bool ExpressionAnalyzer::appendPrewhere(ExpressionActionsChain & chain, bool onl
step.required_output.push_back(prewhere_column_name);
step.can_remove_required_output.push_back(true);
/// Add required columns for sample expression to required output in order not to remove them after
/// prewhere execution because sampling is executed after prewhere.
/// TODO: add sampling execution to common chain.
for (const auto & column : required_sample_columns)
{
step.required_output.push_back(column);
step.can_remove_required_output.push_back(true);
}
{
/// Remove unused source_columns from prewhere actions.
auto tmp_actions = std::make_shared<ExpressionActions>(source_columns, context);
@ -2704,6 +2695,18 @@ bool ExpressionAnalyzer::appendPrewhere(ExpressionActionsChain & chain, bool onl
auto required_columns = tmp_actions->getRequiredColumns();
NameSet required_source_columns(required_columns.begin(), required_columns.end());
/// Add required columns for sample expression to required output in order not to remove them after
/// prewhere execution because sampling is executed after prewhere.
/// TODO: add sampling execution to common chain.
for (const auto & column : required_sample_columns)
{
if (required_source_columns.count(column))
{
step.required_output.push_back(column);
step.can_remove_required_output.push_back(true);
}
}
auto names = step.actions->getSampleBlock().getNames();
NameSet name_set(names.begin(), names.end());

View File

@ -4,3 +4,7 @@ insert into test.tab values (1, 2), (1, 4);
select a from test.tab sample 1 / 2 prewhere b = 2;
drop table if exists test.tab;
DROP TABLE IF EXISTS test.sample_prewhere;
CREATE TABLE test.sample_prewhere (CounterID UInt32, UserID UInt64) ENGINE = MergeTree ORDER BY UserID SAMPLE BY UserID;
SELECT count() FROM test.sample_prewhere SAMPLE 1/2 PREWHERE CounterID = 1;
DROP TABLE test.sample_prewhere;