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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

0.34
/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

UNCOV
31
bool ignoreNegative(int8_t ignoreOption) { return (ignoreOption & 0x1) == 0x1; }
×
UNCOV
32
bool ignoreNull(int8_t ignoreOption) { return (ignoreOption & 0x2) == 0x2; }
×
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

UNCOV
193
void funcInputUpdate(SqlFunctionCtx* pCtx) {
×
UNCOV
194
  SFuncInputRowIter* pIter = &pCtx->rowIter;
×
195

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

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

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

UNCOV
346
bool funcInputGetNextRowAscPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) {
×
UNCOV
347
  if (pIter->hasPrev) {
×
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 {
UNCOV
363
    if (pIter->rowIndex <= pIter->inputEndIndex) {
×
UNCOV
364
      setInputRowInfo(pRow, pIter, pIter->rowIndex, true);
×
365

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

UNCOV
382
bool funcInputGetNextRowNoPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) {
×
UNCOV
383
  if (pIter->rowIndex <= pIter->inputEndIndex) {
×
UNCOV
384
    setInputRowInfo(pRow, pIter, pIter->rowIndex, false);
×
UNCOV
385
    ++pIter->rowIndex;
×
UNCOV
386
    return true;
×
387
  } else {
UNCOV
388
    return false;
×
389
  }
390
}
391

UNCOV
392
int32_t funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, bool* res) {
×
UNCOV
393
  SFuncInputRowIter* pIter = &pCtx->rowIter;
×
UNCOV
394
  if (pCtx->hasPrimaryKey) {
×
UNCOV
395
    if (pCtx->order == TSDB_ORDER_ASC) {
×
UNCOV
396
      *res = funcInputGetNextRowAscPk(pIter, pRow);
×
UNCOV
397
      return TSDB_CODE_SUCCESS;
×
398
    } else {
399
      return funcInputGetNextRowDescPk(pIter, pRow, res);
×
400
    }
401
  } else {
UNCOV
402
    *res = funcInputGetNextRowNoPk(pIter, pRow);
×
UNCOV
403
    return TSDB_CODE_SUCCESS;
×
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
UNCOV
410
int32_t appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t rowIndex, int32_t pos) {
×
UNCOV
411
  if (pCtx->subsidiaries.num <= 0) {
×
412
    return TSDB_CODE_SUCCESS;
×
413
  }
414

UNCOV
415
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
×
UNCOV
416
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
417

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

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

UNCOV
427
    char* pData = colDataGetData(pSrcCol, rowIndex);
×
428

429
    // append to dest col
UNCOV
430
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
×
431

UNCOV
432
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
×
UNCOV
433
    if (NULL == pDstCol) {
×
434
      return TSDB_CODE_OUT_OF_RANGE;
×
435
    }
UNCOV
436
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
×
UNCOV
437
      colDataSetNULL(pDstCol, pos);
×
438
    } else {
UNCOV
439
      int32_t code = colDataSetVal(pDstCol, pos, pData, false);
×
UNCOV
440
      if (TSDB_CODE_SUCCESS != code) {
×
441
        return code;
×
442
      }
443
    }
444
  }
UNCOV
445
  return TSDB_CODE_SUCCESS;
×
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

UNCOV
453
int32_t functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
454
  if (pResultInfo->initialized) {
×
UNCOV
455
    return TSDB_CODE_SUCCESS;  // already initialized
×
456
  }
457

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

UNCOV
462
  initResultRowEntry(pResultInfo, pCtx->resDataInfo.interBufSize);
×
UNCOV
463
  return TSDB_CODE_SUCCESS;
×
464
}
465

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

UNCOV
476
  char* in = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
477
  code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes);
×
478

UNCOV
479
  return code;
×
480
}
481

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

UNCOV
487
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
488
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
489

UNCOV
490
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, true);
×
491

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

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

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

UNCOV
509
  return code;
×
510
}
511

512
EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
32✔
513
  SNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
32✔
514
  if (QUERY_NODE_COLUMN == nodeType(pParam) && PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pParam)->colId) {
32!
515
    return FUNC_DATA_REQUIRED_NOT_LOAD;
30✔
516
  }
517
  return FUNC_DATA_REQUIRED_SMA_LOAD;
2✔
518
}
519

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

UNCOV
525
static int64_t getNumOfElems(SqlFunctionCtx* pCtx) {
×
UNCOV
526
  int64_t numOfElem = 0;
×
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
   */
UNCOV
533
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
534
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
535
  if (1 == pInput->numOfRows && pInput->blankFill) {
×
UNCOV
536
    return 0;
×
537
  }
UNCOV
538
  if (pInput->colDataSMAIsSet && pInput->totalRows == pInput->numOfRows) {
×
UNCOV
539
    numOfElem = pInput->numOfRows - pInput->pColumnDataAgg[0]->numOfNull;
×
540
  } else {
UNCOV
541
    if (pInputCol->hasNull) {
×
UNCOV
542
      for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
×
UNCOV
543
        if (colDataIsNull(pInputCol, pInput->totalRows, i, NULL)) {
×
UNCOV
544
          continue;
×
545
        }
UNCOV
546
        numOfElem += 1;
×
547
      }
548
    } else {
549
      // when counting on the primary time stamp column and no statistics data is presented, use the size value
550
      // directly.
UNCOV
551
      numOfElem = pInput->numOfRows;
×
552
    }
553
  }
UNCOV
554
  return numOfElem;
×
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
 */
UNCOV
561
int32_t countFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
562
  int64_t numOfElem = 0;
×
563

UNCOV
564
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
565
  SInputColumnInfoData* pInput = &pCtx->input;
×
566

UNCOV
567
  int32_t type = pInput->pData[0]->info.type;
×
568

UNCOV
569
  char*   buf = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
570
  int64_t val = *((int64_t*)buf);
×
UNCOV
571
  if (IS_NULL_TYPE(type)) {
×
572
    // select count(NULL) returns 0
UNCOV
573
    numOfElem = 1;
×
UNCOV
574
    val += 0;
×
575
  } else {
UNCOV
576
    numOfElem = getNumOfElems(pCtx);
×
UNCOV
577
    val += numOfElem;
×
578
  }
579
  taosSetInt64Aligned((int64_t*)buf, val);
580

UNCOV
581
  if (tsCountAlwaysReturnValue) {
×
582
    pResInfo->numOfRes = 1;
×
583
  } else {
UNCOV
584
    SET_VAL(pResInfo, val, 1);
×
585
  }
586

UNCOV
587
  return TSDB_CODE_SUCCESS;
×
588
}
589

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

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

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

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

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

UNCOV
611
  SET_VAL(pDResInfo, *((int64_t*)pDBuf), 1);
×
UNCOV
612
  return TSDB_CODE_SUCCESS;
×
613
}
614

UNCOV
615
int32_t sumFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
616
  int32_t numOfElem = 0;
×
617

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

UNCOV
623
  SSumRes* pSumRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
624
  pSumRes->type = type;
×
625

UNCOV
626
  if (IS_NULL_TYPE(type)) {
×
UNCOV
627
    numOfElem = 0;
×
UNCOV
628
    goto _sum_over;
×
629
  }
630

UNCOV
631
  if (pInput->colDataSMAIsSet) {
×
UNCOV
632
    numOfElem = pInput->numOfRows - pAgg->numOfNull;
×
633

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

UNCOV
644
    int32_t start = pInput->startRowIndex;
×
UNCOV
645
    int32_t numOfRows = pInput->numOfRows;
×
646

UNCOV
647
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
×
UNCOV
648
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
×
UNCOV
649
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int8_t, numOfElem);
×
UNCOV
650
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
×
UNCOV
651
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int16_t, numOfElem);
×
UNCOV
652
      } else if (type == TSDB_DATA_TYPE_INT) {
×
UNCOV
653
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int32_t, numOfElem);
×
UNCOV
654
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
×
UNCOV
655
        LIST_ADD_N(pSumRes->isum, pCol, start, numOfRows, int64_t, numOfElem);
×
656
      }
UNCOV
657
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
658
      if (type == TSDB_DATA_TYPE_UTINYINT) {
×
UNCOV
659
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint8_t, numOfElem);
×
UNCOV
660
      } else if (type == TSDB_DATA_TYPE_USMALLINT) {
×
UNCOV
661
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint16_t, numOfElem);
×
UNCOV
662
      } else if (type == TSDB_DATA_TYPE_UINT) {
×
UNCOV
663
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint32_t, numOfElem);
×
UNCOV
664
      } else if (type == TSDB_DATA_TYPE_UBIGINT) {
×
UNCOV
665
        LIST_ADD_N(pSumRes->usum, pCol, start, numOfRows, uint64_t, numOfElem);
×
666
      }
UNCOV
667
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
UNCOV
668
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, double, numOfElem);
×
UNCOV
669
    } else if (type == TSDB_DATA_TYPE_FLOAT) {
×
UNCOV
670
      LIST_ADD_N(pSumRes->dsum, pCol, start, numOfRows, float, numOfElem);
×
671
    }
672
  }
673

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

801
EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
22✔
802
  if (funcNotSupportStringSma(pFunc)) {
22!
UNCOV
803
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
804
  }
805
  return FUNC_DATA_REQUIRED_SMA_LOAD;
22✔
806
}
807

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

UNCOV
816
  SMinmaxResInfo* buf = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
817
  buf->assign = false;
×
UNCOV
818
  buf->tuplePos.pageId = -1;
×
819

UNCOV
820
  buf->nullTupleSaved = false;
×
UNCOV
821
  buf->nullTuplePos.pageId = -1;
×
UNCOV
822
  buf->str = NULL;
×
UNCOV
823
  return TSDB_CODE_SUCCESS;
×
824
}
825

UNCOV
826
bool getMinmaxFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
827
  pEnv->calcMemSize = sizeof(SMinmaxResInfo);
×
UNCOV
828
  return true;
×
829
}
830

UNCOV
831
int32_t minFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
832
  int32_t numOfElems = 0;
×
833
  int32_t code = doMinMaxHelper(pCtx, 1, &numOfElems);
×
UNCOV
834
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
835
    return code;
×
836
  }
UNCOV
837
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
838
  return TSDB_CODE_SUCCESS;
×
839
}
840

UNCOV
841
int32_t maxFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
842
  int32_t numOfElems = 0;
×
843
  int32_t code = doMinMaxHelper(pCtx, 0, &numOfElems);
×
UNCOV
844
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
845
    return code;
×
846
  }
UNCOV
847
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
848
  return TSDB_CODE_SUCCESS;
×
849
}
850

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

UNCOV
855
int32_t minmaxFunctionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
856
  int32_t code = TSDB_CODE_SUCCESS;
×
857

UNCOV
858
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
859
  SMinmaxResInfo*      pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
×
860

UNCOV
861
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
862
  int32_t currentRow = pBlock->info.rows;
×
863

864
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
865
  if (NULL == pCol) {
×
UNCOV
866
    return TSDB_CODE_OUT_OF_RANGE;
×
867
  }
UNCOV
868
  pEntryInfo->isNullRes = (pEntryInfo->numOfRes == 0) ? 1 : 0;
×
869

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

UNCOV
912
  taosMemoryFreeClear(pRes->str);
×
UNCOV
913
  if (pCtx->subsidiaries.num > 0) {
×
UNCOV
914
    if (pEntryInfo->numOfRes > 0) {
×
UNCOV
915
      code = setSelectivityValue(pCtx, pBlock, &pRes->tuplePos, currentRow);
×
916
    } else {
UNCOV
917
      code = setNullSelectivityValue(pCtx, pBlock, currentRow);
×
918
    }
919
  }
920

UNCOV
921
  return code;
×
922
}
923

UNCOV
924
int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex) {
×
UNCOV
925
  if (pCtx->subsidiaries.num <= 0) {
×
UNCOV
926
    return TSDB_CODE_SUCCESS;
×
927
  }
928

UNCOV
929
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
×
UNCOV
930
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
UNCOV
931
    int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
×
932

933
    SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
×
UNCOV
934
    if (NULL == pDstCol) {
×
UNCOV
935
      return terrno;
×
936
    }
UNCOV
937
    colDataSetNULL(pDstCol, rowIndex);
×
938
  }
939

UNCOV
940
  return TSDB_CODE_SUCCESS;
×
941
}
942

UNCOV
943
int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STuplePos* pTuplePos, int32_t rowIndex) {
×
UNCOV
944
  if (pCtx->subsidiaries.num <= 0) {
×
UNCOV
945
    return TSDB_CODE_SUCCESS;
×
946
  }
947

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

UNCOV
959
    bool* nullList = (bool*)p;
×
UNCOV
960
    char* pStart = (char*)(nullList + numOfCols * sizeof(bool));
×
961

962
    // todo set the offset value to optimize the performance.
UNCOV
963
    for (int32_t j = 0; j < numOfCols; ++j) {
×
UNCOV
964
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
UNCOV
965
      int32_t         dstSlotId = pc->pExpr->base.resSchema.slotId;
×
966

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

UNCOV
983
  return TSDB_CODE_SUCCESS;
×
984
}
985

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

UNCOV
993
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
994
  for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
×
UNCOV
995
    SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
996

997
    // get data from source col
UNCOV
998
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
×
UNCOV
999
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
×
1000

1001
    SColumnInfoData* pSrcCol = taosArrayGet(pCtx->pSrcBlock->pDataBlock, srcSlotId);
×
UNCOV
1002
    if (NULL == pSrcCol) {
×
UNCOV
1003
      return TSDB_CODE_OUT_OF_RANGE;
×
1004
    }
1005

UNCOV
1006
    char* pData = colDataGetData(pSrcCol, rowIndex);
×
1007

1008
    // append to dest col
UNCOV
1009
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
×
1010

1011
    SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId);
×
UNCOV
1012
    if (NULL == pDstCol) {
×
UNCOV
1013
      return TSDB_CODE_OUT_OF_RANGE;
×
1014
    }
1015

UNCOV
1016
    if (colDataIsNull_s(pSrcCol, rowIndex) == true) {
×
UNCOV
1017
      colDataSetNULL(pDstCol, pos);
×
1018
    } else {
1019
      code = colDataSetVal(pDstCol, pos, pData, false);
×
UNCOV
1020
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1021
        return code;
×
1022
      }
1023
    }
1024
  }
UNCOV
1025
  return code;
×
1026
}
1027

UNCOV
1028
void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; }
×
1029

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

UNCOV
1035
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
1036
  SMinmaxResInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
1037
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
×
1038

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

UNCOV
1095
int32_t minCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1096
  return minMaxCombine(pDestCtx, pSourceCtx, 1);
×
1097
}
UNCOV
1098
int32_t maxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1099
  return minMaxCombine(pDestCtx, pSourceCtx, 0);
×
1100
}
1101

UNCOV
1102
int32_t getStdInfoSize() { return (int32_t)sizeof(SStdRes); }
×
1103

UNCOV
1104
bool getStdFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
1105
  pEnv->calcMemSize = sizeof(SStdRes);
×
UNCOV
1106
  return true;
×
1107
}
1108

1109
int32_t stdFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
1110
  if (pResultInfo->initialized) {
×
UNCOV
1111
    return TSDB_CODE_SUCCESS;
×
1112
  }
UNCOV
1113
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
1114
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1115
  }
1116

UNCOV
1117
  SStdRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
1118
  (void)memset(pRes, 0, sizeof(SStdRes));
×
UNCOV
1119
  return TSDB_CODE_SUCCESS;
×
1120
}
1121

UNCOV
1122
int32_t stdFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1123
  int32_t numOfElem = 0;
×
1124

1125
  // Only the pre-computing information loaded and actual data does not loaded
UNCOV
1126
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1127
  int32_t               type = pInput->pData[0]->info.type;
×
1128

UNCOV
1129
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
1130
  pStdRes->type = type;
×
1131

1132
  // computing based on the true data block
UNCOV
1133
  SColumnInfoData* pCol = pInput->pData[0];
×
1134

UNCOV
1135
  int32_t start = pInput->startRowIndex;
×
UNCOV
1136
  int32_t numOfRows = pInput->numOfRows;
×
1137

UNCOV
1138
  if (IS_NULL_TYPE(type)) {
×
UNCOV
1139
    numOfElem = 0;
×
UNCOV
1140
    goto _stddev_over;
×
1141
  }
1142

UNCOV
1143
  switch (type) {
×
UNCOV
1144
    case TSDB_DATA_TYPE_TINYINT: {
×
UNCOV
1145
      int8_t* plist = (int8_t*)pCol->pData;
×
UNCOV
1146
      for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
1147
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1148
          continue;
×
1149
        }
1150

UNCOV
1151
        numOfElem += 1;
×
UNCOV
1152
        pStdRes->count += 1;
×
UNCOV
1153
        pStdRes->isum += plist[i];
×
UNCOV
1154
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1155
      }
1156

UNCOV
1157
      break;
×
1158
    }
1159

UNCOV
1160
    case TSDB_DATA_TYPE_SMALLINT: {
×
UNCOV
1161
      int16_t* plist = (int16_t*)pCol->pData;
×
UNCOV
1162
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1163
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1164
          continue;
×
1165
        }
1166

UNCOV
1167
        numOfElem += 1;
×
UNCOV
1168
        pStdRes->count += 1;
×
UNCOV
1169
        pStdRes->isum += plist[i];
×
UNCOV
1170
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1171
      }
UNCOV
1172
      break;
×
1173
    }
1174

UNCOV
1175
    case TSDB_DATA_TYPE_INT: {
×
UNCOV
1176
      int32_t* plist = (int32_t*)pCol->pData;
×
UNCOV
1177
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1178
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1179
          continue;
×
1180
        }
1181

UNCOV
1182
        numOfElem += 1;
×
UNCOV
1183
        pStdRes->count += 1;
×
UNCOV
1184
        pStdRes->isum += plist[i];
×
UNCOV
1185
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1186
      }
1187

UNCOV
1188
      break;
×
1189
    }
1190

UNCOV
1191
    case TSDB_DATA_TYPE_BIGINT: {
×
UNCOV
1192
      int64_t* plist = (int64_t*)pCol->pData;
×
UNCOV
1193
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1194
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1195
          continue;
×
1196
        }
1197

UNCOV
1198
        numOfElem += 1;
×
UNCOV
1199
        pStdRes->count += 1;
×
UNCOV
1200
        pStdRes->isum += plist[i];
×
UNCOV
1201
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1202
      }
UNCOV
1203
      break;
×
1204
    }
1205

UNCOV
1206
    case TSDB_DATA_TYPE_UTINYINT: {
×
UNCOV
1207
      uint8_t* plist = (uint8_t*)pCol->pData;
×
UNCOV
1208
      for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
1209
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1210
          continue;
×
1211
        }
1212

UNCOV
1213
        numOfElem += 1;
×
UNCOV
1214
        pStdRes->count += 1;
×
UNCOV
1215
        pStdRes->usum += plist[i];
×
UNCOV
1216
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1217
      }
1218

UNCOV
1219
      break;
×
1220
    }
1221

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

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

UNCOV
1237
    case TSDB_DATA_TYPE_UINT: {
×
UNCOV
1238
      uint32_t* plist = (uint32_t*)pCol->pData;
×
1239
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1240
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1241
          continue;
×
1242
        }
1243

UNCOV
1244
        numOfElem += 1;
×
UNCOV
1245
        pStdRes->count += 1;
×
UNCOV
1246
        pStdRes->usum += plist[i];
×
UNCOV
1247
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1248
      }
1249

UNCOV
1250
      break;
×
1251
    }
1252

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

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

UNCOV
1268
    case TSDB_DATA_TYPE_FLOAT: {
×
UNCOV
1269
      float* plist = (float*)pCol->pData;
×
UNCOV
1270
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1271
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1272
          continue;
×
1273
        }
1274

UNCOV
1275
        numOfElem += 1;
×
UNCOV
1276
        pStdRes->count += 1;
×
UNCOV
1277
        pStdRes->dsum += plist[i];
×
UNCOV
1278
        pStdRes->quadraticDSum += plist[i] * plist[i];
×
1279
      }
UNCOV
1280
      break;
×
1281
    }
1282

UNCOV
1283
    case TSDB_DATA_TYPE_DOUBLE: {
×
UNCOV
1284
      double* plist = (double*)pCol->pData;
×
UNCOV
1285
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1286
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1287
          continue;
×
1288
        }
1289

UNCOV
1290
        numOfElem += 1;
×
UNCOV
1291
        pStdRes->count += 1;
×
UNCOV
1292
        pStdRes->dsum += plist[i];
×
UNCOV
1293
        pStdRes->quadraticDSum += plist[i] * plist[i];
×
1294
      }
UNCOV
1295
      break;
×
1296
    }
1297

UNCOV
1298
    default:
×
UNCOV
1299
      break;
×
1300
  }
1301

UNCOV
1302
_stddev_over:
×
1303
  // data in the check operation are all null, not output
UNCOV
1304
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
×
UNCOV
1305
  return TSDB_CODE_SUCCESS;
×
1306
}
1307

UNCOV
1308
static void stdTransferInfo(SStdRes* pInput, SStdRes* pOutput) {
×
UNCOV
1309
  if (IS_NULL_TYPE(pInput->type)) {
×
UNCOV
1310
    return;
×
1311
  }
UNCOV
1312
  pOutput->type = pInput->type;
×
UNCOV
1313
  if (IS_SIGNED_NUMERIC_TYPE(pOutput->type)) {
×
UNCOV
1314
    pOutput->quadraticISum += pInput->quadraticISum;
×
UNCOV
1315
    pOutput->isum += pInput->isum;
×
UNCOV
1316
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pOutput->type)) {
×
UNCOV
1317
    pOutput->quadraticUSum += pInput->quadraticUSum;
×
UNCOV
1318
    pOutput->usum += pInput->usum;
×
1319
  } else {
UNCOV
1320
    pOutput->quadraticDSum += pInput->quadraticDSum;
×
UNCOV
1321
    pOutput->dsum += pInput->dsum;
×
1322
  }
1323

UNCOV
1324
  pOutput->count += pInput->count;
×
1325
}
1326

UNCOV
1327
int32_t stdFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
1328
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1329
  SColumnInfoData*      pCol = pInput->pData[0];
×
1330

1331
  if (IS_NULL_TYPE(pCol->info.type)) {
×
UNCOV
1332
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
UNCOV
1333
    return TSDB_CODE_SUCCESS;
×
1334
  }
1335

UNCOV
1336
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
1337
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1338
  }
1339

UNCOV
1340
  SStdRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1341

UNCOV
1342
  for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
×
UNCOV
1343
    if (colDataIsNull_s(pCol, i)) continue;
×
UNCOV
1344
    char*    data = colDataGetData(pCol, i);
×
UNCOV
1345
    SStdRes* pInputInfo = (SStdRes*)varDataVal(data);
×
UNCOV
1346
    stdTransferInfo(pInputInfo, pInfo);
×
1347
  }
1348

UNCOV
1349
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
UNCOV
1350
  return TSDB_CODE_SUCCESS;
×
1351
}
1352

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

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

1361
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1362

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

1366
  int32_t start = pInput->startRowIndex;
1367
  int32_t numOfRows = pInput->numOfRows;
1368

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

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

UNCOV
1420
int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
1421
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1422
  SStdRes*              pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
1423
  int32_t               type = pStddevRes->type;
×
1424
  double                avg;
1425

