diff --git a/src/Interpreters/join_common.cpp b/src/Interpreters/join_common.cpp index 9a9253cee75..eddc16bfb2f 100644 --- a/src/Interpreters/join_common.cpp +++ b/src/Interpreters/join_common.cpp @@ -284,6 +284,11 @@ void joinTotals(const Block & totals, const Block & columns_to_add, const TableJ for (size_t i = 0; i < columns_to_add.columns(); ++i) { const auto & col = columns_to_add.getByPosition(i); + if (block.has(col.name)) + { + /// For StorageJoin we discarded table qualifiers, so some names may clash + continue; + } block.insert({ col.type->createColumnConstWithDefaultValue(1)->convertToFullColumnIfConst(), col.type, diff --git a/tests/queries/0_stateless/00119_storage_join.reference b/tests/queries/0_stateless/00119_storage_join.reference index 2689dadfb42..9b1dfe11616 100644 --- a/tests/queries/0_stateless/00119_storage_join.reference +++ b/tests/queries/0_stateless/00119_storage_join.reference @@ -38,3 +38,7 @@ ghi [] 1 [] 2 3 7 4 1 [] 2 3 8 4 1 [] 2 3 9 4 +0 a [] +1 a [0] + +0 a [] diff --git a/tests/queries/0_stateless/00119_storage_join.sql b/tests/queries/0_stateless/00119_storage_join.sql index 735ef2da16a..e1cc7a67588 100644 --- a/tests/queries/0_stateless/00119_storage_join.sql +++ b/tests/queries/0_stateless/00119_storage_join.sql @@ -11,4 +11,8 @@ SELECT s, x FROM (SELECT number AS k FROM system.numbers LIMIT 10) js1 ANY LEFT SELECT x, s, k FROM (SELECT number AS k FROM system.numbers LIMIT 10) js1 ANY LEFT JOIN join USING k; SELECT 1, x, 2, s, 3, k, 4 FROM (SELECT number AS k FROM system.numbers LIMIT 10) js1 ANY LEFT JOIN join USING k; +SELECT t1.k, t1.s, t2.x +FROM ( SELECT number AS k, 'a' AS s FROM numbers(2) GROUP BY number WITH TOTALS ) AS t1 +ANY LEFT JOIN join AS t2 USING(k); + DROP TABLE join;