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

taosdata / TDengine / #4947

02 Feb 2026 09:27AM UTC coverage: 66.872% (-0.06%) from 66.932%
#4947

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

3610 existing lines in 126 files now uncovered.

205539 of 307364 relevant lines covered (66.87%)

125933663.91 hits per line

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

76.29
/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,
68,168,887✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
68,168,887✔
79

80
  int32_t    lino = 0;
68,186,548✔
81
  int32_t    code = 0;
68,186,548✔
82
  int32_t    num = 0;
68,186,548✔
83
  SExprInfo* pExprInfo = NULL;
68,184,608✔
84
  int32_t    numOfScalarExpr = 0;
68,172,023✔
85
  SExprInfo* pScalarExprInfo = NULL;
68,162,689✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
68,167,958✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
68,053,951✔
89
  if (pInfo == NULL || pOperator == NULL) {
68,033,095✔
90
    code = terrno;
1,666✔
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
68,031,429✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
68,064,173✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
68,220,615✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
68,220,615✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
68,171,451✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
68,171,451✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
68,193,453✔
105
  TSDB_CHECK_CODE(code, lino, _error);
68,177,474✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
136,367,981✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
68,184,860✔
109
  TSDB_CHECK_CODE(code, lino, _error);
68,126,233✔
110

111
  if (pAggNode->pExprs != NULL) {
68,126,233✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
17,195,692✔
113
    TSDB_CHECK_CODE(code, lino, _error);
17,204,016✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
68,145,012✔
117
  TSDB_CHECK_CODE(code, lino, _error);
68,097,416✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
68,146,746✔
120
                            GET_STM_RTINFO(pTaskInfo));
68,097,416✔
121
  TSDB_CHECK_CODE(code, lino, _error);
68,106,457✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
68,106,457✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
68,143,346✔
125
  pInfo->groupId = UINT64_MAX;
68,130,298✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
68,069,010✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
68,095,048✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
68,131,692✔
129
  pInfo->pOperator = pOperator;
68,053,150✔
130
  pInfo->cleanGroupResInfo = false;
68,143,963✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
68,125,240✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
68,135,597✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
68,113,166✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
68,141,208✔
137

138
  pOperator->pPhyNode = pAggNode;
68,117,897✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
68,123,965✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
51,551,781✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
51,554,568✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
51,545,901✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
68,079,537✔
147
  if (code != TSDB_CODE_SUCCESS) {
68,151,465✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
68,151,465✔
152
  return TSDB_CODE_SUCCESS;
68,166,383✔
153

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

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

171
  if (pInfo->pOperator) {
68,210,133✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
68,189,654✔
173
                      pInfo->cleanGroupResInfo);
68,193,135✔
174
    pInfo->pOperator = NULL;
68,157,578✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
68,185,754✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
68,184,004✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
68,176,947✔
179
  taosMemoryFreeClear(param);
68,196,825✔
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) {
86,016,050✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
86,016,050✔
191
  int32_t           lino = 0;
86,016,050✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
86,016,050✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
86,015,094✔
194

195
  if(!pAggInfo) {
85,974,052✔
196
    qError("function:%s, pAggInfo is NULL", __func__);
×
197
    return false;
×
198
  }
199
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
85,974,052✔
200
    return false;
9,335,191✔
201
  }
202

203
  SExprSupp*   pSup = &pOperator->exprSupp;
76,680,984✔
204
  int64_t      st = taosGetTimestampUs();
76,675,501✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
76,675,501✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
76,667,615✔
207

208
  pAggInfo->cleanGroupResInfo = false;
76,663,711✔
209
  if (pBlock) {
76,665,989✔
210
    pAggInfo->pNewGroupBlock = NULL;
666,880✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
666,880✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
666,880✔
213
    QUERY_CHECK_CODE(code, lino, _end);
666,365✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
666,365✔
215
    QUERY_CHECK_CODE(code, lino, _end);
666,880✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
666,880✔
218
    QUERY_CHECK_CODE(code, lino, _end);
676,471✔
219
  }
220
  while (1) {
503,432,501✔
221
    bool blockAllocated = false;
580,108,081✔
222
    pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
580,108,081✔
223
    if (pOperator->pDownstreamGetParams) {
578,034,003✔
224
      pOperator->pDownstreamGetParams[0] = NULL;
17,404,278✔
225
    }
226
    if (pBlock == NULL) {
578,038,511✔
227
      if (!pAggInfo->hasValidBlock) {
77,157,387✔
228
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
17,883,675✔
229
        QUERY_CHECK_CODE(code, lino, _end);
17,881,839✔
230

231
        if (pBlock == NULL) {
17,881,839✔
232
          break;
14,590,343✔
233
        }
234
        blockAllocated = true;
3,291,496✔
235
      } else {
236
        break;
59,273,932✔
237
      }
238
    }
239
    pAggInfo->hasValidBlock = true;
504,172,620✔
240
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
504,199,215✔
241

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

244
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
245
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
504,158,497✔
246
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
75,126,468✔
247
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
75,127,012✔
248
      if (code != TSDB_CODE_SUCCESS) {
75,092,282✔
249
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
31,720✔
250
        T_LONG_JMP(pTaskInfo->env, code);
31,720✔
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) {
504,095,041✔
255
      pAggInfo->pNewGroupBlock = pBlock;
679,265✔
256
      break;
679,265✔
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);
503,419,542✔
260
    if (code != TSDB_CODE_SUCCESS) {
503,421,031✔
261
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
262
      T_LONG_JMP(pTaskInfo->env, code);
×
263
    }
264
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
503,421,031✔
265
    if (code != TSDB_CODE_SUCCESS) {
503,454,966✔
266
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
267
      T_LONG_JMP(pTaskInfo->env, code);
×
268
    }
269

270
    code = doAggregateImpl(pOperator, pSup->pCtx);
503,454,966✔
271
    if (code != TSDB_CODE_SUCCESS) {
503,403,652✔
272
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
2,784✔
273
      T_LONG_JMP(pTaskInfo->env, code);
2,784✔
274
    }
275

276
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
503,400,868✔
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) {
74,543,540✔
281
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
282
  }
283

284
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
74,530,084✔
285
  QUERY_CHECK_CODE(code, lino, _end);
74,540,139✔
286
  pAggInfo->cleanGroupResInfo = true;
74,540,139✔
287

288
_end:
74,543,373✔
289
  if (code != TSDB_CODE_SUCCESS) {
74,539,863✔
290
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
350✔
291
    pTaskInfo->code = code;
350✔
292
    T_LONG_JMP(pTaskInfo->env, code);
×
293
  }
294
  return pBlock != NULL;
74,540,152✔
295
}
296

