Coverage Report

Created: 2025-07-24 00:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/uint24.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
18
#pragma once
19
20
#include <cstdint>
21
#include <cstring>
22
#include <iostream>
23
#include <string>
24
25
namespace doris {
26
27
// 24bit int type, used to store date type in storage
28
struct uint24_t {
29
public:
30
    uint24_t() = default;
31
32
4.54k
    uint24_t(const uint32_t& value) {
33
4.54k
        data[0] = static_cast<uint8_t>(value);
34
4.54k
        data[1] = static_cast<uint8_t>(value >> 8);
35
4.54k
        data[2] = static_cast<uint8_t>(value >> 16);
36
4.54k
    }
37
38
0
    uint24_t& operator=(const uint32_t value) {
39
0
        data[0] = static_cast<uint8_t>(value);
40
0
        data[1] = static_cast<uint8_t>(value >> 8);
41
0
        data[2] = static_cast<uint8_t>(value >> 16);
42
0
        return *this;
43
0
    }
44
45
0
    uint24_t& operator=(const unsigned __int128& value) {
46
0
        data[0] = static_cast<uint8_t>(value);
47
0
        data[1] = static_cast<uint8_t>(value >> 8);
48
0
        data[2] = static_cast<uint8_t>(value >> 16);
49
0
        return *this;
50
0
    }
51
52
8
    uint24_t& operator=(const uint64_t value) {
53
8
        data[0] = static_cast<uint8_t>(value);
54
8
        data[1] = static_cast<uint8_t>(value >> 8);
55
8
        data[2] = static_cast<uint8_t>(value >> 16);
56
8
        return *this;
57
8
    }
58
59
0
    uint24_t& operator+=(const uint24_t& value) {
60
0
        *this = static_cast<int>(*this) + static_cast<int>(value);
61
0
        return *this;
62
0
    }
63
64
0
    uint24_t& operator>>=(const int bits) {
65
0
        *this = static_cast<unsigned int>(*this) >> bits;
66
0
        return *this;
67
0
    }
68
69
0
    uint24_t& operator|=(const uint24_t& value) {
70
0
        *this = static_cast<int>(*this) | static_cast<int>(value);
71
0
        return *this;
72
0
    }
73
74
4.32k
    operator uint32_t() const {
75
4.32k
        uint32_t value = static_cast<uint8_t>(data[0]);
76
4.32k
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[1]))) << 8;
77
4.32k
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[2]))) << 16;
78
4.32k
        return value;
79
4.32k
    }
80
81
3.09k
    uint24_t& operator=(const int value) {
82
3.09k
        data[0] = static_cast<uint8_t>(value);
83
3.09k
        data[1] = static_cast<uint8_t>(value >> 8);
84
3.09k
        data[2] = static_cast<uint8_t>(value >> 16);
85
3.09k
        return *this;
86
3.09k
    }
87
88
0
    uint24_t& operator=(const int64_t value) {
89
0
        data[0] = static_cast<uint8_t>(value);
90
0
        data[1] = static_cast<uint8_t>(value >> 8);
91
0
        data[2] = static_cast<uint8_t>(value >> 16);
92
0
        return *this;
93
0
    }
94
95
2
    bool operator==(const uint24_t& value) const { return cmp(value) == 0; }
96
97
0
    bool operator!=(const uint24_t& value) const { return cmp(value) != 0; }
98
99
95.6k
    bool operator<(const uint24_t& value) const { return cmp(value) < 0; }
100
101
0
    bool operator<=(const uint24_t& value) const { return cmp(value) <= 0; }
102
103
62
    bool operator>(const uint24_t& value) const { return cmp(value) > 0; }
104
105
0
    bool operator>=(const uint24_t& value) const { return cmp(value) >= 0; }
106
107
95.6k
    int32_t cmp(const uint24_t& other) const {
108
95.6k
        if (data[2] > other.data[2]) {
109
114
            return 1;
110
95.5k
        } else if (data[2] < other.data[2]) {
111
54
            return -1;
112
54
        }
113
114
95.5k
        if (data[1] > other.data[1]) {
115
10.8k
            return 1;
116
84.6k
        } else if (data[1] < other.data[1]) {
117
10.8k
            return -1;
118
10.8k
        }
119
120
73.7k
        if (data[0] > other.data[0]) {
121
36.8k
            return 1;
122
36.8k
        } else if (data[0] < other.data[0]) {
123
36.8k
            return -1;
124
36.8k
        }
125
126
8
        return 0;
127
73.7k
    }
128
129
11
    std::string to_string() const {
130
11
        tm time_tm;
131
11
        int value = *reinterpret_cast<const uint24_t*>(data);
132
11
        memset(&time_tm, 0, sizeof(time_tm));
133
11
        time_tm.tm_mday = static_cast<int>(value & 31);
134
11
        time_tm.tm_mon = static_cast<int>(value >> 5 & 15) - 1;
135
11
        time_tm.tm_year = static_cast<int>(value >> 9) - 1900;
136
11
        char buf[20] = {'\0'};
137
11
        strftime(buf, sizeof(buf), "%Y-%m-%d", &time_tm);
138
11
        return std::string(buf);
139
11
    }
140
141
0
    const uint8_t* get_data() const { return data; }
142
143
private:
144
    uint8_t data[3];
145
} __attribute__((packed));
146
147
static_assert(std::is_trivial<uint24_t>::value, "uint24_t should be a POD type");
148
149
0
inline std::ostream& operator<<(std::ostream& os, const uint24_t& val) {
150
0
    os << val.to_string();
151
0
    return os;
152
0
}
153
154
} // namespace doris