hello-stephen commented on code in PR #69674:
URL: https://github.com/apache/doris/pull/69674#discussion_r4795235830


##########
be/src/core/column/column_struct.cpp:
##########
@@ -218,24 +221,96 @@ void ColumnStruct::update_hash_with_value(size_t n, 
SipHash& hash) const {
     }
 }
 
+namespace {
+
+// Invoke `func(run_start, run_end)` for every maximal run of rows in [start, 
end) that are not
+// NULL according to `null_data`. An all-zero mask yields the single run 
[start, end), so the
+// masked and unmasked range paths produce identical hashes and the same 
number of calls.
+template <typename Func>
+void for_each_non_null_run(const uint8_t* __restrict null_data, size_t start, 
size_t end,
+                           Func&& func) {
+    size_t i = start;
+    while (i < end) {
+        while (i < end && null_data[i] != 0) {
+            ++i;
+        }
+        size_t run_start = i;
+        while (i < end && null_data[i] == 0) {
+            ++i;
+        }
+        if (run_start < i) {
+            func(run_start, i);
+        }
+    }
+}
+
+// Batch-hash every row through the field columns, then restore the rows that 
are NULL at the
+// outer level. Surviving rows keep each field's batch hash contract (which 
may differ from the
+// single/range contract, e.g. the width of a NULL default), and the field 
columns are still
+// called once per column instead of once per row.
+template <typename HashType, typename Func>
+void batch_hash_skipping_null_rows(HashType* __restrict hashes, size_t rows,
+                                   const uint8_t* __restrict null_data, Func&& 
hash_all_rows) {
+    // No field hash can survive when every row is NULL; retain the incoming 
seeds.
+    if (!simd::contain_zero(null_data, rows)) {
+        return;
+    }
+    DorisVector<HashType> saved(hashes, hashes + rows);
+    hash_all_rows();

Review Comment:
   [P2] Route child hash writes through the helper's restricted pointer
   
   On a mixed-null batch the helper saves and restores entries through its 
`__restrict hashes` parameter. Both `hash_all_rows` lambdas write those same 
entries through the enclosing member function's captured pointer, which is a 
separate alias. This violates the helper's aliasing contract and can let the 
optimizer reorder accesses across the save, child writes and restore.
   
   Pass `hashes` to the callback and use that argument for its child calls, or 
remove `__restrict` from the helper parameter.



##########
be/src/core/column/column_struct.cpp:
##########
@@ -218,24 +221,96 @@ void ColumnStruct::update_hash_with_value(size_t n, 
SipHash& hash) const {
     }
 }
 
+namespace {
+
+// Invoke `func(run_start, run_end)` for every maximal run of rows in [start, 
end) that are not
+// NULL according to `null_data`. An all-zero mask yields the single run 
[start, end), so the
+// masked and unmasked range paths produce identical hashes and the same 
number of calls.
+template <typename Func>
+void for_each_non_null_run(const uint8_t* __restrict null_data, size_t start, 
size_t end,
+                           Func&& func) {
+    size_t i = start;
+    while (i < end) {
+        while (i < end && null_data[i] != 0) {
+            ++i;
+        }
+        size_t run_start = i;
+        while (i < end && null_data[i] == 0) {
+            ++i;
+        }
+        if (run_start < i) {
+            func(run_start, i);
+        }
+    }
+}
+
+// Batch-hash every row through the field columns, then restore the rows that 
are NULL at the
+// outer level. Surviving rows keep each field's batch hash contract (which 
may differ from the
+// single/range contract, e.g. the width of a NULL default), and the field 
columns are still
+// called once per column instead of once per row.
+template <typename HashType, typename Func>
+void batch_hash_skipping_null_rows(HashType* __restrict hashes, size_t rows,
+                                   const uint8_t* __restrict null_data, Func&& 
hash_all_rows) {
+    // No field hash can survive when every row is NULL; retain the incoming 
seeds.
+    if (!simd::contain_zero(null_data, rows)) {
+        return;
+    }
+    DorisVector<HashType> saved(hashes, hashes + rows);
+    hash_all_rows();
+    for (size_t i = 0; i < rows; ++i) {
+        if (null_data[i] != 0) {
+            hashes[i] = saved[i];
+        }
+    }
+}
+
+} // namespace
+
+// `null_data` is the null map of an outer Nullable wrapping this struct. A 
NULL struct row
+// may still carry arbitrary payload in the field columns (e.g. produced by 
IF/CASE), so those
+// rows must be skipped here rather than hashed by their hidden payload. The 
field columns are
+// hashed with a null `null_data` because their own NULL semantics are handled 
by themselves.
 void ColumnStruct::update_xxHash_with_value(size_t start, size_t end, 
uint64_t& hash,
                                             const uint8_t* __restrict 
null_data) const {
-    for (const auto& column : columns) {
-        column->update_xxHash_with_value(start, end, hash, nullptr);
+    if (null_data) {
+        for (const auto& column : columns) {
+            for_each_non_null_run(null_data, start, end, [&](size_t run_start, 
size_t run_end) {

Review Comment:
   [P1] Keep NULL and zero-valued struct elements distinct in array keys
   
   For an `ARRAY<STRUCT<x:INT>>` column containing `[NULL]` and `[{x:0}]`, this 
range now produces the same key for both. 
`ColumnNullable::update_xxHash_with_value` hashes the NULL marker as four zero 
bytes and the masked STRUCT then skips its fields. The present struct hashes 
its INT zero as the same four bytes. `ColumnArray` delegates to that element 
range, and `AggregateFunctionUniqExactData::get_key` uses the resulting 
`UInt64` as the complete key. `multi_distinct_count` therefore returns 1 
instead of 2, including when every BE runs this head. In the base, the NULL 
struct also hashes its default child, so the two values have different keys.
   
   Please preserve a distinct encoding for a NULL struct element while 
excluding its hidden payload, and add a same-version `multi_distinct_count` 
regression for this pair.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to