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

taosdata / TDengine / #4923

09 Jan 2026 08:13AM UTC coverage: 65.373% (+0.2%) from 65.161%
#4923

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3042 existing lines in 131 files now uncovered.

198273 of 303297 relevant lines covered (65.37%)

118980690.73 hits per line

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

75.98
/source/libs/executor/src/groupoperator.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "filter.h"
17
#include "function.h"
18
#include "os.h"
19
#include "query.h"
20
#include "tname.h"
21
#include "tutil.h"
22

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

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

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

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

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

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

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

75
static void freeGroupKey(void* param) {
63,982,266✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
63,982,266✔
77
  taosMemoryFree(pKey->pData);
63,982,266✔
78
}
63,983,305✔
79

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

86
  cleanupBasicInfo(&pInfo->binfo);
41,564,417✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
41,565,486✔
88
  taosArrayDestroy(pInfo->pGroupCols);
41,563,274✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
41,564,855✔
90
  cleanupExprSupp(&pInfo->scalarSup);
41,564,404✔
91

92
  if (pInfo->pOperator != NULL) {
41,566,418✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
40,698,278✔
94
                      false);
95
    pInfo->pOperator = NULL;
40,698,756✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
41,565,827✔
99
  cleanupAggSup(&pInfo->aggSup);
41,563,343✔
100
  taosMemoryFreeClear(param);
41,564,456✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
47,611,702✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
47,611,702✔
105
  if ((*pGroupColVals) == NULL) {
47,614,751✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
47,610,973✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
120,298,771✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
72,682,113✔
112
    if (!pCol) {
72,686,784✔
113
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
114
      return terrno;
×
115
    }
116
    (*keyLen) += pCol->bytes;  // actual data + null_flag
72,686,784✔
117

118
    SGroupKeys key = {0};
72,686,516✔
119
    key.bytes = pCol->bytes;
72,686,997✔
120
    key.type = pCol->type;
72,683,943✔
121
    key.isNull = false;
72,681,757✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
72,681,757✔
123
    if (key.pData == NULL) {
72,679,119✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
72,679,119✔
128
    if (!tmp) {
72,687,471✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
47,616,658✔
134
  (*keyLen) += nullFlagSize;
47,616,658✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
47,614,998✔
137
  if ((*keyBuf) == NULL) {
47,608,327✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
47,608,171✔
142
}
143

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

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

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

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

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

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

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

198
  return true;
1,585,504,290✔
199
}
200

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

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

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

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

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

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

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

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

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

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

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

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

291
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
292
          int32_t dataLen = getJsonValueLen(data);
14,382✔
293
          memcpy(dest, data, dataLen);
14,382✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,512,018,444✔
296
            blobDataCopy(dest, data);
86✔
297
          } else {
298
            varDataCopy(dest, data);
1,514,345,061✔
299
          }
300
        } else {
301
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
302
        }
303
      } else {  // it is a NULL value
304
        pEntryInfo->isNullRes = 1;
1,168,902,770✔
305
      }
306

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

312
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
242,163,696✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
242,163,696✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
242,196,940✔
315

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

323
  int32_t len = 0;
242,202,806✔
324
  terrno = TSDB_CODE_SUCCESS;
242,202,806✔
325

326
  int32_t num = 0;
242,187,937✔
327
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
328
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
329
    if (!pInfo->isInit) {
2,147,483,647✔
330
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
28,760,966✔
331
      pInfo->isInit = true;
28,757,464✔
332
      num++;
28,757,779✔
333
      continue;
28,757,779✔
334
    }
335

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

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

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

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

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

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

378
    int32_t rowIndex = pBlock->info.rows - num;
242,174,417✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
242,172,471✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
242,160,435✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
242,160,435✔
385
  }
386
}
242,164,350✔
387

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

394
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
1,874,560,887✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,874,560,887✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
1,874,568,015✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,874,569,362✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
1,874,570,053✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
1,874,569,529✔
404

405
  blockDataCleanup(pBlock);
1,874,563,651✔
406
  if (!hasRemainResultByHash(pOperator)) {
1,874,558,588✔
407
    return;
11,617,840✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
1,862,955,431✔
411
  if (!pInfo->binfo.mergeResultBlock) {
1,862,956,498✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
1,840,015,292✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
45,870,070✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
22,940,691✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
22,940,691✔
419
        break;
11,312✔
420
      }
421
      pBlock->info.id.groupId = 0;
22,929,379✔
422
    }
423

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

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
1,834,212,022✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
1,834,212,022✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
57,049✔
432
    return true;  // limit reached, stop processing further rows
21,061✔
433
  }
434
  return false;
1,834,192,719✔
435
}
436

437
static int32_t doGroupResultSlimit(SSDataBlock* pRes, SLimitInfo* pLimitInfo) {
1,874,574,849✔
438
  int32_t code = TSDB_CODE_SUCCESS;
1,874,574,849✔
439
  int32_t lino = 0;
1,874,574,849✔
440

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
1,874,574,849✔
442
    return TSDB_CODE_SUCCESS;
12,151,088✔
443
  }
444

445
  if (pLimitInfo && pLimitInfo->remainGroupOffset > 0) {
1,862,425,971✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
63,744✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
9,829✔
448
      blockDataCleanup(pRes);
9,829✔
449
    } else {
450
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
53,915✔
451
      QUERY_CHECK_CODE(code, lino, _end);
53,915✔
452
    }
