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

taosdata / TDengine / #4898

26 Dec 2025 09:58AM UTC coverage: 65.061% (-0.7%) from 65.717%
#4898

push

travis-ci

web-flow
feat: support encryption of configuration files, data files and metadata files (#33801)

350 of 1333 new or added lines in 31 files covered. (26.26%)

2796 existing lines in 159 files now uncovered.

184024 of 282850 relevant lines covered (65.06%)

113940470.33 hits per line

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

74.77
/source/libs/executor/src/executorInt.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 "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "tdatablock.h"
25
#include "tmsg.h"
26
#include "ttime.h"
27

28
#include "executorInt.h"
29
#include "index.h"
30
#include "operator.h"
31
#include "query.h"
32
#include "querytask.h"
33
#include "storageapi.h"
34
#include "tcompare.h"
35
#include "thash.h"
36
#include "ttypes.h"
37

38
#define SET_REVERSE_SCAN_FLAG(runtime)    ((runtime)->scanFlag = REVERSE_SCAN)
39
#define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) != TSDB_ORDER_DESC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)
40

41
#if 0
42
static UNUSED_FUNC void *u_malloc (size_t __size) {
43
  uint32_t v = taosRand();
44

45
  if (v % 1000 <= 0) {
46
    return NULL;
47
  } else {
48
    return taosMemoryMalloc(__size);
49
  }
50
}
51

52
static UNUSED_FUNC void* u_calloc(size_t num, size_t __size) {
53
  uint32_t v = taosRand();
54
  if (v % 1000 <= 0) {
55
    return NULL;
56
  } else {
57
    return taosMemoryCalloc(num, __size);
58
  }
59
}
60

61
static UNUSED_FUNC void* u_realloc(void* p, size_t __size) {
62
  uint32_t v = taosRand();
63
  if (v % 5 <= 1) {
64
    return NULL;
65
  } else {
66
    return taosMemoryRealloc(p, __size);
67
  }
68
}
69

70
#define calloc  u_calloc
71
#define malloc  u_malloc
72
#define realloc u_realloc
73
#endif
74

75
static int32_t setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExpr, SSDataBlock* pBlock);
76

77
static int32_t initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size);
78
static void    doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag);
79

80
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
81
                                   bool createDummyCol);
82

83
SResultRow* getNewResultRow(SDiskbasedBuf* pResultBuf, int32_t* currentPageId, int32_t interBufSize) {
2,147,483,647✔
84
  SFilePage* pData = NULL;
2,147,483,647✔
85

86
  // in the first scan, new space needed for results
87
  int32_t pageId = -1;
2,147,483,647✔
88
  if (*currentPageId == -1) {
2,147,483,647✔
89
    pData = getNewBufPage(pResultBuf, &pageId);
218,239,665✔
90
    if (pData == NULL) {
218,249,174✔
91
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
92
      return NULL;
×
93
    }
94
    pData->num = sizeof(SFilePage);
218,249,174✔
95
  } else {
96
    pData = getBufPage(pResultBuf, *currentPageId);
2,147,483,647✔
97
    if (pData == NULL) {
2,147,483,647✔
98
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
99
      return NULL;
×
100
    }
101

102
    pageId = *currentPageId;
2,147,483,647✔
103

104
    if (pData->num + interBufSize > getBufPageSize(pResultBuf)) {
2,147,483,647✔
105
      // release current page first, and prepare the next one
106
      releaseBufPage(pResultBuf, pData);
527,509,641✔
107

108
      pData = getNewBufPage(pResultBuf, &pageId);
527,517,887✔
109
      if (pData == NULL) {
527,470,456✔
110
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
111
        return NULL;
×
112
      }
113
      pData->num = sizeof(SFilePage);
527,470,456✔
114
    }
115
  }
116

117
  setBufPageDirty(pData, true);
2,147,483,647✔
118

119
  // set the number of rows in current disk page
120
  SResultRow* pResultRow = (SResultRow*)((char*)pData + pData->num);
2,147,483,647✔
121

122
  memset((char*)pResultRow, 0, interBufSize);
2,147,483,647✔
123
  pResultRow->pageId = pageId;
2,147,483,647✔
124
  pResultRow->offset = (int32_t)pData->num;
2,147,483,647✔
125

126
  *currentPageId = pageId;
2,147,483,647✔
127
  pData->num += interBufSize;
2,147,483,647✔
128
  return pResultRow;
2,147,483,647✔
129
}
130

131
/**
132
 * the struct of key in hash table
133
 * +----------+---------------+
134
 * | group id |   key data    |
135
 * | 8 bytes  | actual length |
136
 * +----------+---------------+
137
 */
138
SResultRow* doSetResultOutBufByKey(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, char* pData,
2,147,483,647✔
139
                                   int32_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo,
140
                                   bool isIntervalQuery, SAggSupporter* pSup, bool keepGroup) {
141
  SET_RES_WINDOW_KEY(pSup->keyBuf, pData, bytes, groupId);
2,147,483,647✔
142
  if (!keepGroup) {
2,147,483,647✔
143
    *(uint64_t*)pSup->keyBuf = calcGroupId(pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
2,147,483,647✔
144
  }
145

146
  SResultRowPosition* p1 =
147
      (SResultRowPosition*)tSimpleHashGet(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
2,147,483,647✔
148

149
  SResultRow* pResult = NULL;
2,147,483,647✔
150

151
  // in case of repeat scan/reverse scan, no new time window added.
152
  if (isIntervalQuery) {
2,147,483,647✔
153
    if (p1 != NULL) {  // the *p1 may be NULL in case of sliding+offset exists.
2,147,483,647✔
154
      pResult = getResultRowByPos(pResultBuf, p1, true);
1,455,433,200✔
155
      if (pResult == NULL) {
1,455,433,200✔
156
        pTaskInfo->code = terrno;
×
157
        return NULL;
×
158
      }
159

160
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
1,455,433,200✔
161
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
126✔
162
        pTaskInfo->code = terrno;
×
163
        return NULL;
×
164
      }
165
    }
166
  } else {
167
    // In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the
168
    // pResultRowInfo object.
169
    if (p1 != NULL) {
2,147,483,647✔
170
      // todo
171
      pResult = getResultRowByPos(pResultBuf, p1, true);
1,848,365,974✔
172
      if (NULL == pResult) {
1,848,365,974✔
173
        pTaskInfo->code = terrno;
×
174
        return NULL;
×
175
      }
176

177
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
1,848,365,974✔
UNCOV
178
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
179
        pTaskInfo->code = terrno;
×
180
        return NULL;
×
181
      }
182
    }
183
  }
184

185
  // 1. close current opened time window
186
  if (pResultRowInfo->cur.pageId != -1 && ((pResult == NULL) || (pResult->pageId != pResultRowInfo->cur.pageId))) {
2,147,483,647✔
187
    SResultRowPosition pos = pResultRowInfo->cur;
2,147,483,647✔
188
    SFilePage*         pPage = getBufPage(pResultBuf, pos.pageId);
2,147,483,647✔
189
    if (pPage == NULL) {
2,147,483,647✔
190
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
191
      pTaskInfo->code = terrno;
×
192
      return NULL;
×
193
    }
194
    releaseBufPage(pResultBuf, pPage);
2,147,483,647✔
195
  }
196

197
  // allocate a new buffer page
198
  if (pResult == NULL) {
2,147,483,647✔
199
    pResult = getNewResultRow(pResultBuf, &pSup->currentPageId, pSup->resultRowSize);
2,147,483,647✔
200
    if (pResult == NULL) {
2,147,483,647✔
201
      pTaskInfo->code = terrno;
×
202
      return NULL;
×
203
    }
204

205
    // add a new result set for a new group
206
    SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset};
2,147,483,647✔
207
     int32_t code = tSimpleHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos,
2,147,483,647✔
208
                                  sizeof(SResultRowPosition));
209
    if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
210
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
211
      pTaskInfo->code = code;
×
212
      return NULL;
×
213
    }
214
  }
215

216
  // 2. set the new time window to be the new active time window
217
  pResultRowInfo->cur = (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset};
2,147,483,647✔
218

219
  // too many time window in query
220
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_BATCH &&
2,147,483,647✔
221
      tSimpleHashGetSize(pSup->pResultRowHashTable) > MAX_INTERVAL_TIME_WINDOW) {
2,147,483,647✔
222
    pTaskInfo->code = TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW;
×
223
    return NULL;
×
224
  }
225

226
  return pResult;
2,147,483,647✔
227
}
228

229
//  query_range_start, query_range_end, window_duration, window_start, window_end
230
int32_t initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
16,338,226✔
231
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
16,338,226✔
232
  pColData->info.bytes = sizeof(int64_t);
16,338,779✔
233

234
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
16,338,905✔
235
  if (code != TSDB_CODE_SUCCESS) {
16,337,420✔
236
    return code;
×
237
  }
238
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
16,337,420✔
239
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
16,336,192✔
240

241
  int64_t interval = 0;
16,338,002✔
242
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
243
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
16,335,245✔
244
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
16,334,848✔
245

246
  interval = -1;
16,335,339✔
247
  colDataSetInt64(pColData, 5,  &interval);
248
  return TSDB_CODE_SUCCESS;
16,337,157✔
249
}
250

251
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
5,208,661✔
252
  int32_t         code = TSDB_CODE_SUCCESS;
5,208,661✔
253
  int32_t         lino = 0;
5,208,661✔
254
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
5,208,661✔
255
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
10,435,350✔
256
    pCtx[i].order = order;
5,226,689✔
257
    pCtx[i].input.numOfRows = pBlock->info.rows;
5,226,689✔
258
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
5,226,689✔
259
    QUERY_CHECK_CODE(code, lino, _end);
5,226,689✔
260
    pCtx[i].pSrcBlock = pBlock;
5,226,689✔
261
    pCtx[i].scanFlag = scanFlag;
5,226,689✔
262
  }
263

264
_end:
5,208,661✔
265
  if (code != TSDB_CODE_SUCCESS) {
5,208,661✔
266
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
267
  }
268
  return code;
5,208,661✔
269
}
270

271
int32_t setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
939,254,364✔
272
                          bool createDummyCol) {
273
  if (pBlock->pBlockAgg != NULL) {
939,254,364✔
274
    return doSetInputDataBlockInfo(pExprSup, pBlock, order, scanFlag);
5,208,661✔
275
  } else {
276
    return doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
934,108,220✔
277
  }
278
}
279

280
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
6,807,488✔
281
                                             int32_t numOfRows) {
282
  int32_t          code = TSDB_CODE_SUCCESS;
6,807,488✔
283
  int32_t          lino = 0;
6,807,488✔
284
  SColumnInfoData* pColInfo = NULL;
6,807,488✔
285
  if (pInput->pData[paramIndex] == NULL) {
6,807,488✔
286
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
96,364✔
287
    QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
96,364✔
288

289
    // Set the correct column info (data type and bytes)
290
    pColInfo->info.type = pFuncParam->param.nType;
96,364✔
291
    pColInfo->info.bytes = pFuncParam->param.nLen;
96,364✔
292

293
    pInput->pData[paramIndex] = pColInfo;
96,364✔
294
  } else {
295
    pColInfo = pInput->pData[paramIndex];
6,711,526✔
296
  }
297

298
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
6,807,890✔
299
  QUERY_CHECK_CODE(code, lino, _end);
6,806,684✔
300

301
  int8_t type = pFuncParam->param.nType;
6,806,684✔
302
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
6,806,079✔
303
    int64_t v = pFuncParam->param.i;
84,990✔
304
    for (int32_t i = 0; i < numOfRows; ++i) {
1,523,532✔
305
      colDataSetInt64(pColInfo, i, &v);
1,437,736✔
306
    }
307
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
6,721,089✔
308
    double v = pFuncParam->param.d;
×
309
    for (int32_t i = 0; i < numOfRows; ++i) {
×
310
      colDataSetDouble(pColInfo, i, &v);
×
311
    }
312
  } else if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_GEOMETRY) {
6,721,089✔
313
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
998✔
314
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
315

316
    STR_WITH_SIZE_TO_VARSTR(tmp, pFuncParam->param.pz, pFuncParam->param.nLen);
×
317
    for (int32_t i = 0; i < numOfRows; ++i) {
×
318
      code = colDataSetVal(pColInfo, i, tmp, false);
×
319
      QUERY_CHECK_CODE(code, lino, _end);
×
320
    }
321
    taosMemoryFree(tmp);
×
322
  }
323

324
_end:
6,720,091✔
325
  if (code != TSDB_CODE_SUCCESS) {
6,805,887✔
326
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
327
  }
328
  return code;
6,805,686✔
329
}
330

331
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
934,029,179✔
332
                                   bool createDummyCol) {
333
  int32_t         code = TSDB_CODE_SUCCESS;
934,029,179✔
334
  int32_t         lino = 0;
934,029,179✔
335
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
934,029,179✔
336

337
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
2,147,483,647✔
338
    pCtx[i].order = order;
2,147,483,647✔
339
    pCtx[i].input.numOfRows = pBlock->info.rows;
2,147,483,647✔
340

341
    pCtx[i].pSrcBlock = pBlock;
2,147,483,647✔
342
    pCtx[i].scanFlag = scanFlag;
2,147,483,647✔
343

344
    SInputColumnInfoData* pInput = &pCtx[i].input;
2,147,483,647✔
345
    pInput->uid = pBlock->info.id.uid;
2,147,483,647✔
346
    pInput->colDataSMAIsSet = false;
2,147,483,647✔
347

348
    SExprInfo* pOneExpr = &pExprSup->pExprInfo[i];
2,147,483,647✔
349
    bool       hasPk = pOneExpr->pExpr->nodeType == QUERY_NODE_FUNCTION && pOneExpr->pExpr->_function.pFunctNode->hasPk;
2,147,483,647✔
350
    pCtx[i].hasPrimaryKey = hasPk;
2,147,483,647✔
351

352
    int16_t tsParamIdx = (!hasPk) ? pOneExpr->base.numOfParams - 1 : pOneExpr->base.numOfParams - 2;
2,147,483,647✔
353
    int16_t pkParamIdx = pOneExpr->base.numOfParams - 1;
2,147,483,647✔
354

355
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
2,147,483,647✔
356
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
2,147,483,647✔
357
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
2,147,483,647✔
358
        int32_t slotId = pFuncParam->pCol->slotId;
2,147,483,647✔
359
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
360
        pInput->totalRows = pBlock->info.rows;
2,147,483,647✔
361
        pInput->numOfRows = pBlock->info.rows;
2,147,483,647✔
362
        pInput->startRowIndex = 0;
2,147,483,647✔
363
        pInput->blankFill = pBlock->info.blankFill;
2,147,483,647✔
364

365
        // NOTE: the last parameter is the primary timestamp column
366
        // todo: refactor this
367

368
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == tsParamIdx)) {
2,147,483,647✔
369
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
2,147,483,647✔
370
        }
371
        if (hasPk && (j == pkParamIdx)) {
2,147,483,647✔
372
          pInput->pPrimaryKey = pInput->pData[j];
26,805,237✔
373
        }
374
        QUERY_CHECK_CONDITION((pInput->pData[j] != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
2,147,483,647✔
375
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
301,690,353✔
376
        // todo avoid case: top(k, 12), 12 is the value parameter.
377
        // sum(11), 11 is also the value parameter.
378
        if (createDummyCol && pOneExpr->base.numOfParams == 1) {
142,989,732✔
379
          pInput->totalRows = pBlock->info.rows;
6,807,687✔
380
          pInput->numOfRows = pBlock->info.rows;
6,807,888✔
381
          pInput->startRowIndex = 0;
6,807,486✔
382
          pInput->blankFill = pBlock->info.blankFill;
6,807,687✔
383

384
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
6,808,091✔
385
          QUERY_CHECK_CODE(code, lino, _end);
162,099✔
386
        }
387
      }
388
    }
389
  }
390

391
_end:
934,113,309✔
392
  if (code != TSDB_CODE_SUCCESS) {
934,113,309✔
393
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
394
  }
395
  return code;
933,999,966✔
396
}
397

398
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
2,147,483,647✔
399
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,147,483,647✔
400

401
  // in case of timestamp column, always generated results.
402
  int32_t functionId = pCtx->functionId;
2,147,483,647✔
403
  if (functionId == -1) {
2,147,483,647✔
404
    return false;
2,147,483,647✔
405
  }
406

407
  if (pCtx->scanFlag == PRE_SCAN) {
2,147,483,647✔
408
    return fmIsRepeatScanFunc(pCtx->functionId);
3,044,536✔
409
  }
410

411
  if (isRowEntryCompleted(pResInfo)) {
2,147,483,647✔
412
    return false;
×
413
  }
414

415
  return true;
2,147,483,647✔
416
}
417

418
static int32_t doCreateConstantValColumnSMAInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t type,
×
419
                                                int32_t paramIndex, int32_t numOfRows) {
420
  if (pInput->pData[paramIndex] == NULL) {
×
421
    pInput->pData[paramIndex] = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
422
    if (pInput->pData[paramIndex] == NULL) {
×
423
      return terrno;
×
424
    }
425

426
    // Set the correct column info (data type and bytes)
427
    pInput->pData[paramIndex]->info.type = type;
×
428
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
×
429
  }
430

431
  SColumnDataAgg* da = NULL;
×
432
  if (pInput->pColumnDataAgg[paramIndex] == NULL) {
×
433
    da = taosMemoryCalloc(1, sizeof(SColumnDataAgg));
×
434
    if (!da) {
×
435
      return terrno;
×
436
    }
437
    pInput->pColumnDataAgg[paramIndex] = da;
×
438
  } else {
439
    da = pInput->pColumnDataAgg[paramIndex];
×
440
  }
441

442
  if (type == TSDB_DATA_TYPE_BIGINT) {
×
443
    int64_t v = pFuncParam->param.i;
×
444
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .sum = v * numOfRows};
×
445
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
446
    double v = pFuncParam->param.d;
×
447
    *da = (SColumnDataAgg){.numOfNull = 0};
×
448

449
    *(double*)&da->min = v;
×
450
    *(double*)&da->max = v;
×
451
    *(double*)&da->sum = v * numOfRows;
×
452
  } else if (type == TSDB_DATA_TYPE_BOOL) {  // todo validate this data type
×
453
    bool v = pFuncParam->param.i;
×
454

455
    *da = (SColumnDataAgg){.numOfNull = 0};
×
456
    *(bool*)&da->min = 0;
×
457
    *(bool*)&da->max = v;
×
458
    *(bool*)&da->sum = v * numOfRows;
×
459
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
×
460
    // do nothing
461
  } else {
462
    qError("invalid constant type for sma info");
×
463
  }
464

465
  return TSDB_CODE_SUCCESS;
×
466
}
467

468
int32_t setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
5,226,689✔
469
  int32_t code = TSDB_CODE_SUCCESS;
5,226,689✔
470
  int32_t lino = 0;
5,226,689✔
471
  int32_t numOfRows = pBlock->info.rows;
5,226,689✔
472

473
  SInputColumnInfoData* pInput = &pCtx->input;
5,226,689✔
474
  pInput->numOfRows = numOfRows;
5,226,689✔
475
  pInput->totalRows = numOfRows;
5,226,689✔
476

477
  if (pBlock->pBlockAgg != NULL) {
5,226,689✔
478
    pInput->colDataSMAIsSet = true;
5,226,689✔
479

480
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
10,453,378✔
481
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
5,226,689✔
482

483
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
5,226,689✔
484
        int32_t slotId = pFuncParam->pCol->slotId;
5,226,689✔
485
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
5,226,689✔
486
        if (pInput->pColumnDataAgg[j]->colId == -1) {
5,226,689✔
487
          pInput->colDataSMAIsSet = false;
17,684✔
488
        }
489

490
        // Here we set the column info data since the data type for each column data is required, but
491
        // the data in the corresponding SColumnInfoData will not be used.
492
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
5,226,689✔
493
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
×
494
        code = doCreateConstantValColumnSMAInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
×
495
        QUERY_CHECK_CODE(code, lino, _end);
×
496
      }
497
    }
498
  } else {
499
    pInput->colDataSMAIsSet = false;
×
500
  }
501

502
_end:
5,226,689✔
503
  if (code != TSDB_CODE_SUCCESS) {
5,226,689✔
504
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
505
  }
506
  return code;
5,226,689✔
507
}
508

509
/////////////////////////////////////////////////////////////////////////////////////////////
510
STimeWindow getAlignQueryTimeWindow(const SInterval* pInterval, int64_t key) {
2,147,483,647✔
511
  STimeWindow win = {0};
2,147,483,647✔
512
  win.skey = taosTimeTruncate(key, pInterval);
2,147,483,647✔
513

514
  /*
515
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
516
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
517
   */
518
  win.ekey = taosTimeGetIntervalEnd(win.skey, pInterval);
2,147,483,647✔
519
  if (win.ekey < win.skey) {
2,147,483,647✔
520
    win.ekey = INT64_MAX;
×
521
  }
522

523
  return win;
2,147,483,647✔
524
}
525

526
int32_t setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput,
2,147,483,647✔
527
                            int32_t* rowEntryInfoOffset) {
528
  bool init = false;
2,147,483,647✔
529
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
530
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
2,147,483,647✔
531
    if (init) {
2,147,483,647✔
532
      continue;
2,147,483,647✔
533
    }
534

535
    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
2,147,483,647✔
536
    
537
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
2,147,483,647✔
538
      continue;
×
539
    }
540

541
    if (pCtx[i].isPseudoFunc) {
2,147,483,647✔
542
      continue;
2,147,483,647✔
543
    }
544

545
    if (!pResInfo->initialized) {
2,147,483,647✔
546
      if (pCtx[i].functionId != -1) {
2,147,483,647✔
547
        int32_t code = pCtx[i].fpSet.init(&pCtx[i], pResInfo);
2,147,483,647✔
548
        if (code != TSDB_CODE_SUCCESS && fmIsUserDefinedFunc(pCtx[i].functionId)) {
2,147,483,647✔
UNCOV
549
          pResInfo->initialized = false;
×
UNCOV
550
          qError("failed to initialize udf, funcId:%d error:%s", pCtx[i].functionId, tstrerror(code));
×
551
          return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
552
        } else if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
553
          qError("failed to initialize function context, funcId:%d error:%s", pCtx[i].functionId, tstrerror(code));
×
554
          return code;
×
555
        }
556
      } else {
557
        pResInfo->initialized = true;
2,147,483,647✔
558
      }
559
    } else {
560
      init = true;
2,147,483,647✔
561
    }
562
  }
563
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
564
}
565

566
void clearResultRowInitFlag(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
861,297✔
567
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,367,923✔
568
    SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
1,506,626✔
569
    if (pResInfo == NULL) {
1,506,626✔
570
      continue;
×
571
    }
572

573
    pResInfo->initialized = false;
1,506,626✔
574
    pResInfo->numOfRes = 0;
1,506,626✔
575
    pResInfo->isNullRes = 0;
1,506,626✔
576
    pResInfo->complete = false;
1,506,626✔
577
  }
578
}
861,297✔
579

580
int32_t doFilter(SSDataBlock* pBlock, SFilterInfo* pFilterInfo, SColMatchInfo* pColMatchInfo, SColumnInfoData** pRet) {
2,147,483,647✔
581
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
582
  int32_t lino = 0;
2,147,483,647✔
583
  if (pFilterInfo == NULL || pBlock->info.rows == 0) {
2,147,483,647✔
584
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
585
  }
586

587
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
140,489,337✔
588
  SColumnInfoData*   p = NULL;
140,494,559✔
589

590
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
140,489,685✔
591
  QUERY_CHECK_CODE(code, lino, _err);
140,486,062✔
592

593
  int32_t status = 0;
140,486,062✔
594
  code =
595
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
140,490,411✔
596
  QUERY_CHECK_CODE(code, lino, _err);
140,459,005✔
597

598
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
139,776,222✔
599
  QUERY_CHECK_CODE(code, lino, _err);
139,797,487✔
600

601
  if (pColMatchInfo != NULL) {
139,797,487✔
602
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
86,209,110✔
603
    for (int32_t i = 0; i < size; ++i) {
86,335,197✔
604
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
86,213,740✔
605
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
86,211,816✔
606
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
86,211,816✔
607
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
86,101,092✔
608
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
86,087,349✔
609
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
86,087,349✔
610
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
86,090,120✔
611
          QUERY_CHECK_CODE(code, lino, _err);
86,087,140✔
612
          break;
86,087,140✔
613
        }
614
      }
615
    }
616
  }
617
  code = blockDataCheck(pBlock);
139,796,974✔
618
  QUERY_CHECK_CODE(code, lino, _err);
139,793,579✔
619
_err:
140,476,362✔
620
  if (code != TSDB_CODE_SUCCESS) {
140,498,921✔
621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
682,783✔
622
  }
623
  colDataDestroy(p);
140,498,921✔
624
  taosMemoryFree(p);
140,483,128✔
625
  return code;
140,471,531✔
626
}
627

628
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
139,925,744✔
629
  int32_t code = TSDB_CODE_SUCCESS;
139,925,744✔
630
  int8_t* pIndicator = (int8_t*)p->pData;
139,925,744✔
631
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
139,941,130✔
632
    // here nothing needs to be done
633
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
80,590,264✔
634
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
26,457,191✔
635
    pBlock->info.rows = 0;
26,456,938✔
636
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
54,133,073✔
637
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
54,135,608✔
638
  } else {
UNCOV
639
    qError("unknown filter result type: %d", status);
×
640
  }
641
  return code;
139,941,857✔
642
}
643

644
void doUpdateNumOfRows(SqlFunctionCtx* pCtx, SResultRow* pRow, int32_t numOfExprs, const int32_t* rowEntryOffset) {
2,147,483,647✔
645
  bool returnNotNull = false;
2,147,483,647✔
646
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,147,483,647✔
647
    SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
2,147,483,647✔
648
    if (!isRowEntryInitialized(pResInfo)) {
2,147,483,647✔
649
      continue;
2,147,483,647✔
650
    } else {
651
    }
652

653
    if (pRow->numOfRows < pResInfo->numOfRes) {
2,147,483,647✔
654
      pRow->numOfRows = pResInfo->numOfRes;
2,147,483,647✔
655
    }
656

657
    if (pCtx[j].isNotNullFunc) {
2,147,483,647✔
658
      returnNotNull = true;
2,147,483,647✔
659
    }
660
  }
661
  // if all expr skips all blocks, e.g. all null inputs for max function, output one row in final result.
662
  //  except for first/last, which require not null output, output no rows
663
  if (pRow->numOfRows == 0 && !returnNotNull) {
2,147,483,647✔
664
    pRow->numOfRows = 1;
28,321,215✔
665
  }
666
}
2,147,483,647✔
667

668
int32_t copyResultrowToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SResultRow* pRow, SqlFunctionCtx* pCtx,
2,147,483,647✔
669
                                 SSDataBlock* pBlock, const int32_t* rowEntryOffset, SExecTaskInfo* pTaskInfo) {
670
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
671
  int32_t lino = 0;
2,147,483,647✔
672
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,147,483,647✔
673
    int32_t slotId = pExprInfo[j].base.resSchema.slotId;
2,147,483,647✔
674

675
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
2,147,483,647✔
676
    if (pCtx[j].fpSet.finalize) {
2,147,483,647✔
677
      if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0 ||
2,147,483,647✔
678
          strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_const_value") == 0) {
2,147,483,647✔
679
        // for groupkey along with functions that output multiple lines(e.g. Histogram)
680
        // need to match groupkey result for each output row of that function.
681
        if (pCtx[j].resultInfo->numOfRes != 0) {
2,147,483,647✔
682
          pCtx[j].resultInfo->numOfRes = pRow->numOfRows;
2,147,483,647✔
683
        }
684
      }
685

686
      code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
2,147,483,647✔
687
      if (TSDB_CODE_SUCCESS != code) {
2,147,483,647✔
688
        qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
689
        QUERY_CHECK_CODE(code, lino, _end);
2,250,576✔
690
      }
691
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
2,147,483,647✔
692
      // do nothing
693
    } else {
694
      // expand the result into multiple rows. E.g., _wstart, top(k, 20)
695
      // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
696
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
697
      QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
2,147,483,647✔
698
      char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
2,147,483,647✔
699
      for (int32_t k = 0; k < pRow->numOfRows; ++k) {        
2,147,483,647✔
700
        code = colDataSetValOrCover(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
2,147,483,647✔
701
        QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
702
      }
703
    }
704
  }
705

706
_end:
2,147,483,647✔
707
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
708
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
709
  }
710
  return code;
2,147,483,647✔
711
}
712

713
// todo refactor. SResultRow has direct pointer in miainfo
714
void finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
836,343,919✔
715
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
716
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
836,343,919✔
717
  if (page == NULL) {
836,344,659✔
718
    qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
719
    T_LONG_JMP(pTaskInfo->env, terrno);
×
720
  }
721

722
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);
836,344,659✔
723

724
  SqlFunctionCtx* pCtx = pSup->pCtx;
836,344,659✔
725
  SExprInfo*      pExprInfo = pSup->pExprInfo;
836,345,029✔
726
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
836,345,029✔
727

728
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
836,345,769✔
729
  if (pRow->numOfRows == 0) {
836,343,919✔
730
    releaseBufPage(pBuf, page);
×
731
    return;
×
732
  }
733

734
  int32_t size = pBlock->info.capacity;
836,343,919✔
735
  while (pBlock->info.rows + pRow->numOfRows > size) {
836,538,274✔
736
    size = size * 1.25;
194,355✔
737
  }
738

739
  int32_t code = blockDataEnsureCapacity(pBlock, size);
836,344,289✔
740
  if (TAOS_FAILED(code)) {
836,342,809✔
741
    releaseBufPage(pBuf, page);
×
742
    qError("%s ensure result data capacity failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
743
    T_LONG_JMP(pTaskInfo->env, code);
×
744
  }
745

746
  code = copyResultrowToDataBlock(pExprInfo, pSup->numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
836,342,809✔
747
  if (TAOS_FAILED(code)) {
836,344,289✔
748
    releaseBufPage(pBuf, page);
×
749
    qError("%s copy result row to datablock failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
750
    T_LONG_JMP(pTaskInfo->env, code);
×
751
  }
752

753
  releaseBufPage(pBuf, page);
836,344,289✔
754
  pBlock->info.rows += pRow->numOfRows;
836,344,659✔
755
}
756

757
void doCopyToSDataBlockByHash(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
1,789,694,761✔
758
                              SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t threshold, bool ignoreGroup) {
759
  int32_t         code = TSDB_CODE_SUCCESS;
1,789,694,761✔
760
  int32_t         lino = 0;
1,789,694,761✔
761
  SExprInfo*      pExprInfo = pSup->pExprInfo;
1,789,694,761✔
762
  int32_t         numOfExprs = pSup->numOfExprs;
1,789,697,021✔
763
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
1,789,698,184✔
764
  SqlFunctionCtx* pCtx = pSup->pCtx;
1,789,700,947✔
765

766
  size_t  keyLen = 0;
1,789,699,229✔
767
  int32_t numOfRows = tSimpleHashGetSize(pHashmap);
1,789,699,494✔
768

769
  // begin from last iter
770
  void*   pData = pGroupResInfo->dataPos;
1,789,699,089✔
771
  int32_t iter = pGroupResInfo->iter;
1,789,699,224✔
772
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
2,147,483,647✔
773
    void*               key = tSimpleHashGetKey(pData, &keyLen);
2,147,483,647✔
774
    SResultRowPosition* pos = pData;
2,147,483,647✔
775
    uint64_t            groupId = *(uint64_t*)key;
2,147,483,647✔
776

777
    SFilePage* page = getBufPage(pBuf, pos->pageId);
2,147,483,647✔
778
    if (page == NULL) {
2,147,483,647✔
779
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
780
      T_LONG_JMP(pTaskInfo->env, terrno);
×
781
    }
782

783
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
2,147,483,647✔
784

785
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
786

787
    // no results, continue to check the next one
788
    if (pRow->numOfRows == 0) {
2,147,483,647✔
789
      pGroupResInfo->index += 1;
×
790
      pGroupResInfo->iter = iter;
×
791
      pGroupResInfo->dataPos = pData;
×
792

793
      releaseBufPage(pBuf, page);
×
794
      continue;
×
795
    }
796

797
    if (!ignoreGroup) {
2,147,483,647✔
798
      if (pBlock->info.id.groupId == 0) {
2,147,483,647✔
799
        pBlock->info.id.groupId = groupId;
1,767,417,900✔
800
      } else {
801
        // current value belongs to different group, it can't be packed into one datablock
802
        if (pBlock->info.id.groupId != groupId) {
1,761,715,764✔
803
          releaseBufPage(pBuf, page);
1,761,716,329✔
804
          break;
1,761,715,199✔
805
        }
806
      }
807
    }
808

809
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,147,483,647✔
810
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - iter) > 1 ? 1 : 0);
×
811
      code = blockDataEnsureCapacity(pBlock, newSize);
×
812
      QUERY_CHECK_CODE(code, lino, _end);
×
813
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
814
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
815
      // todo set the pOperator->resultInfo size
816
    }
817

818
    pGroupResInfo->index += 1;
2,147,483,647✔
819
    pGroupResInfo->iter = iter;
2,147,483,647✔
820
    pGroupResInfo->dataPos = pData;
2,147,483,647✔
821

822
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
2,147,483,647✔
823
    releaseBufPage(pBuf, page);
2,147,483,647✔
824
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
825
    pBlock->info.rows += pRow->numOfRows;
2,147,483,647✔
826
    if (pBlock->info.rows >= threshold) {
2,147,483,647✔
827
      break;
11,120✔
828
    }
829
  }
830

831
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
1,789,729,234✔
832
         pBlock->info.id.groupId);
833
  pBlock->info.dataLoad = 1;
1,789,732,624✔
834
  code = blockDataUpdateTsWindow(pBlock, 0);
1,789,702,881✔
835
  QUERY_CHECK_CODE(code, lino, _end);
1,789,702,374✔
836

837
_end:
1,789,702,374✔
838
  if (code != TSDB_CODE_SUCCESS) {
1,789,702,881✔
839
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
840
    T_LONG_JMP(pTaskInfo->env, code);
×
841
  }
842
}
1,789,702,374✔
843

844
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
92,584,551✔
845
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, int64_t minWindowSize) {
846
  int32_t         code = TSDB_CODE_SUCCESS;
92,584,551✔
847
  int32_t         lino = 0;
92,584,551✔
848
  SExprInfo*      pExprInfo = pSup->pExprInfo;
92,584,551✔
849
  int32_t         numOfExprs = pSup->numOfExprs;
92,584,234✔
850
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
92,583,216✔
851
  SqlFunctionCtx* pCtx = pSup->pCtx;
92,582,702✔
852

853
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
92,584,373✔
854

855
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
2,147,483,647✔
856
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
2,147,483,647✔
857
    SFilePage*  page = getBufPage(pBuf, pPos->pos.pageId);
2,147,483,647✔
858
    if (page == NULL) {
2,147,483,647✔
859
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
860
      T_LONG_JMP(pTaskInfo->env, terrno);
×
861
    }
862

863
    SResultRow* pRow = (SResultRow*)((char*)page + pPos->pos.offset);
2,147,483,647✔
864

865
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
866

867
    // no results, continue to check the next one
868
    if (pRow->numOfRows == 0) {
2,147,483,647✔
869
      pGroupResInfo->index += 1;
110,391✔
870
      releaseBufPage(pBuf, page);
110,391✔
871
      continue;
110,391✔
872
    }
873
    // skip the window which is less than the windowMinSize
874
    if (llabs(pRow->win.ekey - pRow->win.skey) < minWindowSize) {
2,147,483,647✔
875
      qDebug("skip small window, groupId: %" PRId64 ", windowSize: %" PRId64 ", minWindowSize: %" PRId64, pPos->groupId,
×
876
             pRow->win.ekey - pRow->win.skey, minWindowSize);
877
      pGroupResInfo->index += 1;
×
878
      releaseBufPage(pBuf, page);
×
879
      continue;
×
880
    }
881

882
    if (!ignoreGroup) {
2,147,483,647✔
883
      if (pBlock->info.id.groupId == 0) {
2,147,483,647✔
884
        pBlock->info.id.groupId = pPos->groupId;
900,031,700✔
885
      } else {
886
        // current value belongs to different group, it can't be packed into one datablock
887
        if (pBlock->info.id.groupId != pPos->groupId) {
1,279,019,777✔
888
          releaseBufPage(pBuf, page);
11,430,919✔
889
          break;
11,430,919✔
890
        }
891
      }
892
    }
893

894
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,147,483,647✔
895
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - i) > 1 ? 1 : 0);
×
896
      code = blockDataEnsureCapacity(pBlock, newSize);
×
897
      QUERY_CHECK_CODE(code, lino, _end);
×
898
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
899
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
900
      // todo set the pOperator->resultInfo size
901
    }
902

903
    pGroupResInfo->index += 1;
2,147,483,647✔
904
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
2,147,483,647✔
905
    releaseBufPage(pBuf, page);
2,147,483,647✔
906
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
907

908
    pBlock->info.rows += pRow->numOfRows;
2,147,483,647✔
909
    if (pBlock->info.rows >= threshold) {
2,147,483,647✔
910
      break;
13,751,704✔
911
    }
912
  }
913

914
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
92,394,234✔
915
         pBlock->info.id.groupId);
916
  pBlock->info.dataLoad = 1;
92,402,662✔
917
  code = blockDataUpdateTsWindow(pBlock, 0);
92,587,036✔
918
  QUERY_CHECK_CODE(code, lino, _end);
92,587,036✔
919

920
_end:
92,587,036✔
921
  if (code != TSDB_CODE_SUCCESS) {
92,587,036✔
922
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
923
    T_LONG_JMP(pTaskInfo->env, code);
×
924
  }
925
}
92,587,036✔
926

927
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
112,217,815✔
928
                            SDiskbasedBuf* pBuf) {
929
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
112,217,815✔
930
  SSDataBlock*   pBlock = pbInfo->pRes;
112,222,822✔
931

932
  // set output datablock version
933
  pBlock->info.version = pTaskInfo->version;
112,217,963✔
934

935
  blockDataCleanup(pBlock);
112,217,143✔
936
  if (!hasRemainResults(pGroupResInfo)) {
112,220,999✔
937
    return;
19,683,902✔
938
  }
939

940
  // clear the existed group id
941
  pBlock->info.id.groupId = 0;
92,531,603✔
942
  if (!pbInfo->mergeResultBlock) {
92,532,082✔
943
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
32,587,822✔
944
                       false, getMinWindowSize(pOperator));
945
  } else {
946
    while (hasRemainResults(pGroupResInfo)) {
110,914,443✔
947
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
59,941,983✔
948
                         true, getMinWindowSize(pOperator));
949
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
59,944,580✔
950
        break;
8,972,115✔
951
      }
952

953
      // clearing group id to continue to merge data that belong to different groups
954
      pBlock->info.id.groupId = 0;
50,972,465✔
955
    }
956

957
    // clear the group id info in SSDataBlock, since the client does not need it
958
    pBlock->info.id.groupId = 0;
59,944,580✔
959
  }
960
}
961

962
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
380,038,026✔
963
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,436,513,610✔
964
    SExprInfo* pExprInfo = &pExpr[i];
1,056,469,687✔
965
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,147,483,647✔
966
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
1,222,870,367✔
967
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
979,374,745✔
968
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
243,535,493✔
969
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
155,986,755✔
970
      }
971
    }
972

973
    taosMemoryFree(pExprInfo->base.pParam);
1,056,517,715✔
974
    taosMemoryFree(pExprInfo->pExpr);
1,056,545,383✔
975
  }
976
}
380,043,923✔
977

978
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
263,507,258✔
979
  *defaultPgsz = 4096;
263,507,258✔
980
  uint32_t last = *defaultPgsz;
263,539,897✔
981
  while (*defaultPgsz < rowSize * 4) {
319,282,734✔
982
    *defaultPgsz <<= 1u;
55,761,946✔
983
    if (*defaultPgsz < last) {
55,743,177✔
984
      return TSDB_CODE_INVALID_PARA;
×
985
    }
986
    last = *defaultPgsz;
55,739,894✔
987
  }
988

989
  // The default buffer for each operator in query is 10MB.
990
  // at least four pages need to be in buffer
991
  // TODO: make this variable to be configurable.
992
  *defaultBufsz = 4096 * 2560;
263,403,450✔
993
  if ((*defaultBufsz) <= (*defaultPgsz)) {
263,444,379✔
994
    (*defaultBufsz) = (*defaultPgsz) * 4;
×
995
    if (*defaultBufsz < ((int64_t)(*defaultPgsz)) * 4) {
×
996
      return TSDB_CODE_INVALID_PARA;
×
997
    }
998
  }
999

1000
  return 0;
263,416,605✔
1001
}
1002

1003
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
556,169,536✔
1004
  if (numOfRows == 0) {
556,169,536✔
1005
    numOfRows = 4096;
×
1006
  }
1007

1008
  pResultInfo->capacity = numOfRows;
556,169,536✔
1009
  pResultInfo->threshold = numOfRows * 0.75;
556,268,223✔
1010

1011
  if (pResultInfo->threshold == 0) {
556,123,287✔
1012
    pResultInfo->threshold = numOfRows;
745,338✔
1013
  }
1014
  pResultInfo->totalRows = 0;
556,192,164✔
1015
}
556,092,840✔
1016

1017
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
245,349,074✔
1018
  pInfo->pRes = pBlock;
245,349,074✔
1019
  initResultRowInfo(&pInfo->resultRowInfo);
245,409,435✔
1020
}
245,262,557✔
1021

1022
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
1,070,487,980✔
1023
  if (pCtx == NULL) {
1,070,487,980✔
1024
    return;
661,580,958✔
1025
  }
1026

1027
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,447,784,057✔
1028
    if (pExpr != NULL) {
1,038,886,838✔
1029
      SExprInfo* pExprInfo = &pExpr[i];
1,038,883,373✔
1030
      for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,147,483,647✔
1031
        if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
1,208,491,371✔
1032
          colDataDestroy(pCtx[i].input.pData[j]);
154,754,034✔
1033
          taosMemoryFree(pCtx[i].input.pData[j]);
154,757,420✔
1034
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
154,752,984✔
1035
        }
1036
      }
1037
    }
1038
    for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) {
2,147,483,647✔
1039
      taosVariantDestroy(&pCtx[i].param[j].param);
1,208,542,045✔
1040
    }
1041

1042
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,038,923,824✔
1043
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,038,931,408✔
1044
    taosMemoryFree(pCtx[i].input.pData);
1,038,930,721✔
1045
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,038,840,980✔
1046

1047
    if (pCtx[i].udfName != NULL) {
1,038,833,944✔
1048
      taosMemoryFree(pCtx[i].udfName);
17,595✔
1049
    }
1050
  }
1051

1052
  taosMemoryFreeClear(pCtx);
408,897,219✔
1053
  return;
408,886,411✔
1054
}
1055

1056
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
392,320,329✔
1057
  pSup->pExprInfo = pExprInfo;
392,320,329✔
1058
  pSup->numOfExprs = numOfExpr;
392,346,075✔
1059
  if (pSup->pExprInfo != NULL) {
392,333,491✔
1060
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
310,960,340✔
1061
    if (pSup->pCtx == NULL) {
310,843,368✔
1062
      return terrno;
×
1063
    }
1064
  }
1065

1066
  return TSDB_CODE_SUCCESS;
392,201,585✔
1067
}
1068

1069
void checkIndefRowsFuncs(SExprSupp* pSup) {
×
1070
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
×
1071
    if (fmIsIndefiniteRowsFunc(pSup->pCtx[i].functionId)) {
×
1072
      pSup->hasIndefRowsFunc = true;
×
1073
      break;
×
1074
    }
1075
  }
1076
}
×
1077

1078
void cleanupExprSupp(SExprSupp* pSupp) {
981,035,792✔
1079
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
981,035,792✔
1080
  if (pSupp->pExprInfo != NULL) {
981,035,281✔
1081
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
341,104,339✔
1082
    taosMemoryFreeClear(pSupp->pExprInfo);
341,102,580✔
1083
  }
1084

1085
  if (pSupp->pFilterInfo != NULL) {
981,030,024✔
1086
    filterFreeInfo(pSupp->pFilterInfo);
60,521,357✔
1087
    pSupp->pFilterInfo = NULL;
60,520,101✔
1088
  }
1089

1090
  taosMemoryFree(pSupp->rowEntryInfoOffset);
981,032,458✔
1091
  memset(pSupp, 0, sizeof(SExprSupp));
981,015,668✔
1092
}
981,015,668✔
1093

1094
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
89,327,434✔
1095
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
89,327,434✔
1096
  if (pSupp->pExprInfo != NULL) {
89,324,757✔
1097
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
33,772,893✔
1098
    taosMemoryFreeClear(pSupp->pExprInfo);
33,773,580✔
1099
  }
1100

1101
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
89,325,025✔
1102
  pSupp->numOfExprs = 0;
89,325,356✔
1103
  pSupp->hasWindowOrGroup = false;
89,328,379✔
1104
  pSupp->pCtx = NULL;
89,323,461✔
1105
}
89,328,521✔
1106

1107
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
254,883,453✔
1108
  blockDataDestroy(pInfo->pRes);
254,883,453✔
1109
  pInfo->pRes = NULL;
254,881,202✔
1110
}
254,880,637✔
1111

1112
bool groupbyTbname(SNodeList* pGroupList) {
225,289,254✔
1113
  bool   bytbname = false;
225,289,254✔
1114
  SNode* pNode = NULL;
225,289,254✔
1115
  FOREACH(pNode, pGroupList) {
232,334,847✔
1116
    if (pNode->type == QUERY_NODE_FUNCTION) {
38,071,687✔
1117
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
31,024,811✔
1118
      break;
31,024,811✔
1119
    }
1120
  }
1121
  return bytbname;
225,288,956✔
1122
}
1123

1124
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, SExecTaskInfo* pTask, SReadHandle* readHandle) {
304,409,739✔
1125
  switch (pNode->type) {
304,409,739✔
1126
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
68,383✔
1127
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
68,383✔
1128
      if (NULL == pInserterParam) {
68,383✔
1129
        return terrno;
×
1130
      }
1131
      pInserterParam->readHandle = readHandle;
68,383✔
1132

1133
      *pParam = pInserterParam;
68,383✔
1134
      break;
68,383✔
1135
    }
1136
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,676,681✔
1137
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,676,681✔
1138
      if (NULL == pDeleterParam) {
1,644,778✔
1139
        return terrno;
×
1140
      }
1141

1142
      SArray* pInfoList = NULL;
1,644,778✔
1143
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,644,778✔
1144
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,645,314✔
1145
        taosMemoryFree(pDeleterParam);
×
1146
        return code;
×
1147
      }
1148

1149
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
1,645,314✔
1150
      taosArrayDestroy(pInfoList);
1,644,778✔
1151

1152
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,644,778✔
1153

1154
      // TODO extract uid list
1155
      int32_t numOfTables = 0;
1,645,314✔
1156
      code = tableListGetSize(pTableListInfo, &numOfTables);
1,645,314✔
1157
      if (code != TSDB_CODE_SUCCESS) {
1,645,314✔
1158
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1159
        taosMemoryFree(pDeleterParam);
×
1160
        return code;
×
1161
      }
1162

1163
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
1,645,314✔
1164
      if (NULL == pDeleterParam->pUidList) {
1,645,314✔
1165
        taosMemoryFree(pDeleterParam);
×
1166
        return terrno;
×
1167
      }
1168

1169
      for (int32_t i = 0; i < numOfTables; ++i) {
3,515,383✔
1170
        STableKeyInfo* pTable = tableListGetInfo(pTableListInfo, i);
1,870,069✔
1171
        if (!pTable) {
1,870,069✔
1172
          taosArrayDestroy(pDeleterParam->pUidList);
×
1173
          taosMemoryFree(pDeleterParam);
×
1174
          return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1175
        }
1176
        void* tmp = taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
1,870,069✔
1177
        if (!tmp) {
1,870,069✔
1178
          taosArrayDestroy(pDeleterParam->pUidList);
×
1179
          taosMemoryFree(pDeleterParam);
×
1180
          return terrno;
×
1181
        }
1182
      }
1183

1184
      *pParam = pDeleterParam;
1,645,314✔
1185
      break;
1,645,314✔
1186
    }
1187
    default:
302,694,330✔
1188
      break;
302,694,330✔
1189
  }
1190

1191
  return TSDB_CODE_SUCCESS;
304,457,697✔
1192
}
1193

1194
void streamOpReleaseState(SOperatorInfo* pOperator) {
×
1195
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1196
  if (downstream->fpSet.releaseStreamStateFn) {
×
1197
    downstream->fpSet.releaseStreamStateFn(downstream);
×
1198
  }
1199
}
×
1200

1201
void streamOpReloadState(SOperatorInfo* pOperator) {
×
1202
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1203
  if (downstream->fpSet.reloadStreamStateFn) {
×
1204
    downstream->fpSet.reloadStreamStateFn(downstream);
×
1205
  }
1206
}
×
1207

1208
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
12,328,603✔
1209
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
12,328,603✔
1210
  for (int32_t i = 0; i < childrenNum; ++i) {
12,328,603✔
UNCOV
1211
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
×
UNCOV
1212
    freeOperatorParam(pChild, type);
×
1213
  }
1214

1215
  taosArrayDestroy(pParam->pChildren);
12,328,603✔
1216
  pParam->pChildren = NULL;
12,328,603✔
1217

1218
  taosMemoryFreeClear(pParam->value);
12,328,603✔
1219

1220
  taosMemoryFree(pParam);
12,327,920✔
1221
}
12,328,603✔
1222

1223
void freeExchangeGetBasicOperatorParam(void* pParam) {
464,196✔
1224
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
464,196✔
1225
  if (pBasic->uidList) {
464,196✔
1226
    taosArrayDestroy(pBasic->uidList);
464,196✔
1227
    pBasic->uidList = NULL;
464,196✔
1228
  }
1229
  if (pBasic->orgTbInfo) {
464,196✔
1230
    taosArrayDestroy(pBasic->orgTbInfo->colMap);
142,033✔
1231
    taosMemoryFreeClear(pBasic->orgTbInfo);
142,033✔
1232
  }
1233
  if (pBasic->batchOrgTbInfo) {
464,196✔
UNCOV
1234
    taosArrayDestroyEx(pBasic->batchOrgTbInfo, destroySOrgTbInfo);
×
UNCOV
1235
    pBasic->batchOrgTbInfo = NULL;
×
1236
  }
1237
  if (pBasic->tagList) {
464,196✔
UNCOV
1238
    taosArrayDestroyEx(pBasic->tagList, destroyTagVal);
×
UNCOV
1239
    pBasic->tagList = NULL;
×
1240
  }
1241
}
464,196✔
1242

1243
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
369,504✔
1244
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
369,504✔
1245
  if (pExcParam->multiParams) {
369,504✔
1246
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
122,254✔
1247
    tSimpleHashSetFreeFp(pExcBatch->pBatchs, freeExchangeGetBasicOperatorParam);
122,254✔
1248
    tSimpleHashCleanup(pExcBatch->pBatchs);
122,254✔
1249
  } else {
1250
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
247,250✔
1251
  }
1252

1253
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
369,504✔
1254
}
369,504✔
1255

1256
void freeExchangeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1257

1258
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
7,164,564✔
1259

1260
void freeGroupCacheNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1261

1262
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
3,582,282✔
1263

1264
void freeMergeJoinNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1265

1266
void freeTagScanGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
49,968✔
1267

UNCOV
1268
void freeMergeGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
×
1269

1270
void freeDynQueryCtrlGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
×
1271

1272
void freeDynQueryCtrlNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1273

1274
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
1,061,280✔
1275
  STableScanOperatorParam* pTableScanParam = (STableScanOperatorParam*)pParam->value;
1,061,280✔
1276
  taosArrayDestroy(pTableScanParam->pUidList);
1,061,280✔
1277
  if (pTableScanParam->pOrgTbInfo) {
1,061,280✔
1278
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
608,292✔
1279
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
608,292✔
1280
  }
1281
  if (pTableScanParam->pBatchTbInfo) {
1,061,280✔
UNCOV
1282
    for (int32_t i = 0; i < taosArrayGetSize(pTableScanParam->pBatchTbInfo); i++) {
×
UNCOV
1283
      SOrgTbInfo* pOrgTbInfo = (SOrgTbInfo*)taosArrayGet(pTableScanParam->pBatchTbInfo, i);
×
UNCOV
1284
      taosArrayDestroy(pOrgTbInfo->colMap);
×
1285
    }
UNCOV
1286
    taosArrayDestroy(pTableScanParam->pBatchTbInfo);
×
UNCOV
1287
    pTableScanParam->pBatchTbInfo = NULL;
×
1288
  }
1289
  if (pTableScanParam->pTagList) {
1,061,280✔
UNCOV
1290
    for (int32_t i = 0; i < taosArrayGetSize(pTableScanParam->pTagList); i++) {
×
UNCOV
1291
      STagVal* pTagVal = (STagVal*)taosArrayGet(pTableScanParam->pTagList, i);
×
UNCOV
1292
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
×
UNCOV
1293
        taosMemoryFreeClear(pTagVal->pData);
×
1294
      }
1295
    }
UNCOV
1296
    taosArrayDestroy(pTableScanParam->pTagList);
×
UNCOV
1297
    pTableScanParam->pTagList = NULL;
×
1298
  }
1299
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
1,061,280✔
1300
}
1,061,280✔
1301

1302
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1303

1304
void freeTagScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1305

1306
void freeMergeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1307

1308
void freeOpParamItem(void* pItem) {
243,038✔
1309
  SOperatorParam* pParam = *(SOperatorParam**)pItem;
243,038✔
1310
  pParam->reUse = false;
243,038✔
1311
  freeOperatorParam(pParam, OP_GET_PARAM);
243,038✔
1312
}
243,038✔
1313

1314
void freeExternalWindowGetOperatorParam(SOperatorParam* pParam) {
×
1315
  SExternalWindowOperatorParam *pExtParam = (SExternalWindowOperatorParam*)pParam->value;
×
1316
  taosArrayDestroy(pExtParam->ExtWins);
×
1317
  for (int32_t i = 0; i < taosArrayGetSize(pParam->pChildren); i++) {
×
1318
    SOperatorParam* pChild = *(SOperatorParam**)taosArrayGet(pParam->pChildren, i);
×
1319
    pChild->reUse = false;
×
1320
  }
1321
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
×
1322
}
×
1323

1324
void freeVirtualTableScanGetOperatorParam(SOperatorParam* pParam) {
101,005✔
1325
  SVTableScanOperatorParam* pVTableScanParam = (SVTableScanOperatorParam*)pParam->value;
101,005✔
1326
  taosArrayDestroyEx(pVTableScanParam->pOpParamArray, freeOpParamItem);
101,005✔
1327
  freeOpParamItem(&pVTableScanParam->pTagScanOp);
101,005✔
1328
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
101,005✔
1329
}
101,005✔
1330

1331
void freeVTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1332

1333
void freeExternalWindowNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1334

1335
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
481,112,607✔
1336
  if (NULL == pParam || pParam->reUse) {
481,112,607✔
1337
    return;
468,787,878✔
1338
  }
1339

1340
  switch (pParam->opType) {
12,324,729✔
1341
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
369,504✔
1342
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
369,504✔
1343
      break;
369,504✔
1344
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
7,164,564✔
1345
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
7,164,564✔
1346
      break;
7,164,564✔
1347
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
3,582,282✔
1348
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
3,582,282✔
1349
      break;
3,582,282✔
1350
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
1,061,280✔
1351
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
1,061,280✔
1352
      break;
1,061,280✔
1353
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
101,005✔
1354
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
101,005✔
1355
      break;
101,005✔
1356
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
49,968✔
1357
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
49,968✔
1358
      break;
49,968✔
UNCOV
1359
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
×
1360
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
UNCOV
1361
      type == OP_GET_PARAM ? freeMergeGetOperatorParam(pParam) : freeMergeNotifyOperatorParam(pParam);
×
UNCOV
1362
      break;
×
1363
    case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
×
1364
      type == OP_GET_PARAM ? freeExternalWindowGetOperatorParam(pParam) : freeExternalWindowNotifyOperatorParam(pParam);
×
1365
      break;
×
1366
    case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL:
×
1367
      type == OP_GET_PARAM ? freeDynQueryCtrlGetOperatorParam(pParam) : freeDynQueryCtrlNotifyOperatorParam(pParam);
×
1368
      break;
×
UNCOV
1369
    default:
×
UNCOV
1370
      qError("unsupported op %d param, type %d", pParam->opType, type);
×
1371
      break;
×
1372
  }
1373
}
1374

1375
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,399,706,299✔
1376
  SOperatorParam**  ppParam = NULL;
1,399,706,299✔
1377
  SOperatorParam*** pppDownstramParam = NULL;
1,399,706,299✔
1378
  switch (type) {
1,399,706,299✔
1379
    case OP_GET_PARAM:
702,358,013✔
1380
      ppParam = &pOperator->pOperatorGetParam;
702,358,013✔
1381
      pppDownstramParam = &pOperator->pDownstreamGetParams;
702,375,901✔
1382
      break;
702,358,083✔
1383
    case OP_NOTIFY_PARAM:
697,534,636✔
1384
      ppParam = &pOperator->pOperatorNotifyParam;
697,534,636✔
1385
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
697,524,398✔
1386
      break;
697,539,064✔
1387
    default:
×
1388
      return;
×
1389
  }
1390

1391
  if (*ppParam) {
1,399,897,147✔
1392
    freeOperatorParam(*ppParam, type);
3,683,287✔
1393
    *ppParam = NULL;
3,683,287✔
1394
  }
1395

1396
  if (*pppDownstramParam) {
1,399,694,836✔
1397
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
11,991,007✔
1398
      if ((*pppDownstramParam)[i]) {
7,291,164✔
1399
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1400
        (*pppDownstramParam)[i] = NULL;
×
1401
      }
1402
    }
1403
    if (allFree) {
4,699,843✔
1404
      taosMemoryFreeClear(*pppDownstramParam);
655,750✔
1405
    }
1406
  }
1407
}
1408

1409
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
1,304,678,245✔
1410
                                                    SSDataBlock** pResBlock) {
1411
  QRY_PARAM_CHECK(pResBlock);
1,304,678,245✔
1412

1413
  int32_t code = 0;
1,304,814,140✔
1414
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
1,304,814,140✔
1415
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
10,880,350✔
1416
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
21,760,700✔
1417
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
10,880,350✔
1418
    if (clearParam && (code == 0)) {
10,880,350✔
1419
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
×
1420
      pOperator->pDownstreamGetParams[idx] = NULL;
×
1421
    }
1422

1423
    if (code) {
10,880,350✔
1424
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
1425
    }
1426
    return code;
10,880,350✔
1427
  }
1428

1429
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
1,293,934,791✔
1430
  if (code) {
1,289,828,582✔
1431
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1432
  }
1433
  return code;
1,289,788,164✔
1434
}
1435

1436
bool compareVal(const char* v, const SStateKeys* pKey) {
65,002,287✔
1437
  if (IS_VAR_DATA_TYPE(pKey->type)) {
65,002,287✔
1438
    if (IS_STR_DATA_BLOB(pKey->type)) {
366,788✔
1439
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
1440
        return false;
×
1441
      } else {
1442
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
1443
      }
1444
    } else {
1445
      if (varDataLen(v) != varDataLen(pKey->pData)) {
365,808✔
1446
        return false;
×
1447
      } else {
1448
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
365,808✔
1449
      }
1450
    }
1451
  } else {
1452
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
64,635,989✔
1453
  }
1454
}
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