Merge pull request #22129 from vdimir/issue-19303

Shrink totals block to 1 row for JOIN with TOTALS and arrayJoin
This commit is contained in:
alexey-milovidov 2021-03-29 05:26:17 +03:00 committed by GitHub
commit c762fa2f75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -8,3 +8,13 @@
0
0 0 0
0 0
0 0
0 0
0 0
0 0

View File

@ -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;