453
  }
454

455
  pLimitInfo->remainGroupOffset = 0;
1,862,425,971✔
456
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
1,862,425,285✔
457
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
173,335✔
458
    if (pRes->info.rows > remainRows) {
173,335✔
459
      blockDataKeepFirstNRows(pRes, remainRows);
84,110✔
460
    }
461
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
173,335✔
462
  }
463

464
_end:
1,862,252,976✔
465
  if (code != TSDB_CODE_SUCCESS) {
1,862,426,311✔
466
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
467
  }
468
  return code;
1,862,421,710✔
469
}
470

471
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
1,874,554,997✔
472
  int32_t               code = TSDB_CODE_SUCCESS;
1,874,554,997✔
473
  int32_t               lino = 0;
1,874,554,997✔
474
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,874,554,997✔
475
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,874,568,563✔
476
  SSDataBlock*          pRes = pInfo->binfo.pRes;
1,874,568,983✔
477
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
1,874,567,205✔
478

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

483
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
1,874,576,021✔
484
    QUERY_CHECK_CODE(code, lino, _end);
1,874,577,399✔
485

486
    code = doGroupResultSlimit(pRes, pLimitInfo);
1,874,577,399✔
487
    QUERY_CHECK_CODE(code, lino, _end);
1,874,575,460✔
488

489
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
1,874,575,460✔
490
      setOperatorCompleted(pOperator);
40,383,302✔
491
      // clean hash after completed
492
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
40,384,575✔
493
      pInfo->aggSup.pResultRowHashTable = NULL;
40,384,575✔
494
      break;
40,384,575✔
495
    }
496

497
    if (pRes->info.rows > 0) {
1,834,191,681✔
498
      break;
1,834,192,786✔
499
    }
500
  }
501

502
  pOperator->resultInfo.totalRows += pRes->info.rows;
1,874,577,361✔
503

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

512
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,902,837,664✔
513
  int32_t               code = TSDB_CODE_SUCCESS;
1,902,837,664✔
514
  int32_t               lino = 0;
1,902,837,664✔
515
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,902,837,664✔
516
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,902,845,456✔
517
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
1,902,846,627✔
518
  int32_t               order = pInfo->binfo.inputTsOrder;
1,902,847,626✔
519
  int64_t               st = taosGetTimestampUs();
1,902,842,119✔
520

521
  QRY_PARAM_CHECK(ppRes);
1,902,842,119✔
522
  if (pOperator->status == OP_EXEC_DONE) {
1,902,838,812✔
523
    return code;
28,009,287✔
524
  }
525

526
  if (pOperator->status == OP_RES_TO_RETURN) {
1,874,833,965✔
527
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
1,834,183,432✔
528
    return code;
1,834,191,272✔
529
  }
530

531
  while (1) {
242,152,239✔
532
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
282,804,489✔
533
    if (pBlock == NULL) {
282,821,739✔
534
      break;
40,386,147✔
535
    }
536

537
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
242,435,592✔
538

539
    // the pDataBlock are always the same one, no need to call this again
540
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
242,470,338✔
541
    QUERY_CHECK_CODE(code, lino, _end);
242,444,882✔
542

543
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
544
    if (pInfo->scalarSup.pExprInfo != NULL) {
242,444,882✔
545
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
44,458,498✔
546
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
44,465,317✔
547
      QUERY_CHECK_CODE(code, lino, _end);
44,445,644✔
548
    }
549

550
    doHashGroupbyAgg(pOperator, pBlock);
242,163,187✔
551
  }
552

553
  pOperator->status = OP_RES_TO_RETURN;
40,386,147✔
554

555
  // initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
556
  if (pGroupResInfo->pRows != NULL) {
40,386,147✔
557
    taosArrayDestroy(pGroupResInfo->pRows);
×
558
  }
559

560
  if (pGroupResInfo->pBuf) {
40,383,773✔
561
    taosMemoryFree(pGroupResInfo->pBuf);
×
562
    pGroupResInfo->pBuf = NULL;
×
563
  }
564

565
  pGroupResInfo->index = 0;
40,383,603✔
566
  pGroupResInfo->iter = 0;
40,383,438✔
567
  pGroupResInfo->dataPos = NULL;
40,382,672✔
568

569
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
40,383,778✔
570

571
_end:
40,649,846✔
572
  if (code != TSDB_CODE_SUCCESS) {
40,649,846✔
573
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
265,825✔
574
    pTaskInfo->code = code;
265,825✔
575
    T_LONG_JMP(pTaskInfo->env, code);
265,825✔
576
  } else {
577
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
40,384,021✔
578
  }
579

580
  return code;
40,386,147✔
581
}
582

583
static int32_t resetGroupOperState(SOperatorInfo* pOper) {
×
584
  SGroupbyOperatorInfo* pInfo = pOper->info;
×
585
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
586
  SAggPhysiNode* pPhynode = (SAggPhysiNode*)pOper->pPhyNode;
×
587
  resetBasicOperatorState(&pInfo->binfo);
×
588
  pOper->status = OP_NOT_OPENED;
×
589

590
  cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
×
591
    false);
592

593
  cleanupGroupResInfo(&pInfo->groupResInfo);
×
594

595
  qInfo("[group key] len use:%d", pInfo->groupKeyLen);
