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

taosdata / TDengine / #4929

16 Jan 2026 02:32AM UTC coverage: 66.723% (+0.02%) from 66.708%
#4929

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

539 existing lines in 127 files now uncovered.

203225 of 304580 relevant lines covered (66.72%)

128590777.46 hits per line

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

78.39
/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,
83,848,019✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
83,848,019✔
79

80
  int32_t    lino = 0;
83,882,433✔
81
  int32_t    code = 0;
83,882,433✔
82
  int32_t    num = 0;
83,882,433✔
83
  SExprInfo* pExprInfo = NULL;
83,883,281✔
84
  int32_t    numOfScalarExpr = 0;
83,875,024✔
85
  SExprInfo* pScalarExprInfo = NULL;
83,887,232✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
83,855,141✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
83,640,178✔
89
  if (pInfo == NULL || pOperator == NULL) {
83,613,613✔
90
    code = terrno;
40✔
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
83,617,984✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
83,660,830✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
83,942,701✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
83,942,701✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
83,877,110✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
83,877,110✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
83,908,691✔
105
  TSDB_CHECK_CODE(code, lino, _error);
83,814,802✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
167,619,790✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
83,825,414✔
109
  TSDB_CHECK_CODE(code, lino, _error);
83,822,144✔
110

111
  if (pAggNode->pExprs != NULL) {
83,822,144✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
23,056,547✔
113
    TSDB_CHECK_CODE(code, lino, _error);
23,047,256✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
83,137,357✔
117
  TSDB_CHECK_CODE(code, lino, _error);
83,106,525✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
83,036,960✔
120
                            GET_STM_RTINFO(pTaskInfo));
83,106,525✔
121
  TSDB_CHECK_CODE(code, lino, _error);
83,105,378✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
83,105,378✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
83,087,717✔
125
  pInfo->groupId = UINT64_MAX;
83,031,731✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
83,106,870✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
83,076,353✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
83,069,336✔
129
  pInfo->pOperator = pOperator;
83,047,899✔
130
  pInfo->cleanGroupResInfo = false;
82,975,670✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
82,982,144✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
83,131,904✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
83,127,851✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
83,139,042✔
137

138
  pOperator->pPhyNode = pAggNode;
83,132,123✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
83,105,899✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
63,993,037✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
64,010,874✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
63,939,238✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
83,014,969✔
147
  if (code != TSDB_CODE_SUCCESS) {
83,070,574✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
83,070,574✔
152
  return TSDB_CODE_SUCCESS;
83,084,513✔
153

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

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

171
  if (pInfo->pOperator) {
83,926,252✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
83,210,657✔
173
                      pInfo->cleanGroupResInfo);
83,250,719✔
174
    pInfo->pOperator = NULL;
83,190,622✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
83,882,386✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
83,875,315✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
83,827,688✔
179
  taosMemoryFreeClear(param);
83,847,128✔
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) {
100,327,620✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
100,327,620✔
191
  int32_t           lino = 0;
100,327,620✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
100,327,620✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
100,340,745✔
194

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

203
  SExprSupp*   pSup = &pOperator->exprSupp;
90,831,621✔
204
  int64_t      st = taosGetTimestampUs();
91,030,725✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
91,030,725✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
91,012,735✔
207

208
  pAggInfo->cleanGroupResInfo = false;
90,997,266✔
209
  if (pBlock) {
90,979,894✔
210
    pAggInfo->pNewGroupBlock = NULL;
651,676✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
651,676✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
651,676✔
213
    QUERY_CHECK_CODE(code, lino, _end);
651,676✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
651,676✔
215
    QUERY_CHECK_CODE(code, lino, _end);
651,676✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
651,676✔
218
    QUERY_CHECK_CODE(code, lino, _end);
621,483✔
219
  }
220
  while (1) {
445,371,346✔
221
    bool blockAllocated = false;
536,321,047✔
222
    pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
536,321,047✔
223
    if (pOperator->pDownstreamGetParams) {
534,227,061✔
224
      pOperator->pDownstreamGetParams[0] = NULL;
13,746,271✔
225
    }
226
    if (pBlock == NULL) {
534,330,845✔
227
      if (!pAggInfo->hasValidBlock) {
91,550,427✔
228
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
20,417,698✔
229
        QUERY_CHECK_CODE(code, lino, _end);
20,413,741✔
230

231
        if (pBlock == NULL) {
20,413,741✔
232
          break;
15,802,120✔
233
        }
234
        blockAllocated = true;
4,611,621✔
235
      } else {
236
        break;
71,135,772✔
237
      }
238
    }
239
    pAggInfo->hasValidBlock = true;
447,392,039✔
240
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
447,394,474✔
241

242
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
447,360,502✔
243

244
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
245
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
447,182,436✔
246
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
44,379,865✔
247
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
44,370,054✔
248
      if (code != TSDB_CODE_SUCCESS) {
44,379,006✔
249
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
1,217,880✔
250
        T_LONG_JMP(pTaskInfo->env, code);
1,217,880✔
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) {
445,989,741✔
255
      pAggInfo->pNewGroupBlock = pBlock;
663,947✔
256
      break;
663,947✔
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);
445,361,450✔
260
    if (code != TSDB_CODE_SUCCESS) {
445,367,472✔
261
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
262
      T_LONG_JMP(pTaskInfo->env, code);
×
263
    }
264
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
445,367,472✔
265
    if (code != TSDB_CODE_SUCCESS) {
445,419,877✔
266
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
267
      T_LONG_JMP(pTaskInfo->env, code);
×
268
    }
269

270
    code = doAggregateImpl(pOperator, pSup->pCtx);
445,419,877✔
271
    if (code != TSDB_CODE_SUCCESS) {
445,326,192✔
272
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
4,230✔
273
      T_LONG_JMP(pTaskInfo->env, code);
4,230✔
274
    }
275

276
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
445,321,962✔
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) {
87,601,839✔
281
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
282
  }
283

284
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
87,595,532✔
285
  QUERY_CHECK_CODE(code, lino, _end);
87,597,558✔
286
  pAggInfo->cleanGroupResInfo = true;
87,597,558✔
287

288
_end:
87,599,563✔
289
  if (code != TSDB_CODE_SUCCESS) {
87,594,124✔
UNCOV
290
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
291
    pTaskInfo->code = code;
×
292
    T_LONG_JMP(pTaskInfo->env, code);
×
293
  }
294
  return pBlock != NULL;
87,596,744✔
295
}
296

297
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
167,897,407✔
298
  int32_t           code = TSDB_CODE_SUCCESS;
167,897,407✔
299
  int32_t           lino = 0;
167,897,407✔
300
  SAggOperatorInfo* pAggInfo = pOperator->info;
167,897,407✔
301
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
167,923,255✔
302

303
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
167,909,314✔
304
    (*ppRes) = NULL;
67,642,612✔
305
    return code;
67,643,942✔
306
  }
