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

taosdata / TDengine / #5016

03 Apr 2026 03:59PM UTC coverage: 72.299% (+0.01%) from 72.289%
#5016

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%)

13126 existing lines in 156 files now uncovered.

257424 of 356056 relevant lines covered (72.3%)

133108577.02 hits per line

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

81.04
/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,
95,834,908✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
95,834,908✔
79

80
  int32_t    lino = 0;
95,914,614✔
81
  int32_t    code = 0;
95,914,614✔
82
  int32_t    num = 0;
95,914,614✔
83
  SExprInfo* pExprInfo = NULL;
95,912,209✔
84
  int32_t    numOfScalarExpr = 0;
95,881,301✔
85
  SExprInfo* pScalarExprInfo = NULL;
95,807,426✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
95,774,012✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
95,352,961✔
89
  if (pInfo == NULL || pOperator == NULL) {
95,542,674✔
90
    code = terrno;
8,408✔
91
    goto _error;
×
92
  }
93
  initOperatorCostInfo(pOperator);
95,534,266✔
94

95
  pOperator->exprSupp.hasWindowOrGroup = false;
95,947,596✔
96

97
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
95,949,919✔
98
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
96,047,123✔
99
  initBasicInfo(&pInfo->binfo, pResBlock);
96,047,123✔
100

101
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
95,869,746✔
102
  initResultSizeInfo(&pOperator->resultInfo, 4096);
95,869,746✔
103

104

105
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
95,909,047✔
106
  TSDB_CHECK_CODE(code, lino, _error);
95,794,927✔
107

108
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
95,794,927✔
109
                               NULL, &pTaskInfo->storageAPI.functionStore);
110
  TSDB_CHECK_CODE(code, lino, _error);
95,707,687✔
111

112
  if (pAggNode->pExprs != NULL) {
95,707,687✔
113
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
14,996,379✔
114
    TSDB_CHECK_CODE(code, lino, _error);
14,962,403✔
115
  }
116

117
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
95,700,875✔
118
  TSDB_CHECK_CODE(code, lino, _error);
95,484,162✔
119

120
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
95,607,568✔
121
                            GET_STM_RTINFO(pTaskInfo));
95,484,162✔
122
  TSDB_CHECK_CODE(code, lino, _error);
95,577,158✔
123

124
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
95,577,158✔
125
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
95,664,343✔
126
  pInfo->groupId = UINT64_MAX;
95,565,686✔
127
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
95,484,951✔
128
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
95,604,370✔
129
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
95,474,297✔
130
  pInfo->pOperator = pOperator;
95,645,554✔
131
  pInfo->cleanGroupResInfo = false;
95,478,177✔
132

133
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
95,703,214✔
134
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
95,611,552✔
135
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
95,646,749✔
136
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
137
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
95,570,612✔
138

139
  pOperator->pPhyNode = pAggNode;
95,606,491✔
140

141
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
95,552,173✔
142
    STableScanInfo* pTableScanInfo = downstream->info;
54,109,123✔
143
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
54,235,670✔
144
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
54,084,420✔
145
  }
146

147
  code = appendDownstream(pOperator, &downstream, 1);
95,569,434✔
148
  if (code != TSDB_CODE_SUCCESS) {
95,480,622✔
149
    goto _error;
×
150
  }
151

152
  *pOptrInfo = pOperator;
95,480,622✔
153
  return TSDB_CODE_SUCCESS;
95,503,267✔
154

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

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

172
  if (pInfo->pOperator) {
95,939,808✔
173
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
95,903,195✔
174
                      pInfo->cleanGroupResInfo);
95,942,959✔
175
    pInfo->pOperator = NULL;
95,839,817✔
176
  }
177
  cleanupAggSup(&pInfo->aggSup);
95,899,311✔
178
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
95,921,558✔
179
  cleanupGroupResInfo(&pInfo->groupResInfo);
95,838,300✔
180
  taosMemoryFreeClear(param);