×
596
  int32_t code = resetAggSup(&pOper->exprSupp, &pInfo->aggSup, pTaskInfo, pPhynode->pAggFuncs, pPhynode->pGroupKeys,
×
597
    pInfo->groupKeyLen + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
×
598
    &pTaskInfo->storageAPI.functionStore);
599

600
  if (code == 0){
×
601
    code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
602
      &pTaskInfo->storageAPI.functionStore);
603
  }
604

605
  pInfo->isInit = false;
×
606

607
  return code;
×
608
}
609

610
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
41,553,948✔
611
                                SOperatorInfo** pOptrInfo) {
612
  QRY_PARAM_CHECK(pOptrInfo);
41,553,948✔
613

614
  int32_t               code = TSDB_CODE_SUCCESS;
41,558,495✔
615
  int32_t               lino = 0;
41,558,495✔
616
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
41,558,495✔
617
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
41,550,559✔
618
  if (pInfo == NULL || pOperator == NULL) {
41,548,551✔
619
    code = terrno;
728✔
620
    goto _error;
×
621
  }
622

623
  pOperator->pPhyNode = (SNode*)pAggNode;
41,547,875✔
624
  pOperator->exprSupp.hasWindowOrGroup = true;
41,550,130✔
625
  pOperator->exprSupp.hasWindow = false;
41,549,756✔
626

627
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
41,558,236✔
628
  if (pResBlock == NULL) {
41,562,810✔
629
    code = terrno;
×
630
    goto _error;
×
631
  }
632
  initBasicInfo(&pInfo->binfo, pResBlock);
41,562,810✔
633

634
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
41,561,150✔
635

636
  pInfo->pGroupCols = NULL;
41,559,988✔
637
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
41,559,086✔
638
  QUERY_CHECK_CODE(code, lino, _error);
41,551,937✔
639

640
  int32_t    numOfScalarExpr = 0;
41,551,937✔
641
  SExprInfo* pScalarExprInfo = NULL;
41,555,977✔
642
  if (pAggNode->pExprs != NULL) {
41,560,183✔
643
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
19,780,620✔
644
    QUERY_CHECK_CODE(code, lino, _error);
19,775,183✔
645
  }
646

647
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
40,689,993✔
648
  QUERY_CHECK_CODE(code, lino, _error);
40,685,131✔
649

650
  initResultSizeInfo(&pOperator->resultInfo, 4096);
40,685,131✔
651
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
40,691,595✔
652
  QUERY_CHECK_CODE(code, lino, _error);
40,693,988✔
653

654
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
40,693,988✔
655
  QUERY_CHECK_CODE(code, lino, _error);
40,686,890✔
656

657
  int32_t    num = 0;
40,686,890✔
658
  SExprInfo* pExprInfo = NULL;
40,689,651✔
659

660
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
40,690,522✔
661
  QUERY_CHECK_CODE(code, lino, _error);
40,691,455✔
662

663
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
81,386,061✔
664
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
40,691,478✔
665
  QUERY_CHECK_CODE(code, lino, _error);
40,684,959✔
666

667
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
40,692,835✔
668
                            pTaskInfo->pStreamRuntimeInfo);
40,684,959✔
669
  QUERY_CHECK_CODE(code, lino, _error);
40,686,964✔
670

671
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
40,686,964✔
672
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
40,690,687✔
673

674
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
40,693,785✔
675
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
40,687,276✔
676
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
40,688,979✔
677

678
  pInfo->pOperator = pOperator;
40,680,754✔
679

680
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
40,690,657✔
681
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
682
  setOperatorResetStateFn(pOperator, resetGroupOperState);
40,676,936✔
683
  code = appendDownstream(pOperator, &downstream, 1);
40,682,408✔
684
  QUERY_CHECK_CODE(code, lino, _error);
40,690,702✔
685

686
  *pOptrInfo = pOperator;
40,690,702✔
687
  return TSDB_CODE_SUCCESS;
40,686,228✔
688

689
_error:
866,623✔
690
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
866,623✔
691
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
866,623✔
692
  pTaskInfo->code = code;
866,623✔
693
  return code;
866,623✔
694
}
695

696
SSDataBlock* createBlockDataNotLoaded(const SOperatorInfo* pOperator, SSDataBlock* pDataBlock) {
×
697
  int32_t code = TSDB_CODE_SUCCESS;
×
698
  int32_t lino = 0;
×
699
  if (pDataBlock == NULL) {
×
700
    return NULL;
×
701
  }
702

703
  SSDataBlock* pDstBlock = NULL;
×
704
  code = createDataBlock(&pDstBlock);
×
705
  QUERY_CHECK_CODE(code, lino, _end);
×
706

707
  pDstBlock->info = pDataBlock->info;
×
708
  pDstBlock->info.id.blockId = pOperator->resultDataBlockId;
×
709
  pDstBlock->info.capacity = 0;
×
710
  pDstBlock->info.rowSize = 0;
×
711

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

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

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

739
      code = colDataAssign(pDst, pSrc, pDataBlock->info.rows, &pDataBlock->info);
×
740
      QUERY_CHECK_CODE(code, lino, _end);
×
741
    }
742
  }
743

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

753
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
19,993,555✔
754
  int32_t                 code = TSDB_CODE_SUCCESS;
19,993,555✔
755
  int32_t                 lino = 0;
19,993,555✔
756
  SPartitionOperatorInfo* pInfo = pOperator->info;
