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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

73.89
/source/libs/function/src/builtinsimpl.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 "cJSON.h"
18
#include "function.h"
19
#include "functionResInfoInt.h"
20
#include "query.h"
21
#include "querynodes.h"
22
#include "tanalytics.h"
23
#include "tcompare.h"
24
#include "tdatablock.h"
25
#include "tdigest.h"
26
#include "tfunctionInt.h"
27
#include "tglobal.h"
28
#include "thistogram.h"
29
#include "tpercentile.h"
30

31
bool ignoreNegative(int8_t ignoreOption){
36,888,563✔
32
  return (ignoreOption & 0x1) == 0x1;
36,888,563✔
33
}
34
bool ignoreNull(int8_t ignoreOption){
376,491✔
35
  return (ignoreOption & 0x2) == 0x2;
376,491✔
36
}
37

38
typedef enum {
39
  APERCT_ALGO_UNKNOWN = 0,
40
  APERCT_ALGO_DEFAULT,
41
  APERCT_ALGO_TDIGEST,
42
} EAPerctAlgoType;
43

44
typedef enum { UNKNOWN_BIN = 0, USER_INPUT_BIN, LINEAR_BIN, LOG_BIN } EHistoBinType;
45

46
typedef enum {
47
  STATE_OPER_INVALID = 0,
48
  STATE_OPER_LT,
49
  STATE_OPER_GT,
50
  STATE_OPER_LE,
51
  STATE_OPER_GE,
52
  STATE_OPER_NE,
53
  STATE_OPER_EQ,
54
} EStateOperType;
55

56
#define SET_VAL(_info, numOfElem, res) \
57
  do {                                 \
58
    if ((numOfElem) <= 0) {            \
59
      break;                           \
60
    }                                  \
61
    (_info)->numOfRes = (res);         \
62
  } while (0)
63

64
#define GET_TS_LIST(x)    ((TSKEY*)((x)->ptsList))
65
#define GET_TS_DATA(x, y) (GET_TS_LIST(x)[(y)])
66

67
#define DO_UPDATE_SUBSID_RES(ctx, ts)                          \
68
  do {                                                         \
69
    for (int32_t _i = 0; _i < (ctx)->subsidiaries.num; ++_i) { \
70
      SqlFunctionCtx* __ctx = (ctx)->subsidiaries.pCtx[_i];    \
71
      if (__ctx->functionId == FUNCTION_TS_DUMMY) {            \
72
        __ctx->tag.i = (ts);                                   \
73
        __ctx->tag.nType = TSDB_DATA_TYPE_BIGINT;              \
74
      }                                                        \
75
      __ctx->fpSet.process(__ctx);                             \
76
    }                                                          \
77
  } while (0)
78

79
#define UPDATE_DATA(ctx, left, right, num, sign, _ts) \
80
  do {                                                \
81
    if (((left) < (right)) ^ (sign)) {                \
82
      (left) = (right);                               \
83
      DO_UPDATE_SUBSID_RES(ctx, _ts);                 \
84
      (num) += 1;                                     \
85
    }                                                 \
86
  } while (0)
87

88
#define LOOPCHECK_N(val, _col, ctx, _t, _nrow, _start, sign, num)        \
89
  do {                                                                   \
90
    _t* d = (_t*)((_col)->pData);                                        \
91
    for (int32_t i = (_start); i < (_nrow) + (_start); ++i) {            \
92
      if (((_col)->hasNull) && colDataIsNull_f((_col)->nullbitmap, i)) { \
93
        continue;                                                        \
94
      }                                                                  \
95
      TSKEY ts = (ctx)->ptsList != NULL ? GET_TS_DATA(ctx, i) : 0;       \
96
      UPDATE_DATA(ctx, val, d[i], num, sign, ts);                        \
97
    }                                                                    \
98
  } while (0)
99

100
#define LIST_ADD_N(_res, _col, _start, _rows, _t, numOfElem)             \
101
  do {                                                                   \
102
    _t* d = (_t*)(_col->pData);                                          \
103
    for (int32_t i = (_start); i < (_rows) + (_start); ++i) {            \
104
      if (((_col)->hasNull) && colDataIsNull_f((_col)->nullbitmap, i)) { \
105
        continue;                                                        \
106
      };                                                                 \
107
      (_res) += (d)[i];                                                  \
108
      (numOfElem)++;                                                     \
109
    }                                                                    \
110
  } while (0)
111

112
#define LIST_SUB_N(_res, _col, _start, _rows, _t, numOfElem)             \
113
  do {                                                                   \
114
    _t* d = (_t*)(_col->pData);                                          \
115
    for (int32_t i = (_start); i < (_rows) + (_start); ++i) {            \
116
      if (((_col)->hasNull) && colDataIsNull_f((_col)->nullbitmap, i)) { \
117
        continue;                                                        \
118
      };                                                                 \
119
      (_res) -= (d)[i];                                                  \
120
      (numOfElem)++;                                                     \
121
    }                                                                    \
122
  } while (0)
123

124
//#define LIST_AVG_N(sumT, T)                                               \
125
//  do {                                                                    \
126
//    T* plist = (T*)pCol->pData;                                           \
127
//    for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) { \
128
//      if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {        \
129
//        continue;                                                         \
130
//      }                                                                   \
131
//                                                                          \
132
//      numOfElem += 1;                                                     \
133
//      pAvgRes->count -= 1;                                                \
134
//      sumT -= plist[i];                                                   \
135
//    }                                                                     \
136
//  } while (0)
137

138
#define LIST_STDDEV_SUB_N(sumT, T)                                 \
139
  do {                                                             \
140
    T* plist = (T*)pCol->pData;                                    \
141
    for (int32_t i = start; i < numOfRows + start; ++i) {          \
142
      if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) { \
143
        continue;                                                  \
144
      }                                                            \
145
      numOfElem += 1;                                              \
146
      pStddevRes->count -= 1;                                      \
147
      sumT -= plist[i];                                            \
148
      pStddevRes->quadraticISum -= (int64_t)(plist[i] * plist[i]); \
149
    }                                                              \
150
  } while (0)
151

152
#define LEASTSQR_CAL(p, x, y, index, step) \
153
  do {                                     \
154
    (p)[0][0] += (double)(x) * (x);        \
155
    (p)[0][1] += (double)(x);              \
156
    (p)[0][2] += (double)(x) * (y)[index]; \
157
    (p)[1][2] += (y)[index];               \
158
    (x) += step;                           \
159
  } while (0)
160

161
#define STATE_COMP(_op, _lval, _param) STATE_COMP_IMPL(_op, _lval, GET_STATE_VAL(_param))
162

163
#define GET_STATE_VAL(param) ((param.nType == TSDB_DATA_TYPE_BIGINT) ? (param.i) : (param.d))
164

165
#define STATE_COMP_IMPL(_op, _lval, _rval) \
166
  do {                                     \
167
    switch (_op) {                         \
168
      case STATE_OPER_LT:                  \
169
        return ((_lval) < (_rval));        \
170
        break;                             \
171
      case STATE_OPER_GT:                  \
172
        return ((_lval) > (_rval));        \
173
        break;                             \
174
      case STATE_OPER_LE:                  \
175
        return ((_lval) <= (_rval));       \
176
        break;                             \
177
      case STATE_OPER_GE:                  \
178
        return ((_lval) >= (_rval));       \
179
        break;                             \
180
      case STATE_OPER_NE:                  \
181
        return ((_lval) != (_rval));       \
182
        break;                             \
183
      case STATE_OPER_EQ:                  \
184
        return ((_lval) == (_rval));       \
185
        break;                             \
186
      default:                             \
187
        break;                             \
188
    }                                      \
189
  } while (0)
190

191
#define INIT_INTP_POINT(_p, _k, _v) \
192
  do {                              \
193
    (_p).key = (_k);                \
194
    (_p).val = (_v);                \
195
  } while (0)
196

197
void funcInputUpdate(SqlFunctionCtx* pCtx) {
458,281✔
198
  SFuncInputRowIter* pIter = &pCtx->rowIter;
458,281✔
199

200
  if (!pCtx->bInputFinished) {
458,281!
201
    pIter->pInput = &pCtx->input;
458,287✔
202
    pIter->tsList = (TSKEY*)pIter->pInput->pPTS->pData;
458,287✔
203
    pIter->pDataCol = pIter->pInput->pData[0];
458,287✔
204
    pIter->pPkCol = pIter->pInput->pPrimaryKey;
458,287✔
205
    pIter->rowIndex = pIter->pInput->startRowIndex;
458,287✔
206
    pIter->inputEndIndex = pIter->rowIndex + pIter->pInput->numOfRows - 1;
458,287✔
207
    pIter->pSrcBlock = pCtx->pSrcBlock;
458,287✔
208
    if (!pIter->hasGroupId || pIter->groupId != pIter->pSrcBlock->info.id.groupId) {
458,287✔
209
      pIter->hasGroupId = true;
68,438✔
210
      pIter->groupId = pIter->pSrcBlock->info.id.groupId;
68,438✔
211
      pIter->hasPrev = false;
68,438✔
212
    }
213
  } else {
214
    pIter->finalRow = true;
×
215
  }
216
}
458,281✔
217

218
int32_t funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow, bool *res) {
×
219
  if (pIter->finalRow) {
×
220
    if (pIter->hasPrev) {
×
221
      pRow->ts = pIter->prevBlockTsEnd;
×
222
      pRow->isDataNull = pIter->prevIsDataNull;
×
223
      pRow->pData = pIter->pPrevData;
×
224
      pRow->block = pIter->pPrevRowBlock;
×
225
      pRow->rowIndex = 0;
×
226
      
227
      pIter->hasPrev = false;
×
228
      *res = true;
×
229
      return TSDB_CODE_SUCCESS;
×
230
    } else {
231
      *res = false;
×
232
      return TSDB_CODE_SUCCESS;
×
233
    }
234
  }
235
  if (pIter->hasPrev) {
×
236
    if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) {
×
237
      blockDataDestroy(pIter->pPrevRowBlock);
×
238
      int32_t code = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1, &pIter->pPrevRowBlock);
×
239
      if (code) {
×
240
        return code;
×
241
      }
242

243
      pIter->prevIsDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, pIter->inputEndIndex);
×
244

245
      pIter->pPrevData = taosMemoryMalloc(pIter->pDataCol->info.bytes);
×
246
      if (NULL == pIter->pPrevData) {
×
247
        qError("out of memory when function get input row.");
×
248
        return terrno;
×
249
      }
250
      char* srcData = colDataGetData(pIter->pDataCol, pIter->inputEndIndex);
×
251
      (void)memcpy(pIter->pPrevData, srcData, pIter->pDataCol->info.bytes);
×
252

253
      pIter->pPrevPk = taosMemoryMalloc(pIter->pPkCol->info.bytes);
×
254
      if (NULL == pIter->pPrevPk) {
×
255
        qError("out of memory when function get input row.");
×
256
        taosMemoryFree(pIter->pPrevData);
×
257
        return terrno;
×
258
      }
259
      char* pkData = colDataGetData(pIter->pPkCol, pIter->inputEndIndex);
×
260
      (void)memcpy(pIter->pPrevPk, pkData, pIter->pPkCol->info.bytes);
×
261

262
      code = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1, &pIter->pPrevRowBlock);
×
263
      pIter->hasPrev = true;
×
264
      *res = false;
×
265
      return code;
×
266
    } else {
267
      int32_t idx = pIter->rowIndex;
×
268
      while (pIter->tsList[idx] == pIter->prevBlockTsEnd) {
×
269
        ++idx;
×
270
      }
271
      pRow->ts = pIter->prevBlockTsEnd;
×
272
      if (idx == pIter->pInput->startRowIndex) {
×
273
        pRow->isDataNull = pIter->prevIsDataNull;
×
274
        pRow->pData = pIter->pPrevData;
×
275
        pRow->block = pIter->pPrevRowBlock;
×
276
        pRow->rowIndex = 0;
×
277
      } else {
278
        pRow->ts = pIter->tsList[idx - 1];
×
279
        pRow->isDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, idx - 1);
×
280
        pRow->pData = colDataGetData(pIter->pDataCol, idx - 1);
×
281
        pRow->pPk = colDataGetData(pIter->pPkCol, idx - 1);
×
282
        pRow->block = pIter->pSrcBlock;
×
283
        pRow->rowIndex = idx - 1;
×
284
      }
285
      pIter->hasPrev = false;
×
286
      pIter->rowIndex = idx;
×
287
      *res = true;
×
288
      return TSDB_CODE_SUCCESS;
×
289
    }
290
  } else {
291
    TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex];
×
292
    if (pIter->tsList[pIter->rowIndex] != tsEnd) {
×
293
      int32_t idx = pIter->rowIndex;
×
294
      while (pIter->tsList[idx + 1] == pIter->tsList[pIter->rowIndex]) {
×
295
        ++idx;
×
296
      }
297
      pRow->ts = pIter->tsList[idx];
×
298
      pRow->isDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, idx);
×
299
      pRow->pData = colDataGetData(pIter->pDataCol, idx);
×
300
      pRow->pPk = colDataGetData(pIter->pPkCol, idx);
×
301
      pRow->block = pIter->pSrcBlock;
×
302

303
      pIter->rowIndex = idx + 1;
×
304
      *res = true;
×
305
      return TSDB_CODE_SUCCESS;
×
306
    } else {
307
      pIter->hasPrev = true;
×
308
      pIter->prevBlockTsEnd = tsEnd;
×
309
      pIter->prevIsDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, pIter->inputEndIndex);
×
310
      pIter->pPrevData = taosMemoryMalloc(pIter->pDataCol->info.bytes);
×
311
      if (NULL == pIter->pPrevData) {
×
312
        qError("out of memory when function get input row.");
×
313
        return terrno;
×
314
      }
315
      (void)memcpy(pIter->pPrevData, colDataGetData(pIter->pDataCol, pIter->inputEndIndex), pIter->pDataCol->info.bytes);
×
316
      pIter->pPrevPk = taosMemoryMalloc(pIter->pPkCol->info.bytes);
×
317
      if (NULL == pIter->pPrevPk) {
×
318
        qError("out of memory when function get input row.");
×
319
        taosMemoryFree(pIter->pPrevData);
×
320
        return terrno;
×
321
      }
322
      (void)memcpy(pIter->pPrevPk, colDataGetData(pIter->pPkCol, pIter->inputEndIndex), pIter->pPkCol->info.bytes);
×
323

324
      int32_t code = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1, &pIter->pPrevRowBlock);
×
325
      *res = false;
×
326
      return code;
×
327
    }
328
  }
329
}
330

331
static void forwardToNextDiffTsRow(SFuncInputRowIter* pIter, int32_t rowIndex) {
28,232✔
332
  int32_t idx = rowIndex + 1;
28,232✔
333
  while (idx <= pIter->inputEndIndex && pIter->tsList[idx] == pIter->tsList[rowIndex]) {
380,886!
334
    ++idx;
352,654✔
335
  }
336
  pIter->rowIndex = idx;
28,232✔
337
}
28,232✔
338

339
static void setInputRowInfo(SFuncInputRow* pRow, SFuncInputRowIter* pIter, int32_t rowIndex, bool setPk) {
32,172,060✔
340
  pRow->ts = pIter->tsList[rowIndex];
32,172,060✔
341
  pRow->ts = pIter->tsList[rowIndex];
32,172,060✔
342
  pRow->isDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, rowIndex);
32,172,060✔
343
  pRow->pData = colDataGetData(pIter->pDataCol, rowIndex);
32,172,060!
344
  pRow->pPk = setPk? colDataGetData(pIter->pPkCol, rowIndex):NULL;
32,172,060!
345
  pRow->block = pIter->pSrcBlock;
32,172,060✔
346
  pRow->rowIndex = rowIndex;
32,172,060✔
347
}
32,172,060✔
348

349
bool funcInputGetNextRowAscPk(SFuncInputRowIter *pIter, SFuncInputRow* pRow) {
35,662✔
350
  if (pIter->hasPrev) {
35,662✔
351
    if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) {
2,304!
352
      pIter->hasPrev = true;
×
353
      return false;
×
354
    } else {
355
      int32_t idx = pIter->rowIndex;
2,304✔
356
      while (pIter->tsList[idx] == pIter->prevBlockTsEnd) {
2,304!
357
        ++idx;
×
358
      }
359

360
      pIter->hasPrev = false;
2,304✔
361
      setInputRowInfo(pRow, pIter, idx, true);
2,304✔
362
      forwardToNextDiffTsRow(pIter, idx);
2,304✔
363
      return true;
2,304✔
364
    }
365
  } else {
366
    if (pIter->rowIndex <= pIter->inputEndIndex) { 
33,358✔
367
      setInputRowInfo(pRow, pIter, pIter->rowIndex, true);
29,648✔
368

369
      TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex];
29,648✔
370
      if (pIter->tsList[pIter->rowIndex] != tsEnd) {
29,648✔
371
        forwardToNextDiffTsRow(pIter, pIter->rowIndex);
25,928✔
372
      } else {
373
        pIter->rowIndex = pIter->inputEndIndex + 1;
3,720✔
374
      }
375
      return true;
29,648✔
376
    } else {
377
      TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex];
3,710✔
378
      pIter->hasPrev = true;
3,710✔
379
      pIter->prevBlockTsEnd = tsEnd;
3,710✔
380
      return false;
3,710✔
381
    }
382
  }
383
}
384

385
bool funcInputGetNextRowNoPk(SFuncInputRowIter *pIter, SFuncInputRow* pRow) {
32,605,711✔
386
  if (pIter->rowIndex <= pIter->inputEndIndex) {
32,605,711✔
387
    setInputRowInfo(pRow, pIter, pIter->rowIndex, false);
32,139,340✔
388
    ++pIter->rowIndex;
32,140,648✔
389
    return true;    
32,140,648✔
390
  } else {
391
    return false;    
466,371✔
392
  }
393
}
394

395
int32_t funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, bool *res) {
32,641,567✔
396
  SFuncInputRowIter* pIter = &pCtx->rowIter;
32,641,567✔
397
  if (pCtx->hasPrimaryKey) {
32,641,567✔
398
    if (pCtx->order == TSDB_ORDER_ASC) {
35,662!
399
      *res = funcInputGetNextRowAscPk(pIter, pRow);
35,662✔
400
      return TSDB_CODE_SUCCESS;
35,662✔
401
    } else {
402
      return funcInputGetNextRowDescPk(pIter, pRow, res);
×
403
    }
404
  } else {
405
    *res = funcInputGetNextRowNoPk(pIter, pRow);
32,605,905✔
406
    return TSDB_CODE_SUCCESS;
32,607,241✔
407
  }
408
  return TSDB_CODE_SUCCESS;
409
}
410

411
// This function append the selectivity to subsidiaries function context directly, without fetching data
412
// from intermediate disk based buf page
413
int32_t appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t rowIndex, int32_t pos) {
30,193,875✔
414
  if (pCtx->subsidiaries.num <= 0) {
30,193,875!
415
    return TSDB_CODE_SUCCESS;
×
416
  }
417

418
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
86,093,630✔
419
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
55,899,755✔
420

421
    // get data from source col
422
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
55,899,755✔
423
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
55,899,755✔
424

425
    SColumnInfoData* pSrcCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
55,899,755✔
426
    if (NULL == pSrcCol) {
55,899,755!
427
      return TSDB_CODE_OUT_OF_RANGE;
×
428
    }
429

430
    char* pData = colDataGetData(pSrcCol, rowIndex);
55,899,755!
431

432
    // append to dest col
433
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
55,899,755✔
434

435
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
55,899,755✔
436
    if (NULL == pDstCol) {
55,899,755!
437
      return TSDB_CODE_OUT_OF_RANGE;
×
438
    }
439
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
111,799,510✔
440
      colDataSetNULL(pDstCol, pos);
2,580,095✔
441
    } else {
442
      int32_t code = colDataSetVal(pDstCol, pos, pData, false);
53,319,660✔
443
      if (TSDB_CODE_SUCCESS != code) {
53,319,660!
444
        return code;
×
445
      }
446
    }
447
  }
448
  return TSDB_CODE_SUCCESS;
30,193,875✔
449
}
450

451
bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex, int32_t* nextFrom);
452

453
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst);
454

455
int32_t functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
319,982,968✔
456
  if (pResultInfo->initialized) {
319,982,968✔
457
    return TSDB_CODE_SUCCESS;  // already initialized
15,952✔
458
  }
459

460
  if (pCtx->pOutput != NULL) {
319,967,016!
461
    (void)memset(pCtx->pOutput, 0, (size_t)pCtx->resDataInfo.bytes);
×
462
  }
463

464
  initResultRowEntry(pResultInfo, pCtx->resDataInfo.interBufSize);
319,967,016✔
465
  return TSDB_CODE_SUCCESS;
319,967,016✔
466
}
467

468
int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
146,335,266✔
469
  int32_t          code = TSDB_CODE_SUCCESS;
146,335,266✔
470
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
146,335,266✔
471
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
146,335,266✔
472
  if (NULL == pCol) {
145,789,856!
473
    return TSDB_CODE_OUT_OF_RANGE;
×
474
  }
475
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
145,789,856✔
476
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
145,789,856✔
477

478
  char* in = GET_ROWCELL_INTERBUF(pResInfo);
145,789,856✔
479
  code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes);
145,789,856✔
480

481
  return code;
146,969,987✔
482
}
483

484
int32_t firstCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
3✔
485
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
3✔
486
  SFirstLastRes*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
3✔
487
  int32_t              bytes = pDBuf->bytes;
3✔
488

489
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
490
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
491

492
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, true);
3✔
493

494
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
3✔
495
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
3✔
496
  return TSDB_CODE_SUCCESS;
3✔
497
}
498

499
int32_t functionFinalizeWithResultBuf(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, char* finalResult) {
1,084✔
500
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
1,084✔
501
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
1,084✔
502
  if (NULL == pCol) {
1,084!
503
    return TSDB_CODE_OUT_OF_RANGE;
×
504
  }
505
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,084✔
506
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
1,084✔
507

508
  char* in = finalResult;
1,084✔
509
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes);
1,084✔
510

511
  return code;
1,084✔
512
}
513

514
EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
108,517✔
515
  SNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
108,517✔
516
  if (QUERY_NODE_COLUMN == nodeType(pParam) && PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pParam)->colId) {
108,517✔
517
    return FUNC_DATA_REQUIRED_NOT_LOAD;
53,966✔
518
  }
519
  return FUNC_DATA_REQUIRED_SMA_LOAD;
54,551✔
520
}
521

522
bool getCountFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
399,521✔
523
  pEnv->calcMemSize = sizeof(int64_t);
399,521✔
524
  return true;
399,521✔
525
}
526

527
static int64_t getNumOfElems(SqlFunctionCtx* pCtx) {
110,498,917✔
528
  int64_t numOfElem = 0;
110,498,917✔
529

530
  /*
531
   * 1. column data missing (schema modified) causes pInputCol->hasNull == true. pInput->colDataSMAIsSet == true;
532
   * 2. for general non-primary key columns, pInputCol->hasNull may be true or false, pInput->colDataSMAIsSet == true;
533
   * 3. for primary key column, pInputCol->hasNull always be false, pInput->colDataSMAIsSet == false;
534
   */
535
  SInputColumnInfoData* pInput = &pCtx->input;
110,498,917✔
536
  SColumnInfoData*      pInputCol = pInput->pData[0];
110,498,917✔
537
  if(1 == pInput->numOfRows && pInput->blankFill) {
110,498,917✔
538
    return 0;
404,900✔
539
  }
540
  if (pInput->colDataSMAIsSet && pInput->totalRows == pInput->numOfRows) {
110,094,017!
541
    numOfElem = pInput->numOfRows - pInput->pColumnDataAgg[0]->numOfNull;
4,991✔
542
  } else {
543
    if (pInputCol->hasNull) {
110,089,026✔
544
      for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
303,475,804✔
545
        if (colDataIsNull(pInputCol, pInput->totalRows, i, NULL)) {
555,681,178!
546
          continue;
4,336,294✔
547
        }
548
        numOfElem += 1;
273,504,295✔
549
      }
550
    } else {
551
      // when counting on the primary time stamp column and no statistics data is presented, use the size value
552
      // directly.
553
      numOfElem = pInput->numOfRows;
84,453,811✔
554
    }
555
  }
556
  return numOfElem;
110,094,017✔
557
}
558

559
/*
560
 * count function does need the finalize, if data is missing, the default value, which is 0, is used
561
 * count function does not use the pCtx->interResBuf to keep the intermediate buffer
562
 */
563
int32_t countFunction(SqlFunctionCtx* pCtx) {
110,526,096✔
564
  int64_t numOfElem = 0;
110,526,096✔
565

566
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
110,526,096✔
567
  SInputColumnInfoData* pInput = &pCtx->input;
110,526,096✔
568

569
  int32_t type = pInput->pData[0]->info.type;
110,526,096✔
570

571
  char* buf = GET_ROWCELL_INTERBUF(pResInfo);
110,526,096✔
572
  if (IS_NULL_TYPE(type)) {
110,526,096✔
573
    // select count(NULL) returns 0
574
    numOfElem = 1;
13,655✔
575
    *((int64_t*)buf) += 0;
13,655✔
576
  } else {
577
    numOfElem = getNumOfElems(pCtx);
110,512,441✔
578
    *((int64_t*)buf) += numOfElem;
110,224,117✔
579
  }
580

581
  if (tsCountAlwaysReturnValue) {
110,237,772!
582
    pResInfo->numOfRes = 1;
110,296,237✔
583
  } else {
584
    SET_VAL(pResInfo, *((int64_t*)buf), 1);
×
585
  }
586

587
  return TSDB_CODE_SUCCESS;
110,237,772✔
588
}
589

590
#ifdef BUILD_NO_CALL
591
int32_t countInvertFunction(SqlFunctionCtx* pCtx) {
592
  int64_t numOfElem = getNumOfElems(pCtx);
593

594
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
595
  char*                buf = GET_ROWCELL_INTERBUF(pResInfo);
596
  *((int64_t*)buf) -= numOfElem;
597

598
  SET_VAL(pResInfo, *((int64_t*)buf), 1);
599
  return TSDB_CODE_SUCCESS;
600
}
601
#endif
602

603
int32_t combineFunction(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
18✔
604
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
18✔
605
  char*                pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
18✔
606

607
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
18✔
608
  char*                pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
18✔
609
  *((int64_t*)pDBuf) += *((int64_t*)pSBuf);
18✔
610

611
  SET_VAL(pDResInfo, *((int64_t*)pDBuf), 1);
18!
612
  return TSDB_CODE_SUCCESS;
18✔
613
}
614

615
int32_t sumFunction(SqlFunctionCtx* pCtx) {
85,003,210✔
616
  int32_t numOfElem = 0;
85,003,210✔
617

618
  // Only the pre-computing information loaded and actual data does not loaded
619
  SInputColumnInfoData* pInput = &pCtx->input;
85,003,210✔
620
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
85,003,210✔
621
  int32_t               type = pInput->pData[0]->info.type;
85,003,210✔
622

623
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
85,003,210✔
624
  pSumRes->type = type;
85,003,210✔
625

626
  if (IS_NULL_TYPE(type)) {
85,003,210✔
627
    numOfElem = 0;
246✔
628
    goto _sum_over;
246✔
629
  }
630

631
  if (pInput->colDataSMAIsSet) {
85,002,964✔
632
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
2,692✔
633

634
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
2,692!
635
      pSumRes->isum += pAgg->sum;
2,692✔
636
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
637
      pSumRes->usum += pAgg->sum;
×
638
    } else if (IS_FLOAT_TYPE(type)) {
×
639
      pSumRes->dsum += GET_DOUBLE_VAL((const char*)&(pAgg->sum));
×
640
    }
641
  } else {  // computing based on the true data block
642
    SColumnInfoData* pCol = pInput->pData[0];
85,000,272✔
643

644
    int32_t start = pInput->startRowIndex;
85,000,272✔
645
    int32_t numOfRows = pInput->numOfRows;
85,000,272✔
646

647
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
85,000,272!
648
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
84,802,339!
649
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int8_t, numOfElem);
1,425,517✔
650
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
84,443,182✔
651
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int16_t, numOfElem);
448,135✔
652
      } else if (type == TSDB_DATA_TYPE_INT) {
84,393,590✔
653
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int32_t, numOfElem);
399,314,766✔
654
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
18,722,368✔
655
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int64_t, numOfElem);
74,266,156✔
656
      }
657
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
197,933!
658
      if (type == TSDB_DATA_TYPE_UTINYINT) {
678✔
659
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint8_t, numOfElem);
130,039!
660
      } else if (type == TSDB_DATA_TYPE_USMALLINT) {
639✔
661
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint16_t, numOfElem);
130,047✔
662
      } else if (type == TSDB_DATA_TYPE_UINT) {
599✔
663
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint32_t, numOfElem);
133,494!
664
      } else if (type == TSDB_DATA_TYPE_UBIGINT) {
377!
665
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint64_t, numOfElem);
134,257✔
666
      }
667
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
197,255✔
668
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, double, numOfElem);
2,328,725✔
669
    } else if (type == TSDB_DATA_TYPE_FLOAT) {
102,109!
670
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, float, numOfElem);
606,961✔
671
    }
672
  }
673

674
  // check for overflow
675
  if (IS_FLOAT_TYPE(type) && (isinf(pSumRes->dsum) || isnan(pSumRes->dsum))) {
85,002,964!
676
    numOfElem = 0;
×
677
  }
678

679
_sum_over:
85,342,103✔
680
  if (numOfElem == 0) {
85,003,210✔
681
    if (tsCountAlwaysReturnValue && pCtx->pExpr->pExpr->_function.pFunctNode->hasOriginalFunc &&
91,512✔
682
        fmIsCountLikeFunc(pCtx->pExpr->pExpr->_function.pFunctNode->originalFuncId)) {
11,628✔
683
      numOfElem = 1;
14✔
684
    }
685
  }
686
  // data in the check operation are all null, not output
687
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
85,003,210✔
688
  return TSDB_CODE_SUCCESS;
85,003,210✔
689
}
690

691
#ifdef BUILD_NO_CALL
692
int32_t sumInvertFunction(SqlFunctionCtx* pCtx) {
693
  int32_t numOfElem = 0;
694

695
  // Only the pre-computing information loaded and actual data does not loaded
696
  SInputColumnInfoData* pInput = &pCtx->input;
697
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
698
  int32_t               type = pInput->pData[0]->info.type;
699

700
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
701

702
  if (pInput->colDataSMAIsSet) {
703
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
704

705
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
706
      pSumRes->isum -= pAgg->sum;
707
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
708
      pSumRes->usum -= pAgg->sum;
709
    } else if (IS_FLOAT_TYPE(type)) {
710
      pSumRes->dsum -= GET_DOUBLE_VAL((const char*)&(pAgg->sum));
711
    }
712
  } else {  // computing based on the true data block
713
    SColumnInfoData* pCol = pInput->pData[0];
714

715
    int32_t start = pInput->startRowIndex;
716
    int32_t numOfRows = pInput->numOfRows;
717

718
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
719
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
720
        LIST_SUB_N(pSumRes->isum, pCol, start, numOfRows, int8_t, numOfElem);
721
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
722
        LIST_SUB_N(pSumRes->isum, pCol, start, numOfRows, int16_t, numOfElem);
723
      } else if (type == TSDB_DATA_TYPE_INT) {
724
        LIST_SUB_N(pSumRes->isum, pCol, start, numOfRows, int32_t, numOfElem);
725
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
726
        LIST_SUB_N(pSumRes->isum, pCol, start, numOfRows, int64_t, numOfElem);
727
      }
728
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
729
      if (type == TSDB_DATA_TYPE_UTINYINT) {
730
        LIST_SUB_N(pSumRes->usum, pCol, start, numOfRows, uint8_t, numOfElem);
731
      } else if (type == TSDB_DATA_TYPE_USMALLINT) {
732
        LIST_SUB_N(pSumRes->usum, pCol, start, numOfRows, uint16_t, numOfElem);
733
      } else if (type == TSDB_DATA_TYPE_UINT) {
734
        LIST_SUB_N(pSumRes->usum, pCol, start, numOfRows, uint32_t, numOfElem);
735
      } else if (type == TSDB_DATA_TYPE_UBIGINT) {
736
        LIST_SUB_N(pSumRes->usum, pCol, start, numOfRows, uint64_t, numOfElem);
737
      }
738
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
739
      LIST_SUB_N(pSumRes->dsum, pCol, start, numOfRows, double, numOfElem);
740
    } else if (type == TSDB_DATA_TYPE_FLOAT) {
741
      LIST_SUB_N(pSumRes->dsum, pCol, start, numOfRows, float, numOfElem);
742
    }
743
  }
744

745
  // data in the check operation are all null, not output
746
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
747
  return TSDB_CODE_SUCCESS;
748
}
749
#endif
750

751
int32_t sumCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
49✔
752
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
49✔
753
  SSumRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
49✔
754

755
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
49✔
756
  SSumRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
49✔
757
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
49✔
758

759
  if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
49!
760
    pDBuf->isum += pSBuf->isum;
49✔
761
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
762
    pDBuf->usum += pSBuf->usum;
×
763
  } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) {
×
764
    pDBuf->dsum += pSBuf->dsum;
×
765
  }
766
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
49✔
767
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
49✔
768
  return TSDB_CODE_SUCCESS;
49✔
769
}
770

771
bool getSumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
149,780✔
772
  pEnv->calcMemSize = sizeof(SSumRes);
149,780✔
773
  return true;
149,780✔
774
}
775

776
EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
165,014✔
777
  return FUNC_DATA_REQUIRED_SMA_LOAD;
165,014✔
778
}
779

780
int32_t minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
29,381,715✔
781
  if (pResultInfo->initialized) {
29,381,715!
782
    return TSDB_CODE_SUCCESS;
×
783
  }
784
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
29,381,715!
785
    return TSDB_CODE_FUNC_SETUP_ERROR;  // not initialized since it has been initialized
×
786
  }
787

788
  SMinmaxResInfo* buf = GET_ROWCELL_INTERBUF(pResultInfo);
29,405,730✔
789
  buf->assign = false;
29,405,730✔
790
  buf->tuplePos.pageId = -1;
29,405,730✔
791

792
  buf->nullTupleSaved = false;
29,405,730✔
793
  buf->nullTuplePos.pageId = -1;
29,405,730✔
794
  buf->str = NULL;
29,405,730✔
795
  return TSDB_CODE_SUCCESS;
29,405,730✔
796
}
797

798
bool getMinmaxFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
172,045✔
799
  pEnv->calcMemSize = sizeof(SMinmaxResInfo);
172,045✔
800
  return true;
172,045✔
801
}
802

803
int32_t minFunction(SqlFunctionCtx* pCtx) {
16,310,257✔
804
  int32_t numOfElems = 0;
16,310,257✔
805
  int32_t code = doMinMaxHelper(pCtx, 1, &numOfElems);
16,310,257✔
806
  if (code != TSDB_CODE_SUCCESS) {
16,380,296!
807
    return code;
×
808
  }
809
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
16,380,296✔
810
  return TSDB_CODE_SUCCESS;
16,380,296✔
811
}
812

813
int32_t maxFunction(SqlFunctionCtx* pCtx) {
16,341,327✔
814
  int32_t numOfElems = 0;
16,341,327✔
815
  int32_t code = doMinMaxHelper(pCtx, 0, &numOfElems);
16,341,327✔
816
  if (code != TSDB_CODE_SUCCESS) {
16,434,104!
817
    return code;
×
818
  }
819
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
16,434,104✔
820
  return TSDB_CODE_SUCCESS;
16,434,104✔
821
}
822

823
static int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex);
824
static int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos,
825
                                   int32_t rowIndex);
826

827
int32_t minmaxFunctionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
30,169,849✔
828
  int32_t code = TSDB_CODE_SUCCESS;
30,169,849✔
829

830
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
30,169,849✔
831
  SMinmaxResInfo*      pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
30,169,849✔
832

833
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
30,169,849✔
834
  int32_t currentRow = pBlock->info.rows;
30,169,849✔
835

836
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
30,169,849✔
837
  if (NULL == pCol) {
30,194,999!
838
    return TSDB_CODE_OUT_OF_RANGE;
×
839
  }
840
  pEntryInfo->isNullRes = (pEntryInfo->numOfRes == 0) ? 1 : 0;
30,194,999✔
841

842
  // NOTE: do nothing change it, for performance issue
