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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

71.98
/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) { return (ignoreOption & 0x1) == 0x1; }
1,419,078,287✔
32
bool ignoreNull(int8_t ignoreOption) { return (ignoreOption & 0x2) == 0x2; }
1,766,226✔
33

34
typedef enum {
35
  APERCT_ALGO_UNKNOWN = 0,
36
  APERCT_ALGO_DEFAULT,
37
  APERCT_ALGO_TDIGEST,
38
} EAPerctAlgoType;
39

40
typedef enum { UNKNOWN_BIN = 0, USER_INPUT_BIN, LINEAR_BIN, LOG_BIN } EHistoBinType;
41

42
typedef enum {
43
  STATE_OPER_INVALID = 0,
44
  STATE_OPER_LT,
45
  STATE_OPER_GT,
46
  STATE_OPER_LE,
47
  STATE_OPER_GE,
48
  STATE_OPER_NE,
49
  STATE_OPER_EQ,
50
} EStateOperType;
51

52
#define SET_VAL(_info, numOfElem, res) \
53
  do {                                 \
54
    if ((numOfElem) <= 0) {            \
55
      break;                           \
56
    }                                  \
57
    (_info)->numOfRes = (res);         \
58
  } while (0)
59

60
#define GET_TS_LIST(x)    ((TSKEY*)((x)->ptsList))
61
#define GET_TS_DATA(x, y) (GET_TS_LIST(x)[(y)])
62

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

75
#define UPDATE_DATA(ctx, left, right, num, sign, _ts) \
76
  do {                                                \
77
    if (((left) < (right)) ^ (sign)) {                \
78
      (left) = (right);                               \
79
      DO_UPDATE_SUBSID_RES(ctx, _ts);                 \
80
      (num) += 1;                                     \
81
    }                                                 \
82
  } while (0)
83

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

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

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

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

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

148
#define LEASTSQR_CAL(p, x, y, index, step) \
149
  do {                                     \
150
    (p)[0][0] += (double)(x) * (x);        \
151
    (p)[0][1] += (double)(x);              \
152
    (p)[0][2] += (double)(x) * (y)[index]; \
153
    (p)[1][2] += (y)[index];               \
154
    (x) += step;                           \
155
  } while (0)
156

157
#define STATE_COMP(_op, _lval, _param) STATE_COMP_IMPL(_op, _lval, GET_STATE_VAL(_param))
158

159
#define GET_STATE_VAL(param) ((param.nType == TSDB_DATA_TYPE_BIGINT) ? (param.i) : (param.d))
160

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

187
#define INIT_INTP_POINT(_p, _k, _v) \
188
  do {                              \
189
    (_p).key = (_k);                \
190
    (_p).val = (_v);                \
191
  } while (0)
192

193
void funcInputUpdate(SqlFunctionCtx* pCtx) {
23,944,618✔
194
  SFuncInputRowIter* pIter = &pCtx->rowIter;
23,944,618✔
195

196
  if (!pCtx->bInputFinished) {
23,944,618!
197
    pIter->pInput = &pCtx->input;
23,944,664✔
198
    pIter->tsList = (TSKEY*)pIter->pInput->pPTS->pData;
23,944,664✔
199
    pIter->pDataCol = pIter->pInput->pData[0];
23,944,664✔
200
    pIter->pPkCol = pIter->pInput->pPrimaryKey;
23,944,664✔
201
    pIter->rowIndex = pIter->pInput->startRowIndex;
23,944,664✔
202
    pIter->inputEndIndex = pIter->rowIndex + pIter->pInput->numOfRows - 1;
23,944,664✔
203
    pIter->pSrcBlock = pCtx->pSrcBlock;
23,944,664✔
204
    if (!pIter->hasGroupId || pIter->groupId != pIter->pSrcBlock->info.id.groupId) {
23,944,664✔
205
      pIter->hasGroupId = true;
226,846✔
206
      pIter->groupId = pIter->pSrcBlock->info.id.groupId;
226,846✔
207
      pIter->hasPrev = false;
226,846✔
208
    }
209
  } else {
210
    pIter->finalRow = true;
×
211
  }
212
}
23,944,618✔
213

214
int32_t funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow, bool* res) {
×
215
  if (pIter->finalRow) {
×
216
    if (pIter->hasPrev) {
×
217
      pRow->ts = pIter->prevBlockTsEnd;
×
218
      pRow->isDataNull = pIter->prevIsDataNull;
×
219
      pRow->pData = pIter->pPrevData;
×
220
      pRow->block = pIter->pPrevRowBlock;
×
221
      pRow->rowIndex = 0;
×
222

223
      pIter->hasPrev = false;
×
224
      *res = true;
×
225
      return TSDB_CODE_SUCCESS;
×
226
    } else {
227
      *res = false;
×
228
      return TSDB_CODE_SUCCESS;
×
229
    }
230
  }
231
  if (pIter->hasPrev) {
×
232
    if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) {
×
233
      blockDataDestroy(pIter->pPrevRowBlock);
×
234
      int32_t code = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1, &pIter->pPrevRowBlock);
×
235
      if (code) {
×
236
        return code;
×
237
      }
238

239
      pIter->prevIsDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, pIter->inputEndIndex);
×
240

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

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

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

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

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

328
static void forwardToNextDiffTsRow(SFuncInputRowIter* pIter, int32_t rowIndex) {
160✔
329
  int32_t idx = rowIndex + 1;
160✔
330
  while (idx <= pIter->inputEndIndex && pIter->tsList[idx] == pIter->tsList[rowIndex]) {
362!
331
    ++idx;
202✔
332
  }
333
  pIter->rowIndex = idx;
160✔
334
}
160✔
335

336
static void setInputRowInfo(SFuncInputRow* pRow, SFuncInputRowIter* pIter, int32_t rowIndex, bool setPk) {
1,481,366,286✔
337
  pRow->ts = pIter->tsList[rowIndex];
1,481,366,286✔
338
  pRow->ts = pIter->tsList[rowIndex];
1,481,366,286✔
339
  pRow->isDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, rowIndex);
1,481,366,286✔
340
  pRow->pData = colDataGetData(pIter->pDataCol, rowIndex);
1,481,366,286!
341
  pRow->pPk = setPk ? colDataGetData(pIter->pPkCol, rowIndex) : NULL;
1,481,366,286!
342
  pRow->block = pIter->pSrcBlock;
1,481,366,286✔
343
  pRow->rowIndex = rowIndex;
1,481,366,286✔
344
}
1,481,366,286✔
345

346
bool funcInputGetNextRowAscPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) {
330✔
347
  if (pIter->hasPrev) {
330!
348
    if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) {
×
349
      pIter->hasPrev = true;
×
350
      return false;
×
351
    } else {
352
      int32_t idx = pIter->rowIndex;
×
353
      while (pIter->tsList[idx] == pIter->prevBlockTsEnd) {
×
354
        ++idx;
×
355
      }
356

357
      pIter->hasPrev = false;
×
358
      setInputRowInfo(pRow, pIter, idx, true);
×
359
      forwardToNextDiffTsRow(pIter, idx);
×
360
      return true;
×
361
    }
362
  } else {
363
    if (pIter->rowIndex <= pIter->inputEndIndex) {
330✔
364
      setInputRowInfo(pRow, pIter, pIter->rowIndex, true);
248✔
365

366
      TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex];
248✔
367
      if (pIter->tsList[pIter->rowIndex] != tsEnd) {
248✔
368
        forwardToNextDiffTsRow(pIter, pIter->rowIndex);
160✔
369
      } else {
370
        pIter->rowIndex = pIter->inputEndIndex + 1;
88✔
371
      }
372
      return true;
248✔
373
    } else {
374
      TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex];
82✔
375
      pIter->hasPrev = true;
82✔
376
      pIter->prevBlockTsEnd = tsEnd;
82✔
377
      return false;
82✔
378
    }
379
  }
380
}
381

382
bool funcInputGetNextRowNoPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) {
1,505,179,022✔
383
  if (pIter->rowIndex <= pIter->inputEndIndex) {
1,505,179,022✔
384
    setInputRowInfo(pRow, pIter, pIter->rowIndex, false);
1,481,428,536✔
385
    ++pIter->rowIndex;
1,481,243,839✔
386
    return true;
1,481,243,839✔
387
  } else {
388
    return false;
23,750,486✔
389
  }
390
}
391

392
int32_t funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, bool* res) {
1,505,225,096✔
393
  SFuncInputRowIter* pIter = &pCtx->rowIter;
1,505,225,096✔
394
  if (pCtx->hasPrimaryKey) {
1,505,225,096✔
395
    if (pCtx->order == TSDB_ORDER_ASC) {
330!
396
      *res = funcInputGetNextRowAscPk(pIter, pRow);
330✔
397
      return TSDB_CODE_SUCCESS;
330✔
398
    } else {
399
      return funcInputGetNextRowDescPk(pIter, pRow, res);
×
400
    }
401
  } else {
402
    *res = funcInputGetNextRowNoPk(pIter, pRow);
1,505,224,766✔
403
    return TSDB_CODE_SUCCESS;
1,504,985,454✔
404
  }
405
  return TSDB_CODE_SUCCESS;
406
}
407

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

415
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
58,904,695✔
416
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
29,452,382✔
417

418
    // get data from source col
419
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
29,452,382✔
420
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
29,452,382✔
421

422
    SColumnInfoData* pSrcCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
29,452,382✔
423
    if (NULL == pSrcCol) {
29,452,330!
424
      return TSDB_CODE_OUT_OF_RANGE;
×
425
    }
426

427
    char* pData = colDataGetData(pSrcCol, rowIndex);
29,452,330!
428

429
    // append to dest col
430
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
29,452,330✔
431

432
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
29,452,330✔
433
    if (NULL == pDstCol) {
29,452,269!
434
      return TSDB_CODE_OUT_OF_RANGE;
×
435
    }
436
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
58,904,538✔
437
      colDataSetNULL(pDstCol, pos);
20!
438
    } else {
439
      int32_t code = colDataSetVal(pDstCol, pos, pData, false);
29,452,249✔
440
      if (TSDB_CODE_SUCCESS != code) {
29,452,281!
441
        return code;
×
442
      }
443
    }
444
  }
445
  return TSDB_CODE_SUCCESS;
29,452,313✔
446
}
447

448
bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex,
449
                              int32_t* nextFrom);
450

451
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst);
452

453
int32_t functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
721,665,619✔
454
  if (pResultInfo->initialized) {
721,665,619✔
455
    return TSDB_CODE_SUCCESS;  // already initialized
158,417✔
456
  }
457

458
  if (pCtx->pOutput != NULL) {
721,507,202!
459
    (void)memset(pCtx->pOutput, 0, (size_t)pCtx->resDataInfo.bytes);
×
460
  }
461

462
  initResultRowEntry(pResultInfo, pCtx->resDataInfo.interBufSize);
721,507,202✔
463
  return TSDB_CODE_SUCCESS;
721,507,202✔
464
}
465

466
int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
188,846,299✔
467
  int32_t          code = TSDB_CODE_SUCCESS;
188,846,299✔
468
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
188,846,299✔
469
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
188,846,299✔
470
  if (NULL == pCol) {
188,224,225!
471
    return TSDB_CODE_OUT_OF_RANGE;
×
472
  }
473
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
188,224,225✔
474
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
188,224,225✔
475

476
  char* in = GET_ROWCELL_INTERBUF(pResInfo);
188,224,225✔
477
  code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes);
188,224,225✔
478

479
  return code;
189,741,996✔
480
}
481

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

487
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
488
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
489

490
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, true);
3✔
491

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

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

506
  char*   in = finalResult;
39✔
507
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes);
39✔
508

509
  return code;
39✔
510
}
511

512
EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
214,382✔
513
  SNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
214,382✔
514
  if (QUERY_NODE_COLUMN == nodeType(pParam) && PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pParam)->colId) {
214,422✔
515
    return FUNC_DATA_REQUIRED_NOT_LOAD;
164,747✔
516
  }
517
  return FUNC_DATA_REQUIRED_SMA_LOAD;
49,675✔
518
}
519

520
bool getCountFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
1,773,208✔
521
  pEnv->calcMemSize = sizeof(int64_t);
1,773,208✔
522
  return true;
1,773,208✔
523
}
524

525
static int64_t getNumOfElems(SqlFunctionCtx* pCtx) {
118,358,679✔
526
  int64_t numOfElem = 0;
118,358,679✔
527

528
  /*
529
   * 1. column data missing (schema modified) causes pInputCol->hasNull == true. pInput->colDataSMAIsSet == true;
530
   * 2. for general non-primary key columns, pInputCol->hasNull may be true or false, pInput->colDataSMAIsSet == true;
531
   * 3. for primary key column, pInputCol->hasNull always be false, pInput->colDataSMAIsSet == false;
532
   */
533
  SInputColumnInfoData* pInput = &pCtx->input;
118,358,679✔
534
  SColumnInfoData*      pInputCol = pInput->pData[0];
118,358,679✔
535
  if (1 == pInput->numOfRows && pInput->blankFill) {
118,358,679✔
536
    return 0;
455,431✔
537
  }
538
  if (pInput->colDataSMAIsSet && pInput->totalRows == pInput->numOfRows) {
117,903,248!
539
    numOfElem = pInput->numOfRows - pInput->pColumnDataAgg[0]->numOfNull;
1,422✔
540
  } else {
541
    if (pInputCol->hasNull) {
117,901,826✔
542
      for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
337,899,894✔
543
        if (colDataIsNull(pInputCol, pInput->totalRows, i, NULL)) {
612,648,942!
544
          continue;
1,840,382✔
545
        }
546
        numOfElem += 1;
304,484,089✔
547
      }
548
    } else {
549
      // when counting on the primary time stamp column and no statistics data is presented, use the size value
550
      // directly.
551
      numOfElem = pInput->numOfRows;
86,326,403✔
552
    }
553
  }
554
  return numOfElem;
117,903,248✔
555
}
556

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

564
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
118,610,366✔
565
  SInputColumnInfoData* pInput = &pCtx->input;
118,610,366✔
566

567
  int32_t type = pInput->pData[0]->info.type;
118,610,366✔
568

569
  char* buf = GET_ROWCELL_INTERBUF(pResInfo);
118,610,366✔
570
  if (IS_NULL_TYPE(type)) {
118,610,366✔
571
    // select count(NULL) returns 0
572
    numOfElem = 1;
136,600✔
573
    *((int64_t*)buf) += 0;
136,600✔
574
  } else {
575
    numOfElem = getNumOfElems(pCtx);
118,473,766✔
576
    *((int64_t*)buf) += numOfElem;
118,009,855✔
577
  }
578

579
  if (tsCountAlwaysReturnValue) {
118,146,455!
580
    pResInfo->numOfRes = 1;
118,186,518✔
581
  } else {
582
    SET_VAL(pResInfo, *((int64_t*)buf), 1);
×
583
  }
584

585
  return TSDB_CODE_SUCCESS;
118,146,455✔
586
}
587

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

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

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

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

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

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

613
int32_t sumFunction(SqlFunctionCtx* pCtx) {
93,109,874✔
614
  int32_t numOfElem = 0;
93,109,874✔
615

616
  // Only the pre-computing information loaded and actual data does not loaded
617
  SInputColumnInfoData* pInput = &pCtx->input;
93,109,874✔
618
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
93,109,874✔
619
  int32_t               type = pInput->pData[0]->info.type;
93,109,874✔
620

621
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
93,109,874✔
622
  pSumRes->type = type;
93,109,874✔
623

624
  if (IS_NULL_TYPE(type)) {
93,109,874✔
625
    numOfElem = 0;
215✔
626
    goto _sum_over;
215✔
627
  }
628

629
  if (pInput->colDataSMAIsSet) {
93,109,659✔
630
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
429✔
631

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

642
    int32_t start = pInput->startRowIndex;
93,109,230✔
643
    int32_t numOfRows = pInput->numOfRows;
93,109,230✔
644

645
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
93,109,230!
646
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
92,267,906!
647
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int8_t, numOfElem);
17,224,964✔
648
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
87,722,592✔
649
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int16_t, numOfElem);
407,389✔
650
      } else if (type == TSDB_DATA_TYPE_INT) {
87,675,186✔
651
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int32_t, numOfElem);
385,309,688✔
652
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
22,207,065✔
653
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int64_t, numOfElem);
78,323,734✔
654
      }
655
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
841,324!
656
      if (type == TSDB_DATA_TYPE_UTINYINT) {
106✔
657
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint8_t, numOfElem);
50,015!
658
      } else if (type == TSDB_DATA_TYPE_USMALLINT) {
91✔
659
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint16_t, numOfElem);
50,023✔
660
      } else if (type == TSDB_DATA_TYPE_UINT) {
75✔
661
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint32_t, numOfElem);
50,015!
662
      } else if (type == TSDB_DATA_TYPE_UBIGINT) {
60!
663
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint64_t, numOfElem);
50,600✔
664
      }
665
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
841,218✔
666
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, double, numOfElem);
2,115,018✔
667
    } else if (type == TSDB_DATA_TYPE_FLOAT) {
759,214✔
668
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, float, numOfElem);
1,709,401✔
669
    }
670
  }
671

672
  // check for overflow
673
  if (IS_FLOAT_TYPE(type) && (isinf(pSumRes->dsum) || isnan(pSumRes->dsum))) {
93,109,659!
674
    numOfElem = 0;
×
675
  }
676

677
_sum_over:
93,649,522✔
678
  if (numOfElem == 0) {
93,109,874✔
679
    if (tsCountAlwaysReturnValue && pCtx->pExpr->pExpr->_function.pFunctNode->hasOriginalFunc &&
86,932✔
680
        fmIsCountLikeFunc(pCtx->pExpr->pExpr->_function.pFunctNode->originalFuncId)) {
11,365✔
681
      numOfElem = 1;
20✔
682
    }
683
  }
684
  // data in the check operation are all null, not output
685
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
93,109,874✔
686
  return TSDB_CODE_SUCCESS;
93,109,874✔
687
}
688

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

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

698
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
699

700
  if (pInput->colDataSMAIsSet) {
701
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
702

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

713
    int32_t start = pInput->startRowIndex;
714
    int32_t numOfRows = pInput->numOfRows;
715

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

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

749
int32_t sumCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
47✔
750
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
47✔
751
  SSumRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
47✔
752

753
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
47✔
754
  SSumRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
47✔
755
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
47✔
756

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

769
bool getSumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
235,874✔
770
  pEnv->calcMemSize = sizeof(SSumRes);
235,874✔
771
  return true;
235,874✔
772
}
773

774
static bool funcNotSupportStringSma(SFunctionNode* pFunc) {
162,415✔
775
  SNode* pParam;
776
  switch (pFunc->funcType) {
162,415!
777
    case FUNCTION_TYPE_MAX:
162,415✔
778
    case FUNCTION_TYPE_MIN:
779
    case FUNCTION_TYPE_SUM:
780
    case FUNCTION_TYPE_AVG:
781
    case FUNCTION_TYPE_AVG_PARTIAL:
782
    case FUNCTION_TYPE_PERCENTILE:
783
    case FUNCTION_TYPE_SPREAD:
784
    case FUNCTION_TYPE_SPREAD_PARTIAL:
785
    case FUNCTION_TYPE_SPREAD_MERGE:
786
    case FUNCTION_TYPE_TWA:
787
    case FUNCTION_TYPE_ELAPSED:
788
      pParam = nodesListGetNode(pFunc->pParameterList, 0);
162,415✔
789
      if (pParam && nodesIsExprNode(pParam) && (IS_VAR_DATA_TYPE(((SExprNode*)pParam)->resType.type))) {
162,415!
790
        return true;
86✔
791
      }
792
      break;
162,329✔
793
    default:
×
794
      break;
×
795
  }
796
  return false;
162,329✔
797
}
798

799
EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
162,415✔
800
  if(funcNotSupportStringSma(pFunc)) {
162,415✔
801
    return FUNC_DATA_REQUIRED_DATA_LOAD;
86✔
802
  }
803
  return FUNC_DATA_REQUIRED_SMA_LOAD;
162,329✔
804
}
805

806
int32_t minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
54,362,168✔
807
  if (pResultInfo->initialized) {
54,362,168!
808
    return TSDB_CODE_SUCCESS;
×
809
  }
810
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
54,362,168!
811
    return TSDB_CODE_FUNC_SETUP_ERROR;  // not initialized since it has been initialized
×
812
  }
813

814
  SMinmaxResInfo* buf = GET_ROWCELL_INTERBUF(pResultInfo);
54,376,776✔
815
  buf->assign = false;
54,376,776✔
816
  buf->tuplePos.pageId = -1;
54,376,776✔
817

818
  buf->nullTupleSaved = false;
54,376,776✔
819
  buf->nullTuplePos.pageId = -1;
54,376,776✔
820
  buf->str = NULL;
54,376,776✔
821
  return TSDB_CODE_SUCCESS;
54,376,776✔
822
}
823

824
bool getMinmaxFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
410,928✔
825
  pEnv->calcMemSize = sizeof(SMinmaxResInfo);
410,928✔
826
  return true;
410,928✔
827
}
828

829
int32_t minFunction(SqlFunctionCtx* pCtx) {
27,632,231✔
830
  int32_t numOfElems = 0;
27,632,231✔
831
  int32_t code = doMinMaxHelper(pCtx, 1, &numOfElems);
27,632,231✔
832
  if (code != TSDB_CODE_SUCCESS) {
27,788,759!
833
    return code;
×
834
  }
835
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
27,788,759✔
836
  return TSDB_CODE_SUCCESS;
27,788,759✔
837
}
838

839
int32_t maxFunction(SqlFunctionCtx* pCtx) {
31,104,125✔
840
  int32_t numOfElems = 0;
31,104,125✔
841
  int32_t code = doMinMaxHelper(pCtx, 0, &numOfElems);
31,104,125✔
842
  if (code != TSDB_CODE_SUCCESS) {
31,235,619!
843
    return code;
×
844
  }
845
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
31,235,619✔
846
  return TSDB_CODE_SUCCESS;
31,235,619✔
847
}
848

849
static int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex);
850
static int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos,
851
                                   int32_t rowIndex);
852

853
int32_t minmaxFunctionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
52,955,688✔
854
  int32_t code = TSDB_CODE_SUCCESS;
52,955,688✔
855

856
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
52,955,688✔
857
  SMinmaxResInfo*      pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
52,955,688✔
858

859
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
52,955,688✔
860
  int32_t currentRow = pBlock->info.rows;
52,955,688✔
861

862
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
52,955,688✔
863
  if (NULL == pCol) {
52,989,033!
864
    return TSDB_CODE_OUT_OF_RANGE;
×
865
  }
866
  pEntryInfo->isNullRes = (pEntryInfo->numOfRes == 0) ? 1 : 0;
52,989,033✔
867

868
  // NOTE: do nothing change it, for performance issue
869
  if (!pEntryInfo->isNullRes) {
52,989,033✔
870
    switch (pCol->info.type) {
44,877,669!
871
      case TSDB_DATA_TYPE_UBIGINT:
10,614,457✔
872
      case TSDB_DATA_TYPE_BIGINT:
873
        ((int64_t*)pCol->pData)[currentRow] = pRes->v;
10,614,457✔
874
        break;
10,614,457✔
875
      case TSDB_DATA_TYPE_UINT:
18,877,624✔
876
      case TSDB_DATA_TYPE_INT:
877
        colDataSetInt32(pCol, currentRow, (int32_t*)&pRes->v);
18,877,624✔
878
        break;
18,877,624✔
879
      case TSDB_DATA_TYPE_USMALLINT:
215,635✔
880
      case TSDB_DATA_TYPE_SMALLINT:
881
        colDataSetInt16(pCol, currentRow, (int16_t*)&pRes->v);
215,635✔
882
        break;
215,635✔
883
      case TSDB_DATA_TYPE_BOOL:
198,953✔
884
      case TSDB_DATA_TYPE_UTINYINT:
885
      case TSDB_DATA_TYPE_TINYINT:
886
        colDataSetInt8(pCol, currentRow, (int8_t*)&pRes->v);
198,953✔
887
        break;
198,953✔
888
      case TSDB_DATA_TYPE_DOUBLE:
3,340,943✔
889
        colDataSetDouble(pCol, currentRow, (double*)&pRes->v);
3,340,943✔
890
        break;
3,340,943✔
891
      case TSDB_DATA_TYPE_FLOAT: {
5,888,023✔
892
        float v = GET_FLOAT_VAL(&pRes->v);
5,888,023✔
893
        colDataSetFloat(pCol, currentRow, &v);
5,888,023✔
894
        break;
5,888,023✔
895
      }
896
      case TSDB_DATA_TYPE_VARBINARY:
5,808,135✔
897
      case TSDB_DATA_TYPE_VARCHAR:
898
      case TSDB_DATA_TYPE_NCHAR: {
899
        code = colDataSetVal(pCol, currentRow, pRes->str, false);
5,808,135✔
900
        if (TSDB_CODE_SUCCESS != code) {
5,808,135!
901
          return code;
×
902
        }
903
        break;
5,808,135✔
904
      }
905
    }
906
  } else {
907
    colDataSetNULL(pCol, currentRow);
8,111,364!
908
  }
909

910
  taosMemoryFreeClear(pRes->str);
52,989,033!
911
  if (pCtx->subsidiaries.num > 0) {
52,989,033✔
912
    if (pEntryInfo->numOfRes > 0) {
11,726,786✔
913
      code = setSelectivityValue(pCtx, pBlock, &pRes->tuplePos, currentRow);
11,724,341✔
914
    } else {
915
      code = setNullSelectivityValue(pCtx, pBlock, currentRow);
2,445✔
916
    }
917
  }
918

919
  return code;
53,005,669✔
920
}
921

922
int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex) {
2,423,301✔
923
  if (pCtx->subsidiaries.num <= 0) {
2,423,301✔
924
    return TSDB_CODE_SUCCESS;
2,420,827✔
925
  }
926

927
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
6,171✔
928
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
3,697✔
929
    int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
3,697✔
930

931
    SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
3,697✔
932
    if (NULL == pDstCol) {
3,697!
933
      return terrno;
×
934
    }
935
    colDataSetNULL(pDstCol, rowIndex);
3,697✔
936
  }
937

938
  return TSDB_CODE_SUCCESS;
2,474✔
939
}
940

941
int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos, int32_t rowIndex) {
244,878,923✔
942
  if (pCtx->subsidiaries.num <= 0) {
244,878,923✔
943
    return TSDB_CODE_SUCCESS;
114,123,334✔
944
  }
945

946
  if ((pCtx->saveHandle.pBuf != NULL && pTuplePos->pageId != -1) ||
130,755,589!
947
      (pCtx->saveHandle.pState && pTuplePos->streamTupleKey.ts > 0)) {
×
948
    int32_t numOfCols = pCtx->subsidiaries.num;
130,758,412✔
949
    char*   p = NULL;
130,758,412✔
950
    int32_t code = loadTupleData(pCtx, pTuplePos, &p);
130,758,412✔
951
    if (p == NULL || TSDB_CODE_SUCCESS != code) {
131,401,919!
952
      qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(),
×
953
             pTuplePos->streamTupleKey.groupId, pTuplePos->streamTupleKey.ts);
954
      return TSDB_CODE_NOT_FOUND;
×
955
    }
956

957
    bool* nullList = (bool*)p;
131,403,214✔
958
    char* pStart = (char*)(nullList + numOfCols * sizeof(bool));
131,403,214✔
959

960
    // todo set the offset value to optimize the performance.
961
    for (int32_t j = 0; j < numOfCols; ++j) {
262,610,378✔
962
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
131,415,458✔
963
      int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
131,415,458✔
964

965
      SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
131,415,458✔
966
      if (NULL == pDstCol) {
131,305,021!
967
        return terrno;
×
968
      }
969
      if (nullList[j]) {
131,307,040✔
970
        colDataSetNULL(pDstCol, rowIndex);
138!
971
      } else {
972
        code = colDataSetValOrCover(pDstCol, rowIndex, pStart, false);
131,306,902✔
973
        if (TSDB_CODE_SUCCESS != code) {
131,207,026!
974
          return code;
×
975
        }
976
      }
977
      pStart += pDstCol->info.bytes;
131,207,164✔
978
    }
979
  }
980

981
  return TSDB_CODE_SUCCESS;
131,192,097✔
982
}
983

984
// This function append the selectivity to subsidiaries function context directly, without fetching data
985
// from intermediate disk based buf page
986
int32_t appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) {
56,432,133✔
987
  if (pCtx->subsidiaries.num <= 0) {
56,432,133!
988
    return TSDB_CODE_SUCCESS;
×
989
  }
990

991
  int32_t code = TSDB_CODE_SUCCESS;
56,432,133✔
992
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
112,843,464✔
993
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
56,434,239✔
994

995
    // get data from source col
996
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
56,434,239✔
997
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
56,434,239✔
998

999
    SColumnInfoData* pSrcCol = taosArrayGet(pCtx->pSrcBlock->pDataBlock, srcSlotId);
56,434,239✔
1000
    if (NULL == pSrcCol) {
56,414,874!
1001
      return TSDB_CODE_OUT_OF_RANGE;
×
1002
    }
1003

1004
    char* pData = colDataGetData(pSrcCol, rowIndex);
56,414,874!
1005

1006
    // append to dest col
1007
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
56,414,874✔
1008

1009
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
56,414,874✔
1010
    if (NULL == pDstCol) {
56,391,753!
1011
      return TSDB_CODE_OUT_OF_RANGE;
×
1012
    }
1013

1014
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
112,783,506✔
1015
      colDataSetNULL(pDstCol, pos);
392✔
1016
    } else {
1017
      code = colDataSetVal(pDstCol, pos, pData, false);
56,391,361✔
1018
      if (TSDB_CODE_SUCCESS != code) {
56,410,939!
1019
        return code;
×
1020
      }
1021
    }
1022
  }
1023
  return code;
56,409,225✔
1024
}
1025

1026
void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; }
32✔
1027

1028
#define COMPARE_MINMAX_DATA(type) (((*(type*)&pDBuf->v) < (*(type*)&pSBuf->v)) ^ isMinFunc)
1029
int32_t minMaxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx, int32_t isMinFunc) {
58✔
1030
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
58✔
1031
  SMinmaxResInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
58✔
1032

1033
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
58✔
1034
  SMinmaxResInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
58✔
1035
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
58✔
1036

1037
  switch (type) {
58!
1038
    case TSDB_DATA_TYPE_DOUBLE:
3✔
1039
    case TSDB_DATA_TYPE_UBIGINT:
1040
    case TSDB_DATA_TYPE_BIGINT:
1041
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int64_t) || !pDBuf->assign)) {
3!
1042
        pDBuf->v = pSBuf->v;
1✔
1043
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
1✔
1044
        pDBuf->assign = true;
1✔
1045
      }
1046
      break;
3✔
1047
    case TSDB_DATA_TYPE_UINT:
55✔
1048
    case TSDB_DATA_TYPE_INT:
1049
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int32_t) || !pDBuf->assign)) {
55!
1050
        pDBuf->v = pSBuf->v;
31✔
1051
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
31✔
1052
        pDBuf->assign = true;
31✔
1053
      }
1054
      break;
55✔
1055
    case TSDB_DATA_TYPE_USMALLINT:
×
1056
    case TSDB_DATA_TYPE_SMALLINT:
1057
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int16_t) || !pDBuf->assign)) {
×
1058
        pDBuf->v = pSBuf->v;
×
1059
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1060
        pDBuf->assign = true;
×
1061
      }
1062
      break;
×
1063
    case TSDB_DATA_TYPE_BOOL:
×
1064
    case TSDB_DATA_TYPE_UTINYINT:
1065
    case TSDB_DATA_TYPE_TINYINT:
1066
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int8_t) || !pDBuf->assign)) {
×
1067
        pDBuf->v = pSBuf->v;
×
1068
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1069
        pDBuf->assign = true;
×
1070
      }
1071
      break;
×
1072
    case TSDB_DATA_TYPE_FLOAT: {
×
1073
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(double) || !pDBuf->assign)) {
×
1074
        pDBuf->v = pSBuf->v;
×
1075
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1076
        pDBuf->assign = true;
×
1077
      }
1078
      break;
×
1079
    }
1080
    default:
×
1081
      if (pSBuf->assign && (strcmp((char*)&pDBuf->v, (char*)&pSBuf->v) || !pDBuf->assign)) {
×
1082
        pDBuf->v = pSBuf->v;
×
1083
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
×
1084
        pDBuf->assign = true;
×
1085
      }
1086
      break;
×
1087
  }
1088
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
58✔
1089
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
58✔
1090
  return TSDB_CODE_SUCCESS;
58✔
1091
}
1092

1093
int32_t minCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
11✔
1094
  return minMaxCombine(pDestCtx, pSourceCtx, 1);
11✔
1095
}
1096
int32_t maxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
47✔
1097
  return minMaxCombine(pDestCtx, pSourceCtx, 0);
47✔
1098
}
1099

1100
int32_t getStdInfoSize() { return (int32_t)sizeof(SStdRes); }
1,109,927✔
1101

1102
bool getStdFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
145,214✔
1103
  pEnv->calcMemSize = sizeof(SStdRes);
145,214✔
1104
  return true;
145,214✔
1105
}
1106

1107
int32_t stdFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
7,965,089✔
1108
  if (pResultInfo->initialized) {
7,965,089!
1109
    return TSDB_CODE_SUCCESS;
×
1110
  }
1111
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
7,965,089!
1112
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1113
  }
1114

1115
  SStdRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
7,965,102✔
1116
  (void)memset(pRes, 0, sizeof(SStdRes));
7,965,102✔
1117
  return TSDB_CODE_SUCCESS;
7,965,102✔
1118
}
1119

1120
int32_t stdFunction(SqlFunctionCtx* pCtx) {
7,965,956✔
1121
  int32_t numOfElem = 0;
7,965,956✔
1122

1123
  // Only the pre-computing information loaded and actual data does not loaded
1124
  SInputColumnInfoData* pInput = &pCtx->input;
7,965,956✔
1125
  int32_t               type = pInput->pData[0]->info.type;
7,965,956✔
1126

1127
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,965,956✔
1128
  pStdRes->type = type;
7,965,956✔
1129

1130
  // computing based on the true data block
1131
  SColumnInfoData* pCol = pInput->pData[0];
7,965,956✔
1132

1133
  int32_t start = pInput->startRowIndex;
7,965,956✔
1134
  int32_t numOfRows = pInput->numOfRows;
7,965,956✔
1135

1136
  if (IS_NULL_TYPE(type)) {
7,965,956✔
1137
    numOfElem = 0;
119✔
1138
    goto _stddev_over;
119✔
1139
  }
1140

1141
  switch (type) {
7,965,837!
1142
    case TSDB_DATA_TYPE_TINYINT: {
15,053✔
1143
      int8_t* plist = (int8_t*)pCol->pData;
15,053✔
1144
      for (int32_t i = start; i < numOfRows + start; ++i) {
147,168✔
1145
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
132,115✔
1146
          continue;
30,577✔
1147
        }
1148

1149
        numOfElem += 1;
101,538✔
1150
        pStdRes->count += 1;
101,538✔
1151
        pStdRes->isum += plist[i];
101,538✔
1152
        pStdRes->quadraticISum += plist[i] * plist[i];
101,538✔
1153
      }
1154

1155
      break;
15,053✔
1156
    }
1157

1158
    case TSDB_DATA_TYPE_SMALLINT: {
226,929✔
1159
      int16_t* plist = (int16_t*)pCol->pData;
226,929✔
1160
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
2,063,678✔
1161
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,836,749✔
1162
          continue;
129,034✔
1163
        }
1164

1165
        numOfElem += 1;
1,707,715✔
1166
        pStdRes->count += 1;
1,707,715✔
1167
        pStdRes->isum += plist[i];
1,707,715✔
1168
        pStdRes->quadraticISum += plist[i] * plist[i];
1,707,715✔
1169
      }
1170
      break;
226,929✔
1171
    }
1172

1173
    case TSDB_DATA_TYPE_INT: {
17,139✔
1174
      int32_t* plist = (int32_t*)pCol->pData;
17,139✔
1175
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
508,310✔
1176
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
491,171✔
1177
          continue;
97,355✔
1178
        }
1179

1180
        numOfElem += 1;
393,816✔
1181
        pStdRes->count += 1;
393,816✔
1182
        pStdRes->isum += plist[i];
393,816✔
1183
        pStdRes->quadraticISum += plist[i] * plist[i];
