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

taosdata / TDengine / #4416

03 Jul 2025 10:49AM UTC coverage: 61.007% (-1.2%) from 62.241%
#4416

push

travis-ci

GitHub
Merge pull request #31575 from taosdata/fix/huoh/taos_log

150735 of 316232 branches covered (47.67%)

Branch coverage included in aggregate %.

233783 of 314057 relevant lines covered (74.44%)

6782670.15 hits per line

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

62.14
/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
int32_t createAggregateOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
542,156✔
75
                                    SOperatorInfo** pOptrInfo) {
76
  QRY_PARAM_CHECK(pOptrInfo);
542,156!
77

78
  int32_t    lino = 0;
542,156✔
79
  int32_t    code = 0;
542,156✔
80
  int32_t    num = 0;
542,156✔
81
  SExprInfo* pExprInfo = NULL;
542,156✔
82
  int32_t    numOfScalarExpr = 0;
542,156✔
83
  SExprInfo* pScalarExprInfo = NULL;
542,156✔
84

85
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
542,156!
86
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
542,565!
87
  if (pInfo == NULL || pOperator == NULL) {
542,672!
88
    code = terrno;
×
89
    goto _error;
×
90
  }
91

92
  pOperator->exprSupp.hasWindowOrGroup = false;
542,699✔
93

94
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
542,699✔
95
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
542,745!
96
  initBasicInfo(&pInfo->binfo, pResBlock);
542,745✔
97

98
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
542,657✔
99
  initResultSizeInfo(&pOperator->resultInfo, 4096);
542,657✔
100

101
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
542,792✔
102
  TSDB_CHECK_CODE(code, lino, _error);
542,764!
103

104
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
542,764✔
105
                               pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
542,764✔
106
  TSDB_CHECK_CODE(code, lino, _error);
542,631!
107

108
  if (pAggNode->pExprs != NULL) {
542,631✔
109
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
117,458✔
110
    TSDB_CHECK_CODE(code, lino, _error);
117,481!
111
  }
112

113
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
542,654✔
114
  TSDB_CHECK_CODE(code, lino, _error);
542,691!
115

116
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
542,691✔
117
  TSDB_CHECK_CODE(code, lino, _error);
542,486!
118

119
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
542,486✔
120
  pInfo->groupKeyOptimized = pAggNode->groupKeyOptimized;
542,486✔
121
  pInfo->groupId = UINT64_MAX;
542,486✔
122
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
542,486✔
123
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
542,486✔
124
  pInfo->hasCountFunc = pAggNode->hasCountLikeFunc;
542,486✔
125
  pInfo->pOperator = pOperator;
542,486✔
126
  pInfo->cleanGroupResInfo = false;
542,486✔
127

128
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG,
542,486✔
129
                  !pAggNode->node.forceCreateNonBlockingOptr, OP_NOT_OPENED, pInfo, pTaskInfo);
542,486✔
130
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, getAggregateResultNext, NULL, destroyAggOperatorInfo,
542,372✔
131
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
132

133
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
542,298✔
134
    STableScanInfo* pTableScanInfo = downstream->info;
311,821✔
135
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
311,821✔
136
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
311,821✔
137
  }
138

139
  code = appendDownstream(pOperator, &downstream, 1);
542,298✔
140
  if (code != TSDB_CODE_SUCCESS) {
542,648!
141
    goto _error;
×
142
  }
143

144
  *pOptrInfo = pOperator;
542,648✔
145
  return TSDB_CODE_SUCCESS;
542,648✔
146

147
_error:
×
148
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
149
  if (pInfo != NULL) {
×
150
    destroyAggOperatorInfo(pInfo);
×
151
  }
152
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
153
  pTaskInfo->code = code;
×
154
  return code;
×
155
}
156

157
void destroyAggOperatorInfo(void* param) {
542,901✔
158
  if (param == NULL) {
542,901!
159
    return;
×
160
  }
161
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
542,901✔
162
  cleanupBasicInfo(&pInfo->binfo);
542,901✔
163

164
  if (pInfo->pOperator) {
542,971!
165
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
542,971✔
166
                      pInfo->cleanGroupResInfo);
542,971✔
167
    pInfo->pOperator = NULL;
542,907✔
168
  }
