Coverage Report

Created: 2025-05-31 11:07

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