diff --git a/dbms/src/Interpreters/Join.cpp b/dbms/src/Interpreters/Join.cpp index 4b7731b2e42..8858dd0f2b6 100644 --- a/dbms/src/Interpreters/Join.cpp +++ b/dbms/src/Interpreters/Join.cpp @@ -469,9 +469,15 @@ bool Join::insertFromBlock(const Block & block) } else { + NameSet erased; /// HOTFIX: there could be duplicates in JOIN ON section + /// Remove the key columns from stored_block, as they are not needed. for (const auto & name : key_names_right) - stored_block->erase(stored_block->getPositionByName(name)); + { + if (!erased.count(name)) + stored_block->erase(stored_block->getPositionByName(name)); + erased.insert(name); + } } size_t size = stored_block->columns(); diff --git a/dbms/tests/queries/0_stateless/00818_join_bug_4271.reference b/dbms/tests/queries/0_stateless/00818_join_bug_4271.reference new file mode 100644 index 00000000000..4d591381421 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00818_join_bug_4271.reference @@ -0,0 +1,9 @@ +1 1 a 1 1 a +2 2 b \N \N \N +1 1 a 1 1 a +2 2 b \N \N \N +1 1 a 1 1 a +1 1 a 1 1 a +2 2 b \N \N \N +1 1 a 1 1 a +2 2 b \N \N \N diff --git a/dbms/tests/queries/0_stateless/00818_join_bug_4271.sql b/dbms/tests/queries/0_stateless/00818_join_bug_4271.sql new file mode 100644 index 00000000000..8f7fc7f8ef4 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00818_join_bug_4271.sql @@ -0,0 +1,19 @@ +use test; + +drop table if exists t; +drop table if exists s; + +create table t(a Nullable(Int64), b Nullable(Int64), c Nullable(String)) engine = Memory; +create table s(a Nullable(Int64), b Nullable(Int64), c Nullable(String)) engine = Memory; + +insert into t values(1,1,'a'), (2,2,'b'); +insert into s values(1,1,'a'); + +select * from t left join s on t.a = s.a; +select * from t left join s on t.a = s.a and t.a = s.b; +select * from t left join s on t.a = s.a where s.a = 1; +select * from t left join s on t.a = s.a and t.a = s.a; +select * from t left join s on t.a = s.a and t.b = s.a; + +drop table t; +drop table s;