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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

76.09
/source/libs/executor/src/groupoperator.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 "os.h"
19
#include "query.h"
20
#include "tname.h"
21
#include "tutil.h"
22

23
#include "tdatablock.h"
24
#include "tmsg.h"
25

26
#include "executorInt.h"
27
#include "operator.h"
28
#include "querytask.h"
29
#include "tcompare.h"
30
#include "thash.h"
31
#include "ttypes.h"
32

33
typedef struct SGroupbyOperatorInfo {
34
  SOptrBasicInfo binfo;
35
  SAggSupporter  aggSup;
36
  SArray*        pGroupCols;     // group by columns, SArray<SColumn>
37
  SArray*        pGroupColVals;  // current group column values, SArray<SGroupKeys>
38
  bool           isInit;         // denote if current val is initialized or not
39
  char*          keyBuf;         // group by keys for hash
40
  int32_t        groupKeyLen;    // total group by column width
41
  SGroupResInfo  groupResInfo;
42
  SExprSupp      scalarSup;
43
  SOperatorInfo  *pOperator;
44
  SLimitInfo     limitInfo;
45
} SGroupbyOperatorInfo;
46

47
// The sort in partition may be needed later.
48
typedef struct SPartitionOperatorInfo {
49
  SOptrBasicInfo binfo;
50
  SArray*        pGroupCols;
51
  SArray*        pGroupColVals;  // current group column values, SArray<SGroupKeys>
52
  char*          keyBuf;         // group by keys for hash
53
  int32_t        groupKeyLen;    // total group by column width
54
  SHashObj*      pGroupSet;      // quick locate the window object for each result
55

56
  SDiskbasedBuf* pBuf;              // query result buffer based on blocked-wised disk file
57
  int32_t        rowCapacity;       // maximum number of rows for each buffer page
58
  int32_t*       columnOffset;      // start position for each column data
59
  SArray*        sortedGroupArray;  // SDataGroupInfo sorted by group id
60
  int32_t        groupIndex;        // group index
61
  int32_t        pageIndex;         // page index of current group
62
  SExprSupp      scalarSup;
63

64
  int32_t remainRows;
65
  int32_t orderedRows;
66
  SArray* pOrderInfoArr;
67
} SPartitionOperatorInfo;
68

69
static void*    getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len);
70
static int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity);
71
static int32_t  setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
72
                                        int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup);
73
static int32_t  extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes);
74

75
static void freeGroupKey(void* param) {
39,520,613✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
39,520,613✔
77
  taosMemoryFree(pKey->pData);
39,520,613✔
78
}
39,520,658✔
79

80
static void destroyGroupOperatorInfo(void* param) {
30,347,061✔
81
  if (param == NULL) {
30,347,061✔
82
    return;
×
83
  }
84
  SGroupbyOperatorInfo* pInfo = (SGroupbyOperatorInfo*)param;
30,347,061✔
85

86
  cleanupBasicInfo(&pInfo->binfo);
30,347,061✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
30,346,544✔
88
  taosArrayDestroy(pInfo->pGroupCols);
30,347,061✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
30,347,623✔
90
  cleanupExprSupp(&pInfo->scalarSup);
30,345,995✔
91

92
  if (pInfo->pOperator != NULL) {
30,347,106✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
30,256,489✔
94
                      false);
95
    pInfo->pOperator = NULL;
30,254,967✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
30,345,584✔
99
  cleanupAggSup(&pInfo->aggSup);
30,344,456✔
100
  taosMemoryFreeClear(param);
30,345,486✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
33,155,224✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
33,155,224✔
105
  if ((*pGroupColVals) == NULL) {
33,147,817✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
33,152,152✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
76,400,342✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
43,245,615✔
112
    if (!pCol) {
43,249,618✔
113
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
114
      return terrno;
×
115
    }
116
    (*keyLen) += pCol->bytes;  // actual data + null_flag
43,249,618✔
117

118
    SGroupKeys key = {0};
43,248,137✔
119
    key.bytes = pCol->bytes;
43,248,014✔
120
    key.type = pCol->type;
43,239,044✔
121
    key.isNull = false;
43,235,066✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
43,235,066✔
123
    if (key.pData == NULL) {
43,238,219✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
43,238,219✔
128
    if (!tmp) {
43,251,185✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
33,154,727✔
134
  (*keyLen) += nullFlagSize;
33,154,727✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
33,155,602✔
137
  if ((*keyBuf) == NULL) {
33,147,872✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
33,151,207✔
142
}
143

144
static bool groupKeyCompare(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex,
2,147,483,647✔
145
                            int32_t numOfGroupCols) {
146
  SColumnDataAgg* pColAgg = NULL;
2,147,483,647✔
147
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
148
    SColumn*         pCol = taosArrayGet(pGroupCols, i);
2,147,483,647✔
149
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
2,147,483,647✔
150
    if (pBlock->pBlockAgg != NULL) {
2,147,483,647✔
151
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
152
    }
153

154
    bool isNull = colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg);
2,147,483,647✔
155

156
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
157
    if (pkey->isNull && isNull) {
2,147,483,647✔
158
      continue;
2,147,483,647✔
159
    }
160

161
    if (isNull || pkey->isNull) {
2,147,483,647✔
162
      return false;
2,147,483,647✔
163
    }
164

165
    char* val = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
166

167
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
168
      int32_t dataLen = getJsonValueLen(val);
6,790✔
169

170
      if (memcmp(pkey->pData, val, dataLen) == 0) {
6,790✔
171
        continue;
970✔
172
      } else {
173
        return false;
5,820✔
174
      }
175
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
176
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
177
        int32_t len = blobDataLen(val);
130✔
178
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
179
          continue;
×
180
        } else {
181
          return false;
×
182
        }
183
      } else {
184
        int32_t len = varDataLen(val);
2,147,483,647✔
185
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
2,147,483,647✔
186
          continue;
2,147,483,647✔
187
        } else {
188
          return false;
1,115,547,118✔
189
        }
190
      }
191
    } else {
192
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
2,147,483,647✔
193
        return false;
2,147,483,647✔
194
      }
195
    }
196
  }
197

198
  return true;
2,147,483,647✔
199
}
200

201
static void recordNewGroupKeys(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex) {
2,147,483,647✔
202
  SColumnDataAgg* pColAgg = NULL;
2,147,483,647✔
203

204
  size_t numOfGroupCols = taosArrayGetSize(pGroupCols);
2,147,483,647✔
205

206
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
207
    SColumn*         pCol = (SColumn*)taosArrayGet(pGroupCols, i);
2,147,483,647✔
208
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
2,147,483,647✔
209

210
    // valid range check. todo: return error code.
211
    if (pCol->slotId > taosArrayGetSize(pBlock->pDataBlock)) {
2,147,483,647✔
212
      continue;
×
213
    }
214

215
    if (pBlock->pBlockAgg != NULL) {
2,147,483,647✔
216
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
217
    }
218

219
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
220
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
2,147,483,647✔
221
      pkey->isNull = true;
2,147,483,647✔
222
    } else {
223
      pkey->isNull = false;
2,147,483,647✔
224
      char* val = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
225
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
226
        // if (tTagIsJson(val)) {
227
        //   terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
228
        //   return;
229
        // }
230
        int32_t dataLen = getJsonValueLen(val);
40,582✔
231
        memcpy(pkey->pData, val, dataLen);
40,582✔
232
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
233
        if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
234
          memcpy(pkey->pData, val, blobDataTLen(val));
100✔
235
        } else {
236
          memcpy(pkey->pData, val, varDataTLen(val));
2,147,483,647✔
237
        }
