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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

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%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

76.3
/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) {
67,677,908✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
67,677,908✔
77
  taosMemoryFree(pKey->pData);
67,677,908✔
78
}
67,676,894✔
79

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

86
  cleanupBasicInfo(&pInfo->binfo);
49,774,876✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
49,774,694✔
88
  taosArrayDestroy(pInfo->pGroupCols);
49,772,286✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
49,774,254✔
90
  cleanupExprSupp(&pInfo->scalarSup);
49,772,812✔
91

92
  if (pInfo->pOperator != NULL) {
49,775,006✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
48,142,183✔
94
                      false);
95
    pInfo->pOperator = NULL;
48,143,809✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
49,772,254✔
99
  cleanupAggSup(&pInfo->aggSup);
49,769,595✔
100
  taosMemoryFreeClear(param);
49,768,369✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
58,193,223✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
58,193,223✔
105
  if ((*pGroupColVals) == NULL) {
58,156,287✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
58,174,001✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
138,240,711✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
80,055,273✔
112
    if (!pCol) {
80,077,505✔
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
80,077,505✔
117

118
    SGroupKeys key = {0};
80,052,039✔
119
    key.bytes = pCol->bytes;
80,050,759✔
120
    key.type = pCol->type;
80,037,798✔
121
    key.isNull = false;
80,032,055✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
80,032,055✔
123
    if (key.pData == NULL) {
80,037,441✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
80,037,441✔
128
    if (!tmp) {
80,079,761✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
58,185,438✔
134
  (*keyLen) += nullFlagSize;
58,185,438✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
58,185,044✔
137
  if ((*keyBuf) == NULL) {
58,161,939✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
58,165,937✔
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;
266,950,449✔
159
    }
160

161
    if (isNull || pkey->isNull) {
2,147,483,647✔
162
      return false;
22,970,155✔
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,356✔
169

170
      if (memcmp(pkey->pData, val, dataLen) == 0) {
6,356✔
171
        continue;
908✔
172
      } else {
173
        return false;
5,448✔
174
      }
175
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
176
      if (IS_STR_DATA_BLOB(pkey->type)) {
1,983,030,169✔
177
        int32_t len = blobDataLen(val);
172✔
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);
1,984,133,807✔
185
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
1,983,680,181✔
186
          continue;
952,100,650✔
187
        } else {
188
          return false;
1,031,597,213✔
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,321,338,013✔
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,659,104,338✔
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);
37,544✔
231
        memcpy(pkey->pData, val, dataLen);
37,544✔
232
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
233
        if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
234
          memcpy(pkey->pData, val, blobDataTLen(val));
655✔
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,658,379,392✔
254
      continue;
1,658,395,555✔
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);
37,544✔
260
      memcpy(pStart, (pkey->pData), dataLen);
37,544✔
261
      pStart += dataLen;
37,544✔
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✔
UNCOV
264
        blobDataCopy(pStart, pkey->pData);
×
265
        pStart += blobDataTLen(pkey->pData);
×
266
      } else {
267
        varDataCopy(pStart, pkey->pData);
2,147,483,647✔
268
        pStart += varDataTLen(pkey->pData);
2,147,483,647✔
269
      }
270
    } else {
271
      memcpy(pStart, pkey->pData, pkey->bytes);
2,147,483,647✔
272
      pStart += pkey->bytes;
2,147,483,647✔
273
    }
274
  }
275

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

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

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

291
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
292
          int32_t dataLen = getJsonValueLen(data);
14,600✔
293
          memcpy(dest, data, dataLen);
14,600✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,492,243,240✔
UNCOV
296
            blobDataCopy(dest, data);
×
297
          } else {
298
            varDataCopy(dest, data);
1,494,928,544✔
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,209,569,217✔
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) {
243,370,173✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
243,370,173✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
243,422,799✔
315

316
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
243,431,332✔
317
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
243,423,392✔
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;
243,429,564✔
324
  terrno = TSDB_CODE_SUCCESS;
243,429,564✔
325

326
  int32_t num = 0;
243,399,533✔
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);
32,518,807✔
331
      pInfo->isInit = true;
