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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 hits per line

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

76.22
/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,
34,720,888✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
34,720,888✔
79

80
  int32_t    lino = 0;
34,730,108✔
81
  int32_t    code = 0;
34,730,108✔
82
  int32_t    num = 0;
34,730,108✔
83
  SExprInfo* pExprInfo = NULL;
34,729,548✔
84
  int32_t    numOfScalarExpr = 0;
34,726,908✔
85
  SExprInfo* pScalarExprInfo = NULL;
34,722,597✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
34,719,254✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
34,695,137✔
89
  if (pInfo == NULL || pOperator == NULL) {
34,698,462✔
90
    code = terrno;
×
91
    goto _error;
×
92
  }
93

94
  pOperator->exprSupp.hasWindowOrGroup = false;
34,701,075✔
95

96
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
34,702,199✔
97
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
34,743,473✔
98
  initBasicInfo(&pInfo->binfo, pResBlock);
34,743,473✔
99

100
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
34,733,767✔
101
  initResultSizeInfo(&pOperator->resultInfo, 4096);
34,733,767✔
102

103

104
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
34,733,161✔
105
  TSDB_CHECK_CODE(code, lino, _error);
34,731,428✔
106

107
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
69,463,729✔
108
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
34,732,730✔
109
  TSDB_CHECK_CODE(code, lino, _error);
34,719,320✔
110

111
  if (pAggNode->pExprs != NULL) {
34,719,320✔
112
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
4,629,714✔
113
    TSDB_CHECK_CODE(code, lino, _error);
4,627,262✔
114
  }
115

116
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
34,722,178✔
117
  TSDB_CHECK_CODE(code, lino, _error);
34,705,436✔
118

119
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
34,696,797✔
120
                            GET_STM_RTINFO(pTaskInfo));
34,705,436✔
121
  TSDB_CHECK_CODE(code, lino, _error);
34,702,447✔
122

123
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
34,702,447✔
124
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
34,719,814✔
125
  pInfo->groupId = UINT64_MAX;
34,712,134✔
126
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
34,702,885✔
127
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
34,725,006✔
128
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
34,695,671✔
129
  pInfo->pOperator = pOperator;
34,730,702✔
130
  pInfo->cleanGroupResInfo = false;
34,715,798✔
131

132
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
34,677,294✔
133
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
34,708,289✔
134
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
34,719,395✔
135
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
136
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
34,718,047✔
137

138
  pOperator->pPhyNode = pAggNode;
34,717,106✔
139

140
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
34,708,295✔
141
    STableScanInfo* pTableScanInfo = downstream->info;
25,816,814✔
142
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
25,817,549✔
143
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
25,805,237✔
144
  }
145

146
  code = appendDownstream(pOperator, &downstream, 1);
34,711,325✔
147
  if (code != TSDB_CODE_SUCCESS) {
34,706,063✔
148
    goto _error;
×
149
  }
150

151
  *pOptrInfo = pOperator;
34,706,063✔
152
  return TSDB_CODE_SUCCESS;
34,707,038✔
153

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

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

171
  if (pInfo->pOperator) {
34,742,066✔
172
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
34,742,210✔
173
                      pInfo->cleanGroupResInfo);
34,742,832✔
174
    pInfo->pOperator = NULL;
34,742,557✔
175
  }
176
  cleanupAggSup(&pInfo->aggSup);
34,741,459✔
177
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
34,741,152✔
178
  cleanupGroupResInfo(&pInfo->groupResInfo);
34,741,969✔
179
  taosMemoryFreeClear(param);
