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

taosdata / TDengine / #5048

10 May 2026 03:11AM UTC coverage: 73.222% (+0.07%) from 73.152%
#5048

push

travis-ci

web-flow
merge: from main to 3.0 branch #35290

353 of 452 new or added lines in 9 files covered. (78.1%)

587 existing lines in 140 files now uncovered.

278189 of 379928 relevant lines covered (73.22%)

135206397.85 hits per line

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

76.61
/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) {
69,228,192✔
77
  SGroupKeys* pKey = (SGroupKeys*)param;
69,228,192✔
78
  taosMemoryFree(pKey->pData);
69,228,192✔
79
}
69,230,867✔
80

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

87
  cleanupBasicInfo(&pInfo->binfo);
34,182,294✔
88
  taosMemoryFreeClear(pInfo->keyBuf);
34,182,300✔
89
  taosArrayDestroy(pInfo->pGroupCols);
34,179,203✔
90
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
34,183,498✔
91
  cleanupExprSupp(&pInfo->scalarSup);
34,181,797✔
92

93
  if (pInfo->pOperator != NULL) {
34,185,074✔
94
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
34,086,710✔
95
                      false);
96
    pInfo->pOperator = NULL;
34,084,513✔
97
  }
98

99
  cleanupGroupResInfo(&pInfo->groupResInfo);
34,178,981✔
100
  cleanupAggSup(&pInfo->aggSup);
34,179,243✔
101
  taosMemoryFreeClear(param);
34,182,900✔
102
}
103

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

111
  for (int32_t i = 0; i < numOfCols; ++i) {
13,287,857✔
112
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);
10,119,702✔
113
    if (pCol == NULL) {
10,112,892✔
114
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
115
      return -1;
×
116
    }
117

118
    payloadRowSize += blockDataGetPagedColumnReservedBytes(pCol);
10,112,892✔
119
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
10,114,892✔
120
      ++numVarCols;
1,371,230✔
121
    } else {
122
      ++numFixCols;
8,747,256✔
123
    }
124
  }
125

126
  int32_t nRows = payloadSize / payloadRowSize;
3,168,155✔
127
  if (nRows < 1) {
3,168,155✔
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,168,155✔
134
  int32_t start = 1;
3,168,155✔
135
  int32_t end = nRows;
3,168,155✔
136
  while (start <= end) {
27,263,828✔
137
    int32_t mid = start + (end - start) / 2;
24,095,673✔
138
    int32_t midSize = payloadRowSize * mid + numVarCols * sizeof(int32_t) * mid + numFixCols * BitmapLen(mid);
24,095,673✔
139
    if (midSize > payloadSize) {
24,095,673✔
140
      result = mid;
4,773,430✔
141
      end = mid - 1;
4,773,430✔
142
    } else {
143
      start = mid + 1;
19,322,243✔
144
    }
145
  }
146

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

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

156
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
37,241,312✔
157
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
110,545,009✔
158
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
73,300,491✔
159
    if (!pCol) {
73,297,604✔
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
73,297,604✔
164

165
    SGroupKeys key = {0};
73,296,866✔
166
    key.bytes = pCol->bytes;
73,289,177✔
167
    key.type = pCol->type;
73,294,327✔
168
    key.isNull = false;
73,285,458✔
169
    key.pData = taosMemoryCalloc(1, pCol->bytes);
73,285,458✔
170
    if (key.pData == NULL) {
73,284,859✔
171
      return terrno;
×
172
    }
173

174
    void* tmp = taosArrayPush((*pGroupColVals), &key);
73,284,859✔
175
    if (!tmp) {
73,300,560✔
176
      return terrno;
×
177
    }
178
  }
179

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

183
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
37,244,908✔
184
  if ((*keyBuf) == NULL) {
37,237,032✔
185
    return terrno;
×
186
  }
187

188
  return TSDB_CODE_SUCCESS;
37,240,556✔
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,336✔
216

217
      if (memcmp(pkey->pData, val, dataLen) == 0) {
7,336✔
218
        continue;
1,048✔
219
      } else {
220
        return false;
6,288✔
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);
107,871✔
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,208,372,467✔
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);
43,422✔
278
        memcpy(pkey->pData, val, dataLen);
