Coverage Report

Created: 2026-09-23 08:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/raw_value.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/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/exception.h"
27
#include "common/logging.h"
28
#include "core/data_type/define_primitive_type.h"
29
#include "core/packed_int128.h"
30
#include "core/string_ref.h"
31
#include "util/hash_util.hpp"
32
33
namespace doris {
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
39.4M
                                     uint32_t seed) {
48
    // Reject binary even for NULL instead of reaching the default-type assertion or hash path.
49
39.4M
    if (type == TYPE_VARBINARY) {
50
3
        throw Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
51
3
                        "VARBINARY tablet routing hash is not supported");
52
3
    }
53
    // Hash_combine with v = 0
54
39.4M
    if (v == nullptr) {
55
0
        uint32_t value = 0x9e3779b9;
56
0
        return seed ^ (value + (seed << 6) + (seed >> 2));
57
0
    }
58
59
39.4M
    switch (type) {
60
945k
    case TYPE_VARCHAR:
61
945k
    case TYPE_HLL:
62
945k
    case TYPE_STRING:
63
946k
    case TYPE_CHAR: {
64
946k
        return HashUtil::zlib_crc_hash(v, (uint32_t)len, seed);
65
945k
    }
66
67
135
    case TYPE_BOOLEAN:
68
3.09k
    case TYPE_TINYINT:
69
3.09k
        return HashUtil::zlib_crc_hash(v, 1, seed);
70
1.31k
    case TYPE_SMALLINT:
71
1.31k
        return HashUtil::zlib_crc_hash(v, 2, seed);
72
21.7M
    case TYPE_INT:
73
21.7M
        return HashUtil::zlib_crc_hash(v, 4, seed);
74
16.7M
    case TYPE_BIGINT:
75
16.7M
        return HashUtil::zlib_crc_hash(v, 8, seed);
76
28.3k
    case TYPE_LARGEINT:
77
28.3k
        return HashUtil::zlib_crc_hash(v, 16, seed);
78
0
    case TYPE_FLOAT:
79
0
        return HashUtil::zlib_crc_hash(v, 4, seed);
80
0
    case TYPE_DOUBLE:
81
0
        return HashUtil::zlib_crc_hash(v, 8, seed);
82
23
    case TYPE_DATE:
83
48
    case TYPE_DATETIME: {
84
48
        auto* date_val = (const VecDateTimeValue*)v;
85
48
        char buf[64];
86
48
        int date_len = date_val->to_buffer(buf);
87
48
        return HashUtil::zlib_crc_hash(buf, date_len, seed);
88
23
    }
89
90
1.76k
    case TYPE_DATEV2: {
91
1.76k
        return HashUtil::zlib_crc_hash(v, 4, seed);
92
23
    }
93
94
12.4k
    case TYPE_DATETIMEV2:
95
12.5k
    case TYPE_TIMESTAMP_NS: {
96
12.5k
        return HashUtil::zlib_crc_hash(v, 8, seed);
97
12.4k
    }
98
99
2.93k
    case TYPE_TIMESTAMPTZ: {
100
2.93k
        return HashUtil::zlib_crc_hash(v, 8, seed);
101
12.4k
    }
102
103
27
    case TYPE_DECIMALV2: {
104
27
        const DecimalV2Value* dec_val = (const DecimalV2Value*)v;
105
27
        int64_t int_val = dec_val->int_value();
106
27
        int32_t frac_val = dec_val->frac_value();
107
27
        seed = HashUtil::zlib_crc_hash(&int_val, sizeof(int_val), seed);
108
27
        return HashUtil::zlib_crc_hash(&frac_val, sizeof(frac_val), seed);
109
12.4k
    }
110
156
    case TYPE_DECIMAL32:
111
156
        return HashUtil::zlib_crc_hash(v, 4, seed);
112
224
    case TYPE_DECIMAL64:
113
224
        return HashUtil::zlib_crc_hash(v, 8, seed);
114
428
    case TYPE_DECIMAL128I:
115
428
        return HashUtil::zlib_crc_hash(v, 16, seed);
116
4.37k
    case TYPE_DECIMAL256:
117
4.37k
        return HashUtil::zlib_crc_hash(v, 32, seed);
118
3
    case TYPE_IPV4:
119
3
        return HashUtil::zlib_crc_hash(v, 4, seed);
120
3
    case TYPE_IPV6:
121
3
        return HashUtil::zlib_crc_hash(v, 16, seed);
122
0
    default:
123
0
        DCHECK(false) << "invalid type: " << type;
124
0
        return 0;
125
39.4M
    }
126
39.4M
}
127
} // namespace doris