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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

76.83
/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
static int32_t  getPartitionPageRowCapacity(const SSDataBlock* pBlock, size_t pageSize, int32_t extraSize);
75

76
static void freeGroupKey(void* param) {
50,304,698✔
77
  SGroupKeys* pKey = (SGroupKeys*)param;
50,304,698✔
78
  taosMemoryFree(pKey->pData);
50,304,698✔
79
}
50,304,115✔
80

81
static void destroyGroupOperatorInfo(void* param) {
34,694,857✔
82
  if (param == NULL) {
34,694,857✔
83
    return;
×
84
  }
85
  SGroupbyOperatorInfo* pInfo = (SGroupbyOperatorInfo*)param;
34,694,857✔
86

87
  cleanupBasicInfo(&pInfo->binfo);
34,694,857✔
88
  taosMemoryFreeClear(pInfo->keyBuf);
34,695,610✔
89
  taosArrayDestroy(pInfo->pGroupCols);
34,695,926✔
90
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
34,694,897✔
91
  cleanupExprSupp(&pInfo->scalarSup);
34,693,116✔
92

93
  if (pInfo->pOperator != NULL) {
34,695,249✔
94
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
34,592,121✔
95
                      false);
96
    pInfo->pOperator = NULL;
34,593,234✔
97
  }
98

99
  cleanupGroupResInfo(&pInfo->groupResInfo);
34,696,380✔
100
  cleanupAggSup(&pInfo->aggSup);
34,692,497✔
101
  taosMemoryFreeClear(param);
34,693,337✔
102
}
103

104
static int32_t getPartitionPageRowCapacity(const SSDataBlock* pBlock, size_t pageSize, int32_t extraSize) {
3,285,971✔
105
  size_t  numOfCols = taosArrayGetSize(pBlock->pDataBlock);
3,285,971✔
106
  int32_t payloadSize = pageSize - extraSize;
3,291,051✔
107
  int32_t payloadRowSize = 0;
3,291,051✔
108
  int32_t numVarCols = 0;
3,291,051✔
109
  int32_t numFixCols = 0;
3,291,051✔
110

111
  for (int32_t i = 0; i < numOfCols; ++i) {
13,716,169✔
112
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);
10,429,778✔
113
    if (pCol == NULL) {
10,420,797✔
114
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
115
      return -1;
×
116
    }
117

118
    payloadRowSize += blockDataGetPagedColumnReservedBytes(pCol);
10,420,797✔
119
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
10,424,098✔
120
      ++numVarCols;
1,391,202✔
121
    } else {
122
      ++numFixCols;
9,033,916✔
123
    }
124
  }
125

126
  int32_t nRows = payloadSize / payloadRowSize;
3,286,391✔
127
  if (nRows < 1) {
3,286,391✔
128
    uError("rows %d in page is too small, payloadSize:%d, rowSize:%d", nRows, payloadSize, payloadRowSize);
×
129
    terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
130
    return -1;
×
131
  }
132

133
  int32_t result = -1;
3,286,391✔
134
  int32_t start = 1;
3,286,391✔
135
  int32_t end = nRows;
3,286,391✔
136
  while (start <= end) {
28,290,061✔
137
    int32_t mid = start + (end - start) / 2;
25,003,670✔
138
    int32_t midSize = payloadRowSize * mid + numVarCols * sizeof(int32_t) * mid + numFixCols * BitmapLen(mid);
25,003,670✔
139
    if (midSize > payloadSize) {
25,003,670✔
140
      result = mid;
5,017,350✔
141
      end = mid - 1;
5,017,350✔
142
    } else {
143
      start = mid + 1;
19,986,320✔
144
    }
145
  }
146

147
  return (result != -1) ? result - 1 : nRows;
3,286,391✔
148
}
149

150
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
37,871,324✔
151
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
37,871,324✔
152
  if ((*pGroupColVals) == NULL) {
37,860,629✔
153
    return terrno;
×
154
  }
155

156
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
37,866,835✔
157
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
92,349,037✔
158
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
54,475,995✔
159
    if (!pCol) {
54,480,319✔
160
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
161
      return terrno;
×
162
    }
163
    (*keyLen) += pCol->bytes;  // actual data + null_flag
54,480,319✔
164

165
    SGroupKeys key = {0};
54,475,424✔
166
    key.bytes = pCol->bytes;
54,478,941✔
167
    key.type = pCol->type;
54,471,848✔
168
    key.isNull = false;
54,470,823✔
169
    key.pData = taosMemoryCalloc(1, pCol->bytes);
54,470,823✔
170
    if (key.pData == NULL) {
54,474,406✔
171
      return terrno;
×
172
    }
173

174
    void* tmp = taosArrayPush((*pGroupColVals), &key);
54,474,406✔
175
    if (!tmp) {
54,485,393✔
176
      return terrno;
×
177
    }
178
  }
179

180
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
37,873,042✔
181
  (*keyLen) += nullFlagSize;
37,873,042✔
182

183
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
37,871,021✔
184
  if ((*keyBuf) == NULL) {
37,863,325✔
185
    return terrno;
×
186
  }
187

188
  return TSDB_CODE_SUCCESS;
37,867,188✔
189
}
190

191
static bool groupKeyCompare(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex,
2,147,483,647✔
192
                            int32_t numOfGroupCols) {
193
  SColumnDataAgg* pColAgg = NULL;
2,147,483,647✔
194
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
195
    SColumn*         pCol = taosArrayGet(pGroupCols, i);
2,147,483,647✔
196
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
2,147,483,647✔
197
    if (pBlock->pBlockAgg != NULL) {
2,147,483,647✔
198
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
199
    }
200

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

203
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
204
    if (pkey->isNull && isNull) {
2,147,483,647✔
205
      continue;
2,147,483,647✔
206
    }
207

208
    if (isNull || pkey->isNull) {
2,147,483,647✔
209
      return false;
2,147,483,647✔
210
    }
211

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

214
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
215
      int32_t dataLen = getJsonValueLen(val);
7,658✔
216

217
      if (memcmp(pkey->pData, val, dataLen) == 0) {
7,658✔
218
        continue;
1,094✔
219
      } else {
220
        return false;
6,564✔
221
      }
222
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
223
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
224
        int32_t len = blobDataLen(val);
255✔
225
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
226
          continue;
×
227
        } else {
228
          return false;
×
229
        }
230
      } else {
231
        int32_t len = varDataLen(val);
2,147,483,647✔
232
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
2,147,483,647✔
233
          continue;
2,147,483,647✔
234
        } else {
235
          return false;
1,288,917,319✔
236
        }
237
      }
238
    } else {
239
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
2,147,483,647✔
240
        return false;
2,147,483,647✔
241
      }
242
    }
243
  }
244

245
  return true;
2,147,483,647✔
246
}
247

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

251
  size_t numOfGroupCols = taosArrayGetSize(pGroupCols);
2,147,483,647✔
252

253
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
254
    SColumn*         pCol = (SColumn*)taosArrayGet(pGroupCols, i);
2,147,483,647✔
255
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
2,147,483,647✔
256

257
    // valid range check. todo: return error code.
258
    if (pCol->slotId > taosArrayGetSize(pBlock->pDataBlock)) {
2,147,483,647✔
259
      continue;
×
260
    }
261

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

266
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
267
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
2,147,483,647✔
268
      pkey->isNull = true;