169
  cleanupAggSup(&pInfo->aggSup);
542,907✔
170
  cleanupExprSupp(&pInfo->scalarExprSup);
542,964✔
171
  cleanupGroupResInfo(&pInfo->groupResInfo);
542,967✔
172
  taosMemoryFreeClear(param);
542,975!
173
}
174

175
/**
176
 * @brief get blocks from downstream and fill results into groupedRes after aggragation
177
 * @retval false if no more groups
178
 * @retval true if there could have new groups coming
179
 * @note if pOperator.blocking is true, scan all blocks from downstream, all groups are handled
180
 *       if false, fill results of ONE GROUP
181
 * */
182
static bool nextGroupedResult(SOperatorInfo* pOperator) {
666,527✔
183
  int32_t           code = TSDB_CODE_SUCCESS;
666,527✔
184
  int32_t           lino = 0;
666,527✔
185
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
666,527✔
186
  SAggOperatorInfo* pAggInfo = pOperator->info;
666,527✔
187

188
  if(!pAggInfo) {
666,527!
189
    qError("function:%s, pAggInfo is NULL", __func__);
×
190
    return false;
×
191
  }
192
  if (pOperator->blocking && pAggInfo->hasValidBlock) {
666,527✔
193
    return false;
74,250✔
194
  }
195

196
  SExprSupp*   pSup = &pOperator->exprSupp;
592,277✔
197
  int64_t      st = taosGetTimestampUs();
593,226✔
198
  int32_t      order = pAggInfo->binfo.inputTsOrder;
593,226✔
199
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
593,226✔
200

201
  pAggInfo->cleanGroupResInfo = false;
593,226✔
202
  if (pBlock) {
593,226✔
203
    pAggInfo->pNewGroupBlock = NULL;
50,626✔
204
    tSimpleHashClear(pAggInfo->aggSup.pResultRowHashTable);
50,626✔
205
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
50,626✔
206
    QUERY_CHECK_CODE(code, lino, _end);
50,626!
207
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
50,626✔
208
    QUERY_CHECK_CODE(code, lino, _end);
50,625!
209

210
    code = doAggregateImpl(pOperator, pSup->pCtx);
50,625✔
211
    QUERY_CHECK_CODE(code, lino, _end);
50,626!
212
  }
213
  while (1) {
1,705,589✔
214
    bool blockAllocated = false;
2,298,815✔
215
    pBlock = getNextBlockFromDownstream(pOperator, 0);
2,298,815✔
216
    if (pBlock == NULL) {
2,298,453✔
217
      if (!pAggInfo->hasValidBlock) {
559,799✔
218
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
109,588✔
219
        QUERY_CHECK_CODE(code, lino, _end);
109,586!
220

221
        if (pBlock == NULL) {
109,586✔
222
          break;
92,227✔
223
        }
224
        blockAllocated = true;
17,359✔
225
      } else {
226
        break;
450,211✔
227
      }
228
    }
229
    pAggInfo->hasValidBlock = true;
1,756,013✔
230
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
1,756,013✔
231

232
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
233
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
1,756,013✔
234
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
392,924✔
235
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
392,924✔
236
      if (code != TSDB_CODE_SUCCESS) {
392,961!
237
        destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
238
        T_LONG_JMP(pTaskInfo->env, code);
×
239
      }
240
    }
241
    // if non-blocking mode and new group arrived, save the block and break
242
    if (!pOperator->blocking && pAggInfo->groupId != UINT64_MAX && pBlock->info.id.groupId != pAggInfo->groupId) {
1,756,050✔
243
      pAggInfo->pNewGroupBlock = pBlock;
50,691✔
244
      break;
50,691✔
245
    }
246
    // the pDataBlock are always the same one, no need to call this again
247
    code = setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.id.groupId);
1,705,359✔
248
    if (code != TSDB_CODE_SUCCESS) {
1,705,687✔
249
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
30✔
250
      T_LONG_JMP(pTaskInfo->env, code);
30!
251
    }
