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

taosdata / TDengine / #4895

23 Dec 2025 01:08PM UTC coverage: 65.513% (-0.2%) from 65.72%
#4895

push

travis-ci

web-flow
fix: mem leak (#34023)

6 of 9 new or added lines in 1 file covered. (66.67%)

7770 existing lines in 123 files now uncovered.

184705 of 281937 relevant lines covered (65.51%)

112009834.14 hits per line

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

77.06
/source/libs/executor/src/aggregateoperator.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 "nodes.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "executorInt.h"
25
#include "index.h"
26
#include "operator.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "tcompare.h"
30
#include "tdatablock.h"
31
#include "tglobal.h"
32
#include "thash.h"
33
#include "ttypes.h"
34

35
typedef struct {
36
  bool    hasAgg;
37
  int32_t numOfRows;
38
  int32_t startOffset;
39
} SFunctionCtxStatus;
40

41
typedef struct SAggOperatorInfo {
42
  SOptrBasicInfo   binfo;
43
  SAggSupporter    aggSup;
44
  STableQueryInfo* current;
45
  uint64_t         groupId;
46
  SGroupResInfo    groupResInfo;
47
  SExprSupp        scalarExprSup;
48
  bool             groupKeyOptimized;
49
  bool             hasValidBlock;
50
  SSDataBlock*     pNewGroupBlock;
51
  bool             hasCountFunc;
52
  SOperatorInfo*   pOperator;
53
  bool             cleanGroupResInfo;
54
} SAggOperatorInfo;
55

56
static void destroyAggOperatorInfo(void* param);
57
static int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId);
58

59
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock);
60
static void    destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock);
61

62
static int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx);
63
static int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes);
64
static int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
65
                                const char* pKey);
66

67
// static int32_t addNewResultRowBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, uint32_t size);
68

69
static int32_t doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId);
70

71
static void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus);
72
static void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus);
73

74
static int32_t resetAggregateOperatorState(SOperatorInfo* pOper);
75

76
int32_t createAggregateOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
46,697,073✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
46,697,073✔
79

80
  int32_t    lino = 0;
46,702,785✔
81
  int32_t    code = 0;
46,702,785✔
82
  int32_t    num = 0;
46,702,785✔
83
  SExprInfo* pExprInfo = NULL;
46,705,338✔
84
  int32_t    numOfScalarExpr = 0;
46,702,222✔
85
  SExprInfo* pScalarExprInfo = NULL;