2,147,483,647✔
269
    } else {
270
      pkey->isNull = false;
2,147,483,647✔
271
      char* val = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
272
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
273
        // if (tTagIsJson(val)) {
274
        //   terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
275
        //   return;
276
        // }
277
        int32_t dataLen = getJsonValueLen(val);
45,782✔
278
        memcpy(pkey->pData, val, dataLen);
46,226✔
279
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
280
        if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
281
          memcpy(pkey->pData, val, blobDataTLen(val));
1,784,387✔
282
        } else {
283
          memcpy(pkey->pData, val, varDataTLen(val));
2,147,483,647✔
284
        }
285
      } else {
286
        memcpy(pkey->pData, val, pkey->bytes);
2,147,483,647✔
287
      }
288
    }
289
  }
290
}
2,147,483,647✔
291

292
static int32_t buildGroupKeys(void* pKey, const SArray* pGroupColVals) {
2,147,483,647✔
293
  size_t numOfGroupCols = taosArrayGetSize(pGroupColVals);
2,147,483,647✔
294

295
  char* isNull = (char*)pKey;
2,147,483,647✔
296
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
2,147,483,647✔
297
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
298
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
299
    if (pkey->isNull) {
2,147,483,647✔
300
      isNull[i] = 1;
2,147,483,647✔
301
      continue;
2,147,483,647✔
302
    }
303

304
    isNull[i] = 0;
2,147,483,647✔
305
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
306
      int32_t dataLen = getJsonValueLen(pkey->pData);
46,226✔
307
      memcpy(pStart, (pkey->pData), dataLen);
46,226✔
308
      pStart += dataLen;
46,226✔
309
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
310
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
311
        blobDataCopy(pStart, pkey->pData);
21✔
312
        pStart += blobDataTLen(pkey->pData);
×
313
      } else {
314
        varDataCopy(pStart, pkey->pData);
2,147,483,647✔
315
        pStart += varDataTLen(pkey->pData);
2,147,483,647✔
316
      }
317
    } else {
318
      memcpy(pStart, pkey->pData, pkey->bytes);
2,147,483,647✔
319
      pStart += pkey->bytes;
2,147,483,647✔
320
    }
321
  }
322

323
  return (int32_t)(pStart - (char*)pKey);
2,147,483,647✔
324
}
325

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

332
      SColumnInfoData* pColInfoData = pCtx[i].input.pData[0];
2,147,483,647✔
333
      // todo OPT all/all not NULL
334
      if (!colDataIsNull(pColInfoData, totalRows, rowIndex, NULL)) {
2,147,483,647✔
335
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
2,147,483,647✔
336
        char* data = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
337

338
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
339
          int32_t dataLen = getJsonValueLen(data);
17,522✔
340
          memcpy(dest, data, dataLen);
17,522✔
341
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
342
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,785,917,102✔
343
            blobDataCopy(dest, data);
112✔
344
          } else {
345
            varDataCopy(dest, data);
1,785,574,071✔
346
          }
347
        } else {
348
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
349
        }
350
      } else {  // it is a NULL value
351
        pEntryInfo->isNullRes = 1;
2,147,483,647✔
352
      }
353

354
      pEntryInfo->numOfRes = 1;
2,147,483,647✔
355
    }
356
  }
357
}
2,147,483,647✔
358

359
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
273,114,267✔
360
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
273,114,267✔
361
  SGroupbyOperatorInfo* pInfo = pOperator->info;
273,119,239✔
362

363
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
273,118,662✔
364
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
273,120,295✔
365
  //  if (type == TSDB_DATA_TYPE_FLOAT || type == TSDB_DATA_TYPE_DOUBLE) {
366
  //  qError("QInfo:0x%" PRIx64 ", group by not supported on double/float columns, abort", GET_TASKID(pRuntimeEnv));
367
  //    return;
368
  //  }
369

370
  int32_t len = 0;
273,116,686✔
371
  terrno = TSDB_CODE_SUCCESS;
273,116,686✔
372

373
  int32_t num = 0;
273,116,522✔
374
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
375
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
376
    if (!pInfo->isInit) {
2,147,483,647✔
377
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
26,024,045✔
378
      pInfo->isInit = true;
26,028,534✔
379
      num++;
26,027,432✔
380
      continue;
26,027,432✔
381
    }
382

383
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
2,147,483,647✔
384
    if (equal) {
2,147,483,647✔
385
      num++;
2,147,483,647✔
386
      continue;
2,147,483,647✔
387
    }
388

389
    // The first row of a new block does not belongs to the previous existed group
390
    if (j == 0) {
2,147,483,647✔
391
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
240,469,260✔
392
      num = 1;
240,468,671✔
393
      continue;
240,468,671✔
394
    }
395

396
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
2,147,483,647✔
397
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
2,147,483,647✔
398
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
399
    if (ret != TSDB_CODE_SUCCESS) {  // null data, too many state code
2,147,483,647✔
400
      T_LONG_JMP(pTaskInfo->env, ret);
×
401
    }
402

403
    int32_t rowIndex = j - num;
2,147,483,647✔
404
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
2,147,483,647✔
405
                                          pOperator->exprSupp.numOfExprs);
406
    if (ret != TSDB_CODE_SUCCESS) {
2,147,483,647✔
407
      T_LONG_JMP(pTaskInfo->env, ret);
×
408
    }
409

410
    // assign the group keys or user input constant values if required
411
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
2,147,483,647✔
412
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
2,147,483,647✔
413
    num = 1;
2,147,483,647✔
414
  }
415

416
  // The data of the last group is processed here, and if there is only one group, it is also processed here.
417
  if (num > 0) {
273,115,020✔
418
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
273,118,012✔
419
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
273,114,050✔
420
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
421
    if (ret != TSDB_CODE_SUCCESS) {
273,112,956✔
422
      T_LONG_JMP(pTaskInfo->env, ret);
×
423
    }
424

425
    int32_t rowIndex = pBlock->info.rows - num;
273,112,956✔
426
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
273,112,358✔
427
                                          pOperator->exprSupp.numOfExprs);
428
    if (ret != TSDB_CODE_SUCCESS) {
273,102,996✔
429
      T_LONG_JMP(pTaskInfo->env, ret);
×
430
    }
431
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
273,102,996✔
432
  }
433
}
273,105,836✔
434

435
bool hasRemainResultByHash(SOperatorInfo* pOperator) {
2,147,483,647✔
436
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,147,483,647✔
437
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,147,483,647✔
438
  return pInfo->groupResInfo.index < tSimpleHashGetSize(pHashmap);
2,147,483,647✔
439
}
440

441
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
2,147,483,647✔
442
                                  SDiskbasedBuf* pBuf) {
443
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,147,483,647✔
444
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,147,483,647✔
445
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
446

447
  SSDataBlock* pBlock = pInfo->binfo.pRes;
2,147,483,647✔
448

449
  // set output datablock version
450
  pBlock->info.version = pTaskInfo->version;
2,147,483,647✔
451

452
  blockDataCleanup(pBlock);
2,147,483,647✔
453
  if (!hasRemainResultByHash(pOperator)) {
2,147,483,647✔
454
    return;
8,520,313✔
455
  }
456

457
  pBlock->info.id.groupId = 0;
2,147,483,647✔
458
  pBlock->info.id.baseGId = 0;
2,147,483,647✔
459
  if (!pInfo->binfo.mergeResultBlock) {
2,147,483,647✔
460
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
2,147,483,647✔
461
                             pHashmap, pOperator->resultInfo.threshold, false);
462
  } else {
463
    while (hasRemainResultByHash(pOperator)) {
38,905,934✔
464
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
19,461,867✔
465
                               pHashmap, pOperator->resultInfo.threshold, true);
466
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
19,461,867✔
467
        break;
17,800✔
468
      }