843
  if (!pEntryInfo->isNullRes) {
30,194,999✔
844
    switch (pCol->info.type) {
22,919,973!
845
      case TSDB_DATA_TYPE_UBIGINT:
7,428,329✔
846
      case TSDB_DATA_TYPE_BIGINT:
847
        ((int64_t*)pCol->pData)[currentRow] = pRes->v;
7,428,329✔
848
        break;
7,428,329✔
849
      case TSDB_DATA_TYPE_UINT:
15,193,010✔
850
      case TSDB_DATA_TYPE_INT:
851
        colDataSetInt32(pCol, currentRow, (int32_t*)&pRes->v);
15,193,010✔
852
        break;
15,193,010✔
853
      case TSDB_DATA_TYPE_USMALLINT:
59,761✔
854
      case TSDB_DATA_TYPE_SMALLINT:
855
        colDataSetInt16(pCol, currentRow, (int16_t*)&pRes->v);
59,761✔
856
        break;
59,761✔
857
      case TSDB_DATA_TYPE_BOOL:
40,302✔
858
      case TSDB_DATA_TYPE_UTINYINT:
859
      case TSDB_DATA_TYPE_TINYINT:
860
        colDataSetInt8(pCol, currentRow, (int8_t*)&pRes->v);
40,302✔
861
        break;
40,302✔
862
      case TSDB_DATA_TYPE_DOUBLE:
214,165✔
863
        colDataSetDouble(pCol, currentRow, (double*)&pRes->v);
214,165✔
864
        break;
214,165✔
865
      case TSDB_DATA_TYPE_FLOAT: {
3,315✔
866
        float v = GET_FLOAT_VAL(&pRes->v);
3,315✔
867
        colDataSetFloat(pCol, currentRow, &v);
3,315✔
868
        break;
3,315✔
869
      }
870
      case TSDB_DATA_TYPE_VARBINARY:
400✔
871
      case TSDB_DATA_TYPE_VARCHAR:
872
      case TSDB_DATA_TYPE_NCHAR: {
873
        code = colDataSetVal(pCol, currentRow, pRes->str, false);
400✔
874
        if (TSDB_CODE_SUCCESS != code) {
400!
875
          return code;
×
876
        }
877
        break;
400✔
878
      }
879
    }
880
  } else {
881
    colDataSetNULL(pCol, currentRow);
7,275,026!
882
  }
883

884
  taosMemoryFreeClear(pRes->str);
30,194,999✔
885
  if (pCtx->subsidiaries.num > 0) {
30,194,999✔
886
    if (pEntryInfo->numOfRes > 0) {
112,582✔
887
      code = setSelectivityValue(pCtx, pBlock, &pRes->tuplePos, currentRow);
109,792✔
888
    } else {
889
      code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, currentRow);
2,790✔
890
    }
891
  }
892

893
  return code;
30,195,835✔
894
}
895

896
#ifdef BUILD_NO_CALL
897
int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex) {
898
  if (pCtx->subsidiaries.num <= 0) {
899
    return TSDB_CODE_SUCCESS;
900
  }
901

902
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
903
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
904
    int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
905

906
    SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
907
    colDataSetNULL(pDstCol, rowIndex);
908
  }
909

910
  return TSDB_CODE_SUCCESS;
911
}
912
#endif
913

914
int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos, int32_t rowIndex) {
35,277,122✔
915
  if (pCtx->subsidiaries.num <= 0) {
35,277,122✔
916
    return TSDB_CODE_SUCCESS;
35,013,823✔
917
  }
918

919
  if ((pCtx->saveHandle.pBuf != NULL && pTuplePos->pageId != -1) ||
263,299!
920
      (pCtx->saveHandle.pState && pTuplePos->streamTupleKey.ts > 0)) {
91,289!
921
    int32_t     numOfCols = pCtx->subsidiaries.num;
263,309✔
922
    char* p = NULL;
263,309✔
923
    int32_t code = loadTupleData(pCtx, pTuplePos, &p);
263,309✔
924
    if (p == NULL || TSDB_CODE_SUCCESS != code) {
266,063!
925
      qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(),
×
926
             pTuplePos->streamTupleKey.groupId, pTuplePos->streamTupleKey.ts);
927
      return TSDB_CODE_NOT_FOUND;
×
928
    }
929

930
    bool* nullList = (bool*)p;
266,063✔
931
    char* pStart = (char*)(nullList + numOfCols * sizeof(bool));
266,063✔
932

933
    // todo set the offset value to optimize the performance.
934
    for (int32_t j = 0; j < numOfCols; ++j) {
583,331✔
935
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
317,224✔
936
      int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
317,224✔
937

938
      // group_key function has its own process function
939
      // do not process there
940
      if (fmIsGroupKeyFunc(pc->functionId)) {
317,224✔
941
        continue;
35,340✔
942
      }
943

944
      SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
281,891✔
945
      if (NULL == pDstCol) {
281,879!
946
        return TSDB_CODE_OUT_OF_RANGE;
×
947
      }
948
      if (nullList[j]) {
281,879✔
949
        colDataSetNULL(pDstCol, rowIndex);
336!
950
      } else {
951
        code = colDataSetVal(pDstCol, rowIndex, pStart, false);
281,543✔
952
        if (TSDB_CODE_SUCCESS != code) {
281,592!
953
          return code;
×
954
        }
955
      }
956
      pStart += pDstCol->info.bytes;
281,928✔
957
    }
958
  }
959

960
  return TSDB_CODE_SUCCESS;
266,097✔
961
}
962

963
// This function append the selectivity to subsidiaries function context directly, without fetching data
964
// from intermediate disk based buf page
965
int32_t appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) {
4,396✔
966
  if (pCtx->subsidiaries.num <= 0) {
4,396!
967
    return TSDB_CODE_SUCCESS;
×
968
  }
969

970
  int32_t code = TSDB_CODE_SUCCESS;
4,396✔
971
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
11,746✔
972
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
7,350✔
973

974
    // get data from source col
975
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
7,350✔
976
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
7,350✔
977

978
    SColumnInfoData* pSrcCol = taosArrayGet(pCtx->pSrcBlock->pDataBlock, srcSlotId);
7,350✔
979
    if (NULL == pSrcCol) {
7,350!
980
      return TSDB_CODE_OUT_OF_RANGE;
×
981
    }
982

983
    char* pData = colDataGetData(pSrcCol, rowIndex);
7,350!
984

985
    // append to dest col
986
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
7,350✔
987

988
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
7,350✔
989
    if (NULL == pDstCol) {
7,350!
990
      return TSDB_CODE_OUT_OF_RANGE;
×
991
    }
992

993
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
14,700✔
994
      colDataSetNULL(pDstCol, pos);
560✔
995
    } else {
996
      code = colDataSetVal(pDstCol, pos, pData, false);
6,790✔
997
      if (TSDB_CODE_SUCCESS != code) {
6,790!
998
        return code;
×
999
      }
1000
    }
1001
  }
1002
  return code;
4,396✔
1003
}
1004

1005
void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; }
33✔
1006

1007
#define COMPARE_MINMAX_DATA(type) (( (*(type*)&pDBuf->v) < (*(type*)&pSBuf->v) ) ^ isMinFunc)
1008
int32_t minMaxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx, int32_t isMinFunc) {
59✔
1009
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
59✔
1010
  SMinmaxResInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
59✔
1011

1012
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
59✔
1013
  SMinmaxResInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
59✔
1014
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
59✔
1015

1016
  switch (type) {
59!
1017
    case TSDB_DATA_TYPE_DOUBLE:
3✔
1018
    case TSDB_DATA_TYPE_UBIGINT:
1019
    case TSDB_DATA_TYPE_BIGINT:
1020
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int64_t) || !pDBuf->assign)) {
3!
1021
        pDBuf->v = pSBuf->v;
1✔
1022
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
1✔
1023
        pDBuf->assign = true;
1✔
1024
      }
1025
      break;
3✔
1026
    case TSDB_DATA_TYPE_UINT:
56✔
1027
    case TSDB_DATA_TYPE_INT:
1028
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int32_t) || !pDBuf->assign)) {
56!
1029
        pDBuf->v = pSBuf->v;
32✔
1030
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
32✔
1031
        pDBuf->assign = true;
32✔
1032
      }
1033
      break;
56✔
1034
    case TSDB_DATA_TYPE_USMALLINT:
×
1035
    case TSDB_DATA_TYPE_SMALLINT:
1036
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int16_t) || !pDBuf->assign)) {
×
1037
        pDBuf->v = pSBuf->v;
×
1038
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1039
        pDBuf->assign = true;
×
1040
      }
1041
      break;
×
1042
    case TSDB_DATA_TYPE_BOOL:
×
1043
    case TSDB_DATA_TYPE_UTINYINT:
1044
    case TSDB_DATA_TYPE_TINYINT:
1045
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int8_t) || !pDBuf->assign)) {
×
1046
        pDBuf->v = pSBuf->v;
×
1047
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1048
        pDBuf->assign = true;
×
1049
      }
1050
      break;
×
1051
    case TSDB_DATA_TYPE_FLOAT: {
×
1052
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(double) || !pDBuf->assign)) {
×
1053
        pDBuf->v = pSBuf->v;
×
1054
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1055
        pDBuf->assign = true;
×
1056
      }
1057
      break;
×
1058
    }
1059
    default:
×
1060
      if (pSBuf->assign && (strcmp((char*)&pDBuf->v, (char*)&pSBuf->v) || !pDBuf->assign)) {
×
1061
        pDBuf->v = pSBuf->v;
×
1062
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1063
        pDBuf->assign = true;
×
1064
      }
1065
      break;
×
1066
  }
1067
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
59✔
1068
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
59✔
1069
  return TSDB_CODE_SUCCESS;
59✔
1070
}
1071

1072
int32_t minCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
11✔
1073
  return minMaxCombine(pDestCtx, pSourceCtx, 1);
11✔
1074
}
1075
int32_t maxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
48✔
1076
  return minMaxCombine(pDestCtx, pSourceCtx, 0);
48✔
1077
}
1078

1079
int32_t getStdInfoSize() { return (int32_t)sizeof(SStdRes); }
5,727✔
1080

1081
bool getStdFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
32,591✔
1082
  pEnv->calcMemSize = sizeof(SStdRes);
32,591✔
1083
  return true;
32,591✔
1084
}
1085

1086
int32_t stdFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
199,514✔
1087
  if (pResultInfo->initialized) {
199,514!
1088
    return TSDB_CODE_SUCCESS;
×
1089
  }
1090
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
199,514!
1091
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1092
  }
1093

1094
  SStdRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
199,515✔
1095
  (void)memset(pRes, 0, sizeof(SStdRes));
199,515✔
1096
  return TSDB_CODE_SUCCESS;
199,515✔
1097
}
1098

1099
int32_t stdFunction(SqlFunctionCtx* pCtx) {
207,117✔
1100
  int32_t numOfElem = 0;
207,117✔
1101

1102
  // Only the pre-computing information loaded and actual data does not loaded
1103
  SInputColumnInfoData* pInput = &pCtx->input;
207,117✔
1104
  int32_t               type = pInput->pData[0]->info.type;
207,117✔
1105

1106
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
207,117✔
1107
  pStdRes->type = type;
207,117✔
1108

1109
  // computing based on the true data block
1110
  SColumnInfoData* pCol = pInput->pData[0];
207,117✔
1111

1112
  int32_t start = pInput->startRowIndex;
207,117✔
1113
  int32_t numOfRows = pInput->numOfRows;
207,117✔
1114

1115
  if (IS_NULL_TYPE(type)) {
207,117✔
1116
    numOfElem = 0;
29✔
1117
    goto _stddev_over;
29✔
1118
  }
1119

1120
  switch (type) {
207,088✔
1121
    case TSDB_DATA_TYPE_TINYINT: {
11,233✔
1122
      int8_t* plist = (int8_t*)pCol->pData;
11,233✔
1123
      for (int32_t i = start; i < numOfRows + start; ++i) {
148,137✔
1124
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
136,904✔
1125
          continue;
19,500✔
1126
        }
1127

1128
        numOfElem += 1;
117,404✔
1129
        pStdRes->count += 1;
117,404✔
1130
        pStdRes->isum += plist[i];
117,404✔
1131
        pStdRes->quadraticISum += plist[i] * plist[i];
117,404✔
1132
      }
1133

1134
      break;
11,233✔
1135
    }
1136

1137
    case TSDB_DATA_TYPE_SMALLINT: {
87,856✔
1138
      int16_t* plist = (int16_t*)pCol->pData;
87,856✔
1139
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
403,655✔
1140
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
315,799✔
1141
          continue;
30,094✔
1142
        }
1143

1144
        numOfElem += 1;
285,705✔
1145
        pStdRes->count += 1;
285,705✔
1146
        pStdRes->isum += plist[i];
285,705✔
1147
        pStdRes->quadraticISum += plist[i] * plist[i];
285,705✔
1148
      }
1149
      break;
87,856✔
1150
    }
1151

1152
    case TSDB_DATA_TYPE_INT: {
15,247✔
1153
      int32_t* plist = (int32_t*)pCol->pData;
15,247✔
1154
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
252,732✔
1155
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
237,485✔
1156
          continue;
101,586✔
1157
        }
1158

1159
        numOfElem += 1;
135,899✔
1160
        pStdRes->count += 1;
135,899✔
1161
        pStdRes->isum += plist[i];
135,899✔
1162
        pStdRes->quadraticISum += plist[i] * plist[i];
135,899✔
1163
      }
1164

1165
      break;
15,247✔
1166
    }
1167

1168
    case TSDB_DATA_TYPE_BIGINT: {
26,115✔
1169
      int64_t* plist = (int64_t*)pCol->pData;
26,115✔
1170
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
908,822✔
1171
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
882,707✔
1172
          continue;
93,321✔
1173
        }
1174

1175
        numOfElem += 1;
789,386✔
1176
        pStdRes->count += 1;
789,386✔
1177
        pStdRes->isum += plist[i];
789,386✔
1178
        pStdRes->quadraticISum += plist[i] * plist[i];
789,386✔
1179
      }
1180
      break;
26,115✔
1181
    }
1182

1183
    case TSDB_DATA_TYPE_UTINYINT: {
25✔
1184
      uint8_t* plist = (uint8_t*)pCol->pData;
25✔
1185
      for (int32_t i = start; i < numOfRows + start; ++i) {
80,032✔
1186
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,007!
1187
          continue;
4✔
1188
        }
1189

1190
        numOfElem += 1;
80,003✔
1191
        pStdRes->count += 1;
80,003✔
1192
        pStdRes->usum += plist[i];
80,003✔
1193
        pStdRes->quadraticUSum += plist[i] * plist[i];
80,003✔
1194
      }
1195

1196
      break;
25✔
1197
    }
1198

1199
    case TSDB_DATA_TYPE_USMALLINT: {
24✔
1200
      uint16_t* plist = (uint16_t*)pCol->pData;
24✔
1201
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,024✔
1202
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,000!
1203
          continue;
×
1204
        }
1205

1206
        numOfElem += 1;
80,000✔
1207
        pStdRes->count += 1;
80,000✔
1208
        pStdRes->usum += plist[i];
80,000✔
1209
        pStdRes->quadraticUSum += plist[i] * plist[i];
80,000✔
1210
      }
1211
      break;
24✔
1212
    }
1213

1214
    case TSDB_DATA_TYPE_UINT: {
25✔
1215
      uint32_t* plist = (uint32_t*)pCol->pData;
25✔
1216
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,030✔
1217
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,005!
1218
          continue;
×
1219
        }
1220

1221
        numOfElem += 1;
80,005✔
1222
        pStdRes->count += 1;
80,005✔
1223
        pStdRes->usum += plist[i];
80,005✔
1224
        pStdRes->quadraticUSum += plist[i] * plist[i];
80,005✔
1225
      }
1226

1227
      break;
25✔
1228
    }
1229

1230
    case TSDB_DATA_TYPE_UBIGINT: {
24✔
1231
      uint64_t* plist = (uint64_t*)pCol->pData;
24✔
1232
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,024✔
1233
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,000!
1234
          continue;
×
1235
        }
1236

1237
        numOfElem += 1;
80,000✔
1238
        pStdRes->count += 1;
80,000✔
1239
        pStdRes->usum += plist[i];
80,000✔
1240
        pStdRes->quadraticUSum += plist[i] * plist[i];
80,000✔
1241
      }
1242
      break;
24✔
1243
    }
1244

1245
    case TSDB_DATA_TYPE_FLOAT: {
15,573✔
1246
      float* plist = (float*)pCol->pData;
15,573✔
1247
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
740,871✔
1248
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
725,298✔
1249
          continue;
107,660✔
1250
        }
1251

1252
        numOfElem += 1;
617,638✔
1253
        pStdRes->count += 1;
617,638✔
1254
        pStdRes->dsum += plist[i];
617,638✔
1255
        pStdRes->quadraticDSum += plist[i] * plist[i];
617,638✔
1256
      }
1257
      break;
15,573✔
1258
    }
1259

1260
    case TSDB_DATA_TYPE_DOUBLE: {
50,964✔
1261
      double* plist = (double*)pCol->pData;
50,964✔
1262
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
2,042,718✔
1263
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,991,754✔
1264
          continue;
871,492✔
1265
        }
1266

1267
        numOfElem += 1;
1,120,262✔
1268
        pStdRes->count += 1;
1,120,262✔
1269
        pStdRes->dsum += plist[i];
1,120,262✔
1270
        pStdRes->quadraticDSum += plist[i] * plist[i];
1,120,262✔
1271
      }
1272
      break;
50,964✔
1273
    }
1274

1275
    default:
2✔
1276
      break;
2✔
1277
  }
1278

1279
_stddev_over:
207,117✔
1280
  // data in the check operation are all null, not output
1281
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
207,117✔
1282
  return TSDB_CODE_SUCCESS;
207,117✔
1283
}
1284

1285
static void stdTransferInfo(SStdRes* pInput, SStdRes* pOutput) {
4,248✔
1286
  if (IS_NULL_TYPE(pInput->type)) {
4,248!
1287
    return;
×
1288
  }
1289
  pOutput->type = pInput->type;
4,248✔
1290
  if (IS_SIGNED_NUMERIC_TYPE(pOutput->type)) {
4,248!
1291
    pOutput->quadraticISum += pInput->quadraticISum;
4,130✔
1292
    pOutput->isum += pInput->isum;
4,130✔
1293
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pOutput->type)) {
118!
1294
    pOutput->quadraticUSum += pInput->quadraticUSum;
1✔
1295
    pOutput->usum += pInput->usum;
1✔
1296
  } else {
1297
    pOutput->quadraticDSum += pInput->quadraticDSum;
117✔
1298
    pOutput->dsum += pInput->dsum;
117✔
1299
  }
1300

1301
  pOutput->count += pInput->count;
4,248✔
1302
}
1303

1304
int32_t stdFunctionMerge(SqlFunctionCtx* pCtx) {
4,239✔
1305
  SInputColumnInfoData* pInput = &pCtx->input;
4,239✔
1306
  SColumnInfoData*      pCol = pInput->pData[0];
4,239✔
1307

1308
  if (IS_NULL_TYPE(pCol->info.type)) {
4,239!
1309
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
1310
    return TSDB_CODE_SUCCESS;
×
1311
  }
1312

1313
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
4,239!
1314
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1315
  }
1316

1317
  SStdRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
4,239✔
1318

1319
  for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
8,484✔
1320
    if (colDataIsNull_s(pCol, i)) continue;
8,490!
1321
    char*       data = colDataGetData(pCol, i);
4,245!
1322
    SStdRes*    pInputInfo = (SStdRes*)varDataVal(data);
4,245✔
1323
    stdTransferInfo(pInputInfo, pInfo);
4,245✔
1324
  }
1325

1326
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
4,239✔
1327
  return TSDB_CODE_SUCCESS;
4,239✔
1328
}
1329

1330
#ifdef BUILD_NO_CALL
1331
int32_t stdInvertFunction(SqlFunctionCtx* pCtx) {
1332
  int32_t numOfElem = 0;
1333

1334
  // Only the pre-computing information loaded and actual data does not loaded
1335
  SInputColumnInfoData* pInput = &pCtx->input;
1336
  int32_t               type = pInput->pData[0]->info.type;
1337

1338
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1339

1340
  // computing based on the true data block
1341
  SColumnInfoData* pCol = pInput->pData[0];
1342

1343
  int32_t start = pInput->startRowIndex;
1344
  int32_t numOfRows = pInput->numOfRows;
1345

1346
  switch (type) {
1347
    case TSDB_DATA_TYPE_TINYINT: {
1348
      LIST_STDDEV_SUB_N(pStdRes->isum, int8_t);
1349
      break;
1350
    }
1351
    case TSDB_DATA_TYPE_SMALLINT: {
1352
      LIST_STDDEV_SUB_N(pStdRes->isum, int16_t);
1353
      break;
1354
    }
1355
    case TSDB_DATA_TYPE_INT: {
1356
      LIST_STDDEV_SUB_N(pStdRes->isum, int32_t);
1357
      break;
1358
    }
1359
    case TSDB_DATA_TYPE_BIGINT: {
1360
      LIST_STDDEV_SUB_N(pStdRes->isum, int64_t);
1361
      break;
1362
    }
1363
    case TSDB_DATA_TYPE_UTINYINT: {
1364
      LIST_STDDEV_SUB_N(pStdRes->isum, uint8_t);
1365
      break;
1366
    }
1367
    case TSDB_DATA_TYPE_USMALLINT: {
1368
      LIST_STDDEV_SUB_N(pStdRes->isum, uint16_t);
1369
      break;
1370
    }
1371
    case TSDB_DATA_TYPE_UINT: {
1372
      LIST_STDDEV_SUB_N(pStdRes->isum, uint32_t);
1373
      break;
1374
    }
1375
    case TSDB_DATA_TYPE_UBIGINT: {
1376
      LIST_STDDEV_SUB_N(pStdRes->isum, uint64_t);
1377
      break;
1378
    }
1379
    case TSDB_DATA_TYPE_FLOAT: {
1380
      LIST_STDDEV_SUB_N(pStdRes->dsum, float);
1381
      break;
1382
    }
1383
    case TSDB_DATA_TYPE_DOUBLE: {
1384
      LIST_STDDEV_SUB_N(pStdRes->dsum, double);
1385
      break;
1386
    }
1387
    default:
1388
      break;
1389
  }
1390

1391
  // data in the check operation are all null, not output
1392
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
1393
  return TSDB_CODE_SUCCESS;
1394
}
1395
#endif
1396

1397
int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
172,752✔
1398
  SInputColumnInfoData* pInput = &pCtx->input;
172,752✔
1399
  SStdRes*              pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
172,752✔
1400
  int32_t               type = pStddevRes->type;
172,752✔
1401
  double                avg;
1402

1403
  if (pStddevRes->count == 0) {
172,752✔
1404
    GET_RES_INFO(pCtx)->numOfRes = 0;
46,206✔
1405
    return functionFinalize(pCtx, pBlock);
46,206✔
1406
  }
1407

1408
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
126,546!
1409
    avg = pStddevRes->isum / ((double)pStddevRes->count);
90,372✔
1410
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticISum / ((double)pStddevRes->count) - avg * avg));
90,372✔
1411
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
36,174!
1412
    avg = pStddevRes->usum / ((double)pStddevRes->count);
10✔
1413
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticUSum / ((double)pStddevRes->count) - avg * avg));
10✔
1414
  } else {
1415
    avg = pStddevRes->dsum / ((double)pStddevRes->count);
36,164✔
1416
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticDSum / ((double)pStddevRes->count) - avg * avg));
36,164✔
1417
  }
1418

1419
  // check for overflow
1420
  if (isinf(pStddevRes->result) || isnan(pStddevRes->result)) {
126,546!
1421
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1422
  }
1423

1424
  return functionFinalize(pCtx, pBlock);
126,546✔
1425
}
1426

1427
int32_t stdvarFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
1428
  SInputColumnInfoData* pInput = &pCtx->input;
×
1429
  SStdRes*              pStdvarRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1430
  int32_t               type = pStdvarRes->type;
×
1431
  double                avg;
1432

1433
  if (pStdvarRes->count == 0) {
×
1434
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1435
    return functionFinalize(pCtx, pBlock);
×
1436
  }
1437

1438
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
1439
    avg = pStdvarRes->isum / ((double)pStdvarRes->count);
×
1440
    pStdvarRes->result = fabs(pStdvarRes->quadraticISum / ((double)pStdvarRes->count) - avg * avg);
×
1441
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
1442
    avg = pStdvarRes->usum / ((double)pStdvarRes->count);
×
1443
    pStdvarRes->result = fabs(pStdvarRes->quadraticUSum / ((double)pStdvarRes->count) - avg * avg);
×
1444
  } else {
1445
    avg = pStdvarRes->dsum / ((double)pStdvarRes->count);
×
1446
    pStdvarRes->result = fabs(pStdvarRes->quadraticDSum / ((double)pStdvarRes->count) - avg * avg);
×
1447
  }
1448

1449
  // check for overflow
1450
  if (isinf(pStdvarRes->result) || isnan(pStdvarRes->result)) {
×
1451
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1452
  }
1453

1454
  return functionFinalize(pCtx, pBlock);
×
1455
}
1456

1457
int32_t stdPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
4,405✔
1458
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
4,405✔
1459
  SStdRes*             pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
4,405✔
1460
  int32_t              resultBytes = getStdInfoSize();
4,405✔
1461
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
4,406✔
1462

1463
  if (NULL == res) {
4,406!
1464
    return terrno;
×
1465
  }
1466
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
4,406✔
1467
  varDataSetLen(res, resultBytes);
4,406✔
1468

1469
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
4,406✔
1470
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
4,406✔
1471
  if (NULL == pCol) {
4,406!
1472
    taosMemoryFree(res);
×
1473
    return TSDB_CODE_OUT_OF_RANGE;
×
1474
  }
1475

1476
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
4,406✔
1477

1478
  taosMemoryFree(res);
4,406✔
1479
  return code;
4,406✔
1480
}
1481

1482
int32_t stdCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
3✔
1483
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
3✔
1484
  SStdRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
3✔
1485

1486
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
1487
  SStdRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
1488
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
3!
1489

1490
  stdTransferInfo(pSBuf, pDBuf);
3✔
1491

1492
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
3✔
1493
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
3✔
1494
  return TSDB_CODE_SUCCESS;
3✔
1495
}
1496

1497
bool getLeastSQRFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
8,341✔
1498
  pEnv->calcMemSize = sizeof(SLeastSQRInfo);
8,341✔
1499
  return true;
8,341✔
1500
}
1501

1502
int32_t leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
45,825✔
1503
  if (pResultInfo->initialized) {
45,825!
1504
    return TSDB_CODE_SUCCESS;
×
1505
  }
1506
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
45,825!
1507
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1508
  }
1509

1510
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
45,835✔
1511

1512
  GET_TYPED_DATA(pInfo->startVal, double, pCtx->param[1].param.nType, &pCtx->param[1].param.i);
45,835!
1513
  GET_TYPED_DATA(pInfo->stepVal, double, pCtx->param[2].param.nType, &pCtx->param[2].param.i);
45,835!
1514
  return TSDB_CODE_SUCCESS;
45,835✔
1515
}
1516

1517
int32_t leastSQRFunction(SqlFunctionCtx* pCtx) {
50,027✔
1518
  int32_t numOfElem = 0;
50,027✔
1519

1520
  SInputColumnInfoData* pInput = &pCtx->input;
50,027✔
1521
  int32_t               type = pInput->pData[0]->info.type;
50,027✔
1522

1523
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
50,027✔
1524

1525
  SColumnInfoData* pCol = pInput->pData[0];
50,027✔
1526

1527
  double(*param)[3] = pInfo->matrix;
50,027✔
1528
  double x = pInfo->startVal;
50,027✔
1529

1530
  int32_t start = pInput->startRowIndex;
50,027✔
1531
  int32_t numOfRows = pInput->numOfRows;
50,027✔
1532

1533
  switch (type) {
50,027!
1534
    case TSDB_DATA_TYPE_TINYINT: {
4,160✔
1535
      int8_t* plist = (int8_t*)pCol->pData;
4,160✔
1536
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
89,380✔
1537
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
85,220✔
1538
          continue;
2,060✔
1539
        }
1540
        numOfElem++;
83,160✔
1541
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
83,160✔
1542
      }
1543
      break;
4,160✔
1544
    }
1545
    case TSDB_DATA_TYPE_SMALLINT: {
4,666✔
1546
      int16_t* plist = (int16_t*)pCol->pData;
4,666✔
1547
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
225,452✔
1548
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
220,786✔
1549
          continue;
1,860✔
1550
        }
1551

1552
        numOfElem++;
218,926✔
1553
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
218,926✔
1554
      }
1555
      break;
4,666✔
1556
    }
1557

1558
    case TSDB_DATA_TYPE_INT: {
8,273✔
1559
      int32_t* plist = (int32_t*)pCol->pData;
8,273✔
1560
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
251,725✔
1561
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
243,452✔
1562
          continue;
149,434✔
1563
        }
1564

1565
        numOfElem++;
94,018✔
1566
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
94,018✔
1567
      }
1568
      break;
8,273✔
1569
    }
1570

1571
    case TSDB_DATA_TYPE_BIGINT: {
10,091✔
1572
      int64_t* plist = (int64_t*)pCol->pData;
10,091✔
1573
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
379,110✔
1574
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
369,019✔
1575
          continue;
139,269✔
1576
        }
1577

1578
        numOfElem++;
229,750✔
1579
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
229,750✔
1580
      }
1581
      break;
10,091✔
1582
    }
1583

1584
    case TSDB_DATA_TYPE_UTINYINT: {
154✔
1585
      uint8_t* plist = (uint8_t*)pCol->pData;
154✔
1586
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,574✔
1587
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,420!
1588
          continue;
110✔
1589
        }
1590
        numOfElem++;
80,310✔
1591
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
80,310✔
1592
      }
1593
      break;
154✔
1594
    }
1595
    case TSDB_DATA_TYPE_USMALLINT: {
154✔
1596
      uint16_t* plist = (uint16_t*)pCol->pData;
154✔
1597
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,574✔
1598
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,420!
1599
          continue;
100✔
1600
        }
1601

1602
        numOfElem++;
80,320✔
1603
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
80,320✔
1604
      }
1605
      break;
154✔
1606
    }
1607

1608
    case TSDB_DATA_TYPE_UINT: {
154✔
1609
      uint32_t* plist = (uint32_t*)pCol->pData;
154✔
1610
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,574✔
1611
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,420!
1612
          continue;
100✔
1613
        }
1614

1615
        numOfElem++;
80,320✔
1616
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
80,320✔
1617
      }
1618
      break;
154✔
1619
    }
1620

1621
    case TSDB_DATA_TYPE_UBIGINT: {
154✔
1622
      uint64_t* plist = (uint64_t*)pCol->pData;
154✔
1623
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
80,574✔
1624
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
80,420!
1625
          continue;
100✔
1626
        }
1627

1628
        numOfElem++;
80,320✔
1629
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
80,320✔
1630
      }
1631
      break;
154✔
1632
    }
1633

1634
    case TSDB_DATA_TYPE_FLOAT: {
14,824✔
1635
      float* plist = (float*)pCol->pData;
14,824✔
1636
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
396,691✔
1637
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
381,867✔
1638
          continue;
149,879✔
1639
        }
1640

1641
        numOfElem++;
231,988✔
1642
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
231,988✔
1643
      }
1644
      break;
14,824✔
1645
    }
1646

1647
    case TSDB_DATA_TYPE_DOUBLE: {
7,397✔
1648
      double* plist = (double*)pCol->pData;
7,397✔
1649
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
371,557✔
1650
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
364,160✔
1651
          continue;
275,060✔
1652
        }
1653

1654
        numOfElem++;
89,100✔
1655
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
89,100✔
1656
      }
1657
      break;
7,397✔
1658
    }
1659
    case TSDB_DATA_TYPE_NULL: {
×
1660
      GET_RES_INFO(pCtx)->isNullRes = 1;
×
1661
      numOfElem = 1;
×
1662
      break;
×
1663
    }
1664

UNCOV
1665
    default:
×
UNCOV
1666
      break;
×
1667
  }
1668

1669
  pInfo->startVal = x;
50,027✔
1670
  pInfo->num += numOfElem;
50,027✔
1671

1672
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
50,027✔
1673

1674
  return TSDB_CODE_SUCCESS;
50,027✔
1675
}
1676

1677
int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
45,839✔
1678
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
45,839✔
1679
  SLeastSQRInfo*       pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
45,839✔
1680
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
45,839✔
1681
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
45,839✔
1682

1683
  if (NULL == pCol) {
45,841!
1684
    return TSDB_CODE_OUT_OF_RANGE;
×
1685
  }
1686
  int32_t currentRow = pBlock->info.rows;
45,841✔
1687

1688
  if (0 == pInfo->num) {
45,841✔
1689
    colDataSetNULL(pCol, currentRow);
22,931!
1690
    return TSDB_CODE_SUCCESS;
22,931✔
1691
  }
1692

1693
  double(*param)[3] = pInfo->matrix;
22,910✔
1694

1695
  param[1][1] = (double)pInfo->num;
22,910✔
1696
  param[1][0] = param[0][1];
22,910✔
1697

1698
  double param00 = param[0][0] - param[1][0] * (param[0][1] / param[1][1]);
22,910✔
1699
  double param02 = param[0][2] - param[1][2] * (param[0][1] / param[1][1]);
22,910✔
1700

1701
  if (0 == param00) {
22,910✔
1702
    colDataSetNULL(pCol, currentRow);
19,201!
1703
    return TSDB_CODE_SUCCESS;
19,201✔
1704
  }
1705

1706
  // param[0][1] = 0;
1707
  double param12 = param[1][2] - param02 * (param[1][0] / param00);
3,709✔
1708
  // param[1][0] = 0;
1709
  param02 /= param00;
3,709✔
1710

1711
  param12 /= param[1][1];
3,709✔
1712

1713
  char buf[LEASTSQUARES_BUFF_LENGTH] = {0};
3,709✔
1714
  char slopBuf[64] = {0};
3,709✔
1715
  char interceptBuf[64] = {0};
3,709✔
1716
  int  n = tsnprintf(slopBuf, 64, "%.6lf", param02);
3,709✔
1717
  if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) {
3,712✔
1718
    (void)snprintf(slopBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param02);
94✔
1719
  }
1720
  n = tsnprintf(interceptBuf, 64, "%.6lf", param12);
3,712✔
1721
  if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) {
3,712✔
1722
    (void)snprintf(interceptBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param12);
1,070✔
1723
  }
1724
  size_t len =
3,712✔
1725
      snprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{slop:%s, intercept:%s}", slopBuf, interceptBuf);
3,712✔
1726
  varDataSetLen(buf, len);
3,712✔
1727

1728
  int32_t code = colDataSetVal(pCol, currentRow, buf, pResInfo->isNullRes);
3,712✔
1729

1730
  return code;
3,714✔
1731
}
1732

1733
int32_t leastSQRCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
1734
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
1735
  SLeastSQRInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
1736
  int32_t              type = pDestCtx->input.pData[0]->info.type;
×
1737
  double(*pDparam)[3] = pDBuf->matrix;
×
1738

1739
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
1740
  SLeastSQRInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
1741
  double(*pSparam)[3] = pSBuf->matrix;
×
1742
  for (int32_t i = 0; i < pSBuf->num; i++) {
×
1743
    pDparam[0][0] += pDBuf->startVal * pDBuf->startVal;
×
1744
    pDparam[0][1] += pDBuf->startVal;
×
1745
    pDBuf->startVal += pDBuf->stepVal;
×
1746
  }
1747
  pDparam[0][2] += pSparam[0][2] + pDBuf->num * pDBuf->stepVal * pSparam[1][2];
×
1748
  pDparam[1][2] += pSparam[1][2];
×
1749
  pDBuf->num += pSBuf->num;
×
1750
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
1751
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
1752
  return TSDB_CODE_SUCCESS;
×
1753
}
1754

1755
bool getPercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
904✔
1756
  pEnv->calcMemSize = sizeof(SPercentileInfo);
904✔
1757
  return true;
904✔
1758
}
1759

1760
int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
3,382✔
1761
  if (pResultInfo->initialized) {
3,382!
1762
    return TSDB_CODE_SUCCESS;
×
1763
  }
1764
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
3,382!
1765
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1766
  }
1767

1768
  // in the first round, get the min-max value of all involved data
1769
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
3,382✔
1770
  SET_DOUBLE_VAL(&pInfo->minval, DBL_MAX);
3,382✔
1771
  SET_DOUBLE_VAL(&pInfo->maxval, -DBL_MAX);
3,382✔
1772
  pInfo->numOfElems = 0;
3,382✔
1773

1774
  return TSDB_CODE_SUCCESS;
3,382✔
1775
}
1776

1777
void percentileFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
1778
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
1779
    return;
×
1780
  }
1781
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1782
  if (pInfo->pMemBucket != NULL) {
×
1783
    tMemBucketDestroy(&(pInfo->pMemBucket));
×
1784
    pInfo->pMemBucket = NULL;
×
1785
  }
