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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 hits per line

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

89.63
/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) {
10,632,757✔
107
  if (pFunc->pSrcFuncRef) return AVG_RES_GET_SIZE(pFunc->pSrcFuncRef->srcFuncInputType.type);
10,632,757✔
108
  return AVG_RES_GET_SIZE(pFunc->srcFuncInputType.type);
6,994,971✔
109
}
110

111
bool getAvgFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
6,990,562✔
112
  pEnv->calcMemSize =getAvgInfoSize(pFunc);
6,990,562✔
113
  return true;
6,990,606✔
114
}
115

116
int32_t avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
687,838,733✔
117
  if (pResultInfo->initialized) {
687,838,733✔
118
    return TSDB_CODE_SUCCESS;
×
119
  }
120
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
689,060,710✔
121
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
122
  }
123

124
  void* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
691,495,392✔
125
  (void)memset(pRes, 0, pCtx->resDataInfo.interBufSize);
691,494,029✔
126
  return TSDB_CODE_SUCCESS;
691,024,033✔
127
}
128

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

132
  AVG_RES_INC_COUNT(pRes, type, numOfElem);
1,296,504✔
133
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,296,504✔
134
    CHECK_OVERFLOW_SUM_SIGNED(pRes, pAgg->sum);
1,179,342✔
135
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
117,162✔
136
    CHECK_OVERFLOW_SUM_UNSIGNED(pRes, pAgg->sum);
×
137
  } else if (IS_FLOAT_TYPE(type)) {
117,162✔
138
    SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), GET_DOUBLE_VAL((const char*)&(pAgg->sum)));
108,780✔
139
  } else if (IS_DECIMAL_TYPE(type)) {
8,382✔
140
    bool overflow = pAgg->overflow;
8,382✔
141
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
8,382✔
142
    SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes), &pAgg->decimal128Sum, TSDB_DATA_TYPE_DECIMAL);
6,858✔
143
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
6,858✔
144
  }
145

146
  *pNumOfElem = numOfElem;
1,294,980✔
147
  return 0;
1,294,980✔
148
}
149

150
static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputColumnInfoData *pInput, void* pRes, int32_t* pNumOfElem) {
534,067,095✔
151
  int32_t start = pInput->startRowIndex;
534,067,095✔
152
  int32_t numOfRows = pInput->numOfRows;
534,237,042✔
153
  int32_t numOfElems = 0;
534,419,762✔
154

155
  switch (type) {
534,419,762✔
156
    case TSDB_DATA_TYPE_TINYINT: {
11,297,537✔
157
      int8_t* plist = (int8_t*)pCol->pData;
11,297,537✔
158
      for (int32_t i = start; i < numOfRows + start; ++i) {
61,530,072✔
159
        if (colDataIsNull_f(pCol, i)) {
50,232,704✔
160
          continue;
24,189,364✔
161
        }
162

163
        numOfElems += 1;
26,043,171✔
164
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_TINYINT, 1);
26,043,171✔
165
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
26,043,171✔
166
      }
167

168
      break;
11,297,368✔
169
    }
170

171
    case TSDB_DATA_TYPE_SMALLINT: {
312,624✔
172
      int16_t* plist = (int16_t*)pCol->pData;
312,624✔
173
      for (int32_t i = start; i < numOfRows + start; ++i) {
21,688,174✔
174
        if (colDataIsNull_f(pCol, i)) {
21,375,550✔
175
          continue;
702,262✔
176
        }
177

178
        numOfElems += 1;
20,673,288✔
179
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_SMALLINT, 1);
20,673,288✔
180
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
20,672,891✔
181
      }
182
      break;
312,624✔
183
    }
184

185
    case TSDB_DATA_TYPE_INT: {
25,263,988✔
186
      int32_t* plist = (int32_t*)pCol->pData;
25,263,988✔
187
      for (int32_t i = start; i < numOfRows + start; ++i) {
274,110,864✔
188
        if (colDataIsNull_f(pCol, i)) {
248,845,041✔
189
          continue;
42,437,678✔
190
        }
191

192
        numOfElems += 1;
206,382,448✔
193
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_INT, 1);
206,382,448✔
194
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
206,383,994✔
195
      }
196

197
      break;
25,265,823✔
198
    }
199

