Coverage Report

Created: 2024-11-22 16:10

/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
1
    bool operator==(const decimal12_t& value) const { return cmp(value) == 0; }
55
56
95.4k
    auto operator<=>(const decimal12_t& value) const { return cmp(value) <=> 0; }
57
58
95.4k
    int32_t cmp(const decimal12_t& other) const {
59
95.4k
        if (integer > other.integer) {
60
47.7k
            return 1;
61
47.7k
        } else if (integer == other.integer) {
62
5
            if (fraction > other.fraction) {
63
0
                return 1;
64
5
            } else if (fraction == other.fraction) {
65
5
                return 0;
66
5
            }
67
5
        }
68
69
47.7k
        return -1;
70
95.4k
    }
71
72
7
    std::string to_string() const {
73
7
        char buf[128] = {'\0'};
74
75
7
        if (integer < 0 || fraction < 0) {
76
0
            snprintf(buf, sizeof(buf), "-%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
77
7
        } else {
78
7
            snprintf(buf, sizeof(buf), "%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
79
7
        }
80
81
7
        return std::string(buf);
82
7
    }
83
84
    // Not modify this structure, ZoneMap use this from_string and to_string
85
    // to serialize decimalv2 value to segment files
86
5
    Status from_string(const std::string& str) {
87
5
        integer = 0;
88
5
        fraction = 0;
89
5
        const char* value_string = str.c_str();
90
5
        const char* sign = strchr(value_string, '-');
91
92
5
        if (sign != nullptr) {
93
0
            if (sign != value_string) {
94
0
                return Status::Error<ErrorCode::INVALID_ARGUMENT>(
95
0
                        "decimal12_t::from_string meet invalid sign");
96
0
            } else {
97
0
                ++value_string;
98
0
            }
99
0
        }
100
101
5
        const char* sepr = strchr(value_string, '.');
102
5
        if ((sepr != nullptr && sepr - value_string > MAX_INT_DIGITS_NUM) ||
103
5
            (sepr == nullptr && strlen(value_string) > MAX_INT_DIGITS_NUM)) {
104
1
            integer = 999999999999999999;
105
1
            fraction = 999999999;
106
4
        } else {
107
4
            int32_t f = 0;
108
4
            int64_t i = 0;
109
4
            if (sepr == value_string) {
110
0
                int32_t f = 0;
111
0
                sscanf(value_string, ".%9d", &f);
112
4
            } else {
113
4
                sscanf(value_string, "%18" PRId64 ".%9d", &i, &f);
114
4
            }
115
4
            integer = i;
116
4
            fraction = f;
117
118
4
            int32_t frac_len = (nullptr != sepr) ? MAX_FRAC_DIGITS_NUM - strlen(sepr + 1)
119
4
                                                 : MAX_FRAC_DIGITS_NUM;
120
4
            frac_len = frac_len > 0 ? frac_len : 0;
121
4
            fraction *= g_power_table[frac_len];
122
4
        }
123
124
5
        if (sign != nullptr) {
125
0
            fraction = -fraction;
126
0
            integer = -integer;
127
0
        }
128
129
5
        return Status::OK();
130
5
    }
131
132
    static const int32_t FRAC_RATIO = 1000000000;
133
    static const int32_t MAX_INT_DIGITS_NUM = 18;
134
    static const int32_t MAX_FRAC_DIGITS_NUM = 9;
135
136
    int64_t integer;
137
    int32_t fraction;
138
} __attribute__((packed));
139
140
static_assert(std::is_trivial<decimal12_t>::value, "decimal12_t should be a POD type");
141
142
0
inline std::ostream& operator<<(std::ostream& os, const decimal12_t& val) {
143
0
    os << val.to_string();
144
0
    return os;
145
0
}
146
147
} // namespace doris