238
      } else {
239
        memcpy(pkey->pData, val, pkey->bytes);
2,147,483,647✔
240
      }
241
    }
242
  }
243
}
2,147,483,647✔
244

245
static int32_t buildGroupKeys(void* pKey, const SArray* pGroupColVals) {
2,147,483,647✔
246
  size_t numOfGroupCols = taosArrayGetSize(pGroupColVals);
2,147,483,647✔
247

248
  char* isNull = (char*)pKey;
2,147,483,647✔
249
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
2,147,483,647✔
250
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
251
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
252
    if (pkey->isNull) {
2,147,483,647✔
253
      isNull[i] = 1;
2,147,483,647✔
254
      continue;
2,147,483,647✔
255
    }
256

257
    isNull[i] = 0;
2,147,483,647✔
258
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
259
      int32_t dataLen = getJsonValueLen(pkey->pData);
40,582✔
260
      memcpy(pStart, (pkey->pData), dataLen);
40,582✔
261
      pStart += dataLen;
40,582✔
262
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
263
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
264
        blobDataCopy(pStart, pkey->pData);
×
265
        pStart += blobDataTLen(pkey->pData);
×
266
      } else {
267
        varDataCopy(pStart, pkey->pData);
2,147,483,647✔
268
        pStart += varDataTLen(pkey->pData);
2,147,483,647✔
269
      }
270
    } else {
271
      memcpy(pStart, pkey->pData, pkey->bytes);
2,147,483,647✔
272
      pStart += pkey->bytes;
2,147,483,647✔
273
    }
274
  }
275

276
  return (int32_t)(pStart - (char*)pKey);
2,147,483,647✔
277
}
278

279
// assign the group keys or user input constant values if required
280
static void doAssignGroupKeys(SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t totalRows, int32_t rowIndex) {
2,147,483,647✔
281
  for (int32_t i = 0; i < numOfOutput; ++i) {
2,147,483,647✔
282
    if (pCtx[i].functionId == -1) {  // select count(*),key from t group by key.
2,147,483,647✔
283
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[i]);
2,147,483,647✔
284

285
      SColumnInfoData* pColInfoData = pCtx[i].input.pData[0];
2,147,483,647✔
286
      // todo OPT all/all not NULL
287
      if (!colDataIsNull(pColInfoData, totalRows, rowIndex, NULL)) {
2,147,483,647✔
288
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
289
        char* data = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
290

291
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
292
          int32_t dataLen = getJsonValueLen(data);
15,526✔
293
          memcpy(dest, data, dataLen);
15,526✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,493,169,951✔
296
            blobDataCopy(dest, data);
×
297
          } else {
298
            varDataCopy(dest, data);
1,493,517,531✔
299
          }
300
        } else {
301
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
302
        }
303
      } else {  // it is a NULL value
304
        pEntryInfo->isNullRes = 1;
2,147,483,647✔
305
      }
306

307
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
308
    }
309
  }
310
}
2,147,483,647✔
311

312
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
206,051,499✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
206,051,499✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
206,065,694✔
315

316
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
206,070,105✔
317
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
206,075,518✔
318
  //  if (type == TSDB_DATA_TYPE_FLOAT || type == TSDB_DATA_TYPE_DOUBLE) {
319
  //  qError("QInfo:0x%" PRIx64 ", group by not supported on double/float columns, abort", GET_TASKID(pRuntimeEnv));
320
  //    return;
321
  //  }
322

323
  int32_t len = 0;
206,072,873✔
324
  terrno = TSDB_CODE_SUCCESS;
206,072,873✔
325

326
  int32_t num = 0;
206,071,592✔
327
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
328
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
329
    if (!pInfo->isInit) {
2,147,483,647✔
330
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
22,738,895✔
331
      pInfo->isInit = true;
22,741,448✔
332
      num++;
22,746,359✔
333
      continue;
22,746,359✔
334
    }
335

336
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
2,147,483,647✔
337
    if (equal) {
2,147,483,647✔
338
      num++;
2,147,483,647✔
339
      continue;
2,147,483,647✔
340
    }
341

342
    // The first row of a new block does not belongs to the previous existed group
343
    if (j == 0) {
2,147,483,647✔
344
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
177,211,731✔
345
      num = 1;
177,207,234✔
346
      continue;
177,207,234✔
347
    }
348

349
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
2,147,483,647✔
350
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
2,147,483,647✔
351
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
352
    if (ret != TSDB_CODE_SUCCESS) {  // null data, too many state code
2,147,483,647✔
353
      T_LONG_JMP(pTaskInfo->env, ret);
×
354
    }
355

356
    int32_t rowIndex = j - num;
2,147,483,647✔
357
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
2,147,483,647✔
358
                                          pOperator->exprSupp.numOfExprs);
359
    if (ret != TSDB_CODE_SUCCESS) {
2,147,483,647✔
360
      T_LONG_JMP(pTaskInfo->env, ret);
×
361
    }
362

363
    // assign the group keys or user input constant values if required
364
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
2,147,483,647✔
365
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
2,147,483,647✔
366
    num = 1;
2,147,483,647✔
367
  }
368

369
  // The data of the last group is processed here, and if there is only one group, it is also processed here.
370
  if (num > 0) {
206,067,174✔
371
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
206,068,108✔
372
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
206,060,412✔
373
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
374
    if (ret != TSDB_CODE_SUCCESS) {
206,059,852✔
375
      T_LONG_JMP(pTaskInfo->env, ret);
×
376
    }
377

378
    int32_t rowIndex = pBlock->info.rows - num;
206,059,852✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
206,060,216✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
206,055,503✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
206,055,503✔
385
  }
386
}
206,063,091✔
387

388
bool hasRemainResultByHash(SOperatorInfo* pOperator) {
2,147,483,647✔
389
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,147,483,647✔
390
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,147,483,647✔
391
  return pInfo->groupResInfo.index < tSimpleHashGetSize(pHashmap);
2,147,483,647✔
392
}
393

394
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
2,052,347,708✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,052,347,708✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,052,348,084✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,052,346,016✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
2,052,348,597✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
2,052,346,580✔
404

405
  blockDataCleanup(pBlock);
2,052,346,956✔
406
  if (!hasRemainResultByHash(pOperator)) {
2,052,347,283✔
407
    return;
7,470,681✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
2,044,877,567✔
411
  if (!pInfo->binfo.mergeResultBlock) {
2,044,877,567✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
2,027,722,266✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
34,294,418✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
17,155,301✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
17,155,301✔
419
        break;
16,184✔
420
      }
421
      pBlock->info.id.groupId = 0;
17,139,117✔
422
    }
423

424
    // clear the group id info in SSDataBlock, since the client does not need it
425
    pBlock->info.id.groupId = 0;
17,155,301✔
426
  }
427
}
428

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
2,022,221,011✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
2,022,221,011✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
60,892✔
432
    return true;  // limit reached, stop processing further rows