393,816✔
1184
      }
1185

1186
      break;
17,139✔
1187
    }
1188

1189
    case TSDB_DATA_TYPE_BIGINT: {
5,499,275✔
1190
      int64_t* plist = (int64_t*)pCol->pData;
5,499,275✔
1191
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
21,560,314✔
1192
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
16,061,039✔
1193
          continue;
199,759✔
1194
        }
1195

1196
        numOfElem += 1;
15,861,280✔
1197
        pStdRes->count += 1;
15,861,280✔
1198
        pStdRes->isum += plist[i];
15,861,280✔
1199
        pStdRes->quadraticISum += plist[i] * plist[i];
15,861,280✔
1200
      }
1201
      break;
5,499,275✔
1202
    }
1203

1204
    case TSDB_DATA_TYPE_UTINYINT: {
1✔
1205
      uint8_t* plist = (uint8_t*)pCol->pData;
1✔
1206
      for (int32_t i = start; i < numOfRows + start; ++i) {
8✔
1207
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
7!
1208
          continue;
4✔
1209
        }
1210

1211
        numOfElem += 1;
3✔
1212
        pStdRes->count += 1;
3✔
1213
        pStdRes->usum += plist[i];
3✔
1214
        pStdRes->quadraticUSum += plist[i] * plist[i];
3✔
1215
      }
1216

1217
      break;
1✔
1218
    }
1219

1220
    case TSDB_DATA_TYPE_USMALLINT: {
×
1221
      uint16_t* plist = (uint16_t*)pCol->pData;
×
1222
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
1223
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
1224
          continue;
×
1225
        }
1226

1227
        numOfElem += 1;
×
1228
        pStdRes->count += 1;
×
1229
        pStdRes->usum += plist[i];
×
1230
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1231
      }
1232
      break;
×
1233
    }
1234

1235
    case TSDB_DATA_TYPE_UINT: {
1✔
1236
      uint32_t* plist = (uint32_t*)pCol->pData;
1✔
1237
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
6✔
1238
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
5!
1239
          continue;
×
1240
        }
1241

1242
        numOfElem += 1;
5✔
1243
        pStdRes->count += 1;
5✔
1244
        pStdRes->usum += plist[i];
5✔
1245
        pStdRes->quadraticUSum += plist[i] * plist[i];
5✔
1246
      }
1247

1248
      break;
1✔
1249
    }
1250

1251
    case TSDB_DATA_TYPE_UBIGINT: {
×
1252
      uint64_t* plist = (uint64_t*)pCol->pData;
×
1253
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
1254
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
1255
          continue;
×
1256
        }
1257

1258
        numOfElem += 1;
×
1259
        pStdRes->count += 1;
×
1260
        pStdRes->usum += plist[i];
×
1261
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1262
      }
1263
      break;
×
1264
    }
1265

1266
    case TSDB_DATA_TYPE_FLOAT: {
18,783✔
1267
      float* plist = (float*)pCol->pData;
18,783✔
1268
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
1,073,275✔
1269
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,054,492✔
1270
          continue;
28,239✔
1271
        }
1272

1273
        numOfElem += 1;
1,026,253✔
1274
        pStdRes->count += 1;
1,026,253✔
1275
        pStdRes->dsum += plist[i];
1,026,253✔
1276
        pStdRes->quadraticDSum += plist[i] * plist[i];
1,026,253✔
1277
      }
1278
      break;
18,783✔
1279
    }
1280

1281
    case TSDB_DATA_TYPE_DOUBLE: {
2,188,839✔
1282
      double* plist = (double*)pCol->pData;
2,188,839✔
1283
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
8,720,411✔
1284
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
6,531,572✔
1285
          continue;
622,426✔
1286
        }
1287

1288
        numOfElem += 1;
5,909,146✔
1289
        pStdRes->count += 1;
5,909,146✔
1290
        pStdRes->dsum += plist[i];
5,909,146✔
1291
        pStdRes->quadraticDSum += plist[i] * plist[i];
5,909,146✔
1292
      }
1293
      break;
2,188,839✔
1294
    }
1295

1296
    default:
×
1297
      break;
×
1298
  }
1299

1300
_stddev_over:
7,965,956✔
1301
  // data in the check operation are all null, not output
1302
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
7,965,956✔
1303
  return TSDB_CODE_SUCCESS;
7,965,956✔
1304
}
1305

1306
static void stdTransferInfo(SStdRes* pInput, SStdRes* pOutput) {
1,108,435✔
1307
  if (IS_NULL_TYPE(pInput->type)) {
1,108,435✔
1308
    return;
80✔
1309
  }
1310
  pOutput->type = pInput->type;
1,108,355✔
1311
  if (IS_SIGNED_NUMERIC_TYPE(pOutput->type)) {
1,108,355!
1312
    pOutput->quadraticISum += pInput->quadraticISum;
1,108,051✔
1313
    pOutput->isum += pInput->isum;
1,108,051✔
1314
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pOutput->type)) {
304!
1315
    pOutput->quadraticUSum += pInput->quadraticUSum;
1✔
1316
    pOutput->usum += pInput->usum;
1✔
1317
  } else {
1318
    pOutput->quadraticDSum += pInput->quadraticDSum;
303✔
1319
    pOutput->dsum += pInput->dsum;
303✔
1320
  }
1321

1322
  pOutput->count += pInput->count;
1,108,355✔
1323
}
1324

1325
int32_t stdFunctionMerge(SqlFunctionCtx* pCtx) {
1,108,374✔
1326
  SInputColumnInfoData* pInput = &pCtx->input;
1,108,374✔
1327
  SColumnInfoData*      pCol = pInput->pData[0];
1,108,374✔
1328

1329
  if (IS_NULL_TYPE(pCol->info.type)) {
1,108,374!
1330
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
1331
    return TSDB_CODE_SUCCESS;
×
1332
  }
1333

1334
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
1,108,374!
1335
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1336
  }
1337

1338
  SStdRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,108,374✔
1339

1340
  for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
2,216,806✔
1341
    if (colDataIsNull_s(pCol, i)) continue;
2,216,864!
1342
    char*    data = colDataGetData(pCol, i);
1,108,432!
1343
    SStdRes* pInputInfo = (SStdRes*)varDataVal(data);
1,108,432✔
1344
    stdTransferInfo(pInputInfo, pInfo);
1,108,432✔
1345
  }
1346

1347
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
1,108,374✔
1348
  return TSDB_CODE_SUCCESS;
1,108,374✔
1349
}
1350

1351
#ifdef BUILD_NO_CALL
1352
int32_t stdInvertFunction(SqlFunctionCtx* pCtx) {
1353
  int32_t numOfElem = 0;
1354

1355
  // Only the pre-computing information loaded and actual data does not loaded
1356
  SInputColumnInfoData* pInput = &pCtx->input;
1357
  int32_t               type = pInput->pData[0]->info.type;
1358

1359
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1360

1361
  // computing based on the true data block
1362
  SColumnInfoData* pCol = pInput->pData[0];
1363

1364
  int32_t start = pInput->startRowIndex;
1365
  int32_t numOfRows = pInput->numOfRows;
1366

1367
  switch (type) {
1368
    case TSDB_DATA_TYPE_TINYINT: {
1369
      LIST_STDDEV_SUB_N(pStdRes->isum, int8_t);
1370
      break;
1371
    }
1372
    case TSDB_DATA_TYPE_SMALLINT: {
1373
      LIST_STDDEV_SUB_N(pStdRes->isum, int16_t);
1374
      break;
1375
    }
1376
    case TSDB_DATA_TYPE_INT: {
1377
      LIST_STDDEV_SUB_N(pStdRes->isum, int32_t);
1378
      break;
1379
    }
1380
    case TSDB_DATA_TYPE_BIGINT: {
1381
      LIST_STDDEV_SUB_N(pStdRes->isum, int64_t);
1382
      break;
1383
    }
1384
    case TSDB_DATA_TYPE_UTINYINT: {
1385
      LIST_STDDEV_SUB_N(pStdRes->isum, uint8_t);
1386
      break;
1387
    }
1388
    case TSDB_DATA_TYPE_USMALLINT: {
1389
      LIST_STDDEV_SUB_N(pStdRes->isum, uint16_t);
1390
      break;
1391
    }
1392
    case TSDB_DATA_TYPE_UINT: {
1393
      LIST_STDDEV_SUB_N(pStdRes->isum, uint32_t);
1394
      break;
1395
    }
1396
    case TSDB_DATA_TYPE_UBIGINT: {
1397
      LIST_STDDEV_SUB_N(pStdRes->isum, uint64_t);
1398
      break;
1399
    }
1400
    case TSDB_DATA_TYPE_FLOAT: {
1401
      LIST_STDDEV_SUB_N(pStdRes->dsum, float);
1402
      break;
1403
    }
1404
    case TSDB_DATA_TYPE_DOUBLE: {
1405
      LIST_STDDEV_SUB_N(pStdRes->dsum, double);
1406
      break;
1407
    }
1408
    default:
1409
      break;
1410
  }
1411

1412
  // data in the check operation are all null, not output
1413
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
1414
  return TSDB_CODE_SUCCESS;
1415
}
1416
#endif
1417

1418
int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
6,826,072✔
1419
  SInputColumnInfoData* pInput = &pCtx->input;
6,826,072✔
1420
  SStdRes*              pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
6,826,072✔
1421
  int32_t               type = pStddevRes->type;
6,826,072✔
1422
  double                avg;
1423

1424
  if (pStddevRes->count == 0) {
6,826,072✔
1425
    GET_RES_INFO(pCtx)->numOfRes = 0;
54,920✔
1426
    return functionFinalize(pCtx, pBlock);
54,920✔
1427
  }
1428

1429
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
6,771,152!
1430
    avg = pStddevRes->isum / ((double)pStddevRes->count);
4,616,197✔
1431
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticISum / ((double)pStddevRes->count) - avg * avg));
4,616,197✔
1432
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
2,154,955!
1433
    avg = pStddevRes->usum / ((double)pStddevRes->count);
2✔
1434
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticUSum / ((double)pStddevRes->count) - avg * avg));
2✔
1435
  } else {
1436
    avg = pStddevRes->dsum / ((double)pStddevRes->count);
2,154,953✔
1437
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticDSum / ((double)pStddevRes->count) - avg * avg));
2,154,953✔
1438
  }
1439

1440
  // check for overflow
1441
  if (isinf(pStddevRes->result) || isnan(pStddevRes->result)) {
6,771,152!
1442
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1443
  }
1444

1445
  return functionFinalize(pCtx, pBlock);
6,771,152✔
1446
}
1447

1448
int32_t stdvarFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
1449
  SInputColumnInfoData* pInput = &pCtx->input;
×
1450
  SStdRes*              pStdvarRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1451
  int32_t               type = pStdvarRes->type;
×
1452
  double                avg;
1453

1454
  if (pStdvarRes->count == 0) {
×
1455
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1456
    return functionFinalize(pCtx, pBlock);
×
1457
  }
1458

1459
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
1460
    avg = pStdvarRes->isum / ((double)pStdvarRes->count);
×
1461
    pStdvarRes->result = fabs(pStdvarRes->quadraticISum / ((double)pStdvarRes->count) - avg * avg);
×
1462
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
1463
    avg = pStdvarRes->usum / ((double)pStdvarRes->count);
×
1464
    pStdvarRes->result = fabs(pStdvarRes->quadraticUSum / ((double)pStdvarRes->count) - avg * avg);
×
1465
  } else {
1466
    avg = pStdvarRes->dsum / ((double)pStdvarRes->count);
×
1467
    pStdvarRes->result = fabs(pStdvarRes->quadraticDSum / ((double)pStdvarRes->count) - avg * avg);
×
1468
  }
1469

1470
  // check for overflow
1471
  if (isinf(pStdvarRes->result) || isnan(pStdvarRes->result)) {
×
1472
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1473
  }
1474

1475
  return functionFinalize(pCtx, pBlock);
×
1476
}
1477

1478
int32_t stdPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1,108,368✔
1479
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,108,368✔
1480
  SStdRes*             pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,108,368✔
1481
  int32_t              resultBytes = getStdInfoSize();
1,108,368✔
1482
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
1,108,368!
1483

1484
  if (NULL == res) {
1,108,368!
1485
    return terrno;
×
1486
  }
1487
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
1,108,368✔
1488
  varDataSetLen(res, resultBytes);
1,108,368✔
1489

1490
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
1,108,368✔
1491
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
1,108,368✔
1492
  if (NULL == pCol) {
1,108,368!
1493
    taosMemoryFree(res);
×
1494
    return TSDB_CODE_OUT_OF_RANGE;
×
1495
  }
1496

1497
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
1,108,368✔
1498

1499
  taosMemoryFree(res);
1,108,368!
1500
  return code;
1,108,368✔
1501
}
1502

1503
int32_t stdCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
3✔
1504
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
3✔
1505
  SStdRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
3✔
1506

1507
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
1508
  SStdRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
1509
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
3!
1510

1511
  stdTransferInfo(pSBuf, pDBuf);
3✔
1512

1513
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
3✔
1514
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
3✔
1515
  return TSDB_CODE_SUCCESS;
3✔
1516
}
1517

1518
bool getLeastSQRFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
44,375✔
1519
  pEnv->calcMemSize = sizeof(SLeastSQRInfo);
44,375✔
1520
  return true;
44,375✔
1521
}
1522

1523
int32_t leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
3,398,033✔
1524
  if (pResultInfo->initialized) {
3,398,033!
1525
    return TSDB_CODE_SUCCESS;
×
1526
  }
1527
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
3,398,033!
1528
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1529
  }
1530

1531
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
3,398,030✔
1532

1533
  GET_TYPED_DATA(pInfo->startVal, double, pCtx->param[1].param.nType, &pCtx->param[1].param.i);
3,398,030!
1534
  GET_TYPED_DATA(pInfo->stepVal, double, pCtx->param[2].param.nType, &pCtx->param[2].param.i);
3,398,030!
1535
  return TSDB_CODE_SUCCESS;
3,398,030✔
1536
}
1537

1538
int32_t leastSQRFunction(SqlFunctionCtx* pCtx) {
3,422,243✔
1539
  int32_t numOfElem = 0;
3,422,243✔
1540

1541
  SInputColumnInfoData* pInput = &pCtx->input;
3,422,243✔
1542
  int32_t               type = pInput->pData[0]->info.type;
3,422,243✔
1543

1544
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
3,422,243✔
1545

1546
  SColumnInfoData* pCol = pInput->pData[0];
3,422,243✔
1547

1548
  double(*param)[3] = pInfo->matrix;
3,422,243✔
1549
  double x = pInfo->startVal;
3,422,243✔
1550

1551
  int32_t start = pInput->startRowIndex;
3,422,243✔
1552
  int32_t numOfRows = pInput->numOfRows;
3,422,243✔
1553

1554
  switch (type) {
3,422,243!
1555
    case TSDB_DATA_TYPE_TINYINT: {
1,779,393✔
1556
      int8_t* plist = (int8_t*)pCol->pData;
1,779,393✔
1557
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
5,941,156✔
1558
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
4,161,763✔
1559
          continue;
1,236✔
1560
        }
1561
        numOfElem++;
4,160,527✔
1562
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
4,160,527✔
1563
      }
1564
      break;
1,779,393✔
1565
    }
1566
    case TSDB_DATA_TYPE_SMALLINT: {
17,008✔
1567
      int16_t* plist = (int16_t*)pCol->pData;
17,008✔
1568
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
692,910✔
1569
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
675,902✔
1570
          continue;
150,167✔
1571
        }
1572

1573
        numOfElem++;
525,735✔
1574
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
525,735✔
1575
      }
1576
      break;
17,008✔
1577
    }
1578

1579
    case TSDB_DATA_TYPE_INT: {
1,606,780✔
1580
      int32_t* plist = (int32_t*)pCol->pData;
1,606,780✔
1581
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
5,583,034✔
1582
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
3,976,254✔
1583
          continue;
150,447✔
1584
        }
1585

1586
        numOfElem++;
3,825,807✔
1587
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
3,825,807✔
1588
      }
1589
      break;
1,606,780✔
1590
    }
1591

1592
    case TSDB_DATA_TYPE_BIGINT: {
6,114✔
1593
      int64_t* plist = (int64_t*)pCol->pData;
6,114✔
1594
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
285,199✔
1595
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
279,085✔
1596
          continue;
275,869✔
1597
        }
1598

1599
        numOfElem++;
3,216✔
1600
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
3,216✔
1601
      }
1602
      break;
6,114✔
1603
    }
1604

1605
    case TSDB_DATA_TYPE_UTINYINT: {
78✔
1606
      uint8_t* plist = (uint8_t*)pCol->pData;
78✔
1607
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
330✔
1608
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
252!
1609
          continue;
66✔
1610
        }
1611
        numOfElem++;
186✔
1612
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
186✔
1613
      }
1614
      break;
78✔
1615
    }
1616
    case TSDB_DATA_TYPE_USMALLINT: {
78✔
1617
      uint16_t* plist = (uint16_t*)pCol->pData;
78✔
1618
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
330✔
1619
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
252!
1620
          continue;
60✔
1621
        }
1622

1623
        numOfElem++;
192✔
1624
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
192✔
1625
      }
1626
      break;
78✔
1627
    }
1628

1629
    case TSDB_DATA_TYPE_UINT: {
78✔
1630
      uint32_t* plist = (uint32_t*)pCol->pData;
78✔
1631
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
330✔
1632
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
252!
1633
          continue;
60✔
1634
        }
1635

1636
        numOfElem++;
192✔
1637
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
192✔
1638
      }
1639
      break;
78✔
1640
    }
1641

1642
    case TSDB_DATA_TYPE_UBIGINT: {
78✔
1643
      uint64_t* plist = (uint64_t*)pCol->pData;
78✔
1644
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
330✔
1645
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
252!
1646
          continue;
60✔
1647
        }
1648

1649
        numOfElem++;
192✔
1650
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
192✔
1651
      }
1652
      break;
78✔
1653
    }
1654

1655
    case TSDB_DATA_TYPE_FLOAT: {
7,128✔
1656
      float* plist = (float*)pCol->pData;
7,128✔
1657
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
278,525✔
1658
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
271,397✔
1659
          continue;
1,836✔
1660
        }
1661

1662
        numOfElem++;
269,561✔
1663
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
269,561✔
1664
      }
1665
      break;
7,128✔
1666
    }
1667

1668
    case TSDB_DATA_TYPE_DOUBLE: {
5,510✔
1669
      double* plist = (double*)pCol->pData;
5,510✔
1670
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
90,470✔
1671
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
84,960✔
1672
          continue;
2,436✔
1673
        }
1674

1675
        numOfElem++;
82,524✔
1676
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
82,524✔
1677
      }
1678
      break;
5,510✔
1679
    }
1680
    case TSDB_DATA_TYPE_NULL: {
×
1681
      GET_RES_INFO(pCtx)->isNullRes = 1;
×
1682
      numOfElem = 1;
×
1683
      break;
×
1684
    }
1685

1686
    default:
×
1687
      break;
×
1688
  }
1689

1690
  pInfo->startVal = x;
3,422,243✔
1691
  pInfo->num += numOfElem;
3,422,243✔
1692

1693
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
3,422,243✔
1694

1695
  return TSDB_CODE_SUCCESS;
3,422,243✔
1696
}
1697

1698
int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
3,391,094✔
1699
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
3,391,094✔
1700
  SLeastSQRInfo*       pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
3,391,094✔
1701
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
3,391,094✔
1702
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
3,391,094✔
1703

1704
  if (NULL == pCol) {
3,391,095!
1705
    return TSDB_CODE_OUT_OF_RANGE;
×
1706
  }
1707
  int32_t currentRow = pBlock->info.rows;
3,391,095✔
1708

1709
  if (0 == pInfo->num) {
3,391,095✔
1710
    colDataSetNULL(pCol, currentRow);
22,038!
1711
    return TSDB_CODE_SUCCESS;
22,038✔
1712
  }
1713

1714
  double(*param)[3] = pInfo->matrix;
3,369,057✔
1715

1716
  param[1][1] = (double)pInfo->num;
3,369,057✔
1717
  param[1][0] = param[0][1];
3,369,057✔
1718

1719
  double param00 = param[0][0] - param[1][0] * (param[0][1] / param[1][1]);
3,369,057✔
1720
  double param02 = param[0][2] - param[1][2] * (param[0][1] / param[1][1]);
3,369,057✔
1721

1722
  if (0 == param00) {
3,369,057✔
1723
    colDataSetNULL(pCol, currentRow);
2,848,142!
1724
    return TSDB_CODE_SUCCESS;
2,848,142✔
1725
  }
1726

1727
  // param[0][1] = 0;
1728
  double param12 = param[1][2] - param02 * (param[1][0] / param00);
520,915✔
1729
  // param[1][0] = 0;
1730
  param02 /= param00;
520,915✔
1731

1732
  param12 /= param[1][1];
520,915✔
1733

1734
  char buf[LEASTSQUARES_BUFF_LENGTH] = {0};
520,915✔
1735
  char slopBuf[64] = {0};
520,915✔
1736
  char interceptBuf[64] = {0};
520,915✔
1737
  int  n = tsnprintf(slopBuf, 64, "%.6lf", param02);
520,915✔
1738
  if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) {
520,918✔
1739
    (void)snprintf(slopBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param02);
6✔
1740
  }
1741
  n = tsnprintf(interceptBuf, 64, "%.6lf", param12);
520,918✔
1742
  if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) {
520,918✔
1743
    (void)snprintf(interceptBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param12);
18✔
1744
  }
1745
  size_t len =
520,918✔
1746
      snprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{slop:%s, intercept:%s}", slopBuf, interceptBuf);
520,918✔
1747
  varDataSetLen(buf, len);
520,918✔
1748

1749
  int32_t code = colDataSetVal(pCol, currentRow, buf, pResInfo->isNullRes);
520,918✔
1750

1751
  return code;
520,918✔
1752
}
1753

1754
int32_t leastSQRCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
1755
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
1756
  SLeastSQRInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
1757
  int32_t              type = pDestCtx->input.pData[0]->info.type;
×
1758
  double(*pDparam)[3] = pDBuf->matrix;
×
1759

1760
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
1761
  SLeastSQRInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
1762
  double(*pSparam)[3] = pSBuf->matrix;
×
1763
  for (int32_t i = 0; i < pSBuf->num; i++) {
×
1764
    pDparam[0][0] += pDBuf->startVal * pDBuf->startVal;
×
1765
    pDparam[0][1] += pDBuf->startVal;
×
1766
    pDBuf->startVal += pDBuf->stepVal;
×
1767
  }
1768
  pDparam[0][2] += pSparam[0][2] + pDBuf->num * pDBuf->stepVal * pSparam[1][2];
×
1769
  pDparam[1][2] += pSparam[1][2];
×
1770
  pDBuf->num += pSBuf->num;
×
1771
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
1772
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
1773
  return TSDB_CODE_SUCCESS;
×
1774
}
1775

1776
bool getPercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
647✔
1777
  pEnv->calcMemSize = sizeof(SPercentileInfo);
647✔
1778
  return true;
647✔
1779
}
1780

1781
int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
6,023✔
1782
  if (pResultInfo->initialized) {
6,023!
1783
    return TSDB_CODE_SUCCESS;
×
1784
  }
1785
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
6,023!
1786
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1787
  }
1788

1789
  // in the first round, get the min-max value of all involved data
1790
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
6,023✔
1791
  SET_DOUBLE_VAL(&pInfo->minval, DBL_MAX);
6,023✔
1792
  SET_DOUBLE_VAL(&pInfo->maxval, -DBL_MAX);
6,023✔
1793
  pInfo->numOfElems = 0;
6,023✔
1794

1795
  return TSDB_CODE_SUCCESS;
6,023✔
1796
}
1797

1798
void percentileFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
1799
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
1800
    return;
×
1801
  }
1802
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1803
  if (pInfo->pMemBucket != NULL) {
×
1804
    tMemBucketDestroy(&(pInfo->pMemBucket));
×
1805
    pInfo->pMemBucket = NULL;
×
1806
  }
1807
}
1808

1809
int32_t percentileFunction(SqlFunctionCtx* pCtx) {
2,375,274✔
1810
  int32_t              code = TSDB_CODE_SUCCESS;
2,375,274✔
1811
  int32_t              numOfElems = 0;
2,375,274✔
1812
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,375,274✔
1813

1814
  SInputColumnInfoData* pInput = &pCtx->input;
2,375,274✔
1815
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
2,375,274✔
1816

1817
  SColumnInfoData* pCol = pInput->pData[0];
2,375,274✔
1818
  int32_t          type = pCol->info.type;
2,375,274✔
1819

1820
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
2,375,274✔
1821
  if (pCtx->scanFlag == MAIN_SCAN && pInfo->stage == 0) {
2,375,274✔
1822
    pInfo->stage += 1;
6,023✔
1823

1824
    // all data are null, set it completed
1825
    if (pInfo->numOfElems == 0) {
6,023✔
1826
      pResInfo->complete = true;
2,736✔
1827
      return TSDB_CODE_SUCCESS;
2,736✔
1828
    } else {
1829
      code = tMemBucketCreate(pCol->info.bytes, type, pInfo->minval, pInfo->maxval, pCtx->hasWindowOrGroup,
3,287✔
1830
                              &pInfo->pMemBucket, pInfo->numOfElems);
3,287✔
1831
      if (TSDB_CODE_SUCCESS != code) {
3,287!
1832
        return code;
×
1833
      }
1834
    }
1835
  }
1836

1837
  // the first stage, only acquire the min/max value
1838
  if (pInfo->stage == 0) {
2,372,538✔
1839
    if (pCtx->input.colDataSMAIsSet) {
1,187,673✔
1840
      double tmin = 0.0, tmax = 0.0;
1,177,848✔
1841
      if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,177,848!
1842
        tmin = (double)GET_INT64_VAL(&pAgg->min);
×
1843
        tmax = (double)GET_INT64_VAL(&pAgg->max);
×
1844
      } else if (IS_FLOAT_TYPE(type)) {
1,177,848!
1845
        tmin = GET_DOUBLE_VAL(&pAgg->min);
1,177,848✔
1846
        tmax = GET_DOUBLE_VAL(&pAgg->max);
1,177,848✔
1847
      } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
1848
        tmin = (double)GET_UINT64_VAL(&pAgg->min);
×
1849
        tmax = (double)GET_UINT64_VAL(&pAgg->max);
×
1850
      }
1851

1852
      if (GET_DOUBLE_VAL(&pInfo->minval) > tmin) {
1,177,848✔
1853
        SET_DOUBLE_VAL(&pInfo->minval, tmin);
14✔
1854
      }
1855

1856
      if (GET_DOUBLE_VAL(&pInfo->maxval) < tmax) {
1,177,848✔
1857
        SET_DOUBLE_VAL(&pInfo->maxval, tmax);
14✔
1858
      }
1859

1860
      pInfo->numOfElems += (pInput->numOfRows - pAgg->numOfNull);
1,177,848✔
1861
    } else {
1862
      // check the valid data one by one
1863
      int32_t start = pInput->startRowIndex;
9,825✔
1864
      for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
11,726,116✔
1865
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
11,716,291✔
1866
          continue;
3,600✔
1867
        }
1868

1869
        char* data = colDataGetData(pCol, i);
11,712,691!
1870

1871
        double v = 0;
11,712,691✔
1872
        GET_TYPED_DATA(v, double, type, data);
11,712,691!
1873
        if (v < GET_DOUBLE_VAL(&pInfo->minval)) {
11,712,691✔
1874
          SET_DOUBLE_VAL(&pInfo->minval, v);
3,569✔
1875
        }
1876

1877
        if (v > GET_DOUBLE_VAL(&pInfo->maxval)) {
11,712,691✔
1878
          SET_DOUBLE_VAL(&pInfo->maxval, v);
408,907✔
1879
        }
1880

1881
        pInfo->numOfElems += 1;
11,712,691✔
1882
      }
1883
    }
1884
  } else {
1885
    // the second stage, calculate the true percentile value
1886
    int32_t start = pInput->startRowIndex;
1,184,865✔
1887
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
2,147,483,647✔
1888
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
2,147,483,647!
1889
        continue;
×
1890
      }
1891

1892
      char* data = colDataGetData(pCol, i);
2,147,483,647!
1893
      numOfElems += 1;
2,147,483,647✔
1894
      code = tMemBucketPut(pInfo->pMemBucket, data, 1);
2,147,483,647✔
1895
      if (code != TSDB_CODE_SUCCESS) {
2,147,483,647!
1896
        tMemBucketDestroy(&(pInfo->pMemBucket));
×
1897
        return code;
×
1898
      }
1899
    }
1900

1901
    SET_VAL(pResInfo, numOfElems, 1);
1,184,865!
1902
  }
1903

1904
  pCtx->needCleanup = true;
2,372,538✔
1905
  return TSDB_CODE_SUCCESS;
2,372,538✔
1906
}
1907

1908
int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
6,023✔
1909
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
6,023✔
1910
  SPercentileInfo*     ppInfo = (SPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
6,023✔
1911

1912
  int32_t code = 0;
6,023✔
1913
  double  v = 0;
6,023✔
1914

1915
  tMemBucket** pMemBucket = &ppInfo->pMemBucket;
6,023✔
1916
  if ((*pMemBucket) != NULL && (*pMemBucket)->total > 0) {  // check for null
6,023!
1917
    if (pCtx->numOfParams > 2) {
3,287✔
1918
      char buf[3200] = {0};
22✔
1919
      // max length of double num is 317, e.g. use %.6lf to print -1.0e+308, consider the comma and bracket, 3200 is
1920
      // enough.
1921
      size_t len = 1;
22✔
1922

1923
      varDataVal(buf)[0] = '[';
22✔
1924
      for (int32_t i = 1; i < pCtx->numOfParams; ++i) {
210✔
1925
        SVariant* pVal = &pCtx->param[i].param;
188✔
1926

1927
        GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
188!
1928

1929
        code = getPercentile((*pMemBucket), v, &ppInfo->result);
188✔
1930
        if (code != TSDB_CODE_SUCCESS) {
188!
1931
          goto _fin_error;
×
1932
        }
1933

1934
        if (i == pCtx->numOfParams - 1) {
188✔
1935
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf]", ppInfo->result);
22✔
1936
        } else {
1937
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf, ", ppInfo->result);
166✔
1938
        }
1939
      }
1940

1941
      int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
22✔
1942
      SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
22✔
1943
      if (NULL == pCol) {
22!
1944
        code = terrno;
×
1945
        goto _fin_error;
×
1946
      }
1947

1948
      varDataSetLen(buf, len);
22✔
1949
      code = colDataSetVal(pCol, pBlock->info.rows, buf, false);
22✔
1950
      if (code != TSDB_CODE_SUCCESS) {
22!
1951
        goto _fin_error;
×
1952
      }
1953

1954
      tMemBucketDestroy(pMemBucket);
22✔
1955
      return TSDB_CODE_SUCCESS;
22✔
1956
    } else {
1957
      SVariant* pVal = &pCtx->param[1].param;
3,265✔
1958

1959
      GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
3,265!
1960

1961
      code = getPercentile((*pMemBucket), v, &ppInfo->result);
3,265✔
1962
      if (code != TSDB_CODE_SUCCESS) {
3,265!
1963
        goto _fin_error;
×
1964
      }
1965

1966
      tMemBucketDestroy(pMemBucket);
3,265✔
1967
      return functionFinalize(pCtx, pBlock);
3,265✔
1968
    }
1969
  } else {
1970
    return functionFinalize(pCtx, pBlock);
2,736✔
1971
  }
1972

1973
_fin_error:
×
1974

1975
  tMemBucketDestroy(pMemBucket);
×
1976
  return code;
×
1977
}
1978

1979
bool getApercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
72,683✔
1980
  int32_t bytesHist =
72,683✔
1981
      (int32_t)(sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
1982
  int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION));
72,683✔
1983
  pEnv->calcMemSize = TMAX(bytesHist, bytesDigest);
72,683✔
1984
  return true;
72,683✔
1985
}
1986

1987
int32_t getApercentileMaxSize() {
9,686✔
1988
  int32_t bytesHist =
9,686✔
1989
      (int32_t)(sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
1990
  int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION));
9,686✔
1991
  return TMAX(bytesHist, bytesDigest);
9,686✔
1992
}
1993

1994
static int8_t getApercentileAlgo(char* algoStr) {
22,674✔
1995
  int8_t algoType;
1996
  if (strcasecmp(algoStr, "default") == 0) {
22,674✔
1997
    algoType = APERCT_ALGO_DEFAULT;
11,151✔
1998
  } else if (strcasecmp(algoStr, "t-digest") == 0) {
11,523✔
1999
    algoType = APERCT_ALGO_TDIGEST;
11,522✔
2000
  } else {
2001
    algoType = APERCT_ALGO_UNKNOWN;
1✔
2002
  }
2003

2004
  return algoType;
22,674✔
2005
}
2006

2007
static void buildHistogramInfo(SAPercentileInfo* pInfo) {
919,176✔
2008
  pInfo->pHisto = (SHistogramInfo*)((char*)pInfo + sizeof(SAPercentileInfo));
919,176✔
2009
  pInfo->pHisto->elems = (SHistBin*)((char*)pInfo->pHisto + sizeof(SHistogramInfo));
919,176✔
2010
}
919,176✔
2011

2012
static void buildTDigestInfo(SAPercentileInfo* pInfo) {
23,120✔
2013
  pInfo->pTDigest = (TDigest*)((char*)pInfo + sizeof(SAPercentileInfo));
23,120✔
2014
}
23,120✔
2015

2016
int32_t apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
320,850✔
2017
  if (pResultInfo->initialized) {
320,850!
2018
    return TSDB_CODE_SUCCESS;
×
2019
  }
2020
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
320,850!
2021
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
2022
  }
2023

2024
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
320,876✔
2025

2026
  SVariant* pVal = &pCtx->param[1].param;
320,876✔
2027
  pInfo->percent = 0;
320,876✔
2028
  GET_TYPED_DATA(pInfo->percent, double, pVal->nType, &pVal->i);
320,876!
2029

2030
  if (pCtx->numOfParams == 2) {
320,876✔
2031
    pInfo->algo = APERCT_ALGO_DEFAULT;
298,201✔
2032
  } else if (pCtx->numOfParams == 3) {
22,675!
2033
    pInfo->algo = getApercentileAlgo(varDataVal(pCtx->param[2].param.pz));
22,675✔
2034
    if (pInfo->algo == APERCT_ALGO_UNKNOWN) {
22,669!
2035
      return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
2036
    }
2037
  }
2038

2039
  char* tmp = (char*)pInfo + sizeof(SAPercentileInfo);
320,870✔
2040
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
320,870✔
2041
    pInfo->pTDigest = tdigestNewFrom(tmp, COMPRESSION);
11,521✔
2042
  } else {
2043
    buildHistogramInfo(pInfo);
309,349✔
2044
    pInfo->pHisto = tHistogramCreateFrom(tmp, MAX_HISTOGRAM_BIN);
309,354✔
2045
    qDebug("%s set up histogram, numOfElems:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
309,349✔
2046
           pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2047
  }
2048

2049
  return TSDB_CODE_SUCCESS;
320,871✔
2050
}
2051

2052
int32_t apercentileFunction(SqlFunctionCtx* pCtx) {
327,786✔
2053
  int32_t               numOfElems = 0;
327,786✔
2054
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
327,786✔
2055
  SInputColumnInfoData* pInput = &pCtx->input;
327,786✔
2056

2057
  SColumnInfoData* pCol = pInput->pData[0];
327,786✔
2058
  int32_t          type = pCol->info.type;
327,786✔
2059

2060
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
327,786✔
2061

2062
  int32_t start = pInput->startRowIndex;
327,786✔
2063
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
327,786✔
2064
    buildTDigestInfo(pInfo);
11,522✔
2065
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,521✔
2066
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
689,677✔
2067
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
678,164✔
2068
        continue;
284,046✔
2069
      }
2070
      numOfElems += 1;
394,118✔
2071
      char* data = colDataGetData(pCol, i);
394,118!
2072

2073
      double  v = 0;  // value
394,118✔
2074
      int64_t w = 1;  // weigth
394,118✔
2075
      GET_TYPED_DATA(v, double, type, data);
394,118!
2076
      int32_t code = tdigestAdd(pInfo->pTDigest, v, w);
394,118✔
2077
      if (code != TSDB_CODE_SUCCESS) {
394,112!
2078
        return code;
×
2079
      }