32,553,282✔
332
      num++;
32,561,432✔
333
      continue;
32,561,432✔
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,320,440,761✔
339
      continue;
1,320,440,761✔
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);
202,853,483✔
345
      num = 1;
202,832,454✔
346
      continue;
202,832,454✔
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) {
243,426,525✔
371
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
243,432,896✔
372
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
243,406,865✔
373
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
374
    if (ret != TSDB_CODE_SUCCESS) {
243,415,620✔
375
      T_LONG_JMP(pTaskInfo->env, ret);
×
376
    }
377

378
    int32_t rowIndex = pBlock->info.rows - num;
243,415,620✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
243,409,295✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
243,367,873✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
243,367,873✔
385
  }
386
}
243,394,258✔
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,865,911,030✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,865,911,030✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
1,865,917,629✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,865,915,282✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
1,865,920,190✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
1,865,915,587✔
404

405
  blockDataCleanup(pBlock);
1,865,915,158✔
406
  if (!hasRemainResultByHash(pOperator)) {
1,865,918,746✔
407
    return;
14,977,283✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
1,850,938,235✔
411
  if (!pInfo->binfo.mergeResultBlock) {
1,850,939,787✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
1,825,580,493✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
50,701,781✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
25,355,729✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
25,357,332✔
419
        break;
11,280✔
420
      }
421
      pBlock->info.id.groupId = 0;
25,345,561✔
422
    }
423

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

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
1,818,364,562✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
1,818,364,562✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
53,391✔
432
    return true;  // limit reached, stop processing further rows
22,139✔
433
  }
434
  return false;
1,818,342,651✔
435
}
436

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

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
1,865,922,453✔
442
    return TSDB_CODE_SUCCESS;
15,620,495✔
443
  }
444

445
  if (pLimitInfo && pLimitInfo->remainGroupOffset > 0) {
1,850,300,542✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
65,487✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
17,015✔
448
      blockDataCleanup(pRes);
17,015✔
449
    } else {
450
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
48,472✔
451
      QUERY_CHECK_CODE(code, lino, _end);
48,472✔
452
    }
453
  }
454

455
  pLimitInfo->remainGroupOffset = 0;
1,850,302,214✔
456
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
1,850,303,523✔
457
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
170,816✔
458
    if (pRes->info.rows > remainRows) {
170,816✔
459
      blockDataKeepFirstNRows(pRes, remainRows);
84,909✔
460
    }
461
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
170,816✔
462
  }
463

464
_end:
1,850,129,997✔
465
  if (code != TSDB_CODE_SUCCESS) {
1,850,300,813✔
466
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
467
  }
468
  return code;
1,850,300,056✔
469
}
470

471
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
1,865,908,660✔
472
  int32_t               code = TSDB_CODE_SUCCESS;
1,865,908,660✔
473
  int32_t               lino = 0;
1,865,908,660✔
474
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,865,908,660✔
475
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,865,916,402✔
476
  SSDataBlock*          pRes = pInfo->binfo.pRes;
1,865,909,939✔
477
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
1,865,909,513✔
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,865,914,782✔
482

483
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
1,865,920,212✔
484
    QUERY_CHECK_CODE(code, lino, _end);
1,865,920,754✔
485

486
    code = doGroupResultSlimit(pRes, pLimitInfo);
1,865,920,754✔
487
    QUERY_CHECK_CODE(code, lino, _end);
1,865,917,000✔
488

489
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
1,865,917,000✔
490
      setOperatorCompleted(pOperator);
47,573,610✔
491
      // clean hash after completed
492
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
47,576,999✔
493
      pInfo->aggSup.pResultRowHashTable = NULL;
47,574,796✔
494
      break;
47,576,310✔
495
    }
496

