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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

80.46
/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,
92,657,008✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
92,657,008✔
79

80
  int32_t    lino = 0;
92,675,140✔
81
  int32_t    code = 0;
92,675,140✔
82
  int32_t    num = 0;
92,675,140✔
83
  SExprInfo* pExprInfo = NULL;
92,674,758✔
84
  int32_t    numOfScalarExpr = 0;
92,668,017✔
85
  SExprInfo* pScalarExprInfo = NULL;
92,659,514✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
92,658,384✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
92,582,158✔
89
  if (pInfo == NULL || pOperator == NULL) {
92,609,546✔
90
    code = terrno;
×
91
    goto _error;
×
92
  }
93
  initOperatorCostInfo(pOperator);
92,615,716✔
94

95
  pOperator->exprSupp.hasWindowOrGroup = false;
92,675,925✔
96

97
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
92,673,932✔
98
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
92,698,791✔
99
  initBasicInfo(&pInfo->binfo, pResBlock);
92,698,791✔
100

101
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
92,670,247✔
102
  initResultSizeInfo(&pOperator->resultInfo, 4096);
92,670,247✔
103

104

105
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
92,685,607✔
106
  TSDB_CHECK_CODE(code, lino, _error);
92,664,835✔
107

108
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
92,664,835✔
109
                               NULL, &pTaskInfo->storageAPI.functionStore);
110
  TSDB_CHECK_CODE(code, lino, _error);
92,644,323✔
111

112
  if (pAggNode->pExprs != NULL) {
92,644,323✔
113
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
16,369,024✔
114
    TSDB_CHECK_CODE(code, lino, _error);
16,367,793✔
115
  }
116

117
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
92,624,920✔
118
  TSDB_CHECK_CODE(code, lino, _error);
92,591,354✔
119

120
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
92,596,377✔
121
                            GET_STM_RTINFO(pTaskInfo));
92,591,354✔
122
  TSDB_CHECK_CODE(code, lino, _error);
92,606,116✔
123

124
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
92,606,116✔
125
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
92,615,183✔
126
  pInfo->groupId = UINT64_MAX;
92,605,109✔
127
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
92,597,121✔
128
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
92,644,803✔
129
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
92,567,638✔
130
  pInfo->pOperator = pOperator;
92,651,704✔
131
  pInfo->cleanGroupResInfo = false;
92,620,631✔
132

133
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
92,614,848✔
134
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
92,599,160✔
135
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
92,631,392✔
136
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
137
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
92,572,408✔
138

139
  pOperator->pPhyNode = pAggNode;
92,586,447✔
140

141
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
92,574,615✔
142
    STableScanInfo* pTableScanInfo = downstream->info;
53,147,137✔
143
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
53,164,456✔
144
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
53,111,569✔
145
  }
146

147
  code = appendDownstream(pOperator, &downstream, 1);
92,573,162✔
148
  if (code != TSDB_CODE_SUCCESS) {
92,655,523✔
149
    goto _error;
×
150
  }
151

152
  *pOptrInfo = pOperator;
92,655,523✔
153
  return TSDB_CODE_SUCCESS;
92,661,798✔
154

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

165
void destroyAggOperatorInfo(void* param) {
92,683,195✔
166
  if (param == NULL) {
92,683,195✔
167
    return;
×
168
  }
169
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
92,683,195✔
170
  cleanupBasicInfo(&pInfo->binfo);
92,683,195✔
171

172
  if (pInfo->pOperator) {
92,683,856✔
173
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
92,659,430✔
174
                      pInfo->cleanGroupResInfo);
92,669,770✔
175
    pInfo->pOperator = NULL;
92,652,277✔
176
  }
177
  cleanupAggSup(&pInfo->aggSup);
92,674,755✔
178
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
92,670,315✔
179
  cleanupGroupResInfo(&pInfo->groupResInfo);
92,690,098✔
180
  taosMemoryFreeClear(param);
92,674,643✔
181
}
182

