Skip to content

Commit 3bdce39

Browse files
committed
Fix logical error in right storage join witn using
1 parent c2ceb78 commit 3bdce39

7 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/Interpreters/HashJoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,15 +1963,15 @@ IBlocksStreamPtr HashJoin::getNonJoinedBlocks(const Block & left_sample_block,
19631963
/// ... calculate `left_columns_count` ...
19641964
size_t left_columns_count = left_sample_block.columns();
19651965
auto non_joined = std::make_unique<NotJoinedHash<true>>(*this, max_block_size);
1966-
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, table_join->leftToRightKeyRemap());
1966+
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, *table_join);
19671967

19681968
}
19691969
else
19701970
{
19711971
size_t left_columns_count = left_sample_block.columns();
19721972
assert(left_columns_count == result_sample_block.columns() - required_right_keys.columns() - sample_block_with_columns_to_add.columns());
19731973
auto non_joined = std::make_unique<NotJoinedHash<false>>(*this, max_block_size);
1974-
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, table_join->leftToRightKeyRemap());
1974+
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, *table_join);
19751975
}
19761976
}
19771977

src/Interpreters/JoinUtils.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,12 @@ ColumnPtr filterWithBlanks(ColumnPtr src_column, const IColumn::Filter & filter,
718718
NotJoinedBlocks::NotJoinedBlocks(std::unique_ptr<RightColumnsFiller> filler_,
719719
const Block & result_sample_block_,
720720
size_t left_columns_count,
721-
const LeftToRightKeyRemap & left_to_right_key_remap)
721+
const TableJoin & table_join)
722722
: filler(std::move(filler_))
723723
, saved_block_sample(filler->getEmptyBlock())
724724
, result_sample_block(materializeBlock(result_sample_block_))
725725
{
726+
const auto & left_to_right_key_remap = table_join.leftToRightKeyRemap();
726727
for (size_t left_pos = 0; left_pos < left_columns_count; ++left_pos)
727728
{
728729
/// We need right 'x' for 'RIGHT JOIN ... USING(x)'
@@ -739,14 +740,21 @@ NotJoinedBlocks::NotJoinedBlocks(std::unique_ptr<RightColumnsFiller> filler_,
739740

740741
/// `saved_block_sample` may contains non unique column names, get any of them
741742
/// (e.g. in case of `... JOIN (SELECT a, a, b FROM table) as t2`)
742-
for (const auto & [name, right_pos] : saved_block_sample.getNamesToIndexesMap())
743+
for (const auto & [right_name, right_pos] : saved_block_sample.getNamesToIndexesMap())
743744
{
745+
String column_name(right_name);
746+
if (table_join.getStorageJoin())
747+
{
748+
/// StorageJoin operates with original non qualified column names, so apply renaming here
749+
column_name = table_join.renamedRightColumnName(column_name);
750+
}
751+
744752
/// Start from left_columns_count to don't remap left keys twice. We need only qualified right keys here
745753
/// `result_sample_block` may contains non unique column names, need to set index for all of them
746754
for (size_t result_pos = left_columns_count; result_pos < result_sample_block.columns(); ++result_pos)
747755
{
748756
const auto & result_name = result_sample_block.getByPosition(result_pos).name;
749-
if (result_name == name)
757+
if (result_name == column_name)
750758
setRightIndex(right_pos, result_pos);
751759
}
752760
}

src/Interpreters/JoinUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class NotJoinedBlocks final : public IBlocksStream
138138
NotJoinedBlocks(std::unique_ptr<RightColumnsFiller> filler_,
139139
const Block & result_sample_block_,
140140
size_t left_columns_count,
141-
const LeftToRightKeyRemap & left_to_right_key_remap);
141+
const TableJoin & table_join);
142142

143143
Block nextImpl() override;
144144

src/Interpreters/MergeJoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ IBlocksStreamPtr MergeJoin::getNonJoinedBlocks(
11221122
size_t left_columns_count = left_sample_block.columns();
11231123
assert(left_columns_count == result_sample_block.columns() - right_columns_to_add.columns());
11241124
auto non_joined = std::make_unique<NotJoinedMerge>(*this, max_block_size);
1125-
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, table_join->leftToRightKeyRemap());
1125+
return std::make_unique<NotJoinedBlocks>(std::move(non_joined), result_sample_block, left_columns_count, *table_join);
11261126
}
11271127
return nullptr;
11281128
}

src/Interpreters/TableJoin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class TableJoin
346346
void setStorageJoin(std::shared_ptr<const IKeyValueEntity> storage);
347347
void setStorageJoin(std::shared_ptr<StorageJoin> storage);
348348

349-
std::shared_ptr<StorageJoin> getStorageJoin() { return right_storage_join; }
349+
std::shared_ptr<StorageJoin> getStorageJoin() const { return right_storage_join; }
350350

351351
bool isSpecialStorage() const { return !right_storage_name.empty() || right_storage_join || right_kv_storage; }
352352

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2 2 2
2+
3 0 3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS t1;
2+
DROP TABLE IF EXISTS t2;
3+
4+
CREATE TABLE t1 (key UInt64, a UInt64) ENGINE = Memory;
5+
CREATE TABLE t2 (key UInt64, a UInt64) ENGINE = Join(ALL, RIGHT, key);
6+
7+
INSERT INTO t1 VALUES (1, 1), (2, 2);
8+
INSERT INTO t2 VALUES (2, 2), (3, 3);
9+
10+
SELECT * FROM t1 ALL RIGHT JOIN t2 USING (key) ORDER BY key;

0 commit comments

Comments
 (0)