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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

62.6
/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,
2,140,754✔
75
                                    SOperatorInfo** pOptrInfo) {
76
  QRY_PARAM_CHECK(pOptrInfo);
2,140,754!
77

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

85
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
2,140,754!
86
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
2,141,599!
87
  if (pInfo == NULL || pOperator == NULL) {
2,142,233!
88
    code = terrno;
×
89
    goto _error;
×
90
  }
91

92
  pOperator->exprSupp.hasWindowOrGroup = false;
2,142,417✔
93

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

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

101
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
2,143,599✔
102
  TSDB_CHECK_CODE(code, lino, _error);
2,141,996!
103

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

108
  if (pAggNode->pExprs != NULL) {
2,141,751✔
109
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
207,817✔
110
    TSDB_CHECK_CODE(code, lino, _error);
207,828!
111
  }
112

113
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
2,141,762✔
114
  TSDB_CHECK_CODE(code, lino, _error);
2,142,180!
115

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

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

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

133
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
2,141,829✔
134
    STableScanInfo* pTableScanInfo = downstream->info;
535,977✔
135
    pTableScanInfo->base.pdInfo.pExprSup = &pOperator->exprSupp;
535,977✔
136
    pTableScanInfo->base.pdInfo.pAggSup = &pInfo->aggSup;
535,977✔
137
  }
138

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

144
  *pOptrInfo = pOperator;
2,142,575✔
145
  return TSDB_CODE_SUCCESS;
2,142,575✔
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) {
2,143,555✔
158
  if (param == NULL) {
2,143,555!
159
    return;
×
160
  }
161
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
2,143,555✔
162
  cleanupBasicInfo(&pInfo->binfo);
2,143,555✔
163

164
  if (pInfo->pOperator) {
2,144,451!
165
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
2,144,469✔
166
                      pInfo->cleanGroupResInfo);
2,144,469✔
167
    pInfo->pOperator = NULL;
2,143,298✔
168
  }
169
  cleanupAggSup(&pInfo->aggSup);
2,143,280✔
170
  cleanupExprSupp(&pInfo->scalarExprSup);
2,144,455✔
171
  cleanupGroupResInfo(&pInfo->groupResInfo);
2,144,088✔
172
  taosMemoryFreeClear(param);
2,144,443!
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) {
2,264,981✔
183
  int32_t           code = TSDB_CODE_SUCCESS;
2,264,981✔
184
  int32_t           lino = 0;
2,264,981✔
185
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
2,264,981✔
186
  SAggOperatorInfo* pAggInfo = pOperator->info;
2,264,981✔
187

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

196
  SExprSupp*   pSup = &pOperator->exprSupp;
2,189,696✔
197
  int64_t      st = taosGetTimestampUs();
2,192,733✔
198
  int32_t      order = pAggInfo->binfo.inputTsOrder;
2,192,733✔
199
  SSDataBlock* pBlock = pAggInfo->pNewGroupBlock;
2,192,733✔
200

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

210
    code = doAggregateImpl(pOperator, pSup->pCtx);
50,653✔
211
    QUERY_CHECK_CODE(code, lino, _end);
50,653!
212
  }
