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

taosdata / TDengine / #4827

28 Oct 2025 01:21AM UTC coverage: 59.754% (+0.02%) from 59.732%
#4827

push

travis-ci

web-flow
test: tidy up cases on main branch(10-27) (#33381)

121341 of 258518 branches covered (46.94%)

Branch coverage included in aggregate %.

193621 of 268583 relevant lines covered (72.09%)

4339204.91 hits per line

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

60.79
/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
} SGroupbyOperatorInfo;
45

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

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

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

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

74
static void freeGroupKey(void* param) {
96,846✔
75
  SGroupKeys* pKey = (SGroupKeys*)param;
96,846✔
76
  taosMemoryFree(pKey->pData);
96,846!
77
}
96,849✔
78

79
static void destroyGroupOperatorInfo(void* param) {
57,612✔
80
  if (param == NULL) {
57,612!
81
    return;
×
82
  }
83
  SGroupbyOperatorInfo* pInfo = (SGroupbyOperatorInfo*)param;
57,612✔
84

85
  cleanupBasicInfo(&pInfo->binfo);
57,612✔
86
  taosMemoryFreeClear(pInfo->keyBuf);
57,611!
87
  taosArrayDestroy(pInfo->pGroupCols);
57,611✔
88
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
57,612✔
89
  cleanupExprSupp(&pInfo->scalarSup);
57,611✔
90

91
  if (pInfo->pOperator != NULL) {
57,610!
92
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
57,611✔
93
                      false);
94
    pInfo->pOperator = NULL;
57,610✔
95
  }
96

97
  cleanupGroupResInfo(&pInfo->groupResInfo);
57,609✔
98
  cleanupAggSup(&pInfo->aggSup);
57,608✔
99
  taosMemoryFreeClear(param);
57,611!
100
}
101

102
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
62,958✔
103
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
62,958✔
104
  if ((*pGroupColVals) == NULL) {
62,964!
105
    return terrno;
×
106
  }
107

108
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
62,964✔
109
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
166,833✔
110
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
103,856✔
111
    if (!pCol) {
103,853!
112
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
113
      return terrno;
×
114
    }
115
    (*keyLen) += pCol->bytes;  // actual data + null_flag
103,853✔
116

117
    SGroupKeys key = {0};
103,853✔
118
    key.bytes = pCol->bytes;
103,853✔
119
    key.type = pCol->type;
103,853✔
120
    key.isNull = false;
103,853✔
121
    key.pData = taosMemoryCalloc(1, pCol->bytes);
103,853!
122
    if (key.pData == NULL) {
103,869!
123
      return terrno;
×
124
    }
125

126
    void* tmp = taosArrayPush((*pGroupColVals), &key);
103,869✔
127
    if (!tmp) {
103,867!
128
      return terrno;
×
129
    }
130
  }
131

132
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
62,977✔
133
  (*keyLen) += nullFlagSize;
62,977✔
134

135
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
62,977✔
136
  if ((*keyBuf) == NULL) {
62,968!
137
    return terrno;
×
138
  }
139

140
  return TSDB_CODE_SUCCESS;
62,968✔
141
}
142

143
static bool groupKeyCompare(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex,
27,764,175✔
144
                            int32_t numOfGroupCols) {
145
  SColumnDataAgg* pColAgg = NULL;
27,764,175✔
146
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
37,819,785✔
147
    SColumn*         pCol = taosArrayGet(pGroupCols, i);
28,862,598✔
148
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
28,859,684✔
149
    if (pBlock->pBlockAgg != NULL) {
28,852,955!
150
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
151
    }
152

153
    bool isNull = colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg);
28,852,955✔
154

155
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
28,852,955✔
156
    if (pkey->isNull && isNull) {
28,845,674✔
157
      continue;
5,100,439✔
158
    }
159

160
    if (isNull || pkey->isNull) {
23,745,235✔
161
      return false;
5,584,062✔
162
    }
163

164
    char* val = colDataGetData(pColInfoData, rowIndex);
18,161,173!
165

166
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
18,161,173✔
167
      int32_t dataLen = getJsonValueLen(val);
7✔
168

169
      if (memcmp(pkey->pData, val, dataLen) == 0) {
7✔
170
        continue;
1✔
171
      } else {
172
        return false;
6✔
173
      }
174
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
18,161,166!
175
      if (IS_STR_DATA_BLOB(pkey->type)) {
4,684,388!
176
        int32_t len = blobDataLen(val);
×
177
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
178
          continue;
×
179
        } else {
180
          return false;
×
181
        }
182
      } else {
183
        int32_t len = varDataLen(val);
4,700,405✔
184
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
4,700,405✔
185
          continue;
2,839,237✔
186
        } else {
187
          return false;
1,861,168✔
188
        }
189
      }
190
    } else {
191
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
13,476,778✔
192
        return false;
11,360,845✔
193
      }
194
    }
195
  }
196

197
  return true;
8,957,187✔
198
}
199

200
static void recordNewGroupKeys(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex) {
40,287,176✔
201
  SColumnDataAgg* pColAgg = NULL;
40,287,176✔
202

203
  size_t numOfGroupCols = taosArrayGetSize(pGroupCols);
40,287,176✔
204

205
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
96,034,966✔
206
    SColumn*         pCol = (SColumn*)taosArrayGet(pGroupCols, i);
56,004,922✔
207
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
55,953,243✔
208

209
    // valid range check. todo: return error code.
210
    if (pCol->slotId > taosArrayGetSize(pBlock->pDataBlock)) {
55,837,547!
211
      continue;
×
212
    }
213

214
    if (pBlock->pBlockAgg != NULL) {
55,851,327!
215
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
216
    }
217

218
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
55,851,327✔
219
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
111,570,632✔
220
      pkey->isNull = true;
10,443,372✔
221
    } else {
222
      pkey->isNull = false;
45,341,944✔
223
      char* val = colDataGetData(pColInfoData, rowIndex);
45,341,944!
224
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
45,341,944✔
225
        if (tTagIsJson(val)) {
85✔
226
          terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
1✔
227
          return;
1✔
228
        }
229
        int32_t dataLen = getJsonValueLen(val);
84✔
230
        memcpy(pkey->pData, val, dataLen);
84✔
231
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
45,341,859!
232
        if (IS_STR_DATA_BLOB(pkey->type)) {
19,028,769!
233
          memcpy(pkey->pData, val, blobDataTLen(val));
×
234
        } else {
235
          memcpy(pkey->pData, val, varDataTLen(val));
19,284,080✔
236
        }
237
      } else {
238
        memcpy(pkey->pData, val, pkey->bytes);
26,313,090✔
239
      }
240
    }
241
  }
242
}
243