469
      pBlock->info.id.groupId = 0;
19,444,067✔
470
      pBlock->info.id.baseGId = 0;
19,444,067✔
471
    }
472

473
    // clear the group id info in SSDataBlock, since the client does not need it
474
    pBlock->info.id.groupId = 0;
19,461,867✔
475
    pBlock->info.id.baseGId = 0;
19,461,867✔
476
  }
477
}
478

479
static bool slimitReached(SLimitInfo* pLimitInfo) {
2,147,483,647✔
480
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
2,147,483,647✔
481
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
78,185✔
482
    return true;  // limit reached, stop processing further rows
25,510✔
483
  }
484
  return false;
2,147,483,647✔
485
}
486

487
static int32_t doGroupResultSlimit(SSDataBlock* pRes, SLimitInfo* pLimitInfo) {
2,147,483,647✔
488
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
489
  int32_t lino = 0;
2,147,483,647✔
490

491
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
2,147,483,647✔
492
    return TSDB_CODE_SUCCESS;
8,641,328✔
493
  }
494

495
  if (pLimitInfo->remainGroupOffset > 0) {
2,147,483,647✔
496
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
88,748✔
497
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
28,433✔
498
      blockDataCleanup(pRes);
28,433✔
499
      return TSDB_CODE_SUCCESS;
28,433✔
500
    } else {
501
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
60,315✔
502
      QUERY_CHECK_CODE(code, lino, _end);
60,315✔
503
      pLimitInfo->remainGroupOffset = 0;
60,315✔
504
    }
505
  }
506

507
  if (pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
2,147,483,647✔
508
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
225,513✔
509
    if (pRes->info.rows > remainRows) {
225,513✔
510
      blockDataKeepFirstNRows(pRes, remainRows);
89,470✔
511
    }
512
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
225,513✔
513
  }
514

515
_end:
2,147,483,647✔
516
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
517
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
518
  }
519
  return code;
2,147,483,647✔
520
}
521

522
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
2,147,483,647✔
523
  int32_t               code = TSDB_CODE_SUCCESS;
2,147,483,647✔
524
  int32_t               lino = 0;
2,147,483,647✔
525
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
526
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,147,483,647✔
527
  SSDataBlock*          pRes = pInfo->binfo.pRes;
2,147,483,647✔
528
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
2,147,483,647✔
529

530
  // after filter, if result block turn to null, get next from whole set
531
  while (1) {
532
    doBuildResultDatablockByHash(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf);
2,147,483,647✔
533

534
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
2,147,483,647✔
535
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
536

537
    code = doGroupResultSlimit(pRes, pLimitInfo);
2,147,483,647✔
538
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
539

540
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
2,147,483,647✔
541
      setOperatorCompleted(pOperator);
34,474,395✔
542
      // clean hash after completed
543
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
34,474,395✔
544
      pInfo->aggSup.pResultRowHashTable = NULL;
34,474,381✔
545
      break;
34,468,632✔
546
    }
547

548
    if (pRes->info.rows > 0) {
2,147,483,647✔
549
      break;
2,147,483,647✔
550
    }
551
  }
552

553
_end:
2,147,483,647✔
554
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
555
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
556
    T_LONG_JMP(pTaskInfo->env, code);
×
557
  }
558
  return (pRes->info.rows == 0) ? NULL : pRes;
2,147,483,647✔
559
}
560

561
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
2,147,483,647✔
562
  int32_t               code = TSDB_CODE_SUCCESS;
2,147,483,647✔
563
  int32_t               lino = 0;
2,147,483,647✔
564
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
565
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,147,483,647✔
566
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
2,147,483,647✔
567
  int32_t               order = pInfo->binfo.inputTsOrder;
2,147,483,647✔
568

569
  QRY_PARAM_CHECK(ppRes);
2,147,483,647✔
570
  if (pOperator->status == OP_EXEC_DONE) {
2,147,483,647✔
571
    return code;
25,678,888✔
572
  }
573

574
  if (pOperator->status == OP_RES_TO_RETURN) {
2,147,483,647✔
575
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
2,147,483,647✔
576
    return code;
2,147,483,647✔
577
  }
578

579
  if (pOperator->exprSupp.pFilterInfo != NULL) {
34,576,831✔
580
    filterSetExecContext(pOperator->exprSupp.pFilterInfo, pOperator->pTaskInfo, isTaskKilled);
1,167,784✔
581
  }
582

583
  while (1) {
273,107,944✔
584
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
307,684,775✔
585
    if (pBlock == NULL) {
307,693,216✔
586
      break;
34,557,423✔
587
    }
588

589
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
273,135,793✔
590

591
    // the pDataBlock are always the same one, no need to call this again
592
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
273,132,271✔
593
    QUERY_CHECK_CODE(code, lino, _end);
273,138,687✔
594

595
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
596
    if (pInfo->scalarSup.pExprInfo != NULL) {
273,138,687✔
597
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
96,099,796✔
598
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
48,054,906✔
599
      QUERY_CHECK_CODE(code, lino, _end);
48,050,565✔
600
    }
601

602
    doHashGroupbyAgg(pOperator, pBlock);
273,115,787✔
603
  }
604

605
  pOperator->status = OP_RES_TO_RETURN;
34,557,423✔
606

607
  // initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
608
  if (pGroupResInfo->pRows != NULL) {
34,557,423✔
609
    taosArrayDestroy(pGroupResInfo->pRows);
×
610
  }
611

612
  if (pGroupResInfo->pBuf) {
34,557,423✔
613
    taosMemoryFree(pGroupResInfo->pBuf);
×
614
    pGroupResInfo->pBuf = NULL;
×
615
  }
616

617
  pGroupResInfo->index = 0;
34,557,423✔
618
  pGroupResInfo->iter = 0;
34,557,423✔
619
  pGroupResInfo->dataPos = NULL;
34,557,423✔
620

621
_end:
34,577,031✔
622
  if (code != TSDB_CODE_SUCCESS) {
34,577,031✔
623
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
20,774✔
624
    pTaskInfo->code = code;
20,774✔
625
    T_LONG_JMP(pTaskInfo->env, code);
20,774✔
626
  } else {
627
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
34,556,257✔
628
  }
629

630
  return code;
34,557,409✔
631
}
632

633
static int32_t resetGroupOperState(SOperatorInfo* pOper) {
×
634
  SGroupbyOperatorInfo* pInfo = pOper->info;
×
635
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
636
  SAggPhysiNode* pPhynode = (SAggPhysiNode*)pOper->pPhyNode;
×
637
  resetBasicOperatorState(&pInfo->binfo);
×
638
  pOper->status = OP_NOT_OPENED;
×
639

640
  cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
×
641
    false);
642

643
  cleanupGroupResInfo(&pInfo->groupResInfo);
×
644

645
  qInfo("[group key] len use:%d", pInfo->groupKeyLen);
