diff --git a/src/Interpreters/join_common.cpp b/src/Interpreters/join_common.cpp index 4c124f99e571759606c622cb33c26ae520468499..9a9253cee758afcc383f2f8e7b28b3559ba9aac2 100644 --- a/src/Interpreters/join_common.cpp +++ b/src/Interpreters/join_common.cpp @@ -268,6 +268,10 @@ void joinTotals(const Block & totals, const Block & columns_to_add, const TableJ { if (table_join.rightBecomeNullable(col.type)) JoinCommon::convertColumnToNullable(col); + + /// In case of arrayJoin it can be not one row + if (col.column->size() != 1) + col.column = col.column->cloneResized(1); } for (size_t i = 0; i < totals_without_keys.columns(); ++i) diff --git a/src/Processors/Transforms/JoiningTransform.cpp b/src/Processors/Transforms/JoiningTransform.cpp index 26630f80b17989376217d367b5bc06eed0896f5a..dea887fd7d702bf966ec6baf781be5c2d97dd91a 100644 --- a/src/Processors/Transforms/JoiningTransform.cpp +++ b/src/Processors/Transforms/JoiningTransform.cpp @@ -38,7 +38,11 @@ void JoiningTransform::transform(Chunk & chunk) if (on_totals) { /// We have to make chunk empty before return - block = getInputPort().getHeader().cloneWithColumns(chunk.detachColumns()); + /// In case of using `arrayJoin` we can get more or less rows than one + auto cols = chunk.detachColumns(); + for (auto & col : cols) + col = col->cloneResized(1); + block = getInputPort().getHeader().cloneWithColumns(std::move(cols)); /// Drop totals if both out stream and joined stream doesn't have ones. /// See comment in ExpressionTransform.h diff --git a/tests/queries/0_stateless/01107_join_right_table_totals.reference b/tests/queries/0_stateless/01107_join_right_table_totals.reference index 77db8015b0ea05a3a6b993253b909e88a1e43e8b..f71d3b0d05f4226597bf266952b4c66877f3bef7 100644 --- a/tests/queries/0_stateless/01107_join_right_table_totals.reference +++ b/tests/queries/0_stateless/01107_join_right_table_totals.reference @@ -8,3 +8,13 @@ 0 0 0 0 + +0 0 + +0 0 + +0 0 + +0 0 + +0 0 diff --git a/tests/queries/0_stateless/01107_join_right_table_totals.sql b/tests/queries/0_stateless/01107_join_right_table_totals.sql index 77e8848c95729a1bc954a82bd74cae5cf84c76bc..a4f284e5e2dadfe75f6d2be27c1711e5a6d5cc8b 100644 --- a/tests/queries/0_stateless/01107_join_right_table_totals.sql +++ b/tests/queries/0_stateless/01107_join_right_table_totals.sql @@ -35,4 +35,29 @@ FULL JOIN ) rr USING (id); +SELECT id, yago +FROM ( SELECT item_id AS id FROM t GROUP BY id ) AS ll +FULL OUTER JOIN ( SELECT item_id AS id, arrayJoin([111, 222, 333, 444]), SUM(price_sold) AS yago FROM t GROUP BY id WITH TOTALS ) AS rr +USING (id); + +SELECT id, yago +FROM ( SELECT item_id AS id, arrayJoin([111, 222, 333]) FROM t GROUP BY id WITH TOTALS ) AS ll +FULL OUTER JOIN ( SELECT item_id AS id, SUM(price_sold) AS yago FROM t GROUP BY id ) AS rr +USING (id); + +SELECT id, yago +FROM ( SELECT item_id AS id, arrayJoin(emptyArrayInt32()) FROM t GROUP BY id WITH TOTALS ) AS ll +FULL OUTER JOIN ( SELECT item_id AS id, SUM(price_sold) AS yago FROM t GROUP BY id ) AS rr +USING (id); + +SELECT id, yago +FROM ( SELECT item_id AS id FROM t GROUP BY id ) AS ll +FULL OUTER JOIN ( SELECT item_id AS id, arrayJoin(emptyArrayInt32()), SUM(price_sold) AS yago FROM t GROUP BY id WITH TOTALS ) AS rr +USING (id); + +SELECT id, yago +FROM ( SELECT item_id AS id, arrayJoin([111, 222, 333]) FROM t GROUP BY id WITH TOTALS ) AS ll +FULL OUTER JOIN ( SELECT item_id AS id, arrayJoin([111, 222, 333, 444]), SUM(price_sold) AS yago FROM t GROUP BY id WITH TOTALS ) AS rr +USING (id); + DROP TABLE t;