244
static int32_t buildGroupKeys(void* pKey, const SArray* pGroupColVals) {
40,163,169✔
245
  size_t numOfGroupCols = taosArrayGetSize(pGroupColVals);
40,163,169✔
246

247
  char* isNull = (char*)pKey;
40,154,100✔
248
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
40,154,100✔
249
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
96,306,563✔
250
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
56,138,784✔
251
    if (pkey->isNull) {
56,152,463✔
252
      isNull[i] = 1;
10,479,336✔
253
      continue;
10,479,336✔
254
    }
255

256
    isNull[i] = 0;
45,673,127✔
257
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
45,673,127✔
258
      int32_t dataLen = getJsonValueLen(pkey->pData);
84✔
259
      memcpy(pStart, (pkey->pData), dataLen);
84✔
260
      pStart += dataLen;
84✔
261
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
45,673,043!
262
      if (IS_STR_DATA_BLOB(pkey->type)) {
19,218,983!
263
        blobDataCopy(pStart, pkey->pData);
×
264
        pStart += blobDataTLen(pkey->pData);
×
265
      } else {
266
        varDataCopy(pStart, pkey->pData);
19,344,227✔
267
        pStart += varDataTLen(pkey->pData);
19,344,227✔
268
      }
269
    } else {
270
      memcpy(pStart, pkey->pData, pkey->bytes);
26,454,060✔
271
      pStart += pkey->bytes;
26,454,060✔
272
    }
273
  }
274

275
  return (int32_t)(pStart - (char*)pKey);
40,167,779✔
276
}
277

278
// assign the group keys or user input constant values if required
279
static void doAssignGroupKeys(SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t totalRows, int32_t rowIndex) {
18,869,836✔
280
  for (int32_t i = 0; i < numOfOutput; ++i) {
100,443,972✔
281
    if (pCtx[i].functionId == -1) {  // select count(*),key from t group by key.
81,574,136✔
282
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[i]);
25,895,655✔
283

284
      SColumnInfoData* pColInfoData = pCtx[i].input.pData[0];
25,895,655✔
285
      // todo OPT all/all not NULL
286
      if (!colDataIsNull(pColInfoData, totalRows, rowIndex, NULL)) {
51,791,310✔
287
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
20,069,808✔
288
        char* data = colDataGetData(pColInfoData, rowIndex);
20,069,808!
289

290
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
20,069,808✔
291
          int32_t dataLen = getJsonValueLen(data);
20✔
292
          memcpy(dest, data, dataLen);
20✔
293
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
20,069,788!
294
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
2,630,243!
295
            blobDataCopy(dest, data);
×
296
          } else {
297
            varDataCopy(dest, data);
2,649,907✔
298
          }
299
        } else {
300
          memcpy(dest, data, pColInfoData->info.bytes);
17,439,545✔
301
        }
302
      } else {  // it is a NULL value
303
        pEntryInfo->isNullRes = 1;
5,825,847✔
304
      }
305

306
      pEntryInfo->numOfRes = 1;
25,895,655✔
307
    }
308
  }
309
}
18,869,836✔
310

311
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
456,824✔
312
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
456,824✔
313
  SGroupbyOperatorInfo* pInfo = pOperator->info;
456,824✔
314

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

322
  int32_t len = 0;
456,935✔
323
  terrno = TSDB_CODE_SUCCESS;
456,935✔
324

325
  int32_t num = 0;
457,112✔
326
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
28,262,550✔
327
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
328
    if (!pInfo->isInit) {
27,805,682✔
329
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
46,007✔
330
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
45,997✔
331
        T_LONG_JMP(pTaskInfo->env, terrno);
1!
332
      }
333
      pInfo->isInit = true;
45,990✔
334
      num++;
45,990✔
335
      continue;
45,990✔
336
    }
337

338
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
27,759,675✔
339
    if (equal) {
27,762,983✔
340
      num++;
8,957,106✔
341
      continue;
8,957,106✔
342
    }
343

344
    // The first row of a new block does not belongs to the previous existed group
345
    if (j == 0) {
18,805,877✔
346
      num++;
398,296✔
347
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
398,296✔
348
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
398,220!
349
        T_LONG_JMP(pTaskInfo->env, terrno);
×
350
      }
351
      continue;
398,222✔
352
    }
353

354
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
18,407,581✔
355
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
18,401,549✔
356
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
357
    if (ret != TSDB_CODE_SUCCESS) {  // null data, too many state code
18,405,098!
358
      T_LONG_JMP(pTaskInfo->env, ret);
×
359
    }
360

361
    int32_t rowIndex = j - num;
18,405,098✔
362
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
18,405,098✔
363
                                          pOperator->exprSupp.numOfExprs);
364
    if (ret != TSDB_CODE_SUCCESS) {
18,415,205!
365
      T_LONG_JMP(pTaskInfo->env, ret);
×
366
    }
367

368
    // assign the group keys or user input constant values if required
369
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
18,415,205✔
370
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
18,423,447✔
371
    num = 1;
18,404,120✔
372
  }
373

374
  if (num > 0) {
456,868!
375
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
456,882✔
376
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
456,909✔
377
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
378
    if (ret != TSDB_CODE_SUCCESS) {
456,899!
379
      T_LONG_JMP(pTaskInfo->env, ret);
×
380
    }
381

382
    int32_t rowIndex = pBlock->info.rows - num;
456,899✔
383
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
456,899✔
384
                                          pOperator->exprSupp.numOfExprs);
385
    if (ret != TSDB_CODE_SUCCESS) {
456,895!
386
      T_LONG_JMP(pTaskInfo->env, ret);
×
387
    }
388
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
456,895✔
389
  }
390
}
456,910✔
391

392
bool hasRemainResultByHash(SOperatorInfo* pOperator) {
6,885,182✔
393
  SGroupbyOperatorInfo* pInfo = pOperator->info;
6,885,182✔
394
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
6,885,182✔
395
  return pInfo->groupResInfo.index < tSimpleHashGetSize(pHashmap);
6,885,182✔
396
}
397

398
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
3,408,333✔
399
                                  SDiskbasedBuf* pBuf) {
400
  SGroupbyOperatorInfo* pInfo = pOperator->info;
3,408,333✔
401
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
3,408,333✔
402
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
3,408,333✔
403

404
  SSDataBlock* pBlock = pInfo->binfo.pRes;
3,408,333✔
405

406
  // set output datablock version
407
  pBlock->info.version = pTaskInfo->version;
3,408,333✔
408

409
  blockDataCleanup(pBlock);
3,408,333✔
410
  if (!hasRemainResultByHash(pOperator)) {
3,408,312✔
411
    return;
11,598✔
412
  }
413

414
  pBlock->info.id.groupId = 0;
3,396,706✔
415
  if (!pInfo->binfo.mergeResultBlock) {
3,396,706✔
416
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
3,362,328✔
417
                             pHashmap, pOperator->resultInfo.threshold, false);
418
  } else {
419
    while (hasRemainResultByHash(pOperator)) {
68,740✔
420
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
34,378✔
421
                               pHashmap, pOperator->resultInfo.threshold, true);
422
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
34,378✔
423
        break;
16✔
424
      }