183
/**
184
 * @brief get blocks from downstream and fill results into groupedRes after aggragation
185
 * @retval false if no more groups
186
 * @retval true if there could have new groups coming
187
 * @note if pOperator.blocking is true, scan all blocks from downstream, all groups are handled
188
 *       if false, fill results of ONE GROUP
189
 * */
190
static bool nextGroupedResult(SOperatorInfo* pOperator) {
110,103,893✔
191
  int32_t           code = TSDB_CODE_SUCCESS;
110,103,893✔
192
  int32_t           lino = 0;
110,103,893✔
193
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
110,103,893✔
194
  SAggOperatorInfo* pAggInfo = pOperator->info;
110,100,423✔
195

196
  if(!pAggInfo) {
110,101,997✔
197
    qError("function:%s, pAggInfo is NULL", __func__);
×
198
    return false;
×
199
  }
200
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
110,101,997✔
201
    return false;
10,264,702✔
202
  }
203

204
  SExprSupp*   pSup = &pOperator->exprSupp;
99,834,725✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
99,830,375✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
99,824,884✔
207

208
  pAggInfo->cleanGroupResInfo = false;
99,835,963✔
209
  if (pBlock) {
99,827,558✔
210
    pAggInfo->pNewGroupBlock = NULL;
722,874✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
722,874✔
212
    qTrace("%s EXEC_GROUP_TRACE agg_resume blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
722,874✔
213
           " blocking:%d groupKeyOptimized:%d rows:%" PRId64,
214
           GET_TASKID(pTaskInfo), pBlock->info.id.groupId, pAggInfo->groupId, pOperator->blocking,
215
           pAggInfo->groupKeyOptimized, pBlock->info.rows);
216
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
722,874✔
217
    QUERY_CHECK_CODE(code, lino, _end);
722,874✔
218
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
722,874✔
219
    QUERY_CHECK_CODE(code, lino, _end);
722,874✔
220

221
    code = doAggregateImpl(pOperator, pSup->pCtx);
722,874✔
222
    QUERY_CHECK_CODE(code, lino, _end);
735,337✔
223
  }
224
  while (1) {
334,904,445✔
225
    bool blockAllocated = false;
434,744,466✔
226
    pBlock = getNextBlockFromDownstreamRemainDetach(pOperator, 0);
434,744,466✔
227
    if (pBlock == NULL) {
432,713,995✔
228
      if (!pAggInfo->hasValidBlock) {
100,999,913✔
229
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
23,991,885✔
230
        QUERY_CHECK_CODE(code, lino, _end);
23,988,624✔
231

232
        if (pBlock == NULL) {
23,988,624✔
233
          break;
20,027,945✔
234
        }
235
        blockAllocated = true;
3,960,679✔
236
      } else {
237
        break;
77,013,751✔
238
      }
239
    }
240
    pAggInfo->hasValidBlock = true;
335,674,761✔
241
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
335,675,440✔
242
    qTrace("%s EXEC_GROUP_TRACE agg_input blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
335,715,457✔
243
           " blocking:%d groupKeyOptimized:%d rows:%" PRId64,
244
           GET_TASKID(pTaskInfo), pBlock->info.id.groupId, pAggInfo->groupId, pOperator->blocking,
245
           pAggInfo->groupKeyOptimized, pBlock->info.rows);
246

247
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
335,715,457✔
248

249
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
250
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
335,700,010✔
251
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
44,414,968✔
252
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
44,417,697✔
253
      if (code != TSDB_CODE_SUCCESS) {
44,411,181✔
254
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
33,600✔
255
        T_LONG_JMP(pTaskInfo->env, code);
33,600✔
256
      }
257
    }
258
    // if non-blocking mode and new group arrived, save the block and break
259
    if (!pOperator->blocking && pAggInfo->groupId != UINT64_MAX && pBlock->info.id.groupId != pAggInfo->groupId) {
335,689,913✔
260
      qTrace("%s EXEC_GROUP_TRACE agg_new_group currentGroupId:%" PRIu64 " nextBlockGroupId:%" PRIu64
736,239✔
261
             " rows:%" PRId64 " groupKeyOptimized:%d",
262
             GET_TASKID(pTaskInfo), pAggInfo->groupId, pBlock->info.id.groupId, pBlock->info.rows,
263
             pAggInfo->groupKeyOptimized);
264
      pAggInfo->pNewGroupBlock = pBlock;
736,239✔
265
      break;
736,239✔
266
    }
