• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.17
/source/libs/decimal/src/decimal.c
1
/*
2
 *
3
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
4
 *
5
 * This program is free software: you can use, redistribute, and/or modify
6
 * it under the terms of the GNU Affero General Public License, version 3
7
 * or later ("AGPL"), as published by the Free Software Foundation.
8
 *
9
 * This program is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * You should have received a copy of the GNU Affero General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16

17
#include "decimal.h"
18
#include "tdataformat.h"
19
#include "wideInteger.h"
20

21
typedef enum DecimalInternalType {
22
  DECIMAL_64 = 0,
23
  DECIMAL_128 = 1,
24
} DecimalInternalType;
25

26
typedef enum DecimalRoundType {
27
  ROUND_TYPE_CEIL,
28
  ROUND_TYPE_FLOOR,
29
  ROUND_TYPE_TRUNC,
30
  ROUND_TYPE_HALF_ROUND_UP,
31
} DecimalRoundType;
32

33
#define DECIMAL_GET_INTERNAL_TYPE(dataType) ((dataType) == TSDB_DATA_TYPE_DECIMAL ? DECIMAL_128 : DECIMAL_64)
34
#define DECIMAL_GET_WORD_NUM(decimalInternalType) \
35
  ((decimalInternalType) == DECIMAL_64 ? DECIMAL_WORD_NUM(Decimal64) : DECIMAL_WORD_NUM(Decimal128))
36
static SDecimalOps* getDecimalOpsImp(DecimalInternalType t);
37

38
#define DECIMAL_MIN_ADJUSTED_SCALE 6
39

40
static Decimal64 SCALE_MULTIPLIER_64[TSDB_DECIMAL64_MAX_PRECISION + 1] = {1LL,
41
                                                                          10LL,
42
                                                                          100LL,
43
                                                                          1000LL,
44
                                                                          10000LL,
45
                                                                          100000LL,
46
                                                                          1000000LL,
47
                                                                          10000000LL,
48
                                                                          100000000LL,
49
                                                                          1000000000LL,
50
                                                                          10000000000LL,
51
                                                                          100000000000LL,
52
                                                                          1000000000000LL,
53
                                                                          10000000000000LL,
54
                                                                          100000000000000LL,
55
                                                                          1000000000000000LL,
56
                                                                          10000000000000000LL,
57
                                                                          100000000000000000LL,
58
                                                                          1000000000000000000LL};
59

60
typedef struct DecimalVar {
61
  DecimalInternalType type;
62
  int8_t              precision;
63
  int8_t              scale;
64
  int32_t             exponent;
65
  int8_t              sign;
66
  DecimalType*        pDec;
67
  int32_t             weight;
68
} DecimalVar;
69

70
static uint8_t maxPrecision(DecimalInternalType type) {
2,147,483,647✔
71
  switch (type) {
2,147,483,647✔
72
    case DECIMAL_64:
615,998,253✔
73
      return TSDB_DECIMAL64_MAX_PRECISION;
615,998,253✔
74
    case DECIMAL_128:
2,147,483,647✔
75
      return TSDB_DECIMAL128_MAX_PRECISION;
2,147,483,647✔
76
    default:
×
77
      return 0;
×
78
  }
79
}
80

81
static const uint8_t typeConvertDecimalPrec[] = {
82
    0, 1, 3, 5, 10, 19, TSDB_DECIMAL128_MAX_PRECISION, TSDB_DECIMAL_MAX_PRECISION, 0, 19, 10, 3, 5, 10, 20, 0,
83
    0, 0, 0, 0, 0,  0};
84

85
int32_t decimalGetRetType(const SDataType* pLeftT, const SDataType* pRightT, EOperatorType opType,
327,841✔
86
                          SDataType* pOutType) {
87
  if (pLeftT->type == TSDB_DATA_TYPE_JSON || pRightT->type == TSDB_DATA_TYPE_JSON ||
327,841✔
88
      pLeftT->type == TSDB_DATA_TYPE_VARBINARY || pRightT->type == TSDB_DATA_TYPE_VARBINARY)
327,449✔
89
    return TSDB_CODE_TSC_INVALID_OPERATION;
784✔
90
  if ((pLeftT->type >= TSDB_DATA_TYPE_BLOB && pLeftT->type <= TSDB_DATA_TYPE_GEOMETRY) ||
327,057✔
91
      (pRightT->type >= TSDB_DATA_TYPE_BLOB && pRightT->type <= TSDB_DATA_TYPE_GEOMETRY)) {
318,939✔
92
    return TSDB_CODE_TSC_INVALID_OPERATION;
16,236✔
93
  }
94
  if (IS_FLOAT_TYPE(pLeftT->type) || IS_FLOAT_TYPE(pRightT->type) || IS_VAR_DATA_TYPE(pLeftT->type) ||
310,821✔
95
      IS_VAR_DATA_TYPE(pRightT->type)) {
197,364✔
96
    pOutType->type = TSDB_DATA_TYPE_DOUBLE;
150,620✔
97
    pOutType->bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
150,620✔
98
    return 0;
150,620✔
99
  }
100

101
  if (IS_NULL_TYPE(pLeftT->type) || IS_NULL_TYPE(pRightT->type)) {
160,201✔
102
    pOutType->type = TSDB_DATA_TYPE_NULL;
784✔
103
    pOutType->bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
784✔
104
    return 0;
784✔
105
  }
106
  uint8_t p1 = pLeftT->precision, s1 = pLeftT->scale, p2 = pRightT->precision, s2 = pRightT->scale;
159,417✔
107

108
  if (!IS_DECIMAL_TYPE(pLeftT->type)) {
159,417✔
109
    p1 = typeConvertDecimalPrec[pLeftT->type];
48,904✔
110
    s1 = 0;
48,904✔
111
  }
112
  if (!IS_DECIMAL_TYPE(pRightT->type)) {
159,417✔
113
    p2 = typeConvertDecimalPrec[pRightT->type];
62,805✔
114
    s2 = 0;
62,805✔
115
  }
116

117
  switch (opType) {
159,417✔
118
    case OP_TYPE_ADD:
88,190✔
119
    case OP_TYPE_SUB:
120
      pOutType->scale = TMAX(s1, s2);
88,190✔
121
      pOutType->precision = TMAX(p1 - s1, p2 - s2) + pOutType->scale + 1;
88,190✔
122
      break;
88,190✔
123
    case OP_TYPE_MULTI:
47,015✔
124
      pOutType->scale = s1 + s2;
47,015✔
125
      pOutType->precision = p1 + p2 + 1;
47,015✔
126
      break;
47,015✔
127
    case OP_TYPE_DIV:
23,232✔
128
      pOutType->scale = TMAX(s1 + p2 + 1, DECIMAL_MIN_ADJUSTED_SCALE);
23,232✔
129
      pOutType->precision = p1 - s1 + s2 + pOutType->scale;
23,232✔
130
      break;
23,232✔
131
    case OP_TYPE_REM:
980✔
132
      pOutType->scale = TMAX(s1, s2);
980✔
133
      pOutType->precision = TMIN(p1 - s1, p2 - s2) + pOutType->scale;
980✔
134
      break;
980✔
135
    default:
×
136
      return TSDB_CODE_TSC_INVALID_OPERATION;
×
137
  }
138
  if (pOutType->precision > TSDB_DECIMAL_MAX_PRECISION) {
159,417✔
139
    int8_t minScale = TMIN(DECIMAL_MIN_ADJUSTED_SCALE, pOutType->scale);
69,937✔
140
    int8_t delta = pOutType->precision - TSDB_DECIMAL_MAX_PRECISION;
69,937✔
141
    pOutType->precision = TSDB_DECIMAL_MAX_PRECISION;
69,937✔
142
    pOutType->scale = TMAX(minScale, (int8_t)(pOutType->scale) - delta);
69,937✔
143
  }
144
  pOutType->type = TSDB_DATA_TYPE_DECIMAL;
159,417✔
145
  pOutType->bytes = tDataTypes[pOutType->type].bytes;
159,417✔
146
  return 0;
159,417✔
147
}
148

149
int32_t calcCurPrec(int32_t prec, int32_t places, int32_t exp, int32_t weight, int32_t firstValidScale) {
2,147,483,647✔
150
  if (exp == 0) return prec + places;
2,147,483,647✔
151
  if (exp < 0) {
68,298✔
152
    if (weight + exp >= 0) return prec + places;
44,884✔
153
    return prec + places - exp - weight;
30,576✔
154
  }
155
  if (weight > 0) return prec + places;
23,414✔
156
  return prec + places - TMIN(firstValidScale - 1, exp);
8,528✔
157
}
158

159
int32_t calcActualWeight(int32_t prec, int32_t scale, int32_t exp, int32_t weight, int32_t firstValidScale) {
454,166,604✔
160
  if (exp == 0) return prec - scale;
454,166,604✔
161
  if (exp < 0) {
10,383✔
162
    if (weight + exp >= 0) return weight + exp;
4,116✔
163
    return 0;
2,744✔
164
  }
165
  if (weight > 0) return weight + exp;
6,267✔
166
  if (firstValidScale == 0) return 0;
2,744✔
167
  return TMAX(0, exp - firstValidScale);
1,372✔
168
}
169

170
static int32_t decimalVarFromStr(const char* str, int32_t len, DecimalVar* result) {
454,046,755✔
171
  int32_t code = 0, pos = 0;
454,046,755✔
172
  int32_t expectPrecision = result->precision;
454,046,755✔
173
  int32_t expectScale = result->scale;
454,203,115✔
174
  result->precision = 0;
454,249,298✔
175
  result->scale = 0;
454,278,103✔
176
  result->exponent = 0;
454,298,372✔
177
  bool     leadingZeroes = true, afterPoint = false, rounded = false, stop = false;
454,299,498✔
178
  uint32_t places = 0;
454,299,498✔
179
  result->sign = 1;
454,299,498✔
180
  int32_t weight = 0;
454,200,858✔
181
  int32_t firstValidScale = 0;
454,200,858✔
182

183
  if (len == 0) return TSDB_CODE_INVALID_DATA_FMT;
454,200,858✔
184
  SDecimalOps* pOps = getDecimalOpsImp(result->type);
454,200,858✔
185

186
  // sign
187
  switch (str[pos]) {
454,230,654✔
188
    case '-':
46,264,611✔
189
      result->sign = -1;
46,264,611✔
190
    case '+':
46,250,134✔
191
      pos++;
46,250,134✔
192
    default:
454,224,202✔
193
      break;
454,224,202✔
194
  }
195
  int32_t pos2 = pos;
454,224,202✔
196
  while(pos2 < len) {
2,147,483,647✔
197
    if (isdigit(str[pos2] || str[pos] == '.')) continue;
2,147,483,647✔
198
    if (str[pos2] == 'e' || str[pos2] == 'E') {
2,147,483,647✔
199
      result->exponent = taosStr2Int32(str + pos2 + 1, NULL, 10);
10,736✔
200
      break;
10,775✔
201
    }
202
    pos2++;
2,147,483,647✔
203
  }
204

205
  for (; pos < len && !stop; ++pos) {
2,147,483,647✔
206
    switch (str[pos]) {
2,147,483,647✔
207
      case '.':
166,796,774✔
208
        weight = result->precision;
166,796,774✔
209
        afterPoint = true;
166,801,921✔
210
        leadingZeroes = false;
166,801,921✔
211
        break;
166,801,921✔
212
      case '0':
369,983,252✔
213
        if (leadingZeroes) break;
369,983,252✔
214
        if (afterPoint) {
273,406,697✔
215
          places++;
146,812,384✔
216
          break;
146,812,384✔
217
        }
218
      case '1':
219
      case '2':
220
      case '3':
221
      case '4':
222
      case '5':
223
      case '6':
224
      case '7':
225
      case '8':
226
      case '9': {
227
        leadingZeroes = false;
2,147,483,647✔
228
        ++places;
2,147,483,647✔
229
        if (firstValidScale == 0 && afterPoint) firstValidScale = places;
2,147,483,647✔
230

231
        int32_t   curPrec = calcCurPrec(result->precision, places, result->exponent, weight, firstValidScale);
2,147,483,647✔
232
        int32_t   scaleUp = 0;
2,147,483,647✔
233
        Decimal64 delta = {0};
2,147,483,647✔
234
        if (curPrec > maxPrecision(result->type)) {
2,147,483,647✔
235
          if (!afterPoint) return TSDB_CODE_DECIMAL_OVERFLOW;
93,169,345✔
236
          int32_t curScale = result->scale - result->exponent + places;
93,169,149✔
237
          if (rounded || curScale > expectScale + 1 /*scale already overflowed, no need do rounding*/ ||
93,169,149✔
238
              curPrec - 1 != maxPrecision(result->type) /* not the maxPrecision + 1 digit, no need do rounding*/ ||
86,115✔
239
              str[pos] < '5')
86,115✔
240
            break;
241

242
          // Do rounding for the maxPrecision + 1 digit.
243
          // Here we cannot directly add this digit into the results, because it may cause overflow.
244
          DECIMAL64_SET_VALUE(&delta, 1);
51,709✔
245
          scaleUp = places - 1;
51,709✔
246
          rounded = true;
51,709✔
247
        } else {
248
          scaleUp = places;
2,147,483,647✔
249
          DECIMAL64_SET_VALUE(&delta, str[pos] - '0');
2,147,483,647✔
250
        }
251

252
        result->precision += scaleUp;
2,147,483,647✔
253
        if (afterPoint) result->scale += scaleUp;
2,147,483,647✔
254
        while (scaleUp != 0) {
2,147,483,647✔
255
          int32_t curScale = TMIN(17, scaleUp);
2,147,483,647✔
256
          pOps->multiply(result->pDec, &SCALE_MULTIPLIER_64[curScale], DECIMAL_WORD_NUM(Decimal64));
2,147,483,647✔
257
          scaleUp -= curScale;
2,147,483,647✔
258
        }
259
        pOps->add(result->pDec, &delta, DECIMAL_WORD_NUM(Decimal64));
2,147,483,647✔
260
        places = 0;
2,147,483,647✔
261
      } break;
2,147,483,647✔
262
      case 'e':
10,775✔
263
      case 'E': {
264
          stop = true;
10,775✔
265
        } break;
10,775✔
266
      default:
×
267
        stop = true;
×
268
        break;
×
269
    }
270
  }
271
  result->weight = calcActualWeight(result->precision, result->scale, result->exponent, weight, firstValidScale);
452,091,239✔
272
  if (result->precision + result->scale > 0) result->scale -= result->exponent;
454,267,621✔
273
  if (result->sign < 0) {
454,283,286✔
274
    pOps->negate(result->pDec);
46,273,649✔
275
  }
276
  return code;
454,277,831✔
277
}
278

279
int32_t decimal64ToDataVal(const Decimal64* dec, SValue* pVal) {
310,504,967✔
280
  VALUE_SET_TRIVIAL_DATUM(pVal, DECIMAL64_GET_VALUE(dec));
310,504,967✔
281
  return TSDB_CODE_SUCCESS;
310,533,613✔
282
}
283