425
      pBlock->info.id.groupId = 0;
34,362✔
426
    }
427

428
    // clear the group id info in SSDataBlock, since the client does not need it
429
    pBlock->info.id.groupId = 0;
34,378✔
430
  }
431
}
432

433
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
3,408,338✔
434
  int32_t               code = TSDB_CODE_SUCCESS;
3,408,338✔
435
  int32_t               lino = 0;
3,408,338✔
436
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
3,408,338✔
437
  SGroupbyOperatorInfo* pInfo = pOperator->info;
3,408,338✔
438
  SSDataBlock*          pRes = pInfo->binfo.pRes;
3,408,338✔
439

440
  // after filter, if result block turn to null, get next from whole set
441
  while (1) {
442
    doBuildResultDatablockByHash(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf);
3,408,338✔
443

444
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
3,408,434✔
445
    QUERY_CHECK_CODE(code, lino, _end);
3,408,431!
446

447
    if (!hasRemainResultByHash(pOperator)) {
3,408,431✔
448
      setOperatorCompleted(pOperator);
57,416✔
449
      // clean hash after completed
450
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
57,419✔
451
      pInfo->aggSup.pResultRowHashTable = NULL;
57,408✔
452
      break;
57,408✔
453
    }
454
    if (pRes->info.rows > 0) {
3,351,020!
455
      break;
3,351,020✔
456
    }
457
  }
458

459
  pOperator->resultInfo.totalRows += pRes->info.rows;
3,408,428✔
460

461
_end:
3,408,428✔
462
  if (code != TSDB_CODE_SUCCESS) {
3,408,428!
463
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
464
    T_LONG_JMP(pTaskInfo->env, code);
×
465
  }
466
  return (pRes->info.rows == 0) ? NULL : pRes;
3,408,428✔
467
}
468

469
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
3,453,776✔
470
  int32_t               code = TSDB_CODE_SUCCESS;
3,453,776✔
471
  int32_t               lino = 0;
3,453,776✔
472
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
3,453,776✔
473
  SGroupbyOperatorInfo* pInfo = pOperator->info;
3,453,776✔
474
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
3,453,776✔
475
  int32_t               order = pInfo->binfo.inputTsOrder;
3,453,776✔
476
  int64_t               st = taosGetTimestampUs();
3,453,793✔
477

478
  QRY_PARAM_CHECK(ppRes);
3,453,793!
479
  if (pOperator->status == OP_EXEC_DONE) {
3,453,793✔
480
    return code;
45,447✔
481
  }
482

483
  if (pOperator->status == OP_RES_TO_RETURN) {
3,408,346✔
484
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
3,350,744✔
485
    return code;
3,350,819✔
486
  }
487

488
  while (1) {
456,877✔
489
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
514,479✔
490
    if (pBlock == NULL) {
514,437✔
491
      break;
57,597✔
492
    }
493

494
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
456,840✔
495

496
    // the pDataBlock are always the same one, no need to call this again
497
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
456,840✔
498
    QUERY_CHECK_CODE(code, lino, _end);
456,903!
499

500
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
501
    if (pInfo->scalarSup.pExprInfo != NULL) {
456,903✔
502
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
93,090✔
503
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
93,090!
504
      QUERY_CHECK_CODE(code, lino, _end);
92,986!
505
    }
506

507
    doHashGroupbyAgg(pOperator, pBlock);
456,799✔
508
  }
509

510
  pOperator->status = OP_RES_TO_RETURN;
57,597✔
511

512
  // initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
513
  if (pGroupResInfo->pRows != NULL) {
57,597!
514
    taosArrayDestroy(pGroupResInfo->pRows);
×
515
  }
516

517
  if (pGroupResInfo->pBuf) {
57,597!
518
    taosMemoryFree(pGroupResInfo->pBuf);
×
519
    pGroupResInfo->pBuf = NULL;
×
520
  }
521

522
  pGroupResInfo->index = 0;
57,597✔
523
  pGroupResInfo->iter = 0;
57,597✔
524
  pGroupResInfo->dataPos = NULL;
57,597✔
525

526
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
57,599✔
527

528
_end:
57,599✔
529
  if (code != TSDB_CODE_SUCCESS) {
57,599!
530
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
531
    pTaskInfo->code = code;
×
532
    T_LONG_JMP(pTaskInfo->env, code);
×
533
  } else {
534
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
57,599✔
535
  }
536

537
  return code;
57,608✔
538
}
539

540
static int32_t resetGroupOperState(SOperatorInfo* pOper) {
×
541
  SGroupbyOperatorInfo* pInfo = pOper->info;
×
542
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
543
  SAggPhysiNode* pPhynode = (SAggPhysiNode*)pOper->pPhyNode;
×
544
  resetBasicOperatorState(&pInfo->binfo);
×
545
  pOper->status = OP_NOT_OPENED;
×
546

547
  cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
×
548
    false);
549

550
  cleanupGroupResInfo(&pInfo->groupResInfo);
×
551

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

557
  if (code == 0){
×
558
    code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
559
      &pTaskInfo->storageAPI.functionStore);
560
  }
561

562
  pInfo->isInit = false;
×
563

564
  return code;
×
565
}
566

567
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
57,592✔
568
                                SOperatorInfo** pOptrInfo) {
569
  QRY_PARAM_CHECK(pOptrInfo);
57,592!
570

571
  int32_t               code = TSDB_CODE_SUCCESS;
57,592✔
572
  int32_t               lino = 0;
57,592✔
573
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
57,592!
574
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
57,608!
575
  if (pInfo == NULL || pOperator == NULL) {
57,608!
576
    code = terrno;
×
577
    goto _error;
×
578
  }
579

580
  pOperator->pPhyNode = (SNode*)pAggNode;
57,608✔
581
  pOperator->exprSupp.hasWindowOrGroup = true;
57,608✔
582

583
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
57,608✔
584
  if (pResBlock == NULL) {
57,606!
585
    code = terrno;
×
586
    goto _error;
×
587
  }
588
  initBasicInfo(&pInfo->binfo, pResBlock);
57,606✔
589

590
  pInfo->pGroupCols = NULL;
57,607✔
591
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
57,607✔
592
  QUERY_CHECK_CODE(code, lino, _error);
57,611!
593

594
  int32_t    numOfScalarExpr = 0;
57,611✔
595
  SExprInfo* pScalarExprInfo = NULL;
57,611✔
596
  if (pAggNode->pExprs != NULL) {
57,611✔
597
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
31,254✔
598
    QUERY_CHECK_CODE(code, lino, _error);
31,247!
599
  }
600

601
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
57,604✔
602
  QUERY_CHECK_CODE(code, lino, _error);
57,604!
603

604
  initResultSizeInfo(&pOperator->resultInfo, 4096);
57,604✔
605
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
57,602✔
606
  QUERY_CHECK_CODE(code, lino, _error);
57,610!
607

608
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
57,610✔
609
  QUERY_CHECK_CODE(code, lino, _error);
57,611!
610

611
  int32_t    num = 0;
57,611✔
612
  SExprInfo* pExprInfo = NULL;
57,611✔
613

614
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
57,611✔
615
  QUERY_CHECK_CODE(code, lino, _error);
57,609!
616

617
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
57,609✔
618
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
57,609✔
619
  QUERY_CHECK_CODE(code, lino, _error);
57,604!
620

621
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
57,604✔
622
                            pTaskInfo->pStreamRuntimeInfo);
