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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

76.86
/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,
76,515,199✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
76,515,199✔
79

80
  int32_t    lino = 0;
76,533,686✔
81
  int32_t    code = 0;
76,533,686✔
82
  int32_t    num = 0;
76,533,686✔
83
  SExprInfo* pExprInfo = NULL;
76,534,200✔
84
  int32_t    numOfScalarExpr = 0;
76,549,781✔
85
  SExprInfo* pScalarExprInfo = NULL;
76,528,312✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
76,524,162✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
76,384,398✔
89
  if (pInfo == NULL || pOperator == NULL) {
76,358,245✔
90
    code = terrno;
×
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
76,362,972✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
76,380,905✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
76,547,232✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
76,547,232✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
76,541,488✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
76,541,488✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
76,556,545✔
105
  TSDB_CHECK_CODE(code, lino, _error);
76,528,108✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
153,020,559✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
76,532,378✔
109
  TSDB_CHECK_CODE(code, lino, _error);
76,503,218✔
110

111
  if (pAggNode->pExprs != NULL) {
76,503,218✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
18,261,926✔
113
    TSDB_CHECK_CODE(code, lino, _error);
18,265,259✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
75,884,885✔
117
  TSDB_CHECK_CODE(code, lino, _error);
75,862,583✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
75,843,082✔
120
                            GET_STM_RTINFO(pTaskInfo));
75,862,583✔
121
  TSDB_CHECK_CODE(code, lino, _error);
75,837,028✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
75,837,028✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
75,845,550✔
125
  pInfo->groupId = UINT64_MAX;
75,821,239✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
75,836,120✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
75,821,435✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
75,835,292✔
129
  pInfo->pOperator = pOperator;
75,819,603✔
130
  pInfo->cleanGroupResInfo = false;
75,795,357✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
75,785,442✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
75,860,254✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
75,857,056✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
75,874,778✔
137

138
  pOperator->pPhyNode = pAggNode;
75,864,908✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
75,864,461✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
58,409,179✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
58,401,183✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
58,308,816✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
75,718,033✔
147
  if (code != TSDB_CODE_SUCCESS) {
75,853,088✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
75,853,088✔
152
  return TSDB_CODE_SUCCESS;
75,875,239✔
153

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

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

171
  if (pInfo->pOperator) {
76,590,022✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
75,965,925✔
173
                      pInfo->cleanGroupResInfo);
75,968,707✔
174
    pInfo->pOperator = NULL;
75,965,381✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
76,590,457✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
76,580,523✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
76,578,221✔
179
  taosMemoryFreeClear(param);
76,577,475✔
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) {
89,785,022✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
89,785,022✔
191
  int32_t           lino = 0;
89,785,022✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
89,785,022✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
89,779,196✔
194

195
  if(!pAggInfo) {
89,722,372✔
196
    qError("function:%s, pAggInfo is NULL", __func__);
×
197
    return false;
×
198
  }
199
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
89,722,372✔
200
    return false;
8,711,905✔
201
  }
202

203
  SExprSupp*   pSup = &pOperator->exprSupp;
80,959,031✔
204
  int64_t      st = taosGetTimestampUs();
81,039,796✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
81,039,796✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
81,053,003✔
207

208
  pAggInfo->cleanGroupResInfo = false;
81,034,901✔
209
  if (pBlock) {
81,031,277✔
210
    pAggInfo->pNewGroupBlock = NULL;
626,293✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
626,293✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
626,293✔
213
    QUERY_CHECK_CODE(code, lino, _end);
626,293✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
626,293✔
215
    QUERY_CHECK_CODE(code, lino, _end);
626,293✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
626,293✔
218
    QUERY_CHECK_CODE(code, lino, _end);
598,968✔
219
  }
220
  while (1) {
258,114,025✔
221
    bool blockAllocated = false;
339,117,920✔
222
    pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
339,117,920✔
223
    if (pOperator->pDownstreamGetParams) {
337,383,309✔
224
      pOperator->pDownstreamGetParams[0] = NULL;
×
225
    }
226
    if (pBlock == NULL) {
337,459,731✔
227
      if (!pAggInfo->hasValidBlock) {
82,218,562✔
228
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
18,829,552✔
229
        QUERY_CHECK_CODE(code, lino, _end);
18,823,166✔
230

231
        if (pBlock == NULL) {
18,823,166✔
232
          break;
14,120,876✔
233
        }
234
        blockAllocated = true;
4,702,290✔
235
      } else {
236
        break;
63,387,261✔
237
      }
238
    }
239
    pAggInfo->hasValidBlock = true;
259,943,459✔
240
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
259,984,396✔
241

242
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
259,913,077✔
243

244
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
245
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
259,826,273✔
246
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
77,730,147✔
247
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
77,715,342✔
248
      if (code != TSDB_CODE_SUCCESS) {
77,736,840✔
249
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
1,110,798✔
250
        T_LONG_JMP(pTaskInfo->env, code);
1,110,798✔
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) {
258,722,619✔
255
      pAggInfo->pNewGroupBlock = pBlock;
638,227✔
256
      break;
638,227✔
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);
258,136,432✔
260
    if (code != TSDB_CODE_SUCCESS) {
258,138,222✔
261
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
262
      T_LONG_JMP(pTaskInfo->env, code);
×
263
    }
264
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
258,138,222✔
265
    if (code != TSDB_CODE_SUCCESS) {
258,137,243✔
266
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
267
      T_LONG_JMP(pTaskInfo->env, code);
×
268
    }
269

270
    code = doAggregateImpl(pOperator, pSup->pCtx);
258,137,243✔
271
    if (code != TSDB_CODE_SUCCESS) {
258,126,581✔
272
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
2,792✔
273
      T_LONG_JMP(pTaskInfo->env, code);
2,792✔
274
    }
275

276
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
258,123,789✔
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) {
78,146,364✔
281
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
282
  }
283

284
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
78,142,402✔
285
  QUERY_CHECK_CODE(code, lino, _end);
78,151,671✔
286
  pAggInfo->cleanGroupResInfo = true;
78,151,671✔
287

288
_end:
78,143,233✔
289
  if (code != TSDB_CODE_SUCCESS) {
78,151,088✔
290
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2,025✔
291
    pTaskInfo->code = code;
2,025✔
292
    T_LONG_JMP(pTaskInfo->env, code);
×
293
  }
294
  return pBlock != NULL;
78,149,413✔
295
}
296

