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

taosdata / TDengine / #5023

15 Apr 2026 11:15AM UTC coverage: 72.251% (-0.03%) from 72.278%
#5023

push

travis-ci

web-flow
feat: implement fixed bucket table distribution (#35092)

221 of 236 new or added lines in 4 files covered. (93.64%)

613 existing lines in 123 files now uncovered.

257631 of 356577 relevant lines covered (72.25%)

133876647.56 hits per line

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

78.06
/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);
269,422,278✔
91
    if (pData == NULL) {
269,427,767✔
92
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
93
      return NULL;
×
94
    }
95
    pData->num = sizeof(SFilePage);
269,427,767✔
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);
836,023,491✔
108

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

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

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

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

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

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

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

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

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

161
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
1,719,200,144✔
162
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
4,737✔
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;
264✔
180
        pTaskInfo->code = terrno;
×
181
        return NULL;
×
182
      }
183
    }
184
  }
185

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

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

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

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

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

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

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

235
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
9,890,253✔
236
  if (code != TSDB_CODE_SUCCESS) {
9,891,825✔
237
    return code;
×
238
  }
239
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
9,891,825✔
240
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
9,889,837✔
241

242
  int64_t interval = 0;
9,886,942✔
243
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
244
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
9,886,424✔
245
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
9,888,212✔
246

247
  interval = -1;
9,889,992✔
248
  colDataSetInt64(pColData, 5,  &interval);
249
  return TSDB_CODE_SUCCESS;
9,893,304✔
250
}
251

252
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
24,178,929✔
253
  int32_t         code = TSDB_CODE_SUCCESS;
24,178,929✔
254
  int32_t         lino = 0;
24,178,929✔
255
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
24,178,929✔
256
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
130,344,405✔
257
    pCtx[i].order = order;
106,155,391✔
258
    pCtx[i].input.numOfRows = pBlock->info.rows;
106,154,755✔
259
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
106,152,239✔
260
    QUERY_CHECK_CODE(code, lino, _end);
106,165,469✔
261
    pCtx[i].pSrcBlock = pBlock;
106,165,469✔
262
    pCtx[i].scanFlag = scanFlag;
106,167,356✔
263
  }
264

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

272
int32_t setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
2,147,483,647✔
273
                          bool createDummyCol) {
274
  if (pBlock->pBlockAgg != NULL) {
2,147,483,647✔
275
    return doSetInputDataBlockInfo(pExprSup, pBlock, order, scanFlag);
24,182,703✔
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,
234,562,723✔
282
                                             int32_t numOfRows) {
283
  int32_t          code = TSDB_CODE_SUCCESS;
234,562,723✔
284
  int32_t          lino = 0;
234,562,723✔
285
  SColumnInfoData* pColInfo = NULL;
234,562,723✔
286
  if (pInput->pData[paramIndex] == NULL) {
234,562,723✔
287
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
633,137✔
288
    QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
632,694✔
289

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

294
    pInput->pData[paramIndex] = pColInfo;
632,694✔
295
  } else {
296
    pColInfo = pInput->pData[paramIndex];
233,971,036✔
297
  }
298

299
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
234,608,036✔
300
  QUERY_CHECK_CODE(code, lino, _end);
234,577,950✔
301

302
  int8_t type = pFuncParam->param.nType;
234,577,950✔
303
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
234,585,891✔
304
    int64_t v = pFuncParam->param.i;
453,442✔
305
    for (int32_t i = 0; i < numOfRows; ++i) {
991,885,574✔
306
      colDataSetInt64(pColInfo, i, &v);
991,434,836✔
307
    }
308
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
234,132,449✔
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) {
234,132,449✔
314
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
1,952✔
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:
234,130,587✔
326
  if (code != TSDB_CODE_SUCCESS) {
234,581,325✔
327
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
328
  }
329
  return code;
234,562,492✔
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];
149,124,215✔
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) {
580,998,622✔
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);
394,099,104✔
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 &&
394,096,637✔
384
            pOneExpr->pExpr->nodeType == QUERY_NODE_FUNCTION &&