267
    // the pDataBlock are always the same one, no need to call this again
268
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
334,924,936✔
269
    if (code != TSDB_CODE_SUCCESS) {
334,859,314✔
UNCOV
270
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
271
      T_LONG_JMP(pTaskInfo->env, code);
×
272
    }
273
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
334,859,314✔
274
    if (code != TSDB_CODE_SUCCESS) {
334,917,699✔
275
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
276
      T_LONG_JMP(pTaskInfo->env, code);
×
277
    }
278

279
    code = doAggregateImpl(pOperator, pSup->pCtx);
334,917,699✔
280
    if (code != TSDB_CODE_SUCCESS) {
334,884,155✔
281
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
2,988✔
282
      T_LONG_JMP(pTaskInfo->env, code);
2,988✔
283
    }
284

285
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
334,881,167✔
286
  }
287

288
  // the downstream operator may return with error code, so let's check the code before generating results.
289
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
97,777,935✔
290
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
291
  }
292

293
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
97,767,180✔
294
  QUERY_CHECK_CODE(code, lino, _end);
97,775,537✔
295
  pAggInfo->cleanGroupResInfo = true;
97,775,537✔
296

297
_end:
97,774,156✔
298
  if (code != TSDB_CODE_SUCCESS) {
97,775,966✔
299
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2,985✔
300
    pTaskInfo->code = code;
2,985✔
301
    T_LONG_JMP(pTaskInfo->env, code);
×
302
  }
303
  return pBlock != NULL;
97,772,997✔
304
}
305

306
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
183,647,072✔
307
  int32_t           code = TSDB_CODE_SUCCESS;
183,647,072✔
308
  int32_t           lino = 0;
183,647,072✔
309
  SAggOperatorInfo* pAggInfo = pOperator->info;
183,647,072✔
310
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
183,658,261✔
311

312
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
183,654,017✔
313
    (*ppRes) = NULL;
73,561,605✔
314
    return code;
73,562,284✔
315
  }
316

317
  if (pOperator->pOperatorGetParam) {
110,091,795✔
318
    if (pOperator->status == OP_EXEC_DONE) {
2,566,129✔
319
      pOperator->status = OP_OPENED;
1,666,157✔
320
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
1,666,157✔
321
      pAggInfo->groupId = UINT64_MAX;
1,665,524✔
322
      pAggInfo->hasValidBlock = false;
1,665,524✔
323
    }
324
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
2,566,119✔
325
    pOperator->pOperatorGetParam = NULL;
2,566,752✔
326

327
  }
328

329
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
110,088,214✔
330
  bool           hasNewGroups = false;
110,099,274✔
331
  do {
332
    hasNewGroups = nextGroupedResult(pOperator);
110,111,013✔
333
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
108,037,970✔
334
    QUERY_CHECK_CODE(code, lino, _end);
108,043,527✔
335

336
    while (1) {
337
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
108,043,527✔
338
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
108,044,343✔
339
      QUERY_CHECK_CODE(code, lino, _end);
108,046,717✔
340

341
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
108,046,717✔
342
        if (!hasNewGroups) setOperatorCompleted(pOperator);
97,777,559✔
343
        break;
97,777,501✔
344
      }
345

346
      if (pInfo->pRes->info.rows > 0) {
10,271,506✔
347
        break;
10,271,506✔
348
      }
349
    }
350
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
108,049,007✔
351

352
_end:
108,030,276✔
353
  if (code != TSDB_CODE_SUCCESS) {
108,030,276✔
354
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
355
    pTaskInfo->code = code;
×
356
    T_LONG_JMP(pTaskInfo->env, code);
×
357
  }
358

359
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
108,030,276✔
360

361
  (*ppRes) = (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
108,025,801✔
362
  return code;
108,031,540✔
363
}
364

365
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
335,591,519✔
366
  int32_t code = TSDB_CODE_SUCCESS;