57,604✔
623
  QUERY_CHECK_CODE(code, lino, _error);
57,604!
624

625
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
57,604✔
626
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
57,602✔
627

628
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
57,603✔
629
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
57,603✔
630
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
57,603✔
631

632
  pInfo->pOperator = pOperator;
57,603✔
633

634
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
57,603✔
635
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
636
  setOperatorResetStateFn(pOperator, resetGroupOperState);
57,603✔
637
  code = appendDownstream(pOperator, &downstream, 1);
57,604✔
638
  QUERY_CHECK_CODE(code, lino, _error);
57,598!
639

640
  *pOptrInfo = pOperator;
57,598✔
641
  return TSDB_CODE_SUCCESS;
57,598✔
642

643
_error:
×
644
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
×
645
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
646
  pTaskInfo->code = code;
×
647
  return code;
×
648
}
649

650
SSDataBlock* createBlockDataNotLoaded(const SOperatorInfo* pOperator, SSDataBlock* pDataBlock) {
×
651
  int32_t code = TSDB_CODE_SUCCESS;
×
652
  int32_t lino = 0;
×
653
  if (pDataBlock == NULL) {
×
654
    return NULL;
×
655
  }
656

657
  SSDataBlock* pDstBlock = NULL;
×
658
  code = createDataBlock(&pDstBlock);
×
659
  QUERY_CHECK_CODE(code, lino, _end);
×
660

661
  pDstBlock->info = pDataBlock->info;
×
662
  pDstBlock->info.id.blockId = pOperator->resultDataBlockId;
×
663
  pDstBlock->info.capacity = 0;
×
664
  pDstBlock->info.rowSize = 0;
×
665

666
  size_t numOfCols = pOperator->exprSupp.numOfExprs;
×
667
  if (pDataBlock->pBlockAgg) {
×
668
    pDstBlock->pBlockAgg = taosMemoryCalloc(numOfCols, sizeof(SColumnDataAgg));
×
669
    if (pDstBlock->pBlockAgg == NULL) {
×
670
      blockDataDestroy(pDstBlock);
×
671
      return NULL;
×
672
    }
673
    for (int i = 0; i < numOfCols; ++i) {
×
674
      pDstBlock->pBlockAgg[i].colId = -1;
×
675
    }
676
  }
677

678
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
×
679
    SExprInfo*       pExpr = &pOperator->exprSupp.pExprInfo[i];
×
680
    int32_t          slotId = pExpr->base.pParam[0].pCol->slotId;
×
681
    SColumnInfoData* pSrc = taosArrayGet(pDataBlock->pDataBlock, slotId);
×
682
    SColumnInfoData  colInfo = {.hasNull = true, .info = pSrc->info};
×
683
    code = blockDataAppendColInfo(pDstBlock, &colInfo);
×
684
    QUERY_CHECK_CODE(code, lino, _end);
×
685

686
    SColumnInfoData* pDst = taosArrayGet(pDstBlock->pDataBlock, i);
×
687
    if (pDataBlock->pBlockAgg && pDataBlock->pBlockAgg[slotId].colId != -1) {
×
688
      pDstBlock->pBlockAgg[i] = pDataBlock->pBlockAgg[slotId];
×
689
    } else {
690
      code = doEnsureCapacity(pDst, &pDstBlock->info, pDataBlock->info.rows, false);
×
691
      QUERY_CHECK_CODE(code, lino, _end);
×
692

693
      code = colDataAssign(pDst, pSrc, pDataBlock->info.rows, &pDataBlock->info);
×
694
      QUERY_CHECK_CODE(code, lino, _end);
×
695
    }
696
  }
697

698
_end:
×
699
  if (code != TSDB_CODE_SUCCESS) {
×
700
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
701
    blockDataDestroy(pDstBlock);
×
702
    return NULL;
×
703
  }
704
  return pDstBlock;
×
705
}
706

707
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
52,823✔
708
  int32_t                 code = TSDB_CODE_SUCCESS;
52,823✔
709
  int32_t                 lino = 0;
52,823✔
710
  SPartitionOperatorInfo* pInfo = pOperator->info;
52,823✔
711
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
52,823✔
712

713
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
21,488,415✔
714
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
21,435,639✔
715
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
21,270,390✔
716

717
    SDataGroupInfo* pGroupInfo = NULL;
21,349,004✔
718
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
21,349,004✔
719
    if (pPage == NULL) {
21,521,531!
720
      T_LONG_JMP(pTaskInfo->env, terrno);
×
721
    }
722

723
    pGroupInfo->numOfRows += 1;
21,521,531✔
724

725
    // group id
726
    if (pGroupInfo->groupId == 0) {
21,521,531✔
727
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
116,420✔
728
    }
729