1786
}
1787

1788
int32_t percentileFunction(SqlFunctionCtx* pCtx) {
8,764✔
1789
  int32_t              code = TSDB_CODE_SUCCESS;
8,764✔
1790
  int32_t              numOfElems = 0;
8,764✔
1791
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,764✔
1792

1793
  SInputColumnInfoData* pInput = &pCtx->input;
8,764✔
1794
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
8,764✔
1795

1796
  SColumnInfoData* pCol = pInput->pData[0];
8,764✔
1797
  int32_t          type = pCol->info.type;
8,764✔
1798

1799
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
8,764✔
1800
  if (pCtx->scanFlag == MAIN_SCAN && pInfo->stage == 0) {
8,764✔
1801
    pInfo->stage += 1;
3,382✔
1802

1803
    // all data are null, set it completed
1804
    if (pInfo->numOfElems == 0) {
3,382✔
1805
      pResInfo->complete = true;
1,208✔
1806
      return TSDB_CODE_SUCCESS;
1,208✔
1807
    } else {
1808
      code = tMemBucketCreate(pCol->info.bytes, type, pInfo->minval, pInfo->maxval, pCtx->hasWindowOrGroup, &pInfo->pMemBucket, pInfo->numOfElems);
2,174✔
1809
      if (TSDB_CODE_SUCCESS != code) {
2,174!
1810
        return code;
×
1811
      }
1812
    }
1813
  }
1814

1815
  // the first stage, only acquire the min/max value
1816
  if (pInfo->stage == 0) {
7,556✔
1817
    if (pCtx->input.colDataSMAIsSet) {
4,382!
1818
      double tmin = 0.0, tmax = 0.0;
×
1819
      if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
1820
        tmin = (double)GET_INT64_VAL(&pAgg->min);
×
1821
        tmax = (double)GET_INT64_VAL(&pAgg->max);
×
1822
      } else if (IS_FLOAT_TYPE(type)) {
×
1823
        tmin = GET_DOUBLE_VAL(&pAgg->min);
×
1824
        tmax = GET_DOUBLE_VAL(&pAgg->max);
×
1825
      } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
1826
        tmin = (double)GET_UINT64_VAL(&pAgg->min);
×
1827
        tmax = (double)GET_UINT64_VAL(&pAgg->max);
×
1828
      }
1829

1830
      if (GET_DOUBLE_VAL(&pInfo->minval) > tmin) {
×
1831
        SET_DOUBLE_VAL(&pInfo->minval, tmin);
×
1832
      }
1833

1834
      if (GET_DOUBLE_VAL(&pInfo->maxval) < tmax) {
×
1835
        SET_DOUBLE_VAL(&pInfo->maxval, tmax);
×
1836
      }
1837

1838
      pInfo->numOfElems += (pInput->numOfRows - pAgg->numOfNull);
×
1839
    } else {
1840
      // check the valid data one by one
1841
      int32_t start = pInput->startRowIndex;
4,382✔
1842
      for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
517,400✔
1843
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
513,018✔
1844
          continue;
2,000✔
1845
        }
1846

1847
        char* data = colDataGetData(pCol, i);
511,018!
1848

1849
        double v = 0;
511,018✔
1850
        GET_TYPED_DATA(v, double, type, data);
511,018!
1851
        if (v < GET_DOUBLE_VAL(&pInfo->minval)) {
511,018✔
1852
          SET_DOUBLE_VAL(&pInfo->minval, v);
2,414✔
1853
        }
1854

1855
        if (v > GET_DOUBLE_VAL(&pInfo->maxval)) {
511,018✔
1856
          SET_DOUBLE_VAL(&pInfo->maxval, v);
409,436✔
1857
        }
1858

1859
        pInfo->numOfElems += 1;
511,018✔
1860
      }
1861
    }
1862
  } else {
1863
    // the second stage, calculate the true percentile value
1864
    int32_t start = pInput->startRowIndex;
3,174✔
1865
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
514,192✔
1866
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
511,018!
1867
        continue;
×
1868
      }
1869

1870
      char* data = colDataGetData(pCol, i);
511,018!
1871
      numOfElems += 1;
511,018✔
1872
      code = tMemBucketPut(pInfo->pMemBucket, data, 1);
511,018✔
1873
      if (code != TSDB_CODE_SUCCESS) {
511,018!
1874
        tMemBucketDestroy(&(pInfo->pMemBucket));
×
1875
        return code;
×
1876
      }
1877
    }
1878

1879
    SET_VAL(pResInfo, numOfElems, 1);
3,174!
1880
  }
1881

1882
  pCtx->needCleanup = true;
7,556✔
1883
  return TSDB_CODE_SUCCESS;
7,556✔
1884
}
1885

1886
int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
3,382✔
1887
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
3,382✔
1888
  SPercentileInfo*     ppInfo = (SPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
3,382✔
1889

1890
  int32_t code = 0;
3,382✔
1891
  double  v = 0;
3,382✔
1892

1893
  tMemBucket** pMemBucket = &ppInfo->pMemBucket;
3,382✔
1894
  if ((*pMemBucket) != NULL && (*pMemBucket)->total > 0) {  // check for null
3,382!
1895
    if (pCtx->numOfParams > 2) {
2,174✔
1896
      char   buf[3200] = {0};
30✔
1897
      // max length of double num is 317, e.g. use %.6lf to print -1.0e+308, consider the comma and bracket, 3200 is enough.
1898
      size_t len = 1;
30✔
1899

1900
      varDataVal(buf)[0] = '[';
30✔
1901
      for (int32_t i = 1; i < pCtx->numOfParams; ++i) {
330✔
1902
        SVariant* pVal = &pCtx->param[i].param;
300✔
1903

1904
        GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
300!
1905

1906
        code = getPercentile((*pMemBucket), v, &ppInfo->result);
300✔
1907
        if (code != TSDB_CODE_SUCCESS) {
300!
1908
          goto _fin_error;
×
1909
        }
1910

1911
        if (i == pCtx->numOfParams - 1) {
300✔
1912
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf]", ppInfo->result);
30✔
1913
        } else {
1914
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf, ", ppInfo->result);
270✔
1915
        }
1916
      }
1917

1918
      int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
30✔
1919
      SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
30✔
1920
      if (NULL == pCol) {
30!
1921
        code = terrno;
×
1922
        goto _fin_error;
×
1923
      }
1924

1925
      varDataSetLen(buf, len);
30✔
1926
      code = colDataSetVal(pCol, pBlock->info.rows, buf, false);
30✔
1927
      if (code != TSDB_CODE_SUCCESS) {
30!
1928
        goto _fin_error;
×
1929
      }
1930

1931
      tMemBucketDestroy(pMemBucket);
30✔
1932
      return TSDB_CODE_SUCCESS;
30✔
1933
    } else {
1934
      SVariant* pVal = &pCtx->param[1].param;
2,144✔
1935

1936
      GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
2,144!
1937

1938
      code = getPercentile((*pMemBucket), v, &ppInfo->result);
2,144✔
1939
      if (code != TSDB_CODE_SUCCESS) {
2,144!
1940
        goto _fin_error;
×
1941
      }
1942

1943
      tMemBucketDestroy(pMemBucket);
2,144✔
1944
      return functionFinalize(pCtx, pBlock);
2,144✔
1945
    }
1946
  } else {
1947
    return functionFinalize(pCtx, pBlock);
1,208✔
1948
  }
1949

1950
_fin_error:
×
1951

1952
  tMemBucketDestroy(pMemBucket);
×
1953
  return code;
×
1954
}
1955

1956
bool getApercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
39,953✔
1957
  int32_t bytesHist =
39,953✔
1958
      (int32_t)(sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
1959
  int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION));
39,953✔
1960
  pEnv->calcMemSize = TMAX(bytesHist, bytesDigest);
39,953✔
1961
  return true;
39,953✔
1962
}
1963

1964
int32_t getApercentileMaxSize() {
5,636✔
1965
  int32_t bytesHist =
5,636✔
1966
      (int32_t)(sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
1967
  int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION));
5,636✔
1968
  return TMAX(bytesHist, bytesDigest);
5,636✔
1969
}
1970

1971
static int8_t getApercentileAlgo(char* algoStr) {
22,914✔
1972
  int8_t algoType;
1973
  if (strcasecmp(algoStr, "default") == 0) {
22,914✔
1974
    algoType = APERCT_ALGO_DEFAULT;
11,448✔
1975
  } else if (strcasecmp(algoStr, "t-digest") == 0) {
11,466✔
1976
    algoType = APERCT_ALGO_TDIGEST;
11,462✔
1977
  } else {
1978
    algoType = APERCT_ALGO_UNKNOWN;
4✔
1979
  }
1980

1981
  return algoType;
22,914✔
1982
}
1983

1984
static void buildHistogramInfo(SAPercentileInfo* pInfo) {
426,908✔
1985
  pInfo->pHisto = (SHistogramInfo*)((char*)pInfo + sizeof(SAPercentileInfo));
426,908✔
1986
  pInfo->pHisto->elems = (SHistBin*)((char*)pInfo->pHisto + sizeof(SHistogramInfo));
426,908✔
1987
}
426,908✔
1988

1989
static void buildTDigestInfo(SAPercentileInfo* pInfo) {
23,086✔
1990
  pInfo->pTDigest = (TDigest*)((char*)pInfo + sizeof(SAPercentileInfo));
23,086✔
1991
}
23,086✔
1992

1993
int32_t apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
156,189✔
1994
  if (pResultInfo->initialized) {
156,189!
1995
    return TSDB_CODE_SUCCESS;
×
1996
  }
1997
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
156,189!
1998
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1999
  }
2000

2001
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
156,217✔
2002

2003
  SVariant* pVal = &pCtx->param[1].param;
156,217✔
2004
  pInfo->percent = 0;
156,217✔
2005
  GET_TYPED_DATA(pInfo->percent, double, pVal->nType, &pVal->i);
156,217!
2006

2007
  if (pCtx->numOfParams == 2) {
156,217✔
2008
    pInfo->algo = APERCT_ALGO_DEFAULT;
133,298✔
2009
  } else if (pCtx->numOfParams == 3) {
22,919✔
2010
    pInfo->algo = getApercentileAlgo(varDataVal(pCtx->param[2].param.pz));
22,915✔
2011
    if (pInfo->algo == APERCT_ALGO_UNKNOWN) {
22,909!
2012
      return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
2013
    }
2014
  }
2015

2016
  char* tmp = (char*)pInfo + sizeof(SAPercentileInfo);
156,211✔
2017
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
156,211✔
2018
    pInfo->pTDigest = tdigestNewFrom(tmp, COMPRESSION);
11,463✔
2019
  } else {
2020
    buildHistogramInfo(pInfo);
144,748✔
2021
    pInfo->pHisto = tHistogramCreateFrom(tmp, MAX_HISTOGRAM_BIN);
144,744✔
2022
    qDebug("%s set up histogram, numOfElems:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
144,728✔
2023
           pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2024
  }
2025

2026
  return TSDB_CODE_SUCCESS;
156,197✔
2027
}
2028

2029
int32_t apercentileFunction(SqlFunctionCtx* pCtx) {
162,234✔
2030
  int32_t               numOfElems = 0;
162,234✔
2031
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
162,234✔
2032
  SInputColumnInfoData* pInput = &pCtx->input;
162,234✔
2033

2034
  SColumnInfoData* pCol = pInput->pData[0];
162,234✔
2035
  int32_t          type = pCol->info.type;
162,234✔
2036

2037
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
162,234✔
2038

2039
  int32_t start = pInput->startRowIndex;
162,234✔
2040
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
162,234✔
2041
    buildTDigestInfo(pInfo);
11,469✔
2042
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,467✔
2043
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
585,898✔
2044
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
574,438✔
2045
        continue;
352,232✔
2046
      }
2047
      numOfElems += 1;
222,206✔
2048
      char* data = colDataGetData(pCol, i);
222,206!
2049

2050
      double  v = 0;  // value
222,206✔
2051
      int64_t w = 1;  // weigth
222,206✔
2052
      GET_TYPED_DATA(v, double, type, data);
222,206✔
2053
      int32_t code = tdigestAdd(pInfo->pTDigest, v, w);
222,206✔
2054
      if (code != TSDB_CODE_SUCCESS) {
222,200!
2055
        return code;
×
2056
      }
2057
    }
2058
  } else {
2059
    // might be a race condition here that pHisto can be overwritten or setup function
2060
    // has not been called, need to relink the buffer pHisto points to.
2061
    buildHistogramInfo(pInfo);
150,765✔
2062
    qDebug("%s before add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
150,768✔
2063
           __FUNCTION__, numOfElems, pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto,
2064
           pInfo->pHisto->elems);
2065
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
2,018,707✔
2066
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
1,867,755✔
2067
        continue;
968,497✔
2068
      }
2069
      numOfElems += 1;
899,258✔
2070
      char* data = colDataGetData(pCol, i);
899,258!
2071

2072
      double v = 0;
899,258✔
2073
      GET_TYPED_DATA(v, double, type, data);
899,258!
2074
      int32_t code = tHistogramAdd(&pInfo->pHisto, v);
899,258✔
2075
      if (code != TSDB_CODE_SUCCESS) {
899,439!
2076
        return code;
×
2077
      }
2078
    }
2079

2080
    qDebug("%s after add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
150,952✔
2081
           __FUNCTION__, numOfElems, pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto,
2082
           pInfo->pHisto->elems);
2083
  }
2084

2085
  SET_VAL(pResInfo, numOfElems, 1);
162,249✔
2086
  return TSDB_CODE_SUCCESS;
162,249✔
2087
}
2088

2089
static int32_t apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) {
4,161✔
2090
  pOutput->percent = pInput->percent;
4,161✔
2091
  pOutput->algo = pInput->algo;
4,161✔
2092
  if (pOutput->algo == APERCT_ALGO_TDIGEST) {
4,161✔
2093
    buildTDigestInfo(pInput);
153✔
2094
    tdigestAutoFill(pInput->pTDigest, COMPRESSION);
153✔
2095

2096
    if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) {
153✔
2097
      return TSDB_CODE_SUCCESS;
1✔
2098
    }
2099

2100
    if (hasRes) {
152✔
2101
      *hasRes = true;
150✔
2102
    }
2103

2104
    buildTDigestInfo(pOutput);
152✔
2105
    TDigest* pTDigest = pOutput->pTDigest;
152✔
2106
    tdigestAutoFill(pTDigest, COMPRESSION);
152✔
2107

2108
    if (pTDigest->num_centroids <= 0 && pTDigest->num_buffered_pts == 0) {
152!
2109
      (void)memcpy(pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION));
150✔
2110
      tdigestAutoFill(pTDigest, COMPRESSION);
150✔
2111
    } else {
2112
      int32_t code = tdigestMerge(pTDigest, pInput->pTDigest);
2✔
2113
      if (TSDB_CODE_SUCCESS != code) {
2!
2114
        return code;
×
2115
      }
2116
    }
2117
  } else {
2118
    buildHistogramInfo(pInput);
4,008✔
2119
    if (pInput->pHisto->numOfElems <= 0) {
4,008✔
2120
      return TSDB_CODE_SUCCESS;
72✔
2121
    }
2122

2123
    if (hasRes) {
3,936✔
2124
      *hasRes = true;
3,934✔
2125
    }
2126

2127
    buildHistogramInfo(pOutput);
3,936✔
2128
    SHistogramInfo* pHisto = pOutput->pHisto;
3,936✔
2129

2130
    if (pHisto->numOfElems <= 0) {
3,936✔
2131
      (void)memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
3,612✔
2132
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
3,612✔
2133

2134
      qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries,
3,612✔
2135
             pHisto);
2136
    } else {
2137
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
324✔
2138
      qDebug("%s input histogram, elem:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems,
324✔
2139
             pHisto->numOfEntries, pInput->pHisto);
2140

2141
      SHistogramInfo* pRes = NULL;
324✔
2142
      int32_t code = tHistogramMerge(pHisto, pInput->pHisto, MAX_HISTOGRAM_BIN, &pRes);
324✔
2143
      if (TSDB_CODE_SUCCESS != code) {
324!
2144
        tHistogramDestroy(&pRes);
×
2145
        return code;
×
2146
      }
2147
      (void)memcpy(pHisto, pRes, sizeof(SHistogramInfo) + sizeof(SHistBin) * MAX_HISTOGRAM_BIN);
324✔
2148
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
324✔
2149

2150
      qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries,
324✔
2151
             pHisto);
2152
      tHistogramDestroy(&pRes);
324✔
2153
    }
2154
  }
2155
  return TSDB_CODE_SUCCESS;
4,088✔
2156
}
2157

2158
int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) {
4,147✔
2159
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
4,147✔
2160

2161
  SInputColumnInfoData* pInput = &pCtx->input;
4,147✔
2162

2163
  SColumnInfoData* pCol = pInput->pData[0];
4,147✔
2164
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
4,147!
2165
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2166
  }
2167

2168
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
4,147✔
2169

2170
  qDebug("%s total %" PRId64 " rows will merge, %p", __FUNCTION__, pInput->numOfRows, pInfo->pHisto);
4,147✔
2171

2172
  bool hasRes = false;
4,147✔
2173
  int32_t start = pInput->startRowIndex;
4,147✔
2174
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
8,304✔
2175
    char* data = colDataGetData(pCol, i);
4,157!
2176

2177
    SAPercentileInfo* pInputInfo = (SAPercentileInfo*)varDataVal(data);
4,157✔
2178
    int32_t code = apercentileTransferInfo(pInputInfo, pInfo, &hasRes);
4,157✔
2179
    if (TSDB_CODE_SUCCESS != code) {
4,157!
2180
      return code;
×
2181
    }
2182
  }
2183

2184
  if (pInfo->algo != APERCT_ALGO_TDIGEST) {
4,147✔
2185
    buildHistogramInfo(pInfo);
3,996✔
2186
    qDebug("%s after merge, total:%" PRId64 ", numOfEntry:%d, %p", __FUNCTION__, pInfo->pHisto->numOfElems,
3,996✔
2187
           pInfo->pHisto->numOfEntries, pInfo->pHisto);
2188
  }
2189

2190
  SET_VAL(pResInfo, hasRes ? 1 : 0, 1);
4,147✔
2191
  return TSDB_CODE_SUCCESS;
4,147✔
2192
}
2193

2194
int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
130,827✔
2195
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
130,827✔
2196
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
130,827✔
2197

2198
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
130,827✔
2199
    buildTDigestInfo(pInfo);
11,316✔
2200
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,316✔
2201
    if (pInfo->pTDigest->size > 0) {
11,316!
2202
      pInfo->result = tdigestQuantile(pInfo->pTDigest, pInfo->percent / 100);
11,316✔
2203
    } else {  // no need to free
2204
      // setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
2205
      return TSDB_CODE_SUCCESS;
×
2206
    }
2207
  } else {
2208
    buildHistogramInfo(pInfo);
119,511✔
2209
    if (pInfo->pHisto->numOfElems > 0) {
119,505✔
2210
      qDebug("%s get the final res, elements:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
75,449✔
2211
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2212

2213
      double  ratio[] = {pInfo->percent};
75,449✔
2214
      double* res = NULL;
75,449✔
2215
      int32_t code = tHistogramUniform(pInfo->pHisto, ratio, 1, &res);
75,449✔
2216
      if (TSDB_CODE_SUCCESS != code) {
75,451!
2217
        taosMemoryFree(res);
×
2218
        return code;
×
2219
      }
2220
      pInfo->result = *res;
75,451✔
2221
      // memcpy(pCtx->pOutput, res, sizeof(double));
2222
      taosMemoryFree(res);
75,451✔
2223
    } else {  // no need to free
2224
      // setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
2225
      // return TSDB_CODE_SUCCESS;
2226
      qDebug("%s get the final res, elements:%" PRId64 ", numOfEntry:%d. result is null", __FUNCTION__,
44,056✔
2227
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries);
2228
    }
2229
  }
2230

2231
  return functionFinalize(pCtx, pBlock);
130,819✔
2232
}
2233

2234
int32_t apercentilePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
4,167✔
2235
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
4,167✔
2236
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
4,167✔
2237

2238
  int32_t resultBytes = getApercentileMaxSize();
4,167✔
2239
  char*   res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
4,167✔
2240
  if (NULL == res) {
4,168!
2241
    return terrno;
×
2242
  }
2243

2244
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
4,168✔
2245
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
151✔
2246
    varDataSetLen(res, resultBytes);
151✔
2247
  } else {
2248
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
4,017✔
2249
    varDataSetLen(res, resultBytes);
4,017✔
2250
  }
2251

2252
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
4,168✔
2253
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
4,168✔
2254
  if (NULL == pCol) {
4,168!
2255
    taosMemoryFree(res);
×
2256
    return TSDB_CODE_OUT_OF_RANGE;
×
2257
  }
2258

2259
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
4,168✔
2260

2261
  taosMemoryFree(res);
4,168✔
2262
  return code;
4,168✔
2263
}
2264

2265
int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
4✔
2266
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
4✔
2267
  SAPercentileInfo*    pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
4✔
2268

2269
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
4✔
2270
  SAPercentileInfo*    pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
4✔
2271

2272
  qDebug("%s start to combine apercentile, %p", __FUNCTION__, pDBuf->pHisto);
4!
2273

2274
  int32_t code = apercentileTransferInfo(pSBuf, pDBuf, NULL);
4✔
2275
  if (TSDB_CODE_SUCCESS != code) {
4!
2276
    return code;
×
2277
  }
2278
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
4✔
2279
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
4✔
2280
  return TSDB_CODE_SUCCESS;
4✔
2281
}
2282

2283
// TODO: change this function when block data info pks changed
2284
static int32_t comparePkDataWithSValue(int8_t pkType, char* pkData, SValue* pVal, int32_t order) {
6,557✔
2285
  char numVal[8] = {0};
6,557✔
2286
  switch (pkType) {
6,557✔
2287
    case TSDB_DATA_TYPE_INT:
1,131✔
2288
      *(int32_t*)numVal = (int32_t)pVal->val;
1,131✔
2289
      break;
1,131✔
2290
    case TSDB_DATA_TYPE_UINT:
1,103✔
2291
      *(uint32_t*)numVal = (uint32_t)pVal->val;
1,103✔
2292
      break;
1,103✔
2293
    case TSDB_DATA_TYPE_BIGINT:
1,191✔
2294
      *(int64_t*)numVal = (int64_t)pVal->val;
1,191✔
2295
      break;
1,191✔
2296
    case TSDB_DATA_TYPE_UBIGINT:
1,190✔
2297
      *(uint64_t*)numVal = (uint64_t)pVal->val;
1,190✔
2298
      break;
1,190✔
2299
    default:
1,942✔
2300
      break;
1,942✔
2301
  }
2302
  char*         blockData = (IS_NUMERIC_TYPE(pkType)) ? (char*) numVal : (char*)pVal->pData;
6,557!
2303
  __compar_fn_t fn = getKeyComparFunc(pkType, order);
6,557✔
2304
  return fn(pkData, blockData);
6,557✔
2305
}
2306

2307
EFuncDataRequired firstDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
5,023✔
2308
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
5,023✔
2309

2310
  // not initialized yet, data is required
2311
  if (pEntry == NULL) {
5,023!
2312
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2313
  }
2314

2315
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
5,023✔
2316
  if (pResult->hasResult) {
5,023✔
2317
    if (pResult->pkBytes > 0) {
4,959✔
2318
      pResult->pkData = pResult->buf + pResult->bytes;
3,614✔
2319
    } else {
2320
      pResult->pkData = NULL;
1,345✔
2321
    }    
2322
    if (pResult->ts < pBlockInfo->window.skey) {
4,959✔
2323
      return FUNC_DATA_REQUIRED_NOT_LOAD;
1,848✔
2324
    } else if (pResult->ts == pBlockInfo->window.skey) {
3,111✔
2325
      if (NULL == pResult->pkData) {
2,092✔
2326
        return FUNC_DATA_REQUIRED_NOT_LOAD;
188✔
2327
      }
2328
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 0, TSDB_ORDER_ASC) < 0) {
1,904✔
2329
        return FUNC_DATA_REQUIRED_NOT_LOAD;
335✔
2330
      }
2331
    }
2332
    return FUNC_DATA_REQUIRED_DATA_LOAD;
2,587✔
2333
  } else {
2334
    return FUNC_DATA_REQUIRED_DATA_LOAD;
64✔
2335
  }
2336
}
2337

2338
EFuncDataRequired lastDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
10,879✔
2339
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
10,879✔
2340

2341
  // not initialized yet, data is required
2342
  if (pEntry == NULL) {
10,879!
2343
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2344
  }
2345

2346
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
10,879✔
2347
  if (pResult->hasResult) {
10,879✔
2348
    if (pResult->pkBytes > 0) {
10,774✔
2349
      pResult->pkData = pResult->buf + pResult->bytes;
8,794✔
2350
    } else {
2351
      pResult->pkData = NULL;
1,980✔
2352
    }
2353
    if (pResult->ts > pBlockInfo->window.ekey) {
10,774✔
2354
      return FUNC_DATA_REQUIRED_NOT_LOAD;
4,466✔
2355
    } else if (pResult->ts == pBlockInfo->window.ekey && pResult->pkData) {
6,308✔
2356
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 1, TSDB_ORDER_DESC) < 0) {
4,656✔
2357
        return FUNC_DATA_REQUIRED_NOT_LOAD;
1,413✔
2358
      }
2359
    }
2360
    return FUNC_DATA_REQUIRED_DATA_LOAD;
4,895✔
2361
  } else {
2362
    return FUNC_DATA_REQUIRED_DATA_LOAD;
105✔
2363
  }
2364
}
2365

2366
//TODO modify it to include primary key bytes
2367
int32_t getFirstLastInfoSize(int32_t resBytes, int32_t pkBytes) { return sizeof(SFirstLastRes) + resBytes + pkBytes; }
25,439,098✔
2368

2369
bool getFirstLastFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
201,207✔
2370
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
201,207✔
2371
  //TODO: change SFunctionNode to add pk info
2372
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
201,279✔
2373
  pEnv->calcMemSize = getFirstLastInfoSize(pNode->node.resType.bytes, pkBytes);
201,279✔
2374
  return true;
201,285✔
2375
}
2376

2377
bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
140,900✔
2378
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
140,900✔
2379
  pEnv->calcMemSize = pNode->node.resType.bytes;
140,923✔
2380
  return true;
140,923✔
2381
}
2382

2383
bool getGroupKeyFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
251,624✔
2384
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
251,624✔
2385
  pEnv->calcMemSize = sizeof(SGroupKeyInfo) + pNode->node.resType.bytes;
251,799✔
2386
  return true;
251,799✔
2387
}
2388

2389
static FORCE_INLINE TSKEY getRowPTs(SColumnInfoData* pTsColInfo, int32_t rowIndex) {
2390
  if (pTsColInfo == NULL || pTsColInfo->pData == NULL) {
107,315,838!
2391
    return 0;
×
2392
  }
2393

2394
  return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex);
107,403,085!
2395
}
2396

2397
int32_t firstLastFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
1,095,193✔
2398
  if (pResInfo->initialized) {
1,095,193!
2399
    return TSDB_CODE_SUCCESS;
×
2400
  }
2401
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
1,095,193!
2402
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
2403
  }
2404

2405
  SFirstLastRes *       pRes = GET_ROWCELL_INTERBUF(pResInfo);
1,095,223✔
2406
  SInputColumnInfoData* pInput = &pCtx->input;
1,095,223✔
2407

2408
  pRes->nullTupleSaved = false;
1,095,223✔
2409
  pRes->nullTuplePos.pageId = -1;
1,095,223✔
2410
  return TSDB_CODE_SUCCESS;
1,095,223✔
2411
}
2412

2413
static int32_t prepareBuf(SqlFunctionCtx* pCtx) {
715,526✔
2414
  if (pCtx->subsidiaries.rowLen == 0) {
715,526✔
2415
    int32_t rowLen = 0;
35,286✔
2416
    for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
80,358✔
2417
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
45,072✔
2418
      rowLen += pc->pExpr->base.resSchema.bytes;
45,072✔
2419
    }
2420

2421
    pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool);
35,286✔
2422
    pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen);
35,286✔
2423
    if (NULL == pCtx->subsidiaries.buf) {
35,298✔
2424
      return terrno;
4✔
2425
    }
2426
  }
2427
  return TSDB_CODE_SUCCESS;
715,534✔
2428
}
2429

2430
static int32_t firstlastSaveTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SqlFunctionCtx* pCtx,
58,595,291✔
2431
                                      SFirstLastRes* pInfo, bool noElements) {
2432
  int32_t code = TSDB_CODE_SUCCESS;
58,595,291✔
2433

2434
  if (pCtx->subsidiaries.num <= 0) {
58,595,291✔
2435
    return TSDB_CODE_SUCCESS;
58,484,079✔
2436
  }
2437

2438
  if (!pInfo->hasResult) {
111,212✔
2439
    code = saveTupleData(pCtx, rowIndex, pSrcBlock, noElements ? &pInfo->nullTuplePos : &pInfo->pos);
65,762✔
2440
  } else {
2441
    code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
45,450✔
2442
  }
2443

2444
  return code;
87,045✔
2445
}
2446

2447
static int32_t doSaveCurrentVal(SqlFunctionCtx* pCtx, int32_t rowIndex, int64_t currentTs, char* pkData, int32_t type, char* pData) {
33,592,589✔
2448
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
33,592,589✔
2449
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
33,592,589✔
2450

2451
  if (IS_VAR_DATA_TYPE(type)) {
33,592,589!
2452
    pInfo->bytes = varDataTLen(pData);
1,104,181✔
2453
  }
2454

2455
  (void)memcpy(pInfo->buf, pData, pInfo->bytes);
33,592,589✔
2456
  if (pkData != NULL) {
33,592,589✔
2457
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
1,366,768!
2458
      pInfo->pkBytes = varDataTLen(pkData);
455,248✔
2459
    }
2460
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
1,366,768✔
2461
    pInfo->pkData = pInfo->buf + pInfo->bytes;
1,366,768✔
2462
  }
2463

2464
  pInfo->ts = currentTs;
33,592,589✔
2465
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
33,592,589✔
2466
  if (code != TSDB_CODE_SUCCESS) {
33,595,991!
2467
    return code;
×
2468
  }
2469

2470
  pInfo->hasResult = true;
33,595,991✔
2471
  return TSDB_CODE_SUCCESS;
33,595,991✔
2472
}
2473

2474
// This ordinary first function does not care if current scan is ascending order or descending order scan
2475
// the OPTIMIZED version of first function will only handle the ascending order scan
2476
int32_t firstFunction(SqlFunctionCtx* pCtx) {
29,383,966✔
2477
  int32_t numOfElems = 0;
29,383,966✔
2478

2479
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
29,383,966✔
2480
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
29,383,966✔
2481

2482
  SInputColumnInfoData* pInput = &pCtx->input;
29,383,966✔
2483
  SColumnInfoData*      pInputCol = pInput->pData[0];
29,383,966✔
2484

2485
  pInfo->bytes = pInputCol->info.bytes;
29,383,966✔
2486

2487
  if (IS_NULL_TYPE(pInputCol->info.type)) {
29,383,966✔
2488
    return TSDB_CODE_SUCCESS;
1,564✔
2489
  }
2490

2491
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
29,382,402✔
2492
  pInfo->pkType = -1;
29,382,402✔
2493
  __compar_fn_t  pkCompareFn = NULL;
29,382,402✔
2494
  if (pCtx->hasPrimaryKey) {
29,382,402✔
2495
    pInfo->pkType = pkCol->info.type;
252,454✔
2496
    pInfo->pkBytes = pkCol->info.bytes;
252,454✔
2497
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_ASC);
252,454✔
2498
  }
2499

2500
  // All null data column, return directly.
2501
  if (pInput->colDataSMAIsSet && (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows) &&
29,392,443!
2502
      pInputCol->hasNull == true) {
×
2503
    // save selectivity value for column consisted of all null values
2504
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
×
2505
    if (code != TSDB_CODE_SUCCESS) {
×
2506
      return code;
×
2507
    }
2508
    pInfo->nullTupleSaved = true;
×
2509
    return TSDB_CODE_SUCCESS;
×
2510
  }
2511

2512
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
29,392,443!
2513

2514
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
29,392,443!
2515
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
29,392,443!
2516

2517
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
29,392,443✔
2518

2519
  //  please ref. to the comment in lastRowFunction for the reason why disabling the opt version of last/first
2520
  //  function. we will use this opt implementation in an new version that is only available in scan subplan
2521
#if 0
2522
  if (blockDataOrder == TSDB_ORDER_ASC) {
2523
    // filter according to current result firstly
2524
    if (pResInfo->numOfRes > 0) {
2525
      if (pInfo->ts < startKey) {
2526
        return TSDB_CODE_SUCCESS;
2527
      }
2528
    }
2529

2530
    for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
2531
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2532
        continue;
2533
      }
2534

2535
      numOfElems++;
2536

2537
      char* data = colDataGetData(pInputCol, i);
2538
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2539
      if (pResInfo->numOfRes == 0 || pInfo->ts > cts) {
2540
        doSaveCurrentVal(pCtx, i, cts, pInputCol->info.type, data);
2541
        break;
2542
      }
2543
    }
2544
  } else {
2545
    // in case of descending order time stamp serial, which usually happens as the results of the nest query,
2546
    // all data needs to be check.
2547
    if (pResInfo->numOfRes > 0) {
2548
      if (pInfo->ts < endKey) {
2549
        return TSDB_CODE_SUCCESS;
2550
      }
2551
    }
2552

2553
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
2554
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2555
        continue;
2556
      }
2557

2558
      numOfElems++;
2559

2560
      char* data = colDataGetData(pInputCol, i);
2561
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2562

2563
      if (pResInfo->numOfRes == 0 || pInfo->ts > cts) {
2564
        doSaveCurrentVal(pCtx, i, cts, pInputCol->info.type, data);
2565
        break;
2566
      }
2567
    }
2568
  }
2569
#else
2570
  int64_t* pts = (int64_t*)pInput->pPTS->pData;
29,392,443✔
2571

2572
  int from = -1;
29,392,443✔
2573
  int32_t i = -1;
29,392,443✔
2574
  while (funcInputGetNextRowIndex(pInput, from, true, &i, &from)) {
79,268,752✔
2575
    if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
65,788,831!
2576
      continue;
67,332✔
2577
    }
2578

2579
    numOfElems++;
49,870,316✔
2580
    char* data = colDataGetData(pInputCol, i);
49,870,316!
2581
    char* pkData = NULL;
49,870,316✔
2582
    if (pCtx->hasPrimaryKey) {
49,870,316✔
2583
      pkData = colDataGetData(pkCol, i);
1,363,819!
2584
    }
2585
    TSKEY cts = pts[i];
49,870,316✔
2586
    if (pResInfo->numOfRes == 0 || pInfo->ts > cts || 
49,870,316✔
2587
         (pInfo->ts == cts && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
31,103,888!
2588
      int32_t code = doSaveCurrentVal(pCtx, i, cts, pkData, pInputCol->info.type, data);
18,766,428✔
2589
      if (code != TSDB_CODE_SUCCESS) {
18,705,091!
2590
        return code;
×
2591
      }
2592
      pResInfo->numOfRes = 1;
18,705,091✔
2593
    }
2594
  }
2595
#endif
2596

2597
  if (numOfElems == 0) {
28,927,200✔
2598
    // save selectivity value for column consisted of all null values
2599
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
17,705✔
2600
    if (code != TSDB_CODE_SUCCESS) {
17,706!
2601
      return code;
×
2602
    }
2603
    pInfo->nullTupleSaved = true;
17,706✔
2604
  }
2605
  SET_VAL(pResInfo, numOfElems, 1);
28,927,201✔
2606
  return TSDB_CODE_SUCCESS;
28,927,201✔
2607
}
2608