46,685,179✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
46,686,923✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
46,666,287✔
89
  if (pInfo == NULL || pOperator == NULL) {
46,665,959✔
90
    code = terrno;
×
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
46,668,513✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
46,669,149✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
46,709,931✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
46,709,931✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
46,702,952✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
46,702,952✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
46,704,393✔
105
  TSDB_CHECK_CODE(code, lino, _error);
46,700,496✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
93,397,590✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
46,701,925✔
109
  TSDB_CHECK_CODE(code, lino, _error);
46,683,133✔
110

111
  if (pAggNode->pExprs != NULL) {
46,683,133✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
9,947,923✔
113
    TSDB_CHECK_CODE(code, lino, _error);
9,947,614✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
46,363,715✔
117
  TSDB_CHECK_CODE(code, lino, _error);
46,352,268✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
46,360,235✔
120
                            GET_STM_RTINFO(pTaskInfo));
46,352,268✔
121
  TSDB_CHECK_CODE(code, lino, _error);
46,354,519✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
46,354,519✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
46,368,794✔
125
  pInfo->groupId = UINT64_MAX;
46,364,337✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
46,345,475✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
46,378,892✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
46,351,578✔
129
  pInfo->pOperator = pOperator;
46,373,988✔
130
  pInfo->cleanGroupResInfo = false;
46,369,102✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
46,344,589✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
46,359,091✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
46,368,178✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
46,359,836✔
137

138
  pOperator->pPhyNode = pAggNode;
46,357,022✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
46,355,214✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
37,084,347✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
37,080,893✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
37,055,453✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
46,356,941✔
147
  if (code != TSDB_CODE_SUCCESS) {
46,370,816✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
46,370,816✔
152
  return TSDB_CODE_SUCCESS;
46,378,698✔
153

154
_error:
323,492✔
155
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
323,492✔
156
  if (pInfo != NULL) {
323,492✔
157
    destroyAggOperatorInfo(pInfo);
323,492✔
158
  }
159
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
323,492✔
160
  pTaskInfo->code = code;
323,492✔
161
  return code;
323,492✔
162
}
163

164
void destroyAggOperatorInfo(void* param) {
46,709,110✔
165
  if (param == NULL) {
46,709,110✔
166
    return;
×
167
  }
168
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
46,709,110✔
169
  cleanupBasicInfo(&pInfo->binfo);
46,709,110✔
170

171
  if (pInfo->pOperator) {
46,709,824✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
46,387,606✔
173
                      pInfo->cleanGroupResInfo);
46,387,499✔
174
    pInfo->pOperator = NULL;
46,385,915✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
46,710,258✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
46,709,361✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
46,708,280✔
179
  taosMemoryFreeClear(param);
46,709,464✔
180
}
181

182
/**
183
 * @brief get blocks from downstream and fill results into groupedRes after aggragation
184
 * @retval false if no more groups
185
 * @retval true if there could have new groups coming
186
 * @note if pOperator.blocking is true, scan all blocks from downstream, all groups are handled
187
 *       if false, fill results of ONE GROUP
188
 * */
189
static bool nextGroupedResult(SOperatorInfo* pOperator) {
60,242,099✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
60,242,099✔
191
  int32_t           lino = 0;
60,242,099✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
60,242,099✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
60,241,895✔
194

195
  if(!pAggInfo) {
60,241,088✔
196
    qError("function:%s, pAggInfo is NULL", __func__);
×
197
    return false;
×
198
  }
199
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
60,241,088✔
200
    return false;
3,379,193✔
201
  }
202

203
  SExprSupp*   pSup = &pOperator->exprSupp;
56,862,658✔
204
  int64_t      st = taosGetTimestampUs();
56,860,901✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
56,860,901✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
56,862,265✔
207

208
  pAggInfo->cleanGroupResInfo = false;
56,859,751✔
209
  if (pBlock) {
56,857,429✔
210
    pAggInfo->pNewGroupBlock = NULL;
3,324,833✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
3,324,833✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
3,324,833✔
213
    QUERY_CHECK_CODE(code, lino, _end);
3,324,833✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
3,324,833✔
215
    QUERY_CHECK_CODE(code, lino, _end);
3,324,833✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
3,324,833✔
218
    QUERY_CHECK_CODE(code, lino, _end);
3,329,041✔
219
  }
220
  while (1) {
133,002,437✔
221
    bool blockAllocated = false;
189,864,074✔
222
    pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
189,864,074✔
223
    if (pOperator->pDownstreamGetParams) {
186,756,188✔
UNCOV
224
      pOperator->pDownstreamGetParams[0] = NULL;
×
225
    }
226
    if (pBlock == NULL) {
186,756,928✔
227
      if (!pAggInfo->hasValidBlock) {
54,273,373✔
228
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
14,036,050✔
229
        QUERY_CHECK_CODE(code, lino, _end);
14,031,640✔
230

231
        if (pBlock == NULL) {
14,031,640✔
232
          break;
9,588,415✔
233
        }
234
        blockAllocated = true;
4,443,225✔
235
      } else {
236
        break;
40,238,434✔
237
      }
238
    }
239
    pAggInfo->hasValidBlock = true;
136,926,780✔
240
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
136,933,328✔
241

242
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
136,932,868✔
243

244
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
245
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
136,915,076✔
246
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
14,900,916✔
247
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
14,900,163✔
248
      if (code != TSDB_CODE_SUCCESS) {
14,900,466✔
249
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
580,343✔
250
        T_LONG_JMP(pTaskInfo->env, code);
580,343✔
251
      }
252
    }
253
    // if non-blocking mode and new group arrived, save the block and break
254
    if (!pOperator->blocking && pAggInfo->groupId != UINT64_MAX && pBlock->info.id.groupId != pAggInfo->groupId) {
136,335,028✔
255
      pAggInfo->pNewGroupBlock = pBlock;
3,340,854✔
256
      break;
3,340,854✔
257
    }
258
    // the pDataBlock are always the same one, no need to call this again
259
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
133,004,776✔
260
    if (code != TSDB_CODE_SUCCESS) {
132,999,453✔
UNCOV
261
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
262
      T_LONG_JMP(pTaskInfo->env, code);
×
263
    }
264
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
132,999,453✔
265
    if (code != TSDB_CODE_SUCCESS) {
132,998,926✔
UNCOV
266
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
267
      T_LONG_JMP(pTaskInfo->env, code);
×
268
    }
269

270
    code = doAggregateImpl(pOperator, pSup->pCtx);
132,998,926✔
271
    if (code != TSDB_CODE_SUCCESS) {
132,993,373✔
272
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
3,128✔
273
      T_LONG_JMP(pTaskInfo->env, code);
3,128✔
274
    }
275

276
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
132,990,245✔
277
  }