19,993,555✔
757
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
19,995,320✔
758

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

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

769
    pGroupInfo->numOfRows += 1;
2,147,483,647✔
770

771
    // group id
772
    if (pGroupInfo->groupId == 0) {
2,147,483,647✔
773
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
14,596,183✔
774
    }
775

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

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

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

787
        int32_t bytes = pColInfoData->info.bytes;
2,147,483,647✔
788
        int32_t startOffset = pInfo->columnOffset[i];
2,147,483,647✔
789

790
        int32_t* columnLen = NULL;
2,147,483,647✔
791
        int32_t  contentLen = 0;
2,147,483,647✔
792

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

798
          if (colDataIsNull_s(pColInfoData, j)) {
2,147,483,647✔
799
            offset[(*rows)] = -1;
316,203,232✔
800
            contentLen = 0;
316,884,785✔
801
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
802
            offset[*rows] = (*columnLen);
22,368✔
803
            char*   src = colDataGetData(pColInfoData, j);
22,368✔
804
            int32_t dataLen = getJsonValueLen(src);
22,368✔
805

806
            memcpy(data + (*columnLen), src, dataLen);
22,368✔
807
            int32_t v = (data + (*columnLen) + dataLen - (char*)pPage);
22,368✔
808
            QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
22,368✔
809

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

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

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

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

846
        (*columnLen) += contentLen;
2,147,483,647✔
847
      }
848

849
      (*rows) += 1;
2,147,483,647✔
850

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

871
_end:
21,817,756✔
872
  if (code != TSDB_CODE_SUCCESS) {
19,997,647✔
873
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
874
    T_LONG_JMP(pTaskInfo->env, code);
×
875
  }
876
}
19,997,647✔
877

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

883
  void* pPage = NULL;
2,147,483,647✔
884
  if (p == NULL) {  // it is a new group
2,147,483,647✔
885
    SDataGroupInfo gi = {0};
14,595,269✔
886
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
14,595,269✔
887
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
14,594,216✔
888

889
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
14,594,216✔
890
    if (code == TSDB_CODE_DUP_KEY) {
14,600,330✔
891
      code = TSDB_CODE_SUCCESS;
×
892
    }
893
    QUERY_CHECK_CODE(code, lino, _end);
14,600,330✔
894

895
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
14,600,330✔
896

897
    int32_t pageId = 0;
14,598,510✔
898
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
14,598,510✔
899
    if (pPage == NULL) {
14,597,093✔
900
      return pPage;
×
901
    }
902

903
    void* tmp = taosArrayPush(p->pPageList, &pageId);
14,597,093✔
904
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
14,596,346✔
905

906
    *(int32_t*)pPage = 0;
14,596,346✔
907
  } else {
908
    int32_t* curId = taosArrayGetLast(p->pPageList);
2,147,483,647✔
909
    pPage = getBufPage(pInfo->pBuf, *curId);
2,147,483,647✔
910
    if (pPage == NULL) {
2,147,483,647✔
911
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
912
      return pPage;
×
913
    }
914

915
    int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
916
    if (*rows >= pInfo->rowCapacity) {
2,147,483,647✔
917
      // release buffer
918
      releaseBufPage(pInfo->pBuf, pPage);
124,283,429✔
919

920
      // add a new page for current group
921
      int32_t pageId = 0;
124,276,472✔
922
      pPage = getNewBufPage(pInfo->pBuf, &pageId);
124,277,837✔
923
      if (pPage == NULL) {
124,274,448✔
924
        qError("failed to get new buffer, code:%s", tstrerror(terrno));
×
925
        return NULL;
×
926
      }
927

928
      void* tmp = taosArrayPush(p->pPageList, &pageId);
124,274,448✔
929
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
124,270,741✔
930

931
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
124,270,741✔
932
    }
933
  }
934

935
  *pGroupInfo = p;
2,147,483,647✔
936

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

943
  return pPage;
2,147,483,647✔
944
}
945

946
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
6,919,844✔
947
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
6,919,844✔
948
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
6,920,351✔
949
  if (!offset) {
6,920,706✔
950
    return NULL;
×
951
  }
952

953
  offset[0] = sizeof(int32_t) +
6,920,706✔
954
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
955

956
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
12,787,128✔
957
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
5,866,411✔
958

959
    int32_t bytes = pColInfoData->info.bytes;
5,866,766✔
960
    int32_t payloadLen = bytes * rowCapacity;
5,866,766✔
961

962
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
5,866,766✔
963
      // offset segment + content length + payload
964
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
715,285✔
965
    } else {
966
      // bitmap + content length + payload
967
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
5,150,388✔
968
    }
969
  }
970

971
  return offset;
6,920,717✔
972
}
973

974
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
6,123,923✔
975
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
6,123,923✔
976
  for (int32_t i = 0; i < size; i++) {
19,142,062✔
977
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
13,018,139✔
978
    if (pGp && pGp->blockForNotLoaded) {
13,018,139✔
979
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
980
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
981
        if (pBlock) blockDataDestroy(*pBlock);
×
982
      }
983
      taosArrayClear(pGp->blockForNotLoaded);
×
984
      pGp->offsetForNotLoaded = 0;
×
985
    }
986
    taosArrayDestroy(pGp->pPageList);
13,018,139✔
987
  }
988
  taosArrayClear(pInfo->sortedGroupArray);
