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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

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

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 hits per line

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

76.5
/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) {
56,503,474✔
77
  SGroupKeys* pKey = (SGroupKeys*)param;
56,503,474✔
78
  taosMemoryFree(pKey->pData);
56,503,474✔
79
}
56,502,425✔
80

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

87
  cleanupBasicInfo(&pInfo->binfo);
35,042,337✔
88
  taosMemoryFreeClear(pInfo->keyBuf);
35,047,857✔
89
  taosArrayDestroy(pInfo->pGroupCols);
35,042,902✔
90
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
35,043,492✔
91
  cleanupExprSupp(&pInfo->scalarSup);
35,039,447✔
92

93
  if (pInfo->pOperator != NULL) {
35,047,860✔
94
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
34,941,142✔
95
                      false);
96
    pInfo->pOperator = NULL;
34,940,720✔
97
  }
98

99
  cleanupGroupResInfo(&pInfo->groupResInfo);
35,044,707✔
100
  cleanupAggSup(&pInfo->aggSup);
35,031,141✔
101
  taosMemoryFreeClear(param);
35,042,105✔
102
}
103

104
static int32_t getPartitionPageRowCapacity(const SSDataBlock* pBlock, size_t pageSize, int32_t extraSize) {
4,326,679✔
105
  size_t  numOfCols = taosArrayGetSize(pBlock->pDataBlock);
4,326,679✔
106
  int32_t payloadSize = pageSize - extraSize;
4,334,030✔
107
  int32_t payloadRowSize = 0;
4,334,030✔
108
  int32_t numVarCols = 0;
4,334,030✔
109
  int32_t numFixCols = 0;
4,334,030✔
110

111
  for (int32_t i = 0; i < numOfCols; ++i) {
19,103,507✔
112
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);
14,770,531✔
113
    if (pCol == NULL) {
14,730,096✔
114
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
115
      return -1;
×
116
    }
117

118
    payloadRowSize += blockDataGetPagedColumnReservedBytes(pCol);
14,730,096✔
119
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
14,749,811✔
120
      ++numVarCols;
2,922,304✔
121
    } else {
122
      ++numFixCols;
11,847,173✔
123
    }
124
  }
125

126
  int32_t nRows = payloadSize / payloadRowSize;
4,332,976✔
127
  if (nRows < 1) {
4,332,976✔
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;
4,332,976✔
134
  int32_t start = 1;
4,332,976✔
135
  int32_t end = nRows;
4,332,976✔
136
  while (start <= end) {
34,942,102✔
137
    int32_t mid = start + (end - start) / 2;
30,609,126✔
138
    int32_t midSize = payloadRowSize * mid + numVarCols * sizeof(int32_t) * mid + numFixCols * BitmapLen(mid);
30,609,126✔
139
    if (midSize > payloadSize) {
30,609,126✔
140
      result = mid;
5,889,722✔
141
      end = mid - 1;
5,889,722✔
142
    } else {
143
      start = mid + 1;
24,719,404✔
144
    }
145
  }
146

147
  return (result != -1) ? result - 1 : nRows;
4,332,976✔
148
}
149

150
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
39,230,491✔
151
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
39,230,491✔
152
  if ((*pGroupColVals) == NULL) {
39,181,534✔
153
    return terrno;
×
154
  }
155

156
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
39,198,607✔
157
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
100,874,356✔
158
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
61,668,373✔
159
    if (!pCol) {
61,684,525✔
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
61,684,525✔
164

165
    SGroupKeys key = {0};
61,640,237✔
166
    key.bytes = pCol->bytes;
61,666,954✔
167
    key.type = pCol->type;
61,621,349✔
168
    key.isNull = false;
61,584,858✔
169
    key.pData = taosMemoryCalloc(1, pCol->bytes);
61,584,858✔
170
    if (key.pData == NULL) {
61,587,328✔
171
      return terrno;
×
172
    }
173

174
    void* tmp = taosArrayPush((*pGroupColVals), &key);
61,587,328✔
175
    if (!tmp) {
61,690,263✔
176
      return terrno;
×
177
    }
178
  }
179

180
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
39,205,983✔
181
  (*keyLen) += nullFlagSize;