2080
    }
2081
  } else {
2082
    // might be a race condition here that pHisto can be overwritten or setup function
2083
    // has not been called, need to relink the buffer pHisto points to.
2084
    buildHistogramInfo(pInfo);
316,264✔
2085
    qDebug("%s before add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
316,263✔
2086
           __FUNCTION__, numOfElems, pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto,
2087
           pInfo->pHisto->elems);
2088
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
4,956,656✔
2089
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
4,640,389✔
2090
        continue;
831,623✔
2091
      }
2092
      numOfElems += 1;
3,808,766✔
2093
      char* data = colDataGetData(pCol, i);
3,808,766!
2094

2095
      double v = 0;
3,808,766✔
2096
      GET_TYPED_DATA(v, double, type, data);
3,808,766!
2097
      int32_t code = tHistogramAdd(&pInfo->pHisto, v);
3,808,766✔
2098
      if (code != TSDB_CODE_SUCCESS) {
3,808,763!
2099
        return code;
×
2100
      }
2101
    }
2102

2103
    qDebug("%s after add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
316,267✔
2104
           __FUNCTION__, numOfElems, pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto,
2105
           pInfo->pHisto->elems);
2106
  }
2107

2108
  SET_VAL(pResInfo, numOfElems, 1);
327,803✔
2109
  return TSDB_CODE_SUCCESS;
327,803✔
2110
}
2111

2112
static int32_t apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) {
8,340✔
2113
  pOutput->percent = pInput->percent;
8,340✔
2114
  pOutput->algo = pInput->algo;
8,340✔
2115
  if (pOutput->algo == APERCT_ALGO_TDIGEST) {
8,340✔
2116
    buildTDigestInfo(pInput);
93✔
2117
    tdigestAutoFill(pInput->pTDigest, COMPRESSION);
93✔
2118

2119
    if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) {
93✔
2120
      return TSDB_CODE_SUCCESS;
1✔
2121
    }
2122

2123
    if (hasRes) {
92✔
2124
      *hasRes = true;
90✔
2125
    }
2126

2127
    buildTDigestInfo(pOutput);
92✔
2128
    TDigest* pTDigest = pOutput->pTDigest;
92✔
2129
    tdigestAutoFill(pTDigest, COMPRESSION);
92✔
2130

2131
    if (pTDigest->num_centroids <= 0 && pTDigest->num_buffered_pts == 0) {
92!
2132
      (void)memcpy(pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION));
90✔
2133
      tdigestAutoFill(pTDigest, COMPRESSION);
90✔
2134
    } else {
2135
      int32_t code = tdigestMerge(pTDigest, pInput->pTDigest);
2✔
2136
      if (TSDB_CODE_SUCCESS != code) {
2!
2137
        return code;
×
2138
      }
2139
    }
2140
  } else {
2141
    buildHistogramInfo(pInput);
8,247✔
2142
    if (pInput->pHisto->numOfElems <= 0) {
8,247✔
2143
      return TSDB_CODE_SUCCESS;
218✔
2144
    }
2145

2146
    if (hasRes) {
8,029✔
2147
      *hasRes = true;
8,027✔
2148
    }
2149

2150
    buildHistogramInfo(pOutput);
8,029✔
2151
    SHistogramInfo* pHisto = pOutput->pHisto;
8,029✔
2152

2153
    if (pHisto->numOfElems <= 0) {
8,029✔
2154
      (void)memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
7,590✔
2155
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
7,590✔
2156

2157
      qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries,
7,590✔
2158
             pHisto);
2159
    } else {
2160
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
439✔
2161
      qDebug("%s input histogram, elem:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems,
439✔
2162
             pHisto->numOfEntries, pInput->pHisto);
2163

2164
      SHistogramInfo* pRes = NULL;
439✔
2165
      int32_t         code = tHistogramMerge(pHisto, pInput->pHisto, MAX_HISTOGRAM_BIN, &pRes);
439✔
2166
      if (TSDB_CODE_SUCCESS != code) {
439!
2167
        tHistogramDestroy(&pRes);
×
2168
        return code;
×
2169
      }
2170
      (void)memcpy(pHisto, pRes, sizeof(SHistogramInfo) + sizeof(SHistBin) * MAX_HISTOGRAM_BIN);
439✔
2171
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
439✔
2172

2173
      qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries,
439✔
2174
             pHisto);
2175
      tHistogramDestroy(&pRes);
439✔
2176
    }
2177
  }
2178
  return TSDB_CODE_SUCCESS;
8,121✔
2179
}
2180

2181
int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) {
8,270✔
2182
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,270✔
2183

2184
  SInputColumnInfoData* pInput = &pCtx->input;
8,270✔
2185

2186
  SColumnInfoData* pCol = pInput->pData[0];
8,270✔
2187
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
8,270!
2188
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2189
  }
2190

2191
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
8,270✔
2192

2193
  qDebug("%s total %" PRId64 " rows will merge, %p", __FUNCTION__, pInput->numOfRows, pInfo->pHisto);
8,270✔
2194

2195
  bool    hasRes = false;
8,270✔
2196
  int32_t start = pInput->startRowIndex;
8,270✔
2197
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
16,606✔
2198
    char* data = colDataGetData(pCol, i);
8,336!
2199

2200
    SAPercentileInfo* pInputInfo = (SAPercentileInfo*)varDataVal(data);
8,336✔
2201
    int32_t           code = apercentileTransferInfo(pInputInfo, pInfo, &hasRes);
8,336✔
2202
    if (TSDB_CODE_SUCCESS != code) {
8,336!
2203
      return code;
×
2204
    }
2205
  }
2206

2207
  if (pInfo->algo != APERCT_ALGO_TDIGEST) {
8,270✔
2208
    buildHistogramInfo(pInfo);
8,179✔
2209
    qDebug("%s after merge, total:%" PRId64 ", numOfEntry:%d, %p", __FUNCTION__, pInfo->pHisto->numOfElems,
8,179✔
2210
           pInfo->pHisto->numOfEntries, pInfo->pHisto);
2211
  }
2212

2213
  SET_VAL(pResInfo, hasRes ? 1 : 0, 1);
8,270✔
2214
  return TSDB_CODE_SUCCESS;
8,270✔
2215
}
2216

2217
int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
280,621✔
2218
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
280,621✔
2219
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
280,621✔
2220

2221
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
280,621✔
2222
    buildTDigestInfo(pInfo);
11,424✔
2223
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,424✔
2224
    if (pInfo->pTDigest->size > 0) {
11,427!
2225
      pInfo->result = tdigestQuantile(pInfo->pTDigest, pInfo->percent / 100);
11,427✔
2226
    } else {  // no need to free
2227
      // setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
2228
      return TSDB_CODE_SUCCESS;
×
2229
    }
2230
  } else {
2231
    buildHistogramInfo(pInfo);
269,197✔
2232
    if (pInfo->pHisto->numOfElems > 0) {
269,199✔
2233
      qDebug("%s get the final res, elements:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
214,248✔
2234
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2235

2236
      double  ratio[] = {pInfo->percent};
214,248✔
2237
      double* res = NULL;
214,248✔
2238
      int32_t code = tHistogramUniform(pInfo->pHisto, ratio, 1, &res);
214,248✔
2239
      if (TSDB_CODE_SUCCESS != code) {
214,255!
2240
        taosMemoryFree(res);
×
2241
        return code;
×
2242
      }
2243
      pInfo->result = *res;
214,255✔
2244
      // memcpy(pCtx->pOutput, res, sizeof(double));
2245
      taosMemoryFree(res);
214,255✔
2246
    } else {  // no need to free
2247
      // setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
2248
      // return TSDB_CODE_SUCCESS;
2249
      qDebug("%s get the final res, elements:%" PRId64 ", numOfEntry:%d. result is null", __FUNCTION__,
54,951✔
2250
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries);
2251
    }
2252
  }
2253

2254
  return functionFinalize(pCtx, pBlock);
280,631✔
2255
}
2256

2257
int32_t apercentilePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
8,010✔
2258
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,010✔
2259
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
8,010✔
2260

2261
  int32_t resultBytes = getApercentileMaxSize();
8,010✔
2262
  char*   res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
8,010!
2263
  if (NULL == res) {
8,010!
2264
    return terrno;
×
2265
  }
2266

2267
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
8,010✔
2268
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
91✔
2269
    varDataSetLen(res, resultBytes);
91✔
2270
  } else {
2271
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
7,919✔
2272
    varDataSetLen(res, resultBytes);
7,919✔
2273
  }
2274

2275
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
8,010✔
2276
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
8,010✔
2277
  if (NULL == pCol) {
8,010!
2278
    taosMemoryFree(res);
×
2279
    return TSDB_CODE_OUT_OF_RANGE;
×
2280
  }
2281

2282
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
8,010✔
2283

2284
  taosMemoryFree(res);
8,010!
2285
  return code;
8,010✔
2286
}
2287

2288
int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
4✔
2289
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
4✔
2290
  SAPercentileInfo*    pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
4✔
2291

2292
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
4✔
2293
  SAPercentileInfo*    pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
4✔
2294

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

2297
  int32_t code = apercentileTransferInfo(pSBuf, pDBuf, NULL);
4✔
2298
  if (TSDB_CODE_SUCCESS != code) {
4!
2299
    return code;
×
2300
  }
2301
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
4✔
2302
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
4✔
2303
  return TSDB_CODE_SUCCESS;
4✔
2304
}
2305

2306
// TODO: change this function when block data info pks changed
2307
static int32_t comparePkDataWithSValue(int8_t pkType, char* pkData, SValue* pVal, int32_t order) {
11✔
2308
  char numVal[8] = {0};
11✔
2309
  switch (pkType) {
11!
2310
    case TSDB_DATA_TYPE_INT:
11✔
2311
      *(int32_t*)numVal = (int32_t)pVal->val;
11✔
2312
      break;
11✔
2313
    case TSDB_DATA_TYPE_UINT:
×
2314
      *(uint32_t*)numVal = (uint32_t)pVal->val;
×
2315
      break;
×
2316
    case TSDB_DATA_TYPE_BIGINT:
×
2317
      *(int64_t*)numVal = (int64_t)pVal->val;
×
2318
      break;
×
2319
    case TSDB_DATA_TYPE_UBIGINT:
×
2320
      *(uint64_t*)numVal = (uint64_t)pVal->val;
×
2321
      break;
×
2322
    default:
×
2323
      break;
×
2324
  }
2325
  char*         blockData = (IS_NUMERIC_TYPE(pkType)) ? (char*)numVal : (char*)pVal->pData;
11!
2326
  __compar_fn_t fn = getKeyComparFunc(pkType, order);
11✔
2327
  return fn(pkData, blockData);
11✔
2328
}
2329

2330
EFuncDataRequired firstDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
3,732✔
2331
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
3,732✔
2332

2333
  // not initialized yet, data is required
2334
  if (pEntry == NULL) {
3,732!
2335
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2336
  }
2337

2338
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
3,732✔
2339
  if (pResult->hasResult) {
3,732✔
2340
    if (pResult->pkBytes > 0) {
3,678✔
2341
      pResult->pkData = pResult->buf + pResult->bytes;
6✔
2342
    } else {
2343
      pResult->pkData = NULL;
3,672✔
2344
    }
2345
    if (pResult->ts < pBlockInfo->window.skey) {
3,678✔
2346
      return FUNC_DATA_REQUIRED_NOT_LOAD;
2,442✔
2347
    } else if (pResult->ts == pBlockInfo->window.skey) {
1,236✔
2348
      if (NULL == pResult->pkData) {
248✔
2349
        return FUNC_DATA_REQUIRED_NOT_LOAD;
242✔
2350
      }
2351
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 0, TSDB_ORDER_ASC) < 0) {
6!
2352
        return FUNC_DATA_REQUIRED_NOT_LOAD;
6✔
2353
      }
2354
    }
2355
    return FUNC_DATA_REQUIRED_DATA_LOAD;
988✔
2356
  } else {
2357
    return FUNC_DATA_REQUIRED_DATA_LOAD;
54✔
2358
  }
2359
}
2360

2361
EFuncDataRequired lastDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
11,862✔
2362
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
11,862✔
2363

2364
  // not initialized yet, data is required
2365
  if (pEntry == NULL) {
11,862!
2366
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2367
  }
2368

2369
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
11,862✔
2370
  if (pResult->hasResult) {
11,862✔
2371
    if (pResult->pkBytes > 0) {
11,797✔
2372
      pResult->pkData = pResult->buf + pResult->bytes;
5✔
2373
    } else {
2374
      pResult->pkData = NULL;
11,792✔
2375
    }
2376
    if (pResult->ts > pBlockInfo->window.ekey) {
11,797✔
2377
      return FUNC_DATA_REQUIRED_NOT_LOAD;
9,525✔
2378
    } else if (pResult->ts == pBlockInfo->window.ekey && pResult->pkData) {
2,272✔
2379
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 1, TSDB_ORDER_DESC) < 0) {
5!
2380
        return FUNC_DATA_REQUIRED_NOT_LOAD;
×
2381
      }
2382
    }
2383
    return FUNC_DATA_REQUIRED_DATA_LOAD;
2,272✔
2384
  } else {
2385
    return FUNC_DATA_REQUIRED_DATA_LOAD;
65✔
2386
  }
2387
}
2388

2389
// TODO modify it to include primary key bytes
2390
int32_t getFirstLastInfoSize(int32_t resBytes, int32_t pkBytes) { return sizeof(SFirstLastRes) + resBytes + pkBytes; }
74,601,412✔
2391

2392
bool getFirstLastFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
1,791,359✔
2393
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
1,791,359✔
2394
  // TODO: change SFunctionNode to add pk info
2395
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
1,793,285✔
2396
  pEnv->calcMemSize = getFirstLastInfoSize(pNode->node.resType.bytes, pkBytes);
1,793,285✔
2397
  return true;
1,792,298✔
2398
}
2399

2400
bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
1,220,946✔
2401
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
1,220,946✔
2402
  pEnv->calcMemSize = pNode->node.resType.bytes;
1,222,108✔
2403
  return true;
1,222,108✔
2404
}
2405

2406
bool getGroupKeyFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
251,758✔
2407
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
251,758✔
2408
  pEnv->calcMemSize = sizeof(SGroupKeyInfo) + pNode->node.resType.bytes;
252,077✔
2409
  return true;
252,077✔
2410
}
2411

2412
static FORCE_INLINE TSKEY getRowPTs(SColumnInfoData* pTsColInfo, int32_t rowIndex) {
2413
  if (pTsColInfo == NULL || pTsColInfo->pData == NULL) {
348,685,577!
2414
    return 0;
×
2415
  }
2416

2417
  return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex);
348,747,477!
2418
}
2419

2420
int32_t firstLastFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
28,218,563✔
2421
  if (pResInfo->initialized) {
28,218,563!
2422
    return TSDB_CODE_SUCCESS;
×
2423
  }
2424
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
28,218,563!
2425
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
2426
  }
2427

2428
  SFirstLastRes*        pRes = GET_ROWCELL_INTERBUF(pResInfo);
28,220,169✔
2429
  pRes->nullTupleSaved = false;
28,220,169✔
2430
  pRes->nullTuplePos.pageId = -1;
28,220,169✔
2431
  return TSDB_CODE_SUCCESS;
28,220,169✔
2432
}
2433

2434
static int32_t prepareBuf(SqlFunctionCtx* pCtx) {
233,456,412✔
2435
  if (pCtx->subsidiaries.rowLen == 0) {
233,456,412✔
2436
    int32_t rowLen = 0;
623,304✔
2437
    for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
1,252,642✔
2438
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
629,338✔
2439
      rowLen += pc->pExpr->base.resSchema.bytes;
629,338✔
2440
    }
2441

2442
    pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool);
623,304✔
2443
    pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen);
623,304!
2444
    if (NULL == pCtx->subsidiaries.buf) {
620,041!
2445
      return terrno;
×
2446
    }
2447
  }
2448
  return TSDB_CODE_SUCCESS;
233,453,149✔
2449
}
2450

2451
static int32_t firstlastSaveTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SqlFunctionCtx* pCtx,
225,940,172✔
2452
                                      SFirstLastRes* pInfo, bool noElements) {
2453
  int32_t code = TSDB_CODE_SUCCESS;
225,940,172✔
2454

2455
  if (pCtx->subsidiaries.num <= 0) {
225,940,172✔
2456
    return TSDB_CODE_SUCCESS;
112,930,031✔
2457
  }
2458

2459
  if (!pInfo->hasResult) {
113,010,141✔
2460
    code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
90,523,437✔
2461
  } else if (!noElements) {
22,486,704!
2462
    code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
22,552,206✔
2463
  } else { } // dothing
2464

2465
  return code;
112,867,652✔
2466
}
2467

2468
static int32_t doSaveCurrentVal(SqlFunctionCtx* pCtx, int32_t rowIndex, int64_t currentTs, char* pkData, int32_t type,
109,831,233✔
2469
                                char* pData) {
2470
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
109,831,233✔
2471
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
109,831,233✔
2472

2473
  if (IS_VAR_DATA_TYPE(type)) {
109,831,233!
2474
    if (type == TSDB_DATA_TYPE_JSON) {
30,081,027!
2475
      pInfo->bytes = getJsonValueLen(pData);
×
2476
    } else {
2477
      pInfo->bytes = varDataTLen(pData);
30,081,027✔
2478
    }
2479
  }
2480

2481
  (void)memcpy(pInfo->buf, pData, pInfo->bytes);
109,831,233✔
2482
  if (pkData != NULL) {
109,831,233✔
2483
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
30,118!
2484
      if (pInfo->pkType == TSDB_DATA_TYPE_JSON) {
24!
2485
        pInfo->pkBytes = getJsonValueLen(pkData);
×
2486
      } else {
2487
        pInfo->pkBytes = varDataTLen(pkData);
24✔
2488
      }
2489
    }
2490
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
30,118✔
2491
    pInfo->pkData = pInfo->buf + pInfo->bytes;
30,118✔
2492
  }
2493

2494
  pInfo->ts = currentTs;
109,831,233✔
2495
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
109,831,233✔
2496
  if (code != TSDB_CODE_SUCCESS) {
109,822,310!
2497
    return code;
×
2498
  }
2499

2500
  pInfo->hasResult = true;
109,822,310✔
2501
  return TSDB_CODE_SUCCESS;
109,822,310✔
2502
}
2503

2504
// This ordinary first function does not care if current scan is ascending order or descending order scan
2505
// the OPTIMIZED version of first function will only handle the ascending order scan
2506
int32_t firstFunction(SqlFunctionCtx* pCtx) {
55,773,641✔
2507
  int32_t numOfElems = 0;
55,773,641✔
2508

2509
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
55,773,641✔
2510
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
55,773,641✔
2511

2512
  SInputColumnInfoData* pInput = &pCtx->input;
55,773,641✔
2513
  SColumnInfoData*      pInputCol = pInput->pData[0];
55,773,641✔
2514

2515
  pInfo->bytes = pInputCol->info.bytes;
55,773,641✔
2516

2517
  if (IS_NULL_TYPE(pInputCol->info.type)) {
55,773,641✔
2518
    return TSDB_CODE_SUCCESS;
5,105✔
2519
  }
2520

2521
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
55,768,536✔
2522
  pInfo->pkType = -1;
55,768,536✔
2523
  __compar_fn_t pkCompareFn = NULL;
55,768,536✔
2524
  if (pCtx->hasPrimaryKey) {
55,768,536✔
2525
    pInfo->pkType = pkCol->info.type;
51✔
2526
    pInfo->pkBytes = pkCol->info.bytes;
51✔
2527
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_ASC);
51✔
2528
  }
2529

2530
  // All null data column, return directly.
2531
  if (pInput->colDataSMAIsSet && (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows) &&
55,865,790!
2532
      pInputCol->hasNull == true) {
×
2533
    // save selectivity value for column consisted of all null values
2534
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
×
2535
    if (code != TSDB_CODE_SUCCESS) {
×
2536
      return code;
×
2537
    }
2538
    pInfo->nullTupleSaved = true;
×
2539
    return TSDB_CODE_SUCCESS;
×
2540
  }
2541

2542
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
55,865,790!
2543

2544
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
55,865,790!
2545
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
55,865,790!
2546

2547
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
55,865,790✔
2548

2549
  //  please ref. to the comment in lastRowFunction for the reason why disabling the opt version of last/first
2550
  //  function. we will use this opt implementation in an new version that is only available in scan subplan
2551
#if 0
2552
  if (blockDataOrder == TSDB_ORDER_ASC) {
2553
    // filter according to current result firstly
2554
    if (pResInfo->numOfRes > 0) {
2555
      if (pInfo->ts < startKey) {
2556
        return TSDB_CODE_SUCCESS;
2557
      }
2558
    }
2559

2560
    for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
2561
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2562
        continue;
2563
      }
2564

2565
      numOfElems++;
2566

2567
      char* data = colDataGetData(pInputCol, i);
2568
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2569
      if (pResInfo->numOfRes == 0 || pInfo->ts > cts) {
2570
        doSaveCurrentVal(pCtx, i, cts, pInputCol->info.type, data);
2571
        break;
2572
      }
2573
    }
2574
  } else {
2575
    // in case of descending order time stamp serial, which usually happens as the results of the nest query,
2576
    // all data needs to be check.
2577
    if (pResInfo->numOfRes > 0) {
2578
      if (pInfo->ts < endKey) {
2579
        return TSDB_CODE_SUCCESS;
2580
      }
2581
    }
2582

2583
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
2584
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2585
        continue;
2586
      }
2587

2588
      numOfElems++;
2589

2590
      char* data = colDataGetData(pInputCol, i);
2591
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2592

2593
      if (pResInfo->numOfRes == 0 || pInfo->ts > cts) {
2594
        doSaveCurrentVal(pCtx, i, cts, pInputCol->info.type, data);
2595
        break;
2596
      }
2597
    }
2598
  }
2599
#else
2600
  int64_t* pts = (int64_t*)pInput->pPTS->pData;
55,865,790✔
2601

2602
  int     from = -1;
55,865,790✔
2603
  int32_t i = -1;
55,865,790✔
2604
  while (funcInputGetNextRowIndex(pInput, from, true, &i, &from)) {
175,190,016✔
2605
    if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
159,571,942!
2606
      continue;
71,522✔
2607
    }
2608

2609
    numOfElems++;
119,287,297✔
2610
    char* data = colDataGetData(pInputCol, i);
119,287,297!
2611
    char* pkData = NULL;
119,287,297✔
2612
    if (pCtx->hasPrimaryKey) {
119,287,297✔
2613
      pkData = colDataGetData(pkCol, i);
153!
2614
    }
2615
    TSKEY cts = pts[i];
119,287,297✔
2616
    if (pResInfo->numOfRes == 0 || pInfo->ts > cts ||
119,287,297✔
2617
        (pInfo->ts == cts && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
71,167,451!
2618
      int32_t code = doSaveCurrentVal(pCtx, i, cts, pkData, pInputCol->info.type, data);
48,119,846✔
2619
      if (code != TSDB_CODE_SUCCESS) {
48,085,253!
2620
        return code;
×
2621
      }
2622
      pResInfo->numOfRes = 1;
48,085,253✔
2623
    }
2624
  }
2625
#endif
2626

2627
  if (numOfElems == 0) {
55,561,604✔
2628
    // save selectivity value for column consisted of all null values
2629
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
25,478✔
2630
    if (code != TSDB_CODE_SUCCESS) {
25,476!
2631
      return code;
×
2632
    }
2633
    pInfo->nullTupleSaved = true;
25,476✔
2634
  }
2635
  SET_VAL(pResInfo, numOfElems, 1);
55,561,602✔
2636
  return TSDB_CODE_SUCCESS;
55,561,602✔
2637
}
2638

2639
int32_t lastFunction(SqlFunctionCtx* pCtx) {
50,187,924✔
2640
  int32_t numOfElems = 0;
50,187,924✔
2641

2642
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
50,187,924✔
2643
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
50,187,924✔
2644

2645
  SInputColumnInfoData* pInput = &pCtx->input;
50,187,924✔
2646
  SColumnInfoData*      pInputCol = pInput->pData[0];
50,187,924✔
2647

2648
  int32_t type = pInputCol->info.type;
50,187,924✔
2649
  int32_t bytes = pInputCol->info.bytes;
50,187,924✔
2650

2651
  if (IS_NULL_TYPE(type)) {
50,187,924✔
2652
    return TSDB_CODE_SUCCESS;
5,110✔
2653
  }
2654
  pInfo->bytes = bytes;
50,182,814✔
2655

2656
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
50,182,814✔
2657
  pInfo->pkType = -1;
50,182,814✔
2658
  __compar_fn_t pkCompareFn = NULL;
50,182,814✔
2659
  if (pCtx->hasPrimaryKey) {
50,182,814✔
2660
    pInfo->pkType = pkCol->info.type;
30,067✔
2661
    pInfo->pkBytes = pkCol->info.bytes;
30,067✔
2662
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
30,067✔
2663
  }
2664

2665
  // All null data column, return directly.
2666
  if (pInput->colDataSMAIsSet && (pInput->pColumnDataAgg[0]->numOfNull == pInput->totalRows) &&
50,270,817!
2667
      pInputCol->hasNull == true) {
×
2668
    // save selectivity value for column consisted of all null values
2669
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
×
2670
    if (code != TSDB_CODE_SUCCESS) {
×
2671
      return code;
×
2672
    }
2673
    pInfo->nullTupleSaved = true;
×
2674
    return TSDB_CODE_SUCCESS;
×
2675
  }
2676

2677
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
50,270,817!
2678

2679
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
50,270,817!
2680
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
50,270,817!
2681

2682
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
50,270,817✔
2683

2684
  //  please ref. to the comment in lastRowFunction for the reason why disabling the opt version of last/first function.
2685
#if 0
2686
  if (blockDataOrder == TSDB_ORDER_ASC) {
2687
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
2688
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2689
        continue;
2690
      }
2691

2692
      numOfElems++;
2693

2694
      char* data = colDataGetData(pInputCol, i);
2695
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2696
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2697
        doSaveCurrentVal(pCtx, i, cts, type, data);
2698
      }
2699

2700
      break;
2701
    }
2702
  } else {  // descending order
2703
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2704
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2705
        continue;
2706
      }
2707

2708
      numOfElems++;
2709

2710
      char* data = colDataGetData(pInputCol, i);
2711
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2712
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2713
        doSaveCurrentVal(pCtx, i, cts, type, data);
2714
      }
2715
      break;
2716
    }
2717
  }
2718
#else
2719
  int64_t* pts = (int64_t*)pInput->pPTS->pData;
50,270,817✔
2720

2721
#if 0
2722
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2723
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2724
        continue;
2725
      }
2726

2727
      numOfElems++;
2728
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
2729
        char* data = colDataGetData(pInputCol, i);
2730
        doSaveCurrentVal(pCtx, i, pts[i], type, data);
2731
        pResInfo->numOfRes = 1;
2732
      }
2733
    }
2734
#else
2735

2736
  // todo refactor
2737
  if (!pInputCol->hasNull && !pCtx->hasPrimaryKey) {
82,616,979✔
2738
    numOfElems = 1;
32,403,522✔
2739

2740
    int32_t round = pInput->numOfRows >> 2;
32,403,522✔
2741
    int32_t reminder = pInput->numOfRows & 0x03;
32,403,522✔
2742

2743
    for (int32_t i = pInput->startRowIndex, tick = 0; tick < round; i += 4, tick += 1) {
50,243,573✔
2744
      int64_t cts = pts[i];
17,837,755✔
2745
      int32_t chosen = i;
17,837,755✔
2746

2747
      if (cts < pts[i + 1]) {
17,837,755✔
2748
        cts = pts[i + 1];
4,873,049✔
2749
        chosen = i + 1;
4,873,049✔
2750
      }
2751

2752
      if (cts < pts[i + 2]) {
17,837,755✔
2753
        cts = pts[i + 2];
4,873,509✔
2754
        chosen = i + 2;
4,873,509✔
2755
      }
2756

2757
      if (cts < pts[i + 3]) {
17,837,755✔
2758
        cts = pts[i + 3];
4,872,930✔
2759
        chosen = i + 3;
4,872,930✔
2760
      }
2761

2762
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
17,837,755✔
2763
        char*   data = colDataGetData(pInputCol, chosen);
4,943,325!
2764
        int32_t code = doSaveCurrentVal(pCtx, chosen, cts, NULL, type, data);
4,943,325✔
2765
        if (code != TSDB_CODE_SUCCESS) {
4,945,621!
2766
          return code;
×
2767
        }
2768
        pResInfo->numOfRes = 1;
4,945,621✔
2769
      }
2770
    }
2771

2772
    for (int32_t i = pInput->startRowIndex + round * 4; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
70,610,422✔
2773
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
38,264,260✔
2774
        char*   data = colDataGetData(pInputCol, i);
23,992,935!
2775
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], NULL, type, data);
23,992,935✔
2776
        if (code != TSDB_CODE_SUCCESS) {
23,933,279!
2777
          return code;
×
2778
        }
2779
        pResInfo->numOfRes = 1;
23,933,279✔
2780
      }
2781
    }
2782
  } else {
2783
    int     from = -1;
17,867,295✔
2784
    int32_t i = -1;
17,867,295✔
2785
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
165,611,988✔
2786
      if (colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
295,509,712✔
2787
        continue;
5,576,691✔
2788
      }
2789

2790
      numOfElems++;
142,178,165✔
2791
      char* pkData = NULL;
142,178,165✔
2792
      if (pCtx->hasPrimaryKey) {
142,178,165✔
2793
        pkData = colDataGetData(pkCol, i);
100,000,193!
2794
      }
2795
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i] ||
142,178,165✔
2796
          (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
109,443,157!
2797
        char*   data = colDataGetData(pInputCol, i);
32,793,464!
2798
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], pkData, type, data);
32,793,464✔
2799
        if (code != TSDB_CODE_SUCCESS) {
32,783,301!
2800
          return code;
×
2801
        }
2802
        pResInfo->numOfRes = 1;
32,783,301✔
2803
      }
2804
    }
2805
  }
2806
#endif
2807

2808
#endif
2809

2810
  // save selectivity value for column consisted of all null values
2811
  if (numOfElems == 0) {
50,472,064✔
2812
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
4,091,925✔
2813
    if (code != TSDB_CODE_SUCCESS) {
4,088,201!
2814
      return code;
×
2815
    }
2816
    pInfo->nullTupleSaved = true;
4,088,201✔
2817
  }
2818

2819
  return TSDB_CODE_SUCCESS;
50,468,340✔
2820
}
2821

2822
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) {
68,658,730✔
2823
  if (!pInput->hasResult) {
68,658,730✔
2824
    return false;
2✔
2825
  }
2826
  __compar_fn_t pkCompareFn = NULL;
68,658,728✔
2827
  if (pInput->pkData) {
68,658,728✔
2828
    pkCompareFn = getKeyComparFunc(pInput->pkType, (isFirst) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC);
52✔
2829
  }
2830
  if (pOutput->hasResult) {
68,661,330✔
2831
    if (isFirst) {
15,040,752✔
2832
      if (pInput->ts > pOutput->ts ||
8,571,782✔
2833
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
8,571,172!
2834
        return false;
610✔
2835
      }
2836
    } else {
2837
      if (pInput->ts < pOutput->ts ||
6,468,970✔
2838
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
4,472,393!
2839
        return false;
1,996,577✔
2840
      }
2841
    }
2842
  }
2843

2844
  pOutput->isNull = pInput->isNull;
66,664,143✔
2845
  pOutput->ts = pInput->ts;
66,664,143✔
2846
  pOutput->bytes = pInput->bytes;
66,664,143✔
2847
  pOutput->pkType = pInput->pkType;
66,664,143✔
2848

2849
  (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes);
66,664,143✔
2850
  if (pInput->pkData) {
66,664,143✔
2851
    pOutput->pkBytes = pInput->pkBytes;
47✔
2852
    (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes);
47✔
2853
    pOutput->pkData = pOutput->buf + pOutput->bytes;
47✔
2854
  }
2855
  return true;
66,664,143✔
2856
}
2857

2858
static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst,
68,659,024✔
2859
                                     int32_t rowIndex) {
2860
  if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) {
68,659,024✔
2861
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput, false);
66,663,151✔
2862
    if (TSDB_CODE_SUCCESS != code) {
66,653,417!
2863
      return code;
×
2864
    }
2865
    pOutput->hasResult = true;
66,653,417✔
2866
  }
2867
  return TSDB_CODE_SUCCESS;
68,649,745✔
2868
}
2869

2870
static int32_t firstLastFunctionMergeImpl(SqlFunctionCtx* pCtx, bool isFirstQuery) {
53,629,615✔
2871
  SInputColumnInfoData* pInput = &pCtx->input;
53,629,615✔
2872
  SColumnInfoData*      pCol = pInput->pData[0];
53,629,615✔
2873

2874
  if (IS_NULL_TYPE(pCol->info.type)) {
53,629,615!
2875
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
2876
    return TSDB_CODE_SUCCESS;
×
2877
  }
2878

2879
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
53,629,615!
2880
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2881
  }
2882

2883
  SFirstLastRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
53,629,615✔
2884

2885
  int32_t start = pInput->startRowIndex;
53,629,615✔
2886
  int32_t numOfElems = 0;
53,629,615✔
2887

2888
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
124,564,034✔
2889
    if (colDataIsNull_s(pCol, i)) {
141,895,902✔
2890
      continue;
2,285,287✔
2891
    }
2892
    char*          data = colDataGetData(pCol, i);
68,662,664!
2893
    SFirstLastRes* pInputInfo = (SFirstLastRes*)varDataVal(data);
68,662,664✔
2894
    if (pCtx->hasPrimaryKey) {
68,662,664✔
2895
      pInputInfo->pkData = pInputInfo->buf + pInputInfo->bytes;
52✔
2896
    } else {
2897
      pInputInfo->pkData = NULL;
68,662,612✔
2898
    }
2899

2900
    int32_t code = firstLastTransferInfo(pCtx, pInputInfo, pInfo, isFirstQuery, i);
68,662,664✔
2901
    if (code != TSDB_CODE_SUCCESS) {
68,649,132!
2902
      return code;
×
2903
    }
2904
    if (!numOfElems) {
68,649,132✔
2905
      numOfElems = pInputInfo->hasResult ? 1 : 0;
53,618,332✔
2906
    }
2907
  }
2908

2909
  if (numOfElems == 0) {
53,616,083✔
2910
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
8,095✔
2911
    if (code != TSDB_CODE_SUCCESS) {
8,095!
2912
      return code;
×
2913
    }
2914
    pInfo->nullTupleSaved = true;
8,095✔
2915
  }
2916

2917
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
53,616,083✔
2918
  return TSDB_CODE_SUCCESS;
53,616,083✔
2919
}
2920

2921
int32_t firstFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, true); }
13,335,930✔
2922

2923
int32_t lastFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, false); }
40,300,573✔
2924

2925
int32_t firstLastFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
100,611,290✔
2926
  int32_t          code = TSDB_CODE_SUCCESS;
100,611,290✔
2927
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
100,611,290✔
2928
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
100,611,290✔
2929
  if (NULL == pCol) {
100,560,509!
2930
    return TSDB_CODE_OUT_OF_RANGE;
×
2931
  }
2932

2933
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
100,560,509✔
2934
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
100,560,509✔
2935

2936
  SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo);
100,560,509✔
2937

2938
  if (pResInfo->isNullRes) {
100,560,509✔
2939
    colDataSetNULL(pCol, pBlock->info.rows);
40,867✔
2940
    return setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
40,867✔
2941
  }
2942
  code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes);
100,519,642!
2943
  if (TSDB_CODE_SUCCESS != code) {
100,384,175!
2944
    return code;
×
2945
  }
2946

2947
  // handle selectivity
2948
  code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
100,384,175✔
2949

2950
  return code;
100,419,368✔
2951
}
2952

2953
int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
72,800,007✔
2954
  int32_t code = TSDB_CODE_SUCCESS;
72,800,007✔
2955

2956
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
72,800,007✔
2957
  SFirstLastRes*       pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
72,800,007✔
2958

2959
  int32_t resultBytes = getFirstLastInfoSize(pRes->bytes, pRes->pkBytes);
72,800,007✔
2960

2961
  // todo check for failure
2962
  char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
72,751,558!
2963
  if (NULL == res) {
73,268,992!
2964
    return terrno;
×
2965
  }
2966
  (void)memcpy(varDataVal(res), pRes, resultBytes);