252
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
1,705,657✔
253
    if (code != TSDB_CODE_SUCCESS) {
1,705,951!
254
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
255
      T_LONG_JMP(pTaskInfo->env, code);
×
256
    }
257

258
    code = doAggregateImpl(pOperator, pSup->pCtx);
1,705,951✔
259
    if (code != TSDB_CODE_SUCCESS) {
1,705,849✔
260
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
20✔
261
      T_LONG_JMP(pTaskInfo->env, code);
20!
262
    }
263

264
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
1,705,829✔
265
  }
266

267
  // the downstream operator may return with error code, so let's check the code before generating results.
268
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
593,129!
269
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
270
  }
271

272
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
593,129✔
273
  QUERY_CHECK_CODE(code, lino, _end);
593,140!
274
  pAggInfo->cleanGroupResInfo = true;
593,140✔
275

276
_end:
593,140✔
277
  if (code != TSDB_CODE_SUCCESS) {
593,140!
278
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
279
    pTaskInfo->code = code;
×
280
    T_LONG_JMP(pTaskInfo->env, code);
×
281
  }
282
  return pBlock != NULL;
593,140✔
283
}
284

285
int32_t getAggregateResultNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,092,851✔
286
  int32_t           code = TSDB_CODE_SUCCESS;
1,092,851✔
287
  int32_t           lino = 0;
1,092,851✔
288
  SAggOperatorInfo* pAggInfo = pOperator->info;
1,092,851✔
289
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;
1,092,851✔
290

291
  if (pOperator->status == OP_EXEC_DONE) {
1,092,851✔
292
    (*ppRes) = NULL;
426,370✔
293
    return code;
426,370✔
294
  }
295

296
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
666,481✔
297
  bool           hasNewGroups = false;
666,481✔
298
  do {
299
    hasNewGroups = nextGroupedResult(pOperator);
666,523✔
300
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
667,284✔
301
    QUERY_CHECK_CODE(code, lino, _end);
667,466!
302

303
    while (1) {
304
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
667,466✔
305
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL);
667,402✔
306
      QUERY_CHECK_CODE(code, lino, _end);
667,308!
307

308
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
667,308✔
309
        if (!hasNewGroups) setOperatorCompleted(pOperator);
593,121✔
310
        break;
593,197✔
311
      }
312

313
      if (pInfo->pRes->info.rows > 0) {
74,234!
314
        break;
74,234✔
315
      }
316
    }
317
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
667,431✔
318

319
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
667,389✔
320
  pOperator->resultInfo.totalRows += rows;
667,280✔
321

322
_end:
667,280✔
323
  if (code != TSDB_CODE_SUCCESS) {
667,280!
324
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
325
    pTaskInfo->code = code;
×
326
    T_LONG_JMP(pTaskInfo->env, code);
×
327
  }
328

329
  (*ppRes) = (rows == 0) ? NULL : pInfo->pRes;
667,280✔
330
  return code;
667,280✔
331
}
332

333
static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) {
×
334
  SSDataBlock* pRes = NULL;
×
335
  int32_t code = getAggregateResultNext(pOperator, &pRes);
×
336
  return pRes;
×
337
}
338

339
int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
1,756,145✔
340
  int32_t code = TSDB_CODE_SUCCESS;
1,756,145✔
341
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
1,756,145!
342
    qError("%s failed at line %d since pCtx is NULL.", __func__, __LINE__);
×
343
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
344
  }
345
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
6,560,587✔
346
    if (functionNeedToExecute(&pCtx[k])) {
4,804,423✔
347
      // todo add a dummy function to avoid process check
348
      if (pCtx[k].fpSet.process == NULL) {
4,803,039✔
349
        continue;
134,127✔
350
      }
351

352
      if ((&pCtx[k])->input.pData[0] == NULL) {
4,668,912!
353
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
354
        qError("%s aggregate function error happens, input data is NULL.", GET_TASKID(pOperator->pTaskInfo));
×
355
      } else {
356
        code = pCtx[k].fpSet.process(&pCtx[k]);
4,668,912✔
357
      }
358

359
      if (code != TSDB_CODE_SUCCESS) {
4,669,035✔
360
        if (pCtx[k].fpSet.cleanup != NULL) {
20!
361
          pCtx[k].fpSet.cleanup(&pCtx[k]);
×
362
        }
363
        qError("%s aggregate function error happens, code:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
20!
364
        return code;
20✔
365
      }
366
    }
367
  }