730
    if (pBlock->info.dataLoad) {
21,521,537!
731
      // number of rows
732
      int32_t* rows = (int32_t*)pPage;
21,521,537✔
733

734
      size_t numOfCols = pOperator->exprSupp.numOfExprs;
21,521,537✔
735
      for (int32_t i = 0; i < numOfCols; ++i) {
140,263,398✔
736
        SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
118,317,976✔
737
        int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
118,317,976✔
738

739
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
118,317,976✔
740

741
        int32_t bytes = pColInfoData->info.bytes;
118,371,630✔
742
        int32_t startOffset = pInfo->columnOffset[i];
118,371,630✔
743

744
        int32_t* columnLen = NULL;
118,371,630✔
745
        int32_t  contentLen = 0;
118,371,630✔
746

747
        if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
145,207,577!
748
          int32_t* offset = (int32_t*)((char*)pPage + startOffset);
26,835,947✔
749
          columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
26,835,947✔
750
          char* data = (char*)((char*)columnLen + sizeof(int32_t));
26,835,947✔
751

752
          if (colDataIsNull_s(pColInfoData, j)) {
53,671,894✔
753
            offset[(*rows)] = -1;
5,952,836✔
754
            contentLen = 0;
5,952,836✔
755
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
20,883,111✔
756
            offset[*rows] = (*columnLen);
64✔
757
            char*   src = colDataGetData(pColInfoData, j);
64!
758
            int32_t dataLen = getJsonValueLen(src);
64✔
759

760
            memcpy(data + (*columnLen), src, dataLen);
64✔
761
            int32_t v = (data + (*columnLen) + dataLen - (char*)pPage);
64✔
762
            QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
64!
763

764
            contentLen = dataLen;
64✔
765
          } else {
766
            if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
20,883,047!
767
              offset[*rows] = (*columnLen);
×
768
              char* src = colDataGetData(pColInfoData, j);
×
769
              memcpy(data + (*columnLen), src, blobDataTLen(src));
×
770
              int32_t v = (data + (*columnLen) + blobDataTLen(src) - (char*)pPage);
×
771
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
772

773
              contentLen = blobDataTLen(src);
×
774
            } else {
775
              offset[*rows] = (*columnLen);
22,067,251✔
776
              char* src = colDataGetData(pColInfoData, j);
22,067,251!
777
              memcpy(data + (*columnLen), src, varDataTLen(src));
22,067,251✔
778
              int32_t v = (data + (*columnLen) + varDataTLen(src) - (char*)pPage);
22,067,251✔
779
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
22,067,251!
780

781
              contentLen = varDataTLen(src);
22,067,251✔
782
            }
783
          }
784
        } else {
785
          char* bitmap = (char*)pPage + startOffset;
91,535,683✔
786
          columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
91,535,683✔
787
          char* data = (char*)columnLen + sizeof(int32_t);
91,535,683✔
788

789
          bool isNull = colDataIsNull_f(pColInfoData, j);
91,535,683✔
790
          if (isNull) {
91,535,683✔
791
            colDataSetNull_f(bitmap, (*rows));
26,155,558✔
792
          } else {
793
            memcpy(data + (*columnLen), colDataGetData(pColInfoData, j), bytes);
65,380,125!
794
            QUERY_CHECK_CONDITION(((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf)), code,
65,380,125!
795
                                  lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
796
          }
797
          contentLen = bytes;
91,905,914✔
798
        }
799

800
        (*columnLen) += contentLen;
118,741,861✔
801
      }
802

803
      (*rows) += 1;
21,945,422✔
804

805
      setBufPageDirty(pPage, true);
21,945,422✔
806
      releaseBufPage(pInfo->pBuf, pPage);
21,514,609✔
807
    } else {
808
      SSDataBlock* dataNotLoadBlock = createBlockDataNotLoaded(pOperator, pBlock);
×
809
      if (dataNotLoadBlock == NULL) {
×
810
        T_LONG_JMP(pTaskInfo->env, terrno);
×
811
      }
812
      if (pGroupInfo->blockForNotLoaded == NULL) {
×
813
        pGroupInfo->blockForNotLoaded = taosArrayInit(0, sizeof(SSDataBlock*));
×
814
        QUERY_CHECK_NULL(pGroupInfo->blockForNotLoaded, code, lino, _end, terrno);
×
815
        pGroupInfo->offsetForNotLoaded = 0;
×
816
      }
817
      dataNotLoadBlock->info.id.groupId = pGroupInfo->groupId;
×
818
      dataNotLoadBlock->info.dataLoad = 0;
×
819
      void* tmp = taosArrayPush(pGroupInfo->blockForNotLoaded, &dataNotLoadBlock);
×
820
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
821
      break;
×
822
    }
823
  }
824

825
_end:
52,776✔
826
  if (code != TSDB_CODE_SUCCESS) {
52,776!
827
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
828
    T_LONG_JMP(pTaskInfo->env, code);
×
829
  }
830
}
52,776✔
831

832
void* getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len) {
21,383,439✔
833
  int32_t         code = TSDB_CODE_SUCCESS;
21,383,439✔
834
  int32_t         lino = 0;
21,383,439✔
835
  SDataGroupInfo* p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
21,383,439✔
836

837
  void* pPage = NULL;
21,684,620✔
838
  if (p == NULL) {  // it is a new group
21,684,620✔
839
    SDataGroupInfo gi = {0};
116,411✔
840
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
116,411✔
841
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
116,420!
842

843
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
116,420✔
844
    if (code == TSDB_CODE_DUP_KEY) {
116,436!
845
      code = TSDB_CODE_SUCCESS;
×
846
    }
847
    QUERY_CHECK_CODE(code, lino, _end);
116,436!
848

849
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
116,436✔
850

851
    int32_t pageId = 0;
116,415✔
852
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
116,415✔
853
    if (pPage == NULL) {
116,426!
854
      return pPage;
×
855
    }
856

857
    void* tmp = taosArrayPush(p->pPageList, &pageId);
116,426✔
858
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
116,425!
859

860
    *(int32_t*)pPage = 0;
116,425✔
861
  } else {
862
    int32_t* curId = taosArrayGetLast(p->pPageList);
21,568,209✔
863
    pPage = getBufPage(pInfo->pBuf, *curId);
21,529,131✔
864
    if (pPage == NULL) {
21,422,272!
865
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
866
      return pPage;
×
867
    }
868

869
    int32_t* rows = (int32_t*)pPage;
21,422,272✔
870
    if (*rows >= pInfo->rowCapacity) {
21,422,272✔
871
      // release buffer
872
      releaseBufPage(pInfo->pBuf, pPage);
594,044✔
873

874
      // add a new page for current group
875
      int32_t pageId = 0;
593,855✔
876
      pPage = getNewBufPage(pInfo->pBuf, &pageId);
593,855✔
877
      if (pPage == NULL) {
594,733!
878
        qError("failed to get new buffer, code:%s", tstrerror(terrno));
×
879
        return NULL;
×
880
      }
881

882
      void* tmp = taosArrayPush(p->pPageList, &pageId);
594,733✔
883
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
594,694!
884

885
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
594,694✔
886
    }
887
  }
888

889
  *pGroupInfo = p;
21,539,251✔
890

891
_end:
21,539,251✔
892
  if (code != TSDB_CODE_SUCCESS) {
21,539,251!
893
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
894
    return NULL;
×
895
  }
896

897
  return pPage;
21,539,251✔
898
}
899

900
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
5,353✔
901
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
5,353✔
902
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
5,356!
903
  if (!offset) {
5,357!
904
    return NULL;
×
905
  }
906

907
  offset[0] = sizeof(int32_t) +
5,357✔
908
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
909

910
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
18,303✔
911
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
12,946✔
912

913
    int32_t bytes = pColInfoData->info.bytes;
12,946✔
914
    int32_t payloadLen = bytes * rowCapacity;
12,946✔
915

916
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
12,946!
917
      // offset segment + content length + payload
918
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
1,945✔
919
    } else {
920
      // bitmap + content length + payload
921
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
11,001✔
922
    }
923
  }
924

925
  return offset;
5,357✔
926
}
927

928
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
5,247✔
929
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
5,247✔
930
  for (int32_t i = 0; i < size; i++) {
68,989✔
931
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
63,739✔
932
    if (pGp && pGp->blockForNotLoaded) {
63,740!
933
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
934
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
935
        if (pBlock) blockDataDestroy(*pBlock);
×
936
      }
937
      taosArrayClear(pGp->blockForNotLoaded);
×
938
      pGp->offsetForNotLoaded = 0;
×
939
    }
