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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

79.84
/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);
254,906,737✔
90
    if (pData == NULL) {
254,912,377✔
91
      qError("failed to get buffer, code:%s", tstrerror(terrno));
13✔
92
      return NULL;
×
93
    }
94
    pData->num = sizeof(SFilePage);
254,912,364✔
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);
800,689,169✔
107

108
      pData = getNewBufPage(pResultBuf, &pageId);
800,713,207✔
109
      if (pData == NULL) {
800,657,630✔
110
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
111
        return NULL;
×
112
      }
113
      pData->num = sizeof(SFilePage);
800,657,630✔
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,636,820,470✔
155
      if (pResult == NULL) {
1,636,820,470✔
156
        pTaskInfo->code = terrno;
×
157
        return NULL;
×
158
      }
159

160
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
1,636,820,470✔
161
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
1,506✔
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);
2,147,483,647✔
172
      if (NULL == pResult) {
2,147,483,647✔
173
        pTaskInfo->code = terrno;
×
174
        return NULL;
×
175
      }
176

177
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
2,147,483,647✔
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✔
UNCOV
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) {
8,572,076✔
231
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
8,572,076✔
232
  pColData->info.bytes = sizeof(int64_t);
8,573,153✔
233

234
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
8,572,286✔
235
  if (code != TSDB_CODE_SUCCESS) {
8,571,135✔
236
    return code;
×
237
  }
238
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
8,571,135✔
239
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
8,568,691✔
240

241
  int64_t interval = 0;
8,567,831✔
242
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
243
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
8,563,715✔
244
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
8,567,469✔
245

246
  interval = -1;
8,569,959✔
247
  colDataSetInt64(pColData, 5,  &interval);
248
  return TSDB_CODE_SUCCESS;
8,571,397✔
249
}
250

251
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
22,954,592✔
252
  int32_t         code = TSDB_CODE_SUCCESS;
22,954,592✔
253
  int32_t         lino = 0;
22,954,592✔
254
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
22,954,592✔
255
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
124,166,676✔
256
    pCtx[i].order = order;
101,195,939✔
257
    pCtx[i].input.numOfRows = pBlock->info.rows;
101,197,146✔
258
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
101,195,926✔
259
    QUERY_CHECK_CODE(code, lino, _end);
101,209,151✔
260
    pCtx[i].pSrcBlock = pBlock;
101,209,151✔
261
    pCtx[i].scanFlag = scanFlag;
101,213,330✔
262
  }
263

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

271
int32_t setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
2,147,483,647✔
272
                          bool createDummyCol) {
273
  if (pBlock->pBlockAgg != NULL) {
2,147,483,647✔
274
    return doSetInputDataBlockInfo(pExprSup, pBlock, order, scanFlag);
22,968,920✔
275
  } else {
276
    return doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
2,147,483,647✔
277
  }
278
}
279

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

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

293
    pInput->pData[paramIndex] = pColInfo;
321,506✔
294
  } else {
295
    pColInfo = pInput->pData[paramIndex];
31,892,425✔
296
  }
297

298
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
32,215,506✔
299
  QUERY_CHECK_CODE(code, lino, _end);
32,207,611✔
300

301
  int8_t type = pFuncParam->param.nType;
32,207,611✔
302
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
32,208,741✔
303
    int64_t v = pFuncParam->param.i;
382,056✔
304
    for (int32_t i = 0; i < numOfRows; ++i) {
744,719,474✔
305
      colDataSetInt64(pColInfo, i, &v);
744,336,283✔
306
    }
307
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
31,826,685✔
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) {
31,826,685✔
313
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
235✔
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:
31,826,450✔
325
  if (code != TSDB_CODE_SUCCESS) {
32,209,641✔
326
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
327
  }
328
  return code;
32,207,483✔
329
}
330

331
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
2,147,483,647✔
332
                                   bool createDummyCol) {
333
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
334
  int32_t         lino = 0;
2,147,483,647✔
335
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
2,147,483,647✔
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];
27,429,129✔
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) {
360,195,301✔
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) {
186,226,742✔
379
          pInput->totalRows = pBlock->info.rows;
32,213,919✔
380
          pInput->numOfRows = pBlock->info.rows;
32,213,694✔
381
          pInput->startRowIndex = 0;
32,216,179✔
382
          pInput->blankFill = pBlock->info.blankFill;
32,214,599✔
383

384
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
32,216,629✔
385
          QUERY_CHECK_CODE(code, lino, _end);
34,577,090✔
386
        }
387
      }
388
    }
389
  }
390

391
_end:
2,147,483,647✔
392
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
393
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
394
  }
395
  return code;