335,591,519✔
367
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
335,591,519✔
368
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
72✔
369
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
370
  }
371
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
2,147,483,647✔
372
    if (functionNeedToExecute(&pCtx[k])) {
2,147,483,647✔
373
      // todo add a dummy function to avoid process check
374
      if (pCtx[k].fpSet.process == NULL) {
2,147,483,647✔
375
        continue;
12,900,880✔
376
      }
377

378
      if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
379
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
380
        qError("%s aggregate function error happens, input data is NULL.", GET_TASKID(pOperator->pTaskInfo));
×
381
      } else {
382
        code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
383
      }
384

385
      if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
386
        if (pCtx[k].fpSet.cleanup != NULL) {
2,988✔
387
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
388
        }
389
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
2,988✔
390
        return code;
2,988✔
391
      }
392
    }
393
  }
394

395
  return TSDB_CODE_SUCCESS;
335,659,642✔
396
}
397

398
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
23,989,897✔
399
  int32_t code = TSDB_CODE_SUCCESS;
23,989,897✔
400
  int32_t lino = 0;
23,989,897✔
401
  SSDataBlock* pBlock = NULL;
23,989,897✔
402
  if (!tsCountAlwaysReturnValue) {
23,988,108✔
403
    return TSDB_CODE_SUCCESS;
6,551,401✔
404
  }
405

406
  SAggOperatorInfo* pAggInfo = pOperator->info;
17,436,707✔
407
  if (pAggInfo->groupKeyOptimized) {
17,436,191✔
408
    return TSDB_CODE_SUCCESS;
3,773,160✔
409
  }
410

411
  SOperatorInfo* downstream = pOperator->pDownstream[0];
13,656,777✔
412
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
13,661,428✔
413
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
13,627,562✔
414
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
13,597,262✔
415
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
4,673,815✔
416
    return TSDB_CODE_SUCCESS;
64,096✔
417
  }
418

419
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
13,594,586✔
420

421
  if (!pAggInfo->hasCountFunc) {
13,598,128✔
422
    return TSDB_CODE_SUCCESS;
9,636,573✔
423
  }
424

425
  code = createDataBlock(&pBlock);
3,961,828✔
426
  if (code) {
3,962,783✔
427
    return code;
×
428
  }
429

430
  pBlock->info.rows = 1;
3,962,783✔
431
  pBlock->info.capacity = 0;
3,962,783✔
432

433
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
10,655,760✔
434
    SColumnInfoData colInfo = {0};
6,693,079✔
435
    colInfo.hasNull = true;
6,693,079✔
436
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
6,693,079✔
437
    colInfo.info.bytes = 1;
6,693,079✔
438

439
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
6,693,079✔
440
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
13,616,360✔
441
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
6,924,251✔
442
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
6,924,513✔
443
        int32_t slotId = pFuncParam->pCol->slotId;
6,920,303✔
444
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
6,919,636✔
445
        if (slotId >= numOfCols) {
6,919,499✔
446
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,410,955✔
447
          QUERY_CHECK_CODE(code, lino, _end);
4,409,648✔
448

449
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
11,822,805✔
450
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
7,411,850✔
451
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,413,157✔
452
          }
453
        }
454
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
4,210✔
455
        // do nothing
456
      }
457
    }
458
  }
459

460
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
3,962,116✔
461
  QUERY_CHECK_CODE(code, lino, _end);
3,962,405✔
462

463
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
11,375,940✔
464
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
7,412,970✔
465
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
7,411,925✔
466
    colDataSetNULL(pColInfoData, 0);
467
  }
468
  *ppBlock = pBlock;
3,962,218✔
469

470
_end:
3,962,783✔
471
  if (code != TSDB_CODE_SUCCESS) {
3,962,783✔
472
    blockDataDestroy(pBlock);
×
473
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
474
  }
475
  return code;
3,962,405✔
476
}
477

478
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
334,919,758✔
479
  if (!blockAllocated) {
334,919,758✔
480
    return;
330,987,031✔
481
  }
482

483
  blockDataDestroy(*ppBlock);
3,932,727✔
484
  *ppBlock = NULL;