940
    taosArrayDestroy(pGp->pPageList);
63,740✔
941
  }
942
  taosArrayClear(pInfo->sortedGroupArray);
5,250✔
943
  clearDiskbasedBuf(pInfo->pBuf);
5,248✔
944
}
5,249✔
945

946
static int compareDataGroupInfo(const void* group1, const void* group2) {
898,688✔
947
  const SDataGroupInfo* pGroupInfo1 = group1;
898,688✔
948
  const SDataGroupInfo* pGroupInfo2 = group2;
898,688✔
949

950
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
898,688!
951
    return 0;
×
952
  }
953

954
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
898,688✔
955
}
956

957
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
64,849✔
958
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
64,849!
959
    SSDataBlock** pBlock = taosArrayGet(pGroupInfo->blockForNotLoaded, pGroupInfo->offsetForNotLoaded);
×
960
    if (!pBlock) {
×
961
      return NULL;
×
962
    }
963
    pGroupInfo->offsetForNotLoaded++;
×
964
    return *pBlock;
×
965
  }
966
  return NULL;
64,849✔
967
}
968

969
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
679,524✔
970
  int32_t                 code = TSDB_CODE_SUCCESS;
679,524✔
971
  int32_t                 lino = 0;
679,524✔
972
  SPartitionOperatorInfo* pInfo = pOperator->info;
679,524✔
973
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
679,524✔
974

975
  if (pInfo->remainRows == 0) {
679,524✔
976
    blockDataCleanup(pInfo->binfo.pRes);
623,138✔
977
    SDataGroupInfo* pGroupInfo =
623,032✔
978
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
623,074✔
979
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
623,032✔
980
      if (pGroupInfo != NULL) {
70,207✔
981
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
64,849✔
982
        if (ret != NULL) return ret;
64,850!
983
      }
984
      // try next group data
985
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
70,208✔
986
        setOperatorCompleted(pOperator);
5,247✔
987
        clearPartitionOperator(pInfo);
5,248✔
988
        return NULL;
5,249✔
989
      }
990
      ++pInfo->groupIndex;
64,957✔
991

992
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
64,957✔
993
      if (pGroupInfo == NULL) {
64,958!
994
        qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
995
        T_LONG_JMP(pTaskInfo->env, terrno);
×
996
      }
997
      pInfo->pageIndex = 0;
64,958✔
998
    }
999

1000
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
617,774✔
1001
    if (pageId == NULL) {
617,689!
1002
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1003
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1004
    }
1005
    void* page = getBufPage(pInfo->pBuf, *pageId);
617,689✔
1006
    if (page == NULL) {
617,660!
1007
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
1008
      T_LONG_JMP(pTaskInfo->env, terrno);
×
1009
    }
1010
    if (*(int32_t*)page == 0) {
617,701!
1011
      releaseBufPage(pInfo->pBuf, page);
×
1012
      SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
×
1013
      if (ret != NULL) return ret;
×
1014
      if (pInfo->groupIndex + 1 < taosArrayGetSize(pInfo->sortedGroupArray)) {
×
1015
        pInfo->groupIndex++;
×
1016
        pInfo->pageIndex = 0;
×
1017
      } else {
1018
        setOperatorCompleted(pOperator);
×
1019
        clearPartitionOperator(pInfo);
×
1020
        return NULL;
×
1021
      }
1022
      return buildPartitionResult(pOperator);
×
1023
    }
1024

1025
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
617,701✔
1026
    QUERY_CHECK_CODE(code, lino, _end);
617,697!
1027

1028
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
617,697✔
1029
    QUERY_CHECK_CODE(code, lino, _end);
617,843!
1030

1031
    pInfo->pageIndex += 1;
617,843✔
1032
    releaseBufPage(pInfo->pBuf, page);
617,843✔
1033
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
617,820✔
1034
    pInfo->binfo.pRes->info.dataLoad = 1;
617,820✔
1035
    pInfo->orderedRows = 0;
617,820✔
1036
  } else if (pInfo->pOrderInfoArr == NULL) {
56,386!
1037
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1038
  }
1039

1040
  if (pInfo->pOrderInfoArr) {
674,206✔
1041
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
158,058✔
1042
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
158,058✔
1043
    QUERY_CHECK_CODE(code, lino, _end);
158,060!
1044
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
158,060✔
1045
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
158,100✔
1046
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
158,100✔
1047
  }
1048

1049
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
674,248✔
1050
  QUERY_CHECK_CODE(code, lino, _end);
674,192!
1051

1052
_end:
674,192✔
1053
  if (code != TSDB_CODE_SUCCESS) {
674,192!
1054
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1055
    T_LONG_JMP(pTaskInfo->env, code);
×
1056
  }
1057

1058
  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
674,192✔
1059
  return pInfo->binfo.pRes;
674,192✔
1060
}
1061

1062
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
679,548✔
1063
  if (pOperator->status == OP_EXEC_DONE) {
679,548✔
1064
    (*ppRes) = NULL;
29✔
1065
    return TSDB_CODE_SUCCESS;
29✔
1066
  }
1067

1068
  int32_t                 code = TSDB_CODE_SUCCESS;
679,519✔
1069
  int32_t                 lino = 0;
679,519✔
1070
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
679,519✔
1071
  SPartitionOperatorInfo* pInfo = pOperator->info;
679,519✔
1072
  SSDataBlock*            pRes = pInfo->binfo.pRes;
679,519✔
1073

1074
  if (pOperator->status == OP_RES_TO_RETURN) {
679,519✔
1075
    (*ppRes) = buildPartitionResult(pOperator);
674,167✔
1076
    return code;
674,087✔
1077
  }
1078

1079
  int64_t        st = taosGetTimestampUs();
5,358✔
1080
  SOperatorInfo* downstream = pOperator->pDownstream[0];
5,358✔
1081

1082
  while (1) {
52,778✔
1083
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
58,136✔
1084
    if (pBlock == NULL) {
58,180✔
1085
      break;
5,357✔
1086
    }
1087

1088
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
52,823✔
1089
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1090
    if (pInfo->scalarSup.pExprInfo != NULL) {
52,823✔
1091
      code =
1092
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
129✔
1093
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
129!
1094
      QUERY_CHECK_CODE(code, lino, _end);
129!
1095
    }
1096

1097
    terrno = TSDB_CODE_SUCCESS;
52,823✔
1098
    doHashPartition(pOperator, pBlock);
52,822✔
1099
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
52,778!
1100
      code = terrno;
×
1101
      QUERY_CHECK_CODE(code, lino, _end);
×
1102
    }
1103
  }
1104

1105
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
5,357✔
1106
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
5,358!
1107

