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

taosdata / TDengine / #4806

17 Oct 2025 09:36AM UTC coverage: 61.094% (-0.2%) from 61.259%
#4806

push

travis-ci

web-flow
Merge da5cd734f into 21184b20f

155401 of 324487 branches covered (47.89%)

Branch coverage included in aggregate %.

72 of 84 new or added lines in 6 files covered. (85.71%)

778 existing lines in 110 files now uncovered.

207559 of 269610 relevant lines covered (76.98%)

127253104.64 hits per line

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

64.18
/source/libs/executor/src/executorInt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

108
      pData = getNewBufPage(pResultBuf, &pageId);
411,356,280✔
109
      if (pData == NULL) {
411,343,867!
110
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
111
        return NULL;
×
112
      }
113
      pData->num = sizeof(SFilePage);
411,343,867✔
114
    }
115
  }
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

234
  int32_t code = colInfoDataEnsureCapacity(pColData, 6, false);
5,440,329✔
235
  if (code != TSDB_CODE_SUCCESS) {
5,442,207!
236
    return code;
×
237
  }
238
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
5,442,207✔
239
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
5,438,441✔
240

241
  int64_t interval = 0;
5,438,666✔
242
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
243
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
5,441,454✔
244
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
5,435,753✔
245

246
  interval = -1;
5,436,281✔
247
  colDataSetInt64(pColData, 5,  &interval);
248
  return TSDB_CODE_SUCCESS;
5,438,872✔
249
}
250

251
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
7,797,251✔
252
  int32_t         code = TSDB_CODE_SUCCESS;
7,797,251✔
253
  int32_t         lino = 0;
7,797,251✔
254
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
7,797,251✔
255
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
15,609,472✔
256
    pCtx[i].order = order;
7,812,221✔
257
    pCtx[i].input.numOfRows = pBlock->info.rows;
7,812,221✔
258
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
7,812,221✔
259
    QUERY_CHECK_CODE(code, lino, _end);
7,811,606!
260
    pCtx[i].pSrcBlock = pBlock;
7,811,606✔
261
    pCtx[i].scanFlag = scanFlag;
7,811,811✔
262
  }
263

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

271
int32_t setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
818,347,022✔
272
                          bool createDummyCol) {
273
  if (pBlock->pBlockAgg != NULL) {
818,347,022✔
274
    return doSetInputDataBlockInfo(pExprSup, pBlock, order, scanFlag);
7,797,251✔
275
  } else {
276
    return doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
810,602,164✔
277
  }
278
}
279

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

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

293
    pInput->pData[paramIndex] = pColInfo;
143,132✔
294
  } else {
295
    pColInfo = pInput->pData[paramIndex];
16,550,740✔
296
  }
297

298
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
16,691,708✔
299
  QUERY_CHECK_CODE(code, lino, _end);
16,684,751!
300

301
  int8_t type = pFuncParam->param.nType;
16,684,751✔
302
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
16,686,374!
303
    int64_t v = pFuncParam->param.i;
79,956✔
304
    for (int32_t i = 0; i < numOfRows; ++i) {
1,033,732✔
305
      colDataSetInt64(pColInfo, i, &v);
952,694✔
306
    }
307
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,606,418!
308
    double v = pFuncParam->param.d;
×
309
    for (int32_t i = 0; i < numOfRows; ++i) {
×
310
      colDataSetDouble(pColInfo, i, &v);
×
311
    }
312
  } else if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_GEOMETRY) {
16,606,418!
313
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
1,623!
314
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
315

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

324
_end:
16,604,795✔
325
  if (code != TSDB_CODE_SUCCESS) {
16,685,833!
326
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
327
  }
328
  return code;
16,678,259✔
329
}
330

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

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

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

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

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

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

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

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

368
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == tsParamIdx)) {
2,147,483,647✔
369
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
2,147,483,647✔
370
        }
371
        if (hasPk && (j == pkParamIdx)) {
2,147,483,647✔
372
          pInput->pPrimaryKey = pInput->pData[j];
5,822,442✔
373
        }