6,123,923✔
989
  clearDiskbasedBuf(pInfo->pBuf);
6,123,923✔
990
}
6,123,923✔
991

992
static int compareDataGroupInfo(const void* group1, const void* group2) {
52,117,147✔
993
  const SDataGroupInfo* pGroupInfo1 = group1;
52,117,147✔
994
  const SDataGroupInfo* pGroupInfo2 = group2;
52,117,147✔
995

996
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
52,117,147✔
997
    return 0;
×
998
  }
999

1000
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
52,117,654✔
1001
}
1002

1003
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
13,069,672✔
1004
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
13,069,672✔
1005
    SSDataBlock** pBlock = taosArrayGet(pGroupInfo->blockForNotLoaded, pGroupInfo->offsetForNotLoaded);
×
1006
    if (!pBlock) {
×
1007
      return NULL;
×
1008
    }
1009
    pGroupInfo->offsetForNotLoaded++;
×
1010
    return *pBlock;
×
1011
  }
1012
  return NULL;
13,069,672✔
1013
}
1014

1015
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
156,552,888✔
1016
  int32_t                 code = TSDB_CODE_SUCCESS;
156,552,888✔
1017
  int32_t                 lino = 0;
156,552,888✔
1018
  SPartitionOperatorInfo* pInfo = pOperator->info;
156,552,888✔
1019
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
156,554,305✔
1020

1021
  if (pInfo->remainRows == 0) {
156,553,798✔
1022
    blockDataCleanup(pInfo->binfo.pRes);
134,041,930✔
1023
    SDataGroupInfo* pGroupInfo =
134,041,152✔
1024
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
134,038,424✔
1025
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
134,041,152✔
1026
      if (pGroupInfo != NULL) {
19,727,956✔
1027
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
13,070,179✔
1028
        if (ret != NULL) return ret;
13,069,672✔
1029
      }
1030
      // try next group data
1031
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
19,727,449✔
1032
        setOperatorCompleted(pOperator);
6,123,923✔
1033
        clearPartitionOperator(pInfo);
6,123,923✔
1034
        return NULL;
6,123,923✔
1035
      }
1036
      ++pInfo->groupIndex;
13,603,926✔
1037

1038
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
13,604,433✔
1039
      if (pGroupInfo == NULL) {
13,604,433✔
1040
        qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1041
        T_LONG_JMP(pTaskInfo->env, terrno);
×
1042
      }
1043
      pInfo->pageIndex = 0;
13,604,433✔
1044
    }
1045

1046
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
127,915,789✔
1047
    if (pageId == NULL) {
127,914,879✔
1048
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1049
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1050
    }
1051
    void* page = getBufPage(pInfo->pBuf, *pageId);
127,914,879✔
1052
    if (page == NULL) {
127,913,493✔
1053
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1054
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1055
    }
1056
    if (*(int32_t*)page == 0) {
127,913,493✔
1057
      releaseBufPage(pInfo->pBuf, page);
×
1058
      SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
×
1059
      if (ret != NULL) return ret;
×
1060
      if (pInfo->groupIndex + 1 < taosArrayGetSize(pInfo->sortedGroupArray)) {
×
1061
        pInfo->groupIndex++;
×
1062
        pInfo->pageIndex = 0;
×
1063
      } else {
1064
        setOperatorCompleted(pOperator);
×
1065
        clearPartitionOperator(pInfo);
×
1066
        return NULL;
×
1067
      }
1068
      return buildPartitionResult(pOperator);
×
1069
    }
1070

1071
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
127,914,403✔
1072
    QUERY_CHECK_CODE(code, lino, _end);
127,916,623✔
1073

1074
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
127,916,623✔
1075
    QUERY_CHECK_CODE(code, lino, _end);
127,920,737✔
1076

1077
    pInfo->pageIndex += 1;
127,920,737✔
1078
    releaseBufPage(pInfo->pBuf, page);
127,920,737✔
1079
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
127,915,690✔
1080
    pInfo->binfo.pRes->info.dataLoad = 1;
127,915,690✔
1081
    pInfo->orderedRows = 0;
127,914,841✔
1082
  } else if (pInfo->pOrderInfoArr == NULL) {
22,512,375✔
1083
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1084
  }
1085

1086
  if (pInfo->pOrderInfoArr) {
150,426,761✔
1087
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
68,235,824✔
1088
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
68,236,279✔
1089
    QUERY_CHECK_CODE(code, lino, _end);
68,236,279✔
1090
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
68,236,279✔
1091
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
68,237,076✔
1092
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
68,237,076✔
1093
  }
1094

1095
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
150,429,399✔
1096
  QUERY_CHECK_CODE(code, lino, _end);
150,429,965✔
1097

1098
_end:
150,429,965✔
1099
  if (code != TSDB_CODE_SUCCESS) {
150,429,965✔
1100
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1101
    T_LONG_JMP(pTaskInfo->env, code);
×
1102
  }
1103

1104
  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
150,429,965✔
1105
  return pInfo->binfo.pRes;
150,430,441✔
1106
}
1107

1108
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
156,817,957✔
1109
  if (pOperator->status == OP_EXEC_DONE) {
156,817,957✔
1110
    (*ppRes) = NULL;
372✔
1111
    return TSDB_CODE_SUCCESS;
372✔
1112
  }
1113