307

308
  if (pOperator->pOperatorGetParam) {
100,287,220✔
309
    if (pOperator->status == OP_EXEC_DONE) {
4,612,983✔
310
      pOperator->status = OP_OPENED;
2,823,392✔
311
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
2,824,343✔
312
      pAggInfo->groupId = UINT64_MAX;
2,825,801✔
313
      pAggInfo->hasValidBlock = false;
2,826,121✔
314
    }
315
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
4,611,935✔
316
    pOperator->pOperatorGetParam = NULL;
4,612,273✔
317

318
  }
319

320
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
100,304,765✔
321
  bool           hasNewGroups = false;
100,316,286✔
322
  do {
323
    hasNewGroups = nextGroupedResult(pOperator);
100,326,751✔
324
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
96,876,146✔
325
    QUERY_CHECK_CODE(code, lino, _end);
96,883,842✔
326

327
    while (1) {
328
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
96,883,842✔
329
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
96,877,034✔
330
      QUERY_CHECK_CODE(code, lino, _end);
96,879,992✔
331

332
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
96,879,992✔
333
        if (!hasNewGroups) setOperatorCompleted(pOperator);
87,596,269✔
334
        break;
87,596,518✔
335
      }
336

337
      if (pInfo->pRes->info.rows > 0) {
9,285,750✔
338
        break;
9,285,750✔
339
      }
340
    }
341
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
96,882,268✔
342

343
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
96,872,586✔
344
  pOperator->resultInfo.totalRows += rows;
96,856,615✔
345

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

355
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
96,855,186✔
356
  return code;
96,859,877✔
357
}
358

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

389
  return TSDB_CODE_SUCCESS;
446,033,318✔
390
}
391

392
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
20,417,695✔
393
  int32_t code = TSDB_CODE_SUCCESS;
20,417,695✔
394
  int32_t lino = 0;
20,417,695✔
395
  SSDataBlock* pBlock = NULL;
