Coverage Report

Created: 2026-10-09 12:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/hash_table/hash.h
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/ClickHouse/ClickHouse/blob/master/src/Common/HashTable/Hash.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <type_traits>
24
25
#include "core/extended_types.h"
26
#include "core/string_ref.h"
27
#include "core/types.h"
28
#include "core/uint128.h"
29
#include "core/value/timestamp_ns_value.h"
30
#include "parallel_hashmap/phmap_utils.h"
31
32
// Here is an empirical value.
33
static constexpr size_t HASH_MAP_PREFETCH_DIST = 16;
34
35
/** Hash functions that are better than the trivial function std::hash.
36
  *
37
  * Example: when we do aggregation by the visitor ID, the performance increase is more than 5 times.
38
  * This is because of following reasons:
39
  * - in Yandex, visitor identifier is an integer that has timestamp with seconds resolution in lower bits;
40
  * - in typical implementation of standard library, hash function for integers is trivial and just use lower bits;
41
  * - traffic is non-uniformly distributed across a day;
42
  * - we are using open-addressing linear probing hash tables that are most critical to hash function quality,
43
  *   and trivial hash function gives disastrous results.
44
  */
45
46
/** Taken from MurmurHash. This is Murmur finalizer.
47
  * Faster than int_hash32 when inserting into the hash table UInt64 -> UInt64, where the key is the visitor ID.
48
  */
49
60
inline doris::UInt64 int_hash64(doris::UInt64 x) {
50
60
    x ^= x >> 33;
51
60
    x *= 0xff51afd7ed558ccdULL;
52
60
    x ^= x >> 33;
53
60
    x *= 0xc4ceb9fe1a85ec53ULL;
54
60
    x ^= x >> 33;
55
56
60
    return x;
57
60
}
58
59
/** CRC32C is not very high-quality as a hash function,
60
  *  according to avalanche and bit independence tests (see SMHasher software), as well as a small number of bits,
61
  *  but can behave well when used in hash tables,
62
  *  due to high speed (latency 3 + 1 clock cycle, throughput 1 clock cycle).
63
  * Works only with SSE 4.2 support.
64
  */
65
#include "util/sse_util.hpp"
66
67
4.14M
inline doris::UInt64 int_hash_crc32(doris::UInt64 x) {
68
4.14M
#if defined(__SSE4_2__) || (defined(__aarch64__) && defined(__ARM_FEATURE_CRC32))
69
4.14M
    return _mm_crc32_u64(-1ULL, x);
70
#else
71
    /// On other platforms we do not have CRC32. NOTE This can be confusing.
72
    return int_hash64(x);
73
#endif
74
4.14M
}
75
76
template <typename T>
77
60
inline size_t default_hash64(T key) {
78
60
    union {
79
60
        T in;
80
60
        doris::UInt64 out;
81
60
    } u;
82
60
    u.out = 0;
83
60
    u.in = key;
84
60
    return int_hash64(u.out);
85
60
}
Unexecuted instantiation: _Z14default_hash64IhEmT_
Unexecuted instantiation: _Z14default_hash64IaEmT_
Unexecuted instantiation: _Z14default_hash64IsEmT_
_Z14default_hash64IiEmT_
Line
Count
Source
77
16
inline size_t default_hash64(T key) {
78
16
    union {
79
16
        T in;
80
16
        doris::UInt64 out;
81
16
    } u;
82
16
    u.out = 0;
83
16
    u.in = key;
84
16
    return int_hash64(u.out);
85
16
}
Unexecuted instantiation: _Z14default_hash64IlEmT_
_Z14default_hash64InEmT_
Line
Count
Source
77
41
inline size_t default_hash64(T key) {
78
41
    union {
79
41
        T in;
80
41
        doris::UInt64 out;
81
41
    } u;
82
41
    u.out = 0;
83
41
    u.in = key;
84
41
    return int_hash64(u.out);
85
41
}
Unexecuted instantiation: _Z14default_hash64IfEmT_
_Z14default_hash64IdEmT_
Line
Count
Source
77
3
inline size_t default_hash64(T key) {
78
3
    union {
79
3
        T in;
80
3
        doris::UInt64 out;
81
3
    } u;
82
3
    u.out = 0;
83
3
    u.in = key;
84
3
    return int_hash64(u.out);
85
3
}
Unexecuted instantiation: _Z14default_hash64IjEmT_
86
87
template <typename T, typename Enable = void>
88
struct DefaultHash;
89
90
template <typename T>
91
    requires std::is_arithmetic_v<T>