20,769✔
433
  }
434
  return false;
2,022,196,994✔
435
}
436

437
static int32_t doGroupResultSlimit(SSDataBlock* pRes, SLimitInfo* pLimitInfo) {
2,052,349,076✔
438
  int32_t code = TSDB_CODE_SUCCESS;
2,052,349,076✔
439
  int32_t lino = 0;
2,052,349,076✔
440

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
2,052,349,076✔
442
    return TSDB_CODE_SUCCESS;
7,577,896✔
443
  }
444

445
  if (pLimitInfo->remainGroupOffset > 0) {
2,044,771,693✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
68,676✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
24,170✔
448
      blockDataCleanup(pRes);
24,170✔
449
      return TSDB_CODE_SUCCESS;
24,170✔
450
    } else {
451
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
44,506✔
452
      QUERY_CHECK_CODE(code, lino, _end);
44,506✔
453
      pLimitInfo->remainGroupOffset = 0;
44,506✔
454
    }
455
  }
456

457
  if (pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
2,044,747,010✔
458
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
174,478✔
459
    if (pRes->info.rows > remainRows) {
174,478✔
460
      blockDataKeepFirstNRows(pRes, remainRows);
55,570✔
461
    }
462
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
174,478✔
463
  }
464

465
_end:
2,044,573,045✔
466
  if (code != TSDB_CODE_SUCCESS) {
2,044,747,523✔
467
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
468
  }
469
  return code;
2,044,747,010✔
470
}
471

472
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
2,052,345,935✔
473
  int32_t               code = TSDB_CODE_SUCCESS;
2,052,345,935✔
474
  int32_t               lino = 0;
2,052,345,935✔
475
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,052,345,935✔
476
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,052,346,492✔
477
  SSDataBlock*          pRes = pInfo->binfo.pRes;
2,052,346,452✔
478
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
2,052,345,483✔
479

480
  // after filter, if result block turn to null, get next from whole set
481
  while (1) {
482
    doBuildResultDatablockByHash(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf);
2,052,345,551✔
483

484
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
2,052,349,589✔
485
    QUERY_CHECK_CODE(code, lino, _end);
2,052,349,589✔
486

487
    code = doGroupResultSlimit(pRes, pLimitInfo);
2,052,349,589✔
488
    QUERY_CHECK_CODE(code, lino, _end);
2,052,348,700✔
489

490
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
2,052,348,700✔
491
      setOperatorCompleted(pOperator);
30,148,458✔
492
      // clean hash after completed
493
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
30,147,941✔
494
      pInfo->aggSup.pResultRowHashTable = NULL;
30,148,971✔
495
      break;
30,148,458✔
496
    }
497

498
    if (pRes->info.rows > 0) {
2,022,200,242✔
499
      break;
2,022,198,959✔
500
    }
501
  }
502

503
_end:
2,052,347,417✔
504
  if (code != TSDB_CODE_SUCCESS) {
2,052,347,417✔
UNCOV
505
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
506
    T_LONG_JMP(pTaskInfo->env, code);
×
507
  }
508
  return (pRes->info.rows == 0) ? NULL : pRes;
2,052,347,417✔
509
}
510

511
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
2,074,794,716✔
512
  int32_t               code = TSDB_CODE_SUCCESS;
2,074,794,716✔
513
  int32_t               lino = 0;
2,074,794,716✔
514
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,074,794,716✔
515
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,074,796,695✔
516
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
2,074,794,452✔
517
  int32_t               order = pInfo->binfo.inputTsOrder;
2,074,796,035✔
518

519
  QRY_PARAM_CHECK(ppRes);
2,074,793,751✔
520
  if (pOperator->status == OP_EXEC_DONE) {
2,074,795,136✔
521
    return code;
22,433,501✔
522
  }
523

524
  if (pOperator->status == OP_RES_TO_RETURN) {
2,052,354,644✔
525
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
2,022,123,542✔
526
    return code;
2,022,124,988✔
527
  }
528

529
  while (1) {
206,058,455✔
530
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
236,292,808✔
531
    if (pBlock == NULL) {
236,306,944✔
532
      break;
30,222,942✔
533
    }
534

535
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
206,084,002✔
536

537
    // the pDataBlock are always the same one, no need to call this again
538
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
206,096,099✔
539
    QUERY_CHECK_CODE(code, lino, _end);
206,088,882✔
540

541
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
542
    if (pInfo->scalarSup.pExprInfo != NULL) {
206,088,882✔
543
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
41,005,555✔
544
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
41,009,797✔
545
      QUERY_CHECK_CODE(code, lino, _end);
40,998,551✔
546
    }
547

548
    doHashGroupbyAgg(pOperator, pBlock);
206,059,807✔
549
  }
550

551
  pOperator->status = OP_RES_TO_RETURN;
30,222,942✔
552

553
  // initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
554
  if (pGroupResInfo->pRows != NULL) {
30,222,429✔
UNCOV
555
    taosArrayDestroy(pGroupResInfo->pRows);
×
556
  }
557

558
  if (pGroupResInfo->pBuf) {
30,222,942✔
UNCOV
559
    taosMemoryFree(pGroupResInfo->pBuf);
×
UNCOV
560
    pGroupResInfo->pBuf = NULL;
×
561
  }
562

563
  pGroupResInfo->index = 0;
30,222,429✔
564
  pGroupResInfo->iter = 0;
30,222,429✔
565
  pGroupResInfo->dataPos = NULL;
30,221,912✔
566

567
_end:
30,240,101✔
568
  if (code != TSDB_CODE_SUCCESS) {
30,240,101✔
569
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
19,215✔
570
    pTaskInfo->code = code;
19,215✔
571
    T_LONG_JMP(pTaskInfo->env, code);
19,215✔
572
  } else {
573
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
30,220,886✔
574
  }
575

576
  return code;
30,222,451✔
577
}
578

UNCOV
579
static int32_t resetGroupOperState(SOperatorInfo* pOper) {
×
UNCOV
580
  SGroupbyOperatorInfo* pInfo = pOper->info;
×
UNCOV
581
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
UNCOV
582
  SAggPhysiNode* pPhynode = (SAggPhysiNode*)pOper->pPhyNode;
×
UNCOV
583
  resetBasicOperatorState(&pInfo->binfo);
×
584
  pOper->status = OP_NOT_OPENED;
×
585

586
  cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
×
587
    false);
588

589
  cleanupGroupResInfo(&pInfo->groupResInfo);
×
590

591
  qInfo("[group key] len use:%d", pInfo->groupKeyLen);
×
UNCOV
592
  int32_t code = resetAggSup(&pOper->exprSupp, &pInfo->aggSup, pTaskInfo, pPhynode->pAggFuncs, pPhynode->pGroupKeys,
×
UNCOV
593
    pInfo->groupKeyLen + POINTER_BYTES, pTaskInfo->id.str, NULL,
×
594
    &pTaskInfo->storageAPI.functionStore);
595

596
  if (code == 0){
×
597
    code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
598
      &pTaskInfo->storageAPI.functionStore);
599
  }
600

601
  pInfo->isInit = false;
×
602

UNCOV
603
  return code;
×
604
}
605