34,741,559✔
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) {
48,200,635✔
190
  int32_t           code = TSDB_CODE_SUCCESS;
48,200,635✔
191
  int32_t           lino = 0;
48,200,635✔
192
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
48,200,635✔
193
  SAggOperatorInfo* pAggInfo = pOperator->info;
48,201,035✔
194

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

203
  SExprSupp*   pSup = &pOperator->exprSupp;
45,090,320✔
204
  int64_t      st = taosGetTimestampUs();
45,091,703✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
45,091,703✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
45,090,151✔
207

208
  pAggInfo->cleanGroupResInfo = false;
45,091,882✔
209
  if (pBlock) {
45,090,744✔
210
    pAggInfo->pNewGroupBlock = NULL;
3,271,631✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
3,271,631✔
212
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
3,271,631✔
213
    QUERY_CHECK_CODE(code, lino, _end);
3,271,631✔
214
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
3,271,631✔
215
    QUERY_CHECK_CODE(code, lino, _end);
3,271,631✔
216

217
    code = doAggregateImpl(pOperator, pSup->pCtx);
3,271,631✔
218
    QUERY_CHECK_CODE(code, lino, _end);
3,272,407✔
219
  }
220
  while (1) {
115,467,000✔
221
    bool blockAllocated = false;
160,558,520✔
222
    pBlock = getNextBlockFromDownstream(pOperator, 0);
160,558,520✔
223
    if (pBlock == NULL) {
156,797,575✔
224
      if (!pAggInfo->hasValidBlock) {
40,503,733✔
225
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
9,524,438✔
226
        QUERY_CHECK_CODE(code, lino, _end);
9,526,672✔
227

228
        if (pBlock == NULL) {
9,526,672✔
229
          break;
7,042,136✔
230
        }
231
        blockAllocated = true;
2,484,536✔
232
      } else {
233
        break;
30,980,424✔
234
      }
235
    }
236
    pAggInfo->hasValidBlock = true;
118,778,378✔
237
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
118,812,090✔
238

239
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
118,891,088✔
240

241
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
242
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
118,812,385✔
243
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
11,110,613✔
244
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
11,110,211✔
245
      if (code != TSDB_CODE_SUCCESS) {
11,108,320✔
246
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
247
        T_LONG_JMP(pTaskInfo->env, code);
×
248
      }
249
    }
250
    // if non-blocking mode and new group arrived, save the block and break
251
    if (!pOperator->blocking && pAggInfo->groupId != UINT64_MAX && pBlock->info.id.groupId != pAggInfo->groupId) {
118,809,012✔
252
      pAggInfo->pNewGroupBlock = pBlock;
3,287,306✔
253
      break;
3,287,306✔
254
    }
255
    // the pDataBlock are always the same one, no need to call this again
256
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
115,357,352✔
257
    if (code != TSDB_CODE_SUCCESS) {
115,480,215✔
258
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
259
      T_LONG_JMP(pTaskInfo->env, code);
×
260
    }
261
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
115,480,215✔
262
    if (code != TSDB_CODE_SUCCESS) {
115,623,883✔
263
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
264
      T_LONG_JMP(pTaskInfo->env, code);
×
265
    }
266

267
    code = doAggregateImpl(pOperator, pSup->pCtx);
115,623,883✔
268
    if (code != TSDB_CODE_SUCCESS) {
115,569,633✔
269
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
3,028✔
270
      T_LONG_JMP(pTaskInfo->env, code);
3,028✔
271
    }
272

273
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
115,566,605✔
274
  }
275

276
  // the downstream operator may return with error code, so let's check the code before generating results.
277
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
41,309,866✔
278
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
279
  }
280

281
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
41,298,705✔
282
  QUERY_CHECK_CODE(code, lino, _end);
41,300,922✔
283
  pAggInfo->cleanGroupResInfo = true;
41,300,922✔
284

285
_end:
41,295,608✔
286
  if (code != TSDB_CODE_SUCCESS) {
41,300,961✔
287
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2,280✔
288
    pTaskInfo->code = code;
2,280✔
289
    T_LONG_JMP(pTaskInfo->env, code);
×
290
  }
291
  return pBlock != NULL;
41,298,762✔
292
}
293

294
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
78,194,955✔
295
  int32_t           code = TSDB_CODE_SUCCESS;
78,194,955✔
296
  int32_t           lino = 0;
78,194,955✔
297
  SAggOperatorInfo* pAggInfo = pOperator->info;
78,194,955✔
298
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
78,199,095✔
299

300
  if (pOperator->status == OP_EXEC_DONE) {
78,197,979✔
301
    (*ppRes) = NULL;
30,010,659✔
302
    return code;
30,009,633✔
303
  }
304

305
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
48,187,303✔
306
  bool           hasNewGroups = false;
48,185,655✔
307
  do {
308
    hasNewGroups = nextGroupedResult(pOperator);
48,198,985✔
309
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
44,404,893✔
310
    QUERY_CHECK_CODE(code, lino, _end);
44,417,330✔
311

312
    while (1) {
313
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
44,417,330✔
314
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
44,417,201✔
315
      QUERY_CHECK_CODE(code, lino, _end);
44,416,894✔
316

317
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
44,416,894✔
318
        if (!hasNewGroups) setOperatorCompleted(pOperator);
41,306,799✔
319
        break;
41,308,152✔
320
      }
321

322
      if (pInfo->pRes->info.rows > 0) {
3,110,908✔
323
        break;
3,110,908✔
324
      }
325
    }
