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

taosdata / TDengine / #4936

23 Jan 2026 09:40AM UTC coverage: 66.746% (+0.04%) from 66.708%
#4936

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204023 of 305671 relevant lines covered (66.75%)

124768167.97 hits per line

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

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

16
#include "builtinsimpl.h"
17
#include "function.h"
18
#include "tdatablock.h"
19
#include "tfunctionInt.h"
20
#include "tglobal.h"
21

22
#define SET_VAL(_info, numOfElem, res) \
23
  do {                                 \
24
    if ((numOfElem) <= 0) {            \
25
      break;                           \
26
    }                                  \
27
    (_info)->numOfRes = (res);         \
28
  } while (0)
29

30
#define LIST_AVG_N(sumT, T)                                               \
31
  do {                                                                    \
32
    T* plist = (T*)pCol->pData;                                           \
33
    for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) { \
34
      if (colDataIsNull_f(pCol, i)) {                         \
35
        continue;                                                         \
36
      }                                                                   \
37
                                                                          \
38
      numOfElem += 1;                                                     \
39
      pAvgRes->count -= 1;                                                \
40
      sumT -= plist[i];                                                   \
41
    }                                                                     \
42
  } while (0)
43

44
// define signed number sum with check overflow
45
#define CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, val)                                    \
46
  do {                                                                             \
47
    SAvgRes* out = pAvgRes;                                                        \
48
    if (out->sum.overflow) {                                                       \
49
      out->sum.dsum += val;                                                        \
50
    } else if (out->sum.isum > 0 && val > 0 && INT64_MAX - out->sum.isum <= val || \
51
               out->sum.isum < 0 && val < 0 && INT64_MIN - out->sum.isum >= val) { \
52
      double dsum = (double)out->sum.isum;                                         \
53
      out->sum.overflow = true;                                                    \
54
      out->sum.dsum = dsum + val;                                                  \
55
    } else {                                                                       \
56
      out->sum.isum += val;                                                        \
57
    }                                                                              \
58
  } while (0)
59

60
// val is big than INT64_MAX, val come from merge
61
#define CHECK_OVERFLOW_SUM_SIGNED_BIG(pAvgRes, val, big)                                  \
62
  do {                                                                                    \
63
    SAvgRes* out = pAvgRes;                                                               \
64
    if (out->sum.overflow) {                                                              \
65
      out->sum.dsum += val;                                                               \
66
    } else if (out->sum.isum > 0 && val > 0 && INT64_MAX - out->sum.isum <= val ||        \
67
               out->sum.isum < 0 && val < 0 && INT64_MIN - out->sum.isum >= val || big) { \
68
      double dsum = (double)out->sum.isum;                                                \
69
      out->sum.overflow = true;                                                           \
70
      out->sum.dsum = dsum + val;                                                         \
71
    } else {                                                                              \
72
      SUM_RES_INC_ISUM(&AVG_RES_GET_SUM(out), val);                                       \
73
    }                                                                                     \
74
  } while (0)
75

76
// define unsigned number sum with check overflow
77
#define CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, val)   \
78
  do {                                              \
79
    SAvgRes* out = pAvgRes;                         \
80
    if (out->sum.overflow) {                        \
81
      out->sum.dsum += val;                         \
82
    } else if (UINT64_MAX - out->sum.usum <= val) { \
83
      double dsum = (double)out->sum.usum;          \
84
      out->sum.overflow = true;                     \
85
      out->sum.dsum = dsum + val;                   \
86
    } else {                                        \
87
      out->sum.usum += val;                         \
88
    }                                               \
89
  } while (0)
90

91
// val is big than UINT64_MAX, val come from merge
92
#define CHECK_OVERFLOW_SUM_UNSIGNED_BIG(pAvgRes, val, big) \
93
  do {                                                     \
94
    SAvgRes* out = pAvgRes;                                \
95
    if (out->sum.overflow) {                               \
96
      out->sum.dsum += val;                                \
97
    } else if (UINT64_MAX - out->sum.usum <= val || big) { \
98
      double dsum = (double)out->sum.usum;                 \
99
      out->sum.overflow = true;                            \
100
      out->sum.dsum = dsum + val;                          \
101
    } else {                                               \
102
      out->sum.usum += val;                                \
103
    }                                                      \
104
  } while (0)
