A little fix. Added test.

This commit is contained in:
Nikolai Kochetov 2022-07-13 21:06:15 +00:00
parent 3fd367ea58
commit ba863b11a4
3 changed files with 66 additions and 18 deletions

View File

@ -1850,8 +1850,25 @@ ExpressionAnalysisResult::ExpressionAnalysisResult(
};
{
bool join_allow_read_in_order = true;
if (hasJoin())
{
/// You may find it strange but we support read_in_order for HashJoin and do not support for MergeJoin.
join_has_delayed_stream = query_analyzer.analyzedJoin().needStreamWithNonJoinedRows();
join_allow_read_in_order = typeid_cast<HashJoin *>(join.get()) && !join_has_delayed_stream;
}
optimize_read_in_order =
settings.optimize_read_in_order
&& storage
&& query.orderBy()
&& !query_analyzer.hasAggregation()
&& !query_analyzer.hasWindow()
&& !query.final()
&& join_allow_read_in_order;
ExpressionActionsChain chain(context);
Names additional_required_columns_after_prewhere = metadata_snapshot ? metadata_snapshot->getColumnsRequiredForSortingKey() : Names{};
Names additional_required_columns_after_prewhere;
if (storage && (query.sampleSize() || settings.parallel_replicas_count > 1))
{
@ -1867,6 +1884,14 @@ ExpressionAnalysisResult::ExpressionAnalysisResult(
columns_for_final.begin(), columns_for_final.end());
}
if (storage && optimize_read_in_order)
{
std::cerr << "!!!!!!!!!!!!!!\n";
Names columns_for_sorting_key = metadata_snapshot->getColumnsRequiredForSortingKey();
additional_required_columns_after_prewhere.insert(additional_required_columns_after_prewhere.end(),
columns_for_sorting_key.begin(), columns_for_sorting_key.end());
}
if (storage && filter_info_)
{
filter_info = filter_info_;
@ -1951,23 +1976,6 @@ ExpressionAnalysisResult::ExpressionAnalysisResult(
}
}
bool join_allow_read_in_order = true;
if (hasJoin())
{
/// You may find it strange but we support read_in_order for HashJoin and do not support for MergeJoin.
join_has_delayed_stream = query_analyzer.analyzedJoin().needStreamWithNonJoinedRows();
join_allow_read_in_order = typeid_cast<HashJoin *>(join.get()) && !join_has_delayed_stream;
}
optimize_read_in_order =
settings.optimize_read_in_order
&& storage
&& query.orderBy()
&& !query_analyzer.hasAggregation()
&& !query_analyzer.hasWindow()
&& !query.final()
&& join_allow_read_in_order;
/// If there is aggregation, we execute expressions in SELECT and ORDER BY on the initiating server, otherwise on the source servers.
query_analyzer.appendSelect(chain, only_types || (need_aggregate ? !second_stage : !first_stage));

View File

@ -0,0 +1,10 @@
1
1
1
2001
2001
1
1
1
2001
2001

View File

@ -0,0 +1,30 @@
drop table if exists order;
CREATE TABLE order
(
ID Int64,
Type Int64,
Num UInt64,
t DateTime
)
ENGINE = MergeTree()
PARTITION BY toYYYYMMDD(t)
ORDER BY (ID, Type, Num);
system stop merges order;
insert into order select number%2000, 1, number, (1656700561 - intDiv(intHash32(number), 1000)) from numbers(100000);
insert into order select number%2000, 1, number, (1656700561 - intDiv(intHash32(number), 1000)) from numbers(100000);
insert into order select number%2000, 1, number, (1656700561 - intDiv(intHash32(number), 1000)) from numbers(100000);
SELECT Num
FROM order
WHERE Type = 1 AND ID = 1
ORDER BY Num ASC limit 5;
SELECT Num
FROM order
PREWHERE Type = 1
WHERE ID = 1
ORDER BY Num ASC limit 5;