UNCOV
1426
  if (pStddevRes->count == 0) {
×
UNCOV
1427
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
UNCOV
1428
    return functionFinalize(pCtx, pBlock);
×
1429
  }
1430

UNCOV
1431
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
1432
    avg = pStddevRes->isum / ((double)pStddevRes->count);
×
UNCOV
1433
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticISum / ((double)pStddevRes->count) - avg * avg));
×
UNCOV
1434
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
1435
    avg = pStddevRes->usum / ((double)pStddevRes->count);
×
UNCOV
1436
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticUSum / ((double)pStddevRes->count) - avg * avg));
×
1437
  } else {
UNCOV
1438
    avg = pStddevRes->dsum / ((double)pStddevRes->count);
×
UNCOV
1439
    pStddevRes->result = sqrt(fabs(pStddevRes->quadraticDSum / ((double)pStddevRes->count) - avg * avg));
×
1440
  }
1441

1442
  // check for overflow
UNCOV
1443
  if (isinf(pStddevRes->result) || isnan(pStddevRes->result)) {
×
UNCOV
1444
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1445
  }
1446

UNCOV
1447
  return functionFinalize(pCtx, pBlock);
×
1448
}
1449

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

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

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

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

UNCOV
1477
  return functionFinalize(pCtx, pBlock);
×
1478
}
1479

UNCOV
1480
int32_t stdPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
1481
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
1482
  SStdRes*             pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
1483
  int32_t              resultBytes = getStdInfoSize();
×
UNCOV
1484
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
1485

UNCOV
1486
  if (NULL == res) {
×
UNCOV
1487
    return terrno;
×
1488
  }
UNCOV
1489
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
1490
  varDataSetLen(res, resultBytes);
×
1491

UNCOV
1492
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
1493
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
1494
  if (NULL == pCol) {
×
UNCOV
1495
    taosMemoryFree(res);
×
UNCOV
1496
    return TSDB_CODE_OUT_OF_RANGE;
×
1497
  }
1498

UNCOV
1499
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
1500

UNCOV
1501
  taosMemoryFree(res);
×
UNCOV
1502
  return code;
×
1503
}
1504

UNCOV
1505
int32_t stdCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1506
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
1507
  SStdRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
1508

UNCOV
1509
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
1510
  SStdRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
1511
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
×
1512

UNCOV
1513
  stdTransferInfo(pSBuf, pDBuf);
×
1514

UNCOV
1515
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
1516
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
1517
  return TSDB_CODE_SUCCESS;
×
1518
}
1519

UNCOV
1520
bool getLeastSQRFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
1521
  pEnv->calcMemSize = sizeof(SLeastSQRInfo);
×
UNCOV
1522
  return true;
×
1523
}
1524

1525
int32_t leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
1526
  if (pResultInfo->initialized) {
×
UNCOV
1527
    return TSDB_CODE_SUCCESS;
×
1528
  }
UNCOV
1529
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
1530
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1531
  }
1532

UNCOV
1533
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
1534

UNCOV
1535
  GET_TYPED_DATA(pInfo->startVal, double, pCtx->param[1].param.nType, &pCtx->param[1].param.i);
×
UNCOV
1536
  GET_TYPED_DATA(pInfo->stepVal, double, pCtx->param[2].param.nType, &pCtx->param[2].param.i);
×
UNCOV
1537
  return TSDB_CODE_SUCCESS;
×
1538
}
1539

UNCOV
1540
int32_t leastSQRFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1541
  int32_t numOfElem = 0;
×
1542

UNCOV
1543
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1544
  int32_t               type = pInput->pData[0]->info.type;
×
1545

UNCOV
1546
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1547

UNCOV
1548
  SColumnInfoData* pCol = pInput->pData[0];
×
1549

UNCOV
1550
  double(*param)[3] = pInfo->matrix;
×
UNCOV
1551
  double x = pInfo->startVal;
×
1552

UNCOV
1553
  int32_t start = pInput->startRowIndex;
×
UNCOV
1554
  int32_t numOfRows = pInput->numOfRows;
×
1555

UNCOV
1556
  switch (type) {
×
UNCOV
1557
    case TSDB_DATA_TYPE_TINYINT: {
×
UNCOV
1558
      int8_t* plist = (int8_t*)pCol->pData;
×
UNCOV
1559
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1560
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1561
          continue;
×
1562
        }
UNCOV
1563
        numOfElem++;
×
UNCOV
1564
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1565
      }
UNCOV
1566
      break;
×
1567
    }
UNCOV
1568
    case TSDB_DATA_TYPE_SMALLINT: {
×
UNCOV
1569
      int16_t* plist = (int16_t*)pCol->pData;
×
UNCOV
1570
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1571
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1572
          continue;
×
1573
        }
1574

UNCOV
1575
        numOfElem++;
×
UNCOV
1576
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1577
      }
UNCOV
1578
      break;
×
1579
    }
1580

UNCOV
1581
    case TSDB_DATA_TYPE_INT: {
×
UNCOV
1582
      int32_t* plist = (int32_t*)pCol->pData;
×
UNCOV
1583
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1584
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1585
          continue;
×
1586
        }
1587

UNCOV
1588
        numOfElem++;
×
UNCOV
1589
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1590
      }
UNCOV
1591
      break;
×
1592
    }
1593

UNCOV
1594
    case TSDB_DATA_TYPE_BIGINT: {
×
UNCOV
1595
      int64_t* plist = (int64_t*)pCol->pData;
×
UNCOV
1596
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1597
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1598
          continue;
×
1599
        }
1600

UNCOV
1601
        numOfElem++;
×
UNCOV
1602
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1603
      }
UNCOV
1604
      break;
×
1605
    }
1606

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

UNCOV
1625
        numOfElem++;
×
UNCOV
1626
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1627
      }
UNCOV
1628
      break;
×
1629
    }
1630

UNCOV
1631
    case TSDB_DATA_TYPE_UINT: {
×
UNCOV
1632
      uint32_t* plist = (uint32_t*)pCol->pData;
×
UNCOV
1633
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1634
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1635
          continue;
×
1636
        }
1637

UNCOV
1638
        numOfElem++;
×
UNCOV
1639
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1640
      }
UNCOV
1641
      break;
×
1642
    }
1643

UNCOV
1644
    case TSDB_DATA_TYPE_UBIGINT: {
×
UNCOV
1645
      uint64_t* plist = (uint64_t*)pCol->pData;
×
UNCOV
1646
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1647
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1648
          continue;
×
1649
        }
1650

UNCOV
1651
        numOfElem++;
×
UNCOV
1652
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1653
      }
UNCOV
1654
      break;
×
1655
    }
1656

UNCOV
1657
    case TSDB_DATA_TYPE_FLOAT: {
×
UNCOV
1658
      float* plist = (float*)pCol->pData;
×
UNCOV
1659
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1660
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1661
          continue;
×
1662
        }
1663

UNCOV
1664
        numOfElem++;
×
UNCOV
1665
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1666
      }
UNCOV
1667
      break;
×
1668
    }
1669

UNCOV
1670
    case TSDB_DATA_TYPE_DOUBLE: {
×
UNCOV
1671
      double* plist = (double*)pCol->pData;
×
UNCOV
1672
      for (int32_t i = start; i < numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
1673
        if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1674
          continue;
×
1675
        }
1676

UNCOV
1677
        numOfElem++;
×
UNCOV
1678
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1679
      }
1680
      break;
×
1681
    }
1682
    case TSDB_DATA_TYPE_NULL: {
×
1683
      GET_RES_INFO(pCtx)->isNullRes = 1;
×
UNCOV
1684
      numOfElem = 1;
×
UNCOV
1685
      break;
×
1686
    }
1687

UNCOV
1688
    default:
×
UNCOV
1689
      break;
×
1690
  }
1691

UNCOV
1692
  pInfo->startVal = x;
×
UNCOV
1693
  pInfo->num += numOfElem;
×
1694

UNCOV
1695
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
×
1696

UNCOV
1697
  return TSDB_CODE_SUCCESS;
×
1698
}
1699

UNCOV
1700
int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
1701
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
1702
  SLeastSQRInfo*       pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
1703
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
1704
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
1705

UNCOV
1706
  if (NULL == pCol) {
×
UNCOV
1707
    return TSDB_CODE_OUT_OF_RANGE;
×
1708
  }
UNCOV
1709
  int32_t currentRow = pBlock->info.rows;
×
1710

UNCOV
1711
  if (0 == pInfo->num) {
×
UNCOV
1712
    colDataSetNULL(pCol, currentRow);
×
UNCOV
1713
    return TSDB_CODE_SUCCESS;
×
1714
  }
1715

UNCOV
1716
  double(*param)[3] = pInfo->matrix;
×
1717

UNCOV
1718
  param[1][1] = (double)pInfo->num;
×
UNCOV
1719
  param[1][0] = param[0][1];
×
1720

UNCOV
1721
  double param00 = param[0][0] - param[1][0] * (param[0][1] / param[1][1]);
×
UNCOV
1722
  double param02 = param[0][2] - param[1][2] * (param[0][1] / param[1][1]);
×
1723

UNCOV
1724
  if (0 == param00) {
×
UNCOV
1725
    colDataSetNULL(pCol, currentRow);
×
UNCOV
1726
    return TSDB_CODE_SUCCESS;
×
1727
  }
1728

1729
  // param[0][1] = 0;
UNCOV
1730
  double param12 = param[1][2] - param02 * (param[1][0] / param00);
×
1731
  // param[1][0] = 0;
UNCOV
1732
  param02 /= param00;
×
1733

UNCOV
1734
  param12 /= param[1][1];
×
1735

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

UNCOV
1751
  int32_t code = colDataSetVal(pCol, currentRow, buf, pResInfo->isNullRes);
×
1752

UNCOV
1753
  return code;
×
1754
}
1755

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

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

UNCOV
1778
bool getPercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
1779
  pEnv->calcMemSize = sizeof(SPercentileInfo);
×
UNCOV
1780
  return true;
×
1781
}
1782

1783
int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
1784
  if (pResultInfo->initialized) {
×
UNCOV
1785
    return TSDB_CODE_SUCCESS;
×
1786
  }
UNCOV
1787
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
1788
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
1789
  }
1790

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

UNCOV
1797
  return TSDB_CODE_SUCCESS;
×
1798
}
1799

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

UNCOV
1811
int32_t percentileFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1812
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
1813
  int32_t              numOfElems = 0;
×
UNCOV
1814
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
1815

UNCOV
1816
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1817
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
1818

UNCOV
1819
  SColumnInfoData* pCol = pInput->pData[0];
×
UNCOV
1820
  int32_t          type = pCol->info.type;
×
1821

UNCOV
1822
  SPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
1823
  if (pCtx->scanFlag == MAIN_SCAN && pInfo->stage == 0) {
×
UNCOV
1824
    pInfo->stage += 1;
×
1825

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

1839
  // the first stage, only acquire the min/max value
UNCOV
1840
  if (pInfo->stage == 0) {
×
UNCOV
1841
    if (pCtx->input.colDataSMAIsSet) {
×
1842
      double tmin = 0.0, tmax = 0.0;
×
1843
      if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
1844
        tmin = (double)GET_INT64_VAL(&pAgg->min);
×
UNCOV
1845
        tmax = (double)GET_INT64_VAL(&pAgg->max);
×
UNCOV
1846
      } else if (IS_FLOAT_TYPE(type)) {
×
1847
        tmin = GET_DOUBLE_VAL(&pAgg->min);
×
1848
        tmax = GET_DOUBLE_VAL(&pAgg->max);
×
1849
      } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
1850
        tmin = (double)GET_UINT64_VAL(&pAgg->min);
×
UNCOV
1851
        tmax = (double)GET_UINT64_VAL(&pAgg->max);
×
1852
      }
1853

UNCOV
1854
      if (GET_DOUBLE_VAL(&pInfo->minval) > tmin) {
×
UNCOV
1855
        SET_DOUBLE_VAL(&pInfo->minval, tmin);
×
1856
      }
1857

UNCOV
1858
      if (GET_DOUBLE_VAL(&pInfo->maxval) < tmax) {
×
UNCOV
1859
        SET_DOUBLE_VAL(&pInfo->maxval, tmax);
×
1860
      }
1861

UNCOV
1862
      pInfo->numOfElems += (pInput->numOfRows - pAgg->numOfNull);
×
1863
    } else {
1864
      // check the valid data one by one
UNCOV
1865
      int32_t start = pInput->startRowIndex;
×
UNCOV
1866
      for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
1867
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1868
          continue;
×
1869
        }
1870

UNCOV
1871
        char* data = colDataGetData(pCol, i);
×
1872

UNCOV
1873
        double v = 0;
×
UNCOV
1874
        GET_TYPED_DATA(v, double, type, data);
×
UNCOV
1875
        if (v < GET_DOUBLE_VAL(&pInfo->minval)) {
×
UNCOV
1876
          SET_DOUBLE_VAL(&pInfo->minval, v);
×
1877
        }
1878

UNCOV
1879
        if (v > GET_DOUBLE_VAL(&pInfo->maxval)) {
×
UNCOV
1880
          SET_DOUBLE_VAL(&pInfo->maxval, v);
×
1881
        }
1882

UNCOV
1883
        pInfo->numOfElems += 1;
×
1884
      }
1885
    }
1886
  } else {
1887
    // the second stage, calculate the true percentile value
UNCOV
1888
    int32_t start = pInput->startRowIndex;
×
1889
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
1890
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
1891
        continue;
×
1892
      }
1893

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

UNCOV
1903
    SET_VAL(pResInfo, numOfElems, 1);
×
1904
  }
1905

UNCOV
1906
  pCtx->needCleanup = true;
×
UNCOV
1907
  return TSDB_CODE_SUCCESS;
×
1908
}
1909

UNCOV
1910
int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
1911
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
1912
  SPercentileInfo*     ppInfo = (SPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
1913

UNCOV
1914
  int32_t code = 0;
×
UNCOV
1915
  double  v = 0;
×
1916

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

UNCOV
1925
      varDataVal(buf)[0] = '[';
×
UNCOV
1926
      for (int32_t i = 1; i < pCtx->numOfParams; ++i) {
×
UNCOV
1927
        SVariant* pVal = &pCtx->param[i].param;
×
1928

UNCOV
1929
        GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
×
1930

1931
        code = getPercentile((*pMemBucket), v, &ppInfo->result);
×
UNCOV
1932
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1933
          goto _fin_error;
×
1934
        }
1935

UNCOV
1936
        if (i == pCtx->numOfParams - 1) {
×
UNCOV
1937
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf]", ppInfo->result);
×
1938
        } else {
UNCOV
1939
          len += tsnprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf, ", ppInfo->result);
×
1940
        }
1941
      }
1942

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

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

UNCOV
1956
      tMemBucketDestroy(pMemBucket);
×
UNCOV
1957
      return TSDB_CODE_SUCCESS;
×
1958
    } else {
UNCOV
1959
      SVariant* pVal = &pCtx->param[1].param;
×
1960

UNCOV
1961
      GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
×
1962

1963
      code = getPercentile((*pMemBucket), v, &ppInfo->result);
×
UNCOV
1964
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1965
        goto _fin_error;
×
1966
      }
1967

UNCOV
1968
      tMemBucketDestroy(pMemBucket);
×
UNCOV
1969
      return functionFinalize(pCtx, pBlock);
×
1970
    }
1971
  } else {
UNCOV
1972
    return functionFinalize(pCtx, pBlock);
×
1973
  }
1974

1975
_fin_error:
×
1976

UNCOV
1977
  tMemBucketDestroy(pMemBucket);
×
UNCOV
1978
  return code;
×
1979
}
1980

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

UNCOV
1989
int32_t getApercentileMaxSize() {
×
UNCOV
1990
  int32_t bytesHist =
×
1991
      (int32_t)(sizeof(SAPercentileInfo) + sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
UNCOV
1992
  int32_t bytesDigest = (int32_t)(sizeof(SAPercentileInfo) + TDIGEST_SIZE(COMPRESSION));
×
UNCOV
1993
  return TMAX(bytesHist, bytesDigest);
×
1994
}
1995

UNCOV
1996
static int8_t getApercentileAlgo(char* algoStr) {
×
1997
  int8_t algoType;
UNCOV
1998
  if (strcasecmp(algoStr, "default") == 0) {
×
UNCOV
1999
    algoType = APERCT_ALGO_DEFAULT;
×
UNCOV
2000
  } else if (strcasecmp(algoStr, "t-digest") == 0) {
×
2001
    algoType = APERCT_ALGO_TDIGEST;
×
2002
  } else {
UNCOV
2003
    algoType = APERCT_ALGO_UNKNOWN;
×
2004
  }
2005

UNCOV
2006
  return algoType;
×
2007
}
2008

UNCOV
2009
static void buildHistogramInfo(SAPercentileInfo* pInfo) {
×
UNCOV
2010
  pInfo->pHisto = (SHistogramInfo*)((char*)pInfo + sizeof(SAPercentileInfo));
×
UNCOV
2011
  pInfo->pHisto->elems = (SHistBin*)((char*)pInfo->pHisto + sizeof(SHistogramInfo));
×
UNCOV
2012
}
×
2013

UNCOV
2014
static void buildTDigestInfo(SAPercentileInfo* pInfo) {
×
UNCOV
2015
  pInfo->pTDigest = (TDigest*)((char*)pInfo + sizeof(SAPercentileInfo));
×
UNCOV
2016
}
×
2017

2018
int32_t apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
2019
  if (pResultInfo->initialized) {
×
UNCOV
2020
    return TSDB_CODE_SUCCESS;
×
2021
  }
UNCOV
2022
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
2023
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
2024
  }
2025

UNCOV
2026
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
2027

UNCOV
2028
  SVariant* pVal = &pCtx->param[1].param;
×
UNCOV
2029
  pInfo->percent = 0;
×
UNCOV
2030
  GET_TYPED_DATA(pInfo->percent, double, pVal->nType, &pVal->i);
×
2031

UNCOV
2032
  if (pCtx->numOfParams == 2) {
×
UNCOV
2033
    pInfo->algo = APERCT_ALGO_DEFAULT;
×
UNCOV
2034
  } else if (pCtx->numOfParams == 3) {
×
2035
    pInfo->algo = getApercentileAlgo(varDataVal(pCtx->param[2].param.pz));
×
UNCOV
2036
    if (pInfo->algo == APERCT_ALGO_UNKNOWN) {
×
UNCOV
2037
      return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
×
2038
    }
2039
  }
2040

UNCOV
2041
  char* tmp = (char*)pInfo + sizeof(SAPercentileInfo);
×
UNCOV
2042
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
×
UNCOV
2043
    pInfo->pTDigest = tdigestNewFrom(tmp, COMPRESSION);
×
2044
  } else {
UNCOV
2045
    buildHistogramInfo(pInfo);
×
UNCOV
2046
    pInfo->pHisto = tHistogramCreateFrom(tmp, MAX_HISTOGRAM_BIN);
×
UNCOV
2047
    qDebug("%s set up histogram, numOfElems:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems:%p", __FUNCTION__,
×
2048
           pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems);
2049
  }
2050

UNCOV
2051
  return TSDB_CODE_SUCCESS;
×
2052
}
2053

UNCOV
2054
int32_t apercentileFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
2055
  int32_t               numOfElems = 0;
×
UNCOV
2056
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2057
  SInputColumnInfoData* pInput = &pCtx->input;
×
2058

UNCOV
2059
  SColumnInfoData* pCol = pInput->pData[0];
×
UNCOV
2060
  int32_t          type = pCol->info.type;
×
2061

UNCOV
2062
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2063

UNCOV
2064
  int32_t start = pInput->startRowIndex;
×
UNCOV
2065
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
×
UNCOV
2066
    buildTDigestInfo(pInfo);
×
UNCOV
2067
    tdigestAutoFill(pInfo->pTDigest, COMPRESSION);
×
UNCOV
2068
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
2069
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
2070
        continue;
×
2071
      }
UNCOV
2072
      numOfElems += 1;
×
UNCOV
2073
      char* data = colDataGetData(pCol, i);
×
2074

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

UNCOV
2097
      double v = 0;
×
UNCOV
2098
      GET_TYPED_DATA(v, double, type, data);
×
2099
      int32_t code = tHistogramAdd(&pInfo->pHisto, v);
×
UNCOV
2100
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2101
        return code;
×
2102
      }
2103
    }
2104

UNCOV
2105
    qDebug("%s after add %d elements into histogram, total:%" PRId64 ", numOfEntry:%d, pHisto:%p, elems: %p",
×
2106
           __FUNCTION__, numOfElems, pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto,
2107
           pInfo->pHisto->elems);
2108
  }
2109

UNCOV
2110
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
2111
  return TSDB_CODE_SUCCESS;
×
2112
}
2113

UNCOV
2114
static int32_t apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) {
×
UNCOV
2115
  pOutput->percent = pInput->percent;
×
UNCOV
2116
  pOutput->algo = pInput->algo;
×
UNCOV
2117
  if (pOutput->algo == APERCT_ALGO_TDIGEST) {
×
UNCOV
2118
    buildTDigestInfo(pInput);
×
UNCOV
2119
    tdigestAutoFill(pInput->pTDigest, COMPRESSION);
×
2120

UNCOV
2121
    if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) {
×
UNCOV
2122
      return TSDB_CODE_SUCCESS;
×
2123
    }
2124

UNCOV
2125
    if (hasRes) {
×
UNCOV
2126
      *hasRes = true;
×
2127
    }
2128

UNCOV
2129
    buildTDigestInfo(pOutput);
×
UNCOV
2130
    TDigest* pTDigest = pOutput->pTDigest;
×
UNCOV
2131
    tdigestAutoFill(pTDigest, COMPRESSION);
×
2132

UNCOV
2133
    if (pTDigest->num_centroids <= 0 && pTDigest->num_buffered_pts == 0) {
×
UNCOV
2134
      (void)memcpy(pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION));
×
UNCOV
2135
      tdigestAutoFill(pTDigest, COMPRESSION);
×
2136
    } else {
2137
      int32_t code = tdigestMerge(pTDigest, pInput->pTDigest);
×
UNCOV
2138
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
2139
        return code;
×
2140
      }
2141
    }
2142
  } else {
UNCOV
2143
    buildHistogramInfo(pInput);
×
UNCOV
2144
    if (pInput->pHisto->numOfElems <= 0) {
×
UNCOV
2145
      return TSDB_CODE_SUCCESS;
×
2146
    }
2147

UNCOV
2148
    if (hasRes) {
×
UNCOV
2149
      *hasRes = true;
×
2150
    }
2151

UNCOV
2152
    buildHistogramInfo(pOutput);
×
UNCOV
2153
    SHistogramInfo* pHisto = pOutput->pHisto;
×
2154

UNCOV
2155
    if (pHisto->numOfElems <= 0) {
×
UNCOV
2156
      (void)memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1));
×
UNCOV
2157
      pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo));
×
2158

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

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

UNCOV
2175
      qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries,
×
2176
             pHisto);
UNCOV
2177
      tHistogramDestroy(&pRes);
×
2178
    }
2179
  }
UNCOV
2180
  return TSDB_CODE_SUCCESS;
×
2181
}
2182

UNCOV
2183
int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
2184
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
2185

UNCOV
2186
  SInputColumnInfoData* pInput = &pCtx->input;
×
2187

2188
  SColumnInfoData* pCol = pInput->pData[0];
×
UNCOV
2189
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
2190
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2191
  }