368

369
  return TSDB_CODE_SUCCESS;
1,756,164✔
370
}
371

372
static int32_t createDataBlockForEmptyInput(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
109,576✔
373
  int32_t code = TSDB_CODE_SUCCESS;
109,576✔
374
  int32_t lino = 0;
109,576✔
375
  SSDataBlock* pBlock = NULL;
109,576✔
376
  if (!tsCountAlwaysReturnValue) {
109,576✔
377
    return TSDB_CODE_SUCCESS;
48,426✔
378
  }
379

380
  SAggOperatorInfo* pAggInfo = pOperator->info;
61,150✔
381
  if (pAggInfo->groupKeyOptimized) {
61,150✔
382
    return TSDB_CODE_SUCCESS;
20,780✔
383
  }
384

385
  SOperatorInfo* downstream = pOperator->pDownstream[0];
40,370✔
386
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
40,370✔
387
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
40,058✔
388
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
39,789✔
389
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
17,135✔
390
    return TSDB_CODE_SUCCESS;
596✔
391
  }
392

393
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
39,774✔
394

395
  if (!pAggInfo->hasCountFunc) {
39,774✔
396
    return TSDB_CODE_SUCCESS;
22,427✔
397
  }
398

399
  code = createDataBlock(&pBlock);
17,347✔
400
  if (code) {
17,361!
401
    return code;
×
402
  }
403

404
  pBlock->info.rows = 1;
17,361✔
405
  pBlock->info.capacity = 0;
17,361✔
406

407
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
41,382✔
408
    SColumnInfoData colInfo = {0};
24,022✔
409
    colInfo.hasNull = true;
24,022✔
410
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
24,022✔
411
    colInfo.info.bytes = 1;
24,022✔
412

413
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
24,022✔
414
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
48,713✔
415
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
24,692✔
416
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
24,692✔
417
        int32_t slotId = pFuncParam->pCol->slotId;
24,634✔
418
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
24,634✔
419
        if (slotId >= numOfCols) {
24,634✔
420
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
18,547✔
421
          QUERY_CHECK_CODE(code, lino, _end);
18,547!
422

423
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
41,496✔
424
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
22,950✔
425
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
22,949!
426
          }
427
        }
428
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
58✔
429
        // do nothing
430
      }
431
    }
432
  }
433

434
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
17,360✔
435
  QUERY_CHECK_CODE(code, lino, _end);
17,361!
436

437
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
40,314✔
438
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
22,952✔
439
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
22,952!
440
    colDataSetNULL(pColInfoData, 0);
441
  }
442
  *ppBlock = pBlock;
17,360✔
443

444
_end:
17,360✔
445
  if (code != TSDB_CODE_SUCCESS) {
17,360!
446
    blockDataDestroy(pBlock);
×
447
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
448
  }
449
  return code;
17,359✔
450
}
451

452
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
1,705,642✔
453
  if (!blockAllocated) {
1,705,642✔
454
    return;
1,688,482✔
455
  }
456

457
  blockDataDestroy(*ppBlock);
17,160✔
458
  *ppBlock = NULL;
17,361✔
459
}
460

461
int32_t setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
1,755,997✔
462
  int32_t           code = TSDB_CODE_SUCCESS;
1,755,997✔
463
  SAggOperatorInfo* pAggInfo = pOperator->info;
1,755,997✔
464
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
1,755,997✔
465
    return code;
595,320✔
466
  }
467

468
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
1,160,677✔
469

470
  // record the current active group id
471
  pAggInfo->groupId = groupId;
1,161,161✔
472
  return code;
1,161,161✔
473
}
474

475
int32_t doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
1,161,385✔
476
  // for simple group by query without interval, all the tables belong to one group result.
477
  int32_t           code = TSDB_CODE_SUCCESS;
1,161,385✔
478
  int32_t           lino = 0;
1,161,385✔
479
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
1,161,385✔
480
  SAggOperatorInfo* pAggInfo = pOperator->info;
