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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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) {
×
UNCOV
348
    if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) {
×
349
      pIter->hasPrev = true;
×
350
      return false;
×
351
    } else {
UNCOV
352
      int32_t idx = pIter->rowIndex;
×
UNCOV
353
      while (pIter->tsList[idx] == pIter->prevBlockTsEnd) {
×
354
        ++idx;
×
355
      }
356

UNCOV
357
      pIter->hasPrev = false;
×
UNCOV
358
      setInputRowInfo(pRow, pIter, idx, true);
×
UNCOV
359
      forwardToNextDiffTsRow(pIter, idx);
×
UNCOV
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

UNCOV
512
EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
×
UNCOV
513
  SNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
×
UNCOV
514
  if (QUERY_NODE_COLUMN == nodeType(pParam) && PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pParam)->colId) {
×
UNCOV
515
    return FUNC_DATA_REQUIRED_NOT_LOAD;
×
516
  }
UNCOV
517
  return FUNC_DATA_REQUIRED_SMA_LOAD;
×
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
  if (IS_NULL_TYPE(type)) {
×
571
    // select count(NULL) returns 0
UNCOV
572
    numOfElem = 1;
×
UNCOV
573
    *((int64_t*)buf) += 0;
×
574
  } else {
UNCOV
575
    numOfElem = getNumOfElems(pCtx);
×
UNCOV
576
    *((int64_t*)buf) += numOfElem;
×
577
  }
578

UNCOV
579
  if (tsCountAlwaysReturnValue) {
×
UNCOV
580
    pResInfo->numOfRes = 1;
×
581
  } else {
582
    SET_VAL(pResInfo, *((int64_t*)buf), 1);
×
583
  }
584

UNCOV
585
  return TSDB_CODE_SUCCESS;
×
586
}
587

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

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

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

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

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

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

UNCOV
613
int32_t sumFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
614
  int32_t numOfElem = 0;
×
615

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

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

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

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

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

UNCOV
642
    int32_t start = pInput->startRowIndex;
×
UNCOV
643
    int32_t numOfRows = pInput->numOfRows;
×
644

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
799
EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
×
UNCOV
800
  if(funcNotSupportStringSma(pFunc)) {
×
UNCOV
801
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
802
  }
UNCOV
803
  return FUNC_DATA_REQUIRED_SMA_LOAD;
×
804
}
805

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

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

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

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

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

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

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

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

UNCOV
856
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
857
  SMinmaxResInfo*      pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
×
858

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

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

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

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

UNCOV
919
  return code;
×
920
}
921

922
#ifdef BUILD_NO_CALL
923
int32_t setNullSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t rowIndex) {
924
  if (pCtx->subsidiaries.num <= 0) {
925
    return TSDB_CODE_SUCCESS;
926
  }
927

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

932
    SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
933
    colDataSetNULL(pDstCol, rowIndex);
934
  }
935

936
  return TSDB_CODE_SUCCESS;
937
}
938
#endif
939

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

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

UNCOV
956
    bool* nullList = (bool*)p;
×
UNCOV
957
    char* pStart = (char*)(nullList + numOfCols * sizeof(bool));
×
958

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

964
      // group_key function has its own process function
965
      // do not process there
UNCOV
966
      if (fmIsGroupKeyFunc(pc->functionId)) {
×
UNCOV
967
        continue;
×
968
      }
969

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

UNCOV
986
  return TSDB_CODE_SUCCESS;
×
987
}
988

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

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

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

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

UNCOV
1009
    char* pData = colDataGetData(pSrcCol, rowIndex);
×
1010

1011
    // append to dest col
UNCOV
1012
    int32_t dstSlotId = pc->pExpr->base.resSchema.slotId;
×
1013

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

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

UNCOV
1031
void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; }
×
1032

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

UNCOV
1038
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
1039
  SMinmaxResInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
1040
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
×
1041

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

UNCOV
1098
int32_t minCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1099
  return minMaxCombine(pDestCtx, pSourceCtx, 1);
×
1100
}
UNCOV
1101
int32_t maxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1102
  return minMaxCombine(pDestCtx, pSourceCtx, 0);
×
1103
}
1104

UNCOV
1105
int32_t getStdInfoSize() { return (int32_t)sizeof(SStdRes); }
×
1106

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

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

UNCOV
1120
  SStdRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
1121
  (void)memset(pRes, 0, sizeof(SStdRes));
×
UNCOV
1122
  return TSDB_CODE_SUCCESS;
×
1123
}
1124

UNCOV
1125
int32_t stdFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1126
  int32_t numOfElem = 0;
×
1127

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

UNCOV
1132
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
1133
  pStdRes->type = type;
×
1134

1135
  // computing based on the true data block
UNCOV
1136
  SColumnInfoData* pCol = pInput->pData[0];
×
1137

UNCOV
1138
  int32_t start = pInput->startRowIndex;
×
UNCOV
1139
  int32_t numOfRows = pInput->numOfRows;
×
1140

UNCOV
1141
  if (IS_NULL_TYPE(type)) {
×
UNCOV
1142
    numOfElem = 0;
×
UNCOV
1143
    goto _stddev_over;
×
1144
  }
1145

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

UNCOV
1154
        numOfElem += 1;
×
UNCOV
1155
        pStdRes->count += 1;
×
UNCOV
1156
        pStdRes->isum += plist[i];
×
UNCOV
1157
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1158
      }
1159

UNCOV
1160
      break;
×
1161
    }
1162

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

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

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

UNCOV
1185
        numOfElem += 1;
×
UNCOV
1186
        pStdRes->count += 1;
×
UNCOV
1187
        pStdRes->isum += plist[i];
×
UNCOV
1188
        pStdRes->quadraticISum += plist[i] * plist[i];
×
1189
      }
1190

UNCOV
1191
      break;
×
1192
    }
1193

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

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

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

UNCOV
1216
        numOfElem += 1;
×
UNCOV
1217
        pStdRes->count += 1;
×
UNCOV
1218
        pStdRes->usum += plist[i];
×
UNCOV
1219
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1220
      }
1221

UNCOV
1222
      break;
×
1223
    }
1224

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

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

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

UNCOV
1247
        numOfElem += 1;
×
UNCOV
1248
        pStdRes->count += 1;
×
UNCOV
1249
        pStdRes->usum += plist[i];
×
UNCOV
1250
        pStdRes->quadraticUSum += plist[i] * plist[i];
×
1251
      }
1252

UNCOV
1253
      break;
×
1254
    }
1255

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

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

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

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

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

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

1301
    default:
×
1302
      break;
×
1303
  }
1304

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

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

UNCOV
1327
  pOutput->count += pInput->count;
×
1328
}
1329

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

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

UNCOV
1339
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
1340
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1341
  }
1342

UNCOV
1343
  SStdRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1344

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

UNCOV
1352
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
UNCOV
1353
  return TSDB_CODE_SUCCESS;
×
1354
}
1355

1356
#ifdef BUILD_NO_CALL
1357
int32_t stdInvertFunction(SqlFunctionCtx* pCtx) {
1358
  int32_t numOfElem = 0;
1359

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

1364
  SStdRes* pStdRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
1365

1366
  // computing based on the true data block
1367
  SColumnInfoData* pCol = pInput->pData[0];
1368

1369
  int32_t start = pInput->startRowIndex;
1370
  int32_t numOfRows = pInput->numOfRows;
1371

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

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

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

UNCOV
1429
  if (pStddevRes->count == 0) {
×
UNCOV
1430
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
UNCOV
1431
    return functionFinalize(pCtx, pBlock);
×
1432
  }
1433

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

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

UNCOV
1450
  return functionFinalize(pCtx, pBlock);
×
1451
}
1452

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

1459
  if (pStdvarRes->count == 0) {
×
1460
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1461
    return functionFinalize(pCtx, pBlock);
×
1462
  }
1463

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

1475
  // check for overflow
1476
  if (isinf(pStdvarRes->result) || isnan(pStdvarRes->result)) {
×
1477
    GET_RES_INFO(pCtx)->numOfRes = 0;
×
1478
  }
1479

1480
  return functionFinalize(pCtx, pBlock);
×
1481
}
1482

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

UNCOV
1489
  if (NULL == res) {
×
1490
    return terrno;
×
1491
  }
UNCOV
1492
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
1493
  varDataSetLen(res, resultBytes);
×
1494

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

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

UNCOV
1504
  taosMemoryFree(res);
×
UNCOV
1505
  return code;
×
1506
}
1507

UNCOV
1508
int32_t stdCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
1509
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
1510
  SStdRes*             pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
1511

UNCOV
1512
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
1513
  SStdRes*             pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
1514
  int16_t              type = pDBuf->type == TSDB_DATA_TYPE_NULL ? pSBuf->type : pDBuf->type;
×
1515

UNCOV
1516
  stdTransferInfo(pSBuf, pDBuf);
×
1517

UNCOV
1518
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
1519
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
1520
  return TSDB_CODE_SUCCESS;
×
1521
}
1522

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

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

UNCOV
1536
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
1537

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

UNCOV
1543
int32_t leastSQRFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1544
  int32_t numOfElem = 0;
×
1545

UNCOV
1546
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1547
  int32_t               type = pInput->pData[0]->info.type;
×
1548

UNCOV
1549
  SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
1550

UNCOV
1551
  SColumnInfoData* pCol = pInput->pData[0];
×
1552

UNCOV
1553
  double(*param)[3] = pInfo->matrix;
×
UNCOV
1554
  double x = pInfo->startVal;
×
1555

UNCOV
1556
  int32_t start = pInput->startRowIndex;
×
UNCOV
1557
  int32_t numOfRows = pInput->numOfRows;
×
1558

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

UNCOV
1578
        numOfElem++;
×
UNCOV
1579
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1580
      }
UNCOV
1581
      break;
×
1582
    }
1583

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

UNCOV
1591
        numOfElem++;
×
UNCOV
1592
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1593
      }
UNCOV
1594
      break;
×
1595
    }
1596

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

UNCOV
1604
        numOfElem++;
×
UNCOV
1605
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1606
      }
UNCOV
1607
      break;
×
1608
    }
1609

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

UNCOV
1628
        numOfElem++;
×
UNCOV
1629
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1630
      }
UNCOV
1631
      break;
×
1632
    }
1633

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

UNCOV
1641
        numOfElem++;
×
UNCOV
1642
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1643
      }
UNCOV
1644
      break;
×
1645
    }
1646

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

UNCOV
1654
        numOfElem++;
×
UNCOV
1655
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1656
      }
UNCOV
1657
      break;
×
1658
    }
1659

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

UNCOV
1667
        numOfElem++;
×
UNCOV
1668
        LEASTSQR_CAL(param, x, plist, i, pInfo->stepVal);
×
1669
      }
UNCOV
1670
      break;
×
1671
    }
1672

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

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

1691
    default:
×
1692
      break;
×
1693
  }
1694

UNCOV
1695
  pInfo->startVal = x;
×
UNCOV
1696
  pInfo->num += numOfElem;
×
1697

UNCOV
1698
  SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1);
×
1699

UNCOV
1700
  return TSDB_CODE_SUCCESS;
×
1701
}
1702

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

UNCOV
1709
  if (NULL == pCol) {
×
1710
    return TSDB_CODE_OUT_OF_RANGE;
×
1711
  }
UNCOV
1712
  int32_t currentRow = pBlock->info.rows;
×
1713

UNCOV
1714
  if (0 == pInfo->num) {
×
UNCOV
1715
    colDataSetNULL(pCol, currentRow);
×
UNCOV
1716
    return TSDB_CODE_SUCCESS;
×
1717
  }
1718

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

UNCOV
1721
  param[1][1] = (double)pInfo->num;
×
UNCOV
1722
  param[1][0] = param[0][1];
×
1723

UNCOV
1724
  double param00 = param[0][0] - param[1][0] * (param[0][1] / param[1][1]);
×
UNCOV
1725
  double param02 = param[0][2] - param[1][2] * (param[0][1] / param[1][1]);
×
1726

UNCOV
1727
  if (0 == param00) {
×
UNCOV
1728
    colDataSetNULL(pCol, currentRow);
×
UNCOV
1729
    return TSDB_CODE_SUCCESS;
×
1730
  }
1731

1732
  // param[0][1] = 0;
UNCOV
1733
  double param12 = param[1][2] - param02 * (param[1][0] / param00);
×
1734
  // param[1][0] = 0;
UNCOV
1735
  param02 /= param00;
×
1736

UNCOV
1737
  param12 /= param[1][1];
×
1738

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

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

UNCOV
1756
  return code;
×
1757
}
1758

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

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

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

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

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

UNCOV
1800
  return TSDB_CODE_SUCCESS;
×
1801
}
1802

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

UNCOV
1814
int32_t percentileFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
1815
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
1816
  int32_t              numOfElems = 0;
×
UNCOV
1817
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
1818

UNCOV
1819
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
1820
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
1821

UNCOV
1822
  SColumnInfoData* pCol = pInput->pData[0];
×
UNCOV
1823
  int32_t          type = pCol->info.type;
×
1824

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

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

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

UNCOV
1857
      if (GET_DOUBLE_VAL(&pInfo->minval) > tmin) {
×
UNCOV
1858
        SET_DOUBLE_VAL(&pInfo->minval, tmin);
×
1859
      }
1860

UNCOV
1861
      if (GET_DOUBLE_VAL(&pInfo->maxval) < tmax) {
×
UNCOV
1862
        SET_DOUBLE_VAL(&pInfo->maxval, tmax);
×
1863
      }
1864

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

UNCOV
1874
        char* data = colDataGetData(pCol, i);
×
1875

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

UNCOV
1882
        if (v > GET_DOUBLE_VAL(&pInfo->maxval)) {
×
UNCOV
1883
          SET_DOUBLE_VAL(&pInfo->maxval, v);
×
1884
        }
1885

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

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

UNCOV
1906
    SET_VAL(pResInfo, numOfElems, 1);
×
1907
  }
1908

UNCOV
1909
  pCtx->needCleanup = true;
×
UNCOV
1910
  return TSDB_CODE_SUCCESS;
×
1911
}
1912

UNCOV
1913
int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
1914
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
1915
  SPercentileInfo*     ppInfo = (SPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
1916

UNCOV
1917
  int32_t code = 0;
×
UNCOV
1918
  double  v = 0;
×
1919

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

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

UNCOV
1932
        GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
×
1933

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

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

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

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

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

UNCOV
1964
      GET_TYPED_DATA(v, double, pVal->nType, &pVal->i);
×
1965

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

UNCOV
1971
      tMemBucketDestroy(pMemBucket);
×
UNCOV
1972
      return functionFinalize(pCtx, pBlock);
×
1973
    }
1974
  } else {
UNCOV
1975
    return functionFinalize(pCtx, pBlock);
×
1976
  }
1977

1978
_fin_error:
×
1979

1980
  tMemBucketDestroy(pMemBucket);
×
1981
  return code;
×
1982
}
1983

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

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

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

UNCOV
2009
  return algoType;
×
2010
}
2011

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

UNCOV
2017
static void buildTDigestInfo(SAPercentileInfo* pInfo) {
×
UNCOV
2018
  pInfo->pTDigest = (TDigest*)((char*)pInfo + sizeof(SAPercentileInfo));
×
UNCOV
2019
}
×
2020

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

UNCOV
2029
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
2030

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

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

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

UNCOV
2054
  return TSDB_CODE_SUCCESS;
×
2055
}
2056

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

UNCOV
2062
  SColumnInfoData* pCol = pInput->pData[0];
×
UNCOV
2063
  int32_t          type = pCol->info.type;
×
2064

UNCOV
2065
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2066

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

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

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

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

UNCOV
2113
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
2114
  return TSDB_CODE_SUCCESS;
×
2115
}
2116

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

UNCOV
2124
    if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) {
×
UNCOV
2125
      return TSDB_CODE_SUCCESS;
×
2126
    }
2127

UNCOV
2128
    if (hasRes) {
×
UNCOV
2129
      *hasRes = true;
×
2130
    }
2131

UNCOV
2132
    buildTDigestInfo(pOutput);
×
UNCOV
2133
    TDigest* pTDigest = pOutput->pTDigest;
×
UNCOV
2134
    tdigestAutoFill(pTDigest, COMPRESSION);
×
2135

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

UNCOV
2151
    if (hasRes) {
×
UNCOV
2152
      *hasRes = true;
×
2153
    }
2154

UNCOV
2155
    buildHistogramInfo(pOutput);
×
UNCOV
2156
    SHistogramInfo* pHisto = pOutput->pHisto;
×
2157

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

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

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

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

UNCOV
2186
int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
2187
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
2188

UNCOV
2189
  SInputColumnInfoData* pInput = &pCtx->input;
×
2190

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

UNCOV
2196
  SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2197

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

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

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

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

UNCOV
2218
  SET_VAL(pResInfo, hasRes ? 1 : 0, 1);
×
UNCOV
2219
  return TSDB_CODE_SUCCESS;
×
2220
}
2221

UNCOV
2222
int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2223
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2224
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
2225

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

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

UNCOV
2259
  return functionFinalize(pCtx, pBlock);
×
2260
}
2261

UNCOV
2262
int32_t apercentilePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2263
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2264
  SAPercentileInfo*    pInfo = (SAPercentileInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
2265

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

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

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

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

UNCOV
2289
  taosMemoryFree(res);
×
UNCOV
2290
  return code;
×
2291
}
2292

UNCOV
2293
int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
2294
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
2295
  SAPercentileInfo*    pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
2296

UNCOV
2297
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
2298
  SAPercentileInfo*    pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
2299

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

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

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

UNCOV
2335
EFuncDataRequired firstDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
×
UNCOV
2336
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
×
2337

2338
  // not initialized yet, data is required
UNCOV
2339
  if (pEntry == NULL) {
×
2340
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2341
  }
2342

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

UNCOV
2366
EFuncDataRequired lastDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo) {
×
UNCOV
2367
  SResultRowEntryInfo* pEntry = (SResultRowEntryInfo*)pRes;
×
2368

2369
  // not initialized yet, data is required
UNCOV
2370
  if (pEntry == NULL) {
×
2371
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
2372
  }
2373

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

2394
// TODO modify it to include primary key bytes
UNCOV
2395
int32_t getFirstLastInfoSize(int32_t resBytes, int32_t pkBytes) { return sizeof(SFirstLastRes) + resBytes + pkBytes; }
×
2396

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

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

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

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

UNCOV
2422
  return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex);
×
2423
}
2424

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

UNCOV
2433
  SFirstLastRes*        pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
2434
  SInputColumnInfoData* pInput = &pCtx->input;
×
2435