278

279
  // the downstream operator may return with error code, so let's check the code before generating results.
280
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
53,167,703✔
UNCOV
281
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
282
  }
283

284
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
53,160,725✔
285
  QUERY_CHECK_CODE(code, lino, _end);
53,165,025✔
286
  pAggInfo->cleanGroupResInfo = true;
53,165,025✔
287

288
_end:
53,166,541✔
289
  if (code != TSDB_CODE_SUCCESS) {
53,166,537✔
290
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
138✔
291
    pTaskInfo->code = code;
138✔
UNCOV
292
    T_LONG_JMP(pTaskInfo->env, code);
×
293
  }
294
  return pBlock != NULL;
53,168,400✔
295
}
296

297
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
99,423,752✔
298
  int32_t           code = TSDB_CODE_SUCCESS;
99,423,752✔
299
  int32_t           lino = 0;
99,423,752✔
300
  SAggOperatorInfo* pAggInfo = pOperator->info;
99,423,752✔
301
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
99,426,791✔
302

303
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
99,424,602✔
304
    (*ppRes) = NULL;
39,195,386✔
305
    return code;
39,195,386✔
306
  }
307

308
  if (pOperator->pOperatorGetParam) {
60,228,843✔
UNCOV
309
    if (pOperator->status == OP_EXEC_DONE) {
×
UNCOV
310
      pOperator->status = OP_OPENED;
×
UNCOV
311
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
×
UNCOV
312
      pAggInfo->groupId = UINT64_MAX;
×
UNCOV
313
      pAggInfo->hasValidBlock = false;
×
314
    }
UNCOV
315
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
×
UNCOV
316
    pOperator->pOperatorGetParam = NULL;
×
317

318
  }
319

320
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
60,227,492✔
321
  bool           hasNewGroups = false;
60,230,921✔
322
  do {
323
    hasNewGroups = nextGroupedResult(pOperator);
60,244,525✔
324
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
56,548,049✔
325
    QUERY_CHECK_CODE(code, lino, _end);
56,551,782✔
326

327
    while (1) {
328
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
56,551,782✔
329
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
56,551,329✔
330
      QUERY_CHECK_CODE(code, lino, _end);
56,551,047✔
331

332
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
56,551,047✔
333
        if (!hasNewGroups) setOperatorCompleted(pOperator);
53,171,480✔
334
        break;
53,173,309✔
335
      }
336

337
      if (pInfo->pRes->info.rows > 0) {
3,380,419✔
338
        break;
3,380,419✔
339
      }
340
    }
341
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
56,553,728✔
342

343
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
56,536,332✔
344
  pOperator->resultInfo.totalRows += rows;
56,537,011✔
345

346
_end:
56,536,568✔
347
  if (code != TSDB_CODE_SUCCESS) {
56,536,568✔
348
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
349
    pTaskInfo->code = code;
×
UNCOV
350
    T_LONG_JMP(pTaskInfo->env, code);
×
351
  }
352

353
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
56,536,568✔
354

355
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
56,531,588✔
356
  return code;
56,532,835✔
357
}
358

359
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
136,278,544✔
360
  int32_t code = TSDB_CODE_SUCCESS;
136,278,544✔
361
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
136,278,544✔
362
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
304✔
UNCOV
363
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
364
  }