43,422✔
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));
×
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);
43,422✔
307
      memcpy(pStart, (pkey->pData), dataLen);
43,422✔
308
      pStart += dataLen;
43,422✔
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);
183✔
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);
16,808✔
340
          memcpy(dest, data, dataLen);
16,808✔
341
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
342
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,722,078,378✔
343
            blobDataCopy(dest, data);
82,324✔
344
          } else {
345
            varDataCopy(dest, data);
1,723,519,312✔
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) {
289,444,829✔
360
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
289,444,829✔
361
  SGroupbyOperatorInfo* pInfo = pOperator->info;
289,461,767✔
362

363
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
289,458,711✔
364
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
289,459,269✔
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;
289,459,153✔
371
  terrno = TSDB_CODE_SUCCESS;
289,459,153✔
372

373
  int32_t num = 0;
289,449,616✔
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);
25,381,001✔
378
      pInfo->isInit = true;
25,368,045✔
379
      num++;
25,370,158✔
380
      continue;
25,370,158✔
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);
258,195,373✔
392
      num = 1;
258,192,140✔
393
      continue;
258,192,140✔
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) {
289,451,498✔
418
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
289,454,674✔
419
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
289,449,061✔
420
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
421
    if (ret != TSDB_CODE_SUCCESS) {
289,431,887✔
422
      T_LONG_JMP(pTaskInfo->env, ret);
×
423
    }
424

425
    int32_t rowIndex = pBlock->info.rows - num;
289,431,887✔
426
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
289,442,982✔
427
                                          pOperator->exprSupp.numOfExprs);
428
    if (ret != TSDB_CODE_SUCCESS) {
289,438,776✔
429
      T_LONG_JMP(pTaskInfo->env, ret);
×
430
    }
431
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
289,438,776✔
432
  }
433
}
289,440,148✔
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,665,972✔
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)) {
37,147,448✔
464
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
18,582,502✔
465
                               pHashmap, pOperator->resultInfo.threshold, true);
466
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
18,582,504✔
467
        break;
17,560✔
468
      }
469
      pBlock->info.id.groupId = 0;
18,564,944✔
470
      pBlock->info.id.baseGId = 0;
18,564,944✔
471
    }
472

473
    // clear the group id info in SSDataBlock, since the client does not need it
474
    pBlock->info.id.groupId = 0;
18,582,504✔
475
    pBlock->info.id.baseGId = 0;
18,582,504✔
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) {
69,902✔
482
    return true;  // limit reached, stop processing further rows
22,325✔
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,778,837✔
493
  }
494

495
  if (pLimitInfo->remainGroupOffset > 0) {
2,147,483,647✔
496
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
85,216✔
497
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
13,996✔
498
      blockDataCleanup(pRes);
13,996✔
499
      return TSDB_CODE_SUCCESS;
13,996✔
500
    } else {
501
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
71,220✔
502
      QUERY_CHECK_CODE(code, lino, _end);
71,220✔
503
      pLimitInfo->remainGroupOffset = 0;
71,220✔
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;
219,865✔
509
    if (pRes->info.rows > remainRows) {
219,865✔
510
      blockDataKeepFirstNRows(pRes, remainRows);
96,944✔
511
    }
512
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
219,865✔
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);
33,986,028✔
542
      // clean hash after completed
543
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
33,971,760✔
544
      pInfo->aggSup.pResultRowHashTable = NULL;
33,972,159✔
545
      break;
33,922,534✔
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,041,138✔
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,060,056✔
580
    filterSetExecContext(pOperator->exprSupp.pFilterInfo, pOperator->pTaskInfo, isTaskKilled);
1,117,516✔
581
  }
582

583
  while (1) {
289,442,317✔
584
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
323,508,963✔
585
    if (pBlock == NULL) {
323,524,790✔
586
      break;
34,051,775✔
587
    }
588

589
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
289,473,015✔
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);
289,481,223✔
593
    QUERY_CHECK_CODE(code, lino, _end);