374
        QUERY_CHECK_CONDITION((pInput->pData[j] != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
2,147,483,647!
375
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
311,972,423✔
376
        // todo avoid case: top(k, 12), 12 is the value parameter.
377
        // sum(11), 11 is also the value parameter.
378
        if (createDummyCol && pOneExpr->base.numOfParams == 1) {
148,309,137✔
379
          pInput->totalRows = pBlock->info.rows;
16,692,249✔
380
          pInput->numOfRows = pBlock->info.rows;
16,693,331✔
381
          pInput->startRowIndex = 0;
16,693,331✔
382
          pInput->blankFill = pBlock->info.blankFill;
16,693,331!
383

384
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
16,693,331✔
385
          QUERY_CHECK_CODE(code, lino, _end);
628,227!
386
        }
387
      }
388
    }
389
  }
390

391
_end:
810,658,056✔
392
  if (code != TSDB_CODE_SUCCESS) {
810,658,056!
393
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
394
  }
395
  return code;
810,539,107✔
396
}
397

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

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

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

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

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

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

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

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

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

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

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

465
  return TSDB_CODE_SUCCESS;
×
466
}
467

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

473
  SInputColumnInfoData* pInput = &pCtx->input;
7,812,016✔
474
  pInput->numOfRows = numOfRows;
7,812,016✔
475
  pInput->totalRows = numOfRows;
7,812,016✔
476

477
  if (pBlock->pBlockAgg != NULL) {
7,812,016!
478
    pInput->colDataSMAIsSet = true;
7,812,221✔
479

480
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
15,624,032✔
481
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
7,812,221✔
482

483
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,812,016!
484
        int32_t slotId = pFuncParam->pCol->slotId;
7,812,016✔
485
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
7,812,016✔
486
        if (pInput->pColumnDataAgg[j]->colId == -1) {
7,811,811✔
487
          pInput->colDataSMAIsSet = false;
31,864✔
488
        }
489

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

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

509
/////////////////////////////////////////////////////////////////////////////////////////////
510
STimeWindow getAlignQueryTimeWindow(const SInterval* pInterval, int64_t key) {
25,118,524✔
511
  STimeWindow win = {0};
25,118,524✔
512
  win.skey = taosTimeTruncate(key, pInterval);
25,118,524✔
513

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

523
  return win;
25,127,436✔
524
}
525

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

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

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

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

566
void clearResultRowInitFlag(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
544,010,427✔
567
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
568
    SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
2,147,483,647✔
569
    if (pResInfo == NULL) {
2,147,483,647!
570
      continue;
×
571
    }
572

573
    pResInfo->initialized = false;
2,147,483,647✔
574
    pResInfo->numOfRes = 0;
2,147,483,647✔
575
    pResInfo->isNullRes = 0;
2,147,483,647✔
576
    pResInfo->complete = false;
2,147,483,647✔
577
  }
578
}
543,943,971✔
579

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

587
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
139,263,384✔
588
  SColumnInfoData*   p = NULL;
139,258,820✔
589

590
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
139,265,007✔
591
  QUERY_CHECK_CODE(code, lino, _err);
139,265,615!
592

593
  int32_t status = 0;
139,265,615✔
594
  code =
595
      filterExecute(pFilterInfo, pBlock, pRet != NULL ? pRet : &p, NULL, param1.numOfCols, &status);
139,266,612✔
596
  QUERY_CHECK_CODE(code, lino, _err);
139,212,425✔
597

598
  code = extractQualifiedTupleByFilterResult(pBlock, pRet != NULL ? *pRet : p, status);
139,212,182✔
599
  QUERY_CHECK_CODE(code, lino, _err);
139,243,696!
600

601
  if (pColMatchInfo != NULL) {
139,243,696✔
602
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
85,326,570✔
603
    for (int32_t i = 0; i < size; ++i) {
85,399,656✔
604
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
85,336,178✔
605
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
85,320,990!
606
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
85,320,990✔
607
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
85,280,935✔
608
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
85,237,156!
609
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
85,237,156✔
610
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
85,260,725✔
611
          QUERY_CHECK_CODE(code, lino, _err);
85,257,169!
612
          break;
85,257,169✔
613
        }
614
      }