39,205,983✔
182

183
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
39,230,337✔
184
  if ((*keyBuf) == NULL) {
39,153,850✔
185
    return terrno;
×
186
  }
187

188
  return TSDB_CODE_SUCCESS;
39,161,479✔
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,532✔
216

217
      if (memcmp(pkey->pData, val, dataLen) == 0) {
7,532✔
218
        continue;
1,076✔
219
      } else {
220
        return false;
6,456✔
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✔
UNCOV
224
        int32_t len = blobDataLen(val);
×
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,277,079,499✔
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,400✔
278
        memcpy(pkey->pData, val, dataLen);
45,400✔
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✔
UNCOV
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);
45,400✔
307
      memcpy(pStart, (pkey->pData), dataLen);
45,400✔
308
      pStart += dataLen;
45,400✔
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);
×
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,208✔
340
          memcpy(dest, data, dataLen);
17,208✔
341
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
342
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,844,292,039✔
UNCOV
343
            blobDataCopy(dest, data);
×
344
          } else {
345
            varDataCopy(dest, data);
1,847,120,449✔
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) {
298,486,566✔
360
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
298,486,566✔
361
  SGroupbyOperatorInfo* pInfo = pOperator->info;
298,585,811✔
362

363
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
298,611,493✔
364
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
298,621,230✔
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;
298,619,166✔
371
  terrno = TSDB_CODE_SUCCESS;
298,619,166✔
372

373
  int32_t num = 0;
298,588,160✔
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,241,275✔
378
      pInfo->isInit = true;
26,247,295✔
379
      num++;
26,262,185✔
380
      continue;
26,262,185✔
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);
259,947,926✔
392
      num = 1;
259,933,049✔
393
      continue;
259,933,049✔
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) {
298,581,787✔
418
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
298,595,533✔
419
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
298,570,492✔
420
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
421
    if (ret != TSDB_CODE_SUCCESS) {
298,531,112✔
422
      T_LONG_JMP(pTaskInfo->env, ret);
×
423
    }
424

425
    int32_t rowIndex = pBlock->info.rows - num;
298,531,112✔
426
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
298,515,838✔
427
                                          pOperator->exprSupp.numOfExprs);
428
    if (ret != TSDB_CODE_SUCCESS) {
298,444,673✔
429
      T_LONG_JMP(pTaskInfo->env, ret);
×
430
    }
431
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
298,444,673✔
432
  }
433
}
298,519,166✔
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,591,913✔
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)) {
39,231,265✔
464
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
19,624,727✔
465
                               pHashmap, pOperator->resultInfo.threshold, true);
466
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
19,624,746✔
467
        break;
18,208✔
468
      }
469
      pBlock->info.id.groupId = 0;
19,606,538✔
470
      pBlock->info.id.baseGId = 0;
19,606,538✔
471
    }
472

473
    // clear the group id info in SSDataBlock, since the client does not need it
474
    pBlock->info.id.groupId = 0;
19,624,746✔
475
    pBlock->info.id.baseGId = 0;
19,624,746✔
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) {
72,444✔
482
    return true;  // limit reached, stop processing further rows
23,302✔
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,710,536✔
493
  }
494

495
  if (pLimitInfo->remainGroupOffset > 0) {
2,147,483,647✔
496
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
78,646✔
497
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
18,392✔
498
      blockDataCleanup(pRes);
18,392✔
499
      return TSDB_CODE_SUCCESS;
18,392✔
500
    } else {
501
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
60,254✔
502
      QUERY_CHECK_CODE(code, lino, _end);
60,254✔
503
      pLimitInfo->remainGroupOffset = 0;
60,254✔
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;
218,229✔
509
    if (pRes->info.rows > remainRows) {
218,229✔
510
      blockDataKeepFirstNRows(pRes, remainRows);
95,520✔
511
    }
512
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
218,229✔
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,822,769✔
542
      // clean hash after completed
543
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
34,824,461✔
544
      pInfo->aggSup.pResultRowHashTable = NULL;
34,826,232✔
545
      break;
34,824,470✔
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,964,612✔
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,896,812✔
580
    filterSetExecContext(pOperator->exprSupp.pFilterInfo, pOperator->pTaskInfo, isTaskKilled);
1,173,755✔
581
  }