95,881,048✔
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) {
118,267,148✔
191
  int32_t           code = TSDB_CODE_SUCCESS;
118,267,148✔
192
  int32_t           lino = 0;
118,267,148✔
193
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
118,267,148✔
194
  SAggOperatorInfo* pAggInfo = pOperator->info;
118,273,407✔
195

196
  if(!pAggInfo) {
118,205,717✔
197
    qError("function:%s, pAggInfo is NULL", __func__);
×
198
    return false;
×
199
  }
200
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
118,205,717✔
201
    return false;
10,359,771✔
202
  }
203

204
  SExprSupp*   pSup = &pOperator->exprSupp;
107,885,854✔
205
  int32_t      order = pAggInfo->binfo.inputTsOrder;
107,891,473✔
206
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
107,885,701✔
207

208
  pAggInfo->cleanGroupResInfo = false;
107,884,934✔
209
  if (pBlock) {
107,877,507✔
210
    pAggInfo->pNewGroupBlock = NULL;
721,278✔
211
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
721,278✔
212
    qTrace("%s EXEC_GROUP_TRACE agg_resume blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
721,278✔
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);
721,278✔
217
    QUERY_CHECK_CODE(code, lino, _end);
721,278✔
218
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
721,278✔
219
    QUERY_CHECK_CODE(code, lino, _end);
721,278✔
220

221
    code = doAggregateImpl(pOperator, pSup->pCtx);
721,278✔
222
    QUERY_CHECK_CODE(code, lino, _end);
710,782✔
223
  }
224
  while (1) {
714,163,090✔
225
    bool blockAllocated = false;
822,029,582✔
226
    pBlock = getNextBlockFromDownstreamRemainDetach(pOperator, 0);
822,029,582✔
227
    if (pBlock == NULL) {
820,233,487✔
228
      if (!pAggInfo->hasValidBlock) {
108,536,910✔
229
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
25,511,745✔
230
        QUERY_CHECK_CODE(code, lino, _end);
25,518,156✔
231

232
        if (pBlock == NULL) {
25,518,156✔
233
          break;
22,095,981✔
234
        }
235
        blockAllocated = true;
3,422,175✔
236
      } else {
237
        break;
83,044,584✔
238
      }
239
    }
240
    pAggInfo->hasValidBlock = true;
715,118,752✔
241
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
715,034,601✔
242
    qTrace("%s EXEC_GROUP_TRACE agg_input blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
715,148,912✔
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);
715,148,912✔
248

249
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
250
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
715,032,990✔
251
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
85,079,478✔
252
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
85,098,996✔
253
      if (code != TSDB_CODE_SUCCESS) {
85,069,447✔
254
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
33,794✔
255
        T_LONG_JMP(pTaskInfo->env, code);
33,794✔
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) {
715,067,185✔
260
      qTrace("%s EXEC_GROUP_TRACE agg_new_group currentGroupId:%" PRIu64 " nextBlockGroupId:%" PRIu64
734,355✔
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;
734,355✔
265
      break;
734,355✔
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);
714,283,423✔
269
    if (code != TSDB_CODE_SUCCESS) {
714,104,636✔
UNCOV
270
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
271
      T_LONG_JMP(pTaskInfo->env, code);
×
272
    }
273
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
714,104,636✔
274
    if (code != TSDB_CODE_SUCCESS) {
714,278,654✔
UNCOV
275
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
UNCOV
276
      T_LONG_JMP(pTaskInfo->env, code);
×
277
    }
278

279
    code = doAggregateImpl(pOperator, pSup->pCtx);
714,278,654✔
280
    if (code != TSDB_CODE_SUCCESS) {
714,146,875✔
281
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
3,092✔
282
      T_LONG_JMP(pTaskInfo->env, code);
3,092✔
283
    }
284

285
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
714,143,783✔
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) {
105,874,920✔
UNCOV
290
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
291
  }
292

293
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
105,783,068✔
294
  QUERY_CHECK_CODE(code, lino, _end);
105,844,145✔
295
  pAggInfo->cleanGroupResInfo = true;
105,844,145✔
296

297
_end:
105,830,654✔
298
  if (code != TSDB_CODE_SUCCESS) {
105,833,863✔
299
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
16,646✔
300
    pTaskInfo->code = code;
16,646✔
UNCOV
301
    T_LONG_JMP(pTaskInfo->env, code);
×
302
  }
303
  return pBlock != NULL;
105,817,217✔
304
}
305

306
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
196,494,178✔
307
  int32_t           code = TSDB_CODE_SUCCESS;
196,494,178✔
308
  int32_t           lino = 0;
196,494,178✔
309
  SAggOperatorInfo* pAggInfo = pOperator->info;
196,494,178✔
310
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
196,530,800✔
311

312
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
196,521,470✔
313
    (*ppRes) = NULL;
78,250,643✔
314
    return code;
78,251,010✔
315
  }
316

317
  if (pOperator->pOperatorGetParam) {
118,266,024✔
318
    if (pOperator->status == OP_EXEC_DONE) {
11,357,345✔
319
      pOperator->status = OP_OPENED;
7,192,427✔
320
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
7,186,931✔
321
      pAggInfo->groupId = UINT64_MAX;
7,196,994✔
322
      pAggInfo->hasValidBlock = false;
7,196,994✔
323
    }
324
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
11,353,792✔
325
    pOperator->pOperatorGetParam = NULL;
11,354,584✔
326

327
  }
328

329
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
118,224,528✔
330
  bool           hasNewGroups = false;
118,269,713✔
331
  do {
332
    hasNewGroups = nextGroupedResult(pOperator);
118,275,561✔
333
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
116,194,039✔
334
    QUERY_CHECK_CODE(code, lino, _end);
116,244,239✔
335

336
    while (1) {
337
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
116,244,239✔
338
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
116,222,910✔
339
      QUERY_CHECK_CODE(code, lino, _end);
116,238,900✔
340

341
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
116,238,900✔
342
        if (!hasNewGroups) setOperatorCompleted(pOperator);
105,878,953✔
343
        break;
105,881,279✔
344
      }
345

346
      if (pInfo->pRes->info.rows > 0) {
10,367,538✔
347
        break;
10,367,538✔
348
      }
349
    }
350
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
116,248,817✔
351

352
_end:
116,199,253✔
353
  if (code != TSDB_CODE_SUCCESS) {
116,199,253✔
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);
116,199,253✔
360

361
  (*ppRes) = (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
116,196,263✔
362
  return code;
116,207,111✔
363
}
364

365
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
714,994,391✔
366
  int32_t code = TSDB_CODE_SUCCESS;
714,994,391✔
367
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
714,994,391✔
368
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
199,923✔
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;
10,164,527✔
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) {
3,092✔
UNCOV
387
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
388
        }
389
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
3,092✔
390
        return code;
3,092✔
391
      }
392
    }
393
  }
394

395
  return TSDB_CODE_SUCCESS;
715,002,378✔
396
}
397

398
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
25,527,031✔
399
  int32_t code = TSDB_CODE_SUCCESS;
25,527,031✔
400
  int32_t lino = 0;
25,527,031✔
401
  SSDataBlock* pBlock = NULL;
25,527,031✔
402
  if (!tsCountAlwaysReturnValue) {
25,523,985✔
403
    return TSDB_CODE_SUCCESS;
6,549,091✔
404
  }
405

406
  SAggOperatorInfo* pAggInfo = pOperator->info;
18,974,894✔
407
  if (pAggInfo->groupKeyOptimized) {
18,959,600✔
408
    return TSDB_CODE_SUCCESS;
5,321,372✔
409
  }
410

411
  SOperatorInfo* downstream = pOperator->pDownstream[0];
13,595,538✔
412
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
13,615,399✔
413
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
13,576,124✔
414
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
13,553,134✔
415
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
4,467,067✔
416
    return TSDB_CODE_SUCCESS;
63,990✔
417
  }
418

419
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
13,560,458✔
420

421
  if (!pAggInfo->hasCountFunc) {
13,547,965✔
422
    return TSDB_CODE_SUCCESS;
10,119,441✔
423
  }
424

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

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

433
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
9,582,499✔
434
    SColumnInfoData colInfo = {0};
6,155,338✔
435
    colInfo.hasNull = true;
6,156,014✔
436
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
6,156,014✔
437
    colInfo.info.bytes = 1;
6,156,014✔
438

439
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
6,156,014✔
440
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
12,536,581✔
441
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
6,380,805✔
442
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
6,380,421✔
443
        int32_t slotId = pFuncParam->pCol->slotId;
6,374,861✔
444
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
6,377,074✔
445
        if (slotId >= numOfCols) {
6,373,499✔
446
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
3,876,154✔
447
          QUERY_CHECK_CODE(code, lino, _end);
3,876,869✔
448

449
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
10,510,598✔
450
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
6,629,041✔
451
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
6,633,729✔
452
          }
453
        }
454
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
3,148✔
455
        // do nothing
456
      }
457
    }
458
  }
459

460
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
3,428,790✔
461
  QUERY_CHECK_CODE(code, lino, _end);
3,426,475✔
462

463
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
10,059,105✔
464
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,632,139✔
465
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
6,631,852✔
466
    colDataSetNULL(pColInfoData, 0);
467
  }
468
  *ppBlock = pBlock;
3,425,702✔
469

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

478
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
714,128,274✔
479
  if (!blockAllocated) {
714,128,274✔
480
    return;
710,807,824✔
481
  }
482

483
  blockDataDestroy(*ppBlock);
3,320,450✔
484
  *ppBlock = NULL;
3,424,860✔
485
}
486

487
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
714,989,328✔
488
  int32_t           code = TSDB_CODE_SUCCESS;
714,989,328✔
489
  SAggOperatorInfo* pAggInfo = pOperator->info;
714,989,328✔
490
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
715,143,894✔
491
    return code;
512,576,996✔
492
  }
493

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

498
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
202,540,807✔
499

500
  // record the current active group id
501
  pAggInfo->groupId = groupId;
202,459,980✔
502
  return code;
202,501,767✔
503
}
504

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

512
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
202,499,874✔
513
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
202,508,577✔
514
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
202,479,234✔
515

516
  SResultRow* pResultRow =
517
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
202,439,217✔
518
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
519
  if (pResultRow == NULL || pTaskInfo->code != 0) {
202,484,460✔
520
    code = pTaskInfo->code;
47,354✔
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) {
202,448,280✔
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);
202,462,237✔
534
  QUERY_CHECK_CODE(code, lino, _end);
202,474,893✔
535

536
_end:
202,474,893✔
537
  if (code != TSDB_CODE_SUCCESS) {
202,474,893✔
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
202,428,708✔
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,
303,871,579✔
601
                         const char* pKey) {
602
  int32_t code = 0;
303,871,579✔
603
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
604

605
  pAggSup->currentPageId = -1;
303,871,579✔
606
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
303,970,382✔
607
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
303,446,372✔
608
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
303,710,672✔
609

610
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
303,540,678✔
611
    return terrno;
19✔
612
  }
613

614
  uint32_t defaultPgsz = 0;
303,507,600✔
615
  int64_t defaultBufsz = 0;
303,290,528✔
616
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
303,612,890✔
617
  if (code) {
302,922,107✔
UNCOV
618
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
UNCOV
619
    return code;
×
620
  }
621

622
  if (!osTempSpaceAvailable()) {
302,922,107✔
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);
303,872,373✔
629
  if (code != TSDB_CODE_SUCCESS) {
303,652,853✔
630
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
23✔
UNCOV
631
    return code;
×
632
  }
633

634
  return code;
303,652,830✔
635
}
636

637
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
104,071,936✔
638
                                  SGroupResInfo* pGroupResInfo) {
639
  int32_t         numOfExprs = pSup->numOfExprs;
104,071,936✔
640
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
104,140,627✔
641
  SqlFunctionCtx* pCtx = pSup->pCtx;
104,074,866✔
642
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
104,085,033✔
643
  bool            needCleanup = false;
104,011,678✔
644

645
  for (int32_t j = 0; j < numOfExprs; ++j) {
113,395,954✔
646
    needCleanup |= pCtx[j].needCleanup;
9,385,600✔
647
  }
648
  if (!needCleanup) {
104,010,354✔
649
    return;
104,006,577✔
650
  }
651

652
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
3,777✔
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,
36,136,667✔
674
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
675
  int32_t         numOfExprs = pSup->numOfExprs;
36,136,667✔
676
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
36,136,217✔
677
  SqlFunctionCtx* pCtx = pSup->pCtx;
36,138,674✔
678
  bool            needCleanup = false;
36,131,816✔
679
  for (int32_t j = 0; j < numOfExprs; ++j) {
45,686,329✔
680
    needCleanup |= pCtx[j].needCleanup;
9,552,932✔
681
  }
682
  if (!needCleanup) {
36,133,397✔
683
    return;
36,134,124✔
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) {
×
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,
140,232,682✔
712
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
713
  if (cleanGroupResInfo) {
140,232,682✔
714
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
104,122,415✔
715
  } else {
716
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
36,110,267✔
717
  }
718
}
140,107,812✔
719
void cleanupAggSup(SAggSupporter* pAggSup) {
304,589,383✔
720
  taosMemoryFreeClear(pAggSup->keyBuf);
304,589,383✔
721
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
304,539,394✔
722
  destroyDiskbasedBuf(pAggSup->pResultBuf);
304,569,733✔
723
  memset(pAggSup, 0, sizeof(SAggSupporter));
304,514,574✔
724
}
304,514,574✔
725

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

733
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
303,405,164✔
734
  if (code != TSDB_CODE_SUCCESS) {
303,731,020✔
UNCOV
735
    return code;
×
736
  }
737

738
  for (int32_t i = 0; i < numOfCols; ++i) {
1,258,833,146✔
739
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
955,529,226✔
740
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
955,202,746✔
741
    if (pState) {
954,347,985✔
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;
954,347,985✔
747
    }
748
  }
749

750
  return TSDB_CODE_SUCCESS;
303,303,920✔
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,552✔
782
          memcpy(dest, data, dataLen);
15,552✔
783
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
784
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,564,755,628✔
785
            blobDataCopy(dest, data);
26,305✔
786
          } else {
787
            varDataCopy(dest, data);
1,565,409,783✔
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]);
313,281,674✔
801
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
313,311,056✔
802

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

805
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
313,313,253✔
806
      pEntryInfo->isNullRes = 0;
313,351,144✔
807
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
313,329,537✔
808
        pEntryInfo->isNullRes = 1;
5,068✔
809
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
313,339,224✔
810
        void* v = nodesGetValueFromNode(valueNode);
27,820✔
811
        memcpy(p, v, varDataTLen(v));
18,752✔
812
      } else {
813
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
313,296,686✔
814
      }
815
      
816
      pEntryInfo->numOfRes = 1;
313,257,368✔
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) {
462✔
843
            pCtx[k].fpSet.cleanup(&pCtx[k]);
462✔
844
          }
845
          TAOS_CHECK_EXIT(code);
462✔
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));
462✔
858
    taskInfo->code = code;
462✔
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,843,831✔
877
  SAggOperatorInfo* pAgg = pOper->info;
5,843,831✔
878
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
5,844,741✔
879
  
880
  pOper->status = OP_NOT_OPENED;
5,844,977✔
881
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
5,844,745✔
882
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
5,844,745✔
883
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
5,844,745✔
884
                      pAgg->cleanGroupResInfo);
5,844,040✔
885
  cleanupGroupResInfo(&pAgg->groupResInfo);
5,844,977✔
886
  resetBasicOperatorState(&pAgg->binfo);
5,843,732✔
887
  
888
  pAgg->pNewGroupBlock = NULL;
5,844,519✔
889

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

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

898
  pAgg->groupId = UINT64_MAX;
5,844,507✔
899
  pAgg->cleanGroupResInfo = false;
5,844,741✔
900
  pAgg->hasValidBlock = false;
5,844,741✔
901
  return 0;
5,844,741✔
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