105

106
int32_t getAvgInfoSize(SFunctionNode* pFunc) {
26,316,838✔
107
  if (pFunc->pSrcFuncRef) return AVG_RES_GET_SIZE(pFunc->pSrcFuncRef->srcFuncInputType.type);
26,316,838✔
108
  return AVG_RES_GET_SIZE(pFunc->srcFuncInputType.type);
17,565,446✔
109
}
110

111
bool getAvgFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
17,587,395✔
112
  pEnv->calcMemSize =getAvgInfoSize(pFunc);
17,587,395✔
113
  return true;
17,570,544✔
114
}
115

116
int32_t avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
2,147,483,647✔
117
  if (pResultInfo->initialized) {
2,147,483,647✔
118
    return TSDB_CODE_SUCCESS;
×
119
  }
120
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
2,147,483,647✔
121
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
122
  }
123

124
  void* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
2,147,483,647✔
125
  (void)memset(pRes, 0, pCtx->resDataInfo.interBufSize);
2,147,483,647✔
126
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
127
}
128

129
static int32_t calculateAvgBySMAInfo(void* pRes, int32_t numOfRows, int32_t type, const SColumnDataAgg* pAgg, int32_t* pNumOfElem) {
1,057,181✔
130
  int32_t numOfElem = numOfRows - pAgg->numOfNull;
1,057,181✔
131

132
  AVG_RES_INC_COUNT(pRes, type, numOfElem);
1,057,181✔
133
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,057,181✔
134
    CHECK_OVERFLOW_SUM_SIGNED(pRes, pAgg->sum);
997,681✔
135
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
59,500✔
136
    CHECK_OVERFLOW_SUM_UNSIGNED(pRes, pAgg->sum);
×
137
  } else if (IS_FLOAT_TYPE(type)) {
59,500✔
138
    SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), GET_DOUBLE_VAL((const char*)&(pAgg->sum)));
59,500✔
139
  } else if (IS_DECIMAL_TYPE(type)) {
×
140
    bool overflow = pAgg->overflow;
×
141
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
×
142
    SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes), &pAgg->decimal128Sum, TSDB_DATA_TYPE_DECIMAL);
×
143
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
×
144
  }
145

146
  *pNumOfElem = numOfElem;
1,057,181✔
147
  return 0;
1,057,181✔
148
}
149

150
static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputColumnInfoData *pInput, void* pRes, int32_t* pNumOfElem) {
2,147,483,647✔
151
  int32_t start = pInput->startRowIndex;
2,147,483,647✔
152
  int32_t numOfRows = pInput->numOfRows;
2,147,483,647✔
153
  int32_t numOfElems = 0;
2,147,483,647✔
154

155
  switch (type) {
2,147,483,647✔
156
    case TSDB_DATA_TYPE_TINYINT: {
1,158,789,889✔
157
      int8_t* plist = (int8_t*)pCol->pData;
1,158,789,889✔
158
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
159
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
160
          continue;
2,147,483,647✔
161
        }
162

163
        numOfElems += 1;
986,346,326✔
164
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_TINYINT, 1);
986,346,326✔
165
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
986,346,326✔
166
      }
167

168
      break;
1,158,792,573✔
169
    }
170

171
    case TSDB_DATA_TYPE_SMALLINT: {
541,972,460✔
172
      int16_t* plist = (int16_t*)pCol->pData;
541,972,460✔
173
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
174
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
175
          continue;
2,147,483,647✔
176
        }
177

178
        numOfElems += 1;
489,106,943✔
179
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_SMALLINT, 1);
489,106,943✔
180
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
489,106,943✔
181
      }
182
      break;
541,976,564✔
183
    }
184

