This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 3652cd6ce56 branch-4.2: [fix](be) Avoid mutating shared columns 
without COW #70777 (#70854)
3652cd6ce56 is described below

commit 3652cd6ce56220655af732d1b32c767d6d74b58e
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Nov 24 18:46:37 2026 +0800

    branch-4.2: [fix](be) Avoid mutating shared columns without COW #70777 
(#70854)
    
    Cherry-picked from #70777
    
    Co-authored-by: linrrarity <[email protected]>
---
 be/src/core/block/block.cpp                        |  3 ++-
 be/src/exec/common/hash_table/hash_map_context.h   | 28 ++++++++++++----------
 .../operator/join/process_hash_table_probe_impl.h  | 18 +++++++++-----
 be/test/core/block/block_test.cpp                  |  4 ++++
 be/test/exec/hash_map/hash_table_method_test.cpp   | 14 +++++++++++
 .../join/test_hash_join_probe_side_zero_copy.out   |  7 ++++++
 .../test_hash_join_probe_side_zero_copy.groovy     |  8 +++++++
 7 files changed, 63 insertions(+), 19 deletions(-)

diff --git a/be/src/core/block/block.cpp b/be/src/core/block/block.cpp
index 59b7124101b..ee5c6e84609 100644
--- a/be/src/core/block/block.cpp
+++ b/be/src/core/block/block.cpp
@@ -971,8 +971,9 @@ Status Block::filter_block(Block* block, const 
std::vector<uint32_t>& columns_to
     if (const auto* nullable_column = 
check_and_get_column<ColumnNullable>(*filter_column)) {
         const auto& nested_column = nullable_column->get_nested_column_ptr();
 
+        // Another block can own the same nullable wrapper without increasing 
the nested count.
         MutableColumnPtr mutable_holder =
-                nested_column->use_count() == 1
+                nullable_column->is_exclusive()
                         ? nested_column->assert_mutable()
                         : nested_column->clone_resized(nested_column->size());
 
diff --git a/be/src/exec/common/hash_table/hash_map_context.h 
b/be/src/exec/common/hash_table/hash_map_context.h
index afb7ae3fc9d..9c3333e5545 100644
--- a/be/src/exec/common/hash_table/hash_map_context.h
+++ b/be/src/exec/common/hash_table/hash_map_context.h
@@ -952,23 +952,27 @@ struct MethodKeysFixed : public MethodBase<TData> {
         for (size_t j = 0; j < key_columns.size(); ++j) {
             auto goo = [&]<typename Fixed, bool aligned>(Fixed zero) {
                 CHECK_EQ(sizeof(Fixed), key_sizes[j]);
-                if (has_null_column.size() && has_null_column[j]) {
-                    const auto* nullmap =
-                            assert_cast<const 
ColumnUInt8&>(*nullmap_columns[j]).get_data().data();
-                    // make sure null cell is filled by 0x0
-                    // This mutates the same underlying buffer returned by 
get_raw_data(), so get
-                    // the restricted data pointer only after the replacement 
finishes.
-                    
const_cast<IColumn*>(key_columns[j])->replace_column_null_data(nullmap);
-                }
                 auto* __restrict current = result_data + offset;
                 // Do not hoist data out of goo. A reference capture is 
lowered to an ordinary
                 // closure field, so restrict/noalias information is lost when 
goo is not inlined,
                 // which prevents the copy loop from being optimized.
                 const char* __restrict data = 
key_columns[j]->get_raw_data().data;
-                for (size_t i = 0; i < row_numbers; ++i) {
-                    memcpy_fixed<Fixed, aligned>(current, data);
-                    current += sizeof(T);
-                    data += sizeof(Fixed);
+                // Write canonical NULL bytes into the packed key; other keys 
can share the input.
+                if (!has_null_column.empty() && has_null_column[j]) {
+                    const auto& nullmap =
+                            assert_cast<const 
ColumnUInt8&>(*nullmap_columns[j]).get_data();
+                    for (size_t i = 0; i < row_numbers; ++i) {
+                        memcpy_fixed<Fixed, aligned>(
+                                current, nullmap[i] ? reinterpret_cast<const 
char*>(&zero) : data);
+                        current += sizeof(T);
+                        data += sizeof(Fixed);
+                    }
+                } else {
+                    for (size_t i = 0; i < row_numbers; ++i) {
+                        memcpy_fixed<Fixed, aligned>(current, data);
+                        current += sizeof(T);
+                        data += sizeof(Fixed);
+                    }
                 }
             };
             auto foo = [&]<typename Fixed>(Fixed zero) {
diff --git a/be/src/exec/operator/join/process_hash_table_probe_impl.h 
b/be/src/exec/operator/join/process_hash_table_probe_impl.h
index bbdbae53c78..0cc99fe0edd 100644
--- a/be/src/exec/operator/join/process_hash_table_probe_impl.h
+++ b/be/src/exec/operator/join/process_hash_table_probe_impl.h
@@ -235,12 +235,18 @@ typename HashTableType::State 
ProcessHashTableProbe<JoinOpType>::_init_probe_sid
     if (!_parent->_ready_probe) {
         _parent->_ready_probe = true;
         hash_table_ctx.arena.clear();
-        // In order to make the null keys equal when using single null eq, all 
null keys need to be set to default value.
-        if (_parent->_probe_columns.size() == 1 && null_map) {
-            if (simd::contain_one(null_map, probe_rows)) {
-                const_cast<IColumn*>(_parent->_probe_columns[0])
-                        ->replace_column_null_data(null_map);
-            }
+        // A retained NULL bucket compares key payloads. Keep the normalized 
probe key alive
+        // across pulls, since the input may also be an output column (e.g. 
NULLIF(a, 1)).
+        // String keys already produce an empty StringRef for NULL during 
serialization.
+        if (_parent->_probe_columns.size() == 1 && null_map &&
+            hash_table_ctx.hash_table->keep_null_key() &&
+            !_parent->_probe_columns[0]->is_column_string() &&
+            simd::contain_one(null_map, probe_rows)) {
+            const auto* key = _parent->_probe_columns[0];
+            auto normalized = IColumn::mutate(key->get_ptr());
+            normalized->replace_column_null_data(null_map);
+            _parent->_key_columns_holder.emplace_back(std::move(normalized));
+            _parent->_probe_columns[0] = 
_parent->_key_columns_holder.back().get();
         }
 
         hash_table_ctx.init_serialized_keys(_parent->_probe_columns, 
probe_rows, null_map, true,
diff --git a/be/test/core/block/block_test.cpp 
b/be/test/core/block/block_test.cpp
index 87e189b2d4a..8ab046f5cd3 100644
--- a/be/test/core/block/block_test.cpp
+++ b/be/test/core/block/block_test.cpp
@@ -1314,9 +1314,13 @@ TEST(BlockTest, filter) {
 
         
block.insert(ColumnHelper::create_nullable_column_with_name<DataTypeUInt8>({1, 
1, 1},
                                                                                
    {0, 1, 0}));
+        const auto shared_filter = block.get_by_position(2).column;
         auto st = Block::filter_block(&block, {0, 1}, 2, 2);
         ASSERT_TRUE(st.ok()) << st.to_string();
         ASSERT_EQ(block.rows(), 2);
+        EXPECT_EQ(block.get_by_position(0).column->get_int(1), 3);
+        EXPECT_EQ(assert_cast<const 
ColumnNullable&>(*shared_filter).get_nested_column().get_int(1),
+                  1);
     }
 
     {
diff --git a/be/test/exec/hash_map/hash_table_method_test.cpp 
b/be/test/exec/hash_map/hash_table_method_test.cpp
index e556e2669a5..bc71cf37344 100644
--- a/be/test/exec/hash_map/hash_table_method_test.cpp
+++ b/be/test/exec/hash_map/hash_table_method_test.cpp
@@ -102,6 +102,20 @@ TEST(HashTableMethodTest, testMethodFixed) {
               {0, 1, -1, 3, -1, 4});
 }
 
+TEST(HashTableMethodTest, FixedKeysPreserveSharedPayload) {
+    MethodKeysFixed<PHHashMap<UInt64, IColumn::ColumnIndex, 
HashCRC32<UInt64>>> method(
+            Sizes {sizeof(int16_t), sizeof(int16_t)});
+    auto values = ColumnHelper::create_column<DataTypeInt16>({7, 9, 7, 0});
+    auto nulls = ColumnHelper::create_column<DataTypeUInt8>({1, 1, 0, 0});
+    test_insert(method, {ColumnNullable::create(values, nulls), values});
+    EXPECT_EQ(values->get_int(0), 7);
+    EXPECT_EQ(values->get_int(1), 9);
+
+    auto probe = ColumnHelper::create_column<DataTypeInt16>({55, 55, 7, 0});
+    test_find(method, {ColumnNullable::create(probe, nulls), values}, {0, 1, 
2, 3});
+    EXPECT_EQ(probe->get_int(0), 55);
+}
+
 TEST(HashTableMethodTest, testMethodSerialized) {
     MethodSerialized<StringHashMap<IColumn::ColumnIndex>> method;
 
diff --git 
a/regression-test/data/query_p0/join/test_hash_join_probe_side_zero_copy.out 
b/regression-test/data/query_p0/join/test_hash_join_probe_side_zero_copy.out
index 80a9128c28a..74090c72c34 100644
--- a/regression-test/data/query_p0/join/test_hash_join_probe_side_zero_copy.out
+++ b/regression-test/data/query_p0/join/test_hash_join_probe_side_zero_copy.out
@@ -14,3 +14,10 @@
 -- !reuse_probe_block_after_transfer --
 4      40
 6      60
+
+-- !shared_nullable_join_key --
+0      0
+1      2
+2      \N
+3      3
+
diff --git 
a/regression-test/suites/query_p0/join/test_hash_join_probe_side_zero_copy.groovy
 
b/regression-test/suites/query_p0/join/test_hash_join_probe_side_zero_copy.groovy
index 655387838ee..2d442ba8736 100644
--- 
a/regression-test/suites/query_p0/join/test_hash_join_probe_side_zero_copy.groovy
+++ 
b/regression-test/suites/query_p0/join/test_hash_join_probe_side_zero_copy.groovy
@@ -108,4 +108,12 @@ suite("test_hash_join_probe_side_zero_copy", "query,p0") {
           ON p.k = b.k + 3 AND p.n < b.v
         ORDER BY p.k, p.n
     """
+
+    qt_shared_nullable_join_key """
+        SELECT l.number, r.number
+        FROM numbers("number" = "4") l
+        LEFT JOIN [broadcast] numbers("number" = "4") r
+            ON NULLIF(l.number, 1) <=> NULLIF(r.number, 2)
+        ORDER BY l.number, r.number
+    """
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to