213
  while (1) {
9,663,143✔
214
    bool blockAllocated = false;
11,855,876✔
215
    pBlock = getNextBlockFromDownstream(pOperator, 0);
11,855,876✔
216
    if (pBlock == NULL) {
11,824,502✔
217
      if (!pAggInfo->hasValidBlock) {
2,261,706✔
218
        code = createDataBlockForEmptyInput(pOperator, &pBlock);
271,141✔
219
        QUERY_CHECK_CODE(code, lino, _end);
271,016!
220

221
        if (pBlock == NULL) {
271,016✔
222
          break;
124,406✔
223
        }
224
        blockAllocated = true;
146,610✔
225
      } else {
226
        break;
1,990,565✔
227
      }
228
    }
229
    pAggInfo->hasValidBlock = true;
9,709,406✔
230
    pAggInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
9,709,406✔
231

232
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
233
    if (pAggInfo->scalarExprSup.pExprInfo != NULL && !blockAllocated) {
9,709,406✔
234
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
1,474,154✔
235
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
1,474,154✔
236
      if (code != TSDB_CODE_SUCCESS) {
1,474,354!
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) {
9,709,606✔
243
      pAggInfo->pNewGroupBlock = pBlock;
50,720✔
244
      break;
50,720✔
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);
9,658,886✔
248
    if (code != TSDB_CODE_SUCCESS) {
9,662,317✔
249
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
40✔
250
      T_LONG_JMP(pTaskInfo->env, code);
40!
251
    }
252
    code = setInputDataBlock(pSup, pBlock, order, pBlock->info.scanFlag, true);
9,662,277✔
253
    if (code != TSDB_CODE_SUCCESS) {
9,661,843!
254
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
×
255
      T_LONG_JMP(pTaskInfo->env, code);
×
256
    }
257

258
    code = doAggregateImpl(pOperator, pSup->pCtx);
9,661,843✔
259
    if (code != TSDB_CODE_SUCCESS) {
9,663,366✔
260
      destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
22✔
261
      T_LONG_JMP(pTaskInfo->env, code);
22!
262
    }
263

264
    destroyDataBlockForEmptyInput(blockAllocated, &pBlock);
9,663,344✔
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) {
2,165,691✔
269
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
1!
270
  }
271

272
  code = initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
2,165,690✔
273
  QUERY_CHECK_CODE(code, lino, _end);
2,165,355!
274
  pAggInfo->cleanGroupResInfo = true;
2,165,355✔
275

276
_end:
2,165,355✔
277
  if (code != TSDB_CODE_SUCCESS) {
2,165,355!
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;
2,165,355✔
283
}
284

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

291
  if (pOperator->status == OP_EXEC_DONE) {
4,231,327✔
292
    (*ppRes) = NULL;
1,966,584✔
293
    return code;
1,966,584✔
294
  }
295

296
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2,264,743✔
297
  bool           hasNewGroups = false;
2,264,743✔
298
  do {
299
    hasNewGroups = nextGroupedResult(pOperator);
2,264,785✔
300
    code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
2,240,387✔
301
    QUERY_CHECK_CODE(code, lino, _end);
2,241,154!
302

303
    while (1) {
304
      doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
2,241,154✔
305
      code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL);
2,238,673✔
306
      QUERY_CHECK_CODE(code, lino, _end);
2,238,784!
307

308
      if (!hasRemainResults(&pAggInfo->groupResInfo)) {
2,238,784✔
309
        if (!hasNewGroups) setOperatorCompleted(pOperator);
2,164,098✔
310
        break;
2,165,320✔
311
      }
312

313
      if (pInfo->pRes->info.rows > 0) {
75,264!
314
        break;
75,264✔
315
      }
316
    }
317
  } while (pInfo->pRes->info.rows == 0 && hasNewGroups);
2,240,584✔
318

319
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
2,240,542✔
320
  pOperator->resultInfo.totalRows += rows;
2,240,013✔
321

322
_end:
2,240,013✔
323
  if (code != TSDB_CODE_SUCCESS) {
2,240,013!
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;
2,240,013✔
330
  return code;
2,240,013✔
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) {
9,711,690✔
340
  int32_t code = TSDB_CODE_SUCCESS;
9,711,690✔
341
  if (!pOperator || (pOperator->exprSupp.numOfExprs > 0 && pCtx == NULL)) {
9,711,690!
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) {
24,206,125✔
346
    if (functionNeedToExecute(&pCtx[k])) {
14,491,612✔
347
      // todo add a dummy function to avoid process check
348
      if (pCtx[k].fpSet.process == NULL) {
14,490,423✔
349
        continue;
224,354✔
350
      }
351

352
      if ((&pCtx[k])->input.pData[0] == NULL) {
14,266,069!
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]);
14,266,069✔
357
      }
358

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

369
  return TSDB_CODE_SUCCESS;
9,714,513✔
370
}
371

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

380
  SAggOperatorInfo* pAggInfo = pOperator->info;
222,670✔
381
  if (pAggInfo->groupKeyOptimized) {
222,670✔
382
    return TSDB_CODE_SUCCESS;
42,982✔
383
  }
384

385
  SOperatorInfo* downstream = pOperator->pDownstream[0];
179,688✔
386
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_PARTITION ||
179,688✔
387
      downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_SORT ||