284
int32_t decimal128ToDataVal(Decimal128* dec, SValue* pVal) {
136,888,770✔
285
  void* pV = taosMemCalloc(1, sizeof(Decimal128));
136,888,770✔
286
  if (!pV) return terrno;
136,923,320✔
287
  memcpy(pV, dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
136,923,320✔
288
  valueSetDatum(pVal, TSDB_DATA_TYPE_DECIMAL, pV, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
136,924,090✔
289
  return TSDB_CODE_SUCCESS;
136,927,820✔
290
}
291

292
#define DECIMAL64_ONE  SCALE_MULTIPLIER_64[0]
293

294
#define DECIMAL64_GET_MAX(precision, pMax)                                \
295
  do {                                                                    \
296
    *(pMax) = SCALE_MULTIPLIER_64[precision];                             \
297
    decimal64Subtract(pMax, &DECIMAL64_ONE, DECIMAL_WORD_NUM(Decimal64)); \
298
  } while (0)
299

300
#define DECIMAL64_SIGN(pDec) (1 | (DECIMAL64_GET_VALUE(pDec) >> 63))
301

302
static void decimal64GetWhole(const DecimalType* pDec, int8_t scale, DecimalType* pWhole) {
229,388,337✔
303
  const SDecimalOps* pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
229,388,337✔
304
  DECIMAL64_CLONE(pWhole, pDec);
229,388,337✔
305
  Decimal64 scaleMul = SCALE_MULTIPLIER_64[scale];
229,388,337✔
306
  pOps->divide(pWhole, &scaleMul, 1, NULL);
229,388,337✔
307
  pOps->abs(pWhole);
229,388,337✔
308
}
229,388,337✔
309

310
static void decimal64GetFrac(const DecimalType* pDec, int8_t scale, DecimalType* pFrac) {
227,878,457✔
311
  const SDecimalOps* pOps = getDecimalOpsImp(DECIMAL_64);
227,878,457✔
312
  DECIMAL64_CLONE(pFrac, pDec);
227,878,457✔
313
  Decimal64 scaleMul = SCALE_MULTIPLIER_64[scale];
227,878,457✔
314
  pOps->mod(pFrac, &scaleMul, 1);
227,878,457✔
315
  pOps->abs(pFrac);
227,878,457✔
316
}
227,878,457✔
317

318
static void    decimal64Negate(DecimalType* pInt);
319
static void    decimal64Abs(DecimalType* pInt);
320
static void    decimal64Add(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
321
static void    decimal64Subtract(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
322
static void    decimal64Multiply(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
323
static void    decimal64divide(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum,
324
                               DecimalType* pRemainder);
325
static void    decimal64Mod(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
326
static bool    decimal64Lt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
327
static bool    decimal64Gt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
328
static bool    decimal64Eq(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
329
static void    decimal64ScaleDown(Decimal64* pDec, uint8_t scaleDown, bool round);
330
static void    decimal64ScaleUp(Decimal64* pDec, uint8_t scaleUp);
331
static void    decimal64ScaleTo(Decimal64* pDec, uint8_t oldScale, uint8_t newScale);
332
int32_t        decimal64ToStr(const DecimalType* pInt, uint8_t scale, char* pBuf, int32_t bufLen);
333

334
static void decimal64RoundWithPositiveScale(Decimal64* pDec, uint8_t prec, int8_t scale, uint8_t toPrec,
335
                                            uint8_t toScale, DecimalRoundType roundType, bool* overflow);
336

337
static void    decimal128Negate(DecimalType* pInt);
338
static void    decimal128Abs(DecimalType* pWord);
339
static void    decimal128Add(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
340
static void    decimal128Subtract(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
341
static void    decimal128Multiply(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
342
static void    decimal128Divide(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum,
343
                                DecimalType* pRemainder);
344
static void    decimal128Mod(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
345
static bool    decimal128Lt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
346
static bool    decimal128Gt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
347
static bool    decimal128Eq(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum);
348
static void    decimal128ScaleTo(Decimal128* pDec, uint8_t oldScale, uint8_t newScale);
349
static void    decimal128ScaleDown(Decimal128* pDec, uint8_t scaleDown, bool round);
350
static void    decimal128ScaleUp(Decimal128* pDec, uint8_t scaleUp);
351
static int32_t decimal128CountLeadingBinaryZeros(const Decimal128* pDec);
352
static int32_t decimal128FromInt64(DecimalType* pDec, uint8_t prec, uint8_t scale, int64_t val);
353
static int32_t decimal128FromUint64(DecimalType* pDec, uint8_t prec, uint8_t scale, uint64_t val);
354
int32_t        decimal128ToStr(const DecimalType* pInt, uint8_t scale, char* pBuf, int32_t bufLen);
355
//
356
// rounding functions
357
static void    decimal128RoundWithPositiveScale(Decimal128* pDec, uint8_t prec, uint8_t scale, uint8_t toPrec,
358
                                                uint8_t toScale, DecimalRoundType roundType, bool* overflow);
359
static void    decimal128RoundWithNegativeScale(Decimal128* pDec, uint8_t prec, uint8_t scale, int8_t toScale,
360
                                                DecimalRoundType roundType, bool* overflow);
361
static void    decimal128ModifyScaleAndPrecision(Decimal128* pDec, uint8_t scale, uint8_t toPrec, int8_t toScale,
362
                                                 bool* overflow);
363
static int32_t decimal128CountRoundingDelta(const Decimal128* pDec, int8_t scale, int8_t toScale,
364
                                            DecimalRoundType roundType);
365

366
SDecimalOps decimal64Ops = {decimal64Negate,   decimal64Abs,    decimal64Add,  decimal64Subtract,
367
                            decimal64Multiply, decimal64divide, decimal64Mod,  decimal64Lt,
368
                            decimal64Gt,       decimal64Eq,     decimal64ToStr};
369
SDecimalOps decimal128Ops = {decimal128Negate,   decimal128Abs,    decimal128Add,  decimal128Subtract,
370
                             decimal128Multiply, decimal128Divide, decimal128Mod,  decimal128Lt,
371
                             decimal128Gt,       decimal128Eq,     decimal128ToStr};
372

373
static SDecimalOps* getDecimalOpsImp(DecimalInternalType t) {
1,408,320,262✔
374
  switch (t) {
1,408,320,262✔
375
    case DECIMAL_128:
515,920,320✔
376
      return &decimal128Ops;
515,920,320✔
377
    case DECIMAL_64:
892,435,994✔
378
      return &decimal64Ops;
892,435,994✔
379
    default:
×
380
      return NULL;
×
381
  }
382
}
383
const SDecimalOps* getDecimalOps(int8_t dataType) { return getDecimalOpsImp(DECIMAL_GET_INTERNAL_TYPE(dataType)); }
726,246,412✔
384

385
void makeDecimal64(Decimal64* pDec64, int64_t w) { DECIMAL64_SET_VALUE(pDec64, w); }
2,147,483,647✔
386

387
void decimal64Negate(DecimalType* pInt) {
218,807,760✔
388
  Decimal64* pDec = pInt;
218,807,760✔
389
  DECIMAL64_SET_VALUE(pDec, -DECIMAL64_GET_VALUE(pDec));
218,807,760✔
390
}
218,812,104✔
391
void decimal64Abs(DecimalType* pInt) {
2,147,483,647✔
392
  Decimal64* pDec = pInt;
2,147,483,647✔
393
  DECIMAL64_SET_VALUE(pDec, TABS(DECIMAL64_GET_VALUE(pDec)));
2,147,483,647✔
394
}
2,147,483,647✔
395
void decimal64Add(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
616,952,164✔
396
  Decimal64*       pDecL = pLeft;
616,952,164✔
397
  const Decimal64* pDecR = pRight;
616,952,164✔
398
  DECIMAL64_SET_VALUE(pDecL, SAFE_INT64_ADD(DECIMAL64_GET_VALUE(pDecL), DECIMAL64_GET_VALUE(pDecR)));
616,952,164✔
399
}
617,028,402✔
400
void decimal64Subtract(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
341,118,696✔
401
  Decimal64*       pDecL = pLeft;
341,118,696✔
402
  const Decimal64* pDecR = pRight;
341,118,696✔
403
  DECIMAL64_SET_VALUE(pDecL, SAFE_INT64_SUBTRACT(DECIMAL64_GET_VALUE(pDecL), DECIMAL64_GET_VALUE(pDecR)));
341,118,696✔
404
}
341,153,331✔
405
void decimal64Multiply(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
629,936,990✔
406
  Decimal64* pDecL = pLeft;
629,936,990✔
407
  Decimal64  decR = *((Decimal64*)pRight);
629,936,990✔
408
  bool       sign = DECIMAL64_SIGN(pDecL) != DECIMAL64_SIGN(&decR);
630,206,924✔
409
  decimal64Abs(pLeft);
630,296,105✔
410
  decimal64Abs(&decR);
630,393,763✔
411
  uint64_t x = DECIMAL64_GET_VALUE(pDecL), y = DECIMAL64_GET_VALUE(&decR);
630,241,335✔
412
  x *= y;
630,256,201✔
413
  DECIMAL64_SET_VALUE(pDecL, x);
630,256,201✔
414
  if (sign) decimal64Negate(pDecL);
629,588,588✔
415
}
630,318,913✔
416
void decimal64divide(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum, DecimalType* pRemainder) {
723,841,927✔
417
  Decimal64* pDecL = pLeft;
723,841,927✔
418
  Decimal64  decR = *((Decimal64*)pRight);
723,841,927✔
419
  Decimal64* pDecRemainder = pRemainder;
723,841,927✔
420
  bool       sign = DECIMAL64_SIGN(pDecL) != DECIMAL64_SIGN(&decR);
723,841,927✔
421
  decimal64Abs(pDecL);
723,841,927✔
422
  decimal64Abs(&decR);
723,841,927✔
423
  uint64_t x = DECIMAL64_GET_VALUE(pDecL), y = DECIMAL64_GET_VALUE(&decR);
723,841,927✔
424
  uint64_t z = x;
723,841,927✔
425
  x /= y;
723,841,927✔
426
  DECIMAL64_SET_VALUE(pDecL, x);
723,841,927✔
427
  if (sign) decimal64Negate(pDecL);
723,841,927✔
428
  if (pDecRemainder) {
723,841,927✔
429
    z %= y;
484,229,874✔
430
    DECIMAL64_SET_VALUE(pDecRemainder, z);
484,229,874✔
431
  }
432
}
723,841,927✔
433
void decimal64Mod(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
238,402,455✔
434
  Decimal64  remainder = {0};
238,402,455✔
435
  Decimal64* pDec = pLeft;
238,402,455✔
436
  decimal64divide(pLeft, pRight, rightWordNum, &remainder);
238,402,455✔
437
  DECIMAL64_SET_VALUE(pDec, DECIMAL64_GET_VALUE(&remainder));
238,402,455✔
438
}
238,402,455✔
439

440
bool decimal64Lt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
118,853,586✔
441
  const Decimal64 *pDecL = pLeft, *pDecR = pRight;
118,853,586✔
442
  return DECIMAL64_GET_VALUE(pDecL) < DECIMAL64_GET_VALUE(pDecR);
118,853,586✔
443
}
444
bool decimal64Gt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
483,206,723✔
445
  return DECIMAL64_GET_VALUE((Decimal64*)pLeft) > DECIMAL64_GET_VALUE((Decimal64*)pRight);
483,206,723✔
446
}
447
bool decimal64Eq(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
10,523,998✔
448
  return DECIMAL64_GET_VALUE((Decimal64*)pLeft) == DECIMAL64_GET_VALUE(((Decimal64*)pRight));
10,523,998✔
449
}
450
int32_t decimal64ToStr(const DecimalType* pInt, uint8_t scale, char* pBuf, int32_t bufLen) {
229,388,337✔
451
  if (!pBuf) return TSDB_CODE_INVALID_PARA;
229,388,337✔
452
  Decimal whole = {0}, frac = {0};
229,388,337✔
453
  int32_t pos = 0;
229,388,337✔
454
  char    buf[64] = {0};
229,388,337✔
455

456
  if (DECIMAL64_SIGN((Decimal64*)pInt) == -1) {
229,388,337✔
457
    pos = snprintf(buf, sizeof(buf), "-");
60,521,822✔
458
  }
459
  decimal64GetWhole(pInt, scale, &whole);
229,388,337✔
460
  pos += snprintf(buf + pos, bufLen - pos, "%" PRId64, DECIMAL64_GET_VALUE(&whole));
229,388,337✔
461
  if (scale > 0) {
229,388,337✔
462
    decimal64GetFrac(pInt, scale, &frac);
227,878,457✔
463
    if (DECIMAL64_GET_VALUE(&frac) != 0 || DECIMAL64_GET_VALUE(&whole) != 0) {
227,878,457✔
464
      TAOS_STRCAT(buf + pos, ".");
212,422,427✔
465
      pos += 1;
212,422,427✔
466
      // NOTE: never generate format string dynamically
467
      //       decimalTest has been passed.
468
      snprintf(buf + pos, bufLen - pos, "%0*" PRIu64, scale, DECIMAL64_GET_VALUE(&frac));
212,422,427✔
469
    }
470
  }
471
  TAOS_STRNCPY(pBuf, buf, bufLen);
229,388,337✔
472
  return 0;
229,388,337✔
473
}
474

475
// return 1 if positive or zero, else return -1
476
#define DECIMAL128_SIGN(pDec) (1 | (DECIMAL128_HIGH_WORD(pDec) >> 63))
477

478
static const Decimal128 SCALE_MULTIPLIER_128[TSDB_DECIMAL128_MAX_PRECISION + 1] = {
479
    DEFINE_DECIMAL128(1LL, 0),
480
    DEFINE_DECIMAL128(10LL, 0),
481
    DEFINE_DECIMAL128(100LL, 0),
482
    DEFINE_DECIMAL128(1000LL, 0),
483
    DEFINE_DECIMAL128(10000LL, 0),
484
    DEFINE_DECIMAL128(100000LL, 0),
485
    DEFINE_DECIMAL128(1000000LL, 0),
486
    DEFINE_DECIMAL128(10000000LL, 0),
487
    DEFINE_DECIMAL128(100000000LL, 0),
488
    DEFINE_DECIMAL128(1000000000LL, 0),
489
    DEFINE_DECIMAL128(10000000000LL, 0),
490
    DEFINE_DECIMAL128(100000000000LL, 0),
491
    DEFINE_DECIMAL128(1000000000000LL, 0),
492
    DEFINE_DECIMAL128(10000000000000LL, 0),
493
    DEFINE_DECIMAL128(100000000000000LL, 0),
494
    DEFINE_DECIMAL128(1000000000000000LL, 0),
495
    DEFINE_DECIMAL128(10000000000000000LL, 0),
496
    DEFINE_DECIMAL128(100000000000000000LL, 0),
497
    DEFINE_DECIMAL128(1000000000000000000LL, 0),
498
    DEFINE_DECIMAL128(10000000000000000000ULL, 0LL),
499
    DEFINE_DECIMAL128(7766279631452241920ULL, 5LL),
500
    DEFINE_DECIMAL128(3875820019684212736ULL, 54LL),
501
    DEFINE_DECIMAL128(1864712049423024128ULL, 542LL),
502
    DEFINE_DECIMAL128(200376420520689664ULL, 5421LL),
503
    DEFINE_DECIMAL128(2003764205206896640ULL, 54210LL),
504
    DEFINE_DECIMAL128(1590897978359414784ULL, 542101LL),
505
    DEFINE_DECIMAL128(15908979783594147840ULL, 5421010LL),
506
    DEFINE_DECIMAL128(11515845246265065472ULL, 54210108LL),
507
    DEFINE_DECIMAL128(4477988020393345024ULL, 542101086LL),
508
    DEFINE_DECIMAL128(7886392056514347008ULL, 5421010862LL),
509
    DEFINE_DECIMAL128(5076944270305263616ULL, 54210108624LL),
510
    DEFINE_DECIMAL128(13875954555633532928ULL, 542101086242LL),
511
    DEFINE_DECIMAL128(9632337040368467968ULL, 5421010862427LL),
512
    DEFINE_DECIMAL128(4089650035136921600ULL, 54210108624275LL),
513
    DEFINE_DECIMAL128(4003012203950112768ULL, 542101086242752LL),
514
    DEFINE_DECIMAL128(3136633892082024448ULL, 5421010862427522LL),
515
    DEFINE_DECIMAL128(12919594847110692864ULL, 54210108624275221LL),
516
    DEFINE_DECIMAL128(68739955140067328ULL, 542101086242752217LL),
517
    DEFINE_DECIMAL128(687399551400673280ULL, 5421010862427522170LL),
518
};
519

520
static double getDoubleScaleMultiplier(uint8_t scale) {
1,425,822,663✔
521
  static double SCALE_MULTIPLIER_DOUBLE[TSDB_DECIMAL_MAX_PRECISION + 1] = {0};
522
  static bool   initialized = false;
523
  if (!initialized) {
1,425,822,663✔
524
    SCALE_MULTIPLIER_DOUBLE[0] = 1.0;
11,284✔
525
    for (int32_t idx = 1; idx <= TSDB_DECIMAL_MAX_PRECISION; ++idx) {
440,076✔
526
      SCALE_MULTIPLIER_DOUBLE[idx] = SCALE_MULTIPLIER_DOUBLE[idx - 1] * 10;
428,792✔
527
    }
528
    initialized = true;
11,284✔
529
  }
530
  return SCALE_MULTIPLIER_DOUBLE[scale];
1,425,822,663✔
531
};
532

533
#define DECIMAL128_ONE  SCALE_MULTIPLIER_128[0]
534
#define DECIMAL128_TEN  SCALE_MULTIPLIER_128[1]
535

536
// To calculate how many bits for integer X.
537
// eg. 999(3 digits) -> 1111100111(10 bits) -> bitsForNumDigits[3] = 10
538
static const int32_t bitsForNumDigits[] = {0,  4,  7,  10, 14,  17,  20,  24,  27,  30,  34,  37,  40,
539
                                           44, 47, 50, 54, 57,  60,  64,  67,  70,  74,  77,  80,  84,
540
                                           87, 90, 94, 97, 100, 103, 107, 110, 113, 117, 120, 123, 127};
541

542
#define DECIMAL128_GET_MAX(precision, pMax)                                  \
543
  do {                                                                       \
544
    *(pMax) = SCALE_MULTIPLIER_128[precision];                               \
545
    decimal128Subtract(pMax, &DECIMAL128_ONE, DECIMAL_WORD_NUM(Decimal128)); \
546
  } while (0)
547

548
void makeDecimal128(Decimal128* pDec128, int64_t hi, uint64_t low) {
2,147,483,647✔
549
  DECIMAL128_SET_HIGH_WORD(pDec128, hi);
2,147,483,647✔
550
  DECIMAL128_SET_LOW_WORD(pDec128, low);
2,147,483,647✔
551
}
2,147,483,647✔
552

553
static void makeDecimal128FromDecimal64(Decimal128* pTarget, Decimal64 decimal64) {
2,147,483,647✔
554
  bool negative = false;
2,147,483,647✔
555
  if (DECIMAL64_SIGN(&decimal64) == -1) {
2,147,483,647✔
556
    decimal64Negate(&decimal64);
16,197,548✔
557
    negative = true;
16,197,548✔
558
  }
559
  makeDecimal128(pTarget, 0, DECIMAL64_GET_VALUE(&decimal64));
2,147,483,647✔
560
  if (negative) decimal128Negate(pTarget);
2,147,483,647✔
561
}
2,147,483,647✔
562

563
static void decimal128Negate(DecimalType* pWord) {
1,454,308,314✔
564
  Decimal128* pDec = (Decimal128*)pWord;
1,454,308,314✔
565
  uint64_t    lo = ~DECIMAL128_LOW_WORD(pDec) + 1;
1,454,308,314✔
566
  int64_t     hi = ~DECIMAL128_HIGH_WORD(pDec);
1,454,311,575✔
567
  if (lo == 0) hi = SAFE_INT64_ADD(hi, 1);
1,454,315,272✔
568
  makeDecimal128(pDec, hi, lo);
1,454,315,272✔
569
}
1,454,313,018✔
570

571
static void decimal128Abs(DecimalType* pWord) {
2,147,483,647✔
572
  if (DECIMAL128_SIGN((Decimal128*)pWord) == -1) {
2,147,483,647✔
573
    decimal128Negate(pWord);
879,038,380✔
574
  }
575
}
2,147,483,647✔
576

577
#define DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pTarget, rightDec, pWord) \
578
  if (rightWordNum != DECIMAL_WORD_NUM(Decimal128)) {                                   \
579
    Decimal64 d64 = {0};                                                        \
580
    makeDecimal64(&d64, *(int64_t*)pWord);                                      \
581
    makeDecimal128FromDecimal64(&rightDec, d64);                                \
582
    pTarget = &rightDec;                                                        \
583
  }
584

585
static void decimal128Add(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
2,147,483,647✔
586
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
2,147,483,647✔
587
  Decimal128  right = {0};
2,147,483,647✔
588
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
2,147,483,647✔
589

590
  int64_t  hi = SAFE_INT64_ADD(DECIMAL128_HIGH_WORD(pLeftDec), DECIMAL128_HIGH_WORD(pRightDec));
2,147,483,647✔
591
  uint64_t lo = DECIMAL128_LOW_WORD(pLeftDec) + DECIMAL128_LOW_WORD(pRightDec);
2,147,483,647✔
592
  hi = SAFE_INT64_ADD(hi, lo < DECIMAL128_LOW_WORD(pLeftDec));
2,147,483,647✔
593
  makeDecimal128(pLeftDec, hi, lo);
2,147,483,647✔
594
}
2,147,483,647✔
595

596
static void decimal128Subtract(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
750,538,090✔
597
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
750,538,090✔
598
  Decimal128  right = {0};
750,538,090✔
599
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
750,545,376✔
600

601
  int64_t  hi = SAFE_INT64_SUBTRACT(DECIMAL128_HIGH_WORD(pLeftDec), DECIMAL128_HIGH_WORD(pRightDec));
750,545,376✔
602
  uint64_t lo = DECIMAL128_LOW_WORD(pLeftDec) - DECIMAL128_LOW_WORD(pRightDec);
750,556,055✔
603
  hi = SAFE_INT64_SUBTRACT(hi, lo > DECIMAL128_LOW_WORD(pLeftDec));
750,559,629✔
604
  makeDecimal128(pLeftDec, hi, lo);
750,560,434✔
605
}
750,560,088✔
606

607
static void decimal128Multiply(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
2,147,483,647✔
608
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
2,147,483,647✔
609
  Decimal128  right = {0};
2,147,483,647✔
610
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
2,147,483,647✔
611

612
  bool       negate = DECIMAL128_SIGN(pLeftDec) != DECIMAL128_SIGN(pRightDec);
2,147,483,647✔
613
  Decimal128 x = *pLeftDec, y = *pRightDec;
2,147,483,647✔
614
  decimal128Abs(&x);
2,147,483,647✔
615
  decimal128Abs(&y);
2,147,483,647✔
616

617
  UInt128 res = {0}, tmp = {0};
2,147,483,647✔
618
  makeUInt128(&res, DECIMAL128_HIGH_WORD(&x), DECIMAL128_LOW_WORD(&x));
2,147,483,647✔
619
  makeUInt128(&tmp, DECIMAL128_HIGH_WORD(&y), DECIMAL128_LOW_WORD(&y));
2,147,483,647✔
620
  uInt128Multiply(&res, &tmp);
2,147,483,647✔
621
  makeDecimal128(pLeftDec, uInt128Hi(&res), uInt128Lo(&res));
2,147,483,647✔
622
  if (negate) decimal128Negate(pLeftDec);
2,147,483,647✔
623
}
2,147,483,647✔
624

625
static bool decimal128Lt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
2,147,483,647✔
626
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
2,147,483,647✔
627
  Decimal128  right = {0};
2,147,483,647✔
628
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
2,147,483,647✔
629

630
  return DECIMAL128_HIGH_WORD(pLeftDec) < DECIMAL128_HIGH_WORD(pRightDec) ||
2,147,483,647✔
631
         (DECIMAL128_HIGH_WORD(pLeftDec) == DECIMAL128_HIGH_WORD(pRightDec) &&
1,806,925,044✔
632
          DECIMAL128_LOW_WORD(pLeftDec) < DECIMAL128_LOW_WORD(pRightDec));
694,154,907✔
633
}
634

635
static void decimal128Divide(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum,
171,499,272✔
636
                             DecimalType* pRemainder) {
637
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight, *pRemainderDec = (Decimal128*)pRemainder;
171,499,272✔
638
  Decimal128  right = {0};
171,499,272✔
639
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
171,499,272✔
640

641
  bool leftNegate = DECIMAL128_SIGN(pLeftDec) == -1, rightNegate = DECIMAL128_SIGN(pRightDec) == -1;
171,499,272✔
642
  UInt128    a = {0}, b = {0}, c = {0}, d = {0};
171,499,272✔
643
  Decimal128 x = *pLeftDec, y = *pRightDec;
171,499,272✔
644
  decimal128Abs(&x);
171,499,272✔
645
  decimal128Abs(&y);
171,499,272✔
646
  makeUInt128(&a, DECIMAL128_HIGH_WORD(&x), DECIMAL128_LOW_WORD(&x));
171,499,272✔
647
  makeUInt128(&d, DECIMAL128_HIGH_WORD(&x), DECIMAL128_LOW_WORD(&x));
171,499,272✔
648
  makeUInt128(&b, DECIMAL128_HIGH_WORD(&y), DECIMAL128_LOW_WORD(&y));
171,499,272✔
649
  uInt128Divide(&a, &b);
171,499,272✔
650
  uInt128Mod(&d, &b);
171,499,272✔
651
  makeDecimal128(pLeftDec, uInt128Hi(&a), uInt128Lo(&a));
171,499,272✔
652
  if (pRemainder) makeDecimal128(pRemainderDec, uInt128Hi(&d), uInt128Lo(&d));
171,499,272✔
653
  if (leftNegate != rightNegate) decimal128Negate(pLeftDec);
171,499,272✔
654
  if (leftNegate && pRemainder) decimal128Negate(pRemainderDec);
171,499,272✔
655
}
171,499,272✔
656

657
static void decimal128Mod(DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
19,634,001✔
658
  Decimal128 pLeftDec = *(Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight, right = {0};
19,634,001✔
659
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
19,634,001✔
660

661
  decimal128Divide(&pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal128),
19,634,001✔
662
                   pLeft);
663
}
19,634,001✔
664

665
static bool decimal128Gt(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
1,258,337,697✔
666
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
1,258,337,697✔
667
  Decimal128  right = {0};
1,258,337,697✔
668
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
1,258,342,072✔
669

670
  return decimal128Lt(pRightDec, pLeftDec, DECIMAL_WORD_NUM(Decimal128));
1,258,342,072✔
671
}
672

673
static bool decimal128Eq(const DecimalType* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
39,738,388✔
674
  Decimal128 *pLeftDec = (Decimal128*)pLeft, *pRightDec = (Decimal128*)pRight;
39,738,388✔
675
  Decimal128  right = {0};
39,738,388✔
676
  DECIMAL128_CHECK_RIGHT_WORD_NUM(rightWordNum, pRightDec, right, pRight);
39,738,388✔
677

678
  return DECIMAL128_HIGH_WORD(pLeftDec) == DECIMAL128_HIGH_WORD(pRightDec) &&
64,790,870✔
679
         DECIMAL128_LOW_WORD(pLeftDec) == DECIMAL128_LOW_WORD(pRightDec);
25,052,482✔
680
}
681

682
#define DIGIT_NUM_ONCE 18
683
static void extractDecimal128Digits(const Decimal128* pDec, uint64_t* digits, int32_t* digitNum) {
788,103,700✔
684
  UInt128 a = {0};
788,103,700✔
685
  UInt128 b = {0};
788,103,700✔
686
  *digitNum = 0;
788,103,700✔
687
  makeUInt128(&a, DECIMAL128_HIGH_WORD(pDec), DECIMAL128_LOW_WORD(pDec));
788,103,700✔
688
  while (!uInt128Eq(&a, &uInt128Zero)) {
1,910,853,551✔
689
    uint64_t hi = uInt128Hi(&a);
1,122,749,851✔
690
    uint64_t lo = uInt128Lo(&a);
1,122,749,851✔
691

692
    uint64_t hiQuotient = hi / k1e18;
1,122,749,851✔
693
    uint64_t hiRemainder = hi % k1e18;
1,122,749,851✔
694
    makeUInt128(&b, hiRemainder, lo);
1,122,749,851✔
695
    uInt128Divide(&b, &uInt128_1e18);
1,122,749,851✔
696
    uint64_t loQuotient = uInt128Lo(&b);
1,122,749,851✔
697
    makeUInt128(&b, hiRemainder, lo);
1,122,749,851✔
698
    uInt128Mod(&b, &uInt128_1e18);
1,122,749,851✔
699
    uint64_t loRemainder = uInt128Lo(&b);
1,122,749,851✔
700
    makeUInt128(&a, hiQuotient, loQuotient);
1,122,749,851✔
701
    digits[(*digitNum)++] = loRemainder;
1,122,749,851✔
702
  }
703
}
788,103,700✔
704

705
int32_t decimal128ToStr(const DecimalType* pInt, uint8_t scale, char* pBuf, int32_t bufLen) {
788,103,896✔
706
  if (!pBuf) return TSDB_CODE_INVALID_PARA;
788,103,896✔
707
  const Decimal128* pDec = (const Decimal128*)pInt;
788,103,700✔
708
  bool              negative = DECIMAL128_SIGN(pDec) == -1;
788,103,700✔
709
  uint64_t          segments[3] = {0};
788,103,700✔
710
  int32_t           digitNum = 0;
788,103,700✔
711
  char              buf[64] = {0}, buf2[64] = {0};
788,103,700✔
712
  int32_t           len = 0;
788,103,700✔
713
  if (negative) {
788,103,700✔
714
    Decimal128 copy = {0};
230,105,660✔
715
    makeDecimal128(&copy, DECIMAL128_HIGH_WORD(pDec), DECIMAL128_LOW_WORD(pDec));
230,105,660✔
716
    decimal128Abs(&copy);
230,105,660✔
717
    extractDecimal128Digits(&copy, segments, &digitNum);
230,105,660✔
718
    TAOS_STRNCAT(buf2, "-", 2);
230,105,660✔
719
  } else {
720
    extractDecimal128Digits(pDec, segments, &digitNum);
557,998,040✔
721
  }
722
  if (digitNum == 0) {
788,103,700✔
723
    TAOS_STRNCPY(pBuf, "0", bufLen);
57,698,541✔
724
    return 0;
57,698,541✔
725
  }
726
  for (int32_t i = digitNum - 1; i >= 0; --i) {
1,853,155,010✔
727
    len += snprintf(buf + len, 64 - len, i == digitNum - 1 ? "%" PRIu64 : "%018" PRIu64, segments[i]);
1,122,749,851✔
728
  }
729
  int32_t wholeLen = len - scale;
730,405,159✔
730
  if (wholeLen > 0) {
730,405,159✔
731
    TAOS_STRNCAT(buf2, buf, wholeLen);
721,551,414✔
732
  } else {
733
    TAOS_STRNCAT(buf2, "0", 2);
8,853,745✔
734
  }
735
  if (scale > 0) {
730,405,159✔
736
    static const char *format = "0000000000000000000000000000000000000000";
737
    TAOS_STRNCAT(buf2, ".", 2);
729,655,805✔
738
    if (wholeLen < 0) TAOS_STRNCAT(buf2, format, TABS(wholeLen));
729,655,805✔
739
    TAOS_STRNCAT(buf2, buf + TMAX(0, wholeLen), scale);
729,655,805✔
740
  }
741
  TAOS_STRNCPY(pBuf, buf2, bufLen);
730,405,159✔
742
  return 0;
730,405,159✔
743
}
744
int32_t decimalToStr(const DecimalType* pDec, int8_t dataType, int8_t precision, int8_t scale, char* pBuf,
1,017,492,233✔
745
                     int32_t bufLen) {
746
  DecimalInternalType iType = DECIMAL_GET_INTERNAL_TYPE(dataType);
1,017,492,233✔
747
  switch (iType) {
1,017,492,233✔
748
    case DECIMAL_64:
229,388,337✔
749
      return decimal64ToStr(pDec, scale, pBuf, bufLen);
229,388,337✔
750
    case DECIMAL_128:
788,103,896✔
751
      return decimal128ToStr(pDec, scale, pBuf, bufLen);
788,103,896✔
752
    default:
×
753
      break;
×
754
  }
755
  return TSDB_CODE_INVALID_PARA;
×
756
}
757

758
static int32_t decimalAddLargePositive(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
13,252,470✔
759
                                    const SDataType* pOT) {
760
  Decimal wholeX = *pX, wholeY = *pY, fracX = {0}, fracY = {0};
13,252,470✔
761
  decimal128Divide(&wholeX, &SCALE_MULTIPLIER_128[pXT->scale], DECIMAL_WORD_NUM(Decimal), &fracX);
13,252,470✔
762
  decimal128Divide(&wholeY, &SCALE_MULTIPLIER_128[pYT->scale], DECIMAL_WORD_NUM(Decimal), &fracY);
13,252,470✔
763

764
  uint8_t maxScale = TMAX(pXT->scale, pYT->scale);
13,252,470✔
765
  decimal128ScaleUp(&fracX, maxScale - pXT->scale);
13,252,470✔
766
  decimal128ScaleUp(&fracY, maxScale - pYT->scale);
13,252,470✔
767

768
  Decimal pMultiplier = SCALE_MULTIPLIER_128[maxScale];
13,252,470✔
769
  Decimal right = fracX;
13,252,470✔
770
  Decimal carry = {0};
13,252,470✔
771
  decimal128Subtract(&pMultiplier, &fracY, DECIMAL_WORD_NUM(Decimal));
13,252,470✔
772
  if (!decimal128Gt(&pMultiplier, &fracX, DECIMAL_WORD_NUM(Decimal))) {
13,252,470✔
773
    decimal128Subtract(&right, &pMultiplier, DECIMAL_WORD_NUM(Decimal));
2,169,589✔
774
    makeDecimal128(&carry, 0, 1);
2,169,589✔
775
  } else {
776
    decimal128Add(&right, &fracY, DECIMAL_WORD_NUM(Decimal));
11,082,881✔
777
  }
778

779
  decimal128ScaleDown(&right, maxScale - pOT->scale, true);
13,252,470✔
780
  if (decimal128AddCheckOverflow(&wholeX, &wholeY, DECIMAL_WORD_NUM(Decimal))) {
13,252,470✔
781
    return TSDB_CODE_DECIMAL_OVERFLOW;
392✔
782
  }
783
  decimal128Add(&wholeX, &wholeY, DECIMAL_WORD_NUM(Decimal));
13,252,078✔
784
  if (decimal128AddCheckOverflow(&wholeX, &carry, DECIMAL_WORD_NUM(Decimal))) {
13,252,078✔
785
    return TSDB_CODE_DECIMAL_OVERFLOW;
×
786
  }
787
  decimal128Add(&wholeX, &carry, DECIMAL_WORD_NUM(Decimal));
13,252,078✔
788
  decimal128Multiply(&wholeX, &SCALE_MULTIPLIER_128[pOT->scale], DECIMAL_WORD_NUM(Decimal));
13,252,078✔
789
  decimal128Add(&wholeX, &right, DECIMAL_WORD_NUM(Decimal));
13,252,078✔
790
  *pX = wholeX;
13,252,078✔
791
  return 0;
13,252,078✔
792
}
793

794
static void decimalAddLargeNegative(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
11,946,180✔
795
                                    const SDataType* pOT) {
796
  Decimal wholeX = *pX, wholeY = *pY, fracX = {0}, fracY = {0};
11,946,180✔
797
  decimal128Divide(&wholeX, &SCALE_MULTIPLIER_128[pXT->scale], DECIMAL_WORD_NUM(Decimal), &fracX);
11,946,180✔
798
  decimal128Divide(&wholeY, &SCALE_MULTIPLIER_128[pYT->scale], DECIMAL_WORD_NUM(Decimal), &fracY);
11,946,180✔
799

800
  uint8_t maxScale = TMAX(pXT->scale, pYT->scale);
11,946,180✔
801
  decimal128ScaleUp(&fracX, maxScale - pXT->scale);
11,946,180✔
802
  decimal128ScaleUp(&fracY, maxScale - pYT->scale);
11,946,180✔
803

804
  decimal128Add(&wholeX, &wholeY, DECIMAL_WORD_NUM(Decimal));
11,946,180✔
805
  decimal128Add(&fracX, &fracY, DECIMAL_WORD_NUM(Decimal));
11,946,180✔
806

807
  if (DECIMAL128_SIGN(&wholeX) == -1 && decimal128Gt(&fracX, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal128))) {
11,946,180✔
808
    decimal128Add(&wholeX, &DECIMAL128_ONE, DECIMAL_WORD_NUM(Decimal));
2,000,721✔
809
    decimal128Subtract(&fracX, &SCALE_MULTIPLIER_128[maxScale], DECIMAL_WORD_NUM(Decimal));
2,000,721✔
810
  } else if (decimal128Gt(&wholeX, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal128)) && DECIMAL128_SIGN(&fracX) == -1) {
9,945,459✔
811
    decimal128Subtract(&wholeX, &DECIMAL128_ONE, DECIMAL_WORD_NUM(Decimal));
2,250,717✔
812
    decimal128Add(&fracX, &SCALE_MULTIPLIER_128[maxScale], DECIMAL_WORD_NUM(Decimal));
2,250,717✔
813
  }
814

815
  decimal128ScaleDown(&fracX, maxScale - pOT->scale, true);
11,946,180✔
816
  decimal128Multiply(&wholeX, &SCALE_MULTIPLIER_128[pOT->scale], DECIMAL_WORD_NUM(Decimal));
11,946,180✔
817
  decimal128Add(&wholeX, &fracX, DECIMAL_WORD_NUM(Decimal));
11,946,180✔
818
  *pX = wholeX;
11,946,180✔
819
}
11,946,180✔
820

821
static int32_t decimalAdd(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
82,965,076✔
822
                       const SDataType* pOT) {
823
  int32_t code = 0;
82,965,076✔
824
  if (pOT->precision < TSDB_DECIMAL_MAX_PRECISION) {
82,965,076✔
825
    uint8_t maxScale = TMAX(pXT->scale, pYT->scale);
57,766,426✔
826
    Decimal tmpY = *pY;
57,766,426✔
827
    decimal128ScaleTo(pX, pXT->scale, maxScale);
57,766,426✔
828
    decimal128ScaleTo(&tmpY, pYT->scale, maxScale);
57,766,426✔
829
    decimal128Add(pX, &tmpY, DECIMAL_WORD_NUM(Decimal));
57,766,426✔
830
  } else {
831
    int8_t signX = DECIMAL128_SIGN(pX), signY = DECIMAL128_SIGN(pY);
25,198,650✔
832
    if (signX == 1 && signY == 1) {
25,198,650✔
833
      code = decimalAddLargePositive(pX, pXT, pY, pYT, pOT);
10,415,919✔
834
    } else if (signX == -1 && signY == -1) {
14,782,731✔
835
      decimal128Negate(pX);
2,836,551✔
836
      Decimal y = *pY;
2,836,551✔
837
      decimal128Negate(&y);
2,836,551✔
838
      code = decimalAddLargePositive(pX, pXT, &y, pYT, pOT);
2,836,551✔
839
      decimal128Negate(pX);
2,836,551✔
840
    } else {
841
      decimalAddLargeNegative(pX, pXT, pY, pYT, pOT);
11,946,180✔
842
    }
843
  }
844
  return code;
82,965,076✔
845
}
846

847
static void makeInt256FromDecimal128(Int256* pTarget, const Decimal128* pDec) {
362,521,935✔
848
  bool negative = DECIMAL128_SIGN(pDec) == -1;
362,521,935✔
849
  Decimal128 abs = *pDec;
362,521,935✔
850
  decimal128Abs(&abs);
362,521,935✔
851
  UInt128 tmp = {DECIMAL128_LOW_WORD(&abs), DECIMAL128_HIGH_WORD(&abs)};
362,521,935✔
852
  *pTarget = makeInt256(int128Zero, tmp);
362,521,935✔
853
  if (negative) {
362,521,935✔
854
    *pTarget = int256Negate(pTarget);
66,822,804✔
855
  }
856
}
362,521,935✔
857

858
static Int256 int256ScaleBy(const Int256* pX, int32_t scale) {
120,840,645✔
859
  Int256 result = *pX;
120,840,645✔
860
  if (scale > 0) {
120,840,645✔
861
    Int256 multiplier = {0};
120,606,792✔
862
    makeInt256FromDecimal128(&multiplier, &SCALE_MULTIPLIER_128[scale]);
120,606,792✔
863
    result = int256Multiply(pX, &multiplier);
120,606,792✔
864
  } else if (scale < 0) {
233,853✔
865
    Int256 divisor = {0};
233,853✔
866
    makeInt256FromDecimal128(&divisor, &SCALE_MULTIPLIER_128[-scale]);
233,853✔
867
    result = int256Divide(pX, &divisor);
233,853✔
868
    Int256 remainder = int256Mod(pX, &divisor);
233,853✔
869
    Int256 afterShift = int256RightShift(&divisor, 1);
233,853✔
870
    remainder = int256Abs(&remainder);
233,853✔
871
    if (!int256Gt(&afterShift, &remainder)) {
233,853✔
872
      if (int256Gt(pX, &int256Zero)) {
51,400✔
873
        result = int256Add(&result, &int256One);
33,328✔
874
      } else {
875
        result = int256Subtract(&result, &int256One);
18,072✔
876
      }
877
    }
878
  }
879
  return result;
120,840,645✔
880
}
881

882
static bool convertInt256ToDecimal128(const Int256* pX, Decimal128* pDec) {
234,441✔
883
  bool overflow = false;
234,441✔
884
  Int256 abs = int256Abs(pX);
234,441✔
885
  bool isNegative = int256Lt(pX, &int256Zero);
234,441✔
886
  UInt128 low = int256Lo(&abs);
234,441✔
887
  uint64_t lowLow= uInt128Lo(&low);
234,441✔
888
  uint64_t lowHigh = uInt128Hi(&low);
234,441✔
889
  Int256 afterShift = int256RightShift(&abs, 128);
234,441✔
890

891
  if (int256Gt(&afterShift, &int256Zero)) {
234,441✔
892
    overflow = true;
14,503✔
893
  } else if (lowHigh > INT64_MAX) {
219,938✔
894
    overflow = true;
753✔
895
  } else {
896
    makeDecimal128(pDec, lowHigh, lowLow);
219,185✔
897
    if (decimal128Gt(pDec, &decimal128Max, DECIMAL_WORD_NUM(Decimal128))) {
219,185✔
898
      overflow = true;
1,506✔
899
    }
900
  }
901
  if (isNegative) {
234,441✔
902
    decimal128Negate(pDec);
73,794✔
903
  }
904
  return overflow;
234,441✔
905
}
906

907
static int32_t decimalMultiply(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
25,366,547✔
908
                               const SDataType* pOT) {
909
  if (pOT->precision < TSDB_DECIMAL_MAX_PRECISION) {
25,366,547✔
910
    decimal128Multiply(pX, pY, DECIMAL_WORD_NUM(Decimal));
15,581,807✔
911
  } else if (decimal128Eq(pX, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal)) ||
18,452,781✔
912
             decimal128Eq(pY, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal))) {
8,668,041✔
913
    makeDecimal128(pX, 0, 0);
2,088,069✔
914
  } else {
915
    int8_t  deltaScale = pXT->scale + pYT->scale - pOT->scale;
7,696,671✔
916
    Decimal xAbs = *pX, yAbs = *pY;
7,696,671✔
917
    decimal128Abs(&xAbs);
7,696,671✔
918
    decimal128Abs(&yAbs);
7,696,671✔
919
    if (deltaScale == 0) {
7,696,671✔
920
      // no need to trim scale
921
      Decimal max = DECIMAL128_MAX;
4,201,936✔
922

923
      decimal128Divide(&max, &yAbs, DECIMAL_WORD_NUM(Decimal), NULL);
4,201,936✔
924
      if (decimal128Gt(&xAbs, &max, DECIMAL_WORD_NUM(Decimal))) {
4,201,936✔
925
        return TSDB_CODE_DECIMAL_OVERFLOW;
×
926
      } else {
927
        decimal128Multiply(pX, pY, DECIMAL_WORD_NUM(Decimal));
4,201,936✔
928
      }
929
    } else {
930
      int32_t leadingZeros = decimal128CountLeadingBinaryZeros(&xAbs) + decimal128CountLeadingBinaryZeros(&yAbs);
3,494,735✔
931
      if (leadingZeros <= 128) {
3,494,735✔
932
        // need to trim scale
933
        Int256 x256 = {0}, y256 = {0};
233,853✔
934
        makeInt256FromDecimal128(&x256, pX);
233,853✔
935
        makeInt256FromDecimal128(&y256, pY);
233,853✔
936
        Int256 res = int256Multiply(&x256, &y256);
233,853✔
937
        if (deltaScale != 0) {
233,853✔
938
          res = int256ScaleBy(&res, -deltaScale);
233,853✔
939
        }
940
        bool overflow = convertInt256ToDecimal128(&res, pX);
233,853✔
941
        if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
233,853✔
942
      } else {
943
        // no need to trim scale
944
        if (deltaScale <= 38) {
3,260,882✔
945
          decimal128Multiply(pX, pY, DECIMAL_WORD_NUM(Decimal));
3,260,882✔
946
          decimal128ScaleDown(pX, deltaScale, true);
3,260,882✔
947
        } else {
948
          makeDecimal128(pX, 0, 0);
×
949
        }
950
      }
951
    }
952
  }
953
  return 0;
25,349,785✔
954
}
955

956
static int32_t decimalDivide(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
171,762✔
957
                      const SDataType* pOT) {
958
  if (decimal128Eq(pY, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal))) {
171,762✔
959
    return TSDB_CODE_DIVISION_BY_ZERO;
392✔
960
  }
961

962
  int8_t deltaScale = pOT->scale + pYT->scale - pXT->scale;
171,370✔
963

964
  Decimal xTmp = *pX;
171,370✔
965
  decimal128Abs(&xTmp);
171,370✔
966
  int32_t bitsOccupied = 128 - decimal128CountLeadingBinaryZeros(&xTmp);
171,370✔
967
  if (bitsOccupied + bitsForNumDigits[deltaScale] <= 127) {
171,370✔
968
    xTmp = *pX;
170,978✔
969
    decimal128ScaleUp(&xTmp, deltaScale);
170,978✔
970
    Decimal remainder = {0};
170,978✔
971
    decimal128Divide(&xTmp, pY, DECIMAL_WORD_NUM(Decimal), &remainder);
170,978✔
972

973
    Decimal tmpY = *pY;
170,978✔
974
    decimal128Abs(&tmpY);
170,978✔
975
    decimal128Multiply(&remainder, &decimal128Two, DECIMAL_WORD_NUM(Decimal));
170,978✔
976
    decimal128Abs(&remainder);
170,978✔
977
    if (!decimal128Lt(&remainder, &tmpY, DECIMAL_WORD_NUM(Decimal))) {
170,978✔
978
      Decimal64 extra = {(DECIMAL128_SIGN(pX) ^ DECIMAL128_SIGN(pY)) + 1};
4,127✔
979
      decimal128Add(&xTmp, &extra, DECIMAL_WORD_NUM(Decimal64));
4,127✔
980
    }
981
    *pX = xTmp;
170,978✔
982
  } else {
983
    Int256 x256 = {0}, y256 = {0};
392✔
984
    makeInt256FromDecimal128(&x256, pX);
392✔
985
    Int256 xScaledUp = int256ScaleBy(&x256, deltaScale);
392✔
986
    makeInt256FromDecimal128(&y256, pY);
392✔
987
    Int256 res = int256Divide(&xScaledUp, &y256);
392✔
988
    Int256 remainder = int256Mod(&xScaledUp, &y256);
392✔
989

990
    remainder = int256Multiply(&remainder, &int256Two);
392✔
991
    remainder = int256Abs(&remainder);
392✔
992
    y256 = int256Abs(&y256);
392✔
993
    if (!int256Lt(&remainder, &y256)) {
392✔
994
      if ((DECIMAL128_SIGN(pX) ^ DECIMAL128_SIGN(pY)) == 0) {
×
995
        res = int256Add(&res, &int256One);
×
996
      } else {
997
        res = int256Subtract(&res, &int256One);
×
998
      }
999
    }
1000
    bool overflow = convertInt256ToDecimal128(&res, pX);
392✔
1001
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
392✔
1002
  }
1003
  return 0;
171,370✔
1004
}
1005

1006
static int32_t decimalMod(Decimal* pX, const SDataType* pXT, const Decimal* pY, const SDataType* pYT,
980✔
1007
                          const SDataType* pOT) {
1008
  if (decimal128Eq(pY, &DECIMAL128_ZERO, DECIMAL_WORD_NUM(Decimal))) {
980✔
1009
    return TSDB_CODE_DIVISION_BY_ZERO;
×
1010
  }
1011
  Decimal xAbs = *pX, yAbs = *pY;
980✔
1012
  decimal128Abs(&xAbs);
980✔
1013
  decimal128Abs(&yAbs);
980✔
1014
  int32_t xlz = decimal128CountLeadingBinaryZeros(&xAbs), ylz = decimal128CountLeadingBinaryZeros(&yAbs);
980✔
1015
  if (pXT->scale < pYT->scale) {
980✔
1016
    // x scale up
1017
    xlz = xlz - bitsForNumDigits[pYT->scale - pXT->scale];
×
1018
  } else if (pXT->scale > pYT->scale) {
980✔
1019
    // y scale up
1020
    ylz = ylz - bitsForNumDigits[pXT->scale - pYT->scale];
588✔
1021
  }
1022
  int32_t lz = TMIN(xlz, ylz);
980✔
1023
  if (lz >= 2) {
980✔
1024
    // it's safe to scale up
1025
    yAbs = *pY;
784✔
1026
    decimal128ScaleTo(pX, pXT->scale, TMAX(pXT->scale, pYT->scale));
784✔
1027
    decimal128ScaleTo(&yAbs, pYT->scale, TMAX(pXT->scale, pYT->scale));
784✔
1028
    decimal128Mod(pX, &yAbs, DECIMAL_WORD_NUM(Decimal));
784✔
1029
  } else {
1030
    Int256 x256 = {0}, y256 = {0};
196✔
1031
    makeInt256FromDecimal128(&x256, pX);
196✔
1032
    makeInt256FromDecimal128(&y256, pY);
196✔
1033
    if (pXT->scale < pYT->scale) {
196✔
1034
      x256 = int256ScaleBy(&x256, pYT->scale - pXT->scale);
×
1035
    } else if (pXT->scale > pYT->scale) {
196✔
1036
      y256 = int256ScaleBy(&y256, pXT->scale - pYT->scale);
196✔
1037
    }
1038
    Int256 res = int256Mod(&x256, &y256);
196✔
1039
    if (convertInt256ToDecimal128(&res, pX)) {
196✔
1040
      return TSDB_CODE_DECIMAL_OVERFLOW;
×
1041
    }
1042
  }
1043
  return 0;
980✔
1044
}
1045

1046
int32_t decimalOp(EOperatorType op, const SDataType* pLeftT, const SDataType* pRightT, const SDataType* pOutT,
112,220,420✔
1047
                  const void* pLeftData, const void* pRightData, void* pOutputData) {
1048
  int32_t code = 0;
112,220,420✔
1049

1050
  Decimal   left = {0}, right = {0};
112,220,420✔
1051
  SDataType lt = {.type = TSDB_DATA_TYPE_DECIMAL,
112,220,420✔
1052
                  .precision = TSDB_DECIMAL_MAX_PRECISION,
1053
                  .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
224,432,216✔
1054
                  .scale = pLeftT->scale};
112,220,420✔
1055
  SDataType rt = {.type = TSDB_DATA_TYPE_DECIMAL,
112,220,420✔
1056
                  .precision = TSDB_DECIMAL_MAX_PRECISION,
1057
                  .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
112,220,420✔
1058
                  .scale = 0};
1059
  if (pRightT) rt.scale = pRightT->scale;
112,220,420✔
1060
  if (TSDB_DATA_TYPE_DECIMAL != pLeftT->type) {
112,220,420✔
1061
    code = convertToDecimal(pLeftData, pLeftT, &left, &lt);
36,343,317✔
1062
    if (TSDB_CODE_SUCCESS != code) return code;
36,343,317✔
1063
  } else {
1064
    left = *(Decimal*)pLeftData;
75,877,103✔
1065
  }
1066
  if (pRightT && TSDB_DATA_TYPE_DECIMAL != pRightT->type) {
112,220,420✔
1067
    code = convertToDecimal(pRightData, pRightT, &right, &rt);
35,769,908✔
1068
    if (TSDB_CODE_SUCCESS != code) return code;
35,769,908✔
1069
    pRightData = &right;
35,769,908✔
1070
  } else if (pRightData){
76,450,512✔
1071
    right = *(Decimal*)pRightData;
72,734,457✔
1072
  }
1073
#ifdef DEBUG
1074
  char left_var[64] = {0}, right_var[64] = {0};
1075
  decimal128ToStr(&left, lt.scale, left_var, 64);
1076
  decimal128ToStr(&right, rt.scale, right_var, 64);
1077
#endif
1078

1079
  switch (op) {
112,220,420✔
1080
    case OP_TYPE_ADD:
41,483,224✔
1081
      code = decimalAdd(&left, &lt, &right, &rt, pOutT);
41,483,224✔
1082
      break;
41,483,224✔
1083
    case OP_TYPE_SUB:
41,481,852✔
1084
      decimal128Negate(&right);
41,481,852✔
1085
      code = decimalAdd(&left, &lt, &right, &rt, pOutT);
41,481,852✔
1086
      break;
41,481,852✔
1087
    case OP_TYPE_MULTI:
25,366,547✔
1088
      code = decimalMultiply(&left, &lt, &right, &rt, pOutT);
25,366,547✔
1089
      break;
25,366,547✔
1090
    case OP_TYPE_DIV:
171,762✔
1091
      code = decimalDivide(&left, &lt, &right, &rt, pOutT);
171,762✔
1092
      break;
171,762✔
1093
    case OP_TYPE_REM:
980✔
1094
      code = decimalMod(&left, &lt, &right, &rt, pOutT);
980✔
1095
      break;
980✔
1096
    case OP_TYPE_MINUS:
3,716,055✔
1097
      decimal128Negate(&left);
3,716,055✔
1098
      break;
3,716,055✔
1099
    default:
×
1100
      code = TSDB_CODE_TSC_INVALID_OPERATION;
×
1101
      break;
×
1102
  }
1103
  if (0 == code && pOutT->type != TSDB_DATA_TYPE_DECIMAL) {
112,220,420✔
1104
    lt = *pOutT;
743,211✔
1105
    lt.type = TSDB_DATA_TYPE_DECIMAL;
743,211✔
1106
    code = convertToDecimal(&left, &lt, pOutputData, pOutT);
743,211✔
1107
  } else {
1108
    *(Decimal*)pOutputData = left;
111,477,209✔
1109
  }
1110
  return code;
112,220,420✔
1111
}
1112

1113
bool doCompareDecimal128(EOperatorType op, const Decimal128* pLeftDec, const Decimal128* pRightDec) {
834,387,812✔
1114
  switch (op) {
834,387,812✔
1115
    case OP_TYPE_GREATER_THAN:
539,228,384✔
1116
      return decimal128Gt(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
539,228,384✔
1117
    case OP_TYPE_GREATER_EQUAL:
×
1118
      return !decimal128Lt(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
×
1119
    case OP_TYPE_LOWER_THAN:
295,159,232✔
1120
      return decimal128Lt(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
295,159,232✔
1121
    case OP_TYPE_LOWER_EQUAL:
×
1122
      return !decimal128Gt(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
×
1123
    case OP_TYPE_EQUAL:
196✔
1124
      return decimal128Eq(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
196✔
1125
    case OP_TYPE_NOT_EQUAL:
×
1126
      return !decimal128Eq(pLeftDec, pRightDec, DECIMAL_WORD_NUM(Decimal));
×
1127
    default:
×
1128
      break;
×
1129
  }
1130
  return false;
×
1131
}
1132

1133
bool decimal64Compare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight) {
27,235,188✔
1134
  bool ret = false;
27,235,188✔
1135
  uint8_t leftPrec = 0, leftScale = 0, rightPrec = 0, rightScale = 0;
27,235,188✔
1136
  decimalFromTypeMod(pLeft->typeMod, &leftPrec, &leftScale);
27,235,188✔
1137
  decimalFromTypeMod(pRight->typeMod, &rightPrec, &rightScale);
27,235,188✔
1138
  int32_t deltaScale = leftScale - rightScale;
27,235,188✔
1139

1140
  Decimal64 leftDec = *(Decimal64*)pLeft->pData, rightDec = *(Decimal64*)pRight->pData;
27,235,188✔
1141

1142
  if (deltaScale != 0) {
27,235,188✔
1143
    bool needInt128 = (deltaScale < 0 && leftPrec - deltaScale > TSDB_DECIMAL64_MAX_PRECISION) ||
×
1144
                      (rightPrec + deltaScale > TSDB_DECIMAL64_MAX_PRECISION);
×
1145
    if (needInt128) {
×
1146
      Decimal128 dec128L = {0}, dec128R = {0};
×
1147
      makeDecimal128FromDecimal64(&dec128L, leftDec);
×
1148
      makeDecimal128FromDecimal64(&dec128R, rightDec);
×
1149
      return doCompareDecimal128(op, &dec128L, &dec128R);
×
1150
    } else {
1151
      if (deltaScale < 0) {
×
1152
        decimal64ScaleUp(&leftDec, -deltaScale);
×
1153
      } else {
1154
        decimal64ScaleUp(&rightDec, deltaScale);
×
1155
      }
1156
    }
1157
  }
1158

1159
  switch (op) {
27,235,188✔
1160
    case OP_TYPE_GREATER_THAN:
13,629,546✔
1161
      return decimal64Gt(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
13,629,546✔
1162
    case OP_TYPE_GREATER_EQUAL:
×
1163
      return !decimal64Lt(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
×
1164
    case OP_TYPE_LOWER_THAN:
13,605,642✔
1165
      return decimal64Lt(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
13,605,642✔
1166
    case OP_TYPE_LOWER_EQUAL:
×
1167
      return !decimal64Gt(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
×
1168
    case OP_TYPE_EQUAL:
×
1169
      return decimal64Eq(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
×
1170
    case OP_TYPE_NOT_EQUAL:
×
1171
      return !decimal64Eq(&leftDec, &rightDec, DECIMAL_WORD_NUM(Decimal64));
×
1172
    default:
×
1173
      break;
×
1174
  }
1175
  return ret;
×
1176
}
1177

1178
bool decimalCompare(EOperatorType op, const SDecimalCompareCtx* pLeft, const SDecimalCompareCtx* pRight) {
954,994,016✔
1179
  if (pLeft->type == TSDB_DATA_TYPE_DECIMAL64 && pRight->type == TSDB_DATA_TYPE_DECIMAL64) {
954,994,016✔
1180
    return decimal64Compare(op, pLeft, pRight);
×
1181
  }
1182
  bool    ret = false;
954,994,016✔
1183
  uint8_t leftPrec = 0, leftScale = 0, rightPrec = 0, rightScale = 0;
954,994,016✔
1184
  decimalFromTypeMod(pLeft->typeMod, &leftPrec, &leftScale);
954,994,016✔
1185
  decimalFromTypeMod(pRight->typeMod, &rightPrec, &rightScale);
954,994,016✔
1186

1187
  if (pLeft->type == TSDB_DATA_TYPE_DECIMAL64) {
954,994,016✔
1188
    Decimal128 dec128 = {0};
×
1189
    makeDecimal128FromDecimal64(&dec128, *(Decimal64*)pLeft->pData);
×
1190
    SDecimalCompareCtx leftCtx = {.pData = &dec128,
×
1191
                                  .type = TSDB_DATA_TYPE_DECIMAL,
1192
                                  .typeMod = decimalCalcTypeMod(TSDB_DECIMAL128_MAX_PRECISION, leftScale)};
×
1193
    return decimalCompare(op, &leftCtx, pRight);
×
1194
  } else if (pRight->type == TSDB_DATA_TYPE_DECIMAL64) {
954,994,016✔
1195
    Decimal128 dec128 = {0};
×
1196
    makeDecimal128FromDecimal64(&dec128, *(Decimal64*)pRight->pData);
×
1197
    SDecimalCompareCtx rightCtx = {.pData = &dec128,
×
1198
                                  .type = TSDB_DATA_TYPE_DECIMAL,
1199
                                  .typeMod = decimalCalcTypeMod(TSDB_DECIMAL128_MAX_PRECISION, rightScale)};
×
1200
    return decimalCompare(op, pLeft, &rightCtx);
×
1201
  }
1202
  int32_t deltaScale = leftScale - rightScale;
954,994,016✔
1203
  Decimal pLeftDec = *(Decimal*)pLeft->pData, pRightDec = *(Decimal*)pRight->pData;
954,994,016✔
1204

1205
  if (deltaScale != 0) {
954,994,016✔
1206
    bool needInt256 = (deltaScale < 0 && leftPrec - deltaScale > TSDB_DECIMAL_MAX_PRECISION) ||
417,898,106✔
1207
                      (rightPrec + deltaScale > TSDB_DECIMAL_MAX_PRECISION);
176,677,034✔
1208
    if (needInt256) {
241,221,072✔
1209
      Int256 x = {0}, y = {0};
120,606,204✔
1210
      makeInt256FromDecimal128(&x, &pLeftDec);
120,606,204✔
1211
      makeInt256FromDecimal128(&y, &pRightDec);
120,606,204✔
1212
      if (leftScale < rightScale) {
120,606,204✔
1213
        x = int256ScaleBy(&x, rightScale - leftScale);
64,544,038✔
1214
      } else {
1215
        y = int256ScaleBy(&y, leftScale - rightScale);
56,062,166✔
1216
      }
1217
      switch (op) {
120,606,204✔
1218
      case OP_TYPE_GREATER_THAN:
80,306,364✔
1219
        return int256Gt(&x, &y);
80,306,364✔
1220
      case OP_TYPE_GREATER_EQUAL:
×
1221
        return !int256Lt(&x, &y);
×
1222
      case OP_TYPE_LOWER_THAN:
40,299,840✔
1223
        return int256Lt(&x, &y);
40,299,840✔
1224
      case OP_TYPE_LOWER_EQUAL:
×
1225
        return !int256Gt(&x, &y);
×
1226
      case OP_TYPE_EQUAL:
×
1227
        return int256Eq(&x, &y);
×
1228
      case OP_TYPE_NOT_EQUAL:
×
1229
        return !int256Eq(&x, &y);
×
1230
      default:
×
1231
        break;
×
1232
      }
1233
      return false;
×
1234
    } else {
1235
      if (deltaScale < 0) {
120,614,868✔
1236
        decimal128ScaleUp(&pLeftDec, -deltaScale);
61,873,974✔
1237
      } else {
1238
        decimal128ScaleUp(&pRightDec, deltaScale);
58,740,894✔
1239
      }
1240
    }
1241
  }
1242
  return doCompareDecimal128(op, &pLeftDec, &pRightDec);
834,387,812✔
1243
}
1244

1245
#define ABS_INT64(v)  (v) == INT64_MIN ? (uint64_t)INT64_MAX + 1 : (uint64_t)llabs(v)
1246
#define ABS_UINT64(v) (v)
1247

1248
static int64_t int64FromDecimal64(const DecimalType* pDec, uint8_t prec, uint8_t scale) {
1,473,084✔
1249
  Decimal64 rounded = *(Decimal64*)pDec;
1,473,084✔
1250
  bool      overflow = false;
1,473,084✔
1251
  decimal64RoundWithPositiveScale(&rounded, prec, scale, prec, 0, ROUND_TYPE_HALF_ROUND_UP, &overflow);
1,473,084✔
1252
  if (overflow) return 0;
1,473,084✔
1253

1254
  return DECIMAL64_GET_VALUE(&rounded);
1,473,084✔
1255
}
1256

1257
static uint64_t uint64FromDecimal64(const DecimalType* pDec, uint8_t prec, uint8_t scale) {
1,473,084✔
1258
  Decimal64 rounded = *(Decimal64*)pDec;
1,473,084✔
1259
  bool      overflow = false;
1,473,084✔
1260
  decimal64RoundWithPositiveScale(&rounded, prec, scale, prec, 0, ROUND_TYPE_HALF_ROUND_UP, &overflow);
1,473,084✔
1261
  if (overflow) return 0;
1,473,084✔
1262

1263
  return DECIMAL64_GET_VALUE(&rounded);
1,473,084✔
1264
}
1265

1266
static int32_t decimal64FromInt64(DecimalType* pDec, uint8_t prec, uint8_t scale, int64_t val) {
1,607,455✔
1267
  Decimal64 max = {0};
1,607,455✔
1268
  DECIMAL64_GET_MAX(prec - scale, &max);
1,607,455✔
1269
  if (DECIMAL64_GET_VALUE(&max) < val || -DECIMAL64_GET_VALUE(&max) > val) {
1,607,455✔
1270
    return TSDB_CODE_DECIMAL_OVERFLOW;
26,673✔
1271
  }
1272
  DECIMAL64_SET_VALUE((Decimal64*)pDec, val);
1,580,782✔
1273
  decimal64ScaleUp(pDec, scale);
1,580,782✔
1274
  return 0;
1,580,782✔
1275
}
1276

1277
static int32_t decimal64FromUint64(DecimalType* pDec, uint8_t prec, uint8_t scale, uint64_t val) {
23,541✔
1278
  Decimal64 max = {0};
23,541✔
1279
  DECIMAL64_GET_MAX(prec - scale, &max);
23,541✔
1280
  if ((uint64_t)DECIMAL64_GET_VALUE(&max) < val) return TSDB_CODE_DECIMAL_OVERFLOW;
23,541✔
1281
  DECIMAL64_SET_VALUE((Decimal64*)pDec, val);
23,541✔
1282
  decimal64ScaleUp(pDec, scale);
23,541✔
1283
  return 0;
23,541✔
1284
}
1285

1286
static int32_t decimal64FromDouble(DecimalType* pDec, uint8_t prec, uint8_t scale, double val) {
11,215,646✔
1287
  double unscaled = val * getDoubleScaleMultiplier(scale);
11,215,646✔
1288
  if (isnan(unscaled)) {
11,215,646✔
1289
    goto __OVERFLOW__;
×
1290
  }
1291
  unscaled = round(unscaled);
11,215,646✔
1292

1293
  bool negative = unscaled < 0 ? true : false;
11,215,646✔
1294
  double abs = TABS(unscaled);
11,215,646✔
1295
  if (abs > ldexp(1.0, 63) - 1) {
11,215,646✔
1296
    goto __OVERFLOW__;
×
1297
  }
1298

1299
  uint64_t result = (uint64_t)abs;
11,215,646✔
1300
  makeDecimal64(pDec, result);
11,215,646✔
1301
  Decimal64 max = {0};
11,215,646✔
1302
  DECIMAL64_GET_MAX(prec, &max);
11,215,646✔
1303
  if (decimal64Gt(pDec, &max, DECIMAL_WORD_NUM(Decimal64))) goto __OVERFLOW__;
11,215,646✔
1304
  if (negative) decimal64Negate(pDec);
11,214,899✔
1305
  return 0;
11,214,899✔
1306

1307
__OVERFLOW__:
747✔
1308
  makeDecimal64(pDec, 0);
747✔
1309
  return TSDB_CODE_DECIMAL_OVERFLOW;
747✔
1310
}
1311

1312
static int32_t decimal64FromDecimal128(DecimalType* pDec, uint8_t prec, uint8_t scale, const DecimalType* pVal,
749,383✔
1313
                                       uint8_t valPrec, uint8_t valScale) {
1314
  Decimal128 dec128 = *(Decimal128*)pVal, tmpDec128 = {0};
749,383✔
1315
  bool       negative = DECIMAL128_SIGN(&dec128) == -1;
749,383✔
1316
  if (negative) decimal128Negate(&dec128);
749,383✔
1317
  tmpDec128 = dec128;
749,383✔
1318

1319
  Decimal64 max = {0};
749,383✔
1320
  DECIMAL64_GET_MAX(prec - scale, &max);
749,383✔
1321
  decimal128ScaleDown(&dec128, valScale, false);
749,383✔
1322
  if (decimal128Gt(&dec128, &max, DECIMAL_WORD_NUM(Decimal64))) {
749,383✔
1323
    return TSDB_CODE_DECIMAL_OVERFLOW;
3,735✔
1324
  }
1325
  decimal128ScaleTo(&tmpDec128, valScale, scale);
745,648✔
1326
  DECIMAL64_SET_VALUE((Decimal64*)pDec, DECIMAL128_LOW_WORD(&tmpDec128));
745,648✔
1327
  DECIMAL64_GET_MAX(prec, &max);
745,648✔
1328
  if (decimal64Lt(&max, pDec, DECIMAL_WORD_NUM(Decimal64))) {
745,648✔
1329
    return TSDB_CODE_DECIMAL_OVERFLOW;
196✔
1330
  }
1331
  if (negative) decimal64Negate(pDec);
745,452✔
1332
  return 0;
745,452✔
1333
}
1334

1335
static int32_t decimal64FromDecimal64(DecimalType* pDec, uint8_t prec, uint8_t scale, const DecimalType* pVal,
1,474,786✔
1336
                                      uint8_t valPrec, uint8_t valScale) {
1337
  Decimal64 dec64 = *(Decimal64*)pVal, max = {0};
1,474,786✔
1338
  bool      negative = DECIMAL64_SIGN(&dec64) == -1;
1,474,786✔
1339
  if (negative) decimal64Negate(&dec64);
1,474,786✔
1340
  *(Decimal64*)pDec = dec64;
1,474,786✔
1341

1342
  DECIMAL64_GET_MAX(prec - scale, &max);
1,474,786✔
1343
  decimal64ScaleDown(&dec64, valScale, false);
1,474,786✔
1344
  if (decimal64Lt(&max, &dec64, DECIMAL_WORD_NUM(Decimal64))) {
1,474,786✔
1345
    return TSDB_CODE_DECIMAL_OVERFLOW;
×
1346
  }
1347
  decimal64ScaleTo(pDec, valScale, scale);
1,474,786✔
1348
  DECIMAL64_GET_MAX(prec, &max);
1,474,786✔
1349
  if (decimal64Lt(&max, pDec, DECIMAL_WORD_NUM(Decimal64))) {
1,474,786✔
1350
    return TSDB_CODE_DECIMAL_OVERFLOW;
196✔
1351
  }
1352
  if (negative) decimal64Negate(pDec);
1,474,590✔
1353
  return 0;
1,474,590✔
1354
}
1355

1356
static int64_t int64FromDecimal128(const DecimalType* pDec, uint8_t prec, uint8_t scale) {
4,430,690✔
1357
  Decimal128 rounded = *(Decimal128*)pDec;
4,430,690✔
1358
  bool       overflow = false;
4,430,690✔
1359
  decimal128RoundWithPositiveScale(&rounded, prec, scale, prec, 0, ROUND_TYPE_HALF_ROUND_UP, &overflow);
4,430,690✔
1360
  if (overflow) {
4,430,690✔
1361
    return 0;
×
1362
  }
1363
  Decimal128 max = {0}, min = {0};
4,430,690✔
1364
  (void)decimal128FromInt64(&max, TSDB_DECIMAL128_MAX_PRECISION, 0, INT64_MAX);
4,430,690✔
1365
  (void)decimal128FromInt64(&min, TSDB_DECIMAL128_MAX_PRECISION, 0, INT64_MIN);
4,430,690✔
1366
  if (decimal128Gt(&rounded, &max, DECIMAL_WORD_NUM(Decimal128)) || decimal128Lt(&rounded, &min, DECIMAL_WORD_NUM(Decimal128))) {
4,430,690✔
1367
    overflow = true;
709,650✔
1368
    return (int64_t)DECIMAL128_LOW_WORD(&rounded);
709,650✔
1369
  }
1370

1371
  return (int64_t)DECIMAL128_LOW_WORD(&rounded);
3,721,040✔
1372
}
1373

1374
static uint64_t uint64FromDecimal128(const DecimalType* pDec, uint8_t prec, uint8_t scale) {
2,951,630✔
1375
  Decimal128 rounded = *(Decimal128*)pDec;
2,951,630✔
1376
  bool       overflow = false;
2,951,630✔
1377
  decimal128RoundWithPositiveScale(&rounded, prec, scale, prec, 0, ROUND_TYPE_HALF_ROUND_UP, &overflow);
2,951,630✔
1378
  if (overflow) return 0;
2,951,630✔
1379

1380
  Decimal128 max = {0};
2,951,630✔
1381
  (void)decimal128FromUint64(&max, TSDB_DECIMAL128_MAX_PRECISION, 0, UINT64_MAX);
2,951,630✔
1382
  if (decimal128Gt(&rounded, &max, DECIMAL_WORD_NUM(Decimal128)) ||
5,416,216✔
1383
      decimal128Lt(&rounded, &decimal128Zero, DECIMAL_WORD_NUM(Decimal128))) {
2,464,586✔
1384
    overflow = true;
1,295,690✔
1385
    return DECIMAL128_LOW_WORD(&rounded);
1,295,690✔
1386
  }
1387
  return DECIMAL128_LOW_WORD(&rounded);
1,655,940✔
1388
}
1389

1390
static int32_t decimal128FromInt64(DecimalType* pDec, uint8_t prec, uint8_t scale, int64_t val) {
267,752,292✔
1391
  if (prec - scale <= 18) {
267,752,292✔
1392
    Decimal64 max = {0};
2,320,657✔
1393
    DECIMAL64_GET_MAX(prec - scale, &max);
2,320,657✔
1394
    if (DECIMAL64_GET_VALUE(&max) < val || -DECIMAL64_GET_VALUE(&max) > val) return TSDB_CODE_DECIMAL_OVERFLOW;
2,320,657✔
1395
  }
1396
  uint64_t valAbs = ABS_INT64(val);
267,752,292✔
1397
  makeDecimal128(pDec, 0, valAbs);
267,752,292✔
1398
  if (val < 0) decimal128Negate(pDec);
267,752,292✔
1399
  decimal128ScaleUp(pDec, scale);
267,752,292✔
1400
  return 0;
267,752,292✔
1401
}
1402

1403
static int32_t decimal128FromUint64(DecimalType* pDec, uint8_t prec, uint8_t scale, uint64_t val) {
4,048,108✔
1404
  if (prec - scale <= 19) {
4,048,108✔
1405
    Decimal128 max = {0}, decVal = {0};
18,851✔
1406
    DECIMAL128_GET_MAX(prec - scale, &max);
18,851✔
1407
    makeDecimal128(&decVal, 0, val);
18,851✔
1408
    if (decimal128Gt(&decVal, &max, DECIMAL_WORD_NUM(Decimal128))) {
18,851✔
1409
      return TSDB_CODE_DECIMAL_OVERFLOW;
×
1410
    }
1411
  }
1412
  makeDecimal128(pDec, 0, val);
4,048,108✔
1413
  decimal128ScaleUp(pDec, scale);
4,048,108✔
1414
  return 0;
4,048,108✔
1415
}
1416

1417
static int32_t decimal128FromDouble(DecimalType* pDec, uint8_t prec, uint8_t scale, double val) {
2,989,886✔
1418
  double unscaled = val * getDoubleScaleMultiplier(scale);
2,989,886✔
1419
  if (isnan(unscaled)) {
2,989,886✔
1420
    goto __OVERFLOW__;
×
1421
  }
1422
  unscaled = round(unscaled);
2,989,886✔
1423

1424
  bool   negative = unscaled < 0 ? true : false;
2,989,886✔
1425
  double abs = TABS(unscaled);
2,989,886✔
1426
  if (abs > ldexp(1.0, 127) - 1) {
2,989,886✔
1427
    goto __OVERFLOW__;
×
1428
  }
1429

1430
  uint64_t hi = (uint64_t)ldexp(abs, -64), lo = (uint64_t)(abs - ldexp((double)hi, 64));
2,989,886✔
1431
  makeDecimal128(pDec, hi, lo);
2,989,886✔
1432
  Decimal128 max = {0};
2,989,886✔
1433
  DECIMAL128_GET_MAX(prec, &max);
2,989,886✔
1434
  if (decimal128Gt(pDec, &max, DECIMAL_WORD_NUM(Decimal128))) goto __OVERFLOW__;
2,989,886✔
1435
  if (negative) decimal128Negate(pDec);
2,989,886✔
1436
  return 0;
2,989,886✔
1437

1438
__OVERFLOW__:
×
1439
  *(Decimal128*)pDec = decimal128Zero;
×
1440
  return TSDB_CODE_DECIMAL_OVERFLOW;
×
1441
}
1442

1443
static int32_t decimal128FromDecimal64(DecimalType* pDec, uint8_t prec, uint8_t scale, const DecimalType* pVal,
233,967,129✔
1444
                                       uint8_t valPrec, uint8_t valScale) {
1445
  Decimal64 dec64 = *(Decimal64*)pVal;
233,967,129✔
1446
  bool      negative = DECIMAL64_SIGN(&dec64) == -1;
233,967,129✔
1447
  if (negative) decimal64Negate(&dec64);
233,967,129✔
1448

1449
  makeDecimal128(pDec, 0, DECIMAL64_GET_VALUE(&dec64));
233,967,129✔
1450
  Decimal128 max = {0};
233,967,129✔
1451
  DECIMAL128_GET_MAX(prec - scale, &max);
233,967,129✔
1452
  decimal64ScaleDown(&dec64, valScale, false);
233,967,129✔
1453
  if (decimal128Lt(&max, &dec64, DECIMAL_WORD_NUM(Decimal64))) {
233,967,129✔
1454
    return TSDB_CODE_DECIMAL_OVERFLOW;
943✔
1455
  }
1456
  decimal128ScaleTo(pDec, valScale, scale);
233,966,186✔
1457
  DECIMAL128_GET_MAX(prec, &max);
233,966,186✔
1458
  if (decimal128Lt(&max, pDec, DECIMAL_WORD_NUM(Decimal128))) {
233,966,186✔
1459
    return TSDB_CODE_DECIMAL_OVERFLOW;
×
1460
  }
1461
  if (negative) decimal128Negate(pDec);
233,966,186✔
1462
  return 0;
233,966,186✔
1463
}
1464

1465
static int32_t decimal128FromDecimal128(DecimalType* pDec, uint8_t prec, uint8_t scale, const DecimalType* pVal,
3,380✔
1466
                                        uint8_t valPrec, uint8_t valScale) {
1467
  bool       negative = DECIMAL128_SIGN((Decimal128*)pVal) == -1;
3,380✔
1468
  Decimal128 tmpDec = *(Decimal128*)pVal;
3,380✔
1469
  if (negative) decimal128Negate(&tmpDec);
3,380✔
1470
  *(Decimal128*)pDec = tmpDec;
3,380✔
1471

1472
  Decimal128 max = {0};
3,380✔
1473
  DECIMAL128_GET_MAX(prec - scale, &max);
3,380✔
1474
  decimal128ScaleDown(&tmpDec, valScale, false);
3,380✔
1475
  if (decimal128Lt(&max, &tmpDec, DECIMAL_WORD_NUM(Decimal128))) {
3,380✔
1476
    return TSDB_CODE_DECIMAL_OVERFLOW;
747✔
1477
  }
1478
  decimal128ScaleTo(pDec, valScale, scale);
2,633✔
1479
  DECIMAL128_GET_MAX(prec, &max);
2,633✔
1480
  if (decimal128Lt(&max, pDec, DECIMAL_WORD_NUM(Decimal128))) {
2,633✔
1481
    return TSDB_CODE_DECIMAL_OVERFLOW;
196✔
1482
  }
1483
  if (negative) decimal128Negate(pDec);
2,437✔
1484
  return 0;
2,437✔
1485
}
1486

1487
#define CONVERT_TO_DECIMAL(pData, pInputType, pOut, pOutType, decimal)                                               \
1488
  do {                                                                                                               \
1489
    int64_t  val = 0;                                                                                                \
1490
    uint64_t uval = 0;                                                                                               \
1491
    double   dval = 0;                                                                                               \
1492
    switch (pInputType->type) {                                                                                      \
1493
      case TSDB_DATA_TYPE_NULL:                                                                                      \
1494
        break;                                                                                                       \
1495
      case TSDB_DATA_TYPE_BOOL:                                                                                      \
1496
        uval = *(const bool*)pData;                                                                                  \
1497
        code = decimal##FromUint64(pOut, pOutType->precision, pOutType->scale, uval);                                \
1498
        break;                                                                                                       \
1499
      case TSDB_DATA_TYPE_TINYINT:                                                                                   \
1500
        val = *(const int8_t*)pData;                                                                                 \
1501
        code = decimal##FromInt64(pOut, pOutType->precision, pOutType->scale, val);                                  \
1502
        break;                                                                                                       \
1503
      case TSDB_DATA_TYPE_SMALLINT:                                                                                  \
1504
        val = *(const int16_t*)pData;                                                                                \
1505
        code = decimal##FromInt64(pOut, pOutType->precision, pOutType->scale, val);                                  \
1506
        break;                                                                                                       \
1507
      case TSDB_DATA_TYPE_INT:                                                                                       \
1508
        val = *(const int32_t*)pData;                                                                                \
1509
        code = decimal##FromInt64(pOut, pOutType->precision, pOutType->scale, val);                                  \
1510
        break;                                                                                                       \
1511
      case TSDB_DATA_TYPE_TIMESTAMP:                                                                                 \
1512
      case TSDB_DATA_TYPE_BIGINT:                                                                                    \
1513
        val = *(const int64_t*)pData;                                                                                \
1514
        code = decimal##FromInt64(pOut, pOutType->precision, pOutType->scale, val);                                  \
1515
        break;                                                                                                       \
1516
      case TSDB_DATA_TYPE_UTINYINT:                                                                                  \
1517
        uval = *(const uint8_t*)pData;                                                                               \
1518
        code = decimal##FromUint64(pOut, pOutType->precision, pOutType->scale, uval);                                \
1519
        break;                                                                                                       \
1520
      case TSDB_DATA_TYPE_USMALLINT:                                                                                 \
1521
        uval = *(const uint16_t*)pData;                                                                              \
1522
        code = decimal##FromUint64(pOut, pOutType->precision, pOutType->scale, uval);                                \
1523
        break;                                                                                                       \
1524
      case TSDB_DATA_TYPE_UINT:                                                                                      \
1525
        uval = *(const uint32_t*)pData;                                                                              \
1526
        code = decimal##FromUint64(pOut, pOutType->precision, pOutType->scale, uval);                                \
1527
        break;                                                                                                       \
1528
      case TSDB_DATA_TYPE_UBIGINT:                                                                                   \
1529
        uval = *(const uint64_t*)pData;                                                                              \
1530
        code = decimal##FromUint64(pOut, pOutType->precision, pOutType->scale, uval);                                \
1531
        break;                                                                                                       \
1532
      case TSDB_DATA_TYPE_FLOAT: {                                                                                   \
1533
        dval = *(const float*)pData;                                                                                 \
1534
        code = decimal##FromDouble(pOut, pOutType->precision, pOutType->scale, dval);                                \
1535
      } break;                                                                                                       \
1536
      case TSDB_DATA_TYPE_DOUBLE: {                                                                                  \
1537
        dval = *(const double*)pData;                                                                                \
1538
        code = decimal##FromDouble(pOut, pOutType->precision, pOutType->scale, dval);                                \
1539
      } break;                                                                                                       \
1540
      case TSDB_DATA_TYPE_DECIMAL64: {                                                                               \
1541
        code = decimal##FromDecimal64(pOut, pOutType->precision, pOutType->scale, pData, pInputType->precision,      \
1542
                                      pInputType->scale);                                                            \
1543
      } break;                                                                                                       \
1544
      case TSDB_DATA_TYPE_DECIMAL: {                                                                                 \
1545
        code = decimal##FromDecimal128(pOut, pOutType->precision, pOutType->scale, pData, pInputType->precision,     \
1546
                                       pInputType->scale);                                                           \
1547
      } break;                                                                                                       \
1548
      case TSDB_DATA_TYPE_VARCHAR:                                                                                   \
1549
      case TSDB_DATA_TYPE_VARBINARY:                                                                                 \
1550
      case TSDB_DATA_TYPE_NCHAR: {                                                                                   \
1551
        code = decimal##FromStr(pData, pInputType->bytes, pOutType->precision, pOutType->scale,                      \
1552
                                pOut);                                                                               \
1553
      } break;                                                                                                       \
1554
      default:                                                                                                       \
1555
        code = TSDB_CODE_OPS_NOT_SUPPORT;                                                                            \
1556
        break;                                                                                                       \
1557
    }                                                                                                                \
1558
  } while (0)
1559

1560
int32_t convertToDecimal(const void* pData, const SDataType* pInputType, void* pOut, const SDataType* pOutType) {
518,746,454✔
1561
  int32_t code = 0;
518,746,454✔
1562

1563
  switch (pOutType->type) {
518,746,454✔
1564
    case TSDB_DATA_TYPE_DECIMAL64: {
20,300,770✔
1565
      CONVERT_TO_DECIMAL(pData, pInputType, pOut, pOutType, decimal64);
20,300,770✔
1566
    } break;
20,300,770✔
1567
    case TSDB_DATA_TYPE_DECIMAL: {
498,445,684✔
1568
      CONVERT_TO_DECIMAL(pData, pInputType, pOut, pOutType, decimal128);
498,445,684✔
1569
    } break;
498,446,437✔
1570
    default:
×
1571
      code = TSDB_CODE_INTERNAL_ERROR;
×
1572
      break;
×
1573
  }
1574
  return code;
518,747,207✔
1575
}
1576

1577
void decimal64ScaleDown(Decimal64* pDec, uint8_t scaleDown, bool round) {
245,967,615✔
1578
  if (scaleDown > 0) {
245,967,615✔
1579
    Decimal64 divisor = SCALE_MULTIPLIER_64[scaleDown], remainder = {0};
245,827,419✔
1580
    decimal64divide(pDec, &divisor, DECIMAL_WORD_NUM(Decimal64), &remainder);
245,827,419✔
1581
    if (round) {
245,827,419✔
1582
      decimal64Abs(&remainder);
1,702✔
1583
      Decimal64 half = SCALE_MULTIPLIER_64[scaleDown];
1,702✔
1584
      decimal64divide(&half, &decimal64Two, DECIMAL_WORD_NUM(Decimal64), NULL);
1,702✔
1585
      if (!decimal64Lt(&remainder, &half, DECIMAL_WORD_NUM(Decimal64))) {
1,702✔
1586
        Decimal64 delta = {DECIMAL64_SIGN(pDec)};
196✔
1587
        decimal64Add(pDec, &delta, DECIMAL_WORD_NUM(Decimal64));
196✔
1588
      }
1589
    }
1590
  }
1591
}
245,967,615✔
1592

1593
void decimal64ScaleUp(Decimal64* pDec, uint8_t scaleUp) {
310,556,583✔
1594
  if (scaleUp > 0) {
310,556,583✔
1595
    Decimal64 multiplier = SCALE_MULTIPLIER_64[scaleUp];
18,389,499✔
1596
    decimal64Multiply(pDec, &multiplier, DECIMAL_WORD_NUM(Decimal64));
18,389,637✔
1597
  }
1598
}
310,556,758✔
1599

1600
static void decimal64ScaleTo(Decimal64* pDec, uint8_t oldScale, uint8_t newScale) {
1,474,786✔
1601
  if (newScale > oldScale)
1,474,786✔
1602
    decimal64ScaleUp(pDec, newScale - oldScale);
736,542✔
1603
  else if (newScale < oldScale)
738,244✔
1604
    decimal64ScaleDown(pDec, oldScale - newScale, true);
1,702✔
1605
}
1,474,786✔
1606

1607
static void decimal64ScaleAndCheckOverflow(Decimal64* pDec, int8_t scale, uint8_t toPrec, uint8_t toScale,
318,728,032✔
1608
                                           bool* overflow) {
1609
  int8_t deltaScale = toScale - scale;
318,728,032✔
1610
  if (deltaScale >= 0) {
318,728,032✔
1611
    Decimal64 max = {0};
308,238,951✔
1612
    DECIMAL64_GET_MAX(toPrec - deltaScale, &max);
308,265,800✔
1613
    Decimal64 abs = *pDec;
308,262,984✔
1614
    decimal64Abs(&abs);
308,268,409✔
1615
    if (decimal64Gt(&abs, &max, DECIMAL_WORD_NUM(Decimal64))) {
308,265,031✔
1616
      if (overflow) *overflow = true;
980✔
1617
    } else {
1618
      decimal64ScaleUp(pDec, deltaScale);
308,241,229✔
1619
    }
1620
  } else if (deltaScale < 0) {
10,489,081✔
1621
    Decimal64 res = *pDec, max = {0};
10,523,998✔
1622
    decimal64ScaleDown(&res, -deltaScale, false);
10,523,998✔
1623
    DECIMAL64_GET_MAX(toPrec, &max);
10,523,998✔
1624
    Decimal64 abs = res;
10,523,998✔
1625
    decimal64Abs(&abs);
10,523,998✔
1626
    if (decimal64Gt(&abs, &max, DECIMAL_WORD_NUM(Decimal64))) {
10,523,998✔
1627
      if (overflow) *overflow = true;
×
1628
    } else {
1629
      *pDec = res;
10,523,998✔
1630
    }
1631
  }
1632
}
318,722,198✔
1633

1634
static int32_t decimal64CountRoundingDelta(const Decimal64* pDec, int8_t scale, int8_t toScale,
318,700,432✔
1635
                                           DecimalRoundType roundType) {
1636
  if (roundType == ROUND_TYPE_TRUNC || toScale >= scale) return 0;
318,700,432✔
1637

1638
  Decimal64 dec = *pDec;
10,523,998✔
1639
  int32_t   res = 0;
10,523,998✔
1640
  switch (roundType) {
10,523,998✔
1641
    case ROUND_TYPE_HALF_ROUND_UP: {
10,523,998✔
1642
      Decimal64 trailing = dec;
10,523,998✔
1643
      decimal64Mod(&trailing, &SCALE_MULTIPLIER_64[scale - toScale], DECIMAL_WORD_NUM(Decimal64));
10,523,998✔
1644
      if (decimal64Eq(&trailing, &decimal64Zero, DECIMAL_WORD_NUM(Decimal64))) {
10,523,998✔
1645
        res = 0;
301,984✔
1646
        break;
306,499✔
1647
      }
1648
      Decimal64 trailingAbs = trailing, baseDiv2 = SCALE_MULTIPLIER_64[scale - toScale];
10,222,014✔
1649
      decimal64Abs(&trailingAbs);
10,222,014✔
1650
      decimal64divide(&baseDiv2, &decimal64Two, DECIMAL_WORD_NUM(Decimal64), NULL);
10,222,014✔
1651
      if (decimal64Lt(&trailingAbs, &baseDiv2, DECIMAL_WORD_NUM(Decimal64))) {
10,222,014✔
1652
        res = 0;
5,127,226✔
1653
        break;
5,127,226✔
1654
      }
1655
      res = DECIMAL64_SIGN(pDec) == 1 ? 1 : -1;
5,094,788✔
1656
    } break;
5,094,788✔
1657
    default:
×
1658
      break;
×
1659
  }
1660
  return res;
10,523,998✔
1661
}
1662

1663
static void decimal64RoundWithPositiveScale(Decimal64* pDec, uint8_t prec, int8_t scale, uint8_t toPrec,
318,738,388✔
1664
                                            uint8_t toScale, DecimalRoundType roundType, bool* overflow) {
1665
  Decimal64 scaled = *pDec;
318,738,388✔
1666
  bool      overflowLocal = false;
318,771,131✔
1667
  // scale up or down to toScale
1668
  decimal64ScaleAndCheckOverflow(&scaled, scale, toPrec, toScale, &overflowLocal);
318,765,537✔
1669
  if (overflowLocal) {
318,745,679✔
1670
    if (overflow) *overflow = true;
980✔
1671
    *pDec = decimal64Zero;
980✔
1672
    return;
78,595,753✔
1673
  }
1674

1675
  // calc rounding delta
1676
  int32_t delta = decimal64CountRoundingDelta(pDec, scale, toScale, roundType);
318,744,699✔
1677
  if (delta == 0) {
318,719,225✔
1678
    *pDec = scaled;
313,626,205✔
1679
    return;
313,611,421✔
1680
  }
1681

1682
  Decimal64 deltaDec = {delta};
5,093,020✔
1683
  // add the delta
1684
  decimal64Add(&scaled, &deltaDec, DECIMAL_WORD_NUM(Decimal64));
5,094,788✔
1685

1686
  // check overflow again
1687
  if (toPrec < prec) {
5,094,788✔
1688
    Decimal64 max = {0};
2,751,213✔
1689
    DECIMAL64_GET_MAX(toPrec, &max);
2,751,213✔
1690
    Decimal64 scaledAbs = scaled;
2,751,213✔
1691
    decimal64Abs(&scaledAbs);
2,751,213✔
1692
    if (decimal64Gt(&scaledAbs, &max, DECIMAL_WORD_NUM(Decimal64))) {
2,751,213✔
1693
      if (overflow) *overflow = true;
1,334✔
1694
      *pDec = decimal64Zero;
1,334✔
1695
      return;
1,334✔
1696
    }
1697
  }
1698
  *pDec = scaled;
5,093,454✔
1699
}
1700

1701
int32_t decimal64FromStr(const char* str, int32_t len, uint8_t expectPrecision, uint8_t expectScale, Decimal64* pRes) {
315,701,718✔
1702
  int32_t    code = 0;
315,701,718✔
1703
  DecimalVar var = {.type = DECIMAL_64, .pDec = pRes->words, .precision = expectPrecision, .scale = expectScale};
315,701,718✔
1704
  DECIMAL64_SET_VALUE(pRes, 0);
315,772,580✔
1705
  code = decimalVarFromStr(str, len, &var);
315,836,111✔
1706
  if (TSDB_CODE_SUCCESS != code) return code;
315,837,754✔
1707
  if (var.weight > (int32_t)expectPrecision - expectScale) {
315,837,558✔
1708
    return TSDB_CODE_DECIMAL_OVERFLOW;
2,062✔
1709
  }
1710
  bool overflow = false;
315,835,496✔
1711
  decimal64RoundWithPositiveScale(pRes, var.precision, var.scale, expectPrecision, expectScale,
315,823,909✔
1712
                                  ROUND_TYPE_HALF_ROUND_UP, &overflow);
1713
  if (overflow) {
315,779,764✔
1714
    return TSDB_CODE_DECIMAL_OVERFLOW;
2,314✔
1715
  }
1716
  return code;
315,777,450✔
1717
}
1718

1719
static void decimal128ScaleDown(Decimal128* pDec, uint8_t scaleDown, bool round) {
49,228,368✔
1720
  if (scaleDown > 0) {
49,228,368✔
1721
    Decimal128 divisor = SCALE_MULTIPLIER_128[scaleDown], remainder = {0};
49,227,388✔
1722
    decimal128Divide(pDec, &divisor, 2, &remainder);
49,227,388✔
1723
    if (round) {
49,227,388✔
1724
      decimal128Abs(&remainder);
28,841,408✔
1725
      Decimal128 half = SCALE_MULTIPLIER_128[scaleDown];
28,841,408✔
1726
      decimal128Divide(&half, &decimal128Two, 2, NULL);
28,841,408✔
1727
      if (!decimal128Lt(&remainder, &half, DECIMAL_WORD_NUM(Decimal128))) {
28,841,408✔
1728
        Decimal64 delta = {DECIMAL128_SIGN(pDec)};
2,992,453✔
1729
        decimal128Add(pDec, &delta, DECIMAL_WORD_NUM(Decimal64));
2,992,453✔
1730
      }
1731
    }
1732
  }
1733
}
49,228,368✔
1734

1735
static void decimal128ScaleUp(Decimal128* pDec, uint8_t scaleUp) {
622,460,392✔
1736
  if (scaleUp > 0) {
622,460,392✔
1737
    Decimal128 multiplier = SCALE_MULTIPLIER_128[scaleUp];
533,062,825✔
1738
    decimal128Multiply(pDec, &multiplier, DECIMAL_WORD_NUM(Decimal128));
533,063,350✔
1739
  }
1740
}
622,460,882✔
1741

1742
static void decimal128ScaleTo(Decimal128* pDec, uint8_t oldScale, uint8_t newScale) {
350,248,887✔
1743
  if (newScale > oldScale)
350,248,887✔
1744
    decimal128ScaleUp(pDec, newScale - oldScale);
53,319,552✔
1745
  else if (newScale < oldScale)
296,929,335✔
1746
    decimal128ScaleDown(pDec, oldScale - newScale, true);
382,856✔
1747
}
350,248,887✔
1748

1749
int32_t decimal128FromStr(const char* str, int32_t len, uint8_t expectPrecision, uint8_t expectScale,
138,362,185✔
1750
                          Decimal128* pRes) {
1751
  int32_t    code = 0;
138,362,185✔
1752
  DecimalVar var = {.type = DECIMAL_128, .pDec = pRes->words, .precision = expectPrecision, .scale = expectScale};
138,362,185✔
1753
  DECIMAL128_SET_HIGH_WORD(pRes, 0);
138,420,673✔
1754
  DECIMAL128_SET_LOW_WORD(pRes, 0);
138,398,125✔
1755
  code = decimalVarFromStr(str, len, &var);
138,433,905✔
1756
  if (TSDB_CODE_SUCCESS != code) return code;
138,428,362✔
1757
  if (var.weight > (int32_t)expectPrecision - expectScale) {
138,428,362✔
1758
    return TSDB_CODE_DECIMAL_OVERFLOW;
536✔
1759
  }
1760
  bool overflow = false;
138,427,826✔
1761
  decimal128RoundWithPositiveScale(pRes, var.precision, var.scale, expectPrecision, expectScale,
138,441,581✔
1762
                                   ROUND_TYPE_HALF_ROUND_UP, &overflow);
1763
  if (overflow) {
138,414,482✔
1764
    return TSDB_CODE_DECIMAL_OVERFLOW;
588✔
1765
  }
1766
  return code;
138,413,894✔
1767
}
1768

1769
#if 0
1770
__int128 decimal128ToInt128(const Decimal128* pDec) {
1771
  __int128 ret = 0;
1772
  ret = DECIMAL128_HIGH_WORD(pDec);
1773
  ret <<= 64;
1774
  ret |= DECIMAL128_LOW_WORD(pDec);
1775
  return ret;
1776
}
1777
#endif
1778

1779
static int32_t decimal128CountLeadingBinaryZeros(const Decimal128* pDec) {
7,162,800✔
1780
  if (DECIMAL128_HIGH_WORD(pDec) == 0) {
7,162,800✔
1781
    return 64 + countLeadingZeros(DECIMAL128_LOW_WORD(pDec));
5,429,294✔
1782
  } else {
1783
    return countLeadingZeros((uint64_t)DECIMAL128_HIGH_WORD(pDec));
1,733,506✔
1784
  }
1785
}
1786

1787
#define IMPL_INTEGER_TYPE_FROM_DECIMAL_TYPE(oType, decimalType, sign)                    \
1788
  oType oType##From##decimalType(const DecimalType* pDec, uint8_t prec, uint8_t scale) { \
1789
    return (oType)sign##int64##From##decimalType(pDec, prec, scale);                     \
1790
  }
1791
#define IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(oType, decimalType) \
1792
  IMPL_INTEGER_TYPE_FROM_DECIMAL_TYPE(oType, decimalType, )
1793
#define IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(oType, decimalType) \
1794
  IMPL_INTEGER_TYPE_FROM_DECIMAL_TYPE(oType, decimalType, u)
1795

UNCOV
1796
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int8_t, Decimal64)
×
1797
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int16_t, Decimal64)
736,542✔
1798
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int32_t, Decimal64)
×
1799
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int64_t, Decimal64)
736,542✔
1800

UNCOV
1801
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint8_t, Decimal64)
×
1802
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint16_t, Decimal64)
736,542✔
1803
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint32_t, Decimal64)
736,542✔
1804
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint64_t, Decimal64)
×
1805