365
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
2,147,483,647✔
366
    if (functionNeedToExecute(&pCtx[k])) {
2,147,483,647✔
367
      // todo add a dummy function to avoid process check
368
      if (pCtx[k].fpSet.process == NULL) {
2,147,483,647✔
369
        continue;
4,431,882✔
370
      }
371

372
      if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
UNCOV
373
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
UNCOV
374
        qError("%s aggregate function error happens, input data is NULL.", GET_TASKID(pOperator->pTaskInfo));
×
375
      } else {
376
        code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
377
      }
378

379
      if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
380
        if (pCtx[k].fpSet.cleanup != NULL) {
3,128✔
UNCOV
381
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
382
        }
383
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
3,128✔
384
        return code;
3,128✔
385
      }
386
    }
387
  }
388

389
  return TSDB_CODE_SUCCESS;
136,324,242✔
390
}
391

392
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
14,035,042✔
393
  int32_t code = TSDB_CODE_SUCCESS;
14,035,042✔
394
  int32_t lino = 0;
14,035,042✔
395
  SSDataBlock* pBlock = NULL;
14,035,042✔
396
  if (!tsCountAlwaysReturnValue) {
14,037,826✔
397
    return TSDB_CODE_SUCCESS;
1,892,122✔
398
  }
399

400
  SAggOperatorInfo* pAggInfo = pOperator->info;
12,145,704✔
401
  if (pAggInfo->groupKeyOptimized) {
12,138,054✔
402
    return TSDB_CODE_SUCCESS;
1,299,009✔
403
  }
404

405
  SOperatorInfo* downstream = pOperator->pDownstream[0];
10,839,406✔
406
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
10,842,525✔
407
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
10,829,786✔
408
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
10,819,118✔
409
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
7,630,661✔
410
    return TSDB_CODE_SUCCESS;
25,495✔
411
  }
412

413
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
10,811,254✔
414

415
  if (!pAggInfo->hasCountFunc) {
10,814,737✔
416
    return TSDB_CODE_SUCCESS;
6,373,187✔
417
  }
418

419
  code = createDataBlock(&pBlock);
4,444,539✔
420
  if (code) {
4,443,921✔
UNCOV
421
    return code;
×
422
  }
423

424
  pBlock->info.rows = 1;
4,443,921✔
425
  pBlock->info.capacity = 0;
4,443,921✔
426

427
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
11,536,336✔
428
    SColumnInfoData colInfo = {0};
7,092,415✔
429
    colInfo.hasNull = true;
7,092,415✔
430
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
7,092,415✔
431
    colInfo.info.bytes = 1;
7,092,415✔
432

433
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
7,092,415✔
434
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
14,402,658✔
435
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
7,312,175✔
436
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,312,175✔
437
        int32_t slotId = pFuncParam->pCol->slotId;
7,309,679✔
438
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
7,309,679✔
439
        if (slotId >= numOfCols) {
7,308,443✔
440
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,735,999✔
441
          QUERY_CHECK_CODE(code, lino, _end);
4,735,303✔
442

443
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
12,765,677✔
444
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
8,029,060✔
445
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
8,030,374✔
446
          }
447
        }
448
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
2,496✔
449
        // do nothing
450
      }
451
    }
452
  }
453

454
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
4,444,539✔
455
  QUERY_CHECK_CODE(code, lino, _end);
4,444,539✔
456

457
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
12,474,295✔
458
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
8,029,756✔
459
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
8,029,756✔
460
    colDataSetNULL(pColInfoData, 0);
461
  }
462
  *ppBlock = pBlock;
4,444,539✔
463

464
_end:
4,444,539✔
465
  if (code != TSDB_CODE_SUCCESS) {
4,444,539✔
UNCOV
466
    blockDataDestroy(pBlock);
×
UNCOV
467
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
468
  }
469
  return code;
4,444,539✔
470
}
471

472
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
133,584,990✔
473
  if (!blockAllocated) {
133,584,990✔
474
    return;
129,145,444✔
475
  }
476

477
  blockDataDestroy(*ppBlock);
4,439,546✔
478
  *ppBlock = NULL;
4,444,539✔
479
}
480

481
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
136,331,517✔
482
  int32_t           code = TSDB_CODE_SUCCESS;
136,331,517✔
483
  SAggOperatorInfo* pAggInfo = pOperator->info;
136,331,517✔
484
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
136,334,636✔
485
    return code;
64,429,948✔
486
  }
487

488
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
71,905,450✔
489

490
  // record the current active group id