92
struct DefaultHash<T> {
93
60
    size_t operator()(T key) const { return default_hash64<T>(key); }
Unexecuted instantiation: _ZNK11DefaultHashIhvEclEh
Unexecuted instantiation: _ZNK11DefaultHashIavEclEa
Unexecuted instantiation: _ZNK11DefaultHashIsvEclEs
_ZNK11DefaultHashIivEclEi
Line
Count
Source
93
16
    size_t operator()(T key) const { return default_hash64<T>(key); }
Unexecuted instantiation: _ZNK11DefaultHashIlvEclEl
_ZNK11DefaultHashInvEclEn
Line
Count
Source
93
41
    size_t operator()(T key) const { return default_hash64<T>(key); }
Unexecuted instantiation: _ZNK11DefaultHashIfvEclEf
_ZNK11DefaultHashIdvEclEd
Line
Count
Source
93
3
    size_t operator()(T key) const { return default_hash64<T>(key); }
Unexecuted instantiation: _ZNK11DefaultHashIjvEclEj
94
};
95
96
template <>
97
struct DefaultHash<unsigned __int128> {
98
435k
    size_t operator()(unsigned __int128 key) const { return doris::UInt128HashCRC32()(key); }
99
};
100
101
template <>
102
struct DefaultHash<doris::VecDateTimeValue> {
103
0
    size_t operator()(doris::VecDateTimeValue key) const { return int_hash64(*(int64_t*)&key); }
104
};
105
106
template <>
107
struct DefaultHash<doris::DateV2Value<doris::DateTimeV2ValueType>> {
108
0
    size_t operator()(doris::DateV2Value<doris::DateTimeV2ValueType> key) const {
109
0
        return int_hash64(key.to_date_int_val());
110
0
    }
111
};
112
113
template <>
114
struct DefaultHash<doris::DateV2Value<doris::DateV2ValueType>> {
115
0
    size_t operator()(doris::DateV2Value<doris::DateV2ValueType> key) const {
116
0
        return int_hash64(key.to_date_int_val());
117
0
    }
118
};
119
120
template <>
121
struct DefaultHash<doris::TimeStampNsValue> {
122
0
    size_t operator()(doris::TimeStampNsValue key) const { return int_hash64(key.epoch_nanos()); }
123
};
124
125
template <>
126
struct DefaultHash<doris::TimestampTzValue> {
127
0
    size_t operator()(doris::TimestampTzValue key) const {
128
0
        return int_hash64(key.to_date_int_val());
129
0
    }
130
};
131
132
template <>
133
struct DefaultHash<doris::StringRef> : public doris::StringRefHash {};
134
135
template <>
136
struct DefaultHash<wide::Int256> : public std::hash<wide::Int256> {};
137
138
template <typename T>
139
struct HashCRC32;
140
141
template <typename T>
142
4.14M
inline size_t hash_crc32(T key) {
143
4.14M
    union {
144
4.14M
        T in;
145
4.14M
        doris::UInt64 out;
146
4.14M
    } u;
147
4.14M
    u.out = 0;
148
4.14M
    u.in = key;
149
4.14M
    return int_hash_crc32(u.out);
150
4.14M
}
_Z10hash_crc32IlEmT_
Line
Count
Source
142
12
inline size_t hash_crc32(T key) {
143
12
    union {
144
12
        T in;
145
12
        doris::UInt64 out;
146
12
    } u;
147
12
    u.out = 0;
148
12
    u.in = key;
149
12
    return int_hash_crc32(u.out);
150
12
}
_Z10hash_crc32ImEmT_
Line
Count
Source
142
32.1k
inline size_t hash_crc32(T key) {
143
32.1k
    union {
144
32.1k
        T in;
145
32.1k
        doris::UInt64 out;
146
32.1k
    } u;
147
32.1k
    u.out = 0;
148
32.1k
    u.in = key;
149
32.1k
    return int_hash_crc32(u.out);
150
32.1k
}
_Z10hash_crc32IjEmT_
Line
Count
Source
142
4.03M
inline size_t hash_crc32(T key) {
143
4.03M
    union {
144
4.03M
        T in;
145
4.03M
        doris::UInt64 out;
146
4.03M
    } u;
147
4.03M
    u.out = 0;
148
4.03M
    u.in = key;
149
4.03M
    return int_hash_crc32(u.out);
150
4.03M
}
_Z10hash_crc32IhEmT_
Line
Count
Source
142
29.0k
inline size_t hash_crc32(T key) {
143
29.0k
    union {
144
29.0k
        T in;
145
29.0k
        doris::UInt64 out;
146
29.0k
    } u;
147
29.0k
    u.out = 0;
148
29.0k
    u.in = key;
149
29.0k
    return int_hash_crc32(u.out);
150
29.0k
}
_Z10hash_crc32ItEmT_
Line
Count
Source
142
18.7k
inline size_t hash_crc32(T key) {
143
18.7k
    union {
144
18.7k
        T in;
145
18.7k
        doris::UInt64 out;
146
18.7k
    } u;
147
18.7k
    u.out = 0;
148
18.7k
    u.in = key;
149
18.7k
    return int_hash_crc32(u.out);
150
18.7k
}
_Z10hash_crc32IaEmT_
Line
Count
Source
142
8.50k
inline size_t hash_crc32(T key) {
143
8.50k
    union {
144
8.50k
        T in;
145
8.50k
        doris::UInt64 out;
146
8.50k
    } u;
147
8.50k
    u.out = 0;
148
8.50k
    u.in = key;
149
8.50k
    return int_hash_crc32(u.out);
150
8.50k
}
_Z10hash_crc32IsEmT_
Line
Count
Source
142
8.50k
inline size_t hash_crc32(T key) {
143
8.50k
    union {
144
8.50k
        T in;
145
8.50k
        doris::UInt64 out;
146
8.50k
    } u;
147
8.50k
    u.out = 0;
148
8.50k
    u.in = key;
149
8.50k
    return int_hash_crc32(u.out);
150
8.50k
}
_Z10hash_crc32IiEmT_
Line
Count
Source
142
20.5k
inline size_t hash_crc32(T key) {
143
20.5k
    union {
144
20.5k
        T in;
145
20.5k
        doris::UInt64 out;
146
20.5k
    } u;
147
20.5k
    u.out = 0;
148
20.5k
    u.in = key;
149
20.5k
    return int_hash_crc32(u.out);
150
20.5k
}
Unexecuted instantiation: _Z10hash_crc32IfEmT_
_Z10hash_crc32IdEmT_
Line
Count
Source
142
3
inline size_t hash_crc32(T key) {
143
3
    union {
144
3
        T in;
145
3
        doris::UInt64 out;
146
3
    } u;
147
3
    u.out = 0;
148
3
    u.in = key;
149
3
    return int_hash_crc32(u.out);
150
3
}
151
152
template <>
153
20.5k
inline size_t hash_crc32(doris::UInt128 u) {
154
20.5k
    return doris::UInt128HashCRC32()(u);
155
20.5k
}
156
157
template <>
158
26
inline size_t hash_crc32(unsigned __int128 u) {
159
26
    return doris::UInt128HashCRC32()(u);
160
26
}
161
162
template <>
163
12
inline size_t hash_crc32(doris::Int128 u) {
164
12
    return doris::UInt128HashCRC32()({(u >> 64) & int64_t(-1), u & int64_t(-1)});
165
12
}
166
167
template <>
168
0
inline size_t hash_crc32(doris::VecDateTimeValue u) {
169
0
    return hash_crc32(*(int64_t*)&u);
170
0
}
171
172
template <>
173
0
inline size_t hash_crc32(doris::DateV2Value<doris::DateTimeV2ValueType> u) {
174
0
    return hash_crc32(u.to_date_int_val());
175
0
}
176
177
template <>
178
0
inline size_t hash_crc32(doris::DateV2Value<doris::DateV2ValueType> u) {
179
0
    return hash_crc32(u.to_date_int_val());
180
0
}
181
182
template <>
183
6
inline size_t hash_crc32(doris::TimeStampNsValue u) {
184
6
    return hash_crc32(u.epoch_nanos());
185
6
}
186
187
template <>
188
0
inline size_t hash_crc32(doris::TimestampTzValue u) {
189
0
    return hash_crc32(u.to_date_int_val());
190
0
}
191
192
#define DEFINE_HASH(T)                   \
193
    template <>                          \