1114
  int32_t                 code = TSDB_CODE_SUCCESS;
156,817,074✔
1115
  int32_t                 lino = 0;
156,817,074✔
1116
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
156,817,074✔
1117
  SPartitionOperatorInfo* pInfo = pOperator->info;
156,817,130✔
1118
  SSDataBlock*            pRes = pInfo->binfo.pRes;
156,817,074✔
1119

1120
  if (pOperator->status == OP_RES_TO_RETURN) {
156,816,331✔
1121
    (*ppRes) = buildPartitionResult(pOperator);
149,896,021✔
1122
    return code;
149,896,163✔
1123
  }
1124

1125
  int64_t        st = taosGetTimestampUs();
6,920,706✔
1126
  SOperatorInfo* downstream = pOperator->pDownstream[0];
6,920,706✔
1127

1128
  while (1) {
19,997,192✔
1129
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
26,917,898✔
1130
    if (pBlock == NULL) {
26,652,187✔
1131
      break;
6,657,777✔
1132
    }
1133

1134
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
19,994,410✔
1135
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1136
    if (pInfo->scalarSup.pExprInfo != NULL) {
19,998,102✔
1137
      code =
1138
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
598,735✔
1139
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
598,735✔
1140
      QUERY_CHECK_CODE(code, lino, _end);
598,735✔
1141
    }
1142

1143
    terrno = TSDB_CODE_SUCCESS;
19,998,102✔
1144
    doHashPartition(pOperator, pBlock);
19,995,320✔
1145
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
19,998,102✔
1146
      code = terrno;
×
1147
      QUERY_CHECK_CODE(code, lino, _end);
×
1148
    }
1149
  }
1150

1151
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
6,657,777✔
1152
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
6,657,777✔
1153

1154
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
6,657,777✔
1155
  while (pGroupIter != NULL) {
21,258,510✔
1156
    SDataGroupInfo* pGroupInfo = pGroupIter;
14,600,733✔
1157
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
14,601,240✔
1158
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
14,601,240✔
1159
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
14,601,240✔
1160
  }
1161

1162
  taosArraySort(groupArray, compareDataGroupInfo);
6,657,777✔
1163
  pInfo->sortedGroupArray = groupArray;
6,657,777✔
1164
  pInfo->groupIndex = -1;
6,657,270✔
1165
  taosHashClear(pInfo->pGroupSet);
6,657,270✔
1166

1167
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
6,657,777✔
1168

1169
  pOperator->status = OP_RES_TO_RETURN;
6,657,777✔
1170
  code = blockDataEnsureCapacity(pRes, 4096);
6,657,777✔
1171
  QUERY_CHECK_CODE(code, lino, _end);
6,657,777✔
1172

1173
_end:
6,657,777✔
1174
  if (code != TSDB_CODE_SUCCESS) {
6,657,777✔
1175
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1176
    pTaskInfo->code = code;
×
1177
    T_LONG_JMP(pTaskInfo->env, code);
×
1178
  }
1179

1180
  (*ppRes) = buildPartitionResult(pOperator);
6,657,777✔
1181
  return code;
6,657,270✔
1182
}
1183

1184
static void destroyPartitionOperatorInfo(void* param) {
7,191,467✔
1185
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
7,191,467✔
1186
  cleanupBasicInfo(&pInfo->binfo);
7,191,467✔
1187
  taosArrayDestroy(pInfo->pGroupCols);
7,191,811✔
1188

1189
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
15,900,614✔
1190
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
8,709,654✔
1191
    taosMemoryFree(key.pData);
8,709,654✔
1192
  }
1193

1194
  taosArrayDestroy(pInfo->pGroupColVals);
7,191,811✔
1195
  taosMemoryFree(pInfo->keyBuf);
7,191,304✔
1196

1197
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
7,191,811✔
1198
  for (int32_t i = 0; i < size; i++) {
8,774,568✔
1199
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
1,583,101✔
1200
    if (pGp) {
1,583,101✔
1201
      taosArrayDestroy(pGp->pPageList);
1,583,101✔
1202
    }
1203
  }
1204
  taosArrayDestroy(pInfo->sortedGroupArray);
7,191,467✔
1205

1206
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
7,191,811✔
1207
  while (pGroupIter != NULL) {
7,190,616✔
1208
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1209
    taosArrayDestroy(pGroupInfo->pPageList);
×
1210
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1211
  }
1212

1213
  taosHashCleanup(pInfo->pGroupSet);
7,190,616✔
1214
  taosMemoryFree(pInfo->columnOffset);
7,191,467✔
1215

1216
  cleanupExprSupp(&pInfo->scalarSup);
7,191,467✔
1217
  destroyDiskbasedBuf(pInfo->pBuf);
7,191,811✔
1218
  taosArrayDestroy(pInfo->pOrderInfoArr);
7,191,467✔
1219
  taosMemoryFreeClear(param);
7,191,467✔
1220
}
7,191,123✔
1221

1222
static int32_t resetPartitionOperState(SOperatorInfo* pOper) {
×
1223
  SPartitionOperatorInfo* pInfo = pOper->info;
×
1224
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
1225
  SPartitionPhysiNode* pPhynode = (SPartitionPhysiNode*)pOper->pPhyNode;
×
1226
  resetBasicOperatorState(&pInfo->binfo);
×
1227

1228
  int32_t code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
1229
    &pTaskInfo->storageAPI.functionStore);