582

583
  while (1) {
298,487,616✔
584
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
333,369,237✔
585
    if (pBlock == NULL) {
333,483,216✔
586
      break;
34,906,774✔
587
    }
588

589
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
298,576,442✔
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);
298,564,829✔
593
    QUERY_CHECK_CODE(code, lino, _end);
298,561,858✔
594

595
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
596
    if (pInfo->scalarSup.pExprInfo != NULL) {
298,561,858✔
597
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
96,682,845✔
598
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
48,370,556✔
599
      QUERY_CHECK_CODE(code, lino, _end);
48,306,542✔
600
    }
601

602
    doHashGroupbyAgg(pOperator, pBlock);
298,487,226✔
603
  }
604

605
  pOperator->status = OP_RES_TO_RETURN;
34,906,774✔
606

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

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

617
  pGroupResInfo->index = 0;
34,908,296✔
618
  pGroupResInfo->iter = 0;
34,906,456✔
619
  pGroupResInfo->dataPos = NULL;
34,909,180✔
620

621
_end:
34,928,225✔
622
  if (code != TSDB_CODE_SUCCESS) {
34,928,225✔
623
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
21,245✔
624
    pTaskInfo->code = code;
21,245✔
625
    T_LONG_JMP(pTaskInfo->env, code);
21,245✔
626
  } else {
627
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
34,906,980✔
628
  }
629

630
  return code;
34,909,844✔
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,995,971✔
661
                                SOperatorInfo** pOptrInfo) {
662
  QRY_PARAM_CHECK(pOptrInfo);
34,995,971✔
663

664
  int32_t               code = TSDB_CODE_SUCCESS;
35,012,505✔
665
  int32_t               lino = 0;
35,012,505✔
666
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
35,012,505✔
667
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
34,907,565✔
668
  if (pInfo == NULL || pOperator == NULL) {
34,955,603✔
669
    code = terrno;
4,276✔
670
    goto _error;
×
671
  }
672
  initOperatorCostInfo(pOperator);
34,951,471✔
673

674
  pOperator->pPhyNode = (SNode*)pAggNode;
35,001,995✔
675
  pOperator->exprSupp.hasWindowOrGroup = true;
35,026,383✔
676
  pOperator->exprSupp.hasWindow = false;
35,008,590✔
677

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

685
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
35,022,274✔
686

687
  pInfo->pGroupCols = NULL;
35,024,388✔
688
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
35,021,255✔
689
  QUERY_CHECK_CODE(code, lino, _error);
34,943,084✔
690

691
  int32_t    numOfScalarExpr = 0;
34,943,084✔
692
  SExprInfo* pScalarExprInfo = NULL;
34,949,624✔
693
  if (pAggNode->pExprs != NULL) {
34,941,001✔
694
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
16,858,065✔
695
    QUERY_CHECK_CODE(code, lino, _error);
16,829,746✔
696
  }
697

698
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
34,820,767✔
699
  QUERY_CHECK_CODE(code, lino, _error);
34,794,826✔
700

701
  initResultSizeInfo(&pOperator->resultInfo, 4096);
34,794,826✔
702
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
34,856,916✔
703
  QUERY_CHECK_CODE(code, lino, _error);
34,894,368✔
704

705
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
34,894,368✔
706
  QUERY_CHECK_CODE(code, lino, _error);
34,842,561✔
707

708
  int32_t    num = 0;
34,842,561✔
709
  SExprInfo* pExprInfo = NULL;
34,843,870✔
710

711
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
34,861,488✔
712
  QUERY_CHECK_CODE(code, lino, _error);
34,848,997✔
713

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

718
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
34,843,830✔
719
                            pTaskInfo->pStreamRuntimeInfo);
34,852,080✔
720
  QUERY_CHECK_CODE(code, lino, _error);
34,832,888✔
721

722
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
34,832,888✔
723
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
34,788,706✔
724

725
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
34,852,656✔
726
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
34,858,908✔
727
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
34,847,591✔
728

729
  pInfo->pOperator = pOperator;
34,833,135✔
730

