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/apache/impala/blob/branch-2.9.0/be/src/runtime/raw-value.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <string> |
24 | | |
25 | | #include "common/consts.h" |
26 | | #include "common/logging.h" |
27 | | #include "core/data_type/define_primitive_type.h" |
28 | | #include "core/packed_int128.h" |
29 | | #include "core/string_ref.h" |
30 | | #include "util/hash_util.hpp" |
31 | | |
32 | | namespace doris { |
33 | | class SlotDescriptor; |
34 | | |
35 | | // Useful utility functions for runtime values (which are passed around as void*). |
36 | | class RawValue { |
37 | | public: |
38 | | // Same as the up function, only use in vec exec engine. |
39 | | static uint32_t zlib_crc32(const void* value, size_t len, const PrimitiveType& type, |
40 | | uint32_t seed); |
41 | | }; |
42 | | |
43 | | // NOTE: this is just for split data, decimal use old doris hash function |
44 | | // Because crc32 hardware is not equal with zlib crc32 |
45 | | inline uint32_t RawValue::zlib_crc32(const void* v, size_t len, const PrimitiveType& type, |
46 | 38.4M | uint32_t seed) { |
47 | | // Hash_combine with v = 0 |
48 | 38.4M | if (v == nullptr) { |
49 | 0 | uint32_t value = 0x9e3779b9; |
50 | 0 | return seed ^ (value + (seed << 6) + (seed >> 2)); |
51 | 0 | } |
52 | | |
53 | 38.4M | switch (type) { |
54 | 1.07M | case TYPE_VARCHAR: |
55 | 1.07M | case TYPE_HLL: |
56 | 1.07M | case TYPE_STRING: |
57 | 1.07M | case TYPE_CHAR: { |
58 | 1.07M | return HashUtil::zlib_crc_hash(v, (uint32_t)len, seed); |
59 | 1.07M | } |
60 | | |
61 | 147 | case TYPE_BOOLEAN: |
62 | 3.45k | case TYPE_TINYINT: |
63 | 3.45k | return HashUtil::zlib_crc_hash(v, 1, seed); |
64 | 1.36k | case TYPE_SMALLINT: |
65 | 1.36k | return HashUtil::zlib_crc_hash(v, 2, seed); |
66 | 20.6M | case TYPE_INT: |
67 | 20.6M | return HashUtil::zlib_crc_hash(v, 4, seed); |
68 | 16.5M | case TYPE_BIGINT: |
69 | 16.5M | return HashUtil::zlib_crc_hash(v, 8, seed); |
70 | 27.8k | case TYPE_LARGEINT: |
71 | 27.8k | return HashUtil::zlib_crc_hash(v, 16, seed); |
72 | 0 | case TYPE_FLOAT: |
73 | 0 | return HashUtil::zlib_crc_hash(v, 4, seed); |
74 | 0 | case TYPE_DOUBLE: |
75 | 0 | return HashUtil::zlib_crc_hash(v, 8, seed); |
76 | 22 | case TYPE_DATE: |
77 | 47 | case TYPE_DATETIME: { |
78 | 47 | auto* date_val = (const VecDateTimeValue*)v; |
79 | 47 | char buf[64]; |
80 | 47 | int date_len = date_val->to_buffer(buf); |
81 | 47 | return HashUtil::zlib_crc_hash(buf, date_len, seed); |
82 | 22 | } |
83 | | |
84 | 1.65k | case TYPE_DATEV2: { |
85 | 1.65k | return HashUtil::zlib_crc_hash(v, 4, seed); |
86 | 22 | } |
87 | | |
88 | 12.5k | case TYPE_DATETIMEV2: { |
89 | 12.5k | return HashUtil::zlib_crc_hash(v, 8, seed); |
90 | 22 | } |
91 | | |
92 | 2.92k | case TYPE_TIMESTAMPTZ: { |
93 | 2.92k | return HashUtil::zlib_crc_hash(v, 8, seed); |
94 | 22 | } |
95 | | |
96 | 27 | case TYPE_DECIMALV2: { |
97 | 27 | const DecimalV2Value* dec_val = (const DecimalV2Value*)v; |
98 | 27 | int64_t int_val = dec_val->int_value(); |
99 | 27 | int32_t frac_val = dec_val->frac_value(); |
100 | 27 | seed = HashUtil::zlib_crc_hash(&int_val, sizeof(int_val), seed); |
101 | 27 | return HashUtil::zlib_crc_hash(&frac_val, sizeof(frac_val), seed); |
102 | 22 | } |
103 | 156 | case TYPE_DECIMAL32: |
104 | 156 | return HashUtil::zlib_crc_hash(v, 4, seed); |
105 | 224 | case TYPE_DECIMAL64: |
106 | 224 | return HashUtil::zlib_crc_hash(v, 8, seed); |
107 | 447 | case TYPE_DECIMAL128I: |
108 | 447 | return HashUtil::zlib_crc_hash(v, 16, seed); |
109 | 4.37k | case TYPE_DECIMAL256: |
110 | 4.37k | return HashUtil::zlib_crc_hash(v, 32, seed); |
111 | 3 | case TYPE_IPV4: |
112 | 3 | return HashUtil::zlib_crc_hash(v, 4, seed); |
113 | 3 | case TYPE_IPV6: |
114 | 3 | return HashUtil::zlib_crc_hash(v, 16, seed); |
115 | 0 | default: |
116 | 0 | DCHECK(false) << "invalid type: " << type; |
117 | 0 | return 0; |
118 | 38.4M | } |
119 | 38.4M | } |
120 | | } // namespace doris |