1108
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
5,358✔
1109
  while (pGroupIter != NULL) {
121,798✔
1110
    SDataGroupInfo* pGroupInfo = pGroupIter;
116,440✔
1111
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
116,440✔
1112
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
116,440!
1113
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
116,440✔
1114
  }
1115

1116
  taosArraySort(groupArray, compareDataGroupInfo);
5,358✔
1117
  pInfo->sortedGroupArray = groupArray;
5,358✔
1118
  pInfo->groupIndex = -1;
5,358✔
1119
  taosHashClear(pInfo->pGroupSet);
5,358✔
1120

1121
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
5,358✔
1122

1123
  pOperator->status = OP_RES_TO_RETURN;
5,358✔
1124
  code = blockDataEnsureCapacity(pRes, 4096);
5,358✔
1125
  QUERY_CHECK_CODE(code, lino, _end);
5,358!
1126

1127
_end:
5,358✔
1128
  if (code != TSDB_CODE_SUCCESS) {
5,358!
1129
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1130
    pTaskInfo->code = code;
×
1131
    T_LONG_JMP(pTaskInfo->env, code);
×
1132
  }
1133

1134
  (*ppRes) = buildPartitionResult(pOperator);
5,358✔
1135
  return code;
5,356✔
1136
}
1137

1138
static void destroyPartitionOperatorInfo(void* param) {
5,355✔
1139
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
5,355✔
1140
  cleanupBasicInfo(&pInfo->binfo);
5,355✔
1141
  taosArrayDestroy(pInfo->pGroupCols);
5,357✔
1142

1143
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
12,398✔
1144
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
7,041✔
1145
    taosMemoryFree(key.pData);
7,041!
1146
  }
1147

1148
  taosArrayDestroy(pInfo->pGroupColVals);
5,355✔
1149
  taosMemoryFree(pInfo->keyBuf);
5,356!
1150

1151
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
5,358✔
1152
  for (int32_t i = 0; i < size; i++) {
58,054✔
1153
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
52,696✔
1154
    if (pGp) {
52,696!
1155
      taosArrayDestroy(pGp->pPageList);
52,696✔
1156
    }
1157
  }
1158
  taosArrayDestroy(pInfo->sortedGroupArray);
5,358✔
1159

1160
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
5,355✔
1161
  while (pGroupIter != NULL) {
5,354!
1162
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1163
    taosArrayDestroy(pGroupInfo->pPageList);
×
1164
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1165
  }
1166

1167
  taosHashCleanup(pInfo->pGroupSet);
5,354✔
1168
  taosMemoryFree(pInfo->columnOffset);
5,358!
1169

1170
  cleanupExprSupp(&pInfo->scalarSup);
5,357✔
1171
  destroyDiskbasedBuf(pInfo->pBuf);
5,358✔
1172
  taosArrayDestroy(pInfo->pOrderInfoArr);
5,357✔
1173
  taosMemoryFreeClear(param);
5,358!
1174
}
5,353✔
1175

1176
static int32_t resetPartitionOperState(SOperatorInfo* pOper) {
×
1177
  SPartitionOperatorInfo* pInfo = pOper->info;
×
1178
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
1179
  SPartitionPhysiNode* pPhynode = (SPartitionPhysiNode*)pOper->pPhyNode;
×
1180
  resetBasicOperatorState(&pInfo->binfo);
×
1181

1182
  int32_t code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
×
1183
    &pTaskInfo->storageAPI.functionStore);
1184

1185
  clearPartitionOperator(pInfo);
×
1186

1187
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
×
1188
  while (pGroupIter != NULL) {
×
1189
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1190
    taosArrayDestroy(pGroupInfo->pPageList);
×
1191
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1192
  }
1193
  taosHashClear(pInfo->pGroupSet);
×
1194

1195
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
×
1196
  for (int32_t i = 0; i < size; i++) {
×
1197
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
×
1198
    if (pGp) {
×
1199
      taosArrayDestroy(pGp->pPageList);
×
1200
    }
1201
  }
1202
  taosArrayDestroy(pInfo->sortedGroupArray);
×
1203
  pInfo->sortedGroupArray = NULL;
×
1204

1205
  pInfo->groupIndex = 0;
×
1206
  pInfo->pageIndex = 0;
×
1207
  pInfo->remainRows = 0;
×
1208
  pInfo->orderedRows = 0;
×
1209
  return 0;
×
1210
}
1211

1212
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
5,353✔
1213
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1214
  QRY_PARAM_CHECK(pOptrInfo);
5,353!
1215

1216
  int32_t                 code = TSDB_CODE_SUCCESS;
5,353✔
1217
  int32_t                 lino = 0;
5,353✔
1218
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
5,353!
1219
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
5,355!
1220
  if (pInfo == NULL || pOperator == NULL) {
5,354!
1221
    pTaskInfo->code = code = terrno;
×
1222
    goto _error;
×
1223
  }
1224

1225
  pOperator->pPhyNode = pPartNode;
5,355✔
1226
  int32_t    numOfCols = 0;
5,355✔
1227
  SExprInfo* pExprInfo = NULL;
5,355✔
1228
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
5,355✔
1229
  QUERY_CHECK_CODE(code, lino, _error);
5,355!
1230
  pOperator->exprSupp.numOfExprs = numOfCols;
5,355✔
1231
  pOperator->exprSupp.pExprInfo = pExprInfo;
5,355✔
1232

1233
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
5,355✔
1234

1235
  if (pPartNode->needBlockOutputTsOrder) {
5,358✔
1236
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
248✔
1237
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
248✔
1238
    if (!pInfo->pOrderInfoArr) {
248!
1239
      pTaskInfo->code = terrno;
×
1240
      goto _error;
×
1241
    }
1242

1243
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
248✔
1244
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
248!
1245
  }
1246

1247
  if (pPartNode->pExprs != NULL) {
5,358✔
1248
    int32_t    num = 0;
66✔
1249
    SExprInfo* pExprInfo1 = NULL;
66✔
1250
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
66✔
1251
    QUERY_CHECK_CODE(code, lino, _error);
66!
1252

1253
    code = initExprSupp(&pInfo->scalarSup, pExprInfo1, num, &pTaskInfo->storageAPI.functionStore);
66✔
1254
    QUERY_CHECK_CODE(code, lino, _error);
66!
1255
  }
1256

1257
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
5,358✔
1258
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
5,355✔
1259
  if (pInfo->pGroupSet == NULL) {
5,358!
1260
    goto _error;
×
1261
  }
1262

1263
  uint32_t defaultPgsz = 0;
5,358✔
1264
  int64_t  defaultBufsz = 0;
5,358✔
1265

1266
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
5,358✔
1267
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
5,357!
1268
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
5,357✔
1269
  if (code != TSDB_CODE_SUCCESS) {
5,357!
1270
    goto _error;
×
1271
  }
1272