606
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
30,340,609✔
607
                                SOperatorInfo** pOptrInfo) {
608
  QRY_PARAM_CHECK(pOptrInfo);
30,340,609✔
609

610
  int32_t               code = TSDB_CODE_SUCCESS;
30,344,512✔
611
  int32_t               lino = 0;
30,344,512✔
612
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
30,344,512✔
613
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
30,326,479✔
614
  if (pInfo == NULL || pOperator == NULL) {
30,331,891✔
615
    code = terrno;
443✔
UNCOV
616
    goto _error;
×
617
  }
618
  initOperatorCostInfo(pOperator);
30,331,454✔
619

620
  pOperator->pPhyNode = (SNode*)pAggNode;
30,340,509✔
621
  pOperator->exprSupp.hasWindowOrGroup = true;
30,345,135✔
622
  pOperator->exprSupp.hasWindow = false;
30,340,895✔
623

624
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
30,337,382✔
625
  if (pResBlock == NULL) {
30,345,581✔
UNCOV
626
    code = terrno;
×
UNCOV
627
    goto _error;
×
628
  }
629
  initBasicInfo(&pInfo->binfo, pResBlock);
30,345,581✔
630

631
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
30,343,546✔
632

633
  pInfo->pGroupCols = NULL;
30,344,517✔
634
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
30,340,921✔
635
  QUERY_CHECK_CODE(code, lino, _error);
30,332,214✔
636

637
  int32_t    numOfScalarExpr = 0;
30,332,214✔
638
  SExprInfo* pScalarExprInfo = NULL;
30,333,138✔
639
  if (pAggNode->pExprs != NULL) {
30,335,222✔
640
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
14,816,552✔
641
    QUERY_CHECK_CODE(code, lino, _error);
14,812,953✔
642
  }
643

644
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
30,244,710✔
645
  QUERY_CHECK_CODE(code, lino, _error);
30,248,174✔
646

647
  initResultSizeInfo(&pOperator->resultInfo, 4096);
30,248,174✔
648
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
30,248,171✔
649
  QUERY_CHECK_CODE(code, lino, _error);
30,252,054✔
650

651
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
30,252,054✔
652
  QUERY_CHECK_CODE(code, lino, _error);
30,244,861✔
653

654
  int32_t    num = 0;
30,244,861✔
655
  SExprInfo* pExprInfo = NULL;
30,245,868✔
656

657
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
30,250,958✔
658
  QUERY_CHECK_CODE(code, lino, _error);
30,242,286✔
659

660
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
30,242,286✔
661
                    NULL, &pTaskInfo->storageAPI.functionStore);
662
  QUERY_CHECK_CODE(code, lino, _error);
30,244,713✔
663

664
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
30,242,603✔
665
                            pTaskInfo->pStreamRuntimeInfo);
30,244,713✔
666
  QUERY_CHECK_CODE(code, lino, _error);
30,240,493✔
667

668
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
30,240,493✔
669
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
30,243,911✔
670

671
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
30,251,646✔
672
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
30,250,920✔
673
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
30,240,434✔
674

675
  pInfo->pOperator = pOperator;
30,245,435✔
676

677
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
30,242,440✔
678
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
679
  setOperatorResetStateFn(pOperator, resetGroupOperState);
30,244,013✔
680
  code = appendDownstream(pOperator, &downstream, 1);
30,242,576✔
681
  QUERY_CHECK_CODE(code, lino, _error);
30,239,162✔
682

683
  *pOptrInfo = pOperator;
30,239,162✔
684
  return TSDB_CODE_SUCCESS;
30,239,198✔
685

686
_error:
91,134✔
687
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
91,134✔
688
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
91,134✔
689
  pTaskInfo->code = code;
91,134✔
690
  return code;
91,134✔
691
}
692

UNCOV
693
SSDataBlock* createBlockDataNotLoaded(const SOperatorInfo* pOperator, SSDataBlock* pDataBlock) {
×
UNCOV
694
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
695
  int32_t lino = 0;
×
UNCOV
696
  if (pDataBlock == NULL) {
×
697
    return NULL;
×
698
  }
699

700
  SSDataBlock* pDstBlock = NULL;
×
701
  code = createDataBlock(&pDstBlock);
×
UNCOV
702
  QUERY_CHECK_CODE(code, lino, _end);
×
703

704
  pDstBlock->info = pDataBlock->info;
×
705
  pDstBlock->info.id.blockId = pOperator->resultDataBlockId;
×
706
  pDstBlock->info.capacity = 0;
×
UNCOV
707
  pDstBlock->info.rowSize = 0;
×
708

709
  size_t numOfCols = pOperator->exprSupp.numOfExprs;
×
710
  if (pDataBlock->pBlockAgg) {
×
711
    pDstBlock->pBlockAgg = taosMemoryCalloc(numOfCols, sizeof(SColumnDataAgg));
×
UNCOV
712
    if (pDstBlock->pBlockAgg == NULL) {
×
713
      blockDataDestroy(pDstBlock);
×
714
      return NULL;
×
715
    }
716
    for (int i = 0; i < numOfCols; ++i) {
×
717
      pDstBlock->pBlockAgg[i].colId = -1;
×
718
    }
719
  }
720

721
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
×
UNCOV
722
    SExprInfo*       pExpr = &pOperator->exprSupp.pExprInfo[i];
×
UNCOV
723
    int32_t          slotId = pExpr->base.pParam[0].pCol->slotId;
×
UNCOV
724
    SColumnInfoData* pSrc = taosArrayGet(pDataBlock->pDataBlock, slotId);
×
725
    SColumnInfoData  colInfo = {.hasNull = true, .info = pSrc->info};
×
726
    code = blockDataAppendColInfo(pDstBlock, &colInfo);
×
727
    QUERY_CHECK_CODE(code, lino, _end);
×
728

729
    SColumnInfoData* pDst = taosArrayGet(pDstBlock->pDataBlock, i);
×
730
    if (pDataBlock->pBlockAgg && pDataBlock->pBlockAgg[slotId].colId != -1) {
×
731
      pDstBlock->pBlockAgg[i] = pDataBlock->pBlockAgg[slotId];
×
732
    } else {
733
      code = doEnsureCapacity(pDst, &pDstBlock->info, pDataBlock->info.rows, false);
×
734
      QUERY_CHECK_CODE(code, lino, _end);
×
735

UNCOV
736
      code = colDataAssign(pDst, pSrc, pDataBlock->info.rows, &pDataBlock->info);
×
737
      QUERY_CHECK_CODE(code, lino, _end);
×
738
    }
739
  }
740

741
_end:
×
UNCOV
742
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
743
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
744
    blockDataDestroy(pDstBlock);
×
745
    return NULL;
×
746
  }
747
  return pDstBlock;
×
748
}
749

750
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
19,925,303✔
751
  int32_t                 code = TSDB_CODE_SUCCESS;
19,925,303✔
752
  int32_t                 lino = 0;
19,925,303✔
753
  SPartitionOperatorInfo* pInfo = pOperator->info;
19,925,303✔
754
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
19,932,330✔
755

756
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
757
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
2,147,483,647✔
758
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
2,147,483,647✔
759

760
    SDataGroupInfo* pGroupInfo = NULL;