615
    }
616
  }
617
  code = blockDataCheck(pBlock);
139,237,773✔
618
  QUERY_CHECK_CODE(code, lino, _err);
139,246,101!
619
_err:
139,246,344✔
620
  if (code != TSDB_CODE_SUCCESS) {
139,286,497✔
621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
243!
622
  }
623
  colDataDestroy(p);
139,286,497✔
624
  taosMemoryFree(p);
139,262,722✔
625
  return code;
139,228,791✔
626
}
627

628
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
139,505,268✔
629
  int32_t code = TSDB_CODE_SUCCESS;
139,505,268✔
630
  int8_t* pIndicator = (int8_t*)p->pData;
139,505,268✔
631
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
139,528,979✔
632
    // here nothing needs to be done
633
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
80,150,501✔
634
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
36,819,841✔
635
    pBlock->info.rows = 0;
36,813,296✔
636
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
43,330,660!
637
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
43,336,514✔
638
  } else {
UNCOV
639
    qError("unknown filter result type: %d", status);
×
640
  }
641
  return code;
139,526,557✔
642
}
643

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

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

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

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

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

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

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

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

722
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);
579,932,784✔
723

724
  SqlFunctionCtx* pCtx = pSup->pCtx;
579,932,784✔
725
  SExprInfo*      pExprInfo = pSup->pExprInfo;
579,932,784✔
726
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
579,932,784✔
727

728
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
579,932,784✔
729
  if (pRow->numOfRows == 0) {
579,930,616!
730
    releaseBufPage(pBuf, page);
×
731
    return;
×
732
  }
733

734
  int32_t size = pBlock->info.capacity;
579,930,616✔
735
  while (pBlock->info.rows + pRow->numOfRows > size) {
580,118,141✔
736
    size = size * 1.25;
187,525!
737
  }
738

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

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

753
  releaseBufPage(pBuf, page);
579,929,261✔
754
  pBlock->info.rows += pRow->numOfRows;
579,931,158✔
755
}
756

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

766
  size_t  keyLen = 0;
2,147,483,647✔
767
  int32_t numOfRows = tSimpleHashGetSize(pHashmap);
2,147,483,647✔
768

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

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

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

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

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

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

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

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

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

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

831
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
2,147,483,647✔
832
         pBlock->info.id.groupId);
833
  pBlock->info.dataLoad = 1;
2,147,483,647✔
834
  code = blockDataUpdateTsWindow(pBlock, 0);
2,147,483,647✔
835
  QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647!
836

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

844
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
68,875,865✔
845
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup, int64_t minWindowSize) {
846
  int32_t         code = TSDB_CODE_SUCCESS;
68,875,865✔
847
  int32_t         lino = 0;
68,875,865✔
848
  SExprInfo*      pExprInfo = pSup->pExprInfo;
68,875,865✔
849
  int32_t         numOfExprs = pSup->numOfExprs;
68,876,983✔
850
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
68,876,400✔
851
  SqlFunctionCtx* pCtx = pSup->pCtx;
68,875,561✔
852

853
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
68,876,483✔
854

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

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

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

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

882
    if (!ignoreGroup) {
2,147,483,647✔
883
      if (pBlock->info.id.groupId == 0) {
1,969,736,914✔
884
        pBlock->info.id.groupId = pPos->groupId;
994,756,102✔
885
      } else {
886
        // current value belongs to different group, it can't be packed into one datablock
887
        if (pBlock->info.id.groupId != pPos->groupId) {
975,235,365✔
888
          releaseBufPage(pBuf, page);
12,554,350✔
889
          break;
12,553,840✔
890
        }
891
      }
892
    }
893

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

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

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

914
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
68,868,720✔
915
         pBlock->info.id.groupId);
916
  pBlock->info.dataLoad = 1;
68,876,093✔
917
  code = blockDataUpdateTsWindow(pBlock, 0);
68,883,040✔
918
  QUERY_CHECK_CODE(code, lino, _end);
68,881,931!
919

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