3,961,188✔
485
}
486

487
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
335,617,635✔
488
  int32_t           code = TSDB_CODE_SUCCESS;
335,617,635✔
489
  SAggOperatorInfo* pAggInfo = pOperator->info;
335,617,635✔
490
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
335,678,033✔
491
    return code;
173,356,110✔
492
  }
493

494
  qTrace("%s EXEC_GROUP_TRACE agg_set_context oldGroupId:%" PRIu64 " newGroupId:%" PRIu64
162,321,520✔
495
         " groupKeyOptimized:%d numOutput:%d",
496
         GET_TASKID(pOperator->pTaskInfo), pAggInfo->groupId, groupId, pAggInfo->groupKeyOptimized, numOfOutput);
497

498
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
162,321,520✔
499

500
  // record the current active group id
501
  pAggInfo->groupId = groupId;
162,292,819✔
502
  return code;
162,299,424✔
503
}
504

505
int32_t doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
162,302,228✔
506
  // for simple group by query without interval, all the tables belong to one group result.
507
  int32_t           code = TSDB_CODE_SUCCESS;
162,302,228✔
508
  int32_t           lino = 0;
162,302,228✔
509
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
162,302,228✔
510
  SAggOperatorInfo* pAggInfo = pOperator->info;
162,307,240✔
511

512
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
162,308,919✔
513
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
162,317,693✔
514
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
162,311,512✔
515

516
  SResultRow* pResultRow =
517
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
162,302,077✔
518
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
519
  if (pResultRow == NULL || pTaskInfo->code != 0) {
162,288,171✔
520
    code = pTaskInfo->code;
8,806✔
UNCOV
521
    lino = __LINE__;
×
UNCOV
522
    goto _end;
×
523
  }
524
  /*
525
   * not assign result buffer yet, add new result buffer
526
   * all group belong to one result set, and each group result has different group id so set the id to be one
527
   */
528
  if (pResultRow->pageId == -1) {
162,285,849✔
529
    code = addNewResultRowBuf(pResultRow, pAggInfo->aggSup.pResultBuf, pAggInfo->binfo.pRes->info.rowSize);
×
530
    QUERY_CHECK_CODE(code, lino, _end);
×
531
  }
532

533
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
162,308,454✔
534
  QUERY_CHECK_CODE(code, lino, _end);
162,301,761✔
535

536
_end:
162,301,761✔
537
  if (code != TSDB_CODE_SUCCESS) {
162,301,761✔
UNCOV
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
162,296,728✔
541
}
542

543
// a new buffer page for each table. Needs to opt this design
544
int32_t addNewResultRowBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, uint32_t size) {
×
545
  if (pWindowRes->pageId != -1) {
×
546
    return 0;
×
547
  }
548

549
  SFilePage* pData = NULL;
×
550

551
  // in the first scan, new space needed for results
552
  int32_t pageId = -1;
×
553
  SArray* list = getDataBufPagesIdList(pResultBuf);
×
554

555
  if (taosArrayGetSize(list) == 0) {
×
556
    pData = getNewBufPage(pResultBuf, &pageId);
×
557
    if (pData == NULL) {
×
558
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
559
      return terrno;
×
560
    }
561
    pData->num = sizeof(SFilePage);
×
562
  } else {
563
    SPageInfo* pi = getLastPageInfo(list);
×
564
    pData = getBufPage(pResultBuf, getPageId(pi));
×
565
    if (pData == NULL) {
×
566
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
567
      return terrno;
×
568
    }
569

570
    pageId = getPageId(pi);
×
571

572
    if (pData->num + size > getBufPageSize(pResultBuf)) {
×
573
      // release current page first, and prepare the next one
574
      releaseBufPageInfo(pResultBuf, pi);
×
575

576
      pData = getNewBufPage(pResultBuf, &pageId);
×
577
      if (pData == NULL) {
×
578
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
579
        return terrno;
×
580
      }
581
      pData->num = sizeof(SFilePage);
×
582
    }
583
  }
584

585
  if (pData == NULL) {
×
586
    return -1;
×
587
  }
588

589
  // set the number of rows in current disk page
590
  if (pWindowRes->pageId == -1) {  // not allocated yet, allocate new buffer
×
591
    pWindowRes->pageId = pageId;
×
592
    pWindowRes->offset = (int32_t)pData->num;
×
593

594
    pData->num += size;
×
595
  }
596

597
  return 0;
×
598
}
599

