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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

77.45
/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->nullbitmap, 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(out, val)                                      \
46
  if (out->sum.overflow) {                                                       \
47
    out->sum.dsum += val;                                                        \
48
  } else if (out->sum.isum > 0 && val > 0 && INT64_MAX - out->sum.isum <= val || \
49
             out->sum.isum < 0 && val < 0 && INT64_MIN - out->sum.isum >= val) { \
50
    double dsum = (double)out->sum.isum;                                         \
51
    out->sum.overflow = true;                                                    \
52
    out->sum.dsum = dsum + val;                                                  \
53
  } else {                                                                       \
54
    out->sum.isum += val;                                                        \
55
  }
56

57
// val is big than INT64_MAX, val come from merge
58
#define CHECK_OVERFLOW_SUM_SIGNED_BIG(out, val, big)                             \
59
  if (out->sum.overflow) {                                                       \
60
    out->sum.dsum += val;                                                        \
61
  } else if (out->sum.isum > 0 && val > 0 && INT64_MAX - out->sum.isum <= val || \
62
             out->sum.isum < 0 && val < 0 && INT64_MIN - out->sum.isum >= val || \
63
             big) {                                                              \
64
    double dsum = (double)out->sum.isum;                                         \
65
    out->sum.overflow = true;                                                    \
66
    out->sum.dsum = dsum + val;                                                  \
67
  } else {                                                                       \
68
    out->sum.isum += val;                                                        \
69
  }
70

71
// define unsigned number sum with check overflow
72
#define CHECK_OVERFLOW_SUM_UNSIGNED(out, val)                 \
73
  if (out->sum.overflow) {                                    \
74
    out->sum.dsum += val;                                     \
75
  } else if (UINT64_MAX - out->sum.usum <= val) {             \
76
    double dsum = (double)out->sum.usum;                      \
77
    out->sum.overflow = true;                                 \
78
    out->sum.dsum = dsum + val;                               \
79
  } else {                                                    \
80
    out->sum.usum += val;                                     \
81
  }
82

83
// val is big than UINT64_MAX, val come from merge
84
#define CHECK_OVERFLOW_SUM_UNSIGNED_BIG(out, val, big)        \
85
  if (out->sum.overflow) {                                    \
86
    out->sum.dsum += val;                                     \
87
  } else if (UINT64_MAX - out->sum.usum <= val || big) {      \
88
    double dsum = (double)out->sum.usum;                      \
89
    out->sum.overflow = true;                                 \
90
    out->sum.dsum = dsum + val;                               \
91
  } else {                                                    \
92
    out->sum.usum += val;                                     \
93
  }
94

95
int32_t getAvgInfoSize() { return (int32_t)sizeof(SAvgRes); }
21,587,461✔
96

97
bool getAvgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
204,711✔
98
  pEnv->calcMemSize = sizeof(SAvgRes);
204,711✔
99
  return true;
204,711✔
100
}
101

102
int32_t avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
31,287,357✔
103
  if (pResultInfo->initialized) {
31,287,357!
104
    return TSDB_CODE_SUCCESS;
×
105
  }
106
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
31,287,357!
107
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
108
  }
109

110
  SAvgRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
31,281,891✔
111
  (void)memset(pRes, 0, sizeof(SAvgRes));
31,281,891✔
112
  return TSDB_CODE_SUCCESS;
31,281,891✔
113
}
114

115
static int32_t calculateAvgBySMAInfo(SAvgRes* pRes, int32_t numOfRows, int32_t type, const SColumnDataAgg* pAgg) {
131✔
116
  int32_t numOfElem = numOfRows - pAgg->numOfNull;
131✔
117

118
  pRes->count += numOfElem;
131✔
119
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
131!
120
    CHECK_OVERFLOW_SUM_SIGNED(pRes, pAgg->sum);
131!
121
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
122
    CHECK_OVERFLOW_SUM_UNSIGNED(pRes, pAgg->sum);
×
123
  } else if (IS_FLOAT_TYPE(type)) {
×
124
    pRes->sum.dsum += GET_DOUBLE_VAL((const char*)&(pAgg->sum));
×
125
  }
126

127
  return numOfElem;
131✔
128
}
129