2,147,483,647✔
761
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
2,147,483,647✔
762
    if (pPage == NULL) {
2,147,483,647✔
UNCOV
763
      T_LONG_JMP(pTaskInfo->env, terrno);
×
764
    }
765

766
    pGroupInfo->numOfRows += 1;
2,147,483,647✔
767

768
    // group id
769
    if (pGroupInfo->groupId == 0) {
2,147,483,647✔
770
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
73,360,610✔
771
    }
772

773
    if (pBlock->info.dataLoad) {
2,147,483,647✔
774
      // number of rows
775
      int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
776

777
      size_t numOfCols = pOperator->exprSupp.numOfExprs;
2,147,483,647✔
778
      for (int32_t i = 0; i < numOfCols; ++i) {
2,147,483,647✔
779
        SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
2,147,483,647✔
780
        int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
2,147,483,647✔
781

782
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
783

784
        int32_t bytes = pColInfoData->info.bytes;
2,147,483,647✔
785
        int32_t startOffset = pInfo->columnOffset[i];
2,147,483,647✔
786

787
        int32_t* columnLen = NULL;
2,147,483,647✔
788
        int32_t  contentLen = 0;
2,147,483,647✔
789

790
        if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
791
          int32_t* offset = (int32_t*)((char*)pPage + startOffset);
2,147,483,647✔
792
          columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
2,147,483,647✔
793
          char* data = (char*)((char*)columnLen + sizeof(int32_t));
2,147,483,647✔
794

795
          if (colDataIsNull_s(pColInfoData, j)) {
2,147,483,647✔
796
            offset[(*rows)] = -1;
2,147,483,647✔
797
            contentLen = 0;
2,147,483,647✔
798
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
799
            offset[*rows] = (*columnLen);
25,056✔
800
            char*   src = colDataGetData(pColInfoData, j);
25,056✔
801
            int32_t dataLen = getJsonValueLen(src);
25,056✔
802

803
            memcpy(data + (*columnLen), src, dataLen);
25,056✔
804
            int32_t v = (data + (*columnLen) + dataLen - (char*)pPage);
25,056✔
805
            QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
25,056✔
806

807
            contentLen = dataLen;
25,056✔
808
          } else {
809
            if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
2,147,483,647✔
810
              offset[*rows] = (*columnLen);
2,356,217✔
UNCOV
811
              char* src = colDataGetData(pColInfoData, j);
×
UNCOV
812
              memcpy(data + (*columnLen), src, blobDataTLen(src));
×
UNCOV
813
              int32_t v = (data + (*columnLen) + blobDataTLen(src) - (char*)pPage);
×
814
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
815

816
              contentLen = blobDataTLen(src);
×
817
            } else {
818
              offset[*rows] = (*columnLen);
2,147,483,647✔
819
              char* src = colDataGetData(pColInfoData, j);
2,147,483,647✔
820
              memcpy(data + (*columnLen), src, varDataTLen(src));
2,147,483,647✔
821
              int32_t v = (data + (*columnLen) + varDataTLen(src) - (char*)pPage);
2,147,483,647✔
822
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
2,147,483,647✔
823

824
              contentLen = varDataTLen(src);
2,147,483,647✔
825
            }
826
          }
827
        } else {
828
          char* bitmap = (char*)pPage + startOffset;
2,147,483,647✔
829
          columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
2,147,483,647✔
830
          char* data = (char*)columnLen + sizeof(int32_t);
2,147,483,647✔
831

832
          bool isNull = colDataIsNull_f(pColInfoData, j);
2,147,483,647✔
833
          if (isNull) {
2,147,483,647✔
834
            colDataSetNull_f(bitmap, (*rows));
2,147,483,647✔
835
          } else {
836
            memcpy(data + (*columnLen), colDataGetData(pColInfoData, j), bytes);
2,147,483,647✔
837
            QUERY_CHECK_CONDITION(((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf)), code,
2,147,483,647✔
838
                                  lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
839
          }
840
          contentLen = bytes;
2,147,483,647✔
841
        }
842

843
        (*columnLen) += contentLen;
2,147,483,647✔
844
      }
845

846
      (*rows) += 1;
2,147,483,647✔
847

848
      setBufPageDirty(pPage, true);
2,147,483,647✔
849
      releaseBufPage(pInfo->pBuf, pPage);
2,147,483,647✔
850
    } else {
UNCOV
851
      SSDataBlock* dataNotLoadBlock = createBlockDataNotLoaded(pOperator, pBlock);
×
UNCOV
852
      if (dataNotLoadBlock == NULL) {
×
UNCOV
853
        T_LONG_JMP(pTaskInfo->env, terrno);
×
854
      }
855
      if (pGroupInfo->blockForNotLoaded == NULL) {
×
856
        pGroupInfo->blockForNotLoaded = taosArrayInit(0, sizeof(SSDataBlock*));
×
857
        QUERY_CHECK_NULL(pGroupInfo->blockForNotLoaded, code, lino, _end, terrno);
×
UNCOV
858
        pGroupInfo->offsetForNotLoaded = 0;
×
859
      }
860
      dataNotLoadBlock->info.id.groupId = pGroupInfo->groupId;
×
861
      dataNotLoadBlock->info.dataLoad = 0;
×
862
      void* tmp = taosArrayPush(pGroupInfo->blockForNotLoaded, &dataNotLoadBlock);
×
UNCOV
863
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
864
      break;
×
865
    }
866
  }
867

868
_end:
19,897,721✔
869
  if (code != TSDB_CODE_SUCCESS) {
19,935,595✔
UNCOV
870
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
871
    T_LONG_JMP(pTaskInfo->env, code);
×
872
  }
873
}
19,935,595✔
874

875
void* getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len) {
2,147,483,647✔
876
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
877
  int32_t         lino = 0;
2,147,483,647✔
878
  SDataGroupInfo* p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
2,147,483,647✔
879

880
  void* pPage = NULL;
2,147,483,647✔
881
  if (p == NULL) {  // it is a new group
2,147,483,647✔
882
    SDataGroupInfo gi = {0};
73,357,552✔
883
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
73,357,552✔
884
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
73,349,128✔
885

886
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
73,349,128✔
887
    if (code == TSDB_CODE_DUP_KEY) {
73,360,078✔
UNCOV
888
      code = TSDB_CODE_SUCCESS;
×
889
    }
890
    QUERY_CHECK_CODE(code, lino, _end);
73,360,078✔
891

892
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
73,360,078✔
893

894
    int32_t pageId = 0;
73,362,485✔
895
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
73,360,228✔
896
    if (pPage == NULL) {
73,357,812✔
UNCOV
897
      return pPage;
×
898
    }
899

900
    void* tmp = taosArrayPush(p->pPageList, &pageId);
73,357,812✔
901
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
73,362,077✔
902

903
    *(int32_t*)pPage = 0;
73,362,077✔
904
  } else {
905
    int32_t* curId = taosArrayGetLast(p->pPageList);
2,147,483,647✔
906
    pPage = getBufPage(pInfo->pBuf, *curId);
2,147,483,647✔
907
    if (pPage == NULL) {
2,147,483,647✔
UNCOV
908
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
UNCOV
909
      return pPage;
×
910
    }
911

912
    int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
913
    if (*rows >= pInfo->rowCapacity) {
2,147,483,647✔
914
      // release buffer
915
      releaseBufPage(pInfo->pBuf, pPage);
250,940,057✔
916

917
      // add a new page for current group
918
      int32_t pageId = 0;
250,940,057✔
919
      pPage = getNewBufPage(pInfo->pBuf, &pageId);
250,940,057✔
920
      if (pPage == NULL) {
250,893,129✔
UNCOV
921
        qError("failed to get new buffer, code:%s", tstrerror(terrno));
×
UNCOV
922
        return NULL;
×
923
      }
924

925
      void* tmp = taosArrayPush(p->pPageList, &pageId);
250,893,129✔
926
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
250,943,769✔
927

928
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
250,943,769✔
929
    }
930
  }
931

932
  *pGroupInfo = p;
2,147,483,647✔
933

934
_end:
2,147,483,647✔
935
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
UNCOV
936
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
937
    return NULL;
×
938
  }
