Coverage Report

Created: 2026-03-13 21:11

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
#include "common/compile_check_begin.h"
45
1.44G
#define ROTL(x, b) static_cast<UInt64>(((x) << (b)) | ((x) >> (64 - (b))))
46
47
#define SIPROUND           \
48
240M
    do {                   \
49
240M
        v0 += v1;          \
50
240M
        v1 = ROTL(v1, 13); \
51
240M
        v1 ^= v0;          \
52
240M
        v0 = ROTL(v0, 32); \
53
240M
        v2 += v3;          \
54
240M
        v3 = ROTL(v3, 16); \
55
240M
        v3 ^= v2;          \
56
240M
        v0 += v3;          \
57
240M
        v3 = ROTL(v3, 21); \
58
240M
        v3 ^= v0;          \
59
240M
        v2 += v1;          \
60
240M
        v1 = ROTL(v1, 17); \
61
240M
        v1 ^= v2;          \
62
240M
        v2 = ROTL(v2, 32); \
63
240M
    } 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
26.6M
    ALWAYS_INLINE void finalize() {
83
        /// In the last free byte, we write the remainder of the division by 256.
84
26.6M
        current_bytes[7] = uint8_t(cnt);
85
86
26.6M
        v3 ^= current_word;
87
26.6M
        SIPROUND;
88
26.6M
        SIPROUND;
89
26.6M
        v0 ^= current_word;
90
91
26.6M
        v2 ^= 0xff;
92
26.6M
        SIPROUND;
93
26.6M
        SIPROUND;
94
26.6M
        SIPROUND;
95
26.6M
        SIPROUND;
96
26.6M
    }
97
98
public:
99
    /// Arguments - seed.
100
26.6M
    SipHash(UInt64 k0 = 0, UInt64 k1 = 0) {
101
        /// Initialize the state with some random bytes and seed.
102
26.6M
        v0 = 0x736f6d6570736575ULL ^ k0;
103
26.6M
        v1 = 0x646f72616e646f6dULL ^ k1;
104
26.6M
        v2 = 0x6c7967656e657261ULL ^ k0;
105
26.6M
        v3 = 0x7465646279746573ULL ^ k1;
106
107
26.6M
        cnt = 0;
108
26.6M
        current_word = 0;
109
26.6M
    }
110
111
54.2M
    void update(const char* data, UInt64 size) {
112
54.2M
        const char* end = data + size;
113
114
        /// We'll finish to process the remainder of the previous update, if any.
115
54.2M
        if (cnt & 7) {
116
3.73M
            while (cnt & 7 && data < end) {
117
2.35M
                current_bytes[cnt & 7] = *data;
118
2.35M
                ++data;
119
2.35M
                ++cnt;
120
2.35M
            }
121
122
            /// If we still do not have enough bytes to an 8-byte word.
123
1.37M
            if (cnt & 7) return;
124
125
419k
            v3 ^= current_word;
126
419k
            SIPROUND;
127
419k
            SIPROUND;
128
419k
            v0 ^= current_word;
129
419k
        }
130
131
53.2M
        cnt += end - data;
132
133
93.3M
        while (data + 8 <= end) {
134
40.0M
            current_word = unaligned_load<UInt64>(data);
135
136
40.0M
            v3 ^= current_word;
137
40.0M
            SIPROUND;
138
40.0M
            SIPROUND;
139
40.0M
            v0 ^= current_word;
140
141
40.0M
            data += 8;
142
40.0M
        }
143
144
        /// Pad the remainder, which is missing up to an 8-byte word.
145
53.2M
        current_word = 0;
146
53.2M
        switch (end - data) {
147
103k
        case 7:
148
103k
            current_bytes[6] = data[6];
149
103k
            [[fallthrough]];
150
483k
        case 6:
151
483k
            current_bytes[5] = data[5];
152
483k
            [[fallthrough]];
153
558k
        case 5:
154
558k
            current_bytes[4] = data[4];
155
558k
            [[fallthrough]];
156
609k
        case 4:
157
609k
            current_bytes[3] = data[3];
158
609k
            [[fallthrough]];
159
678k
        case 3:
160
678k
            current_bytes[2] = data[2];
161
678k
            [[fallthrough]];
162
729k
        case 2:
163
729k
            current_bytes[1] = data[1];
164
729k
            [[fallthrough]];
165
27.0M
        case 1:
166
27.0M
            current_bytes[0] = data[0];
167
27.0M
            [[fallthrough]];
168
53.4M
        case 0:
169
53.4M
            break;
170
53.2M
        }
171
53.2M
    }