73,268,992✔
2967

2968
  varDataSetLen(res, resultBytes);
73,268,992✔
2969

2970
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
73,268,992✔
2971
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
73,268,992✔
2972
  if (NULL == pCol) {
73,130,532!
2973
    taosMemoryFree(res);
×
2974
    return TSDB_CODE_OUT_OF_RANGE;
×
2975
  }
2976

2977
  if (pEntryInfo->numOfRes == 0) {
73,140,848✔
2978
    colDataSetNULL(pCol, pBlock->info.rows);
2,382,767!
2979
    code = setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
2,382,767✔
2980
  } else {
2981
    code = colDataSetVal(pCol, pBlock->info.rows, res, false);
70,758,081✔
2982
    if (TSDB_CODE_SUCCESS != code) {
70,428,778!
2983
      taosMemoryFree(res);
×
2984
      return code;
×
2985
    }
2986
    code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
70,428,778✔
2987
  }
2988
  taosMemoryFree(res);
72,805,975✔
2989
  return code;
73,212,396✔
2990
}
2991

2992
int32_t lastCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
3✔
2993
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
3✔
2994
  SFirstLastRes*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
3✔
2995
  int32_t              bytes = pDBuf->bytes;
3✔
2996

2997
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
3✔
2998
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
3✔
2999

3000
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, false);
3✔
3001
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
3✔
3002
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
3✔
3003
  return TSDB_CODE_SUCCESS;
3✔
3004
}
3005

3006
static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex, int64_t cts, SFirstLastRes* pInfo) {
45,465,583✔
3007
  SInputColumnInfoData* pInput = &pCtx->input;
45,465,583✔
3008
  SColumnInfoData*      pInputCol = pInput->pData[0];
45,465,583✔
3009
  SColumnInfoData*      pkCol = pInput->pPrimaryKey;
45,465,583✔
3010

3011
  if (colDataIsNull_s(pInputCol, rowIndex)) {
90,931,166✔
3012
    pInfo->isNull = true;
2,212✔
3013
  } else {
3014
    pInfo->isNull = false;
45,463,371✔
3015

3016
    if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
45,463,371!
3017
      if (pInputCol->info.type == TSDB_DATA_TYPE_JSON) {
27,111,702!
3018
        pInfo->bytes = getJsonValueLen(pData);
×
3019
      } else {
3020
        pInfo->bytes = varDataTLen(pData);
27,111,702✔
3021
      }
3022
    }
3023

3024
    (void)memcpy(pInfo->buf, pData, pInfo->bytes);
45,463,371✔
3025
  }
3026

3027
  if (pCtx->hasPrimaryKey && !colDataIsNull_s(pkCol, rowIndex)) {
45,465,656!
3028
    char* pkData = colDataGetData(pkCol, rowIndex);
73!
3029
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
73!
3030
      if (pInfo->pkType == TSDB_DATA_TYPE_JSON) {
12!
3031
        pInfo->pkBytes = getJsonValueLen(pkData);
×
3032
      } else {
3033
        pInfo->pkBytes = varDataTLen(pkData);
12✔
3034
      }
3035
    }
3036
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
73✔
3037
    pInfo->pkData = pInfo->buf + pInfo->bytes;
73✔
3038
  }
3039
  pInfo->ts = cts;
45,465,583✔
3040
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
45,465,583✔
3041
  if (code != TSDB_CODE_SUCCESS) {
45,463,052!
3042
    return code;
×
3043
  }
3044

3045
  pInfo->hasResult = true;
45,463,052✔
3046

3047
  return TSDB_CODE_SUCCESS;
45,463,052✔
3048
}
3049

3050
int32_t lastRowFunction(SqlFunctionCtx* pCtx) {
45,304,175✔
3051
  int32_t numOfElems = 0;
45,304,175✔
3052

3053
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
45,304,175✔
3054
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
45,304,175✔
3055

3056
  SInputColumnInfoData* pInput = &pCtx->input;
45,304,175✔
3057
  SColumnInfoData*      pInputCol = pInput->pData[0];
45,304,175✔
3058

3059
  int32_t type = pInputCol->info.type;
45,304,175✔
3060
  int32_t bytes = pInputCol->info.bytes;
45,304,175✔
3061
  pInfo->bytes = bytes;
45,304,175✔
3062

3063
  if (IS_NULL_TYPE(type)) {
45,304,175✔
3064
    return TSDB_CODE_SUCCESS;
56✔
3065
  }
3066
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
45,304,119✔
3067
  pInfo->pkType = -1;
45,304,119✔
3068
  __compar_fn_t pkCompareFn = NULL;
45,304,119✔
3069
  if (pCtx->hasPrimaryKey) {
45,304,119✔
3070
    pInfo->pkType = pkCol->info.type;
57✔
3071
    pInfo->pkBytes = pkCol->info.bytes;
57✔
3072
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
57✔
3073
  }
3074
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
45,307,659✔
3075
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
45,307,659!
3076

3077
  if (pCtx->order == TSDB_ORDER_ASC && !pCtx->hasPrimaryKey) {
45,307,659!
3078
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
42,717,161!
3079
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
21,359,766✔
3080
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
21,359,766!
3081
      TSKEY cts = getRowPTs(pInput->pPTS, i);
21,359,766!
3082
      numOfElems++;
21,359,766✔
3083

3084
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
21,359,766✔
3085
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
21,342,561✔
3086
        if (code != TSDB_CODE_SUCCESS) return code;
21,340,744!
3087
      }
3088

3089
      break;
21,357,949✔
3090
    }
3091
  } else if (!pCtx->hasPrimaryKey && pCtx->order == TSDB_ORDER_DESC) {
23,948,447!
3092
    // the optimized version only valid if all tuples in one block are monotonious increasing or descreasing.
3093
    // this assumption is NOT always works if project operator exists in downstream.
3094
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
47,909,388!
3095
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
23,954,941✔
3096
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
23,954,941!
3097
      TSKEY cts = getRowPTs(pInput->pPTS, i);
23,954,941!
3098
      numOfElems++;
23,954,941✔
3099

3100
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
23,954,941✔
3101
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
23,842,436✔
3102
        if (code != TSDB_CODE_SUCCESS) return code;
23,842,160!
3103
      }
3104
      break;
23,954,665✔
3105
    }
3106
  } else {
3107
    int64_t* pts = (int64_t*)pInput->pPTS->pData;
×
3108
    int      from = -1;
×
3109
    int32_t  i = -1;
×
3110
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
287,002✔
3111
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
293,278✔
3112
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
293,278!
3113
      TSKEY cts = pts[i];
293,278✔
3114

3115
      numOfElems++;
293,278✔
3116
      char* pkData = NULL;
293,278✔
3117
      if (pCtx->hasPrimaryKey) {
293,278✔
3118
        pkData = colDataGetData(pkCol, i);
171!
3119
      }
3120
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts ||
293,278✔
3121
          (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
9,836!
3122
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
283,448✔
3123
        if (code != TSDB_CODE_SUCCESS) {
283,448!
3124
          return code;
×
3125
        }
3126
        pResInfo->numOfRes = 1;
283,448✔
3127
      }
3128
    }
3129
  }
3130

3131
  SET_VAL(pResInfo, numOfElems, 1);
45,313,169!
3132
  return TSDB_CODE_SUCCESS;
45,313,169✔
3133
}
3134

3135
bool getDiffFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
38,568✔
3136
  pEnv->calcMemSize = sizeof(SDiffInfo);
38,568✔
3137
  return true;
38,568✔
3138
}
3139

3140
int32_t diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
1,804,794✔
3141
  if (pResInfo->initialized) {
1,804,794✔
3142
    return TSDB_CODE_SUCCESS;
1,714,077✔
3143
  }
3144
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
90,717!
3145
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3146
  }
3147
  SDiffInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
90,717✔
3148
  pDiffInfo->hasPrev = false;
90,717✔
3149
  pDiffInfo->isFirstRow = true;
90,717✔
3150
  pDiffInfo->prev.i64 = 0;
90,717✔
3151
  pDiffInfo->prevTs = -1;
90,717✔
3152
  if (pCtx->numOfParams > 1) {
90,717!
3153
    pDiffInfo->ignoreOption = pCtx->param[1].param.i;  // TODO set correct param
90,717✔
3154
  } else {
3155
    pDiffInfo->ignoreOption = 0;
×
3156
  }
3157
  return TSDB_CODE_SUCCESS;
90,717✔
3158
}
3159

3160
static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, int64_t ts) {
768,011✔
3161
  switch (type) {
768,011!
3162
    case TSDB_DATA_TYPE_BOOL:
28✔
3163
      pDiffInfo->prev.i64 = *(bool*)pv ? 1 : 0;
28✔
3164
      break;
28✔
3165
    case TSDB_DATA_TYPE_UTINYINT:
81,733✔
3166
    case TSDB_DATA_TYPE_TINYINT:
3167
      pDiffInfo->prev.i64 = *(int8_t*)pv;
81,733✔
3168
      break;
81,733✔
3169
    case TSDB_DATA_TYPE_UINT:
53,386✔
3170
    case TSDB_DATA_TYPE_INT:
3171
      pDiffInfo->prev.i64 = *(int32_t*)pv;
53,386✔
3172
      break;
53,386✔
3173
    case TSDB_DATA_TYPE_USMALLINT:
81,185✔
3174
    case TSDB_DATA_TYPE_SMALLINT:
3175
      pDiffInfo->prev.i64 = *(int16_t*)pv;
81,185✔
3176
      break;
81,185✔
3177
    case TSDB_DATA_TYPE_TIMESTAMP:
235✔
3178
    case TSDB_DATA_TYPE_UBIGINT:
3179
    case TSDB_DATA_TYPE_BIGINT:
3180
      pDiffInfo->prev.i64 = *(int64_t*)pv;
235✔
3181
      break;
235✔
3182
    case TSDB_DATA_TYPE_FLOAT:
364,347✔
3183
      pDiffInfo->prev.d64 = *(float*)pv;
364,347✔
3184
      break;
364,347✔
3185
    case TSDB_DATA_TYPE_DOUBLE:
187,097✔
3186
      pDiffInfo->prev.d64 = *(double*)pv;
187,097✔
3187
      break;
187,097✔
3188
    default:
×
3189
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3190
  }
3191
  pDiffInfo->prevTs = ts;
768,011✔
3192
  pDiffInfo->hasPrev = true;
768,011✔
3193
  return TSDB_CODE_SUCCESS;
768,011✔
3194
}
3195

3196
static bool diffIsNegtive(SDiffInfo* pDiffInfo, int32_t type, const char* pv) {
2,957,146✔
3197
  switch (type) {
2,957,146!
3198
    case TSDB_DATA_TYPE_UINT: {
×
3199
      int64_t v = *(uint32_t*)pv;
×
3200
      return v < pDiffInfo->prev.i64;
×
3201
    }
3202
    case TSDB_DATA_TYPE_INT: {
6,200✔
3203
      int64_t v = *(int32_t*)pv;
6,200✔
3204
      return v < pDiffInfo->prev.i64;
6,200✔
3205
    }
3206
    case TSDB_DATA_TYPE_BOOL: {
×
3207
      int64_t v = *(bool*)pv;
×
3208
      return v < pDiffInfo->prev.i64;
×
3209
    }
3210
    case TSDB_DATA_TYPE_UTINYINT: {
×
3211
      int64_t v = *(uint8_t*)pv;
×
3212
      return v < pDiffInfo->prev.i64;
×
3213
    }
3214
    case TSDB_DATA_TYPE_TINYINT: {
276,438✔
3215
      int64_t v = *(int8_t*)pv;
276,438✔
3216
      return v < pDiffInfo->prev.i64;
276,438✔
3217
    }
3218
    case TSDB_DATA_TYPE_USMALLINT: {
×
3219
      int64_t v = *(uint16_t*)pv;
×
3220
      return v < pDiffInfo->prev.i64;
×
3221
    }
3222
    case TSDB_DATA_TYPE_SMALLINT: {
279,216✔
3223
      int64_t v = *(int16_t*)pv;
279,216✔
3224
      return v < pDiffInfo->prev.i64;
279,216✔
3225
    }
3226
    case TSDB_DATA_TYPE_UBIGINT: {
24✔
3227
      uint64_t v = *(uint64_t*)pv;
24✔
3228
      return v < (uint64_t)pDiffInfo->prev.i64;
24✔
3229
    }
3230
    case TSDB_DATA_TYPE_TIMESTAMP:
3,199✔
3231
    case TSDB_DATA_TYPE_BIGINT: {
3232
      int64_t v = *(int64_t*)pv;
3,199✔
3233
      return v < pDiffInfo->prev.i64;
3,199✔
3234
    }
3235
    case TSDB_DATA_TYPE_FLOAT: {
1,836,450✔
3236
      float v = *(float*)pv;
1,836,450✔
3237
      return v < pDiffInfo->prev.d64;
1,836,450✔
3238
    }
3239
    case TSDB_DATA_TYPE_DOUBLE: {
555,619✔
3240
      double v = *(double*)pv;
555,619✔
3241
      return v < pDiffInfo->prev.d64;
555,619✔
3242
    }
3243
    default:
×
3244
      return false;
×
3245
  }
3246

3247
  return false;
3248
}
3249

3250
static void tryToSetInt64(SDiffInfo* pDiffInfo, int32_t type, SColumnInfoData* pOutput, int64_t v, int32_t pos) {
1,408,928,219✔
3251
  bool isNegative = v < pDiffInfo->prev.i64;
1,408,928,219✔
3252
  if (type == TSDB_DATA_TYPE_UBIGINT) {
1,408,928,219✔
3253
    isNegative = (uint64_t)v < (uint64_t)pDiffInfo->prev.i64;
453✔
3254
  }
3255
  int64_t delta = v - pDiffInfo->prev.i64;
1,408,928,219✔
3256
  if (isNegative && ignoreNegative(pDiffInfo->ignoreOption)) {
1,408,928,219✔
3257
    colDataSetNull_f_s(pOutput, pos);
161,575✔
3258
    pOutput->hasNull = true;
161,575✔
3259
  } else {
3260
    colDataSetInt64(pOutput, pos, &delta);
1,408,766,644✔
3261
  }
3262
  pDiffInfo->prev.i64 = v;
1,408,928,219✔
3263
}
1,408,928,219✔
3264

3265
static void tryToSetDouble(SDiffInfo* pDiffInfo, SColumnInfoData* pOutput, double v, int32_t pos) {
2,179,369✔
3266
  double delta = v - pDiffInfo->prev.d64;
2,179,369✔
3267
  if (delta < 0 && ignoreNegative(pDiffInfo->ignoreOption)) {
2,179,369✔
3268
    colDataSetNull_f_s(pOutput, pos);
676,189✔
3269
  } else {
3270
    colDataSetDouble(pOutput, pos, &delta);
1,503,180✔
3271
  }
3272
  pDiffInfo->prev.d64 = v;
2,179,369✔
3273
}
2,179,369✔
3274

3275
static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, SColumnInfoData* pOutput, int32_t pos,
1,411,108,157✔
3276
                            int64_t ts) {
3277
  if (!pDiffInfo->hasPrev) {
1,411,108,157✔
3278
    colDataSetNull_f_s(pOutput, pos);
569✔
3279
    return doSetPrevVal(pDiffInfo, type, pv, ts);
569✔
3280
  }
3281
  pDiffInfo->prevTs = ts;
1,411,107,588✔
3282
  switch (type) {
1,411,107,588!
3283
    case TSDB_DATA_TYPE_UINT: {
411✔
3284
      int64_t v = *(uint32_t*)pv;
411✔
3285
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
411✔
3286
      break;
411✔
3287
    }
3288
    case TSDB_DATA_TYPE_INT: {
26,250,320✔
3289
      int64_t v = *(int32_t*)pv;
26,250,320✔
3290
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
26,250,320✔
3291
      break;
26,250,320✔
3292
    }
3293
    case TSDB_DATA_TYPE_BOOL: {
10,617✔
3294
      int64_t v = *(bool*)pv;
10,617✔
3295
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
10,617✔
3296
      break;
10,617✔
3297
    }
3298
    case TSDB_DATA_TYPE_UTINYINT: {
405✔
3299
      int64_t v = *(uint8_t*)pv;
405✔
3300
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
405✔
3301
      break;
405✔
3302
    }
3303
    case TSDB_DATA_TYPE_TINYINT: {
362,761✔
3304
      int64_t v = *(int8_t*)pv;
362,761✔
3305
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
362,761✔
3306
      break;
362,761✔
3307
    }
3308
    case TSDB_DATA_TYPE_USMALLINT: {
405✔
3309
      int64_t v = *(uint16_t*)pv;
405✔
3310
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
405✔
3311
      break;
405✔
3312
    }
3313
    case TSDB_DATA_TYPE_SMALLINT: {
369,797✔
3314
      int64_t v = *(int16_t*)pv;
369,797✔
3315
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
369,797✔
3316
      break;
369,797✔
3317
    }
3318
    case TSDB_DATA_TYPE_TIMESTAMP:
1,381,933,503✔
3319
    case TSDB_DATA_TYPE_UBIGINT:
3320
    case TSDB_DATA_TYPE_BIGINT: {
3321
      int64_t v = *(int64_t*)pv;
1,381,933,503✔
3322
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
1,381,933,503✔
3323
      break;
1,381,933,503✔
3324
    }
3325
    case TSDB_DATA_TYPE_FLOAT: {
1,759,624✔
3326
      double v = *(float*)pv;
1,759,624✔
3327
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
1,759,624✔
3328
      break;
1,759,624✔
3329
    }
3330
    case TSDB_DATA_TYPE_DOUBLE: {
419,745✔
3331
      double v = *(double*)pv;
419,745✔
3332
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
419,745✔
3333
      break;
419,745✔
3334
    }
3335
    default:
×
3336
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3337
  }
3338
  pDiffInfo->hasPrev = true;
1,411,107,588✔
3339
  return TSDB_CODE_SUCCESS;
1,411,107,588✔
3340
}
3341

3342
// TODO: the primary key compare can be skipped for ordered pk if knonwn before
3343
// TODO: for desc ordered, pk shall select the smallest one for one ts. if across block boundaries.
3344
bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex,
340,706,066✔
3345
                              int32_t* nextFrom) {
3346
  if (pInput->pPrimaryKey == NULL) {
340,706,066✔
3347
    if (from == -1) {
240,754,162✔
3348
      from = pInput->startRowIndex;
74,965,021✔
3349
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
165,789,141✔
3350
      return false;
74,658,007✔
3351
    }
3352
    *pRowIndex = from;
166,096,155✔
3353
    *nextFrom = from + 1;
166,096,155✔
3354
    return true;
166,096,155✔
3355
  } else {
3356
    if (from == -1) {
99,951,904✔
3357
      from = pInput->startRowIndex;
30,175✔
3358
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
99,921,729✔
3359
      return false;
30,175✔
3360
    }
3361
    TSKEY*           tsList = (int64_t*)pInput->pPTS->pData;
99,921,729✔
3362
    SColumnInfoData* pkCol = pInput->pPrimaryKey;
99,921,729✔
3363
    int8_t           pkType = pkCol->info.type;
99,921,729✔
3364
    int32_t          order = (firstOccur) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
99,921,729✔
3365
    __compar_fn_t    compareFunc = getKeyComparFunc(pkType, order);
99,921,729✔
3366
    int32_t          select = from;
100,000,517✔
3367
    char*            val = colDataGetData(pkCol, select);
100,000,517!
3368
    while (from < pInput->numOfRows + pInput->startRowIndex - 1 && tsList[from + 1] == tsList[from]) {
100,001,072✔
3369
      char* val1 = colDataGetData(pkCol, from + 1);
555!
3370
      if (compareFunc(val1, val) < 0) {
555!
3371
        select = from + 1;
×
3372
        val = val1;
×
3373
      }
3374
      from = from + 1;
555✔
3375
    }
3376
    *pRowIndex = select;
100,000,517✔
3377
    *nextFrom = from + 1;
100,000,517✔
3378
    return true;
100,000,517✔
3379
  }
3380
}
3381

3382
bool getForecastConfEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
26✔
3383
  pEnv->calcMemSize = sizeof(float);
26✔
3384
  return true;
26✔
3385
}
3386

3387
int32_t diffResultIsNull(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
1,411,974,852✔
3388
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,411,974,852✔
3389
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
1,411,974,852✔
3390

3391
  if (pRow->isDataNull || !pDiffInfo->hasPrev) {
1,411,974,852✔
3392
    return true;
188,697✔
3393
  } else if (ignoreNegative(pDiffInfo->ignoreOption)) {
1,411,786,155✔
3394
    return diffIsNegtive(pDiffInfo, pCtx->input.pData[0]->info.type, pRow->pData);
2,957,146✔
3395
  }
3396
  return false;
1,408,829,009✔
3397
}
3398

3399
bool isFirstRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
1,411,279,043✔
3400
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,411,279,043✔
3401
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
1,411,279,043✔
3402
  return pDiffInfo->isFirstRow;
1,411,279,043✔
3403
}
3404

3405
int32_t trySetPreVal(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
769,313✔
3406
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
769,313✔
3407
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
769,313✔
3408
  pDiffInfo->isFirstRow = false;
769,313✔
3409
  if (pRow->isDataNull) {
769,313✔
3410
    return TSDB_CODE_SUCCESS;
1,871✔
3411
  }
3412

3413
  SInputColumnInfoData* pInput = &pCtx->input;
767,442✔
3414
  SColumnInfoData*      pInputCol = pInput->pData[0];
767,442✔
3415
  int8_t                inputType = pInputCol->info.type;
767,442✔
3416

3417
  char* pv = pRow->pData;
767,442✔
3418
  return doSetPrevVal(pDiffInfo, inputType, pv, pRow->ts);
767,442✔
3419
}
3420

3421
int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) {
1,411,205,539✔
3422
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,411,205,539✔
3423
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
1,411,205,539✔
3424

3425
  SInputColumnInfoData* pInput = &pCtx->input;
1,411,205,539✔
3426
  SColumnInfoData*      pInputCol = pInput->pData[0];
1,411,205,539✔
3427
  int8_t                inputType = pInputCol->info.type;
1,411,205,539✔
3428
  SColumnInfoData*      pOutput = (SColumnInfoData*)pCtx->pOutput;
1,411,205,539✔
3429
  int32_t               code = TSDB_CODE_SUCCESS;
1,411,205,539✔
3430
  if (pRow->isDataNull) {
1,411,205,539✔
3431
    colDataSetNull_f_s(pOutput, pos);
97,361✔
3432
    pOutput->hasNull = true;
97,361✔
3433

3434
    // handle selectivity
3435
    if (pCtx->subsidiaries.num > 0) {
97,361✔
3436
      code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
149✔
3437
      if (code != TSDB_CODE_SUCCESS) {
149!
3438
        return code;
×
3439
      }
3440
    }
3441
    return TSDB_CODE_SUCCESS;
97,361✔
3442
  }
3443

3444
  char* pv = pRow->pData;
1,411,108,178✔
3445

3446
  if (pRow->ts == pDiffInfo->prevTs) {
1,411,108,178✔
3447
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
21✔
3448
  }
3449
  code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts);
1,411,108,157✔
3450
  if (code != TSDB_CODE_SUCCESS) {
1,411,108,157!
3451
    return code;
×
3452
  }
3453
  // handle selectivity
3454
  if (pCtx->subsidiaries.num > 0) {
1,411,108,157✔
3455
    code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
28,339,223✔
3456
    if (code != TSDB_CODE_SUCCESS) {
28,339,223!
3457
      return code;
×
3458
    }
3459
  }
3460

3461
  return TSDB_CODE_SUCCESS;
1,411,108,157✔
3462
}
3463

3464
int32_t diffFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; }
1,766,226✔
3465

3466
int32_t diffFunctionByRow(SArray* pCtxArray) {
1,766,070✔
3467
  int32_t code = TSDB_CODE_SUCCESS;
1,766,070✔
3468
  int     diffColNum = pCtxArray->size;
1,766,070✔
3469
  if (diffColNum == 0) {
1,766,070!
3470
    return TSDB_CODE_SUCCESS;
×
3471
  }
3472
  int32_t numOfElems = 0;
1,766,070✔
3473

3474
  SArray* pRows = taosArrayInit_s(sizeof(SFuncInputRow), diffColNum);
1,766,070✔
3475
  if (NULL == pRows) {
1,766,070!
3476
    return terrno;
×
3477
  }
3478

3479
  bool keepNull = false;
1,766,070✔
3480
  for (int i = 0; i < diffColNum; ++i) {
3,532,296✔
3481
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
1,766,226✔
3482
    if (NULL == pCtx) {
1,766,226!
3483
      code = terrno;
×
3484
      goto _exit;
×
3485
    }
3486
    funcInputUpdate(pCtx);
1,766,226✔
3487
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,766,226✔
3488
    SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
1,766,226✔
3489
    if (!ignoreNull(pDiffInfo->ignoreOption)) {
1,766,226✔
3490
      keepNull = true;
1,743,294✔
3491
    }
3492
  }
3493

3494
  SqlFunctionCtx* pCtx0 = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, 0);
1,766,070✔
3495
  SFuncInputRow*  pRow0 = (SFuncInputRow*)taosArrayGet(pRows, 0);
1,766,070✔
3496
  if (NULL == pCtx0 || NULL == pRow0) {
1,766,070!
3497
    code = terrno;
×
3498
    goto _exit;
×
3499
  }
3500
  int32_t startOffset = pCtx0->offset;
1,766,070✔
3501
  bool    result = false;
1,766,070✔
3502
  while (1) {
1,411,960,337✔
3503
    code = funcInputGetNextRow(pCtx0, pRow0, &result);
1,413,726,407✔
3504
    if (TSDB_CODE_SUCCESS != code) {
1,413,726,407!
3505
      goto _exit;
×
3506
    }
3507
    if (!result) {
1,413,726,407✔
3508
      break;
1,766,049✔
3509
    }
3510
    bool hasNotNullValue = !diffResultIsNull(pCtx0, pRow0);
1,411,960,358✔
3511
    for (int i = 1; i < diffColNum; ++i) {
1,411,974,852✔
3512
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
14,494✔
3513
      SFuncInputRow*  pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
14,494✔
3514
      if (NULL == pCtx || NULL == pRow) {
14,494!
3515
        code = terrno;
×
3516
        goto _exit;
×
3517
      }
3518
      code = funcInputGetNextRow(pCtx, pRow, &result);
14,494✔
3519
      if (TSDB_CODE_SUCCESS != code) {
14,494!
3520
        goto _exit;
×
3521
      }
3522
      if (!result) {
14,494!
3523
        // rows are not equal
3524
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
3525
        goto _exit;
×
3526
      }
3527
      if (!diffResultIsNull(pCtx, pRow)) {
14,494✔
3528
        hasNotNullValue = true;
14,052✔
3529
      }
3530
    }
3531
    int32_t pos = startOffset + numOfElems;
1,411,960,358✔
3532

3533
    bool newRow = false;
1,411,960,358✔
3534
    for (int i = 0; i < diffColNum; ++i) {
2,147,483,647✔
3535
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
1,411,974,852✔
3536
      SFuncInputRow*  pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
1,411,974,852✔
3537
      if (NULL == pCtx || NULL == pRow) {
1,411,974,852!
3538
        code = terrno;
×
3539
        goto _exit;
×
3540
      }
3541
      if ((keepNull || hasNotNullValue) && !isFirstRow(pCtx, pRow)) {
1,411,974,852✔
3542
        code = setDoDiffResult(pCtx, pRow, pos);
1,411,205,539✔
3543
        if (code != TSDB_CODE_SUCCESS) {
1,411,205,539✔
3544
          goto _exit;
21✔
3545
        }
3546
        newRow = true;
1,411,205,518✔
3547
      } else {
3548
        code = trySetPreVal(pCtx, pRow);
769,313✔
3549
        if (code != TSDB_CODE_SUCCESS) {
769,313!
3550
          goto _exit;
×
3551
        }
3552
      }
3553
    }
3554
    if (newRow) ++numOfElems;
1,411,960,337✔
3555
  }
3556

3557
  for (int i = 0; i < diffColNum; ++i) {
3,532,251✔
3558
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
1,766,202✔
3559
    if (NULL == pCtx) {
1,766,202!
3560
      code = terrno;
×
3561
      goto _exit;
×
3562
    }
3563
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,766,202✔
3564
    pResInfo->numOfRes = numOfElems;
1,766,202✔
3565
  }
3566

3567
_exit:
1,766,049✔
3568
  if (pRows) {
1,766,070!
3569
    taosArrayDestroy(pRows);
1,766,070✔
3570
    pRows = NULL;
1,766,070✔
3571
  }
3572
  return code;
1,766,070✔
3573
}
3574

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

3577
bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
81,919✔
3578
  SValueNode* pkNode = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
81,919✔
3579
  pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem);
81,962✔
3580
  return true;
81,962✔
3581
}
3582

3583
int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
32,754,161✔
3584
  if (pResInfo->initialized) {
32,754,161!
3585
    return TSDB_CODE_SUCCESS;
×
3586
  }
3587
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
32,754,161!
3588
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3589
  }
3590

3591
  STopBotRes*           pRes = GET_ROWCELL_INTERBUF(pResInfo);
32,757,213✔
3592
  SInputColumnInfoData* pInput = &pCtx->input;
32,757,213✔
3593

3594
  pRes->maxSize = pCtx->param[1].param.i;
32,757,213✔
3595

3596
  pRes->nullTupleSaved = false;
32,757,213✔
3597
  pRes->nullTuplePos.pageId = -1;
32,757,213✔
3598
  return TSDB_CODE_SUCCESS;
32,757,213✔
3599
}
3600

3601
static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) {
141,100,594✔
3602
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
141,100,594✔
3603
  STopBotRes*          pRes = GET_ROWCELL_INTERBUF(pResInfo);
141,100,594✔
3604
  pRes->pItems = (STopBotResItem*)((char*)pRes + sizeof(STopBotRes));
141,100,594✔
3605

3606
  return pRes;
141,100,594✔
3607
}
3608

3609
static int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock,
3610
                               uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery);
3611

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

3614
int32_t topFunction(SqlFunctionCtx* pCtx) {
16,833,096✔
3615
  int32_t              numOfElems = 0;
16,833,096✔
3616
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
16,833,096✔
3617

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

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

3624
  int32_t start = pInput->startRowIndex;
16,833,063✔
3625
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
58,391,634✔
3626
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
41,557,009✔
3627
      continue;
24,800✔
3628
    }
3629

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

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

3648
int32_t bottomFunction(SqlFunctionCtx* pCtx) {
16,035,609✔
3649
  int32_t              numOfElems = 0;
16,035,609✔
3650
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
16,035,609✔
3651

3652
  SInputColumnInfoData* pInput = &pCtx->input;
16,035,609✔
3653
  SColumnInfoData*      pCol = pInput->pData[0];
16,035,609✔
3654

3655
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
16,035,609✔
3656
  pRes->type = pInput->pData[0]->info.type;
16,035,622✔
3657

3658
  int32_t start = pInput->startRowIndex;
16,035,622✔
3659
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
50,126,696✔
3660
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
34,090,772✔
3661
      continue;
28,720✔
3662
    }
3663

3664
    numOfElems++;
34,062,052✔
3665
    char*   data = colDataGetData(pCol, i);
34,062,052!
3666
    int32_t code = doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, pRes->type, pInput->uid, pResInfo, false);
34,062,052✔
3667
    if (code != TSDB_CODE_SUCCESS) {
34,062,354!
3668
      return code;
×
3669
    }
3670
  }
3671

3672
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pRes->nullTupleSaved) {
16,035,924✔
3673
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pRes->nullTuplePos);
134✔
3674
    if (code != TSDB_CODE_SUCCESS) {
134!
3675
      return code;
×
3676
    }
3677
    pRes->nullTupleSaved = true;
134✔
3678
  }
3679

3680
  return TSDB_CODE_SUCCESS;
16,035,924✔
3681
}
3682

3683
static int32_t topBotResComparFn(const void* p1, const void* p2, const void* param) {
362,087,908✔
3684
  uint16_t type = *(uint16_t*)param;
362,087,908✔
3685

3686
  STopBotResItem* val1 = (STopBotResItem*)p1;
362,087,908✔
3687
  STopBotResItem* val2 = (STopBotResItem*)p2;
362,087,908✔
3688

3689
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
362,087,908!
3690
    if (val1->v.i == val2->v.i) {
96,616,772✔
3691
      return 0;
169,824✔
3692
    }
3693

3694
    return (val1->v.i > val2->v.i) ? 1 : -1;
96,446,948✔
3695
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
265,471,136!
3696
    if (val1->v.u == val2->v.u) {
479,403✔
3697
      return 0;
100,806✔
3698
    }
3699

3700
    return (val1->v.u > val2->v.u) ? 1 : -1;
378,597✔
3701
  } else if (TSDB_DATA_TYPE_FLOAT == type) {
264,991,733✔
3702
    if (val1->v.f == val2->v.f) {
87,222,289✔
3703
      return 0;
63✔
3704
    }
3705

3706
    return (val1->v.f > val2->v.f) ? 1 : -1;
87,222,226✔
3707
  }
3708

3709
  if (val1->v.d == val2->v.d) {
177,769,444✔
3710
    return 0;
11✔
3711
  }
3712

3713
  return (val1->v.d > val2->v.d) ? 1 : -1;
177,769,433✔
3714
}
3715

3716
int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type,
75,581,106✔
3717
                        uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery) {
3718
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
75,581,106✔
3719
  int32_t     code = TSDB_CODE_SUCCESS;
75,577,079✔
3720

3721
  SVariant val = {0};
75,577,079✔
3722
  TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type));
75,577,079!
3723

3724
  STopBotResItem* pItems = pRes->pItems;
75,586,939✔
3725

3726
  // not full yet
3727
  if (pEntryInfo->numOfRes < pRes->maxSize) {
75,586,939✔
3728
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
49,055,670✔
3729
    pItem->v = val;
49,055,670✔
3730
    pItem->uid = uid;
49,055,670✔
3731

3732
    // save the data of this tuple
3733
    if (pCtx->subsidiaries.num > 0) {
49,055,670✔
3734
      code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
27,699,322✔
3735
      if (code != TSDB_CODE_SUCCESS) {
27,695,007!
3736
        return code;
×
3737
      }
3738
    }
3739
#ifdef BUF_PAGE_DEBUG
3740
    qDebug("page_saveTuple i:%d, item:%p,pageId:%d, offset:%d\n", pEntryInfo->numOfRes, pItem, pItem->tuplePos.pageId,
3741
           pItem->tuplePos.offset);
3742
#endif
3743
    // allocate the buffer and keep the data of this row into the new allocated buffer
3744
    pEntryInfo->numOfRes++;
49,051,355✔
3745
    code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
49,051,355✔
3746
                        topBotResComparFn, !isTopQuery);
49,051,355✔
3747
    if (code != TSDB_CODE_SUCCESS) {
49,061,438!
3748
      return code;
×
3749
    }
3750
  } else {  // replace the minimum value in the result
3751
    if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) ||
26,531,269!
3752
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) ||
14,030,691✔
3753
                        (TSDB_DATA_TYPE_FLOAT == type && val.f > pItems[0].v.f) ||
13,976,174✔
3754
                        (TSDB_DATA_TYPE_DOUBLE == type && val.d > pItems[0].v.d))) ||
13,004,563✔
3755
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i < pItems[0].v.i) ||
22,255,139!
3756
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u < pItems[0].v.u) ||
9,792,941!
3757
                         (TSDB_DATA_TYPE_FLOAT == type && val.f < pItems[0].v.f) ||
9,792,002✔
3758
                         (TSDB_DATA_TYPE_DOUBLE == type && val.d < pItems[0].v.d)))) {
8,774,880✔
3759
      // replace the old data and the coresponding tuple data
3760
      STopBotResItem* pItem = &pItems[0];
7,040,475✔
3761
      pItem->v = val;
7,040,475✔
3762
      pItem->uid = uid;
7,040,475✔
3763

3764
      // save the data of this tuple by over writing the old data
3765
      if (pCtx->subsidiaries.num > 0) {
7,040,475✔
3766
        code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
4,708,253✔
3767
        if (code != TSDB_CODE_SUCCESS) {
4,707,304!
3768
          return code;
×
3769
        }
3770
      }
3771
#ifdef BUF_PAGE_DEBUG
3772
      qDebug("page_copyTuple pageId:%d, offset:%d", pItem->tuplePos.pageId, pItem->tuplePos.offset);
3773
#endif
3774
      code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type,
7,039,526✔
3775
                            topBotResComparFn, NULL, !isTopQuery);