185
    case TSDB_DATA_TYPE_INT: {
585,665,771✔
186
      int32_t* plist = (int32_t*)pCol->pData;
585,665,771✔
187
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
188
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
189
          continue;
2,147,483,647✔
190
        }
191

192
        numOfElems += 1;
771,387,694✔
193
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_INT, 1);
771,387,694✔
194
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
771,390,113✔
195
      }
196

197
      break;
585,635,243✔
198
    }
199

200
    case TSDB_DATA_TYPE_BIGINT: {
1,337,501,780✔
201
      int64_t* plist = (int64_t*)pCol->pData;
1,337,501,780✔
202
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
203
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
204
          continue;
2,147,483,647✔
205
        }
206

207
        numOfElems += 1;
667,064,302✔
208
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_BIGINT, 1);
667,064,302✔
209
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
667,064,302✔
210
      }
211
      break;
1,337,600,629✔
212
    }
213

214
    case TSDB_DATA_TYPE_UTINYINT: {
2,147,483,647✔
215
      uint8_t* plist = (uint8_t*)pCol->pData;
2,147,483,647✔
216
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
217
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
218
          continue;
2,147,483,647✔
219
        }
220

221
        numOfElems += 1;
2,147,483,647✔
222
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UTINYINT, 1);
2,147,483,647✔
223
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
2,147,483,647✔
224
      }
225

226
      break;
2,147,483,647✔
227
    }
228

229
    case TSDB_DATA_TYPE_USMALLINT: {
541,492,306✔
230
      uint16_t* plist = (uint16_t*)pCol->pData;
541,492,306✔
231
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
232
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
233
          continue;
2,147,483,647✔
234
        }
235

236
        numOfElems += 1;
579,060,000✔
237
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_USMALLINT, 1);
579,060,000✔
238
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
579,060,000✔
239
      }
240
      break;
541,481,412✔
241
    }
242

243
    case TSDB_DATA_TYPE_UINT: {
1,014,929,261✔
244
      uint32_t* plist = (uint32_t*)pCol->pData;
1,014,929,261✔
245
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
246
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
247
          continue;
2,147,483,647✔
248
        }
249

250
        numOfElems += 1;
1,057,235,265✔
251
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UINT, 1);
1,057,235,265✔
252
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
1,057,235,265✔
253
      }
254

255
      break;
1,014,929,038✔
256
    }
257

258
    case TSDB_DATA_TYPE_UBIGINT: {
541,527,708✔
259
      uint64_t* plist = (uint64_t*)pCol->pData;
541,527,708✔
260
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
261
        if (colDataIsNull_f(pCol, i)) {
2,147,483,647✔
262
          continue;
2,147,483,647✔
263
        }
264

265
        numOfElems += 1;
471,662,634✔
266
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UBIGINT, 1);
471,662,634✔
267
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
471,662,634✔
268
        
269
      }
270
      break;
541,526,498✔
271
    }
272

273
    case TSDB_DATA_TYPE_FLOAT: {
567,900,344✔
274
      float* plist = (float*)pCol->pData;
567,900,344✔
275
      for (int32_t i = start; i < numOfRows + start; ++i) {
1,894,774,005✔
276
        if (colDataIsNull_f(pCol, i)) {
1,326,869,795✔
277
          continue;
812,679,903✔
278
        }
279

280
        numOfElems += 1;
513,809,885✔
281
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_FLOAT, 1);
513,809,885✔
282
        SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), plist[i]);
513,809,439✔
283
      }
284
      break;
567,904,210✔
285
    }
286

287
    case TSDB_DATA_TYPE_DOUBLE: {
624,987,545✔
288
      double* plist = (double*)pCol->pData;
624,987,545✔
289
      for (int32_t i = start; i < numOfRows + start; ++i) {
2,147,483,647✔
290
        if (colDataIsNull_f(pCol, i)) {
1,925,513,304✔
291
          continue;
1,044,818,314✔
292
        }
293

294
        numOfElems += 1;
880,716,544✔
295
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_DOUBLE, 1);
880,716,544✔
296
        SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), plist[i]);
880,711,363✔
297
      }