2609
int32_t lastFunction(SqlFunctionCtx* pCtx) {
23,660,402✔
2610
  int32_t numOfElems = 0;
23,660,402✔
2611

2612
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
23,660,402✔
2613
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
23,660,402✔
2614

2615
  SInputColumnInfoData* pInput = &pCtx->input;
23,660,402✔
2616
  SColumnInfoData*      pInputCol = pInput->pData[0];
23,660,402✔
2617

2618
  int32_t type = pInputCol->info.type;
23,660,402✔
2619
  int32_t bytes = pInputCol->info.bytes;
23,660,402✔
2620
  pInfo->bytes = bytes;
23,660,402✔
2621

2622
  if (IS_NULL_TYPE(type)) {
23,660,402✔
2623
    return TSDB_CODE_SUCCESS;
1,564✔
2624
  }
2625

2626
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
23,658,838✔
2627
  pInfo->pkType = -1;
23,658,838✔
2628
  __compar_fn_t  pkCompareFn = NULL;
23,658,838✔
2629
  if (pCtx->hasPrimaryKey) {
23,658,838✔
2630
    pInfo->pkType = pkCol->info.type;
292,215✔
2631
    pInfo->pkBytes = pkCol->info.bytes;
292,215✔
2632
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
292,215✔
2633
  }
2634

2635
  // All null data column, return directly.
2636
  if (pInput->colDataSMAIsSet && (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows) &&
23,740,451!
2637
      pInputCol->hasNull == true) {
×
2638
    // save selectivity value for column consisted of all null values
2639
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
×
2640
    if (code != TSDB_CODE_SUCCESS) {
×
2641
      return code;
×
2642
    }
2643
    pInfo->nullTupleSaved = true;
×
2644
    return TSDB_CODE_SUCCESS;
×
2645
  }
2646

2647
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
23,740,451!
2648

2649
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
23,740,451!
2650
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
23,740,451!
2651

2652
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
23,740,451✔
2653

2654
  //  please ref. to the comment in lastRowFunction for the reason why disabling the opt version of last/first function.
2655
#if 0
2656
  if (blockDataOrder == TSDB_ORDER_ASC) {
2657
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
2658
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2659
        continue;
2660
      }
2661

2662
      numOfElems++;
2663

2664
      char* data = colDataGetData(pInputCol, i);
2665
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2666
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2667
        doSaveCurrentVal(pCtx, i, cts, type, data);
2668
      }
2669

2670
      break;
2671
    }
2672
  } else {  // descending order
2673
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2674
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2675
        continue;
2676
      }
2677

2678
      numOfElems++;
2679

2680
      char* data = colDataGetData(pInputCol, i);
2681
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2682
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2683
        doSaveCurrentVal(pCtx, i, cts, type, data);
2684
      }
2685
      break;
2686
    }
2687
  }
2688
#else
2689
  int64_t* pts = (int64_t*)pInput->pPTS->pData;
23,740,451✔
2690

2691
#if 0
2692
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2693
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2694
        continue;
2695
      }
2696

2697
      numOfElems++;
2698
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
2699
        char* data = colDataGetData(pInputCol, i);
2700
        doSaveCurrentVal(pCtx, i, pts[i], type, data);
2701
        pResInfo->numOfRes = 1;
2702
      }
2703
    }
2704
#else
2705

2706
// todo refactor
2707
  if (!pInputCol->hasNull && !pCtx->hasPrimaryKey) {
36,619,925✔
2708
    numOfElems = 1;
12,930,907✔
2709

2710
    int32_t round = pInput->numOfRows >> 2;
12,930,907✔
2711
    int32_t reminder = pInput->numOfRows & 0x03;
12,930,907✔
2712

2713
    for (int32_t i = pInput->startRowIndex, tick = 0; tick < round; i += 4, tick += 1) {
20,525,009✔
2714
      int64_t cts = pts[i];
7,593,605✔
2715
      int32_t chosen = i;
7,593,605✔
2716

2717
      if (cts < pts[i + 1]) {
7,593,605✔
2718
        cts = pts[i + 1];
3,927,732✔
2719
        chosen = i + 1;
3,927,732✔
2720
      }
2721

2722
      if (cts < pts[i + 2]) {
7,593,605✔
2723
        cts = pts[i + 2];
3,926,265✔
2724
        chosen = i + 2;
3,926,265✔
2725
      }
2726

2727
      if (cts < pts[i + 3]) {
7,593,605✔
2728
        cts = pts[i + 3];
3,926,586✔
2729
        chosen = i + 3;
3,926,586✔
2730
      }
2731

2732
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
7,593,605✔
2733
        char*   data = colDataGetData(pInputCol, chosen);
2,829,199!
2734
        int32_t code = doSaveCurrentVal(pCtx, i, cts, NULL, type, data);
2,829,199✔
2735
        if (code != TSDB_CODE_SUCCESS) {
2,829,696!
2736
          return code;
×
2737
        }
2738
        pResInfo->numOfRes = 1;
2,829,696✔
2739
      }
2740
    }
2741

2742
    for (int32_t i = pInput->startRowIndex + round * 4; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
27,163,019✔
2743
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
14,283,545✔
2744
        char*   data = colDataGetData(pInputCol, i);
5,921,119!
2745
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], NULL, type, data);
5,921,119✔
2746
        if (code != TSDB_CODE_SUCCESS) {
5,869,189!
2747
          return code;
×
2748
        }
2749
        pResInfo->numOfRes = 1;
5,869,189✔
2750
      }
2751
    }
2752
  } else {
2753
    int from = -1;
10,809,544✔
2754
    int32_t i = -1;
10,809,544✔
2755
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
105,265,864✔
2756
      if (colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
188,953,690✔
2757
        continue;
9,358,349✔
2758
      }
2759

2760
      numOfElems++;
85,118,496✔
2761
      char* pkData = NULL;
85,118,496✔
2762
      if (pCtx->hasPrimaryKey) {
85,118,496✔
2763
        pkData = colDataGetData(pkCol, i);
1,474,904!
2764
      }
2765
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i] ||
85,118,496✔
2766
          (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
79,266,526!
2767
        char*   data = colDataGetData(pInputCol, i);
6,077,348!
2768
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], pkData, type, data);
6,077,348✔
2769
        if (code != TSDB_CODE_SUCCESS) {
6,056,831!
2770
          return code;
×
2771
        }
2772
        pResInfo->numOfRes = 1;
6,056,831✔
2773
      }
2774
    }
2775
  }
2776
#endif
2777

2778
#endif
2779

2780
  // save selectivity value for column consisted of all null values
2781
  if (numOfElems == 0) {
23,777,577✔
2782
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
4,083,310✔
2783
    if (code != TSDB_CODE_SUCCESS) {
4,078,564!
2784
      return code;
×
2785
    }
2786
    pInfo->nullTupleSaved = true;
4,078,564✔
2787
  }
2788

2789
  return TSDB_CODE_SUCCESS;
23,772,831✔
2790
}
2791

2792
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) {
21,066,917✔
2793
  if (!pInput->hasResult) {
21,066,917✔
2794
    return false;
2✔
2795
  }
2796
  __compar_fn_t pkCompareFn = NULL;
21,066,915✔
2797
  if (pInput->pkData) {
21,066,915✔
2798
    pkCompareFn = getKeyComparFunc(pInput->pkType, (isFirst) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC);
27,713✔
2799
  }
2800
  if (pOutput->hasResult) {
21,066,915✔
2801
    if (isFirst) {
13,332,388✔
2802
      if (pInput->ts > pOutput->ts ||
8,583,280✔
2803
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
8,582,758✔
2804
        return false;
898✔
2805
      }
2806
    } else {
2807
      if (pInput->ts < pOutput->ts ||
4,749,108✔
2808
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
4,667,116✔
2809
        return false;
83,258✔
2810
      }
2811
    }
2812
  }
2813

2814
  pOutput->isNull = pInput->isNull;
20,982,759✔
2815
  pOutput->ts = pInput->ts;
20,982,759✔
2816
  pOutput->bytes = pInput->bytes;
20,982,759✔
2817
  pOutput->pkType = pInput->pkType;
20,982,759✔
2818

2819
  (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes);
20,982,759✔
2820
  if (pInput->pkData) {
20,982,759✔
2821
    pOutput->pkBytes = pInput->pkBytes;
25,926✔
2822
    (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes);
25,926✔
2823
    pOutput->pkData = pOutput->buf + pOutput->bytes;
25,926✔
2824
  }
2825
  return true;
20,982,759✔
2826
}
2827

2828
static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst,
21,066,911✔
2829
                                     int32_t rowIndex) {
2830
  if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) {
21,066,911✔
2831
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput, pOutput->nullTupleSaved);
20,982,757✔
2832
    if (TSDB_CODE_SUCCESS != code) {
20,982,757!
2833
      return code;
×
2834
    }
2835
    pOutput->hasResult = true;
20,982,757✔
2836
  }
2837
  return TSDB_CODE_SUCCESS;
21,066,912✔
2838
}
2839

2840
static int32_t firstLastFunctionMergeImpl(SqlFunctionCtx* pCtx, bool isFirstQuery) {
7,763,792✔
2841
  SInputColumnInfoData* pInput = &pCtx->input;
7,763,792✔
2842
  SColumnInfoData*      pCol = pInput->pData[0];
7,763,792✔
2843

2844
  if (IS_NULL_TYPE(pCol->info.type)) {
7,763,792!
2845
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
2846
    return TSDB_CODE_SUCCESS;
×
2847
  }
2848

2849
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
7,763,792!
2850
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2851
  }
2852

2853
  SFirstLastRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,763,792✔
2854

2855
  int32_t start = pInput->startRowIndex;
7,763,792✔
2856
  int32_t numOfElems = 0;
7,763,792✔
2857

2858
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
31,116,047✔
2859
    if (colDataIsNull_s(pCol, i)) {
46,704,508✔
2860
      continue;
2,285,343✔
2861
    }
2862
    char*          data = colDataGetData(pCol, i);
21,066,911!
2863
    SFirstLastRes* pInputInfo = (SFirstLastRes*)varDataVal(data);
21,066,911✔
2864
    if (pCtx->hasPrimaryKey) {
21,066,911✔
2865
      pInputInfo->pkData = pInputInfo->buf + pInputInfo->bytes;
27,712✔
2866
    } else {
2867
      pInputInfo->pkData = NULL;
21,039,199✔
2868
    }
2869

2870
    int32_t code = firstLastTransferInfo(pCtx, pInputInfo, pInfo, isFirstQuery, i);
21,066,911✔
2871
    if (code != TSDB_CODE_SUCCESS) {
21,066,912!
2872
      return code;
×
2873
    }
2874
    if (!numOfElems) {
21,066,912✔
2875
      numOfElems = pInputInfo->hasResult ? 1 : 0;
7,755,797✔
2876
    }
2877
  }
2878

2879
  if (numOfElems == 0) {
7,763,793✔
2880
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
7,995✔
2881
    if (code != TSDB_CODE_SUCCESS) {
7,995!
2882
      return code;
×
2883
    }
2884
    pInfo->nullTupleSaved = true;
7,995✔
2885
  }
2886

2887
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
7,763,793✔
2888
  return TSDB_CODE_SUCCESS;
7,763,793✔
2889
}
2890

2891
int32_t firstFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, true); }
4,276,075✔
2892

2893
int32_t lastFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, false); }
3,487,716✔
2894

2895
int32_t firstLastFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
9,379,659✔
2896
  int32_t          code = TSDB_CODE_SUCCESS;
9,379,659✔
2897
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
9,379,659✔
2898
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
9,379,659✔
2899
  if (NULL == pCol) {
9,379,638!
2900
    return TSDB_CODE_OUT_OF_RANGE;
×
2901
  }
2902

2903
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
9,379,638✔
2904
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
9,379,638✔
2905

2906
  SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo);
9,379,638✔
2907

2908
  if (pResInfo->isNullRes) {
9,379,638✔
2909
    colDataSetNULL(pCol, pBlock->info.rows);
22,886✔
2910
    return setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, pBlock->info.rows);
22,886✔
2911
  }
2912
  code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes);
9,356,752!
2913
  if (TSDB_CODE_SUCCESS != code) {
9,356,813!
2914
    return code;
×
2915
  }
2916

2917
  // handle selectivity
2918
  code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
9,356,813✔
2919

2920
  return code;
9,356,815✔
2921
}
2922

2923
int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
25,179,720✔
2924
  int32_t code = TSDB_CODE_SUCCESS;
25,179,720✔
2925

2926
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
25,179,720✔
2927
  SFirstLastRes*       pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
25,179,720✔
2928

2929
  int32_t resultBytes = getFirstLastInfoSize(pRes->bytes, pRes->pkBytes);
25,179,720✔
2930

2931
  // todo check for failure
2932
  char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
25,117,355✔
2933
  if (NULL == res) {
25,550,333!
2934
    return terrno;
×
2935
  }
2936
  (void)memcpy(varDataVal(res), pRes, resultBytes);
25,550,333✔
2937

2938
  varDataSetLen(res, resultBytes);
25,550,333✔
2939

2940
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
25,550,333✔
2941
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
25,550,333✔
2942
  if (NULL == pCol) {
25,465,072!
2943
    taosMemoryFree(res);
×
2944
    return TSDB_CODE_OUT_OF_RANGE;
×
2945
  }
2946

2947
  if (pEntryInfo->numOfRes == 0) {
25,478,452✔
2948
    colDataSetNULL(pCol, pBlock->info.rows);
2,383,667!
2949
    code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, pBlock->info.rows);
2,383,667✔
2950
  } else {
2951
    code = colDataSetVal(pCol, pBlock->info.rows, res, false);
23,094,785✔
2952
    if (TSDB_CODE_SUCCESS != code) {
22,806,135!
2953
      taosMemoryFree(res);
×
2954
      return code;
×
2955
    }
2956
    code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
22,806,135✔
2957
  }
2958
  taosMemoryFree(res);
25,146,368✔
2959
  return code;
25,444,507✔
2960
}
2961

2962
int32_t lastCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
3✔
2963
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
3✔
2964
  SFirstLastRes*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
3✔
2965
  int32_t              bytes = pDBuf->bytes;
3✔
2966

2967
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
2968
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
2969

2970
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, false);
3✔
2971
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
3✔
2972
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
3✔
2973
  return TSDB_CODE_SUCCESS;
3✔
2974
}
2975

2976
static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex, int64_t cts, SFirstLastRes* pInfo) {
89,126✔
2977
  SInputColumnInfoData* pInput = &pCtx->input;
89,126✔
2978
  SColumnInfoData*      pInputCol = pInput->pData[0];
89,126✔
2979
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
89,126✔
2980

2981

2982
  if (colDataIsNull_s(pInputCol, rowIndex)) {
178,252✔
2983
    pInfo->isNull = true;
6,565✔
2984
  } else {
2985
    pInfo->isNull = false;
82,561✔
2986

2987
    if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
82,561!
2988
      pInfo->bytes = varDataTLen(pData);
23,084✔
2989
    }
2990

2991
    (void)memcpy(pInfo->buf, pData, pInfo->bytes);
82,561✔
2992
  }
2993

2994
  if (pCtx->hasPrimaryKey && !colDataIsNull_s(pkCol, rowIndex)) {
158,186✔
2995
    char* pkData = colDataGetData(pkCol, rowIndex);
69,054!
2996
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
69,054!
2997
      pInfo->pkBytes = varDataTLen(pkData);
22,883✔
2998
    }
2999
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
69,054✔
3000
    pInfo->pkData = pInfo->buf + pInfo->bytes;
69,054✔
3001
  }
3002
  pInfo->ts = cts;
89,126✔
3003
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
89,126✔
3004
  if (code != TSDB_CODE_SUCCESS) {
89,125!
3005
    return code;
×
3006
  }
3007

3008
  pInfo->hasResult = true;
89,125✔
3009

3010
  return TSDB_CODE_SUCCESS;
89,125✔
3011
}
3012

3013
int32_t lastRowFunction(SqlFunctionCtx* pCtx) {
298,670✔
3014
  int32_t numOfElems = 0;
298,670✔
3015

3016
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
298,670✔
3017
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
298,670✔
3018

3019
  SInputColumnInfoData* pInput = &pCtx->input;
298,670✔
3020
  SColumnInfoData*      pInputCol = pInput->pData[0];
298,670✔
3021

3022
  int32_t type = pInputCol->info.type;
298,670✔
3023
  int32_t bytes = pInputCol->info.bytes;
298,670✔
3024
  pInfo->bytes = bytes;
298,670✔
3025

3026
  if (IS_NULL_TYPE(type)) {
298,670✔
3027
    return TSDB_CODE_SUCCESS;
40✔
3028
  }
3029
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
298,630✔
3030
  pInfo->pkType = -1;
298,630✔
3031
  __compar_fn_t  pkCompareFn = NULL;
298,630✔
3032
  if (pCtx->hasPrimaryKey) {
298,630✔
3033
    pInfo->pkType = pkCol->info.type;
269,636✔
3034
    pInfo->pkBytes = pkCol->info.bytes;
269,636✔
3035
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
269,636✔
3036
  }
3037
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
298,712✔
3038
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
298,712!
3039

3040
#if 0
3041
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
3042

3043
  // the optimized version only valid if all tuples in one block are monotonious increasing or descreasing.
3044
  // this assumption is NOT always works if project operator exists in downstream.
3045
  if (blockDataOrder == TSDB_ORDER_ASC) {
3046
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
3047
      char* data = colDataGetData(pInputCol, i);
3048
      TSKEY cts = getRowPTs(pInput->pPTS, i);
3049
      numOfElems++;
3050

3051
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
3052
        doSaveLastrow(pCtx, data, i, cts, pInfo);
3053
      }
3054

3055
      break;
3056
    }
3057
  } else {  // descending order
3058
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
3059
      char* data = colDataGetData(pInputCol, i);
3060
      TSKEY cts = getRowPTs(pInput->pPTS, i);
3061
      numOfElems++;
3062

3063
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
3064
        doSaveLastrow(pCtx, data, i, cts, pInfo);
3065
      }
3066
      break;
3067
    }
3068
  }
3069
#else
3070

3071
  int64_t* pts = (int64_t*)pInput->pPTS->pData;
298,712✔
3072
  int from = -1;
298,712✔
3073
  int32_t i = -1;
298,712✔
3074
  while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
2,015,628✔
3075
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
1,719,729✔
3076
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
1,719,729!
3077
    TSKEY cts = pts[i];
1,719,729✔
3078

3079
    numOfElems++;
1,719,729✔
3080
    char* pkData = NULL;
1,719,729✔
3081
    if (pCtx->hasPrimaryKey) {
1,719,729✔
3082
      pkData = colDataGetData(pkCol, i);
1,411,206!
3083
    }
3084
    if (pResInfo->numOfRes == 0 || pInfo->ts < cts ||
1,719,729✔
3085
        (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
1,632,119✔
3086
      int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
87,616✔
3087
      if (code != TSDB_CODE_SUCCESS) {
84,809!
3088
        return code;
×
3089
      }
3090
      pResInfo->numOfRes = 1;
84,809✔
3091
    }
3092
  }
3093

3094
#endif
3095

3096
  SET_VAL(pResInfo, numOfElems, 1);
294,222!
3097
  return TSDB_CODE_SUCCESS;
294,222✔
3098
}
3099

3100
bool getDiffFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
4,798✔
3101
  pEnv->calcMemSize = sizeof(SDiffInfo);
4,798✔
3102
  return true;
4,798✔
3103
}
3104

3105
int32_t diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
381,289✔
3106
  if (pResInfo->initialized) {
381,289✔
3107
    return TSDB_CODE_SUCCESS;
324,330✔
3108
  }
3109
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
56,959!
3110
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3111
  }
3112
  SDiffInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
56,959✔
3113
  pDiffInfo->hasPrev = false;
56,959✔
3114
  pDiffInfo->isFirstRow = true;
56,959✔
3115
  pDiffInfo->prev.i64 = 0;
56,959✔
3116
  pDiffInfo->prevTs = -1;
56,959✔
3117
  if (pCtx->numOfParams > 1) {
56,959!
3118
    pDiffInfo->ignoreOption = pCtx->param[1].param.i;  // TODO set correct param
56,959✔
3119
  } else {
3120
    pDiffInfo->ignoreOption = 0;
×
3121
  }
3122
  return TSDB_CODE_SUCCESS;
56,959✔
3123
}
3124

3125
static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, int64_t ts) {
55,849✔
3126
  switch (type) {
55,849!
3127
    case TSDB_DATA_TYPE_BOOL:
38✔
3128
      pDiffInfo->prev.i64 = *(bool*)pv ? 1 : 0;
38✔
3129
      break;
38✔
3130
    case TSDB_DATA_TYPE_UTINYINT:
423✔
3131
    case TSDB_DATA_TYPE_TINYINT:
3132
      pDiffInfo->prev.i64 = *(int8_t*)pv;
423✔
3133
      break;
423✔
3134
    case TSDB_DATA_TYPE_UINT:
53,669✔
3135
    case TSDB_DATA_TYPE_INT:
3136
      pDiffInfo->prev.i64 = *(int32_t*)pv;
53,669✔
3137
      break;
53,669✔
3138
    case TSDB_DATA_TYPE_USMALLINT:
378✔
3139
    case TSDB_DATA_TYPE_SMALLINT:
3140
      pDiffInfo->prev.i64 = *(int16_t*)pv;
378✔
3141
      break;
378✔
3142
    case TSDB_DATA_TYPE_TIMESTAMP:
606✔
3143
    case TSDB_DATA_TYPE_UBIGINT:
3144
    case TSDB_DATA_TYPE_BIGINT:
3145
      pDiffInfo->prev.i64 = *(int64_t*)pv;
606✔
3146
      break;
606✔
3147
    case TSDB_DATA_TYPE_FLOAT:
277✔
3148
      pDiffInfo->prev.d64 = *(float*)pv;
277✔
3149
      break;
277✔
3150
    case TSDB_DATA_TYPE_DOUBLE:
458✔
3151
      pDiffInfo->prev.d64 = *(double*)pv;
458✔
3152
      break;
458✔
3153
    default:
×
3154
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3155
  }
3156
  pDiffInfo->prevTs = ts;
55,849✔
3157
  pDiffInfo->hasPrev = true;
55,849✔
3158
  return TSDB_CODE_SUCCESS;
55,849✔
3159
}
3160

3161
static bool diffIsNegtive(SDiffInfo* pDiffInfo, int32_t type, const char* pv) {
33,590✔
3162
  switch (type) {
33,590!
3163
    case TSDB_DATA_TYPE_UINT: {
×
3164
      int64_t v = *(uint32_t*)pv;
×
3165
      return v < pDiffInfo->prev.i64;
×
3166
    }
3167
    case TSDB_DATA_TYPE_INT: {
4,739✔
3168
      int64_t v = *(int32_t*)pv;
4,739✔
3169
      return v < pDiffInfo->prev.i64;
4,739✔
3170
    }
3171
    case TSDB_DATA_TYPE_BOOL: {
×
3172
      int64_t v = *(bool*)pv;
×
3173
      return v < pDiffInfo->prev.i64;
×
3174
    }
3175
    case TSDB_DATA_TYPE_UTINYINT: {
×
3176
      int64_t v = *(uint8_t*)pv;
×
3177
      return v < pDiffInfo->prev.i64;
×
3178
    }
3179
    case TSDB_DATA_TYPE_TINYINT: {
9,163✔
3180
      int64_t v = *(int8_t*)pv;
9,163✔
3181
      return v < pDiffInfo->prev.i64;
9,163✔
3182
    }
3183
    case TSDB_DATA_TYPE_USMALLINT: {
×
3184
      int64_t v = *(uint16_t*)pv;
×
3185
      return v < pDiffInfo->prev.i64;
×
3186
    }
3187
    case TSDB_DATA_TYPE_SMALLINT: {
5,792✔
3188
      int64_t v = *(int16_t*)pv;
5,792✔
3189
      return v < pDiffInfo->prev.i64;
5,792✔
3190
    }
3191
    case TSDB_DATA_TYPE_UBIGINT:{
40✔
3192
      uint64_t v = *(uint64_t*)pv;
40✔
3193
      return v < (uint64_t)pDiffInfo->prev.i64;
40✔
3194
    }
3195
    case TSDB_DATA_TYPE_TIMESTAMP:
4,398✔
3196
    case TSDB_DATA_TYPE_BIGINT: {
3197
      int64_t v = *(int64_t*)pv;
4,398✔
3198
      return v < pDiffInfo->prev.i64;
4,398✔
3199
    }
3200
    case TSDB_DATA_TYPE_FLOAT: {
5,492✔
3201
      float v = *(float*)pv;
5,492✔
3202
      return v < pDiffInfo->prev.d64;
5,492✔
3203
    }
3204
    case TSDB_DATA_TYPE_DOUBLE: {
3,966✔
3205
      double v = *(double*)pv;
3,966✔
3206
      return v < pDiffInfo->prev.d64;
3,966✔
3207
    }
3208
    default:
×
3209
      return false;
×
3210
  }
3211

3212
  return false;
3213
}
3214

3215
static void tryToSetInt64(SDiffInfo* pDiffInfo, int32_t type, SColumnInfoData* pOutput, int64_t v, int32_t pos) {
30,589,563✔
3216
  bool isNegative = v < pDiffInfo->prev.i64;
30,589,563✔
3217
  if(type == TSDB_DATA_TYPE_UBIGINT){
30,589,563✔
3218
    isNegative = (uint64_t)v < (uint64_t)pDiffInfo->prev.i64;
3,745✔
3219
  }
3220
  int64_t delta = v - pDiffInfo->prev.i64;
30,589,563✔
3221
  if (isNegative && ignoreNegative(pDiffInfo->ignoreOption)) {
30,589,563✔
3222
    colDataSetNull_f_s(pOutput, pos);
11,802✔
3223
    pOutput->hasNull = true;
11,802✔
3224
  } else {
3225
    colDataSetInt64(pOutput, pos, &delta);
30,577,761✔
3226
  }
3227
  pDiffInfo->prev.i64 = v;
30,589,563✔
3228
}
30,589,563✔
3229

3230
static void tryToSetDouble(SDiffInfo* pDiffInfo, SColumnInfoData* pOutput, double v, int32_t pos) {
96,681✔
3231
  double delta = v - pDiffInfo->prev.d64;
96,681✔
3232
  if (delta < 0 && ignoreNegative(pDiffInfo->ignoreOption)) {
96,681✔
3233
    colDataSetNull_f_s(pOutput, pos);
4,770✔
3234
  } else {
3235
    colDataSetDouble(pOutput, pos, &delta);
91,911✔
3236
  }
3237
  pDiffInfo->prev.d64 = v;
96,681✔
3238
}
96,681✔
3239

3240
static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, SColumnInfoData* pOutput, int32_t pos,
30,686,857✔
3241
                            int64_t ts) {
3242
  if (!pDiffInfo->hasPrev) {
30,686,857✔
3243
    colDataSetNull_f_s(pOutput, pos);
613✔
3244
    return doSetPrevVal(pDiffInfo, type, pv, ts);
613✔
3245
  }
3246
  pDiffInfo->prevTs = ts;
30,686,244✔
3247
  switch (type) {
30,686,244!
3248
    case TSDB_DATA_TYPE_UINT: {
3,675✔
3249
      int64_t v = *(uint32_t*)pv;
3,675✔
3250
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
3,675✔
3251
      break;
3,675✔
3252
    }
3253
    case TSDB_DATA_TYPE_INT: {
26,250,812✔
3254
      int64_t v = *(int32_t*)pv;
26,250,812✔
3255
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
26,250,812✔
3256
      break;
26,250,812✔
3257
    }
3258
    case TSDB_DATA_TYPE_BOOL: {
10,877✔
3259
      int64_t v = *(bool*)pv;
10,877✔
3260
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
10,877✔
3261
      break;
10,877✔
3262
    }
3263
    case TSDB_DATA_TYPE_UTINYINT: {
675✔
3264
      int64_t v = *(uint8_t*)pv;
675✔
3265
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
675✔
3266
      break;
675✔
3267
    }
3268
    case TSDB_DATA_TYPE_TINYINT: {
41,419✔
3269
      int64_t v = *(int8_t*)pv;
41,419✔
3270
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
41,419✔
3271
      break;
41,419✔
3272
    }
3273
    case TSDB_DATA_TYPE_USMALLINT:{
675✔
3274
      int64_t v = *(uint16_t*)pv;
675✔
3275
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
675✔
3276
      break;
675✔
3277
    }
3278
    case TSDB_DATA_TYPE_SMALLINT: {
36,313✔
3279
      int64_t v = *(int16_t*)pv;
36,313✔
3280
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
36,313✔
3281
      break;
36,313✔
3282
    }
3283
    case TSDB_DATA_TYPE_TIMESTAMP:
4,245,117✔
3284
    case TSDB_DATA_TYPE_UBIGINT:
3285
    case TSDB_DATA_TYPE_BIGINT: {
3286
      int64_t v = *(int64_t*)pv;
4,245,117✔
3287
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
4,245,117✔
3288
      break;
4,245,117✔
3289
    }
3290
    case TSDB_DATA_TYPE_FLOAT: {
48,588✔
3291
      double v = *(float*)pv;
48,588✔
3292
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
48,588✔
3293
      break;
48,588✔
3294
    }
3295
    case TSDB_DATA_TYPE_DOUBLE: {
48,093✔
3296
      double v = *(double*)pv;
48,093✔
3297
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
48,093✔
3298
      break;
48,093✔
3299
    }
3300
    default:
×
3301
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3302
  }
3303
  pDiffInfo->hasPrev = true;
30,686,244✔
3304
  return TSDB_CODE_SUCCESS;
30,686,244✔
3305
}
3306

3307
//TODO: the primary key compare can be skipped for ordered pk if knonwn before
3308
//TODO: for desc ordered, pk shall select the smallest one for one ts. if across block boundaries.
3309
bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex, int32_t* nextFrom) {
185,951,038✔
3310
  if (pInput->pPrimaryKey == NULL) {
185,951,038✔
3311
    if (from == -1) {
180,969,769✔
3312
      from = pInput->startRowIndex;
40,912,659✔
3313
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
140,057,110✔
3314
      return false;
40,495,120✔
3315
    }
3316
    *pRowIndex = from;
140,474,649✔
3317
    *nextFrom = from + 1;
140,474,649✔
3318
    return true;
140,474,649✔
3319
  } else {
3320
    if (from == -1) {
4,981,269✔
3321
      from = pInput->startRowIndex;
814,166✔
3322
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
4,167,103✔
3323
      return false;
814,177✔
3324
    }
3325
    TSKEY* tsList = (int64_t*)pInput->pPTS->pData;
4,167,092✔
3326
    SColumnInfoData* pkCol = pInput->pPrimaryKey;
4,167,092✔
3327
    int8_t pkType = pkCol->info.type;
4,167,092✔
3328
    int32_t order = (firstOccur) ? TSDB_ORDER_ASC: TSDB_ORDER_DESC;
4,167,092✔
3329
    __compar_fn_t compareFunc = getKeyComparFunc(pkType, order);
4,167,092✔
3330
    int32_t select = from;
4,236,213✔
3331
    char* val = colDataGetData(pkCol, select);
4,236,213!
3332
    while (from < pInput->numOfRows + pInput->startRowIndex - 1  &&  tsList[from + 1] == tsList[from]) {
10,610,292✔
3333
      char* val1 = colDataGetData(pkCol, from + 1);
6,375,567!
3334
      if (compareFunc(val1, val) < 0)  {
6,375,567✔
3335
        select = from + 1;
1,925,960✔
3336
        val = val1;
1,925,960✔
3337
      }
3338
      from = from + 1;
6,374,079✔
3339
    }
3340
    *pRowIndex = select;
4,234,725✔
3341
    *nextFrom = from + 1;
4,234,725✔
3342
    return true;
4,234,725✔
3343
  }
3344
}
3345

3346
bool getForecastConfEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
3347
  pEnv->calcMemSize = sizeof(float);
×
3348
  return true;
×
3349
}
3350

3351
int32_t diffResultIsNull(SqlFunctionCtx* pCtx, SFuncInputRow* pRow){
30,835,117✔
3352
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
30,835,117✔
3353
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,835,117✔
3354

3355
  if (pRow->isDataNull || !pDiffInfo->hasPrev ) {
30,835,117✔
3356
    return true;
148,765✔
3357
  }  else if (ignoreNegative(pDiffInfo->ignoreOption)){
30,686,352✔
3358
    return diffIsNegtive(pDiffInfo, pCtx->input.pData[0]->info.type, pRow->pData);
33,590✔
3359
  }
3360
  return false;
30,652,762✔
3361
}
3362

3363
bool isFirstRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
30,834,642✔
3364
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
30,834,642✔
3365
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,834,642✔
3366
  return pDiffInfo->isFirstRow;
30,834,642✔
3367
}
3368

3369
int32_t trySetPreVal(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
57,177✔
3370
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
57,177✔
3371
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
57,177✔
3372
  pDiffInfo->isFirstRow = false;
57,177✔
3373
  if (pRow->isDataNull) {
57,177✔
3374
    return TSDB_CODE_SUCCESS;
1,941✔
3375
  }
3376

3377
  SInputColumnInfoData* pInput = &pCtx->input;
55,236✔
3378
  SColumnInfoData*      pInputCol = pInput->pData[0];
55,236✔
3379
  int8_t                inputType = pInputCol->info.type;
55,236✔
3380

3381
  char*   pv = pRow->pData;
55,236✔
3382
  return doSetPrevVal(pDiffInfo, inputType, pv, pRow->ts);
55,236✔
3383
}
3384

3385
int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) {
30,777,940✔
3386
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
30,777,940✔
3387
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,777,940✔
3388

3389
  SInputColumnInfoData* pInput = &pCtx->input;
30,777,940✔
3390
  SColumnInfoData*      pInputCol = pInput->pData[0];
30,777,940✔
3391
  int8_t                inputType = pInputCol->info.type;
30,777,940✔
3392
  SColumnInfoData*      pOutput = (SColumnInfoData*)pCtx->pOutput;
30,777,940✔
3393
  int32_t               code = TSDB_CODE_SUCCESS;
30,777,940✔
3394
  if (pRow->isDataNull) {
30,777,940✔
3395
    colDataSetNull_f_s(pOutput, pos);
91,060✔
3396
    pOutput->hasNull = true;
91,060✔
3397

3398
    // handle selectivity
3399
    if (pCtx->subsidiaries.num > 0) {
91,060✔
3400
      code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
860✔
3401
      if (code != TSDB_CODE_SUCCESS) {
860!
3402
        return code;
×
3403
      }
3404
    }
3405
    return TSDB_CODE_SUCCESS;
91,060✔
3406
  }
3407

3408
  char* pv = pRow->pData;
30,686,880✔
3409

3410
  if (pRow->ts == pDiffInfo->prevTs) {
30,686,880✔
3411
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
23✔
3412
  }
3413
  code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts);
30,686,857✔
3414
  if (code != TSDB_CODE_SUCCESS) {
30,686,857!
3415
    return code;
×
3416
  }
3417
  // handle selectivity
3418
  if (pCtx->subsidiaries.num > 0) {
30,686,857✔
3419
    code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
30,177,863✔
3420
    if (code != TSDB_CODE_SUCCESS) {
30,177,863!
3421
      return code;
×
3422
    }
3423
  }
3424

3425
  return TSDB_CODE_SUCCESS;
30,686,857✔
3426
}
3427

3428
int32_t diffFunction(SqlFunctionCtx* pCtx) {
376,491✔
3429
  return TSDB_CODE_SUCCESS;
376,491✔
3430
}
3431

3432
int32_t diffFunctionByRow(SArray* pCtxArray) {
376,233✔
3433
  int32_t code = TSDB_CODE_SUCCESS;
376,233✔
3434
  int diffColNum = pCtxArray->size;
376,233✔
3435
  if(diffColNum == 0) {
376,233!
3436
    return TSDB_CODE_SUCCESS;
×
3437
  }
3438
  int32_t numOfElems = 0;
376,233✔
3439

3440
  SArray*  pRows = taosArrayInit_s(sizeof(SFuncInputRow), diffColNum);
376,233✔
3441
  if (NULL == pRows) {
376,233!
3442
    return terrno;
×
3443
  }
3444

3445
  bool keepNull = false;
376,233✔
3446
  for (int i = 0; i < diffColNum; ++i) {
752,724✔
3447
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
376,491✔
3448
    if (NULL == pCtx) {
376,491!
3449
      code = terrno;
×
3450
      goto _exit;
×
3451
    }
3452
    funcInputUpdate(pCtx);
376,491✔
3453
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
376,491✔
3454
    SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
376,491✔
3455
    if (!ignoreNull(pDiffInfo->ignoreOption)) {
376,491✔
3456
      keepNull = true;
376,286✔
3457
    }
3458
  }
3459

3460
  SqlFunctionCtx* pCtx0 = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, 0);
376,233✔
3461
  SFuncInputRow* pRow0 = (SFuncInputRow*)taosArrayGet(pRows, 0);
376,233✔
3462
  if (NULL == pCtx0 || NULL == pRow0) {
376,233!
3463
    code = terrno;
×
3464
    goto _exit;
×
3465
  }
3466
  int32_t startOffset = pCtx0->offset;
376,233✔
3467
  bool    result = false;
