Coverage Report

Created: 2025-09-20 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/runtime/decimalv2_value.cpp
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
#include "runtime/decimalv2_value.h"
19
20
#include <fmt/format.h>
21
22
#include <cmath>
23
#include <cstring>
24
#include <iostream>
25
#include <utility>
26
27
#include "util/frame_of_reference_coding.h"
28
#include "util/string_parser.hpp"
29
30
namespace doris {
31
#include "common/compile_check_begin.h"
32
33
const int128_t DecimalV2Value::MAX_DECIMAL_VALUE;
34
35
12.7k
static inline int128_t abs(const int128_t& x) {
36
12.7k
    return (x < 0) ? -x : x;
37
12.7k
}
38
39
// x>=0 && y>=0
40
4
static int do_add(int128_t x, int128_t y, int128_t* result) {
41
4
    int error = E_DEC_OK;
42
4
    if (DecimalV2Value::MAX_DECIMAL_VALUE - x >= y) {
43
4
        *result = x + y;
44
4
    } else {
45
0
        *result = DecimalV2Value::MAX_DECIMAL_VALUE;
46
0
        error = E_DEC_OVERFLOW;
47
0
    }
48
4
    return error;
49
4
}
50
51
// x>=0 && y>=0
52
3
static int do_sub(int128_t x, int128_t y, int128_t* result) {
53
3
    int error = E_DEC_OK;
54
3
    *result = x - y;
55
3
    return error;
56
3
}
57
58
// clear leading zero for __int128
59
2
static int clz128(unsigned __int128 v) {
60
2
    if (v == 0) return sizeof(__int128);
61
2
    unsigned __int128 shifted = v >> 64;
62
2
    if (shifted != 0) {
63
0
        return leading_zeroes(static_cast<uint64_t>(shifted));
64
2
    } else {
65
2
        return leading_zeroes(static_cast<uint64_t>(v)) + 64;
66
2
    }
67
2
}
68
69
// x>0 && y>0
70
1
static int do_mul(int128_t x, int128_t y, int128_t* result) {
71
1
    int error = E_DEC_OK;
72
1
    int128_t max128 = ~(static_cast<int128_t>(1ll) << 127);
73
74
1
    int leading_zero_bits = clz128(x) + clz128(y);
75
1
    if (leading_zero_bits < sizeof(int128_t) || max128 / x < y) {
76
0
        *result = DecimalV2Value::MAX_DECIMAL_VALUE;
77
0
        error = E_DEC_OVERFLOW;
78
0
        return error;
79
0
    }
80
81
1
    int128_t product = x * y;
82
1
    *result = product / DecimalV2Value::ONE_BILLION;
83
84
    // overflow
85
1
    if (*result > DecimalV2Value::MAX_DECIMAL_VALUE) {
86
0
        *result = DecimalV2Value::MAX_DECIMAL_VALUE;
87
0
        error = E_DEC_OVERFLOW;
88
0
        return error;
89
0
    }
90
91
    // truncate with round
92
1
    int128_t remainder = product % DecimalV2Value::ONE_BILLION;
93
1
    if (remainder != 0) {
94
0
        error = E_DEC_TRUNCATED;
95
0
        if (remainder >= (DecimalV2Value::ONE_BILLION >> 1)) {
96
0
            *result += 1;
97
0
        }
98
0
    }
99
100
1
    return error;
101
1
}
102
103
// x>0 && y>0
104
2
static int do_div(int128_t x, int128_t y, int128_t* result) {
105
2
    int error = E_DEC_OK;
106
2
    int128_t dividend = x * DecimalV2Value::ONE_BILLION;
107
2
    *result = dividend / y;
108
109
    // overflow
110
2
    int128_t remainder = dividend % y;
111
2
    if (remainder != 0) {
112
0
        error = E_DEC_TRUNCATED;
113
0
        if (remainder >= (y >> 1)) {
114
0
            *result += 1;
115
0
        }
116
0
    }
117
118
2
    return error;
119
2
}
120
121
// x>0 && y>0
122
0
static int do_mod(int128_t x, int128_t y, int128_t* result) {
123
0
    int error = E_DEC_OK;
124
0
    *result = x % y;
125
0
    return error;
126
0
}
127
128
4
DecimalV2Value operator+(const DecimalV2Value& v1, const DecimalV2Value& v2) {
129
4
    int128_t result;
130
4
    int128_t x = v1.value();
131
4
    int128_t y = v2.value();
132
4
    if (x == 0) {
133
0
        result = y;
134
4
    } else if (y == 0) {
135
0
        result = x;
136
4
    } else if (x > 0) {
137
2
        if (y > 0) {
138
2
            do_add(x, y, &result);
139
2
        } else {
140
0
            do_sub(x, -y, &result);
141
0
        }
142
2
    } else { // x < 0
143
2
        if (y > 0) {
144
2
            do_sub(y, -x, &result);
145
2
        } else {
146
0
            do_add(-x, -y, &result);
147
0
            result = -result;
148
0
        }
149
2
    }
150
151
4
    return DecimalV2Value(result);
152
4
}
153
154
4
DecimalV2Value operator-(const DecimalV2Value& v1, const DecimalV2Value& v2) {
155
4
    int128_t result;
156
4
    int128_t x = v1.value();
157
4
    int128_t y = v2.value();
158
4
    if (x == 0) {
159
1
        result = -y;
160
3
    } else if (y == 0) {
161
0
        result = x;
162
3
    } else if (x > 0) {
163
2
        if (y > 0) {
164
1
            do_sub(x, y, &result);
165
1
        } else {
166
1
            do_add(x, -y, &result);
167
1
        }
168
2
    } else { // x < 0
169
1
        if (y > 0) {
170
1
            do_add(-x, y, &result);
171
1
            result = -result;
172
1
        } else {
173
0
            do_sub(-x, -y, &result);
174
0
            result = -result;
175
0
        }
176
1
    }
177
178
4
    return DecimalV2Value(result);
179
4
}
180
181
2
DecimalV2Value operator*(const DecimalV2Value& v1, const DecimalV2Value& v2) {
182
2
    int128_t result;
183
2
    int128_t x = v1.value();
184
2
    int128_t y = v2.value();
185
186
2
    if (x == 0 || y == 0) return DecimalV2Value(0);
187
188
1
    bool is_positive = (x > 0 && y > 0) || (x < 0 && y < 0);
189
190
1
    do_mul(abs(x), abs(y), &result);
191
192
1
    if (!is_positive) result = -result;
193
194
1
    return DecimalV2Value(result);
195
2
}
196
197
3
DecimalV2Value operator/(const DecimalV2Value& v1, const DecimalV2Value& v2) {
198
3
    int128_t result;
199
3
    int128_t x = v1.value();
200
3
    int128_t y = v2.value();
201
202
3
    DCHECK(y != 0);
203
3
    if (x == 0 || y == 0) return DecimalV2Value(0);
204
2
    bool is_positive = (x > 0 && y > 0) || (x < 0 && y < 0);
205
2
    do_div(abs(x), abs(y), &result);
206
207
2
    if (!is_positive) result = -result;
208
209
2
    return DecimalV2Value(result);
210
3
}
211
212
0
DecimalV2Value operator%(const DecimalV2Value& v1, const DecimalV2Value& v2) {
213
0
    int128_t result;
214
0
    int128_t x = v1.value();
215
0
    int128_t y = v2.value();
216
217
0
    DCHECK(y != 0);
218
0
    if (x == 0 || y == 0) return DecimalV2Value(0);
219
220
0
    do_mod(x, y, &result);
221
222
0
    return DecimalV2Value(result);
223
0
}
224
225
0
std::ostream& operator<<(std::ostream& os, DecimalV2Value const& decimal_value) {
226
0
    return os << decimal_value.to_string();
227
0
}
228
229
0
std::istream& operator>>(std::istream& ism, DecimalV2Value& decimal_value) {
230
0
    std::string str_buff;
231
0
    ism >> str_buff;
232
0
    decimal_value.parse_from_str(str_buff.c_str(), str_buff.size());
233
0
    return ism;
234
0
}
235
236
1
DecimalV2Value operator-(const DecimalV2Value& v) {
237
1
    return DecimalV2Value(-v.value());
238
1
}
239
240
1
DecimalV2Value& DecimalV2Value::operator+=(const DecimalV2Value& other) {
241
1
    *this = *this + other;
242
1
    return *this;
243
1
}
244
245
// Solve a one-dimensional quadratic equation: ax2 + bx + c =0
246
// Reference: https://gist.github.com/miloyip/1fcc1859c94d33a01957cf41a7c25fdf
247
// Reference: https://www.zhihu.com/question/51381686
248
static std::pair<double, double> quadratic_equation_naive(__uint128_t a, __uint128_t b,
249
0
                                                          __uint128_t c) {
250
0
    __uint128_t dis = b * b - 4 * a * c;
251
    // assert(dis >= 0);
252
    // not handling complex root
253
0
    double sqrtdis = std::sqrt(static_cast<double>(dis));
254
0
    double a_r = static_cast<double>(a);
255
0
    double b_r = static_cast<double>(b);
256
0
    double x1 = (-b_r - sqrtdis) / (a_r + a_r);
257
0
    double x2 = (-b_r + sqrtdis) / (a_r + a_r);
258
0
    return std::make_pair(x1, x2);
259
0
}
260
261
0
static inline double sgn(double x) {
262
0
    if (x > 0)
263
0
        return 1;
264
0
    else if (x < 0)
265
0
        return -1;
266
0
    else
267
0
        return 0;
268
0
}
269
270
// In the above quadratic_equation_naive solution process, we found that -b + sqrtdis will
271
// get the correct answer, and -b-sqrtdis will get the wrong answer. For two close floating-point
272
// decimals a, b, a-b will cause larger errors than a + b, which is called catastrophic cancellation.
273
// Both -b and sqrtdis are positive numbers. We can first find the roots brought by -b + sqrtdis,
274
// and then use the product of the two roots of the quadratic equation in one unknown to find another root
275
0
static std::pair<double, double> quadratic_equation_better(int128_t a, int128_t b, int128_t c) {
276
0
    if (b == 0) return quadratic_equation_naive(a, b, c);
277
0
    int128_t dis = b * b - 4 * a * c;
278
    // assert(dis >= 0);
279
    // not handling complex root
280
0
    if (dis < 0) return std::make_pair(0, 0);
281
282
    // There may be a loss of precision, but here is used to find the mantissa of the square root.
283
    // The current SCALE=9, which is less than the 15 significant digits of the double type,
284
    // so theoretically the loss of precision will not be reflected in the result.
285
0
    double sqrtdis = std::sqrt(static_cast<double>(dis));
286
0
    double a_r = static_cast<double>(a);
287
0
    double b_r = static_cast<double>(b);
288
0
    double c_r = static_cast<double>(c);
289
    // Here b comes from an unsigned integer, and sgn(b) is always 1,
290
    // which is only used to preserve the complete algorithm
291
0
    double x1 = (-b_r - sgn(b_r) * sqrtdis) / (a_r + a_r);
292
0
    double x2 = c_r / (a_r * x1);
293
0
    return std::make_pair(x1, x2);
294
0
}
295
296
// Large integer square roots, returns the integer part.
297
// The time complexity is lower than the traditional dichotomy
298
// and Newton iteration method, and the number of iterations is fixed.
299
// in real-time systems, functions that execute an unpredictable number of iterations
300
// will make the total time per task unpredictable, and introduce jitter
301
// Reference: https://www.embedded.com/integer-square-roots/
302
// Reference: https://link.zhihu.com/?target=https%3A//gist.github.com/miloyip/69663b78b26afa0dcc260382a6034b1a
303
// Reference: https://www.zhihu.com/question/35122102
304
0
static std::pair<__uint128_t, __uint128_t> sqrt_integer(__uint128_t n) {
305
0
    __uint128_t remainder = 0, root = 0;
306
0
    for (size_t i = 0; i < 64; i++) {
307
0
        root <<= 1;
308
0
        ++root;
309
0
        remainder <<= 2;
310
0
        remainder |= n >> 126;
311
0
        n <<= 2; // Extract 2 MSB from n
312
0
        if (root <= remainder) {
313
0
            remainder -= root;
314
0
            ++root;
315
0
        } else {
316
0
            --root;
317
0
        }
318
0
    }
319
0
    return std::make_pair(root >>= 1, remainder);
320
0
}
321
322
// According to the integer part and the remainder of the square root,
323
// Use one-dimensional quadratic equation to solve the fractional part of the square root
324
0
static double sqrt_fractional(int128_t sqrt_int, int128_t remainder) {
325
0
    std::pair<double, double> p = quadratic_equation_better(1, 2 * sqrt_int, -remainder);
326
0
    if ((0 < p.first) && (p.first < 1)) return p.first;
327
0
    if ((0 < p.second) && (p.second < 1)) return p.second;
328
0
    return 0;
329
0
}
330
331
const int128_t DecimalV2Value::SQRT_MOLECULAR_MAGNIFICATION = get_scale_base(PRECISION / 2);
332
const int128_t DecimalV2Value::SQRT_DENOMINATOR = int128_t(
333
        std::sqrt(ONE_BILLION) * static_cast<double>(get_scale_base(PRECISION / 2 - SCALE)));
334
335
0
DecimalV2Value DecimalV2Value::sqrt(const DecimalV2Value& v) {
336
0
    int128_t x = v.value();
337
0
    std::pair<__uint128_t, __uint128_t> sqrt_integer_ret;
338
0
    bool is_negative = (x < 0);
339
0
    if (x == 0) {
340
0
        return DecimalV2Value(0);
341
0
    }
342
0
    sqrt_integer_ret = sqrt_integer(abs(x));
343
0
    int128_t integer_root = static_cast<int128_t>(sqrt_integer_ret.first);
344
0
    int128_t integer_remainder = static_cast<int128_t>(sqrt_integer_ret.second);
345
0
    double fractional = sqrt_fractional(integer_root, integer_remainder);
346
347
    // Multiplying by SQRT_MOLECULAR_MAGNIFICATION here will not overflow,
348
    // because integer_root can be up to 64 bits.
349
0
    int128_t molecular_integer = integer_root * SQRT_MOLECULAR_MAGNIFICATION;
350
0
    int128_t molecular_fractional =
351
0
            static_cast<int128_t>(fractional * static_cast<double>(SQRT_MOLECULAR_MAGNIFICATION));
352
0
    int128_t ret = (molecular_integer + molecular_fractional) / SQRT_DENOMINATOR;
353
0
    if (is_negative) ret = -ret;
354
0
    return DecimalV2Value(ret);
355
0
}
356
357
1.40k
int DecimalV2Value::parse_from_str(const char* decimal_str, size_t length) {
358
1.40k
    int32_t error = E_DEC_OK;
359
1.40k
    StringParser::ParseResult result = StringParser::PARSE_SUCCESS;
360
361
1.40k
    _value = StringParser::string_to_decimal<TYPE_DECIMALV2>(decimal_str, length, PRECISION, SCALE,
362
1.40k
                                                             &result);
363
1.40k
    if (!config::allow_invalid_decimalv2_literal && result != StringParser::PARSE_SUCCESS) {
364
1
        error = E_DEC_BAD_NUM;
365
1.40k
    } else if (config::allow_invalid_decimalv2_literal && result == StringParser::PARSE_FAILURE) {
366
0
        error = E_DEC_BAD_NUM;
367
0
    }
368
1.40k
    return error;
369
1.40k
}
370
371
12.7k
std::string DecimalV2Value::to_string(int scale) const {
372
12.7k
    int64_t int_val = int_value();
373
12.7k
    int32_t frac_val = static_cast<int32_t>(abs(frac_value()));
374
12.7k
    if (scale < 0 || scale > SCALE) {
375
71
        if (frac_val == 0) {
376
21
            scale = 0;
377
50
        } else {
378
50
            scale = SCALE;
379
320
            while (frac_val != 0 && frac_val % 10 == 0) {
380
270
                frac_val = frac_val / 10;
381
270
                scale--;
382
270
            }
383
50
        }
384
12.6k
    } else {
385
        // roundup to FIX 17191
386
12.6k
        if (scale < SCALE) {
387
1.15k
            int32_t frac_val_tmp = frac_val / SCALE_TRIM_ARRAY[scale];
388
1.15k
            if (frac_val / SCALE_TRIM_ARRAY[scale + 1] % 10 >= 5) {
389
215
                frac_val_tmp++;
390
215
                if (frac_val_tmp >= SCALE_TRIM_ARRAY[9 - scale]) {
391
2
                    frac_val_tmp = 0;
392
2
                    _value >= 0 ? int_val++ : int_val--;
393
2
                }
394
215
            }
395
1.15k
            frac_val = frac_val_tmp;
396
1.15k
        }
397
12.6k
    }
398
12.7k
    auto f_int = fmt::format_int(int_val);
399
12.7k
    if (scale == 0) {
400
141
        return f_int.str();
401
141
    }
402
12.5k
    std::string str;
403
12.5k
    if (_value < 0 && int_val == 0 && frac_val != 0) {
404
713
        str.reserve(f_int.size() + scale + 2);
405
713
        str.push_back('-');
406
11.8k
    } else {
407
11.8k
        str.reserve(f_int.size() + scale + 1);
408
11.8k
    }
409
12.5k
    str.append(f_int.data(), f_int.size());
410
12.5k
    str.push_back('.');
411
12.5k
    if (frac_val == 0) {
412
1.29k
        str.append(scale, '0');
413
11.2k
    } else {
414
11.2k
        auto f_frac = fmt::format_int(frac_val);
415
11.2k
        if (f_frac.size() < scale) {
416
4.22k
            str.append(scale - f_frac.size(), '0');
417
4.22k
        }
418
11.2k
        str.append(f_frac.data(), f_frac.size());
419
11.2k
    }
420
12.5k
    return str;
421
12.7k
}
422
423
17
int32_t DecimalV2Value::to_buffer(char* buffer, int scale) const {
424
17
    int64_t int_val = int_value();
425
17
    int32_t frac_val = static_cast<int32_t>(abs(frac_value()));
426
17
    if (scale < 0 || scale > SCALE) {
427
9
        if (frac_val == 0) {
428
4
            scale = 0;
429
5
        } else {
430
5
            scale = SCALE;
431
25
            while (frac_val != 0 && frac_val % 10 == 0) {
432
20
                frac_val = frac_val / 10;
433
20
                scale--;
434
20
            }
435
5
        }
436
9
    } else {
437
        // roundup to FIX 17191
438
8
        if (scale < SCALE) {
439
8
            int32_t frac_val_tmp = frac_val / SCALE_TRIM_ARRAY[scale];
440
8
            if (frac_val / SCALE_TRIM_ARRAY[scale + 1] % 10 >= 5) {
441
2
                frac_val_tmp++;
442
2
                if (frac_val_tmp >= SCALE_TRIM_ARRAY[9 - scale]) {
443
2
                    frac_val_tmp = 0;
444
2
                    _value >= 0 ? int_val++ : int_val--;
445
2
                }
446
2
            }
447
8
            frac_val = frac_val_tmp;
448
8
        }
449
8
    }
450
17
    int extra_sign_size = 0;
451
17
    if (_value < 0 && int_val == 0 && frac_val != 0) {
452
2
        *buffer++ = '-';
453
2
        extra_sign_size = 1;
454
2
    }
455
17
    auto f_int = fmt::format_int(int_val);
456
17
    memcpy(buffer, f_int.data(), f_int.size());
457
17
    if (scale == 0) {
458
4
        return static_cast<int32_t>(f_int.size());
459
4
    }
460
13
    *(buffer + f_int.size()) = '.';
461
13
    buffer = buffer + f_int.size() + 1;
462
13
    if (frac_val == 0) {
463
5
        memset(buffer, '0', scale);
464
8
    } else {
465
8
        auto f_frac = fmt::format_int(frac_val);
466
8
        if (f_frac.size() < scale) {
467
2
            memset(buffer, '0', scale - f_frac.size());
468
2
            buffer = buffer + scale - f_frac.size();
469
2
        }
470
8
        memcpy(buffer, f_frac.data(), f_frac.size());
471
8
    }
472
13
    return static_cast<int32_t>(f_int.size() + scale + 1 + extra_sign_size);
473
17
}
474
475
68
std::string DecimalV2Value::to_string() const {
476
68
    return to_string(-1);
477
68
}
478
479
// NOTE: only change abstract value, do not change sign
480
0
void DecimalV2Value::to_max_decimal(int32_t precision, int32_t scale) {
481
0
    static const int64_t INT_MAX_VALUE[PRECISION] = {9ll,
482
0
                                                     99ll,
483
0
                                                     999ll,
484
0
                                                     9999ll,
485
0
                                                     99999ll,
486
0
                                                     999999ll,
487
0
                                                     9999999ll,
488
0
                                                     99999999ll,
489
0
                                                     999999999ll,
490
0
                                                     9999999999ll,
491
0
                                                     99999999999ll,
492
0
                                                     999999999999ll,
493
0
                                                     9999999999999ll,
494
0
                                                     99999999999999ll,
495
0
                                                     999999999999999ll,
496
0
                                                     9999999999999999ll,
497
0
                                                     99999999999999999ll,
498
0
                                                     999999999999999999ll};
499
0
    static const int32_t FRAC_MAX_VALUE[SCALE] = {900000000, 990000000, 999000000,
500
0
                                                  999900000, 999990000, 999999000,
501
0
                                                  999999900, 999999990, 999999999};
502
503
    // precision > 0 && scale >= 0 && scale <= SCALE
504
0
    if (precision <= 0 || scale < 0) return;
505
0
    if (scale > SCALE) scale = SCALE;
506
507
    // precision: (scale, PRECISION]
508
0
    if (precision > PRECISION) precision = PRECISION;
509
0
    if (precision - scale > PRECISION - SCALE) {
510
0
        precision = PRECISION - SCALE + scale;
511
0
    } else if (precision <= scale) {
512
0
        LOG(WARNING) << "Warning: error precision: " << precision << " or scale: " << scale;
513
0
        precision = scale + 1; // correct error precision
514
0
    }
515
516
0
    int64_t int_value = INT_MAX_VALUE[precision - scale - 1];
517
0
    int64_t frac_value = scale == 0 ? 0 : FRAC_MAX_VALUE[scale - 1];
518
0
    _value = static_cast<int128_t>(int_value) * DecimalV2Value::ONE_BILLION + frac_value;
519
0
}
520
521
18
std::size_t hash_value(DecimalV2Value const& value) {
522
18
    return value.hash(0);
523
18
}
524
525
50
int DecimalV2Value::round(DecimalV2Value* to, int rounding_scale, DecimalRoundMode op) {
526
50
    int32_t error = E_DEC_OK;
527
50
    int128_t result;
528
529
50
    if (rounding_scale >= SCALE) return error;
530
50
    if (rounding_scale < -(PRECISION - SCALE)) return 0;
531
532
50
    int128_t base = get_scale_base(SCALE - rounding_scale);
533
50
    result = _value / base;
534
535
50
    int one = _value > 0 ? 1 : -1;
536
50
    int128_t remainder = _value % base;
537
50
    switch (op) {
538
14
    case HALF_UP:
539
14
    case HALF_EVEN:
540
14
        if (abs(remainder) >= (base >> 1)) {
541
4
            result = (result + one) * base;
542
10
        } else {
543
10
            result = result * base;
544
10
        }
545
14
        break;
546
12
    case CEILING:
547
12
        if (remainder > 0 && _value > 0) {
548
4
            result = (result + one) * base;
549
8
        } else {
550
8
            result = result * base;
551
8
        }
552
12
        break;
553
12
    case FLOOR:
554
12
        if (remainder < 0 && _value < 0) {
555
4
            result = (result + one) * base;
556
8
        } else {
557
8
            result = result * base;
558
8
        }
559
12
        break;
560
12
    case TRUNCATE:
561
12
        result = result * base;
562
12
        break;
563
0
    default:
564
0
        break;
565
50
    }
566
567
50
    to->set_value(result);
568
50
    return error;
569
50
}
570
571
0
bool DecimalV2Value::greater_than_scale(int scale) {
572
0
    if (scale >= SCALE || scale < 0) {
573
0
        return false;
574
0
    } else if (scale == SCALE) {
575
0
        return true;
576
0
    }
577
578
0
    int frac_val = frac_value();
579
0
    if (scale == 0) {
580
0
        bool ret = frac_val == 0 ? false : true;
581
0
        return ret;
582
0
    }
583
584
0
    static const int values[SCALE] = {1,      10,      100,      1000,     10000,
585
0
                                      100000, 1000000, 10000000, 100000000};
586
587
0
    int base = values[SCALE - scale];
588
0
    if (frac_val % base != 0) return true;
589
0
    return false;
590
0
}
591
592
#include "common/compile_check_end.h"
593
} // end namespace doris