927
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
79,778,518✔
928
                            SDiskbasedBuf* pBuf) {
929
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
79,778,518✔
930
  SSDataBlock*   pBlock = pbInfo->pRes;
79,782,295✔
931

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

935
  blockDataCleanup(pBlock);
79,778,030✔
936
  if (!hasRemainResults(pGroupResInfo)) {
79,783,438✔
937
    return;
10,931,091✔
938
  }
939

940
  // clear the existed group id
941
  pBlock->info.id.groupId = 0;
68,844,584✔
942
  if (!pbInfo->mergeResultBlock) {
68,845,460✔
943
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
31,399,082✔
944
                       false, getMinWindowSize(pOperator));
945
  } else {
946
    while (hasRemainResults(pGroupResInfo)) {
70,246,184✔
947
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
37,443,531✔
948
                         true, getMinWindowSize(pOperator));
949
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
37,445,393✔
950
        break;
4,641,660✔
951
      }
952

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

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

962
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
271,366,421✔
963
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,176,618,501✔
964
    SExprInfo* pExprInfo = &pExpr[i];
905,240,525✔
965
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
1,939,515,921✔
966
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
1,034,258,946✔
967
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
864,465,587!
968
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
169,808,227✔
969
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
95,667,001✔
970
      }
971
    }
972

973
    taosMemoryFree(pExprInfo->base.pParam);
905,287,799✔
974
    taosMemoryFree(pExprInfo->pExpr);
905,292,089✔
975
  }
976
}
271,377,976✔
977

978
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, int64_t* defaultBufsz) {
187,665,409✔
979
  *defaultPgsz = 4096;
187,665,409✔
980
  uint32_t last = *defaultPgsz;
187,694,752✔
981
  while (*defaultPgsz < rowSize * 4) {
222,906,938✔
982
    *defaultPgsz <<= 1u;
35,231,494✔
983
    if (*defaultPgsz < last) {
35,228,244!
984
      return TSDB_CODE_INVALID_PARA;
×
985
    }
986
    last = *defaultPgsz;
35,231,076✔
987
  }
988

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

1000
  return 0;
187,591,831✔
1001
}
1002

1003
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
361,696,667✔
1004
  if (numOfRows == 0) {
361,696,667!
1005
    numOfRows = 4096;
×
1006
  }
1007

1008
  pResultInfo->capacity = numOfRows;
361,696,667✔
1009
  pResultInfo->threshold = numOfRows * 0.75;
361,793,545!
1010

1011
  if (pResultInfo->threshold == 0) {
361,720,203✔
1012
    pResultInfo->threshold = numOfRows;
663,337✔
1013
  }
1014
  pResultInfo->totalRows = 0;
361,762,277✔
1015
}
361,709,301✔
1016

1017
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
146,969,027✔
1018
  pInfo->pRes = pBlock;
146,969,027✔
1019
  initResultRowInfo(&pInfo->resultRowInfo);
147,005,702✔
1020
}
146,945,161✔
1021

1022
void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
747,676,948✔
1023
  if (pCtx == NULL) {
747,676,948✔
1024
    return;
463,866,711✔
1025
  }
1026

1027
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,178,963,303✔
1028
    if (pExpr != NULL) {
895,147,416✔
1029
      SExprInfo* pExprInfo = &pExpr[i];
895,136,222✔
1030
      for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
1,917,887,658✔
1031
        if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
1,022,741,997✔
1032
          colDataDestroy(pCtx[i].input.pData[j]);
93,346,054✔
1033
          taosMemoryFree(pCtx[i].input.pData[j]);
93,345,200✔
1034
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
93,342,558✔
1035
        }
1036
      }
1037
    }
1038
    for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) {
1,917,900,820✔
1039
      taosVariantDestroy(&pCtx[i].param[j].param);
1,022,746,074✔
1040
    }
1041

1042
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
895,170,093!
1043
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
895,175,117!
1044
    taosMemoryFree(pCtx[i].input.pData);
895,177,964✔
1045
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
895,139,678✔
1046

1047
    if (pCtx[i].udfName != NULL) {
895,143,189✔
1048
      taosMemoryFree(pCtx[i].udfName);
14,606!
1049
    }