600
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
298,953,455✔
601
                         const char* pKey) {
602
  int32_t code = 0;
298,953,455✔
603
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
604

605
  pAggSup->currentPageId = -1;
298,953,455✔
606
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
298,970,395✔
607
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
298,890,100✔
608
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
298,894,810✔
609

610
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
298,835,535✔
611
    return terrno;
1,044✔
612
  }
613

614
  uint32_t defaultPgsz = 0;
298,844,747✔
615
  int64_t defaultBufsz = 0;
298,811,522✔
616
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
298,881,014✔
617
  if (code) {
298,800,875✔
618
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
619
    return code;
×
620
  }
621

622
  if (!osTempSpaceAvailable()) {
298,800,875✔
623
    code = TSDB_CODE_NO_DISKSPACE;
×
624
    qError("Init stream agg supporter failed since %s, key:%s, tempDir:%s", tstrerror(code), pKey, tsTempDir);
×
625
    return code;
×
626
  }
627

628
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
298,949,977✔
629
  if (code != TSDB_CODE_SUCCESS) {
298,955,132✔
630
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
631
    return code;
×
632
  }
633

634
  return code;
298,955,132✔
635
}
636

637
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
101,772,483✔
638
                                  SGroupResInfo* pGroupResInfo) {
639
  int32_t         numOfExprs = pSup->numOfExprs;
101,772,483✔
640
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
101,784,445✔
641
  SqlFunctionCtx* pCtx = pSup->pCtx;
101,775,791✔
642
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
101,787,377✔
643
  bool            needCleanup = false;
101,764,243✔
644

645
  for (int32_t j = 0; j < numOfExprs; ++j) {
110,516,958✔
646
    needCleanup |= pCtx[j].needCleanup;
8,753,078✔
647
  }
648
  if (!needCleanup) {
101,763,880✔
649
    return;
101,768,704✔
650
  }
651

652
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
×
653
    SResultRow*        pRow = NULL;
×
654
    SResKeyPos*        pPos = taosArrayGetP(pGroupResInfo->pRows, i);
×
655
    SFilePage*         page = getBufPage(pBuf, pPos->pos.pageId);
×
656
    if (page == NULL) {
×
657
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
658
      continue;
×
659
    }
660
    pRow = (SResultRow*)((char*)page + pPos->pos.offset);
×
661

662

663
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
664
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
665
      if (pCtx[j].fpSet.cleanup) {
×
666
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
667
      }
668
    }
669
    releaseBufPage(pBuf, page);
×
670
  }
671
}
672

673
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
35,453,706✔
674
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
675
  int32_t         numOfExprs = pSup->numOfExprs;
35,453,706✔
676
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
35,453,539✔
677
  SqlFunctionCtx* pCtx = pSup->pCtx;
35,453,890✔
678
  bool            needCleanup = false;
35,447,832✔
679
  for (int32_t j = 0; j < numOfExprs; ++j) {
45,063,599✔
680
    needCleanup |= pCtx[j].needCleanup;
9,613,092✔
681
  }
682
  if (!needCleanup) {
35,450,507✔
683
    return;
35,451,275✔
684
  }
685

686
  // begin from last iter
UNCOV
687
  void*   pData = pGroupResInfo->dataPos;
×
688
  int32_t iter = pGroupResInfo->iter;
×
689
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
×
690
    SResultRowPosition* pos = pData;
×
691

692
    SFilePage* page = getBufPage(pBuf, pos->pageId);
×
693
    if (page == NULL) {
×
694
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
695
      continue;
×
696
    }
697

698
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
×
699

700
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
701
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
702
      if (pCtx[j].fpSet.cleanup) {
×
703
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
704
      }
705
    }
706

707
    releaseBufPage(pBuf, page);
×
708
  }
709
}
710

