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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

71.81
/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; }
33,630,263✔
32
bool ignoreNull(int8_t ignoreOption) { return (ignoreOption & 0x2) == 0x2; }
371,549✔
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) {
450,670✔
194
  SFuncInputRowIter* pIter = &pCtx->rowIter;
450,670✔
195

196
  if (!pCtx->bInputFinished) {
450,670!
197
    pIter->pInput = &pCtx->input;
450,675✔
198
    pIter->tsList = (TSKEY*)pIter->pInput->pPTS->pData;
450,675✔
199
    pIter->pDataCol = pIter->pInput->pData[0];
450,675✔
200
    pIter->pPkCol = pIter->pInput->pPrimaryKey;
450,675✔
201
    pIter->rowIndex = pIter->pInput->startRowIndex;
450,675✔
202
    pIter->inputEndIndex = pIter->rowIndex + pIter->pInput->numOfRows - 1;
450,675✔
203
    pIter->pSrcBlock = pCtx->pSrcBlock;
450,675✔
204
    if (!pIter->hasGroupId || pIter->groupId != pIter->pSrcBlock->info.id.groupId) {
450,675✔
205
      pIter->hasGroupId = true;
66,292✔
206
      pIter->groupId = pIter->pSrcBlock->info.id.groupId;
66,292✔
207
      pIter->hasPrev = false;
66,292✔
208
    }
209
  } else {
210
    pIter->finalRow = true;
×
211
  }
212
}
450,670✔
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) {
28,712,961✔
337
  pRow->ts = pIter->tsList[rowIndex];
28,712,961✔
338
  pRow->ts = pIter->tsList[rowIndex];
28,712,961✔
339
  pRow->isDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, rowIndex);
28,712,961✔
340
  pRow->pData = colDataGetData(pIter->pDataCol, rowIndex);
28,712,961!
341
  pRow->pPk = setPk ? colDataGetData(pIter->pPkCol, rowIndex) : NULL;
28,712,961!
342
  pRow->block = pIter->pSrcBlock;
28,712,961✔
343
  pRow->rowIndex = rowIndex;
28,712,961✔
344
}
28,712,961✔
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) {
29,177,992✔
383
  if (pIter->rowIndex <= pIter->inputEndIndex) {
29,177,992✔
384
    setInputRowInfo(pRow, pIter, pIter->rowIndex, false);
28,712,922✔
385
    ++pIter->rowIndex;
28,716,081✔
386
    return true;
28,716,081✔
387
  } else {
388
    return false;
465,070✔
389
  }
390
}
391

392
int32_t funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, bool* res) {
29,178,172✔
393
  SFuncInputRowIter* pIter = &pCtx->rowIter;
29,178,172✔
394
  if (pCtx->hasPrimaryKey) {
29,178,172✔
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);
29,177,842✔
403
    return TSDB_CODE_SUCCESS;
29,179,935✔
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) {
26,702,162✔
411
  if (pCtx->subsidiaries.num <= 0) {
26,702,162!
412
    return TSDB_CODE_SUCCESS;
×
413
  }
414

415
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
53,404,324✔
416
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
26,702,162✔
417

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

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

427
    char* pData = colDataGetData(pSrcCol, rowIndex);
26,702,162!
428

429
    // append to dest col
430
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
26,702,162✔
431

432
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
26,702,162✔
433
    if (NULL == pDstCol) {
26,702,162!
434
      return TSDB_CODE_OUT_OF_RANGE;
×
435
    }
436
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
53,404,324✔
437
      colDataSetNULL(pDstCol, pos);
20!
438
    } else {
439
      int32_t code = colDataSetVal(pDstCol, pos, pData, false);
26,702,142✔
440
      if (TSDB_CODE_SUCCESS != code) {
26,702,142!
441
        return code;
×
442
      }
443
    }
444
  }
445
  return TSDB_CODE_SUCCESS;
26,702,162✔
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) {
309,219,492✔
454
  if (pResultInfo->initialized) {
309,219,492✔
455
    return TSDB_CODE_SUCCESS;  // already initialized
12,735✔
456
  }
457

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

462
  initResultRowEntry(pResultInfo, pCtx->resDataInfo.interBufSize);
309,206,757✔
463
  return TSDB_CODE_SUCCESS;
309,206,757✔
464
}
465

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

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

479
  return code;
140,920,674✔
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) {
223,565✔
513
  SNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
223,565✔
514
  if (QUERY_NODE_COLUMN == nodeType(pParam) && PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pParam)->colId) {
223,597!
515
    return FUNC_DATA_REQUIRED_NOT_LOAD;
164,917✔
516
  }
517
  return FUNC_DATA_REQUIRED_SMA_LOAD;
58,680✔
518
}
519

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

525
static int64_t getNumOfElems(SqlFunctionCtx* pCtx) {
104,876,289✔
526
  int64_t numOfElem = 0;
104,876,289✔
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;
104,876,289✔
534
  SColumnInfoData*      pInputCol = pInput->pData[0];
104,876,289✔
535
  if (1 == pInput->numOfRows && pInput->blankFill) {
104,876,289✔
536
    return 0;
453,490✔
537
  }
538
  if (pInput->colDataSMAIsSet && pInput->totalRows == pInput->numOfRows) {
104,422,799!
539
    numOfElem = pInput->numOfRows - pInput->pColumnDataAgg[0]->numOfNull;
4,677✔
540
  } else {
541
    if (pInputCol->hasNull) {
104,418,122✔
542
      for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
239,587,275✔
543
        if (colDataIsNull(pInputCol, pInput->totalRows, i, NULL)) {
428,500,248!
544
          continue;
1,831,555✔
545
        }
546
        numOfElem += 1;
212,418,569✔
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;
79,080,971✔
552
    }
553
  }
554
  return numOfElem;
104,422,799✔
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) {
104,915,284✔
562
  int64_t numOfElem = 0;
104,915,284✔
563

564
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
104,915,284✔
565
  SInputColumnInfoData* pInput = &pCtx->input;
104,915,284✔
566

567
  int32_t type = pInput->pData[0]->info.type;
104,915,284✔
568

569
  char* buf = GET_ROWCELL_INTERBUF(pResInfo);
104,915,284✔
570
  if (IS_NULL_TYPE(type)) {
104,915,284✔
571
    // select count(NULL) returns 0
572
    numOfElem = 1;
12,737✔
573
    *((int64_t*)buf) += 0;
12,737✔
574
  } else {
575
    numOfElem = getNumOfElems(pCtx);
104,902,547✔
576
    *((int64_t*)buf) += numOfElem;
104,551,965✔
577
  }
578

579
  if (tsCountAlwaysReturnValue) {
104,564,702!
580
    pResInfo->numOfRes = 1;
104,578,338✔
581
  } else {
582
    SET_VAL(pResInfo, *((int64_t*)buf), 1);
×
583
  }
584

585
  return TSDB_CODE_SUCCESS;
104,564,702✔
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) {
83,510,733✔
614
  int32_t numOfElem = 0;
83,510,733✔
615

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

621
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
83,510,733✔
622
  pSumRes->type = type;
83,510,733✔
623

624
  if (IS_NULL_TYPE(type)) {
83,510,733✔
625
    numOfElem = 0;
205✔
626
    goto _sum_over;
205✔
627
  }
628

629
  if (pInput->colDataSMAIsSet) {
83,510,528✔
630
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
519✔
631

632
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
519!
633
      pSumRes->isum += pAgg->sum;
519✔
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];
83,510,009✔
641

642
    int32_t start = pInput->startRowIndex;
83,510,009✔
643
    int32_t numOfRows = pInput->numOfRows;
83,510,009✔
644

645
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
83,510,009!
646
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
83,382,719!
647
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int8_t, numOfElem);
1,425,994✔
648
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
83,050,534✔
649
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int16_t, numOfElem);
340,371✔
650
      } else if (type == TSDB_DATA_TYPE_INT) {
83,003,519✔
651
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int32_t, numOfElem);
385,688,500✔
652
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
17,637,499✔
653
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int64_t, numOfElem);
68,498,904✔
654
      }
655
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
127,290!
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) {
127,184✔
666
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, double, numOfElem);
2,196,065✔
667
    } else if (type == TSDB_DATA_TYPE_FLOAT) {
41,315✔
668
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, float, numOfElem);
325,572✔
669
    }
670
  }
671

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

677
_sum_over:
83,755,248✔
678
  if (numOfElem == 0) {
83,510,733✔
679
    if (tsCountAlwaysReturnValue && pCtx->pExpr->pExpr->_function.pFunctNode->hasOriginalFunc &&
81,865✔
680
        fmIsCountLikeFunc(pCtx->pExpr->pExpr->_function.pFunctNode->originalFuncId)) {
11,312✔
681
      numOfElem = 1;
16✔
682
    }
683
  }
684
  // data in the check operation are all null, not output
685
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
83,510,733✔
686
  return TSDB_CODE_SUCCESS;
83,510,733✔
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) {
35✔
750
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
35✔
751
  SSumRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
35✔
752

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

757
  if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
35!
758
    pDBuf->isum += pSBuf->isum;
35✔
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);
35✔
765
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
35✔
766
  return TSDB_CODE_SUCCESS;
35✔
767
}
768

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

774
static bool funcNotSupportStringSma(SFunctionNode* pFunc) {
187,019✔
775
  SNode* pParam;
776
  switch (pFunc->funcType) {
187,019!
777
    case FUNCTION_TYPE_MAX:
187,019✔
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);
187,019✔
789
      if (pParam && nodesIsExprNode(pParam) && (IS_VAR_DATA_TYPE(((SExprNode*)pParam)->resType.type))) {
187,019!
790
        return true;
86✔
791
      }
792
      break;
186,933✔
793
    default:
×
794
      break;
×
795
  }
796
  return false;
186,933✔
797
}
798

799
EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
187,019✔
800
  if(funcNotSupportStringSma(pFunc)) {
187,019✔
801
    return FUNC_DATA_REQUIRED_DATA_LOAD;
86✔
802
  }
803
  return FUNC_DATA_REQUIRED_SMA_LOAD;
186,933✔
804
}
805

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

814
  SMinmaxResInfo* buf = GET_ROWCELL_INTERBUF(pResultInfo);
29,730,864✔
815
  buf->assign = false;
29,730,864✔
816
  buf->tuplePos.pageId = -1;
29,730,864✔
817

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

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

829
int32_t minFunction(SqlFunctionCtx* pCtx) {
17,426,247✔
830
  int32_t numOfElems = 0;
17,426,247✔
831
  int32_t code = doMinMaxHelper(pCtx, 1, &numOfElems);
17,426,247✔
832
  if (code != TSDB_CODE_SUCCESS) {
17,545,710!
833
    return code;
×
834
  }
835
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
17,545,710✔
836
  return TSDB_CODE_SUCCESS;
17,545,710✔
837
}
838

839
int32_t maxFunction(SqlFunctionCtx* pCtx) {
17,565,584✔
840
  int32_t numOfElems = 0;
17,565,584✔
841
  int32_t code = doMinMaxHelper(pCtx, 0, &numOfElems);
17,565,584✔
842
  if (code != TSDB_CODE_SUCCESS) {
17,657,551!
843
    return code;
×
844
  }
845
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
17,657,551✔
846
  return TSDB_CODE_SUCCESS;
17,657,551✔
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) {
29,571,263✔
854
  int32_t code = TSDB_CODE_SUCCESS;
29,571,263✔
855

856
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
29,571,263✔
857
  SMinmaxResInfo*      pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
29,571,263✔
858

859
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
29,571,263✔
860
  int32_t currentRow = pBlock->info.rows;
29,571,263✔
861

862
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
29,571,263✔
863
  if (NULL == pCol) {
29,577,828!
864
    return TSDB_CODE_OUT_OF_RANGE;
×
865
  }
866
  pEntryInfo->isNullRes = (pEntryInfo->numOfRes == 0) ? 1 : 0;
29,577,828✔
867

868
  // NOTE: do nothing change it, for performance issue
869
  if (!pEntryInfo->isNullRes) {
29,577,828✔
870
    switch (pCol->info.type) {
23,773,778!
871
      case TSDB_DATA_TYPE_UBIGINT:
8,888,305✔
872
      case TSDB_DATA_TYPE_BIGINT:
873
        ((int64_t*)pCol->pData)[currentRow] = pRes->v;
8,888,305✔
874
        break;
8,888,305✔
875
      case TSDB_DATA_TYPE_UINT:
14,778,451✔
876
      case TSDB_DATA_TYPE_INT:
877
        colDataSetInt32(pCol, currentRow, (int32_t*)&pRes->v);
14,778,451✔
878
        break;
14,778,451✔
879
      case TSDB_DATA_TYPE_USMALLINT:
65,916✔
880
      case TSDB_DATA_TYPE_SMALLINT:
881
        colDataSetInt16(pCol, currentRow, (int16_t*)&pRes->v);
65,916✔
882
        break;
65,916✔
883
      case TSDB_DATA_TYPE_BOOL:
57,633✔
884
      case TSDB_DATA_TYPE_UTINYINT:
885
      case TSDB_DATA_TYPE_TINYINT:
886
        colDataSetInt8(pCol, currentRow, (int8_t*)&pRes->v);
57,633✔
887
        break;
57,633✔
888
      case TSDB_DATA_TYPE_DOUBLE:
4,946✔
889
        colDataSetDouble(pCol, currentRow, (double*)&pRes->v);
4,946✔
890
        break;
4,946✔
891
      case TSDB_DATA_TYPE_FLOAT: {
2,366✔
892
        float v = GET_FLOAT_VAL(&pRes->v);
2,366✔
893
        colDataSetFloat(pCol, currentRow, &v);
2,366✔
894
        break;
2,366✔
895
      }
896
      case TSDB_DATA_TYPE_VARBINARY:
704✔
897
      case TSDB_DATA_TYPE_VARCHAR:
898
      case TSDB_DATA_TYPE_NCHAR: {
899
        code = colDataSetVal(pCol, currentRow, pRes->str, false);
704✔
900
        if (TSDB_CODE_SUCCESS != code) {
704!
901
          return code;
×
902
        }
903
        break;
704✔
904
      }
905
    }
906
  } else {
907
    colDataSetNULL(pCol, currentRow);
5,804,050!
908
  }
909

910
  taosMemoryFreeClear(pRes->str);
29,577,828!
911
  if (pCtx->subsidiaries.num > 0) {
29,577,828✔
912
    if (pEntryInfo->numOfRes > 0) {
55,577✔
913
      code = setSelectivityValue(pCtx, pBlock, &pRes->tuplePos, currentRow);
53,101✔
914
    } else {
915
      code = setNullSelectivityValue(pCtx, pBlock, currentRow);
2,476✔
916
    }
917
  }
918

919
  return code;
29,583,566✔
920
}
921

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

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

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

938
  return TSDB_CODE_SUCCESS;
2,514✔
939
}
940

941
int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos, int32_t rowIndex) {
29,488,544✔
942
  if (pCtx->subsidiaries.num <= 0) {
29,488,544✔
943
    return TSDB_CODE_SUCCESS;
29,371,533✔
944
  }
945

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

957
    bool* nullList = (bool*)p;
119,425✔
958
    char* pStart = (char*)(nullList + numOfCols * sizeof(bool));
119,425✔
959

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

965
      SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
136,718✔
966
      if (NULL == pDstCol) {
136,718!
967
        return terrno;
×
968
      }
969
      if (nullList[j]) {
136,718✔
970
        colDataSetNULL(pDstCol, rowIndex);
144!
971
      } else {
972
        code = colDataSetValOrCover(pDstCol, rowIndex, pStart, false);
136,574✔
973
        if (TSDB_CODE_SUCCESS != code) {
136,598!
974
          return code;
×
975
        }
976
      }
977
      pStart += pDstCol->info.bytes;
136,742✔
978
    }
979
  }
980

981
  return TSDB_CODE_SUCCESS;
119,446✔
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) {
976✔
987
  if (pCtx->subsidiaries.num <= 0) {
976!
988
    return TSDB_CODE_SUCCESS;
×
989
  }
990

991
  int32_t code = TSDB_CODE_SUCCESS;
976✔
992
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
3,542✔
993
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
2,566✔
994

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

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

1004
    char* pData = colDataGetData(pSrcCol, rowIndex);
2,566!
1005

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

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

1014
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
5,132✔
1015
      colDataSetNULL(pDstCol, pos);
392✔
1016
    } else {
1017
      code = colDataSetVal(pDstCol, pos, pData, false);
2,174✔
1018
      if (TSDB_CODE_SUCCESS != code) {
2,174!
1019
        return code;
×
1020
      }
1021
    }
1022
  }
1023
  return code;
976✔
1024
}
1025

1026
void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; }
27✔
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) {
51✔
1030
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
51✔
1031
  SMinmaxResInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
51✔
1032

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

1037
  switch (type) {
51!
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:
48✔
1048
    case TSDB_DATA_TYPE_INT:
1049
      if (pSBuf->assign && (COMPARE_MINMAX_DATA(int32_t) || !pDBuf->assign)) {
48!
1050
        pDBuf->v = pSBuf->v;
26✔
1051
        replaceTupleData(&pDBuf->tuplePos, &pSBuf->tuplePos);
26✔
1052
        pDBuf->assign = true;
26✔
1053
      }
1054
      break;
48✔
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);
51✔
1089
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
51✔
1090
  return TSDB_CODE_SUCCESS;
51✔
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) {
40✔
1097
  return minMaxCombine(pDestCtx, pSourceCtx, 0);
40✔
1098
}
1099

1100
int32_t getStdInfoSize() { return (int32_t)sizeof(SStdRes); }
7,763✔
1101

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

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

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

1120
int32_t stdFunction(SqlFunctionCtx* pCtx) {
188,443✔
1121
  int32_t numOfElem = 0;
188,443✔
1122

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

1127
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
188,443✔
1128
  pStdRes->type = type;
188,443✔
1129

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

1133
  int32_t start = pInput->startRowIndex;
188,443✔
1134
  int32_t numOfRows = pInput->numOfRows;
188,443✔
1135

1136
  if (IS_NULL_TYPE(type)) {
188,443✔
1137
    numOfElem = 0;
126✔
1138
    goto _stddev_over;
126✔
1139
  }
1140

1141
  switch (type) {
188,317!
1142
    case TSDB_DATA_TYPE_TINYINT: {
14,340✔
1143
      int8_t* plist = (int8_t*)pCol->pData;
14,340✔
1144
      for (int32_t i = start; i < numOfRows + start; ++i) {
142,497✔
1145
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
128,157✔
1146
          continue;
30,929✔
1147
        }
1148

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

1155
      break;
14,340✔
1156
    }
1157

1158
    case TSDB_DATA_TYPE_SMALLINT: {
78,899✔
1159
      int16_t* plist = (int16_t*)pCol->pData;
78,899✔
1160
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
325,842✔
1161
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
246,943✔
1162
          continue;
62,613✔
1163
        }
1164

1165
        numOfElem += 1;
184,330✔
1166
        pStdRes->count += 1;
184,330✔
1167
        pStdRes->isum += plist[i];
184,330✔
1168
        pStdRes->quadraticISum += plist[i] * plist[i];
184,330✔
1169
      }
1170
      break;
78,899✔
1171
    }
1172

1173
    case TSDB_DATA_TYPE_INT: {
12,839✔
1174
      int32_t* plist = (int32_t*)pCol->pData;
12,839✔
1175
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
150,060✔
1176
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
137,221✔
1177
          continue;
91,596✔
1178
        }
1179

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

1186
      break;
12,839✔
1187
    }