×
646
  int32_t code = resetAggSup(&pOper->exprSupp, &pInfo->aggSup, pTaskInfo, pPhynode->pAggFuncs, pPhynode->pGroupKeys,
×
647
    pInfo->groupKeyLen + POINTER_BYTES, pTaskInfo->id.str, NULL,
×
648
    &pTaskInfo->storageAPI.functionStore);
649

650
  if (code == 0){
×
651
    code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
652
      &pTaskInfo->storageAPI.functionStore);
653
  }
654

655
  pInfo->isInit = false;
×
656

657
  return code;
×
658
}
659

660
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
34,687,297✔
661
                                SOperatorInfo** pOptrInfo) {
662
  QRY_PARAM_CHECK(pOptrInfo);
34,687,297✔
663

664
  int32_t               code = TSDB_CODE_SUCCESS;
34,689,473✔
665
  int32_t               lino = 0;
34,689,473✔
666
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
34,689,473✔
667
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
34,669,129✔
668
  if (pInfo == NULL || pOperator == NULL) {
34,682,901✔
669
    code = terrno;
556✔
670
    goto _error;
×
671
  }
672
  initOperatorCostInfo(pOperator);
34,682,345✔
673

674
  pOperator->pPhyNode = (SNode*)pAggNode;
34,689,497✔
675
  pOperator->exprSupp.hasWindowOrGroup = true;
34,693,029✔
676
  pOperator->exprSupp.hasWindow = false;
34,689,912✔
677

678
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
34,688,937✔
679
  if (pResBlock == NULL) {
34,691,929✔
680
    code = terrno;
×
681
    goto _error;
×
682
  }
683
  initBasicInfo(&pInfo->binfo, pResBlock);
34,691,929✔
684

685
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
34,687,541✔
686

687
  pInfo->pGroupCols = NULL;
34,694,147✔
688
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
34,689,425✔
689
  QUERY_CHECK_CODE(code, lino, _error);
34,681,843✔
690

691
  int32_t    numOfScalarExpr = 0;
34,681,843✔
692
  SExprInfo* pScalarExprInfo = NULL;
34,682,425✔
693
  if (pAggNode->pExprs != NULL) {
34,687,073✔
694
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
16,792,581✔
695
    QUERY_CHECK_CODE(code, lino, _error);
16,787,520✔
696
  }
697

698
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
34,575,500✔
699
  QUERY_CHECK_CODE(code, lino, _error);
34,578,459✔
700

701
  initResultSizeInfo(&pOperator->resultInfo, 4096);
34,578,459✔
702
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
34,582,160✔
703
  QUERY_CHECK_CODE(code, lino, _error);
34,586,714✔
704

705
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
34,586,714✔
706
  QUERY_CHECK_CODE(code, lino, _error);
34,583,219✔
707

708
  int32_t    num = 0;
34,583,219✔
709
  SExprInfo* pExprInfo = NULL;
34,584,871✔
710

711
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
34,587,801✔
712
  QUERY_CHECK_CODE(code, lino, _error);
34,584,752✔
713

714
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
34,584,752✔
715
                    NULL, &pTaskInfo->storageAPI.functionStore);
716
  QUERY_CHECK_CODE(code, lino, _error);
34,581,698✔
717

718
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
34,577,982✔
719
                            pTaskInfo->pStreamRuntimeInfo);
34,581,698✔
720
  QUERY_CHECK_CODE(code, lino, _error);
34,573,711✔
721

722
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
34,573,711✔
723
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
34,574,097✔
724

725
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
34,578,986✔
726
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
34,587,747✔
727
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
34,577,918✔
728

729
  pInfo->pOperator = pOperator;
34,584,421✔
730

731
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
34,571,966✔
732
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
733
  setOperatorResetStateFn(pOperator, resetGroupOperState);
34,583,079✔
734
  code = appendDownstream(pOperator, &downstream, 1);
34,581,342✔
735
  QUERY_CHECK_CODE(code, lino, _error);
34,573,959✔
736

737
  *pOptrInfo = pOperator;
34,573,959✔
738
  return TSDB_CODE_SUCCESS;
34,577,994✔
739

740
_error:
101,426✔
741
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
101,426✔
742
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
101,426✔
743
  pTaskInfo->code = code;
101,426✔
744
  return code;
101,426✔
745
}
746

747
SSDataBlock* createBlockDataNotLoaded(const SOperatorInfo* pOperator, SSDataBlock* pDataBlock) {
×
748
  int32_t code = TSDB_CODE_SUCCESS;
×
749
  int32_t lino = 0;
×
750
  if (pDataBlock == NULL) {
×
751
    return NULL;
×
752
  }
753

754
  SSDataBlock* pDstBlock = NULL;
×
755
  code = createDataBlock(&pDstBlock);
×
756
  QUERY_CHECK_CODE(code, lino, _end);
×
757

758
  pDstBlock->info = pDataBlock->info;
×
759
  pDstBlock->info.id.blockId = pOperator->resultDataBlockId;
×
760
  pDstBlock->info.capacity = 0;
×
761
  pDstBlock->info.rowSize = 0;
×
762

763
  size_t numOfCols = pOperator->exprSupp.numOfExprs;
×
764
  if (pDataBlock->pBlockAgg) {
×
765
    pDstBlock->pBlockAgg = taosMemoryCalloc(numOfCols, sizeof(SColumnDataAgg));
×
766
    if (pDstBlock->pBlockAgg == NULL) {
×
767
      blockDataDestroy(pDstBlock);
×
768
      return NULL;
×
769
    }
770
    for (int i = 0; i < numOfCols; ++i) {
×
771
      pDstBlock->pBlockAgg[i].colId = -1;
×
772
    }
773
  }
774

775
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
×
776
    SExprInfo*       pExpr = &pOperator->exprSupp.pExprInfo[i];
×
777
    int32_t          slotId = pExpr->base.pParam[0].pCol->slotId;
×
778
    SColumnInfoData* pSrc = taosArrayGet(pDataBlock->pDataBlock, slotId);
×
779
    SColumnInfoData  colInfo = {.hasNull = true, .info = pSrc->info};
×
780
    code = blockDataAppendColInfo(pDstBlock, &colInfo);
×
781
    QUERY_CHECK_CODE(code, lino, _end);
×
782

783
    SColumnInfoData* pDst = taosArrayGet(pDstBlock->pDataBlock, i);
×
784
    if (pDataBlock->pBlockAgg && pDataBlock->pBlockAgg[slotId].colId != -1) {
×
785
      pDstBlock->pBlockAgg[i] = pDataBlock->pBlockAgg[slotId];
×
786
    } else {
787
      code = doEnsureCapacity(pDst, &pDstBlock->info, pDataBlock->info.rows, false);
×
788
      QUERY_CHECK_CODE(code, lino, _end);
×
789

790
      code = colDataAssign(pDst, pSrc, pDataBlock->info.rows, &pDataBlock->info);
×
791
      QUERY_CHECK_CODE(code, lino, _end);
×
792
    }
793
  }
794

795
_end:
×
796
  if (code != TSDB_CODE_SUCCESS) {
×
797
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
798
    blockDataDestroy(pDstBlock);
×
799
    return NULL;
×
800
  }
801
  return pDstBlock;
×
802
}
803

804
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
22,647,077✔
805
  int32_t                 code = TSDB_CODE_SUCCESS;
22,647,077✔
806
  int32_t                 lino = 0;
22,647,077✔
807
  SPartitionOperatorInfo* pInfo = pOperator->info;
22,647,077✔
808
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
22,661,643✔
809

810
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
811
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
2,147,483,647✔
812
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
2,147,483,647✔
813

814
    SDataGroupInfo* pGroupInfo = NULL;
2,147,483,647✔
815
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
2,147,483,647✔
816
    if (pPage == NULL) {
2,147,483,647✔
817
      T_LONG_JMP(pTaskInfo->env, terrno);
×
818
    }
819

820
    pGroupInfo->numOfRows += 1;
2,147,483,647✔
821

822
    // group id
823
    if (pGroupInfo->groupId == 0) {
2,147,483,647✔
824
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
81,784,166✔
825
    }
826

827
    if (pBlock->info.dataLoad) {
2,147,483,647✔
828
      // number of rows
829
      int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
830

831
      size_t numOfCols = pOperator->exprSupp.numOfExprs;
2,147,483,647✔
832
      for (int32_t i = 0; i < numOfCols; ++i) {
2,147,483,647✔
833
        SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
2,147,483,647✔
834
        int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
2,147,483,647✔
835
        SColumnInfoData* pSrcColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
836
        SColumnInfoData* pDstColInfoData = taosArrayGet(pInfo->binfo.pRes->pDataBlock, i);
2,147,483,647✔
837
        QUERY_CHECK_NULL(pSrcColInfoData, code, lino, _end, terrno);
2,147,483,647✔
838
        QUERY_CHECK_NULL(pDstColInfoData, code, lino, _end, terrno);
2,147,483,647✔
839

840
        int32_t bytes = pDstColInfoData->info.bytes;
2,147,483,647✔
841
        int32_t startOffset = pInfo->columnOffset[i];
2,147,483,647✔
842
        int32_t reservedBytes = blockDataGetPagedColumnReservedBytes(pDstColInfoData);
2,147,483,647✔
843

844
        int32_t* columnLen = NULL;
2,147,483,647✔
845
        int32_t  contentLen = 0;
2,147,483,647✔
846

847
        if (IS_VAR_DATA_TYPE(pDstColInfoData->info.type)) {
2,147,483,647✔
848
          int32_t* offset = (int32_t*)((char*)pPage + startOffset);
2,147,483,647✔
849
          columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
2,147,483,647✔
850
          char* data = (char*)((char*)columnLen + sizeof(int32_t));
2,147,483,647✔
851

852
          if (colDataIsNull_s(pSrcColInfoData, j)) {
2,147,483,647✔
853
            offset[(*rows)] = -1;
2,147,483,647✔
854
            contentLen = 0;
2,147,483,647✔
855
          } else if (pSrcColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
856
            offset[*rows] = (*columnLen);
28,704✔
857
            char*   src = colDataGetData(pSrcColInfoData, j);
28,704✔
858
            int32_t dataLen = getJsonValueLen(src);
28,260✔
859

860
            memcpy(data + (*columnLen), src, dataLen);
28,704✔
861
            int32_t v = (data + (*columnLen) + dataLen - (char*)pPage);
28,704✔
862
            QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
28,704✔
863

864
            contentLen = dataLen;
28,704✔
865
          } else {
866
            if (IS_STR_DATA_BLOB(pSrcColInfoData->info.type)) {
2,147,483,647✔
867
              offset[*rows] = (*columnLen);
×
868
              char* src = colDataGetData(pSrcColInfoData, j);
×
869
              memcpy(data + (*columnLen), src, blobDataTLen(src));
×
870
              int32_t v = (data + (*columnLen) + blobDataTLen(src) - (char*)pPage);
×
871
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
872

873
              contentLen = blobDataTLen(src);
×
874
            } else {
875
              offset[*rows] = (*columnLen);
2,147,483,647✔
876
              char* src = colDataGetData(pSrcColInfoData, j);
2,147,483,647✔
877
              memcpy(data + (*columnLen), src, varDataTLen(src));
2,147,483,647✔
878
              int32_t v = (data + (*columnLen) + varDataTLen(src) - (char*)pPage);
2,147,483,647✔
879
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
2,147,483,647✔
880

881
              contentLen = varDataTLen(src);
2,147,483,647✔
882
            }
883
          }
884

885
          QUERY_CHECK_CONDITION((contentLen <= reservedBytes), code, lino, _end,
2,147,483,647✔
886
                                TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
887
          QUERY_CHECK_CONDITION(((*columnLen) + contentLen <= reservedBytes * pInfo->rowCapacity), code, lino, _end,
2,147,483,647✔
888
                                TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
889
        } else {
890
          char* bitmap = (char*)pPage + startOffset;
2,147,483,647✔
891
          columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
2,147,483,647✔
892
          char* data = (char*)columnLen + sizeof(int32_t);
2,147,483,647✔
893

894
          bool isNull = colDataIsNull_f(pSrcColInfoData, j);
2,147,483,647✔
895
          if (isNull) {
2,147,483,647✔
896
            colDataSetNull_f(bitmap, (*rows));
2,147,483,647✔
897
          } else {
898
            memcpy(data + (*columnLen), colDataGetData(pSrcColInfoData, j), bytes);
2,147,483,647✔
899
            QUERY_CHECK_CONDITION(((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf)), code,
2,147,483,647✔
900
                                  lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
901
          }
902
          contentLen = bytes;
2,147,483,647✔
903
        }
904

905
        (*columnLen) += contentLen;
2,147,483,647✔
906
      }
907

908
      (*rows) += 1;
2,147,483,647✔
909

910
      setBufPageDirty(pPage, true);
2,147,483,647✔
911
      releaseBufPage(pInfo->pBuf, pPage);
2,147,483,647✔
912
    } else {
913
      SSDataBlock* dataNotLoadBlock = createBlockDataNotLoaded(pOperator, pBlock);
×
914
      if (dataNotLoadBlock == NULL) {
×
915
        T_LONG_JMP(pTaskInfo->env, terrno);
×
916
      }
917
      if (pGroupInfo->blockForNotLoaded == NULL) {
×
918
        pGroupInfo->blockForNotLoaded = taosArrayInit(0, sizeof(SSDataBlock*));
×
919
        QUERY_CHECK_NULL(pGroupInfo->blockForNotLoaded, code, lino, _end, terrno);
×
920
        pGroupInfo->offsetForNotLoaded = 0;
×
921
      }
922
      dataNotLoadBlock->info.id.groupId = pGroupInfo->groupId;
×
923
      dataNotLoadBlock->info.dataLoad = 0;
×
924
      void* tmp = taosArrayPush(pGroupInfo->blockForNotLoaded, &dataNotLoadBlock);
×
925
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
926
      break;
×
927
    }
928
  }
929

930
_end:
26,368,986✔
931
  if (code != TSDB_CODE_SUCCESS) {
22,660,831✔
932
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
933
    T_LONG_JMP(pTaskInfo->env, code);
×
934
  }
935
}
22,660,831✔
936

937
void* getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len) {
2,147,483,647✔
938
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
939
  int32_t         lino = 0;
2,147,483,647✔
940
  SDataGroupInfo* p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
2,147,483,647✔
941

942
  void* pPage = NULL;
2,147,483,647✔
943
  if (p == NULL) {  // it is a new group
2,147,483,647✔
944
    SDataGroupInfo gi = {0};
81,785,021✔
945
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
81,785,474✔
946
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
81,779,559✔
947

948
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
81,779,559✔
949
    if (code == TSDB_CODE_DUP_KEY) {
81,788,265✔
950
      code = TSDB_CODE_SUCCESS;
×
951
    }
952
    QUERY_CHECK_CODE(code, lino, _end);
81,788,265✔
953

954
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
81,788,265✔
955

956
    int32_t pageId = 0;
81,789,091✔
957
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
81,787,812✔
958
    if (pPage == NULL) {
81,788,265✔
959
      return pPage;
×
960
    }
961

962
    void* tmp = taosArrayPush(p->pPageList, &pageId);
81,788,265✔
963
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
81,789,615✔
964

965
    *(int32_t*)pPage = 0;
81,789,615✔
966
  } else {
967
    int32_t* curId = taosArrayGetLast(p->pPageList);
2,147,483,647✔
968
    pPage = getBufPage(pInfo->pBuf, *curId);
2,147,483,647✔
969
    if (pPage == NULL) {
2,147,483,647✔
970
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
971
      return pPage;
×
972
    }
973

974
    int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
975
    if (*rows >= pInfo->rowCapacity) {
2,147,483,647✔
976
      // release buffer
977
      releaseBufPage(pInfo->pBuf, pPage);
280,700,134✔
978

979
      // add a new page for current group
980
      int32_t pageId = 0;
280,696,429✔
981
      pPage = getNewBufPage(pInfo->pBuf, &pageId);
280,696,958✔
982
      if (pPage == NULL) {
280,697,986✔
983
        qError("failed to get new buffer, code:%s", tstrerror(terrno));
×
984
        return NULL;
×
985
      }
986

987
      void* tmp = taosArrayPush(p->pPageList, &pageId);
280,697,986✔
988
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
280,701,689✔
989

990
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
280,701,689✔
991
    }
992
  }
993

994
  *pGroupInfo = p;
2,147,483,647✔
995

996
_end:
2,147,483,647✔
997
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
998
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
999
    return NULL;
×
1000
  }
