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

taosdata / TDengine / #5015

03 Apr 2026 03:59PM UTC coverage: 72.289% (+0.03%) from 72.256%
#5015

push

travis-ci

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

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13044 existing lines in 149 files now uncovered.

257390 of 356056 relevant lines covered (72.29%)

130247228.09 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,
91,108,736✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
91,108,736✔
79

80
  int32_t    lino = 0;
91,125,522✔
81
  int32_t    code = 0;
91,125,522✔
82
  int32_t    num = 0;
91,125,522✔
83
  SExprInfo* pExprInfo = NULL;
91,121,326✔
84
  int32_t    numOfScalarExpr = 0;
91,120,925✔
85
  SExprInfo* pScalarExprInfo = NULL;
91,117,395✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
91,115,713✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
91,056,234✔
89
  if (pInfo == NULL || pOperator == NULL) {
91,075,178✔
UNCOV
90
    code = terrno;
×
91
    goto _error;
×
92
  }
93
  initOperatorCostInfo(pOperator);
91,078,869✔
94

95
  pOperator->exprSupp.hasWindowOrGroup = false;
91,131,479✔
96

97
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
91,132,763✔
98
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
91,144,784✔
99
  initBasicInfo(&pInfo->binfo, pResBlock);
91,144,784✔
100

101
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
91,117,888✔
102
  initResultSizeInfo(&pOperator->resultInfo, 4096);
91,117,888✔
103

104

105
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
91,134,106✔
106
  TSDB_CHECK_CODE(code, lino, _error);
91,106,163✔
107

108
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
91,106,163✔
109
                               NULL, &pTaskInfo->storageAPI.functionStore);
110
  TSDB_CHECK_CODE(code, lino, _error);
91,092,896✔
111

112
  if (pAggNode->pExprs != NULL) {
91,092,896✔
113
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
16,962,625✔
114
    TSDB_CHECK_CODE(code, lino, _error);
16,948,972✔
115
  }
116

117
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
91,072,112✔
118
  TSDB_CHECK_CODE(code, lino, _error);
91,038,716✔
119

120
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
91,046,051✔
121
                            GET_STM_RTINFO(pTaskInfo));
91,038,716✔
122
  TSDB_CHECK_CODE(code, lino, _error);
91,053,449✔
123

124
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
91,053,449✔
125
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
91,061,234✔
126
  pInfo->groupId = UINT64_MAX;
91,047,315✔
127
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
91,030,905✔
128
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
91,094,265✔
129
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
91,017,872✔
130
  pInfo->pOperator = pOperator;
91,102,039✔
131
  pInfo->cleanGroupResInfo = false;
91,084,933✔
132

133
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
91,066,028✔
134
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
91,057,745✔
135
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
91,083,280✔
136
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
137
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
91,029,331✔
138

139
  pOperator->pPhyNode = pAggNode;
91,041,366✔
140

141
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
91,031,837✔
142
    STableScanInfo* pTableScanInfo = downstream->info;
51,740,018✔
143
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
51,757,500✔
144
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
51,716,004✔
145
  }
146

147
  code = appendDownstream(pOperator, &downstream, 1);
91,030,789✔
148
  if (code != TSDB_CODE_SUCCESS) {
91,105,270✔
149
    goto _error;
×
150
  }
151

152
  *pOptrInfo = pOperator;
91,105,270✔
153
  return TSDB_CODE_SUCCESS;
91,114,937✔
154

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

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

172
  if (pInfo->pOperator) {
91,123,476✔
173
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
91,107,474✔
174
                      pInfo->cleanGroupResInfo);
91,112,902✔
175
    pInfo->pOperator = NULL;
91,095,157✔
176
  }
177
  cleanupAggSup(&pInfo->aggSup);
91,120,933✔
178
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
91,123,754✔
179
  cleanupGroupResInfo(&pInfo->groupResInfo);
91,136,082✔
180
  taosMemoryFreeClear(param);
91,123,523✔
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) {
108,234,349✔
191
  int32_t           code = TSDB_CODE_SUCCESS;
108,234,349✔
192
  int32_t           lino = 0;
108,234,349✔
193
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
108,234,349✔
194
  SAggOperatorInfo* pAggInfo = pOperator->info;
108,237,327✔
195

196
  if(!pAggInfo) {
108,228,371✔
197
    qError("function:%s, pAggInfo is NULL", __func__);
×
198
    return false;
×
199
  }
200
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
108,228,371✔
201
    return false;
10,221,559✔
202
  }