297
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
150,471,922✔
298
  int32_t           code = TSDB_CODE_SUCCESS;
150,471,922✔
299
  int32_t           lino = 0;
150,471,922✔
300
  SAggOperatorInfo* pAggInfo = pOperator->info;
150,471,922✔
301
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
150,505,974✔
302

303
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
150,480,422✔
304
    (*ppRes) = NULL;
60,704,767✔
305
    return code;
60,705,585✔
306
  }
307

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

318
  }
319

320
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
89,778,932✔
321
  bool           hasNewGroups = false;
89,771,752✔
322
  do {
323
    hasNewGroups = nextGroupedResult(pOperator);
89,781,753✔
324
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
86,864,943✔
325
    QUERY_CHECK_CODE(code, lino, _end);
86,869,873✔
326

327
    while (1) {
328
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
86,869,873✔
329
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
86,860,637✔
330
      QUERY_CHECK_CODE(code, lino, _end);
86,860,396✔
331

332
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
86,860,396✔
333
        if (!hasNewGroups) setOperatorCompleted(pOperator);
78,154,332✔
334
        break;
78,152,173✔
335
      }
336

337
      if (pInfo->pRes->info.rows > 0) {
8,712,925✔
338
        break;
8,712,925✔
339
      }
340
    }
341
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
86,865,098✔
342

343
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
86,851,097✔
344
  pOperator->resultInfo.totalRows += rows;
86,855,061✔
345

346
_end:
86,850,943✔
347
  if (code != TSDB_CODE_SUCCESS) {
86,850,943✔
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);
86,850,943✔
354

355
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
86,843,804✔
356
  return code;
86,850,781✔
357
}
358

359
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
258,832,294✔
360
  int32_t code = TSDB_CODE_SUCCESS;
258,832,294✔
361
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
258,832,294✔
362
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
140,482✔
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;
12,140,477✔
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) {
2,792✔
381
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
382
        }
383
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
2,792✔
384
        return code;
2,792✔
385
      }
386
    }
387
  }
388

389
  return TSDB_CODE_SUCCESS;
258,826,690✔
390
}
391

392
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
18,829,746✔
393
  int32_t code = TSDB_CODE_SUCCESS;
18,829,746✔
394
  int32_t lino = 0;
18,829,746✔
395
  SSDataBlock* pBlock = NULL;
18,829,746✔
396
  if (!tsCountAlwaysReturnValue) {
18,829,478✔
397
    return TSDB_CODE_SUCCESS;
5,683,883✔
398
  }
399