289,470,902✔
594

595
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
596
    if (pInfo->scalarSup.pExprInfo != NULL) {
289,470,902✔
597
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
91,649,809✔
598
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
45,831,002✔
599
      QUERY_CHECK_CODE(code, lino, _end);
45,818,804✔
600
    }
601

602
    doHashGroupbyAgg(pOperator, pBlock);
289,441,871✔
603
  }
604

605
  pOperator->status = OP_RES_TO_RETURN;
34,051,775✔
606

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

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

617
  pGroupResInfo->index = 0;
34,051,963✔
618
  pGroupResInfo->iter = 0;
34,051,736✔
619
  pGroupResInfo->dataPos = NULL;
34,051,783✔
620

621
_end:
34,071,578✔
622
  if (code != TSDB_CODE_SUCCESS) {
34,071,578✔
623
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
19,822✔
624
    pTaskInfo->code = code;
19,822✔
625
    T_LONG_JMP(pTaskInfo->env, code);
19,822✔
626
  } else {
627
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
34,051,756✔
628
  }
629

630
  return code;
34,052,002✔
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,177,889✔
661
                                SOperatorInfo** pOptrInfo) {
662
  QRY_PARAM_CHECK(pOptrInfo);
34,177,889✔
663

664
  int32_t               code = TSDB_CODE_SUCCESS;
34,181,173✔
665
  int32_t               lino = 0;
34,181,173✔
666
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
34,181,173✔
667
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
34,159,834✔
668
  if (pInfo == NULL || pOperator == NULL) {
34,170,687✔
669
    code = terrno;
×
670
    goto _error;
×
671
  }
672
  initOperatorCostInfo(pOperator);
34,171,201✔
673

674
  pOperator->pPhyNode = (SNode*)pAggNode;
34,181,277✔
675
  pOperator->exprSupp.hasWindowOrGroup = true;
34,181,277✔
676
  pOperator->exprSupp.hasWindow = false;
34,181,907✔
677

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

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

687
  pInfo->pGroupCols = NULL;
34,181,107✔
688
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
34,181,665✔
689
  QUERY_CHECK_CODE(code, lino, _error);
34,174,429✔
690

691
  int32_t    numOfScalarExpr = 0;
34,174,429✔
692
  SExprInfo* pScalarExprInfo = NULL;
34,175,489✔
693
  if (pAggNode->pExprs != NULL) {
34,174,872✔
694
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
16,021,137✔
695
    QUERY_CHECK_CODE(code, lino, _error);
16,019,376✔
696
  }
697

698
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
34,069,012✔
699
  QUERY_CHECK_CODE(code, lino, _error);
34,074,170✔
700

701
  initResultSizeInfo(&pOperator->resultInfo, 4096);
34,074,170✔
702
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
34,079,274✔
703
  QUERY_CHECK_CODE(code, lino, _error);
34,084,993✔
704

705
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
34,084,993✔
706
  QUERY_CHECK_CODE(code, lino, _error);
34,074,554✔
707

708
  int32_t    num = 0;
34,074,554✔
709
  SExprInfo* pExprInfo = NULL;
34,075,156✔
710

711
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
34,074,914✔
712
  QUERY_CHECK_CODE(code, lino, _error);
34,068,645✔
713

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

718
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
34,078,929✔
719
                            pTaskInfo->pStreamRuntimeInfo);
34,072,591✔
720
  QUERY_CHECK_CODE(code, lino, _error);
34,072,067✔
721

722
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
34,072,067✔
723
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
34,076,972✔
724

725
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
34,080,192✔
726
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
34,074,379✔
727
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
34,076,470✔
728

729
  pInfo->pOperator = pOperator;
34,073,101✔
730

731
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
34,074,336✔
732
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
733
  setOperatorResetStateFn(pOperator, resetGroupOperState);
34,071,172✔
734
  code = appendDownstream(pOperator, &downstream, 1);
34,065,224✔
735
  QUERY_CHECK_CODE(code, lino, _error);
