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

taosdata / TDengine / #4932

19 Jan 2026 12:29PM UTC coverage: 66.646% (-0.1%) from 66.749%
#4932

push

travis-ci

web-flow
chore: upgrade taospy (#34272)

202981 of 304565 relevant lines covered (66.65%)

126831443.51 hits per line

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

78.59
/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,
82,344,567✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
82,344,567✔
79

80
  int32_t    lino = 0;
82,378,233✔
81
  int32_t    code = 0;
82,378,233✔
82
  int32_t    num = 0;
82,378,233✔
83
  SExprInfo* pExprInfo = NULL;
82,396,826✔
84
  int32_t    numOfScalarExpr = 0;
82,399,207✔
85
  SExprInfo* pScalarExprInfo = NULL;
82,404,494✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
82,396,477✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
82,272,070✔
89
  if (pInfo == NULL || pOperator == NULL) {
82,226,371✔
90
    code = terrno;
40✔
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
82,226,715✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
82,238,580✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
82,394,307✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
82,394,307✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
82,413,814✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
82,413,814✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
82,422,290✔
105
  TSDB_CHECK_CODE(code, lino, _error);
82,324,247✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
164,646,387✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
82,339,225✔
109
  TSDB_CHECK_CODE(code, lino, _error);
82,335,005✔
110

111
  if (pAggNode->pExprs != NULL) {
82,335,005✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
24,389,339✔
113
    TSDB_CHECK_CODE(code, lino, _error);
24,394,132✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
81,705,919✔
117
  TSDB_CHECK_CODE(code, lino, _error);
81,685,531✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
81,669,915✔
120
                            GET_STM_RTINFO(pTaskInfo));
81,685,531✔
121
  TSDB_CHECK_CODE(code, lino, _error);
81,679,374✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
81,679,374✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
81,709,674✔
125
  pInfo->groupId = UINT64_MAX;
81,662,211✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
81,717,013✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
81,707,188✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
81,696,083✔
129
  pInfo->pOperator = pOperator;
81,664,851✔
130
  pInfo->cleanGroupResInfo = false;
81,614,484✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
81,693,242✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
81,703,124✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
81,725,834✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
81,710,385✔
137

138
  pOperator->pPhyNode = pAggNode;
81,727,929✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
81,728,420✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
63,009,817✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
63,009,425✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
62,951,656✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
81,635,535✔
147
  if (code != TSDB_CODE_SUCCESS) {
81,678,108✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
81,678,108✔
152
  return TSDB_CODE_SUCCESS;
81,694,650✔
153

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

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

171
  if (pInfo->pOperator) {
82,425,047✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
81,774,338✔
173
                      pInfo->cleanGroupResInfo);
81,792,220✔
174
    pInfo->pOperator = NULL;
81,742,818✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
82,385,288✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
82,387,956✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
82,368,933✔
179
  taosMemoryFreeClear(param);
82,394,285✔
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) {
95,560,176✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
95,560,176✔
191
  int32_t           lino = 0;
95,560,176✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
95,560,176✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
95,575,858✔
194

195
  if(!pAggInfo) {
95,463,246✔
196
    qError("function:%s, pAggInfo is NULL", __func__);
×
197
    return false;
×
198
  }
199
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
95,463,246✔
200
    return false;
9,558,447✔
201
  }
202

203
  SExprSupp*   pSup = &pOperator->exprSupp;
85,924,679✔
204
  int64_t      st = taosGetTimestampUs();
85,960,021✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
85,960,021✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
85,977,486✔
207

208
  pAggInfo->cleanGroupResInfo = false;
85,961,935✔
209
  if (pBlock) {
85,968,597✔
210
    pAggInfo->pNewGroupBlock = NULL;
685,731✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
685,731✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
685,731✔
213
    QUERY_CHECK_CODE(code, lino, _end);
685,731✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
685,731✔
215
    QUERY_CHECK_CODE(code, lino, _end);
685,731✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
685,731✔
218
    QUERY_CHECK_CODE(code, lino, _end);
632,965✔
219
  }
220
  while (1) {
278,558,186✔
221
    bool blockAllocated = false;
364,474,007✔
222
    pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
364,474,007✔
223
    if (pOperator->pDownstreamGetParams) {
363,654,038✔
224
      pOperator->pDownstreamGetParams[0] = NULL;
377,662✔
225
    }
226
    if (pBlock == NULL) {
363,722,434✔
227
      if (!pAggInfo->hasValidBlock) {
87,695,378✔
228
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
19,641,027✔
229
        QUERY_CHECK_CODE(code, lino, _end);
19,639,851✔
230

231
        if (pBlock == NULL) {
19,639,851✔
232
          break;
15,196,320✔
233
        }
234
        blockAllocated = true;
4,443,531✔
235
      } else {
236
        break;
68,053,353✔
237
      }
238
    }
239
    pAggInfo->hasValidBlock = true;
280,470,587✔
240
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
280,509,263✔
241

242
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
280,504,839✔
243

244
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
245
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
280,374,864✔
246
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
80,583,836✔
247
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
80,577,544✔
248
      if (code != TSDB_CODE_SUCCESS) {
80,540,126✔
249
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
1,135,665✔
250
        T_LONG_JMP(pTaskInfo->env, code);
1,135,665✔
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) {
279,208,508✔
255
      pAggInfo->pNewGroupBlock = pBlock;
700,670✔
256
      break;
700,670✔
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);
278,536,762✔
260
    if (code != TSDB_CODE_SUCCESS) {
278,574,764✔
261
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
262
      T_LONG_JMP(pTaskInfo->env, code);
×
263
    }
264
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
278,574,764✔
265
    if (code != TSDB_CODE_SUCCESS) {
278,584,266✔
266
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
267
      T_LONG_JMP(pTaskInfo->env, code);
×
268
    }
269

270
    code = doAggregateImpl(pOperator, pSup->pCtx);
278,584,266✔
271
    if (code != TSDB_CODE_SUCCESS) {
278,565,753✔
272
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
3,555✔
273
      T_LONG_JMP(pTaskInfo->env, code);
3,555✔
274
    }
275

276
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
278,562,198✔
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) {
83,950,343✔
281
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
282
  }
283

284
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
83,946,662✔
285
  QUERY_CHECK_CODE(code, lino, _end);
83,947,699✔
286
  pAggInfo->cleanGroupResInfo = true;
83,947,699✔
287

288
_end:
83,947,155✔
289
  if (code != TSDB_CODE_SUCCESS) {
83,949,797✔
290
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
6,852✔
291
    pTaskInfo->code = code;
6,852✔
292
    T_LONG_JMP(pTaskInfo->env, code);
×
293
  }
294
  return pBlock != NULL;
83,942,945✔
295
}
296

297
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
160,652,482✔
298
  int32_t           code = TSDB_CODE_SUCCESS;
160,652,482✔
299
  int32_t           lino = 0;
160,652,482✔
300
  SAggOperatorInfo* pAggInfo = pOperator->info;
160,652,482✔
301
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
160,690,011✔
302

303
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
160,681,105✔
304
    (*ppRes) = NULL;
65,105,336✔
305
    return code;
65,105,373✔
306
  }