1806
double doubleFromDecimal64(const void* pDec, uint8_t prec, uint8_t scale) {
361,206,672✔
1807
  int32_t   sign = DECIMAL64_SIGN((Decimal64*)pDec);
361,206,672✔
1808
  Decimal64 abs = *(Decimal64*)pDec;
361,210,524✔
1809
  decimal64Abs(&abs);
361,210,454✔
1810
  return (double)DECIMAL64_GET_VALUE(&abs) * sign / getDoubleScaleMultiplier(scale);
361,219,124✔
1811
}
1812

UNCOV
1813
bool boolFromDecimal64(const void* pDec, uint8_t prec, uint8_t scale) {
×
UNCOV
1814
  return !decimal64Eq(pDec, &decimal64Zero, DECIMAL_WORD_NUM(Decimal64));
×
1815
}
1816

1817
#define IMPL_REAL_TYPE_FROM_DECIMAL_TYPE(oType, decimalType)                             \
1818
  oType oType##From##decimalType(const DecimalType* pDec, uint8_t prec, uint8_t scale) { \
1819
    return (oType) double##From##decimalType(pDec, prec, scale);                         \
1820
  }
1821

1822
IMPL_REAL_TYPE_FROM_DECIMAL_TYPE(float, Decimal64);
736,542✔
1823