34,071,547✔
736

737
  *pOptrInfo = pOperator;
34,071,547✔
738
  return TSDB_CODE_SUCCESS;
34,070,400✔
739

740
_error:
97,361✔
741
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
97,361✔
742
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
97,361✔
743
  pTaskInfo->code = code;
97,361✔
744
  return code;
97,361✔
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) {
21,836,842✔
805
  int32_t                 code = TSDB_CODE_SUCCESS;
21,836,842✔
806
  int32_t                 lino = 0;
21,836,842✔
807
  SPartitionOperatorInfo* pInfo = pOperator->info;
21,836,842✔
808
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
21,845,034✔
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);
79,274,252✔
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);
26,188✔
857
            char*   src = colDataGetData(pSrcColInfoData, j);
26,614✔
858
            int32_t dataLen = getJsonValueLen(src);
26,614✔
859

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

864
            contentLen = dataLen;
26,188✔
865
          } else {
866
            if (IS_STR_DATA_BLOB(pSrcColInfoData->info.type)) {
2,147,483,647✔
UNCOV
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:
22,241,970✔
931
  if (code != TSDB_CODE_SUCCESS) {
21,852,857✔
932
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
933
    T_LONG_JMP(pTaskInfo->env, code);
×
934
  }
935
}
21,852,857✔
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};
79,276,619✔
945
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
79,276,619✔
946
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
79,267,152✔
947

948
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
79,267,152✔
949
    if (code == TSDB_CODE_DUP_KEY) {
79,275,617✔
950
      code = TSDB_CODE_SUCCESS;
×
951
    }
952
    QUERY_CHECK_CODE(code, lino, _end);
79,275,617✔
953

954
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
79,275,617✔
955

956
    int32_t pageId = 0;
79,274,728✔
957
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
79,274,728✔
958
    if (pPage == NULL) {
79,272,496✔
959
      return pPage;
×
960
    }
961

962
    void* tmp = taosArrayPush(p->pPageList, &pageId);
79,272,496✔
963
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
79,275,198✔
964

965
    *(int32_t*)pPage = 0;
79,275,198✔
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);
271,706,701✔
978

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

987
      void* tmp = taosArrayPush(p->pPageList, &pageId);
271,702,653✔
988
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
271,709,840✔
989

990
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
271,709,840✔
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,165,326✔
1006
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
3,165,326✔
1007
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
3,165,784✔
1008
  if (!offset) {
3,167,458✔
1009
    return NULL;
×
1010
  }
1011

1012
  offset[0] = sizeof(int32_t) +
3,167,458✔
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,120,883✔
1016
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,952,758✔
1017

1018
    int32_t payloadLen = blockDataGetPagedColumnReservedBytes(pColInfoData) * rowCapacity;
6,945,796✔
1019

1020
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
6,946,641✔
1021
      // offset segment + content length + payload
1022
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
1,264,868✔
1023
    } else {
1024
      // bitmap + content length + payload
1025
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
5,680,445✔
1026
    }
1027
  }
1028

1029
  return offset;
3,168,125✔
1030
}
1031

1032
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
3,039,002✔
1033
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
3,039,002✔
1034
  for (int32_t i = 0; i < size; i++) {
47,798,157✔
1035
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
44,759,588✔
1036
    if (pGp && pGp->blockForNotLoaded) {
44,759,595✔
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);
44,759,176✔
1045
  }
1046
  taosArrayClear(pInfo->sortedGroupArray);
3,038,569✔
1047
  clearDiskbasedBuf(pInfo->pBuf);
3,038,569✔
1048
}
3,037,312✔
1049

1050
static int compareDataGroupInfo(const void* group1, const void* group2) {
728,354,664✔
1051
  const SDataGroupInfo* pGroupInfo1 = group1;
728,354,664✔
1052
  const SDataGroupInfo* pGroupInfo2 = group2;
728,354,664✔
1053

1054
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
728,354,664✔
1055
    return 0;
×
1056
  }
1057

1058
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
728,355,949✔
1059
}
1060