307

308
  if (pOperator->pOperatorGetParam) {
95,569,130✔
309
    if (pOperator->status == OP_EXEC_DONE) {
108,737✔
310
      pOperator->status = OP_OPENED;
48,202✔
311
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
47,645✔
312
      pAggInfo->groupId = UINT64_MAX;
47,645✔
313
      pAggInfo->hasValidBlock = false;
47,645✔
314
    }
315
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
108,737✔
316
    pOperator->pOperatorGetParam = NULL;
108,180✔
317

318
  }
319

320
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
95,549,716✔
321
  bool           hasNewGroups = false;
95,544,123✔
322
  do {
323
    hasNewGroups = nextGroupedResult(pOperator);
95,554,701✔
324
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
93,504,602✔
325
    QUERY_CHECK_CODE(code, lino, _end);
93,508,870✔
326

327
    while (1) {
328
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
93,508,870✔
329
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
93,504,219✔
330
      QUERY_CHECK_CODE(code, lino, _end);
93,504,207✔
331

332
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
93,504,207✔
333
        if (!hasNewGroups) setOperatorCompleted(pOperator);
83,942,457✔
334
        break;
83,944,670✔
335
      }
336

337
      if (pInfo->pRes->info.rows > 0) {
9,564,735✔
338
        break;
9,564,735✔
339
      }
340
    }
