Coverage Report

Created: 2026-04-15 08:28

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 <string>
37
#include <type_traits>
38
39
#include "common/compiler_util.h" // IWYU pragma: keep
40
#include "core/types.h"
41
#include "util/unaligned.h"
42
43
namespace doris {
44
3.55G
#define ROTL(x, b) static_cast<UInt64>(((x) << (b)) | ((x) >> (64 - (b))))
45
46
#define SIPROUND           \
47
592M
    do {                   \
48
592M
        v0 += v1;          \
49
592M
        v1 = ROTL(v1, 13); \
50
592M
        v1 ^= v0;          \
51
592M
        v0 = ROTL(v0, 32); \
52
592M
        v2 += v3;          \
53
592M
        v3 = ROTL(v3, 16); \
54
592M
        v3 ^= v2;          \
55
592M
        v0 += v3;          \
56
592M
        v3 = ROTL(v3, 21); \
57
592M
        v3 ^= v0;          \
58
592M
        v2 += v1;          \
59
592M
        v1 = ROTL(v1, 17); \
60
592M
        v1 ^= v2;          \
61
592M
        v2 = ROTL(v2, 32); \
62
592M
    } while (0)
63
64
class SipHash {
65
private:
66
    /// State.
67
    UInt64 v0;
68
    UInt64 v1;
69
    UInt64 v2;
70
    UInt64 v3;
71
72
    /// How many bytes have been processed.
73
    UInt64 cnt;
74
75
    /// The current 8 bytes of input data.
76
    union {
77
        UInt64 current_word;
78
        UInt8 current_bytes[8];
79
    };
80
81
68.6M
    ALWAYS_INLINE void finalize() {
82
        /// In the last free byte, we write the remainder of the division by 256.
83
68.6M
        current_bytes[7] = uint8_t(cnt);
84
85
68.6M
        v3 ^= current_word;
86
68.6M
        SIPROUND;
87
68.6M
        SIPROUND;
88
68.6M
        v0 ^= current_word;
89
90
68.6M
        v2 ^= 0xff;
91
68.6M
        SIPROUND;
92
68.6M
        SIPROUND;
93
68.6M
        SIPROUND;
94
68.6M
        SIPROUND;
95
68.6M
    }
96
97
public:
98
    /// Arguments - seed.
99
68.6M
    SipHash(UInt64 k0 = 0, UInt64 k1 = 0) {
100
        /// Initialize the state with some random bytes and seed.
101
68.6M
        v0 = 0x736f6d6570736575ULL ^ k0;
102
68.6M
        v1 = 0x646f72616e646f6dULL ^ k1;
103
68.6M
        v2 = 0x6c7967656e657261ULL ^ k0;
104
68.6M
        v3 = 0x7465646279746573ULL ^ k1;
105
106
68.6M
        cnt = 0;
107
68.6M
        current_word = 0;
108
68.6M
    }
109
110
137M
    void update(const char* data, UInt64 size) {
111
137M
        const char* end = data + size;
112
113
        /// We'll finish to process the remainder of the previous update, if any.
114
137M
        if (cnt & 7) {
115
5.23M
            while (cnt & 7 && data < end) {
116
3.29M
                current_bytes[cnt & 7] = *data;
117
3.29M
                ++data;
118
3.29M
                ++cnt;
119
3.29M
            }
120
121
            /// If we still do not have enough bytes to an 8-byte word.
122
1.93M
            if (cnt & 7) return;
123
124
574k
            v3 ^= current_word;
125
574k
            SIPROUND;
126
574k
            SIPROUND;
127
574k
            v0 ^= current_word;
128
574k
        }
129
130
135M
        cnt += end - data;
131
132
225M
        while (data + 8 <= end) {
133
89.7M
            current_word = unaligned_load<UInt64>(data);
134
135
89.7M
            v3 ^= current_word;
136
89.7M
            SIPROUND;
137
89.7M
            SIPROUND;
138
89.7M
            v0 ^= current_word;
139
140
89.7M
            data += 8;
141
89.7M
        }
142
143
        /// Pad the remainder, which is missing up to an 8-byte word.
144
135M
        current_word = 0;
145
135M
        switch (end - data) {
146
106k
        case 7:
147
106k
            current_bytes[6] = data[6];
148
106k
            [[fallthrough]];
149
2.00M
        case 6:
150
2.00M
            current_bytes[5] = data[5];
151
2.00M
            [[fallthrough]];
152
2.08M
        case 5:
153
2.08M
            current_bytes[4] = data[4];
154
2.08M
            [[fallthrough]];
155
2.20M
        case 4:
156
2.20M
            current_bytes[3] = data[3];
157
2.20M
            [[fallthrough]];
158
2.28M
        case 3:
159
2.28M
            current_bytes[2] = data[2];
160
2.28M
            [[fallthrough]];
161
2.39M
        case 2:
162
2.39M
            current_bytes[1] = data[1];
163
2.39M
            [[fallthrough]];
164
69.1M
        case 1:
165
69.1M
            current_bytes[0] = data[0];
166
69.1M
            [[fallthrough]];
167
136M
        case 0:
168
136M
            break;
169
135M
        }
170
135M
    }
171
172
    template <typename T>
173
134M
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
134M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
134M
    }
_ZN5doris7SipHash6updateIiEEvRKT_
Line
Count
Source
173
1.97k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
1.97k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
1.97k
    }