497
    if (pRes->info.rows > 0) {
1,818,342,937✔
498
      break;
1,818,345,320✔
499
    }
500
  }
501

502
  pOperator->resultInfo.totalRows += pRes->info.rows;
1,865,921,630✔
503

504
_end:
1,865,915,675✔
505
  if (code != TSDB_CODE_SUCCESS) {
1,865,915,675✔
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,865,915,675✔
510
}
511

512
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,898,074,461✔
513
  int32_t               code = TSDB_CODE_SUCCESS;
1,898,074,461✔
514
  int32_t               lino = 0;
1,898,074,461✔
515
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,898,074,461✔
516
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,898,087,722✔
517
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
1,898,072,850✔
518
  int32_t               order = pInfo->binfo.inputTsOrder;
1,898,086,472✔
519
  int64_t               st = taosGetTimestampUs();
1,898,055,121✔
520

521
  QRY_PARAM_CHECK(ppRes);
1,898,055,121✔
522
  if (pOperator->status == OP_EXEC_DONE) {
1,898,057,559✔
523
    return code;
31,672,385✔
524
  }
525

526
  if (pOperator->status == OP_RES_TO_RETURN) {
1,866,367,827✔
527
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
1,818,339,665✔
528
    return code;
1,818,343,938✔
529
  }
530

531
  while (1) {
243,394,097✔
532
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
291,442,452✔
533
    if (pBlock == NULL) {
291,449,739✔
534
      break;
47,577,556✔
535
    }
536

537
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
243,872,183✔
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);
243,925,605✔
541
    QUERY_CHECK_CODE(code, lino, _end);
243,880,583✔
542

543
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
544
    if (pInfo->scalarSup.pExprInfo != NULL) {
243,880,583✔
545
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
46,506,740✔
546
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
46,513,360✔
547
      QUERY_CHECK_CODE(code, lino, _end);
46,487,656✔
548
    }
549

550
    doHashGroupbyAgg(pOperator, pBlock);
243,371,656✔
551
  }
552

553
  pOperator->status = OP_RES_TO_RETURN;
47,577,556✔
554

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

560
  if (pGroupResInfo->pBuf) {
47,577,318✔
561
    taosMemoryFree(pGroupResInfo->pBuf);
×
562
    pGroupResInfo->pBuf = NULL;
×
563
  }
564

565
  pGroupResInfo->index = 0;
47,576,249✔
566
  pGroupResInfo->iter = 0;
47,577,014✔
567
  pGroupResInfo->dataPos = NULL;
47,577,962✔
568

569
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
47,575,738✔
570

571
_end:
48,077,507✔
572
  if (code != TSDB_CODE_SUCCESS) {
48,077,507✔
573
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
504,844✔
574
    pTaskInfo->code = code;
504,844✔
575
    T_LONG_JMP(pTaskInfo->env, code);
504,844✔
576
  } else {
577
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
47,572,663✔
578
  }
579

580
  return code;