297
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
141,583,179✔
298
  int32_t           code = TSDB_CODE_SUCCESS;
141,583,179✔
299
  int32_t           lino = 0;
141,583,179✔
300
  SAggOperatorInfo* pAggInfo = pOperator->info;
141,583,179✔
301
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
141,597,043✔
302

303
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
141,588,470✔
304
    (*ppRes) = NULL;
55,586,551✔
305
    return code;
55,586,551✔
306
  }
307

308
  if (pOperator->pOperatorGetParam) {
85,997,233✔
309
    if (pOperator->status == OP_EXEC_DONE) {
5,768,506✔
310
      pOperator->status = OP_OPENED;
3,604,668✔
311
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
3,608,130✔
312
      pAggInfo->groupId = UINT64_MAX;
3,608,568✔
313
      pAggInfo->hasValidBlock = false;
3,609,072✔
314
    }
315
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
5,764,664✔
316
    pOperator->pOperatorGetParam = NULL;
5,768,886✔
317

318
  }
319

320
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
86,004,281✔
321
  bool           hasNewGroups = false;
86,001,636✔
322
  do {
323
    hasNewGroups = nextGroupedResult(pOperator);
86,012,445✔
324
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
83,874,727✔
325
    QUERY_CHECK_CODE(code, lino, _end);
83,879,505✔
326

327
    while (1) {
328
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
83,879,505✔
329
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
83,879,366✔
330
      QUERY_CHECK_CODE(code, lino, _end);
83,880,780✔
331

332
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
83,880,780✔
333
        if (!hasNewGroups) setOperatorCompleted(pOperator);
74,539,583✔
334
        break;
74,536,977✔
335
      }
336

337
      if (pInfo->pRes->info.rows > 0) {
9,341,515✔
338
        break;
9,341,515✔
339
      }
340
    }
341
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
83,878,492✔
342

343
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
83,868,185✔
344
  pOperator->resultInfo.totalRows += rows;
83,869,181✔
345

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

355
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
83,866,131✔
356
  return code;
83,870,613✔
357
}
358

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

389
  return TSDB_CODE_SUCCESS;
504,093,504✔
390
}
391

392
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
17,883,591✔
393
  int32_t code = TSDB_CODE_SUCCESS;
17,883,591✔
394
  int32_t lino = 0;
17,883,591✔
395
  SSDataBlock* pBlock = NULL;