298
      break;
625,012,951✔
299
    }
300
    case TSDB_DATA_TYPE_DECIMAL64:
38,335✔
301
    case TSDB_DATA_TYPE_DECIMAL: {
302
      const char* pDec = pCol->pData;
38,335✔
303
      for (int32_t i = start; i < numOfRows + start; ++i) {
23,139,703✔
304
        if (colDataIsNull_f(pCol, i)) {
23,102,762✔
305
          continue;
385,441✔
306
        }
307

308
        numOfElems += 1;
22,717,321✔
309
        AVG_RES_INC_COUNT(pRes, type, 1);
22,717,321✔
310
        bool overflow = false;
22,717,321✔
311
        SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes), (const void*)(pDec + i * tDataTypes[type].bytes), type);
22,717,321✔
312
        if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
22,717,321✔
313
      }
314
    } break;
36,941✔
315
    default:
1,004✔
316
      break;
1,004✔
317
  }
318

319
  *pNumOfElem = numOfElems;
2,147,483,647✔
320
  return 0;
2,147,483,647✔
321
}
322

323
int32_t avgFunction(SqlFunctionCtx* pCtx) {
2,147,483,647✔
324
  int32_t       numOfElem = 0;
2,147,483,647✔
325
  const int32_t THRESHOLD_SIZE = 8;
2,147,483,647✔
326

327
  SInputColumnInfoData* pInput = &pCtx->input;
2,147,483,647✔
328
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
2,147,483,647✔
329
  int32_t               type = pInput->pData[0]->info.type;
2,147,483,647✔
330
  pCtx->inputType = type;
2,147,483,647✔
331

332
  void* pAvgRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
2,147,483,647✔
333

334
  // computing based on the true data block
335
  SColumnInfoData* pCol = pInput->pData[0];
2,147,483,647✔
336

337
  int32_t start = pInput->startRowIndex;
2,147,483,647✔
338
  int32_t numOfRows = pInput->numOfRows;
2,147,483,647✔
339

340
  if (IS_NULL_TYPE(type)) {
2,147,483,647✔
341
    goto _over;
463,718,996✔
342
  }
343

344
  AVG_RES_SET_TYPE(pAvgRes, pCtx->inputType, type);
2,147,483,647✔
345
  if (IS_DECIMAL_TYPE(type)) AVG_RES_SET_INPUT_SCALE(pAvgRes, pInput->pData[0]->info.scale);
2,147,483,647✔
346

347
  if (pInput->colDataSMAIsSet) {  // try to use SMA if available
2,147,483,647✔
348
    int32_t code = calculateAvgBySMAInfo(pAvgRes, numOfRows, type, pAgg, &numOfElem);
1,057,181✔
349
    if (code != 0) return code;
1,057,181✔
350
  } else if (!pCol->hasNull) {  // try to employ the simd instructions to speed up the loop
2,147,483,647✔
351
    numOfElem = pInput->numOfRows;
1,093,556,740✔
352
    AVG_RES_INC_COUNT(pAvgRes, pCtx->inputType, pInput->numOfRows);
1,093,670,348✔
353

354
    switch(type) {
1,093,455,355✔
355
      case TSDB_DATA_TYPE_UTINYINT:
1,103,665✔
356
      case TSDB_DATA_TYPE_TINYINT: {
357
        const int8_t* plist = (const int8_t*) pCol->pData;
1,103,665✔
358

359
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2,147,483,647✔
360
          if (type == TSDB_DATA_TYPE_TINYINT) {
2,147,483,647✔
361
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
767,388,367✔
362
          } else {
363
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint8_t)plist[i]);
1,429,954,562✔
364
          }
365
        }
366
        break;
1,104,165✔
367
      }
368

369
      case TSDB_DATA_TYPE_USMALLINT:
2,388,768✔
370
      case TSDB_DATA_TYPE_SMALLINT: {
371
        const int16_t* plist = (const int16_t*)pCol->pData;
2,388,768✔
372

373
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
1,520,388,062✔
374
          if (type == TSDB_DATA_TYPE_SMALLINT) {
1,521,311,166✔
375
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
770,016,776✔
376
          } else {
377
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint16_t)plist[i]);
751,294,390✔
378
          }
