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

taosdata / TDengine / #5029

21 Apr 2026 10:00AM UTC coverage: 73.003% (+0.02%) from 72.986%
#5029

push

travis-ci

web-flow
fix(tmq): balance vgroup error (#35183)

273843 of 375111 relevant lines covered (73.0%)

134074995.07 hits per line

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

77.55
/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);
270,505,125✔
91
    if (pData == NULL) {
270,543,069✔
92
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
93
      return NULL;
×
94
    }
95
    pData->num = sizeof(SFilePage);
270,543,069✔
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);
877,789,789✔
108

109
      pData = getNewBufPage(pResultBuf, &pageId);
877,659,044✔
110
      if (pData == NULL) {
877,768,916✔
111
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
112
        return NULL;
×
113
      }
114
      pData->num = sizeof(SFilePage);
877,768,916✔
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,701,707,724✔
156
      if (pResult == NULL) {
1,701,707,724✔
157
        pTaskInfo->code = terrno;
×
158
        return NULL;
×
159
      }
160

161
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
1,701,707,724✔
162
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
25,691✔
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✔
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) {
10,444,787✔
232
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
10,444,787✔
233
  pColData->info.bytes = sizeof(int64_t);
10,449,427✔
234

235
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
10,444,576✔
236
  if (code != TSDB_CODE_SUCCESS) {
10,446,561✔
237
    return code;
×
238
  }
239
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
10,446,561✔
240
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
10,440,075✔
241

242
  int64_t interval = 0;
10,446,808✔
243
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
244
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
10,444,714✔
245
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
10,440,556✔
246

247
  interval = -1;
10,434,882✔
248
  colDataSetInt64(pColData, 5,  &interval);
249
  return TSDB_CODE_SUCCESS;
10,438,863✔
250
}
251

252
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
23,951,301✔
253
  int32_t         code = TSDB_CODE_SUCCESS;
23,951,301✔
254
  int32_t         lino = 0;
23,951,301✔
255
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
23,951,301✔
256
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
130,331,652✔
257
    pCtx[i].order = order;
106,364,476✔
258
    pCtx[i].input.numOfRows = pBlock->info.rows;
106,363,206✔
259
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
106,372,096✔
260
    QUERY_CHECK_CODE(code, lino, _end);
106,377,196✔
261
    pCtx[i].pSrcBlock = pBlock;
106,377,196✔
262
    pCtx[i].scanFlag = scanFlag;
106,377,831✔
263
  }
264

265
_end:
23,962,741✔
266
  if (code != TSDB_CODE_SUCCESS) {
23,962,741✔
267
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
268
  }
269
  return code;
23,961,471✔
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);
23,957,016✔
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,
302,276,156✔
282
                                             int32_t numOfRows) {
283
  int32_t          code = TSDB_CODE_SUCCESS;
302,276,156✔
284
  int32_t          lino = 0;
302,276,156✔
285
  SColumnInfoData* pColInfo = NULL;
302,276,156✔
286
  if (pInput->pData[paramIndex] == NULL) {
302,276,156✔
287
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
649,113✔
288
    QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
648,866✔
289

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

294
    pInput->pData[paramIndex] = pColInfo;
648,866✔
295
  } else {
296
    pColInfo = pInput->pData[paramIndex];
301,663,776✔
297
  }
298

299
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
302,303,576✔
300
  QUERY_CHECK_CODE(code, lino, _end);
302,276,927✔
301

302
  int8_t type = pFuncParam->param.nType;
302,276,927✔
303
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
302,290,649✔
304
    int64_t v = pFuncParam->param.i;
443,005✔
305
    for (int32_t i = 0; i < numOfRows; ++i) {
989,559,364✔
306
      colDataSetInt64(pColInfo, i, &v);
989,108,889✔
307
    }
308
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
301,847,644✔
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,847,644✔
314
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
4,382✔
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,843,262✔
326
  if (code != TSDB_CODE_SUCCESS) {
302,293,737✔
327
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
328
  }
329
  return code;
302,287,667✔
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];
29,954,909✔
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) {
654,825,470✔
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);
463,532,503✔
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 &&
463,542,290✔
384
            pOneExpr->pExpr->nodeType == QUERY_NODE_FUNCTION &&
47,215,521✔
385
            fmIsIndefiniteRowsFunc(pOneExpr->pExpr->_function.functionId)) {
14,476,196✔
386
          needDummyCol = true;
×
387
        }
388
        if (needDummyCol) {
463,541,369✔
389
          pInput->totalRows = pBlock->info.rows;
302,297,596✔
390
          pInput->numOfRows = pBlock->info.rows;
302,298,091✔
391
          pInput->startRowIndex = 0;
302,306,086✔
392
          pInput->blankFill = pBlock->info.blankFill;
302,302,467✔
393

394
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
302,301,479✔
395
          QUERY_CHECK_CODE(code, lino, _end);
308,783,972✔
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);
4,086,540✔
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) {
106,340,216✔
479
  int32_t code = TSDB_CODE_SUCCESS;
106,340,216✔
480
  int32_t lino = 0;
106,340,216✔
481
  int32_t numOfRows = pBlock->info.rows;
106,340,216✔
482

483
  SInputColumnInfoData* pInput = &pCtx->input;
106,362,521✔
484
  pInput->numOfRows = numOfRows;
106,363,781✔
485
  pInput->totalRows = numOfRows;
106,375,271✔
486

487
  if (pBlock->pBlockAgg != NULL) {
106,379,736✔
488
    pInput->colDataSMAIsSet = true;
106,405,861✔
489

490
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
212,779,207✔
491
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
106,401,376✔
492

493
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
105,994,341✔
494
        int32_t slotId = pFuncParam->pCol->slotId;
106,393,776✔
495
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
106,389,321✔
496
        if (pInput->pColumnDataAgg[j]->colId == -1) {
106,388,071✔
497
          pInput->colDataSMAIsSet = false;
20,126✔
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);
106,389,956✔
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:
106,295,536✔
513
  if (code != TSDB_CODE_SUCCESS) {
106,295,536✔
514
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
515
  }
516
  return code;
106,376,561✔
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) {
858,921,396✔
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
}
858,921,396✔
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};
184,513,567✔
598
  SColumnInfoData*   p = NULL;
184,501,685✔
599

600
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
184,493,614✔
601
  QUERY_CHECK_CODE(code, lino, _err);
184,501,058✔
602

603
  int32_t status = 0;
184,501,058✔
604
  code =
605
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
184,505,804✔
606
  QUERY_CHECK_CODE(code, lino, _err);
184,499,268✔
607

608
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
183,454,792✔
609
  QUERY_CHECK_CODE(code, lino, _err);
183,458,515✔
610

611
  if (pColMatchInfo != NULL) {
183,458,515✔
612
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
133,243,433✔
613
    for (int32_t i = 0; i < size; ++i) {
133,382,301✔
614
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
133,240,474✔
615
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
133,234,911✔
616
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
133,234,911✔
617
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
133,115,657✔
618
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
133,107,938✔
619
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
133,107,938✔
620
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
133,106,111✔
621
          QUERY_CHECK_CODE(code, lino, _err);
133,101,393✔
622
          break;
133,101,393✔
623
        }
624
      }
625
    }
626
  }
627
  code = blockDataCheck(pBlock);
183,458,302✔
628
  QUERY_CHECK_CODE(code, lino, _err);
183,460,048✔
629
_err:
184,504,524✔
630
  if (code != TSDB_CODE_SUCCESS) {
184,505,279✔
631
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1,044,476✔
632
  }
633
  colDataDestroy(p);
184,505,279✔
634
  taosMemoryFree(p);
184,495,697✔
635
  return code;
184,491,918✔
636
}
637

638
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
183,647,484✔
639
  int32_t code = TSDB_CODE_SUCCESS;
183,647,484✔
640
  int8_t* pIndicator = (int8_t*)p->pData;
183,647,484✔
641
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
183,662,753✔
642
    // here nothing needs to be done