45,700,245✔
385
            fmIsIndefiniteRowsFunc(pOneExpr->pExpr->_function.functionId)) {
14,126,788✔
386
          needDummyCol = true;
×
387
        }
388
        if (needDummyCol) {
394,096,545✔
389
          pInput->totalRows = pBlock->info.rows;
234,585,987✔
390
          pInput->numOfRows = pBlock->info.rows;
234,610,065✔
391
          pInput->startRowIndex = 0;
234,594,508✔
392
          pInput->blankFill = pBlock->info.blankFill;
234,604,408✔
393

394
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
234,601,988✔
395
          QUERY_CHECK_CODE(code, lino, _end);
226,629,803✔
396
        }
397
      }
398
    }
399
  }
400

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

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

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

417
  if (pCtx->scanFlag == PRE_SCAN) {
2,147,483,647✔
418
    return fmIsRepeatScanFunc(pCtx->functionId);
3,560,585✔
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,144,670✔
479
  int32_t code = TSDB_CODE_SUCCESS;
106,144,670✔
480
  int32_t lino = 0;
106,144,670✔
481
  int32_t numOfRows = pBlock->info.rows;
106,144,670✔
482

483
  SInputColumnInfoData* pInput = &pCtx->input;
105,891,819✔
484
  pInput->numOfRows = numOfRows;
106,117,001✔
485
  pInput->totalRows = numOfRows;
106,159,144✔
486

487
  if (pBlock->pBlockAgg != NULL) {
106,162,289✔
488
    pInput->colDataSMAIsSet = true;
106,161,653✔
489

490
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
212,334,698✔
491
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
106,169,851✔
492

493
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
106,169,243✔
494
        int32_t slotId = pFuncParam->pCol->slotId;
106,169,872✔
495
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
106,174,904✔
496
        if (pInput->pColumnDataAgg[j]->colId == -1) {
106,174,897✔
497
          pInput->colDataSMAIsSet = false;
19,814✔
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,171,752✔
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,048,188✔
513
  if (code != TSDB_CODE_SUCCESS) {
106,048,188✔
514
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
515
  }
516
  return code;
106,162,960✔
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) {
648,097,618✔
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
}
648,097,618✔
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};
190,445,471✔
598
  SColumnInfoData*   p = NULL;
190,450,719✔
599

600
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
190,437,601✔
601
  QUERY_CHECK_CODE(code, lino, _err);
190,444,107✔
602

603
  int32_t status = 0;
190,444,107✔
604
  code =
605
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
190,444,723✔
606
  QUERY_CHECK_CODE(code, lino, _err);
190,435,731✔
607

608
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
189,404,961✔
609
  QUERY_CHECK_CODE(code, lino, _err);
189,404,314✔
610

611
  if (pColMatchInfo != NULL) {
189,404,314✔
612
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
139,502,748✔
613
    for (int32_t i = 0; i < size; ++i) {
139,645,541✔
614
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
139,509,882✔
615
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
139,500,486✔
616
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
139,500,486✔
617
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
139,370,620✔
618
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
139,357,965✔
619
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
139,357,965✔
620
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
139,360,760✔
621
          QUERY_CHECK_CODE(code, lino, _err);
139,365,766✔
622
          break;
139,365,766✔
623
        }
624
      }
625
    }
626
  }
627
  code = blockDataCheck(pBlock);
189,402,991✔
628
  QUERY_CHECK_CODE(code, lino, _err);
189,412,731✔
629
_err:
190,443,501✔
630
  if (code != TSDB_CODE_SUCCESS) {
190,450,995✔
631
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1,030,770✔
632
  }
633
  colDataDestroy(p);
190,450,995✔
634
  taosMemoryFree(p);
190,428,170✔
635
  return code;
190,423,532✔
636
}
637

638
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
189,594,916✔
639
  int32_t code = TSDB_CODE_SUCCESS;
189,594,916✔
640
  int8_t* pIndicator = (int8_t*)p->pData;
189,594,916✔
641
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
189,601,091✔
642
    // here nothing needs to be done
643
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
119,342,421✔
644
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
49,252,511✔
645
    pBlock->info.rows = 0;
