fix left join with const column

This commit is contained in:
chertus 2019-03-20 20:20:14 +03:00
parent 2ea11f8528
commit b1f4cc6ed2
3 changed files with 47 additions and 11 deletions

View File

@ -755,13 +755,13 @@ void Join::joinBlockImpl(
if (needed_key_names_right.count(right_name) && !block.has(right_name)) if (needed_key_names_right.count(right_name) && !block.has(right_name))
{ {
const auto & col = block.getByName(left_name); const auto & col = block.getByName(left_name);
auto & column = col.column; ColumnPtr column = col.column->convertToFullColumnIfConst();
MutableColumnPtr mut_column = column->cloneEmpty(); MutableColumnPtr mut_column = column->cloneEmpty();
for (size_t col_no = 0; col_no < filter.size(); ++col_no) for (size_t row = 0; row < filter.size(); ++row)
{ {
if (filter[col_no]) if (filter[row])
mut_column->insertFrom(*column, col_no); mut_column->insertFrom(*column, row);
else else
mut_column->insertDefault(); mut_column->insertDefault();
} }
@ -785,22 +785,22 @@ void Join::joinBlockImpl(
if (needed_key_names_right.count(right_name) && !block.has(right_name)) if (needed_key_names_right.count(right_name) && !block.has(right_name))
{ {
const auto & col = block.getByName(left_name); const auto & col = block.getByName(left_name);
auto & column = col.column; ColumnPtr column = col.column->convertToFullColumnIfConst();
MutableColumnPtr mut_column = column->cloneEmpty(); MutableColumnPtr mut_column = column->cloneEmpty();
size_t last_offset = 0; size_t last_offset = 0;
for (size_t col_no = 0; col_no < column->size(); ++col_no) for (size_t row = 0; row < column->size(); ++row)
{ {
if (size_t to_insert = (*offsets_to_replicate)[col_no] - last_offset) if (size_t to_insert = (*offsets_to_replicate)[row] - last_offset)
{ {
if (!filter[col_no]) if (!filter[row])
mut_column->insertDefault(); mut_column->insertDefault();
else else
for (size_t dup = 0; dup < to_insert; ++dup) for (size_t dup = 0; dup < to_insert; ++dup)
mut_column->insertFrom(*column, col_no); mut_column->insertFrom(*column, row);
} }
last_offset = (*offsets_to_replicate)[col_no]; last_offset = (*offsets_to_replicate)[row];
} }
block.insert({std::move(mut_column), col.type, right_name}); block.insert({std::move(mut_column), col.type, right_name});

View File

@ -3,3 +3,18 @@
1 1
1 1
1 1 1 1
1 0
1 0
1 0
1 1
1 0
1 0
1 0
1 1
0 2
0 2
0 2
1 1
0 2
0 2
0 2

View File

@ -5,4 +5,25 @@ SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 1 AS x) AS t2 USING x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 USING x; SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 USING x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 1 AS x) AS t2 ON t1.x = t2.x; SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 1 AS x) AS t2 ON t1.x = t2.x;
-- (bug) SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x; SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT materialize(1) AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT materialize(2) AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY LEFT JOIN (SELECT 1 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT materialize(1) AS x) AS t1 ANY LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY LEFT JOIN (SELECT materialize(2) AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL RIGHT JOIN (SELECT 1 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL RIGHT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT materialize(1) AS x) AS t1 ALL RIGHT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ALL RIGHT JOIN (SELECT materialize(2) AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY RIGHT JOIN (SELECT 1 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY RIGHT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT materialize(1) AS x) AS t1 ANY RIGHT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
SELECT * FROM (SELECT 1 AS x) AS t1 ANY RIGHT JOIN (SELECT materialize(2) AS x) AS t2 ON t1.x = t2.x;
-- SET join_use_nulls = 1;
-- SELECT * FROM (SELECT 1 AS x) AS t1 ALL LEFT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;
-- SELECT * FROM (SELECT 1 AS x) AS t1 ALL RIGHT JOIN (SELECT 2 AS x) AS t2 ON t1.x = t2.x;