203

204
  SExprSupp*   pSup = &pOperator->exprSupp;
98,010,802✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
98,006,171✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
98,005,316✔
207

208
  pAggInfo->cleanGroupResInfo = false;
98,017,176✔
209
  if (pBlock) {
98,008,112✔
210
    pAggInfo->pNewGroupBlock = NULL;
720,401✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
720,401✔
212
    qTrace("%s EXEC_GROUP_TRACE agg_resume blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
720,401✔
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);
720,401✔
217
    QUERY_CHECK_CODE(code, lino, _end);
720,401✔
218
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
720,401✔
219
    QUERY_CHECK_CODE(code, lino, _end);
720,401✔
220

221
    code = doAggregateImpl(pOperator, pSup->pCtx);
720,401✔
222
    QUERY_CHECK_CODE(code, lino, _end);
723,118✔
223
  }
224
  while (1) {
310,841,903✔
225
    bool blockAllocated = false;
408,852,644✔
226
    pBlock = getNextBlockFromDownstreamRemainDetach(pOperator, 0);
408,852,644✔
227
    if (pBlock == NULL) {
407,098,702✔
228
      if (!pAggInfo->hasValidBlock) {
99,035,028✔
229
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
23,117,629✔
230
        QUERY_CHECK_CODE(code, lino, _end);
23,116,404✔
231

232
        if (pBlock == NULL) {
23,116,404✔
233
          break;
19,573,460✔
234
        }
235
        blockAllocated = true;
3,542,944✔
236
      } else {
237
        break;
75,918,647✔
238
      }
239
    }
240
    pAggInfo->hasValidBlock = true;
311,606,618✔
241
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
311,598,761✔
242
    qTrace("%s EXEC_GROUP_TRACE agg_input blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
311,629,735✔
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);
311,629,735✔
248

249
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
250
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
311,613,839✔
251
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
44,146,560✔
252
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
44,144,988✔
253
      if (code != TSDB_CODE_SUCCESS) {
44,143,317✔
254
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
34,472✔
255
        T_LONG_JMP(pTaskInfo->env, code);
34,472✔
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) {
311,607,280✔
260
      qTrace("%s EXEC_GROUP_TRACE agg_new_group currentGroupId:%" PRIu64 " nextBlockGroupId:%" PRIu64
733,730✔
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;
733,730✔
265
      break;
733,730✔
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);
310,843,189✔
269
    if (code != TSDB_CODE_SUCCESS) {
310,791,302✔
UNCOV
270
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
271
      T_LONG_JMP(pTaskInfo->env, code);
×
272
    }
273
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
310,791,302✔
274
    if (code != TSDB_CODE_SUCCESS) {
310,857,554✔
UNCOV
275
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
276
      T_LONG_JMP(pTaskInfo->env, code);
×
277
    }
278

279
    code = doAggregateImpl(pOperator, pSup->pCtx);
310,857,554✔
280
    if (code != TSDB_CODE_SUCCESS) {
310,824,622✔
281
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
2,988✔
282
      T_LONG_JMP(pTaskInfo->env, code);
2,988✔
283
    }
284

285
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
310,821,634✔
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) {
96,225,837✔
UNCOV
290
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
291
  }
292

293
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
96,219,045✔
294
  QUERY_CHECK_CODE(code, lino, _end);
96,224,068✔
295
  pAggInfo->cleanGroupResInfo = true;
96,224,068✔
296

297
_end:
96,223,781✔
298
  if (code != TSDB_CODE_SUCCESS) {
96,223,781✔
299
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
349✔
300
    pTaskInfo->code = code;
349✔
UNCOV
301
    T_LONG_JMP(pTaskInfo->env, code);
×
302
  }
303
  return pBlock != NULL;
96,223,662✔
304
}
305

306
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
180,678,816✔
307
  int32_t           code = TSDB_CODE_SUCCESS;
180,678,816✔
308
  int32_t           lino = 0;
180,678,816✔
309
  SAggOperatorInfo* pAggInfo = pOperator->info;
180,678,816✔
310
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
180,690,537✔
311