49,248,866✔
646
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
70,089,910✔
647
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
70,093,944✔
648
  } else {
UNCOV
649
    qError("unknown filter result type: %d", status);
×
650
  }
651
  return code;
189,597,677✔
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);
1,187,845✔
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,
983,824,602✔
753
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
754
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
983,824,602✔
755
  if (page == NULL) {
983,831,518✔
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);
983,831,518✔
761

762
  SqlFunctionCtx* pCtx = pSup->pCtx;
983,832,423✔
763
  SExprInfo*      pExprInfo = pSup->pExprInfo;
983,832,423✔
764
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
983,832,423✔
765

766
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
983,832,423✔
767
  if (pRow->numOfRows == 0) {
983,827,246✔
768
    releaseBufPage(pBuf, page);
×
769
    return;
×
770
  }
771

772
  int32_t size = pBlock->info.capacity;
983,827,246✔
773
  while (pBlock->info.rows + pRow->numOfRows > size) {
984,059,104✔
774
    size = size * 1.25;
232,102✔
775
  }
776

777
  int32_t code = blockDataEnsureCapacity(pBlock, size);
983,827,663✔
778
  if (TAOS_FAILED(code)) {
983,825,090✔
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);
983,825,090✔
785
  if (TAOS_FAILED(code)) {
983,826,168✔
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);
983,826,168✔
792
  pBlock->info.rows += pRow->numOfRows;
983,821,022✔
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;
16,656✔
866
    }
867
  }
868

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

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

882
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
122,175,118✔
883
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, STrueForInfo *pTrueForInfo) {
884
  int32_t         code = TSDB_CODE_SUCCESS;
122,175,118✔
885
  int32_t         lino = 0;
122,175,118✔
886
  SExprInfo*      pExprInfo = pSup->pExprInfo;
122,175,118✔
887
  int32_t         numOfExprs = pSup->numOfExprs;
122,177,256✔
888
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
122,177,286✔
889
  SqlFunctionCtx* pCtx = pSup->pCtx;
122,174,717✔
890

891
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
122,174,066✔
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;
116,294✔
908
      releaseBufPage(pBuf, page);
116,294✔
909
      continue;
116,294✔
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,318✔
914
             pRow->win.skey, pRow->win.ekey, pRow->nOrigRows);
915
      pGroupResInfo->index += 1;
45,318✔
916
      releaseBufPage(pBuf, page);
45,318✔
917
      continue;
45,318✔
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,050,729,363✔
926
          releaseBufPage(pBuf, page);
19,758,099✔
927
          break;
19,758,099✔
928
        }
929
      }
930
    }
931

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

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

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

952
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
122,383,231✔
953
         pBlock->info.id.groupId);
954
  pBlock->info.dataLoad = 1;
122,388,269✔
955
  code = blockDataUpdateTsWindow(pBlock, 0);
122,176,781✔
956
  QUERY_CHECK_CODE(code, lino, _end);
122,175,768✔
957

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

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

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

973
  blockDataCleanup(pBlock);
143,992,096✔
974
  if (!hasRemainResults(pGroupResInfo)) {
143,995,164✔
975
    return;
22,257,940✔
976
  }
977

978
  // clear the existed group id
979
  pBlock->info.id.groupId = 0;
121,734,448✔
980
  if (!pbInfo->mergeResultBlock) {
121,732,827✔
981
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
47,916,654✔
982
                       false, getTrueForInfo(pOperator));
983
  } else {
984
    while (hasRemainResults(pGroupResInfo)) {
137,374,226✔
985
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
73,815,765✔
986
                         true, getTrueForInfo(pOperator));
987
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
73,816,320✔
988
        break;
10,257,633✔
989
      }
990

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

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

1000
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
427,300,324✔
1001
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,661,586,265✔
1002
    SExprInfo* pExprInfo = &pExpr[i];
1,234,278,485✔
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,402,125,559✔
1005
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
1,160,481,884✔
1006
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
241,664,717✔
1007
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
140,315,207✔
1008
      }
1009
    }
1010

1011
    taosMemoryFree(pExprInfo->base.pParam);