379
        }
380
        break;
2,388,768✔
381
      }
382

383
      case TSDB_DATA_TYPE_UINT:
54,029,780✔
384
      case TSDB_DATA_TYPE_INT: {
385
        const int32_t* plist = (const int32_t*) pCol->pData;
54,029,780✔
386

387
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2,147,483,647✔
388
          if (type == TSDB_DATA_TYPE_INT) {
2,147,483,647✔
389
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
2,147,483,647✔
390
          } else {
391
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint32_t)plist[i]);
980,320,290✔
392
          }
393
        }
394
        break;
54,066,225✔
395
      }
396

397
      case TSDB_DATA_TYPE_UBIGINT:
870,925,051✔
398
      case TSDB_DATA_TYPE_BIGINT: {
399
        const int64_t* plist = (const int64_t*) pCol->pData;
870,925,051✔
400

401
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2,147,483,647✔
402
          if (type == TSDB_DATA_TYPE_BIGINT) {
2,147,483,647✔
403
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
2,147,483,647✔
404
          } else {
405
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint64_t)plist[i]);
645,181,485✔
406
          }
407
        }
408
        break;
870,571,545✔
409
      }
410

411
      case TSDB_DATA_TYPE_FLOAT: {
30,161,189✔
412
        const float* plist = (const float*) pCol->pData;
30,161,189✔
413

414
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
747,733,249✔
415
          SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pAvgRes), plist[i]);
717,562,536✔
416
        }
417
        break;
30,187,148✔
418
      }
419
      case TSDB_DATA_TYPE_DOUBLE: {
134,675,627✔
420
        const double* plist = (const double*)pCol->pData;
134,675,627✔
421

422
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
1,631,799,722✔
423
          SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pAvgRes), plist[i]);
1,497,149,444✔
424
        }
425
        break;
134,676,854✔
426
      }
427
      case TSDB_DATA_TYPE_DECIMAL:
139,400✔
428
      case TSDB_DATA_TYPE_DECIMAL64: {
429
        const char* pDec = pCol->pData;
139,400✔
430
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
278,800✔
431
          bool overflow = false;
139,400✔
432
          if (type == TSDB_DATA_TYPE_DECIMAL64) {
139,400✔
433
            SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pAvgRes), (const void*)(pDec + i * tDataTypes[type].bytes),
139,400✔
434
                                    TSDB_DATA_TYPE_DECIMAL64);
435
          } else {
436
            SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pAvgRes), (const void*)(pDec + i * tDataTypes[type].bytes),
×
437
                                    TSDB_DATA_TYPE_DECIMAL);
438
          }
439
          if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
139,400✔
440
        }
441
      } break;
139,400✔
442
      default:
31,876✔
443
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
31,876✔
444
    }
445
  } else {
446
    int32_t code = doAddNumericVector(pCol, type, pInput, pAvgRes, &numOfElem);
2,147,483,647✔
447
    if (code) return code;
2,147,483,647✔
448
  }
449

450
_over:
2,147,483,647✔
451
  // data in the check operation are all null, not output
452
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
2,147,483,647✔
453
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
454
}
455

456
static int32_t avgTransferInfo(SqlFunctionCtx* pCtx, void* pInput, void* pOutput) {
796,430,277✔
457
  int32_t inputDT = pCtx->pExpr->pExpr->_function.pFunctNode->srcFuncInputType.type;
796,430,277✔
458
  int32_t type = AVG_RES_GET_TYPE(pInput, inputDT);
796,430,277✔
459
  pCtx->inputType = type;
796,430,277✔
460
  if (IS_NULL_TYPE(type)) {
796,430,277✔
461
    return 0;
755,261✔
462
  }
463

464

465
  AVG_RES_SET_TYPE(pOutput, inputDT, type);
795,675,016✔
466
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,470,111,233✔
467
    bool overflow = AVG_RES_GET_SUM_OVERFLOW(pInput, false, 0);
674,436,217✔
468
    CHECK_OVERFLOW_SUM_SIGNED_BIG(pOutput, (overflow ? SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pInput)) : SUM_RES_GET_ISUM(&AVG_RES_GET_SUM(pInput))), overflow);