1188

1189
    case TSDB_DATA_TYPE_BIGINT: {
19,602✔
1190
      int64_t* plist = (int64_t*)pCol->pData;
19,602✔
1191
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
656,211✔
1192
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
636,609✔
1193
          continue;
29,319✔
1194
        }
1195

1196
        numOfElem += 1;
607,290✔
1197
        pStdRes->count += 1;
607,290✔
1198
        pStdRes->isum += plist[i];
607,290✔
1199
        pStdRes->quadraticISum += plist[i] * plist[i];
607,290✔
1200
      }
1201
      break;
19,602✔
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: {
13,023✔
1267
      float* plist = (float*)pCol->pData;
13,023✔
1268
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
734,410✔
1269
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
721,387✔
1270
          continue;
160,399✔
1271
        }
1272

1273
        numOfElem += 1;
560,988✔
1274
        pStdRes->count += 1;
560,988✔
1275
        pStdRes->dsum += plist[i];
560,988✔
1276
        pStdRes->quadraticDSum += plist[i] * plist[i];
560,988✔
1277
      }
1278
      break;
13,023✔
1279
    }
1280

1281
    case TSDB_DATA_TYPE_DOUBLE: {
49,603✔
1282
      double* plist = (double*)pCol->pData;
49,603✔
1283
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
2,017,519✔
1284
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,967,916✔
1285
          continue;
637,496✔
1286
        }
1287

1288
        numOfElem += 1;
1,330,420✔
1289
        pStdRes->count += 1;
1,330,420✔
1290
        pStdRes->dsum += plist[i];
1,330,420✔
1291
        pStdRes->quadraticDSum += plist[i] * plist[i];
1,330,420✔
1292
      }
1293
      break;
49,603✔
1294
    }
1295

1296
    default:
9✔
1297
      break;
9✔
1298
  }
1299

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

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

1322
  pOutput->count += pInput->count;
6,102✔
1323
}
1324

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

1329
  if (IS_NULL_TYPE(pCol->info.type)) {
6,109!
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) {
6,109!
1335
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1336
  }
1337

1338
  SStdRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
6,109✔
1339

1340
  for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
12,288✔
1341
    if (colDataIsNull_s(pCol, i)) continue;
12,358!
1342
    char*    data = colDataGetData(pCol, i);
6,179!
1343
    SStdRes* pInputInfo = (SStdRes*)varDataVal(data);
6,179✔
1344
    stdTransferInfo(pInputInfo, pInfo);
6,179✔
1345
  }
1346

1347
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
6,109✔
1348
  return TSDB_CODE_SUCCESS;
6,109✔
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) {
161,647✔
1419
  SInputColumnInfoData* pInput = &pCtx->input;
161,647✔
1420
  SStdRes*              pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
161,647✔
1421
  int32_t               type = pStddevRes->type;
161,647✔
1422
  double                avg;
1423

1424
  if (pStddevRes->count == 0) {
161,647✔
1425
    GET_RES_INFO(pCtx)->numOfRes = 0;
44,407✔
1426
    return functionFinalize(pCtx, pBlock);
44,407✔
1427
  }
1428

1429
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
117,240!
1430
    avg = pStddevRes->isum / ((double)pStddevRes->count);
74,098✔
1431
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticISum / ((double)pStddevRes->count) - avg * avg));
74,098✔
1432
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
43,142!
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);
43,140✔
1437
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticDSum / ((double)pStddevRes->count) - avg * avg));
43,140✔
1438
  }
1439

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

1445
  return functionFinalize(pCtx, pBlock);
117,240✔
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) {
6,175✔
1479
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
6,175✔
1480
  SStdRes*             pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
6,175✔
1481
  int32_t              resultBytes = getStdInfoSize();
6,175✔
1482
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
6,175!
1483

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

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

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

1499
  taosMemoryFree(res);
6,175!
1500
  return code;
6,175✔
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) {
8,134✔
1519
  pEnv->calcMemSize = sizeof(SLeastSQRInfo);
8,134✔
1520
  return true;
8,134✔
1521
}
1522

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

1531
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
47,254✔
1532

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

1538
int32_t leastSQRFunction(SqlFunctionCtx* pCtx) {
50,152✔
1539
  int32_t numOfElem = 0;
50,152✔
1540

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

1544
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
50,152✔
1545

1546
  SColumnInfoData* pCol = pInput->pData[0];
50,152✔
1547

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

1551
  int32_t start = pInput->startRowIndex;
50,152✔
1552
  int32_t numOfRows = pInput->numOfRows;
50,152✔
1553

1554
  switch (type) {
50,152!
1555
    case TSDB_DATA_TYPE_TINYINT: {
8,699✔
1556
      int8_t* plist = (int8_t*)pCol->pData;
8,699✔
1557
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
160,040✔
1558
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
151,341✔
1559
          continue;
1,636✔
1560
        }
1561
        numOfElem++;
149,705✔
1562
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
149,705✔
1563
      }
1564
      break;
8,699✔
1565
    }
1566
    case TSDB_DATA_TYPE_SMALLINT: {
8,904✔
1567
      int16_t* plist = (int16_t*)pCol->pData;
8,904✔
1568
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
161,037✔
1569
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
152,133✔
1570
          continue;
2,836✔
1571
        }
1572

1573
        numOfElem++;
149,297✔
1574
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
149,297✔
1575
      }
1576
      break;
8,904✔
1577
    }
1578

1579
    case TSDB_DATA_TYPE_INT: {
6,232✔
1580
      int32_t* plist = (int32_t*)pCol->pData;
6,232✔
1581
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
160,937✔
1582
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
154,705✔
1583
          continue;
140,336✔
1584
        }
1585

1586
        numOfElem++;
14,369✔
1587
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
14,369✔
1588
      }
1589
      break;
6,232✔
1590
    }
1591

1592
    case TSDB_DATA_TYPE_BIGINT: {
4,082✔
1593
      int64_t* plist = (int64_t*)pCol->pData;
4,082✔
1594
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
8,734✔
1595
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
4,652✔
1596
          continue;
2,036✔
1597
        }
1598

1599
        numOfElem++;
2,616✔
1600
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
2,616✔
1601
      }
1602
      break;
4,082✔
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: {
6,512✔
1656
      float* plist = (float*)pCol->pData;
6,512✔
1657
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
285,819✔
1658
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
279,307✔
1659
          continue;
275,491✔
1660
        }
1661

1662
        numOfElem++;
3,816✔
1663
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
3,816✔
1664
      }
1665
      break;
6,512✔
1666
    }
1667

1668
    case TSDB_DATA_TYPE_DOUBLE: {
15,415✔
1669
      double* plist = (double*)pCol->pData;
15,415✔
1670
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
455,312✔
1671
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
439,897✔
1672
          continue;
139,636✔
1673
        }
1674

1675
        numOfElem++;
300,261✔
1676
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
300,261✔
1677
      }
1678
      break;
15,415✔
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;
50,152✔
1691
  pInfo->num += numOfElem;
50,152✔
1692

1693
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
50,152✔
1694

1695
  return TSDB_CODE_SUCCESS;
50,152✔
1696
}
1697

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

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

1709
  if (0 == pInfo->num) {
47,265✔
1710
    colDataSetNULL(pCol, currentRow);
16,378!
1711
    return TSDB_CODE_SUCCESS;
16,378✔
1712
  }
1713

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

1716
  param[1][1] = (double)pInfo->num;
30,887✔
1717
  param[1][0] = param[0][1];
30,887✔
1718

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

1722
  if (0 == param00) {
30,887✔
1723
    colDataSetNULL(pCol, currentRow);
26,537!
1724
    return TSDB_CODE_SUCCESS;
26,537✔
1725
  }
1726

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

1732
  param12 /= param[1][1];
4,350✔
1733

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

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

1751
  return code;
4,362✔
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) {
615✔
1777
  pEnv->calcMemSize = sizeof(SPercentileInfo);
615✔
1778
  return true;
615✔
1779
}
1780

1781
int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
4,951✔
1782
  if (pResultInfo->initialized) {
4,951!
1783
    return TSDB_CODE_SUCCESS;
×
1784
  }
1785
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
4,951!
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);
4,951✔
1791
  SET_DOUBLE_VAL(&pInfo->minval, DBL_MAX);
4,951✔
1792
  SET_DOUBLE_VAL(&pInfo->maxval, -DBL_MAX);
4,951✔
1793
  pInfo->numOfElems = 0;
4,951✔
1794

1795
  return TSDB_CODE_SUCCESS;
4,951✔
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) {
12,108✔
1810
  int32_t              code = TSDB_CODE_SUCCESS;
12,108✔
1811
  int32_t              numOfElems = 0;
12,108✔
1812
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
12,108✔
1813

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

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

1820
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
12,108✔
1821
  if (pCtx->scanFlag == MAIN_SCAN && pInfo->stage == 0) {
12,108✔
1822
    pInfo->stage += 1;
4,951✔
1823

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

1837
  // the first stage, only acquire the min/max value
1838
  if (pInfo->stage == 0) {
10,136✔
1839
    if (pCtx->input.colDataSMAIsSet) {
6,073!
1840
      double tmin = 0.0, tmax = 0.0;
×
1841
      if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
1842
        tmin = (double)GET_INT64_VAL(&pAgg->min);
×
1843
        tmax = (double)GET_INT64_VAL(&pAgg->max);
×
1844
      } else if (IS_FLOAT_TYPE(type)) {
×
1845
        tmin = GET_DOUBLE_VAL(&pAgg->min);
×
1846
        tmax = GET_DOUBLE_VAL(&pAgg->max);
×
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) {
×
1853
        SET_DOUBLE_VAL(&pInfo->minval, tmin);
×
1854
      }
1855

1856
      if (GET_DOUBLE_VAL(&pInfo->maxval) < tmax) {
×
1857
        SET_DOUBLE_VAL(&pInfo->maxval, tmax);
×
1858
      }
1859

1860
      pInfo->numOfElems += (pInput->numOfRows - pAgg->numOfNull);
×
1861
    } else {
1862
      // check the valid data one by one
1863
      int32_t start = pInput->startRowIndex;
6,073✔
1864
      for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
518,176✔
1865
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
512,103✔
1866
          continue;
3,000✔
1867
        }
1868

1869
        char* data = colDataGetData(pCol, i);
509,103!
1870

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

1877
        if (v > GET_DOUBLE_VAL(&pInfo->maxval)) {
509,103✔
1878
          SET_DOUBLE_VAL(&pInfo->maxval, v);
408,583✔
1879
        }
1880

1881
        pInfo->numOfElems += 1;
509,103✔
1882
      }
1883
    }
1884
  } else {
1885
    // the second stage, calculate the true percentile value
1886
    int32_t start = pInput->startRowIndex;
4,063✔
1887
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
513,166✔
1888
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
509,103!
1889
        continue;
×
1890
      }
1891

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

1901
    SET_VAL(pResInfo, numOfElems, 1);
4,063!
1902
  }
1903

1904
  pCtx->needCleanup = true;
10,136✔
1905
  return TSDB_CODE_SUCCESS;
10,136✔
1906
}
1907

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

1912
  int32_t code = 0;
4,951✔
1913
  double  v = 0;
4,951✔
1914

1915
  tMemBucket** pMemBucket = &ppInfo->pMemBucket;
4,951✔
1916
  if ((*pMemBucket) != NULL && (*pMemBucket)->total > 0) {  // check for null
4,951!
1917
    if (pCtx->numOfParams > 2) {
2,979✔
1918
      char buf[3200] = {0};
18✔
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;
18✔
1922

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

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

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

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

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

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

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

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

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

1966
      tMemBucketDestroy(pMemBucket);
2,961✔
1967
      return functionFinalize(pCtx, pBlock);
2,961✔
1968
    }
1969
  } else {
1970
    return functionFinalize(pCtx, pBlock);
1,972✔
1971
  }
1972

1973
_fin_error:
×
1974

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

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

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

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

2004
  return algoType;
22,308✔
2005
}
2006

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

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

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

2024
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
182,307✔
2025

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

2030
  if (pCtx->numOfParams == 2) {
182,307✔
2031
    pInfo->algo = APERCT_ALGO_DEFAULT;
160,001✔
2032
  } else if (pCtx->numOfParams == 3) {
22,306!
2033
    pInfo->algo = getApercentileAlgo(varDataVal(pCtx->param[2].param.pz));
22,311✔
2034
    if (pInfo->algo == APERCT_ALGO_UNKNOWN) {
22,301!
2035
      return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
2036
    }
2037
  }
2038

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

2049
  return TSDB_CODE_SUCCESS;
182,301✔
2050
}
2051

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

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

2060
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
189,098✔
2061

2062
  int32_t start = pInput->startRowIndex;
189,098✔
2063
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
189,098✔
2064
    buildTDigestInfo(pInfo);
11,152✔
2065
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,153✔
2066
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
581,878✔
2067
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
570,754✔
2068
        continue;
273,232✔
2069
      }
2070
      numOfElems += 1;
297,522✔
2071
      char* data = colDataGetData(pCol, i);
297,522!
2072

2073
      double  v = 0;  // value
297,522✔
2074
      int64_t w = 1;  // weigth
297,522✔
2075
      GET_TYPED_DATA(v, double, type, data);
297,522✔
2076
      int32_t code = tdigestAdd(pInfo->pTDigest, v, w);
297,522✔
2077
      if (code != TSDB_CODE_SUCCESS) {
297,493!
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);
177,946✔
2085
    qDebug("%s before add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
177,953✔
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) {
2,063,906✔
2089
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
1,885,987✔
2090
        continue;
830,710✔
2091
      }
2092
      numOfElems += 1;
1,055,277✔
2093
      char* data = colDataGetData(pCol, i);
1,055,277!
2094

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

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

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

2112
static int32_t apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) {
5,930✔
2113
  pOutput->percent = pInput->percent;
5,930✔
2114
  pOutput->algo = pInput->algo;
5,930✔
2115
  if (pOutput->algo == APERCT_ALGO_TDIGEST) {
5,930✔
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);
5,837✔
2142
    if (pInput->pHisto->numOfElems <= 0) {
5,837✔
2143
      return TSDB_CODE_SUCCESS;
163✔
2144
    }
2145

2146
    if (hasRes) {
5,674✔
2147
      *hasRes = true;
5,672✔
2148
    }
2149

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

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

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

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

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

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

2184
  SInputColumnInfoData* pInput = &pCtx->input;
5,838✔
2185

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

2191
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,838✔
2192

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

2195
  bool    hasRes = false;
5,838✔
2196
  int32_t start = pInput->startRowIndex;
5,838✔
2197
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
11,764✔
2198
    char* data = colDataGetData(pCol, i);
5,926!
2199

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

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

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

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

2221
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
158,002✔
2222
    buildTDigestInfo(pInfo);
11,067✔
2223
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
11,067✔
2224
    if (pInfo->pTDigest->size > 0) {
11,067!
2225
      pInfo->result = tdigestQuantile(pInfo->pTDigest, pInfo->percent / 100);
11,067✔
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);
146,935✔
2232
    if (pInfo->pHisto->numOfElems > 0) {
146,936✔
2233
      qDebug("%s get the final res, elements:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
99,264✔
2234
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2235

2236
      double  ratio[] = {pInfo->percent};
99,264✔
2237
      double* res = NULL;
99,264✔
2238
      int32_t code = tHistogramUniform(pInfo->pHisto, ratio, 1, &res);
99,264✔
2239
      if (TSDB_CODE_SUCCESS != code) {
99,266!
2240
        taosMemoryFree(res);
×
2241
        return code;
×
2242
      }
2243
      pInfo->result = *res;
99,266✔
2244
      // memcpy(pCtx->pOutput, res, sizeof(double));
2245
      taosMemoryFree(res);
99,266!
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__,
47,672✔
2250
             pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries);
2251
    }
2252
  }
2253

2254
  return functionFinalize(pCtx, pBlock);
158,011✔
2255
}
2256

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

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

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

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

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

2284
  taosMemoryFree(res);
5,742!
2285
  return code;
5,742✔
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) {
1,022✔
2331
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
1,022✔
2332

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

2338
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
1,022✔
2339
  if (pResult->hasResult) {
1,022✔
2340
    if (pResult->pkBytes > 0) {
975✔
2341
      pResult->pkData = pResult->buf + pResult->bytes;
6✔
2342
    } else {
2343
      pResult->pkData = NULL;
969✔
2344
    }
2345
    if (pResult->ts < pBlockInfo->window.skey) {
975✔
2346
      return FUNC_DATA_REQUIRED_NOT_LOAD;
215✔
2347
    } else if (pResult->ts == pBlockInfo->window.skey) {
760✔
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;
512✔
2356
  } else {
2357
    return FUNC_DATA_REQUIRED_DATA_LOAD;
47✔
2358
  }
2359
}
2360

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

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

2369
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
1,576✔
2370
  if (pResult->hasResult) {
1,576✔
2371
    if (pResult->pkBytes > 0) {
1,517✔
2372
      pResult->pkData = pResult->buf + pResult->bytes;
5✔
2373
    } else {
2374
      pResult->pkData = NULL;
1,512✔
2375
    }
2376
    if (pResult->ts > pBlockInfo->window.ekey) {
1,517✔
2377
      return FUNC_DATA_REQUIRED_NOT_LOAD;
497✔
2378
    } else if (pResult->ts == pBlockInfo->window.ekey && pResult->pkData) {
1,020✔
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;
1,020✔
2384
  } else {
2385
    return FUNC_DATA_REQUIRED_DATA_LOAD;
59✔
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; }
22,898,694✔
2391

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

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

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

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

2417
  return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex);
95,532,965!
2418
}
2419

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

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

2434
static int32_t prepareBuf(SqlFunctionCtx* pCtx) {
336,134✔
2435
  if (pCtx->subsidiaries.rowLen == 0) {
336,134✔
2436
    int32_t rowLen = 0;
18,575✔
2437
    for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
43,247✔
2438
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
24,672✔
2439
      rowLen += pc->pExpr->base.resSchema.bytes;
24,672✔
2440
    }
2441

2442
    pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool);
18,575✔
2443
    pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen);
18,575!
2444
    if (NULL == pCtx->subsidiaries.buf) {
18,590!
2445
      return terrno;
×
2446
    }
2447
  }
2448
  return TSDB_CODE_SUCCESS;
336,149✔
2449
}
2450

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

2455
  if (pCtx->subsidiaries.num <= 0) {
54,628,885!
2456
    return TSDB_CODE_SUCCESS;
54,630,600✔
2457
  }
2458

2459
  if (!pInfo->hasResult) {
×
2460
    code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
14,318✔
2461
  } else if (!noElements) {
×
2462
    code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
2,680✔
2463
  } else { } // dothing
2464

2465
  return code;
17,030✔
2466
}
2467

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

2473
  if (IS_VAR_DATA_TYPE(type)) {
32,174,897!
2474
    if (type == TSDB_DATA_TYPE_JSON) {
176,451!
2475
      pInfo->bytes = getJsonValueLen(pData);
×
2476
    } else {
2477
      pInfo->bytes = varDataTLen(pData);
176,451✔
2478
    }
2479
  }
2480

2481
  (void)memcpy(pInfo->buf, pData, pInfo->bytes);