1,234,324,705✔
1012
    taosMemoryFree(pExprInfo->pExpr);
1,234,302,230✔
1013
  }
1014
}
427,307,780✔
1015

1016
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
307,312,926✔
1017
  *defaultPgsz = 4096;
307,312,926✔
1018
  uint32_t last = *defaultPgsz;
307,335,655✔
1019
  while (*defaultPgsz < rowSize * 4) {
368,507,197✔
1020
    *defaultPgsz <<= 1u;
61,203,420✔
1021
    if (*defaultPgsz < last) {
61,202,442✔
1022
      return TSDB_CODE_INVALID_PARA;
×
1023
    }
1024
    last = *defaultPgsz;
61,196,549✔
1025
  }
1026

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

1038
  return 0;
307,273,244✔
1039
}
1040

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

1046
  pResultInfo->capacity = numOfRows;
657,272,833✔
1047
  pResultInfo->threshold = numOfRows * 0.75;
657,359,887✔
1048

1049
  if (pResultInfo->threshold == 0) {
657,174,397✔
1050
    pResultInfo->threshold = numOfRows;
2,178,843✔
1051
  }
1052
  pResultInfo->totalRows = 0;
657,199,207✔
1053
}
657,212,915✔
1054

1055
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
290,145,730✔
1056
  pInfo->pRes = pBlock;
290,145,730✔
1057
  initResultRowInfo(&pInfo->resultRowInfo);
290,172,005✔
1058
}
290,148,843✔
1059

1060
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
1,244,250,036✔
1061
  if (pCtx == NULL) {
1,244,250,036✔
1062
    return;
789,034,979✔
1063
  }
1064

1065
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,677,207,600✔
1066
    if (pExpr != NULL) {
1,221,999,632✔
1067
      SExprInfo* pExprInfo = &pExpr[i];
1,221,984,177✔
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,389,136,067✔
1070
          colDataDestroy(pCtx[i].input.pData[j]);
138,237,353✔
1071
          taosMemoryFree(pCtx[i].input.pData[j]);
138,238,251✔
1072
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
138,238,004✔
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,389,153,746✔
1078
    }
1079

1080
    if(pCtx[i].fpSet.cleanup) {
1,222,011,240✔
1081
      pCtx[i].fpSet.cleanup(&pCtx[i]);
923,639✔
1082
    }
1083

1084
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,222,049,496✔
1085
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,222,050,459✔
1086
    taosMemoryFree(pCtx[i].input.pData);
1,222,055,034✔
1087
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,221,998,242✔
1088

1089
    if (pCtx[i].udfName != NULL) {
1,221,989,747✔
1090
      taosMemoryFree(pCtx[i].udfName);
36,712✔
1091
    }
1092
  }
1093

1094
  taosMemoryFreeClear(pCtx);
455,207,968✔
1095
  return;
455,189,574✔
1096
}
1097

1098
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
452,672,356✔
1099
  pSup->pExprInfo = pExprInfo;
452,672,356✔
1100
  pSup->numOfExprs = numOfExpr;
452,708,181✔
1101
  if (pSup->pExprInfo != NULL) {
452,690,430✔
1102
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
350,968,909✔
1103
    if (pSup->pCtx == NULL) {
350,981,201✔
1104
      return terrno;
×
1105
    }
1106
  }
1107

1108
  return TSDB_CODE_SUCCESS;
452,650,310✔
1109
}
1110

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

1120
void cleanupExprSupp(SExprSupp* pSupp) {
1,127,604,946✔
1121
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
1,127,604,946✔
1122
  if (pSupp->pExprInfo != NULL) {
1,127,592,632✔
1123
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
392,968,062✔
1124
    taosMemoryFreeClear(pSupp->pExprInfo);
392,962,737✔
1125
  }
1126

1127
  if (pSupp->pFilterInfo != NULL) {
1,127,571,739✔
1128
    filterFreeInfo(pSupp->pFilterInfo);
101,917,606✔
1129
    pSupp->pFilterInfo = NULL;
101,914,083✔
1130
  }
1131

1132
  taosMemoryFree(pSupp->rowEntryInfoOffset);
1,127,615,320✔
1133
  memset(pSupp, 0, sizeof(SExprSupp));
1,127,617,153✔
1134
}
1,127,617,153✔
1135