1230

1231
  clearPartitionOperator(pInfo);
×
1232

1233
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
×
1234
  while (pGroupIter != NULL) {
×
1235
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1236
    taosArrayDestroy(pGroupInfo->pPageList);
×
1237
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1238
  }
1239
  taosHashClear(pInfo->pGroupSet);
×
1240

1241
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
×
1242
  for (int32_t i = 0; i < size; i++) {
×
1243
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
×
1244
    if (pGp) {
×
1245
      taosArrayDestroy(pGp->pPageList);
×
1246
    }
1247
  }
1248
  taosArrayDestroy(pInfo->sortedGroupArray);
×
1249
  pInfo->sortedGroupArray = NULL;
×
1250

1251
  pInfo->groupIndex = 0;
×
1252
  pInfo->pageIndex = 0;
×
1253
  pInfo->remainRows = 0;
×
1254
  pInfo->orderedRows = 0;
×
1255
  return 0;
×
1256
}
1257

1258
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
7,190,949✔
1259
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1260
  QRY_PARAM_CHECK(pOptrInfo);
7,190,949✔
1261

1262
  int32_t                 code = TSDB_CODE_SUCCESS;
7,191,456✔
1263
  int32_t                 lino = 0;
7,191,456✔
1264
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
7,191,456✔
1265
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
7,190,438✔
1266
  if (pInfo == NULL || pOperator == NULL) {
7,190,949✔
1267
    pTaskInfo->code = code = terrno;
×
1268
    goto _error;
×
1269
  }
1270

1271
  pOperator->pPhyNode = pPartNode;
7,190,949✔
1272
  int32_t    numOfCols = 0;
7,190,949✔
1273
  SExprInfo* pExprInfo = NULL;
7,191,456✔
1274
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
7,191,456✔
1275
  QUERY_CHECK_CODE(code, lino, _error);
7,191,811✔
1276
  pOperator->exprSupp.numOfExprs = numOfCols;
7,191,811✔
1277
  pOperator->exprSupp.pExprInfo = pExprInfo;
7,191,304✔
1278

1279
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
7,191,304✔
1280

1281
  if (pPartNode->needBlockOutputTsOrder) {
7,191,456✔
1282
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
1,397,580✔
1283
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
1,397,580✔
1284
    if (!pInfo->pOrderInfoArr) {
1,397,580✔
1285
      pTaskInfo->code = terrno;
×
1286
      goto _error;
×
1287
    }
1288

1289
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
1,397,580✔
1290
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
1,397,580✔
1291
  }
1292

1293
  if (pPartNode->pExprs != NULL) {
7,190,949✔
1294
    int32_t    num = 0;
1,313,485✔
1295
    SExprInfo* pExprInfo1 = NULL;
1,313,485✔
1296
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
1,313,996✔
1297
    QUERY_CHECK_CODE(code, lino, _error);
1,313,996✔
1298

1299
    code = initExprSupp(&pInfo->scalarSup, pExprInfo1, num, &pTaskInfo->storageAPI.functionStore);
1,042,891✔
1300
    QUERY_CHECK_CODE(code, lino, _error);
1,042,891✔
1301
  }
1302

1303
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
6,921,217✔
1304
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
6,919,844✔
1305
  if (pInfo->pGroupSet == NULL) {
6,920,199✔
1306
    goto _error;
×
1307
  }
1308

1309
  uint32_t defaultPgsz = 0;
6,920,199✔
1310
  int64_t  defaultBufsz = 0;
6,920,706✔
1311

1312
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
6,920,706✔
1313
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
6,920,706✔
1314
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
6,920,199✔
1315
  if (code != TSDB_CODE_SUCCESS) {
6,919,844✔
1316
    goto _error;
×
1317
  }
1318

1319
  if (!osTempSpaceAvailable()) {
6,919,844✔
1320
    terrno = TSDB_CODE_NO_DISKSPACE;
×
1321
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
×
1322
    goto _error;
×
1323
  }
1324

1325
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
6,919,844✔
1326
  if (code != TSDB_CODE_SUCCESS) {
6,919,996✔
1327
    goto _error;
×
1328
  }
1329

1330
  pInfo->rowCapacity =
6,919,502✔
1331
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
6,919,996✔
1332
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
6,919,996✔
1333
  if (pInfo->rowCapacity < 0) {
6,919,502✔
1334
    code = terrno;
×
1335
    goto _error;
×
1336
  }
1337

1338
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
6,919,844✔
1339
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
6,920,199✔
1340

1341
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
6,920,351✔
1342
  if (code != TSDB_CODE_SUCCESS) {
6,920,199✔
1343
    goto _error;
×
1344
  }
1345

1346
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
6,920,199✔
1347
                  pTaskInfo);
1348

1349
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
6,920,351✔
1350
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1351

1352
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
6,919,844✔
1353
  code = appendDownstream(pOperator, &downstream, 1);
6,920,351✔
1354
  if (code != TSDB_CODE_SUCCESS) {
6,920,706✔
1355
    goto _error;
×
1356
  }
1357

1358
  *pOptrInfo = pOperator;
6,920,706✔
1359
  return TSDB_CODE_SUCCESS;
6,920,706✔
1360

1361
_error:
271,105✔
1362
  if (pInfo != NULL) {
271,105✔
1363
    destroyPartitionOperatorInfo(pInfo);
271,105✔
1364
  }