326
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
44,419,060✔
327

328
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
44,402,736✔
329
  pOperator->resultInfo.totalRows += rows;
44,405,596✔
330

331
_end:
44,404,773✔
332
  if (code != TSDB_CODE_SUCCESS) {
44,404,773✔
333
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
334
    pTaskInfo->code = code;
×
335
    T_LONG_JMP(pTaskInfo->env, code);
×
336
  }
337

338
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
44,404,773✔
339

340
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
44,399,919✔
341
  return code;
44,403,276✔
342
}
343

344
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
118,856,228✔
345
  int32_t code = TSDB_CODE_SUCCESS;
118,856,228✔
346
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
118,856,228✔
347
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
5,989✔
348
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
349
  }
350
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
2,147,483,647✔
351
    if (functionNeedToExecute(&pCtx[k])) {
2,147,483,647✔
352
      // todo add a dummy function to avoid process check
353
      if (pCtx[k].fpSet.process == NULL) {
2,147,483,647✔
354
        continue;
6,518,957✔
355
      }
356

357
      if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
358
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
359
        qError("%s aggregate function error happens, input data is NULL.", GET_TASKID(pOperator->pTaskInfo));
×
360
      } else {
361
        code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
362
      }
363

364
      if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
365
        if (pCtx[k].fpSet.cleanup != NULL) {
3,028✔
366
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
367
        }
368
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
3,028✔
369
        return code;
3,028✔
370
      }
371
    }
372
  }
373

374
  return TSDB_CODE_SUCCESS;
118,830,932✔
375
}
376

377
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
9,527,432✔
378
  int32_t code = TSDB_CODE_SUCCESS;
9,527,432✔
379
  int32_t lino = 0;
9,527,432✔
380
  SSDataBlock* pBlock = NULL;
9,527,432✔
381
  if (!tsCountAlwaysReturnValue) {
9,525,175✔
382
    return TSDB_CODE_SUCCESS;
1,686,478✔
383
  }
384

385
  SAggOperatorInfo* pAggInfo = pOperator->info;
7,838,697✔
386
  if (pAggInfo->groupKeyOptimized) {
7,840,338✔
387
    return TSDB_CODE_SUCCESS;
1,486,418✔
388
  }
389

390
  SOperatorInfo* downstream = pOperator->pDownstream[0];
6,348,556✔
391
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
6,351,204✔
392
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
6,337,515✔
393
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
6,330,547✔
394
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
3,521,232✔
395
    return TSDB_CODE_SUCCESS;
18,688✔
396
  }
397

398
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
6,334,472✔
399

400
  if (!pAggInfo->hasCountFunc) {
6,331,719✔
401
    return TSDB_CODE_SUCCESS;
3,847,927✔
402
  }
403

404
  code = createDataBlock(&pBlock);
2,484,788✔
405
  if (code) {
2,484,604✔
406
    return code;
×
407
  }
408

409
  pBlock->info.rows = 1;
2,484,604✔
410
  pBlock->info.capacity = 0;
2,484,604✔
411

412
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
7,591,480✔
413
    SColumnInfoData colInfo = {0};
5,108,113✔
414
    colInfo.hasNull = true;
5,107,995✔
415
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
5,107,995✔
416
    colInfo.info.bytes = 1;
5,107,995✔
417

418
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
5,107,995✔
419
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
10,461,039✔
420
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
5,354,452✔
421
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
5,354,281✔
422
        int32_t slotId = pFuncParam->pCol->slotId;
5,351,254✔
423
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
5,350,525✔
424
        if (slotId >= numOfCols) {
5,351,784✔
425
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
2,725,605✔
426
          QUERY_CHECK_CODE(code, lino, _end);
2,723,599✔
427

428
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
8,094,576✔
429
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
5,370,311✔
430
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
5,370,977✔
431
          }
432
        }
433
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
2,600✔
434
        // do nothing
435
      }
436
    }
437
  }
438

439
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
2,483,991✔
440
  QUERY_CHECK_CODE(code, lino, _end);