731
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
34,823,444✔
732
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
733
  setOperatorResetStateFn(pOperator, resetGroupOperState);
34,830,216✔
734
  code = appendDownstream(pOperator, &downstream, 1);
34,808,689✔
735
  QUERY_CHECK_CODE(code, lino, _error);
34,802,380✔
736

737
  *pOptrInfo = pOperator;
34,802,380✔
738
  return TSDB_CODE_SUCCESS;
34,812,197✔
739

740
_error:
101,369✔
741
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
101,369✔
742
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
101,369✔
743
  pTaskInfo->code = code;
101,369✔
744
  return code;
101,369✔
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) {
27,853,566✔
805
  int32_t                 code = TSDB_CODE_SUCCESS;
27,853,566✔
806
  int32_t                 lino = 0;
27,853,566✔
807
  SPartitionOperatorInfo* pInfo = pOperator->info;
27,853,566✔
808
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
27,861,349✔
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);
86,790,953✔
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);
27,750✔
857
            char*   src = colDataGetData(pSrcColInfoData, j);
28,192✔
858
            int32_t dataLen = getJsonValueLen(src);
27,750✔
859

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

864
            contentLen = dataLen;
28,192✔
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:
29,103,600✔
931
  if (code != TSDB_CODE_SUCCESS) {
27,872,701✔
932
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
933
    T_LONG_JMP(pTaskInfo->env, code);
×
934
  }
935
}
27,872,701✔
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};
86,805,944✔
945
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
86,805,944✔
946
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
86,784,005✔
947

948
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
86,784,005✔
949
    if (code == TSDB_CODE_DUP_KEY) {
86,807,300✔
950
      code = TSDB_CODE_SUCCESS;
×
951
    }
952
    QUERY_CHECK_CODE(code, lino, _end);
86,807,300✔
953

954
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
86,807,300✔
955

956
    int32_t pageId = 0;
86,815,515✔
957
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
86,814,977✔
958
    if (pPage == NULL) {
86,806,976✔
959
      return pPage;
×
960
    }
961

962
    void* tmp = taosArrayPush(p->pPageList, &pageId);
86,806,976✔
963
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
86,813,322✔
964

965
    *(int32_t*)pPage = 0;
86,813,322✔
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);
791,570,627✔
978

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

987
      void* tmp = taosArrayPush(p->pPageList, &pageId);
791,586,701✔
988
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
791,582,397✔
989

990
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
791,582,397✔
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) {
4,324,585✔
1006
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
4,324,585✔
1007
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
4,326,197✔
1008
  if (!offset) {
4,319,030✔
1009
    return NULL;
×
1010
  }
1011

1012
  offset[0] = sizeof(int32_t) +
4,319,030✔
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) {
14,756,517✔
1016
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
10,420,117✔
1017

1018
    int32_t payloadLen = blockDataGetPagedColumnReservedBytes(pColInfoData) * rowCapacity;
10,411,279✔
1019

1020
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
10,413,046✔
1021
      // offset segment + content length + payload
1022
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
2,815,956✔
1023
    } else {
1024
      // bitmap + content length + payload
1025
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
7,622,270✔
1026
    }
1027
  }
1028

1029
  return offset;
4,336,400✔
1030
}
1031

1032
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
4,196,969✔
1033
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
4,196,969✔
1034
  for (int32_t i = 0; i < size; i++) {
54,945,341✔
1035
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
50,748,369✔
1036
    if (pGp && pGp->blockForNotLoaded) {
50,755,423✔
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);
50,755,420✔
1045
  }
1046
  taosArrayClear(pInfo->sortedGroupArray);
4,196,972✔
1047
  clearDiskbasedBuf(pInfo->pBuf);
4,197,853✔
1048
}
4,195,652✔
1049

1050
static int compareDataGroupInfo(const void* group1, const void* group2) {
769,385,362✔
1051
  const SDataGroupInfo* pGroupInfo1 = group1;
769,385,362✔
1052
  const SDataGroupInfo* pGroupInfo2 = group2;
769,385,362✔
1053

1054
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
769,385,362✔
1055
    return 0;
×
1056
  }
1057