674,436,217✔
469
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
121,930,149✔
470
    bool overflow = AVG_RES_GET_SUM_OVERFLOW(pInput, false, 0);
691,350✔
471
    CHECK_OVERFLOW_SUM_UNSIGNED_BIG(pOutput, (overflow ? SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pInput)) : SUM_RES_GET_USUM(&AVG_RES_GET_SUM(pInput))), overflow);
691,350✔
472
  } else if (IS_DECIMAL_TYPE(type)) {
120,551,631✔
473
    AVG_RES_SET_INPUT_SCALE(pOutput, AVG_RES_GET_INPUT_SCALE(pInput));
4,182✔
474
    bool overflow = false;
4,182✔
475
    SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pOutput), &AVG_RES_GET_DECIMAL_SUM(pInput), TSDB_DATA_TYPE_DECIMAL);
4,182✔
476
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
4,182✔
477
  } else {
478
    SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pOutput), SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pInput)));
120,543,267✔
479
  }
480

481
  AVG_RES_INC_COUNT(pOutput, type, AVG_RES_GET_COUNT(pInput, true, type));
795,675,016✔
482
  return 0;
795,675,016✔
483
}
484

485
int32_t avgFunctionMerge(SqlFunctionCtx* pCtx) {
379,241,098✔
486
  SInputColumnInfoData* pInput = &pCtx->input;
379,241,098✔
487
  SColumnInfoData*      pCol = pInput->pData[0];
379,241,098✔
488

489
  if (IS_NULL_TYPE(pCol->info.type)) {
379,241,098✔
490
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
491
    return TSDB_CODE_SUCCESS;
×
492
  }
493

494
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
379,241,098✔
495
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
496
  }
497

498
  void* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
379,241,098✔
499

500
  int32_t start = pInput->startRowIndex;
379,241,098✔
501

502
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
1,175,671,375✔
503
    if(colDataIsNull_s(pCol, i)) continue;
1,592,860,554✔
504
    char*    data = colDataGetData(pCol, i);
796,430,277✔
505
    void* pInputInfo = varDataVal(data);
796,430,277✔
506
    int32_t code = avgTransferInfo(pCtx, pInputInfo, pInfo);
796,430,277✔
507
    if (code != 0) return code;
796,430,277✔
508
  }
509

510
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
379,241,098✔
511

512
  return TSDB_CODE_SUCCESS;
379,241,098✔
513
}
514

515
int32_t avgCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
516
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
517
  void*                pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
518
  int32_t              type = AVG_RES_GET_TYPE(pDBuf, pDestCtx->inputType);
×
519

520
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
521
  void*                pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
522
  type = (type == TSDB_DATA_TYPE_NULL) ? AVG_RES_GET_TYPE(pSBuf, pDestCtx->inputType) : type;
×
523

524
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
525
    CHECK_OVERFLOW_SUM_SIGNED(pDBuf, SUM_RES_GET_ISUM(&AVG_RES_GET_SUM(pSBuf)));
×
526
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
527
    CHECK_OVERFLOW_SUM_UNSIGNED(pDBuf, SUM_RES_GET_USUM(&AVG_RES_GET_SUM(pSBuf)));
×
528
  } else if (IS_DECIMAL_TYPE(type)) {
×
529
    bool overflow = false;
×
530
    SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pDBuf), &SUM_RES_GET_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pSBuf)), type);
×
531
    if (overflow) {
532

533
    }
534
  } else {
535
    SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pDBuf), SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pSBuf)));
×
536
  }
537
  AVG_RES_INC_COUNT(pDBuf, pDestCtx->inputType, AVG_RES_GET_COUNT(pSBuf, true, pDestCtx->inputType));
×
538

539
  return TSDB_CODE_SUCCESS;