376,233✔
3468
  while (1) {
30,817,604✔
3469
    code = funcInputGetNextRow(pCtx0, pRow0, &result);
31,193,837✔
3470
    if (TSDB_CODE_SUCCESS != code) {
31,193,837!
3471
      goto _exit;
×
3472
    }
3473
    if (!result) {
31,193,837✔
3474
      break;
376,210✔
3475
    }
3476
    bool hasNotNullValue = !diffResultIsNull(pCtx0, pRow0);
30,817,627✔
3477
    for (int i = 1; i < diffColNum; ++i) {
30,835,117✔
3478
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
17,490✔
3479
      SFuncInputRow* pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
17,490✔
3480
      if (NULL == pCtx || NULL == pRow) {
17,490!
3481
        code = terrno;
×
3482
        goto _exit;
×
3483
      }
3484
      code = funcInputGetNextRow(pCtx, pRow, &result);
17,490✔
3485
      if (TSDB_CODE_SUCCESS != code) {
17,490!
3486
        goto _exit;
×
3487
      }
3488
      if (!result) {
17,490!
3489
        // rows are not equal
3490
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
3491
        goto _exit;
×
3492
      }
3493
      if (!diffResultIsNull(pCtx, pRow)) {
17,490✔
3494
        hasNotNullValue = true;
16,754✔
3495
      }
3496
    }
3497
    int32_t pos = startOffset + numOfElems;
30,817,627✔
3498

3499
    bool newRow = false;
30,817,627✔
3500
    for (int i = 0; i < diffColNum; ++i) {
61,652,721✔
3501
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
30,835,117✔
3502
      SFuncInputRow*  pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
30,835,117✔
3503
      if (NULL == pCtx || NULL == pRow) {
30,835,117!
3504
        code = terrno;
×
3505
        goto _exit;
×
3506
      }
3507
      if ((keepNull || hasNotNullValue) && !isFirstRow(pCtx, pRow)){
30,835,117✔
3508
        code = setDoDiffResult(pCtx, pRow, pos);
30,777,940✔
3509
        if (code != TSDB_CODE_SUCCESS) {
30,777,940✔
3510
          goto _exit;
23✔
3511
        }
3512
        newRow = true;
30,777,917✔
3513
      } else {
3514
        code = trySetPreVal(pCtx, pRow);
57,177✔
3515
        if (code != TSDB_CODE_SUCCESS) {
57,177!
3516
          goto _exit;
×
3517
        } 
3518
      }
3519
    }
3520
    if (newRow) ++numOfElems;
30,817,604✔
3521
  }
3522

3523
  for (int i = 0; i < diffColNum; ++i) {
752,673✔
3524
    SqlFunctionCtx*      pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
376,463✔
3525
    if (NULL == pCtx) {
376,463!
3526
      code = terrno;
×
3527
      goto _exit;
×
3528
    }
3529
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
376,463✔
3530
    pResInfo->numOfRes = numOfElems;
376,463✔
3531
  }
3532

3533
_exit:
376,210✔
3534
  if (pRows) {
376,233!
3535
    taosArrayDestroy(pRows);
376,233✔
3536
    pRows = NULL;
376,233✔
3537
  }
3538
  return code;
376,233✔
3539
}
3540

3541
int32_t getTopBotInfoSize(int64_t numOfItems) { return sizeof(STopBotRes) + numOfItems * sizeof(STopBotResItem); }
×
3542

3543
bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
18,510✔
3544
  SValueNode* pkNode = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
18,510✔
3545
  pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem);
18,514✔
3546
  return true;
18,514✔
3547
}
3548

3549
int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
35,388✔
3550
  if (pResInfo->initialized) {
35,388!
3551
    return TSDB_CODE_SUCCESS;
×
3552
  }
3553
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
35,388!
3554
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3555
  }
3556

3557
  STopBotRes*           pRes = GET_ROWCELL_INTERBUF(pResInfo);
35,384✔
3558
  SInputColumnInfoData* pInput = &pCtx->input;
35,384✔
3559

3560
  pRes->maxSize = pCtx->param[1].param.i;
35,384✔
3561

3562
  pRes->nullTupleSaved = false;
35,384✔
3563
  pRes->nullTuplePos.pageId = -1;
35,384✔
3564
  return TSDB_CODE_SUCCESS;
35,384✔
3565
}
3566

3567
static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) {
713,482✔
3568
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
713,482✔
3569
  STopBotRes*          pRes = GET_ROWCELL_INTERBUF(pResInfo);
713,482✔
3570
  pRes->pItems = (STopBotResItem*)((char*)pRes + sizeof(STopBotRes));
713,482✔
3571

3572
  return pRes;
713,482✔
3573
}
3574

3575
static int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock,
3576
                               uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery);
3577

3578
static int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery);
3579

3580
int32_t topFunction(SqlFunctionCtx* pCtx) {
36,390✔
3581
  int32_t              numOfElems = 0;
36,390✔
3582
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
36,390✔
3583

3584
  SInputColumnInfoData* pInput = &pCtx->input;
36,390✔
3585
  SColumnInfoData*      pCol = pInput->pData[0];
36,390✔
3586

3587
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
36,390✔
3588
  pRes->type = pInput->pData[0]->info.type;
36,386✔
3589

3590
  int32_t start = pInput->startRowIndex;
36,386✔
3591
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
532,847✔
3592
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
496,114✔
3593
      continue;
26,742✔
3594
    }
3595

3596
    numOfElems++;
469,372✔
3597
    char*   data = colDataGetData(pCol, i);
469,372!
3598
    int32_t code = doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, pRes->type, pInput->uid, pResInfo, true);
469,372✔
3599
    if (code != TSDB_CODE_SUCCESS) {
469,719!
3600
      return code;
×
3601
    }
3602
  }
3603

3604
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pRes->nullTupleSaved) {
36,733✔
3605
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pRes->nullTuplePos);
99✔
3606
    if (code != TSDB_CODE_SUCCESS) {
99!
3607
      return code;
×
3608
    }
3609
    pRes->nullTupleSaved = true;
99✔
3610
  }
3611
  return TSDB_CODE_SUCCESS;
36,733✔
3612
}
3613

3614
int32_t bottomFunction(SqlFunctionCtx* pCtx) {
7,228✔
3615
  int32_t              numOfElems = 0;
7,228✔
3616
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
7,228✔
3617

3618
  SInputColumnInfoData* pInput = &pCtx->input;
7,228✔
3619
  SColumnInfoData*      pCol = pInput->pData[0];
7,228✔
3620

3621
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
7,228✔
3622
  pRes->type = pInput->pData[0]->info.type;
7,228✔
3623

3624
  int32_t start = pInput->startRowIndex;
7,228✔
3625
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
198,651✔
3626
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
191,370✔
3627
      continue;
25,594✔
3628
    }
3629

3630
    numOfElems++;
165,776✔
3631
    char*   data = colDataGetData(pCol, i);
165,776!
3632
    int32_t code = doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, pRes->type, pInput->uid, pResInfo, false);
165,776✔
3633
    if (code != TSDB_CODE_SUCCESS) {
165,829!
3634
      return code;
×
3635
    }
3636
  }
3637

3638
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pRes->nullTupleSaved) {
7,281✔
3639
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pRes->nullTuplePos);
98✔
3640
    if (code != TSDB_CODE_SUCCESS) {
98!
3641
      return code;
×
3642
    }
3643
    pRes->nullTupleSaved = true;
98✔
3644
  }
3645

3646
  return TSDB_CODE_SUCCESS;
7,281✔
3647
}
3648

3649
static int32_t topBotResComparFn(const void* p1, const void* p2, const void* param) {
2,239,698✔
3650
  uint16_t type = *(uint16_t*)param;
2,239,698✔
3651

3652
  STopBotResItem* val1 = (STopBotResItem*)p1;
2,239,698✔
3653
  STopBotResItem* val2 = (STopBotResItem*)p2;
2,239,698✔
3654

3655
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
2,239,698!
3656
    if (val1->v.i == val2->v.i) {
1,191,147✔
3657
      return 0;
243,358✔
3658
    }
3659

3660
    return (val1->v.i > val2->v.i) ? 1 : -1;
947,789✔
3661
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
1,048,551!
3662
    if (val1->v.u == val2->v.u) {
828,415✔
3663
      return 0;
178,157✔
3664
    }
3665

3666
    return (val1->v.u > val2->v.u) ? 1 : -1;
650,258✔
3667
  } else if (TSDB_DATA_TYPE_FLOAT == type) {
220,136✔
3668
    if (val1->v.f == val2->v.f) {
35,534✔
3669
      return 0;
63✔
3670
    }
3671

3672
    return (val1->v.f > val2->v.f) ? 1 : -1;
35,471✔
3673
  }
3674

3675
  if (val1->v.d == val2->v.d) {
184,602✔
3676
    return 0;
11✔
3677
  }
3678

3679
  return (val1->v.d > val2->v.d) ? 1 : -1;
184,591✔
3680
}
3681

3682
int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type,
634,995✔
3683
                        uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery) {
3684
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
634,995✔
3685
  int32_t     code = TSDB_CODE_SUCCESS;
634,916✔
3686

3687
  SVariant val = {0};
634,916✔
3688
  TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type));
634,916!
3689

3690
  STopBotResItem* pItems = pRes->pItems;
635,406✔
3691

3692
  // not full yet
3693
  if (pEntryInfo->numOfRes < pRes->maxSize) {
635,406✔
3694
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
154,720✔
3695
    pItem->v = val;
154,720✔
3696
    pItem->uid = uid;
154,720✔
3697

3698
    // save the data of this tuple
3699
    if (pCtx->subsidiaries.num > 0) {
154,720✔
3700
      code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
76,230✔
3701
      if (code != TSDB_CODE_SUCCESS) {
76,091!
3702
        return code;
×
3703
      }
3704
    }
3705
#ifdef BUF_PAGE_DEBUG
3706
    qDebug("page_saveTuple i:%d, item:%p,pageId:%d, offset:%d\n", pEntryInfo->numOfRes, pItem, pItem->tuplePos.pageId,
3707
           pItem->tuplePos.offset);
3708
#endif
3709
    // allocate the buffer and keep the data of this row into the new allocated buffer
3710
    pEntryInfo->numOfRes++;
154,581✔
3711
    code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
154,581✔
3712
                        topBotResComparFn, !isTopQuery);
154,581✔
3713
    if (code != TSDB_CODE_SUCCESS) {
154,975!
3714
      return code;
×
3715
    }
3716
  } else {  // replace the minimum value in the result
3717
    if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) ||
480,686!
3718
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) ||
240,006!
3719
                        (TSDB_DATA_TYPE_FLOAT == type && val.f > pItems[0].v.f) ||
145,863✔
3720
                        (TSDB_DATA_TYPE_DOUBLE == type && val.d > pItems[0].v.d))) ||
145,018✔
3721
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i < pItems[0].v.i) ||
261,235!
3722
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u < pItems[0].v.u) ||
125,923!
3723
                         (TSDB_DATA_TYPE_FLOAT == type && val.f < pItems[0].v.f) ||
122,034✔
3724
                         (TSDB_DATA_TYPE_DOUBLE == type && val.d < pItems[0].v.d)))) {
120,120✔
3725
      // replace the old data and the coresponding tuple data
3726
      STopBotResItem* pItem = &pItems[0];
235,856✔
3727
      pItem->v = val;
235,856✔
3728
      pItem->uid = uid;
235,856✔
3729

3730
      // save the data of this tuple by over writing the old data
3731
      if (pCtx->subsidiaries.num > 0) {
235,856✔
3732
        code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
194,434✔
3733
        if (code != TSDB_CODE_SUCCESS) {
193,374!
3734
          return code;
×
3735
        }
3736
      }
3737
#ifdef BUF_PAGE_DEBUG
3738
      qDebug("page_copyTuple pageId:%d, offset:%d", pItem->tuplePos.pageId, pItem->tuplePos.offset);
3739
#endif
3740
      code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type,
234,796✔
3741
                     topBotResComparFn, NULL, !isTopQuery);
234,796✔
3742
      if (code != TSDB_CODE_SUCCESS) {
235,798!
3743
        return code;
×
3744
      }
3745
    }
3746
  }
3747

3748
  return TSDB_CODE_SUCCESS;
635,603✔
3749
}
3750

3751
/*
3752
 * +------------------------------------+--------------+--------------+
3753
 * |            null bitmap             |              |              |
3754
 * |(n columns, one bit for each column)| src column #1| src column #2|
3755
 * +------------------------------------+--------------+--------------+
3756
 */
3757
int32_t serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsidiaryResInfo* pSubsidiaryies,
715,408✔
3758
                         char* buf, char** res) {
3759
  char* nullList = buf;
715,408✔
3760
  char* pStart = (char*)(nullList + sizeof(bool) * pSubsidiaryies->num);
715,408✔
3761

3762
  int32_t offset = 0;
715,408✔
3763
  for (int32_t i = 0; i < pSubsidiaryies->num; ++i) {
1,542,399✔
3764
    SqlFunctionCtx* pc = pSubsidiaryies->pCtx[i];
827,048✔
3765

3766
    // group_key function has its own process function
3767
    // do not process there
3768
    if (fmIsGroupKeyFunc(pc->functionId)) {
827,048✔
3769
      continue;
53,334✔
3770
    }
3771

3772
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
773,708✔
3773
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
773,708✔
3774

3775
    SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
773,708✔
3776
    if (NULL == pCol) {
773,657!
3777
      return TSDB_CODE_OUT_OF_RANGE;
×
3778
    }
3779
    if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) {
1,547,314✔
3780
      offset += pCol->info.bytes;
472✔
3781
      continue;
472✔
3782
    }
3783

3784
    char* p = colDataGetData(pCol, rowIndex);
773,185!
3785
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
773,185!
3786
      (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p));
49,916!
3787
    } else {
3788
      (void)memcpy(pStart + offset, p, pCol->info.bytes);
723,269✔
3789
    }
3790

3791
    offset += pCol->info.bytes;
773,185✔
3792
  }
3793

3794
  *res = buf;
715,351✔
3795
  return TSDB_CODE_SUCCESS;
715,351✔
3796
}
3797

3798
static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, SWinKey* key,
678,822✔
3799
                               STuplePos* pPos, SFunctionStateStore* pStore) {
3800
  STuplePos p = {0};
678,822✔
3801
  if (pHandle->pBuf != NULL) {
678,822✔
3802
    SFilePage* pPage = NULL;
601,953✔
3803

3804
    if (pHandle->currentPage == -1) {
601,953✔
3805
      pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
38,182✔
3806
      if (pPage == NULL) {
38,188!
3807
        return terrno;
×
3808
      }
3809
      pPage->num = sizeof(SFilePage);
38,188✔
3810
    } else {
3811
      pPage = getBufPage(pHandle->pBuf, pHandle->currentPage);
563,771✔
3812
      if (pPage == NULL) {
563,703!
3813
        return terrno;
×
3814
      }
3815
      if (pPage->num + length > getBufPageSize(pHandle->pBuf)) {
563,703✔
3816
        // current page is all used, let's prepare a new buffer page
3817
        releaseBufPage(pHandle->pBuf, pPage);
2,616✔
3818
        pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
2,616✔
3819
        if (pPage == NULL) {
2,616!
3820
          return terrno;
×
3821
        }
3822
        pPage->num = sizeof(SFilePage);
2,616✔
3823
      }
3824
    }
3825

3826
    p = (STuplePos){.pageId = pHandle->currentPage, .offset = pPage->num};
601,868✔
3827
    (void)memcpy(pPage->data + pPage->num, pBuf, length);
601,868✔
3828

3829
    pPage->num += length;
601,868✔
3830
    setBufPageDirty(pPage, true);
601,868✔
3831
    releaseBufPage(pHandle->pBuf, pPage);
601,520✔
3832
  } else { // other tuple save policy
3833
    if (pStore->streamStateFuncPut(pHandle->pState, key, pBuf, length) >= 0) {
76,869!
3834
      p.streamTupleKey = *key;
76,857✔
3835
    }
3836
  }
3837

3838
  *pPos = p;
678,215✔
3839
  return TSDB_CODE_SUCCESS;
678,215✔
3840
}
3841

3842
int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
259,273✔
3843
  int32_t code = prepareBuf(pCtx);
259,273✔
3844
  if (TSDB_CODE_SUCCESS != code) {
259,311!
3845
    return code;
×
3846
  }
3847

3848
  SWinKey key = {0};
259,311✔
3849
  if (pCtx->saveHandle.pBuf == NULL) {
259,311✔
3850
    SColumnInfoData* pColInfo = taosArrayGet(pSrcBlock->pDataBlock, pCtx->saveHandle.pState->tsIndex);
76,857✔
3851
    if (NULL == pColInfo) {
76,857!
3852
      return TSDB_CODE_OUT_OF_RANGE;
×
3853
    }
3854
    if (pColInfo->info.type != TSDB_DATA_TYPE_TIMESTAMP) {
76,857!
3855
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3856
    }
3857
    key.groupId = pSrcBlock->info.id.groupId;
76,857✔
3858
    key.ts = *(int64_t*)colDataGetData(pColInfo, rowIndex);
76,857!
3859
  }
3860

3861
  char* buf = NULL;
259,311✔
3862
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
259,311✔
3863
  if (TSDB_CODE_SUCCESS != code) {
259,235!
3864
    return code;
×
3865
  }
3866
  return doSaveTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, &key, pPos, pCtx->pStore);
259,235✔
3867
}
3868

3869
static int32_t doUpdateTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, STuplePos* pPos, SFunctionStateStore* pStore) {
456,216✔
3870
  if (pHandle->pBuf != NULL) {
456,216✔
3871
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
267,346✔
3872
    if (pPage == NULL) {
267,065!
3873
      return terrno;
×
3874
    }
3875
    (void)memcpy(pPage->data + pPos->offset, pBuf, length);
267,065✔
3876
    setBufPageDirty(pPage, true);
267,065✔
3877
    releaseBufPage(pHandle->pBuf, pPage);
266,923✔
3878
  } else {
3879
    int32_t code = pStore->streamStateFuncPut(pHandle->pState, &pPos->streamTupleKey, pBuf, length);
188,870✔
3880
    if (TSDB_CODE_SUCCESS != code) {
188,877!
3881
      return code;
×
3882
    }
3883
  }
3884

3885
  return TSDB_CODE_SUCCESS;
455,463✔
3886
}
3887

3888
int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
456,540✔
3889
  int32_t code = prepareBuf(pCtx);
456,540✔
3890
  if (TSDB_CODE_SUCCESS != code) {
456,444!
3891
    return code;
×
3892
  }
3893

3894
  char* buf = NULL;
456,444✔
3895
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
456,444✔
3896
  if (TSDB_CODE_SUCCESS != code) {
456,250!
3897
    return code;
×
3898
  }
3899
  return doUpdateTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, pPos, pCtx->pStore);
456,250✔
3900
}
3901

3902
static int32_t doLoadTupleData(SSerializeDataHandle* pHandle, const STuplePos* pPos, SFunctionStateStore* pStore, char** value) {
269,386✔
3903
  if (pHandle->pBuf != NULL) {
269,386✔
3904
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
175,259✔
3905
    if (pPage == NULL) {
175,221!
3906
      *value = NULL;
×
3907
      return terrno;
×
3908
    }
3909
    *value = pPage->data + pPos->offset;
175,221✔
3910
    releaseBufPage(pHandle->pBuf, pPage);
175,221✔
3911
    return TSDB_CODE_SUCCESS;
175,191✔
3912
  } else {
3913
    *value = NULL;
94,127✔
3914
    int32_t vLen;
3915
    int32_t code = pStore->streamStateFuncGet(pHandle->pState, &pPos->streamTupleKey, (void **)(value), &vLen);
94,127✔
3916
    if (TSDB_CODE_SUCCESS != code) {
94,131!
3917
      return code;
×
3918
    }
3919
    return TSDB_CODE_SUCCESS;
94,131✔
3920
  }
3921
}
3922

3923
int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value) {
269,384✔
3924
  return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore, value);
269,384✔
3925
}
3926

3927
int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
35,376✔
3928
  int32_t code = TSDB_CODE_SUCCESS;
35,376✔
3929

3930
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
35,376✔
3931
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
35,376✔
3932

3933
  int16_t type = pCtx->pExpr->base.resSchema.type;
35,377✔
3934
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
35,377✔
3935

3936
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
35,377✔
3937
  if (NULL == pCol) {
35,376!
3938
    return TSDB_CODE_OUT_OF_RANGE;
×
3939
  }
3940

3941
  // todo assign the tag value and the corresponding row data
3942
  int32_t currentRow = pBlock->info.rows;
35,376✔
3943
  if (pEntryInfo->numOfRes <= 0) {
35,376✔
3944
    colDataSetNULL(pCol, currentRow);
563!
3945
    code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, currentRow);
563✔
3946
    return code;
563✔
3947
  }
3948
  for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) {
189,887✔
3949
    STopBotResItem* pItem = &pRes->pItems[i];
155,081✔
3950
    code = colDataSetVal(pCol, currentRow, (const char*)&pItem->v.i, false);
155,081✔
3951
    if (TSDB_CODE_SUCCESS != code) {
155,077!
3952
      return code;
×
3953
    }
3954
#ifdef BUF_PAGE_DEBUG
3955
    qDebug("page_finalize i:%d,item:%p,pageId:%d, offset:%d\n", i, pItem, pItem->tuplePos.pageId,
3956
           pItem->tuplePos.offset);
3957
#endif
3958
    code = setSelectivityValue(pCtx, pBlock, &pRes->pItems[i].tuplePos, currentRow);
155,077✔
3959
    if (TSDB_CODE_SUCCESS != code) {
155,074!
3960
      return code;
×
3961
    }
3962
    currentRow += 1;
155,074✔
3963
  }
3964

3965
  return code;
34,806✔
3966
}
3967

3968
int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery) {
×
3969
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
3970
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
×
3971
  STopBotResItem*      pItems = pRes->pItems;
×
3972
  int32_t              code = TSDB_CODE_SUCCESS;
×
3973

3974
  // not full yet
3975
  if (pEntryInfo->numOfRes < pRes->maxSize) {
×
3976
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
×
3977
    pItem->v = pSourceItem->v;
×
3978
    pItem->uid = pSourceItem->uid;
×
3979
    pItem->tuplePos.pageId = -1;
×
3980
    replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos);
×
3981
    pEntryInfo->numOfRes++;
×
3982
   code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
×
3983
                        topBotResComparFn, !isTopQuery);
×
3984
   if (TSDB_CODE_SUCCESS != code) {
×
3985
     return code;
×
3986
   }
3987
  } else {  // replace the minimum value in the result
3988
    if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && pSourceItem->v.i > pItems[0].v.i) ||
×
3989
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && pSourceItem->v.u > pItems[0].v.u) ||
×
3990
                        (TSDB_DATA_TYPE_FLOAT == type && pSourceItem->v.f > pItems[0].v.f) ||
×
3991
                        (TSDB_DATA_TYPE_DOUBLE == type && pSourceItem->v.d > pItems[0].v.d))) ||
×
3992
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && pSourceItem->v.i < pItems[0].v.i) ||
×
3993
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && pSourceItem->v.u < pItems[0].v.u) ||
×
3994
                         (TSDB_DATA_TYPE_FLOAT == type && pSourceItem->v.f < pItems[0].v.f) ||
×
3995
                         (TSDB_DATA_TYPE_DOUBLE == type && pSourceItem->v.d < pItems[0].v.d)))) {
×
3996
      // replace the old data and the coresponding tuple data
3997
      STopBotResItem* pItem = &pItems[0];
×
3998
      pItem->v = pSourceItem->v;
×
3999
      pItem->uid = pSourceItem->uid;
×
4000

4001
      // save the data of this tuple by over writing the old data
4002
      replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos);
×
4003
      code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type,
×
4004
                            topBotResComparFn, NULL, !isTopQuery);
×
4005
      if (TSDB_CODE_SUCCESS != code) {
×
4006
        return code;
×
4007
      }
4008
    }
4009
  }
4010
  return code;
×
4011
}
4012

4013
int32_t topCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4014
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4015
  STopBotRes*          pSBuf = getTopBotOutputInfo(pSourceCtx);
×
4016
  int16_t              type = pSBuf->type;
×
4017
  int32_t              code = TSDB_CODE_SUCCESS;
×
4018
  for (int32_t i = 0; i < pSResInfo->numOfRes; i++) {
×
4019
    code = addResult(pDestCtx, pSBuf->pItems + i, type, true);
×
4020
    if (TSDB_CODE_SUCCESS != code) {
×
4021
      return code;
×
4022
    }
4023
  }
4024
  return TSDB_CODE_SUCCESS;
×
4025
}
4026

4027
int32_t bottomCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4028
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4029
  STopBotRes*          pSBuf = getTopBotOutputInfo(pSourceCtx);
×
4030
  int16_t              type = pSBuf->type;
×
4031
  int32_t              code = TSDB_CODE_SUCCESS;
×
4032
  for (int32_t i = 0; i < pSResInfo->numOfRes; i++) {
×
4033
    code = addResult(pDestCtx, pSBuf->pItems + i, type, false);
×
4034
    if (TSDB_CODE_SUCCESS != code) {
×
4035
      return code;
×
4036
    }
4037
  }
4038
  return TSDB_CODE_SUCCESS;
×
4039
}
4040

4041
int32_t getSpreadInfoSize() { return (int32_t)sizeof(SSpreadInfo); }
19,059✔
4042

4043
bool getSpreadFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
34,534✔
4044
  pEnv->calcMemSize = sizeof(SSpreadInfo);
34,534✔
4045
  return true;
34,534✔
4046
}
4047

4048
int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
157,315✔
4049
  if (pResultInfo->initialized) {
157,315!
4050
    return TSDB_CODE_SUCCESS;
×
4051
  }
4052
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
157,315!
4053
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4054
  }
4055

4056
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
157,353✔
4057
  SET_DOUBLE_VAL(&pInfo->min, DBL_MAX);
157,353✔
4058
  SET_DOUBLE_VAL(&pInfo->max, -DBL_MAX);
157,353✔
4059
  pInfo->hasResult = false;
157,353✔
4060
  return TSDB_CODE_SUCCESS;
157,353✔
4061
}
4062

4063
int32_t spreadFunction(SqlFunctionCtx* pCtx) {
171,155✔
4064
  int32_t numOfElems = 0;
171,155✔
4065

4066
  // Only the pre-computing information loaded and actual data does not loaded
4067
  SInputColumnInfoData* pInput = &pCtx->input;
171,155✔
4068
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
171,155✔
4069
  int32_t               type = pInput->pData[0]->info.type;
171,155✔
4070

4071
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
171,155✔
4072

4073
  if (pInput->colDataSMAIsSet) {
171,155!
4074
    numOfElems = pInput->numOfRows - pAgg->numOfNull;
×
4075
    if (numOfElems == 0) {
×
4076
      goto _spread_over;
×
4077
    }
4078
    double tmin = 0.0, tmax = 0.0;
×
4079
    if (IS_SIGNED_NUMERIC_TYPE(type) || IS_TIMESTAMP_TYPE(type)) {
×
4080
      tmin = (double)GET_INT64_VAL(&pAgg->min);
×
4081
      tmax = (double)GET_INT64_VAL(&pAgg->max);
×
4082
    } else if (IS_FLOAT_TYPE(type)) {
×
4083
      tmin = GET_DOUBLE_VAL(&pAgg->min);
×
4084
      tmax = GET_DOUBLE_VAL(&pAgg->max);
×
4085
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
4086
      tmin = (double)GET_UINT64_VAL(&pAgg->min);
×
4087
      tmax = (double)GET_UINT64_VAL(&pAgg->max);
×
4088
    }
4089

4090
    if (GET_DOUBLE_VAL(&pInfo->min) > tmin) {
×
4091
      SET_DOUBLE_VAL(&pInfo->min, tmin);
×
4092
    }
4093

4094
    if (GET_DOUBLE_VAL(&pInfo->max) < tmax) {
×
4095
      SET_DOUBLE_VAL(&pInfo->max, tmax);
×
4096
    }
4097

4098
  } else {  // computing based on the true data block
4099
    SColumnInfoData* pCol = pInput->pData[0];
171,155✔
4100

4101
    int32_t start = pInput->startRowIndex;
171,155✔
4102
    // check the valid data one by one
4103
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
5,129,595✔
4104
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
4,958,440✔
4105
        continue;
1,695,961✔
4106
      }
4107

4108
      char* data = colDataGetData(pCol, i);
3,262,479!
4109

4110
      double v = 0;
3,262,479✔
4111
      GET_TYPED_DATA(v, double, type, data);
3,262,479!
4112
      if (v < GET_DOUBLE_VAL(&pInfo->min)) {
3,262,479✔
4113
        SET_DOUBLE_VAL(&pInfo->min, v);
151,231✔
4114
      }
4115

4116
      if (v > GET_DOUBLE_VAL(&pInfo->max)) {
3,262,479✔
4117
        SET_DOUBLE_VAL(&pInfo->max, v);
562,976✔
4118
      }
4119

4120
      numOfElems += 1;
3,262,479✔
4121
    }
4122
  }
4123

4124
_spread_over:
171,155✔
4125
  // data in the check operation are all null, not output
4126
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
171,155✔
4127
  if (numOfElems > 0) {
171,155✔
4128
    pInfo->hasResult = true;
146,486✔
4129
  }
4130

4131
  return TSDB_CODE_SUCCESS;
171,155✔
4132
}
4133

4134
static void spreadTransferInfo(SSpreadInfo* pInput, SSpreadInfo* pOutput) {
17,573✔
4135
  pOutput->hasResult = pInput->hasResult;
17,573✔
4136
  if (pInput->max > pOutput->max) {
17,573✔
4137
    pOutput->max = pInput->max;
13,130✔
4138
  }
4139

4140
  if (pInput->min < pOutput->min) {
17,573✔
4141
    pOutput->min = pInput->min;
13,128✔
4142
  }
4143
}
17,573✔
4144

4145
int32_t spreadFunctionMerge(SqlFunctionCtx* pCtx) {
13,583✔
4146
  SInputColumnInfoData* pInput = &pCtx->input;
13,583✔
4147
  SColumnInfoData*      pCol = pInput->pData[0];
13,583✔
4148

4149
  if (IS_NULL_TYPE(pCol->info.type)) {
13,583!
4150
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4151
    return TSDB_CODE_SUCCESS;
×
4152
  }
4153

4154
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
13,583!
4155
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4156
  }
4157

4158
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
13,583✔
4159

4160
  int32_t start = pInput->startRowIndex;
13,583✔
4161
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
31,279✔
4162
    if(colDataIsNull_s(pCol, i)) continue;
35,392!
4163
    char*        data = colDataGetData(pCol, i);
17,696!
4164
    SSpreadInfo* pInputInfo = (SSpreadInfo*)varDataVal(data);
17,696✔
4165
    if (pInputInfo->hasResult) {
17,696✔
4166
      spreadTransferInfo(pInputInfo, pInfo);
17,572✔
4167
    }
4168
  }
4169

4170
  if (pInfo->hasResult) {
13,583✔
4171
    GET_RES_INFO(pCtx)->numOfRes = 1;
13,519✔
4172
  }
4173

4174
  return TSDB_CODE_SUCCESS;
13,583✔
4175
}
4176

4177
int32_t spreadFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
134,076✔
4178
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
134,076✔
4179
  if (pInfo->hasResult == true) {
134,076✔
4180
    SET_DOUBLE_VAL(&pInfo->result, pInfo->max - pInfo->min);
109,698✔
4181
  } else {
4182
    GET_RES_INFO(pCtx)->isNullRes = 1;
24,378✔
4183
  }
4184
  return functionFinalize(pCtx, pBlock);
134,076✔
4185
}
4186

4187
int32_t spreadPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
17,761✔
4188
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
17,761✔
4189
  SSpreadInfo*         pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
17,761✔
4190
  int32_t              resultBytes = getSpreadInfoSize();
17,761✔
4191
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
17,761✔
4192

4193
  if (NULL == res) {
17,760!
4194
    return terrno;
×
4195
  }
4196
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
17,760✔
4197
  varDataSetLen(res, resultBytes);
17,760✔
4198

4199
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
17,760✔
4200
  int32_t          code = TSDB_CODE_SUCCESS;
17,760✔
4201
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
17,760✔
4202
  if (NULL == pCol) {
17,761!
4203
    code = terrno;
×
4204
    goto _exit;
×
4205
  }
4206

4207
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
17,761✔
4208
  if (TSDB_CODE_SUCCESS != code) {
17,760!
4209
    goto _exit;
×
4210
  }
4211

4212
_exit:
17,760✔
4213
  taosMemoryFree(res);
17,760✔
4214
  return code;
17,760✔
4215
}
4216

4217
int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
1✔
4218
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
1✔
4219
  SSpreadInfo*         pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
1✔
4220

4221
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
1✔
4222
  SSpreadInfo*         pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
1✔
4223
  spreadTransferInfo(pSBuf, pDBuf);
1✔
4224
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
1✔
4225
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
1✔
4226
  return TSDB_CODE_SUCCESS;
1✔
4227
}
4228

4229
int32_t getElapsedInfoSize() { return (int32_t)sizeof(SElapsedInfo); }
×
4230

4231
bool getElapsedFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
19,467✔
4232
  pEnv->calcMemSize = sizeof(SElapsedInfo);
19,467✔
4233
  return true;
19,467✔
4234
}
4235

4236
int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
37,727✔
4237
  if (pResultInfo->initialized) {
37,727!
4238
    return TSDB_CODE_SUCCESS;
×
4239
  }
4240
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
37,727!
4241
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4242
  }
4243

4244
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
37,744✔
4245
  pInfo->result = 0;
37,744✔
4246
  pInfo->min = TSKEY_MAX;
37,744✔
4247
  pInfo->max = 0;
37,744✔
4248

4249
  if (pCtx->numOfParams > 1) {
37,744✔
4250
    pInfo->timeUnit = pCtx->param[1].param.i;
21,344✔
4251
  } else {
4252
    pInfo->timeUnit = 1;
16,400✔
4253
  }
4254

4255
  return TSDB_CODE_SUCCESS;
37,744✔
4256
}
4257

4258
int32_t elapsedFunction(SqlFunctionCtx* pCtx) {
37,764✔
4259
  int32_t numOfElems = 0;
37,764✔
4260

4261
  // Only the pre-computing information loaded and actual data does not loaded
4262
  SInputColumnInfoData* pInput = &pCtx->input;
37,764✔
4263
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
37,764✔
4264

4265
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
37,764✔
4266

4267
  numOfElems = pInput->numOfRows;  // since this is the primary timestamp, no need to exclude NULL values
37,764✔
4268
  if (numOfElems == 0) {
37,764✔
4269
    // for stream
4270
    if (pCtx->end.key != INT64_MIN) {
52!
4271
      pInfo->max = pCtx->end.key + 1;
52✔
4272
    }
4273
    goto _elapsed_over;
52✔
4274
  }
4275

4276
  if (pInput->colDataSMAIsSet) {
37,712!
4277
    if (pInfo->min == TSKEY_MAX) {
×
4278
      pInfo->min = GET_INT64_VAL(&pAgg->min);
×
4279
      pInfo->max = GET_INT64_VAL(&pAgg->max);
×
4280
    } else {
4281
      if (pCtx->order == TSDB_ORDER_ASC) {
×
4282
        pInfo->max = GET_INT64_VAL(&pAgg->max);
×
4283
      } else {
4284
        pInfo->min = GET_INT64_VAL(&pAgg->min);
×
4285
      }
4286
    }
4287
  } else {  // computing based on the true data block
4288
    if (0 == pInput->numOfRows) {
37,712!
4289
      if (pCtx->order == TSDB_ORDER_DESC) {
×
4290
        if (pCtx->end.key != INT64_MIN) {
×
4291
          pInfo->min = pCtx->end.key;
×
4292
        }
4293
      } else {
4294
        if (pCtx->end.key != INT64_MIN) {
×
4295
          pInfo->max = pCtx->end.key + 1;
×
4296
        }
4297
      }
4298
      goto _elapsed_over;
×
4299
    }
4300

4301
    SColumnInfoData* pCol = pInput->pData[0];
37,712✔
4302

4303
    int32_t start = pInput->startRowIndex;
37,712✔
4304
    TSKEY*  ptsList = (int64_t*)colDataGetData(pCol, 0);
37,712!
4305
    if (pCtx->order == TSDB_ORDER_DESC) {
37,712✔
4306
      if (pCtx->start.key == INT64_MIN) {
316!
4307
        pInfo->max = (pInfo->max < ptsList[start]) ? ptsList[start] : pInfo->max;
316✔
4308
      } else {
4309
        pInfo->max = pCtx->start.key + 1;
×
4310
      }
4311

4312
      if (pCtx->end.key == INT64_MIN) {
316!
4313
        pInfo->min =
316✔
4314
            (pInfo->min > ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->min;
316✔
4315
      } else {
4316
        pInfo->min = pCtx->end.key;
×
4317
      }
4318
    } else {
4319
      if (pCtx->start.key == INT64_MIN) {
37,396✔
4320
        pInfo->min = (pInfo->min > ptsList[start]) ? ptsList[start] : pInfo->min;
35,499✔
4321
      } else {
4322
        pInfo->min = pCtx->start.key;
1,897✔
4323
      }
4324

4325
      if (pCtx->end.key == INT64_MIN) {
37,396✔
4326
        pInfo->max =
35,184✔
4327
            (pInfo->max < ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->max;
35,184✔
4328
      } else {
4329
        pInfo->max = pCtx->end.key + 1;
2,212✔
4330
      }
4331
    }
4332
  }
4333

4334
_elapsed_over:
37,764✔
4335
  // data in the check operation are all null, not output
4336
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
37,764✔
4337

4338
  return TSDB_CODE_SUCCESS;
37,764✔
4339
}
4340

4341
static void elapsedTransferInfo(SElapsedInfo* pInput, SElapsedInfo* pOutput) {
×
4342
  pOutput->timeUnit = pInput->timeUnit;
×
4343
  if (pOutput->min > pInput->min) {
×
4344
    pOutput->min = pInput->min;
×
4345
  }
4346

4347
  if (pOutput->max < pInput->max) {
×
4348
    pOutput->max = pInput->max;
×
4349
  }
4350
}
×
4351

4352
int32_t elapsedFunctionMerge(SqlFunctionCtx* pCtx) {
×
4353
  SInputColumnInfoData* pInput = &pCtx->input;
×
4354
  SColumnInfoData*      pCol = pInput->pData[0];
×
4355
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
4356
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4357
  }
4358

4359
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4360

4361
  int32_t start = pInput->startRowIndex;
×
4362

4363
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
4364
    char*         data = colDataGetData(pCol, i);
×
4365
    SElapsedInfo* pInputInfo = (SElapsedInfo*)varDataVal(data);
×
4366
    elapsedTransferInfo(pInputInfo, pInfo);
×
4367
  }
4368

4369
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
4370
  return TSDB_CODE_SUCCESS;
×
4371
}
4372