7,039,526✔
3776
      if (code != TSDB_CODE_SUCCESS) {
7,034,224!
3777
        return code;
×
3778
      }
3779
    }
3780
  }
3781

3782
  return TSDB_CODE_SUCCESS;
75,586,456✔
3783
}
3784

3785
/*
3786
 * +------------------------------------+--------------+--------------+
3787
 * |            null bitmap             |              |              |
3788
 * |(n columns, one bit for each column)| src column #1| src column #2|
3789
 * +------------------------------------+--------------+--------------+
3790
 */
3791
int32_t serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsidiaryResInfo* pSubsidiaryies,
233,440,082✔
3792
                           char* buf, char** res) {
3793
  char* nullList = buf;
233,440,082✔
3794
  char* pStart = (char*)(nullList + sizeof(bool) * pSubsidiaryies->num);
233,440,082✔
3795

3796
  int32_t offset = 0;
233,440,082✔
3797
  for (int32_t i = 0; i < pSubsidiaryies->num; ++i) {
466,740,241✔
3798
    SqlFunctionCtx* pc = pSubsidiaryies->pCtx[i];
233,473,681✔
3799

3800
    // group_key function has its own process function
3801
    // do not process there
3802
    if (fmIsGroupKeyFunc(pc->functionId)) {
233,473,681!
3803
      continue;
×
3804
    }
3805

3806
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
233,455,020✔
3807
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
233,455,020✔
3808

3809
    SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
233,455,020✔
3810
    if (NULL == pCol) {
233,300,159!
3811
      return TSDB_CODE_OUT_OF_RANGE;
×
3812
    }
3813
    if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) {
466,600,318✔
3814
      offset += pCol->info.bytes;
683✔
3815
      continue;
683✔
3816
    }
3817

3818
    char* p = colDataGetData(pCol, rowIndex);
233,299,476!
3819
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
233,299,476!
3820
      (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p));
34,749!
3821
    } else {
3822
      (void)memcpy(pStart + offset, p, pCol->info.bytes);
233,264,727✔
3823
    }
3824

3825
    offset += pCol->info.bytes;
233,299,476✔
3826
  }
3827

3828
  *res = buf;
233,266,560✔
3829
  return TSDB_CODE_SUCCESS;
233,266,560✔
3830
}
3831

3832
static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, SWinKey* key,
310,391,264✔
3833
                               STuplePos* pPos, SFunctionStateStore* pStore) {
3834
  STuplePos p = {0};
310,391,264✔
3835
  if (pHandle->pBuf != NULL) {
310,391,264✔
3836
    SFilePage* pPage = NULL;
310,332,238✔
3837

3838
    if (pHandle->currentPage == -1) {
310,332,238✔
3839
      pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
632,624✔
3840
      if (pPage == NULL) {
632,631!
3841
        return terrno;
×
3842
      }
3843
      pPage->num = sizeof(SFilePage);
632,631✔
3844
    } else {
3845
      pPage = getBufPage(pHandle->pBuf, pHandle->currentPage);
309,699,614✔
3846
      if (pPage == NULL) {
309,679,314!
3847
        return terrno;
×
3848
      }
3849
      if (pPage->num + length > getBufPageSize(pHandle->pBuf)) {
309,679,314✔
3850
        // current page is all used, let's prepare a new buffer page
3851
        releaseBufPage(pHandle->pBuf, pPage);
3,139,204✔
3852
        pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
3,139,203✔
3853
        if (pPage == NULL) {
3,139,205!
3854
          return terrno;
×
3855
        }
3856
        pPage->num = sizeof(SFilePage);
3,139,205✔
3857
      }
3858
    }
3859

3860
    p = (STuplePos){.pageId = pHandle->currentPage, .offset = pPage->num};
310,304,448✔
3861
    (void)memcpy(pPage->data + pPage->num, pBuf, length);
310,304,448✔
3862

3863
    pPage->num += length;
310,304,448✔
3864
    setBufPageDirty(pPage, true);
310,304,448✔
3865
    releaseBufPage(pHandle->pBuf, pPage);
310,261,525✔
3866
  } else {  // other tuple save policy
3867
    if (pStore->streamStateFuncPut(pHandle->pState, key, pBuf, length) >= 0) {
59,026!
3868
      p.streamTupleKey = *key;
67,371✔
3869
    }
3870
  }
3871

3872
  *pPos = p;
310,288,724✔
3873
  return TSDB_CODE_SUCCESS;
310,288,724✔
3874
}
3875

3876
int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
202,391,304✔
3877
  int32_t code = prepareBuf(pCtx);
202,391,304✔
3878
  if (TSDB_CODE_SUCCESS != code) {
202,376,256!
3879
    return code;
×
3880
  }
3881

3882
  SWinKey key = {0};
202,376,256✔
3883
  if (pCtx->saveHandle.pBuf == NULL) {
202,376,256✔
3884
    SColumnInfoData* pColInfo = taosArrayGet(pSrcBlock->pDataBlock, pCtx->saveHandle.pState->tsIndex);
67,371✔
3885
    if (NULL == pColInfo) {
67,371!
3886
      return TSDB_CODE_OUT_OF_RANGE;
×
3887
    }
3888
    if (pColInfo->info.type != TSDB_DATA_TYPE_TIMESTAMP) {
67,371!
3889
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3890
    }
3891
    key.groupId = pSrcBlock->info.id.groupId;
67,371✔
3892
    key.ts = *(int64_t*)colDataGetData(pColInfo, rowIndex);
67,371!
3893
  }
3894

3895
  char* buf = NULL;
202,376,256✔
3896
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
202,376,256✔
3897
  if (TSDB_CODE_SUCCESS != code) {
202,279,975!
3898
    return code;
×
3899
  }
3900
  return doSaveTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, &key, pPos, pCtx->pStore);
202,279,975✔
3901
}
3902

3903
static int32_t doUpdateTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, STuplePos* pPos,
31,090,751✔
3904
                                 SFunctionStateStore* pStore) {
3905
  if (pHandle->pBuf != NULL) {
31,090,751✔
3906
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
30,976,796✔
3907
    if (pPage == NULL) {
30,976,242!
3908
      return terrno;
×
3909
    }
3910
    (void)memcpy(pPage->data + pPos->offset, pBuf, length);
30,976,242✔
3911
    setBufPageDirty(pPage, true);
30,976,242✔
3912
    releaseBufPage(pHandle->pBuf, pPage);
30,975,369✔
3913
  } else {
3914
    int32_t code = pStore->streamStateFuncPut(pHandle->pState, &pPos->streamTupleKey, pBuf, length);
113,955✔
3915
    if (TSDB_CODE_SUCCESS != code) {
114,082!
3916
      return code;
×
3917
    }
3918
  }
3919

3920
  return TSDB_CODE_SUCCESS;
31,087,533✔
3921
}
3922

3923
int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
31,093,121✔
3924
  int32_t code = prepareBuf(pCtx);
31,093,121✔
3925
  if (TSDB_CODE_SUCCESS != code) {
31,092,989!
3926
    return code;
×
3927
  }
3928

3929
  char* buf = NULL;
31,092,989✔
3930
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
31,092,989✔
3931
  if (TSDB_CODE_SUCCESS != code) {
31,090,617!
3932
    return code;
×
3933
  }
3934
  return doUpdateTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, pPos, pCtx->pStore);
31,090,617✔
3935
}
3936

3937
static int32_t doLoadTupleData(SSerializeDataHandle* pHandle, const STuplePos* pPos, SFunctionStateStore* pStore,
131,346,144✔
3938
                               char** value) {
3939
  if (pHandle->pBuf != NULL) {
131,346,144✔
3940
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
131,288,393✔
3941
    if (pPage == NULL) {
131,399,627!
3942
      *value = NULL;
×
3943
      return terrno;
×
3944
    }
3945
    *value = pPage->data + pPos->offset;
131,399,627✔
3946
    releaseBufPage(pHandle->pBuf, pPage);
131,399,627✔
3947
    return TSDB_CODE_SUCCESS;
131,365,936✔
3948
  } else {
3949
    *value = NULL;
57,751✔
3950
    int32_t vLen;
3951
    int32_t code = pStore->streamStateFuncGet(pHandle->pState, &pPos->streamTupleKey, (void**)(value), &vLen);
57,751✔
3952
    if (TSDB_CODE_SUCCESS != code) {
67,545!
3953
      return code;
×
3954
    }
3955
    return TSDB_CODE_SUCCESS;
67,545✔
3956
  }
3957
}
3958

3959
int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value) {
131,225,413✔
3960
  return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore, value);
131,225,413✔
3961
}
3962

3963
int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
32,700,755✔
3964
  int32_t code = TSDB_CODE_SUCCESS;
32,700,755✔
3965

3966
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
32,700,755✔
3967
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
32,700,755✔
3968

3969
  int16_t type = pCtx->pExpr->base.resSchema.type;
32,700,436✔
3970
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
32,700,436✔
3971

3972
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
32,700,436✔
3973
  if (NULL == pCol) {
32,697,165!
3974
    return TSDB_CODE_OUT_OF_RANGE;
×
3975
  }
3976

3977
  // todo assign the tag value and the corresponding row data
3978
  int32_t currentRow = pBlock->info.rows;
32,697,165✔
3979
  if (pEntryInfo->numOfRes <= 0) {
32,697,165✔
3980
    colDataSetNULL(pCol, currentRow);
545!
3981
    code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, currentRow);
545✔
3982
    return code;
545✔
3983
  }
3984
  for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) {
81,644,265✔
3985
    STopBotResItem* pItem = &pRes->pItems[i];
48,959,306✔
3986
    code = colDataSetVal(pCol, currentRow, (const char*)&pItem->v.i, false);
48,959,306✔
3987
    if (TSDB_CODE_SUCCESS != code) {
48,947,061!
3988
      return code;
×
3989
    }
3990
#ifdef BUF_PAGE_DEBUG
3991
    qDebug("page_finalize i:%d,item:%p,pageId:%d, offset:%d\n", i, pItem, pItem->tuplePos.pageId,
3992
           pItem->tuplePos.offset);
3993
#endif
3994
    code = setSelectivityValue(pCtx, pBlock, &pRes->pItems[i].tuplePos, currentRow);
48,947,061✔
3995
    if (TSDB_CODE_SUCCESS != code) {
48,947,645!
3996
      return code;
×
3997
    }
3998
    currentRow += 1;
48,947,645✔
3999
  }
4000

4001
  return code;
32,684,959✔
4002
}
4003

4004
int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery) {
×
4005
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
4006
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
×
4007
  STopBotResItem*      pItems = pRes->pItems;
×
4008
  int32_t              code = TSDB_CODE_SUCCESS;
×
4009

4010
  // not full yet
4011
  if (pEntryInfo->numOfRes < pRes->maxSize) {
×
4012
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
×
4013
    pItem->v = pSourceItem->v;
×
4014
    pItem->uid = pSourceItem->uid;
×
4015
    pItem->tuplePos.pageId = -1;
×
4016
    replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos);
×
4017
    pEntryInfo->numOfRes++;
×
4018
    code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
×
4019
                        topBotResComparFn, !isTopQuery);
×
4020
    if (TSDB_CODE_SUCCESS != code) {
×
4021
      return code;
×
4022
    }
4023
  } else {  // replace the minimum value in the result
4024
    if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && pSourceItem->v.i > pItems[0].v.i) ||
×
4025
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && pSourceItem->v.u > pItems[0].v.u) ||
×
4026
                        (TSDB_DATA_TYPE_FLOAT == type && pSourceItem->v.f > pItems[0].v.f) ||
×
4027
                        (TSDB_DATA_TYPE_DOUBLE == type && pSourceItem->v.d > pItems[0].v.d))) ||
×
4028
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && pSourceItem->v.i < pItems[0].v.i) ||
×
4029
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && pSourceItem->v.u < pItems[0].v.u) ||
×
4030
                         (TSDB_DATA_TYPE_FLOAT == type && pSourceItem->v.f < pItems[0].v.f) ||
×
4031
                         (TSDB_DATA_TYPE_DOUBLE == type && pSourceItem->v.d < pItems[0].v.d)))) {
×
4032
      // replace the old data and the coresponding tuple data
4033
      STopBotResItem* pItem = &pItems[0];
×
4034
      pItem->v = pSourceItem->v;
×
4035
      pItem->uid = pSourceItem->uid;
×
4036

4037
      // save the data of this tuple by over writing the old data
4038
      replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos);
×
4039
      code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type,
×
4040
                            topBotResComparFn, NULL, !isTopQuery);
×
4041
      if (TSDB_CODE_SUCCESS != code) {
×
4042
        return code;
×
4043
      }
4044
    }
4045
  }
4046
  return code;
×
4047
}
4048

4049
int32_t topCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4050
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4051
  STopBotRes*          pSBuf = getTopBotOutputInfo(pSourceCtx);
×
4052
  int16_t              type = pSBuf->type;
×
4053
  int32_t              code = TSDB_CODE_SUCCESS;
×
4054
  for (int32_t i = 0; i < pSResInfo->numOfRes; i++) {
×
4055
    code = addResult(pDestCtx, pSBuf->pItems + i, type, true);
×
4056
    if (TSDB_CODE_SUCCESS != code) {
×
4057
      return code;
×
4058
    }
4059
  }
4060
  return TSDB_CODE_SUCCESS;
×
4061
}
4062

4063
int32_t bottomCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4064
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4065
  STopBotRes*          pSBuf = getTopBotOutputInfo(pSourceCtx);
×
4066
  int16_t              type = pSBuf->type;
×
4067
  int32_t              code = TSDB_CODE_SUCCESS;
×
4068
  for (int32_t i = 0; i < pSResInfo->numOfRes; i++) {
×
4069
    code = addResult(pDestCtx, pSBuf->pItems + i, type, false);
×
4070
    if (TSDB_CODE_SUCCESS != code) {
×
4071
      return code;
×
4072
    }
4073
  }
4074
  return TSDB_CODE_SUCCESS;
×
4075
}
4076

4077
int32_t getSpreadInfoSize() { return (int32_t)sizeof(SSpreadInfo); }
605,407✔
4078

4079
bool getSpreadFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
105,366✔
4080
  pEnv->calcMemSize = sizeof(SSpreadInfo);
105,366✔
4081
  return true;
105,366✔
4082
}
4083

4084
int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
2,389,989✔
4085
  if (pResultInfo->initialized) {
2,389,989!
4086
    return TSDB_CODE_SUCCESS;
×
4087
  }
4088
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
2,389,989!
4089
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4090
  }
4091

4092
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
2,389,988✔
4093
  SET_DOUBLE_VAL(&pInfo->min, DBL_MAX);
2,389,988✔
4094
  SET_DOUBLE_VAL(&pInfo->max, -DBL_MAX);
2,389,988✔
4095
  pInfo->hasResult = false;
2,389,988✔
4096
  return TSDB_CODE_SUCCESS;
2,389,988✔
4097
}
4098

4099
int32_t spreadFunction(SqlFunctionCtx* pCtx) {
1,839,216✔
4100
  int32_t numOfElems = 0;
1,839,216✔
4101

4102
  // Only the pre-computing information loaded and actual data does not loaded
4103
  SInputColumnInfoData* pInput = &pCtx->input;
1,839,216✔
4104
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
1,839,216✔
4105
  int32_t               type = pInput->pData[0]->info.type;
1,839,216✔
4106

4107
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,839,216✔
4108

4109
  if (pInput->colDataSMAIsSet) {
1,839,216!
4110
    numOfElems = pInput->numOfRows - pAgg->numOfNull;
×
4111
    if (numOfElems == 0) {
×
4112
      goto _spread_over;
×
4113
    }
4114
    double tmin = 0.0, tmax = 0.0;
×
4115
    if (IS_SIGNED_NUMERIC_TYPE(type) || IS_TIMESTAMP_TYPE(type)) {
×
4116
      tmin = (double)GET_INT64_VAL(&pAgg->min);
×
4117
      tmax = (double)GET_INT64_VAL(&pAgg->max);
×
4118
    } else if (IS_FLOAT_TYPE(type)) {
×
4119
      tmin = GET_DOUBLE_VAL(&pAgg->min);
×
4120
      tmax = GET_DOUBLE_VAL(&pAgg->max);
×
4121
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
4122
      tmin = (double)GET_UINT64_VAL(&pAgg->min);
×
4123
      tmax = (double)GET_UINT64_VAL(&pAgg->max);
×
4124
    }
4125

4126
    if (GET_DOUBLE_VAL(&pInfo->min) > tmin) {
×
4127
      SET_DOUBLE_VAL(&pInfo->min, tmin);
×
4128
    }
4129

4130
    if (GET_DOUBLE_VAL(&pInfo->max) < tmax) {
×
4131
      SET_DOUBLE_VAL(&pInfo->max, tmax);
×
4132
    }
4133

4134
  } else {  // computing based on the true data block
4135
    SColumnInfoData* pCol = pInput->pData[0];
1,839,216✔
4136

4137
    int32_t start = pInput->startRowIndex;
1,839,216✔
4138
    // check the valid data one by one
4139
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
11,321,920✔
4140
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
9,482,704✔
4141
        continue;
1,597,756✔
4142
      }
4143

4144
      char* data = colDataGetData(pCol, i);
7,884,948!
4145

4146
      double v = 0;
7,884,948✔
4147
      GET_TYPED_DATA(v, double, type, data);
7,884,948!
4148
      if (v < GET_DOUBLE_VAL(&pInfo->min)) {
7,884,948✔
4149
        SET_DOUBLE_VAL(&pInfo->min, v);
1,849,163✔
4150
      }
4151

4152
      if (v > GET_DOUBLE_VAL(&pInfo->max)) {
7,884,948✔
4153
        SET_DOUBLE_VAL(&pInfo->max, v);
4,530,583✔
4154
      }
4155

4156
      numOfElems += 1;
7,884,948✔
4157
    }
4158
  }
4159

4160
_spread_over:
1,839,216✔
4161
  // data in the check operation are all null, not output
4162
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
1,839,216✔
4163
  if (numOfElems > 0) {
1,839,216✔
4164
    pInfo->hasResult = true;
1,807,852✔
4165
  }
4166

4167
  return TSDB_CODE_SUCCESS;
1,839,216✔
4168
}
4169

4170
static void spreadTransferInfo(SSpreadInfo* pInput, SSpreadInfo* pOutput) {
603,905✔
4171
  pOutput->hasResult = pInput->hasResult;
603,905✔
4172
  if (pInput->max > pOutput->max) {
603,905✔
4173
    pOutput->max = pInput->max;
599,383✔
4174
  }
4175

4176
  if (pInput->min < pOutput->min) {
603,905✔
4177
    pOutput->min = pInput->min;
599,392✔
4178
  }
4179
}
603,905✔
4180

4181
int32_t spreadFunctionMerge(SqlFunctionCtx* pCtx) {
599,951✔
4182
  SInputColumnInfoData* pInput = &pCtx->input;
599,951✔
4183
  SColumnInfoData*      pCol = pInput->pData[0];
599,951✔
4184

4185
  if (IS_NULL_TYPE(pCol->info.type)) {
599,951!
4186
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4187
    return TSDB_CODE_SUCCESS;
×
4188
  }
4189

4190
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
599,951!
4191
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4192
  }
4193

4194
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
599,951✔
4195

4196
  int32_t start = pInput->startRowIndex;
599,951✔
4197
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
1,204,075✔
4198
    if (colDataIsNull_s(pCol, i)) continue;
1,208,248!
4199
    char*        data = colDataGetData(pCol, i);
604,124!
4200
    SSpreadInfo* pInputInfo = (SSpreadInfo*)varDataVal(data);
604,124✔
4201
    if (pInputInfo->hasResult) {
604,124✔
4202
      spreadTransferInfo(pInputInfo, pInfo);
603,904✔
4203
    }
4204
  }
4205

4206
  if (pInfo->hasResult) {
599,951✔
4207
    GET_RES_INFO(pCtx)->numOfRes = 1;
599,797✔
4208
  }
4209

4210
  return TSDB_CODE_SUCCESS;
599,951✔
4211
}
4212

4213
int32_t spreadFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1,776,843✔
4214
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,776,843✔
4215
  if (pInfo->hasResult == true) {
1,776,843✔
4216
    SET_DOUBLE_VAL(&pInfo->result, pInfo->max - pInfo->min);
1,745,872✔
4217
  } else {
4218
    GET_RES_INFO(pCtx)->isNullRes = 1;
30,971✔
4219
  }
4220
  return functionFinalize(pCtx, pBlock);
1,776,843✔
4221
}
4222

4223
int32_t spreadPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
603,907✔
4224
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
603,907✔
4225
  SSpreadInfo*         pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
603,907✔
4226
  int32_t              resultBytes = getSpreadInfoSize();
603,907✔
4227
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
603,908!
4228

4229
  if (NULL == res) {
603,912!
4230
    return terrno;
×
4231
  }
4232
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
603,912✔
4233
  varDataSetLen(res, resultBytes);
603,912✔
4234

4235
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
603,912✔
4236
  int32_t          code = TSDB_CODE_SUCCESS;
603,912✔
4237
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
603,912✔
4238
  if (NULL == pCol) {
603,911!
4239
    code = terrno;
×
4240
    goto _exit;
×
4241
  }
4242

4243
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
603,911✔
4244
  if (TSDB_CODE_SUCCESS != code) {
603,911!
4245
    goto _exit;
×
4246
  }
4247

4248
_exit:
603,911✔
4249
  taosMemoryFree(res);
603,911!
4250
  return code;
603,911✔
4251
}
4252

4253
int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
1✔
4254
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
1✔
4255
  SSpreadInfo*         pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
1✔
4256

4257
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
1✔
4258
  SSpreadInfo*         pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
1✔
4259
  spreadTransferInfo(pSBuf, pDBuf);
1✔
4260
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
1✔
4261
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
1✔
4262
  return TSDB_CODE_SUCCESS;
1✔
4263
}
4264

4265
int32_t getElapsedInfoSize() { return (int32_t)sizeof(SElapsedInfo); }
×
4266

4267
bool getElapsedFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
103,014✔
4268
  pEnv->calcMemSize = sizeof(SElapsedInfo);
103,014✔
4269
  return true;
103,014✔
4270
}
4271

4272
int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
9,577,329✔
4273
  if (pResultInfo->initialized) {
9,577,329!
4274
    return TSDB_CODE_SUCCESS;
×
4275
  }
4276
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
9,577,329!
4277
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4278
  }
4279

4280
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
9,577,329✔
4281
  pInfo->result = 0;
9,577,329✔
4282
  pInfo->min = TSKEY_MAX;
9,577,329✔
4283
  pInfo->max = 0;
9,577,329✔
4284

4285
  if (pCtx->numOfParams > 1) {
9,577,329✔
4286
    pInfo->timeUnit = pCtx->param[1].param.i;
9,559,784✔
4287
  } else {
4288
    pInfo->timeUnit = 1;
17,545✔
4289
  }
4290

4291
  return TSDB_CODE_SUCCESS;
9,577,329✔
4292
}
4293

4294
int32_t elapsedFunction(SqlFunctionCtx* pCtx) {
9,597,614✔
4295
  int32_t numOfElems = 0;
9,597,614✔
4296

4297
  // Only the pre-computing information loaded and actual data does not loaded
4298
  SInputColumnInfoData* pInput = &pCtx->input;
9,597,614✔
4299
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
9,597,614✔
4300

4301
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
9,597,614✔
4302

4303
  numOfElems = pInput->numOfRows;  // since this is the primary timestamp, no need to exclude NULL values
9,597,614✔
4304
  if (numOfElems == 0) {
9,597,614✔
4305
    // for stream
4306
    if (pCtx->end.key != INT64_MIN) {
1,904✔
4307
      pInfo->max = pCtx->end.key + 1;
56✔
4308
    }
4309
    goto _elapsed_over;
1,904✔
4310
  }
4311

4312
  if (pInput->colDataSMAIsSet) {
9,595,710!
4313
    if (pInfo->min == TSKEY_MAX) {
×
4314
      pInfo->min = GET_INT64_VAL(&pAgg->min);
×
4315
      pInfo->max = GET_INT64_VAL(&pAgg->max);
×
4316
    } else {
4317
      if (pCtx->order == TSDB_ORDER_ASC) {
×
4318
        pInfo->max = GET_INT64_VAL(&pAgg->max);
×
4319
      } else {
4320
        pInfo->min = GET_INT64_VAL(&pAgg->min);
×
4321
      }
4322
    }
4323
  } else {  // computing based on the true data block
4324
    if (0 == pInput->numOfRows) {
9,595,710!
4325
      if (pCtx->order == TSDB_ORDER_DESC) {
×
4326
        if (pCtx->end.key != INT64_MIN) {
×
4327
          pInfo->min = pCtx->end.key;
×
4328
        }
4329
      } else {
4330
        if (pCtx->end.key != INT64_MIN) {
×
4331
          pInfo->max = pCtx->end.key + 1;
×
4332
        }
4333
      }
4334
      goto _elapsed_over;
×
4335
    }
4336

4337
    SColumnInfoData* pCol = pInput->pData[0];
9,595,710✔
4338

4339
    int32_t start = pInput->startRowIndex;
9,595,710✔
4340
    TSKEY*  ptsList = (int64_t*)colDataGetData(pCol, 0);
9,595,710!
4341
    if (pCtx->order == TSDB_ORDER_DESC) {
9,595,710✔
4342
      if (pCtx->start.key == INT64_MIN) {
698✔
4343
        pInfo->max = (pInfo->max < ptsList[start]) ? ptsList[start] : pInfo->max;
697✔
4344
      } else {
4345
        pInfo->max = pCtx->start.key + 1;
1✔
4346
      }
4347

4348
      if (pCtx->end.key == INT64_MIN) {
698✔
4349
        pInfo->min =
697✔
4350
            (pInfo->min > ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->min;
697✔
4351
      } else {
4352
        pInfo->min = pCtx->end.key;
1✔
4353
      }
4354
    } else {
4355
      if (pCtx->start.key == INT64_MIN) {
9,595,012✔
4356
        pInfo->min = (pInfo->min > ptsList[start]) ? ptsList[start] : pInfo->min;
4,153,609✔
4357
      } else {
4358
        pInfo->min = pCtx->start.key;
5,441,403✔
4359
      }
4360

4361
      if (pCtx->end.key == INT64_MIN) {
9,595,012✔
4362
        pInfo->max =
3,929,023✔
4363
            (pInfo->max < ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->max;
3,929,023✔
4364
      } else {
4365
        pInfo->max = pCtx->end.key + 1;
5,665,989✔
4366
      }
4367
    }
4368
  }
4369

4370
_elapsed_over:
9,597,614✔
4371
  // data in the check operation are all null, not output
4372
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
9,597,614✔
4373

4374
  return TSDB_CODE_SUCCESS;
9,597,614✔
4375
}
4376

4377
static void elapsedTransferInfo(SElapsedInfo* pInput, SElapsedInfo* pOutput) {
×
4378
  pOutput->timeUnit = pInput->timeUnit;
×
4379
  if (pOutput->min > pInput->min) {
×
4380
    pOutput->min = pInput->min;
×
4381
  }
4382

4383
  if (pOutput->max < pInput->max) {
×
4384
    pOutput->max = pInput->max;
×
4385
  }
4386
}
×
4387

4388
int32_t elapsedFunctionMerge(SqlFunctionCtx* pCtx) {
×
4389
  SInputColumnInfoData* pInput = &pCtx->input;
×
4390
  SColumnInfoData*      pCol = pInput->pData[0];
×
4391
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
4392
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4393
  }
4394

4395
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4396

4397
  int32_t start = pInput->startRowIndex;
×
4398

4399
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
4400
    char*         data = colDataGetData(pCol, i);
×
4401
    SElapsedInfo* pInputInfo = (SElapsedInfo*)varDataVal(data);
×
4402
    elapsedTransferInfo(pInputInfo, pInfo);
×
4403
  }
4404

4405
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
4406
  return TSDB_CODE_SUCCESS;
×
4407
}
4408

4409
int32_t elapsedFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
9,571,042✔
4410
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
9,571,042✔
4411
  double        result = (double)pInfo->max - (double)pInfo->min;
9,571,042✔
4412
  result = (result >= 0) ? result : -result;
9,571,042✔
4413
  pInfo->result = result / pInfo->timeUnit;
9,571,042✔
4414
  return functionFinalize(pCtx, pBlock);
9,571,042✔
4415
}
4416

4417
int32_t elapsedPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
4418
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
4419
  SElapsedInfo*        pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4420
  int32_t              resultBytes = getElapsedInfoSize();
×
4421
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4422

4423
  if (NULL == res) {
×
4424
    return terrno;
×
4425
  }
4426
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
4427
  varDataSetLen(res, resultBytes);
×
4428

4429
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
4430
  int32_t          code = TSDB_CODE_SUCCESS;
×
4431
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
4432
  if (NULL == pCol) {
×
4433
    code = terrno;
×
4434
    goto _exit;
×
4435
  }
4436

4437
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
4438
  if (TSDB_CODE_SUCCESS != code) {
×
4439
    goto _exit;
×
4440
  }
4441
_exit:
×
4442
  taosMemoryFree(res);
×
4443
  return code;
×
4444
}
4445

4446
int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4447
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4448
  SElapsedInfo*        pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4449

4450
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4451
  SElapsedInfo*        pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4452

4453
  elapsedTransferInfo(pSBuf, pDBuf);
×
4454
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4455
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4456
  return TSDB_CODE_SUCCESS;
×
4457
}
4458

4459
int32_t getHistogramInfoSize() {
1,953,555✔
4460
  return (int32_t)sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
1,953,555✔
4461
}
4462

4463
bool getHistogramFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
108,888✔
4464
  pEnv->calcMemSize = sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
108,888✔
4465
  return true;
108,888✔
4466
}
4467

4468
static int8_t getHistogramBinType(char* binTypeStr) {
8,221,352✔
4469
  int8_t binType;
4470
  if (strcasecmp(binTypeStr, "user_input") == 0) {
8,221,352✔
4471
    binType = USER_INPUT_BIN;
3,850✔
4472
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
8,217,502✔
4473
    binType = LINEAR_BIN;
910✔
4474
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
8,216,592!
4475
    binType = LOG_BIN;
8,216,768✔
4476
  } else {
4477
    binType = UNKNOWN_BIN;
×
4478
  }
4479

4480
  return binType;
8,221,352✔
4481
}
4482

4483
static int32_t getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t binType, bool normalized) {
8,221,421✔
4484
  cJSON*  binDesc = cJSON_Parse(binDescStr);
8,221,421✔
4485
  int32_t numOfBins;
4486
  double* intervals;
4487
  if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
8,221,442✔
4488
    int32_t numOfParams = cJSON_GetArraySize(binDesc);
8,217,559✔
4489
    int32_t startIndex;
4490
    if (numOfParams != 4) {
8,217,557!
4491
      cJSON_Delete(binDesc);
×
4492
      return TSDB_CODE_FAILED;
×
4493
    }
4494

4495
    cJSON* start = cJSON_GetObjectItem(binDesc, "start");
8,217,557✔
4496
    cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
8,217,600✔
4497
    cJSON* width = cJSON_GetObjectItem(binDesc, "width");
8,217,609✔
4498
    cJSON* count = cJSON_GetObjectItem(binDesc, "count");
8,217,088✔
4499
    cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
8,217,523✔
4500

4501
    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
8,217,631!
4502
      cJSON_Delete(binDesc);
1✔
4503
      return TSDB_CODE_FAILED;
×
4504
    }
4505

4506
    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
8,217,581!
4507
      cJSON_Delete(binDesc);
×
4508
      return TSDB_CODE_FAILED;
×
4509
    }
4510

4511
    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
8,217,582!
4512
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
8,217,582!
4513
      cJSON_Delete(binDesc);
×
4514
      return TSDB_CODE_FAILED;
×
4515
    }
4516

4517
    int32_t counter = (int32_t)count->valueint;
8,217,582✔
4518
    if (infinity->valueint == false) {
8,217,582✔
4519
      startIndex = 0;
3,085✔
4520
      numOfBins = counter + 1;
3,085✔
4521
    } else {
4522
      startIndex = 1;
8,214,497✔
4523
      numOfBins = counter + 3;
8,214,497✔
4524
    }
4525

4526
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
8,217,582!
4527
    if (NULL == intervals) {
8,217,724!
4528
      cJSON_Delete(binDesc);
×
4529
      qError("histogram function out of memory");
×
4530
      return terrno;
×
4531
    }
4532
    if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) {
8,217,724!
4533
      // linear bin process
4534
      if (width->valuedouble == 0) {
910!
4535
        taosMemoryFree(intervals);
×
4536
        cJSON_Delete(binDesc);
×
4537
        return TSDB_CODE_FAILED;
×
4538
      }
4539
      for (int i = 0; i < counter + 1; ++i) {
4,066✔
4540
        intervals[startIndex] = start->valuedouble + i * width->valuedouble;
3,156✔
4541
        if (isinf(intervals[startIndex])) {
3,156!
4542
          taosMemoryFree(intervals);
×
4543
          cJSON_Delete(binDesc);
×
4544
          return TSDB_CODE_FAILED;
×
4545
        }
4546
        startIndex++;
3,156✔
4547
      }
4548
    } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
8,216,804!
4549
      // log bin process
4550
      if (start->valuedouble == 0) {
8,216,775!
4551
        taosMemoryFree(intervals);
×
4552
        cJSON_Delete(binDesc);
×
4553
        return TSDB_CODE_FAILED;
×
4554
      }
4555
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
8,216,775!
4556
        taosMemoryFree(intervals);
2!
4557
        cJSON_Delete(binDesc);
×
4558
        return TSDB_CODE_FAILED;
×
4559
      }
4560
      for (int i = 0; i < counter + 1; ++i) {
57,510,882✔
4561
        intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
49,294,109✔
4562
        if (isinf(intervals[startIndex])) {
49,294,109!
4563
          taosMemoryFree(intervals);
×
4564
          cJSON_Delete(binDesc);
×
4565
          return TSDB_CODE_FAILED;
×
4566
        }
4567
        startIndex++;
49,294,109✔
4568
      }
4569
    } else {
4570
      taosMemoryFree(intervals);
×
4571
      cJSON_Delete(binDesc);
×
4572
      return TSDB_CODE_FAILED;
×
4573
    }
4574

4575
    if (infinity->valueint == true) {
8,217,683✔
4576
      intervals[0] = -INFINITY;
8,214,552✔
4577
      intervals[numOfBins - 1] = INFINITY;
8,214,552✔
4578
      // in case of desc bin orders, -inf/inf should be swapped
4579
      if (numOfBins < 4) {
8,214,552!
4580
        return TSDB_CODE_FAILED;
×
4581
      }
4582
      if (intervals[1] > intervals[numOfBins - 2]) {
8,214,552✔
4583
        TSWAP(intervals[0], intervals[numOfBins - 1]);
8,214,409✔
4584
      }
4585
    }
4586
  } else if (cJSON_IsArray(binDesc)) { /* user input bins */
3,850!
4587
    if (binType != USER_INPUT_BIN) {
3,849!
4588
      cJSON_Delete(binDesc);
×
4589
      return TSDB_CODE_FAILED;
×
4590
    }
4591
    numOfBins = cJSON_GetArraySize(binDesc);
3,849✔
4592
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
3,850!
4593
    if (NULL == intervals) {
3,850!
4594
      cJSON_Delete(binDesc);
×
4595
      qError("histogram function out of memory");
×
4596
      return terrno;
×
4597
    }
4598
    cJSON* bin = binDesc->child;
3,850✔
4599
    if (bin == NULL) {
3,850!
4600
      taosMemoryFree(intervals);
×
4601
      cJSON_Delete(binDesc);
×
4602
      return TSDB_CODE_FAILED;
×
4603
    }
4604
    int i = 0;
3,850✔
4605
    while (bin) {
16,943✔
4606
      intervals[i] = bin->valuedouble;
13,092✔
4607
      if (!cJSON_IsNumber(bin)) {
13,092!
4608
        taosMemoryFree(intervals);
×
4609
        cJSON_Delete(binDesc);
×
4610
        return TSDB_CODE_FAILED;
×
4611
      }
4612
      if (i != 0 && intervals[i] <= intervals[i - 1]) {
13,093!
4613
        taosMemoryFree(intervals);
×
4614
        cJSON_Delete(binDesc);
×
4615
        return TSDB_CODE_FAILED;
×
4616
      }
4617
      bin = bin->next;
13,093✔
4618
      i++;
13,093✔
4619
    }
4620
  } else {
4621
    cJSON_Delete(binDesc);
×
4622
    return TSDB_CODE_FAILED;
×
4623
  }