1061
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
45,512,122✔
1062
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
45,512,122✔
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;
45,512,122✔
1071
}
1072

1073
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
322,568,736✔
1074
  int32_t                 code = TSDB_CODE_SUCCESS;
322,568,736✔
1075
  int32_t                 lino = 0;
322,568,736✔
1076
  SPartitionOperatorInfo* pInfo = pOperator->info;
322,568,736✔
1077
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
322,579,708✔
1078

1079
  if (pInfo->remainRows == 0) {
322,578,190✔
1080
    blockDataCleanup(pInfo->binfo.pRes);
295,211,597✔
1081
    SDataGroupInfo* pGroupInfo =
295,212,776✔
1082
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
295,213,275✔
1083
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
295,212,776✔
1084
      if (pGroupInfo != NULL) {
48,662,557✔
1085
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
45,513,819✔
1086
        if (ret != NULL) return ret;
45,512,122✔
1087
      }
1088
      // try next group data
1089
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
48,660,860✔
1090
        setOperatorCompleted(pOperator);
3,038,988✔
1091
        clearPartitionOperator(pInfo);
3,039,840✔
1092
        return NULL;
3,037,312✔
1093
      }
1094
      ++pInfo->groupIndex;
45,624,840✔
1095

1096
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
45,624,414✔
1097
      if (pGroupInfo == NULL) {
45,621,719✔
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;
45,621,719✔
1102
    }
1103

1104
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
292,172,783✔
1105
    if (pageId == NULL) {
292,167,544✔
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);
292,167,544✔
1110
    if (page == NULL) {
292,166,367✔
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) {
292,166,367✔
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);
292,166,672✔
1130
    QUERY_CHECK_CODE(code, lino, _end);
292,170,313✔
1131

1132
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
292,170,313✔
1133
    QUERY_CHECK_CODE(code, lino, _end);
292,172,270✔
1134

1135
    pInfo->pageIndex += 1;
292,172,270✔
1136
    releaseBufPage(pInfo->pBuf, page);
292,173,030✔
1137
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
292,170,001✔
1138
    pInfo->binfo.pRes->info.dataLoad = 1;
292,170,580✔
1139
    pInfo->orderedRows = 0;
292,168,476✔
1140
  } else if (pInfo->pOrderInfoArr == NULL) {
27,366,087✔
1141
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1142
  }
1143

1144
  if (pInfo->pOrderInfoArr) {
319,536,168✔
1145
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
81,838,901✔
1146
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
81,840,419✔
1147
    QUERY_CHECK_CODE(code, lino, _end);
81,839,407✔
1148
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
81,839,407✔
1149
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
81,837,889✔
1150
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
81,840,419✔
1151
  }
1152

1153
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
319,525,448✔
1154
  QUERY_CHECK_CODE(code, lino, _end);
319,511,294✔
1155

1156
_end:
319,511,294✔
1157
  if (code != TSDB_CODE_SUCCESS) {
319,511,294✔
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;
319,511,294✔
1163
}
1164

1165
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
322,596,640✔
1166
  if (pOperator->status == OP_EXEC_DONE) {
322,596,640✔
1167
    (*ppRes) = NULL;
13,482✔
1168
    return TSDB_CODE_SUCCESS;
13,482✔
1169
  }
1170

1171
  int32_t                 code = TSDB_CODE_SUCCESS;
322,588,802✔
1172
  int32_t                 lino = 0;
322,588,802✔
1173
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
322,588,802✔
1174
  SPartitionOperatorInfo* pInfo = pOperator->info;
322,592,983✔
1175
  SSDataBlock*            pRes = pInfo->binfo.pRes;
322,594,305✔
1176

1177
  if (pOperator->status == OP_RES_TO_RETURN) {
322,592,944✔
1178
    (*ppRes) = buildPartitionResult(pOperator);
319,431,050✔
1179
    return code;
319,418,242✔
1180
  }
1181