UNCOV
2436
  pRes->nullTupleSaved = false;
×
UNCOV
2437
  pRes->nullTuplePos.pageId = -1;
×
UNCOV
2438
  return TSDB_CODE_SUCCESS;
×
2439
}
2440

UNCOV
2441
static int32_t prepareBuf(SqlFunctionCtx* pCtx) {
×
UNCOV
2442
  if (pCtx->subsidiaries.rowLen == 0) {
×
UNCOV
2443
    int32_t rowLen = 0;
×
UNCOV
2444
    for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) {
×
UNCOV
2445
      SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j];
×
UNCOV
2446
      rowLen += pc->pExpr->base.resSchema.bytes;
×
2447
    }
2448

UNCOV
2449
    pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool);
×
UNCOV
2450
    pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen);
×
UNCOV
2451
    if (NULL == pCtx->subsidiaries.buf) {
×
2452
      return terrno;
×
2453
    }
2454
  }
UNCOV
2455
  return TSDB_CODE_SUCCESS;
×
2456
}
2457

UNCOV
2458
static int32_t firstlastSaveTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SqlFunctionCtx* pCtx,
×
2459
                                      SFirstLastRes* pInfo, bool noElements) {
UNCOV
2460
  int32_t code = TSDB_CODE_SUCCESS;
×
2461

UNCOV
2462
  if (pCtx->subsidiaries.num <= 0) {
×
UNCOV
2463
    return TSDB_CODE_SUCCESS;
×
2464
  }
2465

UNCOV
2466
  if (!pInfo->hasResult) {
×
UNCOV
2467
    code = saveTupleData(pCtx, rowIndex, pSrcBlock, noElements ? &pInfo->nullTuplePos : &pInfo->pos);
×
2468
  } else {
UNCOV
2469
    code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pInfo->pos);
×
2470
  }
2471

UNCOV
2472
  return code;
×
2473
}
2474

UNCOV
2475
static int32_t doSaveCurrentVal(SqlFunctionCtx* pCtx, int32_t rowIndex, int64_t currentTs, char* pkData, int32_t type,
×
2476
                                char* pData) {
UNCOV
2477
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2478
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2479

UNCOV
2480
  if (IS_VAR_DATA_TYPE(type)) {
×
UNCOV
2481
    if (type == TSDB_DATA_TYPE_JSON) {
×
2482
      pInfo->bytes = getJsonValueLen(pData);
×
2483
    } else {
UNCOV
2484
      pInfo->bytes = varDataTLen(pData);
×
2485
    }
2486
  }
2487

UNCOV
2488
  (void)memcpy(pInfo->buf, pData, pInfo->bytes);
×
UNCOV
2489
  if (pkData != NULL) {
×
UNCOV
2490
    if (IS_VAR_DATA_TYPE(pInfo->pkType)) {
×
UNCOV
2491
      if (pInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
2492
        pInfo->pkBytes = getJsonValueLen(pkData);
×
2493
      } else {
UNCOV
2494
        pInfo->pkBytes = varDataTLen(pkData);
×
2495
      }
2496
    }
UNCOV
2497
    (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes);
×
UNCOV
2498
    pInfo->pkData = pInfo->buf + pInfo->bytes;
×
2499
  }
2500

UNCOV
2501
  pInfo->ts = currentTs;
×
UNCOV
2502
  int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pInfo, false);
×
UNCOV
2503
  if (code != TSDB_CODE_SUCCESS) {
×
2504
    return code;
×
2505
  }
2506

UNCOV
2507
  pInfo->hasResult = true;
×
UNCOV
2508
  return TSDB_CODE_SUCCESS;
×
2509
}
2510

2511
// This ordinary first function does not care if current scan is ascending order or descending order scan
2512
// the OPTIMIZED version of first function will only handle the ascending order scan
UNCOV
2513
int32_t firstFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
2514
  int32_t numOfElems = 0;
×
2515

UNCOV
2516
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2517
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2518

UNCOV
2519
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2520
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
2521

UNCOV
2522
  pInfo->bytes = pInputCol->info.bytes;
×
2523

UNCOV
2524
  if (IS_NULL_TYPE(pInputCol->info.type)) {
×
UNCOV
2525
    return TSDB_CODE_SUCCESS;
×
2526
  }
2527

UNCOV
2528
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
2529
  pInfo->pkType = -1;
×
UNCOV
2530
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2531
  if (pCtx->hasPrimaryKey) {
×
UNCOV
2532
    pInfo->pkType = pkCol->info.type;
×
UNCOV
2533
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
2534
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_ASC);
×
2535
  }
2536

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

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

UNCOV
2551
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
×
UNCOV
2552
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
×
2553

UNCOV
2554
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
×
2555

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

2567
    for (int32_t i = pInput->startRowIndex; i < pInput->startRowIndex + pInput->numOfRows; ++i) {
2568
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2569
        continue;
2570
      }
2571

2572
      numOfElems++;
2573

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

2590
    for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
2591
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2592
        continue;
2593
      }
2594

2595
      numOfElems++;
2596

2597
      char* data = colDataGetData(pInputCol, i);
2598
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2599

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

UNCOV
2609
  int     from = -1;
×
UNCOV
2610
  int32_t i = -1;
×
UNCOV
2611
  while (funcInputGetNextRowIndex(pInput, from, true, &i, &from)) {
×
UNCOV
2612
    if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
×
UNCOV
2613
      continue;
×
2614
    }
2615

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

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

UNCOV
2646
int32_t lastFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
2647
  int32_t numOfElems = 0;
×
2648

UNCOV
2649
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2650
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
2651

UNCOV
2652
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2653
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
2654

UNCOV
2655
  int32_t type = pInputCol->info.type;
×
UNCOV
2656
  int32_t bytes = pInputCol->info.bytes;
×
UNCOV
2657
  pInfo->bytes = bytes;
×
2658

UNCOV
2659
  if (IS_NULL_TYPE(type)) {
×
UNCOV
2660
    return TSDB_CODE_SUCCESS;
×
2661
  }
2662

UNCOV
2663
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
2664
  pInfo->pkType = -1;
×
UNCOV
2665
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2666
  if (pCtx->hasPrimaryKey) {
×
UNCOV
2667
    pInfo->pkType = pkCol->info.type;
×
UNCOV
2668
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
2669
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
×
2670
  }
2671

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

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

UNCOV
2686
  TSKEY startKey = getRowPTs(pInput->pPTS, 0);
×
UNCOV
2687
  TSKEY endKey = getRowPTs(pInput->pPTS, pInput->totalRows - 1);
×
2688

UNCOV
2689
  int32_t blockDataOrder = (startKey <= endKey) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
×
2690

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

2699
      numOfElems++;
2700

2701
      char* data = colDataGetData(pInputCol, i);
2702
      TSKEY cts = getRowPTs(pInput->pPTS, i);
2703
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
2704
        doSaveCurrentVal(pCtx, i, cts, type, data);
2705
      }
2706

2707
      break;
2708
    }
2709
  } else {  // descending order
2710
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2711
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2712
        continue;
2713
      }
2714

2715
      numOfElems++;
2716

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

2728
#if 0
2729
    for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
2730
      if (pInputCol->hasNull && colDataIsNull(pInputCol, pInput->totalRows, i, pColAgg)) {
2731
        continue;
2732
      }
2733

2734
      numOfElems++;
2735
      if (pResInfo->numOfRes == 0 || pInfo->ts < pts[i]) {
2736
        char* data = colDataGetData(pInputCol, i);
2737
        doSaveCurrentVal(pCtx, i, pts[i], type, data);
2738
        pResInfo->numOfRes = 1;
2739
      }
2740
    }
2741
#else
2742

2743
  // todo refactor
UNCOV
2744
  if (!pInputCol->hasNull && !pCtx->hasPrimaryKey) {
×
UNCOV
2745
    numOfElems = 1;
×
2746

UNCOV
2747
    int32_t round = pInput->numOfRows >> 2;
×
UNCOV
2748
    int32_t reminder = pInput->numOfRows & 0x03;
×
2749

UNCOV
2750
    for (int32_t i = pInput->startRowIndex, tick = 0; tick < round; i += 4, tick += 1) {
×
UNCOV
2751
      int64_t cts = pts[i];
×
UNCOV
2752
      int32_t chosen = i;
×
2753

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

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

UNCOV
2764
      if (cts < pts[i + 3]) {
×
UNCOV
2765
        cts = pts[i + 3];
×
UNCOV
2766
        chosen = i + 3;
×
2767
      }
2768

UNCOV
2769
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
×
UNCOV
2770
        char*   data = colDataGetData(pInputCol, chosen);
×
UNCOV
2771
        int32_t code = doSaveCurrentVal(pCtx, i, cts, NULL, type, data);
×
UNCOV
2772
        if (code != TSDB_CODE_SUCCESS) {
×
2773
          return code;
×
2774
        }
UNCOV
2775
        pResInfo->numOfRes = 1;
×
2776
      }
2777
    }
2778

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

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

2815
#endif
2816

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

UNCOV
2826
  return TSDB_CODE_SUCCESS;
×
2827
}
2828

UNCOV
2829
static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) {
×
UNCOV
2830
  if (!pInput->hasResult) {
×
UNCOV
2831
    return false;
×
2832
  }
UNCOV
2833
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
2834
  if (pInput->pkData) {
×
UNCOV
2835
    pkCompareFn = getKeyComparFunc(pInput->pkType, (isFirst) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC);
×
2836
  }
UNCOV
2837
  if (pOutput->hasResult) {
×
UNCOV
2838
    if (isFirst) {
×
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
    } else {
UNCOV
2844
      if (pInput->ts < pOutput->ts ||
×
UNCOV
2845
          (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) {
×
UNCOV
2846
        return false;
×
2847
      }
2848
    }
2849
  }
2850

UNCOV
2851
  pOutput->isNull = pInput->isNull;
×
UNCOV
2852
  pOutput->ts = pInput->ts;
×
UNCOV
2853
  pOutput->bytes = pInput->bytes;
×
UNCOV
2854
  pOutput->pkType = pInput->pkType;
×
2855

UNCOV
2856
  (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes);
×
UNCOV
2857
  if (pInput->pkData) {
×
UNCOV
2858
    pOutput->pkBytes = pInput->pkBytes;
×
UNCOV
2859
    (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes);
×
UNCOV
2860
    pOutput->pkData = pOutput->buf + pOutput->bytes;
×
2861
  }
UNCOV
2862
  return true;
×
2863
}
2864

UNCOV
2865
static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst,
×
2866
                                     int32_t rowIndex) {
UNCOV
2867
  if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) {
×
UNCOV
2868
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput, pOutput->nullTupleSaved);
×
UNCOV
2869
    if (TSDB_CODE_SUCCESS != code) {
×
2870
      return code;
×
2871
    }
UNCOV
2872
    pOutput->hasResult = true;
×
2873
  }
UNCOV
2874
  return TSDB_CODE_SUCCESS;
×
2875
}
2876

UNCOV
2877
static int32_t firstLastFunctionMergeImpl(SqlFunctionCtx* pCtx, bool isFirstQuery) {
×
UNCOV
2878
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
2879
  SColumnInfoData*      pCol = pInput->pData[0];
×
2880

UNCOV
2881
  if (IS_NULL_TYPE(pCol->info.type)) {
×
2882
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
2883
    return TSDB_CODE_SUCCESS;
×
2884
  }
2885

UNCOV
2886
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
2887
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
2888
  }
2889

UNCOV
2890
  SFirstLastRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
2891

UNCOV
2892
  int32_t start = pInput->startRowIndex;
×
UNCOV
2893
  int32_t numOfElems = 0;
×
2894

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

UNCOV
2907
    int32_t code = firstLastTransferInfo(pCtx, pInputInfo, pInfo, isFirstQuery, i);
×
UNCOV
2908
    if (code != TSDB_CODE_SUCCESS) {
×
2909
      return code;
×
2910
    }
UNCOV
2911
    if (!numOfElems) {
×
UNCOV
2912
      numOfElems = pInputInfo->hasResult ? 1 : 0;
×
2913
    }
2914
  }
2915

UNCOV
2916
  if (numOfElems == 0) {
×
UNCOV
2917
    int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, pInput->startRowIndex, pCtx, pInfo, !pInfo->nullTupleSaved);
×
UNCOV
2918
    if (code != TSDB_CODE_SUCCESS) {
×
2919
      return code;
×
2920
    }
UNCOV
2921
    pInfo->nullTupleSaved = true;
×
2922
  }
2923

UNCOV
2924
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
2925
  return TSDB_CODE_SUCCESS;
×
2926
}
2927

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

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

UNCOV
2932
int32_t firstLastFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2933
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
2934
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
2935
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
2936
  if (NULL == pCol) {
×
2937
    return TSDB_CODE_OUT_OF_RANGE;
×
2938
  }
2939

UNCOV
2940
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
2941
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
×
2942

UNCOV
2943
  SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
2944

UNCOV
2945
  if (pResInfo->isNullRes) {
×
UNCOV
2946
    colDataSetNULL(pCol, pBlock->info.rows);
×
UNCOV
2947
    return setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, pBlock->info.rows);
×
2948
  }
UNCOV
2949
  code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes);
×
UNCOV
2950
  if (TSDB_CODE_SUCCESS != code) {
×
2951
    return code;
×
2952
  }
2953

2954
  // handle selectivity
UNCOV
2955
  code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
×
2956

UNCOV
2957
  return code;
×
2958
}
2959

UNCOV
2960
int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
2961
  int32_t code = TSDB_CODE_SUCCESS;
×
2962

UNCOV
2963
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
2964
  SFirstLastRes*       pRes = GET_ROWCELL_INTERBUF(pEntryInfo);
×
2965

UNCOV
2966
  int32_t resultBytes = getFirstLastInfoSize(pRes->bytes, pRes->pkBytes);
×
2967

2968
  // todo check for failure
UNCOV
2969
  char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
UNCOV
2970
  if (NULL == res) {
×
2971
    return terrno;
×
2972
  }
UNCOV
2973
  (void)memcpy(varDataVal(res), pRes, resultBytes);
×
2974

UNCOV
2975
  varDataSetLen(res, resultBytes);
×
2976

UNCOV
2977
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
2978
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
2979
  if (NULL == pCol) {
×
2980
    taosMemoryFree(res);
×
2981
    return TSDB_CODE_OUT_OF_RANGE;
×
2982
  }
2983

UNCOV
2984
  if (pEntryInfo->numOfRes == 0) {
×
UNCOV
2985
    colDataSetNULL(pCol, pBlock->info.rows);
×
UNCOV
2986
    code = setSelectivityValue(pCtx, pBlock, &pRes->nullTuplePos, pBlock->info.rows);
×
2987
  } else {
UNCOV
2988
    code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
UNCOV
2989
    if (TSDB_CODE_SUCCESS != code) {
×
2990
      taosMemoryFree(res);
×
2991
      return code;
×
2992
    }
UNCOV
2993
    code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows);
×
2994
  }
UNCOV
2995
  taosMemoryFree(res);
×
UNCOV
2996
  return code;
×
2997
}
2998

UNCOV
2999
int32_t lastCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
3000
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
3001
  SFirstLastRes*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
UNCOV
3002
  int32_t              bytes = pDBuf->bytes;
×
3003

UNCOV
3004
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
3005
  SFirstLastRes*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
3006

UNCOV
3007
  pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, false);
×
UNCOV
3008
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
3009
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
3010
  return TSDB_CODE_SUCCESS;
×
3011
}
3012

UNCOV
3013
static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex, int64_t cts, SFirstLastRes* pInfo) {
×
UNCOV
3014
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3015
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
3016
  SColumnInfoData*      pkCol = pInput->pPrimaryKey;
×
3017

UNCOV
3018
  if (colDataIsNull_s(pInputCol, rowIndex)) {
×
UNCOV
3019
    pInfo->isNull = true;
×
3020
  } else {
UNCOV
3021
    pInfo->isNull = false;
×
3022

UNCOV
3023
    if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
×
UNCOV
3024
      if (pInputCol->info.type == TSDB_DATA_TYPE_JSON) {
×
3025
        pInfo->bytes = getJsonValueLen(pData);
×
3026
      } else {
UNCOV
3027
        pInfo->bytes = varDataTLen(pData);
×
3028
      }
3029
    }
3030

UNCOV
3031
    (void)memcpy(pInfo->buf, pData, pInfo->bytes);
×
3032
  }
3033

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

UNCOV
3052
  pInfo->hasResult = true;
×
3053

UNCOV
3054
  return TSDB_CODE_SUCCESS;
×
3055
}
3056

UNCOV
3057
int32_t lastRowFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3058
  int32_t numOfElems = 0;
×
3059

UNCOV
3060
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3061
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3062

UNCOV
3063
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3064
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
3065

UNCOV
3066
  int32_t type = pInputCol->info.type;
×
UNCOV
3067
  int32_t bytes = pInputCol->info.bytes;
×
UNCOV
3068
  pInfo->bytes = bytes;
×
3069

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

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

UNCOV
3091
      if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
×
UNCOV
3092
        int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
×
UNCOV
3093
        if (code != TSDB_CODE_SUCCESS) return code;
×
3094
      }
3095

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

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

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

UNCOV
3138
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
3139
  return TSDB_CODE_SUCCESS;
×
3140
}
3141

UNCOV
3142
bool getDiffFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
3143
  pEnv->calcMemSize = sizeof(SDiffInfo);
×
UNCOV
3144
  return true;
×
3145
}
3146

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

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

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

3254
  return false;
3255
}
3256

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

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

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

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

UNCOV
3389
bool getForecastConfEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
3390
  pEnv->calcMemSize = sizeof(float);
×
UNCOV
3391
  return true;
×
3392
}
3393

UNCOV
3394
int32_t diffResultIsNull(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3395
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3396
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3397

UNCOV
3398
  if (pRow->isDataNull || !pDiffInfo->hasPrev) {
×
UNCOV
3399
    return true;
×
UNCOV
3400
  } else if (ignoreNegative(pDiffInfo->ignoreOption)) {
×
UNCOV
3401
    return diffIsNegtive(pDiffInfo, pCtx->input.pData[0]->info.type, pRow->pData);
×
3402
  }
UNCOV
3403
  return false;
×
3404
}
3405

UNCOV
3406
bool isFirstRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3407
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3408
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3409
  return pDiffInfo->isFirstRow;
×
3410
}
3411

UNCOV
3412
int32_t trySetPreVal(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) {
×
UNCOV
3413
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3414
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3415
  pDiffInfo->isFirstRow = false;
×
UNCOV
3416
  if (pRow->isDataNull) {
×
UNCOV
3417
    return TSDB_CODE_SUCCESS;
×
3418
  }
3419

UNCOV
3420
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3421
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
3422
  int8_t                inputType = pInputCol->info.type;
×
3423

UNCOV
3424
  char* pv = pRow->pData;
×
UNCOV
3425
  return doSetPrevVal(pDiffInfo, inputType, pv, pRow->ts);
×
3426
}
3427

UNCOV
3428
int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) {
×
UNCOV
3429
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3430
  SDiffInfo*           pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
3431

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

3441
    // handle selectivity
UNCOV
3442
    if (pCtx->subsidiaries.num > 0) {
×
UNCOV
3443
      code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos);
×
UNCOV
3444
      if (code != TSDB_CODE_SUCCESS) {
×
3445
        return code;
×
3446
      }
3447
    }
UNCOV
3448
    return TSDB_CODE_SUCCESS;
×
3449
  }
3450

UNCOV
3451
  char* pv = pRow->pData;
×
3452

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

UNCOV
3468
  return TSDB_CODE_SUCCESS;
×
3469
}
3470

UNCOV
3471
int32_t diffFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; }
×
3472

UNCOV
3473
int32_t diffFunctionByRow(SArray* pCtxArray) {
×
UNCOV
3474
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
3475
  int     diffColNum = pCtxArray->size;
×
UNCOV
3476
  if (diffColNum == 0) {
×
3477
    return TSDB_CODE_SUCCESS;
×
3478
  }
UNCOV
3479
  int32_t numOfElems = 0;
×
3480

UNCOV
3481
  SArray* pRows = taosArrayInit_s(sizeof(SFuncInputRow), diffColNum);
×
UNCOV
3482
  if (NULL == pRows) {
×
3483
    return terrno;
×
3484
  }
3485

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

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

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

UNCOV
3564
  for (int i = 0; i < diffColNum; ++i) {
×
UNCOV
3565
    SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i);
×
UNCOV
3566
    if (NULL == pCtx) {
×
3567
      code = terrno;
×
3568
      goto _exit;
×
3569
    }
UNCOV
3570
    SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3571
    pResInfo->numOfRes = numOfElems;
×
3572
  }
3573

UNCOV
3574
_exit:
×
UNCOV
3575
  if (pRows) {
×
UNCOV
3576
    taosArrayDestroy(pRows);
×
UNCOV
3577
    pRows = NULL;
×
3578
  }
UNCOV
3579
  return code;
×
3580
}
3581

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

UNCOV
3584
bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
3585
  SValueNode* pkNode = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
×
UNCOV
3586
  pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem);
×
UNCOV
3587
  return true;
×
3588
}
3589

UNCOV
3590
int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
3591
  if (pResInfo->initialized) {
×
3592
    return TSDB_CODE_SUCCESS;
×
3593
  }
UNCOV
3594
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
3595
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
3596
  }
3597

UNCOV
3598
  STopBotRes*           pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3599
  SInputColumnInfoData* pInput = &pCtx->input;
×
3600

UNCOV
3601
  pRes->maxSize = pCtx->param[1].param.i;
×
3602

UNCOV
3603
  pRes->nullTupleSaved = false;
×
UNCOV
3604
  pRes->nullTuplePos.pageId = -1;
×
UNCOV
3605
  return TSDB_CODE_SUCCESS;
×
3606
}
3607

UNCOV
3608
static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) {
×
UNCOV
3609
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
3610
  STopBotRes*          pRes = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
3611
  pRes->pItems = (STopBotResItem*)((char*)pRes + sizeof(STopBotRes));
×
3612

UNCOV
3613
  return pRes;
×
3614
}
3615

3616
static int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock,
3617
                               uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery);
3618

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

UNCOV
3621
int32_t topFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3622
  int32_t              numOfElems = 0;
×
UNCOV
3623
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
3624

UNCOV
3625
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3626
  SColumnInfoData*      pCol = pInput->pData[0];
×
3627

UNCOV
3628
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3629
  pRes->type = pInput->pData[0]->info.type;
×
3630

UNCOV
3631
  int32_t start = pInput->startRowIndex;
×
UNCOV
3632
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
3633
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
3634
      continue;
×
3635
    }
3636

UNCOV
3637
    numOfElems++;
×
UNCOV
3638
    char*   data = colDataGetData(pCol, i);
×
UNCOV
3639
    int32_t code = doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, pRes->type, pInput->uid, pResInfo, true);
×
UNCOV
3640
    if (code != TSDB_CODE_SUCCESS) {
×
3641
      return code;
×
3642
    }
3643
  }
3644

UNCOV
3645
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pRes->nullTupleSaved) {
×
UNCOV
3646
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pRes->nullTuplePos);
×
UNCOV
3647
    if (code != TSDB_CODE_SUCCESS) {
×
3648
      return code;
×
3649
    }
UNCOV
3650
    pRes->nullTupleSaved = true;
×
3651
  }
UNCOV
3652
  return TSDB_CODE_SUCCESS;
×
3653
}
3654

UNCOV
3655
int32_t bottomFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
3656
  int32_t              numOfElems = 0;
×
UNCOV
3657
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
3658

UNCOV
3659
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
3660
  SColumnInfoData*      pCol = pInput->pData[0];
×
3661

UNCOV
3662
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3663
  pRes->type = pInput->pData[0]->info.type;
×
3664

UNCOV
3665
  int32_t start = pInput->startRowIndex;
×
UNCOV
3666
  for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
3667
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
3668
      continue;
×
3669
    }
3670

UNCOV
3671
    numOfElems++;
×
UNCOV
3672
    char*   data = colDataGetData(pCol, i);
×
UNCOV
3673
    int32_t code = doAddIntoResult(pCtx, data, i, pCtx->pSrcBlock, pRes->type, pInput->uid, pResInfo, false);
×
UNCOV
3674
    if (code != TSDB_CODE_SUCCESS) {
×
3675
      return code;
×
3676
    }
3677
  }
3678

UNCOV
3679
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pRes->nullTupleSaved) {
×
UNCOV
3680
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pRes->nullTuplePos);
×
UNCOV
3681
    if (code != TSDB_CODE_SUCCESS) {
×
3682
      return code;
×
3683
    }
UNCOV
3684
    pRes->nullTupleSaved = true;
×
3685
  }
3686

UNCOV
3687
  return TSDB_CODE_SUCCESS;
×
3688
}
3689

UNCOV
3690
static int32_t topBotResComparFn(const void* p1, const void* p2, const void* param) {
×
UNCOV
3691
  uint16_t type = *(uint16_t*)param;
×
3692

UNCOV
3693
  STopBotResItem* val1 = (STopBotResItem*)p1;
×
UNCOV
3694
  STopBotResItem* val2 = (STopBotResItem*)p2;
×
3695

UNCOV
3696
  if (IS_SIGNED_NUMERIC_TYPE(type)) {
×
UNCOV
3697
    if (val1->v.i == val2->v.i) {
×
UNCOV
3698
      return 0;
×
3699
    }
3700

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

UNCOV
3707
    return (val1->v.u > val2->v.u) ? 1 : -1;
×
UNCOV
3708
  } else if (TSDB_DATA_TYPE_FLOAT == type) {
×
UNCOV
3709
    if (val1->v.f == val2->v.f) {
×
UNCOV
3710
      return 0;
×
3711
    }
3712

UNCOV
3713
    return (val1->v.f > val2->v.f) ? 1 : -1;
×
3714
  }
3715

UNCOV
3716
  if (val1->v.d == val2->v.d) {
×
UNCOV
3717
    return 0;
×
3718
  }
3719

UNCOV
3720
  return (val1->v.d > val2->v.d) ? 1 : -1;
×
3721
}
3722

UNCOV
3723
int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type,
×
3724
                        uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery) {
UNCOV
3725
  STopBotRes* pRes = getTopBotOutputInfo(pCtx);
×
UNCOV
3726
  int32_t     code = TSDB_CODE_SUCCESS;
×
3727

UNCOV
3728
  SVariant val = {0};
×
UNCOV
3729
  TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type));
×
3730

UNCOV
3731
  STopBotResItem* pItems = pRes->pItems;
×
3732

3733
  // not full yet
UNCOV
3734
  if (pEntryInfo->numOfRes < pRes->maxSize) {
×
UNCOV
3735
    STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes];
×
UNCOV
3736
    pItem->v = val;
×
UNCOV
3737
    pItem->uid = uid;
×
3738

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

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

UNCOV
3789
  return TSDB_CODE_SUCCESS;
×
3790
}
3791

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

UNCOV
3803
  int32_t offset = 0;
×
UNCOV
3804
  for (int32_t i = 0; i < pSubsidiaryies->num; ++i) {
×
UNCOV
3805
    SqlFunctionCtx* pc = pSubsidiaryies->pCtx[i];
×
3806

3807
    // group_key function has its own process function
3808
    // do not process there
UNCOV
3809
    if (fmIsGroupKeyFunc(pc->functionId)) {
×
UNCOV
3810
      continue;
×
3811
    }
3812

UNCOV
3813
    SFunctParam* pFuncParam = &pc->pExpr->base.pParam[0];
×
UNCOV
3814
    int32_t      srcSlotId = pFuncParam->pCol->slotId;
×
3815

UNCOV
3816
    SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
×
UNCOV
3817
    if (NULL == pCol) {
×
3818
      return TSDB_CODE_OUT_OF_RANGE;
×
3819
    }
UNCOV
3820
    if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) {
×
UNCOV
3821
      offset += pCol->info.bytes;
×
UNCOV
3822
      continue;
×
3823
    }
3824

UNCOV
3825
    char* p = colDataGetData(pCol, rowIndex);
×
UNCOV
3826
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
×
UNCOV
3827
      (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p));
×
3828
    } else {
UNCOV
3829
      (void)memcpy(pStart + offset, p, pCol->info.bytes);
×
3830
    }
3831

UNCOV
3832
    offset += pCol->info.bytes;
×
3833
  }
3834

UNCOV
3835
  *res = buf;
×
UNCOV
3836
  return TSDB_CODE_SUCCESS;
×
3837
}
3838

UNCOV
3839
static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, SWinKey* key,
×
3840
                               STuplePos* pPos, SFunctionStateStore* pStore) {
UNCOV
3841
  STuplePos p = {0};
×
UNCOV
3842
  if (pHandle->pBuf != NULL) {
×
UNCOV
3843
    SFilePage* pPage = NULL;
×
3844

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

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

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

UNCOV
3879
  *pPos = p;
×
UNCOV
3880
  return TSDB_CODE_SUCCESS;
×
3881
}
3882

UNCOV
3883
int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
×
UNCOV
3884
  int32_t code = prepareBuf(pCtx);
×
UNCOV
3885
  if (TSDB_CODE_SUCCESS != code) {
×
3886
    return code;
×
3887
  }
3888

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

UNCOV
3902
  char* buf = NULL;
×
UNCOV
3903
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
×
UNCOV
3904
  if (TSDB_CODE_SUCCESS != code) {
×
3905
    return code;
×
3906
  }
UNCOV
3907
  return doSaveTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, &key, pPos, pCtx->pStore);
×
3908
}
3909

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

UNCOV
3927
  return TSDB_CODE_SUCCESS;
×
3928
}
3929

UNCOV
3930
int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) {
×
UNCOV
3931
  int32_t code = prepareBuf(pCtx);
×
UNCOV
3932
  if (TSDB_CODE_SUCCESS != code) {
×
3933
    return code;
×
3934
  }
3935

UNCOV
3936
  char* buf = NULL;
×
UNCOV
3937
  code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf);
×
UNCOV
3938
  if (TSDB_CODE_SUCCESS != code) {
×
3939
    return code;
×
3940
  }
UNCOV
3941
  return doUpdateTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, pPos, pCtx->pStore);
×
3942
}
3943

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

UNCOV
3966
int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value) {
×
UNCOV
3967
  return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore, value);
×
3968
}
3969

UNCOV
3970
int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
3971
  int32_t code = TSDB_CODE_SUCCESS;
×
3972

UNCOV
3973
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
UNCOV
3974
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
×
3975

UNCOV
3976
  int16_t type = pCtx->pExpr->base.resSchema.type;
×
UNCOV
3977
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
×
3978

UNCOV
3979
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
3980
  if (NULL == pCol) {
×
3981
    return TSDB_CODE_OUT_OF_RANGE;
×
3982
  }
3983

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

UNCOV
4008
  return code;
×
4009
}
4010

4011
int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery) {
×
4012
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
4013
  STopBotRes*          pRes = getTopBotOutputInfo(pCtx);
×
4014
  STopBotResItem*      pItems = pRes->pItems;
×
4015
  int32_t              code = TSDB_CODE_SUCCESS;
×
4016

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

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

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

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

UNCOV
4084
int32_t getSpreadInfoSize() { return (int32_t)sizeof(SSpreadInfo); }
×
4085

UNCOV
4086
bool getSpreadFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4087
  pEnv->calcMemSize = sizeof(SSpreadInfo);
×
UNCOV
4088
  return true;
×
4089
}
4090

UNCOV
4091
int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4092
  if (pResultInfo->initialized) {
×
4093
    return TSDB_CODE_SUCCESS;
×
4094
  }
UNCOV
4095
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
4096
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4097
  }
4098

UNCOV
4099
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4100
  SET_DOUBLE_VAL(&pInfo->min, DBL_MAX);
×
UNCOV
4101
  SET_DOUBLE_VAL(&pInfo->max, -DBL_MAX);
×
UNCOV
4102
  pInfo->hasResult = false;
×
UNCOV
4103
  return TSDB_CODE_SUCCESS;
×
4104
}
4105

UNCOV
4106
int32_t spreadFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4107
  int32_t numOfElems = 0;
×
4108

4109
  // Only the pre-computing information loaded and actual data does not loaded
UNCOV
4110
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4111
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
UNCOV
4112
  int32_t               type = pInput->pData[0]->info.type;
×
4113

UNCOV
4114
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4115

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

4133
    if (GET_DOUBLE_VAL(&pInfo->min) > tmin) {
×
4134
      SET_DOUBLE_VAL(&pInfo->min, tmin);
×
4135
    }
4136

4137
    if (GET_DOUBLE_VAL(&pInfo->max) < tmax) {
×
4138
      SET_DOUBLE_VAL(&pInfo->max, tmax);
×
4139
    }
4140

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

UNCOV
4144
    int32_t start = pInput->startRowIndex;
×
4145
    // check the valid data one by one
UNCOV
4146
    for (int32_t i = start; i < pInput->numOfRows + start; ++i) {
×
UNCOV
4147
      if (colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
4148
        continue;
×
4149
      }
4150

UNCOV
4151
      char* data = colDataGetData(pCol, i);
×
4152

UNCOV
4153
      double v = 0;
×
UNCOV
4154
      GET_TYPED_DATA(v, double, type, data);
×
UNCOV
4155
      if (v < GET_DOUBLE_VAL(&pInfo->min)) {
×
UNCOV
4156
        SET_DOUBLE_VAL(&pInfo->min, v);
×
4157
      }
4158

UNCOV
4159
      if (v > GET_DOUBLE_VAL(&pInfo->max)) {
×
UNCOV
4160
        SET_DOUBLE_VAL(&pInfo->max, v);
×
4161
      }
4162

UNCOV
4163
      numOfElems += 1;
×
4164
    }
4165
  }
4166

UNCOV
4167
_spread_over:
×
4168
  // data in the check operation are all null, not output
UNCOV
4169
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
UNCOV
4170
  if (numOfElems > 0) {
×
UNCOV
4171
    pInfo->hasResult = true;
×
4172
  }
4173

UNCOV
4174
  return TSDB_CODE_SUCCESS;
×
4175
}
4176

UNCOV
4177
static void spreadTransferInfo(SSpreadInfo* pInput, SSpreadInfo* pOutput) {
×
UNCOV
4178
  pOutput->hasResult = pInput->hasResult;
×
UNCOV
4179
  if (pInput->max > pOutput->max) {
×
UNCOV
4180
    pOutput->max = pInput->max;
×
4181
  }
4182

UNCOV
4183
  if (pInput->min < pOutput->min) {
×
UNCOV
4184
    pOutput->min = pInput->min;
×
4185
  }
UNCOV
4186
}
×
4187

UNCOV
4188
int32_t spreadFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
4189
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4190
  SColumnInfoData*      pCol = pInput->pData[0];
×
4191

UNCOV
4192
  if (IS_NULL_TYPE(pCol->info.type)) {
×
4193
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4194
    return TSDB_CODE_SUCCESS;
×
4195
  }
4196

UNCOV
4197
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
4198
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4199
  }
4200

UNCOV
4201
  SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4202

UNCOV
4203
  int32_t start = pInput->startRowIndex;
×
UNCOV
4204
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
4205
    if (colDataIsNull_s(pCol, i)) continue;
×
UNCOV
4206
    char*        data = colDataGetData(pCol, i);
×
UNCOV
4207
    SSpreadInfo* pInputInfo = (SSpreadInfo*)varDataVal(data);
×
UNCOV
4208
    if (pInputInfo->hasResult) {
×
UNCOV
4209
      spreadTransferInfo(pInputInfo, pInfo);
×
4210
    }
4211
  }
4212

UNCOV
4213
  if (pInfo->hasResult) {
×
UNCOV
4214
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
4215
  }
4216

UNCOV
4217
  return TSDB_CODE_SUCCESS;
×
4218
}
4219

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

UNCOV
4230
int32_t spreadPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4231
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
4232
  SSpreadInfo*         pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4233
  int32_t              resultBytes = getSpreadInfoSize();
×
UNCOV
4234
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4235

UNCOV
4236
  if (NULL == res) {
×
4237
    return terrno;
×
4238
  }
UNCOV
4239
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
4240
  varDataSetLen(res, resultBytes);
×
4241

UNCOV
4242
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
4243
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
4244
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
4245
  if (NULL == pCol) {
×
4246
    code = terrno;
×
4247
    goto _exit;
×
4248
  }
4249

UNCOV
4250
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
UNCOV
4251
  if (TSDB_CODE_SUCCESS != code) {
×
4252
    goto _exit;
×
4253
  }
4254

UNCOV
4255
_exit:
×
UNCOV
4256
  taosMemoryFree(res);
×
UNCOV
4257
  return code;
×
4258
}
4259

UNCOV
4260
int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
4261
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
4262
  SSpreadInfo*         pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4263

UNCOV
4264
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
4265
  SSpreadInfo*         pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
UNCOV
4266
  spreadTransferInfo(pSBuf, pDBuf);
×
UNCOV
4267
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
4268
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
4269
  return TSDB_CODE_SUCCESS;
×
4270
}
4271

4272
int32_t getElapsedInfoSize() { return (int32_t)sizeof(SElapsedInfo); }
×
4273

UNCOV
4274
bool getElapsedFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4275
  pEnv->calcMemSize = sizeof(SElapsedInfo);
×
UNCOV
4276
  return true;
×
4277
}
4278

UNCOV
4279
int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4280
  if (pResultInfo->initialized) {
×
4281
    return TSDB_CODE_SUCCESS;
×
4282
  }
UNCOV
4283
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
4284
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4285
  }
4286

UNCOV
4287
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4288
  pInfo->result = 0;
×
UNCOV
4289
  pInfo->min = TSKEY_MAX;
×
UNCOV
4290
  pInfo->max = 0;
×
4291

UNCOV
4292
  if (pCtx->numOfParams > 1) {
×
UNCOV
4293
    pInfo->timeUnit = pCtx->param[1].param.i;
×
4294
  } else {
UNCOV
4295
    pInfo->timeUnit = 1;
×
4296
  }
4297

UNCOV
4298
  return TSDB_CODE_SUCCESS;
×
4299
}
4300

UNCOV
4301
int32_t elapsedFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4302
  int32_t numOfElems = 0;
×
4303

4304
  // Only the pre-computing information loaded and actual data does not loaded
UNCOV
4305
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4306
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];
×
4307

UNCOV
4308
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4309

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

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

UNCOV
4344
    SColumnInfoData* pCol = pInput->pData[0];
×
4345

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

UNCOV
4355
      if (pCtx->end.key == INT64_MIN) {
×
UNCOV
4356
        pInfo->min =
×
UNCOV
4357
            (pInfo->min > ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->min;
×
4358
      } else {
4359
        pInfo->min = pCtx->end.key;
×
4360
      }
4361
    } else {
UNCOV
4362
      if (pCtx->start.key == INT64_MIN) {
×
UNCOV
4363
        pInfo->min = (pInfo->min > ptsList[start]) ? ptsList[start] : pInfo->min;
×
4364
      } else {
UNCOV
4365
        pInfo->min = pCtx->start.key;
×
4366
      }
4367

UNCOV
4368
      if (pCtx->end.key == INT64_MIN) {
×
UNCOV
4369
        pInfo->max =
×
UNCOV
4370
            (pInfo->max < ptsList[start + pInput->numOfRows - 1]) ? ptsList[start + pInput->numOfRows - 1] : pInfo->max;
×
4371
      } else {
UNCOV
4372
        pInfo->max = pCtx->end.key + 1;
×
4373
      }
4374
    }
4375
  }
4376

UNCOV
4377
_elapsed_over:
×
4378
  // data in the check operation are all null, not output
UNCOV
4379
  SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1);
×
4380

UNCOV
4381
  return TSDB_CODE_SUCCESS;
×
4382
}
4383

4384
static void elapsedTransferInfo(SElapsedInfo* pInput, SElapsedInfo* pOutput) {
×
4385
  pOutput->timeUnit = pInput->timeUnit;
×
4386
  if (pOutput->min > pInput->min) {
×
4387
    pOutput->min = pInput->min;
×
4388
  }
4389

4390
  if (pOutput->max < pInput->max) {
×
4391
    pOutput->max = pInput->max;
×
4392
  }
4393
}
×
4394

4395
int32_t elapsedFunctionMerge(SqlFunctionCtx* pCtx) {
×
4396
  SInputColumnInfoData* pInput = &pCtx->input;
×
4397
  SColumnInfoData*      pCol = pInput->pData[0];
×
4398
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
4399
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4400
  }
4401

4402
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4403

4404
  int32_t start = pInput->startRowIndex;
×
4405

4406
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
4407
    char*         data = colDataGetData(pCol, i);
×
4408
    SElapsedInfo* pInputInfo = (SElapsedInfo*)varDataVal(data);
×
4409
    elapsedTransferInfo(pInputInfo, pInfo);
×
4410
  }
4411

4412
  SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
4413
  return TSDB_CODE_SUCCESS;
×
4414
}
4415

UNCOV
4416
int32_t elapsedFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4417
  SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4418
  double        result = (double)pInfo->max - (double)pInfo->min;
×
UNCOV
4419
  result = (result >= 0) ? result : -result;
×
UNCOV
4420
  pInfo->result = result / pInfo->timeUnit;
×
UNCOV
4421
  return functionFinalize(pCtx, pBlock);
×
4422
}
4423

4424
int32_t elapsedPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
4425
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
4426
  SElapsedInfo*        pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4427
  int32_t              resultBytes = getElapsedInfoSize();
×
4428
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4429

4430
  if (NULL == res) {
×
4431
    return terrno;
×
4432
  }
4433
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
4434
  varDataSetLen(res, resultBytes);
×
4435

4436
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
4437
  int32_t          code = TSDB_CODE_SUCCESS;
×
4438
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
4439
  if (NULL == pCol) {
×
4440
    code = terrno;
×
4441
    goto _exit;
×
4442
  }
4443

4444
  code = colDataSetVal(pCol, pBlock->info.rows, res, false);
×
4445
  if (TSDB_CODE_SUCCESS != code) {
×
4446
    goto _exit;
×
4447
  }
4448
_exit:
×
4449
  taosMemoryFree(res);
×
4450
  return code;
×
4451
}
4452

4453
int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4454
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4455
  SElapsedInfo*        pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4456

4457
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4458
  SElapsedInfo*        pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4459

4460
  elapsedTransferInfo(pSBuf, pDBuf);
×
4461
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4462
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4463
  return TSDB_CODE_SUCCESS;
×
4464
}
4465

UNCOV
4466
int32_t getHistogramInfoSize() {
×
UNCOV
4467
  return (int32_t)sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
×
4468
}
4469

UNCOV
4470
bool getHistogramFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4471
  pEnv->calcMemSize = sizeof(SHistoFuncInfo) + HISTOGRAM_MAX_BINS_NUM * sizeof(SHistoFuncBin);
×
UNCOV
4472
  return true;
×
4473
}
4474

UNCOV
4475
static int8_t getHistogramBinType(char* binTypeStr) {
×
4476
  int8_t binType;
UNCOV
4477
  if (strcasecmp(binTypeStr, "user_input") == 0) {
×
UNCOV
4478
    binType = USER_INPUT_BIN;
×
UNCOV
4479
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
×
UNCOV
4480
    binType = LINEAR_BIN;
×
UNCOV
4481
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
×
UNCOV
4482
    binType = LOG_BIN;
×
4483
  } else {
4484
    binType = UNKNOWN_BIN;
×
4485
  }
4486

UNCOV
4487
  return binType;
×
4488
}
4489

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

UNCOV
4502
    cJSON* start = cJSON_GetObjectItem(binDesc, "start");
×
UNCOV
4503
    cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
×
UNCOV
4504
    cJSON* width = cJSON_GetObjectItem(binDesc, "width");
×
UNCOV
4505
    cJSON* count = cJSON_GetObjectItem(binDesc, "count");
×
UNCOV
4506
    cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
×
4507

UNCOV
4508
    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
×
4509
      cJSON_Delete(binDesc);
×
4510
      return TSDB_CODE_FAILED;
×
4511
    }
4512

UNCOV
4513
    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
×
4514
      cJSON_Delete(binDesc);
×
4515
      return TSDB_CODE_FAILED;
×
4516
    }
4517

UNCOV
4518
    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
×
UNCOV
4519
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
×
4520
      cJSON_Delete(binDesc);
×
4521
      return TSDB_CODE_FAILED;
×
4522
    }
4523

UNCOV
4524
    int32_t counter = (int32_t)count->valueint;
×
UNCOV
4525
    if (infinity->valueint == false) {
×
UNCOV
4526
      startIndex = 0;
×
UNCOV
4527
      numOfBins = counter + 1;
×
4528
    } else {
UNCOV
4529
      startIndex = 1;
×
UNCOV
4530
      numOfBins = counter + 3;
×
4531
    }
4532

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

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

UNCOV
4632
  pInfo->numOfBins = numOfBins - 1;
×
UNCOV
4633
  pInfo->normalized = normalized;
×
UNCOV
4634
  for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
×
UNCOV
4635
    pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
×
UNCOV
4636
    pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
×
UNCOV
4637
    pInfo->bins[i].count = 0;
×
4638
  }
4639

UNCOV
4640
  taosMemoryFree(intervals);
×
UNCOV
4641
  cJSON_Delete(binDesc);
×
4642

UNCOV
4643
  return TSDB_CODE_SUCCESS;
×
4644
}
4645

UNCOV
4646
int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
4647
  if (pResultInfo->initialized) {
×
4648
    return TSDB_CODE_SUCCESS;
×
4649
  }
UNCOV
4650
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
4651
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
4652
  }
4653

UNCOV
4654
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo);
×
UNCOV
4655
  pInfo->numOfBins = 0;
×
UNCOV
4656
  pInfo->totalCount = 0;
×
UNCOV
4657
  pInfo->normalized = 0;
×
4658

UNCOV
4659
  char* binTypeStr = taosStrndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz));
×
UNCOV
4660
  if (binTypeStr == NULL) {
×
4661
    return terrno;
×
4662
  }
UNCOV
4663
  int8_t binType = getHistogramBinType(binTypeStr);
×
UNCOV
4664
  taosMemoryFree(binTypeStr);
×
4665

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

UNCOV
4685
  return TSDB_CODE_SUCCESS;
×
4686
}
4687

UNCOV
4688
static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) {
×
UNCOV
4689
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4690

UNCOV
4691
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4692
  SColumnInfoData*      pCol = pInput->pData[0];
×
4693

UNCOV
4694
  int32_t type = pInput->pData[0]->info.type;
×
4695

UNCOV
4696
  int32_t start = pInput->startRowIndex;
×
UNCOV
4697
  int32_t numOfRows = pInput->numOfRows;
×
4698

UNCOV
4699
  int32_t numOfElems = 0;
×
UNCOV
4700
  for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
4701
    if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
×
UNCOV
4702
      continue;
×
4703
    }
4704

UNCOV
4705
    numOfElems++;
×
4706

UNCOV
4707
    char*  data = colDataGetData(pCol, i);
×
4708
    double v;
UNCOV
4709
    GET_TYPED_DATA(v, double, type, data);
×
4710

UNCOV
4711
    for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
×
UNCOV
4712
      if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
×
UNCOV
4713
        pInfo->bins[k].count++;
×
UNCOV
4714
        pInfo->totalCount++;
×
UNCOV
4715
        break;
×
4716
      }
4717
    }
4718
  }
4719

UNCOV
4720
  if (!isPartial) {
×
UNCOV
4721
    GET_RES_INFO(pCtx)->numOfRes = pInfo->numOfBins;
×
4722
  } else {
UNCOV
4723
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
4724
  }
UNCOV
4725
  return TSDB_CODE_SUCCESS;
×
4726
}
4727

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

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

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

UNCOV
4743
int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
4744
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4745
  SColumnInfoData*      pCol = pInput->pData[0];
×
UNCOV
4746
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
4747
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
4748
  }
4749

UNCOV
4750
  SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4751

UNCOV
4752
  int32_t start = pInput->startRowIndex;
×
4753

UNCOV
4754
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
4755
    char*           data = colDataGetData(pCol, i);
×
UNCOV
4756
    SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data);
×
UNCOV
4757
    histogramTransferInfo(pInputInfo, pInfo);
×
4758
  }
4759

UNCOV
4760
  SET_VAL(GET_RES_INFO(pCtx), pInfo->numOfBins, pInfo->numOfBins);
×
UNCOV
4761
  return TSDB_CODE_SUCCESS;
×
4762
}
4763

UNCOV
4764
int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4765
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
4766
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4767
  int32_t              slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
4768
  SColumnInfoData*     pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
4769
  int32_t              code = TSDB_CODE_SUCCESS;
×
4770

UNCOV
4771
  int32_t currentRow = pBlock->info.rows;
×
UNCOV
4772
  if (NULL == pCol) {
×
4773
    return TSDB_CODE_OUT_OF_RANGE;
×
4774
  }
4775

UNCOV
4776
  if (pInfo->normalized) {
×
UNCOV
4777
    for (int32_t k = 0; k < pResInfo->numOfRes; ++k) {
×
UNCOV
4778
      if (pInfo->totalCount != 0) {
×
UNCOV
4779
        pInfo->bins[k].percentage = pInfo->bins[k].count / (double)pInfo->totalCount;
×
4780
      } else {
UNCOV
4781
        pInfo->bins[k].percentage = 0;
×
4782
      }
4783
    }
4784
  }
4785

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

UNCOV
4806
  return code;
×
4807
}
4808

UNCOV
4809
int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
4810
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
4811
  SHistoFuncInfo*      pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
4812
  int32_t              resultBytes = getHistogramInfoSize();
×
UNCOV
4813
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
4814

UNCOV
4815
  if (NULL == res) {
×
4816
    return terrno;
×
4817
  }
UNCOV
4818
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
4819
  varDataSetLen(res, resultBytes);
×
4820

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

UNCOV
4830
_exit:
×
UNCOV
4831
  taosMemoryFree(res);
×
UNCOV
4832
  return code;
×
4833
}
4834

4835
int32_t histogramCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
4836
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
4837
  SHistoFuncInfo*      pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
4838

4839
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
4840
  SHistoFuncInfo*      pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
4841

4842
  histogramTransferInfo(pSBuf, pDBuf);
×
4843
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
4844
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
4845
  return TSDB_CODE_SUCCESS;
×
4846
}
4847

UNCOV
4848
int32_t getHLLInfoSize() { return (int32_t)sizeof(SHLLInfo); }
×
4849

UNCOV
4850
bool getHLLFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
4851
  pEnv->calcMemSize = sizeof(SHLLInfo);
×
UNCOV
4852
  return true;
×
4853
}
4854

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

UNCOV
4870
static void hllBucketHisto(uint8_t* buckets, int32_t* bucketHisto) {
×
UNCOV
4871
  uint64_t* word = (uint64_t*)buckets;
×
4872
  uint8_t*  bytes;
4873

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

UNCOV
4905
static double hllSigma(double x) {
×
UNCOV
4906
  if (x == 1.0) return INFINITY;
×
4907
  double zPrime;
UNCOV
4908
  double y = 1;
×
UNCOV
4909
  double z = x;
×
4910
  do {
UNCOV
4911
    x *= x;
×
UNCOV
4912
    zPrime = z;
×
UNCOV
4913
    z += x * y;
×
UNCOV
4914
    y += y;
×
UNCOV
4915
  } while (zPrime != z);
×
UNCOV
4916
  return z;
×
4917
}
4918

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

UNCOV
4926
  double z = m * hllTau((m - buckethisto[HLL_DATA_BITS + 1]) / (double)m);
×
UNCOV
4927
  for (int j = HLL_DATA_BITS; j >= 1; --j) {
×
UNCOV
4928
    z += buckethisto[j];
×
UNCOV
4929
    z *= 0.5;
×
4930
  }
4931

UNCOV
4932
  z += m * hllSigma(buckethisto[0] / (double)m);
×
UNCOV
4933
  double E = (double)llroundl(HLL_ALPHA_INF * m * m / z);
×
4934

UNCOV
4935
  return (uint64_t)E;
×
4936
}
4937

UNCOV
4938
int32_t hllFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
4939
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
4940

UNCOV
4941
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4942
  SColumnInfoData*      pCol = pInput->pData[0];
×
4943

UNCOV
4944
  int32_t type = pCol->info.type;
×
UNCOV
4945
  int32_t bytes = pCol->info.bytes;
×
4946

UNCOV
4947
  int32_t start = pInput->startRowIndex;
×
UNCOV
4948
  int32_t numOfRows = pInput->numOfRows;
×
4949

UNCOV
4950
  int32_t numOfElems = 0;
×
UNCOV
4951
  if (IS_NULL_TYPE(type)) {
×
UNCOV
4952
    goto _hll_over;
×
4953
  }
4954

UNCOV
4955
  for (int32_t i = start; i < numOfRows + start; ++i) {
×
UNCOV
4956
    if (pCol->hasNull && colDataIsNull_s(pCol, i)) {
×
UNCOV
4957
      continue;
×
4958
    }
4959

UNCOV
4960
    numOfElems++;
×
4961

UNCOV
4962
    char* data = colDataGetData(pCol, i);
×
UNCOV
4963
    if (IS_VAR_DATA_TYPE(type)) {
×
UNCOV
4964
      bytes = varDataLen(data);
×
UNCOV
4965
      data = varDataVal(data);
×
4966
    }
4967

UNCOV
4968
    int32_t index = 0;
×
UNCOV
4969
    uint8_t count = hllCountNum(data, bytes, &index);
×
UNCOV
4970
    uint8_t oldcount = pInfo->buckets[index];
×
UNCOV
4971
    if (count > oldcount) {
×
UNCOV
4972
      pInfo->buckets[index] = count;
×
4973
    }
4974
  }
4975

UNCOV
4976
_hll_over:
×
UNCOV
4977
  pInfo->totalCount += numOfElems;
×
4978

UNCOV
4979
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
×
UNCOV
4980
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
4981
  } else {
UNCOV
4982
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
4983
  }
4984

UNCOV
4985
  return TSDB_CODE_SUCCESS;
×
4986
}
4987

UNCOV
4988
static void hllTransferInfo(SHLLInfo* pInput, SHLLInfo* pOutput) {
×
UNCOV
4989
  for (int32_t k = 0; k < HLL_BUCKETS; ++k) {
×
UNCOV
4990
    if (pOutput->buckets[k] < pInput->buckets[k]) {
×
UNCOV
4991
      pOutput->buckets[k] = pInput->buckets[k];
×
4992
    }
4993
  }
UNCOV
4994
  pOutput->totalCount += pInput->totalCount;
×
UNCOV
4995
}
×
4996

UNCOV
4997
int32_t hllFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
4998
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
4999
  SColumnInfoData*      pCol = pInput->pData[0];
×
5000

UNCOV
5001
  if (IS_NULL_TYPE(pCol->info.type)) {
×
5002
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
5003
    return TSDB_CODE_SUCCESS;
×
5004
  }
5005

UNCOV
5006
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
5007
    return TSDB_CODE_SUCCESS;
×
5008
  }
5009

UNCOV
5010
  SHLLInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
5011

UNCOV
5012
  int32_t start = pInput->startRowIndex;
×
5013

UNCOV
5014
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
5015
    if (colDataIsNull_s(pCol, i)) continue;
×
UNCOV
5016
    char*     data = colDataGetData(pCol, i);
×
UNCOV
5017
    SHLLInfo* pInputInfo = (SHLLInfo*)varDataVal(data);
×
UNCOV
5018
    hllTransferInfo(pInputInfo, pInfo);
×
5019
  }
5020

UNCOV
5021
  if (pInfo->totalCount == 0 && !tsCountAlwaysReturnValue) {
×
UNCOV
5022
    SET_VAL(GET_RES_INFO(pCtx), 0, 1);
×
5023
  } else {
UNCOV
5024
    SET_VAL(GET_RES_INFO(pCtx), 1, 1);
×
5025
  }
5026

UNCOV
5027
  return TSDB_CODE_SUCCESS;
×
5028
}
5029

UNCOV
5030
int32_t hllFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5031
  SResultRowEntryInfo* pInfo = GET_RES_INFO(pCtx);
×
5032

UNCOV
5033
  SHLLInfo* pHllInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
5034
  pHllInfo->result = hllCountCnt(pHllInfo->buckets);
×
UNCOV
5035
  if (tsCountAlwaysReturnValue && pHllInfo->result == 0) {
×
UNCOV
5036
    pInfo->numOfRes = 1;
×
5037
  }
5038

UNCOV
5039
  return functionFinalize(pCtx, pBlock);
×
5040
}
5041

UNCOV
5042
int32_t hllPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5043
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5044
  SHLLInfo*            pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
5045
  int32_t              resultBytes = getHLLInfoSize();
×
UNCOV
5046
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
5047

UNCOV
5048
  if (NULL == res) {
×
5049
    return terrno;
×
5050
  }
UNCOV
5051
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
5052
  varDataSetLen(res, resultBytes);
×
5053

UNCOV
5054
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
5055
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
5056
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
5057
  if (NULL == pCol) {
×
5058
    code = terrno;
×
5059
    goto _exit;
×
5060
  }
5061

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

UNCOV
5064
_exit:
×
UNCOV
5065
  taosMemoryFree(res);
×
UNCOV
5066
  return code;
×
5067
}
5068

UNCOV
5069
int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
UNCOV
5070
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
UNCOV
5071
  SHLLInfo*            pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
5072

UNCOV
5073
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
UNCOV
5074
  SHLLInfo*            pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
5075

UNCOV
5076
  hllTransferInfo(pSBuf, pDBuf);
×
UNCOV
5077
  pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes);
×
UNCOV
5078
  pDResInfo->isNullRes &= pSResInfo->isNullRes;
×
UNCOV
5079
  return TSDB_CODE_SUCCESS;
×
5080
}
5081

UNCOV
5082
bool getStateFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5083
  pEnv->calcMemSize = sizeof(SStateInfo);
×
UNCOV
5084
  return true;
×
5085
}
5086

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

UNCOV
5105
  return opType;
×
5106
}
5107

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

UNCOV
5168
int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5169
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5170
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5171
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5172

UNCOV
5173
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5174
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5175

UNCOV
5176
  SColumnInfoData* pInputCol = pInput->pData[0];
×
5177

UNCOV
5178
  int32_t          numOfElems = 0;
×
UNCOV
5179
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5180

UNCOV
5181
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
×
UNCOV
5182
  if (STATE_OPER_INVALID == op) {
×
5183
    return 0;
×
5184
  }
5185

UNCOV
5186
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5187
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
×
5188
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5189
    } else {
UNCOV
5190
      pInfo->prevTs = tsList[i];
×
5191
    }
5192

UNCOV
5193
    pInfo->isPrevTsSet = true;
×
UNCOV
5194
    numOfElems++;
×
5195

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

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

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

5221
    // handle selectivity
UNCOV
5222
    if (pCtx->subsidiaries.num > 0) {
×
UNCOV
5223
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
×
UNCOV
5224
      if (TSDB_CODE_SUCCESS != code) {
×
5225
        return code;
×
5226
      }
5227
    }
5228
  }
5229

UNCOV
5230
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5231
  return TSDB_CODE_SUCCESS;
×
5232
}
5233

UNCOV
5234
int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5235
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5236
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5237
  SStateInfo*          pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5238

UNCOV
5239
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5240
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5241

UNCOV
5242
  SColumnInfoData* pInputCol = pInput->pData[0];
×
5243

UNCOV
5244
  int32_t          numOfElems = 0;
×
UNCOV
5245
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5246

5247
  // TODO: process timeUnit for different db precisions
UNCOV
5248
  int32_t timeUnit = 1;
×
UNCOV
5249
  if (pCtx->numOfParams == 5) {  // TODO: param number incorrect
×
UNCOV
5250
    timeUnit = pCtx->param[3].param.i;
×
5251
  }
5252

UNCOV
5253
  int8_t op = getStateOpType(varDataVal(pCtx->param[1].param.pz));
×
UNCOV
5254
  if (STATE_OPER_INVALID == op) {
×
5255
    return TSDB_CODE_INVALID_PARA;
×
5256
  }
5257

UNCOV
5258
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5259
    if (pInfo->isPrevTsSet == true && tsList[i] == pInfo->prevTs) {
×
5260
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
5261
    } else {
UNCOV
5262
      pInfo->prevTs = tsList[i];
×
5263
    }
5264

UNCOV
5265
    pInfo->isPrevTsSet = true;
×
UNCOV
5266
    numOfElems++;
×
5267

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

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

5297
    // handle selectivity
UNCOV
5298
    if (pCtx->subsidiaries.num > 0) {
×
UNCOV
5299
      code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1);
×
UNCOV
5300
      if (TSDB_CODE_SUCCESS != code) {
×
5301
        return code;
×
5302
      }
5303
    }
5304
  }
5305

UNCOV
5306
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5307
  return TSDB_CODE_SUCCESS;
×
5308
}
5309

UNCOV
5310
bool getCsumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5311
  pEnv->calcMemSize = sizeof(SSumRes);
×
UNCOV
5312
  return true;
×
5313
}
5314

UNCOV
5315
int32_t csumFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5316
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5317
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5318
  SSumRes*             pSumRes = GET_ROWCELL_INTERBUF(pResInfo);
×
5319

UNCOV
5320
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5321
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5322

UNCOV
5323
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5324
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5325

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

UNCOV
5337
    int32_t pos = startOffset + numOfElems;
×
UNCOV
5338
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
5339
      // colDataSetNULL(pOutput, i);
UNCOV
5340
      continue;
×
5341
    }
5342

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

5375
    // handle selectivity
UNCOV
5376
    if (pCtx->subsidiaries.num > 0) {
×
UNCOV
5377
      code = appendSelectivityValue(pCtx, i, pos);
×
UNCOV
5378
      if (TSDB_CODE_SUCCESS != code) {
×
5379
        return code;
×
5380
      }
5381
    }
5382

UNCOV
5383
    numOfElems++;
×
5384
  }
5385

UNCOV
5386
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5387
  return TSDB_CODE_SUCCESS;
×
5388
}
5389

UNCOV
5390
bool getMavgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
×
UNCOV
5391
  pEnv->calcMemSize = sizeof(SMavgInfo) + MAVG_MAX_POINTS_NUM * sizeof(double);
×
UNCOV
5392
  return true;
×
5393
}
5394

UNCOV
5395
int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
5396
  if (pResultInfo->initialized) {
×
UNCOV
5397
    return TSDB_CODE_SUCCESS;
×
5398
  }
UNCOV
5399
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
5400
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5401
  }
5402

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

UNCOV
5414
  return TSDB_CODE_SUCCESS;
×
5415
}
5416

UNCOV
5417
int32_t mavgFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5418
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5419
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5420
  SMavgInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5421

UNCOV
5422
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
5423
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
×
5424

UNCOV
5425
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5426
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
×
UNCOV
5427
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5428

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

UNCOV
5440
    int32_t pos = startOffset + numOfElems;
×
UNCOV
5441
    if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
5442
      // colDataSetNULL(pOutput, i);
UNCOV
5443
      continue;
×
5444
    }
5445

UNCOV
5446
    char*  data = colDataGetData(pInputCol, i);
×
5447
    double v;
UNCOV
5448
    GET_TYPED_DATA(v, double, type, data);
×
5449

UNCOV
5450
    if (!pInfo->pointsMeet && (pInfo->pos < pInfo->numOfPoints - 1)) {
×
UNCOV
5451
      pInfo->points[pInfo->pos] = v;
×
UNCOV
5452
      pInfo->sum += v;
×
5453
    } else {
UNCOV
5454
      if (!pInfo->pointsMeet && (pInfo->pos == pInfo->numOfPoints - 1)) {
×
UNCOV
5455
        pInfo->sum += v;
×
UNCOV
5456
        pInfo->pointsMeet = true;
×
5457
      } else {
UNCOV
5458
        pInfo->sum = pInfo->sum + v - pInfo->points[pInfo->pos];
×
5459
      }
5460

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

5473
      // handle selectivity
UNCOV
5474
      if (pCtx->subsidiaries.num > 0) {
×
UNCOV
5475
        code = appendSelectivityValue(pCtx, i, pos);
×
UNCOV
5476
        if (TSDB_CODE_SUCCESS != code) {
×
5477
          return code;
×
5478
        }
5479
      }
5480

UNCOV
5481
      numOfElems++;
×
5482
    }
5483

UNCOV
5484
    pInfo->pos++;
×
UNCOV
5485
    if (pInfo->pos == pInfo->numOfPoints) {
×
UNCOV
5486
      pInfo->pos = 0;
×
5487
    }
5488
  }
5489

UNCOV
5490
  pResInfo->numOfRes = numOfElems;
×
UNCOV
5491
  return TSDB_CODE_SUCCESS;
×
5492
}
5493

UNCOV
5494
static SSampleInfo* getSampleOutputInfo(SqlFunctionCtx* pCtx) {
×
UNCOV
5495
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5496
  SSampleInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5497

UNCOV
5498
  pInfo->data = (char*)pInfo + sizeof(SSampleInfo);
×
UNCOV
5499
  pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes);
×
5500

UNCOV
5501
  return pInfo;
×
5502
}
5503

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

UNCOV
5512
int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
5513
  if (pResultInfo->initialized) {
×
5514
    return TSDB_CODE_SUCCESS;
×
5515
  }
UNCOV
5516
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
5517
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5518
  }
5519

UNCOV
5520
  taosSeedRand(taosSafeRand());
×
5521

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

UNCOV
5533
  return TSDB_CODE_SUCCESS;
×
5534
}
5535

UNCOV
5536
static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) {
×
UNCOV
5537
  assignVal(pInfo->data + index * pInfo->colBytes, data, pInfo->colBytes, pInfo->colType);
×
UNCOV
5538
}
×
5539

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

UNCOV
5564
  return TSDB_CODE_SUCCESS;
×
5565
}
5566

UNCOV
5567
int32_t sampleFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5568
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5569
  SSampleInfo*         pInfo = getSampleOutputInfo(pCtx);
×
5570

UNCOV
5571
  SInputColumnInfoData* pInput = &pCtx->input;
×
5572

UNCOV
5573
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5574
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
×
UNCOV
5575
    if (colDataIsNull_s(pInputCol, i)) {
×
UNCOV
5576
      continue;
×
5577
    }
5578

UNCOV
5579
    char*   data = colDataGetData(pInputCol, i);
×
UNCOV
5580
    int32_t code = doReservoirSample(pCtx, pInfo, data, i);
×
UNCOV
5581
    if (code != TSDB_CODE_SUCCESS) {
×
5582
      return code;
×
5583
    }
5584
  }
5585

UNCOV
5586
  if (pInfo->numSampled == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
×
UNCOV
5587
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
×
UNCOV
5588
    if (code != TSDB_CODE_SUCCESS) {
×
5589
      return code;
×
5590
    }
UNCOV
5591
    pInfo->nullTupleSaved = true;
×
5592
  }
5593

UNCOV
5594
  SET_VAL(pResInfo, pInfo->numSampled, pInfo->numSampled);
×
UNCOV
5595
  return TSDB_CODE_SUCCESS;
×
5596
}
5597

UNCOV
5598
int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
5599
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
5600
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
×
5601

UNCOV
5602
  SSampleInfo* pInfo = getSampleOutputInfo(pCtx);
×
UNCOV
5603
  pEntryInfo->complete = true;
×
5604

UNCOV
5605
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
5606
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
5607
  if (NULL == pCol) {
×
5608
    return TSDB_CODE_OUT_OF_RANGE;
×
5609
  }
5610

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

UNCOV
5628
  return code;
×
5629
}
5630

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

5641
int32_t tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
5642
#if 0
5643
  if (!functionSetup(pCtx, pResultInfo)) {
5644
    return false;
5645
  }
5646

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

5662
  pInfo->pItems = (STailItem**)((char*)pInfo + sizeof(STailInfo));
5663
  char* pItem = (char*)pInfo->pItems + pInfo->numOfPoints * POINTER_BYTES;
5664

5665
  size_t unitSize = sizeof(STailItem) + pInfo->colBytes;
5666
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5667
    pInfo->pItems[i] = (STailItem*)(pItem + i * unitSize);
5668
    pInfo->pItems[i]->isNull = false;
5669
  }
5670
#endif
5671

5672
  return TSDB_CODE_SUCCESS;
×
5673
}
5674

5675
static void tailAssignResult(STailItem* pItem, char* data, int32_t colBytes, TSKEY ts, bool isNull) {
×
5676
#if 0
5677
  pItem->timestamp = ts;
5678
  if (isNull) {
5679
    pItem->isNull = true;
5680
  } else {
5681
    pItem->isNull = false;
5682
    memcpy(pItem->data, data, colBytes);
5683
  }
5684
#endif
5685
}
×
5686

5687
#if 0
5688
static int32_t tailCompFn(const void* p1, const void* p2, const void* param) {
5689
  STailItem* d1 = *(STailItem**)p1;
5690
  STailItem* d2 = *(STailItem**)p2;
5691
  return compareInt64Val(&d1->timestamp, &d2->timestamp);
5692
}
5693

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

5707
int32_t tailFunction(SqlFunctionCtx* pCtx) {
×
5708
#if 0
5709
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5710
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5711

5712
  SInputColumnInfoData* pInput = &pCtx->input;
5713
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5714

5715
  SColumnInfoData* pInputCol = pInput->pData[0];
5716
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5717

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

5729
  taosqsort(pInfo->pItems, pInfo->numOfPoints, POINTER_BYTES, NULL, tailCompFn);
5730

5731
  for (int32_t i = 0; i < pInfo->numOfPoints; ++i) {
5732
    int32_t    pos = startOffset + i;
5733
    STailItem* pItem = pInfo->pItems[i];
5734
    if (pItem->isNull) {
5735
      colDataSetNULL(pOutput, pos);
5736
    } else {
5737
      colDataSetVal(pOutput, pos, pItem->data, false);
5738
    }
5739
  }
5740

5741
  return pInfo->numOfPoints;
5742
#endif
5743
  return 0;
×
5744
}
5745

5746
int32_t tailFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
5747
#if 0
5748
  SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx);
5749
  STailInfo*           pInfo = GET_ROWCELL_INTERBUF(pEntryInfo);
5750
  pEntryInfo->complete = true;
5751

5752
  int32_t type = pCtx->input.pData[0]->info.type;
5753
  int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
5754

5755
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
5756

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

5765
  return pEntryInfo->numOfRes;
5766
#endif
5767
  return 0;
×
5768
}
5769

5770
bool getUniqueFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
5771
#if 0
5772
  pEnv->calcMemSize = sizeof(SUniqueInfo) + UNIQUE_MAX_RESULT_SIZE;
5773
#endif
5774
  return true;
×
5775
}
5776

5777
int32_t uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
5778
#if 0
5779
  if (!functionSetup(pCtx, pResInfo)) {
5780
    return false;
5781
  }
5782

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

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

5813
  int32_t      hashKeyBytes = IS_VAR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
5814
  SUniqueItem* pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes);
5815
  if (pHashItem == NULL) {
5816
    int32_t      size = sizeof(SUniqueItem) + pInfo->colBytes;
5817
    SUniqueItem* pItem = (SUniqueItem*)(pInfo->pItems + pInfo->numOfPoints * size);
5818
    pItem->timestamp = ts;
5819
    memcpy(pItem->data, data, pInfo->colBytes);
5820

5821
    taosHashPut(pInfo->pHash, data, hashKeyBytes, (char*)pItem, sizeof(SUniqueItem*));
5822
    pInfo->numOfPoints++;
5823
  } else if (pHashItem->timestamp > ts) {
5824
    pHashItem->timestamp = ts;
5825
  }
5826
}
5827
#endif
5828

5829
int32_t uniqueFunction(SqlFunctionCtx* pCtx) {
×
5830
#if 0
5831
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
5832
  SUniqueInfo*         pInfo = GET_ROWCELL_INTERBUF(pResInfo);
5833

5834
  SInputColumnInfoData* pInput = &pCtx->input;
5835
  TSKEY*                tsList = (int64_t*)pInput->pPTS->pData;
5836

5837
  SColumnInfoData* pInputCol = pInput->pData[0];
5838
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
5839
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
5840

5841
  int32_t startOffset = pCtx->offset;
5842
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
5843
    char* data = colDataGetData(pInputCol, i);
5844
    doUniqueAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i));
5845

5846
    if (sizeof(SUniqueInfo) + pInfo->numOfPoints * (sizeof(SUniqueItem) + pInfo->colBytes) >= UNIQUE_MAX_RESULT_SIZE) {
5847
      taosHashCleanup(pInfo->pHash);
5848
      return 0;
5849
    }
5850
  }
5851

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

5864
  return pInfo->numOfPoints;
5865
#endif
5866
  return 0;
×
5867
}
5868

UNCOV
5869
bool getModeFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
5870
  pEnv->calcMemSize = sizeof(SModeInfo);
×
UNCOV
5871
  return true;
×
5872
}
5873

UNCOV
5874
int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
5875
  if (pResInfo->initialized) {
×
5876
    return TSDB_CODE_SUCCESS;
×
5877
  }
UNCOV
5878
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
5879
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
5880
  }
5881

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

UNCOV
5896
  pInfo->buf = taosMemoryMalloc(pInfo->colBytes);
×
UNCOV
5897
  if (NULL == pInfo->buf) {
×
5898
    taosHashCleanup(pInfo->pHash);
×
5899
    pInfo->pHash = NULL;
×
5900
    return terrno;
×
5901
  }
UNCOV
5902
  pCtx->needCleanup = true;
×
UNCOV
5903
  return TSDB_CODE_SUCCESS;
×
5904
}
5905

UNCOV
5906
static void modeFunctionCleanup(SModeInfo* pInfo) {
×
UNCOV
5907
  taosHashCleanup(pInfo->pHash);
×
UNCOV
5908
  pInfo->pHash = NULL;
×
UNCOV
5909
  taosMemoryFreeClear(pInfo->buf);
×
UNCOV
5910
}
×
5911

5912
void modeFunctionCleanupExt(SqlFunctionCtx* pCtx) {
×
5913
  if (pCtx == NULL || GET_RES_INFO(pCtx) == NULL || GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)) == NULL) {
×
5914
    return;
×
5915
  }
5916
  modeFunctionCleanup(GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)));
×
5917
}
5918

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

UNCOV
5930
  return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore);
×
5931
}
5932

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

UNCOV
5946
  SModeItem* pHashItem = (SModeItem*)taosHashGet(pInfo->pHash, data, hashKeyBytes);
×
UNCOV
5947
  if (pHashItem == NULL) {
×
UNCOV
5948
    int32_t   size = sizeof(SModeItem);
×
UNCOV
5949
    SModeItem item = {0};
×
5950

UNCOV
5951
    item.count += 1;
×
UNCOV
5952
    code = saveModeTupleData(pCtx, data, pInfo, &item.dataPos);
×
UNCOV
5953
    if (code != TSDB_CODE_SUCCESS) {
×
5954
      return code;
×
5955
    }
5956

UNCOV
5957
    if (pCtx->subsidiaries.num > 0) {
×
UNCOV
5958
      code = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &item.tuplePos);
×
UNCOV
5959
      if (code != TSDB_CODE_SUCCESS) {
×
5960
        return code;
×
5961
      }
5962
    }
5963

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

UNCOV
5978
  return code;
×
5979
}
5980

UNCOV
5981
int32_t modeFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
5982
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
5983
  SModeInfo*           pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
5984

UNCOV
5985
  SInputColumnInfoData* pInput = &pCtx->input;
×
5986

UNCOV
5987
  SColumnInfoData* pInputCol = pInput->pData[0];
×
UNCOV
5988
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
5989

UNCOV
5990
  int32_t numOfElems = 0;
×
UNCOV
5991
  int32_t startOffset = pCtx->offset;
×
UNCOV
5992
  for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; ++i) {
×
UNCOV
5993
    if (colDataIsNull_s(pInputCol, i)) {
×
UNCOV
5994
      continue;
×
5995
    }
UNCOV
5996
    numOfElems++;
×
5997

UNCOV
5998
    char*   data = colDataGetData(pInputCol, i);
×
UNCOV
5999
    int32_t code = doModeAdd(pInfo, i, pCtx, data);
×
UNCOV
6000
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
6001
      modeFunctionCleanup(pInfo);
×
6002
      return code;
×
6003
    }
6004
  }
6005

UNCOV
6006
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) {
×
UNCOV
6007
    int32_t code = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, &pInfo->nullTuplePos);
×
UNCOV
6008
    if (code != TSDB_CODE_SUCCESS) {
×
6009
      modeFunctionCleanup(pInfo);
×
6010
      return code;
×
6011
    }
UNCOV
6012
    pInfo->nullTupleSaved = true;
×
6013
  }
6014

UNCOV
6015
  SET_VAL(pResInfo, numOfElems, 1);
×
6016

UNCOV
6017
  return TSDB_CODE_SUCCESS;
×
6018
}
6019

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

6032
  STuplePos resDataPos, resTuplePos;
UNCOV
6033
  int32_t   maxCount = 0;
×
6034

UNCOV
6035
  void* pIter = taosHashIterate(pInfo->pHash, NULL);
×
UNCOV
6036
  while (pIter != NULL) {
×
UNCOV
6037
    SModeItem* pItem = (SModeItem*)pIter;
×
UNCOV
6038
    if (pItem->count >= maxCount) {
×
UNCOV
6039
      maxCount = pItem->count;
×
UNCOV
6040
      resDataPos = pItem->dataPos;
×
UNCOV
6041
      resTuplePos = pItem->tuplePos;
×
6042
    }
6043

UNCOV
6044
    pIter = taosHashIterate(pInfo->pHash, pIter);
×
6045
  }
6046

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

UNCOV
6058
    code = colDataSetVal(pCol, currentRow, pData, false);
×
UNCOV
6059
    if (TSDB_CODE_SUCCESS != code) {
×
6060
      modeFunctionCleanup(pInfo);
×
6061
      return code;
×
6062
    }
UNCOV
6063
    code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow);
×
6064
  } else {
UNCOV
6065
    colDataSetNULL(pCol, currentRow);
×
UNCOV
6066
    code = setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
×
6067
  }
6068

UNCOV
6069
  modeFunctionCleanup(pInfo);
×
6070

UNCOV
6071
  return code;
×
6072
}
6073

UNCOV
6074
bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6075
  pEnv->calcMemSize = sizeof(STwaInfo);
×
UNCOV
6076
  return true;
×
6077
}
6078

UNCOV
6079
int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
6080
  if (pResultInfo->initialized) {
×
6081
    return TSDB_CODE_SUCCESS;
×
6082
  }
UNCOV
6083
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
6084
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6085
  }
6086

UNCOV
6087
  STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6088
  pInfo->numOfElems = 0;
×
UNCOV
6089
  pInfo->p.key = INT64_MIN;
×
UNCOV
6090
  pInfo->win = TSWINDOW_INITIALIZER;
×
UNCOV
6091
  return TSDB_CODE_SUCCESS;
×
6092
}
6093

UNCOV
6094
static double twa_get_area(SPoint1 s, SPoint1 e) {
×
UNCOV
6095
  if (e.key == INT64_MAX || s.key == INT64_MIN) {
×
6096
    return 0;
×
6097
  }
6098

UNCOV
6099
  if ((s.val >= 0 && e.val >= 0) || (s.val <= 0 && e.val <= 0)) {
×
UNCOV
6100
    return (s.val + e.val) * (e.key - s.key) / 2;
×
6101
  }
6102

UNCOV
6103
  double x = (s.key * e.val - e.key * s.val) / (e.val - s.val);
×
UNCOV
6104
  double val = (s.val * (x - s.key) + e.val * (e.key - x)) / 2;
×
UNCOV
6105
  return val;
×
6106
}
6107

UNCOV
6108
int32_t twaFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6109
  int32_t               code = TSDB_CODE_SUCCESS;
×
UNCOV
6110
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6111
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6112

UNCOV
6113
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6114
  STwaInfo*            pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
6115
  SPoint1*             last = &pInfo->p;
×
6116

UNCOV
6117
  if (IS_NULL_TYPE(pInputCol->info.type)) {
×
6118
    pInfo->numOfElems = 0;
×
6119
    goto _twa_over;
×
6120
  }
6121

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

UNCOV
6138
      last->key = row.ts;
×
6139

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

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

UNCOV
6160
      last->key = row.ts;
×
6161

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

UNCOV
6164
      pInfo->win.skey = last->key;
×
UNCOV
6165
      pInfo->numOfElems++;
×
UNCOV
6166
      break;
×
6167
    }
6168
  }
6169

UNCOV
6170
  SPoint1 st = {0};
×
6171

6172
  // calculate the value of
6173
  while (1) {
UNCOV
6174
    code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6175
    if (TSDB_CODE_SUCCESS != code) {
×
6176
      return code;
×
6177
    }
UNCOV
6178
    if (!result) {
×
UNCOV
6179
      break;
×
6180
    }
UNCOV
6181
    if (row.isDataNull) {
×
UNCOV
6182
      continue;
×
6183
    }
UNCOV
6184
    pInfo->numOfElems++;
×
UNCOV
6185
    switch (pInputCol->info.type) {
×
UNCOV
6186
      case TSDB_DATA_TYPE_TINYINT: {
×
UNCOV
6187
        INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData);
×
UNCOV
6188
        break;
×
6189
      }
UNCOV
6190
      case TSDB_DATA_TYPE_SMALLINT: {
×
UNCOV
6191
        INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData);
×
UNCOV
6192
        break;
×
6193
      }
UNCOV
6194
      case TSDB_DATA_TYPE_INT: {
×
UNCOV
6195
        INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData);
×
UNCOV
6196
        break;
×
6197
      }
UNCOV
6198
      case TSDB_DATA_TYPE_BIGINT: {
×
UNCOV
6199
        INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData);
×
UNCOV
6200
        break;
×
6201
      }
UNCOV
6202
      case TSDB_DATA_TYPE_FLOAT: {
×
UNCOV
6203
        INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData);
×
UNCOV
6204
        break;
×
6205
      }
UNCOV
6206
      case TSDB_DATA_TYPE_DOUBLE: {
×
UNCOV
6207
        INIT_INTP_POINT(st, row.ts, *(double*)row.pData);
×
UNCOV
6208
        break;
×
6209
      }
UNCOV
6210
      case TSDB_DATA_TYPE_UTINYINT: {
×
UNCOV
6211
        INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData);
×
UNCOV
6212
        break;
×
6213
      }
UNCOV
6214
      case TSDB_DATA_TYPE_USMALLINT: {
×
UNCOV
6215
        INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData);
×
UNCOV
6216
        break;
×
6217
      }
UNCOV
6218
      case TSDB_DATA_TYPE_UINT: {
×
UNCOV
6219
        INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData);
×
UNCOV
6220
        break;
×
6221
      }
UNCOV
6222
      case TSDB_DATA_TYPE_UBIGINT: {
×
UNCOV
6223
        INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData);
×
UNCOV
6224
        break;
×
6225
      }
6226
      default: {
×
6227
        return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
6228
      }
6229
    }
UNCOV
6230
    if (pInfo->p.key == st.key) {
×
6231
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6232
    }
6233

UNCOV
6234
    pInfo->dOutput += twa_get_area(pInfo->p, st);
×
UNCOV
6235
    pInfo->p = st;
×
6236
  }
6237

6238
  // the last interpolated time window value
UNCOV
6239
  if (pCtx->end.key != INT64_MIN) {
×
UNCOV
6240
    pInfo->dOutput += twa_get_area(pInfo->p, pCtx->end);
×
UNCOV
6241
    pInfo->p = pCtx->end;
×
UNCOV
6242
    pInfo->numOfElems += 1;
×
6243
  }
6244

UNCOV
6245
  pInfo->win.ekey = pInfo->p.key;
×
6246

UNCOV
6247
_twa_over:
×
UNCOV
6248
  SET_VAL(pResInfo, 1, 1);
×
UNCOV
6249
  return TSDB_CODE_SUCCESS;
×
6250
}
6251

6252
/*
6253
 * To copy the input to interResBuf to avoid the input buffer space be over writen
6254
 * by next input data. The TWA function only applies to each table, so no merge procedure
6255
 * is required, we simply copy to the resut ot interResBuffer.
6256
 */
6257
// void twa_function_copy(SQLFunctionCtx *pCtx) {
6258
//   SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx);
6259
//
6260
//   memcpy(GET_ROWCELL_INTERBUF(pResInfo), pCtx->pInput, (size_t)pCtx->inputBytes);
6261
//   pResInfo->hasResult = ((STwaInfo *)pCtx->pInput)->hasResult;
6262
// }
6263

UNCOV
6264
int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6265
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
6266

UNCOV
6267
  STwaInfo* pInfo = (STwaInfo*)GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
6268
  if (pInfo->numOfElems == 0) {
×
UNCOV
6269
    pResInfo->numOfRes = 0;
×
6270
  } else {
UNCOV
6271
    if (pInfo->win.ekey == pInfo->win.skey) {
×
UNCOV
6272
      pInfo->dTwaRes = pInfo->p.val;
×
UNCOV
6273
    } else if (pInfo->win.ekey == INT64_MAX || pInfo->win.skey == INT64_MIN) {  // no data in timewindow
×
6274
      pInfo->dTwaRes = 0;
×
6275
    } else {
UNCOV
6276
      pInfo->dTwaRes = pInfo->dOutput / (pInfo->win.ekey - pInfo->win.skey);
×
6277
    }
6278

UNCOV
6279
    pResInfo->numOfRes = 1;
×
6280
  }
6281

UNCOV
6282
  return functionFinalize(pCtx, pBlock);
×
6283
}
6284

UNCOV
6285
int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
6286
  if (pResultInfo->initialized) {
×
6287
    return TSDB_CODE_SUCCESS;
×
6288
  }
UNCOV
6289
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
6290
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6291
  }
6292

UNCOV
6293
  STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6294
  pInfo->minRows = INT32_MAX;
×
UNCOV
6295
  return TSDB_CODE_SUCCESS;
×
6296
}
6297

UNCOV
6298
int32_t blockDistFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6299
  const int32_t BLOCK_DIST_RESULT_ROWS = 25;
×
6300

UNCOV
6301
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6302
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
6303
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6304
  STableBlockDistInfo*  pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6305

UNCOV
6306
  STableBlockDistInfo p1 = {0};
×
UNCOV
6307
  if (tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
×
6308
    qError("failed to deserialize block dist info");
×
6309
    return TSDB_CODE_FAILED;
×
6310
  }
6311

UNCOV
6312
  pDistInfo->numOfBlocks += p1.numOfBlocks;
×
UNCOV
6313
  pDistInfo->numOfTables += p1.numOfTables;
×
UNCOV
6314
  pDistInfo->numOfInmemRows += p1.numOfInmemRows;
×
UNCOV
6315
  pDistInfo->numOfSttRows += p1.numOfSttRows;
×
UNCOV
6316
  pDistInfo->totalSize += p1.totalSize;
×
UNCOV
6317
  pDistInfo->totalRows += p1.totalRows;
×
UNCOV
6318
  pDistInfo->numOfFiles += p1.numOfFiles;
×
6319

UNCOV
6320
  pDistInfo->defMinRows = p1.defMinRows;
×
UNCOV
6321
  pDistInfo->defMaxRows = p1.defMaxRows;
×
UNCOV
6322
  pDistInfo->rowSize = p1.rowSize;
×
6323

UNCOV
6324
  if (pDistInfo->minRows > p1.minRows) {
×
UNCOV
6325
    pDistInfo->minRows = p1.minRows;
×
6326
  }
UNCOV
6327
  if (pDistInfo->maxRows < p1.maxRows) {
×
UNCOV
6328
    pDistInfo->maxRows = p1.maxRows;
×
6329
  }
UNCOV
6330
  pDistInfo->numOfVgroups += (p1.numOfTables != 0 ? 1 : 0);
×
UNCOV
6331
  for (int32_t i = 0; i < tListLen(pDistInfo->blockRowsHisto); ++i) {
×
UNCOV
6332
    pDistInfo->blockRowsHisto[i] += p1.blockRowsHisto[i];
×
6333
  }
6334

UNCOV
6335
  pResInfo->numOfRes = BLOCK_DIST_RESULT_ROWS;  // default output rows
×
UNCOV
6336
  return TSDB_CODE_SUCCESS;
×
6337
}
6338

UNCOV
6339
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo) {
×
UNCOV
6340
  SEncoder encoder = {0};
×
UNCOV
6341
  int32_t  code = 0;
×
6342
  int32_t  lino;
6343
  int32_t  tlen;
UNCOV
6344
  tEncoderInit(&encoder, buf, bufLen);
×
6345

UNCOV
6346
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
×
UNCOV
6347
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->rowSize));
×
6348

UNCOV
6349
  TAOS_CHECK_EXIT(tEncodeU16(&encoder, pInfo->numOfFiles));
×
UNCOV
6350
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfBlocks));
×
UNCOV
6351
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfTables));
×
6352

UNCOV
6353
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalSize));
×
UNCOV
6354
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->totalRows));
×
UNCOV
6355
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->maxRows));
×
UNCOV
6356
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->minRows));
×
UNCOV
6357
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMaxRows));
×
UNCOV
6358
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->defMinRows));
×
UNCOV
6359
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfInmemRows));
×
UNCOV
6360
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfSttRows));
×
UNCOV
6361
  TAOS_CHECK_EXIT(tEncodeU32(&encoder, pInfo->numOfVgroups));
×
6362

UNCOV
6363
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
×
UNCOV
6364
    TAOS_CHECK_EXIT(tEncodeI32(&encoder, pInfo->blockRowsHisto[i]));
×
6365
  }
6366

UNCOV
6367
  tEndEncode(&encoder);
×
6368

UNCOV
6369
_exit:
×
UNCOV
6370
  if (code) {
×
6371
    tlen = code;
×
6372
  } else {
UNCOV
6373
    tlen = encoder.pos;
×
6374
  }
UNCOV
6375
  tEncoderClear(&encoder);
×
UNCOV
6376
  return tlen;
×
6377
}
6378

UNCOV
6379
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo) {
×
UNCOV
6380
  SDecoder decoder = {0};
×
UNCOV
6381
  int32_t  code = 0;
×
6382
  int32_t  lino;
UNCOV
6383
  tDecoderInit(&decoder, buf, bufLen);
×
6384

UNCOV
6385
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
×
UNCOV
6386
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->rowSize));
×
6387

UNCOV
6388
  TAOS_CHECK_EXIT(tDecodeU16(&decoder, &pInfo->numOfFiles));
×
UNCOV
6389
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfBlocks));
×
UNCOV
6390
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfTables));
×
6391

UNCOV
6392
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalSize));
×
UNCOV
6393
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->totalRows));
×
UNCOV
6394
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->maxRows));
×
UNCOV
6395
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->minRows));
×
UNCOV
6396
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMaxRows));
×
UNCOV
6397
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->defMinRows));
×
UNCOV
6398
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfInmemRows));
×
UNCOV
6399
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfSttRows));
×
UNCOV
6400
  TAOS_CHECK_EXIT(tDecodeU32(&decoder, &pInfo->numOfVgroups));
×
6401

UNCOV
6402
  for (int32_t i = 0; i < tListLen(pInfo->blockRowsHisto); ++i) {
×
UNCOV
6403
    TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pInfo->blockRowsHisto[i]));
×
6404
  }
6405

UNCOV
6406
_exit:
×
UNCOV
6407
  tDecoderClear(&decoder);
×
UNCOV
6408
  return code;
×
6409
}
6410

UNCOV
6411
int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6412
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6413
  STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo);
×
6414

UNCOV
6415
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
×
UNCOV
6416
  if (NULL == pColInfo) {
×
6417
    return TSDB_CODE_OUT_OF_RANGE;
×
6418
  }
6419

UNCOV
6420
  if (pData->totalRows == 0) {
×
UNCOV
6421
    pData->minRows = 0;
×
6422
  }
6423

UNCOV
6424
  int32_t row = 0;
×
UNCOV
6425
  char    st[256] = {0};
×
UNCOV
6426
  double  averageSize = 0;
×
UNCOV
6427
  if (pData->numOfBlocks != 0) {
×
UNCOV
6428
    averageSize = ((double)pData->totalSize) / pData->numOfBlocks;
×
6429
  }
UNCOV
6430
  uint64_t totalRawSize = pData->totalRows * pData->rowSize;
×
UNCOV
6431
  double   compRatio = 0;
×
UNCOV
6432
  if (totalRawSize != 0) {
×
UNCOV
6433
    compRatio = pData->totalSize * 100 / (double)totalRawSize;
×
6434
  }
6435

UNCOV
6436
  int32_t len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
6437
                          "Total_Blocks=[%d] Total_Size=[%.2f KiB] Average_size=[%.2f KiB] Compression_Ratio=[%.2f %c]",
UNCOV
6438
                          pData->numOfBlocks, pData->totalSize / 1024.0, averageSize / 1024.0, compRatio, '%');
×
6439

UNCOV
6440
  varDataSetLen(st, len);
×
UNCOV
6441
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6442
  if (TSDB_CODE_SUCCESS != code) {
×
6443
    return code;
×
6444
  }
6445

UNCOV
6446
  int64_t avgRows = 0;
×
UNCOV
6447
  if (pData->numOfBlocks > 0) {
×
UNCOV
6448
    avgRows = pData->totalRows / pData->numOfBlocks;
×
6449
  }
6450

UNCOV
6451
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
6452
                  "Block_Rows=[%" PRId64 "] MinRows=[%d] MaxRows=[%d] AvgRows=[%" PRId64 "]", pData->totalRows,
6453
                  pData->minRows, pData->maxRows, avgRows);
UNCOV
6454
  varDataSetLen(st, len);
×
UNCOV
6455
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6456
  if (TSDB_CODE_SUCCESS != code) {
×
6457
    return code;
×
6458
  }
6459

UNCOV
6460
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Inmem_Rows=[%u] Stt_Rows=[%u] ",
×
6461
                  pData->numOfInmemRows, pData->numOfSttRows);
UNCOV
6462
  varDataSetLen(st, len);
×
UNCOV
6463
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6464
  if (TSDB_CODE_SUCCESS != code) {
×
6465
    return code;
×
6466
  }
6467

UNCOV
6468
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
UNCOV
6469
                  "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables, pData->numOfFiles,
×
6470
                  pData->numOfVgroups);
6471

UNCOV
6472
  varDataSetLen(st, len);
×
UNCOV
6473
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6474
  if (TSDB_CODE_SUCCESS != code) {
×
6475
    return code;
×
6476
  }
6477

UNCOV
6478
  len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE,
×
6479
                  "--------------------------------------------------------------------------------");
UNCOV
6480
  varDataSetLen(st, len);
×
UNCOV
6481
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6482
  if (TSDB_CODE_SUCCESS != code) {
×
6483
    return code;
×
6484
  }
6485

UNCOV
6486
  int32_t maxVal = 0;
×
UNCOV
6487
  int32_t minVal = INT32_MAX;
×
UNCOV
6488
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
×
UNCOV
6489
    if (maxVal < pData->blockRowsHisto[i]) {
×
UNCOV
6490
      maxVal = pData->blockRowsHisto[i];
×
6491
    }
6492

UNCOV
6493
    if (minVal > pData->blockRowsHisto[i]) {
×
UNCOV
6494
      minVal = pData->blockRowsHisto[i];
×
6495
    }
6496
  }
6497

6498
  // maximum number of step is 80
UNCOV
6499
  double factor = pData->numOfBlocks / 80.0;
×
6500

UNCOV
6501
  int32_t numOfBuckets = sizeof(pData->blockRowsHisto) / sizeof(pData->blockRowsHisto[0]);
×
UNCOV
6502
  int32_t bucketRange = ceil(((double)(pData->defMaxRows - pData->defMinRows)) / numOfBuckets);
×
6503

UNCOV
6504
  for (int32_t i = 0; i < tListLen(pData->blockRowsHisto); ++i) {
×
UNCOV
6505
    len =
×
UNCOV
6506
        tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "%04d |", pData->defMinRows + bucketRange * (i + 1));
×
6507

UNCOV
6508
    int32_t num = 0;
×
UNCOV
6509
    if (pData->blockRowsHisto[i] > 0) {
×
UNCOV
6510
      num = (pData->blockRowsHisto[i]) / factor;
×
6511
    }
6512

UNCOV
6513
    for (int32_t j = 0; j < num; ++j) {
×
UNCOV
6514
      int32_t x = tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "%c", '|');
×
UNCOV
6515
      len += x;
×
6516
    }
6517

UNCOV
6518
    if (pData->blockRowsHisto[i] > 0) {
×
UNCOV
6519
      double v = pData->blockRowsHisto[i] * 100.0 / pData->numOfBlocks;
×
UNCOV
6520
      len += tsnprintf(varDataVal(st) + len, sizeof(st) - VARSTR_HEADER_SIZE - len, "  %d (%.2f%c)",
×
6521
                       pData->blockRowsHisto[i], v, '%');
6522
    }
6523

UNCOV
6524
    varDataSetLen(st, len);
×
UNCOV
6525
    code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6526
    if (TSDB_CODE_SUCCESS != code) {
×
6527
      return code;
×
6528
    }
6529
  }
6530

UNCOV
6531
  return TSDB_CODE_SUCCESS;
×
6532
}
UNCOV
6533
int32_t blockDBUsageSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) {
×
UNCOV
6534
  if (pResultInfo->initialized) {
×
6535
    return TSDB_CODE_SUCCESS;
×
6536
  }
UNCOV
6537
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) {
×
6538
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6539
  }
6540

UNCOV
6541
  SDBBlockUsageInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
6542
  return TSDB_CODE_SUCCESS;
×
6543
}
UNCOV
6544
int32_t blockDBUsageFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6545
  const int32_t BLOCK_DISK_USAGE_RESULT_ROWS = 2;
×
6546

UNCOV
6547
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6548
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
UNCOV
6549
  SResultRowEntryInfo*  pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6550
  SDBBlockUsageInfo*    pDistInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6551

UNCOV
6552
  SDBBlockUsageInfo p1 = {0};
×
UNCOV
6553
  if (tDeserializeBlockDbUsage(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) {
×
6554
    qError("failed to deserialize block dist info");
×
6555
    return TSDB_CODE_FAILED;
×
6556
  }
6557

UNCOV
6558
  pDistInfo->dataInDiskSize += p1.dataInDiskSize;
×
UNCOV
6559
  pDistInfo->walInDiskSize += p1.walInDiskSize;
×
UNCOV
6560
  pDistInfo->rawDataSize += p1.rawDataSize;
×
UNCOV
6561
  pResInfo->numOfRes = BLOCK_DISK_USAGE_RESULT_ROWS;  // default output rows
×
UNCOV
6562
  return TSDB_CODE_SUCCESS;
×
6563
}
6564

UNCOV
6565
int32_t tSerializeBlockDbUsage(void* buf, int32_t bufLen, const SDBBlockUsageInfo* pInfo) {
×
UNCOV
6566
  SEncoder encoder = {0};
×
UNCOV
6567
  int32_t  code = 0;
×
6568
  int32_t  lino;
6569
  int32_t  tlen;
UNCOV
6570
  tEncoderInit(&encoder, buf, bufLen);
×
6571

UNCOV
6572
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
×
6573

UNCOV
6574
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->dataInDiskSize));
×
UNCOV
6575
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->walInDiskSize));
×
UNCOV
6576
  TAOS_CHECK_EXIT(tEncodeU64(&encoder, pInfo->rawDataSize));
×
6577

UNCOV
6578
  tEndEncode(&encoder);
×
6579

UNCOV
6580
_exit:
×
UNCOV
6581
  if (code) {
×
6582
    tlen = code;
×
6583
  } else {
UNCOV
6584
    tlen = encoder.pos;
×
6585
  }
UNCOV
6586
  tEncoderClear(&encoder);
×
UNCOV
6587
  return tlen;
×
6588
}
UNCOV
6589
int32_t tDeserializeBlockDbUsage(void* buf, int32_t bufLen, SDBBlockUsageInfo* pInfo) {
×
UNCOV
6590
  SDecoder decoder = {0};
×
UNCOV
6591
  int32_t  code = 0;
×
6592
  int32_t  lino;
UNCOV
6593
  tDecoderInit(&decoder, buf, bufLen);
×
6594

UNCOV
6595
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
×
UNCOV
6596
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->dataInDiskSize));
×
UNCOV
6597
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->walInDiskSize));
×
UNCOV
6598
  TAOS_CHECK_EXIT(tDecodeU64(&decoder, &pInfo->rawDataSize));
×
6599

UNCOV
6600
_exit:
×
UNCOV
6601
  tDecoderClear(&decoder);
×
UNCOV
6602
  return code;
×
6603
}
UNCOV
6604
int32_t blockDBUsageFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
6605
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6606
  SDBBlockUsageInfo*   pData = GET_ROWCELL_INTERBUF(pResInfo);
×
6607

UNCOV
6608
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
×
UNCOV
6609
  if (NULL == pColInfo) {
×
6610
    return TSDB_CODE_OUT_OF_RANGE;
×
6611
  }
UNCOV
6612
  int32_t len = 0;
×
UNCOV
6613
  int32_t row = 0;
×
UNCOV
6614
  char    st[256] = {0};
×
6615

UNCOV
6616
  uint64_t totalDiskSize = pData->dataInDiskSize;
×
UNCOV
6617
  uint64_t rawDataSize = pData->rawDataSize;
×
UNCOV
6618
  double   compressRadio = 0;
×
UNCOV
6619
  if (rawDataSize != 0) {
×
UNCOV
6620
    compressRadio = totalDiskSize * 100 / (double)rawDataSize;
×
UNCOV
6621
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_radio=[%.2f]", compressRadio);
×
6622
  } else {
6623
    len = tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Compress_radio=[NULL]");
×
6624
  }
6625

UNCOV
6626
  varDataSetLen(st, len);
×
UNCOV
6627
  int32_t code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6628
  if (TSDB_CODE_SUCCESS != code) {
×
6629
    return code;
×
6630
  }
6631

UNCOV
6632
  len =
×
UNCOV
6633
      tsnprintf(varDataVal(st), sizeof(st) - VARSTR_HEADER_SIZE, "Disk_occupied=[%" PRId64 "k]", pData->dataInDiskSize);
×
UNCOV
6634
  varDataSetLen(st, len);
×
UNCOV
6635
  code = colDataSetVal(pColInfo, row++, st, false);
×
UNCOV
6636
  if (TSDB_CODE_SUCCESS != code) {
×
6637
    return code;
×
6638
  }
UNCOV
6639
  return code;
×
6640
}
6641

UNCOV
6642
bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6643
  pEnv->calcMemSize = sizeof(SDerivInfo);
×
UNCOV
6644
  return true;
×
6645
}
6646

UNCOV
6647
int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
6648
  if (pResInfo->initialized) {
×
UNCOV
6649
    return TSDB_CODE_SUCCESS;
×
6650
  }
UNCOV
6651
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
6652
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6653
  }
6654

UNCOV
6655
  SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6656

UNCOV
6657
  pDerivInfo->ignoreNegative = pCtx->param[2].param.i;
×
UNCOV
6658
  pDerivInfo->prevTs = -1;
×
UNCOV
6659
  pDerivInfo->tsWindow = pCtx->param[1].param.i;
×
UNCOV
6660
  pDerivInfo->valueSet = false;
×
UNCOV
6661
  return TSDB_CODE_SUCCESS;
×
6662
}
6663

UNCOV
6664
int32_t derivativeFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6665
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6666
  SDerivInfo*          pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6667

UNCOV
6668
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6669
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6670

UNCOV
6671
  int32_t          numOfElems = 0;
×
UNCOV
6672
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
UNCOV
6673
  SColumnInfoData* pTsOutput = pCtx->pTsOutput;
×
UNCOV
6674
  int32_t          code = TSDB_CODE_SUCCESS;
×
6675

UNCOV
6676
  funcInputUpdate(pCtx);
×
6677

UNCOV
6678
  double v = 0;
×
UNCOV
6679
  if (pCtx->order == TSDB_ORDER_ASC) {
×
UNCOV
6680
    SFuncInputRow row = {0};
×
UNCOV
6681
    bool          result = false;
×
UNCOV
6682
    while (1) {
×
UNCOV
6683
      code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6684
      if (TSDB_CODE_SUCCESS != code) {
×
6685
        return code;
×
6686
      }
UNCOV
6687
      if (!result) {
×
UNCOV
6688
        break;
×
6689
      }
UNCOV
6690
      if (row.isDataNull) {
×
UNCOV
6691
        continue;
×
6692
      }
6693

UNCOV
6694
      char* d = row.pData;
×
UNCOV
6695
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
×
6696

UNCOV
6697
      int32_t pos = pCtx->offset + numOfElems;
×
UNCOV
6698
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
×
UNCOV
6699
        pDerivInfo->valueSet = true;
×
6700
      } else {
UNCOV
6701
        if (row.ts == pDerivInfo->prevTs) {
×
6702
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6703
        }
UNCOV
6704
        double r = ((v - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (row.ts - pDerivInfo->prevTs);
×
UNCOV
6705
        if (pDerivInfo->ignoreNegative && r < 0) {
×
6706
        } else {
UNCOV
6707
          if (isinf(r) || isnan(r)) {
×
6708
            colDataSetNULL(pOutput, pos);
×
6709
          } else {
UNCOV
6710
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
×
UNCOV
6711
            if (code != TSDB_CODE_SUCCESS) {
×
6712
              return code;
×
6713
            }
6714
          }
6715

UNCOV
6716
          if (pTsOutput != NULL) {
×
6717
            colDataSetInt64(pTsOutput, pos, &row.ts);
×
6718
          }
6719

6720
          // handle selectivity
UNCOV
6721
          if (pCtx->subsidiaries.num > 0) {
×
UNCOV
6722
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
×
UNCOV
6723
            if (code != TSDB_CODE_SUCCESS) {
×
6724
              return code;
×
6725
            }
6726
          }
6727

UNCOV
6728
          numOfElems++;
×
6729
        }
6730
      }
6731

UNCOV
6732
      pDerivInfo->prevValue = v;
×
UNCOV
6733
      pDerivInfo->prevTs = row.ts;
×
6734
    }
6735
  } else {
UNCOV
6736
    SFuncInputRow row = {0};
×
UNCOV
6737
    bool          result = false;
×
UNCOV
6738
    while (1) {
×
UNCOV
6739
      code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6740
      if (TSDB_CODE_SUCCESS != code) {
×
6741
        return code;
×
6742
      }
UNCOV
6743
      if (!result) {
×
UNCOV
6744
        break;
×
6745
      }
UNCOV
6746
      if (row.isDataNull) {
×
UNCOV
6747
        continue;
×
6748
      }
6749

UNCOV
6750
      char* d = row.pData;
×
UNCOV
6751
      GET_TYPED_DATA(v, double, pInputCol->info.type, d);
×
6752

UNCOV
6753
      int32_t pos = pCtx->offset + numOfElems;
×
UNCOV
6754
      if (!pDerivInfo->valueSet) {  // initial value is not set yet
×
UNCOV
6755
        pDerivInfo->valueSet = true;
×
6756
      } else {
UNCOV
6757
        if (row.ts == pDerivInfo->prevTs) {
×
6758
          return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6759
        }
UNCOV
6760
        double r = ((pDerivInfo->prevValue - v) * pDerivInfo->tsWindow) / (pDerivInfo->prevTs - row.ts);
×
UNCOV
6761
        if (pDerivInfo->ignoreNegative && r < 0) {
×
6762
        } else {
UNCOV
6763
          if (isinf(r) || isnan(r)) {
×
6764
            colDataSetNULL(pOutput, pos);
×
6765
          } else {
UNCOV
6766
            code = colDataSetVal(pOutput, pos, (const char*)&r, false);
×
UNCOV
6767
            if (code != TSDB_CODE_SUCCESS) {
×
6768
              return code;
×
6769
            }
6770
          }
6771

UNCOV
6772
          if (pTsOutput != NULL) {
×
6773
            colDataSetInt64(pTsOutput, pos, &pDerivInfo->prevTs);
×
6774
          }
6775

6776
          // handle selectivity
UNCOV
6777
          if (pCtx->subsidiaries.num > 0) {
×
UNCOV
6778
            code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos);
×
UNCOV
6779
            if (code != TSDB_CODE_SUCCESS) {
×
6780
              return code;
×
6781
            }
6782
          }
UNCOV
6783
          numOfElems++;
×
6784
        }
6785
      }
6786

UNCOV
6787
      pDerivInfo->prevValue = v;
×
UNCOV
6788
      pDerivInfo->prevTs = row.ts;
×
6789
    }
6790
  }
6791

UNCOV
6792
  pResInfo->numOfRes = numOfElems;
×
6793

UNCOV
6794
  return TSDB_CODE_SUCCESS;
×
6795
}
6796

UNCOV
6797
int32_t getIrateInfoSize(int32_t pkBytes) { return (int32_t)sizeof(SRateInfo) + 2 * pkBytes; }
×
6798

UNCOV
6799
bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
×
UNCOV
6800
  int32_t pkBytes = (pFunc->hasPk) ? pFunc->pkBytes : 0;
×
UNCOV
6801
  pEnv->calcMemSize = getIrateInfoSize(pkBytes);
×
UNCOV
6802
  return true;
×
6803
}
6804

UNCOV
6805
int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
×
UNCOV
6806
  if (pResInfo->initialized) {
×
6807
    return TSDB_CODE_SUCCESS;
×
6808
  }
UNCOV
6809
  if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) {
×
6810
    return TSDB_CODE_FUNC_SETUP_ERROR;
×
6811
  }
6812

UNCOV
6813
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6814

UNCOV
6815
  pInfo->firstKey = INT64_MIN;
×
UNCOV
6816
  pInfo->lastKey = INT64_MIN;
×
UNCOV
6817
  pInfo->firstValue = (double)INT64_MIN;
×
UNCOV
6818
  pInfo->lastValue = (double)INT64_MIN;
×
6819

UNCOV
6820
  pInfo->hasResult = 0;
×
UNCOV
6821
  return TSDB_CODE_SUCCESS;
×
6822
}
6823

UNCOV
6824
static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) {
×
UNCOV
6825
  if (isFirst) {
×
UNCOV
6826
    pRateInfo->firstValue = v;
×
UNCOV
6827
    pRateInfo->firstKey = ts;
×
UNCOV
6828
    if (pRateInfo->firstPk) {
×
6829
      int32_t pkBytes;
UNCOV
6830
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
×
UNCOV
6831
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
6832
          pkBytes = getJsonValueLen(pk);
×
6833
        } else {
UNCOV
6834
          pkBytes = varDataTLen(pk);
×
6835
        }
6836
      } else {
UNCOV
6837
        pkBytes = pRateInfo->pkBytes;
×
6838
      }
UNCOV
6839
      (void)memcpy(pRateInfo->firstPk, pk, pkBytes);
×
6840
    }
6841
  } else {
UNCOV
6842
    pRateInfo->lastValue = v;
×
UNCOV
6843
    pRateInfo->lastKey = ts;
×
UNCOV
6844
    if (pRateInfo->lastPk) {
×
6845
      int32_t pkBytes;
UNCOV
6846
      if (IS_VAR_DATA_TYPE(pRateInfo->pkType)) {
×
UNCOV
6847
        if (pRateInfo->pkType == TSDB_DATA_TYPE_JSON) {
×
6848
          pkBytes = getJsonValueLen(pk);
×
6849
        } else {
UNCOV
6850
          pkBytes = varDataTLen(pk);
×
6851
        }
6852
      } else {
UNCOV
6853
        pkBytes = pRateInfo->pkBytes;
×
6854
      }
UNCOV
6855
      (void)memcpy(pRateInfo->lastPk, pk, pkBytes);
×
6856
    }
6857
  }
UNCOV
6858
}
×
6859

UNCOV
6860
static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool isMerge) {
×
UNCOV
6861
  if (pCtx->hasPrimaryKey) {
×
UNCOV
6862
    if (!isMerge) {
×
UNCOV
6863
      pRateInfo->pkType = pCtx->input.pPrimaryKey->info.type;
×
UNCOV
6864
      pRateInfo->pkBytes = pCtx->input.pPrimaryKey->info.bytes;
×
UNCOV
6865
      pRateInfo->firstPk = pRateInfo->pkData;
×
UNCOV
6866
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
×
6867
    } else {
UNCOV
6868
      pRateInfo->firstPk = pRateInfo->pkData;
×
UNCOV
6869
      pRateInfo->lastPk = pRateInfo->pkData + pRateInfo->pkBytes;
×
6870
    }
6871
  } else {
UNCOV
6872
    pRateInfo->firstPk = NULL;
×
UNCOV
6873
    pRateInfo->lastPk = NULL;
×
6874
  }
UNCOV
6875
}
×
6876

UNCOV
6877
int32_t irateFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
6878
  int32_t              code = TSDB_CODE_SUCCESS;
×
UNCOV
6879
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
6880
  SRateInfo*           pRateInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
6881

UNCOV
6882
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
6883
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
6884

UNCOV
6885
  SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
×
6886

UNCOV
6887
  funcInputUpdate(pCtx);
×
6888

UNCOV
6889
  initializeRateInfo(pCtx, pRateInfo, false);
×
6890

UNCOV
6891
  int32_t       numOfElems = 0;
×
UNCOV
6892
  int32_t       type = pInputCol->info.type;
×
UNCOV
6893
  SFuncInputRow row = {0};
×
UNCOV
6894
  bool          result = false;
×
UNCOV
6895
  while (1) {
×
UNCOV
6896
    code = funcInputGetNextRow(pCtx, &row, &result);
×
UNCOV
6897
    if (TSDB_CODE_SUCCESS != code) {
×
6898
      return code;
×
6899
    }
UNCOV
6900
    if (!result) {
×
UNCOV
6901
      break;
×
6902
    }
UNCOV
6903
    if (row.isDataNull) {
×
UNCOV
6904
      continue;
×
6905
    }
6906

UNCOV
6907
    char*  data = row.pData;
×
UNCOV
6908
    double v = 0;
×
UNCOV
6909
    GET_TYPED_DATA(v, double, type, data);
×
6910

UNCOV
6911
    if (INT64_MIN == pRateInfo->lastKey) {
×
UNCOV
6912
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
×
UNCOV
6913
      pRateInfo->hasResult = 1;
×
UNCOV
6914
      continue;
×
6915
    }
6916

UNCOV
6917
    if (row.ts > pRateInfo->lastKey) {
×
UNCOV
6918
      if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
×
UNCOV
6919
        doSaveRateInfo(pRateInfo, true, pRateInfo->lastKey, pRateInfo->lastPk, pRateInfo->lastValue);
×
6920
      }
UNCOV
6921
      doSaveRateInfo(pRateInfo, false, row.ts, row.pPk, v);
×
UNCOV
6922
      continue;
×
UNCOV
6923
    } else if (row.ts == pRateInfo->lastKey) {
×
6924
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6925
    }
6926

UNCOV
6927
    if ((INT64_MIN == pRateInfo->firstKey) || row.ts > pRateInfo->firstKey) {
×
6928
      doSaveRateInfo(pRateInfo, true, row.ts, row.pPk, v);
×
UNCOV
6929
    } else if (row.ts == pRateInfo->firstKey) {
×
6930
      return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6931
    }
6932
  }
6933

UNCOV
6934
  numOfElems++;
×
6935

UNCOV
6936
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
6937
  return TSDB_CODE_SUCCESS;
×
6938
}
6939

UNCOV
6940
static double doCalcRate(const SRateInfo* pRateInfo, double tickPerSec) {
×
UNCOV
6941
  if ((INT64_MIN == pRateInfo->lastKey) || (INT64_MIN == pRateInfo->firstKey) ||
×
UNCOV
6942
      (pRateInfo->firstKey >= pRateInfo->lastKey)) {
×
UNCOV
6943
    return 0.0;
×
6944
  }
6945

UNCOV
6946
  double diff = 0;
×
6947
  // If the previous value of the last is greater than the last value, only keep the last point instead of the delta
6948
  // value between two values.
UNCOV
6949
  diff = pRateInfo->lastValue;
×
UNCOV
6950
  if (diff >= pRateInfo->firstValue) {
×
UNCOV
6951
    diff -= pRateInfo->firstValue;
×
6952
  }
6953

UNCOV
6954
  int64_t duration = pRateInfo->lastKey - pRateInfo->firstKey;
×
UNCOV
6955
  if (duration == 0) {
×
6956
    return 0;
×
6957
  }
6958

UNCOV
6959
  return (duration > 0) ? ((double)diff) / (duration / tickPerSec) : 0.0;
×
6960
}
6961

UNCOV
6962
static void irateTransferInfoImpl(TSKEY inputKey, SRateInfo* pInput, SRateInfo* pOutput, bool isFirstKey) {
×
UNCOV
6963
  if (inputKey > pOutput->lastKey) {
×
UNCOV
6964
    doSaveRateInfo(pOutput, true, pOutput->lastKey, pOutput->lastPk, pOutput->lastValue);
×
UNCOV
6965
    if (isFirstKey) {
×
UNCOV
6966
      doSaveRateInfo(pOutput, false, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
6967
    } else {
UNCOV
6968
      doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
6969
    }
UNCOV
6970
  } else if ((inputKey < pOutput->lastKey) && (inputKey > pOutput->firstKey)) {
×
UNCOV
6971
    if (isFirstKey) {
×
UNCOV
6972
      doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
6973
    } else {
UNCOV
6974
      doSaveRateInfo(pOutput, true, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
6975
    }
6976
  } else {
6977
    // inputKey < pOutput->firstKey
6978
  }
UNCOV
6979
}
×
6980

UNCOV
6981
static void irateCopyInfo(SRateInfo* pInput, SRateInfo* pOutput) {
×
UNCOV
6982
  doSaveRateInfo(pOutput, true, pInput->firstKey, pInput->firstPk, pInput->firstValue);
×
UNCOV
6983
  doSaveRateInfo(pOutput, false, pInput->lastKey, pInput->lastPk, pInput->lastValue);
×
UNCOV
6984
}
×
6985

UNCOV
6986
static int32_t irateTransferInfo(SRateInfo* pInput, SRateInfo* pOutput) {
×
UNCOV
6987
  if ((pInput->firstKey != INT64_MIN &&
×
UNCOV
6988
       (pInput->firstKey == pOutput->firstKey || pInput->firstKey == pOutput->lastKey)) ||
×
UNCOV
6989
      (pInput->lastKey != INT64_MIN && (pInput->lastKey == pOutput->firstKey || pInput->lastKey == pOutput->lastKey))) {
×
6990
    return TSDB_CODE_FUNC_DUP_TIMESTAMP;
×
6991
  }
6992

UNCOV
6993
  if (pOutput->hasResult == 0) {
×
UNCOV
6994
    irateCopyInfo(pInput, pOutput);
×
UNCOV
6995
    pOutput->hasResult = pInput->hasResult;
×
UNCOV
6996
    return TSDB_CODE_SUCCESS;
×
6997
  }
6998

UNCOV
6999
  if (pInput->firstKey != INT64_MIN) {
×
UNCOV
7000
    irateTransferInfoImpl(pInput->firstKey, pInput, pOutput, true);
×
7001
  }
7002

UNCOV
7003
  if (pInput->lastKey != INT64_MIN) {
×
UNCOV
7004
    irateTransferInfoImpl(pInput->lastKey, pInput, pOutput, false);
×
7005
  }
7006

UNCOV
7007
  pOutput->hasResult = pInput->hasResult;
×
UNCOV
7008
  return TSDB_CODE_SUCCESS;
×
7009
}
7010

UNCOV
7011
int32_t irateFunctionMerge(SqlFunctionCtx* pCtx) {
×
UNCOV
7012
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
7013
  SColumnInfoData*      pCol = pInput->pData[0];
×
UNCOV
7014
  if (pCol->info.type != TSDB_DATA_TYPE_BINARY) {
×
7015
    return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
7016
  }
7017

UNCOV
7018
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
7019
  initializeRateInfo(pCtx, pInfo, true);
×
7020

UNCOV
7021
  int32_t start = pInput->startRowIndex;
×
UNCOV
7022
  for (int32_t i = start; i < start + pInput->numOfRows; ++i) {
×
UNCOV
7023
    char*      data = colDataGetData(pCol, i);
×
UNCOV
7024
    SRateInfo* pInputInfo = (SRateInfo*)varDataVal(data);
×
UNCOV
7025
    initializeRateInfo(pCtx, pInfo, true);
×
UNCOV
7026
    if (pInputInfo->hasResult) {
×
UNCOV
7027
      int32_t code = irateTransferInfo(pInputInfo, pInfo);
×
UNCOV
7028
      if (code != TSDB_CODE_SUCCESS) {
×
7029
        return code;
×
7030
      }
7031
    }
7032
  }
7033

UNCOV
7034
  if (pInfo->hasResult) {
×
UNCOV
7035
    GET_RES_INFO(pCtx)->numOfRes = 1;
×
7036
  }
7037

UNCOV
7038
  return TSDB_CODE_SUCCESS;
×
7039
}
7040

UNCOV
7041
int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7042
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7043
  SRateInfo*           pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
×
UNCOV
7044
  int32_t              resultBytes = getIrateInfoSize(pInfo->pkBytes);
×
UNCOV
7045
  char*                res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char));
×
7046

UNCOV
7047
  if (NULL == res) {
×
7048
    return terrno;
×
7049
  }
UNCOV
7050
  (void)memcpy(varDataVal(res), pInfo, resultBytes);
×
UNCOV
7051
  varDataSetLen(res, resultBytes);
×
7052

UNCOV
7053
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
7054
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
7055
  if (NULL == pCol) {
×
7056
    taosMemoryFree(res);
×
7057
    return TSDB_CODE_OUT_OF_RANGE;
×
7058
  }
7059

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

UNCOV
7062
  taosMemoryFree(res);
×
UNCOV
7063
  return code;
×
7064
}
7065

UNCOV
7066
int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7067
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
7068
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
7069
  if (NULL == pCol) {
×
7070
    return TSDB_CODE_OUT_OF_RANGE;
×
7071
  }
7072

UNCOV
7073
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7074
  pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0;
×
7075

UNCOV
7076
  SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
UNCOV
7077
  double     result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i));
×
UNCOV
7078
  int32_t    code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes);
×
7079

UNCOV
7080
  return code;
×
7081
}
7082

UNCOV
7083
int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
7084
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7085
  SGroupKeyInfo*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7086

UNCOV
7087
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
7088
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
7089

UNCOV
7090
  int32_t startIndex = pInput->startRowIndex;
×
7091

7092
  // escape rest of data blocks to avoid first entry to be overwritten.
UNCOV
7093
  if (pInfo->hasResult) {
×
UNCOV
7094
    goto _group_value_over;
×
7095
  }
7096

UNCOV
7097
  if (pInputCol->pData == NULL || colDataIsNull_s(pInputCol, startIndex)) {
×
UNCOV
7098
    pInfo->isNull = true;
×
UNCOV
7099
    pInfo->hasResult = true;
×
UNCOV
7100
    goto _group_value_over;
×
7101
  }
7102

UNCOV
7103
  char* data = colDataGetData(pInputCol, startIndex);
×
UNCOV
7104
  if (IS_VAR_DATA_TYPE(pInputCol->info.type)) {
×
UNCOV
7105
    (void)memcpy(pInfo->data, data,
×
UNCOV
7106
                 (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data));
×
7107
  } else {
UNCOV
7108
    (void)memcpy(pInfo->data, data, pInputCol->info.bytes);
×
7109
  }
UNCOV
7110
  pInfo->hasResult = true;
×
7111

UNCOV
7112
_group_value_over:
×
7113

UNCOV
7114
  SET_VAL(pResInfo, 1, 1);
×
UNCOV
7115
  return TSDB_CODE_SUCCESS;
×
7116
}
7117

UNCOV
7118
int32_t groupKeyFunction(SqlFunctionCtx* pCtx) { return groupConstValueFunction(pCtx); }
×
7119

UNCOV
7120
int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
×
UNCOV
7121
  int32_t          slotId = pCtx->pExpr->base.resSchema.slotId;
×
UNCOV
7122
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
7123
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
×
UNCOV
7124
  if (NULL == pCol) {
×
7125
    return TSDB_CODE_OUT_OF_RANGE;
×
7126
  }
7127

UNCOV
7128
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
7129

UNCOV
7130
  SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7131

UNCOV
7132
  if (pInfo->hasResult) {
×
UNCOV
7133
    int32_t currentRow = pBlock->info.rows;
×
UNCOV
7134
    for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) {
×
UNCOV
7135
      code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false);
×
UNCOV
7136
      if (TSDB_CODE_SUCCESS != code) {
×
7137
        return code;
×
7138
      }
7139
    }
7140
  } else {
7141
    pResInfo->numOfRes = 0;
×
7142
  }
7143

UNCOV
7144
  return code;
×
7145
}
7146

UNCOV
7147
int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return groupConstValueFinalize(pCtx, pBlock); }
×
7148

7149
int32_t groupKeyCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
×
7150
  SResultRowEntryInfo* pDResInfo = GET_RES_INFO(pDestCtx);
×
7151
  SGroupKeyInfo*       pDBuf = GET_ROWCELL_INTERBUF(pDResInfo);
×
7152

7153
  SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx);
×
7154
  SGroupKeyInfo*       pSBuf = GET_ROWCELL_INTERBUF(pSResInfo);
×
7155

7156
  // escape rest of data blocks to avoid first entry to be overwritten.
7157
  if (pDBuf->hasResult) {
×
7158
    goto _group_key_over;
×
7159
  }
7160

7161
  if (pSBuf->isNull) {
×
7162
    pDBuf->isNull = true;
×
7163
    pDBuf->hasResult = true;
×
7164
    goto _group_key_over;
×
7165
  }
7166

7167
  if (IS_VAR_DATA_TYPE(pSourceCtx->resDataInfo.type)) {
×
7168
    (void)memcpy(pDBuf->data, pSBuf->data,
×
7169
                 (pSourceCtx->resDataInfo.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(pSBuf->data)
×
7170
                                                                       : varDataTLen(pSBuf->data));
×
7171
  } else {
7172
    (void)memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes);
×
7173
  }
7174

7175
  pDBuf->hasResult = true;
×
7176

7177
_group_key_over:
×
7178

7179
  SET_VAL(pDResInfo, 1, 1);
×
7180
  return TSDB_CODE_SUCCESS;
×
7181
}
7182

UNCOV
7183
int32_t cachedLastRowFunction(SqlFunctionCtx* pCtx) {
×
UNCOV
7184
  int32_t numOfElems = 0;
×
7185

UNCOV
7186
  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
×
UNCOV
7187
  SFirstLastRes*       pInfo = GET_ROWCELL_INTERBUF(pResInfo);
×
7188

UNCOV
7189
  SInputColumnInfoData* pInput = &pCtx->input;
×
UNCOV
7190
  SColumnInfoData*      pInputCol = pInput->pData[0];
×
7191

UNCOV
7192
  int32_t bytes = pInputCol->info.bytes;
×
UNCOV
7193
  pInfo->bytes = bytes;
×
7194

UNCOV
7195
  SColumnInfoData* pkCol = pInput->pPrimaryKey;
×
UNCOV
7196
  pInfo->pkType = -1;
×
UNCOV
7197
  __compar_fn_t pkCompareFn = NULL;
×
UNCOV
7198
  if (pCtx->hasPrimaryKey) {
×
UNCOV
7199
    pInfo->pkType = pkCol->info.type;
×
UNCOV
7200
    pInfo->pkBytes = pkCol->info.bytes;
×
UNCOV
7201
    pkCompareFn = getKeyComparFunc(pInfo->pkType, TSDB_ORDER_DESC);
×
7202
  }
7203

7204
  // TODO it traverse the different way.
7205
  // last_row function does not ignore the null value
UNCOV
7206
  for (int32_t i = pInput->numOfRows + pInput->startRowIndex - 1; i >= pInput->startRowIndex; --i) {
×
UNCOV
7207
    numOfElems++;
×
7208

UNCOV
7209
    bool  isNull = colDataIsNull(pInputCol, pInput->numOfRows, i, NULL);
×
UNCOV
7210
    char* data = isNull ? NULL : colDataGetData(pInputCol, i);
×
7211

UNCOV
7212
    TSKEY cts = getRowPTs(pInput->pPTS, i);
×
UNCOV
7213
    if (pResInfo->numOfRes == 0 || pInfo->ts < cts) {
×
UNCOV
7214
      int32_t code = doSaveLastrow(pCtx, data, i, cts, pInfo);
×
UNCOV
7215
      if (code != TSDB_CODE_SUCCESS) {
×
7216
        return code;
×
7217
      }
UNCOV
7218
      pResInfo->numOfRes = 1;
×
7219
    }
7220
  }
7221

UNCOV
7222
  SET_VAL(pResInfo, numOfElems, 1);
×
UNCOV
7223
  return TSDB_CODE_SUCCESS;
×
7224
}
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