1050
  }
1051

1052
  taosMemoryFreeClear(pCtx);
283,815,887✔
1053
  return;
283,799,578✔
1054
}
1055

1056
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
277,030,270✔
1057
  pSup->pExprInfo = pExprInfo;
277,030,270✔
1058
  pSup->numOfExprs = numOfExpr;
277,054,667✔
1059
  if (pSup->pExprInfo != NULL) {
277,051,616✔
1060
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
213,361,250✔
1061
    if (pSup->pCtx == NULL) {
213,320,650!
1062
      return terrno;
×
1063
    }
1064
  }
1065

1066
  return TSDB_CODE_SUCCESS;
276,940,756✔
1067
}
1068

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

1078
void cleanupExprSupp(SExprSupp* pSupp) {
648,148,897✔
1079
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
648,148,897✔
1080
  if (pSupp->pExprInfo != NULL) {
648,155,923✔
1081
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
224,781,527✔
1082
    taosMemoryFreeClear(pSupp->pExprInfo);
224,783,073!
1083
  }
1084

1085
  if (pSupp->pFilterInfo != NULL) {
648,154,313✔
1086
    filterFreeInfo(pSupp->pFilterInfo);
45,836,158✔
1087
    pSupp->pFilterInfo = NULL;
45,831,608✔
1088
  }
1089

1090
  taosMemoryFree(pSupp->rowEntryInfoOffset);
648,155,917✔
1091
  memset(pSupp, 0, sizeof(SExprSupp));
648,147,776!
1092
}
648,147,776✔
1093

1094
void cleanupExprSuppWithoutFilter(SExprSupp* pSupp) {
99,508,311✔
1095
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
99,508,311✔
1096
  if (pSupp->pExprInfo != NULL) {
99,506,898✔
1097
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
44,752,006✔
1098
    taosMemoryFreeClear(pSupp->pExprInfo);
44,755,414!
1099
  }
1100

1101
  taosMemoryFreeClear(pSupp->rowEntryInfoOffset);
99,509,877✔
1102
  pSupp->numOfExprs = 0;
99,505,698✔
1103
  pSupp->hasWindowOrGroup = false;
99,508,516✔
1104
  pSupp->pCtx = NULL;
99,505,019✔
1105
}
99,509,506✔
1106

1107
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
149,420,348✔
1108
  blockDataDestroy(pInfo->pRes);
149,420,348✔
1109
  pInfo->pRes = NULL;
149,422,661✔
1110
}
149,423,760✔
1111

1112
bool groupbyTbname(SNodeList* pGroupList) {
150,593,716✔
1113
  bool   bytbname = false;
150,593,716✔
1114
  SNode* pNode = NULL;
150,593,716✔
1115
  FOREACH(pNode, pGroupList) {
153,513,927✔
1116
    if (pNode->type == QUERY_NODE_FUNCTION) {
18,704,835✔
1117
      bytbname = (strcmp(((struct SFunctionNode*)pNode)->functionName, "tbname") == 0);
15,784,909!
1118
      break;
15,785,003✔
1119
    }
1120
  }
1121
  return bytbname;
150,587,656✔
1122
}
1123

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

1133
      *pParam = pInserterParam;
81,746✔
1134
      break;
81,746✔
1135
    }
1136
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
3,113,163✔
1137
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
3,113,163!
1138
      if (NULL == pDeleterParam) {
3,086,807!
1139
        return terrno;
×
1140
      }
1141

1142
      SArray* pInfoList = NULL;
3,086,807✔
1143
      int32_t code = getTableListInfo(pTask, &pInfoList);
3,085,342✔
1144
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
3,086,070!
1145
        taosMemoryFree(pDeleterParam);
×
1146
        return code;
×
1147
      }
1148

1149
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
3,087,544✔
1150
      taosArrayDestroy(pInfoList);
3,084,605✔
1151

1152
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
3,081,684✔
1153

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

1163
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
3,084,614✔
1164
      if (NULL == pDeleterParam->pUidList) {
3,084,605!
1165
        taosMemoryFree(pDeleterParam);
×
1166
        return terrno;
×
1167
      }