_ZN5doris7SipHash6updateIhEEvRKT_
Line
Count
Source
173
713k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
713k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
713k
    }
_ZN5doris7SipHash6updateIaEEvRKT_
Line
Count
Source
173
974
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
974
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
974
    }
_ZN5doris7SipHash6updateIsEEvRKT_
Line
Count
Source
173
888
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
888
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
888
    }
_ZN5doris7SipHash6updateIlEEvRKT_
Line
Count
Source
173
2.14k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
2.14k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
2.14k
    }
_ZN5doris7SipHash6updateInEEvRKT_
Line
Count
Source
173
674
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
674
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
674
    }
_ZN5doris7SipHash6updateIfEEvRKT_
Line
Count
Source
173
737
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
737
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
737
    }
_ZN5doris7SipHash6updateIdEEvRKT_
Line
Count
Source
173
821
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
821
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
821
    }
_ZN5doris7SipHash6updateIjEEvRKT_
Line
Count
Source
173
1.13k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
1.13k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
1.13k
    }
_ZN5doris7SipHash6updateIoEEvRKT_
Line
Count
Source
173
752
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
752
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
752
    }
_ZN5doris7SipHash6updateINS_16VecDateTimeValueEEEvRKT_
Line
Count
Source
173
816
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
816
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
816
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvRKT_
Line
Count
Source
173
256
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
256
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
256
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvRKT_
Line
Count
Source
173
1.95k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
1.95k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
1.95k
    }
Unexecuted instantiation: _ZN5doris7SipHash6updateINS_16TimestampTzValueEEEvRKT_
_ZN5doris7SipHash6updateImEEvRKT_
Line
Count
Source
173
66.6M
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
66.6M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
66.6M
    }
_ZN5doris7SipHash6updateINS_7DecimalIiEEEEvRKT_
Line
Count
Source
173
354
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
354
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
354
    }
_ZN5doris7SipHash6updateINS_7DecimalIlEEEEvRKT_
Line
Count
Source
173
576
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
576
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
576
    }
_ZN5doris7SipHash6updateINS_14DecimalV2ValueEEEvRKT_
Line
Count
Source
173
1.34k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
1.34k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
1.34k
    }
_ZN5doris7SipHash6updateINS_12Decimal128V3EEEvRKT_
Line
Count
Source
173
662
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
662
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
662
    }
_ZN5doris7SipHash6updateINS_7DecimalIN4wide7integerILm256EiEEEEEEvRKT_
Line
Count
Source
173
1.25k
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
1.25k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
1.25k
    }
_ZN5doris7SipHash6updateIbEEvRKT_
Line
Count
Source
173
67.4M
    void update(const T& x) {
174
        if constexpr (std::is_same_v<T, std::string>) {
175
            throw Exception(ErrorCode::INTERNAL_ERROR, "String should not use SipHash!");
176
        }
177
67.4M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
178
67.4M
    }
179
180
    /// Get the result in some form. This can only be done once!
181
182
68.5M
    void get128(char* out) {
183
68.5M
        finalize();
184
68.5M
        reinterpret_cast<UInt64*>(out)[0] = v0 ^ v1;
185
68.5M
        reinterpret_cast<UInt64*>(out)[1] = v2 ^ v3;
186
68.5M
    }
187
188
    /// template for avoiding 'unsigned long long' vs 'unsigned long' problem on old poco in macos
189
    template <typename T>
190
    ALWAYS_INLINE void get128(T& lo, T& hi) {
191
        static_assert(sizeof(T) == 8);
192
        finalize();
193
        lo = v0 ^ v1;
194
        hi = v2 ^ v3;
195
    }
196
197
78.3k
    UInt64 get64() {
198
78.3k
        finalize();
199
78.3k
        return v0 ^ v1 ^ v2 ^ v3;
200
78.3k
    }
201
202
    template <typename T>
203
66.7M
    ALWAYS_INLINE void get128(T& dst) {
204
66.7M
        static_assert(sizeof(T) == 16);
205
66.7M
        get128(reinterpret_cast<char*>(&dst));
206
66.7M
    }
207
};
208
209
#undef ROTL
210
#undef SIPROUND
211
212
#include <cstddef>
213
214
1.86M
inline void sip_hash128(const char* data, const size_t size, char* out) {
215
1.86M
    SipHash hash;
216
1.86M
    hash.update(data, size);
217
1.86M
    hash.get128(out);
218
1.86M
}
219
} // namespace doris