32,174,897✔
2482
  if (pkData != NULL) {
32,174,897✔
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;
32,174,897✔
2495
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
32,174,897✔
2496
  if (code != TSDB_CODE_SUCCESS) {
32,276,438!
2497
    return code;
×
2498
  }
2499

2500
  pInfo->hasResult = true;
32,276,438✔
2501
  return TSDB_CODE_SUCCESS;
32,276,438✔
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) {
28,738,421✔
2507
  int32_t numOfElems = 0;
28,738,421✔
2508

2509
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
28,738,421✔
2510
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
28,738,421✔
2511

2512
  SInputColumnInfoData* pInput = &pCtx->input;
28,738,421✔
2513
  SColumnInfoData*      pInputCol = pInput->pData[0];
28,738,421✔
2514

2515
  pInfo->bytes = pInputCol->info.bytes;
28,738,421✔
2516

2517
  if (IS_NULL_TYPE(pInputCol->info.type)) {
28,738,421✔
2518
    return TSDB_CODE_SUCCESS;
2,432✔
2519
  }
2520

2521
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
28,735,989✔
2522
  pInfo->pkType = -1;
28,735,989✔
2523
  __compar_fn_t pkCompareFn = NULL;
28,735,989✔
2524
  if (pCtx->hasPrimaryKey) {
28,735,989✔
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) &&
28,763,223!
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;
28,763,223!
2543

2544
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
28,763,223!
2545
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
28,763,223!
2546

2547
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
28,763,223✔
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;
28,763,223✔
2601

2602
  int     from = -1;
28,763,223✔
2603
  int32_t i = -1;
28,763,223✔
2604
  while (funcInputGetNextRowIndex(pInput, from, true, &i, &from)) {
79,195,796✔
2605
    if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
63,826,929!
2606
      continue;
55,072✔
2607
    }
2608

2609
    numOfElems++;
50,354,075✔
2610
    char* data = colDataGetData(pInputCol, i);
50,354,075!
2611
    char* pkData = NULL;
50,354,075✔
2612
    if (pCtx->hasPrimaryKey) {
50,354,075✔
2613
      pkData = colDataGetData(pkCol, i);
153!
2614
    }
2615
    TSKEY cts = pts[i];
50,354,075✔
2616
    if (pResInfo->numOfRes == 0 || pInfo->ts > cts ||
50,354,075✔
2617
        (pInfo->ts == cts && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
29,607,922!
2618
      int32_t code = doSaveCurrentVal(pCtx, i, cts, pkData, pInputCol->info.type, data);
20,746,153✔
2619
      if (code != TSDB_CODE_SUCCESS) {
20,769,579!
2620
        return code;
×
2621
      }
2622
      pResInfo->numOfRes = 1;
20,769,579✔
2623
    }
2624
  }
2625
#endif
2626

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

2639
int32_t lastFunction(SqlFunctionCtx* pCtx) {
18,683,899✔
2640
  int32_t numOfElems = 0;
18,683,899✔
2641

2642
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
18,683,899✔
2643
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
18,683,899✔
2644

2645
  SInputColumnInfoData* pInput = &pCtx->input;
18,683,899✔
2646
  SColumnInfoData*      pInputCol = pInput->pData[0];
18,683,899✔
2647

2648
  int32_t type = pInputCol->info.type;
18,683,899✔
2649
  int32_t bytes = pInputCol->info.bytes;
18,683,899✔
2650

2651
  if (IS_NULL_TYPE(type)) {
18,683,899✔
2652
    return TSDB_CODE_SUCCESS;
2,436✔
2653
  }
2654
  pInfo->bytes = bytes;
18,681,463✔
2655

2656
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
18,681,463✔
2657
  pInfo->pkType = -1;
18,681,463✔
2658
  __compar_fn_t pkCompareFn = NULL;
18,681,463✔
2659
  if (pCtx->hasPrimaryKey) {
18,681,463✔
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) &&
18,770,605!
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;
18,770,605!
2678

2679
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
18,770,605!
2680
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
18,770,605!
2681

2682
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
18,770,605✔
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;
18,770,605✔
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) {
31,212,028✔
2738
    numOfElems = 1;
12,465,882✔
2739

2740
    int32_t round = pInput->numOfRows >> 2;
12,465,882✔
2741
    int32_t reminder = pInput->numOfRows & 0x03;
12,465,882✔
2742

2743
    for (int32_t i = pInput->startRowIndex, tick = 0; tick < round; i += 4, tick += 1) {
19,390,194✔
2744
      int64_t cts = pts[i];
6,924,590✔
2745
      int32_t chosen = i;
6,924,590✔
2746

2747
      if (cts < pts[i + 1]) {
6,924,590✔
2748
        cts = pts[i + 1];
3,371,792✔
2749
        chosen = i + 1;
3,371,792✔
2750
      }
2751

2752
      if (cts < pts[i + 2]) {
6,924,590✔
2753
        cts = pts[i + 2];
3,372,099✔
2754
        chosen = i + 2;
3,372,099✔
2755
      }
2756

2757
      if (cts < pts[i + 3]) {
6,924,590✔
2758
        cts = pts[i + 3];
3,372,330✔
2759
        chosen = i + 3;
3,372,330✔
2760
      }
2761

2762
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
6,924,590✔
2763
        char*   data = colDataGetData(pInputCol, chosen);
2,194,601!
2764
        int32_t code = doSaveCurrentVal(pCtx, chosen, cts, NULL, type, data);
2,194,601✔
2765
        if (code != TSDB_CODE_SUCCESS) {
2,194,323!
2766
          return code;
×
2767
        }
2768
        pResInfo->numOfRes = 1;
2,194,323✔
2769
      }
2770
    }
2771

2772
    for (int32_t i = pInput->startRowIndex + round * 4; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
26,050,576✔
2773
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
13,609,153✔
2774
        char*   data = colDataGetData(pInputCol, i);
5,819,242!
2775
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], NULL, type, data);
5,819,242✔
2776
        if (code != TSDB_CODE_SUCCESS) {
5,795,061!
2777
          return code;
×
2778
        }
2779
        pResInfo->numOfRes = 1;
5,795,061✔
2780
      }
2781
    }
2782
  } else {
2783
    int     from = -1;
6,304,723✔
2784
    int32_t i = -1;
6,304,723✔
2785
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
123,173,079✔
2786
      if (colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
233,747,390✔
2787
        continue;
5,557,081✔
2788
      }
2789

2790
      numOfElems++;
111,316,614✔
2791
      char* pkData = NULL;
111,316,614✔
2792
      if (pCtx->hasPrimaryKey) {
111,316,614✔
2793
        pkData = colDataGetData(pkCol, i);
100,000,193!
2794
      }
2795
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i] ||
111,316,614✔
2796
          (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
107,993,487!
2797
        char*   data = colDataGetData(pInputCol, i);
3,392,812!
2798
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], pkData, type, data);
3,392,812✔
2799
        if (code != TSDB_CODE_SUCCESS) {
3,387,473!
2800
          return code;
×
2801
        }
2802
        pResInfo->numOfRes = 1;
3,387,473✔
2803
      }
2804
    }
2805
  }
2806
#endif
2807

2808
#endif
2809

2810
  // save selectivity value for column consisted of all null values
2811
  if (numOfElems == 0) {
18,890,095✔
2812
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
4,080,159✔
2813
    if (code != TSDB_CODE_SUCCESS) {
4,071,528!
2814
      return code;
×
2815
    }
2816
    pInfo->nullTupleSaved = true;
4,071,528✔
2817
  }
2818

2819
  return TSDB_CODE_SUCCESS;
18,881,464✔
2820
}
2821

2822
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) {
18,508,244✔
2823
  if (!pInput->hasResult) {
18,508,244✔
2824
    return false;
2✔
2825
  }
2826
  __compar_fn_t pkCompareFn = NULL;
18,508,242✔
2827
  if (pInput->pkData) {
18,508,242✔
2828
    pkCompareFn = getKeyComparFunc(pInput->pkType, (isFirst) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC);
52✔
2829
  }
2830
  if (pOutput->hasResult) {
18,508,242✔
2831
    if (isFirst) {
11,205,984✔
2832
      if (pInput->ts > pOutput->ts ||
8,571,536✔
2833
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
8,571,190!
2834
        return false;
346✔
2835
      }
2836
    } else {
2837
      if (pInput->ts < pOutput->ts ||
2,634,448✔
2838
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
2,631,877!
2839
        return false;
2,571✔
2840
      }
2841
    }
2842
  }
2843

2844
  pOutput->isNull = pInput->isNull;
18,505,325✔
2845
  pOutput->ts = pInput->ts;
18,505,325✔
2846
  pOutput->bytes = pInput->bytes;
18,505,325✔
2847
  pOutput->pkType = pInput->pkType;
18,505,325✔
2848

2849
  (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes);
18,505,325✔
2850
  if (pInput->pkData) {
18,505,325✔
2851
    pOutput->pkBytes = pInput->pkBytes;
43✔
2852
    (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes);
43✔
2853
    pOutput->pkData = pOutput->buf + pOutput->bytes;
43✔
2854
  }
2855
  return true;
18,505,325✔
2856
}
2857

2858
static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst,
18,508,238✔
2859
                                     int32_t rowIndex) {
2860
  if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) {
18,508,238✔
2861
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput, false);
18,505,322✔
2862
    if (TSDB_CODE_SUCCESS != code) {
18,505,322!
2863
      return code;
×
2864
    }
2865
    pOutput->hasResult = true;
18,505,322✔
2866
  }
2867
  return TSDB_CODE_SUCCESS;
18,508,238✔
2868
}
2869

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

2874
  if (IS_NULL_TYPE(pCol->info.type)) {
7,316,412!
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) {
7,316,412!
2880
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2881
  }
2882

2883
  SFirstLastRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7,316,412✔
2884

2885
  int32_t start = pInput->startRowIndex;
7,316,412✔
2886
  int32_t numOfElems = 0;
7,316,412✔
2887

2888
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
28,109,893✔
2889
    if (colDataIsNull_s(pCol, i)) {
41,586,962✔
2890
      continue;
2,285,243✔
2891
    }
2892
    char*          data = colDataGetData(pCol, i);
18,508,238!
2893
    SFirstLastRes* pInputInfo = (SFirstLastRes*)varDataVal(data);
18,508,238✔
2894
    if (pCtx->hasPrimaryKey) {
18,508,238✔
2895
      pInputInfo->pkData = pInputInfo->buf + pInputInfo->bytes;
52✔
2896
    } else {
2897
      pInputInfo->pkData = NULL;
18,508,186✔
2898
    }
2899

2900
    int32_t code = firstLastTransferInfo(pCtx, pInputInfo, pInfo, isFirstQuery, i);
18,508,238✔
2901
    if (code != TSDB_CODE_SUCCESS) {
18,508,238!
2902
      return code;
×
2903
    }
2904
    if (!numOfElems) {
18,508,238✔
2905
      numOfElems = pInputInfo->hasResult ? 1 : 0;
7,308,361✔
2906
    }
2907
  }
2908

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

2917
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
7,316,412✔
2918
  return TSDB_CODE_SUCCESS;
7,316,412✔
2919
}
2920

2921
int32_t firstFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, true); }
4,265,459✔
2922

2923
int32_t lastFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, false); }
3,050,953✔
2924

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

2933
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,754,274✔
2934
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
8,754,274✔
2935

2936
  SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo);
8,754,274✔
2937

2938
  if (pResInfo->isNullRes) {
8,754,274✔
2939
    colDataSetNULL(pCol, pBlock->info.rows);
31,209✔
2940
    return setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
31,209✔
2941
  }
2942
  code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes);
8,723,065!
2943
  if (TSDB_CODE_SUCCESS != code) {
8,723,068!
2944
    return code;
×
2945
  }
2946

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

2950
  return code;
8,723,068✔
2951
}
2952

2953
int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
22,679,360✔
2954
  int32_t code = TSDB_CODE_SUCCESS;
22,679,360✔
2955

2956
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
22,679,360✔
2957
  SFirstLastRes*       pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
22,679,360✔
2958

2959
  int32_t resultBytes = getFirstLastInfoSize(pRes->bytes, pRes->pkBytes);
22,679,360✔
2960

2961
  // todo check for failure
2962
  char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
22,619,210!
2963
  if (NULL == res) {
22,981,641!
2964
    return terrno;
×
2965
  }
2966
  (void)memcpy(varDataVal(res), pRes, resultBytes);
22,981,641✔
2967

2968
  varDataSetLen(res, resultBytes);
22,981,641✔
2969

2970
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
22,981,641✔
2971
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
22,981,641✔
2972
  if (NULL == pCol) {
22,842,139!
2973
    taosMemoryFree(res);
×
2974
    return TSDB_CODE_OUT_OF_RANGE;
×
2975
  }
2976

2977
  if (pEntryInfo->numOfRes == 0) {
22,909,389✔
2978
    colDataSetNULL(pCol, pBlock->info.rows);
2,377,820!
2979
    code = setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
2,377,820✔
2980
  } else {
2981
    code = colDataSetVal(pCol, pBlock->info.rows, res, false);
20,531,569✔
2982
    if (TSDB_CODE_SUCCESS != code) {
20,293,541!
2983
      taosMemoryFree(res);
×
2984
      return code;
×
2985
    }
2986
    code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
20,293,541✔
2987
  }
2988
  taosMemoryFree(res);
22,658,240✔
2989
  return code;
22,946,369✔
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) {
13,647✔
3007
  SInputColumnInfoData* pInput = &pCtx->input;
13,647✔
3008
  SColumnInfoData*      pInputCol = pInput->pData[0];
13,647✔
3009
  SColumnInfoData*      pkCol = pInput->pPrimaryKey;
13,647✔
3010

3011
  if (colDataIsNull_s(pInputCol, rowIndex)) {
27,294✔
3012
    pInfo->isNull = true;
2,204✔
3013
  } else {
3014
    pInfo->isNull = false;
11,443✔
3015

3016
    if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
11,443!
3017
      if (pInputCol->info.type == TSDB_DATA_TYPE_JSON) {
393!
3018
        pInfo->bytes = getJsonValueLen(pData);
×
3019
      } else {
3020
        pInfo->bytes = varDataTLen(pData);
393✔
3021
      }
3022
    }
3023

3024
    (void)memcpy(pInfo->buf, pData, pInfo->bytes);
11,443✔
3025
  }
3026

3027
  if (pCtx->hasPrimaryKey && !colDataIsNull_s(pkCol, rowIndex)) {
13,718!
3028
    char* pkData = colDataGetData(pkCol, rowIndex);
71!
3029
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
71!
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);
71✔
3037
    pInfo->pkData = pInfo->buf + pInfo->bytes;
71✔
3038
  }
3039
  pInfo->ts = cts;
13,647✔
3040
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
13,647✔
3041
  if (code != TSDB_CODE_SUCCESS) {
13,647!
3042
    return code;
×
3043
  }
3044

3045
  pInfo->hasResult = true;
13,647✔
3046

3047
  return TSDB_CODE_SUCCESS;
13,647✔
3048
}
3049

3050
int32_t lastRowFunction(SqlFunctionCtx* pCtx) {
27,626✔
3051
  int32_t numOfElems = 0;
27,626✔
3052

3053
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
27,626✔
3054
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
27,626✔
3055

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

3059
  int32_t type = pInputCol->info.type;
27,626✔
3060
  int32_t bytes = pInputCol->info.bytes;
27,626✔
3061
  pInfo->bytes = bytes;
27,626✔
3062

3063
  if (IS_NULL_TYPE(type)) {
27,626✔
3064
    return TSDB_CODE_SUCCESS;
56✔
3065
  }
3066
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
27,570✔
3067
  pInfo->pkType = -1;
27,570✔
3068
  __compar_fn_t pkCompareFn = NULL;
27,570✔
3069
  if (pCtx->hasPrimaryKey) {
27,570✔
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);
27,576✔
3075
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
27,576!
3076

3077
  if (pCtx->order == TSDB_ORDER_ASC && !pCtx->hasPrimaryKey) {
27,576✔
3078
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
46,329!
3079
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
23,176✔
3080
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
23,176!
3081
      TSKEY cts = getRowPTs(pInput->pPTS, i);
23,176!
3082
      numOfElems++;
23,176✔
3083

3084
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
23,176!
3085
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
5,904✔
3086
        if (code != TSDB_CODE_SUCCESS) return code;
5,903!
3087
      }
3088

3089
      break;
23,175✔
3090
    }
3091
  } else if (!pCtx->hasPrimaryKey && pCtx->order == TSDB_ORDER_DESC) {
4,422✔
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) {
8,123!
3095
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
4,062✔
3096
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
4,062!
3097
      TSKEY cts = getRowPTs(pInput->pPTS, i);
4,062!
3098
      numOfElems++;
4,062✔
3099

3100
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
4,062✔
3101
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
3,848✔
3102
        if (code != TSDB_CODE_SUCCESS) return code;
3,847!
3103
      }
3104
      break;
4,061✔
3105
    }
3106
  } else {
3107
    int64_t* pts = (int64_t*)pInput->pPTS->pData;
360✔
3108
    int      from = -1;
360✔
3109
    int32_t  i = -1;
360✔
3110
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
12,047✔
3111
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
11,687✔
3112
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
11,687!
3113
      TSKEY cts = pts[i];
11,687✔
3114

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

3131
  SET_VAL(pResInfo, numOfElems, 1);
27,597!
3132
  return TSDB_CODE_SUCCESS;
27,597✔
3133
}
3134

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

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

3160
static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, int64_t ts) {
54,945✔
3161
  switch (type) {
54,945!
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:
295✔
3166
    case TSDB_DATA_TYPE_TINYINT:
3167
      pDiffInfo->prev.i64 = *(int8_t*)pv;
295✔
3168
      break;
295✔
3169
    case TSDB_DATA_TYPE_UINT:
53,362✔
3170
    case TSDB_DATA_TYPE_INT:
3171
      pDiffInfo->prev.i64 = *(int32_t*)pv;
53,362✔
3172
      break;
53,362✔
3173
    case TSDB_DATA_TYPE_USMALLINT:
278✔
3174
    case TSDB_DATA_TYPE_SMALLINT:
3175
      pDiffInfo->prev.i64 = *(int16_t*)pv;
278✔
3176
      break;
278✔
3177
    case TSDB_DATA_TYPE_TIMESTAMP:
295✔
3178
    case TSDB_DATA_TYPE_UBIGINT:
3179
    case TSDB_DATA_TYPE_BIGINT:
3180
      pDiffInfo->prev.i64 = *(int64_t*)pv;
295✔
3181
      break;
295✔
3182
    case TSDB_DATA_TYPE_FLOAT:
261✔
3183
      pDiffInfo->prev.d64 = *(float*)pv;
261✔
3184
      break;
261✔
3185
    case TSDB_DATA_TYPE_DOUBLE:
426✔
3186
      pDiffInfo->prev.d64 = *(double*)pv;
426✔
3187
      break;
426✔
3188
    default:
×
3189
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3190
  }
3191
  pDiffInfo->prevTs = ts;
54,945✔
3192
  pDiffInfo->hasPrev = true;
54,945✔
3193
  return TSDB_CODE_SUCCESS;
54,945✔
3194
}
3195