1136
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
116,348,427✔
1137
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
116,348,427✔
1138
  if (pSupp->pExprInfo != NULL) {
116,349,243✔
1139
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
32,044,825✔
1140
    taosMemoryFreeClear(pSupp->pExprInfo);
32,044,169✔
1141
  }
1142

1143
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
116,347,503✔
1144
  pSupp->numOfExprs = 0;
116,348,153✔
1145
  pSupp->hasWindowOrGroup = false;
116,352,639✔
1146
  pSupp->pCtx = NULL;
116,349,053✔
1147
}
116,341,429✔
1148

1149
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
293,515,294✔
1150
  blockDataDestroy(pInfo->pRes);
293,515,294✔
1151
  pInfo->pRes = NULL;
293,514,311✔
1152
}
293,517,019✔
1153

1154
bool groupbyTbname(SNodeList* pGroupList) {
256,922,471✔
1155
  bool   bytbname = false;
256,922,471✔
1156
  SNode* pNode = NULL;
256,922,471✔
1157
  FOREACH(pNode, pGroupList) {
264,338,357✔
1158
    if (pNode->type == QUERY_NODE_FUNCTION) {
40,244,588✔
1159
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
32,827,398✔
1160
      break;
32,828,464✔
1161
    }
1162
  }
1163
  return bytbname;
256,921,920✔
1164
}
1165

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

1175
      *pParam = pInserterParam;
490,331✔
1176
      break;
490,331✔
1177
    }
1178
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,847,923✔
1179
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,847,923✔
1180
      if (NULL == pDeleterParam) {
1,836,031✔
1181
        return terrno;
×
1182
      }
1183

1184
      SArray* pInfoList = NULL;
1,836,031✔
1185
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,835,446✔
1186
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,836,031✔
UNCOV
1187
        taosMemoryFree(pDeleterParam);
×
1188
        return code;
×
1189
      }
1190

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

1194
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,835,446✔
1195

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

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

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

1226
      *pParam = pDeleterParam;
1,836,031✔
1227
      break;
1,836,616✔
1228
    }
1229
    default:
370,072,673✔
1230
      break;
370,072,673✔
1231
  }
1232

1233
  return TSDB_CODE_SUCCESS;
372,436,887✔
1234
}
1235

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

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

1250
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
136,393,921✔
1251
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
136,393,921✔
1252
  for (int32_t i = 0; i < childrenNum; ++i) {
138,154,556✔
1253
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
1,763,167✔
1254
    freeOperatorParam(pChild, type);
1,763,167✔
1255
  }
1256

1257
  taosArrayDestroy(pParam->pChildren);
136,391,389✔
1258
  pParam->pChildren = NULL;
136,392,677✔
1259

1260
  taosMemoryFreeClear(pParam->value);
136,393,306✔
1261

1262
  taosMemoryFree(pParam);
136,387,079✔
1263
}
136,383,948✔
1264

1265
void freeExchangeGetBasicOperatorParam(void* pParam) {
24,275,499✔
1266
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
24,275,499✔
1267
  if (pBasic->uidList) {
24,275,499✔
1268
    taosArrayDestroy(pBasic->uidList);
19,967,364✔
1269
    pBasic->uidList = NULL;
19,967,364✔
1270
  }
1271
  if (pBasic->orgTbInfo) {
24,275,499✔
1272
    taosArrayDestroy(pBasic->orgTbInfo->colMap);
11,051,333✔
1273
    taosMemoryFreeClear(pBasic->orgTbInfo);
11,051,333✔
1274
  }
1275
  if (pBasic->batchOrgTbInfo) {
24,275,499✔
1276
    taosArrayDestroyEx(pBasic->batchOrgTbInfo, destroySOrgTbInfo);
3,222,997✔
1277
    pBasic->batchOrgTbInfo = NULL;
3,222,997✔
1278
  }
1279
  if (pBasic->tagList) {
24,275,499✔
1280
    taosArrayDestroyEx(pBasic->tagList, destroyTagVal);
849,362✔
1281
    pBasic->tagList = NULL;
849,362✔
1282
  }
1283
}
24,275,499✔
1284

