Coverage Report

Created: 2024-11-18 10:37

/root/doris/be/src/olap/decimal12.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 <iostream>
22
#include <string>
23
24
#include "olap/utils.h"
25
26
namespace doris {
27
28
// the sign of integer must be same as fraction
29
struct decimal12_t {
30
6
    decimal12_t& operator+=(const decimal12_t& value) {
31
6
        fraction += value.fraction;
32
6
        integer += value.integer;
33
34
6
        if (fraction >= FRAC_RATIO) {
35
0
            integer += 1;
36
0
            fraction -= FRAC_RATIO;
37
6
        } else if (fraction <= -FRAC_RATIO) {
38
1
            integer -= 1;
39
1
            fraction += FRAC_RATIO;
40
1
        }
41
42
        // if sign of fraction is different from integer
43
6
        if ((fraction != 0) && (integer != 0) && (fraction ^ integer) < 0) {
44
0
            bool sign = integer < 0;
45
0
            integer += (sign ? 1 : -1);
46
0
            fraction += (sign ? -FRAC_RATIO : FRAC_RATIO);
47
0
        }
48
49
        //LOG(WARNING) << "agg: int=" << integer << ", frac=" << fraction;
50
        //_set_flag();
51
6
        return *this;
52
6
    }
53
54
95.4k
    bool operator<(const decimal12_t& value) const { return cmp(value) < 0; }
55
56
0
    bool operator<=(const decimal12_t& value) const { return cmp(value) <= 0; }
57
58
5
    bool operator>(const decimal12_t& value) const { return cmp(value) > 0; }
59
60
0
    bool operator>=(const decimal12_t& value) const { return cmp(value) >= 0; }
61
62
1
    bool operator==(const decimal12_t& value) const { return cmp(value) == 0; }
63
64
0
    bool operator!=(const decimal12_t& value) const { return cmp(value) != 0; }
65
66
95.4k
    int32_t cmp(const decimal12_t& other) const {
67
95.4k
        if (integer > other.integer) {
68
47.7k
            return 1;
69
47.7k
        } else if (integer == other.integer) {
70
5
            if (fraction > other.fraction) {
71
0
                return 1;
72
5
            } else if (fraction == other.fraction) {
73
5
                return 0;
74
5
            }
75
5
        }
76
77
47.7k
        return -1;
78
95.4k
    }
79
80
7
    std::string to_string() const {
81
7
        char buf[128] = {'\0'};
82
83
7
        if (integer < 0 || fraction < 0) {
84
0
            snprintf(buf, sizeof(buf), "-%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
85
7
        } else {
86
7
            snprintf(buf, sizeof(buf), "%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
87
7
        }
88
89
7
        return std::string(buf);
90
7
    }
91
92
9
    Status from_string(const std::string& str) {
93
9
        integer = 0;
94
9
        fraction = 0;
95
9
        const char* value_string = str.c_str();
96
9
        const char* sign = strchr(value_string, '-');
97
98
9
        if (sign != nullptr) {
99
0
            if (sign != value_string) {
100
0
                return Status::Error<ErrorCode::INVALID_ARGUMENT>(
101
0
                        "decimal12_t::from_string meet invalid sign");
102
0
            } else {
103
0
                ++value_string;
104
0
            }
105
0
        }
106
107
9
        const char* sepr = strchr(value_string, '.');
108
9
        if ((sepr != nullptr && sepr - value_string > MAX_INT_DIGITS_NUM) ||
109
9
            (sepr == nullptr && strlen(value_string) > MAX_INT_DIGITS_NUM)) {
110
1
            integer = 999999999999999999;
111
1
            fraction = 999999999;
112
8
        } else {
113
8
            int32_t f = 0;
114
8
            int64_t i = 0;
115
8
            if (sepr == value_string) {
116
0
                int32_t f = 0;
117
0
                sscanf(value_string, ".%9d", &f);
118
8
            } else {
119
8
                sscanf(value_string, "%18" PRId64 ".%9d", &i, &f);
120
8
            }
121
8
            integer = i;
122
8
            fraction = f;
123
124
8
            int32_t frac_len = (nullptr != sepr) ? MAX_FRAC_DIGITS_NUM - strlen(sepr + 1)
125
8
                                                 : MAX_FRAC_DIGITS_NUM;
126
8
            frac_len = frac_len > 0 ? frac_len : 0;
127
8
            fraction *= g_power_table[frac_len];
128
8
        }
129
130
9
        if (sign != nullptr) {
131
0
            fraction = -fraction;
132
0
            integer = -integer;
133
0
        }
134
135
9
        return Status::OK();
136
9
    }
137
138
    static const int32_t FRAC_RATIO = 1000000000;
139
    static const int32_t MAX_INT_DIGITS_NUM = 18;
140
    static const int32_t MAX_FRAC_DIGITS_NUM = 9;
141
142
    int64_t integer;
143
    int32_t fraction;
144
} __attribute__((packed));
145
146
static_assert(std::is_trivial<decimal12_t>::value, "decimal12_t should be a POD type");
147
148
0
inline std::ostream& operator<<(std::ostream& os, const decimal12_t& val) {
149
0
    os << val.to_string();
150
0
    return os;
151
0
}
152
153
} // namespace doris