4373
int32_t elapsedFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
37,790✔
4374
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
37,790✔
4375
  double        result = (double)pInfo->max - (double)pInfo->min;
37,790✔
4376
  result = (result >= 0) ? result : -result;
37,790✔
4377
  pInfo->result = result / pInfo->timeUnit;
37,790✔
4378
  return functionFinalize(pCtx, pBlock);
37,790✔
4379
}
4380

4381
int32_t elapsedPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
4382
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
4383
  SElapsedInfo*        pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4384
  int32_t              resultBytes = getElapsedInfoSize();
×
4385
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4386

4387
  if (NULL == res) {
×
4388
    return terrno;
×
4389
  }
4390
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
4391
  varDataSetLen(res, resultBytes);
×
4392

4393
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
4394
  int32_t          code = TSDB_CODE_SUCCESS;
×
4395
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
4396
  if (NULL == pCol) {
×
4397
    code = terrno;
×
4398
    goto _exit;
×
4399
  }
4400

4401
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
4402
  if (TSDB_CODE_SUCCESS != code) {
×
4403
    goto _exit;
×
4404
  }
4405
_exit:
×
4406
  taosMemoryFree(res);
×
4407
  return code;
×
4408
}
4409

4410
int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4411
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4412
  SElapsedInfo*        pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4413

4414
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4415
  SElapsedInfo*        pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4416

4417
  elapsedTransferInfo(pSBuf, pDBuf);
×
4418
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4419
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4420
  return TSDB_CODE_SUCCESS;
×
4421
}
4422

4423
int32_t getHistogramInfoSize() {
9,980✔
4424
  return (int32_t)sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
9,980✔
4425
}
4426

4427
bool getHistogramFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
13,223✔
4428
  pEnv->calcMemSize = sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
13,223✔
4429
  return true;
13,223✔
4430
}
4431

4432
static int8_t getHistogramBinType(char* binTypeStr) {
9,073✔
4433
  int8_t binType;
4434
  if (strcasecmp(binTypeStr, "user_input") == 0) {
9,073✔
4435
    binType = USER_INPUT_BIN;
2,788✔
4436
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
6,285✔
4437
    binType = LINEAR_BIN;
3,279✔
4438
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
3,006!
4439
    binType = LOG_BIN;
3,006✔
4440
  } else {
UNCOV
4441
    binType = UNKNOWN_BIN;
×
4442
  }
4443

4444
  return binType;
9,073✔
4445
}
4446

4447
static int32_t getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t binType, bool normalized) {
9,073✔
4448
  cJSON*  binDesc = cJSON_Parse(binDescStr);
9,073✔
4449
  int32_t numOfBins;
4450
  double* intervals;
4451
  if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
9,073✔
4452
    int32_t numOfParams = cJSON_GetArraySize(binDesc);
6,284✔
4453
    int32_t startIndex;
4454
    if (numOfParams != 4) {
6,284!
4455
      cJSON_Delete(binDesc);
×
4456
      return TSDB_CODE_FAILED;
×
4457
    }
4458

4459
    cJSON* start = cJSON_GetObjectItem(binDesc, "start");
6,284✔
4460
    cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
6,285✔
4461
    cJSON* width = cJSON_GetObjectItem(binDesc, "width");
6,285✔
4462
    cJSON* count = cJSON_GetObjectItem(binDesc, "count");
6,285✔
4463
    cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
6,284✔
4464

4465
    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
6,285!
4466
      cJSON_Delete(binDesc);
×
4467
      return TSDB_CODE_FAILED;
×
4468
    }
4469

4470
    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
6,285!
4471
      cJSON_Delete(binDesc);
1✔
4472
      return TSDB_CODE_FAILED;
×
4473
    }
4474

4475
    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
6,284!
4476
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
6,284!
4477
      cJSON_Delete(binDesc);
×
4478
      return TSDB_CODE_FAILED;
×
4479
    }
4480

4481
    int32_t counter = (int32_t)count->valueint;
6,284✔
4482
    if (infinity->valueint == false) {
6,284✔
4483
      startIndex = 0;
4,709✔
4484
      numOfBins = counter + 1;
4,709✔
4485
    } else {
4486
      startIndex = 1;
1,575✔
4487
      numOfBins = counter + 3;
1,575✔
4488
    }
4489

4490
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
6,284✔
4491
    if (NULL == intervals) {
6,285!
4492
      cJSON_Delete(binDesc);
×
4493
      qError("histogram function out of memory");
×
4494
      return terrno;
×
4495
    }
4496
    if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) {
6,285!
4497
      // linear bin process
4498
      if (width->valuedouble == 0) {
3,278!
4499
        taosMemoryFree(intervals);
×
4500
        cJSON_Delete(binDesc);
×
4501
        return TSDB_CODE_FAILED;
×
4502
      }
4503
      for (int i = 0; i < counter + 1; ++i) {
31,704✔
4504
        intervals[startIndex] = start->valuedouble + i * width->valuedouble;
28,426✔
4505
        if (isinf(intervals[startIndex])) {
28,426!
4506
          taosMemoryFree(intervals);
×
4507
          cJSON_Delete(binDesc);
×
4508
          return TSDB_CODE_FAILED;
×
4509
        }
4510
        startIndex++;
28,426✔
4511
      }
4512
    } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
3,006!
4513
      // log bin process
4514
      if (start->valuedouble == 0) {
3,006!
4515
        taosMemoryFree(intervals);
×
4516
        cJSON_Delete(binDesc);
×
4517
        return TSDB_CODE_FAILED;
×
4518
      }
4519
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
3,006!
4520
        taosMemoryFree(intervals);
×
4521
        cJSON_Delete(binDesc);
×
4522
        return TSDB_CODE_FAILED;
×
4523
      }
4524
      for (int i = 0; i < counter + 1; ++i) {
19,422✔
4525
        intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
16,416✔
4526
        if (isinf(intervals[startIndex])) {
16,416!
4527
          taosMemoryFree(intervals);
×
4528
          cJSON_Delete(binDesc);
×
4529
          return TSDB_CODE_FAILED;
×
4530
        }
4531
        startIndex++;
16,416✔
4532
      }
4533
    } else {
4534
      taosMemoryFree(intervals);
×
4535
      cJSON_Delete(binDesc);
×
4536
      return TSDB_CODE_FAILED;
×
4537
    }
4538

4539
    if (infinity->valueint == true) {
6,284✔
4540
      intervals[0] = -INFINITY;
1,575✔
4541
      intervals[numOfBins - 1] = INFINITY;
1,575✔
4542
      // in case of desc bin orders, -inf/inf should be swapped
4543
      if (numOfBins < 4) {
1,575!
4544
        return TSDB_CODE_FAILED;
×
4545
      }
4546
      if (intervals[1] > intervals[numOfBins - 2]) {
1,575!
UNCOV
4547
        TSWAP(intervals[0], intervals[numOfBins - 1]);
×
4548
      }
4549
    }
4550
  } else if (cJSON_IsArray(binDesc)) { /* user input bins */
2,788!
4551
    if (binType != USER_INPUT_BIN) {
2,789!
4552
      cJSON_Delete(binDesc);
×
4553
      return TSDB_CODE_FAILED;
×
4554
    }
4555
    numOfBins = cJSON_GetArraySize(binDesc);
2,789✔
4556
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
2,789✔
4557
    if (NULL == intervals) {
2,788!
4558
      cJSON_Delete(binDesc);
×
4559
      qError("histogram function out of memory");
×
4560
      return terrno;
×
4561
    }
4562
    cJSON* bin = binDesc->child;
2,788✔
4563
    if (bin == NULL) {
2,788!
4564
      taosMemoryFree(intervals);
×
4565
      cJSON_Delete(binDesc);
×
4566
      return TSDB_CODE_FAILED;
×
4567
    }
4568
    int i = 0;
2,788✔
4569
    while (bin) {
11,984✔
4570
      intervals[i] = bin->valuedouble;
9,197✔
4571
      if (!cJSON_IsNumber(bin)) {
9,197!
UNCOV
4572
        taosMemoryFree(intervals);
×
4573
        cJSON_Delete(binDesc);
×
4574
        return TSDB_CODE_FAILED;
×
4575
      }
4576
      if (i != 0 && intervals[i] <= intervals[i - 1]) {
9,196!
4577
        taosMemoryFree(intervals);
×
4578
        cJSON_Delete(binDesc);
×
4579
        return TSDB_CODE_FAILED;
×
4580
      }
4581
      bin = bin->next;
9,196✔
4582
      i++;
9,196✔
4583
    }
4584
  } else {
4585
    cJSON_Delete(binDesc);
×
4586
    return TSDB_CODE_FAILED;
×
4587
  }
4588

4589
  pInfo->numOfBins = numOfBins - 1;
9,071✔
4590
  pInfo->normalized = normalized;
9,071✔
4591
  for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
57,176✔
4592
    pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
48,105✔
4593
    pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
48,105✔
4594
    pInfo->bins[i].count = 0;
48,105✔
4595
  }
4596

4597
  taosMemoryFree(intervals);
9,071✔
4598
  cJSON_Delete(binDesc);
9,072✔
4599

4600
  return TSDB_CODE_SUCCESS;
9,073✔
4601
}
4602

4603
int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
9,073✔
4604
  if (pResultInfo->initialized) {
9,073!
4605
    return TSDB_CODE_SUCCESS;
×
4606
  }
4607
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
9,073!
4608
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4609
  }
4610

4611
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
9,073✔
4612
  pInfo->numOfBins = 0;
9,073✔
4613
  pInfo->totalCount = 0;
9,073✔
4614
  pInfo->normalized = 0;
9,073✔
4615

4616
  char*  binTypeStr = taosStrndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz));
9,073✔
4617
  if (binTypeStr == NULL) {
9,073!
4618
    return terrno;
×
4619
  }
4620
  int8_t binType = getHistogramBinType(binTypeStr);
9,073✔
4621
  taosMemoryFree(binTypeStr);
9,073✔
4622

4623
  if (binType == UNKNOWN_BIN) {
9,073!
4624
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
4625
  }
4626
  char*   binDesc = taosStrndup(varDataVal(pCtx->param[2].param.pz), varDataLen(pCtx->param[2].param.pz));
9,073✔
4627
  if (binDesc == NULL) {
9,073!
4628
    return terrno;
×
4629
  }
4630
  int64_t normalized = pCtx->param[3].param.i;
9,073✔
4631
  if (normalized != 0 && normalized != 1) {
9,073!
4632
    taosMemoryFree(binDesc);
×
4633
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
4634
  }
4635
  int32_t code = getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized);
9,073✔
4636
  if (TSDB_CODE_SUCCESS != code) {
9,072!
4637
    taosMemoryFree(binDesc);
×
4638
    return code;
×
4639
  }
4640
  taosMemoryFree(binDesc);
9,072✔
4641

4642
  return TSDB_CODE_SUCCESS;
9,074✔
4643
}
4644

4645
static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) {
27,121✔
4646
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
27,121✔
4647

4648
  SInputColumnInfoData* pInput = &pCtx->input;
27,121✔
4649
  SColumnInfoData*      pCol = pInput->pData[0];
27,121✔
4650

4651
  int32_t type = pInput->pData[0]->info.type;
27,121✔
4652

4653
  int32_t start = pInput->startRowIndex;
27,121✔
4654
  int32_t numOfRows = pInput->numOfRows;
27,121✔
4655

4656
  int32_t numOfElems = 0;
27,121✔
4657
  for (int32_t i = start; i < numOfRows + start; ++i) {
1,425,999✔
4658
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,398,878✔
4659
      continue;
475,494✔
4660
    }
4661

4662
    numOfElems++;
923,384✔
4663

4664
    char*  data = colDataGetData(pCol, i);
923,384!
4665
    double v;
4666
    GET_TYPED_DATA(v, double, type, data);
923,384!
4667

4668
    for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
3,620,973✔
4669
      if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
3,171,412✔
4670
        pInfo->bins[k].count++;
473,823✔
4671
        pInfo->totalCount++;
473,823✔
4672
        break;
473,823✔
4673
      }
4674
    }
4675
  }
4676

4677
  if (!isPartial) {
27,121✔
4678
    GET_RES_INFO(pCtx)->numOfRes = pInfo->numOfBins;
5,521✔
4679
  } else {
4680
    GET_RES_INFO(pCtx)->numOfRes = 1;
21,600✔
4681
  }
4682
  return TSDB_CODE_SUCCESS;
27,121✔
4683
}
4684

4685
int32_t histogramFunction(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, false); }
5,519✔
4686

4687
int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, true); }
21,600✔
4688

4689
static void histogramTransferInfo(SHistoFuncInfo* pInput, SHistoFuncInfo* pOutput) {
3,561✔
4690
  pOutput->normalized = pInput->normalized;
3,561✔
4691
  pOutput->numOfBins = pInput->numOfBins;
3,561✔
4692
  pOutput->totalCount += pInput->totalCount;
3,561✔
4693
  for (int32_t k = 0; k < pOutput->numOfBins; ++k) {
33,687✔
4694
    pOutput->bins[k].lower = pInput->bins[k].lower;
30,126✔
4695
    pOutput->bins[k].upper = pInput->bins[k].upper;
30,126✔
4696
    pOutput->bins[k].count += pInput->bins[k].count;
30,126✔
4697
  }
4698
}
3,561✔
4699

4700
int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx) {
3,561✔
4701
  SInputColumnInfoData* pInput = &pCtx->input;
3,561✔
4702
  SColumnInfoData*      pCol = pInput->pData[0];
3,561✔
4703
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
3,561!
4704
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4705
  }
4706

4707
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
3,561✔
4708

4709
  int32_t start = pInput->startRowIndex;
3,561✔
4710

4711
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
7,122✔
4712
    char*           data = colDataGetData(pCol, i);
3,561!
4713
    SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data);
3,561✔
4714
    histogramTransferInfo(pInputInfo, pInfo);
3,561✔
4715
  }
4716

4717
  SET_VAL(GET_RES_INFO(pCtx), pInfo->numOfBins, pInfo->numOfBins);
3,561!
4718
  return TSDB_CODE_SUCCESS;
3,561✔
4719
}
4720

4721
int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
7,991✔
4722
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
7,991✔
4723
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,991✔
4724
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
7,991✔
4725
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
7,991✔
4726
  int32_t              code = TSDB_CODE_SUCCESS;
7,991✔
4727

4728
  int32_t currentRow = pBlock->info.rows;
7,991✔
4729
  if (NULL == pCol) {
7,991!
4730
    return TSDB_CODE_OUT_OF_RANGE;
×
4731
  }
4732

4733
  if (pInfo->normalized) {
7,991✔
4734
    for (int32_t k = 0; k < pResInfo->numOfRes; ++k) {
18,453✔
4735
      if (pInfo->totalCount != 0) {
14,468✔
4736
        pInfo->bins[k].percentage = pInfo->bins[k].count / (double)pInfo->totalCount;
6,891✔
4737
      } else {
4738
        pInfo->bins[k].percentage = 0;
7,577✔
4739
      }
4740
    }
4741
  }
4742

4743
  for (int32_t i = 0; i < pResInfo->numOfRes; ++i) {
44,518✔
4744
    int32_t len;
4745
    char    buf[512] = {0};
36,532✔
4746
    if (!pInfo->normalized) {
36,532✔
4747
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%" PRId64 "}",
22,060✔
4748
                    pInfo->bins[i].lower, pInfo->bins[i].upper, pInfo->bins[i].count);
4749
    } else {
4750
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%lf}", pInfo->bins[i].lower,
14,472✔
4751
                    pInfo->bins[i].upper, pInfo->bins[i].percentage);
4752
    }
4753
    varDataSetLen(buf, len);
36,531✔
4754
    code = colDataSetVal(pCol, currentRow, buf, false);
36,531✔
4755
    if (TSDB_CODE_SUCCESS != code) {
36,527!
4756
      return code;
×
4757
    }
4758
    currentRow++;
36,527✔
4759
  }
4760

4761
  return code;
7,986✔
4762
}
4763

4764
int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
3,561✔
4765
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
3,561✔
4766
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
3,561✔
4767
  int32_t              resultBytes = getHistogramInfoSize();
3,561✔
4768
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
3,561✔
4769

4770
  if (NULL == res) {
3,561!
4771
    return terrno;
×
4772
  }
4773
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
3,561✔
4774
  varDataSetLen(res, resultBytes);
3,561✔
4775

4776
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
3,561✔
4777
  int32_t          code = TSDB_CODE_SUCCESS;
3,561✔
4778
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
3,561✔
4779
  if (NULL == pCol) {
3,561!
4780
    code = terrno;
×
4781
    goto _exit;
×
4782
  }
4783
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
3,561✔
4784

4785
_exit:
3,559✔
4786
  taosMemoryFree(res);
3,559✔
4787
  return code;
3,561✔
4788
}
4789

4790
int32_t histogramCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4791
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4792
  SHistoFuncInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4793

4794
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4795
  SHistoFuncInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4796

4797
  histogramTransferInfo(pSBuf, pDBuf);
×
4798
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4799
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4800
  return TSDB_CODE_SUCCESS;
×
4801
}
4802

4803
int32_t getHLLInfoSize() { return (int32_t)sizeof(SHLLInfo); }
7,541✔
4804

4805
bool getHLLFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
39,824✔
4806
  pEnv->calcMemSize = sizeof(SHLLInfo);
39,824✔
4807
  return true;
39,824✔
4808
}
4809

4810
static uint8_t hllCountNum(void* data, int32_t bytes, int32_t* buk) {
3,213,845✔
4811
  uint64_t hash = MurmurHash3_64(data, bytes);
3,213,845✔
4812
  int32_t  index = hash & HLL_BUCKET_MASK;
3,213,250✔
4813
  hash >>= HLL_BUCKET_BITS;
3,213,250✔
4814
  hash |= ((uint64_t)1 << HLL_DATA_BITS);
3,213,250✔
4815
  uint64_t bit = 1;
3,213,250✔
4816
  uint8_t  count = 1;
3,213,250✔
4817
  while ((hash & bit) == 0) {
6,161,036✔
4818
    count++;
2,947,786✔
4819
    bit <<= 1;
2,947,786✔
4820
  }
4821
  *buk = index;
3,213,250✔
4822
  return count;
3,213,250✔
4823
}
4824

4825
static void hllBucketHisto(uint8_t* buckets, int32_t* bucketHisto) {
101,643✔
4826
  uint64_t* word = (uint64_t*)buckets;
101,643✔
4827
  uint8_t*  bytes;
4828

4829
  for (int32_t j = 0; j < HLL_BUCKETS >> 3; j++) {
201,280,286✔
4830
    if (*word == 0) {
201,178,643✔
4831
      bucketHisto[0] += 8;
200,431,513✔
4832
    } else {
4833
      bytes = (uint8_t*)word;
747,130✔
4834
      bucketHisto[bytes[0]]++;
747,130✔
4835
      bucketHisto[bytes[1]]++;
747,130✔
4836
      bucketHisto[bytes[2]]++;
747,130✔
4837
      bucketHisto[bytes[3]]++;
747,130✔
4838
      bucketHisto[bytes[4]]++;
747,130✔
4839
      bucketHisto[bytes[5]]++;
747,130✔
4840
      bucketHisto[bytes[6]]++;
747,130✔
4841
      bucketHisto[bytes[7]]++;
747,130✔
4842
    }
4843
    word++;
201,178,643✔
4844
  }
4845
}
101,643✔
4846
static double hllTau(double x) {
101,643✔
4847
  if (x == 0. || x == 1.) return 0.;
101,643!
4848
  double zPrime;
4849
  double y = 1.0;
×
4850
  double z = 1 - x;
×
4851
  do {
4852
    x = sqrt(x);
×
4853
    zPrime = z;
×
4854
    y *= 0.5;
×
4855
    z -= pow(1 - x, 2) * y;
×
4856
  } while (zPrime != z);
×
4857
  return z / 3;
×
4858
}
4859

4860
static double hllSigma(double x) {
101,648✔
4861
  if (x == 1.0) return INFINITY;
101,648✔
4862
  double zPrime;
4863
  double y = 1;
79,707✔
4864
  double z = x;
79,707✔
4865
  do {
4866
    x *= x;
1,548,983✔
4867
    zPrime = z;
1,548,983✔
4868
    z += x * y;
1,548,983✔
4869
    y += y;
1,548,983✔
4870
  } while (zPrime != z);
1,548,983✔
4871
  return z;
79,707✔
4872
}
4873

4874
// estimate the cardinality, the algorithm refer this paper: "New cardinality estimation algorithms for HyperLogLog
4875
// sketches"
4876
static uint64_t hllCountCnt(uint8_t* buckets) {
101,617✔
4877
  double  m = HLL_BUCKETS;
101,617✔
4878
  int32_t buckethisto[64] = {0};
101,617✔
4879
  hllBucketHisto(buckets, buckethisto);
101,617✔
4880

4881
  double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m);
101,643✔
4882
  for (int j = HLL_DATA_BITS; j >= 1; --j) {
5,179,820✔
4883
    z += buckethisto[j];
5,078,171✔
4884
    z *= 0.5;
5,078,171✔
4885
  }
4886

4887
  z += m * hllSigma(buckethisto[0] / (double)m);
101,649✔
4888
  double E = (double)llroundl(HLL_ALPHA_INF * m * m / z);
101,648✔
4889

4890
  return (uint64_t)E;
101,648✔
4891
}
4892

4893
int32_t hllFunction(SqlFunctionCtx* pCtx) {
113,252✔
4894
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
113,252✔
4895

4896
  SInputColumnInfoData* pInput = &pCtx->input;
113,252✔
4897
  SColumnInfoData*      pCol = pInput->pData[0];
113,252✔
4898

4899
  int32_t type = pCol->info.type;
113,252✔
4900
  int32_t bytes = pCol->info.bytes;
113,252✔
4901

4902
  int32_t start = pInput->startRowIndex;
113,252✔
4903
  int32_t numOfRows = pInput->numOfRows;
113,252✔
4904

4905
  int32_t numOfElems = 0;
113,252✔
4906
  if (IS_NULL_TYPE(type)) {
113,252✔
4907
    goto _hll_over;
1,692✔
4908
  }
4909

4910
  for (int32_t i = start; i < numOfRows + start; ++i) {
4,380,842✔
4911
    if (pCol->hasNull && colDataIsNull_s(pCol, i)) {
5,953,356!
4912
      continue;
1,053,726✔
4913
    }
4914

4915
    numOfElems++;
3,215,917✔
4916

4917
    char* data = colDataGetData(pCol, i);
3,215,917!
4918
    if (IS_VAR_DATA_TYPE(type)) {
3,215,917!
4919
      bytes = varDataLen(data);
804,901✔
4920
      data = varDataVal(data);
804,901✔
4921
    }
4922

4923
    int32_t index = 0;
3,215,917✔
4924
    uint8_t count = hllCountNum(data, bytes, &index);
3,215,917✔
4925
    uint8_t oldcount = pInfo->buckets[index];
3,215,556✔
4926
    if (count > oldcount) {
3,215,556✔
4927
      pInfo->buckets[index] = count;
773,367✔
4928
    }
4929
  }
4930

4931
_hll_over:
111,199✔
4932
  pInfo->totalCount += numOfElems;
112,891✔
4933

4934
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
112,891✔
4935
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
2,781✔
4936
  } else {
4937
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
110,110✔
4938
  }
4939

4940
  return TSDB_CODE_SUCCESS;
112,891✔
4941
}
4942

4943
static void hllTransferInfo(SHLLInfo* pInput, SHLLInfo* pOutput) {
7,519✔
4944
  for (int32_t k = 0; k < HLL_BUCKETS; ++k) {
119,683,730✔
4945
    if (pOutput->buckets[k] < pInput->buckets[k]) {
119,676,211✔
4946
      pOutput->buckets[k] = pInput->buckets[k];
197,862✔
4947
    }
4948
  }
4949
  pOutput->totalCount += pInput->totalCount;
7,519✔
4950
}
7,519✔
4951

4952
int32_t hllFunctionMerge(SqlFunctionCtx* pCtx) {
7,517✔
4953
  SInputColumnInfoData* pInput = &pCtx->input;
7,517✔
4954
  SColumnInfoData*      pCol = pInput->pData[0];
7,517✔
4955

4956
  if (IS_NULL_TYPE(pCol->info.type)) {
7,517!
4957
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4958
    return TSDB_CODE_SUCCESS;
×
4959
  }
4960

4961
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
7,517!
4962
    return TSDB_CODE_SUCCESS;
×
4963
  }
4964

4965
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,517✔
4966

4967
  int32_t start = pInput->startRowIndex;
7,517✔
4968

4969
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
15,035✔
4970
    if (colDataIsNull_s(pCol, i)) continue;
15,034!
4971
    char*     data = colDataGetData(pCol, i);
7,517!
4972
    SHLLInfo* pInputInfo = (SHLLInfo*)varDataVal(data);
7,517✔
4973
    hllTransferInfo(pInputInfo, pInfo);
7,517✔
4974
  }
4975

4976
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
7,518✔
4977
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
5✔
4978
  } else {
4979
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
7,513✔
4980
  }
4981

4982
  return TSDB_CODE_SUCCESS;
7,518✔
4983
}
4984

4985
int32_t hllFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
101,615✔
4986
  SResultRowEntryInfo* pInfo = GET_RES_INFO(pCtx);
101,615✔
4987

4988
  SHLLInfo* pHllInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
101,615✔
4989
  pHllInfo->result = hllCountCnt(pHllInfo->buckets);
101,615✔
4990
  if (tsCountAlwaysReturnValue && pHllInfo->result == 0) {
101,660✔
4991
    pInfo->numOfRes = 1;
19,198✔
4992
  }
4993

4994
  return functionFinalize(pCtx, pBlock);
101,660✔
4995
}
4996

4997
int32_t hllPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
7,541✔
4998
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
7,541✔
4999
  SHLLInfo*            pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,541✔
5000
  int32_t              resultBytes = getHLLInfoSize();
7,541✔
5001
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
7,541✔
5002

5003
  if (NULL == res) {
7,543!
5004
    return terrno;
×
5005
  }
5006
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
7,543✔
5007
  varDataSetLen(res, resultBytes);
7,543✔
5008

5009
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
7,543✔
5010
  int32_t          code = TSDB_CODE_SUCCESS;
7,543✔
5011
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
7,543✔
5012
  if (NULL == pCol) {
7,544!
5013
    code = terrno;
×
5014
    goto _exit;
×
5015
  }
5016

5017
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
7,544✔
5018

5019
_exit:
7,543✔
5020
  taosMemoryFree(res);
7,543✔
5021
  return code;
7,544✔
5022
}
5023

5024
int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
1✔
5025
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
1✔
5026
  SHLLInfo*            pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
1✔
5027

5028
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
1✔
5029
  SHLLInfo*            pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
1✔
5030

5031
  hllTransferInfo(pSBuf, pDBuf);
1✔
5032
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
1✔
5033
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
1✔
5034
  return TSDB_CODE_SUCCESS;
1✔
5035
}
5036

5037
bool getStateFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
11,008✔
5038
  pEnv->calcMemSize = sizeof(SStateInfo);
11,008✔
5039
  return true;
11,008✔
5040
}
5041

5042
static int8_t getStateOpType(char* opStr) {
11,212✔
5043
  int8_t opType;
5044
  if (strncasecmp(opStr, "LT", 2) == 0) {
11,212✔
5045
    opType = STATE_OPER_LT;
2,874✔
5046
  } else if (strncasecmp(opStr, "GT", 2) == 0) {
8,338✔
5047
    opType = STATE_OPER_GT;
1,540✔
5048
  } else if (strncasecmp(opStr, "LE", 2) == 0) {
6,798✔
5049
    opType = STATE_OPER_LE;
992✔
5050
  } else if (strncasecmp(opStr, "GE", 2) == 0) {
5,806✔
5051
    opType = STATE_OPER_GE;
2,884✔
5052
  } else if (strncasecmp(opStr, "NE", 2) == 0) {
2,922✔
5053
    opType = STATE_OPER_NE;
1,847✔
5054
  } else if (strncasecmp(opStr, "EQ", 2) == 0) {
1,075!
5055
    opType = STATE_OPER_EQ;
1,076✔
5056
  } else {
UNCOV
5057
    opType = STATE_OPER_INVALID;
×
5058
  }
5059

5060
  return opType;
11,212✔
5061
}
5062

5063
static bool checkStateOp(int8_t op, SColumnInfoData* pCol, int32_t index, SVariant param) {
469,390✔
5064
  char* data = colDataGetData(pCol, index);
469,390!
5065
  switch (pCol->info.type) {
469,390!
5066
    case TSDB_DATA_TYPE_TINYINT: {
103,196✔
5067
      int8_t v = *(int8_t*)data;
103,196✔
5068
      STATE_COMP(op, v, param);
103,196!
5069
      break;
×
5070
    }
5071
    case TSDB_DATA_TYPE_UTINYINT: {
5,760✔
5072
      uint8_t v = *(uint8_t*)data;
5,760✔
5073
      STATE_COMP(op, v, param);
5,760!
5074
      break;
×
5075
    }
5076
    case TSDB_DATA_TYPE_SMALLINT: {
111,312✔
5077
      int16_t v = *(int16_t*)data;
111,312✔
5078
      STATE_COMP(op, v, param);
111,312!
5079
      break;
×
5080
    }
5081
    case TSDB_DATA_TYPE_USMALLINT: {
5,760✔
5082
      uint16_t v = *(uint16_t*)data;
5,760✔
5083
      STATE_COMP(op, v, param);
5,760!
5084
      break;
×
5085
    }
5086
    case TSDB_DATA_TYPE_INT: {
126,524✔
5087
      int32_t v = *(int32_t*)data;
126,524✔
5088
      STATE_COMP(op, v, param);
126,524!
5089
      break;
×
5090
    }
5091
    case TSDB_DATA_TYPE_UINT: {
5,760✔
5092
      uint32_t v = *(uint32_t*)data;
5,760✔
5093
      STATE_COMP(op, v, param);
5,760!
5094
      break;
×
5095
    }
5096
    case TSDB_DATA_TYPE_BIGINT: {
87,168✔
5097
      int64_t v = *(int64_t*)data;
87,168✔
5098
      STATE_COMP(op, v, param);
87,168!
5099
      break;
×
5100
    }
5101
    case TSDB_DATA_TYPE_UBIGINT: {
5,760✔
5102
      uint64_t v = *(uint64_t*)data;
5,760✔
5103
      STATE_COMP(op, v, param);
5,760!
5104
      break;
×
5105
    }
5106
    case TSDB_DATA_TYPE_FLOAT: {
12,612✔
5107
      float v = *(float*)data;
12,612✔
5108
      STATE_COMP(op, v, param);
12,612!
5109
      break;
×
5110
    }
5111
    case TSDB_DATA_TYPE_DOUBLE: {
7,956✔
5112
      double v = *(double*)data;
7,956✔
5113
      STATE_COMP(op, v, param);
7,956!
5114
      break;
×
5115
    }
5116
    default: {
×
5117
      return false;
×
5118
    }
5119
  }
5120
  return false;
×
5121
}
5122

5123
int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
2,263✔
5124
  int32_t              code = TSDB_CODE_SUCCESS;
2,263✔
5125
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,263✔
5126
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
2,263✔
5127

5128
  SInputColumnInfoData* pInput = &pCtx->input;
2,263✔
5129
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
2,263✔
5130

5131
  SColumnInfoData* pInputCol = pInput->pData[0];
2,263✔
5132

5133
  int32_t          numOfElems = 0;
2,263✔
5134
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
2,263✔
5135

5136
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
2,263✔
5137
  if (STATE_OPER_INVALID == op) {
2,263!
5138
    return 0;
×
5139
  }
5140

5141
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
362,699✔
5142
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
360,436!
5143
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5144
    } else {
5145
      pInfo->prevTs = tsList[i];
360,436✔
5146
    }
5147

5148
    pInfo->isPrevTsSet = true;
360,436✔
5149
    numOfElems++;
360,436✔
5150

5151
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
360,436✔
5152
      colDataSetNULL(pOutput, i);
262,224!
5153
      // handle selectivity
5154
      if (pCtx->subsidiaries.num > 0) {
262,224✔
5155
        code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
44✔
5156
        if (TSDB_CODE_SUCCESS != code) {
44!
5157
          return code;
×
5158
        }
5159
      }
5160
      continue;
262,224✔
5161
    }
5162

5163
    bool ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
98,212✔
5164

5165
    int64_t output = -1;
98,212✔
5166
    if (ret) {
98,212✔
5167
      output = ++pInfo->count;
6,341✔
5168
    } else {
5169
      pInfo->count = 0;
91,871✔
5170
    }
5171
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
98,212✔
5172
    if (TSDB_CODE_SUCCESS != code) {
98,212!
5173
      return code;
×
5174
    }
5175

5176
    // handle selectivity
5177
    if (pCtx->subsidiaries.num > 0) {
98,212✔
5178
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
528✔
5179
      if (TSDB_CODE_SUCCESS != code) {
528!
5180
        return code;
×
5181
      }
5182
    }
5183
  }
5184

5185
  pResInfo->numOfRes = numOfElems;
2,263✔
5186
  return TSDB_CODE_SUCCESS;
2,263✔
5187
}
5188

5189
int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
8,949✔
5190
  int32_t              code = TSDB_CODE_SUCCESS;
8,949✔
5191
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,949✔
5192
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
8,949✔
5193

5194
  SInputColumnInfoData* pInput = &pCtx->input;
8,949✔
5195
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
8,949✔
5196

5197
  SColumnInfoData* pInputCol = pInput->pData[0];
8,949✔
5198

5199
  int32_t          numOfElems = 0;
8,949✔
5200
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
8,949✔
5201

5202
  // TODO: process timeUnit for different db precisions
5203
  int32_t timeUnit = 1;
8,949✔
5204
  if (pCtx->numOfParams == 5) {  // TODO: param number incorrect
8,949✔
5205
    timeUnit = pCtx->param[3].param.i;
6,989✔
5206
  }
5207

5208
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
8,949✔
5209
  if (STATE_OPER_INVALID == op) {
8,950!
5210
    return TSDB_CODE_INVALID_PARA;
×
5211
  }
5212

5213
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
581,160✔
5214
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
572,208!
5215
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5216
    } else {
5217
      pInfo->prevTs = tsList[i];
572,208✔
5218
    }
5219

5220
    pInfo->isPrevTsSet = true;
572,208✔
5221
    numOfElems++;
572,208✔
5222

5223
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
572,208✔
5224
      colDataSetNULL(pOutput, i);
200,994!
5225
      // handle selectivity
5226
      if (pCtx->subsidiaries.num > 0) {
200,994✔
5227
        code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
60✔
5228
        if (TSDB_CODE_SUCCESS != code) {
60!
5229
          return code;
×
5230
        }
5231
      }
5232
      continue;
200,994✔
5233
    }
5234

5235
    bool    ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
371,214✔
5236
    int64_t output = -1;