2,485,386✔
441

442
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
7,857,828✔
443
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
5,372,523✔
444
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
5,371,925✔
445
    colDataSetNULL(pColInfoData, 0);
446
  }
447
  *ppBlock = pBlock;
2,484,589✔
448

449
_end:
2,484,589✔
450
  if (code != TSDB_CODE_SUCCESS) {
2,484,589✔
451
    blockDataDestroy(pBlock);
×
452
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
453
  }
454
  return code;
2,484,788✔
455
}
456

457
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
115,371,934✔
458
  if (!blockAllocated) {
115,371,934✔
459
    return;
113,024,894✔
460
  }
461

462
  blockDataDestroy(*ppBlock);
2,347,040✔
463
  *ppBlock = NULL;
2,483,498✔
464
}
465

466
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
118,706,181✔
467
  int32_t           code = TSDB_CODE_SUCCESS;
118,706,181✔
468
  SAggOperatorInfo* pAggInfo = pOperator->info;
118,706,181✔
469
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
118,808,436✔
470
    return code;
60,826,105✔
471
  }
472

473
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
57,914,966✔
474

475
  // record the current active group id
476
  pAggInfo->groupId = groupId;
57,894,885✔
477
  return code;
58,090,033✔
478
}
479

480
int32_t doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
57,881,910✔
481
  // for simple group by query without interval, all the tables belong to one group result.
482
  int32_t           code = TSDB_CODE_SUCCESS;
57,881,910✔
483
  int32_t           lino = 0;
57,881,910✔
484
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
57,881,910✔
485
  SAggOperatorInfo* pAggInfo = pOperator->info;
58,019,192✔
486

487
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
58,064,246✔
488
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
58,076,528✔
489
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
58,069,529✔
490

491
  SResultRow* pResultRow =
492
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
58,055,521✔
493
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
494
  if (pResultRow == NULL || pTaskInfo->code != 0) {
58,068,786✔
495
    code = pTaskInfo->code;
×
496
    lino = __LINE__;
×
497
    goto _end;
×
498
  }
499
  /*
500
   * not assign result buffer yet, add new result buffer
501
   * all group belong to one result set, and each group result has different group id so set the id to be one
502
   */
503
  if (pResultRow->pageId == -1) {
58,097,368✔
504
    code = addNewResultRowBuf(pResultRow, pAggInfo->aggSup.pResultBuf, pAggInfo->binfo.pRes->info.rowSize);
×
505
    QUERY_CHECK_CODE(code, lino, _end);
×
506
  }
507

508
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
58,118,080✔
509
  QUERY_CHECK_CODE(code, lino, _end);
57,965,162✔
510

511
_end:
57,965,162✔
512
  if (code != TSDB_CODE_SUCCESS) {
57,965,162✔
513
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
514
  }
515
  return code;
57,943,962✔
516
}
517

518
// a new buffer page for each table. Needs to opt this design
519
int32_t addNewResultRowBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, uint32_t size) {
×
520
  if (pWindowRes->pageId != -1) {
×
521
    return 0;
×
522
  }
523

524
  SFilePage* pData = NULL;
×
525

526
  // in the first scan, new space needed for results
527
  int32_t pageId = -1;
×
528
  SArray* list = getDataBufPagesIdList(pResultBuf);
×
529

530
  if (taosArrayGetSize(list) == 0) {
×
531
    pData = getNewBufPage(pResultBuf, &pageId);
×
532
    if (pData == NULL) {
×
533
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
534
      return terrno;
×
535
    }
536
    pData->num = sizeof(SFilePage);
×
537
  } else {
538
    SPageInfo* pi = getLastPageInfo(list);
×
539
    pData = getBufPage(pResultBuf, getPageId(pi));
×
540
    if (pData == NULL) {
×
541
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
542
      return terrno;
×
543
    }
544

545
    pageId = getPageId(pi);
×
546

547
    if (pData->num + size > getBufPageSize(pResultBuf)) {
×
548
      // release current page first, and prepare the next one
549
      releaseBufPageInfo(pResultBuf, pi);
×
550

551
      pData = getNewBufPage(pResultBuf, &pageId);
×
552
      if (pData == NULL) {
×
553
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
554
        return terrno;
×
555
      }
556
      pData->num = sizeof(SFilePage);
×
557
    }
558
  }
559

560
  if (pData == NULL) {
×
561
    return -1;
×
562
  }
563

564
  // set the number of rows in current disk page
565
  if (pWindowRes->pageId == -1) {  // not allocated yet, allocate new buffer
×
566
    pWindowRes->pageId = pageId;
×
567
    pWindowRes->offset = (int32_t)pData->num;
×
568

569
    pData->num += size;
×
570
  }
571

572
  return 0;
×
573
}
574