711
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
137,230,245✔
712
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
713
  if (cleanGroupResInfo) {
137,230,245✔
714
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
101,782,054✔
715
  } else {
716
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
35,448,191✔
717
  }
718
}
137,227,807✔
719
void cleanupAggSup(SAggSupporter* pAggSup) {
299,430,111✔
720
  taosMemoryFreeClear(pAggSup->keyBuf);
299,430,111✔
721
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
299,411,909✔
722
  destroyDiskbasedBuf(pAggSup->pResultBuf);
299,419,541✔
723
  memset(pAggSup, 0, sizeof(SAggSupporter));
299,394,992✔
724
}
299,394,992✔
725

726
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
298,953,967✔
727
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
728
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
298,953,967✔
729
  if (code != TSDB_CODE_SUCCESS) {
298,920,488✔
730
    return code;
×
731
  }
732

733
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
298,920,488✔
734
  if (code != TSDB_CODE_SUCCESS) {
298,934,787✔
735
    return code;
×
736
  }
737

738
  for (int32_t i = 0; i < numOfCols; ++i) {
1,236,937,458✔
739
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
938,043,559✔
740
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
938,082,120✔
741
    if (pState) {
938,022,611✔
742
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
743
      pSup->pCtx[i].saveHandle.pState = pState;
×
744
      pSup->pCtx[i].exprIdx = i;
×
745
    } else {
746
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
938,022,611✔
747
    }
748
  }
749

750
  return TSDB_CODE_SUCCESS;
298,893,899✔
751
}
752

753
int32_t applyAggFunctionOnPartialTuples(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData,
2,147,483,647✔
754
                                        int32_t offset, int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
755
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,147,483,647✔
756
  for (int32_t k = 0; k < numOfOutput; ++k) {
2,147,483,647✔
757
    // keep it temporarily
758
    SFunctionCtxStatus status = {0};
2,147,483,647✔
759
    functionCtxSave(&pCtx[k], &status);
2,147,483,647✔
760

761
    pCtx[k].input.startRowIndex = offset;
2,147,483,647✔
762
    pCtx[k].input.numOfRows = forwardStep;
2,147,483,647✔
763

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

770
    if (pCtx[k].functionId == -1) {
2,147,483,647✔
771
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
2,147,483,647✔
772
      SColumnInfoData*     pColInfoData = pCtx[k].input.pData[0];
2,147,483,647✔
773
      int32_t              rowIndex = offset < numOfTotal ? offset : 0;
2,147,483,647✔
774

775
      if (pColInfoData != NULL && rowIndex >= 0 && rowIndex < numOfTotal &&
2,147,483,647✔
776
          !colDataIsNull(pColInfoData, numOfTotal, rowIndex, NULL)) {
2,147,483,647✔
777
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
778
        char* data = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
779

780
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
781
          int32_t dataLen = getJsonValueLen(data);
15,928✔
782
          memcpy(dest, data, dataLen);
15,928✔
783
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
784
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,644,176,059✔
785
            blobDataCopy(dest, data);
271✔
786
          } else {
787
            varDataCopy(dest, data);
1,645,447,613✔
788
          }
789
        } else {
790
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
791
        }
792

793
        pEntryInfo->isNullRes = 0;
2,147,483,647✔
794
      } else {
795
        pEntryInfo->isNullRes = 1;
2,147,483,647✔
796
      }
797

798
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
799
    } else if (fmIsPlaceHolderFunc(pCtx[k].functionId)) {
2,147,483,647✔
800
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
311,951,412✔
801
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
311,966,567✔
802

803
      TAOS_CHECK_EXIT(fmSetStreamPseudoFuncParamVal(pCtx[k].functionId, pCtx[k].pExpr->base.pParamList, &taskInfo->pStreamRuntimeInfo->funcInfo));
311,963,503✔
804

805
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
311,938,183✔
806
      pEntryInfo->isNullRes = 0;
311,987,675✔
807
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
311,953,294✔
808
        pEntryInfo->isNullRes = 1;
302✔
809
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
311,991,467✔
810
        void* v = nodesGetValueFromNode(valueNode);
35,948✔
811
        memcpy(p, v, varDataTLen(v));
18,904✔
812
      } else {
813
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
311,943,366✔
814
      }