371,043✔
5237
    if (ret) {
371,043✔
5238
      if (pInfo->durationStart == 0) {
270,709✔
5239
        output = 0;
51,771✔
5240
        pInfo->durationStart = tsList[i];
51,771✔
5241
      } else {
5242
        output = (tsList[i] - pInfo->durationStart) / timeUnit;
218,938✔
5243
      }
5244
    } else {
5245
      pInfo->durationStart = 0;
100,334✔
5246
    }
5247
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
371,043✔
5248
    if (TSDB_CODE_SUCCESS != code) {
371,216!
5249
      return code;
×
5250
    }
5251

5252
    // handle selectivity
5253
    if (pCtx->subsidiaries.num > 0) {
371,216✔
5254
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
720✔
5255
      if (TSDB_CODE_SUCCESS != code) {
720!
5256
        return code;
×
5257
      }
5258
    }
5259
  }
5260

5261
  pResInfo->numOfRes = numOfElems;
8,952✔
5262
  return TSDB_CODE_SUCCESS;
8,952✔
5263
}
5264

5265
bool getCsumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
5,270✔
5266
  pEnv->calcMemSize = sizeof(SSumRes);
5,270✔
5267
  return true;
5,270✔
5268
}
5269

5270
int32_t csumFunction(SqlFunctionCtx* pCtx) {
5,908✔
5271
  int32_t              code = TSDB_CODE_SUCCESS;
5,908✔
5272
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5,908✔
5273
  SSumRes*             pSumRes = GET_ROWCELL_INTERBUF(pResInfo);
5,908✔
5274

5275
  SInputColumnInfoData* pInput = &pCtx->input;
5,908✔
5276
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5,908✔
5277

5278
  SColumnInfoData* pInputCol = pInput->pData[0];
5,908✔
5279
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5,908✔
5280

5281
  int32_t numOfElems = 0;
5,908✔
5282
  int32_t type = pInputCol->info.type;
5,908✔
5283
  int32_t startOffset = pCtx->offset;
5,908✔
5284
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
882,480✔
5285
    if (pSumRes->isPrevTsSet == true && tsList[i] == pSumRes->prevTs) {
876,530✔
5286
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
18✔
5287
    } else {
5288
      pSumRes->prevTs = tsList[i];
876,512✔
5289
    }
5290
    pSumRes->isPrevTsSet = true;
876,512✔
5291

5292
    int32_t pos = startOffset + numOfElems;
876,512✔
5293
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
876,512✔
5294
      // colDataSetNULL(pOutput, i);
5295
      continue;
506,351✔
5296
    }
5297

5298
    char* data = colDataGetData(pInputCol, i);
370,161!
5299
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
661,191✔
5300
      int64_t v;
5301
      GET_TYPED_DATA(v, int64_t, type, data);
290,970!
5302
      pSumRes->isum += v;
290,970✔
5303
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false);
290,970✔
5304
      if (TSDB_CODE_SUCCESS != code) {
291,030!
5305
        return code;
×
5306
      }
5307
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
79,871!
5308
      uint64_t v;
5309
      GET_TYPED_DATA(v, uint64_t, type, data);
680!
5310
      pSumRes->usum += v;
680✔
5311
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->usum, false);
680✔
5312
      if (TSDB_CODE_SUCCESS != code) {
680!
5313
        return code;
×
5314
      }
5315
    } else if (IS_FLOAT_TYPE(type)) {
78,511✔
5316
      double v;
5317
      GET_TYPED_DATA(v, double, type, data);
78,500!
5318
      pSumRes->dsum += v;
78,500✔
5319
      // check for overflow
5320
      if (isinf(pSumRes->dsum) || isnan(pSumRes->dsum)) {
78,500!
5321
        colDataSetNULL(pOutput, pos);
8!
5322
      } else {
5323
        code = colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false);
78,492✔
5324
        if (TSDB_CODE_SUCCESS != code) {
78,492!
5325
          return code;
×
5326
        }
5327
      }
5328
    }
5329

5330
    // handle selectivity
5331
    if (pCtx->subsidiaries.num > 0) {
370,221✔
5332
      code = appendSelectivityValue(pCtx, i, pos);
1,524✔
5333
      if (TSDB_CODE_SUCCESS != code) {
1,524!
5334
        return code;
×
5335
      }
5336
    }
5337

5338
    numOfElems++;
370,221✔
5339
  }
5340

5341
  pResInfo->numOfRes = numOfElems;
5,950✔
5342
  return TSDB_CODE_SUCCESS;
5,950✔
5343
}
5344

5345
bool getMavgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
3,918✔
5346
  pEnv->calcMemSize = sizeof(SMavgInfo) + MAVG_MAX_POINTS_NUM * sizeof(double);
3,918✔
5347
  return true;
3,918✔
5348
}
5349

5350
int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
8,683✔
5351
  if (pResultInfo->initialized) {
8,683✔
5352
    return TSDB_CODE_SUCCESS;
3,757✔
5353
  }
5354
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
4,926!
5355
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5356
  }
5357

5358
  SMavgInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
4,926✔
5359
  pInfo->pos = 0;
4,926✔
5360
  pInfo->sum = 0;
4,926✔
5361
  pInfo->prevTs = -1;
4,926✔
5362
  pInfo->isPrevTsSet = false;
4,926✔
5363
  pInfo->numOfPoints = pCtx->param[1].param.i;
4,926✔
5364
  if (pInfo->numOfPoints < 1 || pInfo->numOfPoints > MAVG_MAX_POINTS_NUM) {
4,926!
5365
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
5366
  }
5367
  pInfo->pointsMeet = false;
4,926✔
5368

5369
  return TSDB_CODE_SUCCESS;
4,926✔
5370
}
5371

5372
int32_t mavgFunction(SqlFunctionCtx* pCtx) {
4,762✔
5373
  int32_t              code = TSDB_CODE_SUCCESS;
4,762✔
5374
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
4,762✔
5375
  SMavgInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
4,762✔
5376

5377
  SInputColumnInfoData* pInput = &pCtx->input;
4,762✔
5378
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
4,762✔
5379

5380
  SColumnInfoData* pInputCol = pInput->pData[0];
4,762✔
5381
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
4,762✔
5382
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
4,762✔
5383

5384
  int32_t numOfElems = 0;
4,762✔
5385
  int32_t type = pInputCol->info.type;
4,762✔
5386
  int32_t startOffset = pCtx->offset;
4,762✔
5387
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
720,179✔
5388
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
715,417!
5389
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5390
    } else {
5391
      pInfo->prevTs = tsList[i];
715,417✔
5392
    }
5393
    pInfo->isPrevTsSet = true;
715,417✔
5394

5395
    int32_t pos = startOffset + numOfElems;
715,417✔
5396
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
715,417✔
5397
      // colDataSetNULL(pOutput, i);
5398
      continue;
261,225✔
5399
    }
5400

5401
    char*  data = colDataGetData(pInputCol, i);
454,192!
5402
    double v;
5403
    GET_TYPED_DATA(v, double, type, data);
454,192!
5404

5405
    if (!pInfo->pointsMeet && (pInfo->pos < pInfo->numOfPoints - 1)) {
454,192✔
5406
      pInfo->points[pInfo->pos] = v;
444,484✔
5407
      pInfo->sum += v;
444,484✔
5408
    } else {
5409
      if (!pInfo->pointsMeet && (pInfo->pos == pInfo->numOfPoints - 1)) {
9,708!
5410
        pInfo->sum += v;
948✔
5411
        pInfo->pointsMeet = true;
948✔
5412
      } else {
5413
        pInfo->sum = pInfo->sum + v - pInfo->points[pInfo->pos];
8,760✔
5414
      }
5415

5416
      pInfo->points[pInfo->pos] = v;
9,708✔
5417
      double result = pInfo->sum / pInfo->numOfPoints;
9,708✔
5418
      // check for overflow
5419
      if (isinf(result) || isnan(result)) {
9,708!
5420
        colDataSetNULL(pOutput, pos);
×
5421
      } else {
5422
        code = colDataSetVal(pOutput, pos, (char*)&result, false);
9,770✔
5423
        if (TSDB_CODE_SUCCESS != code) {
9,770!
5424
          return code;
×
5425
        }
5426
      }
5427

5428
      // handle selectivity
5429
      if (pCtx->subsidiaries.num > 0) {
9,708✔
5430
        code = appendSelectivityValue(pCtx, i, pos);
1,520✔
5431
        if (TSDB_CODE_SUCCESS != code) {
1,520!
5432
          return code;
×
5433
        }
5434
      }
5435

5436
      numOfElems++;
9,708✔
5437
    }
5438

5439
    pInfo->pos++;
454,192✔
5440
    if (pInfo->pos == pInfo->numOfPoints) {
454,192✔
5441
      pInfo->pos = 0;
1,568✔
5442
    }
5443
  }
5444

5445
  pResInfo->numOfRes = numOfElems;
4,762✔
5446
  return TSDB_CODE_SUCCESS;
4,762✔
5447
}
5448

5449
static SSampleInfo* getSampleOutputInfo(SqlFunctionCtx* pCtx) {
37,206✔
5450
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
37,206✔
5451
  SSampleInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
37,206✔
5452

5453
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
37,206✔
5454
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
37,206✔
5455

5456
  return pInfo;
37,206✔
5457
}
5458

5459
bool getSampleFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
6,656✔
5460
  SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
6,656✔
5461
  SValueNode*  pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
6,663✔
5462
  int32_t      numOfSamples = pVal->datum.i;
6,665✔
5463
  pEnv->calcMemSize = sizeof(SSampleInfo) + numOfSamples * (pCol->node.resType.bytes + sizeof(STuplePos));
6,665✔
5464
  return true;
6,665✔
5465
}
5466

5467
int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
16,537✔
5468
  if (pResultInfo->initialized) {
16,537!
5469
    return TSDB_CODE_SUCCESS;
×
5470
  }
5471
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
16,537!
5472
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5473
  }
5474

5475
  taosSeedRand(taosSafeRand());
16,538✔
5476

5477
  SSampleInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
16,542✔
5478
  pInfo->samples = pCtx->param[1].param.i;
16,542✔
5479
  pInfo->totalPoints = 0;
16,542✔
5480
  pInfo->numSampled = 0;
16,542✔
5481
  pInfo->colType = pCtx->resDataInfo.type;
16,542✔
5482
  pInfo->colBytes = pCtx->resDataInfo.bytes;
16,542✔
5483
  pInfo->nullTuplePos.pageId = -1;
16,542✔
5484
  pInfo->nullTupleSaved = false;
16,542✔
5485
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
16,542✔
5486
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
16,542✔
5487

5488
  return TSDB_CODE_SUCCESS;
16,542✔
5489
}
5490

5491
static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) {
597,281✔
5492
  assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType);
597,281✔
5493
}
597,336✔
5494

5495
static int32_t doReservoirSample(SqlFunctionCtx* pCtx, SSampleInfo* pInfo, char* data, int32_t index) {
661,957✔
5496
  pInfo->totalPoints++;
661,957✔
5497
  if (pInfo->numSampled < pInfo->samples) {
661,957✔
5498
    sampleAssignResult(pInfo, data, pInfo->numSampled);
545,579✔
5499
    if (pCtx->subsidiaries.num > 0) {
545,577✔
5500
      int32_t code = saveTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[pInfo->numSampled]);
11,226✔
5501
      if (code != TSDB_CODE_SUCCESS) {
11,249!
5502
        return code;
×
5503
      }
5504
    }
5505
    pInfo->numSampled++;
545,600✔
5506
  } else {
5507
    int32_t j = taosRand() % (pInfo->totalPoints);
116,378✔
5508
    if (j < pInfo->samples) {
117,631✔
5509
      sampleAssignResult(pInfo, data, j);
52,778✔
5510
      if (pCtx->subsidiaries.num > 0) {
52,777✔
5511
        int32_t code = updateTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[j]);
4,106✔
5512
        if (code != TSDB_CODE_SUCCESS) {
2,955!
5513
          return code;
×
5514
        }
5515
      }
5516
    }
5517
  }
5518

5519
  return TSDB_CODE_SUCCESS;
662,079✔
5520
}
5521

5522
int32_t sampleFunction(SqlFunctionCtx* pCtx) {
20,666✔
5523
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
20,666✔
5524
  SSampleInfo*         pInfo = getSampleOutputInfo(pCtx);
20,666✔
5525

5526
  SInputColumnInfoData* pInput = &pCtx->input;
20,666✔
5527

5528
  SColumnInfoData* pInputCol = pInput->pData[0];
20,666✔
5529
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
971,851✔
5530
    if (colDataIsNull_s(pInputCol, i)) {
1,897,978✔
5531
      continue;
289,156✔
5532
    }
5533

5534
    char*   data = colDataGetData(pInputCol, i);
659,833!
5535
    int32_t code = doReservoirSample(pCtx, pInfo, data, i);
659,833✔
5536
    if (code != TSDB_CODE_SUCCESS) {
662,029!
5537
      return code;
×
5538
    }
5539
  }
5540

5541
  if (pInfo->numSampled == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
22,862✔
5542
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
35✔
5543
    if (code != TSDB_CODE_SUCCESS) {
35!
5544
      return code;
×
5545
    }
5546
    pInfo->nullTupleSaved = true;
35✔
5547
  }
5548

5549
  SET_VAL(pResInfo, pInfo->numSampled, pInfo->numSampled);
22,862✔
5550
  return TSDB_CODE_SUCCESS;
22,862✔
5551
}
5552

5553
int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
16,542✔
5554
  int32_t              code = TSDB_CODE_SUCCESS;
16,542✔
5555
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
16,542✔
5556

5557
  SSampleInfo* pInfo = getSampleOutputInfo(pCtx);
16,542✔
5558
  pEntryInfo->complete = true;
16,542✔
5559

5560
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
16,542✔
5561
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
16,542✔
5562
  if (NULL == pCol) {
16,542!
5563
    return TSDB_CODE_OUT_OF_RANGE;
×
5564
  }
5565

5566
  int32_t currentRow = pBlock->info.rows;
16,542✔
5567
  if (pInfo->numSampled == 0) {
16,542✔
5568
    colDataSetNULL(pCol, currentRow);
1,907✔
5569
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
1,907✔
5570
    return code;
1,907✔
5571
  }
5572
  for (int32_t i = 0; i < pInfo->numSampled; ++i) {
560,320✔
5573
    code = colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false);
545,738✔
5574
    if (TSDB_CODE_SUCCESS != code) {
545,765!
5575
      return code;
×
5576
    }
5577
    code = setSelectivityValue(pCtx, pBlock, &pInfo->tuplePos[i], currentRow + i);
545,765✔
5578
    if (TSDB_CODE_SUCCESS != code) {
545,685!
5579
      return code;
×
5580
    }
5581
  }
5582

5583
  return code;
14,582✔
5584
}
5585

5586
bool getTailFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
5587
#if 0
5588
  SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
5589
  SValueNode*  pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
5590
  int32_t      numOfPoints = pVal->datum.i;
5591
  pEnv->calcMemSize = sizeof(STailInfo) + numOfPoints * (POINTER_BYTES + sizeof(STailItem) + pCol->node.resType.bytes);
5592
#endif
5593
  return true;
×
5594
}
5595

5596
int32_t tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
5597
#if 0
5598
  if (!functionSetup(pCtx, pResultInfo)) {
5599
    return false;
5600
  }
5601

5602
  STailInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
5603
  pInfo->numAdded = 0;
5604
  pInfo->numOfPoints = pCtx->param[1].param.i;
5605
  if (pCtx->numOfParams == 4) {
5606
    pInfo->offset = pCtx->param[2].param.i;
5607
  } else {
5608
    pInfo->offset = 0;
5609
  }
5610
  pInfo->colType = pCtx->resDataInfo.type;
5611
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5612
  if ((pInfo->numOfPoints < 1 || pInfo->numOfPoints > TAIL_MAX_POINTS_NUM) ||
5613
      (pInfo->numOfPoints < 0 || pInfo->numOfPoints > TAIL_MAX_OFFSET)) {
5614
    return false;
5615
  }
5616

5617
  pInfo->pItems = (STailItem**)((char*)pInfo + sizeof(STailInfo));
5618
  char* pItem = (char*)pInfo->pItems + pInfo->numOfPoints * POINTER_BYTES;
5619

5620
  size_t unitSize = sizeof(STailItem) + pInfo->colBytes;
5621
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5622
    pInfo->pItems[i] = (STailItem*)(pItem + i * unitSize);
5623
    pInfo->pItems[i]->isNull = false;
5624
  }
5625
#endif
5626

5627
  return TSDB_CODE_SUCCESS;
×
5628
}
5629

5630
static void tailAssignResult(STailItem* pItem, char* data, int32_t colBytes, TSKEY ts, bool isNull) {
×
5631
#if 0
5632
  pItem->timestamp = ts;
5633
  if (isNull) {
5634
    pItem->isNull = true;
5635
  } else {
5636
    pItem->isNull = false;
5637
    memcpy(pItem->data, data, colBytes);
5638
  }
5639
#endif
5640
}
×
5641

5642
#if 0
5643
static int32_t tailCompFn(const void* p1, const void* p2, const void* param) {
5644
  STailItem* d1 = *(STailItem**)p1;
5645
  STailItem* d2 = *(STailItem**)p2;
5646
  return compareInt64Val(&d1->timestamp, &d2->timestamp);
5647
}
5648

5649
static void doTailAdd(STailInfo* pInfo, char* data, TSKEY ts, bool isNull) {
5650
  STailItem** pList = pInfo->pItems;
5651
  if (pInfo->numAdded < pInfo->numOfPoints) {
5652
    tailAssignResult(pList[pInfo->numAdded], data, pInfo->colBytes, ts, isNull);
5653
    taosheapsort((void*)pList, sizeof(STailItem**), pInfo->numAdded + 1, NULL, tailCompFn, 0);
5654
    pInfo->numAdded++;
5655
  } else if (pList[0]->timestamp < ts) {
5656
    tailAssignResult(pList[0], data, pInfo->colBytes, ts, isNull);
5657
    taosheapadjust((void*)pList, sizeof(STailItem**), 0, pInfo->numOfPoints - 1, NULL, tailCompFn, NULL, 0);
5658
  }
5659
}
5660
#endif
5661

5662
int32_t tailFunction(SqlFunctionCtx* pCtx) {
×
5663
#if 0
5664
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5665
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5666

5667
  SInputColumnInfoData* pInput = &pCtx->input;
5668
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5669

5670
  SColumnInfoData* pInputCol = pInput->pData[0];
5671
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5672

5673
  int32_t startOffset = pCtx->offset;
5674
  if (pInfo->offset >= pInput->numOfRows) {
5675
    return 0;
5676
  } else {
5677
    pInfo->numOfPoints = TMIN(pInfo->numOfPoints, pInput->numOfRows - pInfo->offset);
5678
  }
5679
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex - pInfo->offset; i += 1) {
5680
    char* data = colDataGetData(pInputCol, i);
5681
    doTailAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5682
  }
5683

5684
  taosqsort(pInfo->pItems, pInfo->numOfPoints, POINTER_BYTES, NULL, tailCompFn);
5685

5686
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5687
    int32_t    pos = startOffset + i;
5688
    STailItem* pItem = pInfo->pItems[i];
5689
    if (pItem->isNull) {
5690
      colDataSetNULL(pOutput, pos);
5691
    } else {
5692
      colDataSetVal(pOutput, pos, pItem->data, false);
5693
    }
5694
  }
5695

5696
  return pInfo->numOfPoints;
5697
#endif
5698
  return 0;
×
5699
}
5700

5701
int32_t tailFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
5702
#if 0
5703
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
5704
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pEntryInfo);
5705
  pEntryInfo->complete = true;
5706

5707
  int32_t type = pCtx->input.pData[0]->info.type;
5708
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
5709

5710
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5711

5712
  // todo assign the tag value and the corresponding row data
5713
  int32_t currentRow = pBlock->info.rows;
5714
  for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) {
5715
    STailItem* pItem = pInfo->pItems[i];
5716
    colDataSetVal(pCol, currentRow, pItem->data, false);
5717
    currentRow += 1;
5718
  }
5719

5720
  return pEntryInfo->numOfRes;
5721
#endif
5722
  return 0;
×
5723
}
5724

5725
bool getUniqueFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
5726
#if 0
5727
  pEnv->calcMemSize = sizeof(SUniqueInfo) + UNIQUE_MAX_RESULT_SIZE;
5728
#endif
5729
  return true;
×
5730
}
5731

5732
int32_t uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
5733
#if 0
5734
  if (!functionSetup(pCtx, pResInfo)) {
5735
    return false;
5736
  }
5737

5738
  SUniqueInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5739
  pInfo->numOfPoints = 0;
5740
  pInfo->colType = pCtx->resDataInfo.type;
5741
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5742
  if (pInfo->pHash != NULL) {
5743
    taosHashClear(pInfo->pHash);
5744
  } else {
5745
    pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5746
  }
5747
#endif
5748
  return TSDB_CODE_SUCCESS;
×
5749
}
5750

5751
#if 0
5752
static void doUniqueAdd(SUniqueInfo* pInfo, char* data, TSKEY ts, bool isNull) {
5753
  // handle null elements
5754
  if (isNull == true) {
5755
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5756
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5757
    if (pInfo->hasNull == false && pItem->isNull == false) {
5758
      pItem->timestamp = ts;
5759
      pItem->isNull = true;
5760
      pInfo->numOfPoints++;
5761
      pInfo->hasNull = true;
5762
    } else if (pItem->timestamp > ts && pItem->isNull == true) {
5763
      pItem->timestamp = ts;
5764
    }
5765
    return;
5766
  }
5767

5768
  int32_t      hashKeyBytes = IS_VAR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
5769
  SUniqueItem* pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes);
5770
  if (pHashItem == NULL) {
5771
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5772
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5773
    pItem->timestamp = ts;
5774
    memcpy(pItem->data, data, pInfo->colBytes);
5775

5776
    taosHashPut(pInfo->pHash, data, hashKeyBytes, (char*)pItem, sizeof(SUniqueItem*));
5777
    pInfo->numOfPoints++;
5778
  } else if (pHashItem->timestamp > ts) {
5779
    pHashItem->timestamp = ts;
5780
  }
5781
}
5782
#endif
5783

5784
int32_t uniqueFunction(SqlFunctionCtx* pCtx) {
×
5785
#if 0
5786
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5787
  SUniqueInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5788

5789
  SInputColumnInfoData* pInput = &pCtx->input;
5790
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5791

5792
  SColumnInfoData* pInputCol = pInput->pData[0];
5793
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
5794
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5795

5796
  int32_t startOffset = pCtx->offset;
5797
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
5798
    char* data = colDataGetData(pInputCol, i);
5799
    doUniqueAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5800

5801
    if (sizeof(SUniqueInfo) + pInfo->numOfPoints * (sizeof(SUniqueItem) + pInfo->colBytes) >= UNIQUE_MAX_RESULT_SIZE) {
5802
      taosHashCleanup(pInfo->pHash);
5803
      return 0;
5804
    }
5805
  }
5806

5807
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5808
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + i * (sizeof(SUniqueItem) + pInfo->colBytes));
5809
    if (pItem->isNull == true) {
5810
      colDataSetNULL(pOutput, i);
5811
    } else {
5812
      colDataSetVal(pOutput, i, pItem->data, false);
5813
    }
5814
    if (pTsOutput != NULL) {
5815
      colDataSetInt64(pTsOutput, i, &pItem->timestamp);
5816
    }
5817
  }
5818

5819
  return pInfo->numOfPoints;
5820
#endif
5821
  return 0;
×
5822
}
5823

5824
bool getModeFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
5,742✔
5825
  pEnv->calcMemSize = sizeof(SModeInfo);
5,742✔
5826
  return true;
5,742✔
5827
}
5828

5829
int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
5,612✔
5830
  if (pResInfo->initialized) {
5,612!
5831
    return TSDB_CODE_SUCCESS;
×
5832
  }
5833
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
5,612!
5834
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5835
  }
5836

5837
  SModeInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,612✔
5838
  pInfo->colType = pCtx->resDataInfo.type;
5,612✔
5839
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5,612✔
5840
  if (pInfo->pHash != NULL) {
5,612!
5841
    taosHashClear(pInfo->pHash);
×
5842
  } else {
5843
    pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5,612✔
5844
    if (NULL == pInfo->pHash) {
5,612!
5845
      return terrno;
×
5846
    }
5847
  }
5848
  pInfo->nullTupleSaved = false;
5,612✔
5849
  pInfo->nullTuplePos.pageId = -1;
5,612✔
5850

5851
  pInfo->buf = taosMemoryMalloc(pInfo->colBytes);
5,612✔
5852
  if (NULL == pInfo->buf) {
5,612!
UNCOV
5853
    taosHashCleanup(pInfo->pHash);
×
5854
    pInfo->pHash = NULL;
×
5855
    return terrno;
×
5856
  }
5857
  pCtx->needCleanup = true;
5,612✔
5858
  return TSDB_CODE_SUCCESS;
5,612✔
5859
}
5860

5861
static void modeFunctionCleanup(SModeInfo * pInfo) {
5,611✔
5862
  taosHashCleanup(pInfo->pHash);
5,611✔
5863
  pInfo->pHash = NULL;
5,611✔
5864
  taosMemoryFreeClear(pInfo->buf);
5,611!
5865
}
5,611✔
5866

5867
void modeFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
5868
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
5869
    return;
×
5870
  }
5871
  modeFunctionCleanup(GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)));
×
5872
}
5873

5874
static int32_t saveModeTupleData(SqlFunctionCtx* pCtx, char* data, SModeInfo *pInfo, STuplePos* pPos) {
419,578✔
5875
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
419,578!
5876
    (void)memcpy(pInfo->buf, data, varDataTLen(data));
104,894✔
5877
  } else {
5878
    (void)memcpy(pInfo->buf, data, pInfo->colBytes);
314,684✔
5879
  }
5880

5881
  return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore);
419,578✔
5882
}
5883

5884
static int32_t doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCtx, char* data) {
560,967✔
5885
  int32_t code = TSDB_CODE_SUCCESS;
560,967✔
5886
  int32_t hashKeyBytes = IS_STR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
560,967✔
5887

5888
  SModeItem* pHashItem = (SModeItem *)taosHashGet(pInfo->pHash, data, hashKeyBytes);
560,967✔
5889
  if (pHashItem == NULL) {
560,449✔
5890
    int32_t    size = sizeof(SModeItem);
419,572✔
5891
    SModeItem  item = {0};
419,572✔
5892

5893
    item.count += 1;
419,572✔
5894
    code = saveModeTupleData(pCtx, data, pInfo, &item.dataPos);
419,572✔
5895
    if (code != TSDB_CODE_SUCCESS) {
419,102!
5896
      return code;
×
5897
    }
5898

5899
    if (pCtx->subsidiaries.num > 0) {
419,102✔
5900
      code = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &item.tuplePos);
1,334✔
5901
      if (code != TSDB_CODE_SUCCESS) {
1,334!
5902
        return code;
×
5903
      }
5904
    }
5905

5906
    code = taosHashPut(pInfo->pHash, data, hashKeyBytes, &item, sizeof(SModeItem));
419,102✔
5907
    if (code != TSDB_CODE_SUCCESS) {
420,410!
5908
      return code;
×
5909
    }
5910
  } else {
5911
    pHashItem->count += 1;
140,877✔
5912
    if (pCtx->subsidiaries.num > 0) {
140,877✔
5913
      code = updateTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &pHashItem->tuplePos);
25,787✔
5914
      if (code != TSDB_CODE_SUCCESS) {
25,787!
5915
        return code;
×
5916
      }
5917
    }
5918
  }
5919

5920
  return code;
561,287✔
5921
}
5922

5923
int32_t modeFunction(SqlFunctionCtx* pCtx) {
9,013✔
5924
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
9,013✔
5925
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
9,013✔
5926

5927
  SInputColumnInfoData* pInput = &pCtx->input;
9,013✔
5928

5929
  SColumnInfoData* pInputCol = pInput->pData[0];
9,013✔
5930
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
9,013✔
5931

5932
  int32_t numOfElems = 0;
9,013✔
5933
  int32_t startOffset = pCtx->offset;
9,013✔
5934
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
988,801✔
5935
    if (colDataIsNull_s(pInputCol, i)) {
1,959,898✔
5936
      continue;
419,213✔
5937
    }
5938
    numOfElems++;
560,736✔
5939

5940
    char*   data = colDataGetData(pInputCol, i);
560,736!
5941
    int32_t code = doModeAdd(pInfo, i, pCtx, data);
560,736✔
5942
    if (code != TSDB_CODE_SUCCESS) {
561,248✔
5943
      modeFunctionCleanup(pInfo);
673✔
5944
      return code;
×
5945
    }
5946
  }
5947

5948
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
8,852!
5949
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
50✔
5950
    if (code != TSDB_CODE_SUCCESS) {
50!
5951
      modeFunctionCleanup(pInfo);
×
5952
      return code;
×
5953
    }
5954
    pInfo->nullTupleSaved = true;
50✔
5955
  }
5956

5957
  SET_VAL(pResInfo, numOfElems, 1);
8,852✔
5958

5959
  return TSDB_CODE_SUCCESS;
8,852✔
5960
}
5961

5962
int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
5,610✔
5963
  int32_t              code = TSDB_CODE_SUCCESS;
5,610✔
5964
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5,610✔
5965
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,610✔
5966
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
5,610✔
5967
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5,610✔
5968
  int32_t              currentRow = pBlock->info.rows;
5,611✔
5969
  if (NULL == pCol) {
5,611!
5970
    modeFunctionCleanup(pInfo);
×
5971
    return TSDB_CODE_OUT_OF_RANGE;
×
5972
  }
5973

5974
  STuplePos resDataPos, resTuplePos;
5975
  int32_t maxCount = 0;
5,611✔
5976

5977
  void *pIter = taosHashIterate(pInfo->pHash, NULL);
5,611✔
5978
  while (pIter != NULL) {
426,175✔
5979
    SModeItem *pItem = (SModeItem *)pIter;
420,563✔
5980
    if (pItem->count >= maxCount) {
420,563✔
5981
      maxCount = pItem->count;
370,928✔
5982
      resDataPos = pItem->dataPos;
370,928✔
5983
      resTuplePos = pItem->tuplePos;
370,928✔
5984
    }
5985

5986
    pIter = taosHashIterate(pInfo->pHash, pIter);
420,563✔
5987
  }
5988

5989
  if (maxCount != 0) {
5,612✔
5990
    char* pData = NULL;
3,256✔
5991
    code = loadTupleData(pCtx, &resDataPos, &pData);
3,256✔
5992
    if (pData == NULL || TSDB_CODE_SUCCESS != code) {
3,256!
5993
      code = terrno = TSDB_CODE_NOT_FOUND;
×
5994
      qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(),
×
5995
             resDataPos.streamTupleKey.groupId, resDataPos.streamTupleKey.ts);
5996
      modeFunctionCleanup(pInfo);
×
5997
      return code;
×
5998
    }
5999

6000
    code = colDataSetVal(pCol, currentRow, pData, false);
3,256✔
6001
    if (TSDB_CODE_SUCCESS != code) {
3,256!
6002
      modeFunctionCleanup(pInfo);
×
6003
     return code;
×
6004
    }
6005
    code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow);
3,256✔
6006
  } else {
6007
    colDataSetNULL(pCol, currentRow);
2,356!
6008
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
2,356✔
6009
  }
6010

6011
  modeFunctionCleanup(pInfo);
5,612✔
6012

6013
  return code;
5,611✔
6014
}
6015

6016
bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
4,006✔
6017
  pEnv->calcMemSize = sizeof(STwaInfo);
4,006✔
6018
  return true;
4,006✔
6019
}
6020

6021
int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
46,241✔
6022
  if (pResultInfo->initialized) {
46,241!
6023
    return TSDB_CODE_SUCCESS;
×
6024
  }
6025
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
46,241!
6026
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6027
  }
6028

6029
  STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
46,245✔
6030
  pInfo->numOfElems = 0;
46,245✔
6031
  pInfo->p.key = INT64_MIN;
46,245✔
6032
  pInfo->win = TSWINDOW_INITIALIZER;
46,245✔
6033
  return TSDB_CODE_SUCCESS;
46,245✔
6034
}
6035

6036
static double twa_get_area(SPoint1 s, SPoint1 e) {
840,031✔
6037
  if (e.key == INT64_MAX || s.key == INT64_MIN) {
840,031!
6038
    return 0;
×
6039
  }
6040

6041
  if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) {
840,158✔
6042
    return (s.val + e.val) * (e.key - s.key) / 2;
755,051✔
6043
  }
6044

6045
  double x = (s.key * e.val - e.key * s.val) / (e.val - s.val);
85,107✔
6046
  double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2;
85,107✔
6047
  return val;
85,107✔
6048
}
6049

6050
int32_t twaFunction(SqlFunctionCtx* pCtx) {
46,708✔
6051
  int32_t               code = TSDB_CODE_SUCCESS;
46,708✔
6052
  SInputColumnInfoData* pInput = &pCtx->input;
46,708✔
6053
  SColumnInfoData*      pInputCol = pInput->pData[0];
46,708✔
6054

6055
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
46,708✔
6056
  STwaInfo*            pInfo = GET_ROWCELL_INTERBUF(pResInfo);
46,708✔
6057
  SPoint1*             last = &pInfo->p;
46,708✔
6058

6059
  if (IS_NULL_TYPE(pInputCol->info.type)) {
46,708!
6060
    pInfo->numOfElems = 0;
×
6061
    goto _twa_over;
×
6062
  }
6063

6064
  funcInputUpdate(pCtx);
46,708✔
6065
  SFuncInputRow row = {0};
46,715✔
6066
  bool          result = false;
46,715✔
6067
  if (pCtx->start.key != INT64_MIN && last->key == INT64_MIN) {
46,715!
6068
    while (1) {
6069
      code = funcInputGetNextRow(pCtx, &row, &result);
13,274✔
6070
      if (TSDB_CODE_SUCCESS != code) {
13,274!
6071
        return code;
×
6072
      }
6073
      if (!result) {
13,274✔
6074
        break;
2✔
6075
      }
6076
      if (row.isDataNull) {
13,272✔
6077
        continue;
2✔
6078
      }
6079

6080
      last->key = row.ts;
13,270✔
6081

6082
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
13,270!
6083

6084
      pInfo->dOutput += twa_get_area(pCtx->start, *last);
13,270✔
6085
      pInfo->win.skey = pCtx->start.key;
13,270✔
6086
      pInfo->numOfElems++;
13,270✔
6087
      break;
13,270✔
6088
    }
6089
  } else if (pInfo->p.key == INT64_MIN) {
33,443✔
6090
    while (1) {
6091
      code = funcInputGetNextRow(pCtx, &row, &result);
153,983✔
6092
      if (TSDB_CODE_SUCCESS != code) {
153,984!
6093
        return code;
×
6094
      }
6095
      if (!result) {
153,984✔
6096
        break;
12,344✔
6097
      }
6098
      if (row.isDataNull) {
141,640✔
6099
        continue;
120,987✔
6100
      }
6101

6102
      last->key = row.ts;
20,653✔
6103

6104
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
20,653✔
6105

6106
      pInfo->win.skey = last->key;
20,653✔
6107
      pInfo->numOfElems++;
20,653✔
6108
      break;
20,653✔
6109
    }
6110
  }
6111

6112
  SPoint1 st = {0};
46,716✔
6113

6114
  // calculate the value of
6115
  while (1) {
6116
    code = funcInputGetNextRow(pCtx, &row, &result);
861,141✔
6117
    if (TSDB_CODE_SUCCESS != code) {
860,910!
6118
      return code;
×
6119
    }
6120
    if (!result) {
860,910✔
6121
      break;
46,719✔
6122
    }
6123
    if (row.isDataNull) {
814,191✔
6124
      continue;
630✔
6125
    }
6126
    pInfo->numOfElems++;
813,561✔
6127
    switch (pInputCol->info.type) {
813,561!
6128
      case TSDB_DATA_TYPE_TINYINT: {
88,358✔
6129
        INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData);
88,358✔
6130
        break;
88,358✔
6131
      }
6132
      case TSDB_DATA_TYPE_SMALLINT: {
81,767✔
6133
        INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData);
81,767✔
6134
        break;
81,767✔
6135
      }
6136
      case TSDB_DATA_TYPE_INT: {
120,109✔
6137
        INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData);
120,109✔
6138
        break;
120,109✔
6139
      }
6140
      case TSDB_DATA_TYPE_BIGINT: {
97,784✔
6141
        INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData);
97,784✔
6142
        break;
97,784✔
6143
      }
6144
      case TSDB_DATA_TYPE_FLOAT: {
69,449✔
6145
        INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData);
69,449✔
6146
        break;