643
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
117,412,552✔
644
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
46,659,152✔
645
    pBlock->info.rows = 0;
46,656,296✔
646
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
70,753,400✔
647
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
70,756,878✔
648
  } else {
649
    qError("unknown filter result type: %d", status);
×
650
  }
651
  return code;
183,655,406✔
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,184,393✔
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,
986,952,858✔
753
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
754
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
986,952,858✔
755
  if (page == NULL) {
987,041,172✔
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);
987,041,172✔
761

762
  SqlFunctionCtx* pCtx = pSup->pCtx;
987,053,207✔
763
  SExprInfo*      pExprInfo = pSup->pExprInfo;
987,046,152✔
764
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
987,058,435✔
765

766
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
987,055,697✔
767
  if (pRow->numOfRows == 0) {
986,994,624✔
768
    releaseBufPage(pBuf, page);
×
769
    return;
×
770
  }
771

772
  int32_t size = pBlock->info.capacity;
987,000,687✔
773
  while (pBlock->info.rows + pRow->numOfRows > size) {
987,259,899✔
774
    size = size * 1.25;
237,650✔
775
  }
776

777
  int32_t code = blockDataEnsureCapacity(pBlock, size);
987,022,664✔
778
  if (TAOS_FAILED(code)) {
986,992,450✔
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);
986,992,450✔
785
  if (TAOS_FAILED(code)) {
986,984,831✔
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);
986,984,831✔
792
  pBlock->info.rows += pRow->numOfRows;
986,961,176✔
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,147,483,647✔
841
          releaseBufPage(pBuf, page);
2,147,483,647✔
842
          break;
2,147,483,647✔
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;
17,040✔
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,
121,763,005✔
883
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, STrueForInfo *pTrueForInfo) {
884
  int32_t         code = TSDB_CODE_SUCCESS;
121,763,005✔
885
  int32_t         lino = 0;
121,763,005✔
886
  SExprInfo*      pExprInfo = pSup->pExprInfo;
121,763,005✔
887
  int32_t         numOfExprs = pSup->numOfExprs;
121,766,214✔
888
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
121,765,524✔
889
  SqlFunctionCtx* pCtx = pSup->pCtx;
121,764,172✔
890

891
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
121,766,090✔
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;
119,763✔
908
      releaseBufPage(pBuf, page);
119,763✔
909
      continue;
119,763✔
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,
45,864✔
914
             pRow->win.skey, pRow->win.ekey, pRow->nOrigRows);
915
      pGroupResInfo->index += 1;
45,864✔
916
      releaseBufPage(pBuf, page);
45,864✔
917
      continue;
45,864✔
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) {
2,140,447,924✔
926
          releaseBufPage(pBuf, page);
20,025,566✔
927
          break;
20,026,256✔
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;
17,342,275✔
949
    }
950
  }
951

952
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
121,580,084✔
953
         pBlock->info.id.groupId);
954
  pBlock->info.dataLoad = 1;
121,586,835✔
955
  code = blockDataUpdateTsWindow(pBlock, 0);
121,766,309✔
956
  QUERY_CHECK_CODE(code, lino, _end);
121,765,763✔
957

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

965
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
143,012,782✔
966
                            SDiskbasedBuf* pBuf) {
967
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
143,012,782✔
968
  SSDataBlock*   pBlock = pbInfo->pRes;
143,015,654✔
969

970
  // set output datablock version
971
  pBlock->info.version = pTaskInfo->version;
143,015,654✔
972

973
  blockDataCleanup(pBlock);
143,013,601✔
974
  if (!hasRemainResults(pGroupResInfo)) {
143,017,962✔
975
    return;
21,696,331✔
976
  }
977

978
  // clear the existed group id
979
  pBlock->info.id.groupId = 0;
121,319,023✔
980
  if (!pbInfo->mergeResultBlock) {
121,319,023✔
981
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
47,336,237✔
982
                       false, getTrueForInfo(pOperator));
983
  } else {
984
    while (hasRemainResults(pGroupResInfo)) {
137,556,577✔
985
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
73,981,432✔
986
                         true, getTrueForInfo(pOperator));
987
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
73,980,892✔
988
        break;
10,405,751✔
989
      }
990

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

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

1000
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
428,185,067✔
1001
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,662,050,425✔
1002
    SExprInfo* pExprInfo = &pExpr[i];
1,233,860,993✔
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,396,193,602✔
1005
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
1,153,369,130✔
1006
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
242,792,596✔
1007
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
138,326,910✔
1008
      }
1009
    }
1010

1011
    taosMemoryFree(pExprInfo->base.pParam);
1,233,887,270✔
1012
    taosMemoryFree(pExprInfo->pExpr);
1,233,866,447✔
1013
  }
1014
}
428,189,432✔
1015

1016
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
308,362,797✔
1017
  *defaultPgsz = 4096;
308,362,797✔
1018
  uint32_t last = *defaultPgsz;
308,382,488✔
1019
  while (*defaultPgsz < rowSize * 4) {
370,450,489✔
1020
    *defaultPgsz <<= 1u;
62,113,061✔
1021
    if (*defaultPgsz < last) {
62,093,390✔
1022
      return TSDB_CODE_INVALID_PARA;
×
1023
    }
1024
    last = *defaultPgsz;
62,094,330✔
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;
308,315,254✔
1031
  if ((*defaultBufsz) <= (*defaultPgsz)) {
308,361,855✔
1032
    (*defaultBufsz) = (*defaultPgsz) * 4;
×
1033
    if (*defaultBufsz < ((int64_t)(*defaultPgsz)) * 4) {
×
1034
      return TSDB_CODE_INVALID_PARA;
×
1035
    }
1036
  }
1037

1038
  return 0;
308,342,316✔
1039
}
1040

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

1046
  pResultInfo->capacity = numOfRows;
662,673,362✔
1047
  pResultInfo->threshold = numOfRows * 0.75;
662,766,715✔
1048

1049
  if (pResultInfo->threshold == 0) {
662,664,534✔
1050
    pResultInfo->threshold = numOfRows;
2,138,933✔
1051
  }
1052
  pResultInfo->totalRows = 0;
662,697,457✔
1053
}
662,671,991✔
1054

1055
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
290,214,386✔
1056
  pInfo->pRes = pBlock;
290,214,386✔
1057
  initResultRowInfo(&pInfo->resultRowInfo);
290,247,985✔
1058
}
290,179,439✔
1059

1060
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
1,253,134,968✔
1061
  if (pCtx == NULL) {
1,253,134,968✔
1062
    return;
796,875,919✔
1063
  }
1064

1065
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,677,502,275✔
1066
    if (pExpr != NULL) {
1,221,244,050✔
1067
      SExprInfo* pExprInfo = &pExpr[i];
1,221,223,138✔
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,382,781,434✔
1070
          colDataDestroy(pCtx[i].input.pData[j]);
136,150,237✔
1071
          taosMemoryFree(pCtx[i].input.pData[j]);
136,151,470✔
1072
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
136,147,225✔
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,382,781,186✔
1078
    }
1079

1080
    if(pCtx[i].fpSet.cleanup) {
1,221,263,559✔
1081
      pCtx[i].fpSet.cleanup(&pCtx[i]);
941,398✔
1082
    }
1083

1084
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,221,284,062✔
1085
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,221,295,491✔
1086
    taosMemoryFree(pCtx[i].input.pData);
1,221,286,098✔
1087
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,221,259,104✔
1088

1089
    if (pCtx[i].udfName != NULL) {
1,221,235,040✔
1090
      taosMemoryFree(pCtx[i].udfName);
37,137✔
1091
    }
1092
  }
1093

1094
  taosMemoryFreeClear(pCtx);
456,258,225✔
1095
  return;
456,233,364✔
1096
}
1097

1098
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
452,758,473✔
1099
  pSup->pExprInfo = pExprInfo;
452,758,473✔
1100
  pSup->numOfExprs = numOfExpr;
452,780,243✔
1101
  if (pSup->pExprInfo != NULL) {
452,765,478✔
1102
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
352,322,309✔
1103
    if (pSup->pCtx == NULL) {
352,247,157✔
1104
      return terrno;
×
1105
    }
1106
  }