×
540
}
541

542
int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1,972,152,132✔
543
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
1,972,152,132✔
544

545
  void*   pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
1,972,168,733✔
546
  int32_t type = AVG_RES_GET_TYPE(pRes, pCtx->inputType);
1,972,183,007✔
547
  int64_t count = AVG_RES_GET_COUNT(pRes, true, type);
1,972,160,207✔
548

549
  if (AVG_RES_GET_COUNT(pRes, true, pCtx->inputType) > 0) {
1,972,105,583✔
550
    
551
    if(AVG_RES_GET_SUM_OVERFLOW(pRes, true, pCtx->inputType)) {
1,757,570,310✔
552
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
4,477,764✔
553
    }else if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,753,091,075✔
554
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_ISUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
560,278,853✔
555
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
1,192,812,222✔
556
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_USUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
976,515,107✔
557
    } else if (IS_DECIMAL_TYPE(type)) {
216,453,243✔
558
      int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
155,044✔
559
      SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
156,128✔
560
      SDataType        sumDt = {.type = TSDB_DATA_TYPE_DECIMAL,
156,128✔
561
                                .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
312,256✔
562
                                .precision = pCol->info.precision,
156,128✔
563
                                .scale = AVG_RES_GET_INPUT_SCALE(pRes)};
156,128✔
564
      SDataType        countDt = {
156,128✔
565
                 .type = TSDB_DATA_TYPE_BIGINT, .bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .precision = 0, .scale = 0};
156,128✔
566
      SDataType avgDt = {.type = TSDB_DATA_TYPE_DECIMAL,
156,128✔
567
                         .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
312,256✔
568
                         .precision = pCol->info.precision,
156,128✔
569
                         .scale = pCol->info.scale};
156,128✔
570
      int64_t   count = AVG_RES_GET_COUNT(pRes, true, type);
156,128✔
571
      int32_t   code =
572
          decimalOp(OP_TYPE_DIV, &sumDt, &countDt, &avgDt, &SUM_RES_GET_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes)),
156,128✔
573
                    &count, &AVG_RES_GET_DECIMAL_AVG(pRes));
156,128✔
574
      if (code != TSDB_CODE_SUCCESS) {
156,128✔
575
        return code;
×
576
      }
577
    } else {
578
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
216,142,071✔
579
    }
580
  }
581
  if (AVG_RES_GET_COUNT(pRes, true, pCtx->inputType) == 0) {
1,972,120,522✔
582
    pEntryInfo->numOfRes = 0;
214,548,699✔
583
  } else if (!IS_DECIMAL_TYPE(pCtx->inputType)) {
1,757,566,144✔
584
    if (isinf(AVG_RES_GET_AVG(pRes)) || isnan(AVG_RES_GET_AVG(pRes))) pEntryInfo->numOfRes = 0;
1,757,411,181✔
585
  } else {
586
    pEntryInfo->numOfRes = 1;
155,550✔
587
  }
588

589
  return functionFinalize(pCtx, pBlock);
1,972,116,162✔
590
}
591

592
int32_t avgPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
851,635,432✔
593
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
851,635,432✔
594
  void*                pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
852,217,694✔
595
  int32_t              resultBytes = AVG_RES_GET_SIZE(pCtx->inputType);
852,443,320✔
596
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
852,531,835✔
597
  int32_t              code = TSDB_CODE_SUCCESS;
849,789,577✔
598
  if (NULL == res) {
849,789,577✔
599
    return terrno;
×
600
  }
601
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
849,789,577✔
602
  varDataSetLen(res, resultBytes);
849,769,257✔
603

604
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
851,269,649✔
605
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
852,091,537✔
606
  if(NULL == pCol) {
851,876,573✔
607
    code = TSDB_CODE_OUT_OF_RANGE;
×
608
    goto _exit;
×
609
  }
610

611
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
851,876,573✔
612

613
_exit:
852,355,491✔
614
  taosMemoryFree(res);
852,355,491✔
615
  return code;
851,178,128✔
616
}
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