3196
static bool diffIsNegtive(SDiffInfo* pDiffInfo, int32_t type, const char* pv) {
34,221✔
3197
  switch (type) {
34,221!
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: {
7,977✔
3203
      int64_t v = *(int32_t*)pv;
7,977✔
3204
      return v < pDiffInfo->prev.i64;
7,977✔
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: {
4,688✔
3215
      int64_t v = *(int8_t*)pv;
4,688✔
3216
      return v < pDiffInfo->prev.i64;
4,688✔
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: {
5,481✔
3223
      int64_t v = *(int16_t*)pv;
5,481✔
3224
      return v < pDiffInfo->prev.i64;
5,481✔
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:
4,202✔
3231
    case TSDB_DATA_TYPE_BIGINT: {
3232
      int64_t v = *(int64_t*)pv;
4,202✔
3233
      return v < pDiffInfo->prev.i64;
4,202✔
3234
    }
3235
    case TSDB_DATA_TYPE_FLOAT: {
6,570✔
3236
      float v = *(float*)pv;
6,570✔
3237
      return v < pDiffInfo->prev.d64;
6,570✔
3238
    }
3239
    case TSDB_DATA_TYPE_DOUBLE: {
5,279✔
3240
      double v = *(double*)pv;
5,279✔
3241
      return v < pDiffInfo->prev.d64;
5,279✔
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) {
27,364,901✔
3251
  bool isNegative = v < pDiffInfo->prev.i64;
27,364,901✔
3252
  if (type == TSDB_DATA_TYPE_UBIGINT) {
27,364,901✔
3253
    isNegative = (uint64_t)v < (uint64_t)pDiffInfo->prev.i64;
453✔
3254
  }
3255
  int64_t delta = v - pDiffInfo->prev.i64;
27,364,901✔
3256
  if (isNegative && ignoreNegative(pDiffInfo->ignoreOption)) {
27,364,901✔
3257
    colDataSetNull_f_s(pOutput, pos);
11,035✔
3258
    pOutput->hasNull = true;
11,035✔
3259
  } else {
3260
    colDataSetInt64(pOutput, pos, &delta);
27,353,866✔
3261
  }
3262
  pDiffInfo->prev.i64 = v;
27,364,901✔
3263
}
27,364,901✔
3264

3265
static void tryToSetDouble(SDiffInfo* pDiffInfo, SColumnInfoData* pOutput, double v, int32_t pos) {
97,584✔
3266
  double delta = v - pDiffInfo->prev.d64;
97,584✔
3267
  if (delta < 0 && ignoreNegative(pDiffInfo->ignoreOption)) {
97,584✔
3268
    colDataSetNull_f_s(pOutput, pos);
5,939✔
3269
  } else {
3270
    colDataSetDouble(pOutput, pos, &delta);
91,645✔
3271
  }
3272
  pDiffInfo->prev.d64 = v;
97,584✔
3273
}
97,584✔
3274

3275
static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, SColumnInfoData* pOutput, int32_t pos,
27,463,054✔
3276
                            int64_t ts) {
3277
  if (!pDiffInfo->hasPrev) {
27,463,054✔
3278
    colDataSetNull_f_s(pOutput, pos);
569✔
3279
    return doSetPrevVal(pDiffInfo, type, pv, ts);
569✔
3280
  }
3281
  pDiffInfo->prevTs = ts;
27,462,485✔
3282
  switch (type) {
27,462,485!
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,248,869✔
3289
      int64_t v = *(int32_t*)pv;
26,248,869✔
3290
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
26,248,869✔
3291
      break;
26,248,869✔
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: {
33,342✔
3304
      int64_t v = *(int8_t*)pv;
33,342✔
3305
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
33,342✔
3306
      break;
33,342✔
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: {
33,674✔
3314
      int64_t v = *(int16_t*)pv;
33,674✔
3315
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
33,674✔
3316
      break;
33,674✔
3317
    }
3318
    case TSDB_DATA_TYPE_TIMESTAMP:
1,037,178✔
3319
    case TSDB_DATA_TYPE_UBIGINT:
3320
    case TSDB_DATA_TYPE_BIGINT: {
3321
      int64_t v = *(int64_t*)pv;
1,037,178✔
3322
      tryToSetInt64(pDiffInfo, type, pOutput, v, pos);
1,037,178✔
3323
      break;
1,037,178✔
3324
    }
3325
    case TSDB_DATA_TYPE_FLOAT: {
47,876✔
3326
      double v = *(float*)pv;
47,876✔
3327
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
47,876✔
3328
      break;
47,876✔
3329
    }
3330
    case TSDB_DATA_TYPE_DOUBLE: {
49,708✔
3331
      double v = *(double*)pv;
49,708✔
3332
      tryToSetDouble(pDiffInfo, pOutput, v, pos);
49,708✔
3333
      break;
49,708✔
3334
    }
3335
    default:
×
3336
      return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
3337
  }
3338
  pDiffInfo->hasPrev = true;
27,462,485✔
3339
  return TSDB_CODE_SUCCESS;
27,462,485✔
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,
201,722,331✔
3345
                              int32_t* nextFrom) {
3346
  if (pInput->pPrimaryKey == NULL) {
201,722,331✔
3347
    if (from == -1) {
101,764,912✔
3348
      from = pInput->startRowIndex;
36,044,138✔
3349
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
65,720,774✔
3350
      return false;
35,786,537✔
3351
    }
3352
    *pRowIndex = from;
65,978,375✔
3353
    *nextFrom = from + 1;
65,978,375✔
3354
    return true;
65,978,375✔
3355
  } else {
3356
    if (from == -1) {
99,957,419✔
3357
      from = pInput->startRowIndex;
30,175✔
3358
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
99,927,244✔
3359
      return false;
30,175✔
3360
    }
3361
    TSKEY*           tsList = (int64_t*)pInput->pPTS->pData;
99,927,244✔
3362
    SColumnInfoData* pkCol = pInput->pPrimaryKey;
99,927,244✔
3363
    int8_t           pkType = pkCol->info.type;
99,927,244✔
3364
    int32_t          order = (firstOccur) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
99,927,244✔
3365
    __compar_fn_t    compareFunc = getKeyComparFunc(pkType, order);
99,927,244✔
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) {
27,608,114✔
3388
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
27,608,114✔
3389
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
27,608,114✔
3390

3391
  if (pRow->isDataNull || !pDiffInfo->hasPrev) {
27,608,114✔
3392
    return true;
145,557✔
3393
  } else if (ignoreNegative(pDiffInfo->ignoreOption)) {
27,462,557✔
3394
    return diffIsNegtive(pDiffInfo, pCtx->input.pData[0]->info.type, pRow->pData);
34,221✔
3395
  }
3396
  return false;
27,428,336✔
3397
}
3398

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

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

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

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

3421
int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) {
27,551,916✔
3422
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
27,551,916✔
3423
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
27,551,916✔
3424

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

3434
    // handle selectivity
3435
    if (pCtx->subsidiaries.num > 0) {
88,841✔
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;
88,841✔
3442
  }
3443

3444
  char* pv = pRow->pData;
27,463,075✔
3445

3446
  if (pRow->ts == pDiffInfo->prevTs) {
27,463,075✔
3447
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
21✔
3448
  }
3449
  code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts);
27,463,054✔
3450
  if (code != TSDB_CODE_SUCCESS) {
27,463,054!
3451
    return code;
×
3452
  }
3453
  // handle selectivity
3454
  if (pCtx->subsidiaries.num > 0) {
27,463,054✔
3455
    code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
26,701,951✔
3456
    if (code != TSDB_CODE_SUCCESS) {
26,701,951!
3457
      return code;
×
3458
    }
3459
  }
3460

3461
  return TSDB_CODE_SUCCESS;
27,463,054✔
3462
}
3463

3464
int32_t diffFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; }
371,549✔
3465

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

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

3479
  bool keepNull = false;
371,393✔
3480
  for (int i = 0; i < diffColNum; ++i) {
742,942✔
3481
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
371,549✔
3482
    if (NULL == pCtx) {
371,549!
3483
      code = terrno;
×
3484
      goto _exit;
×
3485
    }
3486
    funcInputUpdate(pCtx);
371,549✔
3487
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
371,549✔
3488
    SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
371,549✔
3489
    if (!ignoreNull(pDiffInfo->ignoreOption)) {
371,549✔
3490
      keepNull = true;
371,426✔
3491
    }
3492
  }
3493

3494
  SqlFunctionCtx* pCtx0 = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, 0);
371,393✔
3495
  SFuncInputRow*  pRow0 = (SFuncInputRow*)taosArrayGet(pRows, 0);
371,393✔
3496
  if (NULL == pCtx0 || NULL == pRow0) {
371,393!
3497
    code = terrno;
×
3498
    goto _exit;
×
3499
  }
3500
  int32_t startOffset = pCtx0->offset;
371,393✔
3501
  bool    result = false;
371,393✔
3502
  while (1) {
27,593,599✔
3503
    code = funcInputGetNextRow(pCtx0, pRow0, &result);
27,964,992✔
3504
    if (TSDB_CODE_SUCCESS != code) {
27,964,992!
3505
      goto _exit;
×
3506
    }
3507
    if (!result) {
27,964,992✔
3508
      break;
371,372✔
3509
    }
3510
    bool hasNotNullValue = !diffResultIsNull(pCtx0, pRow0);
27,593,620✔
3511
    for (int i = 1; i < diffColNum; ++i) {
27,608,114✔
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;
27,593,620✔
3532

3533
    bool newRow = false;
27,593,620✔
3534
    for (int i = 0; i < diffColNum; ++i) {
55,201,713✔
3535
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
27,608,114✔
3536
      SFuncInputRow*  pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
27,608,114✔
3537
      if (NULL == pCtx || NULL == pRow) {
27,608,114!
3538
        code = terrno;
×
3539
        goto _exit;
×
3540
      }
3541
      if ((keepNull || hasNotNullValue) && !isFirstRow(pCtx, pRow)) {
27,608,114✔
3542
        code = setDoDiffResult(pCtx, pRow, pos);
27,551,916✔
3543
        if (code != TSDB_CODE_SUCCESS) {
27,551,916✔
3544
          goto _exit;
21✔
3545
        }
3546
        newRow = true;
27,551,895✔
3547
      } else {
3548
        code = trySetPreVal(pCtx, pRow);
56,198✔
3549
        if (code != TSDB_CODE_SUCCESS) {
56,198!
3550
          goto _exit;
×
3551
        }
3552
      }
3553
    }
3554
    if (newRow) ++numOfElems;
27,593,599✔
3555
  }
3556

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

3567
_exit:
371,372✔
3568
  if (pRows) {
371,393!
3569
    taosArrayDestroy(pRows);
371,393✔
3570
    pRows = NULL;
371,393✔
3571
  }
3572
  return code;
371,393✔
3573
}
3574

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

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

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

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

3594
  pRes->maxSize = pCtx->param[1].param.i;
24,485✔
3595

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

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

3606
  return pRes;
480,643✔
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) {
23,727✔
3615
  int32_t              numOfElems = 0;
23,727✔
3616
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
23,727✔
3617

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

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

3624
  int32_t start = pInput->startRowIndex;
23,721✔
3625
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
375,724✔
3626
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
351,668✔
3627
      continue;
29,576✔
3628
    }
3629

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

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

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

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

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

3658
  int32_t start = pInput->startRowIndex;
3,196✔
3659
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
139,640✔
3660
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
136,447✔
3661
      continue;
28,466✔
3662
    }
3663

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

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

3680
  return TSDB_CODE_SUCCESS;
3,193✔
3681
}
3682

3683
static int32_t topBotResComparFn(const void* p1, const void* p2, const void* param) {
1,518,737✔
3684
  uint16_t type = *(uint16_t*)param;
1,518,737✔
3685

3686
  STopBotResItem* val1 = (STopBotResItem*)p1;
1,518,737✔
3687
  STopBotResItem* val2 = (STopBotResItem*)p2;
1,518,737✔
3688

3689
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
1,518,737!
3690
    if (val1->v.i == val2->v.i) {
813,780✔
3691
      return 0;
168,580✔
3692
    }
3693

3694
    return (val1->v.i > val2->v.i) ? 1 : -1;
645,200✔
3695
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
704,957!
3696
    if (val1->v.u == val2->v.u) {
484,889✔
3697
      return 0;
106,558✔
3698
    }
3699

3700
    return (val1->v.u > val2->v.u) ? 1 : -1;
378,331✔
3701
  } else if (TSDB_DATA_TYPE_FLOAT == type) {
220,068✔
3702
    if (val1->v.f == val2->v.f) {
29,782✔
3703
      return 0;
63✔
3704
    }
3705

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

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

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

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

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

3724
  STopBotResItem* pItems = pRes->pItems;
430,527✔
3725

3726
  // not full yet
3727
  if (pEntryInfo->numOfRes < pRes->maxSize) {
430,527✔
3728
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
113,798✔
3729
    pItem->v = val;
113,798✔
3730
    pItem->uid = uid;
113,798✔
3731

3732
    // save the data of this tuple
3733
    if (pCtx->subsidiaries.num > 0) {
113,798✔
3734
      code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
45,791✔
3735
      if (code != TSDB_CODE_SUCCESS) {
45,686!
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++;
113,693✔
3745
    code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
113,693✔
3746
                        topBotResComparFn, !isTopQuery);
113,693✔
3747
    if (code != TSDB_CODE_SUCCESS) {
114,040!
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) ||
316,729✔
3752
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) ||
163,696!
3753
                        (TSDB_DATA_TYPE_FLOAT == type && val.f > pItems[0].v.f) ||
109,191✔
3754
                        (TSDB_DATA_TYPE_DOUBLE == type && val.d > pItems[0].v.d))) ||
108,237✔
3755
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i < pItems[0].v.i) ||
177,510!
3756
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u < pItems[0].v.u) ||
81,995✔
3757
                         (TSDB_DATA_TYPE_FLOAT == type && val.f < pItems[0].v.f) ||
81,063✔
3758
                         (TSDB_DATA_TYPE_DOUBLE == type && val.d < pItems[0].v.d)))) {
79,674✔
3759
      // replace the old data and the coresponding tuple data
3760
      STopBotResItem* pItem = &pItems[0];
149,663✔
3761
      pItem->v = val;
149,663✔
3762
      pItem->uid = uid;
149,663✔
3763

3764
      // save the data of this tuple by over writing the old data
3765
      if (pCtx->subsidiaries.num > 0) {
149,663✔
3766
        code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
116,850✔
3767
        if (code != TSDB_CODE_SUCCESS) {
116,008!
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,
148,821✔
3775
                            topBotResComparFn, NULL, !isTopQuery);
148,821✔
3776
      if (code != TSDB_CODE_SUCCESS) {
149,484!
3777
        return code;
×
3778
      }
3779
    }
3780
  }
3781

3782
  return TSDB_CODE_SUCCESS;
430,590✔
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,
336,002✔
3792
                           char* buf, char** res) {
3793
  char* nullList = buf;
336,002✔
3794
  char* pStart = (char*)(nullList + sizeof(bool) * pSubsidiaryies->num);
336,002✔
3795

3796
  int32_t offset = 0;
336,002✔
3797
  for (int32_t i = 0; i < pSubsidiaryies->num; ++i) {
707,204✔
3798
    SqlFunctionCtx* pc = pSubsidiaryies->pCtx[i];
371,292✔
3799

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

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

3809
    SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
371,261✔
3810
    if (NULL == pCol) {
371,202!
3811
      return TSDB_CODE_OUT_OF_RANGE;
×
3812
    }
3813
    if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) {
742,404✔
3814
      offset += pCol->info.bytes;
663✔
3815
      continue;
663✔
3816
    }
3817

3818
    char* p = colDataGetData(pCol, rowIndex);
370,539!
3819
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
370,539✔
3820
      (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p));
31,518!
3821
    } else {
3822
      (void)memcpy(pStart + offset, p, pCol->info.bytes);
339,021✔
3823
    }
3824

3825
    offset += pCol->info.bytes;
370,539✔
3826
  }
3827

3828
  *res = buf;
335,912✔
3829
  return TSDB_CODE_SUCCESS;
335,912✔
3830
}
3831

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

3838
    if (pHandle->currentPage == -1) {
321,365✔
3839
      pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
20,525✔
3840
      if (pPage == NULL) {
20,526!
3841
        return terrno;
×
3842
      }
3843
      pPage->num = sizeof(SFilePage);
20,526✔
3844
    } else {
3845
      pPage = getBufPage(pHandle->pBuf, pHandle->currentPage);
300,840✔
3846
      if (pPage == NULL) {
300,795!
3847
        return terrno;
×
3848
      }
3849
      if (pPage->num + length > getBufPageSize(pHandle->pBuf)) {
300,795✔
3850
        // current page is all used, let's prepare a new buffer page
3851
        releaseBufPage(pHandle->pBuf, pPage);
260✔
3852
        pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
260✔
3853
        if (pPage == NULL) {
260!
3854
          return terrno;
×
3855
        }
3856
        pPage->num = sizeof(SFilePage);
260✔
3857
      }
3858
    }
3859

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

3863
    pPage->num += length;
321,299✔
3864
    setBufPageDirty(pPage, true);
321,299✔
3865
    releaseBufPage(pHandle->pBuf, pPage);
321,252✔
3866
  } else {  // other tuple save policy
3867
    if (pStore->streamStateFuncPut(pHandle->pState, key, pBuf, length) >= 0) {
48,461!
3868
      p.streamTupleKey = *key;
48,465✔
3869
    }
3870
  }
3871

3872
  *pPos = p;
369,650✔
3873
  return TSDB_CODE_SUCCESS;
369,650✔
3874
}
3875

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

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

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

3903
static int32_t doUpdateTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, STuplePos* pPos,
211,575✔
3904
                                 SFunctionStateStore* pStore) {
3905
  if (pHandle->pBuf != NULL) {
211,575✔
3906
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
130,591✔
3907
    if (pPage == NULL) {
130,373!
3908
      return terrno;
×
3909
    }
3910
    (void)memcpy(pPage->data + pPos->offset, pBuf, length);
130,373✔
3911
    setBufPageDirty(pPage, true);
130,373✔
3912
    releaseBufPage(pHandle->pBuf, pPage);
130,269✔
3913
  } else {
3914
    int32_t code = pStore->streamStateFuncPut(pHandle->pState, &pPos->streamTupleKey, pBuf, length);
80,984✔
3915
    if (TSDB_CODE_SUCCESS != code) {
80,994!
3916
      return code;
×
3917
    }
3918
  }
3919

3920
  return TSDB_CODE_SUCCESS;
211,026✔
3921
}
3922

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

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

3937
static int32_t doLoadTupleData(SSerializeDataHandle* pHandle, const STuplePos* pPos, SFunctionStateStore* pStore,
121,517✔
3938
                               char** value) {
3939
  if (pHandle->pBuf != NULL) {
121,517✔
3940
    SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId);
73,584✔
3941
    if (pPage == NULL) {
73,576!
3942
      *value = NULL;
×
3943
      return terrno;
×
3944
    }
3945
    *value = pPage->data + pPos->offset;
73,576✔
3946
    releaseBufPage(pHandle->pBuf, pPage);
73,576✔
3947
    return TSDB_CODE_SUCCESS;
73,570✔
3948
  } else {
3949
    *value = NULL;
47,933✔
3950
    int32_t vLen;
3951
    int32_t code = pStore->streamStateFuncGet(pHandle->pState, &pPos->streamTupleKey, (void**)(value), &vLen);
47,933✔
3952
    if (TSDB_CODE_SUCCESS != code) {
47,934!
3953
      return code;
×
3954
    }
3955
    return TSDB_CODE_SUCCESS;
47,934✔
3956
  }
3957
}
3958

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

3963
int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
24,513✔
3964
  int32_t code = TSDB_CODE_SUCCESS;
24,513✔
3965

3966
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
24,513✔
3967
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
24,513✔
3968

3969
  int16_t type = pCtx->pExpr->base.resSchema.type;
24,514✔
3970
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
24,514✔
3971

3972
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
24,514✔
3973
  if (NULL == pCol) {
24,514!
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;
24,514✔
3979
  if (pEntryInfo->numOfRes <= 0) {
24,514✔
3980
    colDataSetNULL(pCol, currentRow);
582!
3981
    code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, currentRow);
582✔
3982
    return code;
582✔
3983
  }
