diff --git a/dbms/src/Interpreters/Join.cpp b/dbms/src/Interpreters/Join.cpp index a0c667da175..110cce8e7da 100644 --- a/dbms/src/Interpreters/Join.cpp +++ b/dbms/src/Interpreters/Join.cpp @@ -330,6 +330,7 @@ void Join::setSampleBlock(const Block & block) sample_block_with_columns_to_add = materializeBlock(block); + LOG_DEBUG(log, "setSampleBlock sample_block_with_columns_to_add " << sample_block_with_columns_to_add.dumpStructure()); /// Move from `sample_block_with_columns_to_add` key columns to `sample_block_with_keys`, keeping the order. size_t pos = 0; @@ -361,6 +362,9 @@ void Join::setSampleBlock(const Block & block) if (use_nulls && isLeftOrFull(kind)) for (size_t i = 0; i < num_columns_to_add; ++i) convertColumnToNullable(sample_block_with_columns_to_add.getByPosition(i)); + + LOG_DEBUG(log, "setSampleBlock sample_block_with_keys " << sample_block_with_keys.dumpStructure()); + LOG_DEBUG(log, "setSampleBlock sample_block_with_columns_to_add " << sample_block_with_columns_to_add.dumpStructure()); } namespace @@ -618,6 +622,7 @@ public: void fillRightIndices(const Block& rhs_block) { + std::cout << "rhs_block=" << rhs_block.dumpStructure() << std::endl; for(auto& tn : type_name) { right_indexes.push_back(rhs_block.getPositionByName(tn.second)); } @@ -861,8 +866,9 @@ void Join::joinBlockImpl( AddedColumns added(sample_block_with_columns_to_add, block_with_columns_to_add, block); + // the last column in the key names is the asof column if constexpr (STRICTNESS == ASTTableJoin::Strictness::Asof) - added.addColumn(sample_block_with_keys.safeGetByPosition(sample_block_with_keys.columns()-1)); + added.addColumn(sample_block_with_keys.getByName(key_names_right.back())); if(!blocks.empty()) { added.fillRightIndices(*blocks.begin()); diff --git a/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.reference b/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.reference index 2e22280c8da..bb199d0159a 100644 --- a/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.reference +++ b/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.reference @@ -3,3 +3,13 @@ 1 103 3 2 102 1 1 104 4 4 104 1 1 105 5 4 104 1 +1 101 1 0 0 0 +1 102 2 2 102 1 +1 103 3 2 102 1 +1 104 4 4 104 1 +1 105 5 4 104 1 +1 101 1 0 0 0 +1 102 2 2 102 1 +1 103 3 2 102 1 +1 104 4 4 104 1 +1 105 5 4 104 1 diff --git a/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.sql b/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.sql index 27860bb5d05..a813f2fa410 100644 --- a/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.sql +++ b/dbms/tests/queries/0_stateless/00927_asof_join_correct_bt.sql @@ -8,8 +8,18 @@ INSERT INTO A(k,t,a) VALUES (1,101,1),(1,102,2),(1,103,3),(1,104,4),(1,105,5); CREATE TABLE B(k UInt32, t UInt32, b UInt64) ENGINE = MergeTree() ORDER BY (k, t); INSERT INTO B(k,t,b) VALUES (1,102,2), (1,104,4); - SELECT A.k, A.t, A.a, B.b, B.t, B.k FROM A ASOF LEFT JOIN B USING(k,t) ORDER BY (A.k, A.t); +DROP TABLE B; + + +CREATE TABLE B(t UInt32, k UInt32, b UInt64) ENGINE = MergeTree() ORDER BY (k, t); +INSERT INTO B(k,t,b) VALUES (1,102,2), (1,104,4); +SELECT A.k, A.t, A.a, B.b, B.t, B.k FROM A ASOF LEFT JOIN B USING(k,t) ORDER BY (A.k, A.t); +DROP TABLE B; + +CREATE TABLE B(k UInt32, b UInt64, t UInt32) ENGINE = MergeTree() ORDER BY (k, t); +INSERT INTO B(k,t,b) VALUES (1,102,2), (1,104,4); +SELECT A.k, A.t, A.a, B.b, B.t, B.k FROM A ASOF LEFT JOIN B USING(k,t) ORDER BY (A.k, A.t); +DROP TABLE B; DROP TABLE A; -DROP TABLE B;