130
static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputColumnInfoData *pInput, SAvgRes* pRes) {
20,715,236✔
131
  int32_t start = pInput->startRowIndex;
20,715,236✔
132
  int32_t numOfRows = pInput->numOfRows;
20,715,236✔
133
  int32_t numOfElems = 0;
20,715,236✔
134

135
  switch (type) {
20,715,236!
136
    case TSDB_DATA_TYPE_TINYINT: {
11,611✔
137
      int8_t* plist = (int8_t*)pCol->pData;
11,611✔
138
      for (int32_t i = start; i < numOfRows + start; ++i) {
262,475✔
139
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
250,864✔
140
          continue;
161,344✔
141
        }
142

143
        numOfElems += 1;
89,520✔
144
        pRes->count += 1;
89,520✔
145
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i])
89,520!
146
      }
147

148
      break;
11,611✔
149
    }
150

151
    case TSDB_DATA_TYPE_SMALLINT: {
11,849✔
152
      int16_t* plist = (int16_t*)pCol->pData;
11,849✔
153
      for (int32_t i = start; i < numOfRows + start; ++i) {
353,062✔
154
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
341,213✔
155
          continue;
110,713✔
156
        }
157

158
        numOfElems += 1;
230,500✔
159
        pRes->count += 1;
230,500✔
160
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i])
230,500!
161
      }
162
      break;
11,849✔
163
    }
164

165
    case TSDB_DATA_TYPE_INT: {
91,181✔
166
      int32_t* plist = (int32_t*)pCol->pData;
91,181✔
167
      for (int32_t i = start; i < numOfRows + start; ++i) {
16,742,405✔
168
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
16,651,224✔
169
          continue;
25,118✔
170
        }
171

172
        numOfElems += 1;
16,626,106✔
173
        pRes->count += 1;
16,626,106✔
174
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i])
16,626,106!
175
      }
176

177
      break;
91,181✔
178
    }
179

180
    case TSDB_DATA_TYPE_BIGINT: {
20,236,751✔
181
      int64_t* plist = (int64_t*)pCol->pData;
20,236,751✔
182
      for (int32_t i = start; i < numOfRows + start; ++i) {
99,730,325✔
183
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
79,493,574✔
184
          continue;
64,158,003✔
185
        }
186

187
        numOfElems += 1;
15,335,571✔
188
        pRes->count += 1;
15,335,571✔
189
        CHECK_OVERFLOW_SUM_SIGNED(pRes, plist[i])
15,335,571✔
190
      }
191
      break;
20,236,751✔
192
    }
193

194
    case TSDB_DATA_TYPE_UTINYINT: {
25✔
195
      uint8_t* plist = (uint8_t*)pCol->pData;
25✔
196
      for (int32_t i = start; i < numOfRows + start; ++i) {
80,035✔
197
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
80,010!
198
          continue;
×
199
        }
200

201
        numOfElems += 1;
80,010✔
202
        pRes->count += 1;
80,010✔
203
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i])
80,010!
204
      }
205

206
      break;
25✔
207
    }
208

209
    case TSDB_DATA_TYPE_USMALLINT: {
25✔
210
      uint16_t* plist = (uint16_t*)pCol->pData;
25✔
211
      for (int32_t i = start; i < numOfRows + start; ++i) {
80,035✔
212
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
80,010!
213
          continue;
×
214
        }
215

216
        numOfElems += 1;
80,010✔
217
        pRes->count += 1;
80,010✔
218
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i])
80,010!
219
      }
220
      break;
25✔
221
    }
222

223
    case TSDB_DATA_TYPE_UINT: {
109,333✔
224
      uint32_t* plist = (uint32_t*)pCol->pData;
109,333✔
225
      for (int32_t i = start; i < numOfRows + start; ++i) {
329,943✔
226
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
220,610✔
227
          continue;
19,968✔
228
        }
229

230
        numOfElems += 1;
200,642✔
231
        pRes->count += 1;
200,642✔
232
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i])
200,642!
233
      }
234

235
      break;
109,333✔
236
    }
237

238
    case TSDB_DATA_TYPE_UBIGINT: {
55✔
239
      uint64_t* plist = (uint64_t*)pCol->pData;
55✔
240
      for (int32_t i = start; i < numOfRows + start; ++i) {
80,774✔
241
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
80,719✔
242
          continue;
4✔
243
        }
244

245
        numOfElems += 1;
80,715✔
246
        pRes->count += 1;
80,715✔
247
        CHECK_OVERFLOW_SUM_UNSIGNED(pRes, plist[i])
80,715✔
248
        
249
      }
250
      break;
55✔
251
    }
252