4624

4625
  pInfo->numOfBins = numOfBins - 1;
8,221,534✔
4626
  pInfo->normalized = normalized;
8,221,534✔
4627
  for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
65,741,464✔
4628
    pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
57,519,930✔
4629
    pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
57,519,930✔
4630
    pInfo->bins[i].count = 0;
57,519,930✔
4631
  }
4632

4633
  taosMemoryFree(intervals);
8,221,534✔
4634
  cJSON_Delete(binDesc);
8,221,398✔
4635

4636
  return TSDB_CODE_SUCCESS;
8,221,461✔
4637
}
4638

4639
int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
8,221,434✔
4640
  if (pResultInfo->initialized) {
8,221,434!
4641
    return TSDB_CODE_SUCCESS;
×
4642
  }
4643
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
8,221,434!
4644
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4645
  }
4646

4647
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
8,221,422✔
4648
  pInfo->numOfBins = 0;
8,221,422✔
4649
  pInfo->totalCount = 0;
8,221,422✔
4650
  pInfo->normalized = 0;
8,221,422✔
4651

4652
  char* binTypeStr = taosStrndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz));
8,221,422!
4653
  if (binTypeStr == NULL) {
8,221,361!
4654
    return terrno;
×
4655
  }
4656
  int8_t binType = getHistogramBinType(binTypeStr);
8,221,361✔
4657
  taosMemoryFree(binTypeStr);
8,221,477✔
4658

4659
  if (binType == UNKNOWN_BIN) {
8,221,419!
4660
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
4661
  }
4662
  char* binDesc = taosStrndup(varDataVal(pCtx->param[2].param.pz), varDataLen(pCtx->param[2].param.pz));
8,221,419!
4663
  if (binDesc == NULL) {
8,221,444!
4664
    return terrno;
×
4665
  }
4666
  int64_t normalized = pCtx->param[3].param.i;
8,221,444✔
4667
  if (normalized != 0 && normalized != 1) {
8,221,444!
4668
    taosMemoryFree(binDesc);
×
4669
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
4670
  }
4671
  int32_t code = getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized);
8,221,444✔
4672
  if (TSDB_CODE_SUCCESS != code) {
8,221,458!
4673
    taosMemoryFree(binDesc);
×
4674
    return code;
×
4675
  }
4676
  taosMemoryFree(binDesc);
8,221,458!
4677

4678
  return TSDB_CODE_SUCCESS;
8,221,613✔
4679
}
4680

4681
static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) {
8,292,679✔
4682
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
8,292,679✔
4683

4684
  SInputColumnInfoData* pInput = &pCtx->input;
8,292,679✔
4685
  SColumnInfoData*      pCol = pInput->pData[0];
8,292,679✔
4686

4687
  int32_t type = pInput->pData[0]->info.type;
8,292,679✔
4688

4689
  int32_t start = pInput->startRowIndex;
8,292,679✔
4690
  int32_t numOfRows = pInput->numOfRows;
8,292,679✔
4691

4692
  int32_t numOfElems = 0;
8,292,679✔
4693
  for (int32_t i = start; i < numOfRows + start; ++i) {
27,832,048✔
4694
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
19,539,369✔
4695
      continue;
767,625✔
4696
    }
4697

4698
    numOfElems++;
18,771,744✔
4699

4700
    char*  data = colDataGetData(pCol, i);
18,771,744!
4701
    double v;
4702
    GET_TYPED_DATA(v, double, type, data);
18,771,744!
4703

4704
    for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
56,078,047✔
4705
      if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
55,835,336✔
4706
        pInfo->bins[k].count++;
18,529,033✔
4707
        pInfo->totalCount++;
18,529,033✔
4708
        break;
18,529,033✔
4709
      }
4710
    }
4711
  }
4712

4713
  if (!isPartial) {
8,292,679✔
4714
    GET_RES_INFO(pCtx)->numOfRes = pInfo->numOfBins;
6,313,206✔
4715
  } else {
4716
    GET_RES_INFO(pCtx)->numOfRes = 1;
1,979,473✔
4717
  }
4718
  return TSDB_CODE_SUCCESS;
8,292,679✔
4719
}
4720

4721
int32_t histogramFunction(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, false); }
6,313,204✔
4722

4723
int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, true); }
1,979,641✔
4724

4725
static void histogramTransferInfo(SHistoFuncInfo* pInput, SHistoFuncInfo* pOutput) {
1,938,740✔
4726
  pOutput->normalized = pInput->normalized;
1,938,740✔
4727
  pOutput->numOfBins = pInput->numOfBins;
1,938,740✔
4728
  pOutput->totalCount += pInput->totalCount;
1,938,740✔
4729
  for (int32_t k = 0; k < pOutput->numOfBins; ++k) {
15,505,063✔
4730
    pOutput->bins[k].lower = pInput->bins[k].lower;
13,566,323✔
4731
    pOutput->bins[k].upper = pInput->bins[k].upper;
13,566,323✔
4732
    pOutput->bins[k].count += pInput->bins[k].count;
13,566,323✔
4733
  }
4734
}
1,938,740✔
4735

4736
int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx) {
1,938,739✔
4737
  SInputColumnInfoData* pInput = &pCtx->input;
1,938,739✔
4738
  SColumnInfoData*      pCol = pInput->pData[0];
1,938,739✔
4739
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
1,938,739!
4740
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4741
  }
4742

4743
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,938,739✔
4744

4745
  int32_t start = pInput->startRowIndex;
1,938,739✔
4746

4747
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
3,877,479✔
4748
    char*           data = colDataGetData(pCol, i);
1,938,740!
4749
    SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data);
1,938,740✔
4750
    histogramTransferInfo(pInputInfo, pInfo);
1,938,740✔
4751
  }
4752

4753
  SET_VAL(GET_RES_INFO(pCtx), pInfo->numOfBins, pInfo->numOfBins);
1,938,739!
4754
  return TSDB_CODE_SUCCESS;
1,938,739✔
4755
}
4756

4757
int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
8,157,185✔
4758
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,157,185✔
4759
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
8,157,185✔
4760
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
8,157,185✔
4761
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
8,157,185✔
4762
  int32_t              code = TSDB_CODE_SUCCESS;
8,157,153✔
4763

4764
  int32_t currentRow = pBlock->info.rows;
8,157,153✔
4765
  if (NULL == pCol) {
8,157,153!
4766
    return TSDB_CODE_OUT_OF_RANGE;
×
4767
  }
4768

4769
  if (pInfo->normalized) {
8,157,153✔
4770
    for (int32_t k = 0; k < pResInfo->numOfRes; ++k) {
53,131✔
4771
      if (pInfo->totalCount != 0) {
43,835✔
4772
        pInfo->bins[k].percentage = pInfo->bins[k].count / (double)pInfo->totalCount;
35,687✔
4773
      } else {
4774
        pInfo->bins[k].percentage = 0;
8,148✔
4775
      }
4776
    }
4777
  }
4778

4779
  for (int32_t i = 0; i < pResInfo->numOfRes; ++i) {
65,216,526✔
4780
    int32_t len;
4781
    char    buf[512] = {0};
57,070,820✔
4782
    if (!pInfo->normalized) {
57,070,820✔
4783
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE,
57,026,981✔
4784
                      "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%" PRId64 "}", pInfo->bins[i].lower,
4785
                      pInfo->bins[i].upper, pInfo->bins[i].count);
4786
    } else {
4787
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE,
43,839✔
4788
                      "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%lf}", pInfo->bins[i].lower, pInfo->bins[i].upper,
4789
                      pInfo->bins[i].percentage);
4790
    }
4791
    varDataSetLen(buf, len);
57,069,562✔
4792
    code = colDataSetVal(pCol, currentRow, buf, false);
57,069,562✔
4793
    if (TSDB_CODE_SUCCESS != code) {
57,059,373!
4794
      return code;
×
4795
    }
4796
    currentRow++;
57,059,373✔
4797
  }
4798

4799
  return code;
8,145,706✔
4800
}
4801

4802
int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1,947,420✔
4803
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,947,420✔
4804
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,947,420✔
4805
  int32_t              resultBytes = getHistogramInfoSize();
1,947,420✔
4806
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
1,947,420!
4807

4808
  if (NULL == res) {
1,947,420!
4809
    return terrno;
×
4810
  }
4811
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
1,947,420✔
4812
  varDataSetLen(res, resultBytes);
1,947,420✔
4813

4814
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
1,947,420✔
4815
  int32_t          code = TSDB_CODE_SUCCESS;
1,947,420✔
4816
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
1,947,420✔
4817
  if (NULL == pCol) {
1,947,420!
4818
    code = terrno;
×
4819
    goto _exit;
×
4820
  }
4821
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
1,947,420✔
4822

4823
_exit:
1,947,420✔
4824
  taosMemoryFree(res);
1,947,420!
4825
  return code;
1,947,420✔
4826
}
4827

4828
int32_t histogramCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4829
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4830
  SHistoFuncInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4831

4832
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4833
  SHistoFuncInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4834

4835
  histogramTransferInfo(pSBuf, pDBuf);
×
4836
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4837
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4838
  return TSDB_CODE_SUCCESS;
×
4839
}
4840

4841
int32_t getHLLInfoSize() { return (int32_t)sizeof(SHLLInfo); }
10,125✔
4842

4843
bool getHLLFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
66,330✔
4844
  pEnv->calcMemSize = sizeof(SHLLInfo);
66,330✔
4845
  return true;
66,330✔
4846
}
4847

4848
static uint8_t hllCountNum(void* data, int32_t bytes, int32_t* buk) {
3,633,117✔
4849
  uint64_t hash = MurmurHash3_64(data, bytes);
3,633,117✔
4850
  int32_t  index = hash & HLL_BUCKET_MASK;
3,631,878✔
4851
  hash >>= HLL_BUCKET_BITS;
3,631,878✔
4852
  hash |= ((uint64_t)1 << HLL_DATA_BITS);
3,631,878✔
4853
  uint64_t bit = 1;
3,631,878✔
4854
  uint8_t  count = 1;
3,631,878✔
4855
  while ((hash & bit) == 0) {
7,613,639✔
4856
    count++;
3,981,761✔
4857
    bit <<= 1;
3,981,761✔
4858
  }
4859
  *buk = index;
3,631,878✔
4860
  return count;
3,631,878✔
4861
}
4862

4863
static void hllBucketHisto(uint8_t* buckets, int32_t* bucketHisto) {
243,542✔
4864
  uint64_t* word = (uint64_t*)buckets;
243,542✔
4865
  uint8_t*  bytes;
4866

4867
  for (int32_t j = 0; j < HLL_BUCKETS >> 3; j++) {
492,128,297✔
4868
    if (*word == 0) {
491,884,755✔
4869
      bucketHisto[0] += 8;
490,970,831✔
4870
    } else {
4871
      bytes = (uint8_t*)word;
913,924✔
4872
      bucketHisto[bytes[0]]++;
913,924✔
4873
      bucketHisto[bytes[1]]++;
913,924✔
4874
      bucketHisto[bytes[2]]++;
913,924✔
4875
      bucketHisto[bytes[3]]++;
913,924✔
4876
      bucketHisto[bytes[4]]++;
913,924✔
4877
      bucketHisto[bytes[5]]++;
913,924✔
4878
      bucketHisto[bytes[6]]++;
913,924✔
4879
      bucketHisto[bytes[7]]++;
913,924✔
4880
    }
4881
    word++;
491,884,755✔
4882
  }
4883
}
243,542✔
4884
static double hllTau(double x) {
243,543✔
4885
  if (x == 0. || x == 1.) return 0.;
243,543!
4886
  double zPrime;
4887
  double y = 1.0;
×
4888
  double z = 1 - x;
×
4889
  do {
4890
    x = sqrt(x);
×
4891
    zPrime = z;
×
4892
    y *= 0.5;
×
4893
    z -= pow(1 - x, 2) * y;
×
4894
  } while (zPrime != z);
×
4895
  return z / 3;
×
4896
}
4897

4898
static double hllSigma(double x) {
243,554✔
4899
  if (x == 1.0) return INFINITY;
243,554✔
4900
  double zPrime;
4901
  double y = 1;
216,408✔
4902
  double z = x;
216,408✔
4903
  do {
4904
    x *= x;
4,243,401✔
4905
    zPrime = z;
4,243,401✔
4906
    z += x * y;
4,243,401✔
4907
    y += y;
4,243,401✔
4908
  } while (zPrime != z);
4,243,401✔
4909
  return z;
216,408✔
4910
}
4911

4912
// estimate the cardinality, the algorithm refer this paper: "New cardinality estimation algorithms for HyperLogLog
4913
// sketches"
4914
static uint64_t hllCountCnt(uint8_t* buckets) {
243,525✔
4915
  double  m = HLL_BUCKETS;
243,525✔
4916
  int32_t buckethisto[64] = {0};
243,525✔
4917
  hllBucketHisto(buckets, buckethisto);
243,525✔
4918

4919
  double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m);
243,546✔
4920
  for (int j = HLL_DATA_BITS; j >= 1; --j) {
12,417,697✔
4921
    z += buckethisto[j];
12,174,144✔
4922
    z *= 0.5;
12,174,144✔
4923
  }
4924

4925
  z += m * hllSigma(buckethisto[0] / (double)m);
243,553✔
4926
  double E = (double)llroundl(HLL_ALPHA_INF * m * m / z);
243,561✔
4927

4928
  return (uint64_t)E;
243,561✔
4929
}
4930

4931
int32_t hllFunction(SqlFunctionCtx* pCtx) {
268,328✔
4932
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
268,328✔
4933

4934
  SInputColumnInfoData* pInput = &pCtx->input;
268,328✔
4935
  SColumnInfoData*      pCol = pInput->pData[0];
268,328✔
4936

4937
  int32_t type = pCol->info.type;
268,328✔
4938
  int32_t bytes = pCol->info.bytes;
268,328✔
4939

4940
  int32_t start = pInput->startRowIndex;
268,328✔
4941
  int32_t numOfRows = pInput->numOfRows;
268,328✔
4942

4943
  int32_t numOfElems = 0;
268,328✔
4944
  if (IS_NULL_TYPE(type)) {
268,328✔
4945
    goto _hll_over;
1,572✔
4946
  }
4947

4948
  for (int32_t i = start; i < numOfRows + start; ++i) {
4,846,215✔
4949
    if (pCol->hasNull && colDataIsNull_s(pCol, i)) {
6,313,329!
4950
      continue;
946,539✔
4951
    }
4952

4953
    numOfElems++;
3,634,058✔
4954

4955
    char* data = colDataGetData(pCol, i);
3,634,058!
4956
    if (IS_VAR_DATA_TYPE(type)) {
3,634,058!
4957
      bytes = varDataLen(data);
1,079,839✔
4958
      data = varDataVal(data);
1,079,839✔
4959
    }
4960

4961
    int32_t index = 0;
3,634,058✔
4962
    uint8_t count = hllCountNum(data, bytes, &index);
3,634,058✔
4963
    uint8_t oldcount = pInfo->buckets[index];
3,632,920✔
4964
    if (count > oldcount) {
3,632,920✔
4965
      pInfo->buckets[index] = count;
940,657✔
4966
    }
4967
  }
4968

4969
_hll_over:
265,618✔
4970
  pInfo->totalCount += numOfElems;
267,190✔
4971

4972
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
267,190✔
4973
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
2,307✔
4974
  } else {
4975
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
264,883✔
4976
  }
4977

4978
  return TSDB_CODE_SUCCESS;
267,190✔
4979
}
4980

4981
static void hllTransferInfo(SHLLInfo* pInput, SHLLInfo* pOutput) {
10,410✔
4982
  for (int32_t k = 0; k < HLL_BUCKETS; ++k) {
166,548,057✔
4983
    if (pOutput->buckets[k] < pInput->buckets[k]) {
166,537,647✔
4984
      pOutput->buckets[k] = pInput->buckets[k];
260,040✔
4985
    }
4986
  }
4987
  pOutput->totalCount += pInput->totalCount;
10,410✔
4988
}
10,410✔
4989

4990
int32_t hllFunctionMerge(SqlFunctionCtx* pCtx) {
10,355✔
4991
  SInputColumnInfoData* pInput = &pCtx->input;
10,355✔
4992
  SColumnInfoData*      pCol = pInput->pData[0];
10,355✔
4993

4994
  if (IS_NULL_TYPE(pCol->info.type)) {
10,355!
4995
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4996
    return TSDB_CODE_SUCCESS;
×
4997
  }
4998

4999
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
10,355!
5000
    return TSDB_CODE_SUCCESS;
×
5001
  }
5002

5003
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
10,355✔
5004

5005
  int32_t start = pInput->startRowIndex;
10,355✔
5006

5007
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
20,764✔
5008
    if (colDataIsNull_s(pCol, i)) continue;
20,818!
5009
    char*     data = colDataGetData(pCol, i);
10,409!
5010
    SHLLInfo* pInputInfo = (SHLLInfo*)varDataVal(data);
10,409✔
5011
    hllTransferInfo(pInputInfo, pInfo);
10,409✔
5012
  }
5013

5014
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
10,355✔
5015
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
3✔
5016
  } else {
5017
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
10,352✔
5018
  }
5019

5020
  return TSDB_CODE_SUCCESS;
10,355✔
5021
}
5022

5023
int32_t hllFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
243,523✔
5024
  SResultRowEntryInfo* pInfo = GET_RES_INFO(pCtx);
243,523✔
5025

5026
  SHLLInfo* pHllInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
243,523✔
5027
  pHllInfo->result = hllCountCnt(pHllInfo->buckets);
243,523✔
5028
  if (tsCountAlwaysReturnValue && pHllInfo->result == 0) {
243,561✔
5029
    pInfo->numOfRes = 1;
24,861✔
5030
  }
5031

5032
  return functionFinalize(pCtx, pBlock);
243,561✔
5033
}
5034

5035
int32_t hllPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
10,125✔
5036
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
10,125✔
5037
  SHLLInfo*            pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
10,125✔
5038
  int32_t              resultBytes = getHLLInfoSize();
10,125✔
5039
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
10,125!
5040

5041
  if (NULL == res) {
10,126!
5042
    return terrno;
×
5043
  }
5044
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
10,126✔
5045
  varDataSetLen(res, resultBytes);
10,126✔
5046

5047
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
10,126✔
5048
  int32_t          code = TSDB_CODE_SUCCESS;
10,126✔
5049
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
10,126✔
5050
  if (NULL == pCol) {
10,128!
5051
    code = terrno;
×
5052
    goto _exit;
×
5053
  }
5054

5055
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
10,128✔
5056

5057
_exit:
10,128✔
5058
  taosMemoryFree(res);
10,128!
5059
  return code;
10,128✔
5060
}
5061

5062
int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
1✔
5063
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
1✔
5064
  SHLLInfo*            pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
1✔
5065

5066
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
1✔
5067
  SHLLInfo*            pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
1✔
5068

5069
  hllTransferInfo(pSBuf, pDBuf);
1✔
5070
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
1✔
5071
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
1✔
5072
  return TSDB_CODE_SUCCESS;
1✔
5073
}
5074

5075
bool getStateFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
19,271✔
5076
  pEnv->calcMemSize = sizeof(SStateInfo);
19,271✔
5077
  return true;
19,271✔
5078
}
5079

5080
static int8_t getStateOpType(char* opStr) {
140,259✔
5081
  int8_t opType;
5082
  if (strncasecmp(opStr, "LT", 2) == 0) {
140,259✔
5083
    opType = STATE_OPER_LT;
756✔
5084
  } else if (strncasecmp(opStr, "GT", 2) == 0) {
139,503✔
5085
    opType = STATE_OPER_GT;
135,715✔
5086
  } else if (strncasecmp(opStr, "LE", 2) == 0) {
3,788✔
5087
    opType = STATE_OPER_LE;
496✔
5088
  } else if (strncasecmp(opStr, "GE", 2) == 0) {
3,292✔
5089
    opType = STATE_OPER_GE;
1,445✔
5090
  } else if (strncasecmp(opStr, "NE", 2) == 0) {
1,847✔
5091
    opType = STATE_OPER_NE;
496✔
5092
  } else if (strncasecmp(opStr, "EQ", 2) == 0) {
1,351!
5093
    opType = STATE_OPER_EQ;
1,351✔
5094
  } else {
5095
    opType = STATE_OPER_INVALID;
×
5096
  }
5097

5098
  return opType;
140,259✔
5099
}
5100

5101
static bool checkStateOp(int8_t op, SColumnInfoData* pCol, int32_t index, SVariant param) {
35,068,991✔
5102
  char* data = colDataGetData(pCol, index);
35,068,991!
5103
  switch (pCol->info.type) {
35,068,991!
5104
    case TSDB_DATA_TYPE_TINYINT: {
142,548✔
5105
      int8_t v = *(int8_t*)data;
142,548✔
5106
      STATE_COMP(op, v, param);
142,548!
5107
      break;
×
5108
    }
5109
    case TSDB_DATA_TYPE_UTINYINT: {
2,880✔
5110
      uint8_t v = *(uint8_t*)data;
2,880✔
5111
      STATE_COMP(op, v, param);
2,880!
5112
      break;
×
5113
    }
5114
    case TSDB_DATA_TYPE_SMALLINT: {
46,956✔
5115
      int16_t v = *(int16_t*)data;
46,956✔
5116
      STATE_COMP(op, v, param);
46,956!
5117
      break;
×
5118
    }
5119
    case TSDB_DATA_TYPE_USMALLINT: {
2,880✔
5120
      uint16_t v = *(uint16_t*)data;
2,880✔
5121
      STATE_COMP(op, v, param);
2,880!
5122
      break;
×
5123
    }
5124
    case TSDB_DATA_TYPE_INT: {
22,465,779✔
5125
      int32_t v = *(int32_t*)data;
22,465,779✔
5126
      STATE_COMP(op, v, param);
22,465,779!
5127
      break;
×
5128
    }
5129
    case TSDB_DATA_TYPE_UINT: {
2,880✔
5130
      uint32_t v = *(uint32_t*)data;
2,880✔
5131
      STATE_COMP(op, v, param);
2,880!
5132
      break;
×
5133
    }
5134
    case TSDB_DATA_TYPE_BIGINT: {
70,284✔
5135
      int64_t v = *(int64_t*)data;
70,284✔
5136
      STATE_COMP(op, v, param);
70,284!
5137
      break;
×
5138
    }
5139
    case TSDB_DATA_TYPE_UBIGINT: {
2,880✔
5140
      uint64_t v = *(uint64_t*)data;
2,880✔
5141
      STATE_COMP(op, v, param);
2,880!
5142
      break;
×
5143
    }
5144
    case TSDB_DATA_TYPE_FLOAT: {
6,966,566✔
5145
      float v = *(float*)data;
6,966,566✔
5146
      STATE_COMP(op, v, param);
6,966,566!
5147
      break;
×
5148
    }
5149
    case TSDB_DATA_TYPE_DOUBLE: {
5,366,923✔
5150
      double v = *(double*)data;
5,366,923✔
5151
      STATE_COMP(op, v, param);
5,366,923!
5152
      break;
×
5153
    }
5154
    default: {
×
5155
      return false;
×
5156
    }
5157
  }
5158
  return false;
×
5159
}
5160

5161
int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
133,783✔
5162
  int32_t              code = TSDB_CODE_SUCCESS;
133,783✔
5163
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
133,783✔
5164
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
133,783✔
5165

5166
  SInputColumnInfoData* pInput = &pCtx->input;
133,783✔
5167
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
133,783✔
5168

5169
  SColumnInfoData* pInputCol = pInput->pData[0];
133,783✔
5170

5171
  int32_t          numOfElems = 0;
133,783✔
5172
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
133,783✔
5173

5174
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
133,783✔
5175
  if (STATE_OPER_INVALID == op) {
133,783!
5176
    return 0;
×
5177
  }
5178

5179
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
34,914,667✔
5180
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
34,780,884!
5181
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5182
    } else {
5183
      pInfo->prevTs = tsList[i];
34,780,884✔
5184
    }
5185

5186
    pInfo->isPrevTsSet = true;
34,780,884✔
5187
    numOfElems++;
34,780,884✔
5188

5189
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
34,780,884✔
5190
      colDataSetNULL(pOutput, i);
160,312!
5191
      // handle selectivity
5192
      if (pCtx->subsidiaries.num > 0) {
160,312✔
5193
        code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
18✔
5194
        if (TSDB_CODE_SUCCESS != code) {
18!
5195
          return code;
×
5196
        }
5197
      }
5198
      continue;
160,312✔
5199
    }
5200

5201
    bool ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
34,620,572✔
5202

5203
    int64_t output = -1;
34,620,572✔
5204
    if (ret) {
34,620,572✔
5205
      output = ++pInfo->count;
16,139,416✔
5206
    } else {
5207
      pInfo->count = 0;
18,481,156✔
5208
    }
5209
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
34,620,572✔
5210
    if (TSDB_CODE_SUCCESS != code) {
34,620,572!
5211
      return code;
×
5212
    }
5213

5214
    // handle selectivity
5215
    if (pCtx->subsidiaries.num > 0) {
34,620,572✔
5216
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
20,018,796✔
5217
      if (TSDB_CODE_SUCCESS != code) {
20,018,796!
5218
        return code;
×
5219
      }
5220
    }
5221
  }
5222

5223
  pResInfo->numOfRes = numOfElems;
133,783✔
5224
  return TSDB_CODE_SUCCESS;
133,783✔
5225
}
5226

5227
int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
6,476✔
5228
  int32_t              code = TSDB_CODE_SUCCESS;
6,476✔
5229
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
6,476✔
5230
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
6,476✔
5231

5232
  SInputColumnInfoData* pInput = &pCtx->input;
6,476✔
5233
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
6,476✔
5234

5235
  SColumnInfoData* pInputCol = pInput->pData[0];
6,476✔
5236

5237
  int32_t          numOfElems = 0;
6,476✔
5238
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
6,476✔
5239

5240
  // TODO: process timeUnit for different db precisions
5241
  int32_t timeUnit = 1;
6,476✔
5242
  if (pCtx->numOfParams == 5) {  // TODO: param number incorrect
6,476✔
5243
    timeUnit = pCtx->param[3].param.i;
5,494✔
5244
  }
5245

5246
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
6,476✔
5247
  if (STATE_OPER_INVALID == op) {
6,476!
5248
    return TSDB_CODE_INVALID_PARA;
×
5249
  }
5250

5251
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
653,559✔
5252
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
647,061!
5253
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5254
    } else {
5255
      pInfo->prevTs = tsList[i];
647,061✔
5256
    }
5257

5258
    pInfo->isPrevTsSet = true;
647,061✔
5259
    numOfElems++;
647,061✔
5260

5261
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
647,061✔
5262
      colDataSetNULL(pOutput, i);
198,292!
5263
      // handle selectivity
5264
      if (pCtx->subsidiaries.num > 0) {
198,292✔
5265
        code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
26✔
5266
        if (TSDB_CODE_SUCCESS != code) {
26!
5267
          return code;
×
5268
        }
5269
      }
5270
      continue;
198,292✔
5271
    }
5272

5273
    bool    ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
448,769✔
5274
    int64_t output = -1;
448,703✔
5275
    if (ret) {
448,703✔
5276
      if (pInfo->durationStart == 0) {
197,422✔
5277
        output = 0;
78,311✔
5278
        pInfo->durationStart = tsList[i];
78,311✔
5279
      } else {
5280
        output = (tsList[i] - pInfo->durationStart) / timeUnit;
119,111✔
5281
      }
5282
    } else {
5283
      pInfo->durationStart = 0;
251,281✔
5284
    }
5285
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
448,703✔
5286
    if (TSDB_CODE_SUCCESS != code) {
448,791!
5287
      return code;
×
5288
    }
5289

5290
    // handle selectivity
5291
    if (pCtx->subsidiaries.num > 0) {
448,791✔
5292
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
312✔
5293
      if (TSDB_CODE_SUCCESS != code) {
312!
5294
        return code;
×
5295
      }
5296
    }
5297
  }
5298

5299
  pResInfo->numOfRes = numOfElems;
6,498✔
5300
  return TSDB_CODE_SUCCESS;
6,498✔
5301
}
5302

5303
bool getCsumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
15,657✔
5304
  pEnv->calcMemSize = sizeof(SSumRes);
15,657✔
5305
  return true;
15,657✔
5306
}
5307

5308
int32_t csumFunction(SqlFunctionCtx* pCtx) {
19,275✔
5309
  int32_t              code = TSDB_CODE_SUCCESS;
19,275✔
5310
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
19,275✔
5311
  SSumRes*             pSumRes = GET_ROWCELL_INTERBUF(pResInfo);
19,275✔
5312

5313
  SInputColumnInfoData* pInput = &pCtx->input;
19,275✔
5314
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
19,275✔
5315

5316
  SColumnInfoData* pInputCol = pInput->pData[0];
19,275✔
5317
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
19,275✔
5318

5319
  int32_t numOfElems = 0;
19,275✔
5320
  int32_t type = pInputCol->info.type;
19,275✔
5321
  int32_t startOffset = pCtx->offset;
19,275✔
5322
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
3,997,692✔
5323
    if (pSumRes->isPrevTsSet == true && tsList[i] == pSumRes->prevTs) {
3,978,357✔
5324
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
18✔
5325
    } else {
5326
      pSumRes->prevTs = tsList[i];
3,978,339✔
5327
    }
5328
    pSumRes->isPrevTsSet = true;
3,978,339✔
5329

5330
    int32_t pos = startOffset + numOfElems;
3,978,339✔
5331
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
3,978,339✔
5332
      // colDataSetNULL(pOutput, i);
5333
      continue;
347,472✔
5334
    }
5335

5336
    char* data = colDataGetData(pInputCol, i);
3,630,867!
5337
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
7,015,905!
5338
      int64_t v;
5339
      GET_TYPED_DATA(v, int64_t, type, data);
3,384,960!
5340
      pSumRes->isum += v;
3,384,960✔
5341
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false);
3,384,960✔
5342
      if (TSDB_CODE_SUCCESS != code) {
3,385,038!
5343
        return code;
×
5344
      }
5345
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
246,587!
5346
      uint64_t v;
5347
      GET_TYPED_DATA(v, uint64_t, type, data);
680!
5348
      pSumRes->usum += v;
680✔
5349
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->usum, false);
680✔
5350
      if (TSDB_CODE_SUCCESS != code) {
680!
5351
        return code;
×
5352
      }
5353
    } else if (IS_FLOAT_TYPE(type)) {
245,227!
5354
      double v;
5355
      GET_TYPED_DATA(v, double, type, data);
246,300!
5356
      pSumRes->dsum += v;
246,300✔
5357
      // check for overflow
5358
      if (isinf(pSumRes->dsum) || isnan(pSumRes->dsum)) {
246,300!
5359
        colDataSetNULL(pOutput, pos);
8!
5360
      } else {
5361
        code = colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false);
246,292✔
5362
        if (TSDB_CODE_SUCCESS != code) {
246,292!
5363
          return code;
×
5364
        }
5365
      }
5366
    }
5367

5368
    // handle selectivity
5369
    if (pCtx->subsidiaries.num > 0) {
3,630,945✔
5370
      code = appendSelectivityValue(pCtx, i, pos);
2,002,298✔
5371
      if (TSDB_CODE_SUCCESS != code) {
2,002,298!
5372
        return code;
×
5373
      }
5374
    }
5375

5376
    numOfElems++;
3,630,945✔
5377
  }
5378

5379
  pResInfo->numOfRes = numOfElems;
19,335✔
5380
  return TSDB_CODE_SUCCESS;
19,335✔
5381
}
5382

5383
bool getMavgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
13,585✔
5384
  pEnv->calcMemSize = sizeof(SMavgInfo) + MAVG_MAX_POINTS_NUM * sizeof(double);
13,585✔
5385
  return true;
13,585✔
5386
}
5387

5388
int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
66,878✔
5389
  if (pResultInfo->initialized) {
66,878✔
5390
    return TSDB_CODE_SUCCESS;
52,608✔
5391
  }
5392
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
14,270!
5393
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5394
  }
5395

5396
  SMavgInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
14,273✔
5397
  pInfo->pos = 0;
14,273✔
5398
  pInfo->sum = 0;
14,273✔
5399
  pInfo->prevTs = -1;
14,273✔
5400
  pInfo->isPrevTsSet = false;
14,273✔
5401
  pInfo->numOfPoints = pCtx->param[1].param.i;
14,273✔
5402
  if (pInfo->numOfPoints < 1 || pInfo->numOfPoints > MAVG_MAX_POINTS_NUM) {
14,273!
5403
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
1✔
5404
  }
5405
  pInfo->pointsMeet = false;
14,272✔
5406

5407
  return TSDB_CODE_SUCCESS;
14,272✔
5408
}
5409

5410
int32_t mavgFunction(SqlFunctionCtx* pCtx) {
53,294✔
5411
  int32_t              code = TSDB_CODE_SUCCESS;
53,294✔
5412
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
53,294✔
5413
  SMavgInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
53,294✔
5414

5415
  SInputColumnInfoData* pInput = &pCtx->input;
53,294✔
5416
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
53,294✔
5417

5418
  SColumnInfoData* pInputCol = pInput->pData[0];
53,294✔
5419
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
53,294✔
5420
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
53,294✔
5421

5422
  int32_t numOfElems = 0;
53,294✔
5423
  int32_t type = pInputCol->info.type;
53,294✔
5424
  int32_t startOffset = pCtx->offset;
53,294✔
5425
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
58,631,303✔
5426
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
58,578,009!
5427
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5428
    } else {
5429
      pInfo->prevTs = tsList[i];
58,578,009✔
5430
    }
5431
    pInfo->isPrevTsSet = true;
58,578,009✔
5432

5433
    int32_t pos = startOffset + numOfElems;
58,578,009✔
5434
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
58,578,009✔
5435
      // colDataSetNULL(pOutput, i);
5436
      continue;
371,795✔
5437
    }
5438

5439
    char*  data = colDataGetData(pInputCol, i);
58,206,214!
5440
    double v;
5441
    GET_TYPED_DATA(v, double, type, data);
58,206,214!
5442

5443
    if (!pInfo->pointsMeet && (pInfo->pos < pInfo->numOfPoints - 1)) {
58,206,214✔
5444
      pInfo->points[pInfo->pos] = v;
3,608,900✔
5445
      pInfo->sum += v;
3,608,900✔
5446
    } else {
5447
      if (!pInfo->pointsMeet && (pInfo->pos == pInfo->numOfPoints - 1)) {
54,597,314!
5448
        pInfo->sum += v;
10,335✔
5449
        pInfo->pointsMeet = true;
10,335✔
5450
      } else {
5451
        pInfo->sum = pInfo->sum + v - pInfo->points[pInfo->pos];
54,586,979✔
5452
      }
5453

5454
      pInfo->points[pInfo->pos] = v;
54,597,314✔
5455
      double result = pInfo->sum / pInfo->numOfPoints;
54,597,314✔
5456
      // check for overflow
5457
      if (isinf(result) || isnan(result)) {
54,597,314!
5458
        colDataSetNULL(pOutput, pos);
×
5459
      } else {
5460
        code = colDataSetVal(pOutput, pos, (char*)&result, false);
54,597,314✔
5461
        if (TSDB_CODE_SUCCESS != code) {
54,597,314!
5462
          return code;
×
5463
        }
5464
      }
5465

5466
      // handle selectivity
5467
      if (pCtx->subsidiaries.num > 0) {
54,597,314✔
5468
        code = appendSelectivityValue(pCtx, i, pos);
34,497,505✔
5469
        if (TSDB_CODE_SUCCESS != code) {
34,497,505!
5470
          return code;
×
5471
        }
5472
      }
5473

5474
      numOfElems++;
54,597,314✔
5475
    }
5476

5477
    pInfo->pos++;
58,206,214✔
5478
    if (pInfo->pos == pInfo->numOfPoints) {
58,206,214✔
5479
      pInfo->pos = 0;
436,968✔
5480
    }
5481
  }
5482

5483
  pResInfo->numOfRes = numOfElems;
53,294✔
5484
  return TSDB_CODE_SUCCESS;
53,294✔
5485
}
5486

