From b1f4cc6ed24fca22159efe7c199207329c2f68da Mon Sep 17 00:00:00 2001 From: chertus Date: Wed, 20 Mar 2019 20:20:14 +0300 Subject: [PATCH] fix left join with const column --- dbms/src/Interpreters/Join.cpp | 20 ++++++++-------- ...916_join_using_duplicate_columns.reference | 15 ++++++++++++ .../00916_join_using_duplicate_columns.sql | 23 ++++++++++++++++++- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/dbms/src/Interpreters/Join.cpp b/dbms/src/Interpreters/Join.cpp index bafb98fd703..3336f175ebe 100644 --- a/dbms/src/Interpreters/Join.cpp +++ b/dbms/src/Interpreters/Join.cpp @@ -755,13 +755,13 @@ void Join::joinBlockImpl( if (needed_key_names_right.count(right_name) && !block.has(right_name)) { const auto & col = block.getByName(left_name); - auto & column = col.column; + ColumnPtr column = col.column->convertToFullColumnIfConst(); 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]) - mut_column->insertFrom(*column, col_no); + if (filter[row]) + mut_column->insertFrom(*column, row); else mut_column->insertDefault(); } @@ -785,22 +785,22 @@ void Join::joinBlockImpl( if (needed_key_names_right.count(right_name) && !block.has(right_name)) { const auto & col = block.getByName(left_name); - auto & column = col.column; + ColumnPtr column = col.column->convertToFullColumnIfConst(); MutableColumnPtr mut_column = column->cloneEmpty(); 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(); else 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}); diff --git a/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.reference b/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.reference index be9a5a74a14..1e896055e15 100644 --- a/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.reference +++ b/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.reference @@ -3,3 +3,18 @@ 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 diff --git a/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.sql b/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.sql index 97cd1e8cac8..a30aee7f61f 100644 --- a/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.sql +++ b/dbms/tests/queries/0_stateless/00916_join_using_duplicate_columns.sql @@ -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 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;