491
  pAggInfo->groupId = groupId;
71,896,077✔
492
  return code;
71,902,110✔
493
}
494

495
int32_t doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
71,903,563✔
496
  // for simple group by query without interval, all the tables belong to one group result.
497
  int32_t           code = TSDB_CODE_SUCCESS;
71,903,563✔
498
  int32_t           lino = 0;
71,903,563✔
499
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
71,903,563✔
500
  SAggOperatorInfo* pAggInfo = pOperator->info;
71,908,345✔
501

502
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
71,902,867✔
503
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
71,894,597✔
504
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
71,899,644✔
505

506
  SResultRow* pResultRow =
507
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
71,906,638✔
508
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
509
  if (pResultRow == NULL || pTaskInfo->code != 0) {
71,899,228✔
510
    code = pTaskInfo->code;
6,907✔
UNCOV
511
    lino = __LINE__;
×
UNCOV
512
    goto _end;
×
513
  }
514
  /*
515
   * not assign result buffer yet, add new result buffer
516
   * all group belong to one result set, and each group result has different group id so set the id to be one
517
   */
518
  if (pResultRow->pageId == -1) {
71,898,585✔
519
    code = addNewResultRowBuf(pResultRow, pAggInfo->aggSup.pResultBuf, pAggInfo->binfo.pRes->info.rowSize);
×
520
    QUERY_CHECK_CODE(code, lino, _end);
×
521
  }
522

523
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
71,902,425✔
524
  QUERY_CHECK_CODE(code, lino, _end);
71,901,742✔
525

526
_end:
71,901,742✔
527
  if (code != TSDB_CODE_SUCCESS) {
71,901,742✔
528
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
529
  }
530
  return code;
71,899,861✔
531
}
532

533
// a new buffer page for each table. Needs to opt this design
534
int32_t addNewResultRowBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, uint32_t size) {
×
UNCOV
535
  if (pWindowRes->pageId != -1) {
×
536
    return 0;
×
537
  }
538

539
  SFilePage* pData = NULL;
×
540

541
  // in the first scan, new space needed for results
542
  int32_t pageId = -1;
×
UNCOV
543
  SArray* list = getDataBufPagesIdList(pResultBuf);
×
544

545
  if (taosArrayGetSize(list) == 0) {
×
UNCOV
546
    pData = getNewBufPage(pResultBuf, &pageId);
×
547
    if (pData == NULL) {
×
UNCOV
548
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
549
      return terrno;
×
550
    }
551
    pData->num = sizeof(SFilePage);
×
552
  } else {
553
    SPageInfo* pi = getLastPageInfo(list);
×
554
    pData = getBufPage(pResultBuf, getPageId(pi));
×
UNCOV
555
    if (pData == NULL) {
×
556
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
UNCOV
557
      return terrno;
×
558
    }
559

560
    pageId = getPageId(pi);
×
561

UNCOV
562
    if (pData->num + size > getBufPageSize(pResultBuf)) {
×
563
      // release current page first, and prepare the next one
UNCOV
564
      releaseBufPageInfo(pResultBuf, pi);
×
565

566
      pData = getNewBufPage(pResultBuf, &pageId);
×
567
      if (pData == NULL) {
×
UNCOV
568
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
569
        return terrno;
×
570
      }
UNCOV
571
      pData->num = sizeof(SFilePage);
×
572
    }
573
  }
574

UNCOV
575
  if (pData == NULL) {
×
UNCOV
576
    return -1;
×
577
  }
578

579
  // set the number of rows in current disk page
UNCOV
580
  if (pWindowRes->pageId == -1) {  // not allocated yet, allocate new buffer
×
UNCOV
581
    pWindowRes->pageId = pageId;
×
UNCOV
582
    pWindowRes->offset = (int32_t)pData->num;
×
583

UNCOV
584
    pData->num += size;
×
585
  }
586

UNCOV
587
  return 0;
×
588
}
589

590
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
231,699,808✔
591
                         const char* pKey) {
592
  int32_t code = 0;
231,699,808✔
593
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
594

595
  pAggSup->currentPageId = -1;
231,699,808✔
596
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
231,743,810✔
597
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
231,671,599✔
598
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
231,569,258✔
599

600
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
231,579,655✔
601
    return terrno;
5,902✔
602
  }