194
    struct HashCRC32<T> {                \
195
4.17M
        size_t operator()(T key) const { \
196
4.17M
            return hash_crc32<T>(key);   \
197
4.17M
        }                                \
_ZNK9HashCRC32IhEclEh
Line
Count
Source
195
29.0k
        size_t operator()(T key) const { \
196
29.0k
            return hash_crc32<T>(key);   \
197
29.0k
        }                                \
_ZNK9HashCRC32ItEclEt
Line
Count
Source
195
18.7k
        size_t operator()(T key) const { \
196
18.7k
            return hash_crc32<T>(key);   \
197
18.7k
        }                                \
_ZNK9HashCRC32IjEclEj
Line
Count
Source
195
4.03M
        size_t operator()(T key) const { \
196
4.03M
            return hash_crc32<T>(key);   \
197
4.03M
        }                                \
_ZNK9HashCRC32ImEclEm
Line
Count
Source
195
32.1k
        size_t operator()(T key) const { \
196
32.1k
            return hash_crc32<T>(key);   \
197
32.1k
        }                                \
_ZNK9HashCRC32IN4wide7integerILm128EjEEEclES2_
Line
Count
Source
195
20.5k
        size_t operator()(T key) const { \
196
20.5k
            return hash_crc32<T>(key);   \
197
20.5k
        }                                \
_ZNK9HashCRC32IaEclEa
Line
Count
Source
195
8.50k
        size_t operator()(T key) const { \
196
8.50k
            return hash_crc32<T>(key);   \
197
8.50k
        }                                \
_ZNK9HashCRC32IsEclEs
Line
Count
Source
195
8.50k
        size_t operator()(T key) const { \
196
8.50k
            return hash_crc32<T>(key);   \
197
8.50k
        }                                \
_ZNK9HashCRC32IiEclEi
Line
Count
Source
195
20.5k
        size_t operator()(T key) const { \
196
20.5k
            return hash_crc32<T>(key);   \
197
20.5k
        }                                \
_ZNK9HashCRC32IlEclEl
Line
Count
Source
195
6
        size_t operator()(T key) const { \
196
6
            return hash_crc32<T>(key);   \
197
6
        }                                \
_ZNK9HashCRC32InEclEn
Line
Count
Source
195
12
        size_t operator()(T key) const { \
196
12
            return hash_crc32<T>(key);   \
197
12
        }                                \
Unexecuted instantiation: _ZNK9HashCRC32IfEclEf
_ZNK9HashCRC32IdEclEd
Line
Count
Source
195
3
        size_t operator()(T key) const { \
196
3
            return hash_crc32<T>(key);   \
197
3
        }                                \
Unexecuted instantiation: _ZNK9HashCRC32IN5doris16VecDateTimeValueEEclES1_
Unexecuted instantiation: _ZNK9HashCRC32IN5doris11DateV2ValueINS0_19DateTimeV2ValueTypeEEEEclES3_
Unexecuted instantiation: _ZNK9HashCRC32IN5doris11DateV2ValueINS0_15DateV2ValueTypeEEEEclES3_
_ZNK9HashCRC32IN5doris16TimeStampNsValueEEclES1_
Line
Count
Source
195
6
        size_t operator()(T key) const { \
196
6
            return hash_crc32<T>(key);   \
197
6
        }                                \
Unexecuted instantiation: _ZNK9HashCRC32IN5doris16TimestampTzValueEEclES1_
_ZNK9HashCRC32IoEclEo
Line
Count
Source
195
26
        size_t operator()(T key) const { \
196
26
            return hash_crc32<T>(key);   \
197
26
        }                                \
198
    };