1107

1108
  return TSDB_CODE_SUCCESS;
452,736,976✔
1109
}
1110

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

1120
static void cleanupIndefRowsResultRow(SOperatorInfo* pOperator, SResultRow* pRow) {
10,257,843✔
1121
  if (NULL == pOperator || NULL == pRow) {
10,257,843✔
1122
    return;
×
1123
  }
1124

1125
  SExprSupp* pSup = &pOperator->exprSupp;
10,258,715✔
1126
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
22,888,360✔
1127
    pSup->pCtx[i].resultInfo = getResultEntryInfo(pRow, i, pSup->rowEntryInfoOffset);
12,630,081✔
1128
    if (pSup->pCtx[i].fpSet.cleanup != NULL) {
12,630,081✔
1129
      pSup->pCtx[i].fpSet.cleanup(&pSup->pCtx[i]);
13,344✔
1130
    }
1131
    pSup->pCtx[i].resultInfo = NULL;
12,630,081✔
1132
  }
1133
}
1134

1135
static void destroyIndefRowsWindowState(SOperatorInfo* pOperator, SIndefRowsWindowState* pState) {
3,058✔
1136
  if (NULL == pState) {
3,058✔
1137
    return;
×
1138
  }
1139

1140
  cleanupIndefRowsResultRow(pOperator, pState->pRow);
3,058✔
1141
  taosMemoryFreeClear(pState->pRow);
3,058✔
1142

1143
  if (pState->pSealedBlocks != NULL) {
3,058✔
1144
    SListNode* pNode = NULL;
3,058✔
1145
    while ((pNode = tdListPopHead(pState->pSealedBlocks)) != NULL) {
3,058✔
1146
      SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
×
1147
      blockDataDestroy(pBlock);
×
1148
      taosMemoryFree(pNode);
×
1149
    }
1150
    pState->pSealedBlocks = tdListFree(pState->pSealedBlocks);
3,058✔
1151
  }
1152

1153
  blockDataDestroy(pState->pCurBlock);
3,058✔
1154
  taosMemoryFreeClear(pState);
3,058✔
1155
}
1156

1157
static void removeIndefRowsOpenState(SSHashObj* pOpenStatesMap, SIndefRowsWindowState* pState) {
10,254,785✔
1158
  if (NULL == pOpenStatesMap || NULL == pState) {
10,254,785✔
1159
    return;
×
1160
  }
1161

1162
  SIndefRowsStateKey key = {.groupId = pState->groupId, .skey = pState->win.skey};
10,256,093✔
1163
  int32_t  code = tSimpleHashRemove(pOpenStatesMap, &key, sizeof(key));
10,256,093✔
1164
  if (code != TSDB_CODE_SUCCESS) {
10,256,529✔
1165
    qError("failed to remove open state for groupId:%" PRIu64 ", skey:%" PRId64 ", code:%s", pState->groupId, pState->win.skey,
×
1166
           tstrerror(code));
1167
  }
1168
}
1169

1170
static int32_t createIndefRowsWindowState(SIndefRowsRuntime* pRuntime, SSDataBlock* pResultTemplate, uint64_t groupId,
10,257,843✔
1171
                                          const STimeWindow* pWin, int32_t resultRowSize,
1172
                                          SIndefRowsWindowState** ppState) {
1173
  int32_t code = TSDB_CODE_SUCCESS;
10,257,843✔
1174
  int32_t lino = 0;
10,257,843✔
1175

1176
  SIndefRowsWindowState* pState = taosMemoryCalloc(1, sizeof(SIndefRowsWindowState));
10,257,843✔
1177
  TSDB_CHECK_NULL(pState, code, lino, _return, terrno);
10,259,151✔
1178

1179
  pState->pRow = taosMemoryCalloc(1, resultRowSize);
10,259,151✔
1180
  QUERY_CHECK_NULL(pState->pRow, code, lino, _return, terrno);
10,258,715✔
1181

1182
  pState->pSealedBlocks = tdListNew(POINTER_BYTES);
10,258,715✔
1183
  QUERY_CHECK_NULL(pState->pSealedBlocks, code, lino, _return, terrno);
10,258,715✔
1184

1185
  code = createOneDataBlock(pResultTemplate, false, &pState->pCurBlock);
10,258,715✔
1186
  QUERY_CHECK_CODE(code, lino, _return);
10,258,715✔
1187

1188
  pState->groupId = groupId;
10,258,715✔
1189
  pState->win = *pWin;
10,259,587✔
1190
  TAOS_SET_POBJ_ALIGNED(&pState->pRow->win, pWin);
10,259,587✔
1191

1192
  SIndefRowsStateKey key = {.groupId = groupId, .skey = pWin->skey};
10,259,587✔
1193
  code = tSimpleHashPut(pRuntime->pOpenStatesMap, &key, sizeof(key), &pState, POINTER_BYTES);
10,259,587✔
1194
  QUERY_CHECK_CODE(code, lino, _return);
10,259,587✔
1195

1196
  *ppState = pState;
10,259,587✔
1197
  return code;
10,259,587✔
1198

1199
_return:
×
1200
  if (NULL != pState) {
×
1201
    taosMemoryFree(pState->pRow);
×
1202
    pState->pSealedBlocks = tdListFree(pState->pSealedBlocks);
×
1203
    blockDataDestroy(pState->pCurBlock);
×
1204
    taosMemoryFree(pState);
×
1205
  }
1206

1207
  if (code != TSDB_CODE_SUCCESS) {
×
1208
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1209
  }
1210

1211
  return code;
×
1212
}
1213

1214
static int32_t fillIndefRowsWindowPseudoCols(SOperatorInfo* pOperator, SSDataBlock* pBlock, const STimeWindow* pWin,
1,192,470✔
1215
                                             int32_t startOffset, int32_t rows) {
1216
  int32_t code = TSDB_CODE_SUCCESS;
1,192,470✔
1217
  int32_t lino = 0;
1,192,470✔
1218

1219
  if (rows <= 0) {
1,192,470✔
1220
    return code;
×
1221
  }
1222

1223
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
4,843,802✔
1224
    SqlFunctionCtx* pCtx = &pOperator->exprSupp.pCtx[i];
3,651,768✔
1225
    if (!fmIsPseudoColumnFunc(pCtx->functionId)) {
3,650,896✔
1226
      continue;
1,852,882✔
1227
    }
1228

1229
    const char* pFuncName = pCtx->pExpr->pExpr->_function.functionName;
1,798,450✔
1230
    int64_t     val = 0;
1,798,014✔
1231
    if (strcmp(pFuncName, "_wstart") == 0) {
1,795,834✔
1232
      val = pWin->skey;
737,282✔
1233
    } else if (strcmp(pFuncName, "_wend") == 0) {
1,058,552✔
1234
      val = pWin->ekey;
528,840✔
1235
    } else if (strcmp(pFuncName, "_wduration") == 0) {
529,712✔
1236
      val = pWin->ekey - pWin->skey;
528,840✔
1237
    } else {
1238
      continue;
872✔
1239
    }
1240

1241
    int32_t slotId = pOperator->exprSupp.pExprInfo[i].base.resSchema.slotId;
1,795,834✔
1242
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
1,796,270✔
1243
    QUERY_CHECK_NULL(pCol, code, lino, _return, terrno);
1,798,014✔
1244

1245
    for (int32_t row = 0; row < rows; ++row) {
704,456,482✔
1246
      code = colDataSetVal(pCol, startOffset + row, (const char*)&val, false);
702,655,416✔
1247
      QUERY_CHECK_CODE(code, lino, _return);
702,658,468✔
1248
    }
1249
  }
1250

1251
  return code;
1,193,778✔
1252

1253
_return:
×
1254
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1255
  return code;
×
1256
}
1257