939

940
  return pPage;
2,147,483,647✔
941
}
942

943
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
2,904,931✔
944
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
2,904,931✔
945
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
2,906,794✔
946
  if (!offset) {
2,904,298✔
UNCOV
947
    return NULL;
×
948
  }
949

950
  offset[0] = sizeof(int32_t) +
2,904,298✔
951
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
952

953
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
9,215,238✔
954
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,311,843✔
955

956
    int32_t bytes = pColInfoData->info.bytes;
6,310,697✔
957
    int32_t payloadLen = bytes * rowCapacity;
6,310,839✔
958

959
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
6,310,839✔
960
      // offset segment + content length + payload
961
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
1,119,313✔
962
    } else {
963
      // bitmap + content length + payload
964
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
5,199,718✔
965
    }
966
  }
967

968
  return offset;
2,903,395✔
969
}
970

971
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
2,788,075✔
972
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
2,788,075✔
973
  for (int32_t i = 0; i < size; i++) {
44,262,947✔
974
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
41,475,660✔
975
    if (pGp && pGp->blockForNotLoaded) {
41,476,049✔
UNCOV
976
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
UNCOV
977
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
UNCOV
978
        if (pBlock) blockDataDestroy(*pBlock);
×
979
      }
980
      taosArrayClear(pGp->blockForNotLoaded);
×
981
      pGp->offsetForNotLoaded = 0;
×
982
    }
983
    taosArrayDestroy(pGp->pPageList);
41,475,660✔
984
  }
985
  taosArrayClear(pInfo->sortedGroupArray);
2,787,287✔
986
  clearDiskbasedBuf(pInfo->pBuf);
2,787,681✔
987
}
2,785,334✔
988

989
static int compareDataGroupInfo(const void* group1, const void* group2) {
675,577,789✔
990
  const SDataGroupInfo* pGroupInfo1 = group1;
675,577,789✔
991
  const SDataGroupInfo* pGroupInfo2 = group2;
675,577,789✔
992

993
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
675,577,789✔
UNCOV
994
    return 0;
×
995
  }
996

997
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
675,579,739✔
998
}
999

1000
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
42,176,875✔
1001
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
42,176,875✔
UNCOV
1002
    SSDataBlock** pBlock = taosArrayGet(pGroupInfo->blockForNotLoaded, pGroupInfo->offsetForNotLoaded);
×
UNCOV
1003
    if (!pBlock) {
×
UNCOV
1004
      return NULL;
×
1005
    }
1006
    pGroupInfo->offsetForNotLoaded++;
×
1007
    return *pBlock;
×
1008
  }
1009
  return NULL;
42,176,486✔
1010
}
1011

1012
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
298,163,482✔
1013
  int32_t                 code = TSDB_CODE_SUCCESS;
298,163,482✔
1014
  int32_t                 lino = 0;
298,163,482✔
1015
  SPartitionOperatorInfo* pInfo = pOperator->info;
298,163,482✔
1016
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
298,167,972✔
1017

1018
  if (pInfo->remainRows == 0) {
298,168,521✔
1019
    blockDataCleanup(pInfo->binfo.pRes);
273,022,390✔
1020
    SDataGroupInfo* pGroupInfo =
273,025,179✔
1021
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
273,024,321✔
1022
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
273,025,179✔
1023
      if (pGroupInfo != NULL) {
45,065,631✔
1024
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
42,176,092✔
1025
        if (ret != NULL) return ret;
42,176,092✔
1026
      }
1027
      // try next group data
1028
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
45,065,631✔
1029
        setOperatorCompleted(pOperator);
2,788,464✔
1030
        clearPartitionOperator(pInfo);
2,788,464✔
1031
        return NULL;
2,785,334✔
1032
      }
1033
      ++pInfo->groupIndex;
42,277,162✔
1034

1035
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
42,278,334✔
1036
      if (pGroupInfo == NULL) {
42,278,339✔
UNCOV
1037
        qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
UNCOV
1038
        T_LONG_JMP(pTaskInfo->env, terrno);
×
1039
      }
1040
      pInfo->pageIndex = 0;
42,278,339✔
1041
    }
1042

1043
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
270,235,463✔
1044
    if (pageId == NULL) {
270,234,216✔
UNCOV
1045
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
UNCOV
1046
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1047
    }
1048
    void* page = getBufPage(pInfo->pBuf, *pageId);
270,234,216✔
1049
    if (page == NULL) {
270,228,264✔
1050
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
UNCOV
1051
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1052
    }
1053
    if (*(int32_t*)page == 0) {
270,228,264✔
1054
      releaseBufPage(pInfo->pBuf, page);
×
1055
      SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
×
UNCOV
1056
      if (ret != NULL) return ret;
×
UNCOV
1057
      if (pInfo->groupIndex + 1 < taosArrayGetSize(pInfo->sortedGroupArray)) {
×
1058
        pInfo->groupIndex++;
×
1059
        pInfo->pageIndex = 0;
×
1060
      } else {
1061
        setOperatorCompleted(pOperator);
×
1062
        clearPartitionOperator(pInfo);
×
1063
        return NULL;
×
1064
      }
1065
      return buildPartitionResult(pOperator);
×
1066
    }
1067

1068
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
270,230,219✔
1069
    QUERY_CHECK_CODE(code, lino, _end);
270,232,899✔
1070

1071
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
270,232,899✔
1072
    QUERY_CHECK_CODE(code, lino, _end);
270,236,176✔
1073

1074
    pInfo->pageIndex += 1;
270,236,176✔
1075
    releaseBufPage(pInfo->pBuf, page);
270,234,934✔
1076
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
270,230,968✔
1077
    pInfo->binfo.pRes->info.dataLoad = 1;
270,232,819✔
1078
    pInfo->orderedRows = 0;
270,234,081✔
1079
  } else if (pInfo->pOrderInfoArr == NULL) {
25,147,213✔
UNCOV
1080
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1081
  }
1082

1083
  if (pInfo->pOrderInfoArr) {
295,380,187✔
1084
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
75,205,111✔
1085
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
75,205,111✔
1086
    QUERY_CHECK_CODE(code, lino, _end);
75,205,111✔
1087
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
75,205,111✔
1088
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
75,207,895✔
1089
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
75,207,521✔
1090
  }
1091

1092
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
295,383,998✔
1093
  QUERY_CHECK_CODE(code, lino, _end);