1001

1002
  return pPage;
2,147,483,647✔
1003
}
1004

1005
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
3,285,069✔
1006
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
3,285,069✔
1007
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
3,286,549✔
1008
  if (!offset) {
3,282,661✔
1009
    return NULL;
×
1010
  }
1011

1012
  offset[0] = sizeof(int32_t) +
3,282,661✔
1013
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
1014

1015
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
10,421,815✔
1016
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
7,133,342✔
1017

1018
    int32_t payloadLen = blockDataGetPagedColumnReservedBytes(pColInfoData) * rowCapacity;
7,134,694✔
1019

1020
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
7,135,584✔
1021
      // offset segment + content length + payload
1022
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
1,291,710✔
1023
    } else {
1024
      // bitmap + content length + payload
1025
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
5,847,914✔
1026
    }
1027
  }
1028

1029
  return offset;
3,288,473✔
1030
}
1031

1032
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
3,163,060✔
1033
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
3,163,060✔
1034
  for (int32_t i = 0; i < size; i++) {
49,326,571✔
1035
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
46,164,408✔
1036
    if (pGp && pGp->blockForNotLoaded) {
46,164,408✔
1037
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
1038
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
1039
        if (pBlock) blockDataDestroy(*pBlock);
×
1040
      }
1041
      taosArrayClear(pGp->blockForNotLoaded);
×
1042
      pGp->offsetForNotLoaded = 0;
×
1043
    }
1044
    taosArrayDestroy(pGp->pPageList);
46,164,408✔
1045
  }
1046
  taosArrayClear(pInfo->sortedGroupArray);
3,162,163✔
1047
  clearDiskbasedBuf(pInfo->pBuf);
3,162,163✔
1048
}
3,161,727✔
1049

1050
static int compareDataGroupInfo(const void* group1, const void* group2) {
752,655,054✔
1051
  const SDataGroupInfo* pGroupInfo1 = group1;
752,655,054✔
1052
  const SDataGroupInfo* pGroupInfo2 = group2;
752,655,054✔
1053

1054
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
752,655,054✔
1055
    return 0;
×
1056
  }
1057

1058
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
752,655,507✔
1059
}
1060

1061
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
46,913,830✔
1062
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
46,913,830✔
1063
    SSDataBlock** pBlock = taosArrayGet(pGroupInfo->blockForNotLoaded, pGroupInfo->offsetForNotLoaded);
×
1064
    if (!pBlock) {
×
1065
      return NULL;
×
1066
    }
1067
    pGroupInfo->offsetForNotLoaded++;
×
1068
    return *pBlock;
×
1069
  }
1070
  return NULL;
46,914,441✔
1071
}
1072

1073
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
333,415,064✔
1074
  int32_t                 code = TSDB_CODE_SUCCESS;
333,415,064✔
1075
  int32_t                 lino = 0;
333,415,064✔
1076
  SPartitionOperatorInfo* pInfo = pOperator->info;
333,415,064✔
1077
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
333,418,396✔
1078

1079
  if (pInfo->remainRows == 0) {
333,416,979✔
1080
    blockDataCleanup(pInfo->binfo.pRes);
305,268,536✔
1081
    SDataGroupInfo* pGroupInfo =
305,268,616✔
1082
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
305,268,087✔
1083
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
305,268,616✔
1084
      if (pGroupInfo != NULL) {
50,183,203✔
1085
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
46,914,441✔
1086
        if (ret != NULL) return ret;
46,911,759✔
1087
      }
1088
      // try next group data
1089
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
50,180,521✔
1090
        setOperatorCompleted(pOperator);
3,163,051✔
1091
        clearPartitionOperator(pInfo);
3,163,060✔
1092
        return NULL;
3,161,727✔
1093
      }
1094
      ++pInfo->groupIndex;
47,018,367✔
1095

1096
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
47,018,811✔
1097
      if (pGroupInfo == NULL) {
47,018,811✔
1098
        qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1099
        T_LONG_JMP(pTaskInfo->env, terrno);
×
1100
      }
1101
      pInfo->pageIndex = 0;
47,018,811✔
1102
    }
1103

1104
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
302,102,892✔
1105
    if (pageId == NULL) {
302,100,328✔
1106
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1107
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1108
    }
1109
    void* page = getBufPage(pInfo->pBuf, *pageId);
302,100,328✔
1110
    if (page == NULL) {
302,097,669✔
1111
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1112
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1113
    }
1114
    if (*(int32_t*)page == 0) {
302,097,669✔
1115
      releaseBufPage(pInfo->pBuf, page);
×
1116
      SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
×
1117
      if (ret != NULL) return ret;
×
1118
      if (pInfo->groupIndex + 1 < taosArrayGetSize(pInfo->sortedGroupArray)) {
×
1119
        pInfo->groupIndex++;
×
1120
        pInfo->pageIndex = 0;
×
1121
      } else {
1122
        setOperatorCompleted(pOperator);
×
1123
        clearPartitionOperator(pInfo);
×
1124
        return NULL;
×
1125
      }
1126
      return buildPartitionResult(pOperator);
×
1127
    }
1128

1129
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
302,099,898✔
1130
    QUERY_CHECK_CODE(code, lino, _end);
302,102,338✔
1131

1132
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
302,102,338✔
1133
    QUERY_CHECK_CODE(code, lino, _end);
302,107,230✔
1134

