Coverage Report

Created: 2026-07-24 22:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/sip_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/SipHash.h
19
// and modified by Doris
20
21
#pragma once
22
23
/** SipHash is a fast cryptographic hash function for short strings.
24
  * Taken from here: https://www.131002.net/siphash/
25
  *
26
  * This is SipHash 2-4 variant.
27
  *
28
  * Two changes are made:
29
  * - returns also 128 bits, not only 64;
30
  * - done streaming (can be calculated in parts).
31
  *
32
  * On short strings (URL, search phrases) more than 3 times faster than MD5 from OpenSSL.
33
  * (~ 700 MB/sec, 15 million strings per second)
34
  */
35
36
#include <cstddef>
37
#include <string>
38
#include <type_traits>
39
40
#include "common/compiler_util.h" // IWYU pragma: keep
41
#include "core/types.h"
42
#include "util/unaligned.h"
43
44
namespace doris {
45
337M
#define ROTL(x, b) static_cast<UInt64>(((x) << (b)) | ((x) >> (64 - (b))))
46
47
#define SIPROUND           \
48
56.2M
    do {                   \
49
56.2M
        v0 += v1;          \
50
56.2M
        v1 = ROTL(v1, 13); \
51
56.2M
        v1 ^= v0;          \
52
56.2M
        v0 = ROTL(v0, 32); \
53
56.2M
        v2 += v3;          \
54
56.2M
        v3 = ROTL(v3, 16); \
55
56.2M
        v3 ^= v2;          \
56
56.2M
        v0 += v3;          \
57
56.2M
        v3 = ROTL(v3, 21); \
58
56.2M
        v3 ^= v0;          \
59
56.2M
        v2 += v1;          \
60
56.2M
        v1 = ROTL(v1, 17); \
61
56.2M
        v1 ^= v2;          \
62
56.2M
        v2 = ROTL(v2, 32); \
63
56.2M
    } while (0)
64
65
class SipHash {
66
private:
67
    /// State.
68
    UInt64 v0;
69
    UInt64 v1;
70
    UInt64 v2;
71
    UInt64 v3;
72
73
    /// How many bytes have been processed.
74
    UInt64 cnt;
75
76
    /// The current 8 bytes of input data.
77
    union {
78
        UInt64 current_word;
79
        UInt8 current_bytes[8];
80
    };
81
82
1.22M
    ALWAYS_INLINE void finalize() {
83
        /// In the last free byte, we write the remainder of the division by 256.
84
1.22M
        current_bytes[7] = uint8_t(cnt);
85
86
1.22M
        v3 ^= current_word;
87
1.22M
        SIPROUND;
88
1.22M
        SIPROUND;
89
1.22M
        v0 ^= current_word;
90
91
1.22M
        v2 ^= 0xff;
92
1.22M
        SIPROUND;
93
1.22M
        SIPROUND;
94
1.22M
        SIPROUND;
95
1.22M
        SIPROUND;
96
1.22M
    }
97
98
public:
99
    /// Arguments - seed.
100
1.22M
    SipHash(UInt64 k0 = 0, UInt64 k1 = 0) {
101
        /// Initialize the state with some random bytes and seed.
102
1.22M
        v0 = 0x736f6d6570736575ULL ^ k0;
103
1.22M
        v1 = 0x646f72616e646f6dULL ^ k1;
104
1.22M
        v2 = 0x6c7967656e657261ULL ^ k0;
105
1.22M
        v3 = 0x7465646279746573ULL ^ k1;
106
107
1.22M
        cnt = 0;
108
1.22M
        current_word = 0;
109
1.22M
    }
110
111
2.62M
    void update(const char* data, UInt64 size) {
112
2.62M
        const char* end = data + size;
113
114
        /// We'll finish to process the remainder of the previous update, if any.
115
2.62M
        if (cnt & 7) {
116
655k
            while (cnt & 7 && data < end) {
117
468k
                current_bytes[cnt & 7] = *data;
118
468k
                ++data;
119
468k
                ++cnt;
120
468k
            }
121
122
            /// If we still do not have enough bytes to an 8-byte word.
123
186k
            if (cnt & 7) return;
124
125
95.4k
            v3 ^= current_word;
126
95.4k
            SIPROUND;
127
95.4k
            SIPROUND;
128
95.4k
            v0 ^= current_word;
129
95.4k
        }
130
131
2.53M
        cnt += end - data;
132
133
26.8M
        while (data + 8 <= end) {
134
24.3M
            current_word = unaligned_load<UInt64>(data);
135
136
24.3M
            v3 ^= current_word;
137
24.3M
            SIPROUND;
138
24.3M
            SIPROUND;
139
24.3M
            v0 ^= current_word;
140
141
24.3M
            data += 8;
142
24.3M
        }
143
144
        /// Pad the remainder, which is missing up to an 8-byte word.
145
2.53M
        current_word = 0;
146
2.53M
        switch (end - data) {
147
14.1k
        case 7:
148
14.1k
            current_bytes[6] = data[6];
149
14.1k
            [[fallthrough]];
150
26.6k
        case 6:
151
26.6k
            current_bytes[5] = data[5];
152
26.6k
            [[fallthrough]];
153
38.7k
        case 5:
154
38.7k
            current_bytes[4] = data[4];
155
38.7k
            [[fallthrough]];
156
82.5k
        case 4:
157
82.5k
            current_bytes[3] = data[3];
158
82.5k
            [[fallthrough]];
159
100k
        case 3:
160
100k
            current_bytes[2] = data[2];
161
100k
            [[fallthrough]];
162
114k
        case 2:
163
114k
            current_bytes[1] = data[1];
164
114k
            [[fallthrough]];
165
1.31M
        case 1:
166
1.31M
            current_bytes[0] = data[0];
167
1.31M
            [[fallthrough]];
168
2.53M
        case 0:
169
2.53M
            break;
170
2.53M
        }
171
2.53M
    }
172
173
    template <typename T>
174
2.46M
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
2.46M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
2.46M
    }
_ZN5doris7SipHash6updateINS_7DecimalIiEEEEvRKT_
Line
Count
Source
174
708
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
708
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
708
    }