1168

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

1184
      *pParam = pDeleterParam;
3,083,149✔
1185
      break;
3,089,009✔
1186
    }
1187
    default:
207,409,532✔
1188
      break;
207,409,532✔
1189
  }
1190

1191
  return TSDB_CODE_SUCCESS;
210,595,910✔
1192
}
1193

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

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

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

1215
  taosArrayDestroy(pParam->pChildren);
86,884,730✔
1216
  pParam->pChildren = NULL;
86,884,730✔
1217

1218
  taosMemoryFreeClear(pParam->value);
86,884,730!
1219

1220
  taosMemoryFree(pParam);
86,884,730!
1221
}
86,884,730✔
1222

1223
void freeExchangeGetBasicOperatorParam(void* pParam) {
11,169,047✔
1224
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
11,169,047✔
1225
  taosArrayDestroy(pBasic->uidList);
11,169,047✔
1226
  if (pBasic->colMap) {
11,169,047✔
1227
    taosArrayDestroy(pBasic->colMap->colMap);
7,752,356✔
1228
    taosMemoryFreeClear(pBasic->colMap);
7,752,356!
1229
  }
1230
}
11,169,047✔
1231

1232
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
11,017,099✔
1233
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
11,017,099✔
1234
  if (pExcParam->multiParams) {
11,017,099!
1235
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
188,364✔
1236
    tSimpleHashCleanup(pExcBatch->pBatchs);
188,364✔
1237
  } else {
1238
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
10,828,735✔
1239
  }
1240

1241
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
11,017,099✔
1242
}
11,017,099✔
1243

1244
void freeExchangeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1245

1246
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
4,506,868✔
1247

1248
void freeGroupCacheNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1249

1250
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
2,253,434✔
1251

1252
void freeMergeJoinNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1253

1254
void freeTagScanGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
4,509,102✔
1255

1256
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
61,525,580✔
1257
  STableScanOperatorParam* pTableScanParam = (STableScanOperatorParam*)pParam->value;
61,525,580✔
1258
  taosArrayDestroy(pTableScanParam->pUidList);
61,525,580✔
1259
  if (pTableScanParam->pOrgTbInfo) {
61,525,580✔
1260
    taosArrayDestroy(pTableScanParam->pOrgTbInfo->colMap);
60,822,060✔
1261
    taosMemoryFreeClear(pTableScanParam->pOrgTbInfo);
60,821,522!
1262
  }
1263
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
61,525,580✔
1264
}
61,525,580✔
1265

1266
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1267

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

1270
void freeOpParamItem(void* pItem) {
10,825,003✔
1271
  SOperatorParam* pParam = *(SOperatorParam**)pItem;
10,825,003✔
1272
  pParam->reUse = false;
10,825,003✔
1273
  freeOperatorParam(pParam, OP_GET_PARAM);
10,825,003✔
1274
}
10,825,003✔
1275

1276
void freeVirtualTableScanGetOperatorParam(SOperatorParam* pParam) {
3,072,647✔
1277
  SVTableScanOperatorParam* pVTableScanParam = (SVTableScanOperatorParam*)pParam->value;
3,072,647✔
1278
  taosArrayDestroyEx(pVTableScanParam->pOpParamArray, freeOpParamItem);
3,072,647✔
1279
  freeOpParamItem(&pVTableScanParam->pTagScanOp);
3,072,647✔
1280
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
3,072,647✔
1281
}
3,072,647✔
1282

1283
void freeVTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1284

1285
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
467,635,427✔
1286
  if (NULL == pParam || pParam->reUse) {
467,635,427!
1287
    return;
380,743,410✔
1288
  }
1289