1135
    pInfo->pageIndex += 1;
302,107,230✔
1136
    releaseBufPage(pInfo->pBuf, page);
302,106,342✔
1137
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
302,100,921✔
1138
    pInfo->binfo.pRes->info.dataLoad = 1;
302,102,867✔
1139
    pInfo->orderedRows = 0;
302,102,961✔
1140
  } else if (pInfo->pOrderInfoArr == NULL) {
28,152,429✔
1141
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1142
  }
1143

1144
  if (pInfo->pOrderInfoArr) {
330,252,547✔
1145
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
83,243,574✔
1146
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
83,241,987✔
1147
    QUERY_CHECK_CODE(code, lino, _end);
83,240,400✔
1148
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
83,240,400✔
1149
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
83,245,690✔
1150
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
83,245,690✔
1151
  }
1152

1153
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
330,257,014✔
1154
  QUERY_CHECK_CODE(code, lino, _end);
330,248,258✔
1155

1156
_end:
330,248,258✔
1157
  if (code != TSDB_CODE_SUCCESS) {
330,248,258✔
1158
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1159
    T_LONG_JMP(pTaskInfo->env, code);
×
1160
  }
1161

1162
  return pInfo->binfo.pRes;
330,248,258✔
1163
}
1164

1165
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
333,446,743✔
1166
  if (pOperator->status == OP_EXEC_DONE) {
333,446,743✔
1167
    (*ppRes) = NULL;
13,955✔
1168
    return TSDB_CODE_SUCCESS;
13,955✔
1169
  }
1170

1171
  int32_t                 code = TSDB_CODE_SUCCESS;
333,434,208✔
1172
  int32_t                 lino = 0;
333,434,208✔
1173
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
333,434,208✔
1174
  SPartitionOperatorInfo* pInfo = pOperator->info;
333,436,409✔
1175
  SSDataBlock*            pRes = pInfo->binfo.pRes;
333,436,929✔
1176

1177
  if (pOperator->status == OP_RES_TO_RETURN) {
333,432,708✔
1178
    (*ppRes) = buildPartitionResult(pOperator);
330,151,835✔
1179
    return code;
330,150,131✔
1180
  }
1181

1182
  while (1) {
22,661,813✔
1183
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
25,945,615✔
1184
    if (pBlock == NULL) {
25,920,770✔
1185
      break;
3,268,762✔
1186
    }
1187

1188
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
22,652,008✔
1189
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1190
    if (pInfo->scalarSup.pExprInfo != NULL) {
22,663,001✔
1191
      code =
1192
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
229,620✔
1193
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
114,810✔
1194
      QUERY_CHECK_CODE(code, lino, _end);
114,810✔
1195
    }
1196

1197
    terrno = TSDB_CODE_SUCCESS;
22,659,004✔
1198
    doHashPartition(pOperator, pBlock);
22,659,168✔
1199
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
22,661,190✔
1200
      code = terrno;
×
1201
      QUERY_CHECK_CODE(code, lino, _end);
85✔
1202
    }
1203
  }
1204

1205
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
3,268,762✔
1206
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
3,268,318✔
1207

1208
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
3,268,318✔
1209
  while (pGroupIter != NULL) {
85,058,906✔
1210
    SDataGroupInfo* pGroupInfo = pGroupIter;
81,790,588✔
1211
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
81,790,135✔
1212
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
81,790,135✔
1213
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
81,790,135✔
1214
  }
1215

1216
  taosArraySort(groupArray, compareDataGroupInfo);
3,268,318✔
1217
  pInfo->sortedGroupArray = groupArray;
3,268,309✔
1218
  pInfo->groupIndex = -1;
3,268,309✔
1219
  taosHashClear(pInfo->pGroupSet);
3,268,309✔
1220

1221
  pOperator->status = OP_RES_TO_RETURN;
3,268,309✔
1222
  code = blockDataEnsureCapacity(pRes, 4096);
3,268,309✔
1223
  QUERY_CHECK_CODE(code, lino, _end);
3,268,762✔
1224

1225
_end:
3,268,762✔
1226
  if (code != TSDB_CODE_SUCCESS) {
3,268,762✔
1227
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1228
    pTaskInfo->code = code;
×
1229
    T_LONG_JMP(pTaskInfo->env, code);
×
1230
  }
1231

1232
  (*ppRes) = buildPartitionResult(pOperator);
3,268,762✔
1233
  return code;
3,267,865✔
1234
}
1235

1236
static void destroyPartitionOperatorInfo(void* param) {
3,310,310✔
1237
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
3,310,310✔
1238
  cleanupBasicInfo(&pInfo->binfo);
3,310,310✔
1239
  taosArrayDestroy(pInfo->pGroupCols);
3,309,424✔
1240

1241
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
7,494,175✔
1242
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
4,182,625✔
1243
    taosMemoryFree(key.pData);
4,184,133✔
1244
  }
1245

1246
  taosArrayDestroy(pInfo->pGroupColVals);
3,310,033✔
1247
  taosMemoryFree(pInfo->keyBuf);
3,307,168✔
1248

1249
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
3,307,918✔
1250
  for (int32_t i = 0; i < size; i++) {
38,935,446✔
1251
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
35,625,736✔
1252
    if (pGp) {
35,625,736✔
1253
      taosArrayDestroy(pGp->pPageList);
35,625,736✔
1254
    }
1255
  }
1256
  taosArrayDestroy(pInfo->sortedGroupArray);
3,309,710✔
1257

1258
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
3,307,316✔
1259
  while (pGroupIter != NULL) {
3,309,710✔
1260
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1261
    taosArrayDestroy(pGroupInfo->pPageList);
×
1262
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1263
  }
1264

1265
  taosHashCleanup(pInfo->pGroupSet);
3,309,710✔
1266
  taosMemoryFree(pInfo->columnOffset);
3,309,857✔
1267

1268
  cleanupExprSupp(&pInfo->scalarSup);
3,308,072✔
1269
  destroyDiskbasedBuf(pInfo->pBuf);
3,310,449✔
1270
  taosArrayDestroy(pInfo->pOrderInfoArr);
3,307,639✔
1271
  taosMemoryFreeClear(param);
3,307,342✔
1272
}
3,309,894✔
1273

1274
static int32_t resetPartitionOperState(SOperatorInfo* pOper) {
×
1275
  SPartitionOperatorInfo* pInfo = pOper->info;
×
1276
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
1277
  SPartitionPhysiNode* pPhynode = (SPartitionPhysiNode*)pOper->pPhyNode;
×
1278
  resetBasicOperatorState(&pInfo->binfo);
×
1279

1280
  int32_t code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
1281
    &pTaskInfo->storageAPI.functionStore);
1282

1283
  clearPartitionOperator(pInfo);
×
1284

1285
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
×
1286
  while (pGroupIter != NULL) {
×
1287
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1288
    taosArrayDestroy(pGroupInfo->pPageList);
×
1289
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1290
  }
1291
  taosHashClear(pInfo->pGroupSet);
×
1292

1293
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
×
1294
  for (int32_t i = 0; i < size; i++) {
×
1295
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
×
1296
    if (pGp) {
×
1297
      taosArrayDestroy(pGp->pPageList);
×
1298
    }
1299
  }
1300
  taosArrayDestroy(pInfo->sortedGroupArray);
×
1301
  pInfo->sortedGroupArray = NULL;