179,401✔
388
      (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN &&
179,217✔
389
       ((STableScanInfo*)downstream->info)->hasGroupByTag == true)) {
30,726✔
390
    return TSDB_CODE_SUCCESS;
502✔
391
  }
392

393
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
179,186✔
394

395
  if (!pAggInfo->hasCountFunc) {
179,186✔
396
    return TSDB_CODE_SUCCESS;
32,491✔
397
  }
398

399
  code = createDataBlock(&pBlock);
146,695✔
400
  if (code) {
146,716!
401
    return code;
×
402
  }
403

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

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

413
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
159,523✔
414
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
319,708✔
415
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
160,218✔
416
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
160,218✔
417
        int32_t slotId = pFuncParam->pCol->slotId;
160,151✔
418
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
160,151✔
419
        if (slotId >= numOfCols) {
160,086✔
420
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
147,904✔
421
          QUERY_CHECK_CODE(code, lino, _end);
147,907!
422

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

434
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
146,683✔
435
  QUERY_CHECK_CODE(code, lino, _end);
146,734!
436

437
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
299,767✔
438
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
153,000✔
439
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
153,026!
440
    colDataSetNULL(pColInfoData, 0);
441
  }
442
  *ppBlock = pBlock;
146,606✔
443

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

452
void destroyDataBlockForEmptyInput(bool blockAllocated, SSDataBlock** ppBlock) {
9,662,897✔
453
  if (!blockAllocated) {
9,662,897✔
454
    return;
9,516,594✔
455
  }
456

457
  blockDataDestroy(*ppBlock);
146,303✔
458
  *ppBlock = NULL;
146,743✔
459
}
460

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

468
  code = doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
2,806,769✔
469

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

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

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

486
  SResultRow* pResultRow =
487
      doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId, sizeof(groupId), true,
2,808,028✔
488
                             groupId, pTaskInfo, false, &pAggInfo->aggSup, true);
489
  if (pResultRow == NULL || pTaskInfo->code != 0) {
2,808,696!
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) {
2,808,737!
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);
2,808,737✔
504
  QUERY_CHECK_CODE(code, lino, _end);
2,807,456!
505

506
_end:
2,807,456✔
507
  if (code != TSDB_CODE_SUCCESS) {
2,807,415✔
508
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
40!
509
  }
510
  return code;
2,807,499✔
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,
7,044,760✔
571
                         const char* pKey) {
572
  int32_t code = 0;
7,044,760✔
573
  //  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
574

575
  pAggSup->currentPageId = -1;
7,044,760✔
576
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
7,044,760✔
577
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
7,050,646!
578
  pAggSup->pResultRowHashTable = tSimpleHashInit(100, taosFastHash);
7,052,622✔
579

580
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
7,046,337!
UNCOV
581
    return terrno;
×
582
  }
583

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

592
  if (!osTempSpaceAvailable()) {
7,049,140!
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);
7,048,204✔
599
  if (code != TSDB_CODE_SUCCESS) {
7,052,503✔
600
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
1,127!
601
    return code;
×
602
  }
603

604
  return code;
7,051,376✔
605
}
606

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

616
  for (int32_t j = 0; j < numOfExprs; ++j) {
27,087✔
617
    needCleanup |= pCtx[j].needCleanup;
25,244✔
618
  }