253
    case TSDB_DATA_TYPE_FLOAT: {
20,612✔
254
      float* plist = (float*)pCol->pData;
20,612✔
255
      for (int32_t i = start; i < numOfRows + start; ++i) {
298,820✔
256
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
278,208✔
257
          continue;
30,105✔
258
        }
259

260
        numOfElems += 1;
248,103✔
261
        pRes->count += 1;
248,103✔
262
        pRes->sum.dsum += plist[i];
248,103✔
263
      }
264
      break;
20,612✔
265
    }
266

267
    case TSDB_DATA_TYPE_DOUBLE: {
247,347✔
268
      double* plist = (double*)pCol->pData;
247,347✔
269
      for (int32_t i = start; i < numOfRows + start; ++i) {
6,653,835✔
270
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
6,406,488✔
271
          continue;
787,742✔
272
        }
273

274
        numOfElems += 1;
5,618,746✔
275
        pRes->count += 1;
5,618,746✔
276
        pRes->sum.dsum += plist[i];
5,618,746✔
277
      }
278
      break;
247,347✔
279
    }
280

281
    default:
×
282
      break;
×
283
  }
284

285
  return numOfElems;
20,715,236✔
286
}
287

288
int32_t avgFunction(SqlFunctionCtx* pCtx) {
43,361,174✔
289
  int32_t       numOfElem = 0;
43,361,174✔
290
  const int32_t THRESHOLD_SIZE = 8;
43,361,174✔
291

292
  SInputColumnInfoData* pInput = &pCtx->input;
43,361,174✔
293
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
43,361,174✔
294
  int32_t               type = pInput->pData[0]->info.type;
43,361,174✔
295

296
  SAvgRes* pAvgRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
43,361,174✔
297

298
  // computing based on the true data block
299
  SColumnInfoData* pCol = pInput->pData[0];
43,361,174✔
300

301
  int32_t start = pInput->startRowIndex;
43,361,174✔
302
  int32_t numOfRows = pInput->numOfRows;
43,361,174✔
303

304
  if (IS_NULL_TYPE(type)) {
43,361,174✔
305
    goto _over;
150✔
306
  }
307

308
  pAvgRes->type = type;
43,361,024✔
309

310
  if (pInput->colDataSMAIsSet) {  // try to use SMA if available
43,361,024✔
311
    numOfElem = calculateAvgBySMAInfo(pAvgRes, numOfRows, type, pAgg);
131✔
312
  } else if (!pCol->hasNull) {  // try to employ the simd instructions to speed up the loop
43,360,893✔
313
    numOfElem = pInput->numOfRows;
23,768,305✔
314
    pAvgRes->count += pInput->numOfRows;
23,768,305✔
315

316
    switch(type) {
23,768,305!
317
      case TSDB_DATA_TYPE_UTINYINT:
8,812✔
318
      case TSDB_DATA_TYPE_TINYINT: {
319
        const int8_t* plist = (const int8_t*) pCol->pData;
8,812✔
320

321
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
399,514✔
322
          if (type == TSDB_DATA_TYPE_TINYINT) {
390,702✔
323
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i])
340,662!
324
          } else {
325
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint8_t)plist[i])
50,040!
326
          }
327
        }
328
        break;
8,812✔
329
      }
330

331
      case TSDB_DATA_TYPE_USMALLINT:
8,956✔
332
      case TSDB_DATA_TYPE_SMALLINT: {
333
        const int16_t* plist = (const int16_t*)pCol->pData;
8,956✔
334

335
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
649,623✔
336
          if (type == TSDB_DATA_TYPE_SMALLINT) {
640,667✔
337
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i])
590,627!
338
          } else {
339
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint16_t)plist[i])
50,040!
340
          }
341
        }
342
        break;
8,956✔
343
      }
344

345
      case TSDB_DATA_TYPE_UINT:
5,614,557✔
346
      case TSDB_DATA_TYPE_INT: {
347
        const int32_t* plist = (const int32_t*) pCol->pData;
5,614,557✔
348

349
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
96,381,353✔
350
          if (type == TSDB_DATA_TYPE_INT) {
90,766,796✔
351
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i])
90,496,551!
352
          } else {
353
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint32_t)plist[i])
270,245!
354
          }
355
        }
356
        break;
5,614,557✔
357
      }
358

359
      case TSDB_DATA_TYPE_UBIGINT:
17,421,474✔
360
      case TSDB_DATA_TYPE_BIGINT: {
361
        const int64_t* plist = (const int64_t*) pCol->pData;
17,421,474✔
362

363
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
87,126,726✔
364
          if (type == TSDB_DATA_TYPE_BIGINT) {
69,705,252✔
365
            CHECK_OVERFLOW_SUM_SIGNED(pAvgRes, plist[i])
69,651,988✔
366
          } else {
367
            CHECK_OVERFLOW_SUM_UNSIGNED(pAvgRes, (uint64_t)plist[i])
53,264✔
368
          }
369
        }