312
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
180,684,573✔
313
    (*ppRes) = NULL;
72,461,416✔
314
    return code;
72,462,019✔
315
  }
316

317
  if (pOperator->pOperatorGetParam) {
108,223,084✔
318
    if (pOperator->status == OP_EXEC_DONE) {
2,556,747✔
319
      pOperator->status = OP_OPENED;
1,661,065✔
320
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
1,659,812✔
321
      pAggInfo->groupId = UINT64_MAX;
1,661,065✔
322
      pAggInfo->hasValidBlock = false;
1,661,065✔
323
    }
324
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
2,556,146✔
325
    pOperator->pOperatorGetParam = NULL;
2,557,379✔
326

327
  }
328

329
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
108,219,103✔
330
  bool           hasNewGroups = false;
108,225,322✔
331
  do {
332
    hasNewGroups = nextGroupedResult(pOperator);
108,236,442✔
333
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
106,445,917✔
334
    QUERY_CHECK_CODE(code, lino, _end);
106,447,741✔
335

336
    while (1) {
337
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
106,447,741✔
338
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
106,447,091✔
339
      QUERY_CHECK_CODE(code, lino, _end);
106,445,607✔
340

341
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
106,445,607✔
342
        if (!hasNewGroups) setOperatorCompleted(pOperator);
96,220,912✔
343
        break;
96,222,244✔
344
      }
345

346
      if (pInfo->pRes->info.rows > 0) {
10,228,327✔
347
        break;
10,228,327✔
348
      }
349
    }
350
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
106,450,571✔
351

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

359
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
106,434,804✔
360

361
  (*ppRes) = (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
106,431,931✔
362
  return code;
106,435,689✔
363
}
364

365
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
311,568,743✔
366
  int32_t code = TSDB_CODE_SUCCESS;
311,568,743✔
367
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
311,568,743✔
368
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
25,118✔
UNCOV
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;
9,607,498✔
376
      }
377

378
      if ((&pCtx[k])->input.pData[0] == NULL) {
2,147,483,647✔
UNCOV
379
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
UNCOV
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✔
UNCOV
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;
311,572,108✔
396
}
397

398
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
23,116,965✔
399
  int32_t code = TSDB_CODE_SUCCESS;
23,116,965✔
400
  int32_t lino = 0;
23,116,965✔
401
  SSDataBlock* pBlock = NULL;
23,116,965✔
402
  if (!tsCountAlwaysReturnValue) {
23,117,631✔
403
    return TSDB_CODE_SUCCESS;
6,540,390✔
404
  }
405

406
  SAggOperatorInfo* pAggInfo = pOperator->info;
16,577,241✔
407
  if (pAggInfo->groupKeyOptimized) {
16,575,491✔
408
    return TSDB_CODE_SUCCESS;
3,499,337✔
409
  }
410

411
  SOperatorInfo* downstream = pOperator->pDownstream[0];
13,074,003✔
412
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
13,077,168✔
413
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
13,039,064✔
414
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
13,008,656✔
415
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
3,954,956✔
416
    return TSDB_CODE_SUCCESS;
68,512✔
417
  }
418

419
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
13,010,110✔
420

421
  if (!pAggInfo->hasCountFunc) {
13,007,868✔
422
    return TSDB_CODE_SUCCESS;
9,463,697✔
423
  }
424

425
  code = createDataBlock(&pBlock);
3,543,505✔
426
  if (code) {
3,543,505✔
UNCOV
427
    return code;
×
428
  }
429

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

433
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
9,826,248✔
434
    SColumnInfoData colInfo = {0};
6,282,743✔
435
    colInfo.hasNull = true;
6,282,743✔
436
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
6,282,743✔
437
    colInfo.info.bytes = 1;
6,282,743✔
438

439
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
6,282,743✔
440
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
12,794,097✔
441
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
6,511,354✔
442
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
6,511,354✔
443
        int32_t slotId = pFuncParam->pCol->slotId;
6,508,723✔
444
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
6,508,723✔
445
        if (slotId >= numOfCols) {
6,508,723✔
446
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,011,442✔
447
          QUERY_CHECK_CODE(code, lino, _end);
4,011,442✔
448

449
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
10,688,420✔
450
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
6,676,978✔
451
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
6,676,978✔
452
          }