1182
  while (1) {
21,852,777✔
1183
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
25,019,334✔
1184
    if (pBlock == NULL) {
24,980,454✔
1185
      break;
3,148,319✔
1186
    }
1187

1188
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
21,832,135✔
1189
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1190
    if (pInfo->scalarSup.pExprInfo != NULL) {
21,851,189✔
1191
      code =
1192
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
220,364✔
1193
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
110,182✔
1194
      QUERY_CHECK_CODE(code, lino, _end);
109,330✔
1195
    }
1196

1197
    terrno = TSDB_CODE_SUCCESS;
21,849,828✔
1198
    doHashPartition(pOperator, pBlock);
21,844,509✔
1199
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
21,850,327✔
1200
      code = terrno;
×
1201
      QUERY_CHECK_CODE(code, lino, _end);
80✔
1202
    }
1203
  }
1204

1205
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
3,148,319✔
1206
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
3,147,048✔
1207

1208
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
3,147,048✔
1209
  while (pGroupIter != NULL) {
82,423,234✔
1210
    SDataGroupInfo* pGroupInfo = pGroupIter;
79,274,070✔
1211
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
79,276,619✔
1212
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
79,276,619✔
1213
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
79,276,619✔
1214
  }
1215

1216
  taosArraySort(groupArray, compareDataGroupInfo);
3,149,164✔
1217
  pInfo->sortedGroupArray = groupArray;
3,148,738✔
1218
  pInfo->groupIndex = -1;
3,148,312✔
1219
  taosHashClear(pInfo->pGroupSet);
3,148,312✔
1220

1221
  pOperator->status = OP_RES_TO_RETURN;
3,148,738✔
1222
  code = blockDataEnsureCapacity(pRes, 4096);
3,148,738✔
1223
  QUERY_CHECK_CODE(code, lino, _end);
3,149,164✔
1224

1225
_end:
3,149,164✔
1226
  if (code != TSDB_CODE_SUCCESS) {
3,149,164✔
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,149,164✔
1233
  return code;
3,146,201✔
1234
}
1235

1236
static void destroyPartitionOperatorInfo(void* param) {
3,189,122✔
1237
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
3,189,122✔
1238
  cleanupBasicInfo(&pInfo->binfo);
3,189,122✔
1239
  taosArrayDestroy(pInfo->pGroupCols);
3,188,277✔
1240

1241
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
7,265,484✔
1242
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
4,077,619✔
1243
    taosMemoryFree(key.pData);
4,078,052✔
1244
  }
1245

1246
  taosArrayDestroy(pInfo->pGroupColVals);
3,187,437✔
1247
  taosMemoryFree(pInfo->keyBuf);
3,186,173✔
1248

1249
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
3,188,703✔
1250
  for (int32_t i = 0; i < size; i++) {
37,704,042✔
1251
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
34,516,605✔
1252
    if (pGp) {
34,516,605✔
1253
      taosArrayDestroy(pGp->pPageList);
34,516,605✔
1254
    }
1255
  }
1256
  taosArrayDestroy(pInfo->sortedGroupArray);
3,187,437✔
1257

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

1265
  taosHashCleanup(pInfo->pGroupSet);
3,186,599✔
1266
  taosMemoryFree(pInfo->columnOffset);
3,187,437✔
1267

1268
  cleanupExprSupp(&pInfo->scalarSup);
3,189,122✔
1269
  destroyDiskbasedBuf(pInfo->pBuf);
3,189,122✔
1270
  taosArrayDestroy(pInfo->pOrderInfoArr);
3,186,161✔
1271
  taosMemoryFreeClear(param);
3,186,992✔
1272
}
3,188,696✔
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,186,150✔
1311
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1312
  QRY_PARAM_CHECK(pOptrInfo);
3,186,150✔
1313

1314
  int32_t                 code = TSDB_CODE_SUCCESS;
3,187,867✔
1315
  int32_t                 lino = 0;
3,187,867✔
1316
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
3,187,867✔
1317
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
3,181,743✔
1318
  if (pInfo == NULL || pOperator == NULL) {
3,182,719✔
UNCOV
1319
    pTaskInfo->code = code = terrno;
×
1320
    goto _error;
×
1321
  }
1322
  initOperatorCostInfo(pOperator);
3,183,145✔
1323

1324
  pOperator->pPhyNode = pPartNode;
3,185,997✔
1325
  int32_t    numOfCols = 0;
3,187,694✔
1326
  SExprInfo* pExprInfo = NULL;
3,187,708✔
1327
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
3,185,585✔
1328
  QUERY_CHECK_CODE(code, lino, _error);
3,188,988✔
1329
  pOperator->exprSupp.numOfExprs = numOfCols;
3,188,988✔
1330
  pOperator->exprSupp.pExprInfo = pExprInfo;
3,188,988✔
1331

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

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

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

1346
  if (pPartNode->pExprs != NULL) {
3,186,137✔
1347
    int32_t    num = 0;
87,956✔
1348
    SExprInfo* pExprInfo1 = NULL;
87,956✔
1349
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
87,956✔
1350
    QUERY_CHECK_CODE(code, lino, _error);
87,956✔
1351

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

1356
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
3,166,993✔
1357
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
3,165,743✔
1358
  if (pInfo->pGroupSet == NULL) {
3,169,143✔
1359
    goto _error;
×
1360
  }
1361

1362
  uint32_t defaultPgsz = 0;
3,169,143✔
1363
  int64_t  defaultBufsz = 0;
3,169,143✔
1364

1365
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
3,168,560✔
1366
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
3,168,717✔
1367
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
3,167,865✔
1368
  if (code != TSDB_CODE_SUCCESS) {
3,166,608✔
1369
    goto _error;
×
1370
  }
1371

1372
  if (!osTempSpaceAvailable()) {
3,166,608✔
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,165,620✔
1379
  if (code != TSDB_CODE_SUCCESS) {
3,163,382✔
1380
    goto _error;
×
1381
  }
1382

1383
  pInfo->rowCapacity =
3,165,609✔
1384
      getPartitionPageRowCapacity(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
3,166,478✔
1385
                                  blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
3,163,382✔
1386
  if (pInfo->rowCapacity < 0) {
3,166,192✔
1387
    code = terrno;
×
1388
    goto _error;
×
1389
  }
1390

1391
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
3,161,978✔
1392
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
3,165,425✔
1393

1394
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
3,162,544✔
1395
  if (code != TSDB_CODE_SUCCESS) {
3,161,479✔
1396
    goto _error;
×
1397
  }
1398

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

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

1405
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
3,162,181✔
1406
  code = appendDownstream(pOperator, &downstream, 1);
3,161,830✔
1407
  if (code != TSDB_CODE_SUCCESS) {
3,163,066✔
1408
    goto _error;
×
1409
  }
1410

1411
  *pOptrInfo = pOperator;
3,163,066✔
1412
  return TSDB_CODE_SUCCESS;
3,163,515✔
1413

1414
_error:
20,405✔
1415
  if (pInfo != NULL) {
20,405✔
1416
    destroyPartitionOperatorInfo(pInfo);
20,405✔
1417
  }
1418
  pTaskInfo->code = code;
20,405✔
1419
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
20,405✔
1420
  TAOS_RETURN(code);
20,405✔
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,180,451✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
34,180,451✔
1445
  int32_t lino = 0;
34,180,451✔
1446
  size_t  numOfCols = LIST_LENGTH(pNodeList);
34,180,451✔
1447
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
34,180,977✔
1448
  if (pList == NULL) {
34,174,276✔
1449
    code = terrno;
×
1450
    (*pArrayRes) = NULL;
×
1451
    QUERY_CHECK_CODE(code, lino, _end);
2,979✔
1452
  }
1453

1454
  for (int32_t i = 0; i < numOfCols; ++i) {
103,532,171✔
1455
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
69,361,494✔
1456
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
69,357,424✔
1457

1458
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
69,357,424✔
1459
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
69,363,364✔
1460

1461
      SColumn c = extractColumnFromColumnNode(pColNode);
69,367,507✔
1462
      void*   tmp = taosArrayPush(pList, &c);
69,364,705✔
1463
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
69,364,705✔
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,170,677✔
1480

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