370
        break;
17,421,474✔
371
      }
372

373
      case TSDB_DATA_TYPE_FLOAT: {
59,209✔
374
        const float* plist = (const float*) pCol->pData;
59,209✔
375

376
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2,191,549✔
377
          pAvgRes->sum.dsum += plist[i];
2,132,340✔
378
        }
379
        break;
59,209✔
380
      }
381
      case TSDB_DATA_TYPE_DOUBLE: {
664,446✔
382
        const double* plist = (const double*)pCol->pData;
664,446✔
383

384
        for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
13,456,069✔
385
          pAvgRes->sum.dsum += plist[i];
12,791,623✔
386
        }
387
        break;
664,446✔
388
      }
389
      default:
×
390
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
391
    }
392
  } else {
393
    numOfElem = doAddNumericVector(pCol, type, pInput, pAvgRes);
19,592,588✔
394
  }
395

396
_over:
44,648,511✔
397
  // data in the check operation are all null, not output
398
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
44,648,511✔
399
  return TSDB_CODE_SUCCESS;
44,648,511✔
400
}
401

402
static void avgTransferInfo(SAvgRes* pInput, SAvgRes* pOutput) {
19,572,629✔
403
  if (IS_NULL_TYPE(pInput->type)) {
19,572,629✔
404
    return;
138✔
405
  }
406

407
  pOutput->type = pInput->type;
19,572,491✔
408
  if (IS_SIGNED_NUMERIC_TYPE(pOutput->type)) {
38,332,254!
409
    bool overflow = pInput->sum.overflow;
18,759,763✔
410
    CHECK_OVERFLOW_SUM_SIGNED_BIG(pOutput, (overflow ? pInput->sum.dsum : pInput->sum.isum), overflow);
18,759,763!
411
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pOutput->type)) {
819,633!
412
    bool overflow = pInput->sum.overflow;
6,905✔
413
    CHECK_OVERFLOW_SUM_UNSIGNED_BIG(pOutput, (overflow ? pInput->sum.dsum : pInput->sum.usum), overflow);
6,905!
414
  } else {
415
    pOutput->sum.dsum += pInput->sum.dsum;
805,823✔
416
  }
417

418
  pOutput->count += pInput->count;
19,572,491✔
419
}
420

421
int32_t avgFunctionMerge(SqlFunctionCtx* pCtx) {
8,034,534✔
422
  SInputColumnInfoData* pInput = &pCtx->input;
8,034,534✔
423
  SColumnInfoData*      pCol = pInput->pData[0];
8,034,534✔
424

425
  if (IS_NULL_TYPE(pCol->info.type)) {
8,034,534✔
426
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
27✔
427
    return TSDB_CODE_SUCCESS;
27✔
428
  }
429

430
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
8,034,507!
431
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
432
  }
433

434
  SAvgRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
8,034,507✔
435

436
  int32_t start = pInput->startRowIndex;
8,034,507✔
437

438
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
27,609,326✔
439
    if(colDataIsNull_s(pCol, i)) continue;
39,144,260!
440
    char*    data = colDataGetData(pCol, i);
19,572,130!
441
    SAvgRes* pInputInfo = (SAvgRes*)varDataVal(data);
19,572,130✔
442
    avgTransferInfo(pInputInfo, pInfo);
19,572,130✔
443
  }
444

445
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
8,037,196✔
446

447
  return TSDB_CODE_SUCCESS;
8,037,196✔
448
}
449