1824
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int8_t, Decimal128)
739,530✔
UNCOV
1825
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int16_t, Decimal128)
×
1826
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int32_t, Decimal128)
1,475,325✔
1827
IMP_SIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(int64_t, Decimal128)
2,215,835✔
1828

1829
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint8_t, Decimal128)
735,795✔
1830
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint16_t, Decimal128)
735,795✔
1831
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint32_t, Decimal128)
739,530✔
1832
IMP_UNSIGNED_INTEGER_TYPE_FROM_DECIMAL_TYPE(uint64_t, Decimal128)
740,510✔
1833

1834
bool boolFromDecimal128(const void* pDec, uint8_t prec, uint8_t scale) {
1,479,452✔
1835
  return !decimal128Eq(pDec, &decimal128Zero, DECIMAL_WORD_NUM(Decimal128));
1,479,452✔
1836
}
1837

1838
double doubleFromDecimal128(const void* pDec, uint8_t prec, uint8_t scale) {
1,050,673,081✔
1839
  int32_t    sign = DECIMAL128_SIGN((Decimal128*)pDec);
1,050,673,081✔
1840
  Decimal128 abs = *(Decimal128*)pDec;
1,050,735,615✔
1841
  decimal128Abs(&abs);
1,050,722,061✔
1842
  double unscaled = DECIMAL128_LOW_WORD(&abs);
1,050,447,855✔
1843
  unscaled += ldexp((double)DECIMAL128_HIGH_WORD(&abs), 64);
1,050,447,855✔
1844
  return (unscaled * sign) / getDoubleScaleMultiplier(scale);
1,050,447,855✔
1845
}
1846