5487
static SSampleInfo* getSampleOutputInfo(SqlFunctionCtx* pCtx) {
14,023,166✔
5488
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
14,023,166✔
5489
  SSampleInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
14,023,166✔
5490

5491
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
14,023,166✔
5492
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
14,023,166✔
5493

5494
  return pInfo;
14,023,166✔
5495
}
5496

5497
bool getSampleFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
29,964✔
5498
  SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
29,964✔
5499
  SValueNode*  pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
29,968✔
5500
  int32_t      numOfSamples = pVal->datum.i;
29,971✔
5501
  pEnv->calcMemSize = sizeof(SSampleInfo) + numOfSamples * (pCol->node.resType.bytes + sizeof(STuplePos));
29,971✔
5502
  return true;
29,971✔
5503
}
5504

5505
int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
7,025,292✔
5506
  if (pResultInfo->initialized) {
7,025,292!
5507
    return TSDB_CODE_SUCCESS;
×
5508
  }
5509
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
7,025,292!
5510
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5511
  }
5512

5513
  taosSeedRand(taosSafeRand());
7,025,294✔
5514

5515
  SSampleInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
7,025,296✔
5516
  pInfo->samples = pCtx->param[1].param.i;
7,025,296✔
5517
  pInfo->totalPoints = 0;
7,025,296✔
5518
  pInfo->numSampled = 0;
7,025,296✔
5519
  pInfo->colType = pCtx->resDataInfo.type;
7,025,296✔
5520
  pInfo->colBytes = pCtx->resDataInfo.bytes;
7,025,296✔
5521
  pInfo->nullTuplePos.pageId = -1;
7,025,296✔
5522
  pInfo->nullTupleSaved = false;
7,025,296✔
5523
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
7,025,296✔
5524
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
7,025,296✔
5525

5526
  return TSDB_CODE_SUCCESS;
7,025,296✔
5527
}
5528

5529
static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) {
15,917,952✔
5530
  assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType);
15,917,952✔
5531
}
15,917,963✔
5532

5533
static int32_t doReservoirSample(SqlFunctionCtx* pCtx, SSampleInfo* pInfo, char* data, int32_t index) {
17,506,245✔
5534
  pInfo->totalPoints++;
17,506,245✔
5535
  if (pInfo->numSampled < pInfo->samples) {
17,506,245✔
5536
    sampleAssignResult(pInfo, data, pInfo->numSampled);
14,064,254✔
5537
    if (pCtx->subsidiaries.num > 0) {
14,064,278✔
5538
      int32_t code = saveTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[pInfo->numSampled]);
1,626,561✔
5539
      if (code != TSDB_CODE_SUCCESS) {
1,626,484!
5540
        return code;
×
5541
      }
5542
    }
5543
    pInfo->numSampled++;
14,064,201✔
5544
  } else {
5545
    int32_t j = taosRand() % (pInfo->totalPoints);
3,441,991✔
5546
    if (j < pInfo->samples) {
3,442,395✔
5547
      sampleAssignResult(pInfo, data, j);
1,853,848✔
5548
      if (pCtx->subsidiaries.num > 0) {
1,853,847✔
5549
        int32_t code = updateTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[j]);
895,039✔
5550
        if (code != TSDB_CODE_SUCCESS) {
894,764!
5551
          return code;
×
5552
        }
5553
      }
5554
    }
5555
  }
5556

5557
  return TSDB_CODE_SUCCESS;
17,506,320✔
5558
}
5559

5560
int32_t sampleFunction(SqlFunctionCtx* pCtx) {
7,118,704✔
5561
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
7,118,704✔
5562
  SSampleInfo*         pInfo = getSampleOutputInfo(pCtx);
7,118,704✔
5563

5564
  SInputColumnInfoData* pInput = &pCtx->input;
7,118,704✔
5565

5566
  SColumnInfoData* pInputCol = pInput->pData[0];
7,118,704✔
5567
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
24,956,528✔
5568
    if (colDataIsNull_s(pInputCol, i)) {
35,672,016✔
5569
      continue;
331,653✔
5570
    }
5571

5572
    char*   data = colDataGetData(pInputCol, i);
17,504,355!
5573
    int32_t code = doReservoirSample(pCtx, pInfo, data, i);
17,504,355✔
5574
    if (code != TSDB_CODE_SUCCESS) {
17,506,171!
5575
      return code;
×
5576
    }
5577
  }
5578

5579
  if (pInfo->numSampled == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
7,120,520✔
5580
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
12✔
5581
    if (code != TSDB_CODE_SUCCESS) {
12!
5582
      return code;
×
5583
    }
5584
    pInfo->nullTupleSaved = true;
12✔
5585
  }
5586

5587
  SET_VAL(pResInfo, pInfo->numSampled, pInfo->numSampled);
7,120,520✔
5588
  return TSDB_CODE_SUCCESS;
7,120,520✔
5589
}
5590

5591
int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
6,904,464✔
5592
  int32_t              code = TSDB_CODE_SUCCESS;
6,904,464✔
5593
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
6,904,464✔
5594

5595
  SSampleInfo* pInfo = getSampleOutputInfo(pCtx);
6,904,464✔
5596
  pEntryInfo->complete = true;
6,904,464✔
5597

5598
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
6,904,464✔
5599
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
6,904,464✔
5600
  if (NULL == pCol) {
6,904,464!
5601
    return TSDB_CODE_OUT_OF_RANGE;
×
5602
  }
5603

5604
  int32_t currentRow = pBlock->info.rows;
6,904,464✔
5605
  if (pInfo->numSampled == 0) {
6,904,464✔
5606
    colDataSetNULL(pCol, currentRow);
1,961✔
5607
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
1,961✔
5608
    return code;
1,961✔
5609
  }
5610
  for (int32_t i = 0; i < pInfo->numSampled; ++i) {
20,735,906✔
5611
    code = colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false);
13,833,649✔
5612
    if (TSDB_CODE_SUCCESS != code) {
13,833,441!
5613
      return code;
×
5614
    }
5615
    code = setSelectivityValue(pCtx, pBlock, &pInfo->tuplePos[i], currentRow + i);
13,833,441✔
5616
    if (TSDB_CODE_SUCCESS != code) {
13,833,403!
5617
      return code;
×
5618
    }
5619
  }
5620

5621
  return code;
6,902,257✔
5622
}
5623

5624
bool getTailFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
5625
#if 0
5626
  SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
5627
  SValueNode*  pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
5628
  int32_t      numOfPoints = pVal->datum.i;
5629
  pEnv->calcMemSize = sizeof(STailInfo) + numOfPoints * (POINTER_BYTES + sizeof(STailItem) + pCol->node.resType.bytes);
5630
#endif
5631
  return true;
×
5632
}
5633

5634
int32_t tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
5635
#if 0
5636
  if (!functionSetup(pCtx, pResultInfo)) {
5637
    return false;
5638
  }
5639

5640
  STailInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
5641
  pInfo->numAdded = 0;
5642
  pInfo->numOfPoints = pCtx->param[1].param.i;
5643
  if (pCtx->numOfParams == 4) {
5644
    pInfo->offset = pCtx->param[2].param.i;
5645
  } else {
5646
    pInfo->offset = 0;
5647
  }
5648
  pInfo->colType = pCtx->resDataInfo.type;
5649
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5650
  if ((pInfo->numOfPoints < 1 || pInfo->numOfPoints > TAIL_MAX_POINTS_NUM) ||
5651
      (pInfo->numOfPoints < 0 || pInfo->numOfPoints > TAIL_MAX_OFFSET)) {
5652
    return false;
5653
  }
5654

5655
  pInfo->pItems = (STailItem**)((char*)pInfo + sizeof(STailInfo));
5656
  char* pItem = (char*)pInfo->pItems + pInfo->numOfPoints * POINTER_BYTES;
5657

5658
  size_t unitSize = sizeof(STailItem) + pInfo->colBytes;
5659
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5660
    pInfo->pItems[i] = (STailItem*)(pItem + i * unitSize);
5661
    pInfo->pItems[i]->isNull = false;
5662
  }
5663
#endif
5664

5665
  return TSDB_CODE_SUCCESS;
×
5666
}
5667

5668
static void tailAssignResult(STailItem* pItem, char* data, int32_t colBytes, TSKEY ts, bool isNull) {
×
5669
#if 0
5670
  pItem->timestamp = ts;
5671
  if (isNull) {
5672
    pItem->isNull = true;
5673
  } else {
5674
    pItem->isNull = false;
5675
    memcpy(pItem->data, data, colBytes);
5676
  }
5677
#endif
5678
}
×
5679

5680
#if 0
5681
static int32_t tailCompFn(const void* p1, const void* p2, const void* param) {
5682
  STailItem* d1 = *(STailItem**)p1;
5683
  STailItem* d2 = *(STailItem**)p2;
5684
  return compareInt64Val(&d1->timestamp, &d2->timestamp);
5685
}
5686

5687
static void doTailAdd(STailInfo* pInfo, char* data, TSKEY ts, bool isNull) {
5688
  STailItem** pList = pInfo->pItems;
5689
  if (pInfo->numAdded < pInfo->numOfPoints) {
5690
    tailAssignResult(pList[pInfo->numAdded], data, pInfo->colBytes, ts, isNull);
5691
    taosheapsort((void*)pList, sizeof(STailItem**), pInfo->numAdded + 1, NULL, tailCompFn, 0);
5692
    pInfo->numAdded++;
5693
  } else if (pList[0]->timestamp < ts) {
5694
    tailAssignResult(pList[0], data, pInfo->colBytes, ts, isNull);
5695
    taosheapadjust((void*)pList, sizeof(STailItem**), 0, pInfo->numOfPoints - 1, NULL, tailCompFn, NULL, 0);
5696
  }
5697
}
5698
#endif
5699

5700
int32_t tailFunction(SqlFunctionCtx* pCtx) {
×
5701
#if 0
5702
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5703
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5704

5705
  SInputColumnInfoData* pInput = &pCtx->input;
5706
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5707

5708
  SColumnInfoData* pInputCol = pInput->pData[0];
5709
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5710

5711
  int32_t startOffset = pCtx->offset;
5712
  if (pInfo->offset >= pInput->numOfRows) {
5713
    return 0;
5714
  } else {
5715
    pInfo->numOfPoints = TMIN(pInfo->numOfPoints, pInput->numOfRows - pInfo->offset);
5716
  }
5717
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex - pInfo->offset; i += 1) {
5718
    char* data = colDataGetData(pInputCol, i);
5719
    doTailAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5720
  }
5721

5722
  taosqsort(pInfo->pItems, pInfo->numOfPoints, POINTER_BYTES, NULL, tailCompFn);
5723

5724
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5725
    int32_t    pos = startOffset + i;
5726
    STailItem* pItem = pInfo->pItems[i];
5727
    if (pItem->isNull) {
5728
      colDataSetNULL(pOutput, pos);
5729
    } else {
5730
      colDataSetVal(pOutput, pos, pItem->data, false);
5731
    }
5732
  }
5733

5734
  return pInfo->numOfPoints;
5735
#endif
5736
  return 0;
×
5737
}
5738

5739
int32_t tailFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
5740
#if 0
5741
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
5742
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pEntryInfo);
5743
  pEntryInfo->complete = true;
5744

5745
  int32_t type = pCtx->input.pData[0]->info.type;
5746
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
5747

5748
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5749

5750
  // todo assign the tag value and the corresponding row data
5751
  int32_t currentRow = pBlock->info.rows;
5752
  for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) {
5753
    STailItem* pItem = pInfo->pItems[i];
5754
    colDataSetVal(pCol, currentRow, pItem->data, false);
5755
    currentRow += 1;
5756
  }
5757

5758
  return pEntryInfo->numOfRes;
5759
#endif
5760
  return 0;
×
5761
}
5762

5763
bool getUniqueFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
5764
#if 0
5765
  pEnv->calcMemSize = sizeof(SUniqueInfo) + UNIQUE_MAX_RESULT_SIZE;
5766
#endif
5767
  return true;
×
5768
}
5769

5770
int32_t uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
5771
#if 0
5772
  if (!functionSetup(pCtx, pResInfo)) {
5773
    return false;
5774
  }
5775

5776
  SUniqueInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5777
  pInfo->numOfPoints = 0;
5778
  pInfo->colType = pCtx->resDataInfo.type;
5779
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5780
  if (pInfo->pHash != NULL) {
5781
    taosHashClear(pInfo->pHash);
5782
  } else {
5783
    pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5784
  }
5785
#endif
5786
  return TSDB_CODE_SUCCESS;
×
5787
}
5788

5789
#if 0
5790
static void doUniqueAdd(SUniqueInfo* pInfo, char* data, TSKEY ts, bool isNull) {
5791
  // handle null elements
5792
  if (isNull == true) {
5793
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5794
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5795
    if (pInfo->hasNull == false && pItem->isNull == false) {
5796
      pItem->timestamp = ts;
5797
      pItem->isNull = true;
5798
      pInfo->numOfPoints++;
5799
      pInfo->hasNull = true;
5800
    } else if (pItem->timestamp > ts && pItem->isNull == true) {
5801
      pItem->timestamp = ts;
5802
    }
5803
    return;
5804
  }
5805

5806
  int32_t      hashKeyBytes = IS_VAR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
5807
  SUniqueItem* pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes);
5808
  if (pHashItem == NULL) {
5809
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5810
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5811
    pItem->timestamp = ts;
5812
    memcpy(pItem->data, data, pInfo->colBytes);
5813

5814
    taosHashPut(pInfo->pHash, data, hashKeyBytes, (char*)pItem, sizeof(SUniqueItem*));
5815
    pInfo->numOfPoints++;
5816
  } else if (pHashItem->timestamp > ts) {
5817
    pHashItem->timestamp = ts;
5818
  }
5819
}
5820
#endif
5821

5822
int32_t uniqueFunction(SqlFunctionCtx* pCtx) {
×
5823
#if 0
5824
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5825
  SUniqueInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5826

5827
  SInputColumnInfoData* pInput = &pCtx->input;
5828
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5829

5830
  SColumnInfoData* pInputCol = pInput->pData[0];
5831
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
5832
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5833

5834
  int32_t startOffset = pCtx->offset;
5835
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
5836
    char* data = colDataGetData(pInputCol, i);
5837
    doUniqueAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5838

5839
    if (sizeof(SUniqueInfo) + pInfo->numOfPoints * (sizeof(SUniqueItem) + pInfo->colBytes) >= UNIQUE_MAX_RESULT_SIZE) {
5840
      taosHashCleanup(pInfo->pHash);
5841
      return 0;
5842
    }
5843
  }
5844

5845
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5846
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + i * (sizeof(SUniqueItem) + pInfo->colBytes));
5847
    if (pItem->isNull == true) {
5848
      colDataSetNULL(pOutput, i);
5849
    } else {
5850
      colDataSetVal(pOutput, i, pItem->data, false);
5851
    }
5852
    if (pTsOutput != NULL) {
5853
      colDataSetInt64(pTsOutput, i, &pItem->timestamp);
5854
    }
5855
  }
5856

5857
  return pInfo->numOfPoints;
5858
#endif
5859
  return 0;
×
5860
}
5861

5862
bool getModeFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
24,831✔
5863
  pEnv->calcMemSize = sizeof(SModeInfo);
24,831✔
5864
  return true;
24,831✔
5865
}
5866

5867
int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
25,670✔
5868
  if (pResInfo->initialized) {
25,670!
5869
    return TSDB_CODE_SUCCESS;
×
5870
  }
5871
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
25,670!
5872
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5873
  }
5874

5875
  SModeInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
25,670✔
5876
  pInfo->colType = pCtx->resDataInfo.type;
25,670✔
5877
  pInfo->colBytes = pCtx->resDataInfo.bytes;
25,670✔
5878
  if (pInfo->pHash != NULL) {
25,670!
5879
    taosHashClear(pInfo->pHash);
×
5880
  } else {
5881
    pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
25,670✔
5882
    if (NULL == pInfo->pHash) {
25,670!
5883
      return terrno;
×
5884
    }
5885
  }
5886
  pInfo->nullTupleSaved = false;
25,670✔
5887
  pInfo->nullTuplePos.pageId = -1;
25,670✔
5888

5889
  pInfo->buf = taosMemoryMalloc(pInfo->colBytes);
25,670!
5890
  if (NULL == pInfo->buf) {
25,670!
5891
    taosHashCleanup(pInfo->pHash);
×
5892
    pInfo->pHash = NULL;
×
5893
    return terrno;
×
5894
  }
5895
  pCtx->needCleanup = true;
25,670✔
5896
  return TSDB_CODE_SUCCESS;
25,670✔
5897
}
5898

5899
static void modeFunctionCleanup(SModeInfo* pInfo) {
25,670✔
5900
  taosHashCleanup(pInfo->pHash);
25,670✔
5901
  pInfo->pHash = NULL;
25,670✔
5902
  taosMemoryFreeClear(pInfo->buf);
25,670!
5903
}
25,670✔
5904

5905
void modeFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
5906
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
5907
    return;
×
5908
  }
5909
  modeFunctionCleanup(GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)));
×
5910
}
5911

5912
static int32_t saveModeTupleData(SqlFunctionCtx* pCtx, char* data, SModeInfo* pInfo, STuplePos* pPos) {
108,115,724✔
5913
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
108,115,724!
5914
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
32,425,217!
5915
      (void)memcpy(pInfo->buf, data, getJsonValueLen(data));
×
5916
    } else {
5917
      (void)memcpy(pInfo->buf, data, varDataTLen(data));
32,425,217✔
5918
    }
5919
  } else {
5920
    (void)memcpy(pInfo->buf, data, pInfo->colBytes);
75,690,507✔
5921
  }
5922

5923
  return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore);
108,115,724✔
5924
}
5925

5926
static int32_t doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCtx, char* data) {
123,374,267✔
5927
  int32_t code = TSDB_CODE_SUCCESS;
123,374,267✔
5928
  int32_t hashKeyBytes;
5929
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
123,374,267!
5930
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
32,425,644!
5931
      hashKeyBytes = getJsonValueLen(data);
×
5932
    } else {
5933
      hashKeyBytes = varDataTLen(data);
32,425,644✔
5934
    }
5935
  } else {
5936
    hashKeyBytes = pInfo->colBytes;
90,948,623✔
5937
  }
5938

5939
  SModeItem* pHashItem = (SModeItem*)taosHashGet(pInfo->pHash, data, hashKeyBytes);
123,374,621✔
5940
  if (pHashItem == NULL) {
123,373,509✔
5941
    int32_t   size = sizeof(SModeItem);
108,115,659✔
5942
    SModeItem item = {0};
108,115,659✔
5943

5944
    item.count += 1;
108,115,659✔
5945
    code = saveModeTupleData(pCtx, data, pInfo, &item.dataPos);
108,115,659✔
5946
    if (code != TSDB_CODE_SUCCESS) {
108,115,093!
5947
      return code;
×
5948
    }
5949

5950
    if (pCtx->subsidiaries.num > 0) {
108,115,093✔
5951
      code = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &item.tuplePos);
70,793,117✔
5952
      if (code != TSDB_CODE_SUCCESS) {
70,793,117!
5953
        return code;
×
5954
      }
5955
    }
5956

5957
    code = taosHashPut(pInfo->pHash, data, hashKeyBytes, &item, sizeof(SModeItem));
108,115,093✔
5958
    if (code != TSDB_CODE_SUCCESS) {
108,116,861!
5959
      return code;
×
5960
    }
5961
  } else {
5962
    pHashItem->count += 1;
15,257,850✔
5963
    if (pCtx->subsidiaries.num > 0) {
15,257,850✔
5964
      code = updateTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &pHashItem->tuplePos);
1,927,069✔
5965
      if (code != TSDB_CODE_SUCCESS) {
1,927,069!
5966
        return code;
×
5967
      }
5968
    }
5969
  }
5970

5971
  return code;
123,374,711✔
5972
}
5973

5974
int32_t modeFunction(SqlFunctionCtx* pCtx) {
2,218,577✔
5975
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,218,577✔
5976
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
2,218,577✔
5977

5978
  SInputColumnInfoData* pInput = &pCtx->input;
2,218,577✔
5979

5980
  SColumnInfoData* pInputCol = pInput->pData[0];
2,218,577✔
5981
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
2,218,577✔
5982

5983
  int32_t numOfElems = 0;
2,218,577✔
5984
  int32_t startOffset = pCtx->offset;
2,218,577✔
5985
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
126,027,319✔
5986
    if (colDataIsNull_s(pInputCol, i)) {
247,617,454✔
5987
      continue;
434,670✔
5988
    }
5989
    numOfElems++;
123,374,057✔
5990

5991
    char*   data = colDataGetData(pInputCol, i);
123,374,057✔
5992
    int32_t code = doModeAdd(pInfo, i, pCtx, data);
123,374,057✔
5993
    if (code != TSDB_CODE_SUCCESS) {
123,374,674✔
5994
      modeFunctionCleanup(pInfo);
602✔
5995
      return code;
×
5996
    }
5997
  }
5998

5999
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
2,218,592!
6000
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
30✔
6001
    if (code != TSDB_CODE_SUCCESS) {
30!
6002
      modeFunctionCleanup(pInfo);
×
6003
      return code;
×
6004
    }
6005
    pInfo->nullTupleSaved = true;
30✔
6006
  }
6007

6008
  SET_VAL(pResInfo, numOfElems, 1);
2,218,592✔
6009

6010
  return TSDB_CODE_SUCCESS;
2,218,592✔
6011
}
6012

6013
int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
25,670✔
6014
  int32_t              code = TSDB_CODE_SUCCESS;
25,670✔
6015
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
25,670✔
6016
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
25,670✔
6017
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
25,670✔
6018
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
25,670✔
6019
  int32_t              currentRow = pBlock->info.rows;
25,670✔
6020
  if (NULL == pCol) {
25,670!
6021
    modeFunctionCleanup(pInfo);
×
6022
    return TSDB_CODE_OUT_OF_RANGE;
×
6023
  }
6024

6025
  STuplePos resDataPos, resTuplePos;
6026
  int32_t   maxCount = 0;
25,670✔
6027

6028
  void* pIter = taosHashIterate(pInfo->pHash, NULL);
25,670✔
6029
  while (pIter != NULL) {
108,142,712✔
6030
    SModeItem* pItem = (SModeItem*)pIter;
108,117,042✔
6031
    if (pItem->count >= maxCount) {
108,117,042✔
6032
      maxCount = pItem->count;
63,521,111✔
6033
      resDataPos = pItem->dataPos;
63,521,111✔
6034
      resTuplePos = pItem->tuplePos;
63,521,111✔
6035
    }
6036

6037
    pIter = taosHashIterate(pInfo->pHash, pIter);
108,117,042✔
6038
  }
6039

6040
  if (maxCount != 0) {
25,670✔
6041
    char* pData = NULL;
23,213✔
6042
    code = loadTupleData(pCtx, &resDataPos, &pData);
23,213✔
6043
    if (pData == NULL || TSDB_CODE_SUCCESS != code) {
23,213!
6044
      code = terrno = TSDB_CODE_NOT_FOUND;
×
6045
      qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(),
×
6046
             resDataPos.streamTupleKey.groupId, resDataPos.streamTupleKey.ts);
6047
      modeFunctionCleanup(pInfo);
×
6048
      return code;
×
6049
    }
6050

6051
    code = colDataSetVal(pCol, currentRow, pData, false);
23,213✔
6052
    if (TSDB_CODE_SUCCESS != code) {
23,213!
6053
      modeFunctionCleanup(pInfo);
×
6054
      return code;
×
6055
    }
6056
    code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow);
23,213✔
6057
  } else {
6058
    colDataSetNULL(pCol, currentRow);
2,457✔
6059
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
2,457✔
6060
  }
6061

6062
  modeFunctionCleanup(pInfo);
25,670✔
6063

6064
  return code;
25,670✔
6065
}
6066

6067
bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
78,506✔
6068
  pEnv->calcMemSize = sizeof(STwaInfo);
78,506✔
6069
  return true;
78,506✔
6070
}
6071

6072
int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
10,845,275✔
6073
  if (pResultInfo->initialized) {
10,845,275!
6074
    return TSDB_CODE_SUCCESS;
×
6075
  }
6076
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
10,845,275!
6077
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6078
  }
6079

6080
  STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
10,845,298✔
6081
  pInfo->numOfElems = 0;
10,845,298✔
6082
  pInfo->p.key = INT64_MIN;
10,845,298✔
6083
  pInfo->win = TSWINDOW_INITIALIZER;
10,845,298✔
6084
  return TSDB_CODE_SUCCESS;
10,845,298✔
6085
}
6086

6087
static double twa_get_area(SPoint1 s, SPoint1 e) {
29,494,076✔
6088
  if (e.key == INT64_MAX || s.key == INT64_MIN) {
29,494,076!
6089
    return 0;
×
6090
  }
6091

6092
  if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) {
29,494,324✔
6093
    return (s.val + e.val) * (e.key - s.key) / 2;
17,017,110✔
6094
  }
6095

6096
  double x = (s.key * e.val - e.key * s.val) / (e.val - s.val);
12,477,214✔
6097
  double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2;
12,477,214✔
6098
  return val;
12,477,214✔
6099
}
6100

6101
int32_t twaFunction(SqlFunctionCtx* pCtx) {
10,860,167✔
6102
  int32_t               code = TSDB_CODE_SUCCESS;
10,860,167✔
6103
  SInputColumnInfoData* pInput = &pCtx->input;
10,860,167✔
6104
  SColumnInfoData*      pInputCol = pInput->pData[0];
10,860,167✔
6105

6106
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
10,860,167✔
6107
  STwaInfo*            pInfo = GET_ROWCELL_INTERBUF(pResInfo);
10,860,167✔
6108
  SPoint1*             last = &pInfo->p;
10,860,167✔
6109

6110
  if (IS_NULL_TYPE(pInputCol->info.type)) {
10,860,167!
6111
    pInfo->numOfElems = 0;
×
6112
    goto _twa_over;
×
6113
  }
6114

6115
  funcInputUpdate(pCtx);
10,860,167✔
6116
  SFuncInputRow row = {0};
10,860,238✔
6117
  bool          result = false;
10,860,238✔
6118
  if (pCtx->start.key != INT64_MIN && last->key == INT64_MIN) {
10,860,238!
6119
    while (1) {
6120
      code = funcInputGetNextRow(pCtx, &row, &result);
3,449,463✔
6121
      if (TSDB_CODE_SUCCESS != code) {
3,449,488!
6122
        return code;
×
6123
      }
6124
      if (!result) {
3,449,488✔
6125
        break;
2✔
6126
      }
6127
      if (row.isDataNull) {
3,449,486✔
6128
        continue;
2✔
6129
      }
6130

6131
      last->key = row.ts;
3,449,484✔
6132

6133
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
3,449,484!
6134

6135
      pInfo->dOutput += twa_get_area(pCtx->start, *last);
3,449,484✔
6136
      pInfo->win.skey = pCtx->start.key;
3,449,478✔
6137
      pInfo->numOfElems++;
3,449,478✔
6138
      break;
3,449,478✔
6139
    }
6140
  } else if (pInfo->p.key == INT64_MIN) {
7,410,777✔
6141
    while (1) {
6142
      code = funcInputGetNextRow(pCtx, &row, &result);
7,528,740✔
6143
      if (TSDB_CODE_SUCCESS != code) {
7,528,535!
6144
        return code;
×
6145
      }
6146
      if (!result) {
7,528,535✔
6147
        break;
13,901✔
6148
      }
6149
      if (row.isDataNull) {
7,514,634✔
6150
        continue;
132,476✔
6151
      }
6152

6153
      last->key = row.ts;
7,382,158✔
6154

6155
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
7,382,158!
6156

6157
      pInfo->win.skey = last->key;
7,382,158✔
6158
      pInfo->numOfElems++;
7,382,158✔
6159
      break;
7,382,158✔
6160
    }
6161
  }
6162

6163
  SPoint1 st = {0};
10,860,052✔
6164

6165
  // calculate the value of
6166
  while (1) {
6167
    code = funcInputGetNextRow(pCtx, &row, &result);
33,325,675✔
6168
    if (TSDB_CODE_SUCCESS != code) {
33,325,015!
6169
      return code;
×
6170
    }
6171
    if (!result) {
33,325,015✔
6172
      break;
10,860,304✔
6173
    }
6174
    if (row.isDataNull) {
22,464,711✔
6175
      continue;
629✔
6176
    }
6177
    pInfo->numOfElems++;
22,464,082✔
6178
    switch (pInputCol->info.type) {
22,464,082!
6179
      case TSDB_DATA_TYPE_TINYINT: {
66,811✔
6180
        INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData);
66,811✔
6181
        break;
66,811✔
6182
      }
6183
      case TSDB_DATA_TYPE_SMALLINT: {
62,064✔
6184
        INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData);
62,064✔
6185
        break;
62,064✔
6186
      }
6187
      case TSDB_DATA_TYPE_INT: {
98,289✔
6188
        INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData);
98,289✔
6189
        break;
98,289✔
6190
      }
6191
      case TSDB_DATA_TYPE_BIGINT: {
3,318,911✔
6192
        INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData);
3,318,911✔
6193
        break;
3,318,911✔
6194
      }
6195
      case TSDB_DATA_TYPE_FLOAT: {
51,943✔
6196
        INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData);
51,943✔
6197
        break;
51,943✔
6198
      }
6199
      case TSDB_DATA_TYPE_DOUBLE: {
18,680,852✔
6200
        INIT_INTP_POINT(st, row.ts, *(double*)row.pData);
18,680,852✔
6201
        break;
18,680,852✔
6202
      }
6203
      case TSDB_DATA_TYPE_UTINYINT: {
48,622✔
6204
        INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData);
48,622✔
6205
        break;
48,622✔
6206
      }
6207
      case TSDB_DATA_TYPE_USMALLINT: {
48,691✔
6208
        INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData);
48,691✔
6209
        break;
48,691✔
6210
      }
6211
      case TSDB_DATA_TYPE_UINT: {
50,299✔
6212
        INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData);
50,299✔
6213
        break;
50,299✔
6214
      }
6215
      case TSDB_DATA_TYPE_UBIGINT: {
38,970✔
6216
        INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData);
38,970✔
6217
        break;
38,970✔
6218
      }
6219
      default: {
×
6220
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
6221
      }
6222
    }
6223
    if (pInfo->p.key == st.key) {
22,465,452!
6224
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6225
    }
6226

6227
    pInfo->dOutput += twa_get_area(pInfo->p, st);
22,465,452✔
6228
    pInfo->p = st;
22,464,994✔
6229
  }
6230

6231
  // the last interpolated time window value
6232
  if (pCtx->end.key != INT64_MIN) {
10,860,304✔
6233
    pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end);
3,581,172✔
6234
    pInfo->p = pCtx->end;
3,581,164✔
6235
    pInfo->numOfElems += 1;
3,581,164✔
6236
  }
6237

6238
  pInfo->win.ekey = pInfo->p.key;
10,860,296✔
6239

6240
_twa_over:
10,860,296✔
6241
  SET_VAL(pResInfo, 1, 1);
10,860,296✔
6242
  return TSDB_CODE_SUCCESS;
10,860,296✔
6243
}
6244

6245
/*
6246
 * To copy the input to interResBuf to avoid the input buffer space be over writen
6247
 * by next input data. The TWA function only applies to each table, so no merge procedure
6248
 * is required, we simply copy to the resut ot interResBuffer.
6249
 */
6250
// void twa_function_copy(SQLFunctionCtx *pCtx) {
6251
//   SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx);
6252
//
6253
//   memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes);
6254
//   pResInfo->hasResult = ((STwaInfo *)pCtx->pInput)->hasResult;
6255
// }
6256

6257
int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
10,839,178✔
6258
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
10,839,178✔
6259

6260
  STwaInfo* pInfo = (STwaInfo*)GET_ROWCELL_INTERBUF(pResInfo);
10,839,178✔
6261
  if (pInfo->numOfElems == 0) {
10,839,178✔
6262
    pResInfo->numOfRes = 0;
13,803✔
6263
  } else {
6264
    if (pInfo->win.ekey == pInfo->win.skey) {
10,825,375✔
6265
      pInfo->dTwaRes = pInfo->p.val;
5,912,122✔
6266
    } else if (pInfo->win.ekey == INT64_MAX || pInfo->win.skey == INT64_MIN) {  // no data in timewindow
4,913,253!
6267
      pInfo->dTwaRes = 0;
×
6268
    } else {
6269
      pInfo->dTwaRes = pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey);
4,913,540✔
6270
    }
6271

6272
    pResInfo->numOfRes = 1;
10,825,375✔
6273
  }
6274

6275
  return functionFinalize(pCtx, pBlock);
10,839,178✔
6276
}
6277

6278
int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
1,628✔
6279
  if (pResultInfo->initialized) {
1,628!
6280
    return TSDB_CODE_SUCCESS;
×
6281
  }
6282
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
1,628!
6283
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6284
  }
6285

6286
  STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1,628✔
6287
  pInfo->minRows = INT32_MAX;
1,628✔
6288
  return TSDB_CODE_SUCCESS;
1,628✔
6289
}
6290

6291
int32_t blockDistFunction(SqlFunctionCtx* pCtx) {
3,253✔
6292
  const int32_t BLOCK_DIST_RESULT_ROWS = 25;
3,253✔
6293

6294
  SInputColumnInfoData* pInput = &pCtx->input;
3,253✔
6295
  SColumnInfoData*      pInputCol = pInput->pData[0];
3,253✔
6296
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
3,253✔
6297
  STableBlockDistInfo*  pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
3,253✔
6298

6299
  STableBlockDistInfo p1 = {0};
3,253✔
6300
  if (tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
3,253!
6301
    qError("failed to deserialize block dist info");
×
6302
    return TSDB_CODE_FAILED;
×
6303
  }
6304

6305
  pDistInfo->numOfBlocks += p1.numOfBlocks;
3,253✔
6306
  pDistInfo->numOfTables += p1.numOfTables;
3,253✔
6307
  pDistInfo->numOfInmemRows += p1.numOfInmemRows;
3,253✔
6308
  pDistInfo->numOfSttRows += p1.numOfSttRows;
3,253✔
6309
  pDistInfo->totalSize += p1.totalSize;
3,253✔
6310
  pDistInfo->totalRows += p1.totalRows;
3,253✔
6311
  pDistInfo->numOfFiles += p1.numOfFiles;
3,253✔
6312

6313
  pDistInfo->defMinRows = p1.defMinRows;
3,253✔
6314
  pDistInfo->defMaxRows = p1.defMaxRows;
3,253✔
6315
  pDistInfo->rowSize = p1.rowSize;
3,253✔
6316

6317
  if (pDistInfo->minRows > p1.minRows) {
3,253✔
6318
    pDistInfo->minRows = p1.minRows;
2✔
6319
  }
6320
  if (pDistInfo->maxRows < p1.maxRows) {
3,253✔
6321
    pDistInfo->maxRows = p1.maxRows;
2✔
6322
  }
6323
  pDistInfo->numOfVgroups += (p1.numOfTables != 0 ? 1 : 0);
3,253✔
6324
  for (int32_t i = 0; i < tListLen(pDistInfo->blockRowsHisto); ++i) {
68,313✔
6325
    pDistInfo->blockRowsHisto[i] += p1.blockRowsHisto[i];
65,060✔
6326
  }
6327

6328
  pResInfo->numOfRes = BLOCK_DIST_RESULT_ROWS;  // default output rows
3,253✔
6329
  return TSDB_CODE_SUCCESS;
3,253✔
6330
}
6331

6332
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo) {
6,491✔
6333
  SEncoder encoder = {0};
6,491✔
6334
  int32_t  code = 0;
6,491✔
6335
  int32_t  lino;
6336
  int32_t  tlen;
6337
  tEncoderInit(&encoder, buf, bufLen);
6,491✔
6338

6339
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
6,492!
6340
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->rowSize));
12,990!
6341

6342
  TAOS_CHECK_EXIT(tEncodeU16(&encoder, pInfo->numOfFiles));
12,990!
6343
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfBlocks));
12,990!
6344
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfTables));
12,990!
6345

6346
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalSize));
12,990!
6347
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalRows));
12,990!
6348
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->maxRows));
12,990!
6349
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->minRows));
12,990!
6350
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMaxRows));
12,990!
6351
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMinRows));
12,990!
6352
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfInmemRows));
12,990!
6353
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfSttRows));
12,990!
6354
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfVgroups));
12,990!
6355