2,147,483,647✔
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,709,468✔
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) {
101,188,139✔
469
  int32_t code = TSDB_CODE_SUCCESS;
101,188,139✔
470
  int32_t lino = 0;
101,188,139✔
471
  int32_t numOfRows = pBlock->info.rows;
101,188,139✔
472

473
  SInputColumnInfoData* pInput = &pCtx->input;
101,202,506✔
474
  pInput->numOfRows = numOfRows;
101,205,517✔
475
  pInput->totalRows = numOfRows;
101,230,033✔
476

477
  if (pBlock->pBlockAgg != NULL) {
101,245,672✔
478
    pInput->colDataSMAIsSet = true;
101,246,178✔
479

480
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
202,442,117✔
481
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
101,234,199✔
482

483
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
101,236,639✔
484
        int32_t slotId = pFuncParam->pCol->slotId;
101,234,212✔
485
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
101,231,850✔
486
        if (pInput->pColumnDataAgg[j]->colId == -1) {
101,246,814✔
487
          pInput->colDataSMAIsSet = false;
19,005✔
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);
101,233,018✔
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);
10,149✔
496
      }
497
    }
498
  } else {
499
    pInput->colDataSMAIsSet = false;
×
500
  }
501

502
_end:
101,056,993✔
503
  if (code != TSDB_CODE_SUCCESS) {
101,056,993✔
504
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
505
  }
506
  return code;
101,207,360✔
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✔
549
          pResInfo->initialized = false;
×
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) {
624,003,619✔
567
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
568
    SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
2,147,483,647✔
569
    if (pResInfo == NULL) {
2,147,483,647✔
570
      continue;
×
571
    }
572

573
    pResInfo->initialized = false;
2,147,483,647✔
574
    pResInfo->numOfRes = 0;
2,147,483,647✔
575
    pResInfo->isNullRes = 0;
2,147,483,647✔
576
    pResInfo->complete = false;
2,147,483,647✔
577
  }
578
}
624,003,619✔
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};
167,647,942✔
588
  SColumnInfoData*   p = NULL;
167,630,419✔
589

590
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
167,625,841✔
591
  QUERY_CHECK_CODE(code, lino, _err);
167,632,465✔
592

593
  int32_t status = 0;
167,632,465✔
594
  code =
595
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
167,637,386✔
596
  QUERY_CHECK_CODE(code, lino, _err);
167,612,019✔
597

598
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
166,642,629✔
599
  QUERY_CHECK_CODE(code, lino, _err);
166,652,585✔
600

601
  if (pColMatchInfo != NULL) {
166,652,585✔
602
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
124,331,447✔
603
    for (int32_t i = 0; i < size; ++i) {
124,474,081✔
604
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
124,345,410✔
605
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
124,333,455✔
606
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
124,333,455✔
607
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
124,219,289✔
608
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
124,208,035✔
609
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
124,208,035✔
610
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
124,206,000✔
611
          QUERY_CHECK_CODE(code, lino, _err);
124,205,547✔
612
          break;
124,205,547✔
613
        }
614
      }
615
    }
616
  }
617
  code = blockDataCheck(pBlock);
166,655,356✔
618
  QUERY_CHECK_CODE(code, lino, _err);
166,659,994✔
619
_err:
167,629,384✔
620
  if (code != TSDB_CODE_SUCCESS) {
167,628,067✔
621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
969,390✔
622
  }
623
  colDataDestroy(p);
167,628,067✔
624
  taosMemoryFree(p);
167,621,389✔
625
  return code;
167,622,401✔
626
}
627

628
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
166,831,853✔
629
  int32_t code = TSDB_CODE_SUCCESS;
166,831,853✔
630
  int8_t* pIndicator = (int8_t*)p->pData;
166,831,853✔
631
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
166,849,560✔
632
    // here nothing needs to be done
633
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
104,688,327✔
634
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
45,155,729✔
635
    pBlock->info.rows = 0;
45,140,931✔
636
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
59,532,598✔
637
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
59,534,225✔
638
  } else {
639
    qError("unknown filter result type: %d", status);
322✔
640
  }
641
  return code;
166,841,294✔
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;
2,147,483,647✔
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,185,038✔
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,
921,608,513✔
715
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
716
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
921,608,513✔
717
  if (page == NULL) {
921,608,513✔
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);
921,608,513✔
723

724
  SqlFunctionCtx* pCtx = pSup->pCtx;
921,608,738✔
725
  SExprInfo*      pExprInfo = pSup->pExprInfo;
921,608,513✔
726
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
921,608,513✔
727

728
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
921,608,513✔
729
  if (pRow->numOfRows == 0) {
921,608,170✔
730
    releaseBufPage(pBuf, page);
×
731
    return;
×
732
  }
733

734
  int32_t size = pBlock->info.capacity;
921,608,170✔
735
  while (pBlock->info.rows + pRow->numOfRows > size) {
921,825,084✔
736
    size = size * 1.25;
216,571✔
737
  }
738

739
  int32_t code = blockDataEnsureCapacity(pBlock, size);
921,608,513✔
740
  if (TAOS_FAILED(code)) {
921,608,104✔
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);
921,608,104✔
747
  if (TAOS_FAILED(code)) {
921,608,063✔
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);
921,608,063✔
754
  pBlock->info.rows += pRow->numOfRows;
921,607,838✔
755
}
756

757
void doCopyToSDataBlockByHash(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
2,070,485,120✔
758
                              SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t threshold, bool ignoreGroup) {
759
  int32_t         code = TSDB_CODE_SUCCESS;
2,070,485,120✔
760
  int32_t         lino = 0;
2,070,485,120✔
761
  SExprInfo*      pExprInfo = pSup->pExprInfo;
2,070,485,120✔
762
  int32_t         numOfExprs = pSup->numOfExprs;
2,070,493,077✔
763
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
2,070,494,460✔
764
  SqlFunctionCtx* pCtx = pSup->pCtx;
2,070,494,710✔
765

766
  size_t  keyLen = 0;
2,070,495,820✔
767
  int32_t numOfRows = tSimpleHashGetSize(pHashmap);
2,070,496,705✔
768

769
  // begin from last iter
770
  void*   pData = pGroupResInfo->dataPos;
2,070,494,345✔
771
  int32_t iter = pGroupResInfo->iter;
2,070,494,718✔
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;
2,052,999,939✔
800
      } else {
801
        // current value belongs to different group, it can't be packed into one datablock
802
        if (pBlock->info.id.groupId != groupId) {
2,046,731,122✔
803
          releaseBufPage(pBuf, page);
2,046,730,280✔
804
          break;
2,046,733,845✔
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;
16,248✔
828
    }
829
  }
830

831
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
2,070,489,390✔
832
         pBlock->info.id.groupId);
833
  pBlock->info.dataLoad = 1;
2,070,421,463✔
834
  code = blockDataUpdateTsWindow(pBlock, 0);
2,070,494,596✔
835
  QUERY_CHECK_CODE(code, lino, _end);
2,070,494,302✔
836

837
_end:
2,070,494,302✔
838
  if (code != TSDB_CODE_SUCCESS) {
2,070,492,075✔
839
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
840
    T_LONG_JMP(pTaskInfo->env, code);
×
841
  }
842
}
2,070,494,302✔
843

844
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
119,225,675✔
845
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, STrueForInfo *pTrueForInfo) {
846
  int32_t         code = TSDB_CODE_SUCCESS;
119,225,675✔
847
  int32_t         lino = 0;
119,225,675✔
848
  SExprInfo*      pExprInfo = pSup->pExprInfo;
119,225,675✔
849
  int32_t         numOfExprs = pSup->numOfExprs;
119,226,927✔
850
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
119,228,133✔
851
  SqlFunctionCtx* pCtx = pSup->pCtx;
119,224,686✔
852

853
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
119,221,584✔
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;
119,180✔
870
      releaseBufPage(pBuf, page);
119,180✔
871
      continue;
119,180✔
872
    }
873
    // skip the window which is less than the windowMinSize
874
    if (!isTrueForSatisfied(pTrueForInfo, pRow->win.skey, pRow->win.ekey, pRow->nOrigRows)) {
2,147,483,647✔
875
      qDebug("skip small window, groupId: %" PRId64 ", skey: %" PRId64 ", ekey: %" PRId64 ", nrows: %u", pPos->groupId,
44,590✔
876
             pRow->win.skey, pRow->win.ekey, pRow->nOrigRows);
877
      pGroupResInfo->index += 1;
44,590✔
878
      releaseBufPage(pBuf, page);
44,590✔
879
      continue;
44,590✔
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;
2,147,483,647✔
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,943,168,640✔
888
          releaseBufPage(pBuf, page);
18,969,298✔
889
          break;
18,970,605✔
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;
16,147,539✔
911
    }
912
  }
913

914
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
119,173,697✔
915
         pBlock->info.id.groupId);
916
  pBlock->info.dataLoad = 1;
119,176,254✔
917
  code = blockDataUpdateTsWindow(pBlock, 0);
119,231,862✔
918
  QUERY_CHECK_CODE(code, lino, _end);
119,231,498✔
919

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

927
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
141,820,587✔
928
                            SDiskbasedBuf* pBuf) {
929
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
141,820,587✔
930
  SSDataBlock*   pBlock = pbInfo->pRes;
141,825,307✔
931

932
  // set output datablock version
933
  pBlock->info.version = pTaskInfo->version;
141,824,085✔
934

935
  blockDataCleanup(pBlock);
141,823,465✔
936
  if (!hasRemainResults(pGroupResInfo)) {
141,826,368✔
937
    return;
23,018,650✔
938
  }
939

940
  // clear the existed group id
941
  pBlock->info.id.groupId = 0;
118,806,909✔
942
  if (!pbInfo->mergeResultBlock) {
118,804,423✔
943
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
47,092,416✔
944
                       false, getTrueForInfo(pOperator));
945
  } else {
946
    while (hasRemainResults(pGroupResInfo)) {
133,562,554✔
947
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
71,712,374✔
948
                         true, getTrueForInfo(pOperator));
949
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
71,712,382✔
950
        break;
9,860,866✔
951
      }
952

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

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

962
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
411,072,082✔
963
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,681,294,132✔
964
    SExprInfo* pExprInfo = &pExpr[i];
1,270,231,212✔
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,465,192,809✔
967
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
1,223,080,267✔
968
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
242,179,598✔
969
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
145,968,660✔
970
      }
971
    }
972

973
    taosMemoryFree(pExprInfo->base.pParam);
1,270,312,671✔
974
    taosMemoryFree(pExprInfo->pExpr);
1,270,311,204✔
975
  }
976
}
411,062,920✔
977

978
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
292,327,532✔
979
  *defaultPgsz = 4096;
292,327,532✔
980
  uint32_t last = *defaultPgsz;
292,352,917✔
981
  while (*defaultPgsz < rowSize * 4) {
356,460,998✔
982
    *defaultPgsz <<= 1u;
64,159,568✔
983
    if (*defaultPgsz < last) {
64,129,128✔
984
      return TSDB_CODE_INVALID_PARA;
×
985
    }
986
    last = *defaultPgsz;
64,135,171✔
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;
292,269,422✔
993
  if ((*defaultBufsz) <= (*defaultPgsz)) {
292,317,863✔
994
    (*defaultBufsz) = (*defaultPgsz) * 4;
×
995
    if (*defaultBufsz < ((int64_t)(*defaultPgsz)) * 4) {
×
996
      return TSDB_CODE_INVALID_PARA;
×
997
    }
998
  }
999

1000
  return 0;
292,319,685✔
1001
}
1002

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

1008
  pResultInfo->capacity = numOfRows;
617,289,919✔
1009
  pResultInfo->threshold = numOfRows * 0.75;
617,420,595✔
1010

1011
  if (pResultInfo->threshold == 0) {
617,134,480✔
1012
    pResultInfo->threshold = numOfRows;
1,459,359✔
1013
  }
1014
  pResultInfo->totalRows = 0;
617,173,201✔
1015
}
617,193,665✔
1016

1017
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
275,435,555✔
1018
  pInfo->pRes = pBlock;
275,435,555✔
1019
  initResultRowInfo(&pInfo->resultRowInfo);
275,485,392✔
1020
}
275,446,342✔
1021

1022
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
1,173,622,401✔
1023
  if (pCtx == NULL) {
1,173,622,401✔
1024
    return;
736,581,618✔
1025
  }
1026

1027
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,692,637,505✔
1028
    if (pExpr != NULL) {
1,255,596,447✔
1029
      SExprInfo* pExprInfo = &pExpr[i];
1,255,557,965✔
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,450,060,472✔
1032
          colDataDestroy(pCtx[i].input.pData[j]);
144,171,780✔
1033
          taosMemoryFree(pCtx[i].input.pData[j]);
144,170,973✔
1034
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
144,166,481✔
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,450,071,657✔
1040
    }
1041

1042
    if(pCtx[i].fpSet.cleanup) {
1,255,657,407✔
1043
      pCtx[i].fpSet.cleanup(&pCtx[i]);
1,308,352✔
1044
    }
1045

1046
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,255,673,751✔
1047
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,255,693,531✔
1048
    taosMemoryFree(pCtx[i].input.pData);
1,255,701,643✔
1049
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,255,626,674✔
1050

1051
    if (pCtx[i].udfName != NULL) {
1,255,602,232✔
1052
      taosMemoryFree(pCtx[i].udfName);
35,862✔
1053
    }
1054
  }
1055

1056
  taosMemoryFreeClear(pCtx);
437,041,058✔
1057
  return;
437,014,205✔
1058
}
1059

1060
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
433,912,286✔
1061
  pSup->pExprInfo = pExprInfo;
433,912,286✔
1062
  pSup->numOfExprs = numOfExpr;
433,938,812✔
1063
  if (pSup->pExprInfo != NULL) {
433,941,026✔
1064
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
337,672,718✔
1065
    if (pSup->pCtx == NULL) {
337,713,281✔
1066
      return terrno;
13✔
1067
    }
1068
  }
1069

1070
  return TSDB_CODE_SUCCESS;
433,888,865✔
1071
}
1072

1073
void checkIndefRowsFuncs(SExprSupp* pSup) {
2,025✔
1074
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
8,100✔
1075
    if (fmIsIndefiniteRowsFunc(pSup->pCtx[i].functionId)) {
6,075✔
1076
      pSup->hasIndefRowsFunc = true;
×
1077
      break;
×
1078
    }
1079
  }
1080
}
2,025✔
1081

1082
void cleanupExprSupp(SExprSupp* pSupp) {
1,061,427,279✔
1083
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
1,061,427,279✔
1084
  if (pSupp->pExprInfo != NULL) {
1,061,383,699✔
1085
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
376,065,066✔
1086
    taosMemoryFreeClear(pSupp->pExprInfo);
376,069,087✔
1087
  }
1088

1089
  if (pSupp->pFilterInfo != NULL) {
1,061,372,056✔
1090
    filterFreeInfo(pSupp->pFilterInfo);
96,095,249✔
1091
    pSupp->pFilterInfo = NULL;
96,090,708✔
1092
  }
1093

1094
  taosMemoryFree(pSupp->rowEntryInfoOffset);
1,061,441,731✔
1095
  memset(pSupp, 0, sizeof(SExprSupp));
1,061,435,160✔
1096
}
1,061,435,160✔
1097

1098
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
111,978,676✔
1099
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
111,978,676✔
1100
  if (pSupp->pExprInfo != NULL) {
111,973,191✔
1101
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
32,900,897✔
1102
    taosMemoryFreeClear(pSupp->pExprInfo);
32,901,842✔
1103
  }
1104

1105
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
111,970,357✔
1106
  pSupp->numOfExprs = 0;
111,986,439✔
1107
  pSupp->hasWindowOrGroup = false;
111,987,588✔
1108
  pSupp->pCtx = NULL;
111,978,127✔
1109
}
111,965,952✔
1110

1111
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
279,372,356✔
1112
  blockDataDestroy(pInfo->pRes);
279,372,356✔
1113
  pInfo->pRes = NULL;
279,372,508✔
1114
}
279,373,688✔
1115

1116
bool groupbyTbname(SNodeList* pGroupList) {
246,176,120✔
1117
  bool   bytbname = false;
246,176,120✔
1118
  SNode* pNode = NULL;
246,176,120✔
1119
  FOREACH(pNode, pGroupList) {
254,128,152✔
1120
    if (pNode->type == QUERY_NODE_FUNCTION) {
39,109,797✔
1121
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
31,155,618✔
1122
      break;
31,155,643✔
1123
    }
1124
  }
1125
  return bytbname;
246,175,645✔
1126
}
1127

1128
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, SExecTaskInfo* pTask, SReadHandle* readHandle) {
348,106,140✔
1129
  switch (pNode->type) {
348,106,140✔
1130
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
317,026✔
1131
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
317,026✔
1132
      if (NULL == pInserterParam) {
317,026✔
1133
        return terrno;
×
1134
      }
1135
      pInserterParam->readHandle = readHandle;
317,026✔
1136

1137
      *pParam = pInserterParam;
317,026✔
1138
      break;
317,026✔
1139
    }
1140
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,746,617✔
1141
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,746,617✔
1142
      if (NULL == pDeleterParam) {
1,748,549✔
1143
        return terrno;
×
1144
      }
1145

1146
      SArray* pInfoList = NULL;
1,748,549✔
1147
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,749,109✔
1148
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,750,227✔
1149
        taosMemoryFree(pDeleterParam);
1,118✔
1150
        return code;
×
1151
      }
1152

1153
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
1,749,109✔
1154
      taosArrayDestroy(pInfoList);
1,749,669✔
1155

1156
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,748,549✔
1157

1158
      // TODO extract uid list
1159
      int32_t numOfTables = 0;
1,749,109✔
1160
      code = tableListGetSize(pTableListInfo, &numOfTables);
1,749,109✔
1161
      if (code != TSDB_CODE_SUCCESS) {
1,749,669✔
1162
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1163
        taosMemoryFree(pDeleterParam);
×
1164
        return code;
×
1165
      }
1166

1167
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
1,749,669✔
1168
      if (NULL == pDeleterParam->pUidList) {
1,749,109✔
1169
        taosMemoryFree(pDeleterParam);
×
1170
        return terrno;
×
1171
      }
1172

1173
      for (int32_t i = 0; i < numOfTables; ++i) {
3,735,994✔
1174
        STableKeyInfo* pTable = tableListGetInfo(pTableListInfo, i);
1,986,325✔
1175
        if (!pTable) {
1,986,325✔
1176
          taosArrayDestroy(pDeleterParam->pUidList);
×
1177
          taosMemoryFree(pDeleterParam);
×
1178
          return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1179
        }
1180
        void* tmp = taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
1,986,325✔
1181
        if (!tmp) {
1,986,325✔
1182
          taosArrayDestroy(pDeleterParam->pUidList);
×
1183
          taosMemoryFree(pDeleterParam);
×
1184
          return terrno;
×
1185
        }
1186
      }
1187

1188
      *pParam = pDeleterParam;
1,749,669✔
1189
      break;
1,750,227✔
1190
    }
1191
    default:
346,027,581✔
1192
      break;
346,027,581✔
1193
  }
1194

1195
  return TSDB_CODE_SUCCESS;
348,157,122✔
1196
}
1197

1198
void streamOpReleaseState(SOperatorInfo* pOperator) {
×
1199
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1200
  if (downstream->fpSet.releaseStreamStateFn) {
×
1201
    downstream->fpSet.releaseStreamStateFn(downstream);
×
1202
  }
1203
}
×
1204

1205
void streamOpReloadState(SOperatorInfo* pOperator) {
×
1206
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1207
  if (downstream->fpSet.reloadStreamStateFn) {
×
1208
    downstream->fpSet.reloadStreamStateFn(downstream);
×
1209
  }
1210
}
×
1211

1212
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
132,069,757✔
1213
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
132,069,757✔
1214
  for (int32_t i = 0; i < childrenNum; ++i) {
137,745,561✔
1215
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
5,685,768✔
1216
    freeOperatorParam(pChild, type);
5,685,768✔
1217
  }
1218

1219
  taosArrayDestroy(pParam->pChildren);
132,059,793✔
1220
  pParam->pChildren = NULL;
132,079,769✔
1221

1222
  taosMemoryFreeClear(pParam->value);
132,078,596✔
1223

1224
  taosMemoryFree(pParam);
132,064,851✔
1225
}
132,036,538✔
1226

1227
void freeExchangeGetBasicOperatorParam(void* pParam) {
24,091,194✔
1228
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
24,091,194✔
1229
  if (pBasic->uidList) {
24,091,194✔
1230
    taosArrayDestroy(pBasic->uidList);
19,948,733✔
1231
    pBasic->uidList = NULL;
19,948,733✔
1232
  }
1233
  if (pBasic->orgTbInfo) {
24,091,194✔
1234
    taosArrayDestroy(pBasic->orgTbInfo->colMap);
7,420,104✔
1235
    taosMemoryFreeClear(pBasic->orgTbInfo);
7,420,104✔
1236
  }
1237
  if (pBasic->batchOrgTbInfo) {
24,091,194✔
1238
    taosArrayDestroyEx(pBasic->batchOrgTbInfo, destroySOrgTbInfo);
6,327,188✔
1239
    pBasic->batchOrgTbInfo = NULL;
6,327,188✔
1240
  }
1241
  if (pBasic->tagList) {
24,091,194✔
1242
    taosArrayDestroyEx(pBasic->tagList, destroyTagVal);
2,767,346✔
1243
    pBasic->tagList = NULL;
2,767,346✔
1244
  }
1245
}
24,091,194✔
1246

1247
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
23,341,203✔
1248
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
23,341,203✔
1249
  if (pExcParam->multiParams) {
23,341,203✔
1250
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
5,843,028✔
1251
    tSimpleHashSetFreeFp(pExcBatch->pBatchs, freeExchangeGetBasicOperatorParam);
5,843,028✔
1252
    tSimpleHashCleanup(pExcBatch->pBatchs);
5,843,028✔
1253
  } else {
1254
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
17,498,175✔
1255
  }
1256

1257
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
23,341,203✔
1258
}
23,341,203✔
1259

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

1262
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
7,562,278✔
1263

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

1266
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
3,781,139✔
1267

1268
void freeMergeJoinNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1269

1270
void freeTagScanGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
7,270,100✔
1271

1272
void freeMergeGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
14,693,102✔
1273

1274
void freeDynQueryCtrlGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
×
1275

1276
void freeDynQueryCtrlNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1277

1278
void freeInterpFuncGetOperatorParam(SOperatorParam* pParam) {
×
1279
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
×
1280
}
×
1281

1282
void freeInterpFuncNotifyOperatorParam(SOperatorParam* pParam) {
×
1283
  freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM);
×
1284
}
×
1285

1286
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
69,260,378✔
1287
  STableScanOperatorParam* pTableScanParam =
69,260,378✔
1288
    (STableScanOperatorParam*)pParam->value;
1289
  taosArrayDestroy(pTableScanParam->pUidList);
69,274,077✔
1290
  if (pTableScanParam->pOrgTbInfo) {
69,263,623✔
1291
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
51,403,466✔
1292
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
51,403,466✔
1293
  }
1294
  if (pTableScanParam->pBatchTbInfo) {
69,268,930✔
1295
    for (int32_t i = 0;
6,277,838✔
1296
      i < taosArrayGetSize(pTableScanParam->pBatchTbInfo); ++i) {
17,006,949✔
1297
      SOrgTbInfo* pOrgTbInfo =
1298
        (SOrgTbInfo*)taosArrayGet(pTableScanParam->pBatchTbInfo, i);
10,729,111✔
1299
      taosArrayDestroy(pOrgTbInfo->colMap);
10,729,111✔
1300
    }
1301
    taosArrayDestroy(pTableScanParam->pBatchTbInfo);
6,277,838✔
1302
    pTableScanParam->pBatchTbInfo = NULL;
6,277,838✔
1303
  }
1304
  if (pTableScanParam->pTagList) {
69,270,404✔
1305
    for (int32_t i = 0;
2,767,346✔
1306
      i < taosArrayGetSize(pTableScanParam->pTagList); ++i) {
18,520,472✔
1307
      STagVal* pTagVal =
1308
        (STagVal*)taosArrayGet(pTableScanParam->pTagList, i);
15,753,126✔
1309
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
15,753,126✔
1310
        taosMemoryFreeClear(pTagVal->pData);
6,915,942✔
1311
      }
1312
    }
1313
    taosArrayDestroy(pTableScanParam->pTagList);
2,767,346✔
1314
    pTableScanParam->pTagList = NULL;
2,767,346✔
1315
  }
1316
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
69,272,587✔
1317
}
69,221,476✔
1318

1319
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1320

1321
void freeTagScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1322

1323
void freeMergeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1324

1325
void freeOpParamItem(void* pItem) {
12,629,312✔
1326
  SOperatorParam* pParam = *(SOperatorParam**)pItem;
12,629,312✔
1327
  pParam->reUse = false;
12,629,312✔
1328
  freeOperatorParam(pParam, OP_GET_PARAM);
12,629,312✔
1329
}
12,629,312✔
1330

1331
static void destroyRefColIdGroupParam(void* info) {
8,059✔
1332
  SRefColIdGroup* pGroup = (SRefColIdGroup*)info;
8,059✔
1333
  if (pGroup && pGroup->pSlotIdList) {
8,059✔
1334
    taosArrayDestroy(pGroup->pSlotIdList);
8,059✔
1335
    pGroup->pSlotIdList = NULL;
8,059✔
1336
  }
1337
}
8,059✔
1338

1339
void freeExternalWindowGetOperatorParam(SOperatorParam* pParam) {
951,981✔
1340
  SExternalWindowOperatorParam *pExtParam = (SExternalWindowOperatorParam*)pParam->value;
951,981✔
1341
  taosArrayDestroy(pExtParam->ExtWins);
951,981✔
1342
  for (int32_t i = 0; i < taosArrayGetSize(pParam->pChildren); i++) {
951,981✔
1343
    SOperatorParam* pChild = *(SOperatorParam**)taosArrayGet(pParam->pChildren, i);
×
1344
    pChild->reUse = false;
×
1345
  }
1346
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
951,981✔
1347
}
951,981✔
1348

1349
void freeVirtualTableScanGetOperatorParam(SOperatorParam* pParam) {
5,209,208✔
1350
  SVTableScanOperatorParam* pVTableScanParam = (SVTableScanOperatorParam*)pParam->value;
5,209,208✔
1351
  taosArrayDestroyEx(pVTableScanParam->pOpParamArray, freeOpParamItem);
5,209,208✔
1352
  if (pVTableScanParam->pRefColGroups) {
5,209,208✔
1353
    taosArrayDestroyEx(pVTableScanParam->pRefColGroups, destroyRefColIdGroupParam);
4,964✔
1354
    pVTableScanParam->pRefColGroups = NULL;
4,964✔
1355
  }
1356
  freeOpParamItem(&pVTableScanParam->pTagScanOp);
5,209,208✔
1357
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
5,209,208✔
1358
}
5,209,208✔
1359

1360
void freeVTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1361

1362
void freeExternalWindowNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1363

1364
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
764,984,039✔
1365
  if (NULL == pParam || pParam->reUse) {
764,984,039✔
1366
    return;
632,916,468✔
1367
  }
1368

1369
  switch (pParam->opType) {
132,082,252✔
1370
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
23,341,203✔
1371
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
23,341,203✔
1372
      break;
23,341,203✔
1373
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
7,562,278✔
1374
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
7,562,278✔
1375
      break;
7,562,278✔
1376
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
3,781,139✔
1377
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
3,781,139✔
1378
      break;
3,781,139✔
1379
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
69,272,855✔
1380
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
1381
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
69,272,855✔
1382
      break;
69,229,339✔
1383
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
5,209,208✔
1384
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
5,209,208✔
1385
      break;
5,209,208✔
1386
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
7,270,100✔
1387
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
7,270,100✔
1388
      break;
7,270,100✔
1389
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
14,693,462✔
1390
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
1391
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
1392
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL:
1393
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
1394
      type == OP_GET_PARAM ? freeMergeGetOperatorParam(pParam) : freeMergeNotifyOperatorParam(pParam);
14,693,462✔
1395
      break;
14,692,689✔
1396
    case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
951,981✔
1397
      type == OP_GET_PARAM ? freeExternalWindowGetOperatorParam(pParam) : freeExternalWindowNotifyOperatorParam(pParam);
951,981✔
1398
      break;
951,981✔
1399
    case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL:
×
1400
      type == OP_GET_PARAM ? freeDynQueryCtrlGetOperatorParam(pParam) : freeDynQueryCtrlNotifyOperatorParam(pParam);
×
1401
      break;
×
1402
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
×
1403
      type == OP_GET_PARAM ? freeInterpFuncGetOperatorParam(pParam) : freeInterpFuncNotifyOperatorParam(pParam);
×
1404
      break;
×
1405
    default:
10,466✔
1406
      qError("%s unsupported op %d param, param type %d, param:%p value:%p children:%p reuse:%d",
10,466✔
1407
             __func__, pParam->opType, type, pParam, pParam->value, pParam->pChildren, pParam->reUse);
1408
      break;
×
1409
  }
1410
}
1411

1412
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,647,419,066✔
1413
  SOperatorParam**  ppParam = NULL;
1,647,419,066✔
1414
  SOperatorParam*** pppDownstramParam = NULL;
1,647,419,066✔
1415
  switch (type) {
1,647,419,066✔
1416
    case OP_GET_PARAM:
873,026,869✔
1417
      ppParam = &pOperator->pOperatorGetParam;
873,026,869✔
1418
      pppDownstramParam = &pOperator->pDownstreamGetParams;
873,040,067✔
1419
      break;
873,030,345✔
1420
    case OP_NOTIFY_PARAM:
774,460,222✔
1421
      ppParam = &pOperator->pOperatorNotifyParam;
774,460,222✔
1422
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
774,454,493✔
1423
      break;
774,462,937✔
1424
    default:
149✔
1425
      return;
149✔
1426
  }
1427

1428
  if (*ppParam) {
1,647,493,282✔
1429
    qDebug("%s free self param, operator:%s type:%d paramType:%d param:%p", __func__, pOperator->name,
9,072,597✔
1430
           pOperator->operatorType, type, *ppParam);
1431
    freeOperatorParam(*ppParam, type);
9,072,597✔
1432
    *ppParam = NULL;
9,072,597✔
1433
  }
1434

1435
  if (*pppDownstramParam) {
1,647,444,103✔
1436
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
131,478,993✔
1437
      if ((*pppDownstramParam)[i]) {
29,884,988✔
1438
        qDebug("%s free downstream param, operator:%s type:%d idx:%d downstream:%s paramType:%d param:%p", __func__,
×
1439
               pOperator->name, pOperator->operatorType, i, pOperator->pDownstream[i]->name, type,
1440
               (*pppDownstramParam)[i]);
1441
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1442
        (*pppDownstramParam)[i] = NULL;
×
1443
      }
1444
    }
1445
    if (allFree) {
101,592,895✔
1446
      taosMemoryFreeClear(*pppDownstramParam);
24,547,513✔
1447
    }
1448
  }
1449
}
1450

1451
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
928,337,905✔
1452
                                                    SSDataBlock** pResBlock) {
1453
  QRY_PARAM_CHECK(pResBlock);
928,337,905✔
1454

1455
  int32_t code = 0;
928,418,242✔
1456
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
928,418,242✔
1457
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
18,650,535✔
1458
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
37,304,744✔
1459
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
18,652,375✔
1460
    if (clearParam && (code == 0)) {
18,652,375✔
1461
      qDebug("%s clear downstream param, operator:%s type:%d idx:%d downstream:%s param:%p", __func__,
7,160,901✔
1462
             pOperator->name, pOperator->operatorType, idx, pOperator->pDownstream[idx]->name,
1463
             pOperator->pDownstreamGetParams[idx]);
1464
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
7,160,901✔
1465
      pOperator->pDownstreamGetParams[idx] = NULL;
7,160,901✔
1466
    }
1467

1468
    if (code) {
18,652,375✔
1469
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
1470
    }
1471
    return code;
18,652,375✔
1472
  }
1473

1474
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
909,728,807✔
1475
  if (code) {
907,114,235✔
1476
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1477
  }
1478
  return code;
907,102,661✔
1479
}
1480

1481
bool compareVal(const char* v, const SStateKeys* pKey) {
2,147,483,647✔
1482
  if (IS_VAR_DATA_TYPE(pKey->type)) {
2,147,483,647✔
1483
    if (IS_STR_DATA_BLOB(pKey->type)) {
309,387✔
1484
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
1485
        return false;
×
1486
      } else {
1487
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
1488
      }
1489
    } else {
1490
      if (varDataLen(v) != varDataLen(pKey->pData)) {
309,387✔
1491
        return false;
×
1492
      } else {
1493
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
309,387✔
1494
      }
1495
    }
1496
  } else {
1497
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
2,147,483,647✔
1498
  }
1499
}
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