1058
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
769,393,745✔
1059
}
1060

1061
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
51,547,858✔
1062
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
51,547,858✔
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;
51,546,093✔
1071
}
1072

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

1079
  if (pInfo->remainRows == 0) {
850,328,421✔
1080
    blockDataCleanup(pInfo->binfo.pRes);
821,277,338✔
1081
    SDataGroupInfo* pGroupInfo =
821,276,265✔
1082
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
821,274,399✔
1083
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
821,276,265✔
1084
      if (pGroupInfo != NULL) {
55,866,999✔
1085
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
51,550,946✔
1086
        if (ret != NULL) return ret;
51,544,770✔
1087
      }
1088
      // try next group data
1089
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
55,860,823✔
1090
        setOperatorCompleted(pOperator);
4,199,173✔
1091
        clearPartitionOperator(pInfo);
4,200,938✔
1092
        return NULL;
4,196,533✔
1093
      }
1094
      ++pInfo->groupIndex;
51,665,173✔
1095

1096
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
51,666,994✔
1097
      if (pGroupInfo == NULL) {
51,665,674✔
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;
51,665,674✔
1102
    }
1103

1104
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
817,073,115✔
1105
    if (pageId == NULL) {
817,063,055✔
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);
817,063,055✔
1110
    if (page == NULL) {
817,059,347✔
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) {
817,059,347✔
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);
817,058,463✔
1130
    QUERY_CHECK_CODE(code, lino, _end);
817,064,149✔
1131

1132
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
817,064,149✔
1133
    QUERY_CHECK_CODE(code, lino, _end);
817,082,515✔
1134

1135
    pInfo->pageIndex += 1;
817,082,515✔
1136
    releaseBufPage(pInfo->pBuf, page);
817,078,540✔
1137
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
817,066,033✔
1138
    pInfo->binfo.pRes->info.dataLoad = 1;
817,069,419✔
1139
    pInfo->orderedRows = 0;
817,065,679✔
1140
  } else if (pInfo->pOrderInfoArr == NULL) {
29,052,057✔
1141
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1142
  }
1143

1144
  if (pInfo->pOrderInfoArr) {
846,116,262✔
1145
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
86,860,028✔
1146
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
86,858,952✔
1147
    QUERY_CHECK_CODE(code, lino, _end);
86,857,338✔
1148
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
86,857,338✔
1149
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
86,862,180✔
1150
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
86,862,180✔
1151
  }
1152

1153
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
846,124,139✔
1154
  QUERY_CHECK_CODE(code, lino, _end);
846,114,587✔
1155

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

1165
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
850,350,505✔
1166
  if (pOperator->status == OP_EXEC_DONE) {
850,350,505✔
1167
    (*ppRes) = NULL;
14,081✔
1168
    return TSDB_CODE_SUCCESS;
14,081✔
1169
  }
1170

1171
  int32_t                 code = TSDB_CODE_SUCCESS;
850,342,216✔
1172
  int32_t                 lino = 0;
850,342,216✔
1173
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
850,342,216✔
1174
  SPartitionOperatorInfo* pInfo = pOperator->info;
850,338,642✔
1175
  SSDataBlock*            pRes = pInfo->binfo.pRes;
850,344,247✔
1176

1177
  if (pOperator->status == OP_RES_TO_RETURN) {
850,340,110✔
1178
    (*ppRes) = buildPartitionResult(pOperator);
846,008,111✔
1179
    return code;
846,011,322✔
1180
  }
1181

1182
  while (1) {
27,875,443✔
1183
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
32,205,836✔
1184
    if (pBlock == NULL) {
32,164,841✔
1185
      break;
4,314,288✔
1186
    }
1187

1188
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
27,850,553✔
1189
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1190
    if (pInfo->scalarSup.pExprInfo != NULL) {
27,873,256✔
1191
      code =
1192
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
2,412,013✔
1193
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
1,205,482✔
1194
      QUERY_CHECK_CODE(code, lino, _end);
1,205,924✔
1195
    }
1196

1197
    terrno = TSDB_CODE_SUCCESS;
27,870,801✔
1198
    doHashPartition(pOperator, pBlock);
27,864,014✔
1199
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
27,871,087✔
1200
      code = terrno;
×
1201
      QUERY_CHECK_CODE(code, lino, _end);
1,076✔
1202
    }
