Coverage Report

Created: 2025-11-03 18:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/hash_util.hpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/hash-util.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/Types_types.h>
24
#include <xxh3.h>
25
#include <zlib.h>
26
27
#include <bit>
28
#include <functional>
29
30
#include "common/compiler_util.h" // IWYU pragma: keep
31
#include "util/cpu_info.h"
32
#include "util/hash/city.h"
33
#include "util/murmur_hash3.h"
34
#include "util/sse_util.hpp"
35
36
namespace doris {
37
#include "common/compile_check_begin.h"
38
// Utility class to compute hash values.
39
class HashUtil {
40
public:
41
3.78M
    static uint32_t zlib_crc_hash(const void* data, uint32_t bytes, uint32_t hash) {
42
3.78M
        return (uint32_t)crc32(hash, (const unsigned char*)data, bytes);
43
3.78M
    }
44
45
1.08M
    static uint32_t zlib_crc_hash_null(uint32_t hash) {
46
        // null is treat as 0 when hash
47
1.08M
        static const int INT_VALUE = 0;
48
1.08M
        return (uint32_t)crc32(hash, (const unsigned char*)(&INT_VALUE), 4);
49
1.08M
    }
50
51
#if defined(__SSE4_2__) || defined(__aarch64__)
52
    // Compute the Crc32 hash for data using SSE4 instructions.  The input hash parameter is
53
    // the current hash/seed value.
54
    // This should only be called if SSE is supported.
55
    // This is ~4x faster than Fnv/Boost Hash.
56
    // NOTE: DO NOT use this method for checksum! This does not generate the standard CRC32 checksum!
57
    //       For checksum, use CRC-32C algorithm from crc32c.h
58
    // NOTE: Any changes made to this function need to be reflected in Codegen::GetHashFn.
59
    // TODO: crc32 hashes with different seeds do not result in different hash functions.
60
    // The resulting hashes are correlated.
61
173k
    static uint32_t crc_hash(const void* data, uint32_t bytes, uint32_t hash) {
62
173k
        if (!CpuInfo::is_supported(CpuInfo::SSE4_2)) {
63
0
            return zlib_crc_hash(data, bytes, hash);
64
0
        }
65
173k
        uint32_t words = bytes / sizeof(uint32_t);
66
173k
        bytes = bytes % sizeof(uint32_t);
67
68
173k
        const uint32_t* p = reinterpret_cast<const uint32_t*>(data);
69
70
312k
        while (words--) {
71
139k
            hash = _mm_crc32_u32(hash, *p);
72
139k
            ++p;
73
139k
        }
74
75
173k
        const uint8_t* s = reinterpret_cast<const uint8_t*>(p);
76
77
174k
        while (bytes--) {
78
1.40k
            hash = _mm_crc32_u8(hash, *s);
79
1.40k
            ++s;
80
1.40k
        }
81
82
        // The lower half of the CRC hash has has poor uniformity, so swap the halves
83
        // for anyone who only uses the first several bits of the hash.
84
173k
        hash = (hash << 16) | (hash >> 16);
85
173k
        return hash;
86
173k
    }
87
88
0
    static uint64_t crc_hash64(const void* data, uint32_t bytes, uint64_t hash) {
89
0
        uint32_t words = bytes / sizeof(uint32_t);
90
0
        bytes = bytes % sizeof(uint32_t);
91
0
92
0
        uint32_t h1 = hash >> 32;
93
0
        uint32_t h2 = (hash << 32) >> 32;
94
0
95
0
        const uint32_t* p = reinterpret_cast<const uint32_t*>(data);
96
0
        while (words--) {
97
0
            (words & 1) ? (h1 = _mm_crc32_u32(h1, *p)) : (h2 = _mm_crc32_u32(h2, *p));
98
0
            ++p;
99
0
        }
100
0
101
0
        const uint8_t* s = reinterpret_cast<const uint8_t*>(p);
102
0
        while (bytes--) {
103
0
            (bytes & 1) ? (h1 = _mm_crc32_u8(h1, *s)) : (h2 = _mm_crc32_u8(h2, *s));
104
0
            ++s;
105
0
        }
106
0
        union {
107
0
            uint64_t u64;
108
0
            uint32_t u32[2];
109
0
        } converter;
110
0
        converter.u64 = hash;
111
0
112
0
        h1 = (h1 << 16) | (h1 >> 16);
113
0
        h2 = (h2 << 16) | (h2 >> 16);
114
0
        converter.u32[0] = h1;
115
0
        converter.u32[1] = h2;
116
0
117
0
        return converter.u64;
118
0
    }
119
#else
120
    static uint32_t crc_hash(const void* data, uint32_t bytes, uint32_t hash) {
121
        return zlib_crc_hash(data, bytes, hash);
122
    }
123
#endif
124
125
    // refer to https://github.com/apache/commons-codec/blob/master/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
126
    static const uint32_t MURMUR3_32_SEED = 104729;
127
128
    // modify from https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
129
20
    static uint32_t murmur_hash3_32(const void* key, int64_t len, uint32_t seed) {
130
20
        uint32_t out = 0;
131
20
        murmur_hash3_x86_32(key, len, seed, &out);
132
20
        return out;
133
20
    }
134
135
    template <bool is_mmh64_v2>
136
15
    static uint64_t murmur_hash3_64(const void* key, int64_t len, uint64_t seed) {
137
15
        uint64_t out = 0;
138
15
        if constexpr (is_mmh64_v2) {
139
3
            murmur_hash3_x64_64_shared(key, len, seed, &out);
140
12
        } else {
141
12
            murmur_hash3_x64_64(key, len, seed, &out);
142
12
        }
143
15
        return out;
144
15
    }
_ZN5doris8HashUtil15murmur_hash3_64ILb0EEEmPKvlm
Line
Count
Source
136
12
    static uint64_t murmur_hash3_64(const void* key, int64_t len, uint64_t seed) {
137
12
        uint64_t out = 0;
138
        if constexpr (is_mmh64_v2) {
139
            murmur_hash3_x64_64_shared(key, len, seed, &out);
140
12
        } else {
141
12
            murmur_hash3_x64_64(key, len, seed, &out);
142
12
        }
143
12
        return out;
144
12
    }
_ZN5doris8HashUtil15murmur_hash3_64ILb1EEEmPKvlm
Line
Count
Source
136
3
    static uint64_t murmur_hash3_64(const void* key, int64_t len, uint64_t seed) {
137
3
        uint64_t out = 0;
138
3
        if constexpr (is_mmh64_v2) {
139
3
            murmur_hash3_x64_64_shared(key, len, seed, &out);
140
        } else {
141
            murmur_hash3_x64_64(key, len, seed, &out);
142
        }
143
3
        return out;
144
3
    }
145
146
    static const int MURMUR_R = 47;
147
148
    // Murmur2 hash implementation returning 64-bit hashes.
149
0
    static uint64_t murmur_hash2_64(const void* input, int len, uint64_t seed) {
150
0
        uint64_t h = seed ^ (len * MURMUR_PRIME);
151
0
152
0
        const uint64_t* data = reinterpret_cast<const uint64_t*>(input);
153
0
        const uint64_t* end = data + (len / sizeof(uint64_t));
154
0
155
0
        while (data != end) {
156
0
            uint64_t k = *data++;
157
0
            k *= MURMUR_PRIME;
158
0
            k ^= k >> MURMUR_R;
159
0
            k *= MURMUR_PRIME;
160
0
            h ^= k;
161
0
            h *= MURMUR_PRIME;
162
0
        }
163
0
164
0
        const uint8_t* data2 = reinterpret_cast<const uint8_t*>(data);
165
0
        switch (len & 7) {
166
0
        case 7:
167
0
            h ^= uint64_t(data2[6]) << 48;
168
0
            [[fallthrough]];
169
0
        case 6:
170
0
            h ^= uint64_t(data2[5]) << 40;
171
0
            [[fallthrough]];
172
0
        case 5:
173
0
            h ^= uint64_t(data2[4]) << 32;
174
0
            [[fallthrough]];
175
0
        case 4:
176
0
            h ^= uint64_t(data2[3]) << 24;
177
0
            [[fallthrough]];
178
0
        case 3:
179
0
            h ^= uint64_t(data2[2]) << 16;
180
0
            [[fallthrough]];
181
0
        case 2:
182
0
            h ^= uint64_t(data2[1]) << 8;
183
0
            [[fallthrough]];
184
0
        case 1:
185
0
            h ^= uint64_t(data2[0]);
186
0
            h *= MURMUR_PRIME;
187
0
        }
188
0
189
0
        h ^= h >> MURMUR_R;
190
0
        h *= MURMUR_PRIME;
191
0
        h ^= h >> MURMUR_R;
192
0
        return h;
193
0
    }
194
195
    // default values recommended by http://isthe.com/chongo/tech/comp/fnv/
196
    static const uint32_t FNV_PRIME = 0x01000193; //   16777619
197
    static const uint32_t FNV_SEED = 0x811C9DC5;  // 2166136261
198
    static const uint64_t FNV64_PRIME = 1099511628211UL;
199
    static const uint64_t FNV64_SEED = 14695981039346656037UL;
200
    static const uint64_t MURMUR_PRIME = 0xc6a4a7935bd1e995ULL;
201
    static const uint32_t MURMUR_SEED = 0xadc83b19ULL;
202
    // Implementation of the Fowler–Noll–Vo hash function.  This is not as performant
203
    // as boost's hash on int types (2x slower) but has bit entropy.
204
    // For ints, boost just returns the value of the int which can be pathological.
205
    // For example, if the data is <1000, 2000, 3000, 4000, ..> and then the mod of 1000
206
    // is taken on the hash, all values will collide to the same bucket.
207
    // For string values, Fnv is slightly faster than boost.
208
0
    static uint32_t fnv_hash(const void* data, uint32_t bytes, uint32_t hash) {
209
0
        const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
210
211
0
        while (bytes--) {
212
0
            hash = (*ptr ^ hash) * FNV_PRIME;
213
0
            ++ptr;
214
0
        }
215
216
0
        return hash;
217
0
    }
218
219
0
    static uint64_t fnv_hash64(const void* data, uint32_t bytes, uint64_t hash) {
220
0
        const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
221
0
222
0
        while (bytes--) {
223
0
            hash = (*ptr ^ hash) * FNV64_PRIME;
224
0
            ++ptr;
225
0
        }
226
0
227
0
        return hash;
228
0
    }
229
230
    // Our hash function is MurmurHash2, 64 bit version.
231
    // It was modified in order to provide the same result in
232
    // big and little endian archs (endian neutral).
233
67.8k
    static uint64_t murmur_hash64A(const void* key, int64_t len, unsigned int seed) {
234
67.8k
        const uint64_t m = MURMUR_PRIME;
235
67.8k
        const int r = 47;
236
67.8k
        uint64_t h = seed ^ (len * m);
237
67.8k
        const uint8_t* data = (const uint8_t*)key;
238
67.8k
        const uint8_t* end = data + (len - (len & 7));
239
240
135k
        while (data != end) {
241
67.8k
            uint64_t k;
242
            if constexpr (std::endian::native == std::endian::big) {
243
                k = (uint64_t)data[0];
244
                k |= (uint64_t)data[1] << 8;
245
                k |= (uint64_t)data[2] << 16;
246
                k |= (uint64_t)data[3] << 24;
247
                k |= (uint64_t)data[4] << 32;
248
                k |= (uint64_t)data[5] << 40;
249
                k |= (uint64_t)data[6] << 48;
250
                k |= (uint64_t)data[7] << 56;
251
67.8k
            } else if constexpr (std::endian::native == std::endian::little) {
252
67.8k
                memcpy(&k, data, sizeof(k));
253
            } else {
254
                static_assert(std::endian::native == std::endian::big ||
255
                                      std::endian::native == std::endian::little,
256
                              "Unsupported endianness");
257
            }
258
259
67.8k
            k *= m;
260
67.8k
            k ^= k >> r;
261
67.8k
            k *= m;
262
67.8k
            h ^= k;
263
67.8k
            h *= m;
264
67.8k
            data += 8;
265
67.8k
        }
266
267
67.8k
        switch (len & 7) {
268
0
        case 7:
269
0
            h ^= (uint64_t)data[6] << 48;
270
0
            [[fallthrough]];
271
0
        case 6:
272
0
            h ^= (uint64_t)data[5] << 40;
273
0
            [[fallthrough]];
274
0
        case 5:
275
0
            h ^= (uint64_t)data[4] << 32;
276
0
            [[fallthrough]];
277
3
        case 4:
278
3
            h ^= (uint64_t)data[3] << 24;
279
3
            [[fallthrough]];
280
3
        case 3:
281
3
            h ^= (uint64_t)data[2] << 16;
282
3
            [[fallthrough]];
283
3
        case 2:
284
3
            h ^= (uint64_t)data[1] << 8;
285
3
            [[fallthrough]];
286
6
        case 1:
287
6
            h ^= (uint64_t)data[0];
288
6
            h *= m;
289
67.8k
        }
290
291
67.8k
        h ^= h >> r;
292
67.8k
        h *= m;
293
67.8k
        h ^= h >> r;
294
67.8k
        return h;
295
67.8k
    }
296
297
    // Computes the hash value for data.  Will call either CrcHash or FnvHash
298
    // depending on hardware capabilities.
299
    // Seed values for different steps of the query execution should use different seeds
300
    // to prevent accidental key collisions. (See IMPALA-219 for more details).
301
173k
    static uint32_t hash(const void* data, uint32_t bytes, uint32_t seed) {
302
173k
#ifdef __SSE4_2__
303
304
173k
        if (LIKELY(CpuInfo::is_supported(CpuInfo::SSE4_2))) {
305
173k
            return crc_hash(data, bytes, seed);
306
173k
        } else {
307
0
            return fnv_hash(data, bytes, seed);
308
0
        }
309
310
#else
311
        return fnv_hash(data, bytes, seed);
312
#endif
313
173k
    }
314
315
76.5k
    static uint64_t hash64(const void* data, uint64_t bytes, uint64_t seed) {
316
#ifdef _SSE4_2_
317
        if (LIKELY(CpuInfo::is_supported(CpuInfo::SSE4_2))) {
318
            return crc_hash64(data, bytes, seed);
319
320
        } else {
321
            uint64_t hash = 0;
322
            murmur_hash3_x64_64(data, bytes, seed, &hash);
323
            return hash;
324
        }
325
#else
326
76.5k
        uint64_t hash = 0;
327
76.5k
        murmur_hash3_x64_64(data, bytes, seed, &hash);
328
76.5k
        return hash;
329
76.5k
#endif
330
76.5k
    }
331
    // hash_combine is the same with boost hash_combine,
332
    // except replace boost::hash with std::hash
333
    template <class T>
334
353
    static inline void hash_combine(std::size_t& seed, const T& v) {
335
353
        std::hash<T> hasher;
336
353
        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
337
353
    }
_ZN5doris8HashUtil12hash_combineINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRmRKT_
Line
Count
Source
334
351
    static inline void hash_combine(std::size_t& seed, const T& v) {
335
351
        std::hash<T> hasher;
336
351
        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
337
351
    }
_ZN5doris8HashUtil12hash_combineIlEEvRmRKT_
Line
Count
Source
334
2
    static inline void hash_combine(std::size_t& seed, const T& v) {
335
2
        std::hash<T> hasher;
336
2
        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
337
2
    }
338
339
#if defined(__clang__)
340
#pragma clang diagnostic push
341
#pragma clang diagnostic ignored "-Wused-but-marked-unused"
342
#endif
343
    // xxHash function for a byte array.  For convenience, a 64-bit seed is also
344
    // hashed into the result.  The mapping may change from time to time.
345
24
    static xxh_u32 xxHash32WithSeed(const char* s, size_t len, xxh_u32 seed) {
346
24
        return XXH32(s, len, seed);
347
24
    }
348
349
    // same to the up function, just for null value
350
0
    static xxh_u32 xxHash32NullWithSeed(xxh_u32 seed) {
351
0
        static const int INT_VALUE = 0;
352
0
        return XXH32(reinterpret_cast<const char*>(&INT_VALUE), sizeof(int), seed);
353
0
    }
354
355
149k
    static xxh_u64 xxHash64WithSeed(const char* s, size_t len, xxh_u64 seed) {
356
149k
        return XXH3_64bits_withSeed(s, len, seed);
357
149k
    }
358
359
    // same to the up function, just for null value
360
1.08M
    static xxh_u64 xxHash64NullWithSeed(xxh_u64 seed) {
361
1.08M
        static const int INT_VALUE = 0;
362
1.08M
        return XXH3_64bits_withSeed(reinterpret_cast<const char*>(&INT_VALUE), sizeof(int), seed);
363
1.08M
    }
364
365
#if defined(__clang__)
366
#pragma clang diagnostic pop
367
#endif
368
};
369
370
} // namespace doris
371
372
template <>
373
struct std::hash<doris::TUniqueId> {
374
7.65k
    size_t operator()(const doris::TUniqueId& id) const {
375
7.65k
        uint32_t seed = 0;
376
7.65k
        seed = doris::HashUtil::hash(&id.lo, sizeof(id.lo), seed);
377
7.65k
        seed = doris::HashUtil::hash(&id.hi, sizeof(id.hi), seed);
378
7.65k
        return seed;
379
7.65k
    }
380
};
381
382
template <>
383
struct std::hash<doris::TNetworkAddress> {
384
0
    size_t operator()(const doris::TNetworkAddress& address) const {
385
0
        uint32_t seed = 0;
386
0
        seed = doris::HashUtil::hash(address.hostname.data(), (uint32_t)address.hostname.size(),
387
0
                                     seed);
388
0
        seed = doris::HashUtil::hash(&address.port, 4, seed);
389
0
        return seed;
390
0
    }
391
};
392
393
template <>
394
struct std::hash<std::pair<doris::TUniqueId, int64_t>> {
395
0
    size_t operator()(const std::pair<doris::TUniqueId, int64_t>& pair) const {
396
0
        uint32_t seed = 0;
397
0
        seed = doris::HashUtil::hash(&pair.first.lo, sizeof(pair.first.lo), seed);
398
0
        seed = doris::HashUtil::hash(&pair.first.hi, sizeof(pair.first.hi), seed);
399
0
        seed = doris::HashUtil::hash(&pair.second, sizeof(pair.second), seed);
400
0
        return seed;
401
0
    }
402
};
403
404
template <class First, class Second>
405
struct std::hash<std::pair<First, Second>> {
406
84.8k
    size_t operator()(const pair<First, Second>& p) const {
407
84.8k
        size_t h1 = std::hash<First>()(p.first);
408
84.8k
        size_t h2 = std::hash<Second>()(p.second);
409
84.8k
        return doris::util_hash::HashLen16(h1, h2);
410
84.8k
    }
Unexecuted instantiation: _ZNKSt4hashISt4pairIlN5doris8RowsetIdEEEclERKS3_
_ZNKSt4hashISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEElEEclERKS7_
Line
Count
Source
406
26
    size_t operator()(const pair<First, Second>& p) const {
407
26
        size_t h1 = std::hash<First>()(p.first);
408
26
        size_t h2 = std::hash<Second>()(p.second);
409
26
        return doris::util_hash::HashLen16(h1, h2);
410
26
    }
_ZNKSt4hashISt4pairIiN5doris10vectorized10PathInDataEEEclERKS4_
Line
Count
Source
406
84.8k
    size_t operator()(const pair<First, Second>& p) const {
407
84.8k
        size_t h1 = std::hash<First>()(p.first);
408
84.8k
        size_t h2 = std::hash<Second>()(p.second);
409
84.8k
        return doris::util_hash::HashLen16(h1, h2);
410
84.8k
    }
_ZNKSt4hashISt4pairIllEEclERKS1_
Line
Count
Source
406
60
    size_t operator()(const pair<First, Second>& p) const {
407
60
        size_t h1 = std::hash<First>()(p.first);
408
60
        size_t h2 = std::hash<Second>()(p.second);
409
60
        return doris::util_hash::HashLen16(h1, h2);
410
60
    }
Unexecuted instantiation: _ZNKSt4hashISt4pairIN5doris9TUniqueIdEiEEclERKS3_
411
};
412
413
#include "common/compile_check_end.h"