1258
int32_t initIndefRowsRuntime(SIndefRowsRuntime* pRuntime, SqlFunctionCtx* pCtx, int32_t numOfExprs, int32_t blockCapacity) {
114,937✔
1259
  int32_t code = TSDB_CODE_SUCCESS;
114,937✔
1260
  int32_t lino = 0;
114,937✔
1261

1262
  if (NULL == pRuntime) {
114,937✔
1263
    return TSDB_CODE_INVALID_PARA;
×
1264
  }
1265

1266
  pRuntime->blockCapacity = blockCapacity > 0 ? blockCapacity : 4096;
114,937✔
1267

1268
  pRuntime->pOpenStatesMap = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
114,937✔
1269
  QUERY_CHECK_NULL(pRuntime->pOpenStatesMap, code, lino, _return, terrno);
114,937✔
1270

1271
  pRuntime->pReadyBlocks = tdListNew(POINTER_BYTES);
114,937✔
1272
  QUERY_CHECK_NULL(pRuntime->pReadyBlocks, code, lino, _return, terrno);
114,937✔
1273

1274
  code = setRowTsColumnOutputInfo(pCtx, numOfExprs, &pRuntime->pPseudoColInfo);
114,937✔
1275
  QUERY_CHECK_CODE(code, lino, _return);
114,937✔
1276

1277
  return code;
114,937✔
1278

1279
_return:
×
1280
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1281
  pRuntime->pReadyBlocks = tdListFree(pRuntime->pReadyBlocks);
×
1282
  tSimpleHashCleanup(pRuntime->pOpenStatesMap);
×
1283
  pRuntime->pOpenStatesMap = NULL;
×
1284
  return code;
×
1285
}
1286

1287
void resetIndefRowsRuntime(SIndefRowsRuntime* pRuntime, SOperatorInfo* pOperator) {
8,342,159✔
1288
  if (NULL == pRuntime) {
8,342,159✔
1289
    return;
×
1290
  }
1291

1292
  if (pRuntime->pReturnedBlock != NULL) {
8,342,159✔
1293
    blockDataDestroy(pRuntime->pReturnedBlock);
×
1294
    pRuntime->pReturnedBlock = NULL;
×
1295
  }
1296

1297
  if (pRuntime->pReadyBlocks != NULL) {
8,341,556✔
1298
    SListNode* pNode = NULL;
114,937✔
1299
    while ((pNode = tdListPopHead(pRuntime->pReadyBlocks)) != NULL) {
114,937✔
1300
      SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
×
1301
      blockDataDestroy(pBlock);
×
1302
      taosMemoryFree(pNode);
×
1303
    }
1304
  }
1305

1306
  if (pRuntime->pOpenStatesMap != NULL) {
8,339,083✔
1307
    int32_t iter = 0;
114,937✔
1308
    void*   pData = NULL;
114,937✔
1309
    while ((pData = tSimpleHashIterate(pRuntime->pOpenStatesMap, pData, &iter)) != NULL) {
117,995✔
1310
      SIndefRowsWindowState* pState = *(SIndefRowsWindowState**)pData;
3,058✔
1311
      destroyIndefRowsWindowState(pOperator, pState);
3,058✔
1312
    }
1313
    tSimpleHashClear(pRuntime->pOpenStatesMap);
114,937✔
1314
  }
1315

1316
  if (pRuntime->pTmpBlock != NULL) {
8,342,159✔
1317
    blockDataCleanup(pRuntime->pTmpBlock);
102,597✔
1318
  }
1319
}
1320

1321
void cleanupIndefRowsRuntime(SIndefRowsRuntime* pRuntime, SOperatorInfo* pOperator) {
8,284,940✔
1322
  if (NULL == pRuntime) {
8,284,940✔
1323
    return;
×
1324
  }
1325

1326
  resetIndefRowsRuntime(pRuntime, pOperator);
8,284,940✔
1327
  tSimpleHashCleanup(pRuntime->pOpenStatesMap);
8,280,793✔
1328
  pRuntime->pOpenStatesMap = NULL;
8,281,030✔
1329
  pRuntime->pReadyBlocks = tdListFree(pRuntime->pReadyBlocks);
8,281,899✔
1330
  taosArrayDestroy(pRuntime->pPseudoColInfo);
8,286,723✔
1331
  pRuntime->pPseudoColInfo = NULL;
8,282,516✔
1332
  blockDataDestroy(pRuntime->pTmpBlock);
8,281,566✔
1333
  pRuntime->pTmpBlock = NULL;
8,282,280✔
1334
}
1335

1336
SIndefRowsWindowState* findIndefRowsWindowState(const SIndefRowsRuntime* pRuntime, uint64_t groupId, TSKEY winSKey) {
10,561,551✔
1337
  if (NULL == pRuntime || NULL == pRuntime->pOpenStatesMap) {
10,561,551✔
1338
    return NULL;
×
1339
  }
1340

1341
  SIndefRowsStateKey key = {.groupId = groupId, .skey = winSKey};
10,561,551✔
1342
  SIndefRowsWindowState** ppState = tSimpleHashGet(pRuntime->pOpenStatesMap, &key, sizeof(key));
10,562,859✔
1343
  return ppState ? *ppState : NULL;
10,562,859✔
1344
}
1345

1346
int32_t applyIndefRowsFuncOnWindowState(SOperatorInfo* pOperator, SIndefRowsRuntime* pRuntime,
10,481,115✔
1347
                                        SIndefRowsWindowState** ppState, SSDataBlock* pResultTemplate,
1348
                                        uint64_t groupId, const STimeWindow* pWin, SSDataBlock* pInputBlock,
1349
                                        int32_t startRow, int32_t numRows, int32_t inputTsOrder,
1350
                                        int32_t resultRowSize) {
1351
  int32_t                code = TSDB_CODE_SUCCESS;
10,481,115✔
1352
  int32_t                lino = 0;
10,481,115✔
1353
  SIndefRowsWindowState* pState = NULL;
10,481,115✔
1354

1355
  QRY_PARAM_CHECK(ppState);
10,481,987✔
1356
  if (numRows <= 0) {
10,482,423✔
1357
    return code;
×
1358
  }
1359

1360
  if (NULL == pRuntime || NULL == pRuntime->pPseudoColInfo) {
10,482,423✔
1361
    qError("%s invalid parameter, pRuntime:%p, pPseudoColInfo:%p", __func__, pRuntime, pRuntime ? pRuntime->pPseudoColInfo : NULL);
1,308✔
1362
    return TSDB_CODE_INVALID_PARA;
×
1363
  }
1364

1365
  pState = findIndefRowsWindowState(pRuntime, groupId, pWin->skey);
10,481,987✔
1366
  if (NULL == pState) {
10,482,859✔
1367
    code = createIndefRowsWindowState(pRuntime, pResultTemplate, groupId, pWin, resultRowSize, &pState);
10,259,151✔
1368
    QUERY_CHECK_CODE(code, lino, _return);
10,259,587✔
1369
    for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
22,897,528✔
1370
      pOperator->exprSupp.pCtx[i].pOutput = NULL;
12,637,069✔
1371
    }
1372
  } else {
1373
    // Only update ekey — groupId and skey already matched by find
1374
    pState->win.ekey = pWin->ekey;
223,708✔
1375
    pState->pRow->win.ekey = pWin->ekey;
223,708✔
1376
  }
1377

1378
  code = setResultRowInitCtx(pState->pRow, pOperator->exprSupp.pCtx, pOperator->exprSupp.numOfExprs,
10,483,731✔
1379
                             pOperator->exprSupp.rowEntryInfoOffset);
1380
  QUERY_CHECK_CODE(code, lino, _return);
10,483,295✔
1381

1382
  if (NULL == pRuntime->pTmpBlock) {
10,483,295✔
1383
    code = createOneDataBlock(pInputBlock, false, &pRuntime->pTmpBlock);
102,597✔
1384
    QUERY_CHECK_CODE(code, lino, _return);
102,597✔
1385
  } else {
1386
    blockDataCleanup(pRuntime->pTmpBlock);
10,380,698✔
1387
  }
1388

1389
  SSDataBlock* pWindowBlock = pRuntime->pTmpBlock;