1273
  if (!osTempSpaceAvailable()) {
5,357!
1274
    terrno = TSDB_CODE_NO_DISKSPACE;
×
1275
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
×
1276
    goto _error;
×
1277
  }
1278

1279
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
5,355✔
1280
  if (code != TSDB_CODE_SUCCESS) {
5,355!
1281
    goto _error;
×
1282
  }
1283

1284
  pInfo->rowCapacity =
5,357✔
1285
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
5,356✔
1286
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
5,355✔
1287
  if (pInfo->rowCapacity < 0) {
5,357!
1288
    code = terrno;
×
1289
    goto _error;
×
1290
  }
1291

1292
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
5,357✔
1293
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
5,357!
1294

1295
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
5,357✔
1296
  if (code != TSDB_CODE_SUCCESS) {
5,357!
1297
    goto _error;
×
1298
  }
1299

1300
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
5,357✔
1301
                  pTaskInfo);
1302

1303
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
5,356✔
1304
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1305

1306
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
5,357✔
1307
  code = appendDownstream(pOperator, &downstream, 1);
5,358✔
1308
  if (code != TSDB_CODE_SUCCESS) {
5,356!
1309
    goto _error;
×
1310
  }
1311

1312
  *pOptrInfo = pOperator;
5,356✔
1313
  return TSDB_CODE_SUCCESS;
5,356✔
1314

1315
_error:
×
1316
  if (pInfo != NULL) {
×
1317
    destroyPartitionOperatorInfo(pInfo);
×
1318
  }
1319
  pTaskInfo->code = code;
×
1320
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
1321
  TAOS_RETURN(code);
×
1322
}
1323

1324
int32_t setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
18,858,458✔
1325
                                int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
1326
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
18,858,458✔
1327
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
18,858,458✔
1328
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
18,858,458✔
1329

1330
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
18,858,458✔
1331
                                                  false, pAggSup, false);
1332
  if (pResultRow == NULL || pTaskInfo->code != 0) {
18,892,896!
1333
    return pTaskInfo->code;
×
1334
  }
1335

1336
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
18,894,395✔
1337
}
1338

1339
SSDataBlock* buildCreateTableBlock(SExprSupp* tbName, SExprSupp* tag) {
1,000✔
1340
  int32_t      code = TSDB_CODE_SUCCESS;
1,000✔
1341
  int32_t      lino = 0;
1,000✔
1342
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1,000!
1343
  if (!pBlock) {
1,005!
1344
    return NULL;
×
1345
  }
1346
  pBlock->info.hasVarCol = false;
1,005✔
1347
  pBlock->info.id.groupId = 0;
1,005✔
1348
  pBlock->info.rows = 0;
1,005✔
1349
  pBlock->info.type = STREAM_CREATE_CHILD_TABLE;
1,005✔
1350
  pBlock->info.watermark = INT64_MIN;
1,005✔
1351

1352
  pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));
1,005✔
1353
  QUERY_CHECK_NULL(pBlock->pDataBlock, code, lino, _end, terrno);
1,005!
1354
  SColumnInfoData infoData = {0};
1,005✔
1355
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
1,005✔
1356
  if (tbName->numOfExprs > 0) {
1,005!
1357
    infoData.info.bytes = tbName->pExprInfo->base.resSchema.bytes;
×
1358
  } else {
1359
    infoData.info.bytes = 1;
1,005✔
1360
  }
1361
  pBlock->info.rowSize += infoData.info.bytes;
1,005✔
1362
  // sub table name
1363
  void* tmp = taosArrayPush(pBlock->pDataBlock, &infoData);
1,005✔
1364
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,005!
1365

1366
  SColumnInfoData gpIdData = {0};
1,005✔
1367
  gpIdData.info.type = TSDB_DATA_TYPE_UBIGINT;
1,005✔
1368
  gpIdData.info.bytes = 8;
1,005✔
1369
  pBlock->info.rowSize += gpIdData.info.bytes;
1,005✔
1370
  // group id
1371
  tmp = taosArrayPush(pBlock->pDataBlock, &gpIdData);
1,005✔
1372
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,005!
1373

1374
  for (int32_t i = 0; i < tag->numOfExprs; i++) {
1,005!
1375
    SColumnInfoData tagCol = {0};
×
1376
    tagCol.info.type = tag->pExprInfo[i].base.resSchema.type;
×
1377
    tagCol.info.bytes = tag->pExprInfo[i].base.resSchema.bytes;
×
1378
    tagCol.info.precision = tag->pExprInfo[i].base.resSchema.precision;
×
1379
    // tag info
1380
    tmp = taosArrayPush(pBlock->pDataBlock, &tagCol);
×
1381
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1382
    pBlock->info.rowSize += tagCol.info.bytes;
×
1383
  }
1384

1385
_end:
1,005✔
1386
  if (code != TSDB_CODE_SUCCESS) {
1,005!
1387
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1388
    blockDataDestroy(pBlock);
×
1389
    return NULL;
×
1390
  }
1391
  return pBlock;
1,005✔
1392
}
1393

1394
void freePartItem(void* ptr) {
×
1395
  SPartitionDataInfo* pPart = (SPartitionDataInfo*)ptr;
×
1396
  taosArrayDestroy(pPart->rowIds);
×
1397
}
×
1398

1399
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
57,582✔
1400
  int32_t code = TSDB_CODE_SUCCESS;
57,582✔
1401
  int32_t lino = 0;
57,582✔
1402
  size_t  numOfCols = LIST_LENGTH(pNodeList);
57,582!
1403
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
57,582✔
1404
  if (pList == NULL) {
57,610!
1405
    code = terrno;
×
1406
    (*pArrayRes) = NULL;
×
1407
    QUERY_CHECK_CODE(code, lino, _end);
×
1408
  }
1409

1410
  for (int32_t i = 0; i < numOfCols; ++i) {
154,407✔
1411
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
96,791✔
1412
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
96,655!
1413

1414
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
96,662!
1415
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
96,662✔
1416

1417
      SColumn c = extractColumnFromColumnNode(pColNode);
96,662✔
1418
      void*   tmp = taosArrayPush(pList, &c);
96,797✔
1419
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
96,797!
1420
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
×
1421
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
×
1422
      SColumn     c = {0};
×
1423
      c.slotId = pNode->slotId;
×
1424
      c.colId = pNode->slotId;
×
1425
      c.type = pValNode->node.type;
×
1426
      c.bytes = pValNode->node.resType.bytes;
×
1427
      c.scale = pValNode->node.resType.scale;
×
1428
      c.precision = pValNode->node.resType.precision;
×
1429

1430
      void* tmp = taosArrayPush(pList, &c);
×
1431
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1432
    }
1433
  }
1434

1435
  (*pArrayRes) = pList;
57,616✔
1436

1437
_end:
57,616✔
1438
  if (code != TSDB_CODE_SUCCESS) {
57,616!
1439
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1440
  }
1441
  return code;
57,609✔
1442
}
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