47,571,648✔
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,
49,742,455✔
611
                                SOperatorInfo** pOptrInfo) {
612
  QRY_PARAM_CHECK(pOptrInfo);
49,742,455✔
613

614
  int32_t               code = TSDB_CODE_SUCCESS;
49,755,429✔
615
  int32_t               lino = 0;
49,755,429✔
616
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
49,755,429✔
617
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
49,662,944✔
618
  if (pInfo == NULL || pOperator == NULL) {
49,658,467✔
619
    code = terrno;
2,732✔
620
    goto _error;
×
621
  }
622

623
  pOperator->pPhyNode = (SNode*)pAggNode;
49,655,735✔
624
  pOperator->exprSupp.hasWindowOrGroup = true;
49,668,125✔
625
  pOperator->exprSupp.hasWindow = false;
49,712,281✔
626

627
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
49,730,468✔
628
  if (pResBlock == NULL) {
49,767,809✔
629
    code = terrno;
×
630
    goto _error;
×
631
  }
632
  initBasicInfo(&pInfo->binfo, pResBlock);
49,767,809✔
633

634
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
49,749,587✔
635

636
  pInfo->pGroupCols = NULL;
49,761,794✔
637
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
49,759,986✔
638
  QUERY_CHECK_CODE(code, lino, _error);
49,744,909✔
639

640
  int32_t    numOfScalarExpr = 0;
49,744,909✔
641
  SExprInfo* pScalarExprInfo = NULL;
49,757,849✔
642
  if (pAggNode->pExprs != NULL) {
49,751,536✔
643
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
23,429,686✔
644
    QUERY_CHECK_CODE(code, lino, _error);
23,451,899✔
645
  }
646

647
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
48,112,909✔
648
  QUERY_CHECK_CODE(code, lino, _error);
48,117,875✔
649

650
  initResultSizeInfo(&pOperator->resultInfo, 4096);
48,117,875✔
651
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
48,128,152✔
652
  QUERY_CHECK_CODE(code, lino, _error);
48,131,186✔
653

654
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
48,131,186✔
655
  QUERY_CHECK_CODE(code, lino, _error);
48,100,145✔
656

657
  int32_t    num = 0;
48,100,145✔
658
  SExprInfo* pExprInfo = NULL;
48,104,551✔
659

660
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
48,096,255✔
661
  QUERY_CHECK_CODE(code, lino, _error);
48,115,561✔
662

663
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
96,241,518✔
664
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
48,116,637✔
665
  QUERY_CHECK_CODE(code, lino, _error);
48,117,430✔
666

667
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
48,128,030✔
668
                            pTaskInfo->pStreamRuntimeInfo);
48,117,430✔
669
  QUERY_CHECK_CODE(code, lino, _error);
48,118,653✔
670

671
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
48,118,653✔
672
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
48,123,624✔
673

674
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
48,126,188✔
675
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
48,117,333✔
676
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
48,114,340✔
677

678
  pInfo->pOperator = pOperator;
48,113,851✔
679

680
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
48,128,666✔
681
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
682
  setOperatorResetStateFn(pOperator, resetGroupOperState);
48,088,783✔
683
  code = appendDownstream(pOperator, &downstream, 1);
48,089,078✔
684
  QUERY_CHECK_CODE(code, lino, _error);
48,121,535✔
685

686
  *pOptrInfo = pOperator;
48,121,535✔
687
  return TSDB_CODE_SUCCESS;
48,115,615✔
688

689
_error:
1,629,737✔
690
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
1,629,737✔
691
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
1,629,466✔
692
  pTaskInfo->code = code;
1,629,195✔
693
  return code;
1,629,466✔
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) {
21,870,002✔
754
  int32_t                 code = TSDB_CODE_SUCCESS;
21,870,002✔
755
  int32_t                 lino = 0;
21,870,002✔
756
  SPartitionOperatorInfo* pInfo = pOperator->info;
21,870,002✔
757
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
21,874,688✔
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);
18,110,911✔
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;
317,477,189✔
800
            contentLen = 0;
318,798,210✔
801
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
802
            offset[*rows] = (*columnLen);
22,944✔
803
            char*   src = colDataGetData(pColInfoData, j);
22,944✔
804
            int32_t dataLen = getJsonValueLen(src);
22,944✔
805

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

810
            contentLen = dataLen;
22,944✔
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));
797,217,222✔
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:
20,405,094✔
872
  if (code != TSDB_CODE_SUCCESS) {
21,891,684✔
873
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
874
    T_LONG_JMP(pTaskInfo->env, code);
×
875
  }
876
}
21,891,684✔
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};
18,103,494✔
886
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
18,103,765✔
887
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
18,076,909✔
888

889
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
18,076,909✔
890
    if (code == TSDB_CODE_DUP_KEY) {
18,112,286✔
891
      code = TSDB_CODE_SUCCESS;
×
892
    }
893
    QUERY_CHECK_CODE(code, lino, _end);
18,112,286✔
894