450
#ifdef BUILD_NO_CALL
451
int32_t avgInvertFunction(SqlFunctionCtx* pCtx) {
452
  int32_t numOfElem = 0;
453

454
  // Only the pre-computing information loaded and actual data does not loaded
455
  SInputColumnInfoData* pInput = &pCtx->input;
456
  SAvgRes* pAvgRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
457

458
  // computing based on the true data block
459
  SColumnInfoData* pCol = pInput->pData[0];
460

461
  int32_t start = pInput->startRowIndex;
462
  int32_t numOfRows = pInput->numOfRows;
463

464
  switch (pCol->info.type) {
465
    case TSDB_DATA_TYPE_TINYINT: {
466
      LIST_AVG_N(pAvgRes->sum.isum, int8_t);
467
      break;
468
    }
469
    case TSDB_DATA_TYPE_SMALLINT: {
470
      LIST_AVG_N(pAvgRes->sum.isum, int16_t);
471
      break;
472
    }
473
    case TSDB_DATA_TYPE_INT: {
474
      LIST_AVG_N(pAvgRes->sum.isum, int32_t);
475
      break;
476
    }
477
    case TSDB_DATA_TYPE_BIGINT: {
478
      LIST_AVG_N(pAvgRes->sum.isum, int64_t);
479
      break;
480
    }
481
    case TSDB_DATA_TYPE_UTINYINT: {
482
      LIST_AVG_N(pAvgRes->sum.usum, uint8_t);
483
      break;
484
    }
485
    case TSDB_DATA_TYPE_USMALLINT: {
486
      LIST_AVG_N(pAvgRes->sum.usum, uint16_t);
487
      break;
488
    }
489
    case TSDB_DATA_TYPE_UINT: {
490
      LIST_AVG_N(pAvgRes->sum.usum, uint32_t);
491
      break;
492
    }
493
    case TSDB_DATA_TYPE_UBIGINT: {
494
      LIST_AVG_N(pAvgRes->sum.usum, uint64_t);
495
      break;
496
    }
497
    case TSDB_DATA_TYPE_FLOAT: {
498
      LIST_AVG_N(pAvgRes->sum.dsum, float);
499
      break;
500
    }
501
    case TSDB_DATA_TYPE_DOUBLE: {
502
      LIST_AVG_N(pAvgRes->sum.dsum, double);
503
      break;
504
    }
505
    default:
506
      break;
507
  }
508

509
  // data in the check operation are all null, not output
510
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
511
  return TSDB_CODE_SUCCESS;
512
}
513
#endif
514

515
int32_t avgCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
516
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
517
  SAvgRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
518

519
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
520
  SAvgRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
521
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
×
522

523
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
524
    CHECK_OVERFLOW_SUM_SIGNED(pDBuf, pSBuf->sum.isum)
×
525
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
526
    CHECK_OVERFLOW_SUM_UNSIGNED(pDBuf, pSBuf->sum.usum)
×
527
  } else {
528
    pDBuf->sum.dsum += pSBuf->sum.dsum;
×
529
  }
530
  pDBuf->count += pSBuf->count;
×
531

532
  return TSDB_CODE_SUCCESS;
×
533
}
534

535
int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
10,404,670✔
536
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
10,404,670✔
537

538
  SAvgRes* pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
10,404,670✔
539
  int32_t  type = pRes->type;
10,404,670✔
540

541
  if (pRes->count > 0) {
10,404,670✔
542
    if(pRes->sum.overflow) {
10,339,186✔
543
      // overflow flag set , use dsum
544
      pRes->result = pRes->sum.dsum / ((double)pRes->count);
467,643✔
545
    }else if (IS_SIGNED_NUMERIC_TYPE(type)) {
9,871,543!
546
      pRes->result = pRes->sum.isum / ((double)pRes->count);
8,828,656✔
547
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
1,042,887!
548
      pRes->result = pRes->sum.usum / ((double)pRes->count);
151,162✔
549
    } else {
550
      pRes->result = pRes->sum.dsum / ((double)pRes->count);
891,725✔
551
    }
552
  }
553

554
  if (pRes->count == 0 || isinf(pRes->result) || isnan(pRes->result)) {
10,404,670!
555
    pEntryInfo->numOfRes = 0;
65,498✔
556
  } else {
557
    pEntryInfo->numOfRes = 1;
10,339,172✔
558
  }
559

560
  return functionFinalize(pCtx, pBlock);
10,404,670✔
561
}
562

563
int32_t avgPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
21,526,219✔
564
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
21,526,219✔
565
  SAvgRes*             pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
21,526,219✔
566
  int32_t              resultBytes = getAvgInfoSize();
21,526,219✔
567
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
21,504,576✔
568
  int32_t              code = TSDB_CODE_SUCCESS;
21,983,022✔
569
  if (NULL == res) {
21,983,022!
UNCOV
570
    return terrno;
×
571
  }
572
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
21,983,022✔
573
  varDataSetLen(res, resultBytes);
21,983,022✔
574

575
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
21,983,022✔
576
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
21,983,022✔
577
  if(NULL == pCol) {
21,897,193!
578
    code = TSDB_CODE_OUT_OF_RANGE;
×
579
    goto _exit;
×
580
  }
581

582
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
21,897,193✔
583

584
_exit:
21,752,362✔
585
  taosMemoryFree(res);
21,752,362✔
586
  return code;
21,909,785✔
587
}
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