453
        }
454
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
2,631✔
455
        // do nothing
456
      }
457
    }
458
  }
459

460
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
3,543,505✔
461
  QUERY_CHECK_CODE(code, lino, _end);
3,542,974✔
462

463
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
10,220,513✔
464
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,677,539✔
465
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
6,677,539✔
466
    colDataSetNULL(pColInfoData, 0);
467
  }
468
  *ppBlock = pBlock;
3,543,505✔
469

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

478
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
310,863,257✔
479
  if (!blockAllocated) {
310,863,257✔
480
    return;
307,342,887✔
481
  }
482

483
  blockDataDestroy(*ppBlock);
3,520,370✔
484
  *ppBlock = NULL;
3,543,040✔
485
}
486

487
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
311,543,463✔
488
  int32_t           code = TSDB_CODE_SUCCESS;
311,543,463✔
489
  SAggOperatorInfo* pAggInfo = pOperator->info;
311,543,463✔
490
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
311,591,923✔
491
    return code;
156,933,361✔
492
  }
493

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

498
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
154,661,604✔
499

500
  // record the current active group id
501
  pAggInfo->groupId = groupId;
154,638,708✔
502
  return code;
154,643,816✔
503
}
504

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

512
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
154,645,235✔
513
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
154,658,441✔
514
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
154,649,185✔
515

516
  SResultRow* pResultRow =
517
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
154,635,366✔
518
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
519
  if (pResultRow == NULL || pTaskInfo->code != 0) {
154,637,395✔
520
    code = pTaskInfo->code;
14,009✔
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) {
154,626,784✔
UNCOV
529
    code = addNewResultRowBuf(pResultRow, pAggInfo->aggSup.pResultBuf, pAggInfo->binfo.pRes->info.rowSize);
×
UNCOV
530
    QUERY_CHECK_CODE(code, lino, _end);
×
531
  }
532

533
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
154,650,533✔
534
  QUERY_CHECK_CODE(code, lino, _end);
154,641,892✔
535

536
_end:
154,641,892✔
537
  if (code != TSDB_CODE_SUCCESS) {
154,641,892✔
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
154,639,306✔
541
}
542

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

UNCOV
549
  SFilePage* pData = NULL;
×
550

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

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

UNCOV
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

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

UNCOV
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
×
UNCOV
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,
293,418,998✔
601
                         const char* pKey) {
602
  int32_t code = 0;
293,418,998✔
603
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
604

605
  pAggSup->currentPageId = -1;
293,418,998✔
606
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
293,454,468✔
607
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
293,402,491✔
608
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
293,391,670✔
609

610
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
293,342,480✔
611
    return terrno;
654✔
612
  }
613

614
  uint32_t defaultPgsz = 0;
293,355,079✔
615
  int64_t defaultBufsz = 0;
293,317,074✔
616
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
293,391,004✔
617
  if (code) {
293,326,197✔
UNCOV
618
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
UNCOV
619
    return code;
×
620
  }
621

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

628
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
293,427,096✔
629
  if (code != TSDB_CODE_SUCCESS) {
293,422,797✔
UNCOV
630
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
UNCOV
631
    return code;
×
632
  }
633

634
  return code;
293,422,797✔
635
}
636

637
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
99,913,845✔
638
                                  SGroupResInfo* pGroupResInfo) {
639
  int32_t         numOfExprs = pSup->numOfExprs;
99,913,845✔
640
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
99,927,541✔
641
  SqlFunctionCtx* pCtx = pSup->pCtx;
99,913,588✔
642
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
99,931,043✔
643
  bool            needCleanup = false;
99,900,332✔
644

645
  for (int32_t j = 0; j < numOfExprs; ++j) {
108,582,178✔
646
    needCleanup |= pCtx[j].needCleanup;
8,681,146✔
647
  }
648
  if (!needCleanup) {
99,901,032✔
649
    return;
99,905,827✔
650
  }
651

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

662

UNCOV
663
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
UNCOV
664
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
UNCOV
665
      if (pCtx[j].fpSet.cleanup) {
×
UNCOV
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,084,325✔
674
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
675
  int32_t         numOfExprs = pSup->numOfExprs;
35,084,325✔
676
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
35,083,234✔
677
  SqlFunctionCtx* pCtx = pSup->pCtx;
35,082,819✔
678
  bool            needCleanup = false;
35,078,471✔
679
  for (int32_t j = 0; j < numOfExprs; ++j) {
43,651,488✔
680
    needCleanup |= pCtx[j].needCleanup;
8,571,059✔
681
  }
682
  if (!needCleanup) {
35,080,429✔
683
    return;
35,080,890✔
684
  }
685

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

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

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

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

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

711
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
134,997,169✔
712
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
713
  if (cleanGroupResInfo) {
134,997,169✔
714
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
99,920,084✔
715
  } else {
716
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
35,077,085✔
717
  }
718
}
134,995,260✔
719
void cleanupAggSup(SAggSupporter* pAggSup) {
293,907,712✔
720
  taosMemoryFreeClear(pAggSup->keyBuf);
293,907,712✔
721
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
293,888,299✔
722
  destroyDiskbasedBuf(pAggSup->pResultBuf);
293,892,498✔
723
  memset(pAggSup, 0, sizeof(SAggSupporter));
293,869,283✔
724
}
293,869,283✔
725

726
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
293,440,911✔
727
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
728
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
293,440,911✔
729
  if (code != TSDB_CODE_SUCCESS) {
293,403,687✔
UNCOV
730
    return code;
×
731
  }
732

733
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
293,403,687✔
734
  if (code != TSDB_CODE_SUCCESS) {
293,428,804✔
UNCOV
735
    return code;
×
736
  }
737

738
  for (int32_t i = 0; i < numOfCols; ++i) {
1,223,242,180✔
739
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
929,849,965✔
740
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
929,894,305✔
741
    if (pState) {
929,835,788✔
UNCOV
742
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
UNCOV
743
      pSup->pCtx[i].saveHandle.pState = pState;
×
UNCOV
744
      pSup->pCtx[i].exprIdx = i;
×
745
    } else {
746
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
929,835,788✔
747
    }
748
  }
749

750
  return TSDB_CODE_SUCCESS;
293,392,215✔
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✔
UNCOV
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,868✔
782
          memcpy(dest, data, dataLen);
15,868✔
783
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
784
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,716,783,358✔
785
            blobDataCopy(dest, data);
718✔
786
          } else {
787
            varDataCopy(dest, data);
1,718,697,739✔
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,827,187✔
801
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
311,844,347✔
802

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

805
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
311,786,237✔
806
      pEntryInfo->isNullRes = 0;
311,864,907✔
807
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
311,799,747✔
808
        pEntryInfo->isNullRes = 1;
298✔
809
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
311,864,026✔
810
        void* v = nodesGetValueFromNode(valueNode);
43,824✔
811
        memcpy(p, v, varDataTLen(v));
18,864✔
812
      } else {
813
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
311,818,695✔
814
      }
815
      
816
      pEntryInfo->numOfRes = 1;
311,816,039✔
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✔
UNCOV
835
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
UNCOV
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) {
469✔
843
            pCtx[k].fpSet.cleanup(&pCtx[k]);
469✔
844
          }
845
          TAOS_CHECK_EXIT(code);
469✔
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));
469✔
858
    taskInfo->code = code;
469✔
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,415,182✔
877
  SAggOperatorInfo* pAgg = pOper->info;
5,415,182✔
878
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
5,416,572✔
879
  
880
  pOper->status = OP_NOT_OPENED;
5,417,516✔
881
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
5,417,516✔
882
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
5,417,516✔
883
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
5,416,744✔
884
                      pAgg->cleanGroupResInfo);
5,414,843✔
885
  cleanupGroupResInfo(&pAgg->groupResInfo);
5,417,033✔
886
  resetBasicOperatorState(&pAgg->binfo);
5,416,561✔
887
  
888
  pAgg->pNewGroupBlock = NULL;
5,416,303✔
889

890
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
10,831,287✔
891
    keyBufSize, pTaskInfo->id.str, NULL, &pTaskInfo->storageAPI.functionStore);
5,416,508✔
892

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

898
  pAgg->groupId = UINT64_MAX;
5,416,356✔
899
  pAgg->cleanGroupResInfo = false;
5,416,356✔
900
  pAgg->hasValidBlock = false;
5,416,797✔
901
  return 0;
5,416,782✔
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