200
    case TSDB_DATA_TYPE_BIGINT: {
459,802,365✔
201
      int64_t* plist = (int64_t*)pCol->pData;
459,802,365✔
202
      for (int32_t i = start; i < numOfRows + start; ++i) {
978,273,197✔
203
        if (colDataIsNull_f(pCol, i)) {
517,884,343✔
204
          continue;
489,317,566✔
205
        }
206

207
        numOfElems += 1;
29,305,275✔
208
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_BIGINT, 1);
29,305,275✔
209
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i]);
29,305,275✔
210
      }
211
      break;
460,388,854✔
212
    }
213

214
    case TSDB_DATA_TYPE_UTINYINT: {
6,060✔
215
      uint8_t* plist = (uint8_t*)pCol->pData;
6,060✔
216
      for (int32_t i = start; i < numOfRows + start; ++i) {
20,206,060✔
217
        if (colDataIsNull_f(pCol, i)) {
20,200,000✔
UNCOV
218
          continue;
×
219
        }
220

221
        numOfElems += 1;
20,200,000✔
222
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UTINYINT, 1);
20,200,000✔
223
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
20,200,000✔
224
      }
225

226
      break;
6,060✔
227
    }
228

229
    case TSDB_DATA_TYPE_USMALLINT: {
6,060✔
230
      uint16_t* plist = (uint16_t*)pCol->pData;
6,060✔
231
      for (int32_t i = start; i < numOfRows + start; ++i) {
20,206,060✔
232
        if (colDataIsNull_f(pCol, i)) {
20,200,000✔
UNCOV
233
          continue;
×
234
        }
235

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

243
    case TSDB_DATA_TYPE_UINT: {
51,365✔
244
      uint32_t* plist = (uint32_t*)pCol->pData;
51,365✔
245
      for (int32_t i = start; i < numOfRows + start; ++i) {
20,390,404✔
246
        if (colDataIsNull_f(pCol, i)) {
20,339,039✔
247
          continue;
3,148✔
248
        }
249

250
        numOfElems += 1;
20,335,891✔
251
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UINT, 1);
20,335,891✔
252
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
20,335,891✔
253
      }
254

255
      break;
51,365✔
256
    }
257

258
    case TSDB_DATA_TYPE_UBIGINT: {
9,806✔
259
      uint64_t* plist = (uint64_t*)pCol->pData;
9,806✔
260
      for (int32_t i = start; i < numOfRows + start; ++i) {
20,215,684✔
261
        if (colDataIsNull_f(pCol, i)) {
20,205,878✔
262
          continue;
2,680✔
263
        }
264

265
        numOfElems += 1;
20,203,198✔
266
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_UBIGINT, 1);
20,203,198✔
267
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i]);
20,203,198✔
268
        
269
      }
270
      break;
9,806✔
271
    }
272

273
    case TSDB_DATA_TYPE_FLOAT: {
11,527,631✔
274
      float* plist = (float*)pCol->pData;
11,527,631✔
275
      for (int32_t i = start; i < numOfRows + start; ++i) {
83,157,970✔
276
        if (colDataIsNull_f(pCol, i)) {
71,630,864✔
277
          continue;
22,770,192✔
278
        }
279

280
        numOfElems += 1;
48,861,022✔
281
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_FLOAT, 1);
48,861,022✔
282
        SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), plist[i]);
48,860,497✔
283
      }
284
      break;
11,527,106✔
285
    }
286

287
    case TSDB_DATA_TYPE_DOUBLE: {
26,108,798✔
288
      double* plist = (double*)pCol->pData;
26,108,798✔
289
      for (int32_t i = start; i < numOfRows + start; ++i) {
198,069,840✔
290
        if (colDataIsNull_f(pCol, i)) {
171,961,663✔
291
          continue;
70,164,497✔
292
        }
293

294
        numOfElems += 1;
101,796,533✔
295
        AVG_RES_INC_COUNT(pRes, TSDB_DATA_TYPE_DOUBLE, 1);
101,796,533✔
296
        SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pRes), plist[i]);
101,796,299✔
297
      }
298
      break;
26,108,177✔
299
    }
300
    case TSDB_DATA_TYPE_DECIMAL64:
