/root/doris/be/src/runtime/raw_value.h
Line | Count | Source (jump to first uncovered line) |
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 "runtime/types.h" |
28 | | #include "util/hash_util.hpp" |
29 | | #include "util/types.h" |
30 | | #include "vec/common/string_ref.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | class SlotDescriptor; |
35 | | |
36 | | // Useful utility functions for runtime values (which are passed around as void*). |
37 | | class RawValue { |
38 | | public: |
39 | | // Same as the up function, only use in vec exec engine. |
40 | | static uint32_t zlib_crc32(const void* value, size_t len, const PrimitiveType& type, |
41 | | uint32_t seed); |
42 | | }; |
43 | | |
44 | | // NOTE: this is just for split data, decimal use old doris hash function |
45 | | // Because crc32 hardware is not equal with zlib crc32 |
46 | | inline uint32_t RawValue::zlib_crc32(const void* v, size_t len, const PrimitiveType& type, |
47 | 31 | uint32_t seed) { |
48 | | // Hash_combine with v = 0 |
49 | 31 | if (v == nullptr) { |
50 | 0 | uint32_t value = 0x9e3779b9; |
51 | 0 | return seed ^ (value + (seed << 6) + (seed >> 2)); |
52 | 0 | } |
53 | | |
54 | 31 | switch (type) { |
55 | 12 | case TYPE_VARCHAR: |
56 | 12 | case TYPE_HLL: |
57 | 12 | case TYPE_STRING: |
58 | 12 | case TYPE_CHAR: { |
59 | 12 | return HashUtil::zlib_crc_hash(v, len, seed); |
60 | 12 | } |
61 | | |
62 | 0 | case TYPE_BOOLEAN: |
63 | 0 | case TYPE_TINYINT: |
64 | 0 | return HashUtil::zlib_crc_hash(v, 1, seed); |
65 | 0 | case TYPE_SMALLINT: |
66 | 0 | return HashUtil::zlib_crc_hash(v, 2, seed); |
67 | 12 | case TYPE_INT: |
68 | 12 | return HashUtil::zlib_crc_hash(v, 4, seed); |
69 | 0 | case TYPE_BIGINT: |
70 | 0 | return HashUtil::zlib_crc_hash(v, 8, seed); |
71 | 0 | case TYPE_LARGEINT: |
72 | 0 | return HashUtil::zlib_crc_hash(v, 16, seed); |
73 | 0 | case TYPE_FLOAT: |
74 | 0 | return HashUtil::zlib_crc_hash(v, 4, seed); |
75 | 0 | case TYPE_DOUBLE: |
76 | 0 | return HashUtil::zlib_crc_hash(v, 8, seed); |
77 | 0 | case TYPE_DATE: |
78 | 0 | case TYPE_DATETIME: { |
79 | 0 | auto* date_val = (const VecDateTimeValue*)v; |
80 | 0 | char buf[64]; |
81 | 0 | int len = date_val->to_buffer(buf); |
82 | 0 | return HashUtil::zlib_crc_hash(buf, len, seed); |
83 | 0 | } |
84 | | |
85 | 0 | case TYPE_DATEV2: { |
86 | 0 | return HashUtil::zlib_crc_hash(v, 4, seed); |
87 | 0 | } |
88 | | |
89 | 0 | case TYPE_DATETIMEV2: { |
90 | 0 | return HashUtil::zlib_crc_hash(v, 8, seed); |
91 | 0 | } |
92 | | |
93 | 3 | case TYPE_DECIMALV2: { |
94 | 3 | const DecimalV2Value* dec_val = (const DecimalV2Value*)v; |
95 | 3 | int64_t int_val = dec_val->int_value(); |
96 | 3 | int32_t frac_val = dec_val->frac_value(); |
97 | 3 | seed = HashUtil::zlib_crc_hash(&int_val, sizeof(int_val), seed); |
98 | 3 | return HashUtil::zlib_crc_hash(&frac_val, sizeof(frac_val), seed); |
99 | 0 | } |
100 | 0 | case TYPE_DECIMAL32: |
101 | 0 | return HashUtil::zlib_crc_hash(v, 4, seed); |
102 | 0 | case TYPE_DECIMAL64: |
103 | 0 | return HashUtil::zlib_crc_hash(v, 8, seed); |
104 | 0 | case TYPE_DECIMAL128I: |
105 | 0 | return HashUtil::zlib_crc_hash(v, 16, seed); |
106 | 4 | case TYPE_DECIMAL256: |
107 | 4 | return HashUtil::zlib_crc_hash(v, 32, seed); |
108 | 0 | default: |
109 | 0 | DCHECK(false) << "invalid type: " << type; |
110 | 0 | return 0; |
111 | 31 | } |
112 | 31 | } |
113 | | |
114 | | } // namespace doris |