575
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
130,972,608✔
576
                         const char* pKey) {
577
  int32_t code = 0;
130,972,608✔
578
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
579

580
  pAggSup->currentPageId = -1;
130,972,608✔
581
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
130,994,186✔
582
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
130,977,881✔
583
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
130,954,665✔
584

585
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
130,955,615✔
586
    return terrno;
28,822✔
587
  }
588

589
  uint32_t defaultPgsz = 0;
130,948,112✔
590
  int64_t defaultBufsz = 0;
130,958,430✔
591
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
130,975,199✔
592
  if (code) {
130,923,180✔
593
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
594
    return code;
×
595
  }
596

597
  if (!osTempSpaceAvailable()) {
130,923,180✔
598
    code = TSDB_CODE_NO_DISKSPACE;
×
599
    qError("Init stream agg supporter failed since %s, key:%s, tempDir:%s", tstrerror(code), pKey, tsTempDir);
×
600
    return code;
×
601
  }
602

603
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
130,973,360✔
604
  if (code != TSDB_CODE_SUCCESS) {
130,981,218✔
605
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
606
    return code;
×
607
  }
608

609
  return code;
130,981,218✔
610
}
611

612
void cleanupResultInfoInStream(SExecTaskInfo* pTaskInfo, void* pState, SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
×
613
  int32_t         code = TSDB_CODE_SUCCESS;
×
614
  SStorageAPI*    pAPI = &pTaskInfo->storageAPI;
×
615
  int32_t         numOfExprs = pSup->numOfExprs;
×
616
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
×
617
  SqlFunctionCtx* pCtx = pSup->pCtx;
×
618
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
×
619
  bool            needCleanup = false;
×
620

621
  for (int32_t j = 0; j < numOfExprs; ++j) {
×
622
    needCleanup |= pCtx[j].needCleanup;
×
623
  }
624
  if (!needCleanup) {
×
625
    return;
×
626
  }
627
  
628
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
×
629
    SResultWindowInfo* pWinInfo = taosArrayGet(pGroupResInfo->pRows, i);
×
630
    SRowBuffPos*       pPos = pWinInfo->pStatePos;
×
631
    SResultRow*        pRow = NULL;
×
632

633
    code = pAPI->stateStore.streamStateGetByPos(pState, pPos, (void**)&pRow);
×
634
    if (TSDB_CODE_SUCCESS != code) {
×
635
      qError("failed to get state by pos, code:%s, %s", tstrerror(code), GET_TASKID(pTaskInfo));
×
636
      continue;
×
637
    }
638

639
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
640
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
641
      if (pCtx[j].fpSet.cleanup) {
×
642
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
643
      }
644
    }
645
  }
646
}
647

648
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
40,652,459✔
649
                                  SGroupResInfo* pGroupResInfo) {
650
  int32_t         numOfExprs = pSup->numOfExprs;
40,652,459✔
651
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
40,652,746✔
652
  SqlFunctionCtx* pCtx = pSup->pCtx;
40,652,586✔
653
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
40,654,636✔
654
  bool            needCleanup = false;
40,652,016✔
655

656
  for (int32_t j = 0; j < numOfExprs; ++j) {
144,150,023✔
657
    needCleanup |= pCtx[j].needCleanup;
103,496,931✔
658
  }
659
  if (!needCleanup) {
40,653,092✔
660
    return;
40,299,773✔
661
  }
662

663
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
353,319✔
664
    SResultRow*        pRow = NULL;
×
665
    SResKeyPos*        pPos = taosArrayGetP(pGroupResInfo->pRows, i);
×
666
    SFilePage*         page = getBufPage(pBuf, pPos->pos.pageId);
×
667
    if (page == NULL) {
×
668
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
669
      continue;
×
670
    }
671
    pRow = (SResultRow*)((char*)page + pPos->pos.offset);
×
672

673

674
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
675
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
676
      if (pCtx[j].fpSet.cleanup) {
×
677
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
678
      }
679
    }
680
    releaseBufPage(pBuf, page);
×
681
  }
682
}
683

684
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
23,595,743✔
685
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
686
  int32_t         numOfExprs = pSup->numOfExprs;
23,595,743✔
687
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
23,597,146✔
688
  SqlFunctionCtx* pCtx = pSup->pCtx;
23,595,743✔
689
  bool            needCleanup = false;
23,594,232✔
690
  for (int32_t j = 0; j < numOfExprs; ++j) {
72,623,773✔
691
    needCleanup |= pCtx[j].needCleanup;
49,026,818✔
692
  }
693
  if (!needCleanup) {
23,596,955✔
694
    return;
23,593,713✔
695
  }
696

697
  // begin from last iter
698
  void*   pData = pGroupResInfo->dataPos;
3,242✔
699
  int32_t iter = pGroupResInfo->iter;
3,242✔
700
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
3,773✔
701
    SResultRowPosition* pos = pData;
531✔
702

703
    SFilePage* page = getBufPage(pBuf, pos->pageId);
531✔
704
    if (page == NULL) {
531✔
705
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
706
      continue;
×
707
    }
708

709
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
531✔
710

711
    for (int32_t j = 0; j < numOfExprs; ++j) {
11,151✔
712
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
10,620✔
713
      if (pCtx[j].fpSet.cleanup) {
10,620✔
714
        pCtx[j].fpSet.cleanup(&pCtx[j]);
1,062✔
715
      }
716
    }
717

718
    releaseBufPage(pBuf, page);
531✔
719
  }
720
}
721

722
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
64,249,149✔
723
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
724
  if (cleanGroupResInfo) {
64,249,149✔
725
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
40,652,903✔
726
  } else {
727
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
23,596,246✔
728
  }
729
}
64,248,742✔
730
void cleanupAggSup(SAggSupporter* pAggSup) {
131,021,911✔
731
  taosMemoryFreeClear(pAggSup->keyBuf);
131,021,911✔
732
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
131,018,082✔
733
  destroyDiskbasedBuf(pAggSup->pResultBuf);
131,018,262✔
734
  memset(pAggSup, 0, sizeof(SAggSupporter));
131,015,164✔
735
}
131,015,164✔
736

737
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
130,986,933✔
738
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
739
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
130,986,933✔
740
  if (code != TSDB_CODE_SUCCESS) {
130,973,728✔
741
    return code;
×
742
  }
743

744
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
130,973,728✔
745
  if (code != TSDB_CODE_SUCCESS) {
130,981,612✔
746
    return code;
×
747
  }
748

749
  for (int32_t i = 0; i < numOfCols; ++i) {
678,869,031✔
750
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
547,906,376✔
751
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
547,922,463✔
752
    if (pState) {
547,898,374✔
753
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
754
      pSup->pCtx[i].saveHandle.pState = pState;
×
755
      pSup->pCtx[i].exprIdx = i;
×
756
    } else {
757
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
547,898,374✔
758
    }
759
  }
760

761
  return TSDB_CODE_SUCCESS;
130,962,655✔
762
}
763