341
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
93,509,405✔
342

343
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
93,494,698✔
344
  pOperator->resultInfo.totalRows += rows;
93,498,781✔
345

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

353
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
93,498,287✔
354

355
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
93,492,542✔
356
  return code;
93,494,947✔
357
}
358

359
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
279,315,363✔
360
  int32_t code = TSDB_CODE_SUCCESS;
279,315,363✔
361
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
279,315,363✔
362
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
69,145✔
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;
11,332,714✔
370
      }
371

372
      if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
373
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
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,555✔
381
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
382
        }
383
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
3,555✔
384
        return code;
3,555✔
385
      }
386
    }
387
  }
388

389
  return TSDB_CODE_SUCCESS;
279,315,238✔
390
}
391

392
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
19,641,027✔
393
  int32_t code = TSDB_CODE_SUCCESS;
19,641,027✔
394
  int32_t lino = 0;
19,641,027✔
395
  SSDataBlock* pBlock = NULL;
19,641,027✔
396
  if (!tsCountAlwaysReturnValue) {
19,641,652✔
397
    return TSDB_CODE_SUCCESS;
6,107,572✔
398
  }
399

400
  SAggOperatorInfo* pAggInfo = pOperator->info;
13,534,080✔
401
  if (pAggInfo->groupKeyOptimized) {
13,532,324✔
402
    return TSDB_CODE_SUCCESS;
3,482,382✔
403
  }
404

405
  SOperatorInfo* downstream = pOperator->pDownstream[0];
10,050,112✔
406
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
10,049,487✔
407
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
10,012,990✔
408
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
10,016,740✔
409
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
7,150,889✔
410
    return TSDB_CODE_SUCCESS;
34,622✔
411
  }
412

413
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
10,015,490✔
414

415
  if (!pAggInfo->hasCountFunc) {
10,016,115✔
416
    return TSDB_CODE_SUCCESS;
5,571,289✔
417
  }
418

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

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

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

433
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
6,986,582✔
434
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
14,186,214✔
435
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
7,200,257✔
436
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,199,632✔
437
        int32_t slotId = pFuncParam->pCol->slotId;
7,196,664✔
438
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
7,196,664✔
439
        if (slotId >= numOfCols) {
7,196,039✔
440
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,857,914✔
441
          QUERY_CHECK_CODE(code, lino, _end);
4,857,914✔
442

443
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
12,764,018✔
444
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
7,905,479✔
445
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,906,104✔
446
          }
447
        }
448
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
2,968✔
449
        // do nothing
450
      }
451
    }
452
  }
453

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

457
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
12,350,260✔
458
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
7,906,104✔
459
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
7,905,479✔
460
    colDataSetNULL(pColInfoData, 0);
461
  }
462
  *ppBlock = pBlock;
4,444,781✔
463

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

472
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
279,681,964✔
473
  if (!blockAllocated) {
279,681,964✔
474
    return;
275,280,997✔
475
  }
476

477
  blockDataDestroy(*ppBlock);
4,400,967✔
478
  *ppBlock = NULL;
4,444,826✔
479
}
480

481
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
279,269,792✔
482
  int32_t           code = TSDB_CODE_SUCCESS;
279,269,792✔
483
  SAggOperatorInfo* pAggInfo = pOperator->info;