69,449✔
6147
      }
6148
      case TSDB_DATA_TYPE_DOUBLE: {
89,955✔
6149
        INIT_INTP_POINT(st, row.ts, *(double*)row.pData);
89,955✔
6150
        break;
89,955✔
6151
      }
6152
      case TSDB_DATA_TYPE_UTINYINT: {
68,215✔
6153
        INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData);
68,215✔
6154
        break;
68,215✔
6155
      }
6156
      case TSDB_DATA_TYPE_USMALLINT: {
67,774✔
6157
        INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData);
67,774✔
6158
        break;
67,774✔
6159
      }
6160
      case TSDB_DATA_TYPE_UINT: {
71,053✔
6161
        INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData);
71,053✔
6162
        break;
71,053✔
6163
      }
6164
      case TSDB_DATA_TYPE_UBIGINT: {
59,251✔
6165
        INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData);
59,251✔
6166
        break;
59,251✔
6167
      }
UNCOV
6168
      default: {
×
UNCOV
6169
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
6170
      }
6171
    }
6172
    if (pInfo->p.key == st.key) {
813,715!
6173
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6174
    }
6175

6176
    pInfo->dOutput += twa_get_area(pInfo->p, st);
813,715✔
6177
    pInfo->p = st;
813,795✔
6178
  }
6179

6180
  // the last interpolated time window value
6181
  if (pCtx->end.key != INT64_MIN) {
46,719✔
6182
    pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end);
13,277✔
6183
    pInfo->p = pCtx->end;
13,277✔
6184
    pInfo->numOfElems += 1;
13,277✔
6185
  }
6186

6187
  pInfo->win.ekey = pInfo->p.key;
46,719✔
6188

6189
_twa_over:
46,719✔
6190
  SET_VAL(pResInfo, 1, 1);
46,719✔
6191
  return TSDB_CODE_SUCCESS;
46,719✔
6192
}
6193

6194
/*
6195
 * To copy the input to interResBuf to avoid the input buffer space be over writen
6196
 * by next input data. The TWA function only applies to each table, so no merge procedure
6197
 * is required, we simply copy to the resut ot interResBuffer.
6198
 */
6199
// void twa_function_copy(SQLFunctionCtx *pCtx) {
6200
//   SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx);
6201
//
6202
//   memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes);
6203
//   pResInfo->hasResult = ((STwaInfo *)pCtx->pInput)->hasResult;
6204
// }
6205

6206
int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
46,331✔
6207
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
46,331✔
6208

6209
  STwaInfo* pInfo = (STwaInfo*)GET_ROWCELL_INTERBUF(pResInfo);
46,331✔
6210
  if (pInfo->numOfElems == 0) {
46,331✔
6211
    pResInfo->numOfRes = 0;
12,322✔
6212
  } else {
6213
    if (pInfo->win.ekey == pInfo->win.skey) {
34,009✔
6214
      pInfo->dTwaRes = pInfo->p.val;
13,599✔
6215
    } else if (pInfo->win.ekey == INT64_MAX || pInfo->win.skey == INT64_MIN) {  // no data in timewindow
20,410!
6216
      pInfo->dTwaRes = 0;
1✔
6217
    } else {
6218
      pInfo->dTwaRes = pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey);
20,409✔
6219
    }
6220

6221
    pResInfo->numOfRes = 1;
34,009✔
6222
  }
6223

6224
  return functionFinalize(pCtx, pBlock);
46,331✔
6225
}
6226

6227
int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
6✔
6228
  if (pResultInfo->initialized) {
6!
6229
    return TSDB_CODE_SUCCESS;
×
6230
  }
6231
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
6!
6232
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6233
  }
6234

6235
  STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
6✔
6236
  pInfo->minRows = INT32_MAX;
6✔
6237
  return TSDB_CODE_SUCCESS;
6✔
6238
}
6239

6240
int32_t blockDistFunction(SqlFunctionCtx* pCtx) {
10✔
6241
  const int32_t BLOCK_DIST_RESULT_ROWS = 25;
10✔
6242

6243
  SInputColumnInfoData* pInput = &pCtx->input;
10✔
6244
  SColumnInfoData*      pInputCol = pInput->pData[0];
10✔
6245
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
10✔
6246
  STableBlockDistInfo*  pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
10✔
6247

6248
  STableBlockDistInfo p1 = {0};
10✔
6249
  if (tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
10!
6250
    qError("failed to deserialize block dist info");
×
6251
    return TSDB_CODE_FAILED;
×
6252
  }
6253

6254
  pDistInfo->numOfBlocks += p1.numOfBlocks;
10✔
6255
  pDistInfo->numOfTables += p1.numOfTables;
10✔
6256
  pDistInfo->numOfInmemRows += p1.numOfInmemRows;
10✔
6257
  pDistInfo->numOfSttRows += p1.numOfSttRows;
10✔
6258
  pDistInfo->totalSize += p1.totalSize;
10✔
6259
  pDistInfo->totalRows += p1.totalRows;
10✔
6260
  pDistInfo->numOfFiles += p1.numOfFiles;
10✔
6261

6262
  pDistInfo->defMinRows = p1.defMinRows;
10✔
6263
  pDistInfo->defMaxRows = p1.defMaxRows;
10✔
6264
  pDistInfo->rowSize = p1.rowSize;
10✔
6265

6266
  if (pDistInfo->minRows > p1.minRows) {
10✔
6267
    pDistInfo->minRows = p1.minRows;
1✔
6268
  }
6269
  if (pDistInfo->maxRows < p1.maxRows) {
10✔
6270
    pDistInfo->maxRows = p1.maxRows;
1✔
6271
  }
6272
  pDistInfo->numOfVgroups += (p1.numOfTables != 0 ? 1 : 0);
10✔
6273
  for (int32_t i = 0; i < tListLen(pDistInfo->blockRowsHisto); ++i) {
210✔
6274
    pDistInfo->blockRowsHisto[i] += p1.blockRowsHisto[i];
200✔
6275
  }
6276

6277
  pResInfo->numOfRes = BLOCK_DIST_RESULT_ROWS;  // default output rows
10✔
6278
  return TSDB_CODE_SUCCESS;
10✔
6279
}
6280

6281
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo) {
20✔
6282
  SEncoder encoder = {0};
20✔
6283
  int32_t  code = 0;
20✔
6284
  int32_t  lino;
6285
  int32_t  tlen;
6286
  tEncoderInit(&encoder, buf, bufLen);
20✔
6287

6288
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
20!
6289
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->rowSize));
40!
6290

6291
  TAOS_CHECK_EXIT(tEncodeU16(&encoder, pInfo->numOfFiles));
40!
6292
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfBlocks));
40!
6293
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfTables));
40!
6294

6295
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalSize));
40!
6296
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalRows));
40!
6297
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->maxRows));
40!
6298
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->minRows));
40!
6299
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMaxRows));
40!
6300
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMinRows));
40!
6301
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfInmemRows));
40!
6302
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfSttRows));
40!
6303
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfVgroups));
40!
6304

6305
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
404✔
6306
    TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->blockRowsHisto[i]));
768!
6307
  }
6308

6309
  tEndEncode(&encoder);
20✔
6310

6311
_exit:
20✔
6312
  if (code) {
20!
6313
    tlen = code;
×
6314
  } else {
6315
    tlen = encoder.pos;
20✔
6316
  }
6317
  tEncoderClear(&encoder);
20✔
6318
  return tlen;
20✔
6319
}
6320

6321
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo) {
10✔
6322
  SDecoder decoder = {0};
10✔
6323
  int32_t  code = 0;
10✔
6324
  int32_t  lino;
6325
  tDecoderInit(&decoder, buf, bufLen);
10✔
6326

6327
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
10!
6328
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->rowSize));
20!
6329

6330
  TAOS_CHECK_EXIT(tDecodeU16(&decoder, &pInfo->numOfFiles));
20!
6331
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfBlocks));
20!
6332
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfTables));
20!
6333

6334
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalSize));
20!
6335
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalRows));
20!
6336
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->maxRows));
20!
6337
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->minRows));
20!
6338
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMaxRows));
20!
6339
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMinRows));
20!
6340
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfInmemRows));
20!
6341
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfSttRows));
20!
6342
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfVgroups));
20!
6343

6344
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
210✔
6345
    TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->blockRowsHisto[i]));
400!
6346
  }
6347

6348
_exit:
10✔
6349
  tDecoderClear(&decoder);
10✔
6350
  return code;
10✔
6351
}
6352

6353
int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
6✔
6354
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
6✔
6355
  STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo);
6✔
6356

6357
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
6✔
6358
  if (NULL == pColInfo) {
6!
6359
    return TSDB_CODE_OUT_OF_RANGE;
×
6360
  }
6361

6362
  if (pData->totalRows == 0) {
6✔
6363
    pData->minRows = 0;
5✔
6364
  }
6365

6366
  int32_t row = 0;
6✔
6367
  char    st[256] = {0};
6✔
6368
  double  averageSize = 0;
6✔
6369
  if (pData->numOfBlocks != 0) {
6✔
6370
    averageSize = ((double)pData->totalSize) / pData->numOfBlocks;
1✔
6371
  }
6372
  uint64_t totalRawSize = pData->totalRows * pData->rowSize;
6✔
6373
  double   compRatio = 0;
6✔
6374
  if (totalRawSize != 0) {
6✔
6375
    compRatio = pData->totalSize * 100 / (double)totalRawSize;
1✔
6376
  }
6377

6378
  int32_t len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
12✔
6379
                        "Total_Blocks=[%d] Total_Size=[%.2f KiB] Average_size=[%.2f KiB] Compression_Ratio=[%.2f %c]",
6380
                        pData->numOfBlocks, pData->totalSize / 1024.0, averageSize / 1024.0, compRatio, '%');
6✔
6381

6382
  varDataSetLen(st, len);
6✔
6383
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
6✔
6384
  if (TSDB_CODE_SUCCESS != code) {
6!
6385
    return code;
×
6386
  }
6387

6388
  int64_t avgRows = 0;
6✔
6389
  if (pData->numOfBlocks > 0) {
6✔
6390
    avgRows = pData->totalRows / pData->numOfBlocks;
1✔
6391
  }
6392

6393
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Block_Rows=[%" PRId64 "] MinRows=[%d] MaxRows=[%d] AvgRows=[%" PRId64 "]",
6✔
6394
                pData->totalRows, pData->minRows, pData->maxRows, avgRows);
6395
  varDataSetLen(st, len);
6✔
6396
  code = colDataSetVal(pColInfo, row++, st, false);
6✔
6397
  if (TSDB_CODE_SUCCESS != code) {
6!
6398
    return code;
×
6399
  }
6400

6401
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Inmem_Rows=[%d] Stt_Rows=[%d] ", pData->numOfInmemRows, pData->numOfSttRows);
6✔
6402
  varDataSetLen(st, len);
6✔
6403
  code = colDataSetVal(pColInfo, row++, st, false);
6✔
6404
  if (TSDB_CODE_SUCCESS != code) {
6!
6405
    return code;
×
6406
  }
6407

6408
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables,
12✔
6409
                pData->numOfFiles, pData->numOfVgroups);
6✔
6410

6411
  varDataSetLen(st, len);
6✔
6412
  code = colDataSetVal(pColInfo, row++, st, false);
6✔
6413
  if (TSDB_CODE_SUCCESS != code) {
6!
6414
    return code;
×
6415
  }
6416

6417
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
6✔
6418
                "--------------------------------------------------------------------------------");
6419
  varDataSetLen(st, len);
6✔
6420
  code = colDataSetVal(pColInfo, row++, st, false);
6✔
6421
  if (TSDB_CODE_SUCCESS != code) {
6!
6422
    return code;
×
6423
  }
6424

6425
  int32_t maxVal = 0;
6✔
6426
  int32_t minVal = INT32_MAX;
6✔
6427
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
126✔
6428
    if (maxVal < pData->blockRowsHisto[i]) {
120✔
6429
      maxVal = pData->blockRowsHisto[i];
1✔
6430
    }
6431

6432
    if (minVal > pData->blockRowsHisto[i]) {
120✔
6433
      minVal = pData->blockRowsHisto[i];
7✔
6434
    }
6435
  }
6436

6437
  // maximum number of step is 80
6438
  double factor = pData->numOfBlocks / 80.0;
6✔
6439

6440
  int32_t numOfBuckets = sizeof(pData->blockRowsHisto) / sizeof(pData->blockRowsHisto[0]);
6✔
6441
  int32_t bucketRange = ceil(((double) (pData->defMaxRows - pData->defMinRows)) / numOfBuckets);
6✔
6442

6443
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
126✔
6444
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "%04d |", pData->defMinRows + bucketRange * (i + 1));
120✔
6445

6446
    int32_t num = 0;
120✔
6447
    if (pData->blockRowsHisto[i] > 0) {
120✔
6448
      num = (pData->blockRowsHisto[i]) / factor;
1✔
6449
    }
6450

6451
    for (int32_t j = 0; j < num; ++j) {
200✔
6452
      int32_t x = tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "%c", '|');
80✔
6453
      len += x;
80✔
6454
    }
6455

6456
    if (pData->blockRowsHisto[i] > 0) {
120✔
6457
      double v = pData->blockRowsHisto[i] * 100.0 / pData->numOfBlocks;
1✔
6458
      len += tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "  %d (%.2f%c)", pData->blockRowsHisto[i], v, '%');
1✔
6459
    }
6460

6461
    varDataSetLen(st, len);
120✔
6462
    code = colDataSetVal(pColInfo, row++, st, false);
120✔
6463
    if (TSDB_CODE_SUCCESS != code) {
120!
6464
      return code;
×
6465
    }
6466
  }
6467

6468
  return TSDB_CODE_SUCCESS;
6✔
6469
}
6470

6471
bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
1,423✔
6472
  pEnv->calcMemSize = sizeof(SDerivInfo);
1,423✔
6473
  return true;
1,423✔
6474
}
6475

6476
int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
4,284✔
6477
  if (pResInfo->initialized) {
4,284✔
6478
    return TSDB_CODE_SUCCESS;
2,717✔
6479
  }
6480
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
1,567!
6481
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6482
  }
6483

6484
  SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
1,567✔
6485

6486
  pDerivInfo->ignoreNegative = pCtx->param[2].param.i;
1,567✔
6487
  pDerivInfo->prevTs = -1;
1,567✔
6488
  pDerivInfo->tsWindow = pCtx->param[1].param.i;
1,567✔
6489
  pDerivInfo->valueSet = false;
1,567✔
6490
  return TSDB_CODE_SUCCESS;
1,567✔
6491
}
6492

6493
int32_t derivativeFunction(SqlFunctionCtx* pCtx) {
2,861✔
6494
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,861✔
6495
  SDerivInfo*          pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
2,861✔
6496

6497
  SInputColumnInfoData* pInput = &pCtx->input;
2,861✔
6498
  SColumnInfoData*      pInputCol = pInput->pData[0];
2,861✔
6499

6500
  int32_t          numOfElems = 0;
2,861✔
6501
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
2,861✔
6502
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
2,861✔
6503
  int32_t          code = TSDB_CODE_SUCCESS;
2,861✔
6504

6505
  funcInputUpdate(pCtx);
2,861✔
6506

6507
  double v = 0;
2,861✔
6508
  if (pCtx->order == TSDB_ORDER_ASC) {
2,861✔
6509
    SFuncInputRow row = {0};
2,826✔
6510
    bool result = false;
2,826✔
6511
    while (1) {
97,663✔
6512
      code = funcInputGetNextRow(pCtx, &row, &result);
100,489✔
6513
      if (TSDB_CODE_SUCCESS != code) {
100,489!
6514
        return code;
×
6515
      }
6516
      if (!result) {
100,489✔
6517
        break;
2,826✔
6518
      }
6519
      if (row.isDataNull) {
97,663✔
6520
        continue;
41,413✔
6521
      }
6522

6523
      char* d = row.pData;
56,250✔
6524
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
56,250!
6525

6526
      int32_t pos = pCtx->offset + numOfElems;
56,250✔
6527
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
56,250✔
6528
        pDerivInfo->valueSet = true;
1,106✔
6529
      } else {
6530
        if (row.ts == pDerivInfo->prevTs) {
55,144!
6531
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6532
        }
6533
        double r = ((v - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (row.ts - pDerivInfo->prevTs);
55,144✔
6534
        if (pDerivInfo->ignoreNegative && r < 0) {
55,144✔
6535
        } else {
6536
          if (isinf(r) || isnan(r)) {
48,254!
6537
            colDataSetNULL(pOutput, pos);
×
6538
          } else {
6539
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
48,254✔
6540
            if (code != TSDB_CODE_SUCCESS) {
48,254!
6541
              return code;
×
6542
            }
6543
          }
6544

6545
          if (pTsOutput != NULL) {
48,254!
6546
            colDataSetInt64(pTsOutput, pos, &row.ts);
×
6547
          }
6548

6549
          // handle selectivity
6550
          if (pCtx->subsidiaries.num > 0) {
48,254✔
6551
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
15,152✔
6552
            if (code != TSDB_CODE_SUCCESS) {
15,152!
6553
              return code;
×
6554
            }
6555
          }
6556

6557
          numOfElems++;
48,254✔
6558
        }
6559
      }
6560

6561
      pDerivInfo->prevValue = v;
56,250✔
6562
      pDerivInfo->prevTs = row.ts;
56,250✔
6563
    }
6564
  } else {
6565
    SFuncInputRow row = {0};
35✔
6566
    bool          result = false;
35✔
6567
    while (1) {
171✔
6568
      code = funcInputGetNextRow(pCtx, &row, &result);
206✔
6569
      if (TSDB_CODE_SUCCESS != code) {
206!
6570
        return code;
×
6571
      }
6572
      if (!result) {
206✔
6573
        break;
35✔
6574
      }
6575
      if (row.isDataNull) {
171✔
6576
        continue;
79✔
6577
      }
6578

6579
      char* d = row.pData;
92✔
6580
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
92!
6581

6582
      int32_t pos = pCtx->offset + numOfElems;
92✔
6583
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
92✔
6584
        pDerivInfo->valueSet = true;
19✔
6585
      } else {
6586
        if (row.ts == pDerivInfo->prevTs) {
73!
6587
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6588
        }
6589
        double r = ((pDerivInfo->prevValue - v) * pDerivInfo->tsWindow) / (pDerivInfo->prevTs - row.ts);
73✔
6590
        if (pDerivInfo->ignoreNegative && r < 0) {
73✔
6591
        } else {
6592
          if (isinf(r) || isnan(r)) {
40!
6593
            colDataSetNULL(pOutput, pos);
×
6594
          } else {
6595
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
40✔
6596
            if (code != TSDB_CODE_SUCCESS) {
40!
6597
              return code;
×
6598
            }
6599
          }
6600

6601
          if (pTsOutput != NULL) {
40!
6602
            colDataSetInt64(pTsOutput, pos, &pDerivInfo->prevTs);
×
6603
          }
6604

6605
          // handle selectivity
6606
          if (pCtx->subsidiaries.num > 0) {
40!
6607
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
×
6608
            if (code != TSDB_CODE_SUCCESS) {
×
6609
              return code;
×
6610
            }
6611
          }
6612
          numOfElems++;
40✔
6613
        }
6614
      }
6615

6616
      pDerivInfo->prevValue = v;
92✔
6617
      pDerivInfo->prevTs = row.ts;
92✔
6618
    }
6619
  }
6620

6621
  pResInfo->numOfRes = numOfElems;
2,861✔
6622

6623
  return TSDB_CODE_SUCCESS;
2,861✔
6624
}
6625

6626
int32_t getIrateInfoSize(int32_t pkBytes) { return (int32_t)sizeof(SRateInfo) + 2 * pkBytes; }
2,865✔
6627

6628
bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
2,467✔
6629
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
2,467✔
6630
  pEnv->calcMemSize = getIrateInfoSize(pkBytes);
2,467✔
6631
  return true;
2,474✔
6632
}
6633

6634
int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
31,444✔
6635
  if (pResInfo->initialized) {
31,444!
6636
    return TSDB_CODE_SUCCESS;
×
6637
  }
6638
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
31,444!
6639
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6640
  }
6641

6642
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
31,445✔
6643

6644
  pInfo->firstKey = INT64_MIN;
31,445✔
6645
  pInfo->lastKey = INT64_MIN;
31,445✔
6646
  pInfo->firstValue = (double)INT64_MIN;
31,445✔
6647
  pInfo->lastValue = (double)INT64_MIN;
31,445✔
6648

6649
  pInfo->hasResult = 0;
31,445✔
6650
  return TSDB_CODE_SUCCESS;
31,445✔
6651
}
6652

6653
static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) {
268,476✔
6654
  if (isFirst) {
268,476✔
6655
    pRateInfo->firstValue = v;
126,121✔
6656
    pRateInfo->firstKey = ts;
126,121✔
6657
    if (pRateInfo->firstPk) {
126,121✔
6658
      int32_t pkBytes = IS_VAR_DATA_TYPE(pRateInfo->pkType) ? varDataTLen(pk) : pRateInfo->pkBytes;
35!
6659
      (void)memcpy(pRateInfo->firstPk, pk, pkBytes);
35✔
6660
    }
6661
  } else {
6662
    pRateInfo->lastValue = v;
142,355✔
6663
    pRateInfo->lastKey = ts;
142,355✔
6664
    if (pRateInfo->lastPk) {
142,355✔
6665
      int32_t pkBytes = IS_VAR_DATA_TYPE(pRateInfo->pkType) ? varDataTLen(pk) : pRateInfo->pkBytes;
52!
6666
      (void)memcpy(pRateInfo->lastPk, pk, pkBytes);
52✔
6667
    }
6668
  }
6669
}
268,476✔
6670

6671
static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool isMerge) {
32,840✔
6672
  if (pCtx->hasPrimaryKey) {
32,840✔
6673
    if (!isMerge) {
19✔
6674
      pRateInfo->pkType = pCtx->input.pPrimaryKey->info.type;
17✔
6675
      pRateInfo->pkBytes = pCtx->input.pPrimaryKey->info.bytes;
17✔
6676
      pRateInfo->firstPk = pRateInfo->pkData;
17✔
6677
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
17✔
6678
    } else {
6679
      pRateInfo->firstPk = pRateInfo->pkData;
2✔
6680
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
2✔
6681
    }
6682
  } else {
6683
    pRateInfo->firstPk = NULL;
32,821✔
6684
    pRateInfo->lastPk = NULL;
32,821✔
6685
  }  
6686
}
32,840✔
6687

6688
int32_t irateFunction(SqlFunctionCtx* pCtx) {
32,220✔
6689
  int32_t              code = TSDB_CODE_SUCCESS;
32,220✔
6690
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
32,220✔
6691
  SRateInfo*           pRateInfo = GET_ROWCELL_INTERBUF(pResInfo);
32,220✔
6692

6693
  SInputColumnInfoData* pInput = &pCtx->input;
32,220✔
6694
  SColumnInfoData*      pInputCol = pInput->pData[0];
32,220✔
6695

6696
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
32,220✔
6697

6698
  funcInputUpdate(pCtx);
32,220✔
6699
  
6700
  initializeRateInfo(pCtx, pRateInfo, false);
32,224✔
6701

6702
  int32_t numOfElems = 0;
32,225✔
6703
  int32_t type = pInputCol->info.type;
32,225✔
6704
  SFuncInputRow row = {0};
32,225✔
6705
  bool          result = false;
32,225✔
6706
  while (1)  {
269,959✔
6707
    code = funcInputGetNextRow(pCtx, &row, &result);
302,184✔
6708
    if (TSDB_CODE_SUCCESS != code) {
302,228!
6709
      return code;
×
6710
    }
6711
    if (!result) {
302,228✔
6712
      break;
32,225✔
6713
    }
6714
    if (row.isDataNull) {
270,003✔
6715
      continue;
123,464✔
6716
    }
6717

6718
    char*  data = row.pData;
146,539✔
6719
    double v = 0;
146,539✔
6720
    GET_TYPED_DATA(v, double, type, data);
146,539!
6721

6722
    if (INT64_MIN == pRateInfo->lastKey) {
146,539✔
6723
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
16,632✔
6724
      pRateInfo->hasResult = 1;
16,632✔
6725
      continue;
16,632✔
6726
    }
6727

6728
    if (row.ts > pRateInfo->lastKey) {
129,907✔
6729
      if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
125,737✔
6730
        doSaveRateInfo(pRateInfo, true, pRateInfo->lastKey, pRateInfo->lastPk, pRateInfo->lastValue);
125,735✔
6731
      }
6732
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
125,734✔
6733
      continue;
125,662✔
6734
    } else if (row.ts == pRateInfo->lastKey) {
4,170!
6735
        return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6736
    }
6737
    
6738

6739
    if ((INT64_MIN == pRateInfo->firstKey) || row.ts > pRateInfo->firstKey) {
4,170!
6740
      doSaveRateInfo(pRateInfo, true, row.ts, row.pPk, v);    
26✔
6741
    } else if (row.ts == pRateInfo->firstKey) {
4,144!
6742
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6743
    }
6744
  }
6745

6746
  numOfElems++;
32,225✔
6747
  
6748
  SET_VAL(pResInfo, numOfElems, 1);
32,225!
6749
  return TSDB_CODE_SUCCESS;
32,225✔
6750
}
6751

6752
static double doCalcRate(const SRateInfo* pRateInfo, double tickPerSec) {
31,141✔
6753
  if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) ||
31,141✔
6754
      (pRateInfo->firstKey >= pRateInfo->lastKey)) {
1,519!
6755
    return 0.0;
29,622✔
6756
  }
6757

6758
  double diff = 0;
1,519✔
6759
  // If the previous value of the last is greater than the last value, only keep the last point instead of the delta
6760
  // value between two values.
6761
  diff = pRateInfo->lastValue;
1,519✔
6762
  if (diff >= pRateInfo->firstValue) {
1,519✔
6763
    diff -= pRateInfo->firstValue;
770✔
6764
  }
6765

6766
  int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey;
1,519✔
6767
  if (duration == 0) {
1,519!
6768
    return 0;
×
6769
  }
6770

6771
  return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0;
1,519!
6772
}
6773

6774
static void irateTransferInfoImpl(TSKEY inputKey, SRateInfo* pInput, SRateInfo* pOutput, bool isFirstKey) {
290✔
6775
  if (inputKey > pOutput->lastKey) {
290✔
6776
    doSaveRateInfo(pOutput, true, pOutput->lastKey, pOutput->lastPk, pOutput->lastValue);
176✔
6777
    if (isFirstKey) {
176✔
6778
      doSaveRateInfo(pOutput, false, pInput->firstKey, pInput->firstPk, pInput->firstValue);
80✔
6779
    } else {
6780
      doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
96✔
6781
    }
6782
  } else if ((inputKey < pOutput->lastKey) && (inputKey > pOutput->firstKey)) {
114!
6783
    if (isFirstKey) {
28✔
6784
      doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
14✔
6785
    } else {
6786
      doSaveRateInfo(pOutput, true, pInput->lastKey, pInput->lastPk, pInput->lastValue);
14✔
6787
    }
6788
  } else {
6789
    // inputKey < pOutput->firstKey
6790
  }
6791
}
290✔
6792

6793
static void irateCopyInfo(SRateInfo* pInput, SRateInfo* pOutput) {
151✔
6794
  doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
151✔
6795
  doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
151✔
6796
}
151✔
6797

6798
static int32_t irateTransferInfo(SRateInfo* pInput, SRateInfo* pOutput) {
296✔
6799
  if ((pInput->firstKey != INT64_MIN && (pInput->firstKey == pOutput->firstKey || pInput->firstKey == pOutput->lastKey)) ||
296!
6800
      (pInput->lastKey != INT64_MIN && (pInput->lastKey  == pOutput->firstKey || pInput->lastKey  == pOutput->lastKey))) {
296!
6801
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6802
  }
6803

6804
  if (pOutput->hasResult == 0) {
296✔
6805
    irateCopyInfo(pInput, pOutput);
151✔
6806
    pOutput->hasResult = pInput->hasResult;
151✔
6807
    return TSDB_CODE_SUCCESS;
151✔
6808
  }
6809

6810
  if (pInput->firstKey != INT64_MIN) {
145!
6811
    irateTransferInfoImpl(pInput->firstKey, pInput, pOutput, true);
145✔
6812
  }
6813

6814
  if (pInput->lastKey != INT64_MIN) {
145!
6815
    irateTransferInfoImpl(pInput->lastKey, pInput, pOutput, false);
145✔
6816
  }
6817

6818
  pOutput->hasResult = pInput->hasResult;
145✔
6819
  return TSDB_CODE_SUCCESS;
145✔
6820
}
6821

6822
int32_t irateFunctionMerge(SqlFunctionCtx* pCtx) {
308✔
6823
  SInputColumnInfoData* pInput = &pCtx->input;
308✔
6824
  SColumnInfoData*      pCol = pInput->pData[0];
308✔
6825
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
308!
6826
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
6827
  }
6828

6829
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
308✔
6830
  initializeRateInfo(pCtx, pInfo, true);
308✔
6831

6832
  int32_t start = pInput->startRowIndex;
308✔
6833
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
616✔
6834
    char*        data = colDataGetData(pCol, i);
308!
6835
    SRateInfo*   pInputInfo = (SRateInfo*)varDataVal(data);
308✔
6836
    initializeRateInfo(pCtx, pInfo, true);
308✔
6837
    if (pInputInfo->hasResult) {
308✔
6838
      int32_t code = irateTransferInfo(pInputInfo, pInfo);
296✔
6839
      if (code != TSDB_CODE_SUCCESS) {
296!
6840
        return code;
×
6841
      }
6842
    }
6843
  }
6844

6845
  if (pInfo->hasResult) {
308✔
6846
    GET_RES_INFO(pCtx)->numOfRes = 1;
296✔
6847
  }
6848

6849
  return TSDB_CODE_SUCCESS;
308✔
6850
}
6851

6852
int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
307✔
6853
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
307✔
6854
  SRateInfo*           pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
307✔
6855
  int32_t              resultBytes = getIrateInfoSize(pInfo->pkBytes);
307✔
6856
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
307✔
6857

6858
  if (NULL == res) {
308!
6859
    return terrno;
×
6860
  }
6861
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
308✔
6862
  varDataSetLen(res, resultBytes);
308✔
6863

6864
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
308✔
6865
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
308✔
6866
  if (NULL == pCol) {
307!
6867
      taosMemoryFree(res);
×
6868
      return TSDB_CODE_OUT_OF_RANGE;
×
6869
  }
6870

6871
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
307✔
6872

6873
  taosMemoryFree(res);
307✔
6874
  return code;
307✔
6875
}
6876

6877
int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
31,141✔
6878
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
31,141✔
6879
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
31,141✔
6880
  if (NULL == pCol) {
31,141!
6881
    return TSDB_CODE_OUT_OF_RANGE;
×
6882
  }
6883

6884
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
31,141✔
6885
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
31,141✔
6886

6887
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
31,141✔
6888
  double     result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i));
31,141!
6889
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes);
31,141✔
6890

6891
  return code;
31,141✔
6892
}
6893

6894
int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) {
109,102,461✔
6895
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
109,102,461✔
6896
  SGroupKeyInfo*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
109,102,461✔
6897

6898
  SInputColumnInfoData* pInput = &pCtx->input;
109,102,461✔
6899
  SColumnInfoData*      pInputCol = pInput->pData[0];
109,102,461✔
6900

6901
  int32_t startIndex = pInput->startRowIndex;
109,102,461✔
6902

6903
  // escape rest of data blocks to avoid first entry to be overwritten.
6904
  if (pInfo->hasResult) {
109,102,461✔
6905
    goto _group_value_over;
11,651,663✔
6906
  }
6907

6908
  if (pInputCol->pData == NULL || colDataIsNull_s(pInputCol, startIndex)) {
194,475,695✔
6909
    pInfo->isNull = true;
2,562,149✔
6910
    pInfo->hasResult = true;
2,562,149✔
6911
    goto _group_value_over;
2,562,149✔
6912
  }
6913

6914
  char* data = colDataGetData(pInputCol, startIndex);
94,888,649!
6915
  if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
94,888,649!
6916
    (void)memcpy(pInfo->data, data,
73,051,539✔
6917
                 (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data));
73,051,539✔
6918
  } else {
6919
    (void)memcpy(pInfo->data, data, pInputCol->info.bytes);
21,837,110✔
6920
  }
6921
  pInfo->hasResult = true;
94,888,649✔
6922

6923
_group_value_over:
109,102,461✔
6924

6925
  SET_VAL(pResInfo, 1, 1);
109,102,461✔
6926
  return TSDB_CODE_SUCCESS;
109,102,461✔
6927
}
6928

6929
int32_t groupKeyFunction(SqlFunctionCtx* pCtx) {
108,034,152✔
6930
  return groupConstValueFunction(pCtx);
108,034,152✔
6931
}
6932

6933
int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
95,108,262✔
6934
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
95,108,262✔
6935
  int32_t          code = TSDB_CODE_SUCCESS;
95,108,262✔
6936
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
95,108,262✔
6937
  if (NULL == pCol) {
95,179,497!
6938
    return TSDB_CODE_OUT_OF_RANGE;
×
6939
  }
6940

6941
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
95,179,497✔
6942

6943
  SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
95,179,497✔
6944

6945
  if (pInfo->hasResult) {
95,179,497!
6946
    int32_t currentRow = pBlock->info.rows;
95,206,290✔
6947
    for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) {
190,676,923✔
6948
      code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false);
95,223,991✔
6949
      if (TSDB_CODE_SUCCESS != code) {
95,470,633!
6950
        return code;
×
6951
      }
6952
    }
6953
  } else {
6954
    pResInfo->numOfRes = 0;
×
6955
  }
6956

6957
  return code;
95,426,139✔
6958
}
6959

6960
int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock){
94,847,187✔
6961
  return groupConstValueFinalize(pCtx, pBlock);
94,847,187✔
6962
}
6963

6964
int32_t groupKeyCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
6965
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
6966
  SGroupKeyInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
6967

6968
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
6969
  SGroupKeyInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
6970

6971
  // escape rest of data blocks to avoid first entry to be overwritten.
6972
  if (pDBuf->hasResult) {
×
6973
    goto _group_key_over;
×
6974
  }
6975

6976
  if (pSBuf->isNull) {
×
6977
    pDBuf->isNull = true;
×
6978
    pDBuf->hasResult = true;
×
6979
    goto _group_key_over;
×
6980
  }
6981

6982
  if (IS_VAR_DATA_TYPE(pSourceCtx->resDataInfo.type)) {
×
6983
    (void)memcpy(pDBuf->data, pSBuf->data,
×
6984
           (pSourceCtx->resDataInfo.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(pSBuf->data) : varDataTLen(pSBuf->data));
×
6985
  } else {
6986
    (void)memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes);
×
6987
  }
6988

6989
  pDBuf->hasResult = true;
×
6990

6991
_group_key_over:
×
6992

6993
  SET_VAL(pDResInfo, 1, 1);
×
6994
  return TSDB_CODE_SUCCESS;
×
6995
}
6996

6997
int32_t cachedLastRowFunction(SqlFunctionCtx* pCtx) {
5,641✔
6998
  int32_t numOfElems = 0;
5,641✔
6999

7000
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5,641✔
7001
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,641✔
7002

7003
  SInputColumnInfoData* pInput = &pCtx->input;
5,641✔
7004
  SColumnInfoData*      pInputCol = pInput->pData[0];
5,641✔
7005

7006
  int32_t bytes = pInputCol->info.bytes;
5,641✔
7007
  pInfo->bytes = bytes;
5,641✔
7008

7009
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
5,641✔
7010
  pInfo->pkType = -1;
5,641✔
7011
  __compar_fn_t  pkCompareFn = NULL;
5,641✔
7012
  if (pCtx->hasPrimaryKey) {
5,641✔
7013
    pInfo->pkType = pkCol->info.type;
1,751✔
7014
    pInfo->pkBytes = pkCol->info.bytes;
1,751✔
7015
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
1,751✔
7016
  }
7017

7018
  // TODO it traverse the different way.
7019
  // last_row function does not ignore the null value
7020
  for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
11,292✔
7021
    numOfElems++;
5,651✔
7022

7023
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
5,651✔
7024
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
5,651!
7025

7026
    TSKEY cts = getRowPTs(pInput->pPTS, i);
5,651!
7027
    if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
5,651✔
7028
      int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
4,308✔
7029
      if (code != TSDB_CODE_SUCCESS) {
4,308!
7030
        return code;
×
7031
      }
7032
      pResInfo->numOfRes = 1;
4,308✔
7033
    }
7034
  }
7035

7036
  SET_VAL(pResInfo, numOfElems, 1);
5,641!
7037
  return TSDB_CODE_SUCCESS;
5,641✔
7038
}
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