33,528✔
301
    case TSDB_DATA_TYPE_DECIMAL: {
302
      const char* pDec = pCol->pData;
33,528✔
303
      for (int32_t i = start; i < numOfRows + start; ++i) {
18,336,768✔
304
        if (colDataIsNull_f(pCol, i)) {
18,303,240✔
305
          continue;
299,466✔
306
        }
307

308
        numOfElems += 1;
18,003,774✔
309
        AVG_RES_INC_COUNT(pRes, type, 1);
18,003,774✔
310
        bool overflow = false;
18,003,774✔
311
        SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes), (const void*)(pDec + i * tDataTypes[type].bytes), type);
18,003,774✔
312
        if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
18,003,774✔
313
      }
314
    } break;
33,528✔
315
    default:
×
316
      break;
×
317
  }
318

319
  *pNumOfElem = numOfElems;
535,006,771✔
320
  return 0;
534,469,209✔
321
}
322

323
int32_t avgFunction(SqlFunctionCtx* pCtx) {
1,124,354,629✔
324
  int32_t       numOfElem = 0;
1,124,354,629✔
325
  const int32_t THRESHOLD_SIZE = 8;
1,126,590,331✔
326

327
  SInputColumnInfoData* pInput = &pCtx->input;
1,126,590,331✔
328
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
1,127,244,264✔
329
  int32_t               type = pInput->pData[0]->info.type;
1,130,340,149✔
330
  pCtx->inputType = type;
1,130,286,409✔
331

332
  void* pAvgRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,130,605,219✔
333

334
  // computing based on the true data block
335
  SColumnInfoData* pCol = pInput->pData[0];
1,130,593,407✔
336

337
  int32_t start = pInput->startRowIndex;
1,130,337,056✔
338
  int32_t numOfRows = pInput->numOfRows;
1,130,452,505✔
339

340
  if (IS_NULL_TYPE(type)) {
1,130,606,071✔
341
    goto _over;
1,233,282✔
342
  }
343

344
  AVG_RES_SET_TYPE(pAvgRes, pCtx->inputType, type);
1,129,372,789✔
345
  if (IS_DECIMAL_TYPE(type)) AVG_RES_SET_INPUT_SCALE(pAvgRes, pInput->pData[0]->info.scale);
1,127,879,797✔
346

347
  if (pInput->colDataSMAIsSet) {  // try to use SMA if available
1,128,009,695✔
348
    int32_t code = calculateAvgBySMAInfo(pAvgRes, numOfRows, type, pAgg, &numOfElem);
1,296,504✔
349
    if (code != 0) return code;
1,296,504✔
350
  } else if (!pCol->hasNull) {  // try to employ the simd instructions to speed up the loop
1,125,347,930✔
351
    numOfElem = pInput->numOfRows;
594,306,020✔
352
    AVG_RES_INC_COUNT(pAvgRes, pCtx->inputType, pInput->numOfRows);
594,289,123✔
353

354
    switch(type) {
594,264,471✔
355
      case TSDB_DATA_TYPE_UTINYINT:
558,809✔
356
      case TSDB_DATA_TYPE_TINYINT: {
357
        const int8_t* plist = (const int8_t*) pCol->pData;
558,809✔
358

359
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
8,879,136✔
360
          if (type == TSDB_DATA_TYPE_TINYINT) {
8,320,327✔
361
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
6,724,997✔
362
          } else {
363
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint8_t)plist[i]);
1,595,330✔
364
          }
365
        }
366
        break;
558,809✔
367
      }
368

369
      case TSDB_DATA_TYPE_USMALLINT:
1,220,513✔
370
      case TSDB_DATA_TYPE_SMALLINT: {
371
        const int16_t* plist = (const int16_t*)pCol->pData;
1,220,513✔
372

373
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
11,942,608✔
374
          if (type == TSDB_DATA_TYPE_SMALLINT) {
10,722,095✔
375
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
9,126,765✔
376
          } else {
377
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint16_t)plist[i]);
1,595,330✔
378
          }
379
        }
380
        break;
1,220,513✔
381
      }
382

383
      case TSDB_DATA_TYPE_UINT:
9,605,381✔
384
      case TSDB_DATA_TYPE_INT: {
385
        const int32_t* plist = (const int32_t*) pCol->pData;
9,605,381✔
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]);
6,695,330✔
392
          }
393
        }
394
        break;
