#include "FunctionsConsistentHashing.h" #include namespace DB { namespace { /// Code from https://arxiv.org/pdf/1406.2294.pdf inline int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets) { int64_t b = -1, j = 0; while (j < num_buckets) { b = j; key = key * 2862933555777941757ULL + 1; j = static_cast((b + 1) * (static_cast(1LL << 31) / static_cast((key >> 33) + 1))); } return static_cast(b); } struct JumpConsistentHashImpl { static constexpr auto name = "jumpConsistentHash"; using HashType = UInt64; using ResultType = Int32; using BucketsType = ResultType; static constexpr auto max_buckets = static_cast(std::numeric_limits::max()); static inline ResultType apply(UInt64 hash, BucketsType n) { return JumpConsistentHash(hash, n); } }; using FunctionJumpConsistentHash = FunctionConsistentHashImpl; } REGISTER_FUNCTION(JumpConsistentHash) { factory.registerFunction(); } }