295,378,131✔
1094

1095
_end:
295,378,131✔
1096
  if (code != TSDB_CODE_SUCCESS) {
295,378,131✔
UNCOV
1097
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
1098
    T_LONG_JMP(pTaskInfo->env, code);
×
1099
  }
1100

1101
  return pInfo->binfo.pRes;
295,378,131✔
1102
}
1103

1104
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
298,195,693✔
1105
  if (pOperator->status == OP_EXEC_DONE) {
298,195,693✔
1106
    (*ppRes) = NULL;
12,456✔
1107
    return TSDB_CODE_SUCCESS;
12,456✔
1108
  }
1109

1110
  int32_t                 code = TSDB_CODE_SUCCESS;
298,185,858✔
1111
  int32_t                 lino = 0;
298,185,858✔
1112
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
298,185,858✔
1113
  SPartitionOperatorInfo* pInfo = pOperator->info;
298,186,799✔
1114
  SSDataBlock*            pRes = pInfo->binfo.pRes;
298,187,263✔
1115

1116
  if (pOperator->status == OP_RES_TO_RETURN) {
298,186,435✔
1117
    (*ppRes) = buildPartitionResult(pOperator);
295,280,214✔
1118
    return code;
295,280,107✔
1119
  }
1120

1121
  while (1) {
19,935,206✔
1122
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
22,841,304✔
1123
    if (pBlock == NULL) {
22,813,841✔
1124
      break;
2,889,928✔
1125
    }
1126

1127
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
19,923,913✔
1128
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1129
    if (pInfo->scalarSup.pExprInfo != NULL) {
19,936,451✔
1130
      code =
1131
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
102,821✔
1132
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
102,821✔
1133
      QUERY_CHECK_CODE(code, lino, _end);
102,821✔
1134
    }
1135

1136
    terrno = TSDB_CODE_SUCCESS;
19,930,155✔
1137
    doHashPartition(pOperator, pBlock);
19,930,806✔
1138
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
19,935,131✔
UNCOV
1139
      code = terrno;
×
UNCOV
1140
      QUERY_CHECK_CODE(code, lino, _end);
×
1141
    }
1142
  }
1143

1144
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
2,889,928✔
1145
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
2,889,534✔
1146

1147
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
2,889,534✔
1148
  while (pGroupIter != NULL) {
76,252,788✔
1149
    SDataGroupInfo* pGroupInfo = pGroupIter;
73,363,638✔
1150
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
73,364,037✔
1151
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
73,364,037✔
1152
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
73,364,037✔
1153
  }
1154

1155
  taosArraySort(groupArray, compareDataGroupInfo);
2,889,150✔
1156
  pInfo->sortedGroupArray = groupArray;
2,889,145✔
1157
  pInfo->groupIndex = -1;
2,889,534✔
1158
  taosHashClear(pInfo->pGroupSet);
2,889,145✔
1159

1160
  pOperator->status = OP_RES_TO_RETURN;
2,889,534✔
1161
  code = blockDataEnsureCapacity(pRes, 4096);
2,889,534✔
1162
  QUERY_CHECK_CODE(code, lino, _end);
2,888,761✔
1163

1164
_end:
2,888,761✔
1165
  if (code != TSDB_CODE_SUCCESS) {
2,888,761✔
UNCOV
1166
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
1167
    pTaskInfo->code = code;
×
UNCOV
1168
    T_LONG_JMP(pTaskInfo->env, code);
×
1169
  }
1170

1171
  (*ppRes) = buildPartitionResult(pOperator);
2,888,761✔
1172
  return code;
2,887,973✔
1173
}
1174

1175
static void destroyPartitionOperatorInfo(void* param) {
2,928,358✔
1176
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
2,928,358✔
1177
  cleanupBasicInfo(&pInfo->binfo);
2,928,358✔
1178
  taosArrayDestroy(pInfo->pGroupCols);
2,927,964✔
1179

1180
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
6,662,370✔
1181
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
3,735,568✔
1182
    taosMemoryFree(key.pData);
3,734,391✔
1183
  }
1184

1185
  taosArrayDestroy(pInfo->pGroupColVals);
2,926,802✔
1186
  taosMemoryFree(pInfo->keyBuf);
2,926,797✔
1187

1188
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
2,926,408✔
1189
  for (int32_t i = 0; i < size; i++) {
34,816,735✔
1190
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
31,888,766✔
1191
    if (pGp) {
31,888,766✔
1192
      taosArrayDestroy(pGp->pPageList);
31,888,766✔
1193
    }
1194
  }
1195
  taosArrayDestroy(pInfo->sortedGroupArray);
2,927,969✔
1196

1197
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
2,927,580✔
1198
  while (pGroupIter != NULL) {
2,927,969✔
UNCOV
1199
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
UNCOV
1200
    taosArrayDestroy(pGroupInfo->pPageList);
×
UNCOV
1201
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1202
  }
1203

1204
  taosHashCleanup(pInfo->pGroupSet);
2,927,969✔
1205
  taosMemoryFree(pInfo->columnOffset);
2,926,403✔
1206

1207
  cleanupExprSupp(&pInfo->scalarSup);
2,927,186✔
1208
  destroyDiskbasedBuf(pInfo->pBuf);
2,928,358✔
1209
  taosArrayDestroy(pInfo->pOrderInfoArr);
2,927,580✔
1210
  taosMemoryFreeClear(param);
2,927,580✔
1211
}
2,927,186✔
1212

UNCOV
1213
static int32_t resetPartitionOperState(SOperatorInfo* pOper) {
×
UNCOV
1214
  SPartitionOperatorInfo* pInfo = pOper->info;
×
UNCOV
1215
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
UNCOV
1216
  SPartitionPhysiNode* pPhynode = (SPartitionPhysiNode*)pOper->pPhyNode;
×
UNCOV
1217
  resetBasicOperatorState(&pInfo->binfo);
×
1218

UNCOV
1219
  int32_t code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
1220
    &pTaskInfo->storageAPI.functionStore);
1221

UNCOV
1222
  clearPartitionOperator(pInfo);
×
1223

1224
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
×
1225
  while (pGroupIter != NULL) {
×
1226
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1227
    taosArrayDestroy(pGroupInfo->pPageList);
×
UNCOV
1228
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1229
  }
UNCOV
1230
  taosHashClear(pInfo->pGroupSet);
×
1231

1232
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
×
UNCOV
1233
  for (int32_t i = 0; i < size; i++) {
×
1234
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
×
1235
    if (pGp) {
×
1236
      taosArrayDestroy(pGp->pPageList);
×
1237
    }
1238
  }
UNCOV
1239
  taosArrayDestroy(pInfo->sortedGroupArray);
×
1240
  pInfo->sortedGroupArray = NULL;
×
1241

1242
  pInfo->groupIndex = 0;
×
1243
  pInfo->pageIndex = 0;
×
1244
  pInfo->remainRows = 0;
×
1245
  pInfo->orderedRows = 0;
×
1246
  return 0;
×
1247
}
1248

1249
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
2,924,916✔
1250
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1251
  QRY_PARAM_CHECK(pOptrInfo);
2,924,916✔
1252

1253
  int32_t                 code = TSDB_CODE_SUCCESS;