20,417,695✔
396
  if (!tsCountAlwaysReturnValue) {
20,417,922✔
397
    return TSDB_CODE_SUCCESS;
5,974,385✔
398
  }
399

400
  SAggOperatorInfo* pAggInfo = pOperator->info;
14,443,537✔
401
  if (pAggInfo->groupKeyOptimized) {
14,442,690✔
402
    return TSDB_CODE_SUCCESS;
4,016,130✔
403
  }
404

405
  SOperatorInfo* downstream = pOperator->pDownstream[0];
10,426,017✔
406
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
10,425,704✔
407
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
10,390,963✔
408
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
10,368,284✔
409
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
7,258,787✔
410
    return TSDB_CODE_SUCCESS;
59,286✔
411
  }
412

413
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
10,368,893✔
414

415
  if (!pAggInfo->hasCountFunc) {
10,366,115✔
416
    return TSDB_CODE_SUCCESS;
5,751,075✔
417
  }
418

419
  code = createDataBlock(&pBlock);
4,613,174✔
420
  if (code) {
4,614,255✔
421
    return code;
×
422
  }
423

424
  pBlock->info.rows = 1;
4,614,255✔
425
  pBlock->info.capacity = 0;
4,614,875✔
426

427
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
11,664,178✔
428
    SColumnInfoData colInfo = {0};
7,051,005✔
429
    colInfo.hasNull = true;
7,049,842✔
430
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
7,049,842✔
431
    colInfo.info.bytes = 1;
7,049,842✔
432

433
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
7,049,842✔
434
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
14,307,526✔
435
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
7,260,467✔
436
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,260,545✔
437
        int32_t slotId = pFuncParam->pCol->slotId;
7,257,625✔
438
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
7,257,630✔
439
        if (slotId >= numOfCols) {
7,256,014✔
440
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,952,579✔
441
          QUERY_CHECK_CODE(code, lino, _end);
4,951,955✔
442

443
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
12,916,213✔
444
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
7,963,720✔
445
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,964,258✔
446
          }
447
        }
448
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
3,458✔
449
        // do nothing
450
      }
451
    }
452
  }
453

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

457
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
12,577,356✔
458
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
7,964,258✔
459
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
7,963,720✔
460
    colDataSetNULL(pColInfoData, 0);
461
  }
462
  *ppBlock = pBlock;
4,613,092✔
463

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

472
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
446,487,038✔
473
  if (!blockAllocated) {
446,487,038✔
474
    return;
441,999,215✔
475
  }
476

477
  blockDataDestroy(*ppBlock);
4,487,823✔
478
  *ppBlock = NULL;
4,612,635✔
479
}
480

481
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
446,066,422✔
482
  int32_t           code = TSDB_CODE_SUCCESS;
446,066,422✔
483
  SAggOperatorInfo* pAggInfo = pOperator->info;
446,066,422✔
484
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
446,198,866✔
485
    return code;
295,250,498✔
486
  }
487

488
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
150,942,820✔
489

490
  // record the current active group id
491
  pAggInfo->groupId = groupId;
150,846,082✔
492
  return code;
150,892,742✔
493
}
494

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

502
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
150,910,085✔
503
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
150,903,278✔
504
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
150,886,162✔
505

506
  SResultRow* pResultRow =
507
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
150,870,172✔
508
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
509
  if (pResultRow == NULL || pTaskInfo->code != 0) {
150,899,624✔
510
    code = pTaskInfo->code;
24,588✔
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) {
150,890,458✔
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);
150,892,198✔
524
  QUERY_CHECK_CODE(code, lino, _end);
150,833,242✔
525

526
_end:
150,833,242✔
527
  if (code != TSDB_CODE_SUCCESS) {
150,833,242✔
528
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
529
  }
530
  return code;
150,854,198✔
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,
317,408,676✔
591
                         const char* pKey) {
592
  int32_t code = 0;
317,408,676✔
593
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
594

595
  pAggSup->currentPageId = -1;
317,408,676✔
596
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
317,524,655✔
597
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
317,060,127✔
598
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
317,335,101✔
599

600
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
316,926,796✔
601
    return terrno;
20,933✔
602
  }
603

604
  uint32_t defaultPgsz = 0;
316,907,090✔
605
  int64_t defaultBufsz = 0;
317,064,106✔
606
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
317,084,942✔
607
  if (code) {
316,871,281✔
608
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
609
    return code;
×
610
  }
611