400
  SAggOperatorInfo* pAggInfo = pOperator->info;
13,145,595✔
401
  if (pAggInfo->groupKeyOptimized) {
13,140,936✔
402
    return TSDB_CODE_SUCCESS;
2,712,285✔
403
  }
404

405
  SOperatorInfo* downstream = pOperator->pDownstream[0];
10,424,416✔
406
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
10,427,391✔
407
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
10,394,052✔
408
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
10,379,788✔
409
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
7,519,962✔
410
    return TSDB_CODE_SUCCESS;
46,530✔
411
  }
412

413
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
10,386,415✔
414

415
  if (!pAggInfo->hasCountFunc) {
10,380,237✔
416
    return TSDB_CODE_SUCCESS;
5,674,548✔
417
  }
418

419
  code = createDataBlock(&pBlock);
4,702,960✔
420
  if (code) {
4,704,728✔
421
    return code;
×
422
  }
423

424
  pBlock->info.rows = 1;
4,704,728✔
425
  pBlock->info.capacity = 0;
4,704,192✔
426

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

433
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
7,092,118✔
434
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
14,387,393✔
435
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
7,297,021✔
436
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,296,600✔
437
        int32_t slotId = pFuncParam->pCol->slotId;
7,294,018✔
438
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
7,294,692✔
439
        if (slotId >= numOfCols) {
7,293,903✔
440
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
5,030,978✔
441
          QUERY_CHECK_CODE(code, lino, _end);
5,031,162✔
442

443
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
13,208,948✔
444
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
8,177,365✔
445
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
8,177,786✔
446
          }
447
        }
448
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
1,908✔
449
        // do nothing
450
      }
451
    }
452
  }
453

454
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
4,705,218✔
455
  QUERY_CHECK_CODE(code, lino, _end);
4,704,151✔
456

457
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
12,881,475✔
458
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
8,178,391✔
459
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
8,177,970✔
460
    colDataSetNULL(pColInfoData, 0);
461
  }
462
  *ppBlock = pBlock;
4,704,641✔
463

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

472
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
259,216,047✔
473
  if (!blockAllocated) {
259,216,047✔
474
    return;
254,547,204✔
475
  }
476

477
  blockDataDestroy(*ppBlock);
4,668,843✔
478
  *ppBlock = NULL;
4,705,333✔
479
}
480

481
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
258,807,337✔
482
  int32_t           code = TSDB_CODE_SUCCESS;
258,807,337✔
483
  SAggOperatorInfo* pAggInfo = pOperator->info;
258,807,337✔
484
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
258,864,885✔
485
    return code;
86,621,261✔
486
  }
487

488
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
172,194,136✔
489

490
  // record the current active group id
491
  pAggInfo->groupId = groupId;
172,178,618✔
492
  return code;
172,205,001✔
493
}
494

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

502
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
172,193,185✔
503
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
172,184,497✔
504
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
172,173,893✔
505

506
  SResultRow* pResultRow =
507
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
172,166,070✔
508
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
509
  if (pResultRow == NULL || pTaskInfo->code != 0) {
172,199,114✔
510
    code = pTaskInfo->code;
56,270✔
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) {
172,148,498✔
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);
172,181,466✔
524
  QUERY_CHECK_CODE(code, lino, _end);
172,172,780✔
525

526
_end:
172,172,780✔
527
  if (code != TSDB_CODE_SUCCESS) {
172,172,780✔
528
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
529
  }
530
  return code;
172,187,506✔
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,
285,061,971✔
591
                         const char* pKey) {
592
  int32_t code = 0;
285,061,971✔
593
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
594

595
  pAggSup->currentPageId = -1;
285,061,971✔
596
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
285,219,825✔
597
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
284,952,649✔
598
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
285,047,956✔
599

600
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
284,807,005✔
601
    return terrno;
1,102✔
602
  }
603

604
  uint32_t defaultPgsz = 0;
284,862,212✔
605
  int64_t defaultBufsz = 0;
284,875,177✔
606
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
284,944,720✔
607
  if (code) {
284,707,791✔
608
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
609
    return code;
×
610
  }
611

612
  if (!osTempSpaceAvailable()) {
284,707,791✔
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);
285,105,414✔
619
  if (code != TSDB_CODE_SUCCESS) {
285,048,054✔
620
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
621
    return code;
×
622
  }
623

624
  return code;