3984
  for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) {
137,761✔
3985
    STopBotResItem* pItem = &pRes->pItems[i];
113,827✔
3986
    code = colDataSetVal(pCol, currentRow, (const char*)&pItem->v.i, false);
113,827✔
3987
    if (TSDB_CODE_SUCCESS != code) {
113,809!
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);
113,809✔
3995
    if (TSDB_CODE_SUCCESS != code) {
113,829!
3996
      return code;
×
3997
    }
3998
    currentRow += 1;
113,829✔
3999
  }
4000

4001
  return code;
23,934✔
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); }
20,853✔
4078

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

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

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

4099
int32_t spreadFunction(SqlFunctionCtx* pCtx) {
159,703✔
4100
  int32_t numOfElems = 0;
159,703✔
4101

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

4107
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
159,703✔
4108

4109
  if (pInput->colDataSMAIsSet) {
159,703!
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];
159,703✔
4136

4137
    int32_t start = pInput->startRowIndex;
159,703✔
4138
    // check the valid data one by one
4139
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
5,301,526✔
4140
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
5,141,823✔
4141
        continue;
1,550,861✔
4142
      }
4143

4144
      char* data = colDataGetData(pCol, i);
3,590,962!
4145

4146
      double v = 0;
3,590,962✔
4147
      GET_TYPED_DATA(v, double, type, data);
3,590,962!
4148
      if (v < GET_DOUBLE_VAL(&pInfo->min)) {
3,590,962✔
4149
        SET_DOUBLE_VAL(&pInfo->min, v);
139,147✔
4150
      }
4151

4152
      if (v > GET_DOUBLE_VAL(&pInfo->max)) {
3,590,962✔
4153
        SET_DOUBLE_VAL(&pInfo->max, v);
571,764✔
4154
      }
4155

4156
      numOfElems += 1;
3,590,962✔
4157
    }
4158
  }
4159

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

4167
  return TSDB_CODE_SUCCESS;
159,703✔
4168
}
4169

4170
static void spreadTransferInfo(SSpreadInfo* pInput, SSpreadInfo* pOutput) {
19,293✔
4171
  pOutput->hasResult = pInput->hasResult;
19,293✔
4172
  if (pInput->max > pOutput->max) {
19,293✔
4173
    pOutput->max = pInput->max;
14,941✔
4174
  }
4175

4176
  if (pInput->min < pOutput->min) {
19,293✔
4177
    pOutput->min = pInput->min;
14,939✔
4178
  }
4179
}
19,293✔
4180

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

4185
  if (IS_NULL_TYPE(pCol->info.type)) {
15,295!
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) {
15,295!
4191
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4192
  }
4193

4194
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
15,295✔
4195

4196
  int32_t start = pInput->startRowIndex;
15,295✔
4197
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
34,791✔
4198
    if (colDataIsNull_s(pCol, i)) continue;
38,992!
4199
    char*        data = colDataGetData(pCol, i);
19,496!
4200
    SSpreadInfo* pInputInfo = (SSpreadInfo*)varDataVal(data);
19,496✔
4201
    if (pInputInfo->hasResult) {
19,496✔
4202
      spreadTransferInfo(pInputInfo, pInfo);
19,292✔
4203
    }
4204
  }
4205

4206
  if (pInfo->hasResult) {
15,295✔
4207
    GET_RES_INFO(pCtx)->numOfRes = 1;
15,160✔
4208
  }
4209

4210
  return TSDB_CODE_SUCCESS;
15,295✔
4211
}
4212

4213
int32_t spreadFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
128,119✔
4214
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
128,119✔
4215
  if (pInfo->hasResult == true) {
128,119✔
4216
    SET_DOUBLE_VAL(&pInfo->result, pInfo->max - pInfo->min);
110,322✔
4217
  } else {
4218
    GET_RES_INFO(pCtx)->isNullRes = 1;
17,797✔
4219
  }
4220
  return functionFinalize(pCtx, pBlock);
128,119✔
4221
}
4222

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

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

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

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

4248
_exit:
19,363✔
4249
  taosMemoryFree(res);
19,363!
4250
  return code;
19,366✔
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) {
20,229✔
4268
  pEnv->calcMemSize = sizeof(SElapsedInfo);
20,229✔
4269
  return true;
20,229✔
4270
}
4271

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

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

4285
  if (pCtx->numOfParams > 1) {
38,248✔
4286
    pInfo->timeUnit = pCtx->param[1].param.i;
22,485✔
4287
  } else {
4288
    pInfo->timeUnit = 1;
15,763✔
4289
  }
4290

4291
  return TSDB_CODE_SUCCESS;
38,248✔
4292
}
4293

4294
int32_t elapsedFunction(SqlFunctionCtx* pCtx) {
38,285✔
4295
  int32_t numOfElems = 0;
38,285✔
4296

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

4301
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
38,285✔
4302

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

4312
  if (pInput->colDataSMAIsSet) {
38,233!
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) {
38,233!
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];
38,233✔
4338

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

4348
      if (pCtx->end.key == INT64_MIN) {
216!
4349
        pInfo->min =
216✔
4350
            (pInfo->min > ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->min;
216✔
4351
      } else {
4352
        pInfo->min = pCtx->end.key;
×
4353
      }
4354
    } else {
4355
      if (pCtx->start.key == INT64_MIN) {
38,017✔
4356
        pInfo->min = (pInfo->min > ptsList[start]) ? ptsList[start] : pInfo->min;
36,110✔
4357
      } else {
4358
        pInfo->min = pCtx->start.key;
1,907✔
4359
      }
4360

4361
      if (pCtx->end.key == INT64_MIN) {
38,017✔
4362
        pInfo->max =
35,790✔
4363
            (pInfo->max < ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->max;
35,790✔
4364
      } else {
4365
        pInfo->max = pCtx->end.key + 1;
2,227✔
4366
      }
4367
    }
4368
  }
4369

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

4374
  return TSDB_CODE_SUCCESS;
38,285✔
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) {
38,311✔
4410
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
38,311✔
4411
  double        result = (double)pInfo->max - (double)pInfo->min;
38,311✔
4412
  result = (result >= 0) ? result : -result;
38,311✔
4413
  pInfo->result = result / pInfo->timeUnit;
38,311✔
4414
  return functionFinalize(pCtx, pBlock);
38,311✔
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() {
7,782✔
4460
  return (int32_t)sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
7,782✔
4461
}
4462

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

4468
static int8_t getHistogramBinType(char* binTypeStr) {
7,154✔
4469
  int8_t binType;
4470
  if (strcasecmp(binTypeStr, "user_input") == 0) {
7,154✔
4471
    binType = USER_INPUT_BIN;
4,822✔
4472
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
2,332✔
4473
    binType = LINEAR_BIN;
1,539✔
4474
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
793!
4475
    binType = LOG_BIN;
793✔
4476
  } else {
4477
    binType = UNKNOWN_BIN;
×
4478
  }
4479

4480
  return binType;
7,154✔
4481
}
4482

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

4495
    cJSON* start = cJSON_GetObjectItem(binDesc, "start");
2,333✔
4496
    cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
2,333✔
4497
    cJSON* width = cJSON_GetObjectItem(binDesc, "width");
2,333✔
4498
    cJSON* count = cJSON_GetObjectItem(binDesc, "count");
2,333✔
4499
    cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
2,333✔
4500

4501
    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
2,333!
4502
      cJSON_Delete(binDesc);
×
4503
      return TSDB_CODE_FAILED;
×
4504
    }
4505

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

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

4517
    int32_t counter = (int32_t)count->valueint;
2,333✔
4518
    if (infinity->valueint == false) {
2,333✔
4519
      startIndex = 0;
2,227✔
4520
      numOfBins = counter + 1;
2,227✔
4521
    } else {
4522
      startIndex = 1;
106✔
4523
      numOfBins = counter + 3;
106✔
4524
    }
4525

4526
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
2,333!
4527
    if (NULL == intervals) {
2,333!
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) {
2,333!
4533
      // linear bin process
4534
      if (width->valuedouble == 0) {
1,540!
4535
        taosMemoryFree(intervals);
×
4536
        cJSON_Delete(binDesc);
×
4537
        return TSDB_CODE_FAILED;
×
4538
      }
4539
      for (int i = 0; i < counter + 1; ++i) {
8,475✔
4540
        intervals[startIndex] = start->valuedouble + i * width->valuedouble;
6,935✔
4541
        if (isinf(intervals[startIndex])) {
6,935!
4542
          taosMemoryFree(intervals);
×
4543
          cJSON_Delete(binDesc);
×
4544
          return TSDB_CODE_FAILED;
×
4545
        }
4546
        startIndex++;
6,935✔
4547
      }
4548
    } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
793!
4549
      // log bin process
4550
      if (start->valuedouble == 0) {
793!
4551
        taosMemoryFree(intervals);
×
4552
        cJSON_Delete(binDesc);
×
4553
        return TSDB_CODE_FAILED;
×
4554
      }
4555
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
793!
4556
        taosMemoryFree(intervals);
×
4557
        cJSON_Delete(binDesc);
×
4558
        return TSDB_CODE_FAILED;
×
4559
      }
4560
      for (int i = 0; i < counter + 1; ++i) {
3,247✔
4561
        intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
2,454✔
4562
        if (isinf(intervals[startIndex])) {
2,454!
4563
          taosMemoryFree(intervals);
×
4564
          cJSON_Delete(binDesc);
×
4565
          return TSDB_CODE_FAILED;
×
4566
        }
4567
        startIndex++;
2,454✔
4568
      }
4569
    } else {
4570
      taosMemoryFree(intervals);
×
4571
      cJSON_Delete(binDesc);
×
4572
      return TSDB_CODE_FAILED;
×
4573
    }
4574

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

4625
  pInfo->numOfBins = numOfBins - 1;
7,158✔
4626
  pInfo->normalized = normalized;
7,158✔
4627
  for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
26,582✔
4628
    pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
19,424✔
4629
    pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
19,424✔
4630
    pInfo->bins[i].count = 0;
19,424✔
4631
  }
4632

4633
  taosMemoryFree(intervals);
7,158✔
4634
  cJSON_Delete(binDesc);
7,155✔
4635

4636
  return TSDB_CODE_SUCCESS;
7,154✔
4637
}
4638

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

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

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

4659
  if (binType == UNKNOWN_BIN) {
7,154!
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));
7,154!
4663
  if (binDesc == NULL) {
7,155!
4664
    return terrno;
×
4665
  }
4666
  int64_t normalized = pCtx->param[3].param.i;
7,155✔
4667
  if (normalized != 0 && normalized != 1) {
7,155!
4668
    taosMemoryFree(binDesc);
×
4669
    return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
4670
  }
4671
  int32_t code = getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized);
7,155✔
4672
  if (TSDB_CODE_SUCCESS != code) {
7,153!
4673
    taosMemoryFree(binDesc);
×
4674
    return code;
×
4675
  }
4676
  taosMemoryFree(binDesc);
7,153✔
4677

4678
  return TSDB_CODE_SUCCESS;
7,154✔
4679
}
4680

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

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

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

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

4692
  int32_t numOfElems = 0;
8,099✔
4693
  for (int32_t i = start; i < numOfRows + start; ++i) {
1,221,276✔
4694
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
1,213,177✔
4695
      continue;
489,181✔
4696
    }
4697

4698
    numOfElems++;
723,996✔
4699

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

4704
    for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
2,220,831✔
4705
      if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
1,833,392✔
4706
        pInfo->bins[k].count++;
336,557✔
4707
        pInfo->totalCount++;
336,557✔
4708
        break;
336,557✔
4709
      }
4710
    }
4711
  }
4712

4713
  if (!isPartial) {
8,099✔
4714
    GET_RES_INFO(pCtx)->numOfRes = pInfo->numOfBins;
5,506✔
4715
  } else {
4716
    GET_RES_INFO(pCtx)->numOfRes = 1;
2,593✔
4717
  }
4718
  return TSDB_CODE_SUCCESS;
8,099✔
4719
}
4720

4721
int32_t histogramFunction(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, false); }
5,502✔
4722

4723
int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, true); }
2,597✔
4724

4725
static void histogramTransferInfo(SHistoFuncInfo* pInput, SHistoFuncInfo* pOutput) {
1,657✔
4726
  pOutput->normalized = pInput->normalized;
1,657✔
4727
  pOutput->numOfBins = pInput->numOfBins;
1,657✔
4728
  pOutput->totalCount += pInput->totalCount;
1,657✔
4729
  for (int32_t k = 0; k < pOutput->numOfBins; ++k) {
7,613✔
4730
    pOutput->bins[k].lower = pInput->bins[k].lower;
5,956✔
4731
    pOutput->bins[k].upper = pInput->bins[k].upper;
5,956✔
4732
    pOutput->bins[k].count += pInput->bins[k].count;
5,956✔
4733
  }
4734
}
1,657✔
4735

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

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

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

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

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

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

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

4769
  if (pInfo->normalized) {
7,033✔
4770
    for (int32_t k = 0; k < pResInfo->numOfRes; ++k) {
11,082✔
4771
      if (pInfo->totalCount != 0) {
8,046✔
4772
        pInfo->bins[k].percentage = pInfo->bins[k].count / (double)pInfo->totalCount;
1,039✔
4773
      } else {
4774
        pInfo->bins[k].percentage = 0;
7,007✔
4775
      }
4776
    }
4777
  }
4778

4779
  for (int32_t i = 0; i < pResInfo->numOfRes; ++i) {
25,920✔
4780
    int32_t len;
4781
    char    buf[512] = {0};
18,887✔
4782
    if (!pInfo->normalized) {
18,887✔
4783
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE,
10,837✔
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,
8,050✔
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);
18,887✔
4792
    code = colDataSetVal(pCol, currentRow, buf, false);
18,887✔
4793
    if (TSDB_CODE_SUCCESS != code) {
18,887!
4794
      return code;
×
4795
    }
4796
    currentRow++;
18,887✔
4797
  }
4798

4799
  return code;
7,033✔
4800
}
4801

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

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

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

4823
_exit:
1,657✔
4824
  taosMemoryFree(res);
1,657!
4825
  return code;
1,657✔
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); }
8,674✔
4842

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

4848
static uint8_t hllCountNum(void* data, int32_t bytes, int32_t* buk) {
3,252,488✔
4849
  uint64_t hash = MurmurHash3_64(data, bytes);
3,252,488✔
4850
  int32_t  index = hash & HLL_BUCKET_MASK;
3,251,436✔
4851
  hash >>= HLL_BUCKET_BITS;
3,251,436✔
4852
  hash |= ((uint64_t)1 << HLL_DATA_BITS);
3,251,436✔
4853
  uint64_t bit = 1;
3,251,436✔
4854
  uint8_t  count = 1;
3,251,436✔
4855
  while ((hash & bit) == 0) {
6,401,767✔
4856
    count++;
3,150,331✔
4857
    bit <<= 1;
3,150,331✔
4858
  }
4859
  *buk = index;
3,251,436✔
4860
  return count;
3,251,436✔
4861
}
4862

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

4867
  for (int32_t j = 0; j < HLL_BUCKETS >> 3; j++) {
239,055,486✔
4868
    if (*word == 0) {
238,935,100✔
4869
      bucketHisto[0] += 8;
238,321,244✔
4870
    } else {
4871
      bytes = (uint8_t*)word;
613,856✔
4872
      bucketHisto[bytes[0]]++;
613,856✔
4873
      bucketHisto[bytes[1]]++;
613,856✔
4874
      bucketHisto[bytes[2]]++;
613,856✔
4875
      bucketHisto[bytes[3]]++;
613,856✔
4876
      bucketHisto[bytes[4]]++;
613,856✔
4877
      bucketHisto[bytes[5]]++;
613,856✔
4878
      bucketHisto[bytes[6]]++;
613,856✔
4879
      bucketHisto[bytes[7]]++;
613,856✔
4880
    }
4881
    word++;
238,935,100✔
4882
  }
4883
}
120,386✔
4884
static double hllTau(double x) {
120,383✔
4885
  if (x == 0. || x == 1.) return 0.;
120,383!
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) {
120,398✔
4899
  if (x == 1.0) return INFINITY;
120,398✔
4900
  double zPrime;
4901
  double y = 1;
101,074✔
4902
  double z = x;
101,074✔
4903
  do {
4904
    x *= x;
1,985,613✔
4905
    zPrime = z;
1,985,613✔
4906
    z += x * y;
1,985,613✔
4907
    y += y;
1,985,613✔
4908
  } while (zPrime != z);
1,985,613✔
4909
  return z;
101,074✔
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) {
120,356✔
4915
  double  m = HLL_BUCKETS;
120,356✔
4916
  int32_t buckethisto[64] = {0};
120,356✔
4917
  hllBucketHisto(buckets, buckethisto);
120,356✔
4918

4919
  double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m);
120,381✔
4920
  for (int j = HLL_DATA_BITS; j >= 1; --j) {
6,135,396✔
4921
    z += buckethisto[j];
6,014,999✔
4922
    z *= 0.5;
6,014,999✔
4923
  }
4924

4925
  z += m * hllSigma(buckethisto[0] / (double)m);
120,397✔
4926
  double E = (double)llroundl(HLL_ALPHA_INF * m * m / z);
120,396✔
4927

4928
  return (uint64_t)E;
120,396✔
4929
}
4930

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

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

4937
  int32_t type = pCol->info.type;
133,055✔
4938
  int32_t bytes = pCol->info.bytes;
133,055✔
4939

4940
  int32_t start = pInput->startRowIndex;
133,055✔
4941
  int32_t numOfRows = pInput->numOfRows;
133,055✔
4942

4943
  int32_t numOfElems = 0;
133,055✔
4944
  if (IS_NULL_TYPE(type)) {
133,055✔
4945
    goto _hll_over;
1,574✔
4946
  }
4947

4948
  for (int32_t i = start; i < numOfRows + start; ++i) {
4,422,936✔
4949
    if (pCol->hasNull && colDataIsNull_s(pCol, i)) {
5,933,603!
4950
      continue;
1,038,065✔
4951
    }
4952

4953
    numOfElems++;
3,254,069✔
4954

4955
    char* data = colDataGetData(pCol, i);
3,254,069!
4956
    if (IS_VAR_DATA_TYPE(type)) {
3,254,069!
4957
      bytes = varDataLen(data);
866,640✔
4958
      data = varDataVal(data);
866,640✔
4959
    }
4960

4961
    int32_t index = 0;
3,254,069✔
4962
    uint8_t count = hllCountNum(data, bytes, &index);
3,254,069✔
4963
    uint8_t oldcount = pInfo->buckets[index];
3,253,390✔
4964
    if (count > oldcount) {
3,253,390✔
4965
      pInfo->buckets[index] = count;
641,130✔
4966
    }
4967
  }
4968

4969
_hll_over:
130,802✔
4970
  pInfo->totalCount += numOfElems;
132,376✔
4971

4972
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
132,376✔
4973
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
3,674✔
4974
  } else {
4975
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
128,702✔
4976
  }
4977

4978
  return TSDB_CODE_SUCCESS;
132,376✔
4979
}
4980

4981
static void hllTransferInfo(SHLLInfo* pInput, SHLLInfo* pOutput) {
8,844✔
4982
  for (int32_t k = 0; k < HLL_BUCKETS; ++k) {
140,943,394✔
4983
    if (pOutput->buckets[k] < pInput->buckets[k]) {
140,934,550✔
4984
      pOutput->buckets[k] = pInput->buckets[k];
184,811✔
4985
    }
4986
  }
4987
  pOutput->totalCount += pInput->totalCount;
8,844✔
4988
}
8,844✔
4989

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