279,269,792✔
484
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
279,376,539✔
485
    return code;
108,120,729✔
486
  }
487

488
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
171,247,939✔
489

490
  // record the current active group id
491
  pAggInfo->groupId = groupId;
171,170,652✔
492
  return code;
171,203,543✔
493
}
494

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

502
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
171,233,413✔
503
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
171,201,921✔
504
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
171,183,883✔
505

506
  SResultRow* pResultRow =
507
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
171,181,895✔
508
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
509
  if (pResultRow == NULL || pTaskInfo->code != 0) {
171,202,884✔
510
    code = pTaskInfo->code;
23,560✔
511
    lino = __LINE__;
×
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) {
171,176,613✔
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);
171,199,404✔
524
  QUERY_CHECK_CODE(code, lino, _end);
171,182,820✔
525

526
_end:
171,182,820✔
527
  if (code != TSDB_CODE_SUCCESS) {
171,182,820✔
528
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
529
  }
530
  return code;
171,178,197✔
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) {
×
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;
×
543
  SArray* list = getDataBufPagesIdList(pResultBuf);
×
544

545
  if (taosArrayGetSize(list) == 0) {
×
546
    pData = getNewBufPage(pResultBuf, &pageId);
×
547
    if (pData == NULL) {
×
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));
×
555
    if (pData == NULL) {
×
556
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
557
      return terrno;
×
558
    }
559

560
    pageId = getPageId(pi);
×
561

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

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

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

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

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

587
  return 0;
×
588
}
589

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

595
  pAggSup->currentPageId = -1;
313,490,963✔
596
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
313,610,829✔
597
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
313,205,201✔
598
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
313,554,605✔
599

600
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
313,285,820✔
601
    return terrno;
×
602
  }
603

604
  uint32_t defaultPgsz = 0;
313,385,151✔
605
  int64_t defaultBufsz = 0;
313,400,221✔
606
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
313,430,096✔
607
  if (code) {
313,081,771✔
608
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
609
    return code;
×
610
  }
611

612
  if (!osTempSpaceAvailable()) {
313,081,771✔
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);
313,500,233✔
619
  if (code != TSDB_CODE_SUCCESS) {
313,472,847✔
620
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
621
    return code;
×
622
  }
623

624
  return code;
313,472,847✔
625
}
626

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

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

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

663
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
100,283,668✔
664
                                  SGroupResInfo* pGroupResInfo) {
665
  int32_t         numOfExprs = pSup->numOfExprs;
100,283,668✔
666
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
100,309,555✔
667
  SqlFunctionCtx* pCtx = pSup->pCtx;
100,304,414✔
668
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
100,294,606✔
669
  bool            needCleanup = false;
100,295,660✔
670

671
  for (int32_t j = 0; j < numOfExprs; ++j) {
360,096,884✔
672
    needCleanup |= pCtx[j].needCleanup;
259,782,331✔
673
  }
674
  if (!needCleanup) {
100,314,553✔
675
    return;
99,672,182✔
676
  }
677

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

688

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

699
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
54,204,641✔
700
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
701
  int32_t         numOfExprs = pSup->numOfExprs;
54,204,641✔
702
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
54,209,136✔
703
  SqlFunctionCtx* pCtx = pSup->pCtx;
54,207,653✔
704
  bool            needCleanup = false;
54,201,657✔
705
  for (int32_t j = 0; j < numOfExprs; ++j) {
160,968,913✔
706
    needCleanup |= pCtx[j].needCleanup;
106,756,164✔
707
  }
708
  if (!needCleanup) {
54,212,749✔
709
    return;
54,207,409✔
710
  }
711

712
  // begin from last iter
713
  void*   pData = pGroupResInfo->dataPos;
5,340✔
714
  int32_t iter = pGroupResInfo->iter;
4,952✔
715
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
5,380✔
716
    SResultRowPosition* pos = pData;
428✔
717

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

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

726
    for (int32_t j = 0; j < numOfExprs; ++j) {
8,988✔
727
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
8,560✔
728
      if (pCtx[j].fpSet.cleanup) {
8,560✔
729
        pCtx[j].fpSet.cleanup(&pCtx[j]);
856✔
730
      }
731
    }
732

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

737
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
154,488,599✔
738
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
739
  if (cleanGroupResInfo) {
154,488,599✔
740
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
100,303,709✔
741
  } else {
742
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
54,184,890✔
743
  }
744
}
154,481,606✔
745
void cleanupAggSup(SAggSupporter* pAggSup) {
320,624,858✔
746
  taosMemoryFreeClear(pAggSup->keyBuf);
320,624,858✔
747
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
320,702,357✔
748
  destroyDiskbasedBuf(pAggSup->pResultBuf);
320,725,816✔
749
  memset(pAggSup, 0, sizeof(SAggSupporter));
320,649,009✔
750
}
320,649,009✔
751