815
      
816
      pEntryInfo->numOfRes = 1;
311,922,780✔
817
    } else if (pCtx[k].isPseudoFunc) {
2,147,483,647✔
818
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
2,147,483,647✔
819

820
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
821

822
      SColumnInfoData idata = {0};
2,147,483,647✔
823
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
2,147,483,647✔
824
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
2,147,483,647✔
825
      idata.pData = p;
2,147,483,647✔
826

827
      SScalarParam out = {.columnData = &idata};
2,147,483,647✔
828
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
2,147,483,647✔
829
      TAOS_CHECK_EXIT(pCtx[k].sfp.process(&tw, 1, &out));
2,147,483,647✔
830
      pEntryInfo->isNullRes = colDataIsNull_s(&idata, 0);
2,147,483,647✔
831
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
832
    } else {
833
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
2,147,483,647✔
834
        if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
835
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
836
          qError("%s apply functions error, input data is NULL.", GET_TASKID(taskInfo));
×
837
        } else {
838
          code = pCtx[k].fpSet.process(&pCtx[k]);
2,147,483,647✔
839
        }
840

841
        if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
842
          if (pCtx[k].fpSet.cleanup != NULL) {
471✔
843
            pCtx[k].fpSet.cleanup(&pCtx[k]);
471✔
844
          }
845
          TAOS_CHECK_EXIT(code);
471✔
846
        }
847
      }
848

849
      // restore it
850
      functionCtxRestore(&pCtx[k], &status);
2,147,483,647✔
851
    }
852
  }
853

854
_exit:
2,147,483,647✔
855

856
  if (code) {
2,147,483,647✔
857
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
471✔
858
    taskInfo->code = code;
471✔
859
  }
860

861
  return code;
2,147,483,647✔
862
}
863

864
void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
865
  pStatus->hasAgg = pCtx->input.colDataSMAIsSet;
2,147,483,647✔
866
  pStatus->numOfRows = pCtx->input.numOfRows;
2,147,483,647✔
867
  pStatus->startOffset = pCtx->input.startRowIndex;
2,147,483,647✔
868
}
2,147,483,647✔
869

870
void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
2,147,483,647✔
871
  pCtx->input.colDataSMAIsSet = pStatus->hasAgg;
2,147,483,647✔
872
  pCtx->input.numOfRows = pStatus->numOfRows;
2,147,483,647✔
873
  pCtx->input.startRowIndex = pStatus->startOffset;
2,147,483,647✔
874
}
2,147,483,647✔
875

876
static int32_t resetAggregateOperatorState(SOperatorInfo* pOper) {
5,656,277✔
877
  SAggOperatorInfo* pAgg = pOper->info;
5,656,277✔
878
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
5,658,716✔
879
  
880
  pOper->status = OP_NOT_OPENED;
5,658,954✔
881
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
5,657,705✔
882
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
5,657,705✔
883
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
5,657,975✔
884
                      pAgg->cleanGroupResInfo);
5,656,277✔
885
  cleanupGroupResInfo(&pAgg->groupResInfo);
5,658,419✔
886
  resetBasicOperatorState(&pAgg->binfo);
5,656,753✔
887
  
888
  pAgg->pNewGroupBlock = NULL;
5,658,954✔
889

890
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
11,316,615✔
891
    keyBufSize, pTaskInfo->id.str, NULL, &pTaskInfo->storageAPI.functionStore);
5,659,192✔
892

893
  if (code == 0) {
5,656,574✔
894
    code = resetExprSupp(&pAgg->scalarExprSup, pTaskInfo, pAggNode->pExprs, NULL,
5,657,288✔
895
                          &pTaskInfo->storageAPI.functionStore);
896
  }
897

898
  pAgg->groupId = UINT64_MAX;
5,657,050✔
899
  pAgg->cleanGroupResInfo = false;
5,657,764✔
900
  pAgg->hasValidBlock = false;
5,657,764✔
901
  return 0;
5,658,240✔
902
}
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