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

Mryange pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


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

commit e13882c29f02fb5ad9fa28d61acbcf86258d6dca
Author: linrrarity <[email protected]>
AuthorDate: Mon Nov 23 09:56:56 2026 +0800

    [fix](be) Avoid mutating shared columns without COW (#70777)
    
    ### Background
    
    `NULLIF(k, 1)` can share its value buffer with `k`. When `k` is `1`, the
    expression marks the row as NULL, but the original `1` must remain
    available to any expression that reads `k`.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Bugfix
    
    A query that joins on `NULLIF(l.k, 1) <=> NULLIF(r.k, 2)` and selects
    `l.k, r.k` should return `(1, 2)` for the matching NULL keys. Preparing
    the probe key could write `0` into the buffer shared with `l.k`, so the
    join could find the right match but return the wrong value.
    
    ### Details
    
    - **Hash join probe.** When a non-string probe key needs a default value
    for NULL comparisons, use `IColumn::mutate` to obtain a writable key.
    The join keeps that key alive until it finishes probing the input block.
    The projected column retains its original values.
    - **Fixed-width composite keys.** Write zero bytes for NULL directly
    into the packed key buffer. Previously, if the two fields in `(NULL, 7)`
    shared a value buffer, packing the first field could overwrite the `7`
    before the second field was read.
    - **Nullable filters.** Two blocks can share one `ColumnNullable` even
    when its nested column has a reference count of one.
    `Block::filter_block` now checks `ColumnNullable::is_exclusive()` before
    applying the NULL mask in place. When the column is shared, it applies
    the mask to a copy.
    
    ### Testing
    
    The BE and FE builds passed. All 53 selected ASAN unit tests and
    `clang-tidy` passed. The unit tests check that `(NULL, 7)` and `(NULL,
    9)` remain distinct hash keys and that filtering preserves the
    underlying value observed by a second owner.
    
    The `test_hash_join_probe_side_zero_copy` P0 suite passed, including the
    regression case that checks the join returns `(1, 2)`.
    
    ### Release note
    
    Fix incorrect query results when handling NULLs overwrites column values
    shared with another hash key or output column.
    
    ### Check List (For Author)
    
    - Behavior changed: Yes (see Details).
    - Does this need documentation: No.
    - Pre-submission status: Ready.
    
    ### AI Disclosure
    
    - Tool: OpenAI Codex, GPT-6.
    - Generated: Implementation, tests, and PR description.
    - Human review: Pending.
---
 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 930a639ab16..d4006109700 100644
--- a/be/src/core/block/block.cpp
+++ b/be/src/core/block/block.cpp
@@ -998,8 +998,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 fb8184f9fea..5010abdabb2 100644
--- a/be/src/exec/common/hash_table/hash_map_context.h
+++ b/be/src/exec/common/hash_table/hash_map_context.h
@@ -956,23 +956,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 4e726665a46..d093b6d7dea 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
@@ -225,12 +225,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 566eaccc982..2fe6a0d3645 100644
--- a/be/test/core/block/block_test.cpp
+++ b/be/test/core/block/block_test.cpp
@@ -1324,9 +1324,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 0fcab89040d..2380fee2950 100644
--- a/be/test/exec/hash_map/hash_table_method_test.cpp
+++ b/be/test/exec/hash_map/hash_table_method_test.cpp
@@ -103,6 +103,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