1847
IMPL_REAL_TYPE_FROM_DECIMAL_TYPE(float, Decimal128);
739,530✔
1848

1849
static void decimal128RoundWithPositiveScale(Decimal128* pDec, uint8_t prec, uint8_t scale, uint8_t toPrec,
145,803,840✔
1850
                                             uint8_t toScale, DecimalRoundType roundType, bool* overflow) {
1851
  Decimal128 scaled = *pDec;
145,803,840✔
1852
  bool       overflowLocal = false;
145,822,324✔
1853
  // scale up or down to toScale
1854
  decimal128ModifyScaleAndPrecision(&scaled, scale, toPrec, toScale, &overflowLocal);
145,818,789✔
1855
  if (overflowLocal) {
145,813,942✔
1856
    if (overflow) *overflow = true;
392✔
1857
    *pDec = decimal128Zero;
392✔
1858
    return;
74,581✔
1859
  }
1860

1861
  // calc rounding delta, 1 or -1
1862
  int32_t delta = decimal128CountRoundingDelta(pDec, scale, toScale, roundType);
145,813,550✔
1863
  if (delta == 0) {
145,799,626✔
1864
    *pDec = scaled;
136,382,477✔
1865
    return;
136,384,548✔
1866
  }
1867

1868
  Decimal64 deltaDec = {delta};
9,417,149✔
1869
  // add the delta
1870
  decimal128Add(&scaled, &deltaDec, DECIMAL_WORD_NUM(Decimal64));
9,420,068✔
1871

1872
  // check overflow again
1873
  if (toPrec < prec) {
9,420,068✔
1874
    Decimal128 max = {0};
3,542,277✔
1875
    DECIMAL128_GET_MAX(toPrec, &max);
3,542,277✔
1876
    Decimal128 scaledAbs = scaled;
3,542,277✔
1877
    decimal128Abs(&scaledAbs);
3,542,277✔
1878
    if (decimal128Gt(&scaledAbs, &max, DECIMAL_WORD_NUM(Decimal128))) {
3,542,277✔
1879
      if (overflow) *overflow = true;
196✔
1880
      *(Decimal128*)pDec = decimal128Zero;
196✔
1881
      return;
196✔
1882
    }
1883
  }
1884
  *(Decimal128*)pDec = scaled;
9,419,872✔
1885
}
1886