10,483,295✔
1390
  code = blockDataEnsureCapacity(pWindowBlock, TMAX(1, numRows));
10,482,859✔
1391
  QUERY_CHECK_CODE(code, lino, _return);
10,483,295✔
1392
  code = blockDataMergeNRows(pWindowBlock, pInputBlock, startRow, numRows);
10,483,295✔
1393
  QUERY_CHECK_CODE(code, lino, _return);
10,481,987✔
1394

1395
  pWindowBlock->info.id = pInputBlock->info.id;
10,481,987✔
1396
  pWindowBlock->info.window = *pWin;
10,482,859✔
1397
  pWindowBlock->info.scanFlag = pInputBlock->info.scanFlag;
10,483,295✔
1398
  pWindowBlock->info.dataLoad = pInputBlock->info.dataLoad;
10,483,295✔
1399

1400
  code = setInputDataBlock(&pOperator->exprSupp, pWindowBlock, inputTsOrder, pWindowBlock->info.scanFlag, false);
10,483,295✔
1401
  QUERY_CHECK_CODE(code, lino, _return);
10,482,859✔
1402

1403
  pState->pCurBlock->info.id = pInputBlock->info.id;
10,482,859✔
1404
  pState->pCurBlock->info.window = *pWin;
10,483,295✔
1405
  pState->pCurBlock->info.scanFlag = pInputBlock->info.scanFlag;
10,483,295✔
1406
  pState->pCurBlock->info.dataLoad = 1;
10,483,295✔
1407
  code = blockDataEnsureCapacity(pState->pCurBlock, pState->pCurBlock->info.rows + pWindowBlock->info.rows);
10,483,295✔
1408
  QUERY_CHECK_CODE(code, lino, _return);
10,483,295✔
1409

1410
  code = projectApplyFunctions(pOperator->exprSupp.pExprInfo, pState->pCurBlock, pWindowBlock,
10,483,295✔
1411
                               pOperator->exprSupp.pCtx, pOperator->exprSupp.numOfExprs,
1412
                               pRuntime->pPseudoColInfo,
1413
                               GET_STM_RTINFO(pOperator->pTaskInfo));
10,483,295✔
1414
  QUERY_CHECK_CODE(code, lino, _return);
10,482,859✔
1415

1416
  pState->pRow->nOrigRows += numRows;
10,479,801✔
1417

1418
  // Seal current block if it reached capacity
1419
  if (pState->pCurBlock->info.rows >= pRuntime->blockCapacity) {
10,480,237✔
1420
    code = tdListAppend(pState->pSealedBlocks, &pState->pCurBlock);
301,436✔
1421
    QUERY_CHECK_CODE(code, lino, _return);
301,436✔
1422
    pState->pCurBlock = NULL;
301,436✔
1423
    code = createOneDataBlock(pResultTemplate, false, &pState->pCurBlock);
301,436✔
1424
    QUERY_CHECK_CODE(code, lino, _return);
301,436✔
1425
  }
1426

1427
  *ppState = pState;
10,479,801✔
1428

1429
_return:
10,482,423✔
1430
  if (code != TSDB_CODE_SUCCESS) {
10,482,859✔
1431
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
3,058✔
1432
  }
1433

1434
  return code;
10,482,423✔
1435
}
1436

1437
int32_t closeIndefRowsWindowState(SOperatorInfo* pOperator, SIndefRowsRuntime* pRuntime, SIndefRowsWindowState* pState) {
10,256,093✔
1438
  int32_t code = TSDB_CODE_SUCCESS;
10,256,093✔
1439
  int32_t lino = 0;
10,256,093✔
1440

1441
  if (NULL == pRuntime || NULL == pState) {
10,256,093✔
1442
    return code;
×
1443
  }
1444

1445
  // Phase 1: Seal current block into sealed list (so all blocks are in one place)
1446
  if (pState->pCurBlock != NULL && pState->pCurBlock->info.rows > 0) {
10,256,529✔
1447
    code = tdListAppend(pState->pSealedBlocks, &pState->pCurBlock);
891,906✔
1448
    QUERY_CHECK_CODE(code, lino, _error);
891,470✔
1449
    pState->pCurBlock = NULL;
891,470✔
1450
  } else {
1451
    blockDataDestroy(pState->pCurBlock);
9,364,187✔
1452
    pState->pCurBlock = NULL;
9,364,187✔
1453
  }
1454

1455
  // Phase 2: Process each block — fill pseudo cols, filter, push to readyBlocks
1456
  SListNode* pNode = NULL;
10,254,785✔
1457
  while ((pNode = tdListPopHead(pState->pSealedBlocks)) != NULL) {
11,448,127✔
1458
    SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
1,192,906✔
1459
    taosMemoryFree(pNode);
1,192,906✔
1460

1461
    code = fillIndefRowsWindowPseudoCols(pOperator, pBlock, &pState->win, 0, pBlock->info.rows);
1,192,470✔
1462
    if (code != TSDB_CODE_SUCCESS) {
1,192,470✔
1463
      blockDataDestroy(pBlock);
×
1464
      qError("%s fillPseudoCols failed since %s", __func__, tstrerror(code));
×
1465
      goto _cleanup;
×
1466
    }
1467

1468
    if (pOperator->exprSupp.pFilterInfo != NULL) {
1,192,470✔
1469
      code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL);
×
1470
      if (code != TSDB_CODE_SUCCESS) {
×
1471
        blockDataDestroy(pBlock);
×
1472
        qError("%s doFilter failed since %s", __func__, tstrerror(code));
×
1473
        goto _cleanup;
×
1474
      }
1475
    }
1476

1477
    if (pBlock->info.rows > 0) {
1,192,906✔
1478
      code = tdListAppend(pRuntime->pReadyBlocks, &pBlock);
1,193,342✔
1479
      if (code != TSDB_CODE_SUCCESS) {
1,193,778✔
1480
        blockDataDestroy(pBlock);
×
1481
        qError("%s append readyBlocks failed since %s", __func__, tstrerror(code));
×
1482
        goto _cleanup;
×
1483
      }
1484
    } else {
1485
      blockDataDestroy(pBlock);
×
1486
    }
1487
  }
1488

1489
  // Phase 3: All succeeded — remove from open states and destroy the state shell
1490
  removeIndefRowsOpenState(pRuntime->pOpenStatesMap, pState);
10,254,785✔
1491
  cleanupIndefRowsResultRow(pOperator, pState->pRow);
10,255,657✔
1492
  taosMemoryFreeClear(pState->pRow);
10,256,529✔
1493
  pState->pSealedBlocks = tdListFree(pState->pSealedBlocks);
10,256,093✔
1494
  taosMemoryFreeClear(pState);
10,254,785✔
1495

1496
  return TSDB_CODE_SUCCESS;
10,256,529✔
1497

1498
_error:
×
1499
  // Seal failed — state still intact in openStates, let dropAll clean up
1500
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1501
  return code;
×
1502

1503
_cleanup:
×
1504
  // Some blocks already pushed to readyBlocks (they'll be consumed normally).
1505
  // Remaining blocks still in pSealedBlocks — destroy them along with state.
1506
  removeIndefRowsOpenState(pRuntime->pOpenStatesMap, pState);
×
1507
  destroyIndefRowsWindowState(pOperator, pState);
×
1508
  return code;
×
1509
}
1510