172
173
    template <typename T>
174
53.4M
    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
53.4M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
53.4M
    }
_ZN5doris7SipHash6updateINS_7DecimalIiEEEEvRKT_
Line
Count
Source
174
354
    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
354
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
354
    }
_ZN5doris7SipHash6updateINS_7DecimalIlEEEEvRKT_
Line
Count
Source
174
576
    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
576
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
576
    }
_ZN5doris7SipHash6updateINS_14DecimalV2ValueEEEvRKT_
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
    }
_ZN5doris7SipHash6updateINS_12Decimal128V3EEEvRKT_
Line
Count
Source
174
662
    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
662
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
662
    }
_ZN5doris7SipHash6updateINS_7DecimalIN4wide7integerILm256EiEEEEEEvRKT_
Line
Count
Source
174
1.25k
    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.25k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.25k
    }
_ZN5doris7SipHash6updateIhEEvRKT_
Line
Count
Source
174
503k
    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
503k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
503k
    }
_ZN5doris7SipHash6updateIaEEvRKT_
Line
Count
Source
174
974
    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
974
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
974
    }
_ZN5doris7SipHash6updateIsEEvRKT_
Line
Count
Source
174
888
    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
888
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
888
    }
_ZN5doris7SipHash6updateIiEEvRKT_
Line
Count
Source
174
1.97k
    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.97k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.97k
    }
_ZN5doris7SipHash6updateIlEEvRKT_
Line
Count
Source
174
2.14k
    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.14k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
2.14k
    }
_ZN5doris7SipHash6updateInEEvRKT_
Line
Count
Source
174
674
    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
674
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
674
    }
_ZN5doris7SipHash6updateIfEEvRKT_
Line
Count
Source
174
737
    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
737
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
737
    }
_ZN5doris7SipHash6updateIdEEvRKT_
Line
Count
Source
174
821
    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
821
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
821
    }
_ZN5doris7SipHash6updateIjEEvRKT_
Line
Count
Source
174
1.13k
    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.13k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.13k
    }
_ZN5doris7SipHash6updateIoEEvRKT_
Line
Count
Source
174
752
    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
752
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
752
    }
_ZN5doris7SipHash6updateINS_16VecDateTimeValueEEEvRKT_
Line
Count
Source
174
816
    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
816
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
816
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvRKT_
Line
Count
Source
174
256
    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
256
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
256
    }
_ZN5doris7SipHash6updateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvRKT_
Line
Count
Source
174
1.95k
    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.95k
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
1.95k
    }
Unexecuted instantiation: _ZN5doris7SipHash6updateINS_16TimestampTzValueEEEvRKT_
_ZN5doris7SipHash6updateImEEvRKT_
Line
Count
Source
174
26.2M
    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
26.2M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
26.2M
    }
_ZN5doris7SipHash6updateIbEEvRKT_
Line
Count
Source
174
26.7M
    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
26.7M
        update(reinterpret_cast<const char*>(&x), sizeof(x));
179
26.7M
    }
180
181
    /// Get the result in some form. This can only be done once!
182
183
26.5M
    void get128(char* out) {
184
26.5M
        finalize();
185
26.5M
        reinterpret_cast<UInt64*>(out)[0] = v0 ^ v1;
186
26.5M
        reinterpret_cast<UInt64*>(out)[1] = v2 ^ v3;
187
26.5M
    }
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
62.7k
    UInt64 get64() {
199
62.7k
        finalize();
200
62.7k
        return v0 ^ v1 ^ v2 ^ v3;
201
62.7k
    }
202
203
    template <typename T>
204
26.2M
    ALWAYS_INLINE void get128(T& dst) {
205
26.2M
        static_assert(sizeof(T) == 16);
206
26.2M
        get128(reinterpret_cast<char*>(&dst));
207
26.2M
    }
208
};
209
210
#undef ROTL
211
#undef SIPROUND
212
213
#include <cstddef>
214
215
355k
inline void sip_hash128(const char* data, const size_t size, char* out) {
216
355k
    SipHash hash;
217
355k
    hash.update(data, size);
218
355k
    hash.get128(out);
219
355k
}
220
#include "common/compile_check_end.h"
221
} // namespace doris