fix distinct with join

This commit is contained in:
chertus 2019-04-12 20:04:38 +03:00
parent cdc65eca23
commit 950f8a7042
4 changed files with 26 additions and 2 deletions

View File

@ -760,11 +760,11 @@ void InterpreterSelectQuery::executeImpl(Pipeline & pipeline, const BlockInputSt
executeExpression(pipeline, expressions.before_order_and_select);
executeDistinct(pipeline, true, expressions.selected_columns);
need_second_distinct_pass = query.distinct && pipeline.hasMoreThanOneStream();
need_second_distinct_pass = query.distinct && pipeline.hasMixedStreams();
}
else
{
need_second_distinct_pass = query.distinct && pipeline.hasMoreThanOneStream();
need_second_distinct_pass = query.distinct && pipeline.hasMixedStreams();
if (query.group_by_with_totals && !aggregate_final)
{
@ -1533,6 +1533,7 @@ void InterpreterSelectQuery::executeUnion(Pipeline & pipeline)
pipeline.firstStream() = std::make_shared<UnionBlockInputStream>(pipeline.streams, pipeline.stream_with_non_joined_data, max_streams);
pipeline.stream_with_non_joined_data = nullptr;
pipeline.streams.resize(1);
pipeline.union_stream = true;
}
else if (pipeline.stream_with_non_joined_data)
{

View File

@ -100,6 +100,7 @@ private:
* It is appended to the main streams in UnionBlockInputStream or ParallelAggregatingBlockInputStream.
*/
BlockInputStreamPtr stream_with_non_joined_data;
bool union_stream = false;
BlockInputStreamPtr & firstStream() { return streams.at(0); }
@ -117,6 +118,11 @@ private:
{
return streams.size() + (stream_with_non_joined_data ? 1 : 0) > 1;
}
bool hasMixedStreams() const
{
return hasMoreThanOneStream() || union_stream;
}
};
void executeImpl(Pipeline & pipeline, const BlockInputStreamPtr & prepared_input, bool dry_run);

View File

@ -0,0 +1,2 @@
0
1

View File

@ -0,0 +1,15 @@
use test;
drop table if exists fooL;
drop table if exists fooR;
create table fooL (a Int32, v String) engine = Memory;
create table fooR (a Int32, v String) engine = Memory;
insert into fooL select number, 'L' || toString(number) from numbers(2);
insert into fooL select number, 'LL' || toString(number) from numbers(2);
insert into fooR select number, 'R' || toString(number) from numbers(2);
select distinct a from fooL any join fooR using(a) order by a;
drop table fooL;
drop table fooR;