1290
  switch (pParam->opType) {
86,892,017!
1291
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
11,017,099✔
1292
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
11,017,099!
1293
      break;
11,017,099✔
1294
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
4,506,868✔
1295
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
4,506,868!
1296
      break;
4,506,868✔
1297
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
2,253,434✔
1298
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
2,253,434!
1299
      break;
2,253,434✔
1300
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
61,525,580✔
1301
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
61,525,580!
1302
      break;
61,525,580✔
1303
    case QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN:
3,072,647✔
1304
      type == OP_GET_PARAM ? freeVirtualTableScanGetOperatorParam(pParam) : freeVTableScanNotifyOperatorParam(pParam);
3,072,647!
1305
      break;
3,072,647✔
1306
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
4,509,102✔
1307
      type == OP_GET_PARAM ? freeTagScanGetOperatorParam(pParam) : freeTagScanNotifyOperatorParam(pParam);
4,509,102!
1308
      break;
4,509,102✔
1309
    default:
×
1310
      qError("unsupported op %d param, type %d", pParam->opType, type);
×
1311
      break;
×
1312
  }
1313
}
1314

1315
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
1,149,367,141✔
1316
  SOperatorParam**  ppParam = NULL;
1,149,367,141✔
1317
  SOperatorParam*** pppDownstramParam = NULL;
1,149,367,141✔
1318
  switch (type) {
1,149,367,141!
1319
    case OP_GET_PARAM:
610,326,792✔
1320
      ppParam = &pOperator->pOperatorGetParam;
610,326,792✔
1321
      pppDownstramParam = &pOperator->pDownstreamGetParams;
610,324,115✔
1322
      break;
610,322,353✔
1323
    case OP_NOTIFY_PARAM:
539,091,161✔
1324
      ppParam = &pOperator->pOperatorNotifyParam;
539,091,161✔
1325
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
539,090,300✔
1326
      break;
539,091,850✔
1327
    default:
×
1328
      return;
×
1329
  }
1330

1331
  if (*ppParam) {
1,149,414,203✔
1332
    freeOperatorParam(*ppParam, type);
5,326,081✔
1333
    *ppParam = NULL;
5,326,081✔
1334
  }
1335

1336
  if (*pppDownstramParam) {
1,149,365,629✔
1337
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
81,042,881✔
1338
      if ((*pppDownstramParam)[i]) {
9,834,066!
1339
        freeOperatorParam((*pppDownstramParam)[i], type);
×
1340
        (*pppDownstramParam)[i] = NULL;
×
1341
      }
1342
    }
1343
    if (allFree) {
71,208,815✔
1344
      taosMemoryFreeClear(*pppDownstramParam);
7,115,717!
1345
    }
1346
  }
1347
}
1348

1349
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
1,072,682,589✔
1350
                                                    SSDataBlock** pResBlock) {
1351
  QRY_PARAM_CHECK(pResBlock);
1,072,682,589!
1352

1353
  int32_t code = 0;
1,072,790,835✔
1354
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
1,072,790,835!
1355
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
6,974,561!
1356
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
13,949,122✔
1357
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
6,974,561✔
1358
    if (clearParam && (code == 0)) {
6,974,561!
1359
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
×
1360
      pOperator->pDownstreamGetParams[idx] = NULL;
×
1361
    }
1362

1363
    if (code) {
6,974,561!
1364
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
1365
    }
1366
    return code;
6,974,561✔
1367
  }
1368

1369
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
1,065,841,609✔
1370
  if (code) {
1,045,885,076!
1371
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1372
  }
1373
  return code;
1,045,814,770✔
1374
}
1375

1376
bool compareVal(const char* v, const SStateKeys* pKey) {
594,552,337✔
1377
  if (IS_VAR_DATA_TYPE(pKey->type)) {
594,552,337!
1378
    if (IS_STR_DATA_BLOB(pKey->type)) {
219,519!
1379
      if (blobDataLen(v) != blobDataLen(pKey->pData)) {
×
1380
        return false;
×
1381
      } else {
1382
        return memcmp(blobDataVal(v), blobDataVal(pKey->pData), blobDataLen(v)) == 0;
×
1383
      }
1384
    } else {
1385
      if (varDataLen(v) != varDataLen(pKey->pData)) {
219,997!
1386
        return false;
×
1387
      } else {
1388
        return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
219,997!
1389
      }
1390
    }
1391
  } else {
1392
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
594,332,340!
1393
  }
1394
}
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