Coverage Report

Created: 2026-04-10 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/core/decimal12.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 <iostream>
22
#include <string>
23
24
#include "storage/utils.h"
25
26
namespace doris {
27
28
// the sign of integer must be same as fraction
29
struct decimal12_t {
30
    decimal12_t& operator+=(const decimal12_t& value) {
31
        fraction += value.fraction;
32
        integer += value.integer;
33
34
        if (fraction >= FRAC_RATIO) {
35
            integer += 1;
36
            fraction -= FRAC_RATIO;
37
        } else if (fraction <= -FRAC_RATIO) {
38
            integer -= 1;
39
            fraction += FRAC_RATIO;
40
        }
41
42
        // if sign of fraction is different from integer
43
        if ((fraction != 0) && (integer != 0) && (fraction ^ integer) < 0) {
44
            bool sign = integer < 0;
45
            integer += (sign ? 1 : -1);
46
            fraction += (sign ? -FRAC_RATIO : FRAC_RATIO);
47
        }
48
49
        //LOG(WARNING) << "agg: int=" << integer << ", frac=" << fraction;
50
        //_set_flag();
51
        return *this;
52
    }
53
54
    bool operator==(const decimal12_t& value) const { return cmp(value) == 0; }
55
56
95.6k
    auto operator<=>(const decimal12_t& value) const { return cmp(value) <=> 0; }
57
58
95.7k
    int32_t cmp(const decimal12_t& other) const {
59
95.7k
        if (integer > other.integer) {
60
47.8k
            return 1;
61
47.8k
        } else if (integer == other.integer) {
62
77
            if (fraction > other.fraction) {
63
0
                return 1;
64
77
            } else if (fraction == other.fraction) {
65
77
                return 0;
66
77
            }
67
77
        }
68
69
47.7k
        return -1;
70
95.7k
    }
71
72
160
    std::string to_string() const {
73
160
        char buf[128] = {'\0'};
74
75
160
        if (integer < 0 || fraction < 0) {
76
32
            snprintf(buf, sizeof(buf), "-%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
77
128
        } else {
78
128
            snprintf(buf, sizeof(buf), "%" PRIu64 ".%09u", std::abs(integer), std::abs(fraction));
79
128
        }
80
81
160
        return std::string(buf);
82
160
    }
83
84
    // Not modify this structure, ZoneMap use this from_string and to_string
85
    // to serialize decimalv2 value to segment files
86
0
    Status from_string(const std::string& str) {
87
0
        integer = 0;
88
0
        fraction = 0;
89
0
        const char* value_string = str.c_str();
90
0
        const char* sign = strchr(value_string, '-');
91
0
92
0
        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
0
101
0
        const char* sepr = strchr(value_string, '.');
102
0
        if ((sepr != nullptr && sepr - value_string > MAX_INT_DIGITS_NUM) ||
103
0
            (sepr == nullptr && strlen(value_string) > MAX_INT_DIGITS_NUM)) {
104
0
            integer = 999999999999999999;
105
0
            fraction = 999999999;
106
0
        } else {
107
0
            int32_t f = 0;
108
0
            int64_t i = 0;
109
0
            if (sepr == value_string) {
110
0
                sscanf(value_string, ".%9d", &f);
111
0
            } else {
112
0
                sscanf(value_string, "%18" PRId64 ".%9d", &i, &f);
113
0
            }
114
0
            integer = i;
115
0
            fraction = f;
116
0
117
0
            int64_t frac_len =
118
0
                    (nullptr != sepr) ? MAX_FRAC_DIGITS_NUM - static_cast<int64_t>(strlen(sepr + 1))
119
0
                                      : MAX_FRAC_DIGITS_NUM;
120
0
            frac_len = frac_len > 0 ? frac_len : 0;
121
0
            fraction *= g_power_table[frac_len];
122
0
        }
123
0
124
0
        if (sign != nullptr) {
125
0
            fraction = -fraction;
126
0
            integer = -integer;
127
0
        }
128
0
129
0
        return Status::OK();
130
0
    }
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
inline std::ostream& operator<<(std::ostream& os, const decimal12_t& val) {
143
    os << val.to_string();
144
    return os;
145
}
146
147
} // namespace doris