895
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
18,112,286✔
896

897
    int32_t pageId = 0;
18,118,693✔
898
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
18,118,693✔
899
    if (pPage == NULL) {
18,092,687✔
900
      return pPage;
×
901
    }
902

903
    void* tmp = taosArrayPush(p->pPageList, &pageId);
18,092,687✔
904
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
18,112,992✔
905

906
    *(int32_t*)pPage = 0;
18,112,992✔
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,565,646✔
919

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

928
      void* tmp = taosArrayPush(p->pPageList, &pageId);
124,565,623✔
929
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
124,561,982✔
930

931
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
124,561,982✔
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) {
10,061,856✔
947
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
10,061,856✔
948
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
10,068,556✔
949
  if (!offset) {
10,053,499✔
950
    return NULL;
×
951
  }
952

953
  offset[0] = sizeof(int32_t) +
10,053,499✔
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) {
16,890,117✔
957
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,839,189✔
958

959
    int32_t bytes = pColInfoData->info.bytes;
6,839,220✔
960
    int32_t payloadLen = bytes * rowCapacity;
6,840,356✔
961

962
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
6,840,356✔
963
      // offset segment + content length + payload
964
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
751,653✔
965
    } else {
966
      // bitmap + content length + payload
967
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
6,072,971✔
968
    }
969
  }
970

971
  return offset;
10,050,928✔
972
}
973

974
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
8,787,305✔
975
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
8,787,305✔
976
  for (int32_t i = 0; i < size; i++) {
24,594,842✔
977
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
15,807,023✔
978
    if (pGp && pGp->blockForNotLoaded) {
15,807,294✔
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);
15,807,294✔
987
  }
988
  taosArrayClear(pInfo->sortedGroupArray);
8,787,819✔
989
  clearDiskbasedBuf(pInfo->pBuf);
8,787,819✔
990
}
8,787,548✔
991

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

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

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

1003
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
15,869,904✔
1004
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
15,869,904✔
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;
15,870,175✔
1013
}
1014

1015
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
162,480,848✔
1016
  int32_t                 code = TSDB_CODE_SUCCESS;
162,480,848✔
1017
  int32_t                 lino = 0;
162,480,848✔
1018
  SPartitionOperatorInfo* pInfo = pOperator->info;
162,480,848✔
1019
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
162,482,231✔
1020

1021
  if (pInfo->remainRows == 0) {
162,480,687✔
1022
    blockDataCleanup(pInfo->binfo.pRes);
139,965,208✔
1023
    SDataGroupInfo* pGroupInfo =
139,968,269✔
1024
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
139,965,649✔
1025
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
139,968,269✔
1026
      if (pGroupInfo != NULL) {
25,440,870✔
1027
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
15,872,256✔
1028
        if (ret != NULL) return ret;
15,870,175✔
1029
      }
1030
      // try next group data
1031
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
25,438,789✔
1032
        setOperatorCompleted(pOperator);
8,787,819✔
1033
        clearPartitionOperator(pInfo);
8,787,305✔
1034
        return NULL;
8,787,548✔
1035
      }
1036
      ++pInfo->groupIndex;
16,652,325✔
1037

1038
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
16,652,596✔
1039
      if (pGroupInfo == NULL) {
16,652,141✔
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;
16,652,141✔
1044
    }
1045

1046
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
131,175,718✔
1047
    if (pageId == NULL) {
131,176,626✔
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);
131,176,626✔
1052
    if (page == NULL) {
131,176,085✔
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) {
131,176,085✔
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);
131,176,085✔
1072
    QUERY_CHECK_CODE(code, lino, _end);
131,177,785✔
1073

1074
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
131,177,785✔
1075
    QUERY_CHECK_CODE(code, lino, _end);
131,179,095✔
1076

1077
    pInfo->pageIndex += 1;
131,179,095✔
1078
    releaseBufPage(pInfo->pBuf, page);
131,179,908✔
1079
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
131,180,179✔
1080
    pInfo->binfo.pRes->info.dataLoad = 1;
