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