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


##########
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) {
+                column->update_xxHash_with_value(run_start, run_end, hash, 
nullptr);

Review Comment:
   [P1] Preserve the hash-key contract of existing multi_distinct_count states
   
   This also changes the key produced for an ARRAY<STRUCT<x:INT>> containing a 
NULL struct element, not just the shuffle hash. For the same typed array [NULL] 
with a default INT field, the old ColumnNullable/ColumnStruct range path 
performs the XXHash NULL-marker update and then hashes the field's default; the 
new path performs only the outer marker update.
   
   `AggregateFunctionUniqExactData::get_key` in 
`be/src/exprs/aggregate/aggregate_function_uniq.h:76-79` uses this range hash 
for TYPE_ARRAY, and its serialize/deserialize_and_merge methods (lines 224-244) 
store and merge only UInt64 keys. Explicit `multi_distinct_count(array_col)` 
and its `_state`/`_union`/`_merge` combinators are supported. An old persisted 
state for [NULL] merged with a new state for the same value can therefore count 
it twice, even after every BE is upgraded. There are no original array values 
in the state from which to rebuild the hash, and the existing 
DataTypeAggState/BeExecVersionManager compatibility check has no corresponding 
function-version registration.
   
   Please retain a compatible hash contract for old states or explicitly 
version/reject incompatible states with a rebuild/migration path, and cover 
mixed-version partial aggregation and persisted-state merging. A shuffle-only 
compatibility gate will not address this consumer.



##########
be/src/core/column/column_struct.cpp:
##########
@@ -259,15 +334,30 @@ void ColumnStruct::update_crcs_with_value(uint32_t* 
__restrict hash, PrimitiveTy
 
 void ColumnStruct::update_crc32c_batch(uint32_t* __restrict hashes,
                                        const uint8_t* __restrict null_map) 
const {
-    for (const auto& column : columns) {
-        column->update_crc32c_batch(hashes, nullptr);
+    auto hash_all_rows = [&]() {
+        for (const auto& column : columns) {
+            column->update_crc32c_batch(hashes, nullptr);
+        }
+    };
+    if (null_map) {
+        batch_hash_skipping_null_rows(hashes, size(), null_map, hash_all_rows);

Review Comment:
   [P1] Keep nullable STRUCT shuffle hashes consistent during rolling BE 
upgrades
   
   Even if two NULL STRUCT<x:INT> values have the same hidden zero field, the 
base BE hashes the outer NULL marker plus that field; the patched BE hashes 
only the outer marker. With the existing CRC32C shuffle seed 0x9e3779b9, these 
are respectively 0x3cd828a6 and 0x888097dd; `ShiftChannelIds` in partitioner.h 
routes them to channels 1 and 0 for two partitions.
   
   `ExchangeSinkLocalState::init` 
(be/src/exec/operator/exchange_sink_operator.cpp:129-138) selects CRC32C for 
remote HASH_PARTITIONED exchanges solely from `enable_new_shuffle_hash_method`, 
which defaults to true. During a rolling upgrade, a one-phase GROUP BY with 
scan senders on old and new BEs can consequently receive the same logical NULL 
key in different final aggregation instances and return two groups, even for 
canonical identical payloads that co-located before this PR.
   
   Please select a consistent query-wide hashing contract/capability for all 
participating BEs during upgrades and add cross-version coverage. Nested 
nullable STRUCTs also change legacy CRC range hashing, so merely disabling 
CRC32C is not a complete fallback for all affected key shapes.



-- 
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