752
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
313,518,074✔
753
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
754
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
313,518,074✔
755
  if (code != TSDB_CODE_SUCCESS) {
313,295,348✔
756
    return code;
×
757
  }
758

759
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
313,295,348✔
760
  if (code != TSDB_CODE_SUCCESS) {
313,533,373✔
761
    return code;
×
762
  }
763

764
  for (int32_t i = 0; i < numOfCols; ++i) {
1,188,847,511✔
765
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
875,326,740✔
766
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
875,352,837✔
767
    if (pState) {
875,051,083✔
768
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
769
      pSup->pCtx[i].saveHandle.pState = pState;
×
770
      pSup->pCtx[i].exprIdx = i;
×
771
    } else {
772
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
875,051,083✔
773
    }
774
  }
775

776
  return TSDB_CODE_SUCCESS;
313,520,771✔
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✔
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]);
32,509,706✔
798
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
32,511,641✔
799

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

802
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
32,522,607✔
803
      pEntryInfo->isNullRes = 0;
32,523,897✔
804
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
32,524,112✔
805
        pEntryInfo->isNullRes = 1;
215✔
806
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
32,523,252✔
807
        void* v = nodesGetValueFromNode(valueNode);
13,507✔
808
        memcpy(p, v, varDataTLen(v));
9,636✔
809
      } else {
810
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
32,511,251✔
811
      }
812
      
813
      pEntryInfo->numOfRes = 1;
32,520,457✔
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✔
832
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
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) {
428✔
840
            pCtx[k].fpSet.cleanup(&pCtx[k]);
428✔
841
          }
842
          TAOS_CHECK_EXIT(code);
428✔
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));
428✔
855
    taskInfo->code = code;
428✔
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) {
4,023,987✔
874
  SAggOperatorInfo* pAgg = pOper->info;
4,023,987✔
875
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
4,025,171✔
876
  
877
  pOper->status = OP_NOT_OPENED;
4,025,159✔
878
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
4,025,172✔
879
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
4,025,172✔
880
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
4,025,387✔
881
                      pAgg->cleanGroupResInfo);
4,025,172✔
882
  cleanupGroupResInfo(&pAgg->groupResInfo);
4,024,203✔
883
  resetBasicOperatorState(&pAgg->binfo);
4,023,475✔
884
  
885
  pAgg->pNewGroupBlock = NULL;
4,025,387✔
886

887
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
8,048,210✔
888
    keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
4,025,387✔
889

890
  if (code == 0) {
4,024,525✔
891
    code = resetExprSupp(&pAgg->scalarExprSup, pTaskInfo, pAggNode->pExprs, NULL,
4,024,309✔
892
                          &pTaskInfo->storageAPI.functionStore);
893
  }
894

895
  pAgg->groupId = UINT64_MAX;
4,025,172✔
896
  pAgg->cleanGroupResInfo = false;
4,024,956✔
897
  pAgg->hasValidBlock = false;
4,024,956✔
898
  return 0;
4,024,955✔
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