1,161,385✔
481

482
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
1,161,385✔
483
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
1,161,385✔
484
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
1,161,385✔
485

486
  SResultRow* pResultRow =
487
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
1,161,385✔
488
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
489
  if (pResultRow == NULL || pTaskInfo->code != 0) {
1,161,778!
490
    code = pTaskInfo->code;
×
491
    lino = __LINE__;
×
492
    goto _end;
×
493
  }
494
  /*
495
   * not assign result buffer yet, add new result buffer
496
   * all group belong to one result set, and each group result has different group id so set the id to be one
497
   */
498
  if (pResultRow->pageId == -1) {
1,161,795!
499
    code = addNewResultRowBuf(pResultRow, pAggInfo->aggSup.pResultBuf, pAggInfo->binfo.pRes->info.rowSize);
×
500
    QUERY_CHECK_CODE(code, lino, _end);
×
501
  }
502

503
  code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
1,161,795✔
504
  QUERY_CHECK_CODE(code, lino, _end);
1,161,115✔
505

506
_end:
1,161,089✔
507
  if (code != TSDB_CODE_SUCCESS) {
1,161,098✔
508
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
30!
509
  }
510
  return code;
1,161,145✔
511
}
512

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

519
  SFilePage* pData = NULL;
×
520

521
  // in the first scan, new space needed for results
522
  int32_t pageId = -1;
×
523
  SArray* list = getDataBufPagesIdList(pResultBuf);
×
524

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

540
    pageId = getPageId(pi);
×
541

542
    if (pData->num + size > getBufPageSize(pResultBuf)) {
×
543
      // release current page first, and prepare the next one
544
      releaseBufPageInfo(pResultBuf, pi);
×
545

546
      pData = getNewBufPage(pResultBuf, &pageId);
×
547
      if (pData == NULL) {
×
548
        qError("failed to get buffer, code:%s", tstrerror(terrno));
×
549
        return terrno;
×
550
      }
551
      pData->num = sizeof(SFilePage);
×
552
    }
553
  }
554

555
  if (pData == NULL) {
×
556
    return -1;
×
557
  }
558

559
  // set the number of rows in current disk page
560
  if (pWindowRes->pageId == -1) {  // not allocated yet, allocate new buffer
×
561
    pWindowRes->pageId = pageId;
×
562
    pWindowRes->offset = (int32_t)pData->num;
×
563

564
    pData->num += size;
×
565
  }
566

567
  return 0;
×
568
}
569

570
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
1,711,108✔
571
                         const char* pKey) {
572
  int32_t code = 0;
1,711,108✔
573
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
574

575
  pAggSup->currentPageId = -1;
1,711,108✔
576
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
1,711,108✔
577
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
1,712,172!
578
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
1,712,769✔
579

580
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
1,711,805!
581
    return terrno;
80✔
582
  }
583

584
  uint32_t defaultPgsz = 0;
1,711,725✔
585
  int64_t defaultBufsz = 0;
1,711,725✔
586
  code = getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
1,711,725✔
587
  if (code) {
1,712,163!
588
    qError("failed to get buff page size, rowSize:%d", pAggSup->resultRowSize);
×
589
    return code;
×
590
  }
591

592
  if (!osTempSpaceAvailable()) {
1,712,163!
593
    code = TSDB_CODE_NO_DISKSPACE;
×
594
    qError("Init stream agg supporter failed since %s, key:%s, tempDir:%s", tstrerror(code), pKey, tsTempDir);
×
595
    return code;
×
596
  }
597

598
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
1,711,585✔
599
  if (code != TSDB_CODE_SUCCESS) {
1,712,363!
600
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
×
601
    return code;
×
602
  }
603

604
  return code;
1,712,438✔
605
}
606

607
void cleanupResultInfoInStream(SExecTaskInfo* pTaskInfo, void* pState, SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
1,041✔
608
  int32_t         code = TSDB_CODE_SUCCESS;
1,041✔
609
  SStorageAPI*    pAPI = &pTaskInfo->storageAPI;
1,041✔
610
  int32_t         numOfExprs = pSup->numOfExprs;
1,041✔
611
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
1,041✔
612
  SqlFunctionCtx* pCtx = pSup->pCtx;
1,041✔
613
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
1,041✔
614
  bool            needCleanup = false;
1,041✔
615

616
  for (int32_t j = 0; j < numOfExprs; ++j) {
8,970✔
617
    needCleanup |= pCtx[j].needCleanup;
7,929✔
618
  }
619
  if (!needCleanup) {
1,041!
620
    return;
1,041✔
621
  }
622
  
623
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
×
624
    SResultWindowInfo* pWinInfo = taosArrayGet(pGroupResInfo->pRows, i);
×
625
    SRowBuffPos*       pPos = pWinInfo->pStatePos;
×
626
    SResultRow*        pRow = NULL;
×
627

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

634
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
635
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
636
      if (pCtx[j].fpSet.cleanup) {
×
637
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
638
      }
639
    }
640
  }
641
}
642

643
void cleanupResultInfoInGroupResInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
654,253✔
644
                                  SGroupResInfo* pGroupResInfo) {
645
  int32_t         numOfExprs = pSup->numOfExprs;
654,253✔
646
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
654,253✔
647
  SqlFunctionCtx* pCtx = pSup->pCtx;
654,253✔
648
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
654,253✔
649
  bool            needCleanup = false;
654,327✔
650

651
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,397,735✔
652
    needCleanup |= pCtx[j].needCleanup;
1,743,408✔
653
  }
654
  if (!needCleanup) {
654,327✔
655
    return;
648,706✔
656
  }
657

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

668

669
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
670
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
671
      if (pCtx[j].fpSet.cleanup) {
×
672
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
673
      }
674
    }
675
    releaseBufPage(pBuf, page);
×
676
  }
677
}
678

679
void cleanupResultInfoInHashMap(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SDiskbasedBuf* pBuf,
282,628✔
680
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
681
  int32_t         numOfExprs = pSup->numOfExprs;
282,628✔
682
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
282,628✔
683
  SqlFunctionCtx* pCtx = pSup->pCtx;
282,628✔
684
  bool            needCleanup = false;
282,628✔
685
  for (int32_t j = 0; j < numOfExprs; ++j) {
822,455✔
686
    needCleanup |= pCtx[j].needCleanup;
539,827✔
687
  }
688
  if (!needCleanup) {
282,628✔
689
    return;
282,611✔
690
  }
691

692
  // begin from last iter
693
  void*   pData = pGroupResInfo->dataPos;
17✔
694
  int32_t iter = pGroupResInfo->iter;
17✔
695
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
17!
696
    SResultRowPosition* pos = pData;
×
697

698
    SFilePage* page = getBufPage(pBuf, pos->pageId);
×
699
    if (page == NULL) {
×
700
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
701
      continue;
×
702
    }
703

704
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
×
705

706
    for (int32_t j = 0; j < numOfExprs; ++j) {
×
707
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
×
708
      if (pCtx[j].fpSet.cleanup) {
×
709
        pCtx[j].fpSet.cleanup(&pCtx[j]);
×
710
      }
711
    }
712

713
    releaseBufPage(pBuf, page);
×
714
  }
715
}
716

717
void cleanupResultInfo(SExecTaskInfo* pTaskInfo, SExprSupp* pSup, SGroupResInfo* pGroupResInfo,
936,916✔
718
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
719
  if (cleanGroupResInfo) {
936,916✔
720
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
654,305✔
721
  } else {
722
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
282,611✔
723
  }
724
}
936,984✔
725
void cleanupAggSup(SAggSupporter* pAggSup) {
1,712,862✔
726
  taosMemoryFreeClear(pAggSup->keyBuf);
1,712,862!
727
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
1,712,987✔
728
  destroyDiskbasedBuf(pAggSup->pResultBuf);
1,713,054✔
729
}
1,713,041✔
730

731
int32_t initAggSup(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
1,711,572✔
732
                   const char* pkey, void* pState, SFunctionStateStore* pStore) {
733
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols, pStore);
1,711,572✔
734
  if (code != TSDB_CODE_SUCCESS) {
1,712,052!
735
    return code;
×
736
  }
737

738
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
1,712,052✔
739
  if (code != TSDB_CODE_SUCCESS) {
1,712,539!
740
    return code;
×
741
  }
742

743
  for (int32_t i = 0; i < numOfCols; ++i) {
7,296,003✔
744
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
5,583,464✔
745
    if (pState) {
5,583,464✔
746
      pSup->pCtx[i].saveHandle.pBuf = NULL;
33,426✔
747
      pSup->pCtx[i].saveHandle.pState = pState;
33,426✔
748
      pSup->pCtx[i].exprIdx = i;
33,426✔
749
    } else {
750
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
5,550,038✔
751
    }
752
  }
753

754
  return TSDB_CODE_SUCCESS;
1,712,539✔
755
}
756

757
int32_t applyAggFunctionOnPartialTuples(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData,
149,325,289✔
758
                                        int32_t offset, int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
759
  int32_t code = TSDB_CODE_SUCCESS;
149,325,289✔
760
  for (int32_t k = 0; k < numOfOutput; ++k) {
861,427,654✔
761
    // keep it temporarily
762
    SFunctionCtxStatus status = {0};
711,978,342✔
763
    functionCtxSave(&pCtx[k], &status);
711,978,342✔
764

765
    pCtx[k].input.startRowIndex = offset;
710,155,508✔
766
    pCtx[k].input.numOfRows = forwardStep;
710,155,508✔
767

768
    // not a whole block involved in query processing, statistics data can not be used
769
    // NOTE: the original value of isSet have been changed here
770
    if (pCtx[k].input.colDataSMAIsSet && forwardStep < numOfTotal) {
710,155,508!
771
      pCtx[k].input.colDataSMAIsSet = false;
×
772
    }
773

774
    if (pCtx[k].isPseudoFunc) {
710,155,508✔
775
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
191,209,914✔
776

777
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
191,209,914✔
778

779
      SColumnInfoData idata = {0};
191,209,914✔
780
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
191,209,914✔
781
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
191,209,914✔
782
      idata.pData = p;
191,209,914✔
783

784
      SScalarParam out = {.columnData = &idata};
191,209,914✔
785
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
191,209,914✔
786
      code = pCtx[k].sfp.process(&tw, 1, &out);
191,209,914✔
787
      if (code != TSDB_CODE_SUCCESS) {
195,835,964✔
788
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
652,697!
789
        taskInfo->code = code;
×
790
        return code;
×
791
      }
792
      pEntryInfo->numOfRes = 1;
195,183,267✔
793
    } else {
794
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
518,945,594✔
795
        if ((&pCtx[k])->input.pData[0] == NULL) {
469,718,619!
796
          code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
797
          qError("%s apply functions error, input data is NULL.", GET_TASKID(taskInfo));
×
798
        } else {
799
          code = pCtx[k].fpSet.process(&pCtx[k]);
469,718,619✔
800
        }
801

802
        if (code != TSDB_CODE_SUCCESS) {
471,606,660!
803
          if (pCtx[k].fpSet.cleanup != NULL) {
×
804
            pCtx[k].fpSet.cleanup(&pCtx[k]);
×
805
          }
806
          qError("%s apply functions error, code:%s", GET_TASKID(taskInfo), tstrerror(code));
×
807
          taskInfo->code = code;
×
808
          return code;
×
809
        }
810
      }
811

812
      // restore it
813
      functionCtxRestore(&pCtx[k], &status);
530,347,806✔
814
    }
815
  }
816
  return code;
149,449,312✔
817
}
818

819
void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
711,172,027✔
820
  pStatus->hasAgg = pCtx->input.colDataSMAIsSet;
711,172,027✔
821
  pStatus->numOfRows = pCtx->input.numOfRows;
711,172,027✔
822
  pStatus->startOffset = pCtx->input.startRowIndex;
711,172,027✔
823
}
711,172,027✔
824

825
void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
529,417,470✔
826
  pCtx->input.colDataSMAIsSet = pStatus->hasAgg;
529,417,470✔
827
  pCtx->input.numOfRows = pStatus->numOfRows;
529,417,470✔
828
  pCtx->input.startRowIndex = pStatus->startOffset;
529,417,470✔
829
}
529,417,470✔
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