9,606,712✔
395
      }
396

397
      case TSDB_DATA_TYPE_UBIGINT:
482,876,214✔
398
      case TSDB_DATA_TYPE_BIGINT: {
399
        const int64_t* plist = (const int64_t*) pCol->pData;
482,876,214✔
400

401
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
1,332,522,796✔
402
          if (type == TSDB_DATA_TYPE_BIGINT) {
849,733,138✔
403
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i]);
847,472,063✔
404
          } else {
405
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint64_t)plist[i]);
2,261,075✔
406
          }
407
        }
408
        break;
482,832,083✔
409
      }
410

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

414
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
500,824,456✔
415
          SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pAvgRes), plist[i]);
470,644,683✔
416
        }
417
        break;
30,182,844✔
418
      }
419
      case TSDB_DATA_TYPE_DOUBLE: {
69,670,195✔
420
        const double* plist = (const double*)pCol->pData;
69,670,195✔
421

422
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
844,986,601✔
423
          SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pAvgRes), plist[i]);
775,358,314✔
424
        }
425
        break;
69,667,366✔
426
      }
427
      case TSDB_DATA_TYPE_DECIMAL:
152,400✔
428
      case TSDB_DATA_TYPE_DECIMAL64: {
429
        const char* pDec = pCol->pData;
152,400✔
430
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
304,800✔
431
          bool overflow = false;
152,400✔
432
          if (type == TSDB_DATA_TYPE_DECIMAL64) {
152,400✔
433
            SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pAvgRes), (const void*)(pDec + i * tDataTypes[type].bytes),
152,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;
152,400✔
440
        }
441
      } break;
152,400✔
442
      default:
×
443
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
444
    }
445
  } else {
446
    int32_t code = doAddNumericVector(pCol, type, pInput, pAvgRes, &numOfElem);
529,542,202✔
447
    if (code) return code;
534,059,619✔
448
  }
449

450
_over:
1,130,808,608✔
451
  // data in the check operation are all null, not output
452
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
1,128,202,008✔
453
  return TSDB_CODE_SUCCESS;
1,129,813,374✔
454
}
455

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

464

465
  AVG_RES_SET_TYPE(pOutput, inputDT, type);
402,425,459✔
466
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
764,454,498✔
467
    bool overflow = AVG_RES_GET_SUM_OVERFLOW(pInput, false, 0);
362,029,039✔
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);
362,029,039✔
469
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
40,410,869✔
470
    bool overflow = AVG_RES_GET_SUM_OVERFLOW(pInput, false, 0);
14,449✔
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);
14,449✔
472
  } else if (IS_DECIMAL_TYPE(type)) {
40,386,543✔
473
    AVG_RES_SET_INPUT_SCALE(pOutput, AVG_RES_GET_INPUT_SCALE(pInput));
4,572✔
474
    bool overflow = false;
4,572✔
475
    SUM_RES_INC_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pOutput), &AVG_RES_GET_DECIMAL_SUM(pInput), TSDB_DATA_TYPE_DECIMAL);
4,572✔
476
    if (overflow) return TSDB_CODE_DECIMAL_OVERFLOW;
4,572✔
477
  } else {
478
    SUM_RES_INC_DSUM(&AVG_RES_GET_SUM(pOutput), SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pInput)));
40,377,399✔
479
  }
480

481
  AVG_RES_INC_COUNT(pOutput, type, AVG_RES_GET_COUNT(pInput, true, type));
402,425,459✔
482
  return 0;
402,425,459✔
483
}
484

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

489
  if (IS_NULL_TYPE(pCol->info.type)) {
155,637,092✔
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) {
155,637,092✔
495
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
496
  }
497

498
  void* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
155,637,092✔
499

500
  int32_t start = pInput->startRowIndex;
155,637,092✔
501

502
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
558,922,532✔
503
    if(colDataIsNull_s(pCol, i)) continue;
806,570,880✔
504
    char*    data = colDataGetData(pCol, i);
403,285,440✔
505
    void* pInputInfo = varDataVal(data);
403,285,440✔
506
    int32_t code = avgTransferInfo(pCtx, pInputInfo, pInfo);
403,285,440✔
507
    if (code != 0) return code;
403,285,440✔
508
  }
509

510
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
155,637,092✔
511