17,883,591✔
396
  if (!tsCountAlwaysReturnValue) {
17,883,771✔
397
    return TSDB_CODE_SUCCESS;
6,057,436✔
398
  }
399

400
  SAggOperatorInfo* pAggInfo = pOperator->info;
11,826,335✔
401
  if (pAggInfo->groupKeyOptimized) {
11,820,788✔
402
    return TSDB_CODE_SUCCESS;
4,647,535✔
403
  }
404

405
  SOperatorInfo* downstream = pOperator->pDownstream[0];
7,169,642✔
406
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
7,171,101✔
407
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
7,142,480✔
408
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
7,114,104✔
409
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
4,131,274✔
410
    return TSDB_CODE_SUCCESS;
62,700✔
411
  }
412

413
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
7,102,448✔
414

415
  if (!pAggInfo->hasCountFunc) {
7,109,730✔
416
    return TSDB_CODE_SUCCESS;
3,818,264✔
417
  }
418

419
  code = createDataBlock(&pBlock);
3,289,541✔
420
  if (code) {
3,291,401✔
421
    return code;
×
422
  }
423

424
  pBlock->info.rows = 1;
3,291,401✔
425
  pBlock->info.capacity = 0;
3,290,262✔
426

427
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
9,044,561✔
428
    SColumnInfoData colInfo = {0};
5,752,540✔
429
    colInfo.hasNull = true;
5,753,160✔
430
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
5,753,160✔
431
    colInfo.info.bytes = 1;
5,753,160✔
432

433
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
5,753,160✔
434
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
11,720,629✔
435
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
5,968,089✔
436
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
5,966,440✔
437
        int32_t slotId = pFuncParam->pCol->slotId;
5,963,739✔
438
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
5,962,064✔
439
        if (slotId >= numOfCols) {
5,963,330✔
440
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
3,655,333✔
441
          QUERY_CHECK_CODE(code, lino, _end);
3,654,111✔
442

443
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
9,904,048✔
444
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
6,248,306✔
445
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
6,249,937✔
446
          }
447
        }
448
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
4,350✔
449
        // do nothing
450
      }
451
    }
452
  }
453

454
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
3,291,810✔
455
  QUERY_CHECK_CODE(code, lino, _end);
3,292,430✔
456

457
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
9,542,412✔
458
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,249,937✔
459
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
6,249,317✔
460
    colDataSetNULL(pColInfoData, 0);
461
  }
462
  *ppBlock = pBlock;
3,291,402✔
463

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

472
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
503,449,073✔
473
  if (!blockAllocated) {
503,449,073✔
474
    return;
500,187,033✔
475
  }
476

477
  blockDataDestroy(*ppBlock);
3,262,040✔
478
  *ppBlock = NULL;
3,290,034✔
479
}
480

481
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
504,098,629✔
482
  int32_t           code = TSDB_CODE_SUCCESS;
504,098,629✔
483
  SAggOperatorInfo* pAggInfo = pOperator->info;
504,098,629✔
484
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
504,151,670✔
485
    return code;
349,730,358✔
486
  }
487

488
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
154,413,107✔
489

490
  // record the current active group id
491
  pAggInfo->groupId = groupId;
154,384,201✔
492
  return code;
154,401,076✔
493
}
494

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

502
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
154,398,606✔
503
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
154,417,104✔
504
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
154,403,747✔
505

506
  SResultRow* pResultRow =
507
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
154,383,440✔
508
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
509
  if (pResultRow == NULL || pTaskInfo->code != 0) {
154,399,767✔
510
    code = pTaskInfo->code;
16,110✔
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) {
154,384,768✔
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);
154,412,426✔
524
  QUERY_CHECK_CODE(code, lino, _end);
154,391,772✔
525

526
_end:
154,391,772✔
527
  if (code != TSDB_CODE_SUCCESS) {
154,391,772✔
528
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
529
  }
530
  return code;
154,385,302✔
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,
201,782,492✔
591
                         const char* pKey) {
592
  int32_t code = 0;
201,782,492✔
593
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
594

595
  pAggSup->currentPageId = -1;
201,782,492✔
596
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
201,778,404✔
597
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
201,661,772✔
598
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
201,731,608✔
599

600
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
201,666,870✔
601
    return terrno;
22✔
602
  }
603

604
  uint32_t defaultPgsz = 0;
201,685,443✔
605
  int64_t defaultBufsz = 0;
201,641,882✔
606
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
201,741,622✔
607
  if (code) {
201,570,715✔
608
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
609
    return code;
×
610
  }
611