1203
  }
1204

1205
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
4,314,288✔
1206
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
4,313,410✔
1207

1208
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
4,313,410✔
1209
  while (pGroupIter != NULL) {
91,130,207✔
1210
    SDataGroupInfo* pGroupInfo = pGroupIter;
86,814,163✔
1211
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
86,818,571✔
1212
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
86,818,571✔
1213
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
86,818,571✔
1214
  }
1215

1216
  taosArraySort(groupArray, compareDataGroupInfo);
4,316,044✔
1217
  pInfo->sortedGroupArray = groupArray;
4,314,276✔
1218
  pInfo->groupIndex = -1;
4,311,194✔
1219
  taosHashClear(pInfo->pGroupSet);
4,315,160✔
1220

1221
  pOperator->status = OP_RES_TO_RETURN;
4,316,486✔
1222
  code = blockDataEnsureCapacity(pRes, 4096);
4,316,486✔
1223
  QUERY_CHECK_CODE(code, lino, _end);
4,317,809✔
1224

1225
_end:
4,317,809✔
1226
  if (code != TSDB_CODE_SUCCESS) {
4,317,809✔
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);
4,317,809✔
1233
  return code;
4,310,319✔
1234
}
1235

1236
static void destroyPartitionOperatorInfo(void* param) {
4,356,161✔
1237
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
4,356,161✔
1238
  cleanupBasicInfo(&pInfo->binfo);
4,356,161✔
1239
  taosArrayDestroy(pInfo->pGroupCols);
4,359,421✔
1240

1241
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
9,603,521✔
1242
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
5,245,865✔
1243
    taosMemoryFree(key.pData);
5,247,353✔
1244
  }
1245

1246
  taosArrayDestroy(pInfo->pGroupColVals);
4,357,656✔
1247
  taosMemoryFree(pInfo->keyBuf);
4,358,101✔
1248

1249
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
4,357,052✔
1250
  for (int32_t i = 0; i < size; i++) {
40,414,923✔
1251
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
36,060,959✔
1252
    if (pGp) {
36,060,959✔
1253
      taosArrayDestroy(pGp->pPageList);
36,060,959✔
1254
    }
1255
  }
1256
  taosArrayDestroy(pInfo->sortedGroupArray);
4,353,964✔
1257

1258
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
4,353,693✔
1259
  while (pGroupIter != NULL) {
4,351,324✔
1260
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1261
    taosArrayDestroy(pGroupInfo->pPageList);
×
1262
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1263
  }
1264

1265
  taosHashCleanup(pInfo->pGroupSet);
4,351,324✔
1266
  taosMemoryFree(pInfo->columnOffset);
4,357,211✔
1267

1268
  cleanupExprSupp(&pInfo->scalarSup);
4,355,278✔
1269
  destroyDiskbasedBuf(pInfo->pBuf);
4,359,863✔
1270
  taosArrayDestroy(pInfo->pOrderInfoArr);
4,352,367✔
1271
  taosMemoryFreeClear(param);
4,351,483✔
1272
}
4,354,742✔
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,
4,347,867✔
1311
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1312
  QRY_PARAM_CHECK(pOptrInfo);
4,347,867✔
1313

1314
  int32_t                 code = TSDB_CODE_SUCCESS;
4,349,873✔
1315
  int32_t                 lino = 0;
4,349,873✔
1316
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
4,349,873✔
1317
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
4,327,378✔
1318
  if (pInfo == NULL || pOperator == NULL) {
4,336,592✔
1319
    pTaskInfo->code = code = terrno;
13✔
1320
    goto _error;
×
1321
  }
1322
  initOperatorCostInfo(pOperator);
4,337,021✔
1323

1324
  pOperator->pPhyNode = pPartNode;
4,355,077✔
1325
  int32_t    numOfCols = 0;
4,353,741✔
1326
  SExprInfo* pExprInfo = NULL;
4,354,767✔
1327
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
4,349,458✔
1328
  QUERY_CHECK_CODE(code, lino, _error);