512
  return TSDB_CODE_SUCCESS;
155,637,092✔
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) {
249,411,757✔
543
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
249,411,757✔
544

545
  void*   pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
249,412,696✔
546
  int32_t type = AVG_RES_GET_TYPE(pRes, pCtx->inputType);
249,412,160✔
547
  int64_t count = AVG_RES_GET_COUNT(pRes, true, type);
249,411,408✔
548

549
  if (AVG_RES_GET_COUNT(pRes, true, pCtx->inputType) > 0) {
249,411,134✔
550
    
551
    if(AVG_RES_GET_SUM_OVERFLOW(pRes, true, pCtx->inputType)) {
227,427,703✔
552
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_DSUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
247,458✔
553
    }else if (IS_SIGNED_NUMERIC_TYPE(type)) {
227,180,112✔
554
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_ISUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
129,718,118✔
555
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
97,461,994✔
556
      AVG_RES_GET_AVG(pRes) = SUM_RES_GET_USUM(&AVG_RES_GET_SUM(pRes)) / ((double)AVG_RES_GET_COUNT(pRes, false, 0));
57,069✔
557
    } else if (IS_DECIMAL_TYPE(type)) {
97,575,613✔
558
      int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
169,616✔
559
      SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
170,688✔
560
      SDataType        sumDt = {.type = TSDB_DATA_TYPE_DECIMAL,
170,688✔
561
                                .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
341,376✔
562
                                .precision = pCol->info.precision,
170,688✔
563
                                .scale = AVG_RES_GET_INPUT_SCALE(pRes)};
170,688✔
564
      SDataType        countDt = {
170,688✔
565
                 .type = TSDB_DATA_TYPE_BIGINT, .bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .precision = 0, .scale = 0};
170,688✔
566
      SDataType avgDt = {.type = TSDB_DATA_TYPE_DECIMAL,
170,688✔
567
                         .bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes,
341,376✔
568
                         .precision = pCol->info.precision,
170,688✔
569
                         .scale = pCol->info.scale};
170,688✔
570
      int64_t   count = AVG_RES_GET_COUNT(pRes, true, type);
170,688✔
571
      int32_t   code =
572
          decimalOp(OP_TYPE_DIV, &sumDt, &countDt, &avgDt, &SUM_RES_GET_DECIMAL_SUM(&AVG_RES_GET_DECIMAL_SUM(pRes)),
170,688✔
573
                    &count, &AVG_RES_GET_DECIMAL_AVG(pRes));
170,688✔
574
      if (code != TSDB_CODE_SUCCESS) {
170,688✔
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));
97,235,309✔
579
    }
580
  }
581
  if (AVG_RES_GET_COUNT(pRes, true, pCtx->inputType) == 0) {
249,410,465✔
582
    pEntryInfo->numOfRes = 0;
21,983,597✔
583
  } else if (!IS_DECIMAL_TYPE(pCtx->inputType)) {
227,426,465✔
584
    if (isinf(AVG_RES_GET_AVG(pRes)) || isnan(AVG_RES_GET_AVG(pRes))) pEntryInfo->numOfRes = 0;
227,257,148✔
585
  } else {
586
    pEntryInfo->numOfRes = 1;
171,494✔
587
  }
588

589
  return functionFinalize(pCtx, pBlock);
249,411,757✔
590
}
591

592
int32_t avgPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
436,366,690✔
593
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
436,366,690✔
594
  void*                pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
436,533,403✔
595
  int32_t              resultBytes = AVG_RES_GET_SIZE(pCtx->inputType);
436,533,624✔
596
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
436,567,010✔
597
  int32_t              code = TSDB_CODE_SUCCESS;
436,158,606✔
598
  if (NULL == res) {
436,158,606✔
599
    return terrno;
×
600
  }
601
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
436,158,606✔
602
  varDataSetLen(res, resultBytes);
436,144,325✔
603

604
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
436,325,662✔
605
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
436,494,750✔
606
  if(NULL == pCol) {
436,488,517✔
607
    code = TSDB_CODE_OUT_OF_RANGE;
×
608
    goto _exit;
×
609
  }
610

611
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
436,488,517✔
612

613
_exit:
436,314,576✔
614
  taosMemoryFree(res);
436,314,576✔
615
  return code;
436,288,237✔
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