2,925,707✔
1254
  int32_t                 lino = 0;
2,925,707✔
1255
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
2,925,707✔
1256
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
2,914,571✔
1257
  if (pInfo == NULL || pOperator == NULL) {
2,915,249✔
UNCOV
1258
    pTaskInfo->code = code = terrno;
×
UNCOV
1259
    goto _error;
×
1260
  }
1261
  initOperatorCostInfo(pOperator);
2,915,763✔
1262

1263
  pOperator->pPhyNode = pPartNode;
2,925,732✔
1264
  int32_t    numOfCols = 0;
2,924,789✔
1265
  SExprInfo* pExprInfo = NULL;
2,924,789✔
1266
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
2,921,787✔
1267
  QUERY_CHECK_CODE(code, lino, _error);
2,926,080✔
1268
  pOperator->exprSupp.numOfExprs = numOfCols;
2,926,080✔
1269
  pOperator->exprSupp.pExprInfo = pExprInfo;
2,926,075✔
1270

1271
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
2,926,395✔
1272

1273
  if (pPartNode->needBlockOutputTsOrder) {
2,925,707✔
1274
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
265,561✔
1275
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
265,012✔
1276
    if (!pInfo->pOrderInfoArr) {
265,561✔
UNCOV
1277
      pTaskInfo->code = terrno;
×
UNCOV
1278
      goto _error;
×
1279
    }
1280

1281
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
265,012✔
1282
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
265,561✔
1283
  }
1284

1285
  if (pPartNode->pExprs != NULL) {
2,926,243✔
1286
    int32_t    num = 0;
82,324✔
1287
    SExprInfo* pExprInfo1 = NULL;
82,324✔
1288
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
81,775✔
1289
    QUERY_CHECK_CODE(code, lino, _error);
81,922✔
1290

1291
    code = initExprSupp(&pInfo->scalarSup, pExprInfo1, num, &pTaskInfo->storageAPI.functionStore);
62,707✔
1292
    QUERY_CHECK_CODE(code, lino, _error);
63,109✔
1293
  }
1294

1295
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
2,906,644✔
1296
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
2,906,639✔
1297
  if (pInfo->pGroupSet == NULL) {
2,908,360✔
UNCOV
1298
    goto _error;
×
1299
  }
1300

1301
  uint32_t defaultPgsz = 0;
2,907,966✔
1302
  int64_t  defaultBufsz = 0;
2,907,966✔
1303

1304
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
2,907,577✔
1305
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
2,908,360✔
1306
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
2,908,360✔
1307
  if (code != TSDB_CODE_SUCCESS) {
2,908,355✔
UNCOV
1308
    goto _error;
×
1309
  }
1310

1311
  if (!osTempSpaceAvailable()) {
2,908,355✔
UNCOV
1312
    terrno = TSDB_CODE_NO_DISKSPACE;
×
UNCOV
1313
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
×
UNCOV
1314
    goto _error;
×
1315
  }
1316

1317
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
2,905,068✔
1318
  if (code != TSDB_CODE_SUCCESS) {
2,904,308✔
UNCOV
1319
    goto _error;
×
1320
  }
1321

1322
  pInfo->rowCapacity =
2,902,583✔
1323
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
2,902,450✔
1324
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
2,904,308✔
1325
  if (pInfo->rowCapacity < 0) {
2,903,363✔
UNCOV
1326
    code = terrno;
×
UNCOV
1327
    goto _error;
×
1328
  }
1329

1330
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
2,900,803✔
1331
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
2,907,430✔
1332

1333
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
2,900,139✔
1334
  if (code != TSDB_CODE_SUCCESS) {
2,904,013✔
1335
    goto _error;
×
1336
  }
1337

1338
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
2,904,013✔
1339
                  pTaskInfo);
1340

1341
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
2,905,864✔
1342
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1343

1344
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
2,900,281✔
1345
  code = appendDownstream(pOperator, &downstream, 1);
2,905,208✔
1346
  if (code != TSDB_CODE_SUCCESS) {
2,898,652✔
UNCOV
1347
    goto _error;
×
1348
  }
1349

1350
  *pOptrInfo = pOperator;
2,898,652✔
1351
  return TSDB_CODE_SUCCESS;
2,899,966✔
1352

1353
_error:
19,215✔
1354
  if (pInfo != NULL) {
19,215✔
1355
    destroyPartitionOperatorInfo(pInfo);
19,215✔
1356
  }
1357
  pTaskInfo->code = code;
19,215✔
1358
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
19,215✔
1359
  TAOS_RETURN(code);
19,215✔
1360
}
1361

1362
int32_t setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
2,147,483,647✔
1363
                                int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
1364
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
1365
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
2,147,483,647✔
1366
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
2,147,483,647✔
1367

1368
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
2,147,483,647✔
1369
                                                  false, pAggSup, false);
1370
  if (pResultRow == NULL || pTaskInfo->code != 0) {
2,147,483,647✔
UNCOV
1371
    return pTaskInfo->code;
×
1372
  }
1373

1374
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
2,147,483,647✔
1375
}
1376

UNCOV
1377
void freePartItem(void* ptr) {
×
UNCOV
1378
  SPartitionDataInfo* pPart = (SPartitionDataInfo*)ptr;
×
UNCOV
1379
  taosArrayDestroy(pPart->rowIds);
×
1380
}
×
1381

1382
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
30,340,378✔
1383
  int32_t code = TSDB_CODE_SUCCESS;
30,340,378✔
1384
  int32_t lino = 0;
30,340,378✔
1385
  size_t  numOfCols = LIST_LENGTH(pNodeList);
30,340,378✔
1386
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
30,342,480✔
1387
  if (pList == NULL) {
30,335,325✔
1388
    code = terrno;
×
1389
    (*pArrayRes) = NULL;
×
1390
    QUERY_CHECK_CODE(code, lino, _end);
5,152✔
1391
  }
1392

1393
  for (int32_t i = 0; i < numOfCols; ++i) {
69,978,336✔
1394
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
39,642,679✔
1395
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
39,643,779✔
1396

1397
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
39,643,779✔
1398
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
39,648,505✔
1399

1400
      SColumn c = extractColumnFromColumnNode(pColNode);
39,647,237✔
1401
      void*   tmp = taosArrayPush(pList, &c);
39,646,498✔
1402
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
39,646,498✔
UNCOV
1403
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
×
UNCOV
1404
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
×
UNCOV
1405
      SColumn     c = {0};
×
UNCOV
1406
      c.slotId = pNode->slotId;
×
UNCOV
1407
      c.colId = pNode->slotId;
×
UNCOV
1408
      c.type = pValNode->node.type;
×
UNCOV
1409
      c.bytes = pValNode->node.resType.bytes;
×
UNCOV
1410
      c.scale = pValNode->node.resType.scale;
×
UNCOV
1411
      c.precision = pValNode->node.resType.precision;
×
1412

1413
      void* tmp = taosArrayPush(pList, &c);
×
1414
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1415
    }
1416
  }
1417

1418
  (*pArrayRes) = pList;
30,335,657✔
1419

1420
_end:
30,343,239✔
1421
  if (code != TSDB_CODE_SUCCESS) {
30,343,239✔
1422
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1423
  }
1424
  return code;
30,339,664✔
1425
}
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