4994
  if (IS_NULL_TYPE(pCol->info.type)) {
8,772!
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) {
8,772!
5000
    return TSDB_CODE_SUCCESS;
×
5001
  }
5002

5003
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
8,772✔
5004

5005
  int32_t start = pInput->startRowIndex;
8,772✔
5006

5007
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
17,615✔
5008
    if (colDataIsNull_s(pCol, i)) continue;
17,684!
5009
    char*     data = colDataGetData(pCol, i);
8,842!
5010
    SHLLInfo* pInputInfo = (SHLLInfo*)varDataVal(data);
8,842✔
5011
    hllTransferInfo(pInputInfo, pInfo);
8,842✔
5012
  }
5013

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

5020
  return TSDB_CODE_SUCCESS;
8,773✔
5021
}
5022

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

5026
  SHLLInfo* pHllInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
120,355✔
5027
  pHllInfo->result = hllCountCnt(pHllInfo->buckets);
120,355✔
5028
  if (tsCountAlwaysReturnValue && pHllInfo->result == 0) {
120,400✔
5029
    pInfo->numOfRes = 1;
15,670✔
5030
  }
5031

5032
  return functionFinalize(pCtx, pBlock);
120,400✔
5033
}
5034

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

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

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

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

5057
_exit:
8,674✔
5058
  taosMemoryFree(res);
8,674!
5059
  return code;
8,673✔
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) {
7,744✔
5076
  pEnv->calcMemSize = sizeof(SStateInfo);
7,744✔
5077
  return true;
7,744✔
5078
}
5079

5080
static int8_t getStateOpType(char* opStr) {
7,961✔
5081
  int8_t opType;
5082
  if (strncasecmp(opStr, "LT", 2) == 0) {
7,961✔
5083
    opType = STATE_OPER_LT;
2,294✔
5084
  } else if (strncasecmp(opStr, "GT", 2) == 0) {
5,667✔
5085
    opType = STATE_OPER_GT;
1,711✔
5086
  } else if (strncasecmp(opStr, "LE", 2) == 0) {
3,956✔
5087
    opType = STATE_OPER_LE;
496✔
5088
  } else if (strncasecmp(opStr, "GE", 2) == 0) {
3,460✔
5089
    opType = STATE_OPER_GE;
1,359✔
5090
  } else if (strncasecmp(opStr, "NE", 2) == 0) {
2,101✔
5091
    opType = STATE_OPER_NE;
1,351✔
5092
  } else if (strncasecmp(opStr, "EQ", 2) == 0) {
750!
5093
    opType = STATE_OPER_EQ;
750✔
5094
  } else {
5095
    opType = STATE_OPER_INVALID;
×
5096
  }
5097

5098
  return opType;
7,961✔
5099
}
5100

5101
static bool checkStateOp(int8_t op, SColumnInfoData* pCol, int32_t index, SVariant param) {
422,472✔
5102
  char* data = colDataGetData(pCol, index);
422,472!
5103
  switch (pCol->info.type) {
422,472!
5104
    case TSDB_DATA_TYPE_TINYINT: {
166,348✔
5105
      int8_t v = *(int8_t*)data;
166,348✔
5106
      STATE_COMP(op, v, param);
166,348!
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: {
17,956✔
5115
      int16_t v = *(int16_t*)data;
17,956✔
5116
      STATE_COMP(op, v, param);
17,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: {
6,174✔
5125
      int32_t v = *(int32_t*)data;
6,174✔
5126
      STATE_COMP(op, v, param);
6,174!
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: {
99,184✔
5135
      int64_t v = *(int64_t*)data;
99,184✔
5136
      STATE_COMP(op, v, param);
99,184!
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: {
27,556✔
5145
      float v = *(float*)data;
27,556✔
5146
      STATE_COMP(op, v, param);
27,556!
5147
      break;
×
5148
    }
5149
    case TSDB_DATA_TYPE_DOUBLE: {
93,778✔
5150
      double v = *(double*)data;
93,778✔
5151
      STATE_COMP(op, v, param);
93,778!
5152
      break;
×
5153
    }
5154
    default: {
×
5155
      return false;
×
5156
    }
5157
  }
5158
  return false;
×
5159
}
5160

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

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

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

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

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

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

5186
    pInfo->isPrevTsSet = true;
442,481✔
5187
    numOfElems++;
442,481✔
5188

5189
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
442,481✔
5190
      colDataSetNULL(pOutput, i);
171,312!
5191
      // handle selectivity
5192
      if (pCtx->subsidiaries.num > 0) {
171,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;
171,312✔
5199
    }
5200

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

5203
    int64_t output = -1;
271,160✔
5204
    if (ret) {
271,160✔
5205
      output = ++pInfo->count;
210,134✔
5206
    } else {
5207
      pInfo->count = 0;
61,026✔
5208
    }
5209
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
271,160✔
5210
    if (TSDB_CODE_SUCCESS != code) {
271,169!
5211
      return code;
×
5212
    }
5213

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

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

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

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

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

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

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

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

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

5258
    pInfo->isPrevTsSet = true;
458,587✔
5259
    numOfElems++;
458,587✔
5260

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

5273
    bool    ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
151,304✔
5274
    int64_t output = -1;
151,304✔
5275
    if (ret) {
151,304✔
5276
      if (pInfo->durationStart == 0) {
80,953✔
5277
        output = 0;
32,033✔
5278
        pInfo->durationStart = tsList[i];
32,033✔
5279
      } else {
5280
        output = (tsList[i] - pInfo->durationStart) / timeUnit;
48,920✔
5281
      }
5282
    } else {
5283
      pInfo->durationStart = 0;
70,351✔
5284
    }
5285
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
151,304✔
5286
    if (TSDB_CODE_SUCCESS != code) {
151,304!
5287
      return code;
×
5288
    }
5289

5290
    // handle selectivity
5291
    if (pCtx->subsidiaries.num > 0) {
151,304✔
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;
5,441✔
5300
  return TSDB_CODE_SUCCESS;
5,441✔
5301
}
5302

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

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

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

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

5319
  int32_t numOfElems = 0;
5,891✔
5320
  int32_t type = pInputCol->info.type;
5,891✔
5321
  int32_t startOffset = pCtx->offset;
5,891✔
5322
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
879,892✔
5323
    if (pSumRes->isPrevTsSet == true && tsList[i] == pSumRes->prevTs) {
873,991✔
5324
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
18✔
5325
    } else {
5326
      pSumRes->prevTs = tsList[i];
873,973✔
5327
    }
5328
    pSumRes->isPrevTsSet = true;
873,973✔
5329

5330
    int32_t pos = startOffset + numOfElems;
873,973✔
5331
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
873,973✔
5332
      // colDataSetNULL(pOutput, i);
5333
      continue;
494,445✔
5334
    }
5335

5336
    char* data = colDataGetData(pInputCol, i);
379,528!
5337
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
678,087✔
5338
      int64_t v;
5339
      GET_TYPED_DATA(v, int64_t, type, data);
298,531!
5340
      pSumRes->isum += v;
298,531✔
5341
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false);
298,531✔
5342
      if (TSDB_CODE_SUCCESS != code) {
298,559!
5343
        return code;
×
5344
      }
5345
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
81,677!
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)) {
80,317!
5354
      double v;
5355
      GET_TYPED_DATA(v, double, type, data);
81,800!
5356
      pSumRes->dsum += v;
81,800✔
5357
      // check for overflow
5358
      if (isinf(pSumRes->dsum) || isnan(pSumRes->dsum)) {
81,800!
5359
        colDataSetNULL(pOutput, pos);
8!
5360
      } else {
5361
        code = colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false);
81,792✔
5362
        if (TSDB_CODE_SUCCESS != code) {
81,792!
5363
          return code;
×
5364
        }
5365
      }
5366
    }
5367

5368
    // handle selectivity
5369
    if (pCtx->subsidiaries.num > 0) {
379,556✔
5370
      code = appendSelectivityValue(pCtx, i, pos);
194✔
5371
      if (TSDB_CODE_SUCCESS != code) {
194!
5372
        return code;
×
5373
      }
5374
    }
5375

5376
    numOfElems++;
379,556✔
5377
  }
5378

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

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

5388
int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
8,233✔
5389
  if (pResultInfo->initialized) {
8,233✔
5390
    return TSDB_CODE_SUCCESS;
3,723✔
5391
  }
5392
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
4,510!
5393
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5394
  }
5395

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

5407
  return TSDB_CODE_SUCCESS;
4,511✔
5408
}
5409

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

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

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

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

5433
    int32_t pos = startOffset + numOfElems;
715,435✔
5434
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
715,435✔
5435
      // colDataSetNULL(pOutput, i);
5436
      continue;
298,495✔
5437
    }
5438

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

5443
    if (!pInfo->pointsMeet && (pInfo->pos < pInfo->numOfPoints - 1)) {
416,940✔
5444
      pInfo->points[pInfo->pos] = v;
415,142✔
5445
      pInfo->sum += v;
415,142✔
5446
    } else {
5447
      if (!pInfo->pointsMeet && (pInfo->pos == pInfo->numOfPoints - 1)) {
1,798!
5448
        pInfo->sum += v;
550✔
5449
        pInfo->pointsMeet = true;
550✔
5450
      } else {
5451
        pInfo->sum = pInfo->sum + v - pInfo->points[pInfo->pos];
1,248✔
5452
      }
5453

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

5466
      // handle selectivity
5467
      if (pCtx->subsidiaries.num > 0) {
1,798✔
5468
        code = appendSelectivityValue(pCtx, i, pos);
210✔
5469
        if (TSDB_CODE_SUCCESS != code) {
210!
5470
          return code;
×
5471
        }
5472
      }
5473

5474
      numOfElems++;
1,798✔
5475
    }
5476

5477
    pInfo->pos++;
416,940✔
5478
    if (pInfo->pos == pInfo->numOfPoints) {
416,940✔
5479
      pInfo->pos = 0;
900✔
5480
    }
5481
  }
5482

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

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

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

5494
  return pInfo;
24,972✔
5495
}
5496

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

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

5513
  taosSeedRand(taosSafeRand());
11,741✔
5514

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

5526
  return TSDB_CODE_SUCCESS;
11,742✔
5527
}
5528

5529
static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) {
294,028✔
5530
  assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType);
294,028✔
5531
}
294,063✔
5532

5533
static int32_t doReservoirSample(SqlFunctionCtx* pCtx, SSampleInfo* pInfo, char* data, int32_t index) {
305,511✔
5534
  pInfo->totalPoints++;
305,511✔
5535
  if (pInfo->numSampled < pInfo->samples) {
305,511✔
5536
    sampleAssignResult(pInfo, data, pInfo->numSampled);
288,035✔
5537
    if (pCtx->subsidiaries.num > 0) {
288,067✔
5538
      int32_t code = saveTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[pInfo->numSampled]);
6,162✔
5539
      if (code != TSDB_CODE_SUCCESS) {
6,180!
5540
        return code;
×
5541
      }
5542
    }
5543
    pInfo->numSampled++;
288,085✔
5544
  } else {
5545
    int32_t j = taosRand() % (pInfo->totalPoints);
17,476✔
5546
    if (j < pInfo->samples) {
17,480✔
5547
      sampleAssignResult(pInfo, data, j);
5,993✔
5548
      if (pCtx->subsidiaries.num > 0) {
5,993✔
5549
        int32_t code = updateTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[j]);
191✔
5550
        if (code != TSDB_CODE_SUCCESS) {
210!
5551
          return code;
×
5552
        }
5553
      }
5554
    }
5555
  }
5556

5557
  return TSDB_CODE_SUCCESS;
305,584✔
5558
}
5559

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

5564
  SInputColumnInfoData* pInput = &pCtx->input;
13,231✔
5565

5566
  SColumnInfoData* pInputCol = pInput->pData[0];
13,231✔
5567
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
908,429✔
5568
    if (colDataIsNull_s(pInputCol, i)) {
1,789,144✔
5569
      continue;
589,647✔
5570
    }
5571

5572
    char*   data = colDataGetData(pInputCol, i);
304,925!
5573
    int32_t code = doReservoirSample(pCtx, pInfo, data, i);
304,925✔
5574
    if (code != TSDB_CODE_SUCCESS) {
305,551!
5575
      return code;
×
5576
    }
5577
  }
5578

5579
  if (pInfo->numSampled == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
13,857✔
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);
13,857✔
5588
  return TSDB_CODE_SUCCESS;
13,857✔
5589
}
5590

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

5595
  SSampleInfo* pInfo = getSampleOutputInfo(pCtx);
11,741✔
5596
  pEntryInfo->complete = true;
11,741✔
5597

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

5604
  int32_t currentRow = pBlock->info.rows;
11,740✔
5605
  if (pInfo->numSampled == 0) {
11,740✔
5606
    colDataSetNULL(pCol, currentRow);
3,340✔
5607
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
3,340✔
5608
    return code;
3,341✔
5609
  }
5610
  for (int32_t i = 0; i < pInfo->numSampled; ++i) {
296,149✔
5611
    code = colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false);
287,754✔
5612
    if (TSDB_CODE_SUCCESS != code) {
287,782!
5613
      return code;
×
5614
    }
5615
    code = setSelectivityValue(pCtx, pBlock, &pInfo->tuplePos[i], currentRow + i);
287,782✔
5616
    if (TSDB_CODE_SUCCESS != code) {
287,749!
5617
      return code;
×
5618
    }
5619
  }
5620

5621
  return code;
8,395✔
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) {
5,565✔
5863
  pEnv->calcMemSize = sizeof(SModeInfo);
5,565✔
5864
  return true;
5,565✔
5865
}
5866

5867
int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
5,279✔
5868
  if (pResInfo->initialized) {
5,279!
5869
    return TSDB_CODE_SUCCESS;
×
5870
  }
5871
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
5,279!
5872
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5873
  }
5874

5875
  SModeInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,279✔
5876
  pInfo->colType = pCtx->resDataInfo.type;
5,279✔
5877
  pInfo->colBytes = pCtx->resDataInfo.bytes;
5,279✔
5878
  if (pInfo->pHash != NULL) {
5,279!
5879
    taosHashClear(pInfo->pHash);
×
5880
  } else {
5881
    pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5,279✔
5882
    if (NULL == pInfo->pHash) {
5,279!
5883
      return terrno;
×
5884
    }
5885
  }
5886
  pInfo->nullTupleSaved = false;
5,279✔
5887
  pInfo->nullTuplePos.pageId = -1;
5,279✔
5888

5889
  pInfo->buf = taosMemoryMalloc(pInfo->colBytes);
5,279!
5890
  if (NULL == pInfo->buf) {
5,279!
5891
    taosHashCleanup(pInfo->pHash);
×
5892
    pInfo->pHash = NULL;
×
5893
    return terrno;
×
5894
  }
5895
  pCtx->needCleanup = true;
5,279✔
5896
  return TSDB_CODE_SUCCESS;
5,279✔
5897
}
5898

5899
static void modeFunctionCleanup(SModeInfo* pInfo) {
5,279✔
5900
  taosHashCleanup(pInfo->pHash);
5,279✔
5901
  pInfo->pHash = NULL;
5,279✔
5902
  taosMemoryFreeClear(pInfo->buf);
5,279!
5903
}
5,279✔
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) {
245,502✔
5913
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
245,502!
5914
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
9,547!
5915
      (void)memcpy(pInfo->buf, data, getJsonValueLen(data));
×
5916
    } else {
5917
      (void)memcpy(pInfo->buf, data, varDataTLen(data));
9,547✔
5918
    }
5919
  } else {
5920
    (void)memcpy(pInfo->buf, data, pInfo->colBytes);
235,955✔
5921
  }
5922

5923
  return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore);
245,502✔
5924
}
5925

5926
static int32_t doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCtx, char* data) {
359,424✔
5927
  int32_t code = TSDB_CODE_SUCCESS;
359,424✔
5928
  int32_t hashKeyBytes;
5929
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
359,424!
5930
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
9,596!
5931
      hashKeyBytes = getJsonValueLen(data);
×
5932
    } else {
5933
      hashKeyBytes = varDataTLen(data);
9,596✔
5934
    }
5935
  } else {
5936
    hashKeyBytes = pInfo->colBytes;
349,828✔
5937
  }
5938

5939
  SModeItem* pHashItem = (SModeItem*)taosHashGet(pInfo->pHash, data, hashKeyBytes);
359,405✔
5940
  if (pHashItem == NULL) {
359,148✔
5941
    int32_t   size = sizeof(SModeItem);
245,439✔
5942
    SModeItem item = {0};
245,439✔
5943

5944
    item.count += 1;
245,439✔
5945
    code = saveModeTupleData(pCtx, data, pInfo, &item.dataPos);
245,439✔
5946
    if (code != TSDB_CODE_SUCCESS) {
245,396!
5947
      return code;
×
5948
    }
5949

5950
    if (pCtx->subsidiaries.num > 0) {
245,396✔
5951
      code = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &item.tuplePos);
325✔
5952
      if (code != TSDB_CODE_SUCCESS) {
325!
5953
        return code;
×
5954
      }
5955
    }
5956

5957
    code = taosHashPut(pInfo->pHash, data, hashKeyBytes, &item, sizeof(SModeItem));
245,396✔
5958
    if (code != TSDB_CODE_SUCCESS) {
245,809!
5959
      return code;
×
5960
    }
5961
  } else {
5962
    pHashItem->count += 1;
113,709✔
5963
    if (pCtx->subsidiaries.num > 0) {
113,709✔
5964
      code = updateTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &pHashItem->tuplePos);
109✔
5965
      if (code != TSDB_CODE_SUCCESS) {
109!
5966
        return code;
×
5967
      }
5968
    }
5969
  }
5970

5971
  return code;
359,518✔
5972
}
5973

5974
int32_t modeFunction(SqlFunctionCtx* pCtx) {
6,335✔
5975
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
6,335✔
5976
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
6,335✔
5977

5978
  SInputColumnInfoData* pInput = &pCtx->input;
6,335✔
5979

5980
  SColumnInfoData* pInputCol = pInput->pData[0];
6,335✔
5981
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
6,335✔
5982

5983
  int32_t numOfElems = 0;
6,335✔
5984
  int32_t startOffset = pCtx->offset;
6,335✔
5985
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
949,449✔
5986
    if (colDataIsNull_s(pInputCol, i)) {
1,886,328✔
5987
      continue;
584,418✔
5988
    }
5989
    numOfElems++;
358,746✔
5990

5991
    char*   data = colDataGetData(pInputCol, i);
358,746!
5992
    int32_t code = doModeAdd(pInfo, i, pCtx, data);
358,746✔
5993
    if (code != TSDB_CODE_SUCCESS) {
359,513✔
5994
      modeFunctionCleanup(pInfo);
817✔
5995
      return code;
×
5996
    }
5997
  }
5998

5999
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
6,285!
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);
6,285✔
6009

6010
  return TSDB_CODE_SUCCESS;
6,285✔
6011
}
6012

6013
int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
5,279✔
6014
  int32_t              code = TSDB_CODE_SUCCESS;
5,279✔
6015
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5,279✔
6016
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5,279✔
6017
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
5,279✔
6018
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5,279✔
6019
  int32_t              currentRow = pBlock->info.rows;
5,279✔
6020
  if (NULL == pCol) {
5,279!
6021
    modeFunctionCleanup(pInfo);
×
6022
    return TSDB_CODE_OUT_OF_RANGE;
×
6023
  }