619
  if (!needCleanup) {
1,843!
620
    return;
1,843✔
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,
3,764,620✔
644
                                  SGroupResInfo* pGroupResInfo) {
645
  int32_t         numOfExprs = pSup->numOfExprs;
3,764,620✔
646
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
3,764,620✔
647
  SqlFunctionCtx* pCtx = pSup->pCtx;
3,764,620✔
648
  int32_t         numOfRows = getNumOfTotalRes(pGroupResInfo);
3,764,620✔
649
  bool            needCleanup = false;
3,763,869✔
650

651
  for (int32_t j = 0; j < numOfExprs; ++j) {
11,090,591✔
652
    needCleanup |= pCtx[j].needCleanup;
7,326,722✔
653
  }
654
  if (!needCleanup) {
3,763,869✔
655
    return;
3,743,438✔
656
  }
657

658
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
20,431!
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,
991,061✔
680
                       SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap) {
681
  int32_t         numOfExprs = pSup->numOfExprs;
991,061✔
682
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
991,061✔
683
  SqlFunctionCtx* pCtx = pSup->pCtx;
991,061✔
684
  bool            needCleanup = false;
991,061✔
685
  for (int32_t j = 0; j < numOfExprs; ++j) {
2,856,226✔
686
    needCleanup |= pCtx[j].needCleanup;
1,865,165✔
687
  }
688
  if (!needCleanup) {
991,061✔
689
    return;
989,689✔
690
  }
691

692
  // begin from last iter
693
  void*   pData = pGroupResInfo->dataPos;
1,372✔
694
  int32_t iter = pGroupResInfo->iter;
1,372✔
695
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
1,372!
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,
4,755,640✔
718
                       SAggSupporter *pAggSup, bool cleanGroupResInfo) {
719
  if (cleanGroupResInfo) {
4,755,640✔
720
    cleanupResultInfoInGroupResInfo(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo);
3,764,954✔
721
  } else {
722
    cleanupResultInfoInHashMap(pTaskInfo, pSup, pAggSup->pResultBuf, pGroupResInfo, pAggSup->pResultRowHashTable);
990,686✔
723
  }
724
}
4,755,185✔
725
void cleanupAggSup(SAggSupporter* pAggSup) {
7,053,837✔
726
  taosMemoryFreeClear(pAggSup->keyBuf);
7,053,837!
727
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
7,055,192✔
728
  destroyDiskbasedBuf(pAggSup->pResultBuf);
7,055,670✔
729
}
7,056,040✔
730

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

738
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
7,048,655✔
739
  if (code != TSDB_CODE_SUCCESS) {
7,051,945!
740
    return code;
×
741
  }
742

743
  for (int32_t i = 0; i < numOfCols; ++i) {
28,244,001✔
744
    pSup->pCtx[i].hasWindowOrGroup = pSup->hasWindowOrGroup;
21,192,056✔
745
    if (pState) {
21,192,056✔
746
      pSup->pCtx[i].saveHandle.pBuf = NULL;
76,743✔
747
      pSup->pCtx[i].saveHandle.pState = pState;
76,743✔
748
      pSup->pCtx[i].exprIdx = i;
76,743✔
749
    } else {
750
      pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
21,115,313✔
751
    }
752
  }
753

754
  return TSDB_CODE_SUCCESS;
7,051,945✔
755
}
756

757
int32_t applyAggFunctionOnPartialTuples(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData,
413,043,335✔
758
                                        int32_t offset, int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
759
  int32_t code = TSDB_CODE_SUCCESS;
413,043,335✔
760
  for (int32_t k = 0; k < numOfOutput; ++k) {
1,595,081,721✔
761
    // keep it temporarily
762
    SFunctionCtxStatus status = {0};
1,184,035,988✔
763
    functionCtxSave(&pCtx[k], &status);
1,184,035,988✔
764

765
    pCtx[k].input.startRowIndex = offset;
1,180,513,867✔
766
    pCtx[k].input.numOfRows = forwardStep;
1,180,513,867✔
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) {
1,180,513,867!
771
      pCtx[k].input.colDataSMAIsSet = false;
×
772
    }
773

774
    if (pCtx[k].isPseudoFunc) {
1,180,513,867✔
775
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
292,561,776✔
776

777
      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
292,561,776✔
778

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

784
      SScalarParam out = {.columnData = &idata};
292,561,776✔
785
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
292,561,776✔
786
      code = pCtx[k].sfp.process(&tw, 1, &out);
292,561,776✔
787
      if (code != TSDB_CODE_SUCCESS) {
297,393,978✔
788
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
454,355!
789
        taskInfo->code = code;
×
790
        return code;
×
791
      }
792
      pEntryInfo->numOfRes = 1;
296,939,623✔
793
    } else {
794
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
887,952,091✔
795
        if ((&pCtx[k])->input.pData[0] == NULL) {
722,148,660!
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]);
722,148,660✔
800
        }
801

802
        if (code != TSDB_CODE_SUCCESS) {
723,445,525!
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);
900,023,800✔
814
    }
815
  }
816
  return code;
411,045,733✔
817
}
818

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

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

© 2025 Coveralls, Inc