764
int32_t applyAggFunctionOnPartialTuples(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData,
2,147,483,647✔
765
                                        int32_t offset, int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
766
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,147,483,647✔
767
  for (int32_t k = 0; k < numOfOutput; ++k) {
2,147,483,647✔
768
    // keep it temporarily
769
    SFunctionCtxStatus status = {0};
2,147,483,647✔
770
    functionCtxSave(&pCtx[k], &status);
2,147,483,647✔
771

772
    pCtx[k].input.startRowIndex = offset;
2,147,483,647✔
773
    pCtx[k].input.numOfRows = forwardStep;
2,147,483,647✔
774

775
    // not a whole block involved in query processing, statistics data can not be used
776
    // NOTE: the original value of isSet have been changed here
777
    if (pCtx[k].input.colDataSMAIsSet && forwardStep < numOfTotal) {
2,147,483,647✔
778
      pCtx[k].input.colDataSMAIsSet = false;
×
779
    }
780

781
    if (fmIsPlaceHolderFunc(pCtx[k].functionId)) {
2,147,483,647✔
782
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
13,609,265✔
783
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
13,609,673✔
784

785
      TAOS_CHECK_EXIT(fmSetStreamPseudoFuncParamVal(pCtx[k].functionId, pCtx[k].pExpr->base.pParamList, &taskInfo->pStreamRuntimeInfo->funcInfo));
13,609,265✔
786

787
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
13,608,041✔
788
      pEntryInfo->isNullRes = 0;
13,608,449✔
789
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
13,608,857✔
790
        pEntryInfo->isNullRes = 1;
1,224✔
791
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
13,607,633✔
792
        void* v = nodesGetValueFromNode(valueNode);
18,348✔
793
        memcpy(p, v, varDataTLen(v));
18,348✔
794
      } else {
795
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
13,591,325✔
796
      }
797
      
798
      pEntryInfo->numOfRes = 1;
13,608,041✔
799
    } else if (pCtx[k].isPseudoFunc) {
2,147,483,647✔
800
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
2,147,483,647✔
801

802
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
803

804
      SColumnInfoData idata = {0};
2,147,483,647✔
805
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
2,147,483,647✔
806
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
2,147,483,647✔
807
      idata.pData = p;
2,147,483,647✔
808

809
      SScalarParam out = {.columnData = &idata};
2,147,483,647✔
810
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
2,147,483,647✔
811
      TAOS_CHECK_EXIT(pCtx[k].sfp.process(&tw, 1, &out));
2,147,483,647✔
812
      pEntryInfo->isNullRes = colDataIsNull_s(&idata, 0);
2,147,483,647✔
813
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
814
    } else {
815
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
2,147,483,647✔
816
        if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
817
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
818
          qError("%s apply functions error, input data is NULL.", GET_TASKID(taskInfo));
×
819
        } else {
820
          code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
821
        }
822

823
        if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
824
          if (pCtx[k].fpSet.cleanup != NULL) {
531✔
825
            pCtx[k].fpSet.cleanup(&pCtx[k]);
531✔
826
          }
827
          TAOS_CHECK_EXIT(code);
531✔
828
        }
829
      }
830

831
      // restore it
832
      functionCtxRestore(&pCtx[k], &status);
2,147,483,647✔
833
    }
834
  }
835

836
_exit:
2,147,483,647✔
837

838
  if (code) {
2,147,483,647✔
839
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
531✔
840
    taskInfo->code = code;
531✔
841
  }
842

843
  return code;
2,147,483,647✔
844
}
845

846
void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
847
  pStatus->hasAgg = pCtx->input.colDataSMAIsSet;
2,147,483,647✔
848
  pStatus->numOfRows = pCtx->input.numOfRows;
2,147,483,647✔
849
  pStatus->startOffset = pCtx->input.startRowIndex;
2,147,483,647✔
850
}
2,147,483,647✔
851

852
void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
853
  pCtx->input.colDataSMAIsSet = pStatus->hasAgg;
2,147,483,647✔
854
  pCtx->input.numOfRows = pStatus->numOfRows;
2,147,483,647✔
855
  pCtx->input.startRowIndex = pStatus->startOffset;
2,147,483,647✔
856
}
2,147,483,647✔
857

858
static int32_t resetAggregateOperatorState(SOperatorInfo* pOper) {
7,590,932✔
859
  SAggOperatorInfo* pAgg = pOper->info;
7,590,932✔
860
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
7,591,367✔
861
  
862
  pOper->status = OP_NOT_OPENED;
7,590,963✔
863
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
7,591,088✔
864
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
7,591,088✔
865
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
7,591,367✔
866
                      pAgg->cleanGroupResInfo);
7,589,722✔
867
  cleanupGroupResInfo(&pAgg->groupResInfo);
7,591,367✔
868
  resetBasicOperatorState(&pAgg->binfo);
7,590,155✔
869
  
870
  pAgg->pNewGroupBlock = NULL;
7,590,570✔
871

872
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
15,181,933✔
873
    keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
7,590,695✔
874

875
  if (code == 0) {
7,590,151✔
876
    code = resetExprSupp(&pAgg->scalarExprSup, pTaskInfo, pAggNode->pExprs, NULL,
7,590,555✔
877
                          &pTaskInfo->storageAPI.functionStore);
878
  }
879

880
  pAgg->groupId = UINT64_MAX;
7,590,963✔
881
  pAgg->cleanGroupResInfo = false;
7,591,771✔
882
  pAgg->hasValidBlock = false;
7,591,771✔
883
  return 0;
7,591,771✔
884
}
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