1365
  pTaskInfo->code = code;
271,105✔
1366
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
271,105✔
1367
  TAOS_RETURN(code);
271,105✔
1368
}
1369

1370
int32_t setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
2,147,483,647✔
1371
                                int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
1372
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
1373
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
2,147,483,647✔
1374
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
2,147,483,647✔
1375

1376
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
2,147,483,647✔
1377
                                                  false, pAggSup, false);
1378
  if (pResultRow == NULL || pTaskInfo->code != 0) {
2,147,483,647✔
1379
    return pTaskInfo->code;
×
1380
  }
1381

1382
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
2,147,483,647✔
1383
}
1384

1385
SSDataBlock* buildCreateTableBlock(SExprSupp* tbName, SExprSupp* tag) {
309,715✔
1386
  int32_t      code = TSDB_CODE_SUCCESS;
309,715✔
1387
  int32_t      lino = 0;
309,715✔
1388
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
309,715✔
1389
  if (!pBlock) {
310,633✔
1390
    return NULL;
×
1391
  }
1392
  pBlock->info.hasVarCol = false;
310,633✔
1393
  pBlock->info.id.groupId = 0;
310,633✔
1394
  pBlock->info.rows = 0;
310,633✔
1395
  pBlock->info.type = STREAM_CREATE_CHILD_TABLE;
309,789✔
1396
  pBlock->info.watermark = INT64_MIN;
310,916✔
1397

1398
  pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));
310,916✔
1399
  QUERY_CHECK_NULL(pBlock->pDataBlock, code, lino, _end, terrno);
310,331✔
1400
  SColumnInfoData infoData = {0};
310,612✔
1401
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
310,612✔
1402
  if (tbName->numOfExprs > 0) {
310,612✔
1403
    infoData.info.bytes = tbName->pExprInfo->base.resSchema.bytes;
×
1404
  } else {
1405
    infoData.info.bytes = 1;
310,331✔
1406
  }
1407
  pBlock->info.rowSize += infoData.info.bytes;
310,331✔
1408
  // sub table name
1409
  void* tmp = taosArrayPush(pBlock->pDataBlock, &infoData);
310,635✔
1410
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
310,612✔
1411

1412
  SColumnInfoData gpIdData = {0};
310,612✔
1413
  gpIdData.info.type = TSDB_DATA_TYPE_UBIGINT;
310,331✔
1414
  gpIdData.info.bytes = 8;
310,331✔
1415
  pBlock->info.rowSize += gpIdData.info.bytes;
310,331✔
1416
  // group id
1417
  tmp = taosArrayPush(pBlock->pDataBlock, &gpIdData);
310,635✔
1418
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
310,612✔
1419

1420
  for (int32_t i = 0; i < tag->numOfExprs; i++) {
310,612✔
1421
    SColumnInfoData tagCol = {0};
×
1422
    tagCol.info.type = tag->pExprInfo[i].base.resSchema.type;
×
1423
    tagCol.info.bytes = tag->pExprInfo[i].base.resSchema.bytes;
×
1424
    tagCol.info.precision = tag->pExprInfo[i].base.resSchema.precision;
×
1425
    // tag info
1426
    tmp = taosArrayPush(pBlock->pDataBlock, &tagCol);
×
1427
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1428
    pBlock->info.rowSize += tagCol.info.bytes;
×
1429
  }
1430

1431
_end:
310,635✔
1432
  if (code != TSDB_CODE_SUCCESS) {
310,324✔
1433
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1434
    blockDataDestroy(pBlock);
×
1435
    return NULL;
×
1436
  }
1437
  return pBlock;
310,324✔
1438
}
1439

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

1445
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
41,553,709✔
1446
  int32_t code = TSDB_CODE_SUCCESS;
41,553,709✔
1447
  int32_t lino = 0;
41,553,709✔
1448
  size_t  numOfCols = LIST_LENGTH(pNodeList);
41,553,709✔
1449
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
41,557,412✔
1450
  if (pList == NULL) {
41,558,729✔
1451
    code = terrno;
×
1452
    (*pArrayRes) = NULL;
×
1453
    QUERY_CHECK_CODE(code, lino, _end);
3,800✔
1454
  }
1455

1456
  for (int32_t i = 0; i < numOfCols; ++i) {
106,717,630✔
1457
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
65,108,065✔
1458
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
65,162,857✔
1459

1460
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
65,162,857✔
1461
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
65,164,924✔
1462

1463
      SColumn c = extractColumnFromColumnNode(pColNode);
65,164,924✔
1464
      void*   tmp = taosArrayPush(pList, &c);
65,162,433✔
1465
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
65,162,433✔
1466
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
×
1467
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
×
1468
      SColumn     c = {0};
×
1469
      c.slotId = pNode->slotId;
×
1470
      c.colId = pNode->slotId;
×
1471
      c.type = pValNode->node.type;
×
1472
      c.bytes = pValNode->node.resType.bytes;
×
1473
      c.scale = pValNode->node.resType.scale;
×
1474
      c.precision = pValNode->node.resType.precision;
×
1475

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

1481
  (*pArrayRes) = pList;
41,609,565✔
1482

1483
_end:
41,558,799✔
1484
  if (code != TSDB_CODE_SUCCESS) {
41,558,799✔
1485
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1486
  }
1487
  return code;
41,557,411✔
1488
}
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