4,353,559✔
1329
  pOperator->exprSupp.numOfExprs = numOfCols;
4,353,559✔
1330
  pOperator->exprSupp.pExprInfo = pExprInfo;
4,354,321✔
1331

1332
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
4,351,499✔
1333

1334
  if (pPartNode->needBlockOutputTsOrder) {
4,345,081✔
1335
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
299,690✔
1336
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
297,193✔
1337
    if (!pInfo->pOrderInfoArr) {
298,417✔
1338
      pTaskInfo->code = terrno;
×
1339
      goto _error;
×
1340
    }
1341

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

1346
  if (pPartNode->pExprs != NULL) {
4,349,504✔
1347
    int32_t    num = 0;
252,862✔
1348
    SExprInfo* pExprInfo1 = NULL;
252,255✔
1349
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
252,862✔
1350
    QUERY_CHECK_CODE(code, lino, _error);
252,862✔
1351

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

1356
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
4,327,272✔
1357
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
4,321,096✔
1358
  if (pInfo->pGroupSet == NULL) {
4,338,889✔
1359
    goto _error;
×
1360
  }
1361

1362
  uint32_t defaultPgsz = 0;
4,336,302✔
1363
  int64_t  defaultBufsz = 0;
4,337,866✔
1364

1365
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
4,336,398✔
1366
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
4,339,054✔
1367
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
4,334,333✔
1368
  if (code != TSDB_CODE_SUCCESS) {
4,328,400✔
1369
    goto _error;
×
1370
  }
1371

1372
  if (!osTempSpaceAvailable()) {
4,328,400✔
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);
4,329,132✔
1379
  if (code != TSDB_CODE_SUCCESS) {
4,322,265✔
1380
    goto _error;
×
1381
  }
1382

1383
  pInfo->rowCapacity =
4,318,560✔
1384
      getPartitionPageRowCapacity(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
4,322,675✔
1385
                                  blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
4,322,265✔
1386
  if (pInfo->rowCapacity < 0) {
4,316,639✔
1387
    code = terrno;
×
1388
    goto _error;
×
1389
  }
1390

1391
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
4,308,151✔
1392
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
4,332,149✔
1393

1394
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
4,315,068✔
1395
  if (code != TSDB_CODE_SUCCESS) {
4,309,372✔
1396
    goto _error;
×
1397
  }
1398

1399
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
4,309,372✔
1400
                  pTaskInfo);
1401

1402
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
4,320,433✔
1403
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1404

1405
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
4,310,945✔
1406
  code = appendDownstream(pOperator, &downstream, 1);
4,321,474✔
1407
  if (code != TSDB_CODE_SUCCESS) {
4,313,593✔
1408
    goto _error;
×
1409
  }
1410

1411
  *pOptrInfo = pOperator;
4,313,593✔
1412
  return TSDB_CODE_SUCCESS;
4,314,270✔
1413

1414
_error:
21,670✔
1415
  if (pInfo != NULL) {
21,245✔
1416
    destroyPartitionOperatorInfo(pInfo);
21,245✔
1417
  }
1418
  pTaskInfo->code = code;
21,245✔
1419
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
21,245✔
1420
  TAOS_RETURN(code);
21,245✔
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,996,688✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
34,996,688✔
1445
  int32_t lino = 0;
34,996,688✔
1446
  size_t  numOfCols = LIST_LENGTH(pNodeList);
34,996,688✔
1447
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
35,003,011✔
1448
  if (pList == NULL) {
34,987,202✔
1449
    code = terrno;
×
1450
    (*pArrayRes) = NULL;
×
1451
    QUERY_CHECK_CODE(code, lino, _end);
6,147✔
1452
  }
1453

1454
  for (int32_t i = 0; i < numOfCols; ++i) {
91,530,633✔
1455
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
56,582,222✔
1456
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
56,564,669✔
1457

1458
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
56,564,669✔
1459
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
56,592,906✔
1460

1461
      SColumn c = extractColumnFromColumnNode(pColNode);
56,588,637✔
1462
      void*   tmp = taosArrayPush(pList, &c);
56,601,422✔
1463
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
56,601,422✔
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,948,411✔
1480

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