199
200
DEFINE_HASH(doris::UInt8)
201
DEFINE_HASH(doris::UInt16)
202
DEFINE_HASH(doris::UInt32)
203
DEFINE_HASH(doris::UInt64)
204
DEFINE_HASH(doris::UInt128)
205
DEFINE_HASH(doris::Int8)
206
DEFINE_HASH(doris::Int16)
207
DEFINE_HASH(doris::Int32)
208
DEFINE_HASH(doris::Int64)
209
DEFINE_HASH(doris::Int128)
210
DEFINE_HASH(doris::Float32)
211
DEFINE_HASH(doris::Float64)
212
DEFINE_HASH(doris::VecDateTimeValue)
213
DEFINE_HASH(doris::DateV2Value<doris::DateTimeV2ValueType>)
214
DEFINE_HASH(doris::DateV2Value<doris::DateV2ValueType>)
215
DEFINE_HASH(doris::TimeStampNsValue)
216
DEFINE_HASH(doris::TimestampTzValue)
217
DEFINE_HASH(unsigned __int128)
218
219
#undef DEFINE_HASH
220
221
template <typename Key, typename Hash = HashCRC32<Key>>
222
struct HashMixWrapper {
223
3.99M
    size_t operator()(Key key) const { return phmap::phmap_mix<sizeof(size_t)>()(Hash()(key)); }
_ZNK14HashMixWrapperIj9HashCRC32IjEEclEj
Line
Count
Source
223
3.99M
    size_t operator()(Key key) const { return phmap::phmap_mix<sizeof(size_t)>()(Hash()(key)); }
_ZNK14HashMixWrapperIm9HashCRC32ImEEclEm
Line
Count
Source
223
243
    size_t operator()(Key key) const { return phmap::phmap_mix<sizeof(size_t)>()(Hash()(key)); }
224
};
225
226
template <>
227
struct HashCRC32<doris::UInt256> {
228
8
    size_t operator()(const doris::UInt256& x) const {
229
8
#if defined(__SSE4_2__) || defined(__aarch64__)
230
8
        doris::UInt64 crc = -1ULL;
231
8
        crc = _mm_crc32_u64(crc, x.items[0]);
232
8
        crc = _mm_crc32_u64(crc, x.items[1]);
233
8
        crc = _mm_crc32_u64(crc, x.items[2]);
234
8
        crc = _mm_crc32_u64(crc, x.items[3]);
235
8
        return crc;
236
#else
237
        return Hash128to64({Hash128to64({x.a, x.b}), Hash128to64({x.c, x.d})});
238
#endif
239
8
    }
240
};
241
242
template <>
243
struct HashCRC32<wide::Int256> {
244
8.19k
    size_t operator()(const wide::Int256& x) const {
245
8.19k
#if defined(__SSE4_2__) || defined(__aarch64__)
246
8.19k
        doris::UInt64 crc = -1ULL;
247
8.19k
        crc = _mm_crc32_u64(crc, x.items[0]);
248
8.19k
        crc = _mm_crc32_u64(crc, x.items[1]);
249
8.19k
        crc = _mm_crc32_u64(crc, x.items[2]);
250
8.19k
        crc = _mm_crc32_u64(crc, x.items[3]);
251
8.19k
        return crc;
252
#else
253
        return Hash128to64(
254
                {Hash128to64({x.items[0], x.items[1]}), Hash128to64({x.items[2], x.items[3]})});
255
#endif
256
8.19k
    }
257
};
258
259
template <>
260
struct HashCRC32<doris::Decimal256> {
261
0
    size_t operator()(const doris::Decimal256& value) const {
262
0
        return HashCRC32<wide::Int256>()(value.value);
263
0
    }
264
};
265
266
template <>
267
struct HashCRC32<doris::Decimal32> {
268
0
    size_t operator()(const doris::Decimal32& value) const {
269
0
        return HashCRC32<int32_t>()(value.value);
270
0
    }
271
};
272
273
template <>
274
struct HashCRC32<doris::Decimal64> {
275
0
    size_t operator()(const doris::Decimal64& value) const {
276
0
        return HashCRC32<int64_t>()(value.value);
277
0
    }
278
};
279
280
template <>
281
struct HashCRC32<doris::Decimal128V3> {
282
0
    size_t operator()(const doris::Decimal128V3& value) const {
283
0
        return HashCRC32<__int128>()(value.value);
284
0
    }
285
};
286
287
template <>
288
struct HashCRC32<doris::Decimal128V2> {
289
0
    size_t operator()(const doris::Decimal128V2& value) const {
290
0
        return HashCRC32<__int128>()(value.value);
291
0
    }
292
};
293
294
template <>
295
struct HashCRC32<doris::DecimalV2Value> {
296
0
    size_t operator()(const doris::DecimalV2Value& value) const {
297
0
        return HashCRC32<__int128>()(value.value());
298
0
    }
299
};
300
301
#include "common/compile_check_avoid_begin.h"
302
303
template <>
304
struct HashCRC32<doris::UInt72> {
305
0
    size_t operator()(const doris::UInt72& x) const {
306
0
        doris::UInt64 crc = -1ULL;
307
0
        crc = _mm_crc32_u8(crc, x.a);
308
0
        crc = _mm_crc32_u64(crc, x.b);
309
0
        return crc;
310
0
    }
311
};
312
313
template <>
314
struct HashCRC32<doris::UInt96> {
315
21
    size_t operator()(const doris::UInt96& x) const {
316
21
        doris::UInt64 crc = -1ULL;
317
21
        crc = _mm_crc32_u32(crc, x.a);
318
21
        crc = _mm_crc32_u64(crc, x.b);
319
21
        return crc;
320
21
    }
321
};
322
323
template <>
324
struct HashCRC32<doris::UInt104> {
325
0
    size_t operator()(const doris::UInt104& x) const {
326
0
        doris::UInt64 crc = -1ULL;
327
0
        crc = _mm_crc32_u8(crc, x.a);
328
0
        crc = _mm_crc32_u32(crc, x.b);
329
0
        crc = _mm_crc32_u64(crc, x.c);
330
0
        return crc;
331
0
    }
332
};
333
334
template <>
335
struct HashCRC32<doris::UInt136> {
336
2
    size_t operator()(const doris::UInt136& x) const {
337
2
        doris::UInt64 crc = -1ULL;
338
2
        crc = _mm_crc32_u8(crc, x.a);
339
2
        crc = _mm_crc32_u64(crc, x.b);
340
2
        crc = _mm_crc32_u64(crc, x.c);
341
2
        return crc;
342
2
    }
343
};
344
345
#include "common/compile_check_avoid_end.h"