131,180,179✔
1081
    pInfo->orderedRows = 0;
131,178,998✔
1082
  } else if (pInfo->pOrderInfoArr == NULL) {
22,516,752✔
1083
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1084
  }
1085

1086
  if (pInfo->pOrderInfoArr) {
153,696,043✔
1087
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
69,095,742✔
1088
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
69,096,001✔
1089
    QUERY_CHECK_CODE(code, lino, _end);
69,091,315✔
1090
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
69,091,315✔
1091
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
69,097,540✔
1092
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
69,096,814✔
1093
  }
1094

1095
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
153,695,912✔
1096
  QUERY_CHECK_CODE(code, lino, _end);
153,691,112✔
1097

1098
_end:
153,691,112✔
1099
  if (code != TSDB_CODE_SUCCESS) {
153,691,112✔
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;
153,691,112✔
1105
  return pInfo->binfo.pRes;
153,692,261✔
1106
}
1107

1108
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
162,968,411✔
1109
  if (pOperator->status == OP_EXEC_DONE) {
162,968,411✔
1110
    (*ppRes) = NULL;
378✔
1111
    return TSDB_CODE_SUCCESS;
378✔
1112
  }
1113

1114
  int32_t                 code = TSDB_CODE_SUCCESS;
162,941,774✔
1115
  int32_t                 lino = 0;
162,941,774✔
1116
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
162,941,774✔
1117
  SPartitionOperatorInfo* pInfo = pOperator->info;
162,952,450✔
1118
  SSDataBlock*            pRes = pInfo->binfo.pRes;
162,968,750✔
1119

1120
  if (pOperator->status == OP_RES_TO_RETURN) {
162,984,982✔
1121
    (*ppRes) = buildPartitionResult(pOperator);
152,913,508✔
1122
    return code;
152,912,105✔
1123
  }
1124

1125
  int64_t        st = taosGetTimestampUs();
10,060,919✔
1126
  SOperatorInfo* downstream = pOperator->pDownstream[0];
10,060,919✔
1127

1128
  while (1) {
21,891,500✔
1129
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
31,953,774✔
1130
    if (pBlock == NULL) {
31,437,435✔
1131
      break;
9,568,885✔
1132
    }
1133

1134
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
21,868,550✔
1135
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1136
    if (pInfo->scalarSup.pExprInfo != NULL) {
21,876,032✔
1137
      code =
1138
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
930,384✔
1139
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
930,384✔
1140
      QUERY_CHECK_CODE(code, lino, _end);
929,029✔
1141
    }
1142

1143
    terrno = TSDB_CODE_SUCCESS;
21,872,016✔
1144
    doHashPartition(pOperator, pBlock);
21,870,479✔
1145
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
21,891,500✔
1146
      code = terrno;
×
1147
      QUERY_CHECK_CODE(code, lino, _end);
×
1148
    }
1149
  }
1150

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

1154
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
9,568,885✔
1155
  while (pGroupIter != NULL) {
27,691,566✔
1156
    SDataGroupInfo* pGroupInfo = pGroupIter;
18,122,681✔
1157
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
18,122,681✔
1158
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
18,122,681✔
1159
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
18,122,681✔
1160
  }
1161

1162
  taosArraySort(groupArray, compareDataGroupInfo);
9,568,885✔
1163
  pInfo->sortedGroupArray = groupArray;
9,568,885✔
1164
  pInfo->groupIndex = -1;
9,568,885✔
1165
  taosHashClear(pInfo->pGroupSet);
9,568,885✔
1166

1167
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
9,568,885✔
1168

1169
  pOperator->status = OP_RES_TO_RETURN;
9,568,885✔
1170
  code = blockDataEnsureCapacity(pRes, 4096);
9,568,885✔
1171
  QUERY_CHECK_CODE(code, lino, _end);
9,568,885✔
1172