6024

6025
  STuplePos resDataPos, resTuplePos;
6026
  int32_t   maxCount = 0;
5,279✔
6027

6028
  void* pIter = taosHashIterate(pInfo->pHash, NULL);
5,279✔
6029
  while (pIter != NULL) {
251,239✔
6030
    SModeItem* pItem = (SModeItem*)pIter;
245,961✔
6031
    if (pItem->count >= maxCount) {
245,961✔
6032
      maxCount = pItem->count;
228,697✔
6033
      resDataPos = pItem->dataPos;
228,697✔
6034
      resTuplePos = pItem->tuplePos;
228,697✔
6035
    }
6036

6037
    pIter = taosHashIterate(pInfo->pHash, pIter);
245,961✔
6038
  }
6039

6040
  if (maxCount != 0) {
5,278✔
6041
    char* pData = NULL;
2,080✔
6042
    code = loadTupleData(pCtx, &resDataPos, &pData);
2,080✔
6043
    if (pData == NULL || TSDB_CODE_SUCCESS != code) {
2,080!
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);
2,080✔
6052
    if (TSDB_CODE_SUCCESS != code) {
2,080!
6053
      modeFunctionCleanup(pInfo);
×
6054
      return code;
×
6055
    }
6056
    code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow);
2,080✔
6057
  } else {
6058
    colDataSetNULL(pCol, currentRow);
3,198✔
6059
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
3,198✔
6060
  }
6061

6062
  modeFunctionCleanup(pInfo);
5,279✔
6063

6064
  return code;
5,279✔
6065
}
6066

6067
bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
3,895✔
6068
  pEnv->calcMemSize = sizeof(STwaInfo);
3,895✔
6069
  return true;
3,895✔
6070
}
6071

6072
int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
46,892✔
6073
  if (pResultInfo->initialized) {
46,892!
6074
    return TSDB_CODE_SUCCESS;
×
6075
  }
6076
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
46,892!
6077
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6078
  }
6079

6080
  STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
46,894✔
6081
  pInfo->numOfElems = 0;
46,894✔
6082
  pInfo->p.key = INT64_MIN;
46,894✔
6083
  pInfo->win = TSWINDOW_INITIALIZER;
46,894✔
6084
  return TSDB_CODE_SUCCESS;
46,894✔
6085
}
6086

6087
static double twa_get_area(SPoint1 s, SPoint1 e) {
627,596✔
6088
  if (e.key == INT64_MAX || s.key == INT64_MIN) {
627,596!
6089
    return 0;
×
6090
  }
6091

6092
  if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) {
627,784✔
6093
    return (s.val + e.val) * (e.key - s.key) / 2;
549,367✔
6094
  }
6095

6096
  double x = (s.key * e.val - e.key * s.val) / (e.val - s.val);
78,417✔
6097
  double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2;
78,417✔
6098
  return val;
78,417✔
6099
}
6100

6101
int32_t twaFunction(SqlFunctionCtx* pCtx) {
47,603✔
6102
  int32_t               code = TSDB_CODE_SUCCESS;
47,603✔
6103
  SInputColumnInfoData* pInput = &pCtx->input;
47,603✔
6104
  SColumnInfoData*      pInputCol = pInput->pData[0];
47,603✔
6105

6106
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
47,603✔
6107
  STwaInfo*            pInfo = GET_ROWCELL_INTERBUF(pResInfo);
47,603✔
6108
  SPoint1*             last = &pInfo->p;
47,603✔
6109

6110
  if (IS_NULL_TYPE(pInputCol->info.type)) {
47,603!
6111
    pInfo->numOfElems = 0;
×
6112
    goto _twa_over;
×
6113
  }
6114

6115
  funcInputUpdate(pCtx);
47,603✔
6116
  SFuncInputRow row = {0};
47,611✔
6117
  bool          result = false;
47,611✔
6118
  if (pCtx->start.key != INT64_MIN && last->key == INT64_MIN) {
47,611!
6119
    while (1) {
6120
      code = funcInputGetNextRow(pCtx, &row, &result);
13,314✔
6121
      if (TSDB_CODE_SUCCESS != code) {
13,314!
6122
        return code;
×
6123
      }
6124
      if (!result) {
13,314✔
6125
        break;
2✔
6126
      }
6127
      if (row.isDataNull) {
13,312✔
6128
        continue;
2✔
6129
      }
6130

6131
      last->key = row.ts;
13,310✔
6132

6133
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
13,310!
6134

6135
      pInfo->dOutput += twa_get_area(pCtx->start, *last);
13,310✔
6136
      pInfo->win.skey = pCtx->start.key;
13,310✔
6137
      pInfo->numOfElems++;
13,310✔
6138
      break;
13,310✔
6139
    }
6140
  } else if (pInfo->p.key == INT64_MIN) {
34,299✔
6141
    while (1) {
6142
      code = funcInputGetNextRow(pCtx, &row, &result);
158,824✔
6143
      if (TSDB_CODE_SUCCESS != code) {
158,819!
6144
        return code;
×
6145
      }
6146
      if (!result) {
158,819✔
6147
        break;
14,853✔
6148
      }
6149
      if (row.isDataNull) {
143,966✔
6150
        continue;
125,076✔
6151
      }
6152

6153
      last->key = row.ts;
18,890✔
6154

6155
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
18,890!
6156

6157
      pInfo->win.skey = last->key;
18,890✔
6158
      pInfo->numOfElems++;
18,890✔
6159
      break;
18,890✔
6160
    }
6161
  }
6162

6163
  SPoint1 st = {0};
47,606✔
6164

6165
  // calculate the value of
6166
  while (1) {
6167
    code = funcInputGetNextRow(pCtx, &row, &result);
649,590✔
6168
    if (TSDB_CODE_SUCCESS != code) {
650,357!
6169
      return code;
×
6170
    }
6171
    if (!result) {
650,357✔
6172
      break;
47,613✔
6173
    }
6174
    if (row.isDataNull) {
602,744✔
6175
      continue;
630✔
6176
    }
6177
    pInfo->numOfElems++;
602,114✔
6178
    switch (pInputCol->info.type) {
602,114✔
6179
      case TSDB_DATA_TYPE_TINYINT: {
65,750✔
6180
        INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData);
65,750✔
6181
        break;
65,750✔
6182
      }
6183
      case TSDB_DATA_TYPE_SMALLINT: {
65,390✔
6184
        INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData);
65,390✔
6185
        break;
65,390✔
6186
      }
6187
      case TSDB_DATA_TYPE_INT: {
98,037✔
6188
        INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData);
98,037✔
6189
        break;
98,037✔
6190
      }
6191
      case TSDB_DATA_TYPE_BIGINT: {
75,422✔
6192
        INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData);
75,422✔
6193
        break;
75,422✔
6194
      }
6195
      case TSDB_DATA_TYPE_FLOAT: {
42,289✔
6196
        INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData);
42,289✔
6197
        break;
42,289✔
6198
      }
6199
      case TSDB_DATA_TYPE_DOUBLE: {
68,364✔
6200
        INIT_INTP_POINT(st, row.ts, *(double*)row.pData);
68,364✔
6201
        break;
68,364✔
6202
      }
6203
      case TSDB_DATA_TYPE_UTINYINT: {
47,982✔
6204
        INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData);
47,982✔
6205
        break;
47,982✔
6206
      }
6207
      case TSDB_DATA_TYPE_USMALLINT: {
48,397✔
6208
        INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData);
48,397✔
6209
        break;
48,397✔
6210
      }
6211
      case TSDB_DATA_TYPE_UINT: {
50,886✔
6212
        INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData);
50,886✔
6213
        break;
50,886✔
6214
      }
6215
      case TSDB_DATA_TYPE_UBIGINT: {
38,632✔
6216
        INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData);
38,632✔
6217
        break;
38,632✔
6218
      }
6219
      default: {
965✔
6220
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
965✔
6221
      }
6222
    }
6223
    if (pInfo->p.key == st.key) {
601,149!
6224
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6225
    }
6226

6227
    pInfo->dOutput += twa_get_area(pInfo->p, st);
601,149✔
6228
    pInfo->p = st;
601,354✔
6229
  }
6230

6231
  // the last interpolated time window value
6232
  if (pCtx->end.key != INT64_MIN) {
47,613✔
6233
    pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end);
13,317✔
6234
    pInfo->p = pCtx->end;
13,317✔
6235
    pInfo->numOfElems += 1;
13,317✔
6236
  }
6237

6238
  pInfo->win.ekey = pInfo->p.key;
47,613✔
6239

6240
_twa_over:
47,613✔
6241
  SET_VAL(pResInfo, 1, 1);
47,613✔
6242
  return TSDB_CODE_SUCCESS;
47,613✔
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) {
47,009✔
6258
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
47,009✔
6259

6260
  STwaInfo* pInfo = (STwaInfo*)GET_ROWCELL_INTERBUF(pResInfo);
47,009✔
6261
  if (pInfo->numOfElems == 0) {
47,009✔
6262
    pResInfo->numOfRes = 0;
14,685✔
6263
  } else {
6264
    if (pInfo->win.ekey == pInfo->win.skey) {
32,324✔
6265
      pInfo->dTwaRes = pInfo->p.val;
11,892✔
6266
    } else if (pInfo->win.ekey == INT64_MAX || pInfo->win.skey == INT64_MIN) {  // no data in timewindow
20,432!
6267
      pInfo->dTwaRes = 0;
2✔
6268
    } else {
6269
      pInfo->dTwaRes = pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey);
20,430✔
6270
    }
6271

6272
    pResInfo->numOfRes = 1;
32,324✔
6273
  }
6274

6275
  return functionFinalize(pCtx, pBlock);
47,009✔
6276
}
6277

6278
int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
7✔
6279
  if (pResultInfo->initialized) {
7!
6280
    return TSDB_CODE_SUCCESS;
×
6281
  }
6282
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
7!
6283
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6284
  }
6285

6286
  STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
7✔
6287
  pInfo->minRows = INT32_MAX;
7✔
6288
  return TSDB_CODE_SUCCESS;
7✔
6289
}
6290

6291
int32_t blockDistFunction(SqlFunctionCtx* pCtx) {
11✔
6292
  const int32_t BLOCK_DIST_RESULT_ROWS = 25;
11✔
6293

6294
  SInputColumnInfoData* pInput = &pCtx->input;
11✔
6295
  SColumnInfoData*      pInputCol = pInput->pData[0];
11✔
6296
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
11✔
6297
  STableBlockDistInfo*  pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
11✔
6298

6299
  STableBlockDistInfo p1 = {0};
11✔
6300
  if (tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
11!
6301
    qError("failed to deserialize block dist info");
×
6302
    return TSDB_CODE_FAILED;
×
6303
  }
6304

6305
  pDistInfo->numOfBlocks += p1.numOfBlocks;
11✔
6306
  pDistInfo->numOfTables += p1.numOfTables;
11✔
6307
  pDistInfo->numOfInmemRows += p1.numOfInmemRows;
11✔
6308
  pDistInfo->numOfSttRows += p1.numOfSttRows;
11✔
6309
  pDistInfo->totalSize += p1.totalSize;
11✔
6310
  pDistInfo->totalRows += p1.totalRows;
11✔
6311
  pDistInfo->numOfFiles += p1.numOfFiles;
11✔
6312

6313
  pDistInfo->defMinRows = p1.defMinRows;
11✔
6314
  pDistInfo->defMaxRows = p1.defMaxRows;
11✔
6315
  pDistInfo->rowSize = p1.rowSize;
11✔
6316

6317
  if (pDistInfo->minRows > p1.minRows) {
11✔
6318
    pDistInfo->minRows = p1.minRows;
1✔
6319
  }
6320
  if (pDistInfo->maxRows < p1.maxRows) {
11✔
6321
    pDistInfo->maxRows = p1.maxRows;
1✔
6322
  }
6323
  pDistInfo->numOfVgroups += (p1.numOfTables != 0 ? 1 : 0);
11✔
6324
  for (int32_t i = 0; i < tListLen(pDistInfo->blockRowsHisto); ++i) {
231✔
6325
    pDistInfo->blockRowsHisto[i] += p1.blockRowsHisto[i];
220✔
6326
  }
6327

6328
  pResInfo->numOfRes = BLOCK_DIST_RESULT_ROWS;  // default output rows
11✔
6329
  return TSDB_CODE_SUCCESS;
11✔
6330
}
6331

6332
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo) {
21✔
6333
  SEncoder encoder = {0};
21✔
6334
  int32_t  code = 0;
21✔
6335
  int32_t  lino;
6336
  int32_t  tlen;
6337
  tEncoderInit(&encoder, buf, bufLen);
21✔
6338

6339
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
21!
6340
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->rowSize));
44!
6341

6342
  TAOS_CHECK_EXIT(tEncodeU16(&encoder, pInfo->numOfFiles));
44!
6343
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfBlocks));
44!
6344
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfTables));
44!
6345

6346
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalSize));
44!
6347
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalRows));
44!
6348
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->maxRows));
44!
6349
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->minRows));
44!
6350
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMaxRows));
44!
6351
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMinRows));
44!
6352
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfInmemRows));
44!
6353
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfSttRows));
44!
6354
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfVgroups));
44!
6355

6356
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
462✔
6357
    TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->blockRowsHisto[i]));
880!
6358
  }
6359

6360
  tEndEncode(&encoder);
22✔
6361

6362
_exit:
22✔
6363
  if (code) {
22!
6364
    tlen = code;
×
6365
  } else {
6366
    tlen = encoder.pos;
22✔
6367
  }
6368
  tEncoderClear(&encoder);
22✔
6369
  return tlen;
22✔
6370
}
6371

6372
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo) {
11✔
6373
  SDecoder decoder = {0};
11✔
6374
  int32_t  code = 0;
11✔
6375
  int32_t  lino;
6376
  tDecoderInit(&decoder, buf, bufLen);
11✔
6377

6378
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
11!
6379
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->rowSize));
22!
6380

6381
  TAOS_CHECK_EXIT(tDecodeU16(&decoder, &pInfo->numOfFiles));
22!
6382
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfBlocks));
22!
6383
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfTables));
22!
6384

6385
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalSize));
22!
6386
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalRows));
22!
6387
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->maxRows));
22!
6388
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->minRows));
22!
6389
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMaxRows));
22!
6390
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMinRows));
22!
6391
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfInmemRows));
22!
6392
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfSttRows));
22!
6393
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfVgroups));
22!
6394

6395
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
231✔
6396
    TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->blockRowsHisto[i]));
440!
6397
  }
6398

6399
_exit:
11✔
6400
  tDecoderClear(&decoder);
11✔
6401
  return code;
11✔
6402
}
6403

6404
int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
7✔
6405
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
7✔
6406
  STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo);
7✔
6407

6408
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
7✔
6409
  if (NULL == pColInfo) {
7!
6410
    return TSDB_CODE_OUT_OF_RANGE;
×
6411
  }
6412

6413
  if (pData->totalRows == 0) {
7✔
6414
    pData->minRows = 0;
6✔
6415
  }
6416

6417
  int32_t row = 0;
7✔
6418
  char    st[256] = {0};
7✔
6419
  double  averageSize = 0;
7✔
6420
  if (pData->numOfBlocks != 0) {
7✔
6421
    averageSize = ((double)pData->totalSize) / pData->numOfBlocks;
1✔
6422
  }
6423
  uint64_t totalRawSize = pData->totalRows * pData->rowSize;
7✔
6424
  double   compRatio = 0;
7✔
6425
  if (totalRawSize != 0) {
7✔
6426
    compRatio = pData->totalSize * 100 / (double)totalRawSize;
1✔
6427
  }
6428

6429
  int32_t len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
14✔
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, '%');
7✔
6432

6433
  varDataSetLen(st, len);
7✔
6434
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
7✔
6435
  if (TSDB_CODE_SUCCESS != code) {
7!
6436
    return code;
×
6437
  }
6438

6439
  int64_t avgRows = 0;
7✔
6440
  if (pData->numOfBlocks > 0) {
7✔
6441
    avgRows = pData->totalRows / pData->numOfBlocks;
1✔
6442
  }
6443

6444
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
7✔
6445
                  "Block_Rows=[%" PRId64 "] MinRows=[%d] MaxRows=[%d] AvgRows=[%" PRId64 "]", pData->totalRows,
6446
                  pData->minRows, pData->maxRows, avgRows);
6447
  varDataSetLen(st, len);
7✔
6448
  code = colDataSetVal(pColInfo, row++, st, false);
7✔
6449
  if (TSDB_CODE_SUCCESS != code) {
7!
6450
    return code;
×
6451
  }
6452

6453
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Inmem_Rows=[%u] Stt_Rows=[%u] ",
7✔
6454
                  pData->numOfInmemRows, pData->numOfSttRows);
6455
  varDataSetLen(st, len);
7✔
6456
  code = colDataSetVal(pColInfo, row++, st, false);
7✔
6457
  if (TSDB_CODE_SUCCESS != code) {
7!
6458
    return code;
×
6459
  }
6460

6461
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
14✔
6462
                  "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables, pData->numOfFiles,
7✔
6463
                  pData->numOfVgroups);
6464

6465
  varDataSetLen(st, len);
7✔
6466
  code = colDataSetVal(pColInfo, row++, st, false);
7✔
6467
  if (TSDB_CODE_SUCCESS != code) {
7!
6468
    return code;
×
6469
  }
6470

6471
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
7✔
6472
                  "--------------------------------------------------------------------------------");
6473
  varDataSetLen(st, len);
7✔
6474
  code = colDataSetVal(pColInfo, row++, st, false);
7✔
6475
  if (TSDB_CODE_SUCCESS != code) {
7!
6476
    return code;
×
6477
  }
6478

6479
  int32_t maxVal = 0;
7✔
6480
  int32_t minVal = INT32_MAX;
7✔
6481
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
147✔
6482
    if (maxVal < pData->blockRowsHisto[i]) {
140✔
6483
      maxVal = pData->blockRowsHisto[i];
1✔
6484
    }
6485

6486
    if (minVal > pData->blockRowsHisto[i]) {
140✔
6487
      minVal = pData->blockRowsHisto[i];
8✔
6488
    }
6489
  }
6490

6491
  // maximum number of step is 80
6492
  double factor = pData->numOfBlocks / 80.0;
7✔
6493

6494
  int32_t numOfBuckets = sizeof(pData->blockRowsHisto) / sizeof(pData->blockRowsHisto[0]);
7✔
6495
  int32_t bucketRange = ceil(((double)(pData->defMaxRows - pData->defMinRows)) / numOfBuckets);
7✔
6496

6497
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
147✔
6498
    len =
140✔
6499
        tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "%04d |", pData->defMinRows + bucketRange * (i + 1));
140✔
6500

6501
    int32_t num = 0;
140✔
6502
    if (pData->blockRowsHisto[i] > 0) {
140✔
6503
      num = (pData->blockRowsHisto[i]) / factor;
1✔
6504
    }
6505

6506
    for (int32_t j = 0; j < num; ++j) {
220✔
6507
      int32_t x = tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "%c", '|');
80✔
6508
      len += x;
80✔
6509
    }
6510

6511
    if (pData->blockRowsHisto[i] > 0) {
140✔
6512
      double v = pData->blockRowsHisto[i] * 100.0 / pData->numOfBlocks;
1✔
6513
      len += tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "  %d (%.2f%c)",
1✔
6514
                       pData->blockRowsHisto[i], v, '%');
6515
    }
6516

6517
    varDataSetLen(st, len);
140✔
6518
    code = colDataSetVal(pColInfo, row++, st, false);
140✔
6519
    if (TSDB_CODE_SUCCESS != code) {
140!
6520
      return code;
×
6521
    }
6522
  }
6523

6524
  return TSDB_CODE_SUCCESS;
7✔
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) {
708✔
6636
  pEnv->calcMemSize = sizeof(SDerivInfo);
708✔
6637
  return true;
708✔
6638
}
6639

6640
int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
1,525✔
6641
  if (pResInfo->initialized) {
1,525✔
6642
    return TSDB_CODE_SUCCESS;
702✔
6643
  }
6644
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
823!
6645
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6646
  }
6647

6648
  SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
823✔
6649

6650
  pDerivInfo->ignoreNegative = pCtx->param[2].param.i;
823✔
6651
  pDerivInfo->prevTs = -1;
823✔
6652
  pDerivInfo->tsWindow = pCtx->param[1].param.i;
823✔
6653
  pDerivInfo->valueSet = false;
823✔
6654
  return TSDB_CODE_SUCCESS;
823✔
6655
}
6656

6657
int32_t derivativeFunction(SqlFunctionCtx* pCtx) {
817✔
6658
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
817✔
6659
  SDerivInfo*          pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
817✔
6660

6661
  SInputColumnInfoData* pInput = &pCtx->input;
817✔
6662
  SColumnInfoData*      pInputCol = pInput->pData[0];
817✔
6663

6664
  int32_t          numOfElems = 0;
817✔
6665
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
817✔
6666
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
817✔
6667
  int32_t          code = TSDB_CODE_SUCCESS;
817✔
6668

6669
  funcInputUpdate(pCtx);
817✔
6670

6671
  double v = 0;
817✔
6672
  if (pCtx->order == TSDB_ORDER_ASC) {
817✔
6673
    SFuncInputRow row = {0};
771✔
6674
    bool          result = false;
771✔
6675
    while (1) {
79,172✔
6676
      code = funcInputGetNextRow(pCtx, &row, &result);
79,943✔
6677
      if (TSDB_CODE_SUCCESS != code) {
79,943!
6678
        return code;
×
6679
      }
6680
      if (!result) {
79,943✔
6681
        break;
771✔
6682
      }
6683
      if (row.isDataNull) {
79,172✔
6684
        continue;
32,984✔
6685
      }
6686

6687
      char* d = row.pData;
46,188✔
6688
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
46,188!
6689

6690
      int32_t pos = pCtx->offset + numOfElems;
46,188✔
6691
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
46,188✔
6692
        pDerivInfo->valueSet = true;
432✔
6693
      } else {
6694
        if (row.ts == pDerivInfo->prevTs) {
45,756!
6695
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6696
        }
6697
        double r = ((v - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (row.ts - pDerivInfo->prevTs);
45,756✔
6698
        if (pDerivInfo->ignoreNegative && r < 0) {
45,756✔
6699
        } else {
6700
          if (isinf(r) || isnan(r)) {
38,471!
6701
            colDataSetNULL(pOutput, pos);
×
6702
          } else {
6703
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
38,471✔
6704
            if (code != TSDB_CODE_SUCCESS) {
38,471!
6705
              return code;
×
6706
            }
6707
          }
6708

6709
          if (pTsOutput != NULL) {
38,471!
6710
            colDataSetInt64(pTsOutput, pos, &row.ts);
×
6711
          }
6712

6713
          // handle selectivity
6714
          if (pCtx->subsidiaries.num > 0) {
38,471✔
6715
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
62✔
6716
            if (code != TSDB_CODE_SUCCESS) {
62!
6717
              return code;
×
6718
            }
6719
          }
6720

6721
          numOfElems++;
38,471✔
6722
        }
6723
      }
6724

6725
      pDerivInfo->prevValue = v;
46,188✔
6726
      pDerivInfo->prevTs = row.ts;
46,188✔
6727
    }
6728
  } else {
6729
    SFuncInputRow row = {0};
46✔
6730
    bool          result = false;
46✔
6731
    while (1) {
988✔
6732
      code = funcInputGetNextRow(pCtx, &row, &result);
1,034✔
6733
      if (TSDB_CODE_SUCCESS != code) {
1,034!
6734
        return code;
×
6735
      }
6736
      if (!result) {
1,034✔
6737
        break;
46✔
6738
      }
6739
      if (row.isDataNull) {
988✔
6740
        continue;
70✔
6741
      }
6742

6743
      char* d = row.pData;
918✔
6744
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
918!
6745

6746
      int32_t pos = pCtx->offset + numOfElems;
918✔
6747
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
918✔
6748
        pDerivInfo->valueSet = true;
38✔
6749
      } else {
6750
        if (row.ts == pDerivInfo->prevTs) {
880!
6751
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6752
        }
6753
        double r = ((pDerivInfo->prevValue - v) * pDerivInfo->tsWindow) / (pDerivInfo->prevTs - row.ts);
880✔
6754
        if (pDerivInfo->ignoreNegative && r < 0) {
880✔
6755
        } else {
6756
          if (isinf(r) || isnan(r)) {
647!
6757
            colDataSetNULL(pOutput, pos);
×
6758
          } else {
6759
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
647✔
6760
            if (code != TSDB_CODE_SUCCESS) {
647!
6761
              return code;
×
6762
            }
6763
          }
6764

6765
          if (pTsOutput != NULL) {
647!
6766
            colDataSetInt64(pTsOutput, pos, &pDerivInfo->prevTs);
×
6767
          }
6768

6769
          // handle selectivity
6770
          if (pCtx->subsidiaries.num > 0) {
647!
6771
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
×
6772
            if (code != TSDB_CODE_SUCCESS) {
×
6773
              return code;
×
6774
            }
6775
          }
6776
          numOfElems++;
647✔
6777
        }
6778
      }
6779

6780
      pDerivInfo->prevValue = v;
918✔
6781
      pDerivInfo->prevTs = row.ts;
918✔
6782
    }
6783
  }
6784

6785
  pResInfo->numOfRes = numOfElems;
817✔
6786

6787
  return TSDB_CODE_SUCCESS;
817✔
6788
}
6789

6790
int32_t getIrateInfoSize(int32_t pkBytes) { return (int32_t)sizeof(SRateInfo) + 2 * pkBytes; }
2,483✔
6791

6792
bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
2,226✔
6793
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
2,226✔
6794
  pEnv->calcMemSize = getIrateInfoSize(pkBytes);
2,226✔
6795
  return true;
2,231✔
6796
}
6797

6798
int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
30,160✔
6799
  if (pResInfo->initialized) {
30,160!
6800
    return TSDB_CODE_SUCCESS;
×
6801
  }
6802
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
30,160!
6803
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6804
  }
6805

6806
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,160✔
6807

6808
  pInfo->firstKey = INT64_MIN;
30,160✔
6809
  pInfo->lastKey = INT64_MIN;
30,160✔
6810
  pInfo->firstValue = (double)INT64_MIN;
30,160✔
6811
  pInfo->lastValue = (double)INT64_MIN;
30,160✔
6812

6813
  pInfo->hasResult = 0;
30,160✔
6814
  return TSDB_CODE_SUCCESS;
30,160✔
6815
}
6816

6817
static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) {
270,743✔
6818
  if (isFirst) {
270,743✔
6819
    pRateInfo->firstValue = v;
127,107✔
6820
    pRateInfo->firstKey = ts;
127,107✔
6821
    if (pRateInfo->firstPk) {
127,107✔
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;
143,636✔
6836
    pRateInfo->lastKey = ts;
143,636✔
6837
    if (pRateInfo->lastPk) {
143,636✔
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
}
270,743✔
6852

6853
static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool isMerge) {
31,080✔
6854
  if (pCtx->hasPrimaryKey) {
31,080✔
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;
31,061✔
6866
    pRateInfo->lastPk = NULL;
31,061✔
6867
  }
6868
}
31,080✔
6869

6870
int32_t irateFunction(SqlFunctionCtx* pCtx) {
30,704✔
6871
  int32_t              code = TSDB_CODE_SUCCESS;
30,704✔
6872
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
30,704✔
6873
  SRateInfo*           pRateInfo = GET_ROWCELL_INTERBUF(pResInfo);
30,704✔
6874

6875
  SInputColumnInfoData* pInput = &pCtx->input;
30,704✔
6876
  SColumnInfoData*      pInputCol = pInput->pData[0];
30,704✔
6877

6878
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
30,704✔
6879

6880
  funcInputUpdate(pCtx);
30,704✔
6881

6882
  initializeRateInfo(pCtx, pRateInfo, false);
30,704✔
6883

6884
  int32_t       numOfElems = 0;
30,705✔
6885
  int32_t       type = pInputCol->info.type;
30,705✔
6886
  SFuncInputRow row = {0};
30,705✔
6887
  bool          result = false;
30,705✔
6888
  while (1) {
266,584✔
6889
    code = funcInputGetNextRow(pCtx, &row, &result);
297,289✔
6890
    if (TSDB_CODE_SUCCESS != code) {
297,289!
6891
      return code;
×
6892
    }
6893
    if (!result) {
297,289✔
6894
      break;
30,706✔
6895
    }
6896
    if (row.isDataNull) {
266,583✔
6897
      continue;
117,150✔
6898
    }
6899

6900
    char*  data = row.pData;
149,433✔
6901
    double v = 0;
149,433✔
6902
    GET_TYPED_DATA(v, double, type, data);
149,433!
6903

6904
    if (INT64_MIN == pRateInfo->lastKey) {
149,433✔
6905
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
16,586✔
6906
      pRateInfo->hasResult = 1;
16,586✔
6907
      continue;
16,586✔
6908
    }
6909

6910
    if (row.ts > pRateInfo->lastKey) {
132,847✔
6911
      if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
126,891!
6912
        doSaveRateInfo(pRateInfo, true, pRateInfo->lastKey, pRateInfo->lastPk, pRateInfo->lastValue);
126,891✔
6913
      }
6914
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
126,892✔
6915
      continue;
126,890✔
6916
    } else if (row.ts == pRateInfo->lastKey) {
5,956!
6917
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6918
    }
6919

6920
    if ((INT64_MIN == pRateInfo->firstKey) || row.ts > pRateInfo->firstKey) {
5,956!
6921
      doSaveRateInfo(pRateInfo, true, row.ts, row.pPk, v);
36✔
6922
    } else if (row.ts == pRateInfo->firstKey) {
5,920!
6923
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6924
    }
6925
  }
6926

6927
  numOfElems++;
30,706✔
6928

6929
  SET_VAL(pResInfo, numOfElems, 1);
30,706!
6930
  return TSDB_CODE_SUCCESS;
30,706✔
6931
}
6932

6933
static double doCalcRate(const SRateInfo* pRateInfo, double tickPerSec) {
29,972✔
6934
  if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) ||
29,972✔
6935
      (pRateInfo->firstKey >= pRateInfo->lastKey)) {
1,281!
6936
    return 0.0;
28,691✔
6937
  }
6938

6939
  double diff = 0;
1,281✔
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;
1,281✔
6943
  if (diff >= pRateInfo->firstValue) {
1,281✔
6944
    diff -= pRateInfo->firstValue;
518✔
6945
  }
6946

6947
  int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey;
1,281✔
6948
  if (duration == 0) {
1,281!
6949
    return 0;
×
6950
  }
6951

6952
  return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0;
1,281!
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);
74✔
6958
    if (isFirstKey) {
74✔
6959
      doSaveRateInfo(pOutput, false, pInput->firstKey, pInput->firstPk, pInput->firstValue);
30✔
6960
    } else {
6961
      doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
44✔
6962
    }
6963
  } else if ((inputKey < pOutput->lastKey) && (inputKey > pOutput->firstKey)) {
100!
6964
    if (isFirstKey) {
8✔
6965
      doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
4✔
6966
    } else {
6967
      doSaveRateInfo(pOutput, true, pInput->lastKey, pInput->lastPk, pInput->lastValue);
4✔
6968
    }
6969
  } else {
6970
    // inputKey < pOutput->firstKey
6971
  }
6972
}
174✔
6973

6974
static void irateCopyInfo(SRateInfo* pInput, SRateInfo* pOutput) {
93✔
6975
  doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
93✔
6976
  doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
93✔
6977
}
93✔
6978

6979
static int32_t irateTransferInfo(SRateInfo* pInput, SRateInfo* pOutput) {
180✔
6980
  if ((pInput->firstKey != INT64_MIN &&
180✔
6981
       (pInput->firstKey == pOutput->firstKey || pInput->firstKey == pOutput->lastKey)) ||
175!
6982
      (pInput->lastKey != INT64_MIN && (pInput->lastKey == pOutput->firstKey || pInput->lastKey == pOutput->lastKey))) {
180!
6983
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6984
  }
6985

6986
  if (pOutput->hasResult == 0) {
180✔
6987
    irateCopyInfo(pInput, pOutput);
93✔
6988
    pOutput->hasResult = pInput->hasResult;
93✔
6989
    return TSDB_CODE_SUCCESS;
93✔
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) {
188✔
7005
  SInputColumnInfoData* pInput = &pCtx->input;
188✔
7006
  SColumnInfoData*      pCol = pInput->pData[0];
188✔
7007
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
188!
7008
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
7009
  }
7010

7011
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
188✔
7012
  initializeRateInfo(pCtx, pInfo, true);
188✔
7013

7014
  int32_t start = pInput->startRowIndex;
188✔
7015
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
376✔
7016
    char*      data = colDataGetData(pCol, i);
188!
7017
    SRateInfo* pInputInfo = (SRateInfo*)varDataVal(data);
188✔
7018
    initializeRateInfo(pCtx, pInfo, true);
188✔
7019
    if (pInputInfo->hasResult) {
188✔
7020
      int32_t code = irateTransferInfo(pInputInfo, pInfo);
180✔
7021
      if (code != TSDB_CODE_SUCCESS) {
180!
7022
        return code;
×
7023
      }
7024
    }
7025
  }
7026

7027
  if (pInfo->hasResult) {
188✔
7028
    GET_RES_INFO(pCtx)->numOfRes = 1;
180✔
7029
  }
7030

7031
  return TSDB_CODE_SUCCESS;
188✔
7032
}
7033

7034
int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
188✔
7035
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
188✔
7036
  SRateInfo*           pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
188✔
7037
  int32_t              resultBytes = getIrateInfoSize(pInfo->pkBytes);
188✔
7038
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
188!
7039

7040
  if (NULL == res) {
188!
7041
    return terrno;
×
7042
  }
7043
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
188✔
7044
  varDataSetLen(res, resultBytes);
188✔
7045

7046
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
188✔
7047
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
188✔
7048
  if (NULL == pCol) {
188!
7049
    taosMemoryFree(res);
×
7050
    return TSDB_CODE_OUT_OF_RANGE;
×
7051
  }
7052

7053
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
188✔
7054

7055
  taosMemoryFree(res);
188!
7056
  return code;
188✔
7057
}
7058

7059
int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
29,972✔
7060
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
29,972✔
7061
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
29,972✔
7062
  if (NULL == pCol) {
29,972!
7063
    return TSDB_CODE_OUT_OF_RANGE;
×
7064
  }
7065

7066
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
29,972✔
7067
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
29,972✔
7068

7069
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
29,972✔
7070
  double     result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i));
29,972!
7071
  int32_t    code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes);
29,972✔
7072

7073
  return code;
29,972✔
7074
}
7075

7076
int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) {
104,800,067✔
7077
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
104,800,067✔
7078
  SGroupKeyInfo*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
104,800,067✔
7079

7080
  SInputColumnInfoData* pInput = &pCtx->input;
104,800,067✔
7081
  SColumnInfoData*      pInputCol = pInput->pData[0];
104,800,067✔
7082

7083
  int32_t startIndex = pInput->startRowIndex;
104,800,067✔
7084

7085
  // escape rest of data blocks to avoid first entry to be overwritten.
7086
  if (pInfo->hasResult) {
104,800,067✔
7087
    goto _group_value_over;
9,510,754✔
7088
  }
7089

7090
  if (pInputCol->pData == NULL || colDataIsNull_s(pInputCol, startIndex)) {
190,159,498✔
7091
    pInfo->isNull = true;
2,687,612✔
7092
    pInfo->hasResult = true;
2,687,612✔
7093
    goto _group_value_over;
2,687,612✔
7094
  }
7095

7096
  char* data = colDataGetData(pInputCol, startIndex);
92,601,701!
7097
  if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
92,601,701!
7098
    (void)memcpy(pInfo->data, data,
72,947,425✔
7099
                 (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data));
72,947,425✔
7100
  } else {
7101
    (void)memcpy(pInfo->data, data, pInputCol->info.bytes);
19,654,276✔
7102
  }
7103
  pInfo->hasResult = true;
92,601,701✔
7104

7105
_group_value_over:
104,800,067✔
7106

7107
  SET_VAL(pResInfo, 1, 1);
104,800,067✔
7108
  return TSDB_CODE_SUCCESS;
104,800,067✔
7109
}
7110

7111
int32_t groupKeyFunction(SqlFunctionCtx* pCtx) { return groupConstValueFunction(pCtx); }
104,788,995✔
7112

7113
int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
93,670,500✔
7114
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
93,670,500✔
7115
  int32_t          code = TSDB_CODE_SUCCESS;
93,670,500✔
7116
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
93,670,500✔
7117
  if (NULL == pCol) {
93,580,303!
7118
    return TSDB_CODE_OUT_OF_RANGE;
×
7119
  }
7120

7121
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
93,580,303✔
7122

7123
  SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
93,580,303✔
7124

7125
  if (pInfo->hasResult) {
93,580,303!
7126
    int32_t currentRow = pBlock->info.rows;
93,610,551✔
7127
    for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) {
187,899,853✔
7128
      code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false);
93,620,034✔
7129
      if (TSDB_CODE_SUCCESS != code) {
94,289,302!
7130
        return code;
×
7131
      }
7132
    }
7133
  } else {
7134
    pResInfo->numOfRes = 0;
×
7135
  }
7136

7137
  return code;
94,249,571✔
7138
}
7139

7140
int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return groupConstValueFinalize(pCtx, pBlock); }
93,705,856✔
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) {
2,395✔
7177
  int32_t numOfElems = 0;
2,395✔
7178

7179
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,395✔
7180
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
2,395✔
7181

7182
  SInputColumnInfoData* pInput = &pCtx->input;
2,395✔
7183
  SColumnInfoData*      pInputCol = pInput->pData[0];
2,395✔
7184

7185
  int32_t bytes = pInputCol->info.bytes;
2,395✔
7186
  pInfo->bytes = bytes;
2,395✔
7187

7188
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
2,395✔
7189
  pInfo->pkType = -1;
2,395✔
7190
  __compar_fn_t pkCompareFn = NULL;
2,395✔
7191
  if (pCtx->hasPrimaryKey) {
2,395✔
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) {
4,801✔
7200
    numOfElems++;
2,405✔
7201

7202
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
2,405✔
7203
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
2,405!
7204

7205
    TSKEY cts = getRowPTs(pInput->pPTS, i);
2,405!
7206
    if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2,405✔
7207
      int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
2,005✔
7208
      if (code != TSDB_CODE_SUCCESS) {
2,006!
7209
        return code;
×
7210
      }
7211
      pResInfo->numOfRes = 1;
2,006✔
7212
    }
7213
  }
7214

7215
  SET_VAL(pResInfo, numOfElems, 1);
2,396!
7216
  return TSDB_CODE_SUCCESS;
2,396✔
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