603

604
  uint32_t defaultPgsz = 0;
231,589,339✔
605
  int64_t defaultBufsz = 0;
231,641,386✔
606
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
231,698,875✔
607
  if (code) {
231,515,047✔
UNCOV
608
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
UNCOV
609
    return code;
×
610
  }
611

612
  if (!osTempSpaceAvailable()) {
231,515,047✔
613
    code = TSDB_CODE_NO_DISKSPACE;
×
614
    qError("Init stream agg supporter failed since %s, key:%s, tempDir:%s", tstrerror(code), pKey, tsTempDir);
×
615
    return code;
×
616
  }
617

618
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
231,701,385✔
619
  if (code != TSDB_CODE_SUCCESS) {
231,705,972✔
UNCOV
620
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
621
    return code;
×
622
  }
623

624
  return code;
231,705,972✔
625
}
626

UNCOV
627
void cleanupResultInfoInStream(SExecTaskInfo* pTaskInfo, void* pState, SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
×
628
  int32_t         code = TSDB_CODE_SUCCESS;
×
629
  SStorageAPI*    pAPI = &pTaskInfo->storageAPI;
×
630
  int32_t         numOfExprs = pSup->numOfExprs;
×
631
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
×
UNCOV
632
  SqlFunctionCtx* pCtx = pSup->pCtx;
×
633
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
×
634
  bool            needCleanup = false;
×
635

636
  for (int32_t j = 0; j < numOfExprs; ++j) {
×
UNCOV
637
    needCleanup |= pCtx[j].needCleanup;
×
638
  }
639
  if (!needCleanup) {
×
640
    return;
×
641
  }
642
  
UNCOV
643
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
×
UNCOV
644
    SResultWindowInfo* pWinInfo = taosArrayGet(pGroupResInfo->pRows, i);
×
UNCOV
645
    SRowBuffPos*       pPos = pWinInfo->pStatePos;
×
UNCOV
646
    SResultRow*        pRow = NULL;
×
647

UNCOV
648
    code = pAPI->stateStore.streamStateGetByPos(pState, pPos, (void**)&pRow);
×
UNCOV
649
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
650
      qError("failed to get state by pos, code:%s, %s", tstrerror(code), GET_TASKID(pTaskInfo));
×
UNCOV
651
      continue;
×
652
    }
653

UNCOV
654
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
UNCOV
655
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
UNCOV
656
      if (pCtx[j].fpSet.cleanup) {
×
UNCOV
657
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
658
      }
659
    }
660
  }
661
}
662

663
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
63,721,474✔
664
                                  SGroupResInfo* pGroupResInfo) {
665
  int32_t         numOfExprs = pSup->numOfExprs;
63,721,474✔
666
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
63,724,424✔
667
  SqlFunctionCtx* pCtx = pSup->pCtx;
63,720,506✔
668
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
63,725,411✔
669
  bool            needCleanup = false;
63,723,919✔
670

671
  for (int32_t j = 0; j < numOfExprs; ++j) {
189,676,012✔
672
    needCleanup |= pCtx[j].needCleanup;
125,949,561✔
673
  }
674
  if (!needCleanup) {
63,726,451✔
675
    return;
63,363,198✔
676
  }
677

678
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
363,253✔
UNCOV
679
    SResultRow*        pRow = NULL;
×
680
    SResKeyPos*        pPos = taosArrayGetP(pGroupResInfo->pRows, i);
×
UNCOV
681
    SFilePage*         page = getBufPage(pBuf, pPos->pos.pageId);
×
UNCOV
682
    if (page == NULL) {
×
UNCOV
683
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
UNCOV
684
      continue;
×
685
    }
UNCOV
686
    pRow = (SResultRow*)((char*)page + pPos->pos.offset);
×
687

688

UNCOV
689
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
UNCOV
690
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
UNCOV
691
      if (pCtx[j].fpSet.cleanup) {
×
UNCOV
692
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
693
      }
694
    }
UNCOV
695
    releaseBufPage(pBuf, page);
×
696
  }
697
}
698

699
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
40,908,020✔
700
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
701
  int32_t         numOfExprs = pSup->numOfExprs;
40,908,020✔
702
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
40,907,949✔
703
  SqlFunctionCtx* pCtx = pSup->pCtx;
