Rename joined table totals columns (#9939)

This commit is contained in:
Artem Zuikov 2020-03-31 13:38:24 +03:00 committed by GitHub
parent 6a48af8021
commit cd1683a823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 3 deletions

View File

@ -53,7 +53,11 @@ bool SubqueryForSet::insertJoinedBlock(Block & block)
void SubqueryForSet::setTotals()
{
if (join && source)
join->setTotals(source->getTotals());
{
Block totals = source->getTotals();
renameColumns(totals);
join->setTotals(totals);
}
}
}

View File

@ -42,6 +42,7 @@ struct SubqueryForSet
private:
NamesWithAliases joined_block_aliases; /// Rename column from joined block from this list.
/// Rename source right table column names into qualified column names if they conflicts with left table ones.
void renameColumns(Block & block);
};

View File

@ -26,7 +26,7 @@ void ExpressionTransform::transform(Chunk & chunk)
{
initialized = true;
if (expression->resultIsAlwaysEmpty())
if (expression->resultIsAlwaysEmpty() && !on_totals)
{
stopReading();
chunk.clear();

View File

@ -25,7 +25,7 @@ void InflatingExpressionTransform::transform(Chunk & chunk)
{
initialized = true;
if (expression->resultIsAlwaysEmpty())
if (expression->resultIsAlwaysEmpty() && !on_totals)
{
stopReading();
chunk.clear();

View File

@ -0,0 +1,10 @@
0
0
0
0
0 0 0

View File

@ -0,0 +1,38 @@
DROP TABLE IF EXISTS t;
CREATE TABLE t (item_id UInt64, price_sold Float32, date Date) ENGINE MergeTree ORDER BY item_id;
SELECT item_id
FROM (SELECT item_id FROM t GROUP BY item_id WITH TOTALS) l
FULL JOIN (SELECT item_id FROM t GROUP BY item_id WITH TOTALS) r
USING (item_id);
SELECT id
FROM (SELECT item_id AS id FROM t GROUP BY id WITH TOTALS) l
FULL JOIN (SELECT item_id AS id FROM t GROUP BY id WITH TOTALS) r
USING (id);
SELECT item_id
FROM (SELECT item_id FROM t GROUP BY item_id WITH TOTALS) l
INNER JOIN (SELECT item_id FROM t GROUP BY item_id WITH TOTALS) r
USING (item_id);
SELECT id
FROM (SELECT item_id AS id FROM t GROUP BY id WITH TOTALS) l
INNER JOIN (SELECT item_id AS id FROM t GROUP BY id WITH TOTALS) r
USING (id);
SELECT id, yago, recent
FROM (
SELECT item_id AS id, SUM(price_sold) AS recent
FROM t WHERE (date BETWEEN '2019-12-16' AND '2020-03-08')
GROUP BY id WITH TOTALS
) ll
FULL JOIN
(
SELECT item_id AS id, SUM(price_sold) AS yago
FROM t WHERE (date BETWEEN '2018-12-17' AND '2019-03-10')
GROUP BY id WITH TOTALS
) rr
USING (id);
DROP TABLE t;