1173
_end:
9,568,885✔
1174
  if (code != TSDB_CODE_SUCCESS) {
9,568,885✔
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);
9,568,885✔
1181
  return code;
9,563,493✔
1182
}
1183

1184
static void destroyPartitionOperatorInfo(void* param) {
10,595,594✔
1185
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
10,595,594✔
1186
  cleanupBasicInfo(&pInfo->binfo);
10,595,594✔
1187
  taosArrayDestroy(pInfo->pGroupCols);
10,594,430✔
1188

1189
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
23,018,502✔
1190
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
12,423,450✔
1191
    taosMemoryFree(key.pData);
12,424,263✔
1192
  }
1193

1194
  taosArrayDestroy(pInfo->pGroupColVals);
10,595,052✔
1195
  taosMemoryFree(pInfo->keyBuf);
10,595,323✔
1196

1197
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
10,594,510✔
1198
  for (int32_t i = 0; i < size; i++) {
12,909,654✔
1199
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
2,314,602✔
1200
    if (pGp) {
2,314,873✔
1201
      taosArrayDestroy(pGp->pPageList);
2,314,873✔
1202
    }
1203
  }
1204
  taosArrayDestroy(pInfo->sortedGroupArray);
10,595,052✔
1205

1206
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
10,595,514✔
1207
  while (pGroupIter != NULL) {
10,596,407✔
1208
    SDataGroupInfo* pGroupInfo = pGroupIter;
1,084✔
1209
    taosArrayDestroy(pGroupInfo->pPageList);
1,084✔
1210
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
1,084✔
1211
  }
1212

1213
  taosHashCleanup(pInfo->pGroupSet);
10,595,323✔
1214
  taosMemoryFree(pInfo->columnOffset);
10,595,052✔
1215

1216
  cleanupExprSupp(&pInfo->scalarSup);
10,594,972✔
1217
  destroyDiskbasedBuf(pInfo->pBuf);
10,595,865✔
1218
  taosArrayDestroy(pInfo->pOrderInfoArr);
10,592,599✔
1219
  taosMemoryFreeClear(param);
10,594,686✔
1220
}
10,592,965✔
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,
10,574,783✔
1259
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1260
  QRY_PARAM_CHECK(pOptrInfo);
10,574,783✔
1261

1262
  int32_t                 code = TSDB_CODE_SUCCESS;
10,583,726✔
1263
  int32_t                 lino = 0;
10,583,726✔
1264
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
10,583,726✔
1265
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
10,537,217✔
1266
  if (pInfo == NULL || pOperator == NULL) {
10,523,535✔
1267
    pTaskInfo->code = code = terrno;
×
1268
    goto _error;
×
1269
  }
1270

1271
  pOperator->pPhyNode = pPartNode;
10,525,213✔
1272
  int32_t    numOfCols = 0;
10,537,328✔
1273
  SExprInfo* pExprInfo = NULL;
10,544,725✔
1274
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
10,539,225✔
1275
  QUERY_CHECK_CODE(code, lino, _error);
10,561,747✔
1276
  pOperator->exprSupp.numOfExprs = numOfCols;
10,561,747✔
1277
  pOperator->exprSupp.pExprInfo = pExprInfo;
10,537,712✔
1278

1279
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
10,555,327✔
1280

1281
  if (pPartNode->needBlockOutputTsOrder) {
10,559,826✔
1282
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
2,234,244✔
1283
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
2,242,617✔
1284
    if (!pInfo->pOrderInfoArr) {
2,238,309✔
1285
      pTaskInfo->code = terrno;
×
1286
      goto _error;
×
1287
    }
1288

1289
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
2,236,141✔
1290
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
2,245,299✔
1291
  }
1292

1293
  if (pPartNode->pExprs != NULL) {
10,573,591✔
1294
    int32_t    num = 0;
2,160,096✔
1295
    SExprInfo* pExprInfo1 = NULL;
2,159,283✔
1296
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
2,158,498✔
1297
    QUERY_CHECK_CODE(code, lino, _error);
2,162,535✔
1298

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

1303
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
10,067,128✔
1304
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
10,060,720✔
1305
  if (pInfo->pGroupSet == NULL) {
10,073,385✔
1306
    goto _error;
×
1307
  }
1308

1309
  uint32_t defaultPgsz = 0;
10,074,660✔
1310
  int64_t  defaultBufsz = 0;
10,075,202✔
1311

1312
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
10,072,572✔
1313
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
10,077,721✔
1314
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
10,062,545✔
1315
  if (code != TSDB_CODE_SUCCESS) {
10,056,965✔
1316
    goto _error;
×
1317
  }
1318

1319
  if (!osTempSpaceAvailable()) {
10,056,965✔
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);
10,060,616✔
1326
  if (code != TSDB_CODE_SUCCESS) {
10,063,469✔
1327
    goto _error;
×
1328
  }
1329

1330
  pInfo->rowCapacity =
10,064,646✔
1331
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
10,056,993✔
1332
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
10,063,469✔
1333
  if (pInfo->rowCapacity < 0) {
10,066,786✔
1334
    code = terrno;
×
1335
    goto _error;
×
1336
  }
1337

1338
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
10,038,004✔
1339
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
10,038,944✔
1340

1341
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
10,040,547✔
1342
  if (code != TSDB_CODE_SUCCESS) {
10,055,400✔
1343
    goto _error;
×
1344
  }
1345

1346
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
10,055,400✔
1347
                  pTaskInfo);
1348

1349
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
10,062,298✔
1350
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1351

1352
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
10,045,221✔
1353
  code = appendDownstream(pOperator, &downstream, 1);
10,056,985✔
1354
  if (code != TSDB_CODE_SUCCESS) {
10,061,314✔
1355
    goto _error;
×
1356
  }
1357

1358
  *pOptrInfo = pOperator;
10,061,314✔
1359
  return TSDB_CODE_SUCCESS;
10,062,521✔
1360

1361
_error:
516,789✔
1362
  if (pInfo != NULL) {
517,331✔
1363
    destroyPartitionOperatorInfo(pInfo);
517,331✔
1364
  }
1365
  pTaskInfo->code = code;
517,060✔
1366
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
517,060✔
1367
  TAOS_RETURN(code);
516,817✔
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;
1,458✔
1380
  }
1381

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

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

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

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

1420
  for (int32_t i = 0; i < tag->numOfExprs; i++) {
320,406✔
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:
320,439✔
1432
  if (code != TSDB_CODE_SUCCESS) {
320,710✔
1433
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1434
    blockDataDestroy(pBlock);
×
1435
    return NULL;
×
1436
  }
1437
  return pBlock;
320,710✔
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) {
49,743,532✔
1446
  int32_t code = TSDB_CODE_SUCCESS;
49,743,532✔
1447
  int32_t lino = 0;
49,743,532✔
1448
  size_t  numOfCols = LIST_LENGTH(pNodeList);
49,743,532✔
1449
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
49,751,735✔
1450
  if (pList == NULL) {
49,720,141✔
1451
    code = terrno;
×
1452
    (*pArrayRes) = NULL;
×
1453
    QUERY_CHECK_CODE(code, lino, _end);
7,317✔
1454
  }
1455

1456
  for (int32_t i = 0; i < numOfCols; ++i) {
119,561,394✔
1457
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
69,857,035✔
1458
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
69,883,576✔
1459

1460
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
69,883,576✔
1461
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
69,882,741✔
1462

1463
      SColumn c = extractColumnFromColumnNode(pColNode);
69,887,576✔
1464
      void*   tmp = taosArrayPush(pList, &c);
69,878,500✔
1465
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
69,878,500✔
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;
49,704,359✔
1482

1483
_end:
49,777,587✔
1484
  if (code != TSDB_CODE_SUCCESS) {
49,777,587✔
1485
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1486
  }
1487
  return code;
49,723,735✔
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