40,907,944✔
704
  bool            needCleanup = false;
40,907,971✔
705
  for (int32_t j = 0; j < numOfExprs; ++j) {
116,974,644✔
706
    needCleanup |= pCtx[j].needCleanup;
76,066,482✔
707
  }
708
  if (!needCleanup) {
40,908,162✔
709
    return;
40,906,527✔
710
  }
711

712
  // begin from last iter
713
  void*   pData = pGroupResInfo->dataPos;
1,635✔
714
  int32_t iter = pGroupResInfo->iter;
2,164✔
715
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
2,691✔
716
    SResultRowPosition* pos = pData;
527✔
717

718
    SFilePage* page = getBufPage(pBuf, pos->pageId);
527✔
719
    if (page == NULL) {
527✔
UNCOV
720
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
UNCOV
721
      continue;
×
722
    }
723

724
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
527✔
725

726
    for (int32_t j = 0; j < numOfExprs; ++j) {
11,067✔
727
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
10,540✔
728
      if (pCtx[j].fpSet.cleanup) {
10,540✔
729
        pCtx[j].fpSet.cleanup(&pCtx[j]);
1,054✔
730
      }
731
    }
732

733
    releaseBufPage(pBuf, page);
527✔
734
  }
735
}
736

737
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
104,629,941✔
738
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
739
  if (cleanGroupResInfo) {
104,629,941✔
740
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
63,722,888✔
741
  } else {
742
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
40,907,053✔
743
  }
744
}
104,630,159✔
745
void cleanupAggSup(SAggSupporter* pAggSup) {
235,819,421✔
746
  taosMemoryFreeClear(pAggSup->keyBuf);
235,819,421✔
747
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
235,813,812✔
748
  destroyDiskbasedBuf(pAggSup->pResultBuf);
235,815,561✔
749
  memset(pAggSup, 0, sizeof(SAggSupporter));
235,802,647✔
750
}
235,802,647✔
751

752
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
231,714,552✔
753
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
754
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
231,714,552✔
755
  if (code != TSDB_CODE_SUCCESS) {
231,610,647✔
UNCOV
756
    return code;
×
757
  }
758

759
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
231,610,647✔
760
  if (code != TSDB_CODE_SUCCESS) {
231,675,959✔
UNCOV
761
    return code;
×
762
  }
763

764
  for (int32_t i = 0; i < numOfCols; ++i) {
912,868,251✔
765
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
681,251,754✔
766
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
681,281,658✔
767
    if (pState) {
681,220,954✔
UNCOV
768
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
UNCOV
769
      pSup->pCtx[i].saveHandle.pState = pState;
×
UNCOV
770
      pSup->pCtx[i].exprIdx = i;
×
771
    } else {
772
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
681,220,954✔
773
    }
774
  }
775

776
  return TSDB_CODE_SUCCESS;
231,616,497✔
777
}
778