1511
int32_t closeAllIndefRowsWindowStates(SOperatorInfo* pOperator, SIndefRowsRuntime* pRuntime) {
107,131✔
1512
  int32_t code = TSDB_CODE_SUCCESS;
107,131✔
1513
  int32_t lino = 0;
107,131✔
1514

1515
  if (NULL == pRuntime || NULL == pRuntime->pOpenStatesMap) {
107,131✔
1516
    return code;
×
1517
  }
1518

1519
  // Collect all states first since closeIndefRowsWindowState removes from the map
1520
  int32_t numOpen = tSimpleHashGetSize(pRuntime->pOpenStatesMap);
107,131✔
1521
  if (numOpen == 0) {
107,131✔
1522
    return code;
14,118✔
1523
  }
1524

1525
  SArray* pStates = taosArrayInit(numOpen, POINTER_BYTES);
93,013✔
1526
  QUERY_CHECK_NULL(pStates, code, lino, _return, terrno);
93,013✔
1527

1528
  int32_t iter = 0;
93,013✔
1529
  void*   pData = NULL;
93,013✔
1530
  while ((pData = tSimpleHashIterate(pRuntime->pOpenStatesMap, pData, &iter)) != NULL) {
584,634✔
1531
    SIndefRowsWindowState* pState = *(SIndefRowsWindowState**)pData;
491,621✔
1532
    QUERY_CHECK_NULL(taosArrayPush(pStates, &pState), code, lino, _return, terrno);
491,621✔
1533
  }
1534

1535
  for (int32_t i = 0; i < taosArrayGetSize(pStates); ++i) {
584,634✔
1536
    SIndefRowsWindowState* pState = taosArrayGetP(pStates, i);
491,621✔
1537
    code = closeIndefRowsWindowState(pOperator, pRuntime, pState);
491,621✔
1538
    QUERY_CHECK_CODE(code, lino, _return);
491,621✔
1539
  }
1540

1541
_return:
93,013✔
1542
  taosArrayDestroy(pStates);
93,013✔
1543
  if (code != TSDB_CODE_SUCCESS) {
93,013✔
1544
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1545
  }
1546
  return code;
93,013✔
1547
}
1548

1549
void dropIndefRowsWindowState(SOperatorInfo* pOperator, SIndefRowsRuntime* pRuntime, SIndefRowsWindowState* pState) {
×
1550
  if (NULL == pRuntime || NULL == pState) {
×
1551
    return;
×
1552
  }
1553

1554
  removeIndefRowsOpenState(pRuntime->pOpenStatesMap, pState);
×
1555
  destroyIndefRowsWindowState(pOperator, pState);
×
1556
}
1557

1558
void dropAllIndefRowsWindowStates(SOperatorInfo* pOperator, SIndefRowsRuntime* pRuntime) {
20,548✔
1559
  if (NULL == pRuntime || NULL == pRuntime->pOpenStatesMap) {
20,548✔
1560
    return;
×
1561
  }
1562

1563
  int32_t openCount = tSimpleHashGetSize(pRuntime->pOpenStatesMap);
20,548✔
1564
  if (openCount > 0) {
20,548✔
1565
    qDebug("%s dropping %d unclosed indefRows window state(s)", GET_TASKID(pOperator->pTaskInfo), openCount);
×
1566
  }
1567

1568
  int32_t iter = 0;
20,548✔
1569
  void*   pData = NULL;
20,548✔
1570
  while ((pData = tSimpleHashIterate(pRuntime->pOpenStatesMap, pData, &iter)) != NULL) {
20,548✔
1571
    SIndefRowsWindowState* pState = *(SIndefRowsWindowState**)pData;
×
1572
    destroyIndefRowsWindowState(pOperator, pState);
×
1573
  }
1574
  tSimpleHashClear(pRuntime->pOpenStatesMap);
20,548✔
1575
}
1576

1577
SSDataBlock* getNextIndefRowsResultBlock(SIndefRowsRuntime* pRuntime, SOperatorInfo* pOperator) {
1,391,773✔
1578
  if (NULL == pRuntime) {
1,391,773✔
1579
    return NULL;
×
1580
  }
1581

1582
  if (pRuntime->pReturnedBlock != NULL) {
1,391,773✔
1583
    blockDataDestroy(pRuntime->pReturnedBlock);
1,193,778✔
1584
    pRuntime->pReturnedBlock = NULL;
1,193,778✔
1585
  }
1586

1587
  if (NULL == pRuntime->pReadyBlocks) {
1,391,773✔
1588
    return NULL;
×
1589
  }
1590

1591
  while (listNEles(pRuntime->pReadyBlocks) > 0) {
1,391,773✔
1592
    SListNode* pNode = tdListPopHead(pRuntime->pReadyBlocks);
1,193,778✔
1593
    if (NULL == pNode) {
1,193,778✔
1594
      return NULL;
×
1595
    }
1596

1597
    SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
1,193,778✔
1598
    taosMemoryFree(pNode);
1,193,778✔
1599
    if (NULL == pBlock) {
1,193,778✔
1600
      continue;
×
1601
    }
1602

1603
    if (pBlock->info.rows > 0) {
1,193,778✔
1604
      pBlock->info.version = pOperator->pTaskInfo->version;
1,193,778✔
1605
      pBlock->info.dataLoad = 1;
1,193,778✔
1606
      pRuntime->pReturnedBlock = pBlock;
1,193,778✔
1607
      return pBlock;
1,193,778✔
1608
    }
1609

1610
    blockDataDestroy(pBlock);
×
1611
  }
1612

1613
  return NULL;
197,995✔
1614
}
1615

1616
void cleanupExprSupp(SExprSupp* pSupp) {
1,137,105,179✔
1617
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
1,137,105,179✔
1618
  if (pSupp->pExprInfo != NULL) {
1,137,086,399✔
1619
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
393,080,837✔
1620
    taosMemoryFreeClear(pSupp->pExprInfo);
393,075,023✔
1621
  }
1622

1623
  if (pSupp->pFilterInfo != NULL) {
1,137,069,334✔
1624
    filterFreeInfo(pSupp->pFilterInfo);
102,987,093✔
1625
    pSupp->pFilterInfo = NULL;
102,981,673✔
1626
  }
1627

1628
  taosMemoryFree(pSupp->rowEntryInfoOffset);
1,137,105,278✔
1629
  memset(pSupp, 0, sizeof(SExprSupp));
1,137,113,680✔
1630
}
1,137,113,680✔
1631

1632
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
115,670,772✔
1633
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
115,670,772✔
1634
  if (pSupp->pExprInfo != NULL) {
115,667,766✔
1635
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
32,704,620✔
1636
    taosMemoryFreeClear(pSupp->pExprInfo);
32,703,374✔
1637
  }
1638

1639
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
115,670,246✔
1640
  pSupp->numOfExprs = 0;
115,669,231✔
1641
  pSupp->hasWindowOrGroup = false;
115,673,931✔
1642
  pSupp->pCtx = NULL;
115,666,730✔
1643
}
115,664,637✔
1644

1645
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
293,642,215✔
1646
  blockDataDestroy(pInfo->pRes);
293,642,215✔
1647
  pInfo->pRes = NULL;
293,634,949✔
1648
}
293,637,758✔
1649

1650
bool groupbyTbname(SNodeList* pGroupList) {
258,962,371✔
1651
  bool   bytbname = false;
258,962,371✔
1652
  SNode* pNode = NULL;
258,962,371✔
1653
  FOREACH(pNode, pGroupList) {
266,073,811✔
1654
    if (pNode->type == QUERY_NODE_FUNCTION) {
38,847,454✔
1655
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
31,735,226✔
1656
      break;
31,736,731✔
1657
    }
1658
  }
1659
  return bytbname;
258,964,451✔
1660
}
1661

1662
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, SExecTaskInfo* pTask, SReadHandle* readHandle) {
378,065,385✔
1663
  switch (pNode->type) {
378,065,385✔
1664
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
538,177✔
1665
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
538,177✔
1666
      if (NULL == pInserterParam) {
538,177✔
1667
        return terrno;
×
1668
      }
1669
      pInserterParam->readHandle = readHandle;
538,177✔
1670

1671
      *pParam = pInserterParam;
538,177✔
1672
      break;
538,177✔
1673
    }
1674
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,892,184✔
1675
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,892,184✔
1676
      if (NULL == pDeleterParam) {
1,867,088✔
1677
        return terrno;
×
1678
      }
1679

1680
      SArray* pInfoList = NULL;
1,867,088✔
1681
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,867,088✔
1682
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,866,496✔
1683
        taosMemoryFree(pDeleterParam);
×
1684
        return code;
×
1685
      }
1686

1687
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
1,866,496✔
1688
      taosArrayDestroy(pInfoList);
1,866,496✔
1689

1690
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,866,496✔
1691

1692
      // TODO extract uid list
1693
      int32_t numOfTables = 0;
1,866,487✔
1694
      code = tableListGetSize(pTableListInfo, &numOfTables);
1,866,496✔
1695
      if (code != TSDB_CODE_SUCCESS) {
1,866,496✔
1696
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1697
        taosMemoryFree(pDeleterParam);
×
1698
        return code;
×
1699
      }
1700

1701
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
1,866,496✔
1702
      if (NULL == pDeleterParam->pUidList) {
1,867,088✔
1703
        taosMemoryFree(pDeleterParam);
×
1704
        return terrno;
×
1705
      }
1706

1707
      for (int32_t i = 0; i < numOfTables; ++i) {
3,984,649✔
1708
        STableKeyInfo* pTable = tableListGetInfo(pTableListInfo, i);
2,118,153✔
1709
        if (!pTable) {
2,118,153✔
1710
          taosArrayDestroy(pDeleterParam->pUidList);
×
1711
          taosMemoryFree(pDeleterParam);
×
1712
          return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1713
        }
1714
        void* tmp = taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
2,118,153✔
1715
        if (!tmp) {
2,118,153✔
1716
          taosArrayDestroy(pDeleterParam->pUidList);
×
1717
          taosMemoryFree(pDeleterParam);
×
1718
          return terrno;
×
1719
        }
1720
      }
1721

1722
      *pParam = pDeleterParam;
1,866,496✔
1723
      break;
1,867,088✔
1724
    }
1725
    default:
375,646,278✔
1726
      break;
375,646,278✔
1727
  }
1728

1729
  return TSDB_CODE_SUCCESS;
378,083,242✔
1730
}
1731

1732
void streamOpReleaseState(SOperatorInfo* pOperator) {
×
1733
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1734
  if (downstream->fpSet.releaseStreamStateFn) {
×
1735
    downstream->fpSet.releaseStreamStateFn(downstream);
×
1736
  }
1737
}
×
1738

1739
void streamOpReloadState(SOperatorInfo* pOperator) {
×
1740
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
1741
  if (downstream->fpSet.reloadStreamStateFn) {
×
1742
    downstream->fpSet.reloadStreamStateFn(downstream);
×
1743
  }
1744
}
×
1745

1746
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
149,031,225✔
1747
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
149,031,225✔
1748
  for (int32_t i = 0; i < childrenNum; ++i) {
150,851,176✔
1749
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
1,820,564✔
1750
    freeOperatorParam(pChild, type);
1,820,564✔
1751
  }
1752

1753
  taosArrayDestroy(pParam->pChildren);
149,030,612✔
1754
  pParam->pChildren = NULL;
149,039,707✔
1755

1756
  taosMemoryFreeClear(pParam->value);
149,040,214✔
1757

1758
  taosMemoryFree(pParam);
149,034,823✔
1759
}
149,026,916✔
1760

1761
void freeExchangeGetBasicOperatorParam(void* pParam) {
27,090,354✔
1762
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
27,090,354✔
1763
  if (pBasic->uidList) {
27,090,354✔
1764
    taosArrayDestroy(pBasic->uidList);
22,727,882✔
1765
    pBasic->uidList = NULL;
22,727,882✔
1766
  }
1767
  if (pBasic->orgTbInfo) {
27,090,354✔
1768
    taosArrayDestroy(pBasic->orgTbInfo->colMap);
12,110,019✔
1769
    taosMemoryFreeClear(pBasic->orgTbInfo);
12,110,019✔
1770
  }
1771
  if (pBasic->batchOrgTbInfo) {
27,090,354✔
1772
    taosArrayDestroyEx(pBasic->batchOrgTbInfo, destroySOrgTbInfo);
4,311,572✔
1773
    pBasic->batchOrgTbInfo = NULL;
4,311,572✔
1774
  }
1775
  if (pBasic->tagList) {
27,090,354✔
1776
    taosArrayDestroyEx(pBasic->tagList, destroyTagVal);
858,902✔
1777
    pBasic->tagList = NULL;
858,902✔
1778
  }
1779
}
27,090,354✔
1780

1781
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
25,140,250✔
1782
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
25,140,250✔
1783
  if (pExcParam->multiParams) {
25,140,250✔
1784
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
2,617,082✔
1785
    tSimpleHashSetFreeFp(pExcBatch->pBatchs, freeExchangeGetBasicOperatorParam);
2,617,082✔
1786
    tSimpleHashCleanup(pExcBatch->pBatchs);
2,617,082✔
1787
  } else {
1788
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
22,523,168✔
1789
  }
1790

1791
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
25,140,250✔
1792
}
25,140,250✔
1793

1794
void freeExchangeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1795

1796
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
7,928,214✔
1797

1798
void freeGroupCacheNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1799

1800
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
3,964,107✔
1801

1802
void freeMergeJoinNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1803

1804
void freeTagScanGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
6,393,540✔
1805

1806
void freeMergeGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
4,989,855✔
1807

1808
void freeDynQueryCtrlGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
×
1809

1810
void freeDynQueryCtrlNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1811

1812
void freeInterpFuncGetOperatorParam(SOperatorParam* pParam) {
×
1813
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
×
1814
}
×
1815

1816
void freeInterpFuncNotifyOperatorParam(SOperatorParam* pParam) {
×
1817
  freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM);
×
1818
}
×
1819

1820
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
94,221,737✔
1821
  STableScanOperatorParam* pTableScanParam =
94,221,737✔
1822
    (STableScanOperatorParam*)pParam->value;
1823
  taosArrayDestroy(pTableScanParam->pUidList);
94,224,922✔
1824
  if (pTableScanParam->pOrgTbInfo) {
94,233,719✔
1825
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
82,330,838✔
1826
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
82,330,838✔
1827
  }
1828
  if (pTableScanParam->pBatchTbInfo) {
94,234,999✔
1829
    for (int32_t i = 0;
3,214,490✔
1830
      i < taosArrayGetSize(pTableScanParam->pBatchTbInfo); ++i) {
9,497,766✔
1831
      SOrgTbInfo* pOrgTbInfo =
1832
        (SOrgTbInfo*)taosArrayGet(pTableScanParam->pBatchTbInfo, i);
6,283,276✔
1833
      taosArrayDestroy(pOrgTbInfo->colMap);
6,283,276✔
1834
    }
1835
    taosArrayDestroy(pTableScanParam->pBatchTbInfo);
3,214,490✔
1836
    pTableScanParam->pBatchTbInfo = NULL;
3,214,490✔
1837
  }
1838
  if (pTableScanParam->pTagList) {
94,239,219✔
1839
    for (int32_t i = 0;
858,902✔
1840
      i < taosArrayGetSize(pTableScanParam->pTagList); ++i) {
5,320,344✔
1841
      STagVal* pTagVal =
1842
        (STagVal*)taosArrayGet(pTableScanParam->pTagList, i);
4,461,442✔
1843
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
4,461,442✔
1844
        taosMemoryFreeClear(pTagVal->pData);
1,936,770✔
1845
      }
1846
    }
1847
    taosArrayDestroy(pTableScanParam->pTagList);
858,902✔
1848
    pTableScanParam->pTagList = NULL;
858,902✔
1849
  }
1850
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
94,239,140✔
1851
}
94,219,513✔
1852

1853
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1854

1855
void freeTagScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1856

1857
void freeMergeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1858

1859
void freeOpParamItem(void* pItem) {
17,387,926✔
1860
  SOperatorParam* pParam = *(SOperatorParam**)pItem;
17,387,926✔
1861
  pParam->reUse = false;
17,387,926✔
1862
  freeOperatorParam(pParam, OP_GET_PARAM);
17,387,926✔
1863
}
17,387,926✔
1864

1865
static void destroyRefColIdGroupParam(void* info) {
8,519✔
1866
  SRefColIdGroup* pGroup = (SRefColIdGroup*)info;
8,519✔
1867
  if (pGroup && pGroup->pSlotIdList) {
8,519✔
1868
    taosArrayDestroy(pGroup->pSlotIdList);
8,519✔
1869
    pGroup->pSlotIdList = NULL;
8,519✔
1870
  }
1871
}
8,519✔
1872

1873
void freeExternalWindowGetOperatorParam(SOperatorParam* pParam) {
1,111,289✔
1874
  SExternalWindowOperatorParam *pExtParam = (SExternalWindowOperatorParam*)pParam->value;
1,111,289✔
1875
  taosArrayDestroy(pExtParam->ExtWins);
1,111,289✔
1876
  for (int32_t i = 0; i < taosArrayGetSize(pParam->pChildren); i++) {
1,111,289✔
1877
    SOperatorParam* pChild = *(SOperatorParam**)taosArrayGet(pParam->pChildren, i);
×
1878
    pChild->reUse = false;
×
1879
  }
1880
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
1,111,289✔
1881
}
1,111,289✔
1882

1883
void freeVirtualTableScanGetOperatorParam(SOperatorParam* pParam) {
5,277,907✔
1884
  SVTableScanOperatorParam* pVTableScanParam = (SVTableScanOperatorParam*)pParam->value;
5,277,907✔
1885
  taosArrayDestroyEx(pVTableScanParam->pOpParamArray, freeOpParamItem);
5,277,907✔
1886
  if (pVTableScanParam->pRefColGroups) {
5,277,907✔
1887
    taosArrayDestroyEx(pVTableScanParam->pRefColGroups, destroyRefColIdGroupParam);
5,244✔
1888
    pVTableScanParam->pRefColGroups = NULL;
5,244✔
1889
  }
1890
  freeOpParamItem(&pVTableScanParam->pTagScanOp);
5,277,907✔
1891
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
5,277,907✔
1892
}
5,277,907✔
1893

1894
void freeVTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1895

1896
void freeExternalWindowNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1897

1898
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
840,369,463✔
1899
  if (NULL == pParam || pParam->reUse) {
840,369,463✔
1900
    return;
691,328,565✔
1901
  }
1902

1903
  switch (pParam->opType) {
149,042,769✔
1904
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
25,140,250✔
1905
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
25,140,250✔
1906
      break;
25,140,250✔
1907
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
7,928,214✔
1908
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
7,928,214✔
1909
      break;
7,928,214✔
1910
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
3,964,107✔
1911
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
3,964,107✔
1912
      break;
3,964,107✔
1913
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
94,235,370✔
1914
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
1915
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
94,235,370✔
1916
      break;
94,221,350✔
1917
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
5,277,907✔
1918
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
5,277,907✔
1919
      break;
5,277,907✔
1920
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
6,393,540✔
1921
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
6,393,540✔
1922
      break;
6,393,540✔
1923
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
4,989,855✔
1924
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
1925
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
1926
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL:
1927
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
1928
      type == OP_GET_PARAM ? freeMergeGetOperatorParam(pParam) : freeMergeNotifyOperatorParam(pParam);
4,989,855✔
1929
      break;
4,987,295✔
1930
    case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
1,111,289✔
1931
      type == OP_GET_PARAM ? freeExternalWindowGetOperatorParam(pParam) : freeExternalWindowNotifyOperatorParam(pParam);
1,111,289✔
1932
      break;
1,111,289✔
1933
    case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL:
×
1934
      type == OP_GET_PARAM ? freeDynQueryCtrlGetOperatorParam(pParam) : freeDynQueryCtrlNotifyOperatorParam(pParam);
×
1935
      break;
×
1936
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
×
1937
      type == OP_GET_PARAM ? freeInterpFuncGetOperatorParam(pParam) : freeInterpFuncNotifyOperatorParam(pParam);
×
1938
      break;
×
1939
    default:
8,943✔
1940
      qError("%s unsupported op %d param, param type %d, param:%p value:%p children:%p reuse:%d",
8,943✔
1941
             __func__, pParam->opType, type, pParam, pParam->value, pParam->pChildren, pParam->reUse);
1942
      break;
×
1943
  }
1944
}
1945

1946
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,777,350,404✔
1947
  SOperatorParam**  ppParam = NULL;
1,777,350,404✔
1948
  SOperatorParam*** pppDownstramParam = NULL;
1,777,350,404✔
1949
  switch (type) {
1,777,350,404✔
1950
    case OP_GET_PARAM:
947,150,458✔
1951
      ppParam = &pOperator->pOperatorGetParam;
947,150,458✔
1952
      pppDownstramParam = &pOperator->pDownstreamGetParams;
947,162,572✔
1953
      break;
947,149,330✔
1954
    case OP_NOTIFY_PARAM:
830,231,208✔
1955
      ppParam = &pOperator->pOperatorNotifyParam;
830,231,208✔
1956
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
830,233,661✔
1957
      break;
830,235,430✔
1958
    default:
×
1959
      return;
×
1960
  }
1961

1962
  if (*ppParam) {
1,777,384,760✔
1963
    qDebug("%s free self param, operator:%s type:%d paramType:%d param:%p", __func__, pOperator->name,
9,368,730✔
1964
           pOperator->operatorType, type, *ppParam);
1965
    freeOperatorParam(*ppParam, type);
9,368,730✔
1966
    *ppParam = NULL;
9,368,730✔
1967
  }
1968

1969
  if (*pppDownstramParam) {
1,777,365,820✔
1970
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
144,866,619✔
1971
      if ((*pppDownstramParam)[i]) {
24,761,383✔
1972
        qDebug("%s free downstream param, operator:%s type:%d idx:%d downstream:%s paramType:%d param:%p", __func__,
×
1973
               pOperator->name, pOperator->operatorType, i, pOperator->pDownstream[i]->name, type,
1974
               (*pppDownstramParam)[i]);
1975
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1976
        (*pppDownstramParam)[i] = NULL;
×
1977
      }
1978
    }
1979
    if (allFree) {
120,104,662✔
1980
      taosMemoryFreeClear(*pppDownstramParam);
23,446,237✔
1981
    }
1982
  }
1983
}
1984

1985
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
1,137,978,022✔
1986
                                                    SSDataBlock** pResBlock) {
1987
  QRY_PARAM_CHECK(pResBlock);
1,137,978,022✔
1988

1989
  int32_t code = 0;
1,138,024,372✔
1990
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
1,138,024,372✔
1991
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
19,597,464✔
1992
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
39,194,928✔
1993
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
19,597,464✔
1994
    if (clearParam && (code == 0)) {
19,597,464✔
1995
      qDebug("%s clear downstream param, operator:%s type:%d idx:%d downstream:%s param:%p", __func__,
7,542,705✔
1996
             pOperator->name, pOperator->operatorType, idx, pOperator->pDownstream[idx]->name,
1997
             pOperator->pDownstreamGetParams[idx]);
1998
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
7,542,705✔
1999
      pOperator->pDownstreamGetParams[idx] = NULL;
7,542,705✔
2000
    }
2001

2002
    if (code) {
19,597,464✔
2003
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
2004
    }
2005
    return code;
19,597,464✔
2006
  }
2007

2008
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
1,118,409,672✔
2009
  if (code) {
1,115,785,956✔
2010
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
2011
  }
2012
  return code;
1,115,797,312✔
2013
}
2014

2015
bool compareVal(const char* v, const SStateKeys* pKey) {
2,147,483,647✔
2016
  if (IS_VAR_DATA_TYPE(pKey->type)) {
2,147,483,647✔
2017
    if (IS_STR_DATA_BLOB(pKey->type)) {
367,195✔
2018
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
2019
        return false;
×
2020
      } else {
2021
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
2022
      }
2023
    } else {
2024
      if (varDataLen(v) != varDataLen(pKey->pData)) {
367,195✔
2025
        return false;
×
2026
      } else {
2027
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
367,195✔
2028
      }
2029
    }
2030
  } else {
2031
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
2,147,483,647✔
2032
  }
2033
}
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