_ZN5doris7SipHash6updateINS_7DecimalIlEEEEvRKT_
Line
Count
Source
174
1.15k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.15k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.15k
    }
_ZN5doris7SipHash6updateINS_14DecimalV2ValueEEEvRKT_
Line
Count
Source
174
2.68k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
2.68k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
2.68k
    }
_ZN5doris7SipHash6updateINS_12Decimal128V3EEEvRKT_
Line
Count
Source
174
1.32k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.32k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.32k
    }
_ZN5doris7SipHash6updateINS_7DecimalIN4wide7integerILm256EiEEEEEEvRKT_
Line
Count
Source
174
2.51k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
2.51k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
2.51k
    }
_ZN5doris7SipHash6updateIiEEvRKT_
Line
Count
Source
174
3.94k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
3.94k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
3.94k
    }
_ZN5doris7SipHash6updateIhEEvRKT_
Line
Count
Source
174
29.9k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
29.9k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
29.9k
    }
_ZN5doris7SipHash6updateIaEEvRKT_
Line
Count
Source
174
1.94k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.94k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.94k
    }
_ZN5doris7SipHash6updateIsEEvRKT_
Line
Count
Source
174
1.77k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.77k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.77k
    }
_ZN5doris7SipHash6updateIlEEvRKT_
Line
Count
Source
174
4.28k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
4.28k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
4.28k
    }
_ZN5doris7SipHash6updateInEEvRKT_
Line
Count
Source
174
1.34k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.34k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.34k
    }
_ZN5doris7SipHash6updateIfEEvRKT_
Line
Count
Source
174
1.47k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.47k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.47k
    }
_ZN5doris7SipHash6updateIdEEvRKT_
Line
Count
Source
174
1.64k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.64k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.64k
    }
_ZN5doris7SipHash6updateIjEEvRKT_
Line
Count
Source
174
2.27k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
2.27k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
2.27k
    }
_ZN5doris7SipHash6updateIoEEvRKT_
Line
Count
Source
174
1.50k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.50k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.50k
    }
_ZN5doris7SipHash6updateINS_16VecDateTimeValueEEEvRKT_
Line
Count
Source
174
1.63k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.63k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.63k
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvRKT_
Line
Count
Source
174
512
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
512
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
512
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvRKT_
Line
Count
Source
174
3.90k
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
3.90k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
3.90k
    }
Unexecuted instantiation: _ZN5doris7SipHash6updateINS_16TimestampTzValueEEEvRKT_
_ZN5doris7SipHash6updateImEEvRKT_
Line
Count
Source
174
1.18M
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.18M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.18M
    }
_ZN5doris7SipHash6updateIbEEvRKT_
Line
Count
Source
174
1.21M
    void update(const T& x) {
175
        if constexpr (std::is_same_v<T, std::string>) {
176
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
177
        }
178
1.21M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.21M
    }
180
181
    /// Get the result in some form. This can only be done once!
182
183
1.21M
    void get128(char* out) {
184
1.21M
        finalize();
185
1.21M
        reinterpret_cast<UInt64*>(out)[0] = v0 ^ v1;
186
1.21M
        reinterpret_cast<UInt64*>(out)[1] = v2 ^ v3;
187
1.21M
    }
188
189
    /// template for avoiding 'unsigned long long' vs 'unsigned long' problem on old poco in macos
190
    template <typename T>
191
    ALWAYS_INLINE void get128(T& lo, T& hi) {
192
        static_assert(sizeof(T) == 8);
193
        finalize();
194
        lo = v0 ^ v1;
195
        hi = v2 ^ v3;
196
    }
197
198
10.9k
    UInt64 get64() {
199
10.9k
        finalize();
200
10.9k
        return v0 ^ v1 ^ v2 ^ v3;
201
10.9k
    }
202
203
    template <typename T>
204
1.18M
    ALWAYS_INLINE void get128(T& dst) {
205
1.18M
        static_assert(sizeof(T) == 16);
206
1.18M
        get128(reinterpret_cast<char*>(&dst));
207
1.18M
    }
208
};
209
210
#undef ROTL
211
#undef SIPROUND
212
213
29.4k
inline void sip_hash128(const char* data, const size_t size, char* out) {
214
29.4k
    SipHash hash;
215
29.4k
    hash.update(data, size);
216
29.4k
    hash.get128(out);
217
29.4k
}
218
} // namespace doris