612
  if (!osTempSpaceAvailable()) {
316,871,281✔
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);
317,413,581✔
619
  if (code != TSDB_CODE_SUCCESS) {
317,434,323✔
620
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
621
    return code;
×
622
  }
623

624
  return code;
317,434,335✔
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,
101,277,441✔
664
                                  SGroupResInfo* pGroupResInfo) {
665
  int32_t         numOfExprs = pSup->numOfExprs;
101,277,441✔
666
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
101,299,593✔
667
  SqlFunctionCtx* pCtx = pSup->pCtx;
101,257,496✔
668
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
101,219,198✔
669
  bool            needCleanup = false;
101,250,073✔
670

671
  for (int32_t j = 0; j < numOfExprs; ++j) {
365,391,291✔
672
    needCleanup |= pCtx[j].needCleanup;
264,108,096✔
673
  }
674
  if (!needCleanup) {
101,283,195✔
675
    return;
100,620,081✔
676
  }
677

678
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
663,114✔
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,
56,228,463✔
700
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
701
  int32_t         numOfExprs = pSup->numOfExprs;
56,228,463✔
702
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
56,229,011✔
703
  SqlFunctionCtx* pCtx = pSup->pCtx;
56,225,099✔
704
  bool            needCleanup = false;
56,219,028✔
705
  for (int32_t j = 0; j < numOfExprs; ++j) {
165,839,157✔
706
    needCleanup |= pCtx[j].needCleanup;
109,615,024✔
707
  }
708
  if (!needCleanup) {
56,224,133✔
709
    return;
56,220,948✔
710
  }
711

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

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

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

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

737
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
157,501,274✔
738
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
739
  if (cleanGroupResInfo) {
157,501,274✔
740
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
101,288,307✔
741
  } else {
742
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
56,212,967✔
743
  }
744
}
157,476,922✔
745
void cleanupAggSup(SAggSupporter* pAggSup) {
324,911,230✔
746
  taosMemoryFreeClear(pAggSup->keyBuf);
324,911,230✔
747
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
324,932,592✔
748
  destroyDiskbasedBuf(pAggSup->pResultBuf);
324,986,000✔
749
  memset(pAggSup, 0, sizeof(SAggSupporter));
324,810,704✔
750
}
324,810,704✔
751

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

759
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
317,026,664✔
760
  if (code != TSDB_CODE_SUCCESS) {
317,397,953✔
761
    return code;
×
762
  }
763

764
  for (int32_t i = 0; i < numOfCols; ++i) {
1,219,746,834✔
765
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
902,558,520✔
766
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
902,503,434✔
767
    if (pState) {
902,135,888✔
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;
902,135,888✔
773
    }
774
  }
775

776
  return TSDB_CODE_SUCCESS;
317,188,314✔
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,958,335✔
798
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
31,959,807✔
799

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

802
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
31,965,590✔
803
      pEntryInfo->isNullRes = 0;
31,967,275✔
804
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
31,966,846✔
805
        pEntryInfo->isNullRes = 1;
2✔
806
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
31,968,561✔
807
        void* v = nodesGetValueFromNode(valueNode);
8,018✔
808
        memcpy(p, v, varDataTLen(v));
9,724✔
809
      } else {
810
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
31,964,722✔
811
      }
812
      
813
      pEntryInfo->numOfRes = 1;
31,968,085✔
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) {
425✔
840
            pCtx[k].fpSet.cleanup(&pCtx[k]);
425✔
841
          }
842
          TAOS_CHECK_EXIT(code);
425✔
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));
425✔
855
    taskInfo->code = code;
425✔
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,390,822✔
874
  SAggOperatorInfo* pAgg = pOper->info;
5,390,822✔
875
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
5,391,431✔
876
  
877
  pOper->status = OP_NOT_OPENED;
5,391,431✔
878
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
5,391,623✔
879
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
5,391,623✔
880
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
5,392,040✔
881
                      pAgg->cleanGroupResInfo);
5,391,420✔
882
  cleanupGroupResInfo(&pAgg->groupResInfo);
5,391,210✔
883
  resetBasicOperatorState(&pAgg->binfo);
5,390,383✔
884
  
885
  pAgg->pNewGroupBlock = NULL;
5,391,417✔
886

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

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

895
  pAgg->groupId = UINT64_MAX;
5,391,634✔
896
  pAgg->cleanGroupResInfo = false;
5,391,634✔
897
  pAgg->hasValidBlock = false;
5,391,634✔
898
  return 0;
5,391,634✔
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