1285
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
23,074,939✔
1286
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
23,074,939✔
1287
  if (pExcParam->multiParams) {
23,074,939✔
1288
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
2,273,467✔
1289
    tSimpleHashSetFreeFp(pExcBatch->pBatchs, freeExchangeGetBasicOperatorParam);
2,273,467✔
1290
    tSimpleHashCleanup(pExcBatch->pBatchs);
2,273,467✔
1291
  } else {
1292
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
20,801,472✔
1293
  }
1294

1295
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
23,074,939✔
1296
}
23,074,939✔
1297

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

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

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

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

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

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

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

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

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

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

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

1324
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
84,967,251✔
1325
  STableScanOperatorParam* pTableScanParam =
84,967,251✔
1326
    (STableScanOperatorParam*)pParam->value;
1327
  taosArrayDestroy(pTableScanParam->pUidList);
84,969,779✔
1328
  if (pTableScanParam->pOrgTbInfo) {
84,972,832✔
1329
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
74,200,960✔
1330
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
74,200,960✔
1331
  }
1332
  if (pTableScanParam->pBatchTbInfo) {
84,973,428✔
1333
    for (int32_t i = 0;
2,642,615✔
1334
      i < taosArrayGetSize(pTableScanParam->pBatchTbInfo); ++i) {
7,643,326✔
1335
      SOrgTbInfo* pOrgTbInfo =
1336
        (SOrgTbInfo*)taosArrayGet(pTableScanParam->pBatchTbInfo, i);
5,000,711✔
1337
      taosArrayDestroy(pOrgTbInfo->colMap);
5,000,711✔
1338
    }
1339
    taosArrayDestroy(pTableScanParam->pBatchTbInfo);
2,642,615✔
1340
    pTableScanParam->pBatchTbInfo = NULL;
2,642,615✔
1341
  }
1342
  if (pTableScanParam->pTagList) {
84,975,933✔
1343
    for (int32_t i = 0;
849,362✔
1344
      i < taosArrayGetSize(pTableScanParam->pTagList); ++i) {
5,260,344✔
1345
      STagVal* pTagVal =
1346
        (STagVal*)taosArrayGet(pTableScanParam->pTagList, i);
4,410,982✔
1347
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
4,410,982✔
1348
        taosMemoryFreeClear(pTagVal->pData);
1,914,774✔
1349
      }
1350
    }
1351
    taosArrayDestroy(pTableScanParam->pTagList);
849,362✔
1352
    pTableScanParam->pTagList = NULL;
849,362✔
1353
  }
1354
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
84,973,445✔
1355
}
84,960,967✔
1356

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

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

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

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

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

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

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

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

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

1402
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
814,351,069✔
1403
  if (NULL == pParam || pParam->reUse) {
814,351,069✔
1404
    return;
677,956,844✔
1405
  }
1406

1407
  switch (pParam->opType) {
136,397,375✔
1408
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
23,074,939✔
1409
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
23,074,939✔
1410
      break;
23,074,939✔
1411
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
7,796,514✔
1412
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
7,796,514✔
1413
      break;
7,796,514✔
1414
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
3,898,257✔
1415
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
3,898,257✔
1416
      break;
3,898,257✔
1417
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
84,974,041✔
1418
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
1419
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
84,974,041✔
1420
      break;
84,961,596✔
1421
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
4,677,292✔
1422
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
4,677,292✔
1423
      break;
4,677,292✔
1424
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
6,154,902✔
1425
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
6,154,902✔
1426
      break;
6,154,902✔
1427
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
4,822,173✔
1428
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
1429
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
1430
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL:
1431
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
1432
      type == OP_GET_PARAM ? freeMergeGetOperatorParam(pParam) : freeMergeNotifyOperatorParam(pParam);
4,822,173✔
1433
      break;
4,822,173✔
1434
    case QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW:
999,476✔
1435
      type == OP_GET_PARAM ? freeExternalWindowGetOperatorParam(pParam) : freeExternalWindowNotifyOperatorParam(pParam);
999,476✔
1436
      break;
999,476✔
1437
    case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL:
×
1438
      type == OP_GET_PARAM ? freeDynQueryCtrlGetOperatorParam(pParam) : freeDynQueryCtrlNotifyOperatorParam(pParam);
×
1439
      break;
×
1440
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
×
1441
      type == OP_GET_PARAM ? freeInterpFuncGetOperatorParam(pParam) : freeInterpFuncNotifyOperatorParam(pParam);
×
1442
      break;
×
1443
    default:
32✔
1444
      qError("%s unsupported op %d param, param type %d, param:%p value:%p children:%p reuse:%d",
32✔
1445
             __func__, pParam->opType, type, pParam, pParam->value, pParam->pChildren, pParam->reUse);
1446
      break;
×
1447
  }