6356
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
136,337✔
6357
    TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->blockRowsHisto[i]));
259,684!
6358
  }
6359

6360
  tEndEncode(&encoder);
6,495✔
6361

6362
_exit:
6,500✔
6363
  if (code) {
6,500!
6364
    tlen = code;
×
6365
  } else {
6366
    tlen = encoder.pos;
6,500✔
6367
  }
6368
  tEncoderClear(&encoder);
6,500✔
6369
  return tlen;
6,498✔
6370
}
6371

6372
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo) {
3,253✔
6373
  SDecoder decoder = {0};
3,253✔
6374
  int32_t  code = 0;
3,253✔
6375
  int32_t  lino;
6376
  tDecoderInit(&decoder, buf, bufLen);
3,253✔
6377

6378
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
3,253!
6379
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->rowSize));
6,506!
6380

6381
  TAOS_CHECK_EXIT(tDecodeU16(&decoder, &pInfo->numOfFiles));
6,506!
6382
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfBlocks));
6,506!
6383
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfTables));
6,506!
6384

6385
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalSize));
6,506!
6386
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalRows));
6,506!
6387
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->maxRows));
6,506!
6388
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->minRows));
6,506!
6389
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMaxRows));
6,506!
6390
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMinRows));
6,506!
6391
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfInmemRows));
6,506!
6392
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfSttRows));
6,506!
6393
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfVgroups));
6,506!
6394

6395
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
68,313✔
6396
    TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->blockRowsHisto[i]));
130,120!
6397
  }
6398

6399
_exit:
3,253✔
6400
  tDecoderClear(&decoder);
3,253✔
6401
  return code;
3,253✔
6402
}
6403

6404
int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1,628✔
6405
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1,628✔
6406
  STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo);
1,628✔
6407

6408
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
1,628✔
6409
  if (NULL == pColInfo) {
1,628!
6410
    return TSDB_CODE_OUT_OF_RANGE;
×
6411
  }
6412

6413
  if (pData->totalRows == 0) {
1,628✔
6414
    pData->minRows = 0;
1,626✔
6415
  }
6416

6417
  int32_t row = 0;
1,628✔
6418
  char    st[256] = {0};
1,628✔
6419
  double  averageSize = 0;
1,628✔
6420
  if (pData->numOfBlocks != 0) {
1,628✔
6421
    averageSize = ((double)pData->totalSize) / pData->numOfBlocks;
2✔
6422
  }
6423
  uint64_t totalRawSize = pData->totalRows * pData->rowSize;
1,628✔
6424
  double   compRatio = 0;
1,628✔
6425
  if (totalRawSize != 0) {
1,628✔
6426
    compRatio = pData->totalSize * 100 / (double)totalRawSize;
2✔
6427
  }
6428

6429
  int32_t len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
3,256✔
6430
                          "Total_Blocks=[%d] Total_Size=[%.2f KiB] Average_size=[%.2f KiB] Compression_Ratio=[%.2f %c]",
6431
                          pData->numOfBlocks, pData->totalSize / 1024.0, averageSize / 1024.0, compRatio, '%');
1,628✔
6432

6433
  varDataSetLen(st, len);
1,628✔
6434
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
1,628✔
6435
  if (TSDB_CODE_SUCCESS != code) {
1,628!
6436
    return code;
×
6437
  }
6438

6439
  int64_t avgRows = 0;
1,628✔
6440
  if (pData->numOfBlocks > 0) {
1,628✔
6441
    avgRows = pData->totalRows / pData->numOfBlocks;
2✔
6442
  }
6443

6444
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
1,628✔
6445
                  "Block_Rows=[%" PRId64 "] MinRows=[%d] MaxRows=[%d] AvgRows=[%" PRId64 "]", pData->totalRows,
6446
                  pData->minRows, pData->maxRows, avgRows);
6447
  varDataSetLen(st, len);
1,628✔
6448
  code = colDataSetVal(pColInfo, row++, st, false);
1,628✔
6449
  if (TSDB_CODE_SUCCESS != code) {
1,628!
6450
    return code;
×
6451
  }
6452

6453
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Inmem_Rows=[%u] Stt_Rows=[%u] ",
1,628✔
6454
                  pData->numOfInmemRows, pData->numOfSttRows);
6455
  varDataSetLen(st, len);
1,628✔
6456
  code = colDataSetVal(pColInfo, row++, st, false);
1,628✔
6457
  if (TSDB_CODE_SUCCESS != code) {
1,628!
6458
    return code;
×
6459
  }
6460

6461
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
3,256✔
6462
                  "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables, pData->numOfFiles,
1,628✔
6463
                  pData->numOfVgroups);
6464

6465
  varDataSetLen(st, len);
1,628✔
6466
  code = colDataSetVal(pColInfo, row++, st, false);
1,628✔
6467
  if (TSDB_CODE_SUCCESS != code) {
1,628!
6468
    return code;
×
6469
  }
6470

6471
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
1,628✔
6472
                  "--------------------------------------------------------------------------------");
6473
  varDataSetLen(st, len);
1,628✔
6474
  code = colDataSetVal(pColInfo, row++, st, false);
1,628✔
6475
  if (TSDB_CODE_SUCCESS != code) {
1,628!
6476
    return code;
×
6477
  }
6478

6479
  int32_t maxVal = 0;
1,628✔
6480
  int32_t minVal = INT32_MAX;
1,628✔
6481
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
34,188✔
6482
    if (maxVal < pData->blockRowsHisto[i]) {
32,560✔
6483
      maxVal = pData->blockRowsHisto[i];
3✔
6484
    }
6485

6486
    if (minVal > pData->blockRowsHisto[i]) {
32,560✔
6487
      minVal = pData->blockRowsHisto[i];
1,629✔
6488
    }
6489
  }
6490

6491
  // maximum number of step is 80
6492
  double factor = pData->numOfBlocks / 80.0;
1,628✔
6493

6494
  int32_t numOfBuckets = sizeof(pData->blockRowsHisto) / sizeof(pData->blockRowsHisto[0]);
1,628✔
6495
  int32_t bucketRange = ceil(((double)(pData->defMaxRows - pData->defMinRows)) / numOfBuckets);
1,628✔
6496

6497
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
34,188✔
6498
    len =
32,560✔
6499
        tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "%04d |", pData->defMinRows + bucketRange * (i + 1));
32,560✔
6500

6501
    int32_t num = 0;
32,560✔
6502
    if (pData->blockRowsHisto[i] > 0) {
32,560✔
6503
      num = (pData->blockRowsHisto[i]) / factor;
3✔
6504
    }
6505

6506
    for (int32_t j = 0; j < num; ++j) {
32,719✔
6507
      int32_t x = tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "%c", '|');
159✔
6508
      len += x;
159✔
6509
    }
6510

6511
    if (pData->blockRowsHisto[i] > 0) {
32,560✔
6512
      double v = pData->blockRowsHisto[i] * 100.0 / pData->numOfBlocks;
3✔
6513
      len += tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "  %d (%.2f%c)",
3✔
6514
                       pData->blockRowsHisto[i], v, '%');
6515
    }
6516

6517
    varDataSetLen(st, len);
32,560✔
6518
    code = colDataSetVal(pColInfo, row++, st, false);
32,560✔
6519
    if (TSDB_CODE_SUCCESS != code) {
32,560!
6520
      return code;
×
6521
    }
6522
  }
6523

6524
  return TSDB_CODE_SUCCESS;
1,628✔
6525
}
6526
int32_t blockDBUsageSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
1✔
6527
  if (pResultInfo->initialized) {
1!
6528
    return TSDB_CODE_SUCCESS;
×
6529
  }
6530
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
1!
6531
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6532
  }
6533

6534
  SDBBlockUsageInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1✔
6535
  return TSDB_CODE_SUCCESS;
1✔
6536
}
6537
int32_t blockDBUsageFunction(SqlFunctionCtx* pCtx) {
2✔
6538
  const int32_t BLOCK_DISK_USAGE_RESULT_ROWS = 2;
2✔
6539

6540
  SInputColumnInfoData* pInput = &pCtx->input;
2✔
6541
  SColumnInfoData*      pInputCol = pInput->pData[0];
2✔
6542
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
2✔
6543
  SDBBlockUsageInfo*    pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
2✔
6544

6545
  SDBBlockUsageInfo p1 = {0};
2✔
6546
  if (tDeserializeBlockDbUsage(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
2!
6547
    qError("failed to deserialize block dist info");
×
6548
    return TSDB_CODE_FAILED;
×
6549
  }
6550

6551
  pDistInfo->dataInDiskSize += p1.dataInDiskSize;
2✔
6552
  pDistInfo->walInDiskSize += p1.walInDiskSize;
2✔
6553
  pDistInfo->rawDataSize += p1.rawDataSize;
2✔
6554
  pResInfo->numOfRes = BLOCK_DISK_USAGE_RESULT_ROWS;  // default output rows
2✔
6555
  return TSDB_CODE_SUCCESS;
2✔
6556
}
6557

6558
int32_t tSerializeBlockDbUsage(void* buf, int32_t bufLen, const SDBBlockUsageInfo* pInfo) {
4✔
6559
  SEncoder encoder = {0};
4✔
6560
  int32_t  code = 0;
4✔
6561
  int32_t  lino;
6562
  int32_t  tlen;
6563
  tEncoderInit(&encoder, buf, bufLen);
4✔
6564

6565
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
4!
6566

6567
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->dataInDiskSize));
8!
6568
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->walInDiskSize));
8!
6569
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->rawDataSize));
8!
6570

6571
  tEndEncode(&encoder);
4✔
6572

6573
_exit:
4✔
6574
  if (code) {
4!
6575
    tlen = code;
×
6576
  } else {
6577
    tlen = encoder.pos;
4✔
6578
  }
6579
  tEncoderClear(&encoder);
4✔
6580
  return tlen;
4✔
6581
}
6582
int32_t tDeserializeBlockDbUsage(void* buf, int32_t bufLen, SDBBlockUsageInfo* pInfo) {
2✔
6583
  SDecoder decoder = {0};
2✔
6584
  int32_t  code = 0;
2✔
6585
  int32_t  lino;
6586
  tDecoderInit(&decoder, buf, bufLen);
2✔
6587

6588
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
2!
6589
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->dataInDiskSize));
4!
6590
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->walInDiskSize));
4!
6591
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->rawDataSize));
4!
6592

6593
_exit:
2✔
6594
  tDecoderClear(&decoder);
2✔
6595
  return code;
2✔
6596
}
6597
int32_t blockDBUsageFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
1✔
6598
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
1✔
6599
  SDBBlockUsageInfo*   pData = GET_ROWCELL_INTERBUF(pResInfo);
1✔
6600

6601
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
1✔
6602
  if (NULL == pColInfo) {
1!
6603
    return TSDB_CODE_OUT_OF_RANGE;
×
6604
  }
6605
  int32_t len = 0;
1✔
6606
  int32_t row = 0;
1✔
6607
  char    st[256] = {0};
1✔
6608

6609
  uint64_t totalDiskSize = pData->dataInDiskSize;
1✔
6610
  uint64_t rawDataSize = pData->rawDataSize;
1✔
6611
  double   compressRadio = 0;
1✔
6612
  if (rawDataSize != 0) {
1!
6613
    compressRadio = totalDiskSize * 100 / (double)rawDataSize;
1✔
6614
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_radio=[%.2f]", compressRadio);
1✔
6615
  } else {
6616
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_radio=[NULL]");
×
6617
  }
6618

6619
  varDataSetLen(st, len);
1✔
6620
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
1✔
6621
  if (TSDB_CODE_SUCCESS != code) {
1!
6622
    return code;
×
6623
  }
6624

6625
  len =
1✔
6626
      tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Disk_occupied=[%" PRId64 "k]", pData->dataInDiskSize);
1✔
6627
  varDataSetLen(st, len);
1✔
6628
  code = colDataSetVal(pColInfo, row++, st, false);
1✔
6629
  if (TSDB_CODE_SUCCESS != code) {
1!
6630
    return code;
×
6631
  }
6632
  return code;
1✔
6633
}
6634

6635
bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
30,569✔
6636
  pEnv->calcMemSize = sizeof(SDerivInfo);
30,569✔
6637
  return true;
30,569✔
6638
}
6639

6640
int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
61,235✔
6641
  if (pResInfo->initialized) {
61,235✔
6642
    return TSDB_CODE_SUCCESS;
30,567✔
6643
  }
6644
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
30,668!
6645
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6646
  }
6647

6648
  SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,668✔
6649

6650
  pDerivInfo->ignoreNegative = pCtx->param[2].param.i;
30,668✔
6651
  pDerivInfo->prevTs = -1;
30,668✔
6652
  pDerivInfo->tsWindow = pCtx->param[1].param.i;
30,668✔
6653
  pDerivInfo->valueSet = false;
30,668✔
6654
  return TSDB_CODE_SUCCESS;
30,668✔
6655
}
6656

6657
int32_t derivativeFunction(SqlFunctionCtx* pCtx) {
30,666✔
6658
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
30,666✔
6659
  SDerivInfo*          pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,666✔
6660

6661
  SInputColumnInfoData* pInput = &pCtx->input;
30,666✔
6662
  SColumnInfoData*      pInputCol = pInput->pData[0];
30,666✔
6663

6664
  int32_t          numOfElems = 0;
30,666✔
6665
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
30,666✔
6666
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
30,666✔
6667
  int32_t          code = TSDB_CODE_SUCCESS;
30,666✔
6668

6669
  funcInputUpdate(pCtx);
30,666✔
6670

6671
  double v = 0;
30,666✔
6672
  if (pCtx->order == TSDB_ORDER_ASC) {
30,666✔
6673
    SFuncInputRow row = {0};
27,284✔
6674
    bool          result = false;
27,284✔
6675
    while (1) {
2,728,231✔
6676
      code = funcInputGetNextRow(pCtx, &row, &result);
2,755,515✔
6677
      if (TSDB_CODE_SUCCESS != code) {
2,755,515!
6678
        return code;
×
6679
      }
6680
      if (!result) {
2,755,515✔
6681
        break;
27,284✔
6682
      }
6683
      if (row.isDataNull) {
2,728,231✔
6684
        continue;
35,846✔
6685
      }
6686

6687
      char* d = row.pData;
2,692,385✔
6688
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
2,692,385!
6689

6690
      int32_t pos = pCtx->offset + numOfElems;
2,692,385✔
6691
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
2,692,385✔
6692
        pDerivInfo->valueSet = true;
26,909✔
6693
      } else {
6694
        if (row.ts == pDerivInfo->prevTs) {
2,665,476!
6695
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6696
        }
6697
        double r = ((v - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (row.ts - pDerivInfo->prevTs);
2,665,476✔
6698
        if (pDerivInfo->ignoreNegative && r < 0) {
2,665,476✔
6699
        } else {
6700
          if (isinf(r) || isnan(r)) {
1,615,161!
6701
            colDataSetNULL(pOutput, pos);
×
6702
          } else {
6703
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
1,615,161✔
6704
            if (code != TSDB_CODE_SUCCESS) {
1,615,161!
6705
              return code;
×
6706
            }
6707
          }
6708

6709
          if (pTsOutput != NULL) {
1,615,161!
6710
            colDataSetInt64(pTsOutput, pos, &row.ts);
×
6711
          }
6712

6713
          // handle selectivity
6714
          if (pCtx->subsidiaries.num > 0) {
1,615,161✔
6715
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
1,056,991✔
6716
            if (code != TSDB_CODE_SUCCESS) {
1,056,991!
6717
              return code;
×
6718
            }
6719
          }
6720

6721
          numOfElems++;
1,615,161✔
6722
        }
6723
      }
6724

6725
      pDerivInfo->prevValue = v;
2,692,385✔
6726
      pDerivInfo->prevTs = row.ts;
2,692,385✔
6727
    }
6728
  } else {
6729
    SFuncInputRow row = {0};
3,382✔
6730
    bool          result = false;
3,382✔
6731
    while (1) {
334,069✔
6732
      code = funcInputGetNextRow(pCtx, &row, &result);
337,451✔
6733
      if (TSDB_CODE_SUCCESS != code) {
337,451!
6734
        return code;
×
6735
      }
6736
      if (!result) {
337,451✔
6737
        break;
3,382✔
6738
      }
6739
      if (row.isDataNull) {
334,069✔
6740
        continue;
137✔
6741
      }
6742

6743
      char* d = row.pData;
333,932✔
6744
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
333,932!
6745

6746
      int32_t pos = pCtx->offset + numOfElems;
333,932✔
6747
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
333,932✔
6748
        pDerivInfo->valueSet = true;
3,348✔
6749
      } else {
6750
        if (row.ts == pDerivInfo->prevTs) {
330,584!
6751
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6752
        }
6753
        double r = ((pDerivInfo->prevValue - v) * pDerivInfo->tsWindow) / (pDerivInfo->prevTs - row.ts);
330,584✔
6754
        if (pDerivInfo->ignoreNegative && r < 0) {
330,584✔
6755
        } else {
6756
          if (isinf(r) || isnan(r)) {
163,881!
6757
            colDataSetNULL(pOutput, pos);
×
6758
          } else {
6759
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
163,881✔
6760
            if (code != TSDB_CODE_SUCCESS) {
163,881!
6761
              return code;
×
6762
            }
6763
          }
6764

6765
          if (pTsOutput != NULL) {
163,881!
6766
            colDataSetInt64(pTsOutput, pos, &pDerivInfo->prevTs);
×
6767
          }
6768

6769
          // handle selectivity
6770
          if (pCtx->subsidiaries.num > 0) {
163,881✔
6771
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
56,154✔
6772
            if (code != TSDB_CODE_SUCCESS) {
56,154!
6773
              return code;
×
6774
            }
6775
          }
6776
          numOfElems++;
163,881✔
6777
        }
6778
      }
6779

6780
      pDerivInfo->prevValue = v;
333,932✔
6781
      pDerivInfo->prevTs = row.ts;
333,932✔
6782
    }
6783
  }
6784

6785
  pResInfo->numOfRes = numOfElems;
30,666✔
6786

6787
  return TSDB_CODE_SUCCESS;
30,666✔
6788
}
6789

6790
int32_t getIrateInfoSize(int32_t pkBytes) { return (int32_t)sizeof(SRateInfo) + 2 * pkBytes; }
2,354,284✔
6791

6792
bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
123,918✔
6793
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
123,918✔
6794
  pEnv->calcMemSize = getIrateInfoSize(pkBytes);
123,918✔
6795
  return true;
124,200✔
6796
}
6797

6798
int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
13,520,926✔
6799
  if (pResInfo->initialized) {
13,520,926!
6800
    return TSDB_CODE_SUCCESS;
×
6801
  }
6802
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
13,520,926!
6803
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6804
  }
6805

6806
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
13,520,939✔
6807

6808
  pInfo->firstKey = INT64_MIN;
13,520,939✔
6809
  pInfo->lastKey = INT64_MIN;
13,520,939✔
6810
  pInfo->firstValue = (double)INT64_MIN;
13,520,939✔
6811
  pInfo->lastValue = (double)INT64_MIN;
13,520,939✔
6812

6813
  pInfo->hasResult = 0;
13,520,939✔
6814
  return TSDB_CODE_SUCCESS;
13,520,939✔
6815
}
6816

6817
static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) {
59,779,096✔
6818
  if (isFirst) {
59,779,096✔
6819
    pRateInfo->firstValue = v;
24,252,726✔
6820
    pRateInfo->firstKey = ts;
24,252,726✔
6821
    if (pRateInfo->firstPk) {
24,252,726✔
6822
      int32_t pkBytes;
6823
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
35!
6824
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
8!
6825
          pkBytes = getJsonValueLen(pk);
×
6826
        } else {
6827
          pkBytes = varDataTLen(pk);
8✔
6828
        }
6829
      } else {
6830
        pkBytes = pRateInfo->pkBytes;
27✔
6831
      }
6832
      (void)memcpy(pRateInfo->firstPk, pk, pkBytes);
35✔
6833
    }
6834
  } else {
6835
    pRateInfo->lastValue = v;
35,526,370✔
6836
    pRateInfo->lastKey = ts;
35,526,370✔
6837
    if (pRateInfo->lastPk) {
35,526,370✔
6838
      int32_t pkBytes;
6839
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
52!
6840
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
12!
6841
          pkBytes = getJsonValueLen(pk);
×
6842
        } else {
6843
          pkBytes = varDataTLen(pk);
12✔
6844
        }
6845
      } else {
6846
        pkBytes = pRateInfo->pkBytes;
40✔
6847
      }
6848
      (void)memcpy(pRateInfo->lastPk, pk, pkBytes);
52✔
6849
    }
6850
  }
6851
}
59,779,096✔
6852

6853
static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool isMerge) {
15,751,809✔
6854
  if (pCtx->hasPrimaryKey) {
15,751,809✔
6855
    if (!isMerge) {
19✔
6856
      pRateInfo->pkType = pCtx->input.pPrimaryKey->info.type;
17✔
6857
      pRateInfo->pkBytes = pCtx->input.pPrimaryKey->info.bytes;
17✔
6858
      pRateInfo->firstPk = pRateInfo->pkData;
17✔
6859
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
17✔
6860
    } else {
6861
      pRateInfo->firstPk = pRateInfo->pkData;
2✔
6862
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
2✔
6863
    }
6864
  } else {
6865
    pRateInfo->firstPk = NULL;
15,751,790✔
6866
    pRateInfo->lastPk = NULL;
15,751,790✔
6867
  }
6868
}
15,751,809✔
6869

6870
int32_t irateFunction(SqlFunctionCtx* pCtx) {
11,291,887✔
6871
  int32_t              code = TSDB_CODE_SUCCESS;
11,291,887✔
6872
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
11,291,887✔
6873
  SRateInfo*           pRateInfo = GET_ROWCELL_INTERBUF(pResInfo);
11,291,887✔
6874

6875
  SInputColumnInfoData* pInput = &pCtx->input;
11,291,887✔
6876
  SColumnInfoData*      pInputCol = pInput->pData[0];
11,291,887✔
6877

6878
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
11,291,887✔
6879

6880
  funcInputUpdate(pCtx);
11,291,887✔
6881

6882
  initializeRateInfo(pCtx, pRateInfo, false);
11,291,902✔
6883

6884
  int32_t       numOfElems = 0;
11,291,899✔
6885
  int32_t       type = pInputCol->info.type;
11,291,899✔
6886
  SFuncInputRow row = {0};
11,291,899✔
6887
  bool          result = false;
11,291,899✔
6888
  while (1) {
33,556,762✔
6889
    code = funcInputGetNextRow(pCtx, &row, &result);
44,848,661✔
6890
    if (TSDB_CODE_SUCCESS != code) {
44,847,561!
6891
      return code;
×
6892
    }
6893
    if (!result) {
44,847,561✔
6894
      break;
11,291,908✔
6895
    }
6896
    if (row.isDataNull) {
33,555,653✔
6897
      continue;
117,382✔
6898
    }
6899

6900
    char*  data = row.pData;
33,438,271✔
6901
    double v = 0;
33,438,271✔
6902
    GET_TYPED_DATA(v, double, type, data);
33,438,271!
6903

6904
    if (INT64_MIN == pRateInfo->lastKey) {
33,438,271✔
6905
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
11,275,670✔
6906
      pRateInfo->hasResult = 1;
11,275,663✔
6907
      continue;
11,275,663✔
6908
    }
6909

6910
    if (row.ts > pRateInfo->lastKey) {
22,162,601✔
6911
      if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
22,022,241!
6912
        doSaveRateInfo(pRateInfo, true, pRateInfo->lastKey, pRateInfo->lastPk, pRateInfo->lastValue);
22,022,243✔
6913
      }
6914
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
22,022,217✔
6915
      continue;
22,022,135✔
6916
    } else if (row.ts == pRateInfo->lastKey) {
140,360!
6917
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6918
    }
6919

6920
    if ((INT64_MIN == pRateInfo->firstKey) || row.ts > pRateInfo->firstKey) {
140,360!
6921
      doSaveRateInfo(pRateInfo, true, row.ts, row.pPk, v);
×
6922
    } else if (row.ts == pRateInfo->firstKey) {
141,659!
6923
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6924
    }
6925
  }
6926

6927
  numOfElems++;
11,291,908✔
6928

6929
  SET_VAL(pResInfo, numOfElems, 1);
11,291,908!
6930
  return TSDB_CODE_SUCCESS;
11,291,908✔
6931
}
6932

6933
static double doCalcRate(const SRateInfo* pRateInfo, double tickPerSec) {
11,287,739✔
6934
  if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) ||
11,287,739✔
6935
      (pRateInfo->firstKey >= pRateInfo->lastKey)) {
4,155,948!
6936
    return 0.0;
7,131,791✔
6937
  }
6938

6939
  double diff = 0;
4,155,948✔
6940
  // If the previous value of the last is greater than the last value, only keep the last point instead of the delta
6941
  // value between two values.
6942
  diff = pRateInfo->lastValue;
4,155,948✔
6943
  if (diff >= pRateInfo->firstValue) {
4,155,948✔
6944
    diff -= pRateInfo->firstValue;
1,390,143✔
6945
  }
6946

6947
  int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey;
4,155,948✔
6948
  if (duration == 0) {
4,155,948!
6949
    return 0;
×
6950
  }
6951

6952
  return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0;
4,155,948!
6953
}
6954

6955
static void irateTransferInfoImpl(TSKEY inputKey, SRateInfo* pInput, SRateInfo* pOutput, bool isFirstKey) {
174✔
6956
  if (inputKey > pOutput->lastKey) {
174✔
6957
    doSaveRateInfo(pOutput, true, pOutput->lastKey, pOutput->lastPk, pOutput->lastValue);
97✔
6958
    if (isFirstKey) {
97✔
6959
      doSaveRateInfo(pOutput, false, pInput->firstKey, pInput->firstPk, pInput->firstValue);
45✔
6960
    } else {
6961
      doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
52✔
6962
    }
6963
  } else if ((inputKey < pOutput->lastKey) && (inputKey > pOutput->firstKey)) {
77!
6964
    if (isFirstKey) {
22✔
6965
      doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
11✔
6966
    } else {
6967
      doSaveRateInfo(pOutput, true, pInput->lastKey, pInput->lastPk, pInput->lastValue);
11✔
6968
    }
6969
  } else {
6970
    // inputKey < pOutput->firstKey
6971
  }
6972
}
174✔
6973

6974
static void irateCopyInfo(SRateInfo* pInput, SRateInfo* pOutput) {
2,229,894✔
6975
  doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
2,229,894✔
6976
  doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
2,229,894✔
6977
}
2,229,894✔
6978

6979
static int32_t irateTransferInfo(SRateInfo* pInput, SRateInfo* pOutput) {
2,229,981✔
6980
  if ((pInput->firstKey != INT64_MIN &&
2,229,981✔
6981
       (pInput->firstKey == pOutput->firstKey || pInput->firstKey == pOutput->lastKey)) ||
2,139,530!
6982
      (pInput->lastKey != INT64_MIN && (pInput->lastKey == pOutput->firstKey || pInput->lastKey == pOutput->lastKey))) {
2,229,981!
6983
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6984
  }
6985

6986
  if (pOutput->hasResult == 0) {
2,229,981✔
6987
    irateCopyInfo(pInput, pOutput);
2,229,894✔
6988
    pOutput->hasResult = pInput->hasResult;
2,229,894✔
6989
    return TSDB_CODE_SUCCESS;
2,229,894✔
6990
  }
6991

6992
  if (pInput->firstKey != INT64_MIN) {
87!
6993
    irateTransferInfoImpl(pInput->firstKey, pInput, pOutput, true);
87✔
6994
  }
6995

6996
  if (pInput->lastKey != INT64_MIN) {
87!
6997
    irateTransferInfoImpl(pInput->lastKey, pInput, pOutput, false);
87✔
6998
  }
6999

7000
  pOutput->hasResult = pInput->hasResult;
87✔
7001
  return TSDB_CODE_SUCCESS;
87✔
7002
}
7003

7004
int32_t irateFunctionMerge(SqlFunctionCtx* pCtx) {
2,229,989✔
7005
  SInputColumnInfoData* pInput = &pCtx->input;
2,229,989✔
7006
  SColumnInfoData*      pCol = pInput->pData[0];
2,229,989✔
7007
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
2,229,989!
7008
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
7009
  }
7010

7011
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
2,229,989✔
7012
  initializeRateInfo(pCtx, pInfo, true);
2,229,989✔
7013

7014
  int32_t start = pInput->startRowIndex;
2,229,989✔
7015
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
4,459,978✔
7016
    char*      data = colDataGetData(pCol, i);
2,229,989!
7017
    SRateInfo* pInputInfo = (SRateInfo*)varDataVal(data);
2,229,989✔
7018
    initializeRateInfo(pCtx, pInfo, true);
2,229,989✔
7019
    if (pInputInfo->hasResult) {
2,229,989✔
7020
      int32_t code = irateTransferInfo(pInputInfo, pInfo);
2,229,981✔
7021
      if (code != TSDB_CODE_SUCCESS) {
2,229,981!
7022
        return code;
×
7023
      }
7024
    }
7025
  }
7026

7027
  if (pInfo->hasResult) {
2,229,989✔
7028
    GET_RES_INFO(pCtx)->numOfRes = 1;
2,229,981✔
7029
  }
7030

7031
  return TSDB_CODE_SUCCESS;
2,229,989✔
7032
}
7033

7034
int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
2,229,988✔
7035
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,229,988✔
7036
  SRateInfo*           pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
2,229,988✔
7037
  int32_t              resultBytes = getIrateInfoSize(pInfo->pkBytes);
2,229,988✔
7038
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
2,229,988!
7039

7040
  if (NULL == res) {
2,229,989!
7041
    return terrno;
×
7042
  }
7043
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
2,229,989✔
7044
  varDataSetLen(res, resultBytes);
2,229,989✔
7045

7046
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
2,229,989✔
7047
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
2,229,989✔
7048
  if (NULL == pCol) {
2,229,989!
7049
    taosMemoryFree(res);
×
7050
    return TSDB_CODE_OUT_OF_RANGE;
×
7051
  }
7052

7053
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
2,229,989✔
7054

7055
  taosMemoryFree(res);
2,229,989!
7056
  return code;
2,229,989✔
7057
}
7058

7059
int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
11,287,920✔
7060
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
11,287,920✔
7061
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
11,287,920✔
7062
  if (NULL == pCol) {
11,287,741!
7063
    return TSDB_CODE_OUT_OF_RANGE;
×
7064
  }
7065

7066
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
11,287,741✔
7067
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
11,287,741✔
7068

7069
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
11,287,741✔
7070
  double     result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i));
11,287,741!
7071
  int32_t    code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes);
11,287,703✔
7072

7073
  return code;
11,287,606✔
7074
}
7075

7076
int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) {
107,527,001✔
7077
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
107,527,001✔
7078
  SGroupKeyInfo*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
107,527,001✔
7079

7080
  SInputColumnInfoData* pInput = &pCtx->input;
107,527,001✔
7081
  SColumnInfoData*      pInputCol = pInput->pData[0];
107,527,001✔
7082

7083
  int32_t startIndex = pInput->startRowIndex;
107,527,001✔
7084

7085
  // escape rest of data blocks to avoid first entry to be overwritten.
7086
  if (pInfo->hasResult) {
107,527,001✔
7087
    goto _group_value_over;
10,665,199✔
7088
  }
7089

7090
  if (pInputCol->pData == NULL || colDataIsNull_s(pInputCol, startIndex)) {
193,291,501✔
7091
    pInfo->isNull = true;
2,661,786✔
7092
    pInfo->hasResult = true;
2,661,786✔
7093
    goto _group_value_over;
2,661,786✔
7094
  }
7095

7096
  char* data = colDataGetData(pInputCol, startIndex);
94,200,016!
7097
  if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
94,200,016!
7098
    (void)memcpy(pInfo->data, data,
74,071,735✔
7099
                 (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data));
74,071,735✔
7100
  } else {
7101
    (void)memcpy(pInfo->data, data, pInputCol->info.bytes);
20,128,281✔
7102
  }
7103
  pInfo->hasResult = true;
94,200,016✔
7104

7105
_group_value_over:
107,527,001✔
7106

7107
  SET_VAL(pResInfo, 1, 1);
107,527,001✔
7108
  return TSDB_CODE_SUCCESS;
107,527,001✔
7109
}
7110

7111
int32_t groupKeyFunction(SqlFunctionCtx* pCtx) { return groupConstValueFunction(pCtx); }
107,539,811✔
7112

7113
int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
94,967,080✔
7114
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
94,967,080✔
7115
  int32_t          code = TSDB_CODE_SUCCESS;
94,967,080✔
7116
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
94,967,080✔
7117
  if (NULL == pCol) {
95,051,939!
7118
    return TSDB_CODE_OUT_OF_RANGE;
×
7119
  }
7120

7121
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
95,051,939✔
7122

7123
  SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
95,051,939✔
7124

7125
  if (pInfo->hasResult) {
95,051,939!
7126
    int32_t currentRow = pBlock->info.rows;
95,106,231✔
7127
    for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) {
190,748,041✔
7128
      code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false);
95,097,284✔
7129
      if (TSDB_CODE_SUCCESS != code) {
95,641,810!
7130
        return code;
×
7131
      }
7132
    }
7133
  } else {
7134
    pResInfo->numOfRes = 0;
×
7135
  }
7136

7137
  return code;
95,596,465✔
7138
}
7139

7140
int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return groupConstValueFinalize(pCtx, pBlock); }
94,960,138✔
7141

7142
int32_t groupKeyCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
7143
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
7144
  SGroupKeyInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
7145

7146
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
7147
  SGroupKeyInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
7148

7149
  // escape rest of data blocks to avoid first entry to be overwritten.
7150
  if (pDBuf->hasResult) {
×
7151
    goto _group_key_over;
×
7152
  }
7153

7154
  if (pSBuf->isNull) {
×
7155
    pDBuf->isNull = true;
×
7156
    pDBuf->hasResult = true;
×
7157
    goto _group_key_over;
×
7158
  }
7159

7160
  if (IS_VAR_DATA_TYPE(pSourceCtx->resDataInfo.type)) {
×
7161
    (void)memcpy(pDBuf->data, pSBuf->data,
×
7162
                 (pSourceCtx->resDataInfo.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(pSBuf->data)
×
7163
                                                                       : varDataTLen(pSBuf->data));
×
7164
  } else {
7165
    (void)memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes);
×
7166
  }
7167

7168
  pDBuf->hasResult = true;
×
7169

7170
_group_key_over:
×
7171

7172
  SET_VAL(pDResInfo, 1, 1);
×
7173
  return TSDB_CODE_SUCCESS;
×
7174
}
7175

7176
int32_t cachedLastRowFunction(SqlFunctionCtx* pCtx) {
3,112✔
7177
  int32_t numOfElems = 0;
3,112✔
7178

7179
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
3,112✔
7180
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
3,112✔
7181

7182
  SInputColumnInfoData* pInput = &pCtx->input;
3,112✔
7183
  SColumnInfoData*      pInputCol = pInput->pData[0];
3,112✔
7184

7185
  int32_t bytes = pInputCol->info.bytes;
3,112✔
7186
  pInfo->bytes = bytes;
3,112✔
7187

7188
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
3,112✔
7189
  pInfo->pkType = -1;
3,112✔
7190
  __compar_fn_t pkCompareFn = NULL;
3,112✔
7191
  if (pCtx->hasPrimaryKey) {
3,112✔
7192
    pInfo->pkType = pkCol->info.type;
20✔
7193
    pInfo->pkBytes = pkCol->info.bytes;
20✔
7194
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
20✔
7195
  }
7196

7197
  // TODO it traverse the different way.
7198
  // last_row function does not ignore the null value
7199
  for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
6,233✔
7200
    numOfElems++;
3,122✔
7201

7202
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
3,122✔
7203
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
3,122!
7204

7205
    TSKEY cts = getRowPTs(pInput->pPTS, i);
3,122!
7206
    if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
3,122✔
7207
      int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
2,790✔
7208
      if (code != TSDB_CODE_SUCCESS) {
2,789!
7209
        return code;
×
7210
      }
7211
      pResInfo->numOfRes = 1;
2,789✔
7212
    }
7213
  }
7214

7215
  SET_VAL(pResInfo, numOfElems, 1);
3,111!
7216
  return TSDB_CODE_SUCCESS;
3,111✔
7217
}
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