779
int32_t applyAggFunctionOnPartialTuples(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData,
2,147,483,647✔
780
                                        int32_t offset, int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
781
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,147,483,647✔
782
  for (int32_t k = 0; k < numOfOutput; ++k) {
2,147,483,647✔
783
    // keep it temporarily
784
    SFunctionCtxStatus status = {0};
2,147,483,647✔
785
    functionCtxSave(&pCtx[k], &status);
2,147,483,647✔
786

787
    pCtx[k].input.startRowIndex = offset;
2,147,483,647✔
788
    pCtx[k].input.numOfRows = forwardStep;
2,147,483,647✔
789

790
    // not a whole block involved in query processing, statistics data can not be used
791
    // NOTE: the original value of isSet have been changed here
792
    if (pCtx[k].input.colDataSMAIsSet && forwardStep < numOfTotal) {
2,147,483,647✔
UNCOV
793
      pCtx[k].input.colDataSMAIsSet = false;
×
794
    }
795

796
    if (fmIsPlaceHolderFunc(pCtx[k].functionId)) {
2,147,483,647✔
797
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
13,682,680✔
798
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
13,682,680✔
799

800
      TAOS_CHECK_EXIT(fmSetStreamPseudoFuncParamVal(pCtx[k].functionId, pCtx[k].pExpr->base.pParamList, &taskInfo->pStreamRuntimeInfo->funcInfo));
13,683,090✔
801

802
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
13,680,215✔
803
      pEntryInfo->isNullRes = 0;
13,681,452✔
804
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
13,681,862✔
805
        pEntryInfo->isNullRes = 1;
815✔
806
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
13,681,860✔
807
        void* v = nodesGetValueFromNode(valueNode);
16,539✔
808
        memcpy(p, v, varDataTLen(v));
18,172✔
809
      } else {
810
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
13,662,873✔
811
      }
812
      
813
      pEntryInfo->numOfRes = 1;
13,681,045✔
814
    } else if (pCtx[k].isPseudoFunc) {
2,147,483,647✔
815
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
2,147,483,647✔
816

817
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
818

819
      SColumnInfoData idata = {0};
2,147,483,647✔
820
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
2,147,483,647✔
821
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
2,147,483,647✔
822
      idata.pData = p;
2,147,483,647✔
823

824
      SScalarParam out = {.columnData = &idata};
2,147,483,647✔
825
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
2,147,483,647✔
826
      TAOS_CHECK_EXIT(pCtx[k].sfp.process(&tw, 1, &out));
2,147,483,647✔
827
      pEntryInfo->isNullRes = colDataIsNull_s(&idata, 0);
2,147,483,647✔
828
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
829
    } else {
830
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
2,147,483,647✔
831
        if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
UNCOV
832
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
UNCOV
833
          qError("%s apply functions error, input data is NULL.", GET_TASKID(taskInfo));
×
834
        } else {
835
          code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
836
        }
837

838
        if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
839
          if (pCtx[k].fpSet.cleanup != NULL) {
527✔
840
            pCtx[k].fpSet.cleanup(&pCtx[k]);
527✔
841
          }
842
          TAOS_CHECK_EXIT(code);
527✔
843
        }
844
      }
845

846
      // restore it
847
      functionCtxRestore(&pCtx[k], &status);
2,147,483,647✔
848
    }
849
  }
850

851
_exit:
2,147,483,647✔
852

853
  if (code) {
2,147,483,647✔
854
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
527✔
855
    taskInfo->code = code;
527✔
856
  }
857

858
  return code;
2,147,483,647✔
859
}
860

861
void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
862
  pStatus->hasAgg = pCtx->input.colDataSMAIsSet;
2,147,483,647✔
863
  pStatus->numOfRows = pCtx->input.numOfRows;
2,147,483,647✔
864
  pStatus->startOffset = pCtx->input.startRowIndex;
2,147,483,647✔
865
}
2,147,483,647✔
866

867
void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
868
  pCtx->input.colDataSMAIsSet = pStatus->hasAgg;
2,147,483,647✔
869
  pCtx->input.numOfRows = pStatus->numOfRows;
2,147,483,647✔
870
  pCtx->input.startRowIndex = pStatus->startOffset;
2,147,483,647✔
871
}
2,147,483,647✔
872

873
static int32_t resetAggregateOperatorState(SOperatorInfo* pOper) {
7,814,599✔
874
  SAggOperatorInfo* pAgg = pOper->info;
7,814,599✔
875
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
7,819,079✔
876
  
877
  pOper->status = OP_NOT_OPENED;
7,817,859✔
878
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
7,818,671✔
879
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
7,818,671✔
880
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
7,817,047✔
881
                      pAgg->cleanGroupResInfo);
7,818,263✔
882
  cleanupGroupResInfo(&pAgg->groupResInfo);
7,817,043✔
883
  resetBasicOperatorState(&pAgg->binfo);
7,812,971✔
884
  
885
  pAgg->pNewGroupBlock = NULL;
7,818,671✔
886

887
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
15,637,342✔
888
    keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
7,818,671✔
889

890
  if (code == 0) {
7,817,447✔
891
    code = resetExprSupp(&pAgg->scalarExprSup, pTaskInfo, pAggNode->pExprs, NULL,
7,817,855✔
892
                          &pTaskInfo->storageAPI.functionStore);
893
  }
894

895
  pAgg->groupId = UINT64_MAX;
7,816,635✔
896
  pAgg->cleanGroupResInfo = false;
7,817,043✔
897
  pAgg->hasValidBlock = false;
7,818,671✔
898
  return 0;
7,818,671✔
899
}
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