2192

UNCOV
2193
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2194

UNCOV
2195
  qDebug("%s total %" PRId64 " rows will merge, %p", __FUNCTION__, pInput->numOfRows, pInfo->pHisto);
×
2196

UNCOV
2197
  bool    hasRes = false;
×
UNCOV
2198
  int32_t start = pInput->startRowIndex;
×
UNCOV
2199
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
2200
    char* data = colDataGetData(pCol, i);
×
2201

UNCOV
2202
    SAPercentileInfo* pInputInfo = (SAPercentileInfo*)varDataVal(data);
×
2203
    int32_t           code = apercentileTransferInfo(pInputInfo, pInfo, &hasRes);
×
UNCOV
2204
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
2205
      return code;
×
2206
    }
2207
  }
2208

UNCOV
2209
  if (pInfo->algo != APERCT_ALGO_TDIGEST) {
×
UNCOV
2210
    buildHistogramInfo(pInfo);
×
UNCOV
2211
    qDebug("%s after merge, total:%" PRId64 ", numOfEntry:%d, %p", __FUNCTION__, pInfo->pHisto->numOfElems,
×
2212
           pInfo->pHisto->numOfEntries, pInfo->pHisto);
2213
  }
2214

UNCOV
2215
  SET_VAL(pResInfo, hasRes ? 1 : 0, 1);
×
UNCOV
2216
  return TSDB_CODE_SUCCESS;
×
2217
}
2218

UNCOV
2219
int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2220
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2221
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
2222

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

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

UNCOV
2256
  return functionFinalize(pCtx, pBlock);
×
2257
}
2258

UNCOV
2259
int32_t apercentilePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2260
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2261
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
2262

UNCOV
2263
  int32_t resultBytes = getApercentileMaxSize();
×
2264
  char*   res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
UNCOV
2265
  if (NULL == res) {
×
UNCOV
2266
    return terrno;
×
2267
  }
2268

UNCOV
2269
  if (pInfo->algo == APERCT_ALGO_TDIGEST) {
×
UNCOV
2270
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
2271
    varDataSetLen(res, resultBytes);
×
2272
  } else {
UNCOV
2273
    (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
2274
    varDataSetLen(res, resultBytes);
×
2275
  }
2276

UNCOV
2277
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
2278
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
2279
  if (NULL == pCol) {
×
UNCOV
2280
    taosMemoryFree(res);
×
UNCOV
2281
    return TSDB_CODE_OUT_OF_RANGE;
×
2282
  }
2283

UNCOV
2284
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
2285

UNCOV
2286
  taosMemoryFree(res);
×
UNCOV
2287
  return code;
×
2288
}
2289

UNCOV
2290
int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
2291
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
2292
  SAPercentileInfo*    pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
2293

UNCOV
2294
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
2295
  SAPercentileInfo*    pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
2296

UNCOV
2297
  qDebug("%s start to combine apercentile, %p", __FUNCTION__, pDBuf->pHisto);
×
2298

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

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

UNCOV
2332
EFuncDataRequired firstDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
×
UNCOV
2333
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
×
2334

2335
  // not initialized yet, data is required
UNCOV
2336
  if (pEntry == NULL) {
×
UNCOV
2337
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2338
  }
2339

UNCOV
2340
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
×
UNCOV
2341
  if (pResult->hasResult) {
×
UNCOV
2342
    if (pResult->pkBytes > 0) {
×
UNCOV
2343
      pResult->pkData = pResult->buf + pResult->bytes;
×
2344
    } else {
UNCOV
2345
      pResult->pkData = NULL;
×
2346
    }
UNCOV
2347
    if (pResult->ts < pBlockInfo->window.skey) {
×
UNCOV
2348
      return FUNC_DATA_REQUIRED_NOT_LOAD;
×
UNCOV
2349
    } else if (pResult->ts == pBlockInfo->window.skey) {
×
UNCOV
2350
      if (NULL == pResult->pkData) {
×
UNCOV
2351
        return FUNC_DATA_REQUIRED_NOT_LOAD;
×
2352
      }
UNCOV
2353
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 0, TSDB_ORDER_ASC) < 0) {
×
UNCOV
2354
        return FUNC_DATA_REQUIRED_NOT_LOAD;
×
2355
      }
2356
    }
UNCOV
2357
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2358
  } else {
UNCOV
2359
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2360
  }
2361
}
2362

UNCOV
2363
EFuncDataRequired lastDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
×
UNCOV
2364
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
×
2365

2366
  // not initialized yet, data is required
UNCOV
2367
  if (pEntry == NULL) {
×
UNCOV
2368
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2369
  }
2370

UNCOV
2371
  SFirstLastRes* pResult = GET_ROWCELL_INTERBUF(pEntry);
×
UNCOV
2372
  if (pResult->hasResult) {
×
UNCOV
2373
    if (pResult->pkBytes > 0) {
×
UNCOV
2374
      pResult->pkData = pResult->buf + pResult->bytes;
×
2375
    } else {
UNCOV
2376
      pResult->pkData = NULL;
×
2377
    }
UNCOV
2378
    if (pResult->ts > pBlockInfo->window.ekey) {
×
UNCOV
2379
      return FUNC_DATA_REQUIRED_NOT_LOAD;
×
2380
    } else if (pResult->ts == pBlockInfo->window.ekey && pResult->pkData) {
×
UNCOV
2381
      if (comparePkDataWithSValue(pResult->pkType, pResult->pkData, pBlockInfo->pks + 1, TSDB_ORDER_DESC) < 0) {
×
UNCOV
2382
        return FUNC_DATA_REQUIRED_NOT_LOAD;
×
2383
      }
2384
    }
UNCOV
2385
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2386
  } else {
UNCOV
2387
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2388
  }
2389
}
2390

2391
// TODO modify it to include primary key bytes
2392
int32_t getFirstLastInfoSize(int32_t resBytes, int32_t pkBytes) { return sizeof(SFirstLastRes) + resBytes + pkBytes; }
7✔
2393

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

UNCOV
2402
bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
2403
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
×
UNCOV
2404
  pEnv->calcMemSize = pNode->node.resType.bytes;
×
UNCOV
2405
  return true;
×
2406
}
2407

UNCOV
2408
bool getGroupKeyFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
2409
  SColumnNode* pNode = (SColumnNode*)nodesListGetNode(pFunc->pParameterList, 0);
×
UNCOV
2410
  pEnv->calcMemSize = sizeof(SGroupKeyInfo) + pNode->node.resType.bytes;
×
UNCOV
2411
  return true;
×
2412
}
2413

2414
static FORCE_INLINE TSKEY getRowPTs(SColumnInfoData* pTsColInfo, int32_t rowIndex) {
UNCOV
2415
  if (pTsColInfo == NULL || pTsColInfo->pData == NULL) {
×
UNCOV
2416
    return 0;
×
2417
  }
2418

UNCOV
2419
  return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex);
×
2420
}
2421

2422
int32_t firstLastFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
2423
  if (pResInfo->initialized) {
×
UNCOV
2424
    return TSDB_CODE_SUCCESS;
×
2425
  }
UNCOV
2426
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
UNCOV
2427
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
2428
  }
2429

UNCOV
2430
  SFirstLastRes*        pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
2431
  pRes->nullTupleSaved = false;
×
UNCOV
2432
  pRes->nullTuplePos.pageId = -1;
×
UNCOV
2433
  return TSDB_CODE_SUCCESS;
×
2434
}
2435

UNCOV
2436
static int32_t prepareBuf(SqlFunctionCtx* pCtx) {
×
UNCOV
2437
  if (pCtx->subsidiaries.rowLen == 0) {
×
UNCOV
2438
    int32_t rowLen = 0;
×
UNCOV
2439
    for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
×
UNCOV
2440
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
UNCOV
2441
      rowLen += pc->pExpr->base.resSchema.bytes;
×
2442
    }
2443

UNCOV
2444
    pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool);
×
2445
    pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen);
×
UNCOV
2446
    if (NULL == pCtx->subsidiaries.buf) {
×
UNCOV
2447
      return terrno;
×
2448
    }
2449
  }
UNCOV
2450
  return TSDB_CODE_SUCCESS;
×
2451
}
2452

UNCOV
2453
static int32_t firstlastSaveTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SqlFunctionCtx* pCtx,
×
2454
                                      SFirstLastRes* pInfo, bool noElements) {
UNCOV
2455
  int32_t code = TSDB_CODE_SUCCESS;
×
2456

UNCOV
2457
  if (pCtx->subsidiaries.num <= 0) {
×
UNCOV
2458
    return TSDB_CODE_SUCCESS;
×
2459
  }
2460

UNCOV
2461
  if (!pInfo->hasResult) {
×
UNCOV
2462
    code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
×
UNCOV
2463
  } else if (!noElements) {
×
UNCOV
2464
    code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
×
2465
  } else { } // dothing
2466

UNCOV
2467
  return code;
×
2468
}
2469

UNCOV
2470
static int32_t doSaveCurrentVal(SqlFunctionCtx* pCtx, int32_t rowIndex, int64_t currentTs, char* pkData, int32_t type,
×
2471
                                char* pData) {
UNCOV
2472
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2473
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2474

2475
  if (IS_VAR_DATA_TYPE(type)) {
×
UNCOV
2476
    if (type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
2477
      pInfo->bytes = getJsonValueLen(pData);
×
2478
    } else {
UNCOV
2479
      pInfo->bytes = varDataTLen(pData);
×
2480
    }
2481
  }
2482

UNCOV
2483
  (void)memcpy(pInfo->buf, pData, pInfo->bytes);
×
UNCOV
2484
  if (pkData != NULL) {
×
2485
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
×
UNCOV
2486
      if (pInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
2487
        pInfo->pkBytes = getJsonValueLen(pkData);
×
2488
      } else {
UNCOV
2489
        pInfo->pkBytes = varDataTLen(pkData);
×
2490
      }
2491
    }
UNCOV
2492
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
×
UNCOV
2493
    pInfo->pkData = pInfo->buf + pInfo->bytes;
×
2494
  }
2495

UNCOV
2496
  pInfo->ts = currentTs;
×
2497
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
×
UNCOV
2498
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2499
    return code;
×
2500
  }
2501

UNCOV
2502
  pInfo->hasResult = true;
×
UNCOV
2503
  return TSDB_CODE_SUCCESS;
×
2504
}
2505

2506
// This ordinary first function does not care if current scan is ascending order or descending order scan
2507
// the OPTIMIZED version of first function will only handle the ascending order scan
UNCOV
2508
int32_t firstFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
2509
  int32_t numOfElems = 0;
×
2510

UNCOV
2511
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2512
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2513

UNCOV
2514
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2515
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
2516

UNCOV
2517
  pInfo->bytes = pInputCol->info.bytes;
×
2518

UNCOV
2519
  if (IS_NULL_TYPE(pInputCol->info.type)) {
×
UNCOV
2520
    return TSDB_CODE_SUCCESS;
×
2521
  }
2522

UNCOV
2523
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
2524
  pInfo->pkType = -1;
×
UNCOV
2525
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2526
  if (pCtx->hasPrimaryKey) {
×
UNCOV
2527
    pInfo->pkType = pkCol->info.type;
×
UNCOV
2528
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
2529
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_ASC);
×
2530
  }
2531

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

UNCOV
2544
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
×
2545

UNCOV
2546
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
×
UNCOV
2547
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
×
2548

UNCOV
2549
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
×
2550

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

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

2567
      numOfElems++;
2568

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

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

2590
      numOfElems++;
2591

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

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

UNCOV
2604
  int     from = -1;
×
UNCOV
2605
  int32_t i = -1;
×
UNCOV
2606
  while (funcInputGetNextRowIndex(pInput, from, true, &i, &from)) {
×
UNCOV
2607
    if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
×
UNCOV
2608
      continue;
×
2609
    }
2610

UNCOV
2611
    numOfElems++;
×
UNCOV
2612
    char* data = colDataGetData(pInputCol, i);
×
UNCOV
2613
    char* pkData = NULL;
×
UNCOV
2614
    if (pCtx->hasPrimaryKey) {
×
UNCOV
2615
      pkData = colDataGetData(pkCol, i);
×
2616
    }
UNCOV
2617
    TSKEY cts = pts[i];
×
UNCOV
2618
    if (pResInfo->numOfRes == 0 || pInfo->ts > cts ||
×
UNCOV
2619
        (pInfo->ts == cts && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
×
2620
      int32_t code = doSaveCurrentVal(pCtx, i, cts, pkData, pInputCol->info.type, data);
×
UNCOV
2621
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2622
        return code;
×
2623
      }
UNCOV
2624
      pResInfo->numOfRes = 1;
×
2625
    }
2626
  }
2627
#endif
2628

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

UNCOV
2641
int32_t lastFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
2642
  int32_t numOfElems = 0;
×
2643

UNCOV
2644
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2645
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2646

UNCOV
2647
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2648
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
2649

UNCOV
2650
  int32_t type = pInputCol->info.type;
×
UNCOV
2651
  int32_t bytes = pInputCol->info.bytes;
×
2652

UNCOV
2653
  if (IS_NULL_TYPE(type)) {
×
UNCOV
2654
    return TSDB_CODE_SUCCESS;
×
2655
  }
UNCOV
2656
  pInfo->bytes = bytes;
×
2657

UNCOV
2658
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
2659
  pInfo->pkType = -1;
×
UNCOV
2660
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2661
  if (pCtx->hasPrimaryKey) {
×
UNCOV
2662
    pInfo->pkType = pkCol->info.type;
×
UNCOV
2663
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
2664
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
×
2665
  }
2666

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

UNCOV
2679
  SColumnDataAgg* pColAgg = (pInput->colDataSMAIsSet) ? pInput->pColumnDataAgg[0] : NULL;
×
2680

UNCOV
2681
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
×
UNCOV
2682
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
×
2683

UNCOV
2684
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
×
2685

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

2694
      numOfElems++;
2695

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

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

2710
      numOfElems++;
2711

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

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

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

2738
  // todo refactor
UNCOV
2739
  if (!pInputCol->hasNull && !pCtx->hasPrimaryKey) {
×
UNCOV
2740
    numOfElems = 1;
×
2741

UNCOV
2742
    int32_t round = pInput->numOfRows >> 2;
×
UNCOV
2743
    int32_t reminder = pInput->numOfRows & 0x03;
×
2744

UNCOV
2745
    for (int32_t i = pInput->startRowIndex, tick = 0; tick < round; i += 4, tick += 1) {
×
UNCOV
2746
      int64_t cts = pts[i];
×
UNCOV
2747
      int32_t chosen = i;
×
2748

UNCOV
2749
      if (cts < pts[i + 1]) {
×
UNCOV
2750
        cts = pts[i + 1];
×
UNCOV
2751
        chosen = i + 1;
×
2752
      }
2753

UNCOV
2754
      if (cts < pts[i + 2]) {
×
UNCOV
2755
        cts = pts[i + 2];
×
UNCOV
2756
        chosen = i + 2;
×
2757
      }
2758

UNCOV
2759
      if (cts < pts[i + 3]) {
×
UNCOV
2760
        cts = pts[i + 3];
×
UNCOV
2761
        chosen = i + 3;
×
2762
      }
2763

UNCOV
2764
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
×
UNCOV
2765
        char*   data = colDataGetData(pInputCol, chosen);
×
2766
        int32_t code = doSaveCurrentVal(pCtx, chosen, cts, NULL, type, data);
×
UNCOV
2767
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2768
          return code;
×
2769
        }
UNCOV
2770
        pResInfo->numOfRes = 1;
×
2771
      }
2772
    }
2773

UNCOV
2774
    for (int32_t i = pInput->startRowIndex + round * 4; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
×
UNCOV
2775
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
×
UNCOV
2776
        char*   data = colDataGetData(pInputCol, i);
×
2777
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], NULL, type, data);
×
UNCOV
2778
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2779
          return code;
×
2780
        }
UNCOV
2781
        pResInfo->numOfRes = 1;
×
2782
      }
2783
    }
2784
  } else {
UNCOV
2785
    int     from = -1;
×
UNCOV
2786
    int32_t i = -1;
×
UNCOV
2787
    while (funcInputGetNextRowIndex(pInput, from, false, &i, &from)) {
×
UNCOV
2788
      if (colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
×
UNCOV
2789
        continue;
×
2790
      }
2791

UNCOV
2792
      numOfElems++;
×
UNCOV
2793
      char* pkData = NULL;
×
UNCOV
2794
      if (pCtx->hasPrimaryKey) {
×
UNCOV
2795
        pkData = colDataGetData(pkCol, i);
×
2796
      }
UNCOV
2797
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i] ||
×
UNCOV
2798
          (pInfo->ts == pts[i] && pkCompareFn && pkCompareFn(pkData, pInfo->pkData) < 0)) {
×
UNCOV
2799
        char*   data = colDataGetData(pInputCol, i);
×
2800
        int32_t code = doSaveCurrentVal(pCtx, i, pts[i], pkData, type, data);
×
UNCOV
2801
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2802
          return code;
×
2803
        }
UNCOV
2804
        pResInfo->numOfRes = 1;
×
2805
      }
2806
    }
2807
  }
2808
#endif
2809

2810
#endif
2811

2812
  // save selectivity value for column consisted of all null values
UNCOV
2813
  if (numOfElems == 0) {
×
2814
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
×
UNCOV
2815
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2816
      return code;
×
2817
    }
UNCOV
2818
    pInfo->nullTupleSaved = true;
×
2819
  }
2820

UNCOV
2821
  return TSDB_CODE_SUCCESS;
×
2822
}
2823

UNCOV
2824
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) {
×
UNCOV
2825
  if (!pInput->hasResult) {
×
UNCOV
2826
    return false;
×
2827
  }
UNCOV
2828
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2829
  if (pInput->pkData) {
×
UNCOV
2830
    pkCompareFn = getKeyComparFunc(pInput->pkType, (isFirst) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC);
×
2831
  }
UNCOV
2832
  if (pOutput->hasResult) {
×
UNCOV
2833
    if (isFirst) {
×
UNCOV
2834
      if (pInput->ts > pOutput->ts ||
×
UNCOV
2835
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
×
UNCOV
2836
        return false;
×
2837
      }
2838
    } else {
UNCOV
2839
      if (pInput->ts < pOutput->ts ||
×
UNCOV
2840
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
×
UNCOV
2841
        return false;
×
2842
      }
2843
    }
2844
  }
2845

UNCOV
2846
  pOutput->isNull = pInput->isNull;
×
UNCOV
2847
  pOutput->ts = pInput->ts;
×
UNCOV
2848
  pOutput->bytes = pInput->bytes;
×
UNCOV
2849
  pOutput->pkType = pInput->pkType;
×
2850

UNCOV
2851
  (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes);
×
UNCOV
2852
  if (pInput->pkData) {
×
UNCOV
2853
    pOutput->pkBytes = pInput->pkBytes;
×
UNCOV
2854
    (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes);
×
UNCOV
2855
    pOutput->pkData = pOutput->buf + pOutput->bytes;
×
2856
  }
UNCOV
2857
  return true;
×
2858
}
2859

UNCOV
2860
static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst,
×
2861
                                     int32_t rowIndex) {
UNCOV
2862
  if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) {
×
2863
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput, false);
×
UNCOV
2864
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
2865
      return code;
×
2866
    }
UNCOV
2867
    pOutput->hasResult = true;
×
2868
  }
UNCOV
2869
  return TSDB_CODE_SUCCESS;
×
2870
}
2871

UNCOV
2872
static int32_t firstLastFunctionMergeImpl(SqlFunctionCtx* pCtx, bool isFirstQuery) {
×
UNCOV
2873
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2874
  SColumnInfoData*      pCol = pInput->pData[0];
×
2875

2876
  if (IS_NULL_TYPE(pCol->info.type)) {
×
UNCOV
2877
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
UNCOV
2878
    return TSDB_CODE_SUCCESS;
×
2879
  }
2880

UNCOV
2881
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
2882
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2883
  }
2884

UNCOV
2885
  SFirstLastRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
2886

UNCOV
2887
  int32_t start = pInput->startRowIndex;
×
UNCOV
2888
  int32_t numOfElems = 0;
×
2889

UNCOV
2890
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
2891
    if (colDataIsNull_s(pCol, i)) {
×
UNCOV
2892
      continue;
×
2893
    }
UNCOV
2894
    char*          data = colDataGetData(pCol, i);
×
UNCOV
2895
    SFirstLastRes* pInputInfo = (SFirstLastRes*)varDataVal(data);
×
UNCOV
2896
    if (pCtx->hasPrimaryKey) {
×
UNCOV
2897
      pInputInfo->pkData = pInputInfo->buf + pInputInfo->bytes;
×
2898
    } else {
UNCOV
2899
      pInputInfo->pkData = NULL;
×
2900
    }
2901

2902
    int32_t code = firstLastTransferInfo(pCtx, pInputInfo, pInfo, isFirstQuery, i);
×
UNCOV
2903
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2904
      return code;
×
2905
    }
UNCOV
2906
    if (!numOfElems) {
×
UNCOV
2907
      numOfElems = pInputInfo->hasResult ? 1 : 0;
×
2908
    }
2909
  }
2910

UNCOV
2911
  if (numOfElems == 0) {
×
2912
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, true);
×
UNCOV
2913
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2914
      return code;
×
2915
    }
UNCOV
2916
    pInfo->nullTupleSaved = true;
×
2917
  }
2918

UNCOV
2919
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
2920
  return TSDB_CODE_SUCCESS;
×
2921
}
2922

UNCOV
2923
int32_t firstFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, true); }
×
2924

UNCOV
2925
int32_t lastFunctionMerge(SqlFunctionCtx* pCtx) { return firstLastFunctionMergeImpl(pCtx, false); }
×
2926

UNCOV
2927
int32_t firstLastFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2928
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
2929
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
2930
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
2931
  if (NULL == pCol) {
×
UNCOV
2932
    return TSDB_CODE_OUT_OF_RANGE;
×
2933
  }
2934

UNCOV
2935
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2936
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
×
2937

UNCOV
2938
  SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
2939

UNCOV
2940
  if (pResInfo->isNullRes) {
×
UNCOV
2941
    colDataSetNULL(pCol, pBlock->info.rows);
×
UNCOV
2942
    return setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
×
2943
  }
2944
  code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes);
×
UNCOV
2945
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
2946
    return code;
×
2947
  }
2948

2949
  // handle selectivity
UNCOV
2950
  code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
×
2951

UNCOV
2952
  return code;
×
2953
}
2954

UNCOV
2955
int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2956
  int32_t code = TSDB_CODE_SUCCESS;
×
2957

UNCOV
2958
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
2959
  SFirstLastRes*       pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
×
2960

UNCOV
2961
  int32_t resultBytes = getFirstLastInfoSize(pRes->bytes, pRes->pkBytes);
×
2962

2963
  // todo check for failure
2964
  char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
UNCOV
2965
  if (NULL == res) {
×
UNCOV
2966
    return terrno;
×
2967
  }
UNCOV
2968
  (void)memcpy(varDataVal(res), pRes, resultBytes);
×
2969

UNCOV
2970
  varDataSetLen(res, resultBytes);
×
2971

UNCOV
2972
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
2973
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
2974
  if (NULL == pCol) {
×
UNCOV
2975
    taosMemoryFree(res);
×
UNCOV
2976
    return TSDB_CODE_OUT_OF_RANGE;
×
2977
  }
2978

UNCOV
2979
  if (pEntryInfo->numOfRes == 0) {
×
UNCOV
2980
    colDataSetNULL(pCol, pBlock->info.rows);
×
UNCOV
2981
    code = setNullSelectivityValue(pCtx, pBlock, pBlock->info.rows);
×
2982
  } else {
2983
    code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
2984
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
2985
      taosMemoryFree(res);
×
UNCOV
2986
      return code;
×
2987
    }
UNCOV
2988
    code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
×
2989
  }
UNCOV
2990
  taosMemoryFree(res);
×
UNCOV
2991
  return code;
×
2992
}
2993

UNCOV
2994
int32_t lastCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
2995
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
2996
  SFirstLastRes*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
UNCOV
2997
  int32_t              bytes = pDBuf->bytes;
×
2998

UNCOV
2999
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
3000
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
3001

UNCOV
3002
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, false);
×
UNCOV
3003
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
3004
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
3005
  return TSDB_CODE_SUCCESS;
×
3006
}
3007

UNCOV
3008
static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex, int64_t cts, SFirstLastRes* pInfo) {
×
UNCOV
3009
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3010
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
3011
  SColumnInfoData*      pkCol = pInput->pPrimaryKey;
×
3012

UNCOV
3013
  if (colDataIsNull_s(pInputCol, rowIndex)) {
×
UNCOV
3014
    pInfo->isNull = true;
×
3015
  } else {
UNCOV
3016
    pInfo->isNull = false;
×
3017

3018
    if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
×
UNCOV
3019
      if (pInputCol->info.type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
3020
        pInfo->bytes = getJsonValueLen(pData);
×
3021
      } else {
UNCOV
3022
        pInfo->bytes = varDataTLen(pData);
×
3023
      }
3024
    }
3025

UNCOV
3026
    (void)memcpy(pInfo->buf, pData, pInfo->bytes);
×
3027
  }
3028

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

UNCOV
3047
  pInfo->hasResult = true;
×
3048

UNCOV
3049
  return TSDB_CODE_SUCCESS;
×
3050
}
3051

UNCOV
3052
int32_t lastRowFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3053
  int32_t numOfElems = 0;
×
3054

UNCOV
3055
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3056
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3057

UNCOV
3058
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3059
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
3060

UNCOV
3061
  int32_t type = pInputCol->info.type;
×
UNCOV
3062
  int32_t bytes = pInputCol->info.bytes;
×
UNCOV
3063
  pInfo->bytes = bytes;
×
3064

UNCOV
3065
  if (IS_NULL_TYPE(type)) {
×
UNCOV
3066
    return TSDB_CODE_SUCCESS;
×
3067
  }
UNCOV
3068
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
3069
  pInfo->pkType = -1;
×
UNCOV
3070
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
3071
  if (pCtx->hasPrimaryKey) {
×
UNCOV
3072
    pInfo->pkType = pkCol->info.type;
×
UNCOV
3073
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
3074
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
×
3075
  }
UNCOV
3076
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
×
UNCOV
3077
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
×
3078

UNCOV
3079
  if (pCtx->order == TSDB_ORDER_ASC && !pCtx->hasPrimaryKey) {
×
UNCOV
3080
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
×
UNCOV
3081
      bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
×
UNCOV
3082
      char* data = isNull ? NULL : colDataGetData(pInputCol, i);
×
UNCOV
3083
      TSKEY cts = getRowPTs(pInput->pPTS, i);
×
UNCOV
3084
      numOfElems++;
×
3085

UNCOV
3086
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
×
UNCOV
3087
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
×
UNCOV
3088
        if (code != TSDB_CODE_SUCCESS) return code;
×
3089
      }
3090

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

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

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

UNCOV
3133
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
3134
  return TSDB_CODE_SUCCESS;
×
3135
}
3136

UNCOV
3137
bool getDiffFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
3138
  pEnv->calcMemSize = sizeof(SDiffInfo);
×
UNCOV
3139
  return true;
×
3140
}
3141

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

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

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

3249
  return false;
3250
}
3251

UNCOV
3252
static void tryToSetInt64(SDiffInfo* pDiffInfo, int32_t type, SColumnInfoData* pOutput, int64_t v, int32_t pos) {
×
UNCOV
3253
  bool isNegative = v < pDiffInfo->prev.i64;
×
UNCOV
3254
  if (type == TSDB_DATA_TYPE_UBIGINT) {
×
UNCOV
3255
    isNegative = (uint64_t)v < (uint64_t)pDiffInfo->prev.i64;
×
3256
  }
UNCOV
3257
  int64_t delta = v - pDiffInfo->prev.i64;
×
UNCOV
3258
  if (isNegative && ignoreNegative(pDiffInfo->ignoreOption)) {
×
UNCOV
3259
    colDataSetNull_f_s(pOutput, pos);
×
UNCOV
3260
    pOutput->hasNull = true;
×
3261
  } else {
UNCOV
3262
    colDataSetInt64(pOutput, pos, &delta);
×
3263
  }
UNCOV
3264
  pDiffInfo->prev.i64 = v;
×
UNCOV
3265
}
×
3266

UNCOV
3267
static void tryToSetDouble(SDiffInfo* pDiffInfo, SColumnInfoData* pOutput, double v, int32_t pos) {
×
UNCOV
3268
  double delta = v - pDiffInfo->prev.d64;
×
UNCOV
3269
  if (delta < 0 && ignoreNegative(pDiffInfo->ignoreOption)) {
×
UNCOV
3270
    colDataSetNull_f_s(pOutput, pos);
×
3271
  } else {
UNCOV
3272
    colDataSetDouble(pOutput, pos, &delta);
×
3273
  }
UNCOV
3274
  pDiffInfo->prev.d64 = v;
×
UNCOV
3275
}
×
3276

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

3344
// TODO: the primary key compare can be skipped for ordered pk if knonwn before
3345
// TODO: for desc ordered, pk shall select the smallest one for one ts. if across block boundaries.
UNCOV
3346
bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex,
×
3347
                              int32_t* nextFrom) {
UNCOV
3348
  if (pInput->pPrimaryKey == NULL) {
×
UNCOV
3349
    if (from == -1) {
×
UNCOV
3350
      from = pInput->startRowIndex;
×
UNCOV
3351
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
×
UNCOV
3352
      return false;
×
3353
    }
UNCOV
3354
    *pRowIndex = from;
×
UNCOV
3355
    *nextFrom = from + 1;
×
UNCOV
3356
    return true;
×
3357
  } else {
UNCOV
3358
    if (from == -1) {
×
UNCOV
3359
      from = pInput->startRowIndex;
×
UNCOV
3360
    } else if (from >= pInput->numOfRows + pInput->startRowIndex) {
×
UNCOV
3361
      return false;
×
3362
    }
UNCOV
3363
    TSKEY*           tsList = (int64_t*)pInput->pPTS->pData;
×
UNCOV
3364
    SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
3365
    int8_t           pkType = pkCol->info.type;
×
UNCOV
3366
    int32_t          order = (firstOccur) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
×
UNCOV
3367
    __compar_fn_t    compareFunc = getKeyComparFunc(pkType, order);
×
UNCOV
3368
    int32_t          select = from;
×
UNCOV
3369
    char*            val = colDataGetData(pkCol, select);
×
UNCOV
3370
    while (from < pInput->numOfRows + pInput->startRowIndex - 1 && tsList[from + 1] == tsList[from]) {
×
3371
      char* val1 = colDataGetData(pkCol, from + 1);
×
3372
      if (compareFunc(val1, val) < 0) {
×
UNCOV
3373
        select = from + 1;
×
UNCOV
3374
        val = val1;
×
3375
      }
UNCOV
3376
      from = from + 1;
×
3377
    }
UNCOV
3378
    *pRowIndex = select;
×
UNCOV
3379
    *nextFrom = from + 1;
×
UNCOV
3380
    return true;
×
3381
  }
3382
}
3383

UNCOV
3384
bool getForecastConfEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
3385
  pEnv->calcMemSize = sizeof(float);
×
UNCOV
3386
  return true;
×
3387
}
3388

UNCOV
3389
int32_t diffResultIsNull(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3390
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3391
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3392

UNCOV
3393
  if (pRow->isDataNull || !pDiffInfo->hasPrev) {
×
UNCOV
3394
    return true;
×
UNCOV
3395
  } else if (ignoreNegative(pDiffInfo->ignoreOption)) {
×
UNCOV
3396
    return diffIsNegtive(pDiffInfo, pCtx->input.pData[0]->info.type, pRow->pData);
×
3397
  }
UNCOV
3398
  return false;
×
3399
}
3400

UNCOV
3401
bool isFirstRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3402
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3403
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3404
  return pDiffInfo->isFirstRow;
×
3405
}
3406

UNCOV
3407
int32_t trySetPreVal(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3408
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3409
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3410
  pDiffInfo->isFirstRow = false;
×
UNCOV
3411
  if (pRow->isDataNull) {
×
UNCOV
3412
    return TSDB_CODE_SUCCESS;
×
3413
  }
3414

UNCOV
3415
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3416
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
3417
  int8_t                inputType = pInputCol->info.type;
×
3418

UNCOV
3419
  char* pv = pRow->pData;
×
UNCOV
3420
  return doSetPrevVal(pDiffInfo, inputType, pv, pRow->ts);
×
3421
}
3422

UNCOV
3423
int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) {
×
UNCOV
3424
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3425
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3426

UNCOV
3427
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3428
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
3429
  int8_t                inputType = pInputCol->info.type;
×
UNCOV
3430
  SColumnInfoData*      pOutput = (SColumnInfoData*)pCtx->pOutput;
×
UNCOV
3431
  int32_t               code = TSDB_CODE_SUCCESS;
×
UNCOV
3432
  if (pRow->isDataNull) {
×
UNCOV
3433
    colDataSetNull_f_s(pOutput, pos);
×
UNCOV
3434
    pOutput->hasNull = true;
×
3435

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

UNCOV
3446
  char* pv = pRow->pData;
×
3447

UNCOV
3448
  if (pRow->ts == pDiffInfo->prevTs) {
×
UNCOV
3449
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
3450
  }
3451
  code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts);
×
UNCOV
3452
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3453
    return code;
×
3454
  }
3455
  // handle selectivity
UNCOV
3456
  if (pCtx->subsidiaries.num > 0) {
×
3457
    code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
×
UNCOV
3458
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3459
      return code;
×
3460
    }
3461
  }
3462

UNCOV
3463
  return TSDB_CODE_SUCCESS;
×
3464
}
3465

UNCOV
3466
int32_t diffFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; }
×
3467

UNCOV
3468
int32_t diffFunctionByRow(SArray* pCtxArray) {
×
UNCOV
3469
  int32_t code = TSDB_CODE_SUCCESS;
×
3470
  int     diffColNum = pCtxArray->size;
×
UNCOV
3471
  if (diffColNum == 0) {
×
UNCOV
3472
    return TSDB_CODE_SUCCESS;
×
3473
  }
UNCOV
3474
  int32_t numOfElems = 0;
×
3475

3476
  SArray* pRows = taosArrayInit_s(sizeof(SFuncInputRow), diffColNum);
×
UNCOV
3477
  if (NULL == pRows) {
×
UNCOV
3478
    return terrno;
×
3479
  }
3480

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

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

UNCOV
3535
    bool newRow = false;
×
UNCOV
3536
    for (int i = 0; i < diffColNum; ++i) {
×
UNCOV
3537
      SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
×
3538
      SFuncInputRow*  pRow = (SFuncInputRow*)taosArrayGet(pRows, i);
×
3539
      if (NULL == pCtx || NULL == pRow) {
×
UNCOV
3540
        code = terrno;
×
UNCOV
3541
        goto _exit;
×
3542
      }
UNCOV
3543
      if ((keepNull || hasNotNullValue) && !isFirstRow(pCtx, pRow)) {
×
UNCOV
3544
        code = setDoDiffResult(pCtx, pRow, pos);
×
UNCOV
3545
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3546
          goto _exit;
×
3547
        }
UNCOV
3548
        newRow = true;
×
3549
      } else {
3550
        code = trySetPreVal(pCtx, pRow);
×
UNCOV
3551
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3552
          goto _exit;
×
3553
        }
3554
      }
3555
    }
UNCOV
3556
    if (newRow) ++numOfElems;
×
3557
  }
3558

UNCOV
3559
  for (int i = 0; i < diffColNum; ++i) {
×
3560
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
×
3561
    if (NULL == pCtx) {
×
UNCOV
3562
      code = terrno;
×
UNCOV
3563
      goto _exit;
×
3564
    }
UNCOV
3565
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3566
    pResInfo->numOfRes = numOfElems;
×
3567
  }
3568

UNCOV
3569
_exit:
×
UNCOV
3570
  if (pRows) {
×
UNCOV
3571
    taosArrayDestroy(pRows);
×
UNCOV
3572
    pRows = NULL;
×
3573
  }
UNCOV
3574
  return code;
×
3575
}
3576

UNCOV
3577
int32_t getTopBotInfoSize(int64_t numOfItems) { return sizeof(STopBotRes) + numOfItems * sizeof(STopBotResItem); }
×
3578

UNCOV
3579
bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
3580
  SValueNode* pkNode = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
×
UNCOV
3581
  pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem);
×
UNCOV
3582
  return true;
×
3583
}
3584

3585
int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
3586
  if (pResInfo->initialized) {
×
UNCOV
3587
    return TSDB_CODE_SUCCESS;
×
3588
  }
UNCOV
3589
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
UNCOV
3590
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3591
  }
3592

UNCOV
3593
  STopBotRes*           pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3594
  SInputColumnInfoData* pInput = &pCtx->input;
×
3595

UNCOV
3596
  pRes->maxSize = pCtx->param[1].param.i;
×
3597

UNCOV
3598
  pRes->nullTupleSaved = false;
×
UNCOV
3599
  pRes->nullTuplePos.pageId = -1;
×
UNCOV
3600
  return TSDB_CODE_SUCCESS;
×
3601
}
3602

UNCOV
3603
static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) {
×
UNCOV
3604
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3605
  STopBotRes*          pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3606
  pRes->pItems = (STopBotResItem*)((char*)pRes + sizeof(STopBotRes));
×
3607

UNCOV
3608
  return pRes;
×
3609
}
3610

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

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

UNCOV
3616
int32_t topFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3617
  int32_t              numOfElems = 0;
×
UNCOV
3618
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
3619

UNCOV
3620
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3621
  SColumnInfoData*      pCol = pInput->pData[0];
×
3622

UNCOV
3623
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3624
  pRes->type = pInput->pData[0]->info.type;
×
3625

UNCOV
3626
  int32_t start = pInput->startRowIndex;
×
UNCOV
3627
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
3628
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
3629
      continue;
×
3630
    }
3631

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

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

UNCOV
3650
int32_t bottomFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3651
  int32_t              numOfElems = 0;
×
UNCOV
3652
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
3653

UNCOV
3654
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3655
  SColumnInfoData*      pCol = pInput->pData[0];
×
3656

UNCOV
3657
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3658
  pRes->type = pInput->pData[0]->info.type;
×
3659

UNCOV
3660
  int32_t start = pInput->startRowIndex;
×
UNCOV
3661
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
3662
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
3663
      continue;
×
3664
    }
3665

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

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

UNCOV
3682
  return TSDB_CODE_SUCCESS;
×
3683
}
3684

UNCOV
3685
static int32_t topBotResComparFn(const void* p1, const void* p2, const void* param) {
×
UNCOV
3686
  uint16_t type = *(uint16_t*)param;
×
3687

UNCOV
3688
  STopBotResItem* val1 = (STopBotResItem*)p1;
×
UNCOV
3689
  STopBotResItem* val2 = (STopBotResItem*)p2;
×
3690

UNCOV
3691
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
3692
    if (val1->v.i == val2->v.i) {
×
UNCOV
3693
      return 0;
×
3694
    }
3695

UNCOV
3696
    return (val1->v.i > val2->v.i) ? 1 : -1;
×
UNCOV
3697
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
3698
    if (val1->v.u == val2->v.u) {
×
UNCOV
3699
      return 0;
×
3700
    }
3701

UNCOV
3702
    return (val1->v.u > val2->v.u) ? 1 : -1;
×
UNCOV
3703
  } else if (TSDB_DATA_TYPE_FLOAT == type) {
×
UNCOV
3704
    if (val1->v.f == val2->v.f) {
×
UNCOV
3705
      return 0;
×
3706
    }
3707

UNCOV
3708
    return (val1->v.f > val2->v.f) ? 1 : -1;
×
3709
  }
3710

UNCOV
3711
  if (val1->v.d == val2->v.d) {
×
UNCOV
3712
    return 0;
×
3713
  }
3714

UNCOV
3715
  return (val1->v.d > val2->v.d) ? 1 : -1;
×
3716
}
3717

UNCOV
3718
int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type,
×
3719
                        uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery) {
UNCOV
3720
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3721
  int32_t     code = TSDB_CODE_SUCCESS;
×
3722

UNCOV
3723
  SVariant val = {0};
×
UNCOV
3724
  TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type));
×
3725

UNCOV
3726
  STopBotResItem* pItems = pRes->pItems;
×
3727

3728
  // not full yet
UNCOV
3729
  if (pEntryInfo->numOfRes < pRes->maxSize) {
×
UNCOV
3730
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
×
UNCOV
3731
    pItem->v = val;
×
UNCOV
3732
    pItem->uid = uid;
×
3733

3734
    // save the data of this tuple
UNCOV
3735
    if (pCtx->subsidiaries.num > 0) {
×
3736
      code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos);
×
UNCOV
3737
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3738
        return code;
×
3739
      }
3740
    }
3741
#ifdef BUF_PAGE_DEBUG
3742
    qDebug("page_saveTuple i:%d, item:%p,pageId:%d, offset:%d\n", pEntryInfo->numOfRes, pItem, pItem->tuplePos.pageId,
3743
           pItem->tuplePos.offset);
3744
#endif
3745
    // allocate the buffer and keep the data of this row into the new allocated buffer
UNCOV
3746
    pEntryInfo->numOfRes++;
×
UNCOV
3747
    code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type,
×
3748
                        topBotResComparFn, !isTopQuery);
×
UNCOV
3749
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
3750
      return code;
×
3751
    }
3752
  } else {  // replace the minimum value in the result
UNCOV
3753
    if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) ||
×
UNCOV
3754
                        (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) ||
×
UNCOV
3755
                        (TSDB_DATA_TYPE_FLOAT == type && val.f > pItems[0].v.f) ||
×
UNCOV
3756
                        (TSDB_DATA_TYPE_DOUBLE == type && val.d > pItems[0].v.d))) ||
×
UNCOV
3757
        (!isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i < pItems[0].v.i) ||
×
UNCOV
3758
                         (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u < pItems[0].v.u) ||
×
UNCOV
3759
                         (TSDB_DATA_TYPE_FLOAT == type && val.f < pItems[0].v.f) ||
×
UNCOV
3760
                         (TSDB_DATA_TYPE_DOUBLE == type && val.d < pItems[0].v.d)))) {
×
3761
      // replace the old data and the coresponding tuple data
UNCOV
3762
      STopBotResItem* pItem = &pItems[0];
×
UNCOV
3763
      pItem->v = val;
×
UNCOV
3764
      pItem->uid = uid;
×
3765

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

UNCOV
3784
  return TSDB_CODE_SUCCESS;
×
3785
}
3786

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

UNCOV
3798
  int32_t offset = 0;
×
UNCOV
3799
  for (int32_t i = 0; i < pSubsidiaryies->num; ++i) {
×
UNCOV
3800
    SqlFunctionCtx* pc = pSubsidiaryies->pCtx[i];
×
3801

3802
    // group_key function has its own process function
3803
    // do not process there
UNCOV
3804
    if (fmIsGroupKeyFunc(pc->functionId)) {
×
UNCOV
3805
      continue;
×
3806
    }
3807

UNCOV
3808
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
×
UNCOV
3809
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
×
3810

3811
    SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
×
UNCOV
3812
    if (NULL == pCol) {
×
UNCOV
3813
      return TSDB_CODE_OUT_OF_RANGE;
×
3814
    }
UNCOV
3815
    if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) {
×
UNCOV
3816
      offset += pCol->info.bytes;
×
UNCOV
3817
      continue;
×
3818
    }
3819

UNCOV
3820
    char* p = colDataGetData(pCol, rowIndex);
×
UNCOV
3821
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
×
UNCOV
3822
      (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p));
×
3823
    } else {
UNCOV
3824
      (void)memcpy(pStart + offset, p, pCol->info.bytes);
×
3825
    }
3826

UNCOV
3827
    offset += pCol->info.bytes;
×
3828
  }
3829

UNCOV
3830
  *res = buf;
×
UNCOV
3831
  return TSDB_CODE_SUCCESS;
×
3832
}
3833

UNCOV
3834
static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, SWinKey* key,
×
3835
                               STuplePos* pPos, SFunctionStateStore* pStore) {
UNCOV
3836
  STuplePos p = {0};
×
UNCOV
3837
  if (pHandle->pBuf != NULL) {
×
UNCOV
3838
    SFilePage* pPage = NULL;
×
3839

UNCOV
3840
    if (pHandle->currentPage == -1) {
×
3841
      pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
×
UNCOV
3842
      if (pPage == NULL) {
×
UNCOV
3843
        return terrno;
×
3844
      }
UNCOV
3845
      pPage->num = sizeof(SFilePage);
×
3846
    } else {
3847
      pPage = getBufPage(pHandle->pBuf, pHandle->currentPage);
×
UNCOV
3848
      if (pPage == NULL) {
×
UNCOV
3849
        return terrno;
×
3850
      }
UNCOV
3851
      if (pPage->num + length > getBufPageSize(pHandle->pBuf)) {
×
3852
        // current page is all used, let's prepare a new buffer page
UNCOV
3853
        releaseBufPage(pHandle->pBuf, pPage);
×
3854
        pPage = getNewBufPage(pHandle->pBuf, &pHandle->currentPage);
×
UNCOV
3855
        if (pPage == NULL) {
×
UNCOV
3856
          return terrno;
×
3857
        }
UNCOV
3858
        pPage->num = sizeof(SFilePage);
×
3859
      }
3860
    }
3861

UNCOV
3862
    p = (STuplePos){.pageId = pHandle->currentPage, .offset = pPage->num};
×
UNCOV
3863
    (void)memcpy(pPage->data + pPage->num, pBuf, length);
×
3864

UNCOV
3865
    pPage->num += length;
×
UNCOV
3866
    setBufPageDirty(pPage, true);
×
UNCOV
3867
    releaseBufPage(pHandle->pBuf, pPage);
×
3868
  } else {  // other tuple save policy
UNCOV
3869
    if (pStore->streamStateFuncPut(pHandle->pState, key, pBuf, length) >= 0) {
×
UNCOV
3870
      p.streamTupleKey = *key;
×
3871
    }
3872
  }
3873

UNCOV
3874
  *pPos = p;
×
UNCOV
3875
  return TSDB_CODE_SUCCESS;
×
3876
}
3877

UNCOV
3878
int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
×
3879
  int32_t code = prepareBuf(pCtx);
×
UNCOV
3880
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
3881
    return code;
×
3882
  }
3883

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

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

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

UNCOV
3922
  return TSDB_CODE_SUCCESS;
×
3923
}
3924

UNCOV
3925
int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
×
3926
  int32_t code = prepareBuf(pCtx);
×
UNCOV
3927
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
3928
    return code;
×
3929
  }
3930

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

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

UNCOV
3961
int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value) {
×
UNCOV
3962
  return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore, value);
×
3963
}
3964

UNCOV
3965
int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
3966
  int32_t code = TSDB_CODE_SUCCESS;
×
3967

UNCOV
3968
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
3969
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
×
3970

UNCOV
3971
  int16_t type = pCtx->pExpr->base.resSchema.type;
×
UNCOV
3972
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
×
3973

3974
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
3975
  if (NULL == pCol) {
×
UNCOV
3976
    return TSDB_CODE_OUT_OF_RANGE;
×
3977
  }
3978

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

UNCOV
4003
  return code;
×
4004
}
4005

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

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

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

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

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

UNCOV
4079
int32_t getSpreadInfoSize() { return (int32_t)sizeof(SSpreadInfo); }
×
4080

UNCOV
4081
bool getSpreadFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4082
  pEnv->calcMemSize = sizeof(SSpreadInfo);
×
UNCOV
4083
  return true;
×
4084
}
4085

4086
int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4087
  if (pResultInfo->initialized) {
×
UNCOV
4088
    return TSDB_CODE_SUCCESS;
×
4089
  }
UNCOV
4090
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
4091
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4092
  }
4093

UNCOV
4094
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4095
  SET_DOUBLE_VAL(&pInfo->min, DBL_MAX);
×
UNCOV
4096
  SET_DOUBLE_VAL(&pInfo->max, -DBL_MAX);
×
UNCOV
4097
  pInfo->hasResult = false;
×
UNCOV
4098
  return TSDB_CODE_SUCCESS;
×
4099
}
4100

UNCOV
4101
int32_t spreadFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4102
  int32_t numOfElems = 0;
×
4103

4104
  // Only the pre-computing information loaded and actual data does not loaded
UNCOV
4105
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4106
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
UNCOV
4107
  int32_t               type = pInput->pData[0]->info.type;
×
4108

UNCOV
4109
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4110

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

UNCOV
4128
    if (GET_DOUBLE_VAL(&pInfo->min) > tmin) {
×
UNCOV
4129
      SET_DOUBLE_VAL(&pInfo->min, tmin);
×
4130
    }
4131

UNCOV
4132
    if (GET_DOUBLE_VAL(&pInfo->max) < tmax) {
×
UNCOV
4133
      SET_DOUBLE_VAL(&pInfo->max, tmax);
×
4134
    }
4135

4136
  } else {  // computing based on the true data block
UNCOV
4137
    SColumnInfoData* pCol = pInput->pData[0];
×
4138

UNCOV
4139
    int32_t start = pInput->startRowIndex;
×
4140
    // check the valid data one by one
UNCOV
4141
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
4142
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
4143
        continue;
×
4144
      }
4145

UNCOV
4146
      char* data = colDataGetData(pCol, i);
×
4147

UNCOV
4148
      double v = 0;
×
UNCOV
4149
      GET_TYPED_DATA(v, double, type, data);
×
UNCOV
4150
      if (v < GET_DOUBLE_VAL(&pInfo->min)) {
×
UNCOV
4151
        SET_DOUBLE_VAL(&pInfo->min, v);
×
4152
      }
4153

UNCOV
4154
      if (v > GET_DOUBLE_VAL(&pInfo->max)) {
×
UNCOV
4155
        SET_DOUBLE_VAL(&pInfo->max, v);
×
4156
      }
4157

UNCOV
4158
      numOfElems += 1;
×
4159
    }
4160
  }
4161

UNCOV
4162
_spread_over:
×
4163
  // data in the check operation are all null, not output
UNCOV
4164
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
4165
  if (numOfElems > 0) {
×
UNCOV
4166
    pInfo->hasResult = true;
×
4167
  }
4168

UNCOV
4169
  return TSDB_CODE_SUCCESS;
×
4170
}
4171

UNCOV
4172
static void spreadTransferInfo(SSpreadInfo* pInput, SSpreadInfo* pOutput) {
×
UNCOV
4173
  pOutput->hasResult = pInput->hasResult;
×
UNCOV
4174
  if (pInput->max > pOutput->max) {
×
UNCOV
4175
    pOutput->max = pInput->max;
×
4176
  }
4177

UNCOV
4178
  if (pInput->min < pOutput->min) {
×
UNCOV
4179
    pOutput->min = pInput->min;
×
4180
  }
UNCOV
4181
}
×
4182

UNCOV
4183
int32_t spreadFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
4184
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4185
  SColumnInfoData*      pCol = pInput->pData[0];
×
4186

4187
  if (IS_NULL_TYPE(pCol->info.type)) {
×
UNCOV
4188
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
UNCOV
4189
    return TSDB_CODE_SUCCESS;
×
4190
  }
4191

UNCOV
4192
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
4193
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4194
  }
4195

UNCOV
4196
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4197

UNCOV
4198
  int32_t start = pInput->startRowIndex;
×
UNCOV
4199
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
4200
    if (colDataIsNull_s(pCol, i)) continue;
×
UNCOV
4201
    char*        data = colDataGetData(pCol, i);
×
UNCOV
4202
    SSpreadInfo* pInputInfo = (SSpreadInfo*)varDataVal(data);
×
UNCOV
4203
    if (pInputInfo->hasResult) {
×
UNCOV
4204
      spreadTransferInfo(pInputInfo, pInfo);
×
4205
    }
4206
  }
4207

UNCOV
4208
  if (pInfo->hasResult) {
×
UNCOV
4209
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
4210
  }
4211

UNCOV
4212
  return TSDB_CODE_SUCCESS;
×
4213
}
4214

UNCOV
4215
int32_t spreadFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4216
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4217
  if (pInfo->hasResult == true) {
×
UNCOV
4218
    SET_DOUBLE_VAL(&pInfo->result, pInfo->max - pInfo->min);
×
4219
  } else {
UNCOV
4220
    GET_RES_INFO(pCtx)->isNullRes = 1;
×
4221
  }
UNCOV
4222
  return functionFinalize(pCtx, pBlock);
×
4223
}
4224

UNCOV
4225
int32_t spreadPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4226
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
4227
  SSpreadInfo*         pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4228
  int32_t              resultBytes = getSpreadInfoSize();
×
UNCOV
4229
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4230

UNCOV
4231
  if (NULL == res) {
×
UNCOV
4232
    return terrno;
×
4233
  }
UNCOV
4234
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
4235
  varDataSetLen(res, resultBytes);
×
4236

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

4245
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
UNCOV
4246
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
4247
    goto _exit;
×
4248
  }
4249

UNCOV
4250
_exit:
×
UNCOV
4251
  taosMemoryFree(res);
×
UNCOV
4252
  return code;
×
4253
}
4254

UNCOV
4255
int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
4256
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
4257
  SSpreadInfo*         pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4258

UNCOV
4259
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
4260
  SSpreadInfo*         pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
4261
  spreadTransferInfo(pSBuf, pDBuf);
×
UNCOV
4262
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
4263
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
4264
  return TSDB_CODE_SUCCESS;
×
4265
}
4266

UNCOV
4267
int32_t getElapsedInfoSize() { return (int32_t)sizeof(SElapsedInfo); }
×
4268

UNCOV
4269
bool getElapsedFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4270
  pEnv->calcMemSize = sizeof(SElapsedInfo);
×
UNCOV
4271
  return true;
×
4272
}
4273

4274
int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4275
  if (pResultInfo->initialized) {
×
UNCOV
4276
    return TSDB_CODE_SUCCESS;
×
4277
  }
UNCOV
4278
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
4279
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4280
  }
4281

UNCOV
4282
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4283
  pInfo->result = 0;
×
UNCOV
4284
  pInfo->min = TSKEY_MAX;
×
UNCOV
4285
  pInfo->max = 0;
×
4286

UNCOV
4287
  if (pCtx->numOfParams > 1) {
×
UNCOV
4288
    pInfo->timeUnit = pCtx->param[1].param.i;
×
4289
  } else {
UNCOV
4290
    pInfo->timeUnit = 1;
×
4291
  }
4292

UNCOV
4293
  return TSDB_CODE_SUCCESS;
×
4294
}
4295

UNCOV
4296
int32_t elapsedFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4297
  int32_t numOfElems = 0;
×
4298

4299
  // Only the pre-computing information loaded and actual data does not loaded
UNCOV
4300
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4301
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
4302

UNCOV
4303
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4304

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

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

UNCOV
4339
    SColumnInfoData* pCol = pInput->pData[0];
×
4340

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

UNCOV
4350
      if (pCtx->end.key == INT64_MIN) {
×
UNCOV
4351
        pInfo->min =
×
4352
            (pInfo->min > ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->min;
×
4353
      } else {
UNCOV
4354
        pInfo->min = pCtx->end.key;
×
4355
      }
4356
    } else {
UNCOV
4357
      if (pCtx->start.key == INT64_MIN) {
×
UNCOV
4358
        pInfo->min = (pInfo->min > ptsList[start]) ? ptsList[start] : pInfo->min;
×
4359
      } else {
UNCOV
4360
        pInfo->min = pCtx->start.key;
×
4361
      }
4362

UNCOV
4363
      if (pCtx->end.key == INT64_MIN) {
×
UNCOV
4364
        pInfo->max =
×
UNCOV
4365
            (pInfo->max < ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->max;
×
4366
      } else {
UNCOV
4367
        pInfo->max = pCtx->end.key + 1;
×
4368
      }
4369
    }
4370
  }
4371

UNCOV
4372
_elapsed_over:
×
4373
  // data in the check operation are all null, not output
UNCOV
4374
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
4375

UNCOV
4376
  return TSDB_CODE_SUCCESS;
×
4377
}
4378

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

UNCOV
4385
  if (pOutput->max < pInput->max) {
×
4386
    pOutput->max = pInput->max;
×
4387
  }
4388
}
×
4389

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

4397
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4398

4399
  int32_t start = pInput->startRowIndex;
×
4400

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

UNCOV
4407
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
UNCOV
4408
  return TSDB_CODE_SUCCESS;
×
4409
}
4410

UNCOV
4411
int32_t elapsedFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4412
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4413
  double        result = (double)pInfo->max - (double)pInfo->min;
×
UNCOV
4414
  result = (result >= 0) ? result : -result;
×
UNCOV
4415
  pInfo->result = result / pInfo->timeUnit;
×
UNCOV
4416
  return functionFinalize(pCtx, pBlock);
×
4417
}
4418

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

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

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

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

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

UNCOV
4452
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4453
  SElapsedInfo*        pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4454

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

UNCOV
4461
int32_t getHistogramInfoSize() {
×
UNCOV
4462
  return (int32_t)sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
×
4463
}
4464

UNCOV
4465
bool getHistogramFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4466
  pEnv->calcMemSize = sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
×
UNCOV
4467
  return true;
×
4468
}
4469

UNCOV
4470
static int8_t getHistogramBinType(char* binTypeStr) {
×
4471
  int8_t binType;
UNCOV
4472
  if (strcasecmp(binTypeStr, "user_input") == 0) {
×
UNCOV
4473
    binType = USER_INPUT_BIN;
×
UNCOV
4474
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
×
UNCOV
4475
    binType = LINEAR_BIN;
×
UNCOV
4476
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
×
4477
    binType = LOG_BIN;
×
4478
  } else {
UNCOV
4479
    binType = UNKNOWN_BIN;
×
4480
  }
4481

UNCOV
4482
  return binType;
×
4483
}
4484

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

UNCOV
4497
    cJSON* start = cJSON_GetObjectItem(binDesc, "start");
×
UNCOV
4498
    cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
×
UNCOV
4499
    cJSON* width = cJSON_GetObjectItem(binDesc, "width");
×
UNCOV
4500
    cJSON* count = cJSON_GetObjectItem(binDesc, "count");
×
UNCOV
4501
    cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
×
4502

4503
    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
×
UNCOV
4504
      cJSON_Delete(binDesc);
×
UNCOV
4505
      return TSDB_CODE_FAILED;
×
4506
    }
4507

4508
    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
×
UNCOV
4509
      cJSON_Delete(binDesc);
×
UNCOV
4510
      return TSDB_CODE_FAILED;
×
4511
    }
4512

4513
    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
×
4514
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
×
UNCOV
4515
      cJSON_Delete(binDesc);
×
UNCOV
4516
      return TSDB_CODE_FAILED;
×
4517
    }
4518

UNCOV
4519
    int32_t counter = (int32_t)count->valueint;
×
UNCOV
4520
    if (infinity->valueint == false) {
×
UNCOV
4521
      startIndex = 0;
×
UNCOV
4522
      numOfBins = counter + 1;
×
4523
    } else {
UNCOV
4524
      startIndex = 1;
×
UNCOV
4525
      numOfBins = counter + 3;
×
4526
    }
4527

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

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

UNCOV
4627
  pInfo->numOfBins = numOfBins - 1;
×
UNCOV
4628
  pInfo->normalized = normalized;
×
UNCOV
4629
  for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
×
UNCOV
4630
    pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
×
UNCOV
4631
    pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
×
UNCOV
4632
    pInfo->bins[i].count = 0;
×
4633
  }
4634

UNCOV
4635
  taosMemoryFree(intervals);
×
UNCOV
4636
  cJSON_Delete(binDesc);
×
4637

UNCOV
4638
  return TSDB_CODE_SUCCESS;
×
4639
}
4640

4641
int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4642
  if (pResultInfo->initialized) {
×
UNCOV
4643
    return TSDB_CODE_SUCCESS;
×
4644
  }
UNCOV
4645
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
4646
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4647
  }
4648

UNCOV
4649
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4650
  pInfo->numOfBins = 0;
×
UNCOV
4651
  pInfo->totalCount = 0;
×
UNCOV
4652
  pInfo->normalized = 0;
×
4653

4654
  char* binTypeStr = taosStrndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz));
×
UNCOV
4655
  if (binTypeStr == NULL) {
×
UNCOV
4656
    return terrno;
×
4657
  }
UNCOV
4658
  int8_t binType = getHistogramBinType(binTypeStr);
×
UNCOV
4659
  taosMemoryFree(binTypeStr);
×
4660

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

UNCOV
4680
  return TSDB_CODE_SUCCESS;
×
4681
}
4682

UNCOV
4683
static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) {
×
UNCOV
4684
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4685

UNCOV
4686
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4687
  SColumnInfoData*      pCol = pInput->pData[0];
×
4688

UNCOV
4689
  int32_t type = pInput->pData[0]->info.type;
×
4690

UNCOV
4691
  int32_t start = pInput->startRowIndex;
×
UNCOV
4692
  int32_t numOfRows = pInput->numOfRows;
×
4693

UNCOV
4694
  int32_t numOfElems = 0;
×
UNCOV
4695
  for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
4696
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
4697
      continue;
×
4698
    }
4699

UNCOV
4700
    numOfElems++;
×
4701

UNCOV
4702
    char*  data = colDataGetData(pCol, i);
×
4703
    double v;
UNCOV
4704
    GET_TYPED_DATA(v, double, type, data);
×
4705

UNCOV
4706
    for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
×
UNCOV
4707
      if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
×
UNCOV
4708
        pInfo->bins[k].count++;
×
UNCOV
4709
        pInfo->totalCount++;
×
UNCOV
4710
        break;
×
4711
      }
4712
    }
4713
  }
4714

UNCOV
4715
  if (!isPartial) {
×
UNCOV
4716
    GET_RES_INFO(pCtx)->numOfRes = pInfo->numOfBins;
×
4717
  } else {
UNCOV
4718
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
4719
  }
UNCOV
4720
  return TSDB_CODE_SUCCESS;
×
4721
}
4722

UNCOV
4723
int32_t histogramFunction(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, false); }
×
4724

UNCOV
4725
int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx) { return histogramFunctionImpl(pCtx, true); }
×
4726

UNCOV
4727
static void histogramTransferInfo(SHistoFuncInfo* pInput, SHistoFuncInfo* pOutput) {
×
UNCOV
4728
  pOutput->normalized = pInput->normalized;
×
UNCOV
4729
  pOutput->numOfBins = pInput->numOfBins;
×
UNCOV
4730
  pOutput->totalCount += pInput->totalCount;
×
UNCOV
4731
  for (int32_t k = 0; k < pOutput->numOfBins; ++k) {
×
UNCOV
4732
    pOutput->bins[k].lower = pInput->bins[k].lower;
×
UNCOV
4733
    pOutput->bins[k].upper = pInput->bins[k].upper;
×
UNCOV
4734
    pOutput->bins[k].count += pInput->bins[k].count;
×
4735
  }
UNCOV
4736
}
×
4737

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

UNCOV
4745
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4746

UNCOV
4747
  int32_t start = pInput->startRowIndex;
×
4748

UNCOV
4749
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
4750
    char*           data = colDataGetData(pCol, i);
×
UNCOV
4751
    SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data);
×
UNCOV
4752
    histogramTransferInfo(pInputInfo, pInfo);
×
4753
  }
4754

UNCOV
4755
  SET_VAL(GET_RES_INFO(pCtx), pInfo->numOfBins, pInfo->numOfBins);
×
UNCOV
4756
  return TSDB_CODE_SUCCESS;
×
4757
}
4758

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

4766
  int32_t currentRow = pBlock->info.rows;
×
UNCOV
4767
  if (NULL == pCol) {
×
UNCOV
4768
    return TSDB_CODE_OUT_OF_RANGE;
×
4769
  }
4770

UNCOV
4771
  if (pInfo->normalized) {
×
UNCOV
4772
    for (int32_t k = 0; k < pResInfo->numOfRes; ++k) {
×
UNCOV
4773
      if (pInfo->totalCount != 0) {
×
UNCOV
4774
        pInfo->bins[k].percentage = pInfo->bins[k].count / (double)pInfo->totalCount;
×
4775
      } else {
UNCOV
4776
        pInfo->bins[k].percentage = 0;
×
4777
      }
4778
    }
4779
  }
4780

UNCOV
4781
  for (int32_t i = 0; i < pResInfo->numOfRes; ++i) {
×
4782
    int32_t len;
UNCOV
4783
    char    buf[512] = {0};
×
UNCOV
4784
    if (!pInfo->normalized) {
×
UNCOV
4785
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE,
×
4786
                      "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%" PRId64 "}", pInfo->bins[i].lower,
4787
                      pInfo->bins[i].upper, pInfo->bins[i].count);
4788
    } else {
UNCOV
4789
      len = tsnprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE,
×
4790
                      "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%lf}", pInfo->bins[i].lower, pInfo->bins[i].upper,
4791
                      pInfo->bins[i].percentage);
4792
    }
UNCOV
4793
    varDataSetLen(buf, len);
×
4794
    code = colDataSetVal(pCol, currentRow, buf, false);
×
UNCOV
4795
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
4796
      return code;
×
4797
    }
UNCOV
4798
    currentRow++;
×
4799
  }
4800

UNCOV
4801
  return code;
×
4802
}
4803

UNCOV
4804
int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4805
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
4806
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4807
  int32_t              resultBytes = getHistogramInfoSize();
×
UNCOV
4808
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4809

UNCOV
4810
  if (NULL == res) {
×
UNCOV
4811
    return terrno;
×
4812
  }
UNCOV
4813
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
4814
  varDataSetLen(res, resultBytes);
×
4815

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

UNCOV
4825
_exit:
×
UNCOV
4826
  taosMemoryFree(res);
×
UNCOV
4827
  return code;
×
4828
}
4829

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

UNCOV
4834
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4835
  SHistoFuncInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4836

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

UNCOV
4843
int32_t getHLLInfoSize() { return (int32_t)sizeof(SHLLInfo); }
×
4844

UNCOV
4845
bool getHLLFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4846
  pEnv->calcMemSize = sizeof(SHLLInfo);
×
UNCOV
4847
  return true;
×
4848
}
4849

UNCOV
4850
static uint8_t hllCountNum(void* data, int32_t bytes, int32_t* buk) {
×
UNCOV
4851
  uint64_t hash = MurmurHash3_64(data, bytes);
×
UNCOV
4852
  int32_t  index = hash & HLL_BUCKET_MASK;
×
UNCOV
4853
  hash >>= HLL_BUCKET_BITS;
×
UNCOV
4854
  hash |= ((uint64_t)1 << HLL_DATA_BITS);
×
UNCOV
4855
  uint64_t bit = 1;
×
UNCOV
4856
  uint8_t  count = 1;
×
UNCOV
4857
  while ((hash & bit) == 0) {
×
UNCOV
4858
    count++;
×
UNCOV
4859
    bit <<= 1;
×
4860
  }
UNCOV
4861
  *buk = index;
×
UNCOV
4862
  return count;
×
4863
}
4864

UNCOV
4865
static void hllBucketHisto(uint8_t* buckets, int32_t* bucketHisto) {
×
UNCOV
4866
  uint64_t* word = (uint64_t*)buckets;
×
4867
  uint8_t*  bytes;
4868

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

UNCOV
4900
static double hllSigma(double x) {
×
UNCOV
4901
  if (x == 1.0) return INFINITY;
×
4902
  double zPrime;
UNCOV
4903
  double y = 1;
×
UNCOV
4904
  double z = x;
×
4905
  do {
UNCOV
4906
    x *= x;
×
UNCOV
4907
    zPrime = z;
×
UNCOV
4908
    z += x * y;
×
UNCOV
4909
    y += y;
×
UNCOV
4910
  } while (zPrime != z);
×
UNCOV
4911
  return z;
×
4912
}
4913

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

UNCOV
4921
  double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m);
×
UNCOV
4922
  for (int j = HLL_DATA_BITS; j >= 1; --j) {
×
UNCOV
4923
    z += buckethisto[j];
×
UNCOV
4924
    z *= 0.5;
×
4925
  }
4926

UNCOV
4927
  z += m * hllSigma(buckethisto[0] / (double)m);
×
UNCOV
4928
  double E = (double)llroundl(HLL_ALPHA_INF * m * m / z);
×
4929

UNCOV
4930
  return (uint64_t)E;
×
4931
}
4932

UNCOV
4933
int32_t hllFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4934
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4935

UNCOV
4936
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4937
  SColumnInfoData*      pCol = pInput->pData[0];
×
4938

UNCOV
4939
  int32_t type = pCol->info.type;
×
UNCOV
4940
  int32_t bytes = pCol->info.bytes;
×
4941

UNCOV
4942
  int32_t start = pInput->startRowIndex;
×
UNCOV
4943
  int32_t numOfRows = pInput->numOfRows;
×
4944

UNCOV
4945
  int32_t numOfElems = 0;
×
UNCOV
4946
  if (IS_NULL_TYPE(type)) {
×
UNCOV
4947
    goto _hll_over;
×
4948
  }
4949

UNCOV
4950
  for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
4951
    if (pCol->hasNull && colDataIsNull_s(pCol, i)) {
×
UNCOV
4952
      continue;
×
4953
    }
4954

UNCOV
4955
    numOfElems++;
×
4956

UNCOV
4957
    char* data = colDataGetData(pCol, i);
×
UNCOV
4958
    if (IS_VAR_DATA_TYPE(type)) {
×
UNCOV
4959
      bytes = varDataLen(data);
×
UNCOV
4960
      data = varDataVal(data);
×
4961
    }
4962

UNCOV
4963
    int32_t index = 0;
×
UNCOV
4964
    uint8_t count = hllCountNum(data, bytes, &index);
×
UNCOV
4965
    uint8_t oldcount = pInfo->buckets[index];
×
UNCOV
4966
    if (count > oldcount) {
×
UNCOV
4967
      pInfo->buckets[index] = count;
×
4968
    }
4969
  }
4970

UNCOV
4971
_hll_over:
×
UNCOV
4972
  pInfo->totalCount += numOfElems;
×
4973

UNCOV
4974
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
×
UNCOV
4975
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4976
  } else {
UNCOV
4977
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
4978
  }
4979

UNCOV
4980
  return TSDB_CODE_SUCCESS;
×
4981
}
4982

UNCOV
4983
static void hllTransferInfo(SHLLInfo* pInput, SHLLInfo* pOutput) {
×
UNCOV
4984
  for (int32_t k = 0; k < HLL_BUCKETS; ++k) {
×
UNCOV
4985
    if (pOutput->buckets[k] < pInput->buckets[k]) {
×
UNCOV
4986
      pOutput->buckets[k] = pInput->buckets[k];
×
4987
    }
4988
  }
UNCOV
4989
  pOutput->totalCount += pInput->totalCount;
×
UNCOV
4990
}
×
4991

UNCOV
4992
int32_t hllFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
4993
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4994
  SColumnInfoData*      pCol = pInput->pData[0];
×
4995

4996
  if (IS_NULL_TYPE(pCol->info.type)) {
×
UNCOV
4997
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
UNCOV
4998
    return TSDB_CODE_SUCCESS;
×
4999
  }
5000

UNCOV
5001
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
5002
    return TSDB_CODE_SUCCESS;
×
5003
  }
5004

UNCOV
5005
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
5006

UNCOV
5007
  int32_t start = pInput->startRowIndex;
×
5008

UNCOV
5009
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
5010
    if (colDataIsNull_s(pCol, i)) continue;
×
UNCOV
5011
    char*     data = colDataGetData(pCol, i);
×
UNCOV
5012
    SHLLInfo* pInputInfo = (SHLLInfo*)varDataVal(data);
×
UNCOV
5013
    hllTransferInfo(pInputInfo, pInfo);
×
5014
  }
5015

UNCOV
5016
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
×
UNCOV
5017
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
5018
  } else {
UNCOV
5019
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
5020
  }
5021

UNCOV
5022
  return TSDB_CODE_SUCCESS;
×
5023
}
5024

UNCOV
5025
int32_t hllFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5026
  SResultRowEntryInfo* pInfo = GET_RES_INFO(pCtx);
×
5027

UNCOV
5028
  SHLLInfo* pHllInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
5029
  pHllInfo->result = hllCountCnt(pHllInfo->buckets);
×
UNCOV
5030
  if (tsCountAlwaysReturnValue && pHllInfo->result == 0) {
×
UNCOV
5031
    pInfo->numOfRes = 1;
×
5032
  }
5033

UNCOV
5034
  return functionFinalize(pCtx, pBlock);
×
5035
}
5036

UNCOV
5037
int32_t hllPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5038
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5039
  SHLLInfo*            pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
5040
  int32_t              resultBytes = getHLLInfoSize();
×
UNCOV
5041
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
5042

UNCOV
5043
  if (NULL == res) {
×
UNCOV
5044
    return terrno;
×
5045
  }
UNCOV
5046
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
5047
  varDataSetLen(res, resultBytes);
×
5048

UNCOV
5049
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
5050
  int32_t          code = TSDB_CODE_SUCCESS;
×
5051
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
5052
  if (NULL == pCol) {
×
UNCOV
5053
    code = terrno;
×
UNCOV
5054
    goto _exit;
×
5055
  }
5056

UNCOV
5057
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
5058

UNCOV
5059
_exit:
×
UNCOV
5060
  taosMemoryFree(res);
×
UNCOV
5061
  return code;
×
5062
}
5063

UNCOV
5064
int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
5065
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
5066
  SHLLInfo*            pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
5067

UNCOV
5068
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
5069
  SHLLInfo*            pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
5070

UNCOV
5071
  hllTransferInfo(pSBuf, pDBuf);
×
UNCOV
5072
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
5073
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
5074
  return TSDB_CODE_SUCCESS;
×
5075
}
5076

UNCOV
5077
bool getStateFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5078
  pEnv->calcMemSize = sizeof(SStateInfo);
×
UNCOV
5079
  return true;
×
5080
}
5081

UNCOV
5082
static int8_t getStateOpType(char* opStr) {
×
5083
  int8_t opType;
UNCOV
5084
  if (strncasecmp(opStr, "LT", 2) == 0) {
×
UNCOV
5085
    opType = STATE_OPER_LT;
×
UNCOV
5086
  } else if (strncasecmp(opStr, "GT", 2) == 0) {
×
UNCOV
5087
    opType = STATE_OPER_GT;
×
UNCOV
5088
  } else if (strncasecmp(opStr, "LE", 2) == 0) {
×
UNCOV
5089
    opType = STATE_OPER_LE;
×
UNCOV
5090
  } else if (strncasecmp(opStr, "GE", 2) == 0) {
×
UNCOV
5091
    opType = STATE_OPER_GE;
×
UNCOV
5092
  } else if (strncasecmp(opStr, "NE", 2) == 0) {
×
UNCOV
5093
    opType = STATE_OPER_NE;
×
UNCOV
5094
  } else if (strncasecmp(opStr, "EQ", 2) == 0) {
×
5095
    opType = STATE_OPER_EQ;
×
5096
  } else {
UNCOV
5097
    opType = STATE_OPER_INVALID;
×
5098
  }
5099

UNCOV
5100
  return opType;
×
5101
}
5102

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

UNCOV
5163
int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5164
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5165
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5166
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5167

UNCOV
5168
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5169
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5170

UNCOV
5171
  SColumnInfoData* pInputCol = pInput->pData[0];
×
5172

UNCOV
5173
  int32_t          numOfElems = 0;
×
UNCOV
5174
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5175

5176
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
×
UNCOV
5177
  if (STATE_OPER_INVALID == op) {
×
UNCOV
5178
    return 0;
×
5179
  }
5180

5181
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5182
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
×
UNCOV
5183
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5184
    } else {
UNCOV
5185
      pInfo->prevTs = tsList[i];
×
5186
    }
5187

UNCOV
5188
    pInfo->isPrevTsSet = true;
×
UNCOV
5189
    numOfElems++;
×
5190

UNCOV
5191
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
UNCOV
5192
      colDataSetNULL(pOutput, i);
×
5193
      // handle selectivity
UNCOV
5194
      if (pCtx->subsidiaries.num > 0) {
×
5195
        code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
×
UNCOV
5196
        if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5197
          return code;
×
5198
        }
5199
      }
UNCOV
5200
      continue;
×
5201
    }
5202

UNCOV
5203
    bool ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
×
5204

UNCOV
5205
    int64_t output = -1;
×
UNCOV
5206
    if (ret) {
×
UNCOV
5207
      output = ++pInfo->count;
×
5208
    } else {
UNCOV
5209
      pInfo->count = 0;
×
5210
    }
5211
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
×
UNCOV
5212
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5213
      return code;
×
5214
    }
5215

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

UNCOV
5225
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5226
  return TSDB_CODE_SUCCESS;
×
5227
}
5228

UNCOV
5229
int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5230
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5231
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5232
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5233

UNCOV
5234
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5235
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5236

UNCOV
5237
  SColumnInfoData* pInputCol = pInput->pData[0];
×
5238

UNCOV
5239
  int32_t          numOfElems = 0;
×
UNCOV
5240
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5241

5242
  // TODO: process timeUnit for different db precisions
UNCOV
5243
  int32_t timeUnit = 1;
×
UNCOV
5244
  if (pCtx->numOfParams == 5) {  // TODO: param number incorrect
×
UNCOV
5245
    timeUnit = pCtx->param[3].param.i;
×
5246
  }
5247

5248
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
×
UNCOV
5249
  if (STATE_OPER_INVALID == op) {
×
UNCOV
5250
    return TSDB_CODE_INVALID_PARA;
×
5251
  }
5252

5253
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5254
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
×
UNCOV
5255
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5256
    } else {
UNCOV
5257
      pInfo->prevTs = tsList[i];
×
5258
    }
5259

UNCOV
5260
    pInfo->isPrevTsSet = true;
×
UNCOV
5261
    numOfElems++;
×
5262

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

UNCOV
5275
    bool    ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
×
UNCOV
5276
    int64_t output = -1;
×
UNCOV
5277
    if (ret) {
×
UNCOV
5278
      if (pInfo->durationStart == 0) {
×
UNCOV
5279
        output = 0;
×
UNCOV
5280
        pInfo->durationStart = tsList[i];
×
5281
      } else {
UNCOV
5282
        output = (tsList[i] - pInfo->durationStart) / timeUnit;
×
5283
      }
5284
    } else {
UNCOV
5285
      pInfo->durationStart = 0;
×
5286
    }
5287
    code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false);
×
UNCOV
5288
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5289
      return code;
×
5290
    }
5291

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

UNCOV
5301
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5302
  return TSDB_CODE_SUCCESS;
×
5303
}
5304

UNCOV
5305
bool getCsumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5306
  pEnv->calcMemSize = sizeof(SSumRes);
×
UNCOV
5307
  return true;
×
5308
}
5309

UNCOV
5310
int32_t csumFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5311
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5312
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5313
  SSumRes*             pSumRes = GET_ROWCELL_INTERBUF(pResInfo);
×
5314

UNCOV
5315
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5316
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5317

UNCOV
5318
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5319
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5320

UNCOV
5321
  int32_t numOfElems = 0;
×
UNCOV
5322
  int32_t type = pInputCol->info.type;
×
UNCOV
5323
  int32_t startOffset = pCtx->offset;
×
UNCOV
5324
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5325
    if (pSumRes->isPrevTsSet == true && tsList[i] == pSumRes->prevTs) {
×
UNCOV
5326
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5327
    } else {
UNCOV
5328
      pSumRes->prevTs = tsList[i];
×
5329
    }
UNCOV
5330
    pSumRes->isPrevTsSet = true;
×
5331

UNCOV
5332
    int32_t pos = startOffset + numOfElems;
×
UNCOV
5333
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
5334
      // colDataSetNULL(pOutput, i);
UNCOV
5335
      continue;
×
5336
    }
5337

UNCOV
5338
    char* data = colDataGetData(pInputCol, i);
×
UNCOV
5339
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
5340
      int64_t v;
UNCOV
5341
      GET_TYPED_DATA(v, int64_t, type, data);
×
UNCOV
5342
      pSumRes->isum += v;
×
5343
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false);
×
UNCOV
5344
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5345
        return code;
×
5346
      }
UNCOV
5347
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
×
5348
      uint64_t v;
UNCOV
5349
      GET_TYPED_DATA(v, uint64_t, type, data);
×
UNCOV
5350
      pSumRes->usum += v;
×
5351
      code = colDataSetVal(pOutput, pos, (char*)&pSumRes->usum, false);
×
UNCOV
5352
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5353
        return code;
×
5354
      }
UNCOV
5355
    } else if (IS_FLOAT_TYPE(type)) {
×
5356
      double v;
UNCOV
5357
      GET_TYPED_DATA(v, double, type, data);
×
UNCOV
5358
      pSumRes->dsum += v;
×
5359
      // check for overflow
UNCOV
5360
      if (isinf(pSumRes->dsum) || isnan(pSumRes->dsum)) {
×
UNCOV
5361
        colDataSetNULL(pOutput, pos);
×
5362
      } else {
5363
        code = colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false);
×
UNCOV
5364
        if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5365
          return code;
×
5366
        }
5367
      }
5368
    }
5369

5370
    // handle selectivity
UNCOV
5371
    if (pCtx->subsidiaries.num > 0) {
×
5372
      code = appendSelectivityValue(pCtx, i, pos);
×
UNCOV
5373
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5374
        return code;
×
5375
      }
5376
    }
5377

UNCOV
5378
    numOfElems++;
×
5379
  }
5380

UNCOV
5381
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5382
  return TSDB_CODE_SUCCESS;
×
5383
}
5384

UNCOV
5385
bool getMavgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5386
  pEnv->calcMemSize = sizeof(SMavgInfo) + MAVG_MAX_POINTS_NUM * sizeof(double);
×
UNCOV
5387
  return true;
×
5388
}
5389

UNCOV
5390
int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
5391
  if (pResultInfo->initialized) {
×
UNCOV
5392
    return TSDB_CODE_SUCCESS;
×
5393
  }
UNCOV
5394
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
5395
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5396
  }
5397

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

UNCOV
5409
  return TSDB_CODE_SUCCESS;
×
5410
}
5411

UNCOV
5412
int32_t mavgFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5413
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5414
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5415
  SMavgInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5416

UNCOV
5417
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5418
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5419

UNCOV
5420
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5421
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
×
UNCOV
5422
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5423

UNCOV
5424
  int32_t numOfElems = 0;
×
UNCOV
5425
  int32_t type = pInputCol->info.type;
×
UNCOV
5426
  int32_t startOffset = pCtx->offset;
×
5427
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5428
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
×
UNCOV
5429
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5430
    } else {
UNCOV
5431
      pInfo->prevTs = tsList[i];
×
5432
    }
UNCOV
5433
    pInfo->isPrevTsSet = true;
×
5434

UNCOV
5435
    int32_t pos = startOffset + numOfElems;
×
UNCOV
5436
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
5437
      // colDataSetNULL(pOutput, i);
UNCOV
5438
      continue;
×
5439
    }
5440

UNCOV
5441
    char*  data = colDataGetData(pInputCol, i);
×
5442
    double v;
UNCOV
5443
    GET_TYPED_DATA(v, double, type, data);
×
5444

UNCOV
5445
    if (!pInfo->pointsMeet && (pInfo->pos < pInfo->numOfPoints - 1)) {
×
UNCOV
5446
      pInfo->points[pInfo->pos] = v;
×
UNCOV
5447
      pInfo->sum += v;
×
5448
    } else {
UNCOV
5449
      if (!pInfo->pointsMeet && (pInfo->pos == pInfo->numOfPoints - 1)) {
×
UNCOV
5450
        pInfo->sum += v;
×
UNCOV
5451
        pInfo->pointsMeet = true;
×
5452
      } else {
UNCOV
5453
        pInfo->sum = pInfo->sum + v - pInfo->points[pInfo->pos];
×
5454
      }
5455

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

5468
      // handle selectivity
UNCOV
5469
      if (pCtx->subsidiaries.num > 0) {
×
5470
        code = appendSelectivityValue(pCtx, i, pos);
×
UNCOV
5471
        if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5472
          return code;
×
5473
        }
5474
      }
5475

UNCOV
5476
      numOfElems++;
×
5477
    }
5478

UNCOV
5479
    pInfo->pos++;
×
UNCOV
5480
    if (pInfo->pos == pInfo->numOfPoints) {
×
UNCOV
5481
      pInfo->pos = 0;
×
5482
    }
5483
  }
5484

UNCOV
5485
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5486
  return TSDB_CODE_SUCCESS;
×
5487
}
5488

UNCOV
5489
static SSampleInfo* getSampleOutputInfo(SqlFunctionCtx* pCtx) {
×
UNCOV
5490
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5491
  SSampleInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5492

UNCOV
5493
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
×
UNCOV
5494
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
×
5495

UNCOV
5496
  return pInfo;
×
5497
}
5498

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

5507
int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
5508
  if (pResultInfo->initialized) {
×
UNCOV
5509
    return TSDB_CODE_SUCCESS;
×
5510
  }
UNCOV
5511
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
5512
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5513
  }
5514

UNCOV
5515
  taosSeedRand(taosSafeRand());
×
5516

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

UNCOV
5528
  return TSDB_CODE_SUCCESS;
×
5529
}
5530

UNCOV
5531
static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) {
×
UNCOV
5532
  assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType);
×
UNCOV
5533
}
×
5534

UNCOV
5535
static int32_t doReservoirSample(SqlFunctionCtx* pCtx, SSampleInfo* pInfo, char* data, int32_t index) {
×
UNCOV
5536
  pInfo->totalPoints++;
×
UNCOV
5537
  if (pInfo->numSampled < pInfo->samples) {
×
UNCOV
5538
    sampleAssignResult(pInfo, data, pInfo->numSampled);
×
UNCOV
5539
    if (pCtx->subsidiaries.num > 0) {
×
5540
      int32_t code = saveTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[pInfo->numSampled]);
×
UNCOV
5541
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5542
        return code;
×
5543
      }
5544
    }
UNCOV
5545
    pInfo->numSampled++;
×
5546
  } else {
UNCOV
5547
    int32_t j = taosRand() % (pInfo->totalPoints);
×
UNCOV
5548
    if (j < pInfo->samples) {
×
UNCOV
5549
      sampleAssignResult(pInfo, data, j);
×
UNCOV
5550
      if (pCtx->subsidiaries.num > 0) {
×
5551
        int32_t code = updateTupleData(pCtx, index, pCtx->pSrcBlock, &pInfo->tuplePos[j]);
×
UNCOV
5552
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5553
          return code;
×
5554
        }
5555
      }
5556
    }
5557
  }
5558

UNCOV
5559
  return TSDB_CODE_SUCCESS;
×
5560
}
5561

UNCOV
5562
int32_t sampleFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5563
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5564
  SSampleInfo*         pInfo = getSampleOutputInfo(pCtx);
×
5565

UNCOV
5566
  SInputColumnInfoData* pInput = &pCtx->input;
×
5567

UNCOV
5568
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5569
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5570
    if (colDataIsNull_s(pInputCol, i)) {
×
UNCOV
5571
      continue;
×
5572
    }
5573

UNCOV
5574
    char*   data = colDataGetData(pInputCol, i);
×
5575
    int32_t code = doReservoirSample(pCtx, pInfo, data, i);
×
UNCOV
5576
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5577
      return code;
×
5578
    }
5579
  }
5580

UNCOV
5581
  if (pInfo->numSampled == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
×
5582
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
×
UNCOV
5583
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5584
      return code;
×
5585
    }
UNCOV
5586
    pInfo->nullTupleSaved = true;
×
5587
  }
5588

UNCOV
5589
  SET_VAL(pResInfo, pInfo->numSampled, pInfo->numSampled);
×
UNCOV
5590
  return TSDB_CODE_SUCCESS;
×
5591
}
5592

UNCOV
5593
int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5594
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5595
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
5596

UNCOV
5597
  SSampleInfo* pInfo = getSampleOutputInfo(pCtx);
×
UNCOV
5598
  pEntryInfo->complete = true;
×
5599

UNCOV
5600
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
5601
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
5602
  if (NULL == pCol) {
×
UNCOV
5603
    return TSDB_CODE_OUT_OF_RANGE;
×
5604
  }
5605

UNCOV
5606
  int32_t currentRow = pBlock->info.rows;
×
UNCOV
5607
  if (pInfo->numSampled == 0) {
×
UNCOV
5608
    colDataSetNULL(pCol, currentRow);
×
UNCOV
5609
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
×
UNCOV
5610
    return code;
×
5611
  }
UNCOV
5612
  for (int32_t i = 0; i < pInfo->numSampled; ++i) {
×
5613
    code = colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false);
×
UNCOV
5614
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5615
      return code;
×
5616
    }
5617
    code = setSelectivityValue(pCtx, pBlock, &pInfo->tuplePos[i], currentRow + i);
×
UNCOV
5618
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
5619
      return code;
×
5620
    }
5621
  }
5622

UNCOV
5623
  return code;
×
5624
}
5625

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

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

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

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

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

UNCOV
5667
  return TSDB_CODE_SUCCESS;
×
5668
}
5669

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

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

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

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

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

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

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

5724
  taosqsort(pInfo->pItems, pInfo->numOfPoints, POINTER_BYTES, NULL, tailCompFn);
5725

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

5736
  return pInfo->numOfPoints;
5737
#endif
UNCOV
5738
  return 0;
×
5739
}
5740

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

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

5750
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5751

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

5760
  return pEntryInfo->numOfRes;
5761
#endif
UNCOV
5762
  return 0;
×
5763
}
5764

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

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

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

5791
#if 0
5792
static void doUniqueAdd(SUniqueInfo* pInfo, char* data, TSKEY ts, bool isNull) {
5793
  // handle null elements
5794
  if (isNull == true) {
5795
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5796
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5797
    if (pInfo->hasNull == false && pItem->isNull == false) {
5798
      pItem->timestamp = ts;
5799
      pItem->isNull = true;
5800
      pInfo->numOfPoints++;
5801
      pInfo->hasNull = true;
5802
    } else if (pItem->timestamp > ts && pItem->isNull == true) {
5803
      pItem->timestamp = ts;
5804
    }
5805
    return;
5806
  }
5807

5808
  int32_t      hashKeyBytes = IS_VAR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
5809
  SUniqueItem* pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes);
5810
  if (pHashItem == NULL) {
5811
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5812
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5813
    pItem->timestamp = ts;
5814
    memcpy(pItem->data, data, pInfo->colBytes);
5815

5816
    taosHashPut(pInfo->pHash, data, hashKeyBytes, (char*)pItem, sizeof(SUniqueItem*));
5817
    pInfo->numOfPoints++;
5818
  } else if (pHashItem->timestamp > ts) {
5819
    pHashItem->timestamp = ts;
5820
  }
5821
}
5822
#endif
5823

UNCOV
5824
int32_t uniqueFunction(SqlFunctionCtx* pCtx) {
×
5825
#if 0
5826
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5827
  SUniqueInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5828

5829
  SInputColumnInfoData* pInput = &pCtx->input;
5830
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5831

5832
  SColumnInfoData* pInputCol = pInput->pData[0];
5833
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
5834
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5835

5836
  int32_t startOffset = pCtx->offset;
5837
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
5838
    char* data = colDataGetData(pInputCol, i);
5839
    doUniqueAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5840

5841
    if (sizeof(SUniqueInfo) + pInfo->numOfPoints * (sizeof(SUniqueItem) + pInfo->colBytes) >= UNIQUE_MAX_RESULT_SIZE) {
5842
      taosHashCleanup(pInfo->pHash);
5843
      return 0;
5844
    }
5845
  }
5846

5847
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5848
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + i * (sizeof(SUniqueItem) + pInfo->colBytes));
5849
    if (pItem->isNull == true) {
5850
      colDataSetNULL(pOutput, i);
5851
    } else {
5852
      colDataSetVal(pOutput, i, pItem->data, false);
5853
    }
5854
    if (pTsOutput != NULL) {
5855
      colDataSetInt64(pTsOutput, i, &pItem->timestamp);
5856
    }
5857
  }
5858

5859
  return pInfo->numOfPoints;
5860
#endif
UNCOV
5861
  return 0;
×
5862
}
5863

UNCOV
5864
bool getModeFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
5865
  pEnv->calcMemSize = sizeof(SModeInfo);
×
UNCOV
5866
  return true;
×
5867
}
5868

5869
int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
5870
  if (pResInfo->initialized) {
×
UNCOV
5871
    return TSDB_CODE_SUCCESS;
×
5872
  }
UNCOV
5873
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
UNCOV
5874
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5875
  }
5876

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

5891
  pInfo->buf = taosMemoryMalloc(pInfo->colBytes);
×
5892
  if (NULL == pInfo->buf) {
×
5893
    taosHashCleanup(pInfo->pHash);
×
UNCOV
5894
    pInfo->pHash = NULL;
×
UNCOV
5895
    return terrno;
×
5896
  }
UNCOV
5897
  pCtx->needCleanup = true;
×
UNCOV
5898
  return TSDB_CODE_SUCCESS;
×
5899
}
5900

UNCOV
5901
static void modeFunctionCleanup(SModeInfo* pInfo) {
×
UNCOV
5902
  taosHashCleanup(pInfo->pHash);
×
UNCOV
5903
  pInfo->pHash = NULL;
×
UNCOV
5904
  taosMemoryFreeClear(pInfo->buf);
×
5905
}
×
5906

5907
void modeFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
UNCOV
5908
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
5909
    return;
×
5910
  }
UNCOV
5911
  modeFunctionCleanup(GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)));
×
5912
}
5913

UNCOV
5914
static int32_t saveModeTupleData(SqlFunctionCtx* pCtx, char* data, SModeInfo* pInfo, STuplePos* pPos) {
×
5915
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
×
UNCOV
5916
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
5917
      (void)memcpy(pInfo->buf, data, getJsonValueLen(data));
×
5918
    } else {
UNCOV
5919
      (void)memcpy(pInfo->buf, data, varDataTLen(data));
×
5920
    }
5921
  } else {
UNCOV
5922
    (void)memcpy(pInfo->buf, data, pInfo->colBytes);
×
5923
  }
5924

UNCOV
5925
  return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore);
×
5926
}
5927

UNCOV
5928
static int32_t doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCtx, char* data) {
×
UNCOV
5929
  int32_t code = TSDB_CODE_SUCCESS;
×
5930
  int32_t hashKeyBytes;
5931
  if (IS_VAR_DATA_TYPE(pInfo->colType)) {
×
UNCOV
5932
    if (pInfo->colType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
5933
      hashKeyBytes = getJsonValueLen(data);
×
5934
    } else {
UNCOV
5935
      hashKeyBytes = varDataTLen(data);
×
5936
    }
5937
  } else {
UNCOV
5938
    hashKeyBytes = pInfo->colBytes;
×
5939
  }
5940

UNCOV
5941
  SModeItem* pHashItem = (SModeItem*)taosHashGet(pInfo->pHash, data, hashKeyBytes);
×
UNCOV
5942
  if (pHashItem == NULL) {
×
UNCOV
5943
    int32_t   size = sizeof(SModeItem);
×
UNCOV
5944
    SModeItem item = {0};
×
5945

UNCOV
5946
    item.count += 1;
×
5947
    code = saveModeTupleData(pCtx, data, pInfo, &item.dataPos);
×
UNCOV
5948
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5949
      return code;
×
5950
    }
5951

UNCOV
5952
    if (pCtx->subsidiaries.num > 0) {
×
5953
      code = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &item.tuplePos);
×
UNCOV
5954
      if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5955
        return code;
×
5956
      }
5957
    }
5958

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

UNCOV
5973
  return code;
×
5974
}
5975

UNCOV
5976
int32_t modeFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5977
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5978
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5979

UNCOV
5980
  SInputColumnInfoData* pInput = &pCtx->input;
×
5981

UNCOV
5982
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5983
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5984

UNCOV
5985
  int32_t numOfElems = 0;
×
UNCOV
5986
  int32_t startOffset = pCtx->offset;
×
UNCOV
5987
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
5988
    if (colDataIsNull_s(pInputCol, i)) {
×
UNCOV
5989
      continue;
×
5990
    }
UNCOV
5991
    numOfElems++;
×
5992

UNCOV
5993
    char*   data = colDataGetData(pInputCol, i);
×
UNCOV
5994
    int32_t code = doModeAdd(pInfo, i, pCtx, data);
×
5995
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
5996
      modeFunctionCleanup(pInfo);
×
UNCOV
5997
      return code;
×
5998
    }
5999
  }
6000

UNCOV
6001
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
×
6002
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
×
6003
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
6004
      modeFunctionCleanup(pInfo);
×
UNCOV
6005
      return code;
×
6006
    }
UNCOV
6007
    pInfo->nullTupleSaved = true;
×
6008
  }
6009

UNCOV
6010
  SET_VAL(pResInfo, numOfElems, 1);
×
6011

UNCOV
6012
  return TSDB_CODE_SUCCESS;
×
6013
}
6014

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

6027
  STuplePos resDataPos, resTuplePos;
UNCOV
6028
  int32_t   maxCount = 0;
×
6029

UNCOV
6030
  void* pIter = taosHashIterate(pInfo->pHash, NULL);
×
UNCOV
6031
  while (pIter != NULL) {
×
UNCOV
6032
    SModeItem* pItem = (SModeItem*)pIter;
×
UNCOV
6033
    if (pItem->count >= maxCount) {
×
UNCOV
6034
      maxCount = pItem->count;
×
UNCOV
6035
      resDataPos = pItem->dataPos;
×
UNCOV
6036
      resTuplePos = pItem->tuplePos;
×
6037
    }
6038

UNCOV
6039
    pIter = taosHashIterate(pInfo->pHash, pIter);
×
6040
  }
6041

UNCOV
6042
  if (maxCount != 0) {
×
UNCOV
6043
    char* pData = NULL;
×
6044
    code = loadTupleData(pCtx, &resDataPos, &pData);
×
6045
    if (pData == NULL || TSDB_CODE_SUCCESS != code) {
×
UNCOV
6046
      code = terrno = TSDB_CODE_NOT_FOUND;
×
6047
      qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(),
×
6048
             resDataPos.streamTupleKey.groupId, resDataPos.streamTupleKey.ts);
UNCOV
6049
      modeFunctionCleanup(pInfo);
×
UNCOV
6050
      return code;
×
6051
    }
6052

6053
    code = colDataSetVal(pCol, currentRow, pData, false);
×
6054
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6055
      modeFunctionCleanup(pInfo);
×
UNCOV
6056
      return code;
×
6057
    }
UNCOV
6058
    code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow);
×
6059
  } else {
UNCOV
6060
    colDataSetNULL(pCol, currentRow);
×
UNCOV
6061
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
×
6062
  }
6063

UNCOV
6064
  modeFunctionCleanup(pInfo);
×
6065

UNCOV
6066
  return code;
×
6067
}
6068

UNCOV
6069
bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6070
  pEnv->calcMemSize = sizeof(STwaInfo);
×
UNCOV
6071
  return true;
×
6072
}
6073

6074
int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
6075
  if (pResultInfo->initialized) {
×
UNCOV
6076
    return TSDB_CODE_SUCCESS;
×
6077
  }
UNCOV
6078
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
6079
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6080
  }
6081

UNCOV
6082
  STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6083
  pInfo->numOfElems = 0;
×
UNCOV
6084
  pInfo->p.key = INT64_MIN;
×
UNCOV
6085
  pInfo->win = TSWINDOW_INITIALIZER;
×
UNCOV
6086
  return TSDB_CODE_SUCCESS;
×
6087
}
6088

6089
static double twa_get_area(SPoint1 s, SPoint1 e) {
×
UNCOV
6090
  if (e.key == INT64_MAX || s.key == INT64_MIN) {
×
UNCOV
6091
    return 0;
×
6092
  }
6093

UNCOV
6094
  if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) {
×
UNCOV
6095
    return (s.val + e.val) * (e.key - s.key) / 2;
×
6096
  }
6097

UNCOV
6098
  double x = (s.key * e.val - e.key * s.val) / (e.val - s.val);
×
UNCOV
6099
  double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2;
×
UNCOV
6100
  return val;
×
6101
}
6102

UNCOV
6103
int32_t twaFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6104
  int32_t               code = TSDB_CODE_SUCCESS;
×
UNCOV
6105
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6106
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6107

UNCOV
6108
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6109
  STwaInfo*            pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
6110
  SPoint1*             last = &pInfo->p;
×
6111

6112
  if (IS_NULL_TYPE(pInputCol->info.type)) {
×
UNCOV
6113
    pInfo->numOfElems = 0;
×
UNCOV
6114
    goto _twa_over;
×
6115
  }
6116

UNCOV
6117
  funcInputUpdate(pCtx);
×
UNCOV
6118
  SFuncInputRow row = {0};
×
UNCOV
6119
  bool          result = false;
×
UNCOV
6120
  if (pCtx->start.key != INT64_MIN && last->key == INT64_MIN) {
×
6121
    while (1) {
6122
      code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6123
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6124
        return code;
×
6125
      }
UNCOV
6126
      if (!result) {
×
UNCOV
6127
        break;
×
6128
      }
UNCOV
6129
      if (row.isDataNull) {
×
UNCOV
6130
        continue;
×
6131
      }
6132

UNCOV
6133
      last->key = row.ts;
×
6134

UNCOV
6135
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
×
6136

UNCOV
6137
      pInfo->dOutput += twa_get_area(pCtx->start, *last);
×
UNCOV
6138
      pInfo->win.skey = pCtx->start.key;
×
UNCOV
6139
      pInfo->numOfElems++;
×
UNCOV
6140
      break;
×
6141
    }
UNCOV
6142
  } else if (pInfo->p.key == INT64_MIN) {
×
6143
    while (1) {
6144
      code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6145
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6146
        return code;
×
6147
      }
UNCOV
6148
      if (!result) {
×
UNCOV
6149
        break;
×
6150
      }
UNCOV
6151
      if (row.isDataNull) {
×
UNCOV
6152
        continue;
×
6153
      }
6154

UNCOV
6155
      last->key = row.ts;
×
6156

UNCOV
6157
      GET_TYPED_DATA(last->val, double, pInputCol->info.type, row.pData);
×
6158

UNCOV
6159
      pInfo->win.skey = last->key;
×
UNCOV
6160
      pInfo->numOfElems++;
×
UNCOV
6161
      break;
×
6162
    }
6163
  }
6164

UNCOV
6165
  SPoint1 st = {0};
×
6166

6167
  // calculate the value of
6168
  while (1) {
6169
    code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6170
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6171
      return code;
×
6172
    }
UNCOV
6173
    if (!result) {
×
UNCOV
6174
      break;
×
6175
    }
UNCOV
6176
    if (row.isDataNull) {
×
UNCOV
6177
      continue;
×
6178
    }
UNCOV
6179
    pInfo->numOfElems++;
×
UNCOV
6180
    switch (pInputCol->info.type) {
×
UNCOV
6181
      case TSDB_DATA_TYPE_TINYINT: {
×
UNCOV
6182
        INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData);
×
UNCOV
6183
        break;
×
6184
      }
UNCOV
6185
      case TSDB_DATA_TYPE_SMALLINT: {
×
UNCOV
6186
        INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData);
×
UNCOV
6187
        break;
×
6188
      }
UNCOV
6189
      case TSDB_DATA_TYPE_INT: {
×
UNCOV
6190
        INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData);
×
UNCOV
6191
        break;
×
6192
      }
UNCOV
6193
      case TSDB_DATA_TYPE_BIGINT: {
×
UNCOV
6194
        INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData);
×
UNCOV
6195
        break;
×
6196
      }
UNCOV
6197
      case TSDB_DATA_TYPE_FLOAT: {
×
UNCOV
6198
        INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData);
×
UNCOV
6199
        break;
×
6200
      }
UNCOV
6201
      case TSDB_DATA_TYPE_DOUBLE: {
×
UNCOV
6202
        INIT_INTP_POINT(st, row.ts, *(double*)row.pData);
×
UNCOV
6203
        break;
×
6204
      }
UNCOV
6205
      case TSDB_DATA_TYPE_UTINYINT: {
×
UNCOV
6206
        INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData);
×
UNCOV
6207
        break;
×
6208
      }
UNCOV
6209
      case TSDB_DATA_TYPE_USMALLINT: {
×
UNCOV
6210
        INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData);
×
UNCOV
6211
        break;
×
6212
      }
UNCOV
6213
      case TSDB_DATA_TYPE_UINT: {
×
UNCOV
6214
        INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData);
×
UNCOV
6215
        break;
×
6216
      }
UNCOV
6217
      case TSDB_DATA_TYPE_UBIGINT: {
×
UNCOV
6218
        INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData);
×
6219
        break;
×
6220
      }
UNCOV
6221
      default: {
×
UNCOV
6222
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
6223
      }
6224
    }
UNCOV
6225
    if (pInfo->p.key == st.key) {
×
UNCOV
6226
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6227
    }
6228

UNCOV
6229
    pInfo->dOutput += twa_get_area(pInfo->p, st);
×
UNCOV
6230
    pInfo->p = st;
×
6231
  }
6232

6233
  // the last interpolated time window value
UNCOV
6234
  if (pCtx->end.key != INT64_MIN) {
×
UNCOV
6235
    pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end);
×
UNCOV
6236
    pInfo->p = pCtx->end;
×
UNCOV
6237
    pInfo->numOfElems += 1;
×
6238
  }
6239

UNCOV
6240
  pInfo->win.ekey = pInfo->p.key;
×
6241

UNCOV
6242
_twa_over:
×
UNCOV
6243
  SET_VAL(pResInfo, 1, 1);
×
UNCOV
6244
  return TSDB_CODE_SUCCESS;
×
6245
}
6246

6247
/*
6248
 * To copy the input to interResBuf to avoid the input buffer space be over writen
6249
 * by next input data. The TWA function only applies to each table, so no merge procedure
6250
 * is required, we simply copy to the resut ot interResBuffer.
6251
 */
6252
// void twa_function_copy(SQLFunctionCtx *pCtx) {
6253
//   SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx);
6254
//
6255
//   memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes);
6256
//   pResInfo->hasResult = ((STwaInfo *)pCtx->pInput)->hasResult;
6257
// }
6258

UNCOV
6259
int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6260
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
6261

UNCOV
6262
  STwaInfo* pInfo = (STwaInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
6263
  if (pInfo->numOfElems == 0) {
×
UNCOV
6264
    pResInfo->numOfRes = 0;
×
6265
  } else {
UNCOV
6266
    if (pInfo->win.ekey == pInfo->win.skey) {
×
6267
      pInfo->dTwaRes = pInfo->p.val;
×
UNCOV
6268
    } else if (pInfo->win.ekey == INT64_MAX || pInfo->win.skey == INT64_MIN) {  // no data in timewindow
×
UNCOV
6269
      pInfo->dTwaRes = 0;
×
6270
    } else {
UNCOV
6271
      pInfo->dTwaRes = pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey);
×
6272
    }
6273

UNCOV
6274
    pResInfo->numOfRes = 1;
×
6275
  }
6276

UNCOV
6277
  return functionFinalize(pCtx, pBlock);
×
6278
}
6279

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

UNCOV
6288
  STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6289
  pInfo->minRows = INT32_MAX;
×
UNCOV
6290
  return TSDB_CODE_SUCCESS;
×
6291
}
6292

UNCOV
6293
int32_t blockDistFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6294
  const int32_t BLOCK_DIST_RESULT_ROWS = 25;
×
6295

UNCOV
6296
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6297
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
6298
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6299
  STableBlockDistInfo*  pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6300

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

UNCOV
6307
  pDistInfo->numOfBlocks += p1.numOfBlocks;
×
UNCOV
6308
  pDistInfo->numOfTables += p1.numOfTables;
×
UNCOV
6309
  pDistInfo->numOfInmemRows += p1.numOfInmemRows;
×
UNCOV
6310
  pDistInfo->numOfSttRows += p1.numOfSttRows;
×
UNCOV
6311
  pDistInfo->totalSize += p1.totalSize;
×
UNCOV
6312
  pDistInfo->totalRows += p1.totalRows;
×
UNCOV
6313
  pDistInfo->numOfFiles += p1.numOfFiles;
×
6314

UNCOV
6315
  pDistInfo->defMinRows = p1.defMinRows;
×
UNCOV
6316
  pDistInfo->defMaxRows = p1.defMaxRows;
×
UNCOV
6317
  pDistInfo->rowSize = p1.rowSize;
×
6318

UNCOV
6319
  if (pDistInfo->minRows > p1.minRows) {
×
UNCOV
6320
    pDistInfo->minRows = p1.minRows;
×
6321
  }
UNCOV
6322
  if (pDistInfo->maxRows < p1.maxRows) {
×
UNCOV
6323
    pDistInfo->maxRows = p1.maxRows;
×
6324
  }
UNCOV
6325
  pDistInfo->numOfVgroups += (p1.numOfTables != 0 ? 1 : 0);
×
UNCOV
6326
  for (int32_t i = 0; i < tListLen(pDistInfo->blockRowsHisto); ++i) {
×
UNCOV
6327
    pDistInfo->blockRowsHisto[i] += p1.blockRowsHisto[i];
×
6328
  }
6329

UNCOV
6330
  pResInfo->numOfRes = BLOCK_DIST_RESULT_ROWS;  // default output rows
×
UNCOV
6331
  return TSDB_CODE_SUCCESS;
×
6332
}
6333

UNCOV
6334
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo) {
×
UNCOV
6335
  SEncoder encoder = {0};
×
UNCOV
6336
  int32_t  code = 0;
×
6337
  int32_t  lino;
6338
  int32_t  tlen;
UNCOV
6339
  tEncoderInit(&encoder, buf, bufLen);
×
6340

UNCOV
6341
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
×
UNCOV
6342
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->rowSize));
×
6343

UNCOV
6344
  TAOS_CHECK_EXIT(tEncodeU16(&encoder, pInfo->numOfFiles));
×
UNCOV
6345
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfBlocks));
×
UNCOV
6346
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfTables));
×
6347

UNCOV
6348
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalSize));
×
UNCOV
6349
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalRows));
×
UNCOV
6350
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->maxRows));
×
UNCOV
6351
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->minRows));
×
UNCOV
6352
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMaxRows));
×
UNCOV
6353
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMinRows));
×
UNCOV
6354
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfInmemRows));
×
UNCOV
6355
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfSttRows));
×
UNCOV
6356
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfVgroups));
×
6357

UNCOV
6358
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
×
UNCOV
6359
    TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->blockRowsHisto[i]));
×
6360
  }
6361

UNCOV
6362
  tEndEncode(&encoder);
×
6363

6364
_exit:
×
UNCOV
6365
  if (code) {
×
UNCOV
6366
    tlen = code;
×
6367
  } else {
UNCOV
6368
    tlen = encoder.pos;
×
6369
  }
UNCOV
6370
  tEncoderClear(&encoder);
×
UNCOV
6371
  return tlen;
×
6372
}
6373

UNCOV
6374
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo) {
×
UNCOV
6375
  SDecoder decoder = {0};
×
UNCOV
6376
  int32_t  code = 0;
×
6377
  int32_t  lino;
UNCOV
6378
  tDecoderInit(&decoder, buf, bufLen);
×
6379

UNCOV
6380
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
×
UNCOV
6381
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->rowSize));
×
6382

UNCOV
6383
  TAOS_CHECK_EXIT(tDecodeU16(&decoder, &pInfo->numOfFiles));
×
UNCOV
6384
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfBlocks));
×
UNCOV
6385
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfTables));
×
6386

UNCOV
6387
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalSize));
×
UNCOV
6388
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalRows));
×
UNCOV
6389
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->maxRows));
×
UNCOV
6390
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->minRows));
×
UNCOV
6391
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMaxRows));
×
UNCOV
6392
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMinRows));
×
UNCOV
6393
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfInmemRows));
×
UNCOV
6394
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfSttRows));
×
UNCOV
6395
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfVgroups));
×
6396

UNCOV
6397
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
×
UNCOV
6398
    TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->blockRowsHisto[i]));
×
6399
  }
6400

UNCOV
6401
_exit:
×
UNCOV
6402
  tDecoderClear(&decoder);
×
UNCOV
6403
  return code;
×
6404
}
6405

UNCOV
6406
int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6407
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6408
  STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo);
×
6409

6410
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
×
UNCOV
6411
  if (NULL == pColInfo) {
×
UNCOV
6412
    return TSDB_CODE_OUT_OF_RANGE;
×
6413
  }
6414

UNCOV
6415
  if (pData->totalRows == 0) {
×
UNCOV
6416
    pData->minRows = 0;
×
6417
  }
6418

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

UNCOV
6431
  int32_t len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
6432
                          "Total_Blocks=[%d] Total_Size=[%.2f KiB] Average_size=[%.2f KiB] Compression_Ratio=[%.2f %c]",
UNCOV
6433
                          pData->numOfBlocks, pData->totalSize / 1024.0, averageSize / 1024.0, compRatio, '%');
×
6434

UNCOV
6435
  varDataSetLen(st, len);
×
6436
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6437
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6438
    return code;
×
6439
  }
6440

UNCOV
6441
  int64_t avgRows = 0;
×
UNCOV
6442
  if (pData->numOfBlocks > 0) {
×
UNCOV
6443
    avgRows = pData->totalRows / pData->numOfBlocks;
×
6444
  }
6445

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

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

UNCOV
6463
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
UNCOV
6464
                  "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables, pData->numOfFiles,
×
6465
                  pData->numOfVgroups);
6466

UNCOV
6467
  varDataSetLen(st, len);
×
6468
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6469
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6470
    return code;
×
6471
  }
6472

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

UNCOV
6481
  int32_t maxVal = 0;
×
UNCOV
6482
  int32_t minVal = INT32_MAX;
×
UNCOV
6483
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
×
UNCOV
6484
    if (maxVal < pData->blockRowsHisto[i]) {
×
UNCOV
6485
      maxVal = pData->blockRowsHisto[i];
×
6486
    }
6487

UNCOV
6488
    if (minVal > pData->blockRowsHisto[i]) {
×
UNCOV
6489
      minVal = pData->blockRowsHisto[i];
×
6490
    }
6491
  }
6492

6493
  // maximum number of step is 80
UNCOV
6494
  double factor = pData->numOfBlocks / 80.0;
×
6495

UNCOV
6496
  int32_t numOfBuckets = sizeof(pData->blockRowsHisto) / sizeof(pData->blockRowsHisto[0]);
×
UNCOV
6497
  int32_t bucketRange = ceil(((double)(pData->defMaxRows - pData->defMinRows)) / numOfBuckets);
×
6498

UNCOV
6499
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
×
UNCOV
6500
    len =
×
UNCOV
6501
        tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "%04d |", pData->defMinRows + bucketRange * (i + 1));
×
6502

UNCOV
6503
    int32_t num = 0;
×
UNCOV
6504
    if (pData->blockRowsHisto[i] > 0) {
×
UNCOV
6505
      num = (pData->blockRowsHisto[i]) / factor;
×
6506
    }
6507

UNCOV
6508
    for (int32_t j = 0; j < num; ++j) {
×
UNCOV
6509
      int32_t x = tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "%c", '|');
×
UNCOV
6510
      len += x;
×
6511
    }
6512

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

UNCOV
6519
    varDataSetLen(st, len);
×
6520
    code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6521
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6522
      return code;
×
6523
    }
6524
  }
6525

UNCOV
6526
  return TSDB_CODE_SUCCESS;
×
6527
}
6528
int32_t blockDBUsageSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
6529
  if (pResultInfo->initialized) {
×
UNCOV
6530
    return TSDB_CODE_SUCCESS;
×
6531
  }
UNCOV
6532
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
UNCOV
6533
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6534
  }
6535

UNCOV
6536
  SDBBlockUsageInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6537
  return TSDB_CODE_SUCCESS;
×
6538
}
UNCOV
6539
int32_t blockDBUsageFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6540
  const int32_t BLOCK_DISK_USAGE_RESULT_ROWS = 2;
×
6541

UNCOV
6542
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6543
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
6544
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6545
  SDBBlockUsageInfo*    pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6546

6547
  SDBBlockUsageInfo p1 = {0};
×
6548
  if (tDeserializeBlockDbUsage(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
×
UNCOV
6549
    qError("failed to deserialize block dist info");
×
UNCOV
6550
    return TSDB_CODE_FAILED;
×
6551
  }
6552

UNCOV
6553
  pDistInfo->dataInDiskSize += p1.dataInDiskSize;
×
UNCOV
6554
  pDistInfo->walInDiskSize += p1.walInDiskSize;
×
UNCOV
6555
  pDistInfo->rawDataSize += p1.rawDataSize;
×
UNCOV
6556
  pResInfo->numOfRes = BLOCK_DISK_USAGE_RESULT_ROWS;  // default output rows
×
UNCOV
6557
  return TSDB_CODE_SUCCESS;
×
6558
}
6559

UNCOV
6560
int32_t tSerializeBlockDbUsage(void* buf, int32_t bufLen, const SDBBlockUsageInfo* pInfo) {
×
UNCOV
6561
  SEncoder encoder = {0};
×
UNCOV
6562
  int32_t  code = 0;
×
6563
  int32_t  lino;
6564
  int32_t  tlen;
UNCOV
6565
  tEncoderInit(&encoder, buf, bufLen);
×
6566

UNCOV
6567
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
×
6568

UNCOV
6569
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->dataInDiskSize));
×
UNCOV
6570
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->walInDiskSize));
×
UNCOV
6571
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->rawDataSize));
×
6572

UNCOV
6573
  tEndEncode(&encoder);
×
6574

6575
_exit:
×
UNCOV
6576
  if (code) {
×
UNCOV
6577
    tlen = code;
×
6578
  } else {
UNCOV
6579
    tlen = encoder.pos;
×
6580
  }
UNCOV
6581
  tEncoderClear(&encoder);
×
UNCOV
6582
  return tlen;
×
6583
}
UNCOV
6584
int32_t tDeserializeBlockDbUsage(void* buf, int32_t bufLen, SDBBlockUsageInfo* pInfo) {
×
UNCOV
6585
  SDecoder decoder = {0};
×
UNCOV
6586
  int32_t  code = 0;
×
6587
  int32_t  lino;
UNCOV
6588
  tDecoderInit(&decoder, buf, bufLen);
×
6589

UNCOV
6590
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
×
UNCOV
6591
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->dataInDiskSize));
×
UNCOV
6592
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->walInDiskSize));
×
UNCOV
6593
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->rawDataSize));
×
6594

UNCOV
6595
_exit:
×
UNCOV
6596
  tDecoderClear(&decoder);
×
UNCOV
6597
  return code;
×
6598
}
UNCOV
6599
int32_t blockDBUsageFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6600
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6601
  SDBBlockUsageInfo*   pData = GET_ROWCELL_INTERBUF(pResInfo);
×
6602

6603
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
×
UNCOV
6604
  if (NULL == pColInfo) {
×
UNCOV
6605
    return TSDB_CODE_OUT_OF_RANGE;
×
6606
  }
UNCOV
6607
  int32_t len = 0;
×
UNCOV
6608
  int32_t row = 0;
×
UNCOV
6609
  char    st[256] = {0};
×
6610

UNCOV
6611
  uint64_t totalDiskSize = pData->dataInDiskSize;
×
UNCOV
6612
  uint64_t rawDataSize = pData->rawDataSize;
×
UNCOV
6613
  double   compressRatio = 0;
×
UNCOV
6614
  if (rawDataSize != 0) {
×
UNCOV
6615
    compressRatio = totalDiskSize * 100 / (double)rawDataSize;
×
6616
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_ratio=[%.2f%]", compressRatio);
×
6617
  } else {
UNCOV
6618
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_ratio=[NULL]");
×
6619
  }
6620

UNCOV
6621
  varDataSetLen(st, len);
×
6622
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6623
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6624
    return code;
×
6625
  }
6626

UNCOV
6627
  len =
×
UNCOV
6628
      tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Disk_occupied=[%" PRId64 "k]", pData->dataInDiskSize);
×
UNCOV
6629
  varDataSetLen(st, len);
×
6630
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6631
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6632
    return code;
×
6633
  }
UNCOV
6634
  return code;
×
6635
}
6636

UNCOV
6637
bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6638
  pEnv->calcMemSize = sizeof(SDerivInfo);
×
UNCOV
6639
  return true;
×
6640
}
6641

UNCOV
6642
int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
6643
  if (pResInfo->initialized) {
×
UNCOV
6644
    return TSDB_CODE_SUCCESS;
×
6645
  }
UNCOV
6646
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
UNCOV
6647
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6648
  }
6649

UNCOV
6650
  SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6651

UNCOV
6652
  pDerivInfo->ignoreNegative = pCtx->param[2].param.i;
×
UNCOV
6653
  pDerivInfo->prevTs = -1;
×
UNCOV
6654
  pDerivInfo->tsWindow = pCtx->param[1].param.i;
×
UNCOV
6655
  pDerivInfo->valueSet = false;
×
UNCOV
6656
  return TSDB_CODE_SUCCESS;
×
6657
}
6658

UNCOV
6659
int32_t derivativeFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6660
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6661
  SDerivInfo*          pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6662

UNCOV
6663
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6664
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6665

UNCOV
6666
  int32_t          numOfElems = 0;
×
UNCOV
6667
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
UNCOV
6668
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
×
UNCOV
6669
  int32_t          code = TSDB_CODE_SUCCESS;
×
6670

UNCOV
6671
  funcInputUpdate(pCtx);
×
6672

UNCOV
6673
  double v = 0;
×
UNCOV
6674
  if (pCtx->order == TSDB_ORDER_ASC) {
×
UNCOV
6675
    SFuncInputRow row = {0};
×
UNCOV
6676
    bool          result = false;
×
UNCOV
6677
    while (1) {
×
6678
      code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6679
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6680
        return code;
×
6681
      }
UNCOV
6682
      if (!result) {
×
UNCOV
6683
        break;
×
6684
      }
UNCOV
6685
      if (row.isDataNull) {
×
UNCOV
6686
        continue;
×
6687
      }
6688

UNCOV
6689
      char* d = row.pData;
×
UNCOV
6690
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
×
6691

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

UNCOV
6711
          if (pTsOutput != NULL) {
×
UNCOV
6712
            colDataSetInt64(pTsOutput, pos, &row.ts);
×
6713
          }
6714

6715
          // handle selectivity
UNCOV
6716
          if (pCtx->subsidiaries.num > 0) {
×
6717
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
×
UNCOV
6718
            if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
6719
              return code;
×
6720
            }
6721
          }
6722

UNCOV
6723
          numOfElems++;
×
6724
        }
6725
      }
6726

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

UNCOV
6745
      char* d = row.pData;
×
UNCOV
6746
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
×
6747

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

UNCOV
6767
          if (pTsOutput != NULL) {
×
UNCOV
6768
            colDataSetInt64(pTsOutput, pos, &pDerivInfo->prevTs);
×
6769
          }
6770

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

UNCOV
6782
      pDerivInfo->prevValue = v;
×
UNCOV
6783
      pDerivInfo->prevTs = row.ts;
×
6784
    }
6785
  }
6786

UNCOV
6787
  pResInfo->numOfRes = numOfElems;
×
6788

UNCOV
6789
  return TSDB_CODE_SUCCESS;
×
6790
}
6791

UNCOV
6792
int32_t getIrateInfoSize(int32_t pkBytes) { return (int32_t)sizeof(SRateInfo) + 2 * pkBytes; }
×
6793

UNCOV
6794
bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6795
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
×
UNCOV
6796
  pEnv->calcMemSize = getIrateInfoSize(pkBytes);
×
UNCOV
6797
  return true;
×
6798
}
6799

6800
int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
6801
  if (pResInfo->initialized) {
×
UNCOV
6802
    return TSDB_CODE_SUCCESS;
×
6803
  }
UNCOV
6804
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
UNCOV
6805
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6806
  }
6807

UNCOV
6808
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6809

UNCOV
6810
  pInfo->firstKey = INT64_MIN;
×
UNCOV
6811
  pInfo->lastKey = INT64_MIN;
×
UNCOV
6812
  pInfo->firstValue = (double)INT64_MIN;
×
UNCOV
6813
  pInfo->lastValue = (double)INT64_MIN;
×
6814

UNCOV
6815
  pInfo->hasResult = 0;
×
UNCOV
6816
  return TSDB_CODE_SUCCESS;
×
6817
}
6818

UNCOV
6819
static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) {
×
UNCOV
6820
  if (isFirst) {
×
UNCOV
6821
    pRateInfo->firstValue = v;
×
UNCOV
6822
    pRateInfo->firstKey = ts;
×
UNCOV
6823
    if (pRateInfo->firstPk) {
×
6824
      int32_t pkBytes;
6825
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
×
UNCOV
6826
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
6827
          pkBytes = getJsonValueLen(pk);
×
6828
        } else {
UNCOV
6829
          pkBytes = varDataTLen(pk);
×
6830
        }
6831
      } else {
UNCOV
6832
        pkBytes = pRateInfo->pkBytes;
×
6833
      }
UNCOV
6834
      (void)memcpy(pRateInfo->firstPk, pk, pkBytes);
×
6835
    }
6836
  } else {
UNCOV
6837
    pRateInfo->lastValue = v;
×
UNCOV
6838
    pRateInfo->lastKey = ts;
×
UNCOV
6839
    if (pRateInfo->lastPk) {
×
6840
      int32_t pkBytes;
6841
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
×
UNCOV
6842
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
6843
          pkBytes = getJsonValueLen(pk);
×
6844
        } else {
UNCOV
6845
          pkBytes = varDataTLen(pk);
×
6846
        }
6847
      } else {
UNCOV
6848
        pkBytes = pRateInfo->pkBytes;
×
6849
      }
UNCOV
6850
      (void)memcpy(pRateInfo->lastPk, pk, pkBytes);
×
6851
    }
6852
  }
UNCOV
6853
}
×
6854

UNCOV
6855
static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool isMerge) {
×
UNCOV
6856
  if (pCtx->hasPrimaryKey) {
×
UNCOV
6857
    if (!isMerge) {
×
UNCOV
6858
      pRateInfo->pkType = pCtx->input.pPrimaryKey->info.type;
×
UNCOV
6859
      pRateInfo->pkBytes = pCtx->input.pPrimaryKey->info.bytes;
×
UNCOV
6860
      pRateInfo->firstPk = pRateInfo->pkData;
×
UNCOV
6861
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
×
6862
    } else {
UNCOV
6863
      pRateInfo->firstPk = pRateInfo->pkData;
×
UNCOV
6864
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
×
6865
    }
6866
  } else {
UNCOV
6867
    pRateInfo->firstPk = NULL;
×
UNCOV
6868
    pRateInfo->lastPk = NULL;
×
6869
  }
UNCOV
6870
}
×
6871

UNCOV
6872
int32_t irateFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6873
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
6874
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6875
  SRateInfo*           pRateInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6876

UNCOV
6877
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6878
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6879

UNCOV
6880
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
6881

UNCOV
6882
  funcInputUpdate(pCtx);
×
6883

UNCOV
6884
  initializeRateInfo(pCtx, pRateInfo, false);
×
6885

UNCOV
6886
  int32_t       numOfElems = 0;
×
UNCOV
6887
  int32_t       type = pInputCol->info.type;
×
UNCOV
6888
  SFuncInputRow row = {0};
×
UNCOV
6889
  bool          result = false;
×
UNCOV
6890
  while (1) {
×
6891
    code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6892
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
6893
      return code;
×
6894
    }
UNCOV
6895
    if (!result) {
×
UNCOV
6896
      break;
×
6897
    }
UNCOV
6898
    if (row.isDataNull) {
×
UNCOV
6899
      continue;
×
6900
    }
6901

UNCOV
6902
    char*  data = row.pData;
×
UNCOV
6903
    double v = 0;
×
UNCOV
6904
    GET_TYPED_DATA(v, double, type, data);
×
6905

UNCOV
6906
    if (INT64_MIN == pRateInfo->lastKey) {
×
UNCOV
6907
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
×
UNCOV
6908
      pRateInfo->hasResult = 1;
×
UNCOV
6909
      continue;
×
6910
    }
6911

UNCOV
6912
    if (row.ts > pRateInfo->lastKey) {
×
UNCOV
6913
      if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
×
UNCOV
6914
        doSaveRateInfo(pRateInfo, true, pRateInfo->lastKey, pRateInfo->lastPk, pRateInfo->lastValue);
×
6915
      }
UNCOV
6916
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
×
6917
      continue;
×
UNCOV
6918
    } else if (row.ts == pRateInfo->lastKey) {
×
UNCOV
6919
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6920
    }
6921

UNCOV
6922
    if ((INT64_MIN == pRateInfo->firstKey) || row.ts > pRateInfo->firstKey) {
×
6923
      doSaveRateInfo(pRateInfo, true, row.ts, row.pPk, v);
×
UNCOV
6924
    } else if (row.ts == pRateInfo->firstKey) {
×
UNCOV
6925
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6926
    }
6927
  }
6928

UNCOV
6929
  numOfElems++;
×
6930

UNCOV
6931
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
6932
  return TSDB_CODE_SUCCESS;
×
6933
}
6934

UNCOV
6935
static double doCalcRate(const SRateInfo* pRateInfo, double tickPerSec) {
×
UNCOV
6936
  if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) ||
×
UNCOV
6937
      (pRateInfo->firstKey >= pRateInfo->lastKey)) {
×
UNCOV
6938
    return 0.0;
×
6939
  }
6940

UNCOV
6941
  double diff = 0;
×
6942
  // If the previous value of the last is greater than the last value, only keep the last point instead of the delta
6943
  // value between two values.
UNCOV
6944
  diff = pRateInfo->lastValue;
×
UNCOV
6945
  if (diff >= pRateInfo->firstValue) {
×
UNCOV
6946
    diff -= pRateInfo->firstValue;
×
6947
  }
6948

6949
  int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey;
×
UNCOV
6950
  if (duration == 0) {
×
UNCOV
6951
    return 0;
×
6952
  }
6953

UNCOV
6954
  return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0;
×
6955
}
6956

UNCOV
6957
static void irateTransferInfoImpl(TSKEY inputKey, SRateInfo* pInput, SRateInfo* pOutput, bool isFirstKey) {
×
UNCOV
6958
  if (inputKey > pOutput->lastKey) {
×
UNCOV
6959
    doSaveRateInfo(pOutput, true, pOutput->lastKey, pOutput->lastPk, pOutput->lastValue);
×
UNCOV
6960
    if (isFirstKey) {
×
UNCOV
6961
      doSaveRateInfo(pOutput, false, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
6962
    } else {
UNCOV
6963
      doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
6964
    }
UNCOV
6965
  } else if ((inputKey < pOutput->lastKey) && (inputKey > pOutput->firstKey)) {
×
UNCOV
6966
    if (isFirstKey) {
×
UNCOV
6967
      doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
6968
    } else {
UNCOV
6969
      doSaveRateInfo(pOutput, true, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
6970
    }
6971
  } else {
6972
    // inputKey < pOutput->firstKey
6973
  }
UNCOV
6974
}
×
6975

UNCOV
6976
static void irateCopyInfo(SRateInfo* pInput, SRateInfo* pOutput) {
×
UNCOV
6977
  doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
UNCOV
6978
  doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
UNCOV
6979
}
×
6980

UNCOV
6981
static int32_t irateTransferInfo(SRateInfo* pInput, SRateInfo* pOutput) {
×
UNCOV
6982
  if ((pInput->firstKey != INT64_MIN &&
×
6983
       (pInput->firstKey == pOutput->firstKey || pInput->firstKey == pOutput->lastKey)) ||
×
UNCOV
6984
      (pInput->lastKey != INT64_MIN && (pInput->lastKey == pOutput->firstKey || pInput->lastKey == pOutput->lastKey))) {
×
UNCOV
6985
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6986
  }
6987

UNCOV
6988
  if (pOutput->hasResult == 0) {
×
UNCOV
6989
    irateCopyInfo(pInput, pOutput);
×
UNCOV
6990
    pOutput->hasResult = pInput->hasResult;
×
UNCOV
6991
    return TSDB_CODE_SUCCESS;
×
6992
  }
6993

UNCOV
6994
  if (pInput->firstKey != INT64_MIN) {
×
UNCOV
6995
    irateTransferInfoImpl(pInput->firstKey, pInput, pOutput, true);
×
6996
  }
6997

UNCOV
6998
  if (pInput->lastKey != INT64_MIN) {
×
UNCOV
6999
    irateTransferInfoImpl(pInput->lastKey, pInput, pOutput, false);
×
7000
  }
7001

UNCOV
7002
  pOutput->hasResult = pInput->hasResult;
×
UNCOV
7003
  return TSDB_CODE_SUCCESS;
×
7004
}
7005

UNCOV
7006
int32_t irateFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
7007
  SInputColumnInfoData* pInput = &pCtx->input;
×
7008
  SColumnInfoData*      pCol = pInput->pData[0];
×
UNCOV
7009
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
UNCOV
7010
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
7011
  }
7012

UNCOV
7013
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
7014
  initializeRateInfo(pCtx, pInfo, true);
×
7015

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

UNCOV
7029
  if (pInfo->hasResult) {
×
UNCOV
7030
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
7031
  }
7032

UNCOV
7033
  return TSDB_CODE_SUCCESS;
×
7034
}
7035

UNCOV
7036
int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7037
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7038
  SRateInfo*           pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
7039
  int32_t              resultBytes = getIrateInfoSize(pInfo->pkBytes);
×
UNCOV
7040
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
7041

UNCOV
7042
  if (NULL == res) {
×
UNCOV
7043
    return terrno;
×
7044
  }
UNCOV
7045
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
7046
  varDataSetLen(res, resultBytes);
×
7047

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

UNCOV
7055
  int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
7056

UNCOV
7057
  taosMemoryFree(res);
×
UNCOV
7058
  return code;
×
7059
}
7060

UNCOV
7061
int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7062
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
7063
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
7064
  if (NULL == pCol) {
×
UNCOV
7065
    return TSDB_CODE_OUT_OF_RANGE;
×
7066
  }
7067

UNCOV
7068
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7069
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
×
7070

UNCOV
7071
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
7072
  double     result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i));
×
UNCOV
7073
  int32_t    code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes);
×
7074

UNCOV
7075
  return code;
×
7076
}
7077

UNCOV
7078
int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
7079
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7080
  SGroupKeyInfo*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7081

UNCOV
7082
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
7083
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
7084

UNCOV
7085
  int32_t startIndex = pInput->startRowIndex;
×
7086

7087
  // escape rest of data blocks to avoid first entry to be overwritten.
UNCOV
7088
  if (pInfo->hasResult) {
×
UNCOV
7089
    goto _group_value_over;
×
7090
  }
7091

UNCOV
7092
  if (pInputCol->pData == NULL || colDataIsNull_s(pInputCol, startIndex)) {
×
UNCOV
7093
    pInfo->isNull = true;
×
UNCOV
7094
    pInfo->hasResult = true;
×
UNCOV
7095
    goto _group_value_over;
×
7096
  }
7097

UNCOV
7098
  char* data = colDataGetData(pInputCol, startIndex);
×
UNCOV
7099
  if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
×
UNCOV
7100
    (void)memcpy(pInfo->data, data,
×
UNCOV
7101
                 (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data));
×
7102
  } else {
UNCOV
7103
    (void)memcpy(pInfo->data, data, pInputCol->info.bytes);
×
7104
  }
UNCOV
7105
  pInfo->hasResult = true;
×
7106

UNCOV
7107
_group_value_over:
×
7108

UNCOV
7109
  SET_VAL(pResInfo, 1, 1);
×
UNCOV
7110
  return TSDB_CODE_SUCCESS;
×
7111
}
7112

UNCOV
7113
int32_t groupKeyFunction(SqlFunctionCtx* pCtx) { return groupConstValueFunction(pCtx); }
×
7114

UNCOV
7115
int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7116
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
7117
  int32_t          code = TSDB_CODE_SUCCESS;
×
7118
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
7119
  if (NULL == pCol) {
×
UNCOV
7120
    return TSDB_CODE_OUT_OF_RANGE;
×
7121
  }
7122

UNCOV
7123
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
7124

UNCOV
7125
  SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7126

UNCOV
7127
  if (pInfo->hasResult) {
×
UNCOV
7128
    int32_t currentRow = pBlock->info.rows;
×
UNCOV
7129
    for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) {
×
7130
      code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false);
×
UNCOV
7131
      if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
7132
        return code;
×
7133
      }
7134
    }
7135
  } else {
UNCOV
7136
    pResInfo->numOfRes = 0;
×
7137
  }
7138

UNCOV
7139
  return code;
×
7140
}
7141

7142
int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return groupConstValueFinalize(pCtx, pBlock); }
×
7143

7144
int32_t groupKeyCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
7145
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
7146
  SGroupKeyInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
7147

UNCOV
7148
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
7149
  SGroupKeyInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
7150

7151
  // escape rest of data blocks to avoid first entry to be overwritten.
UNCOV
7152
  if (pDBuf->hasResult) {
×
UNCOV
7153
    goto _group_key_over;
×
7154
  }
7155

7156
  if (pSBuf->isNull) {
×
7157
    pDBuf->isNull = true;
×
UNCOV
7158
    pDBuf->hasResult = true;
×
UNCOV
7159
    goto _group_key_over;
×
7160
  }
7161

7162
  if (IS_VAR_DATA_TYPE(pSourceCtx->resDataInfo.type)) {
×
7163
    (void)memcpy(pDBuf->data, pSBuf->data,
×
UNCOV
7164
                 (pSourceCtx->resDataInfo.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(pSBuf->data)
×
7165
                                                                       : varDataTLen(pSBuf->data));
×
7166
  } else {
UNCOV
7167
    (void)memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes);
×
7168
  }
7169

7170
  pDBuf->hasResult = true;
×
7171

7172
_group_key_over:
×
7173

UNCOV
7174
  SET_VAL(pDResInfo, 1, 1);
×
UNCOV
7175
  return TSDB_CODE_SUCCESS;
×
7176
}
7177

UNCOV
7178
int32_t cachedLastRowFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
7179
  int32_t numOfElems = 0;
×
7180

UNCOV
7181
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7182
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7183

UNCOV
7184
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
7185
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
7186

UNCOV
7187
  int32_t bytes = pInputCol->info.bytes;
×
UNCOV
7188
  pInfo->bytes = bytes;
×
7189

UNCOV
7190
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
7191
  pInfo->pkType = -1;
×
UNCOV
7192
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
7193
  if (pCtx->hasPrimaryKey) {
×
UNCOV
7194
    pInfo->pkType = pkCol->info.type;
×
UNCOV
7195
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
7196
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
×
7197
  }
7198

7199
  // TODO it traverse the different way.
7200
  // last_row function does not ignore the null value
UNCOV
7201
  for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
×
UNCOV
7202
    numOfElems++;
×
7203

UNCOV
7204
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
×
UNCOV
7205
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
×
7206

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

UNCOV
7217
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
7218
  return TSDB_CODE_SUCCESS;
×
7219
}
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