×
1302

1303
  pInfo->groupIndex = 0;
×
1304
  pInfo->pageIndex = 0;
×
1305
  pInfo->remainRows = 0;
×
1306
  pInfo->orderedRows = 0;
×
1307
  return 0;
×
1308
}
1309

1310
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
3,307,646✔
1311
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1312
  QRY_PARAM_CHECK(pOptrInfo);
3,307,646✔
1313

1314
  int32_t                 code = TSDB_CODE_SUCCESS;
3,308,994✔
1315
  int32_t                 lino = 0;
3,308,994✔
1316
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
3,308,994✔
1317
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
3,299,529✔
1318
  if (pInfo == NULL || pOperator == NULL) {
3,301,057✔
UNCOV
1319
    pTaskInfo->code = code = terrno;
×
1320
    goto _error;
×
1321
  }
1322
  initOperatorCostInfo(pOperator);
3,302,121✔
1323

1324
  pOperator->pPhyNode = pPartNode;
3,310,477✔
1325
  int32_t    numOfCols = 0;
3,309,891✔
1326
  SExprInfo* pExprInfo = NULL;
3,309,438✔
1327
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
3,305,884✔
1328
  QUERY_CHECK_CODE(code, lino, _error);
3,310,477✔
1329
  pOperator->exprSupp.numOfExprs = numOfCols;
3,310,477✔
1330
  pOperator->exprSupp.pExprInfo = pExprInfo;
3,310,024✔
1331

1332
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
3,308,515✔
1333

1334
  if (pPartNode->needBlockOutputTsOrder) {
3,306,904✔
1335
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
265,359✔
1336
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
265,359✔
1337
    if (!pInfo->pOrderInfoArr) {
265,359✔
1338
      pTaskInfo->code = terrno;
×
1339
      goto _error;
×
1340
    }
1341

1342
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
265,359✔
1343
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
265,359✔
1344
  }
1345

1346
  if (pPartNode->pExprs != NULL) {
3,307,786✔
1347
    int32_t    num = 0;
91,727✔
1348
    SExprInfo* pExprInfo1 = NULL;
91,727✔
1349
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
90,672✔
1350
    QUERY_CHECK_CODE(code, lino, _error);
91,727✔
1351

1352
    code = initExprSupp(&pInfo->scalarSup, pExprInfo1, num, &pTaskInfo->storageAPI.functionStore);
70,953✔
1353
    QUERY_CHECK_CODE(code, lino, _error);
70,953✔
1354
  }
1355

1356
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
3,288,351✔
1357
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
3,284,563✔
1358
  if (pInfo->pGroupSet == NULL) {
3,289,259✔
1359
    goto _error;
×
1360
  }
1361

1362
  uint32_t defaultPgsz = 0;
3,288,228✔
1363
  int64_t  defaultBufsz = 0;
3,289,117✔
1364

1365
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
3,289,117✔
1366
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
3,288,220✔
1367
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
3,287,775✔
1368
  if (code != TSDB_CODE_SUCCESS) {
3,284,918✔
1369
    goto _error;
×
1370
  }
1371

1372
  if (!osTempSpaceAvailable()) {
3,284,918✔
1373
    terrno = TSDB_CODE_NO_DISKSPACE;
×
1374
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
×
1375
    goto _error;
×
1376
  }
1377

1378
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
3,285,841✔
1379
  if (code != TSDB_CODE_SUCCESS) {
3,286,391✔
1380
    goto _error;
×
1381
  }
1382

1383
  pInfo->rowCapacity =
3,285,218✔
1384
      getPartitionPageRowCapacity(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
3,284,014✔
1385
                                  blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
3,286,391✔
1386
  if (pInfo->rowCapacity < 0) {
3,284,639✔
1387
    code = terrno;
×
1388
    goto _error;
×
1389
  }
1390

1391
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
3,278,650✔
1392
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
3,289,092✔
1393

1394
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
3,281,187✔
1395
  if (code != TSDB_CODE_SUCCESS) {
3,283,008✔
1396
    goto _error;
×
1397
  }
1398

1399
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
3,283,008✔
1400
                  pTaskInfo);
1401

1402
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
3,285,353✔
1403
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1404

1405
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
3,282,452✔
1406
  code = appendDownstream(pOperator, &downstream, 1);
3,284,884✔
1407
  if (code != TSDB_CODE_SUCCESS) {
3,278,883✔
1408
    goto _error;
×
1409
  }
1410

1411
  *pOptrInfo = pOperator;
3,278,883✔
1412
  return TSDB_CODE_SUCCESS;
3,277,819✔
1413

1414
_error:
20,774✔
1415
  if (pInfo != NULL) {
20,774✔
1416
    destroyPartitionOperatorInfo(pInfo);
20,774✔
1417
  }
1418
  pTaskInfo->code = code;
20,774✔
1419
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
20,774✔
1420
  TAOS_RETURN(code);
20,774✔
1421
}
1422

1423
int32_t setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
2,147,483,647✔
1424
                                int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
1425
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
1426
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
2,147,483,647✔
1427
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
2,147,483,647✔
1428

1429
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
2,147,483,647✔
1430
                                                  false, pAggSup, false);
1431
  if (pResultRow == NULL || pTaskInfo->code != 0) {
2,147,483,647✔
1432
    return pTaskInfo->code;
×
1433
  }
1434

1435
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
2,147,483,647✔
1436
}
1437

1438
void freePartItem(void* ptr) {
×
1439
  SPartitionDataInfo* pPart = (SPartitionDataInfo*)ptr;
×
1440
  taosArrayDestroy(pPart->rowIds);
×
1441
}
×
1442

1443
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
34,688,183✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
34,688,183✔
1445
  int32_t lino = 0;
34,688,183✔
1446
  size_t  numOfCols = LIST_LENGTH(pNodeList);
34,688,183✔
1447
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
34,688,348✔
1448
  if (pList == NULL) {
34,682,205✔
1449
    code = terrno;
×
1450
    (*pArrayRes) = NULL;
×
1451
    QUERY_CHECK_CODE(code, lino, _end);
5,961✔
1452
  }
1453

1454
  for (int32_t i = 0; i < numOfCols; ++i) {
85,124,359✔
1455
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
50,441,027✔
1456
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
50,442,991✔
1457

1458
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
50,442,991✔
1459
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
50,443,647✔
1460

1461
      SColumn c = extractColumnFromColumnNode(pColNode);
50,444,931✔
1462
      void*   tmp = taosArrayPush(pList, &c);
50,446,970✔
1463
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
50,446,970✔
1464
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
×
1465
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
×
1466
      SColumn     c = {0};
×
1467
      c.slotId = pNode->slotId;
×
1468
      c.colId = pNode->slotId;
×
1469
      c.type = pValNode->node.type;
×
1470
      c.bytes = pValNode->node.resType.bytes;
×
1471
      c.scale = pValNode->node.resType.scale;
×
1472
      c.precision = pValNode->node.resType.precision;
×
1473

1474
      void* tmp = taosArrayPush(pList, &c);
×
1475
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1476
    }
1477
  }
1478

1479
  (*pArrayRes) = pList;
34,683,332✔
1480

1481
_end:
34,694,173✔
1482
  if (code != TSDB_CODE_SUCCESS) {
34,694,173✔
1483
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1484
  }
1485
  return code;
34,689,389✔
1486
}
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