612
  if (!osTempSpaceAvailable()) {
201,570,715✔
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);
201,808,253✔
619
  if (code != TSDB_CODE_SUCCESS) {
201,789,514✔
620
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
621
    return code;
×
622
  }
623

624
  return code;
201,789,514✔
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,
75,342,565✔
664
                                  SGroupResInfo* pGroupResInfo) {
665
  int32_t         numOfExprs = pSup->numOfExprs;
75,342,565✔
666
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
75,354,141✔
667
  SqlFunctionCtx* pCtx = pSup->pCtx;
75,340,507✔
668
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
75,337,852✔
669
  bool            needCleanup = false;
75,343,948✔
670

671
  for (int32_t j = 0; j < numOfExprs; ++j) {
83,230,953✔
672
    needCleanup |= pCtx[j].needCleanup;
7,887,723✔
673
  }
674
  if (!needCleanup) {
75,343,230✔
675
    return;
75,348,587✔
676
  }
677

678
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
158✔
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,
33,900,714✔
700
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
701
  int32_t         numOfExprs = pSup->numOfExprs;
33,900,714✔
702
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
33,900,605✔
703
  SqlFunctionCtx* pCtx = pSup->pCtx;
33,893,451✔
704
  bool            needCleanup = false;
33,899,592✔
705
  for (int32_t j = 0; j < numOfExprs; ++j) {
43,590,137✔
706
    needCleanup |= pCtx[j].needCleanup;
9,688,870✔
707
  }
708
  if (!needCleanup) {
33,901,267✔
709
    return;
33,902,954✔
710
  }
711

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

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

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

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

737
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
109,243,320✔
738
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
739
  if (cleanGroupResInfo) {
109,243,320✔
740
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
75,350,830✔
741
  } else {
742
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
33,892,490✔
743
  }
744
}
109,214,726✔
745
void cleanupAggSup(SAggSupporter* pAggSup) {
202,203,553✔
746
  taosMemoryFreeClear(pAggSup->keyBuf);
202,203,553✔
747
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
202,237,350✔
748
  destroyDiskbasedBuf(pAggSup->pResultBuf);
202,217,471✔
749
  memset(pAggSup, 0, sizeof(SAggSupporter));
202,169,466✔
750
}
202,169,466✔
751

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

759
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
201,727,488✔
760
  if (code != TSDB_CODE_SUCCESS) {
201,769,789✔
761
    return code;
×
762
  }
763

764
  for (int32_t i = 0; i < numOfCols; ++i) {
1,009,548,431✔
765
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
807,908,049✔
766
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
807,781,608✔
767
    if (pState) {
807,667,039✔
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;
807,667,039✔
773
    }
774
  }
775

776
  return TSDB_CODE_SUCCESS;
201,640,382✔
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]);
31,844,006✔
798
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
31,845,691✔
799

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

802
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
31,859,761✔
803
      pEntryInfo->isNullRes = 0;
31,859,561✔
804
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
31,859,561✔
UNCOV
805
        pEntryInfo->isNullRes = 1;
×
806
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
31,860,186✔
807
        void* v = nodesGetValueFromNode(valueNode);
8,937✔
808
        memcpy(p, v, varDataTLen(v));
9,812✔
809
      } else {
810
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
31,848,454✔
811
      }
812
      
813
      pEntryInfo->numOfRes = 1;
31,857,246✔
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) {
431✔
840
            pCtx[k].fpSet.cleanup(&pCtx[k]);
431✔
841
          }
842
          TAOS_CHECK_EXIT(code);
431✔
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));
431✔
855
    taskInfo->code = code;
431✔
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) {
5,284,713✔
874
  SAggOperatorInfo* pAgg = pOper->info;
5,284,713✔
875
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
5,285,973✔
876
  
877
  pOper->status = OP_NOT_OPENED;
5,285,973✔
878
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
5,286,901✔
879
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
5,286,901✔
880
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
5,287,321✔
881
                      pAgg->cleanGroupResInfo);
5,285,641✔
882
  cleanupGroupResInfo(&pAgg->groupResInfo);
5,287,111✔
883
  resetBasicOperatorState(&pAgg->binfo);
5,286,476✔
884
  
885
  pAgg->pNewGroupBlock = NULL;
5,286,247✔
886

887
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
10,571,206✔
888
    keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
5,286,247✔
889

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

895
  pAgg->groupId = UINT64_MAX;
5,286,691✔
896
  pAgg->cleanGroupResInfo = false;
5,286,901✔
897
  pAgg->hasValidBlock = false;
5,286,901✔
898
  return 0;
5,287,321✔
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