285,048,054✔
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,
92,039,090✔
664
                                  SGroupResInfo* pGroupResInfo) {
665
  int32_t         numOfExprs = pSup->numOfExprs;
92,039,090✔
666
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
92,043,753✔
667
  SqlFunctionCtx* pCtx = pSup->pCtx;
92,039,175✔
668
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
92,039,121✔
669
  bool            needCleanup = false;
92,043,392✔
670

671
  for (int32_t j = 0; j < numOfExprs; ++j) {
284,287,154✔
672
    needCleanup |= pCtx[j].needCleanup;
192,244,721✔
673
  }
674
  if (!needCleanup) {
92,042,433✔
675
    return;
91,436,929✔
676
  }
677

678
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
605,504✔
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,
50,042,958✔
700
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
701
  int32_t         numOfExprs = pSup->numOfExprs;
50,042,958✔
702
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
50,044,049✔
703
  SqlFunctionCtx* pCtx = pSup->pCtx;
50,041,103✔
704
  bool            needCleanup = false;
50,037,058✔
705
  for (int32_t j = 0; j < numOfExprs; ++j) {
132,688,528✔
706
    needCleanup |= pCtx[j].needCleanup;
82,646,056✔
707
  }
708
  if (!needCleanup) {
50,042,472✔
709
    return;
50,037,051✔
710
  }
711

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

718
    SFilePage* page = getBufPage(pBuf, pos->pageId);
412✔
719
    if (page == NULL) {
412✔
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);
412✔
725

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

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

737
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
142,081,706✔
738
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
739
  if (cleanGroupResInfo) {
142,081,706✔
740
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
92,039,908✔
741
  } else {
742
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
50,041,798✔
743
  }
744
}
142,082,865✔
745
void cleanupAggSup(SAggSupporter* pAggSup) {
291,935,270✔
746
  taosMemoryFreeClear(pAggSup->keyBuf);
291,935,270✔
747
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
291,929,880✔
748
  destroyDiskbasedBuf(pAggSup->pResultBuf);
291,938,994✔
749
  memset(pAggSup, 0, sizeof(SAggSupporter));
291,908,462✔
750
}
291,908,462✔
751

752
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
285,099,441✔
753
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
754
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
285,099,441✔
755
  if (code != TSDB_CODE_SUCCESS) {
284,837,163✔
756
    return code;
×
757
  }
758

759
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
284,837,163✔
760
  if (code != TSDB_CODE_SUCCESS) {
285,096,742✔
761
    return code;
×
762
  }
763

764
  for (int32_t i = 0; i < numOfCols; ++i) {
1,029,680,842✔
765
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
744,572,975✔
766
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
744,533,564✔
767
    if (pState) {
744,313,626✔
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;
744,313,626✔
773
    }
774
  }
775

776
  return TSDB_CODE_SUCCESS;
285,107,867✔
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]);
7,084,928✔
798
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
7,085,340✔
799

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

802
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
7,084,516✔
803
      pEntryInfo->isNullRes = 0;
7,084,516✔
804
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
7,084,516✔
UNCOV
805
        pEntryInfo->isNullRes = 1;
×
806
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
7,084,722✔
807
        void* v = nodesGetValueFromNode(valueNode);
10,844✔
808
        memcpy(p, v, varDataTLen(v));
9,196✔
809
      } else {
810
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
7,074,908✔
811
      }
812
      
813
      pEntryInfo->numOfRes = 1;
7,084,928✔
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) {
412✔
840
            pCtx[k].fpSet.cleanup(&pCtx[k]);
412✔
841
          }
842
          TAOS_CHECK_EXIT(code);
412✔
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));
412✔
855
    taskInfo->code = code;
412✔
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,985,595✔
874
  SAggOperatorInfo* pAgg = pOper->info;
4,985,595✔
875
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
4,985,989✔
876
  
877
  pOper->status = OP_NOT_OPENED;
4,986,190✔
878
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
4,986,190✔
879
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
4,986,190✔
880
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
4,985,989✔
881
                      pAgg->cleanGroupResInfo);
4,985,796✔
882
  cleanupGroupResInfo(&pAgg->groupResInfo);
4,985,997✔
883
  resetBasicOperatorState(&pAgg->binfo);
4,985,188✔
884
  
885
  pAgg->pNewGroupBlock = NULL;
4,985,788✔
886

887
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
9,969,679✔
888
    keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
4,985,788✔
889

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

895
  pAgg->groupId = UINT64_MAX;
4,985,185✔
896
  pAgg->cleanGroupResInfo = false;
4,984,577✔
897
  pAgg->hasValidBlock = false;
4,985,185✔
898
  return 0;
4,985,381✔
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