1887
static void decimal128ModifyScaleAndPrecision(Decimal128* pDec, uint8_t scale, uint8_t toPrec, int8_t toScale,
145,802,022✔
1888
                                              bool* overflow) {
1889
  int8_t deltaScale = toScale - scale;
145,802,022✔
1890
  if (deltaScale >= 0) {
145,802,022✔
1891
    Decimal128 max = {0};
126,186,311✔
1892
    DECIMAL128_GET_MAX(toPrec - deltaScale, &max);
126,198,458✔
1893
    Decimal128 abs = *pDec;
126,196,020✔
1894
    decimal128Abs(&abs);
126,165,012✔
1895
    if (decimal128Gt(&abs, &max, DECIMAL_WORD_NUM(Decimal128))) {
126,189,419✔
1896
      if (overflow) *overflow = true;
392✔
1897
    } else {
1898
      decimal128ScaleUp(pDec, deltaScale);
126,188,765✔
1899
    }
1900
  } else {
1901
    Decimal128 res = *pDec, max = {0};
19,615,711✔
1902
    decimal128ScaleDown(&res, -deltaScale, false);
19,633,217✔
1903
    DECIMAL128_GET_MAX(toPrec, &max);
19,633,217✔
1904
    if (decimal128Gt(&res, &max, DECIMAL_WORD_NUM(Decimal128))) {
19,633,217✔
1905
      if (overflow) *overflow = true;
×
1906
    } else {
1907
      *(Decimal128*)pDec = res;
19,633,217✔
1908
    }
1909
  }
1910
}
145,785,781✔
1911

1912
static int32_t decimal128CountRoundingDelta(const Decimal128* pDec, int8_t scale, int8_t toScale,
145,789,161✔
1913
                                            DecimalRoundType roundType) {
1914
  if (roundType == ROUND_TYPE_TRUNC || toScale >= scale) return 0;
145,789,161✔
1915
  Decimal128 dec128 = *pDec;
19,633,217✔
1916
  int32_t    res = 0;
19,633,217✔
1917
  switch (roundType) {
19,633,217✔
1918
    case ROUND_TYPE_HALF_ROUND_UP: {
19,633,217✔
1919
      Decimal128 trailing = dec128;
19,633,217✔
1920
      decimal128Mod(&trailing, &SCALE_MULTIPLIER_128[scale - toScale], DECIMAL_WORD_NUM(Decimal128));
19,633,217✔
1921
      if (decimal128Eq(&trailing, &decimal128Zero, DECIMAL_WORD_NUM(Decimal128))) {
19,633,217✔
1922
        res = 0;
606,956✔
1923
        break;
609,694✔
1924
      }
1925
      Decimal128 tailingAbs = trailing, baseDiv2 = SCALE_MULTIPLIER_128[scale - toScale];
19,026,261✔
1926
      decimal128Abs(&tailingAbs);
19,026,261✔
1927
      decimal128Divide(&baseDiv2, &decimal128Two, DECIMAL_WORD_NUM(Decimal128), NULL);
19,026,261✔
1928
      if (decimal128Lt(&tailingAbs, &baseDiv2, DECIMAL_WORD_NUM(Decimal128))) {
19,026,261✔
1929
        res = 0;
9,606,193✔
1930
        break;
9,606,193✔
1931
      }
1932
      res = DECIMAL128_SIGN(pDec) == -1 ? -1 : 1;
9,420,068✔
1933
    } break;
9,420,068✔
1934
    case ROUND_TYPE_TRUNC:
×
1935
    default:
1936
      break;
×
1937
  }
1938
  return res;
19,633,217✔
1939
}
1940

1941
bool decimal128AddCheckOverflow(const Decimal128* pLeft, const DecimalType* pRight, uint8_t rightWordNum) {
110,566,514✔
1942
  if (DECIMAL128_SIGN(pLeft) == 0) {
1943
    Decimal128 max = decimal128Max;
1944
    decimal128Subtract(&max, pLeft, DECIMAL_WORD_NUM(Decimal128));
1945
    return decimal128Lt(&max, pRight, rightWordNum);
1946
  } else {
1947
    Decimal128 min = decimal128Min;
110,566,514✔
1948
    decimal128Subtract(&min, pLeft, DECIMAL_WORD_NUM(Decimal128));
110,566,514✔
1949
    return decimal128Gt(&min, pRight, rightWordNum);
110,566,514✔
1950
  }
1951
}
1952

1953
int32_t TEST_decimal64From_int64_t(Decimal64* pDec, uint8_t prec, uint8_t scale, int64_t v) {
×
1954
  return decimal64FromInt64(pDec, prec, scale, v);
×
1955
}
1956
int32_t TEST_decimal64From_uint64_t(Decimal64* pDec, uint8_t prec, uint8_t scale, uint64_t v) {
×
1957
  return decimal64FromUint64(pDec, prec, scale, v);
×
1958
}
1959
int32_t TEST_decimal64From_double(Decimal64* pDec, uint8_t prec, uint8_t scale, double v) {
2,384✔
1960
  return decimal64FromDouble(pDec, prec, scale, v);
2,384✔
1961
}
1962
double  TEST_decimal64ToDouble(Decimal64* pDec, uint8_t prec, uint8_t scale) {
196✔
1963
  return doubleFromDecimal64(pDec, prec, scale);
196✔
1964
}
1965

1966
int32_t TEST_decimal128From_int64_t(Decimal128* pDec, uint8_t prec, uint8_t scale, int64_t v) {
588✔
1967
  return decimal128FromInt64(pDec, prec, scale, v);
588✔
1968
}
1969
int32_t TEST_decimal128From_uint64_t(Decimal128* pDec, uint8_t prec, uint8_t scale, uint64_t v) {
×
1970
  return decimal128FromUint64(pDec, prec, scale, v);
×
1971
}
1972
int32_t TEST_decimal128From_double(Decimal128* pDec, uint8_t prec, uint8_t scale, double v) {
392✔
1973
  return decimal128FromDouble(pDec, prec, scale, v);
392✔
1974
}
1975
double  TEST_decimal128ToDouble(Decimal128* pDec, uint8_t prec, uint8_t scale) {
×
1976
  return doubleFromDecimal128(pDec, prec, scale);
×
1977
}
1978

1979
int32_t TEST_decimal64FromDecimal64(const Decimal64* pInput, uint8_t inputPrec, uint8_t inputScale, Decimal64* pOutput,
196✔
1980
                                    uint8_t outputPrec, uint8_t outputScale) {
1981
  return decimal64FromDecimal64(pOutput, outputPrec, outputScale, pInput, inputPrec, inputScale);
196✔
1982
}
1983

1984
int32_t TEST_decimal64FromDecimal128(const Decimal128* pInput, uint8_t prec, uint8_t inputScale, Decimal64* pOutput,
196✔
1985
                                     uint8_t outputPrec, uint8_t outputScale) {
1986
  return decimal64FromDecimal128(pOutput, outputPrec, outputScale, pInput, prec, inputScale);
196✔
1987
}
1988

1989
int32_t TEST_decimal128FromDecimal64(const Decimal64* pInput, uint8_t inputPrec, uint8_t inputScale,
196✔
1990
                                     Decimal128* pOutput, uint8_t outputPrec, uint8_t outputScale) {
1991
  return decimal128FromDecimal64(pOutput, outputPrec, outputScale, pInput, inputPrec, inputScale);
196✔
1992
}
1993
int32_t TEST_decimal128FromDecimal128(const Decimal128* pDec, uint8_t prec, uint8_t scale, Decimal128* pOutput,
196✔
1994
                                      uint8_t outputPrec, uint8_t outputScale) {
1995
  return decimal128FromDecimal128(pOutput, outputPrec, outputScale, pDec, prec, scale);
196✔
1996
}
1997

1998
void encodeDecimal(const DecimalType* pDec, int8_t type, void* pBuf) {
×
1999
  switch (type) {
×
2000
    case TSDB_DATA_TYPE_DECIMAL64:
×
2001
      *(DecimalWord*)pBuf = htobe64((DecimalWord)DECIMAL64_GET_VALUE((Decimal64*)pDec));
×
2002
      break;
×
2003
    case TSDB_DATA_TYPE_DECIMAL:
×
2004
      ((Decimal128*)pBuf)->words[0] = htobe64(DECIMAL128_LOW_WORD((Decimal128*)pDec));
×
2005
      ((Decimal128*)pBuf)->words[1] = htobe64(DECIMAL128_HIGH_WORD((Decimal128*)pDec));
×
2006
      break;
×
2007
    default:
×
2008
      break;
×
2009
  }
2010
}
×
2011

2012
void decodeDecimal(const void* pBuf, int8_t type, DecimalType* pDec) {
×
2013
  switch (type) {
×
2014
    case TSDB_DATA_TYPE_DECIMAL64:
×
2015
      DECIMAL64_SET_VALUE((Decimal64*)pDec, be64toh(*(DecimalWord*)pBuf));
×
2016
      break;
×
2017
    case TSDB_DATA_TYPE_DECIMAL:
×
2018
      DECIMAL128_SET_LOW_WORD((Decimal128*)pDec, be64toh(((Decimal128*)pBuf)->words[0]));
×
2019
      DECIMAL128_SET_HIGH_WORD((Decimal128*)pDec, be64toh(((Decimal128*)pBuf)->words[1]));
×
2020
      break;
×
2021
    default:
×
2022
      break;
×
2023
  }
2024
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc