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

taosdata / TDengine / #5024

16 Apr 2026 10:31AM UTC coverage: 72.954% (+0.7%) from 72.251%
#5024

push

travis-ci

web-flow
merge: from main to 3.0 branch #35157

120 of 200 new or added lines in 14 files covered. (60.0%)

655 existing lines in 130 files now uncovered.

273150 of 374416 relevant lines covered (72.95%)

129604715.8 hits per line

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

77.95
/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 "../../function/inc/functionResInfoInt.h"
20
#include "os.h"
21
#include "querynodes.h"
22
#include "tfill.h"
23
#include "tname.h"
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

109
      pData = getNewBufPage(pResultBuf, &pageId);
820,098,001✔
110
      if (pData == NULL) {
820,109,487✔
111
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
112
        return NULL;
×
113
      }
114
      pData->num = sizeof(SFilePage);
820,109,487✔
115
    }
116
  }
117

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

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

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

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

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

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

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

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

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

178
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
2,147,483,647✔
UNCOV
179
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
180
        pTaskInfo->code = terrno;
×
181
        return NULL;
×
182
      }
183
    }
184
  }
185

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

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

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

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

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

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

230
//  query_range_start, query_range_end, window_duration, window_start, window_end
231
int32_t initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
9,949,158✔
232
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
9,949,158✔
233
  pColData->info.bytes = sizeof(int64_t);
9,950,140✔
234

235
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
9,947,927✔
236
  if (code != TSDB_CODE_SUCCESS) {
9,946,713✔
237
    return code;
×
238
  }
239
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
9,946,713✔
240
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
9,945,653✔
241

242
  int64_t interval = 0;
9,950,552✔
243
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
244
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
9,936,202✔
245
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
9,934,471✔
246

247
  interval = -1;
9,931,873✔
248
  colDataSetInt64(pColData, 5,  &interval);
249
  return TSDB_CODE_SUCCESS;
9,939,328✔
250
}
251

252
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
24,623,231✔
253
  int32_t         code = TSDB_CODE_SUCCESS;
24,623,231✔
254
  int32_t         lino = 0;
24,623,231✔
255
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
24,623,231✔
256
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
134,810,581✔
257
    pCtx[i].order = order;
110,160,931✔
258
    pCtx[i].input.numOfRows = pBlock->info.rows;
110,155,849✔
259
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
110,171,662✔
260
    QUERY_CHECK_CODE(code, lino, _end);
110,174,229✔
261
    pCtx[i].pSrcBlock = pBlock;
110,174,229✔
262
    pCtx[i].scanFlag = scanFlag;
110,178,528✔
263
  }
264

265
_end:
24,647,879✔
266
  if (code != TSDB_CODE_SUCCESS) {
24,647,879✔
267
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
268
  }
269
  return code;
24,636,373✔
270
}
271

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

281
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
301,573,637✔
282
                                             int32_t numOfRows) {
283
  int32_t          code = TSDB_CODE_SUCCESS;
301,573,637✔
284
  int32_t          lino = 0;
301,573,637✔
285
  SColumnInfoData* pColInfo = NULL;
301,573,637✔
286
  if (pInput->pData[paramIndex] == NULL) {
301,573,637✔
287
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
623,234✔
288
    QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
623,234✔
289

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

294
    pInput->pData[paramIndex] = pColInfo;
623,234✔
295
  } else {
296
    pColInfo = pInput->pData[paramIndex];
301,067,786✔
297
  }
298

299
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
301,676,768✔
300
  QUERY_CHECK_CODE(code, lino, _end);
301,663,298✔
301

302
  int8_t type = pFuncParam->param.nType;
301,663,298✔
303
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
301,662,195✔
304
    int64_t v = pFuncParam->param.i;
415,666✔
305
    for (int32_t i = 0; i < numOfRows; ++i) {
896,470,612✔
306
      colDataSetInt64(pColInfo, i, &v);
896,051,119✔
307
    }
308
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
301,246,529✔
309
    double v = pFuncParam->param.d;
×
310
    for (int32_t i = 0; i < numOfRows; ++i) {
×
311
      colDataSetDouble(pColInfo, i, &v);
×
312
    }
313
  } else if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_GEOMETRY) {
301,246,529✔
314
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
4,562✔
315
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
316

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

325
_end:
301,242,117✔
326
  if (code != TSDB_CODE_SUCCESS) {
301,661,610✔
327
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
328
  }
329
  return code;
301,629,255✔
330
}
331

332
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
2,147,483,647✔
333
                                   bool createDummyCol) {
334
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
335
  int32_t         lino = 0;
2,147,483,647✔
336
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
2,147,483,647✔
337

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

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

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

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

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

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

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

369
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == tsParamIdx)) {
2,147,483,647✔
370
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
2,147,483,647✔
371
        }
372
        if (hasPk && (j == pkParamIdx)) {
2,147,483,647✔
373
          pInput->pPrimaryKey = pInput->pData[j];
147,068,153✔
374
        }
375
        QUERY_CHECK_CONDITION((pInput->pData[j] != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
2,147,483,647✔
376
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
647,072,082✔
377
        // todo avoid case: top(k, 12), 12 is the value parameter.
378
        // sum(11), 11 is also the value parameter.
379
        bool needDummyCol = (createDummyCol && pOneExpr->base.numOfParams == 1);
460,739,372✔
380
        // For indefinite-rows functions (e.g. mavg, csum, diff), the first parameter
381
        // is the data-input column.  When that argument is a constant we must expand
382
        // it into a per-row column so the function implementation can iterate over it.
383
        if (!needDummyCol && j == 0 &&
460,730,783✔
384
            pOneExpr->pExpr->nodeType == QUERY_NODE_FUNCTION &&
46,020,051✔
385
            fmIsIndefiniteRowsFunc(pOneExpr->pExpr->_function.functionId)) {
14,132,172✔
386
          needDummyCol = true;
×
387
        }
388
        if (needDummyCol) {
460,727,067✔
389
          pInput->totalRows = pBlock->info.rows;
301,632,159✔
390
          pInput->numOfRows = pBlock->info.rows;
301,680,788✔
391
          pInput->startRowIndex = 0;
301,680,075✔
392
          pInput->blankFill = pBlock->info.blankFill;
301,679,462✔
393

394
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
301,667,211✔
395
          QUERY_CHECK_CODE(code, lino, _end);
303,652,353✔
396
        }
397
      }
398
    }
399
  }
400

401
_end:
2,147,483,647✔
402
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
403
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
404
  }
405
  return code;
2,147,483,647✔
406
}
407

408
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
2,147,483,647✔
409
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
2,147,483,647✔
410

411
  // in case of timestamp column, always generated results.
412
  int32_t functionId = pCtx->functionId;
2,147,483,647✔
413
  if (functionId == -1) {
2,147,483,647✔
414
    return false;
×
415
  }
416

417
  if (pCtx->scanFlag == PRE_SCAN) {
2,147,483,647✔
418
    return fmIsRepeatScanFunc(pCtx->functionId);
3,865,001✔
419
  }
420

421
  if (isRowEntryCompleted(pResInfo)) {
2,147,483,647✔
422
    return false;
×
423
  }
424

425
  return true;
2,147,483,647✔
426
}
427

428
static int32_t doCreateConstantValColumnSMAInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t type,
×
429
                                                int32_t paramIndex, int32_t numOfRows) {
430
  if (pInput->pData[paramIndex] == NULL) {
×
431
    pInput->pData[paramIndex] = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
432
    if (pInput->pData[paramIndex] == NULL) {
×
433
      return terrno;
×
434
    }
435

436
    // Set the correct column info (data type and bytes)
437
    pInput->pData[paramIndex]->info.type = type;
×
438
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
×
439
  }
440

441
  SColumnDataAgg* da = NULL;
×
442
  if (pInput->pColumnDataAgg[paramIndex] == NULL) {
×
443
    da = taosMemoryCalloc(1, sizeof(SColumnDataAgg));
×
444
    if (!da) {
×
445
      return terrno;
×
446
    }
447
    pInput->pColumnDataAgg[paramIndex] = da;
×
448
  } else {
449
    da = pInput->pColumnDataAgg[paramIndex];
×
450
  }
451

452
  if (type == TSDB_DATA_TYPE_BIGINT) {
×
453
    int64_t v = pFuncParam->param.i;
×
454
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .sum = v * numOfRows};
×
455
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
456
    double v = pFuncParam->param.d;
×
457
    *da = (SColumnDataAgg){.numOfNull = 0};
×
458

459
    *(double*)&da->min = v;
×
460
    *(double*)&da->max = v;
×
461
    *(double*)&da->sum = v * numOfRows;
×
462
  } else if (type == TSDB_DATA_TYPE_BOOL) {  // todo validate this data type
×
463
    bool v = pFuncParam->param.i;
×
464

465
    *da = (SColumnDataAgg){.numOfNull = 0};
×
466
    *(bool*)&da->min = 0;
×
467
    *(bool*)&da->max = v;
×
468
    *(bool*)&da->sum = v * numOfRows;
×
469
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
×
470
    // do nothing
471
  } else {
472
    qError("invalid constant type for sma info");
×
473
  }
474

475
  return TSDB_CODE_SUCCESS;
×
476
}
477

478
int32_t setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
110,141,222✔
479
  int32_t code = TSDB_CODE_SUCCESS;
110,141,222✔
480
  int32_t lino = 0;
110,141,222✔
481
  int32_t numOfRows = pBlock->info.rows;
110,141,222✔
482

483
  SInputColumnInfoData* pInput = &pCtx->input;
110,171,597✔
484
  pInput->numOfRows = numOfRows;
110,173,493✔
485
  pInput->totalRows = numOfRows;
110,158,351✔
486

487
  if (pBlock->pBlockAgg != NULL) {
110,182,999✔
488
    pInput->colDataSMAIsSet = true;
110,205,210✔
489

490
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
220,369,834✔
491
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
110,198,232✔
492

493
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
110,199,384✔
494
        int32_t slotId = pFuncParam->pCol->slotId;
110,196,250✔
495
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
110,193,834✔
496
        if (pInput->pColumnDataAgg[j]->colId == -1) {
110,197,639✔
497
          pInput->colDataSMAIsSet = false;
19,920✔
498
        }
499

500
        // Here we set the column info data since the data type for each column data is required, but
501
        // the data in the corresponding SColumnInfoData will not be used.
502
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
110,188,765✔
503
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
×
504
        code = doCreateConstantValColumnSMAInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
×
505
        QUERY_CHECK_CODE(code, lino, _end);
×
506
      }
507
    }
508
  } else {
509
    pInput->colDataSMAIsSet = false;
×
510
  }
511

512
_end:
110,079,806✔
513
  if (code != TSDB_CODE_SUCCESS) {
110,079,806✔
514
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
515
  }
516
  return code;
110,169,805✔
517
}
518

519
/////////////////////////////////////////////////////////////////////////////////////////////
520
STimeWindow getAlignQueryTimeWindow(const SInterval* pInterval, int64_t key) {
2,147,483,647✔
521
  STimeWindow win = {0};
2,147,483,647✔
522
  win.skey = taosTimeTruncate(key, pInterval);
2,147,483,647✔
523

524
  /*
525
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
526
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
527
   */
528
  win.ekey = taosTimeGetIntervalEnd(win.skey, pInterval);
2,147,483,647✔
529
  if (win.ekey < win.skey) {
2,147,483,647✔
530
    win.ekey = INT64_MAX;
×
531
  }
532

533
  return win;
2,147,483,647✔
534
}
535

536
int32_t setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput,
2,147,483,647✔
537
                            int32_t* rowEntryInfoOffset) {
538
  bool init = false;
2,147,483,647✔
539
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
540
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
2,147,483,647✔
541
    if (init) {
2,147,483,647✔
542
      continue;
2,147,483,647✔
543
    }
544

545
    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
2,147,483,647✔
546
    
547
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
2,147,483,647✔
548
      continue;
×
549
    }
550

551
    if (pCtx[i].isPseudoFunc) {
2,147,483,647✔
552
      continue;
2,147,483,647✔
553
    }
554

555
    if (!pResInfo->initialized) {
2,147,483,647✔
556
      if (pCtx[i].functionId != -1) {
2,147,483,647✔
557
        int32_t code = pCtx[i].fpSet.init(&pCtx[i], pResInfo);
2,147,483,647✔
558
        if (code != TSDB_CODE_SUCCESS && fmIsUserDefinedFunc(pCtx[i].functionId)) {
2,147,483,647✔
559
          pResInfo->initialized = false;
×
560
          qError("failed to initialize udf, funcId:%d error:%s", pCtx[i].functionId, tstrerror(code));
×
561
          return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
562
        } else if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
563
          qError("failed to initialize function context, funcId:%d error:%s", pCtx[i].functionId, tstrerror(code));
×
564
          return code;
×
565
        }
566
      } else {
567
        pResInfo->initialized = true;
2,147,483,647✔
568
      }
569
    } else {
570
      init = true;
2,147,483,647✔
571
    }
572
  }
573
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
574
}
575

576
void clearResultRowInitFlag(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
645,328,080✔
577
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
578
    SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
2,147,483,647✔
579
    if (pResInfo == NULL) {
2,147,483,647✔
580
      continue;
×
581
    }
582

583
    pResInfo->initialized = false;
2,147,483,647✔
584
    pResInfo->numOfRes = 0;
2,147,483,647✔
585
    pResInfo->isNullRes = 0;
2,147,483,647✔
586
    pResInfo->complete = false;
2,147,483,647✔
587
  }
588
}
645,328,080✔
589

590
int32_t doFilter(SSDataBlock* pBlock, SFilterInfo* pFilterInfo, SColMatchInfo* pColMatchInfo, SColumnInfoData** pRet) {
2,147,483,647✔
591
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
592
  int32_t lino = 0;
2,147,483,647✔
593
  if (pFilterInfo == NULL || pBlock->info.rows == 0) {
2,147,483,647✔
594
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
595
  }
596

597
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
189,281,624✔
598
  SColumnInfoData*   p = NULL;
189,274,601✔
599

600
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
189,251,439✔
601
  QUERY_CHECK_CODE(code, lino, _err);
189,264,496✔
602

603
  int32_t status = 0;
189,264,496✔
604
  code =
605
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
189,277,165✔
606
  QUERY_CHECK_CODE(code, lino, _err);
189,248,161✔
607

608
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
188,241,748✔
609
  QUERY_CHECK_CODE(code, lino, _err);
188,249,746✔
610

611
  if (pColMatchInfo != NULL) {
188,249,746✔
612
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
138,683,112✔
613
    for (int32_t i = 0; i < size; ++i) {
138,826,323✔
614
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
138,685,090✔
615
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
138,680,361✔
616
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
138,680,361✔
617
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
138,563,447✔
618
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
138,551,944✔
619
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
138,551,944✔
620
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
138,551,486✔
621
          QUERY_CHECK_CODE(code, lino, _err);
138,546,261✔
622
          break;
138,546,261✔
623
        }
624
      }
625
    }
626
  }
627
  code = blockDataCheck(pBlock);
188,254,128✔
628
  QUERY_CHECK_CODE(code, lino, _err);
188,264,447✔
629
_err:
189,270,860✔
630
  if (code != TSDB_CODE_SUCCESS) {
189,270,432✔
631
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1,006,413✔
632
  }
633
  colDataDestroy(p);
189,270,432✔
634
  taosMemoryFree(p);
189,267,813✔
635
  return code;
189,250,003✔
636
}
637

638
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
188,437,477✔
639
  int32_t code = TSDB_CODE_SUCCESS;
188,437,477✔
640
  int8_t* pIndicator = (int8_t*)p->pData;
188,437,477✔
641
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
188,459,227✔
642
    // here nothing needs to be done
643
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
117,850,718✔
644
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
48,822,907✔
645
    pBlock->info.rows = 0;
48,826,705✔
646
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
69,027,811✔
647
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
69,032,457✔
648
  } else {
649
    qError("unknown filter result type: %d", status);
×
650
  }
651
  return code;
188,443,785✔
652
}
653

654
void doUpdateNumOfRows(SqlFunctionCtx* pCtx, SResultRow* pRow, int32_t numOfExprs, const int32_t* rowEntryOffset) {
2,147,483,647✔
655
  bool returnNotNull = false;
2,147,483,647✔
656
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,147,483,647✔
657
    SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
2,147,483,647✔
658
    if (!isRowEntryInitialized(pResInfo)) {
2,147,483,647✔
659
      continue;
2,147,483,647✔
660
    } else {
661
    }
662

663
    if (pRow->numOfRows < pResInfo->numOfRes) {
2,147,483,647✔
664
      pRow->numOfRows = pResInfo->numOfRes;
2,147,483,647✔
665
    }
666

667
    if (pCtx[j].isNotNullFunc) {
2,147,483,647✔
668
      returnNotNull = true;
2,147,483,647✔
669
    }
670
  }
671
  // if all expr skips all blocks, e.g. all null inputs for max function, output one row in final result.
672
  //  except for first/last, which require not null output, output no rows
673
  if (pRow->numOfRows == 0 && !returnNotNull) {
2,147,483,647✔
674
    pRow->numOfRows = 1;
2,147,483,647✔
675
  }
676
}
2,147,483,647✔
677

678
int32_t copyResultrowToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SResultRow* pRow, SqlFunctionCtx* pCtx,
2,147,483,647✔
679
                                 SSDataBlock* pBlock, const int32_t* rowEntryOffset, SExecTaskInfo* pTaskInfo) {
680
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
681
  int32_t lino = 0;
2,147,483,647✔
682
  int32_t groupKeyIdx = 0;
2,147,483,647✔
683
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,147,483,647✔
684
    int32_t slotId = pExprInfo[j].base.resSchema.slotId;
2,147,483,647✔
685

686
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
2,147,483,647✔
687
    if (pCtx[j].fpSet.finalize) {
2,147,483,647✔
688
      if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0 ||
2,147,483,647✔
689
          strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_const_value") == 0) {
2,147,483,647✔
690
        // for groupkey along with functions that output multiple lines(e.g. Histogram)
691
        if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0 &&
2,147,483,647✔
692
            pCtx[j].resultInfo->numOfRes == 0 && pTaskInfo->pStreamRuntimeInfo != NULL) {
2,147,483,647✔
693
          SArray* pVals = pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPartColVals;
×
694
          if (pVals != NULL && groupKeyIdx < taosArrayGetSize(pVals)) {
×
695
            SStreamGroupValue* pValue = taosArrayGet(pVals, groupKeyIdx);
×
696
            if (pValue != NULL) {
×
697
              SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
×
698
              pInfo->hasResult = true;
×
699
              pInfo->isNull = pValue->isNull;
×
700
              if (!pValue->isNull) {
×
701
                if (IS_VAR_DATA_TYPE(pValue->data.type) || pValue->data.type == TSDB_DATA_TYPE_DECIMAL) {
×
702
                  if (pValue->data.pData != NULL && pValue->data.nData > 0) {
×
703
                    memcpy(pInfo->data, pValue->data.pData, pValue->data.nData);
×
704
                  }
705
                } else {
706
                  memcpy(pInfo->data, &pValue->data.val, pExprInfo[j].base.resSchema.bytes);
×
707
                }
708
              }
709
              pCtx[j].resultInfo->numOfRes = 1;
×
710
            }
711
          }
712
        }
713

714
        if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) {
2,147,483,647✔
715
          ++groupKeyIdx;
2,147,483,647✔
716
        }
717

718
        // need to match groupkey result for each output row of that function.
719
        if (pCtx[j].resultInfo->numOfRes != 0) {
2,147,483,647✔
720
          pCtx[j].resultInfo->numOfRes = pRow->numOfRows;
2,147,483,647✔
721
        }
722
      }
723

724
      code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
2,147,483,647✔
725
      if (TSDB_CODE_SUCCESS != code) {
2,147,483,647✔
726
        qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
727
        QUERY_CHECK_CODE(code, lino, _end);
3,186,697✔
728
      }
729
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
2,147,483,647✔
730
      // do nothing
731
    } else {
732
      // expand the result into multiple rows. E.g., _wstart, top(k, 20)
733
      // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
734
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
735
      QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
2,147,483,647✔
736
      char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
2,147,483,647✔
737
      for (int32_t k = 0; k < pRow->numOfRows; ++k) {        
2,147,483,647✔
738
        code = colDataSetValOrCover(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
2,147,483,647✔
739
        QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
740
      }
741
    }
742
  }
743

744
_end:
2,147,483,647✔
745
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
746
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
747
  }
748
  return code;
2,147,483,647✔
749
}
750

751
// todo refactor. SResultRow has direct pointer in miainfo
752
void finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
966,754,310✔
753
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
754
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
966,754,310✔
755
  if (page == NULL) {
966,759,118✔
756
    qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
757
    T_LONG_JMP(pTaskInfo->env, terrno);
×
758
  }
759

760
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);
966,759,118✔
761

762
  SqlFunctionCtx* pCtx = pSup->pCtx;
966,759,782✔
763
  SExprInfo*      pExprInfo = pSup->pExprInfo;
966,758,932✔
764
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
966,758,932✔
765

766
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
966,758,932✔
767
  if (pRow->numOfRows == 0) {
966,753,168✔
768
    releaseBufPage(pBuf, page);
×
769
    return;
×
770
  }
771

772
  int32_t size = pBlock->info.capacity;
966,752,265✔
773
  while (pBlock->info.rows + pRow->numOfRows > size) {
966,984,871✔
774
    size = size * 1.25;
231,756✔
775
  }
776

777
  int32_t code = blockDataEnsureCapacity(pBlock, size);
966,754,018✔
778
  if (TAOS_FAILED(code)) {
966,749,104✔
779
    releaseBufPage(pBuf, page);
×
780
    qError("%s ensure result data capacity failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
781
    T_LONG_JMP(pTaskInfo->env, code);
×
782
  }
783

784
  code = copyResultrowToDataBlock(pExprInfo, pSup->numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
966,749,104✔
785
  if (TAOS_FAILED(code)) {
966,747,458✔
786
    releaseBufPage(pBuf, page);
×
787
    qError("%s copy result row to datablock failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
788
    T_LONG_JMP(pTaskInfo->env, code);
×
789
  }
790

791
  releaseBufPage(pBuf, page);
966,747,458✔
792
  pBlock->info.rows += pRow->numOfRows;
966,738,905✔
793
}
794

795
void doCopyToSDataBlockByHash(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
2,147,483,647✔
796
                              SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t threshold, bool ignoreGroup) {
797
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
798
  int32_t         lino = 0;
2,147,483,647✔
799
  SExprInfo*      pExprInfo = pSup->pExprInfo;
2,147,483,647✔
800
  int32_t         numOfExprs = pSup->numOfExprs;
2,147,483,647✔
801
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
2,147,483,647✔
802
  SqlFunctionCtx* pCtx = pSup->pCtx;
2,147,483,647✔
803

804
  size_t  keyLen = 0;
2,147,483,647✔
805
  int32_t numOfRows = tSimpleHashGetSize(pHashmap);
2,147,483,647✔
806

807
  // begin from last iter
808
  void*   pData = pGroupResInfo->dataPos;
2,147,483,647✔
809
  int32_t iter = pGroupResInfo->iter;
2,147,483,647✔
810
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
2,147,483,647✔
811
    void*               key = tSimpleHashGetKey(pData, &keyLen);
2,147,483,647✔
812
    SResultRowPosition* pos = pData;
2,147,483,647✔
813
    uint64_t            groupId = calcGroupId((char*)key + sizeof(uint64_t), keyLen - sizeof(uint64_t));
2,147,483,647✔
814

815
    SFilePage* page = getBufPage(pBuf, pos->pageId);
2,147,483,647✔
816
    if (page == NULL) {
2,147,483,647✔
817
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
818
      T_LONG_JMP(pTaskInfo->env, terrno);
×
819
    }
820

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

823
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
824

825
    // no results, continue to check the next one
826
    if (pRow->numOfRows == 0) {
2,147,483,647✔
827
      pGroupResInfo->index += 1;
×
828
      pGroupResInfo->iter = iter;
×
829
      pGroupResInfo->dataPos = pData;
×
830

831
      releaseBufPage(pBuf, page);
×
832
      continue;
×
833
    }
834

835
    if (!ignoreGroup) {
2,147,483,647✔
836
      if (pBlock->info.id.groupId == 0) {
2,147,483,647✔
837
        pBlock->info.id.groupId = groupId;
2,147,483,647✔
838
      } else {
839
        // current value belongs to different group, it can't be packed into one datablock
840
        if (pBlock->info.id.groupId != groupId) {
2,146,869,925✔
841
          releaseBufPage(pBuf, page);
2,146,870,566✔
842
          break;
2,146,870,566✔
843
        }
844
      }
845
    }
846

847
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,147,483,647✔
848
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - iter) > 1 ? 1 : 0);
×
849
      code = blockDataEnsureCapacity(pBlock, newSize);
×
850
      QUERY_CHECK_CODE(code, lino, _end);
×
851
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
852
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
853
      // todo set the pOperator->resultInfo size
854
    }
855

856
    pGroupResInfo->index += 1;
2,147,483,647✔
857
    pGroupResInfo->iter = iter;
2,147,483,647✔
858
    pGroupResInfo->dataPos = pData;
2,147,483,647✔
859

860
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
2,147,483,647✔
861
    releaseBufPage(pBuf, page);
2,147,483,647✔
862
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
863
    pBlock->info.rows += pRow->numOfRows;
2,147,483,647✔
864
    if (pBlock->info.rows >= threshold) {
2,147,483,647✔
865
      break;
16,712✔
866
    }
867
  }
868

869
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
2,147,483,647✔
870
         pBlock->info.id.groupId);
871
  pBlock->info.dataLoad = 1;
2,147,483,647✔
872
  code = blockDataUpdateTsWindow(pBlock, 0);
2,147,483,647✔
873
  QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
874

875
_end:
2,147,483,647✔
876
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
877
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
878
    T_LONG_JMP(pTaskInfo->env, code);
×
879
  }
880
}
2,147,483,647✔
881

882
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
122,496,819✔
883
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, STrueForInfo *pTrueForInfo) {
884
  int32_t         code = TSDB_CODE_SUCCESS;
122,496,819✔
885
  int32_t         lino = 0;
122,496,819✔
886
  SExprInfo*      pExprInfo = pSup->pExprInfo;
122,496,819✔
887
  int32_t         numOfExprs = pSup->numOfExprs;
122,498,485✔
888
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
122,500,756✔
889
  SqlFunctionCtx* pCtx = pSup->pCtx;
122,498,551✔
890

891
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
122,500,982✔
892

893
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
2,147,483,647✔
894
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
2,147,483,647✔
895
    SFilePage*  page = getBufPage(pBuf, pPos->pos.pageId);
2,147,483,647✔
896
    if (page == NULL) {
2,147,483,647✔
897
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
898
      T_LONG_JMP(pTaskInfo->env, terrno);
×
899
    }
900

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

903
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
904

905
    // no results, continue to check the next one
906
    if (pRow->numOfRows == 0) {
2,147,483,647✔
907
      pGroupResInfo->index += 1;
130,759✔
908
      releaseBufPage(pBuf, page);
130,759✔
909
      continue;
130,759✔
910
    }
911
    // skip the window which is less than the windowMinSize
912
    if (!isTrueForSatisfied(pTrueForInfo, pRow->win.skey, pRow->win.ekey, pRow->nOrigRows)) {
2,147,483,647✔
913
      qDebug("skip small window, groupId: %" PRId64 ", skey: %" PRId64 ", ekey: %" PRId64 ", nrows: %u", pPos->groupId,
46,774✔
914
             pRow->win.skey, pRow->win.ekey, pRow->nOrigRows);
915
      pGroupResInfo->index += 1;
46,774✔
916
      releaseBufPage(pBuf, page);
46,774✔
917
      continue;
46,774✔
918
    }
919

920
    if (!ignoreGroup) {
2,147,483,647✔
921
      if (pBlock->info.id.groupId == 0) {
2,147,483,647✔
922
        pBlock->info.id.groupId = pPos->groupId;
2,147,483,647✔
923
      } else {
924
        // current value belongs to different group, it can't be packed into one datablock
925
        if (pBlock->info.id.groupId != pPos->groupId) {
1,986,499,512✔
926
          releaseBufPage(pBuf, page);
19,841,899✔
927
          break;
19,843,137✔
928
        }
929
      }
930
    }
931

932
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,147,483,647✔
933
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - i) > 1 ? 1 : 0);
×
934
      code = blockDataEnsureCapacity(pBlock, newSize);
×
935
      QUERY_CHECK_CODE(code, lino, _end);
×
936
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
937
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
938
      // todo set the pOperator->resultInfo size
939
    }
940

941
    pGroupResInfo->index += 1;
2,147,483,647✔
942
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
2,147,483,647✔
943
    releaseBufPage(pBuf, page);
2,147,483,647✔
944
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
945

946
    pBlock->info.rows += pRow->numOfRows;
2,147,483,647✔
947
    if (pBlock->info.rows >= threshold) {
2,147,483,647✔
948
      break;
16,654,132✔
949
    }
950
  }
951

952
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
122,895,835✔
953
         pBlock->info.id.groupId);
954
  pBlock->info.dataLoad = 1;
122,904,452✔
955
  code = blockDataUpdateTsWindow(pBlock, 0);
122,504,224✔
956
  QUERY_CHECK_CODE(code, lino, _end);
122,502,183✔
957

958
_end:
122,502,183✔
959
  if (code != TSDB_CODE_SUCCESS) {
122,502,183✔
960
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
961
    T_LONG_JMP(pTaskInfo->env, code);
×
962
  }
963
}
122,502,183✔
964

965
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
144,875,861✔
966
                            SDiskbasedBuf* pBuf) {
967
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
144,875,861✔
968
  SSDataBlock*   pBlock = pbInfo->pRes;
144,880,941✔
969

970
  // set output datablock version
971
  pBlock->info.version = pTaskInfo->version;
144,881,652✔
972

973
  blockDataCleanup(pBlock);
144,876,070✔
974
  if (!hasRemainResults(pGroupResInfo)) {
144,883,971✔
975
    return;
22,819,683✔
976
  }
977

978
  // clear the existed group id
979
  pBlock->info.id.groupId = 0;
122,056,817✔
980
  if (!pbInfo->mergeResultBlock) {
122,057,201✔
981
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
47,274,584✔
982
                       false, getTrueForInfo(pOperator));
983
  } else {
984
    while (hasRemainResults(pGroupResInfo)) {
139,453,575✔
985
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
74,779,145✔
986
                         true, getTrueForInfo(pOperator));
987
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
74,781,036✔
988
        break;
10,109,107✔
989
      }
990

991
      // clearing group id to continue to merge data that belong to different groups
992
      pBlock->info.id.groupId = 0;
64,672,377✔
993
    }
994

995
    // clear the group id info in SSDataBlock, since the client does not need it
996
    pBlock->info.id.groupId = 0;
74,781,289✔
997
  }
998
}
999

1000
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
433,119,091✔
1001
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,740,993,175✔
1002
    SExprInfo* pExprInfo = &pExpr[i];
1,307,876,205✔
1003
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,147,483,647✔
1004
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
1,499,483,689✔
1005
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
1,245,504,372✔
1006
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
253,898,756✔
1007
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
152,537,070✔
1008
      }
1009
    }
1010

1011
    taosMemoryFree(pExprInfo->base.pParam);
1,307,949,989✔
1012
    taosMemoryFree(pExprInfo->pExpr);
1,307,899,074✔
1013
  }
1014
}
433,116,970✔
1015

1016
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
307,630,027✔
1017
  *defaultPgsz = 4096;
307,630,027✔
1018
  uint32_t last = *defaultPgsz;
307,652,091✔
1019
  while (*defaultPgsz < rowSize * 4) {
372,144,663✔
1020
    *defaultPgsz <<= 1u;
64,520,265✔
1021
    if (*defaultPgsz < last) {
64,504,480✔
1022
      return TSDB_CODE_INVALID_PARA;
×
1023
    }
1024
    last = *defaultPgsz;
64,488,539✔
1025
  }
1026

1027
  // The default buffer for each operator in query is 10MB.
1028
  // at least four pages need to be in buffer
1029
  // TODO: make this variable to be configurable.
1030
  *defaultBufsz = 4096 * 2560;
307,576,042✔
1031
  if ((*defaultBufsz) <= (*defaultPgsz)) {
307,641,642✔
1032
    (*defaultBufsz) = (*defaultPgsz) * 4;
×
1033
    if (*defaultBufsz < ((int64_t)(*defaultPgsz)) * 4) {
×
1034
      return TSDB_CODE_INVALID_PARA;
×
1035
    }
1036
  }
1037

1038
  return 0;
307,617,404✔
1039
}
1040

1041
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
655,654,792✔
1042
  if (numOfRows == 0) {
655,654,792✔
1043
    numOfRows = 4096;
×
1044
  }
1045

1046
  pResultInfo->capacity = numOfRows;
655,654,792✔
1047
  pResultInfo->threshold = numOfRows * 0.75;
655,787,685✔
1048

1049
  if (pResultInfo->threshold == 0) {
655,657,662✔
1050
    pResultInfo->threshold = numOfRows;
2,251,023✔
1051
  }
1052
  pResultInfo->totalRows = 0;
655,705,335✔
1053
}
655,668,258✔
1054

1055
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
289,360,772✔
1056
  pInfo->pRes = pBlock;
289,360,772✔
1057
  initResultRowInfo(&pInfo->resultRowInfo);
289,415,868✔
1058
}
289,280,770✔
1059

1060
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
1,244,930,725✔
1061
  if (pCtx == NULL) {
1,244,930,725✔
1062
    return;
783,929,931✔
1063
  }
1064

1065
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,756,362,708✔
1066
    if (pExpr != NULL) {
1,295,408,165✔
1067
      SExprInfo* pExprInfo = &pExpr[i];
1,295,364,965✔
1068
      for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,147,483,647✔
1069
        if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
1,486,266,613✔
1070
          colDataDestroy(pCtx[i].input.pData[j]);
150,398,501✔
1071
          taosMemoryFree(pCtx[i].input.pData[j]);
150,401,903✔
1072
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
150,401,365✔
1073
        }
1074
      }
1075
    }
1076
    for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) {
2,147,483,647✔
1077
      taosVariantDestroy(&pCtx[i].param[j].param);
1,486,270,039✔
1078
    }
1079

1080
    if(pCtx[i].fpSet.cleanup) {
1,295,378,633✔
1081
      pCtx[i].fpSet.cleanup(&pCtx[i]);
907,727✔
1082
    }
1083

1084
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,295,430,362✔
1085
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,295,443,828✔
1086
    taosMemoryFree(pCtx[i].input.pData);
1,295,421,117✔
1087
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,295,426,596✔
1088

1089
    if (pCtx[i].udfName != NULL) {
1,295,383,603✔
1090
      taosMemoryFree(pCtx[i].udfName);
37,477✔
1091
    }
1092
  }
1093

1094
  taosMemoryFreeClear(pCtx);
460,954,543✔
1095
  return;
460,939,350✔
1096
}
1097

1098
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
455,005,658✔
1099
  pSup->pExprInfo = pExprInfo;
455,005,658✔
1100
  pSup->numOfExprs = numOfExpr;
455,040,228✔
1101
  if (pSup->pExprInfo != NULL) {
455,028,805✔
1102
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
354,036,320✔
1103
    if (pSup->pCtx == NULL) {
353,894,644✔
1104
      return terrno;
×
1105
    }
1106
  }
1107

1108
  return TSDB_CODE_SUCCESS;
454,957,144✔
1109
}
1110

1111
void checkIndefRowsFuncs(SExprSupp* pSup) {
8,711✔
1112
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
25,004✔
1113
    if (fmIsIndefiniteRowsFunc(pSup->pCtx[i].functionId)) {
16,293✔
1114
      pSup->hasIndefRowsFunc = true;
×
1115
      break;
×
1116
    }
1117
  }
1118
}
8,711✔
1119

1120
void cleanupExprSupp(SExprSupp* pSupp) {
1,125,570,481✔
1121
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
1,125,570,481✔
1122
  if (pSupp->pExprInfo != NULL) {
1,125,532,337✔
1123
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
394,828,878✔
1124
    taosMemoryFreeClear(pSupp->pExprInfo);
394,873,236✔
1125
  }
1126

1127
  if (pSupp->pFilterInfo != NULL) {
1,125,552,583✔
1128
    filterFreeInfo(pSupp->pFilterInfo);
101,061,513✔
1129
    pSupp->pFilterInfo = NULL;
101,059,071✔
1130
  }
1131

1132
  taosMemoryFree(pSupp->rowEntryInfoOffset);
1,125,540,442✔
1133
  memset(pSupp, 0, sizeof(SExprSupp));
1,125,537,613✔
1134
}
1,125,537,613✔
1135

1136
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
118,976,326✔
1137
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
118,976,326✔
1138
  if (pSupp->pExprInfo != NULL) {
118,964,804✔
1139
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
35,867,452✔
1140
    taosMemoryFreeClear(pSupp->pExprInfo);
35,869,404✔
1141
  }
1142

1143
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
118,962,308✔
1144
  pSupp->numOfExprs = 0;
118,970,417✔
1145
  pSupp->hasWindowOrGroup = false;
118,984,989✔
1146
  pSupp->pCtx = NULL;
118,974,486✔
1147
}
118,986,604✔
1148

1149
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
292,749,139✔
1150
  blockDataDestroy(pInfo->pRes);
292,749,139✔
1151
  pInfo->pRes = NULL;
292,758,509✔
1152
}
292,749,515✔
1153

1154
bool groupbyTbname(SNodeList* pGroupList) {
256,476,583✔
1155
  bool   bytbname = false;
256,476,583✔
1156
  SNode* pNode = NULL;
256,476,583✔
1157
  FOREACH(pNode, pGroupList) {
264,908,173✔
1158
    if (pNode->type == QUERY_NODE_FUNCTION) {
42,523,322✔
1159
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
34,090,427✔
1160
      break;
34,090,617✔
1161
    }
1162
  }
1163
  return bytbname;
256,471,463✔
1164
}
1165

1166
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, SExecTaskInfo* pTask, SReadHandle* readHandle) {
371,866,501✔
1167
  switch (pNode->type) {
371,866,501✔
1168
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
493,899✔
1169
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
493,899✔
1170
      if (NULL == pInserterParam) {
493,899✔
1171
        return terrno;
×
1172
      }
1173
      pInserterParam->readHandle = readHandle;
493,899✔
1174

1175
      *pParam = pInserterParam;
493,899✔
1176
      break;
493,899✔
1177
    }
1178
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,890,371✔
1179
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,890,371✔
1180
      if (NULL == pDeleterParam) {
1,845,943✔
1181
        return terrno;
×
1182
      }
1183

1184
      SArray* pInfoList = NULL;
1,845,943✔
1185
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,845,943✔
1186
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,844,168✔
1187
        taosMemoryFree(pDeleterParam);
×
1188
        return code;
×
1189
      }
1190

1191
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
1,844,761✔
1192
      taosArrayDestroy(pInfoList);
1,844,761✔
1193

1194
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,845,350✔
1195

1196
      // TODO extract uid list
1197
      int32_t numOfTables = 0;
1,845,943✔
1198
      code = tableListGetSize(pTableListInfo, &numOfTables);
1,845,943✔
1199
      if (code != TSDB_CODE_SUCCESS) {
1,844,172✔
1200
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1201
        taosMemoryFree(pDeleterParam);
×
1202
        return code;
×
1203
      }
1204

1205
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
1,844,172✔
1206
      if (NULL == pDeleterParam->pUidList) {
1,845,350✔
1207
        taosMemoryFree(pDeleterParam);
×
1208
        return terrno;
×
1209
      }
1210

1211
      for (int32_t i = 0; i < numOfTables; ++i) {
3,939,966✔
1212
        STableKeyInfo* pTable = tableListGetInfo(pTableListInfo, i);
2,094,616✔
1213
        if (!pTable) {
2,094,616✔
1214
          taosArrayDestroy(pDeleterParam->pUidList);
×
1215
          taosMemoryFree(pDeleterParam);
×
1216
          return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1217
        }
1218
        void* tmp = taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
2,094,616✔
1219
        if (!tmp) {
2,094,616✔
1220
          taosArrayDestroy(pDeleterParam->pUidList);
×
1221
          taosMemoryFree(pDeleterParam);
×
1222
          return terrno;
×
1223
        }
1224
      }
1225

1226
      *pParam = pDeleterParam;
1,845,350✔
1227
      break;
1,845,943✔
1228
    }
1229
    default:
369,519,445✔
1230
      break;
369,519,445✔
1231
  }
1232

1233
  return TSDB_CODE_SUCCESS;
371,893,826✔
1234
}
1235

1236
void streamOpReleaseState(SOperatorInfo* pOperator) {
×
1237
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1238
  if (downstream->fpSet.releaseStreamStateFn) {
×
1239
    downstream->fpSet.releaseStreamStateFn(downstream);
×
1240
  }
1241
}
×
1242

1243
void streamOpReloadState(SOperatorInfo* pOperator) {
×
1244
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1245
  if (downstream->fpSet.reloadStreamStateFn) {
×
1246
    downstream->fpSet.reloadStreamStateFn(downstream);
×
1247
  }
1248
}
×
1249

1250
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
136,616,414✔
1251
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
136,616,414✔
1252
  for (int32_t i = 0; i < childrenNum; ++i) {
138,398,291✔
1253
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
1,776,015✔
1254
    freeOperatorParam(pChild, type);
1,776,015✔
1255
  }
1256

1257
  taosArrayDestroy(pParam->pChildren);
136,622,276✔
1258
  pParam->pChildren = NULL;
136,623,085✔
1259

1260
  taosMemoryFreeClear(pParam->value);
136,624,345✔
1261

1262
  taosMemoryFree(pParam);
136,622,722✔
1263
}
136,612,761✔
1264

1265
void freeExchangeGetBasicOperatorParam(void* pParam) {
24,384,785✔
1266
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
24,384,785✔
1267
  if (pBasic->uidList) {
24,384,785✔
1268
    taosArrayDestroy(pBasic->uidList);
20,009,169✔
1269
    pBasic->uidList = NULL;
20,009,169✔
1270
  }
1271
  if (pBasic->orgTbInfo) {
24,384,785✔
1272
    taosArrayDestroy(pBasic->orgTbInfo->colMap);
11,052,419✔
1273
    taosMemoryFreeClear(pBasic->orgTbInfo);
11,052,419✔
1274
  }
1275
  if (pBasic->batchOrgTbInfo) {
24,384,785✔
1276
    taosArrayDestroyEx(pBasic->batchOrgTbInfo, destroySOrgTbInfo);
3,224,236✔
1277
    pBasic->batchOrgTbInfo = NULL;
3,224,236✔
1278
  }
1279
  if (pBasic->tagList) {
24,384,785✔
1280
    taosArrayDestroyEx(pBasic->tagList, destroyTagVal);
856,136✔
1281
    pBasic->tagList = NULL;
856,136✔
1282
  }
1283
}
24,384,785✔
1284

1285
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
23,191,797✔
1286
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
23,191,797✔
1287
  if (pExcParam->multiParams) {
23,191,797✔
1288
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
2,286,169✔
1289
    tSimpleHashSetFreeFp(pExcBatch->pBatchs, freeExchangeGetBasicOperatorParam);
2,286,169✔
1290
    tSimpleHashCleanup(pExcBatch->pBatchs);
2,286,169✔
1291
  } else {
1292
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
20,905,628✔
1293
  }
1294

1295
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
23,191,797✔
1296
}
23,191,797✔
1297

1298
void freeExchangeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1299

1300
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
7,925,464✔
1301

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

1304
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
3,962,732✔
1305

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

1308
void freeTagScanGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
6,153,136✔
1309

1310
void freeMergeGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
4,855,879✔
1311

1312
void freeDynQueryCtrlGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
×
1313

1314
void freeDynQueryCtrlNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1315

1316
void freeInterpFuncGetOperatorParam(SOperatorParam* pParam) {
×
1317
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
×
1318
}
×
1319

1320
void freeInterpFuncNotifyOperatorParam(SOperatorParam* pParam) {
×
1321
  freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM);
×
1322
}
×
1323

1324
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
84,814,175✔
1325
  STableScanOperatorParam* pTableScanParam =
84,814,175✔
1326
    (STableScanOperatorParam*)pParam->value;
1327
  taosArrayDestroy(pTableScanParam->pUidList);
84,818,638✔
1328
  if (pTableScanParam->pOrgTbInfo) {
84,825,646✔
1329
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
74,027,778✔
1330
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
74,027,778✔
1331
  }
1332
  if (pTableScanParam->pBatchTbInfo) {
84,825,592✔
1333
    for (int32_t i = 0;
2,648,702✔
1334
      i < taosArrayGetSize(pTableScanParam->pBatchTbInfo); ++i) {
7,653,296✔
1335
      SOrgTbInfo* pOrgTbInfo =
1336
        (SOrgTbInfo*)taosArrayGet(pTableScanParam->pBatchTbInfo, i);
5,004,594✔
1337
      taosArrayDestroy(pOrgTbInfo->colMap);
5,004,594✔
1338
    }
1339
    taosArrayDestroy(pTableScanParam->pBatchTbInfo);
2,648,702✔
1340
    pTableScanParam->pBatchTbInfo = NULL;
2,648,702✔
1341
  }
1342
  if (pTableScanParam->pTagList) {
84,829,367✔
1343
    for (int32_t i = 0;
856,136✔
1344
      i < taosArrayGetSize(pTableScanParam->pTagList); ++i) {
5,305,008✔
1345
      STagVal* pTagVal =
1346
        (STagVal*)taosArrayGet(pTableScanParam->pTagList, i);
4,448,872✔
1347
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
4,448,872✔
1348
        taosMemoryFreeClear(pTagVal->pData);
1,931,496✔
1349
      }
1350
    }
1351
    taosArrayDestroy(pTableScanParam->pTagList);
856,136✔
1352
    pTableScanParam->pTagList = NULL;
856,136✔
1353
  }
1354
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
84,831,207✔
1355
}
84,812,833✔
1356

1357
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1358

1359
void freeTagScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1360

1361
void freeMergeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1362

1363
void freeOpParamItem(void* pItem) {
15,764,004✔
1364
  SOperatorParam* pParam = *(SOperatorParam**)pItem;
15,764,004✔
1365
  pParam->reUse = false;
15,764,004✔
1366
  freeOperatorParam(pParam, OP_GET_PARAM);
15,764,004✔
1367
}
15,764,004✔
1368

1369
static void destroyRefColIdGroupParam(void* info) {
8,497✔
1370
  SRefColIdGroup* pGroup = (SRefColIdGroup*)info;
8,497✔
1371
  if (pGroup && pGroup->pSlotIdList) {
8,497✔
1372
    taosArrayDestroy(pGroup->pSlotIdList);
8,497✔
1373
    pGroup->pSlotIdList = NULL;
8,497✔
1374
  }
1375
}
8,497✔
1376

1377
void freeExternalWindowGetOperatorParam(SOperatorParam* pParam) {
998,963✔
1378
  SExternalWindowOperatorParam *pExtParam = (SExternalWindowOperatorParam*)pParam->value;
998,963✔
1379
  taosArrayDestroy(pExtParam->ExtWins);
998,963✔
1380
  for (int32_t i = 0; i < taosArrayGetSize(pParam->pChildren); i++) {
998,963✔
1381
    SOperatorParam* pChild = *(SOperatorParam**)taosArrayGet(pParam->pChildren, i);
×
1382
    pChild->reUse = false;
×
1383
  }
1384
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
998,963✔
1385
}
998,963✔
1386

1387
void freeVirtualTableScanGetOperatorParam(SOperatorParam* pParam) {
4,711,585✔
1388
  SVTableScanOperatorParam* pVTableScanParam = (SVTableScanOperatorParam*)pParam->value;
4,711,585✔
1389
  taosArrayDestroyEx(pVTableScanParam->pOpParamArray, freeOpParamItem);
4,711,585✔
1390
  if (pVTableScanParam->pRefColGroups) {
4,711,585✔
1391
    taosArrayDestroyEx(pVTableScanParam->pRefColGroups, destroyRefColIdGroupParam);
5,232✔
1392
    pVTableScanParam->pRefColGroups = NULL;
5,232✔
1393
  }
1394
  freeOpParamItem(&pVTableScanParam->pTagScanOp);
4,711,585✔
1395
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
4,711,585✔
1396
}
4,711,585✔
1397

1398
void freeVTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1399

1400
void freeExternalWindowNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1401

1402
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
817,611,906✔
1403
  if (NULL == pParam || pParam->reUse) {
817,611,906✔
1404
    return;
680,993,662✔
1405
  }
1406

1407
  switch (pParam->opType) {
136,622,008✔
1408
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
23,191,797✔
1409
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
23,191,797✔
1410
      break;
23,191,797✔
1411
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
7,925,464✔
1412
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
7,925,464✔
1413
      break;
7,925,464✔
1414
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
3,962,732✔
1415
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
3,962,732✔
1416
      break;
3,962,732✔
1417
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
84,826,235✔
1418
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
1419
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
84,826,235✔
1420
      break;
84,817,788✔
1421
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
4,711,585✔
1422
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
4,711,585✔
1423
      break;
4,711,585✔
1424
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
6,153,136✔
1425
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
6,153,136✔
1426
      break;
6,153,136✔
1427
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
4,855,879✔
1428
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
1429
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
1430
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL:
1431
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
1432
      type == OP_GET_PARAM ? freeMergeGetOperatorParam(pParam) : freeMergeNotifyOperatorParam(pParam);
4,855,879✔
1433
      break;
4,853,983✔
1434
    case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
998,963✔
1435
      type == OP_GET_PARAM ? freeExternalWindowGetOperatorParam(pParam) : freeExternalWindowNotifyOperatorParam(pParam);
998,963✔
1436
      break;
998,963✔
1437
    case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL:
×
1438
      type == OP_GET_PARAM ? freeDynQueryCtrlGetOperatorParam(pParam) : freeDynQueryCtrlNotifyOperatorParam(pParam);
×
1439
      break;
×
1440
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
×
1441
      type == OP_GET_PARAM ? freeInterpFuncGetOperatorParam(pParam) : freeInterpFuncNotifyOperatorParam(pParam);
×
1442
      break;
×
1443
    default:
4,909✔
1444
      qError("%s unsupported op %d param, param type %d, param:%p value:%p children:%p reuse:%d",
4,909✔
1445
             __func__, pParam->opType, type, pParam, pParam->value, pParam->pChildren, pParam->reUse);
1446
      break;
×
1447
  }
1448
}
1449

1450
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,752,436,675✔
1451
  SOperatorParam**  ppParam = NULL;
1,752,436,675✔
1452
  SOperatorParam*** pppDownstramParam = NULL;
1,752,436,675✔
1453
  switch (type) {
1,752,436,675✔
1454
    case OP_GET_PARAM:
929,725,483✔
1455
      ppParam = &pOperator->pOperatorGetParam;
929,725,483✔
1456
      pppDownstramParam = &pOperator->pDownstreamGetParams;
929,732,583✔
1457
      break;
929,751,231✔
1458
    case OP_NOTIFY_PARAM:
822,798,740✔
1459
      ppParam = &pOperator->pOperatorNotifyParam;
822,798,740✔
1460
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
822,796,662✔
1461
      break;
822,799,600✔
1462
    default:
×
1463
      return;
×
1464
  }
1465

1466
  if (*ppParam) {
1,752,550,831✔
1467
    qDebug("%s free self param, operator:%s type:%d paramType:%d param:%p", __func__, pOperator->name,
8,760,621✔
1468
           pOperator->operatorType, type, *ppParam);
1469
    freeOperatorParam(*ppParam, type);
8,760,621✔
1470
    *ppParam = NULL;
8,760,621✔
1471
  }
1472

1473
  if (*pppDownstramParam) {
1,752,434,622✔
1474
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
133,701,052✔
1475
      if ((*pppDownstramParam)[i]) {
23,624,877✔
1476
        qDebug("%s free downstream param, operator:%s type:%d idx:%d downstream:%s paramType:%d param:%p", __func__,
×
1477
               pOperator->name, pOperator->operatorType, i, pOperator->pDownstream[i]->name, type,
1478
               (*pppDownstramParam)[i]);
1479
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1480
        (*pppDownstramParam)[i] = NULL;
×
1481
      }
1482
    }
1483
    if (allFree) {
110,074,952✔
1484
      taosMemoryFreeClear(*pppDownstramParam);
21,632,676✔
1485
    }
1486
  }
1487
}
1488

1489
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
1,106,710,085✔
1490
                                                    SSDataBlock** pResBlock) {
1491
  QRY_PARAM_CHECK(pResBlock);
1,106,710,085✔
1492

1493
  int32_t code = 0;
1,106,815,269✔
1494
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
1,106,815,269✔
1495
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
19,573,950✔
1496
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
39,147,900✔
1497
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
19,573,950✔
1498
    if (clearParam && (code == 0)) {
19,573,515✔
1499
      qDebug("%s clear downstream param, operator:%s type:%d idx:%d downstream:%s param:%p", __func__,
7,524,522✔
1500
             pOperator->name, pOperator->operatorType, idx, pOperator->pDownstream[idx]->name,
1501
             pOperator->pDownstreamGetParams[idx]);
1502
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
7,524,957✔
1503
      pOperator->pDownstreamGetParams[idx] = NULL;
7,524,522✔
1504
    }
1505

1506
    if (code) {
19,573,515✔
1507
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
1508
    }
1509
    return code;
19,573,950✔
1510
  }
1511

1512
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
1,087,261,456✔
1513
  if (code) {
1,084,146,486✔
1514
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1515
  }
1516
  return code;
1,084,159,384✔
1517
}
1518

1519
bool compareVal(const char* v, const SStateKeys* pKey) {
2,147,483,647✔
1520
  if (IS_VAR_DATA_TYPE(pKey->type)) {
2,147,483,647✔
1521
    if (IS_STR_DATA_BLOB(pKey->type)) {
323,220✔
1522
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
1523
        return false;
×
1524
      } else {
1525
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
1526
      }
1527
    } else {
1528
      if (varDataLen(v) != varDataLen(pKey->pData)) {
323,220✔
1529
        return false;
×
1530
      } else {
1531
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
323,220✔
1532
      }
1533
    }
1534
  } else {
1535
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
2,147,483,647✔
1536
  }
1537
}
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