1448
}
1449

1450
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,749,373,747✔
1451
  SOperatorParam**  ppParam = NULL;
1,749,373,747✔
1452
  SOperatorParam*** pppDownstramParam = NULL;
1,749,373,747✔
1453
  switch (type) {
1,749,373,747✔
1454
    case OP_GET_PARAM:
928,153,163✔
1455
      ppParam = &pOperator->pOperatorGetParam;
928,153,163✔
1456
      pppDownstramParam = &pOperator->pDownstreamGetParams;
928,161,500✔
1457
      break;
928,156,384✔
1458
    case OP_NOTIFY_PARAM:
821,254,363✔
1459
      ppParam = &pOperator->pOperatorNotifyParam;
821,254,363✔
1460
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
821,251,753✔
1461
      break;
821,259,097✔
1462
    default:
×
1463
      return;
×
1464
  }
1465

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

1473
  if (*pppDownstramParam) {
1,749,387,871✔
1474
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
133,466,210✔
1475
      if ((*pppDownstramParam)[i]) {
23,441,552✔
1476
        qDebug("%s free downstream param, operator:%s type:%d idx:%d downstream:%s paramType:%d param:%p", __func__,
×
1477
               pOperator->name, pOperator->operatorType, i, pOperator->pDownstream[i]->name, type,
1478
               (*pppDownstramParam)[i]);
1479
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1480
        (*pppDownstramParam)[i] = NULL;
×
1481
      }
1482
    }
1483
    if (allFree) {
110,025,287✔
1484
      taosMemoryFreeClear(*pppDownstramParam);
21,551,957✔
1485
    }
1486
  }
1487
}
1488

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

1493
  int32_t code = 0;
1,126,645,002✔
1494
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
1,126,645,002✔
1495
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
19,302,395✔
1496
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
38,604,790✔
1497
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
19,302,395✔
1498
    if (clearParam && (code == 0)) {
19,301,879✔
1499
      qDebug("%s clear downstream param, operator:%s type:%d idx:%d downstream:%s param:%p", __func__,
7,449,024✔
1500
             pOperator->name, pOperator->operatorType, idx, pOperator->pDownstream[idx]->name,
1501
             pOperator->pDownstreamGetParams[idx]);
1502
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
7,449,024✔
1503
      pOperator->pDownstreamGetParams[idx] = NULL;
7,449,024✔
1504
    }
1505

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

1512
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
1,107,315,220✔
1513
  if (code) {
1,104,619,454✔
1514
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1515
  }
1516
  return code;
1,104,623,080✔
1517
}
1518

1519
bool compareVal(const char* v, const SStateKeys* pKey) {
2,147,483,647✔
1520
  if (IS_VAR_DATA_TYPE(pKey->type)) {
2,147,483,647✔
1521
    if (IS_STR_DATA_BLOB(pKey->type)) {
320,773✔
1522
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
1523
        return false;
×
1524
      } else {
1525
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
1526
      }
1527
    } else {
1528
      if (varDataLen(v) != varDataLen(pKey->pData)) {
320,060✔
1529
        return false;
×
1530
      } else {
1531
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
320,060✔
1532
      }
1533
    }
1534
  } else {
1535
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
2,147,483,647✔
1536
  }
1537
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc