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

taosdata / TDengine / #5056

17 May 2026 01:15AM UTC coverage: 73.384% (+0.03%) from 73.355%
#5056

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281643 of 383795 relevant lines covered (73.38%)

135942701.67 hits per line

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

79.54
/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,
104,071,994✔
77
                                    SOperatorInfo** pOptrInfo) {
78
  QRY_PARAM_CHECK(pOptrInfo);
104,071,994✔
79

80
  int32_t    lino = 0;
104,071,460✔
81
  int32_t    code = 0;
104,071,460✔
82
  int32_t    num = 0;
104,071,460✔
83
  SExprInfo* pExprInfo = NULL;
104,073,644✔
84
  int32_t    numOfScalarExpr = 0;
104,070,859✔
85
  SExprInfo* pScalarExprInfo = NULL;
104,065,074✔
86

87
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
104,060,817✔
88
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
103,988,878✔
89
  if (pInfo == NULL || pOperator == NULL) {
104,032,738✔
90
    code = terrno;
4✔
91
    goto _error;
×
92
  }
93
  initOperatorCostInfo(pOperator);
104,043,056✔
94

95
  pOperator->exprSupp.hasWindowOrGroup = false;
104,117,347✔
96

97
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
104,116,751✔
98
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
104,131,085✔
99
  initBasicInfo(&pInfo->binfo, pResBlock);
104,131,085✔
100

101
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
104,086,637✔
102
  initResultSizeInfo(&pOperator->resultInfo, 4096);
104,086,637✔
103

104

105
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
104,115,568✔
106
  TSDB_CHECK_CODE(code, lino, _error);
104,077,201✔
107

108
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
104,077,201✔
109
                               NULL, &pTaskInfo->storageAPI.functionStore);
110
  TSDB_CHECK_CODE(code, lino, _error);
104,058,218✔
111

112
  if (pAggNode->pExprs != NULL) {
104,058,218✔
113
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
17,879,292✔
114
    TSDB_CHECK_CODE(code, lino, _error);
17,869,969✔
115
  }
116

117
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
104,034,496✔
118
  TSDB_CHECK_CODE(code, lino, _error);
103,988,457✔
119

120
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
104,018,118✔
121
                            GET_STM_RTINFO(pTaskInfo));
103,988,457✔
122
  TSDB_CHECK_CODE(code, lino, _error);
103,993,834✔
123
  filterSetExecContext(pOperator->exprSupp.pFilterInfo, pTaskInfo, isTaskKilled);
103,993,834✔
124

125
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
104,016,572✔
126
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
104,075,353✔
127
  pInfo->groupId = UINT64_MAX;
104,008,018✔
128
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
104,040,725✔
129
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
104,044,275✔
130
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
104,037,691✔
131
  pInfo->pOperator = pOperator;
104,006,833✔
132
  pInfo->cleanGroupResInfo = false;
103,966,878✔
133

134
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
104,085,525✔
135
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
104,036,393✔
136
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
104,031,127✔
137
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
138
  setOperatorResetStateFn(pOperator, resetAggregateOperatorState);
103,955,888✔
139

140
  pOperator->pPhyNode = pAggNode;
103,958,425✔
141

142
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
104,048,725✔
143
    STableScanInfo* pTableScanInfo = downstream->info;
59,747,571✔
144
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
59,760,703✔
145
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
59,747,812✔
146
  }
147

148
  code = appendDownstream(pOperator, &downstream, 1);
104,006,896✔
149
  if (code != TSDB_CODE_SUCCESS) {
103,983,992✔
150
    goto _error;
×
151
  }
152

153
  *pOptrInfo = pOperator;
103,983,992✔
154
  return TSDB_CODE_SUCCESS;
104,015,483✔
155

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

166
void destroyAggOperatorInfo(void* param) {
104,112,749✔
167
  if (param == NULL) {
104,112,749✔
168
    return;
×
169
  }
170
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
104,112,749✔
171
  cleanupBasicInfo(&pInfo->binfo);
104,112,749✔
172

173
  if (pInfo->pOperator) {
104,120,018✔
174
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
104,097,746✔
175
                      pInfo->cleanGroupResInfo);
104,098,331✔
176
    pInfo->pOperator = NULL;
104,062,089✔
177
  }
178
  cleanupAggSup(&pInfo->aggSup);
104,092,343✔
179
  cleanupExprSuppWithoutFilter(&pInfo->scalarExprSup);
104,097,126✔
180
  cleanupGroupResInfo(&pInfo->groupResInfo);
104,090,531✔
181
  taosMemoryFreeClear(param);
104,095,739✔
182
}
183

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

197
  if(!pAggInfo) {
124,632,728✔
198
    qError("function:%s, pAggInfo is NULL", __func__);
×
199
    return false;
×
200
  }
201
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
124,632,728✔
202
    return false;
11,298,913✔
203
  }
204

205
  SExprSupp*   pSup = &pOperator->exprSupp;
113,361,079✔
206
  int32_t      order = pAggInfo->binfo.inputTsOrder;
113,372,820✔
207
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
113,367,630✔
208

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

222
    code = doAggregateImpl(pOperator, pSup->pCtx);
798,465✔
223
    QUERY_CHECK_CODE(code, lino, _end);
797,698✔
224
  }
225
  while (1) {
536,342,791✔
226
    bool blockAllocated = false;
649,708,452✔
227
    pBlock = getNextBlockFromDownstreamRemainDetach(pOperator, 0);
649,708,452✔
228
    if (pBlock == NULL) {
648,103,621✔
229
      if (!pAggInfo->hasValidBlock) {
115,085,495✔
230
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
26,919,444✔
231
        QUERY_CHECK_CODE(code, lino, _end);
26,918,429✔
232

233
        if (pBlock == NULL) {
26,918,429✔
234
          break;
22,820,860✔
235
        }
236
        blockAllocated = true;
4,097,569✔
237
      } else {
238
        break;
88,168,638✔
239
      }
240
    }
241
    pAggInfo->hasValidBlock = true;
537,115,695✔
242
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
537,218,805✔
243
    qTrace("%s EXEC_GROUP_TRACE agg_input blockGroupId:%" PRIu64 " currentGroupId:%" PRIu64
537,315,204✔
244
           " blocking:%d groupKeyOptimized:%d rows:%" PRId64,
245
           GET_TASKID(pTaskInfo), pBlock->info.id.groupId, pAggInfo->groupId, pOperator->blocking,
246
           pAggInfo->groupKeyOptimized, pBlock->info.rows);
247

248
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
537,315,204✔
249

250
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
251
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
537,164,796✔
252
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
90,937,593✔
253
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
90,943,219✔
254
      if (code != TSDB_CODE_SUCCESS) {
90,919,650✔
255
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
37,944✔
256
        T_LONG_JMP(pTaskInfo->env, code);
37,944✔
257
      }
258
    }
259
    // if non-blocking mode and new group arrived, save the block and break
260
    if (!pOperator->blocking && pAggInfo->groupId != UINT64_MAX && pBlock->info.id.groupId != pAggInfo->groupId) {
537,120,084✔
261
      qTrace("%s EXEC_GROUP_TRACE agg_new_group currentGroupId:%" PRIu64 " nextBlockGroupId:%" PRIu64
813,001✔
262
             " rows:%" PRId64 " groupKeyOptimized:%d",
263
             GET_TASKID(pTaskInfo), pAggInfo->groupId, pBlock->info.id.groupId, pBlock->info.rows,
264
             pAggInfo->groupKeyOptimized);
265
      pAggInfo->pNewGroupBlock = pBlock;
813,001✔
266
      break;
813,001✔
267
    }
268
    // the pDataBlock are always the same one, no need to call this again
269
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
536,266,105✔
270
    if (code != TSDB_CODE_SUCCESS) {
536,252,100✔
271
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
272
      T_LONG_JMP(pTaskInfo->env, code);
×
273
    }
274
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
536,252,100✔
275
    if (code != TSDB_CODE_SUCCESS) {
536,375,796✔
276
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
277
      T_LONG_JMP(pTaskInfo->env, code);
×
278
    }
279

280
    code = doAggregateImpl(pOperator, pSup->pCtx);
536,375,796✔
281
    if (code != TSDB_CODE_SUCCESS) {
536,325,412✔
282
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
3,252✔
283
      T_LONG_JMP(pTaskInfo->env, code);
3,252✔
284
    }
285

286
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
536,322,160✔
287
  }
288

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

294
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
111,787,172✔
295
  QUERY_CHECK_CODE(code, lino, _end);
111,790,836✔
296
  pAggInfo->cleanGroupResInfo = true;
111,790,836✔
297

298
_end:
111,793,898✔
299
  if (code != TSDB_CODE_SUCCESS) {
111,796,077✔
300
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
6,934✔
301
    pTaskInfo->code = code;
6,934✔
302
    T_LONG_JMP(pTaskInfo->env, code);
×
303
  }
304
  return pBlock != NULL;
111,789,143✔
305
}
306

307
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
208,567,435✔
308
  int32_t           code = TSDB_CODE_SUCCESS;
208,567,435✔
309
  int32_t           lino = 0;
208,567,435✔
310
  SAggOperatorInfo* pAggInfo = pOperator->info;
208,567,435✔
311
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
208,579,418✔
312

313
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
208,578,073✔
314
    (*ppRes) = NULL;
83,917,784✔
315
    return code;
83,917,911✔
316
  }
317

318
  if (pOperator->pOperatorGetParam) {
124,653,974✔
319
    if (pOperator->status == OP_EXEC_DONE) {
6,527,512✔
320
      pOperator->status = OP_OPENED;
3,929,767✔
321
      tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
3,930,683✔
322
      pAggInfo->groupId = UINT64_MAX;
3,930,464✔
323
      pAggInfo->hasValidBlock = false;
3,930,464✔
324
    }
325
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
6,525,484✔
326
    pOperator->pOperatorGetParam = NULL;
6,526,596✔
327

328
  }
329

330
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
124,657,723✔
331
  bool           hasNewGroups = false;
124,655,739✔
332
  do {
333
    hasNewGroups = nextGroupedResult(pOperator);
124,668,079✔
334
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
123,089,120✔
335
    QUERY_CHECK_CODE(code, lino, _end);
123,099,371✔
336

337
    while (1) {
338
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
123,099,371✔
339
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
123,090,679✔
340
      QUERY_CHECK_CODE(code, lino, _end);
123,098,958✔
341

342
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
123,098,958✔
343
        if (!hasNewGroups) setOperatorCompleted(pOperator);
111,793,619✔
344
        break;
111,788,691✔
345
      }
346

347
      if (pInfo->pRes->info.rows > 0) {
11,306,811✔
348
        break;
11,306,811✔
349
      }
350
    }
351
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
123,095,502✔
352

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

360
  printDataBlock(pInfo->pRes, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
123,079,844✔
361

362
  (*ppRes) = (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
123,081,600✔
363
  return code;
123,082,620✔
364
}
365

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

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

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

396
  return TSDB_CODE_SUCCESS;
537,177,117✔
397
}
398

399
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
26,920,485✔
400
  int32_t code = TSDB_CODE_SUCCESS;
26,920,485✔
401
  int32_t lino = 0;
26,920,485✔
402
  SSDataBlock* pBlock = NULL;
26,920,485✔
403
  if (!tsCountAlwaysReturnValue) {
26,921,185✔
404
    return TSDB_CODE_SUCCESS;
7,193,068✔
405
  }
406

407
  SAggOperatorInfo* pAggInfo = pOperator->info;
19,728,117✔
408
  if (pAggInfo->groupKeyOptimized) {
19,725,326✔
409
    return TSDB_CODE_SUCCESS;
4,490,492✔
410
  }
411

412
  SOperatorInfo* downstream = pOperator->pDownstream[0];
15,232,384✔
413
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
15,234,290✔
414
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
15,196,162✔
415
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
15,162,102✔
416
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
4,769,284✔
417
    return TSDB_CODE_SUCCESS;
75,630✔
418
  }
419

420
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
15,154,375✔
421

422
  if (!pAggInfo->hasCountFunc) {
15,156,476✔
423
    return TSDB_CODE_SUCCESS;
11,055,891✔
424
  }
425

426
  code = createDataBlock(&pBlock);
4,100,694✔
427
  if (code) {
4,102,087✔
428
    return code;
×
429
  }
430

431
  pBlock->info.rows = 1;
4,102,087✔
432
  pBlock->info.capacity = 0;
4,102,087✔
433

434
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
11,539,506✔
435
    SColumnInfoData colInfo = {0};
7,437,367✔
436
    colInfo.hasNull = true;
7,437,367✔
437
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
7,437,367✔
438
    colInfo.info.bytes = 1;
7,437,367✔
439

440
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
7,437,367✔
441
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
15,123,300✔
442
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
7,685,831✔
443
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
7,685,182✔
444
        int32_t slotId = pFuncParam->pCol->slotId;
7,684,393✔
445
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
7,684,379✔
446
        if (slotId >= numOfCols) {
7,683,424✔
447
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
4,950,107✔
448
          QUERY_CHECK_CODE(code, lino, _end);
4,950,237✔
449

450
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
12,895,732✔
451
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
7,944,656✔
452
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,945,495✔
453
          }
454
        }
455
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
1,730✔
456
        // do nothing
457
      }
458
    }
459
  }
460

461
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
4,102,139✔
462
  QUERY_CHECK_CODE(code, lino, _end);
4,102,139✔
463

464
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
12,046,904✔
465
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
7,945,495✔
466
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
7,945,495✔
467
    colDataSetNULL(pColInfoData, 0);
468
  }
469
  *ppBlock = pBlock;
4,100,825✔
470

471
_end:
4,100,825✔
472
  if (code != TSDB_CODE_SUCCESS) {
4,100,825✔
473
    blockDataDestroy(pBlock);
×
474
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
475
  }
476
  return code;
4,101,555✔
477
}
478

479
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
536,334,699✔
480
  if (!blockAllocated) {
536,334,699✔
481
    return;
532,309,797✔
482
  }
483

484
  blockDataDestroy(*ppBlock);
4,024,902✔
485
  *ppBlock = NULL;
4,102,117✔
486
}
487

488
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
537,059,597✔
489
  int32_t           code = TSDB_CODE_SUCCESS;
537,059,597✔
490
  SAggOperatorInfo* pAggInfo = pOperator->info;
537,059,597✔
491
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
537,245,750✔
492
    return code;
323,129,374✔
493
  }
494

495
  qTrace("%s EXEC_GROUP_TRACE agg_set_context oldGroupId:%" PRIu64 " newGroupId:%" PRIu64
214,058,863✔
496
         " groupKeyOptimized:%d numOutput:%d",
497
         GET_TASKID(pOperator->pTaskInfo), pAggInfo->groupId, groupId, pAggInfo->groupKeyOptimized, numOfOutput);
498

499
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
214,058,863✔
500

501
  // record the current active group id
502
  pAggInfo->groupId = groupId;
214,102,155✔
503
  return code;
214,143,756✔
504
}
505

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

513
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
214,121,657✔
514
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
214,090,820✔
515
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
214,117,781✔
516

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

534
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
214,124,214✔
535
  QUERY_CHECK_CODE(code, lino, _end);
214,104,251✔
536

537
_end:
214,104,251✔
538
  if (code != TSDB_CODE_SUCCESS) {
214,104,251✔
539
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
540
  }
541
  return code;
214,099,655✔
542
}
543

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

550
  SFilePage* pData = NULL;
×
551

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

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

571
    pageId = getPageId(pi);
×
572

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

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

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

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

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

598
  return 0;
×
599
}
600

601
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
331,595,050✔
602
                         const char* pKey) {
603
  int32_t code = 0;
331,595,050✔
604
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
605

606
  pAggSup->currentPageId = -1;
331,595,050✔
607
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
331,623,746✔
608
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
331,573,123✔
609
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
331,496,805✔
610

611
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
331,435,743✔
612
    return terrno;
14,944✔
613
  }
614

615
  uint32_t defaultPgsz = 0;
331,473,608✔
616
  int64_t defaultBufsz = 0;
331,539,212✔
617
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
331,607,261✔
618
  if (code) {
331,375,725✔
619
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
620
    return code;
×
621
  }
622

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

629
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
331,582,639✔
630
  if (code != TSDB_CODE_SUCCESS) {
331,610,511✔
631
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
632
    return code;
×
633
  }
634

635
  return code;
331,610,511✔
636
}
637

638
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
113,737,045✔
639
                                  SGroupResInfo* pGroupResInfo) {
640
  int32_t         numOfExprs = pSup->numOfExprs;
113,737,045✔
641
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
113,753,914✔
642
  SqlFunctionCtx* pCtx = pSup->pCtx;
113,742,209✔
643
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
113,746,629✔
644
  bool            needCleanup = false;
113,739,787✔
645

646
  for (int32_t j = 0; j < numOfExprs; ++j) {
125,440,189✔
647
    needCleanup |= pCtx[j].needCleanup;
11,699,296✔
648
  }
649
  if (!needCleanup) {
113,740,893✔
650
    return;
113,743,881✔
651
  }
652

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

663

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

674
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
39,382,961✔
675
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
676
  int32_t         numOfExprs = pSup->numOfExprs;
39,382,961✔
677
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
39,384,757✔
678
  SqlFunctionCtx* pCtx = pSup->pCtx;
39,383,316✔
679
  bool            needCleanup = false;
39,382,987✔
680
  for (int32_t j = 0; j < numOfExprs; ++j) {
48,137,551✔
681
    needCleanup |= pCtx[j].needCleanup;
8,754,028✔
682
  }
683
  if (!needCleanup) {
39,383,523✔
684
    return;
39,385,544✔
685
  }
686

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

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

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

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

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

712
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
153,121,509✔
713
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
714
  if (cleanGroupResInfo) {
153,121,509✔
715
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
113,750,641✔
716
  } else {
717
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
39,370,868✔
718
  }
719
}
153,094,349✔
720
void cleanupAggSup(SAggSupporter* pAggSup) {
332,164,411✔
721
  taosMemoryFreeClear(pAggSup->keyBuf);
332,164,411✔
722
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
332,200,533✔
723
  destroyDiskbasedBuf(pAggSup->pResultBuf);
332,175,509✔
724
  memset(pAggSup, 0, sizeof(SAggSupporter));
332,122,633✔
725
}
332,122,633✔
726

727
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
331,619,265✔
728
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
729
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
331,619,265✔
730
  if (code != TSDB_CODE_SUCCESS) {
331,550,718✔
731
    return code;
×
732
  }
733

734
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
331,550,718✔
735
  if (code != TSDB_CODE_SUCCESS) {
331,609,703✔
736
    return code;
×
737
  }
738

739
  for (int32_t i = 0; i < numOfCols; ++i) {
1,371,452,521✔
740
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
1,039,971,975✔
741
    pSup->pCtx[i].hasWindow= pSup->hasWindow;
1,039,905,534✔
742
    if (pState) {
1,039,805,684✔
743
      pSup->pCtx[i].saveHandle.pBuf = NULL;
×
744
      pSup->pCtx[i].saveHandle.pState = pState;
×
745
      pSup->pCtx[i].exprIdx = i;
×
746
    } else {
747
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
1,039,805,684✔
748
    }
749
  }
750

751
  return TSDB_CODE_SUCCESS;
331,480,546✔
752
}
753

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

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

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

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

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

781
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
782
          int32_t dataLen = getJsonValueLen(data);
17,502✔
783
          memcpy(dest, data, dataLen);
17,502✔
784
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
785
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,751,388,273✔
786
            blobDataCopy(dest, data);
×
787
          } else {
788
            varDataCopy(dest, data);
1,751,905,957✔
789
          }
790
        } else {
791
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
792
        }
793

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

799
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
800
    } else if (fmIsPlaceHolderFunc(pCtx[k].functionId)) {
2,147,483,647✔
801
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
418,462,432✔
802
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
418,472,127✔
803

804
      TAOS_CHECK_EXIT(fmSetStreamPseudoFuncParamVal(pCtx[k].functionId, pCtx[k].pExpr->base.pParamList, &taskInfo->pStreamRuntimeInfo->funcInfo));
418,456,676✔
805

806
      SValueNode *valueNode = (SValueNode *)nodesListGetNode(pCtx[k].pExpr->base.pParamList, 0);
418,513,636✔
807
      pEntryInfo->isNullRes = 0;
418,518,842✔
808
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
418,519,116✔
809
        pEntryInfo->isNullRes = 1;
153✔
810
      } else if (IS_VAR_DATA_TYPE(pCtx[k].pExpr->base.resSchema.type)){
418,518,145✔
811
        void* v = nodesGetValueFromNode(valueNode);
3,539✔
812
        memcpy(p, v, varDataTLen(v));
22,394✔
813
      } else {
814
        memcpy(p, nodesGetValueFromNode(valueNode), pCtx[k].pExpr->base.resSchema.bytes);
418,479,489✔
815
      }
816
      
817
      pEntryInfo->numOfRes = 1;
418,411,059✔
818
    } else if (pCtx[k].isPseudoFunc) {
2,147,483,647✔
819
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
2,147,483,647✔
820

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

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

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

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

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

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

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

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

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

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

877
static int32_t resetAggregateOperatorState(SOperatorInfo* pOper) {
6,648,831✔
878
  SAggOperatorInfo* pAgg = pOper->info;
6,648,831✔
879
  SAggPhysiNode*   pAggNode = (SAggPhysiNode*)pOper->pPhyNode;
6,651,266✔
880
  
881
  pOper->status = OP_NOT_OPENED;
6,651,266✔
882
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
6,651,266✔
883
  SExecTaskInfo*  pTaskInfo = pOper->pTaskInfo;
6,651,266✔
884
  cleanupResultInfo(pTaskInfo, &pOper->exprSupp, &pAgg->groupResInfo, &pAgg->aggSup,
6,651,543✔
885
                      pAgg->cleanGroupResInfo);
6,649,335✔
886
  cleanupGroupResInfo(&pAgg->groupResInfo);
6,649,677✔
887
  resetBasicOperatorState(&pAgg->binfo);
6,648,292✔
888
  
889
  pAgg->pNewGroupBlock = NULL;
6,650,182✔
890

891
  int32_t code = resetAggSup(&pOper->exprSupp, &pAgg->aggSup, pTaskInfo, pAggNode->pAggFuncs, pAggNode->pGroupKeys,
13,301,363✔
892
    keyBufSize, pTaskInfo->id.str, NULL, &pTaskInfo->storageAPI.functionStore);
6,651,039✔
893

894
  if (code == 0) {
6,650,437✔
895
    code = resetExprSupp(&pAgg->scalarExprSup, pTaskInfo, pAggNode->pExprs, NULL,
6,650,437✔
896
                          &pTaskInfo->storageAPI.functionStore);
897
  }
898

899
  pAgg->groupId = UINT64_MAX;
6,649,889✔
900
  pAgg->cleanGroupResInfo = false;
6,650,716✔
901
  pAgg->hasValidBlock = false;
6,650,716✔
902
  return 0;
6,650,163✔
903
}
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