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

taosdata / TDengine / #4673

14 Aug 2025 01:04PM UTC coverage: 59.715% (+0.01%) from 59.704%
#4673

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

137065 of 292055 branches covered (46.93%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

1656 existing lines in 17 files now uncovered.

207155 of 284385 relevant lines covered (72.84%)

20501687.34 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) {
795,068✔
75
  SGroupKeys* pKey = (SGroupKeys*)param;
795,068✔
76
  taosMemoryFree(pKey->pData);
795,068!
77
}
795,244✔
78

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

85
  cleanupBasicInfo(&pInfo->binfo);
547,016✔
86
  taosMemoryFreeClear(pInfo->keyBuf);
547,136!
87
  taosArrayDestroy(pInfo->pGroupCols);
547,131✔
88
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
547,117✔
89
  cleanupExprSupp(&pInfo->scalarSup);
547,095✔
90

91
  if (pInfo->pOperator != NULL) {
547,032!
92
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
547,035✔
93
                      false);
94
    pInfo->pOperator = NULL;
546,944✔
95
  }
96

97
  cleanupGroupResInfo(&pInfo->groupResInfo);
546,941✔
98
  cleanupAggSup(&pInfo->aggSup);
546,972✔
99
  taosMemoryFreeClear(param);
547,117!
100
}
101

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

108
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
563,990✔
109
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
1,380,298✔
110
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
816,273✔
111
    if (!pCol) {
815,752!
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
815,752✔
116

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

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

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

135
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
564,025!
136
  if ((*keyBuf) == NULL) {
564,022!
137
    return terrno;
×
138
  }
139

140
  return TSDB_CODE_SUCCESS;
564,022✔
141
}
142

143
static bool groupKeyCompare(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex,
62,235,031✔
144
                            int32_t numOfGroupCols) {
145
  SColumnDataAgg* pColAgg = NULL;
62,235,031✔
146
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
89,008,311✔
147
    SColumn*         pCol = taosArrayGet(pGroupCols, i);
64,505,562✔
148
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
64,396,947✔
149
    if (pBlock->pBlockAgg != NULL) {
64,354,493!
150
      pColAgg = &pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
×
151
    }
152

153
    bool isNull = colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg);
64,354,493✔
154

155
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
64,354,493✔
156
    if (pkey->isNull && isNull) {
64,273,521✔
157
      continue;
2,399,707✔
158
    }
159

160
    if (isNull || pkey->isNull) {
61,873,814✔
161
      return false;
1,297,410✔
162
    }
163

164
    char* val = colDataGetData(pColInfoData, rowIndex);
60,576,404!
165

166
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
60,576,404✔
167
      int32_t dataLen = getJsonValueLen(val);
28✔
168

169
      if (memcmp(pkey->pData, val, dataLen) == 0) {
28✔
170
        continue;
4✔
171
      } else {
172
        return false;
24✔
173
      }
174
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
60,576,376!
175
      if (IS_STR_DATA_BLOB(pkey->type)) {
34,201,762!
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);
34,347,437✔
184
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
34,347,437✔
185
          continue;
21,978,081✔
186
        } else {
187
          return false;
12,369,356✔
188
        }
189
      }
190
    } else {
191
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
26,374,614✔
192
        return false;
23,979,126✔
193
      }
194
    }
195
  }
196

197
  return true;
24,502,749✔
198
}
199

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

203
  size_t numOfGroupCols = taosArrayGetSize(pGroupCols);
87,396,572✔
204

205
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
231,344,769✔
206
    SColumn*         pCol = (SColumn*)taosArrayGet(pGroupCols, i);
145,615,678✔
207
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
145,232,286✔
208

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

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

218
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
144,571,919✔
219
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
288,369,752✔
220
      pkey->isNull = true;
13,496,512✔
221
    } else {
222
      pkey->isNull = false;
130,688,364✔
223
      char* val = colDataGetData(pColInfoData, rowIndex);
130,688,364!
224
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
130,688,364✔
225
        if (tTagIsJson(val)) {
184✔
226
          terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
4✔
227
          return;
4✔
228
        }
229
        int32_t dataLen = getJsonValueLen(val);
180✔
230
        memcpy(pkey->pData, val, dataLen);
180✔
231
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
130,688,180!
232
        if (IS_STR_DATA_BLOB(pkey->type)) {
78,769,853!
233
          memcpy(pkey->pData, val, blobDataTLen(val));
×
234
        } else {
235
          memcpy(pkey->pData, val, varDataTLen(val));
80,262,286✔
236
        }
237
      } else {
238
        memcpy(pkey->pData, val, pkey->bytes);
51,918,327✔
239
      }
240
    }
241
  }
242
}
243

244
static int32_t buildGroupKeys(void* pKey, const SArray* pGroupColVals) {
86,849,548✔
245
  size_t numOfGroupCols = taosArrayGetSize(pGroupColVals);
86,849,548✔
246

247
  char* isNull = (char*)pKey;
86,733,780✔
248
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
86,733,780✔
249
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
232,724,175✔
250
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
146,080,621✔
251
    if (pkey->isNull) {
145,990,395✔
252
      isNull[i] = 1;
13,661,216✔
253
      continue;
13,661,216✔
254
    }
255

256
    isNull[i] = 0;
132,329,179✔
257
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
132,329,179✔
258
      int32_t dataLen = getJsonValueLen(pkey->pData);
180✔
259
      memcpy(pStart, (pkey->pData), dataLen);
180✔
260
      pStart += dataLen;
180✔
261
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
132,328,999!
262
      if (IS_STR_DATA_BLOB(pkey->type)) {
79,967,510!
263
        blobDataCopy(pStart, pkey->pData);
×
264
        pStart += blobDataTLen(pkey->pData);
×
265
      } else {
266
        varDataCopy(pStart, pkey->pData);
80,478,182✔
267
        pStart += varDataTLen(pkey->pData);
80,478,182✔
268
      }
269
    } else {
270
      memcpy(pStart, pkey->pData, pkey->bytes);
52,361,489✔
271
      pStart += pkey->bytes;
52,361,489✔
272
    }
273
  }
274

275
  return (int32_t)(pStart - (char*)pKey);
86,643,554✔
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) {
38,422,148✔
280
  for (int32_t i = 0; i < numOfOutput; ++i) {
141,473,619✔
281
    if (pCtx[i].functionId == -1) {  // select count(*),key from t group by key.
103,051,471✔
282
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[i]);
62,827,209✔
283

284
      SColumnInfoData* pColInfoData = pCtx[i].input.pData[0];
62,827,209✔
285
      // todo OPT all/all not NULL
286
      if (!colDataIsNull(pColInfoData, totalRows, rowIndex, NULL)) {
125,654,418✔
287
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
53,672,938✔
288
        char* data = colDataGetData(pColInfoData, rowIndex);
53,672,938!
289

290
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
53,672,938✔
291
          int32_t dataLen = getJsonValueLen(data);
52✔
292
          memcpy(dest, data, dataLen);
52✔
293
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
53,672,886!
294
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
16,189,543!
295
            blobDataCopy(dest, data);
×
296
          } else {
297
            varDataCopy(dest, data);
16,275,767✔
298
          }
299
        } else {
300
          memcpy(dest, data, pColInfoData->info.bytes);
37,483,343✔
301
        }
302
      } else {  // it is a NULL value
303
        pEntryInfo->isNullRes = 1;
9,154,271✔
304
      }
305

306
      pEntryInfo->numOfRes = 1;
62,827,209✔
307
    }
308
  }
309
}
38,422,148✔
310

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

315
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
9,936,192✔
316
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
9,936,192✔
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;
9,936,372✔
323
  terrno = TSDB_CODE_SUCCESS;
9,936,372✔
324

325
  int32_t num = 0;
9,951,086✔
326
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
72,602,415✔
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) {
62,666,256✔
329
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
490,561✔
330
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
490,520✔
331
        T_LONG_JMP(pTaskInfo->env, terrno);
4!
332
      }
333
      pInfo->isInit = true;
490,490✔
334
      num++;
490,490✔
335
      continue;
490,490✔
336
    }
337

338
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
62,175,695✔
339
    if (equal) {
62,139,245✔
340
      num++;
24,507,551✔
341
      continue;
24,507,551✔
342
    }
343

344
    // The first row of a new block does not belongs to the previous existed group
345
    if (j == 0) {
37,631,694✔
346
      num++;
9,272,072✔
347
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
9,272,072✔
348
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
9,271,886!
349
        T_LONG_JMP(pTaskInfo->env, terrno);
×
350
      }
351
      continue;
9,271,820✔
352
    }
353

354
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
28,359,622✔
355
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
28,387,377✔
356
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
357
    if (ret != TSDB_CODE_SUCCESS) {  // null data, too many state code
28,457,923!
358
      T_LONG_JMP(pTaskInfo->env, ret);
×
359
    }
360

361
    int32_t rowIndex = j - num;
28,457,923✔
362
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
28,457,923✔
363
                                          pOperator->exprSupp.numOfExprs);
364
    if (ret != TSDB_CODE_SUCCESS) {
28,494,372!
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);
28,494,372✔
370
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
28,546,005✔
371
    num = 1;
28,381,468✔
372
  }
373

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

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

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

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

404
  SSDataBlock* pBlock = pInfo->binfo.pRes;
12,608,177✔
405

406
  // set output datablock version
407
  pBlock->info.version = pTaskInfo->version;
12,608,177✔
408

409
  blockDataCleanup(pBlock);
12,608,177✔
410
  if (!hasRemainResultByHash(pOperator)) {
12,609,096✔
411
    return;
56,254✔
412
  }
413

414
  pBlock->info.id.groupId = 0;
12,552,782✔
415
  if (!pInfo->binfo.mergeResultBlock) {
12,552,782✔
416
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
12,131,747✔
417
                             pHashmap, pOperator->resultInfo.threshold, false);
418
  } else {
419
    while (hasRemainResultByHash(pOperator)) {
841,689✔
420
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
420,655✔
421
                               pHashmap, pOperator->resultInfo.threshold, true);
422
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
420,654!
423
        break;
×
424
      }
425
      pBlock->info.id.groupId = 0;
420,654✔
426
    }
427

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

433
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
12,608,133✔
434
  int32_t               code = TSDB_CODE_SUCCESS;
12,608,133✔
435
  int32_t               lino = 0;
12,608,133✔
436
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
12,608,133✔
437
  SGroupbyOperatorInfo* pInfo = pOperator->info;
12,608,133✔
438
  SSDataBlock*          pRes = pInfo->binfo.pRes;
12,608,133✔
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);
12,608,133✔
443

444
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL);
12,592,409✔
445
    QUERY_CHECK_CODE(code, lino, _end);
12,593,079!
446

447
    if (!hasRemainResultByHash(pOperator)) {
12,593,079✔
448
      setOperatorCompleted(pOperator);
546,298✔
449
      // clean hash after completed
450
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
546,782✔
451
      pInfo->aggSup.pResultRowHashTable = NULL;
547,476✔
452
      break;
547,476✔
453
    }
454
    if (pRes->info.rows > 0) {
12,052,869!
455
      break;
12,052,869✔
456
    }
457
  }
458

459
  pOperator->resultInfo.totalRows += pRes->info.rows;
12,600,345✔
460

461
_end:
12,600,345✔
462
  if (code != TSDB_CODE_SUCCESS) {
12,600,345!
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;
12,600,345✔
467
}
468

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

478
  QRY_PARAM_CHECK(ppRes);
13,096,523!
479
  if (pOperator->status == OP_EXEC_DONE) {
13,096,523✔
480
    return code;
489,275✔
481
  }
482

483
  if (pOperator->status == OP_RES_TO_RETURN) {
12,607,248✔
484
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
12,061,230✔
485
    return code;
12,053,351✔
486
  }
487

488
  while (1) {
9,936,350✔
489
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
10,482,368✔
490
    if (pBlock == NULL) {
10,482,794✔
491
      break;
546,915✔
492
    }
493

494
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
9,935,879✔
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);
9,935,879✔
498
    QUERY_CHECK_CODE(code, lino, _end);
9,936,284!
499

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

507
    doHashGroupbyAgg(pOperator, pBlock);
9,936,241✔
508
  }
509

510
  pOperator->status = OP_RES_TO_RETURN;
546,915✔
511

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

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

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

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

528
_end:
546,852✔
529
  if (code != TSDB_CODE_SUCCESS) {
546,852!
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);
546,852✔
535
  }
536

537
  return code;
546,975✔
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,
546,484✔
568
                                SOperatorInfo** pOptrInfo) {
569
  QRY_PARAM_CHECK(pOptrInfo);
546,484!
570

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

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

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

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

594
  int32_t    numOfScalarExpr = 0;
546,777✔
595
  SExprInfo* pScalarExprInfo = NULL;
546,777✔
596
  if (pAggNode->pExprs != NULL) {
546,777✔
597
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
145,599✔
598
    QUERY_CHECK_CODE(code, lino, _error);
145,575!
599
  }
600

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

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

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

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

614
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
546,993✔
615
  QUERY_CHECK_CODE(code, lino, _error);
546,984!
616

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

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

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

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

632
  pInfo->pOperator = pOperator;
546,881✔
633

634
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
546,881✔
635
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
636
  setOperatorResetStateFn(pOperator, resetGroupOperState);
546,728✔
637
  code = appendDownstream(pOperator, &downstream, 1);
546,762✔
638
  QUERY_CHECK_CODE(code, lino, _error);
546,844!
639

640
  *pOptrInfo = pOperator;
546,844✔
641
  return TSDB_CODE_SUCCESS;
546,844✔
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) {
201,891✔
708
  int32_t                 code = TSDB_CODE_SUCCESS;
201,891✔
709
  int32_t                 lino = 0;
201,891✔
710
  SPartitionOperatorInfo* pInfo = pOperator->info;
201,891✔
711
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
201,891✔
712

713
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
49,348,933✔
714
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
49,147,188✔
715
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
48,333,076✔
716

717
    SDataGroupInfo* pGroupInfo = NULL;
48,335,657✔
718
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
48,335,657✔
719
    if (pPage == NULL) {
49,418,729!
720
      T_LONG_JMP(pTaskInfo->env, terrno);
×
721
    }
722

723
    pGroupInfo->numOfRows += 1;
49,418,729✔
724

725
    // group id
726
    if (pGroupInfo->groupId == 0) {
49,418,729✔
727
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
120,649✔
728
    }
729

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

734
      size_t numOfCols = pOperator->exprSupp.numOfExprs;
49,418,766✔
735
      for (int32_t i = 0; i < numOfCols; ++i) {
254,204,670✔
736
        SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
202,767,204✔
737
        int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
202,767,204✔
738

739
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
202,767,204✔
740

741
        int32_t bytes = pColInfoData->info.bytes;
203,038,693✔
742
        int32_t startOffset = pInfo->columnOffset[i];
203,038,693✔
743

744
        int32_t* columnLen = NULL;
203,038,693✔
745
        int32_t  contentLen = 0;
203,038,693✔
746

747
        if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
262,020,942!
748
          int32_t* offset = (int32_t*)((char*)pPage + startOffset);
58,982,249✔
749
          columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
58,982,249✔
750
          char* data = (char*)((char*)columnLen + sizeof(int32_t));
58,982,249✔
751

752
          if (colDataIsNull_s(pColInfoData, j)) {
117,964,498✔
753
            offset[(*rows)] = -1;
3,577,006✔
754
            contentLen = 0;
3,577,006✔
755
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
55,405,243✔
756
            offset[*rows] = (*columnLen);
128✔
757
            char*   src = colDataGetData(pColInfoData, j);
128!
758
            int32_t dataLen = getJsonValueLen(src);
128✔
759

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

764
            contentLen = dataLen;
128✔
765
          } else {
766
            if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
55,405,115!
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);
60,747,622✔
776
              char* src = colDataGetData(pColInfoData, j);
60,747,622!
777
              memcpy(data + (*columnLen), src, varDataTLen(src));
60,747,622✔
778
              int32_t v = (data + (*columnLen) + varDataTLen(src) - (char*)pPage);
60,747,622✔
779
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
60,747,622!
780

781
              contentLen = varDataTLen(src);
60,747,622✔
782
            }
783
          }
784
        } else {
785
          char* bitmap = (char*)pPage + startOffset;
144,056,444✔
786
          columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
144,056,444✔
787
          char* data = (char*)columnLen + sizeof(int32_t);
144,056,444✔
788

789
          bool isNull = colDataIsNull_f(pColInfoData, j);
144,056,444✔
790
          if (isNull) {
144,056,444✔
791
            colDataSetNull_f(bitmap, (*rows));
10,008,860✔
792
          } else {
793
            memcpy(data + (*columnLen), colDataGetData(pColInfoData, j), bytes);
134,047,584!
794
            QUERY_CHECK_CONDITION(((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf)), code,
134,047,584!
795
                                  lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
796
          }
797
          contentLen = bytes;
145,803,655✔
798
        }
799

800
        (*columnLen) += contentLen;
204,785,904✔
801
      }
802

803
      (*rows) += 1;
51,437,466✔
804

805
      setBufPageDirty(pPage, true);
51,437,466✔
806
      releaseBufPage(pInfo->pBuf, pPage);
49,610,512✔
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:
201,745✔
826
  if (code != TSDB_CODE_SUCCESS) {
201,745!
827
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
828
    T_LONG_JMP(pTaskInfo->env, code);
×
829
  }
830
}
201,745✔
831

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

837
  void* pPage = NULL;
49,881,743✔
838
  if (p == NULL) {  // it is a new group
49,881,743✔
839
    SDataGroupInfo gi = {0};
120,557✔
840
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
120,557✔
841
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
120,625!
842

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

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

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

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

860
    *(int32_t*)pPage = 0;
120,668✔
861
  } else {
862
    int32_t* curId = taosArrayGetLast(p->pPageList);
49,761,186✔
863
    pPage = getBufPage(pInfo->pBuf, *curId);
49,638,410✔
864
    if (pPage == NULL) {
49,376,583!
865
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
866
      return pPage;
×
867
    }
868

869
    int32_t* rows = (int32_t*)pPage;
49,376,583✔
870
    if (*rows >= pInfo->rowCapacity) {
49,376,583✔
871
      // release buffer
872
      releaseBufPage(pInfo->pBuf, pPage);
1,155,824✔
873

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

882
      void* tmp = taosArrayPush(p->pPageList, &pageId);
1,158,718✔
883
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,158,347!
884

885
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
1,158,347✔
886
    }
887
  }
888

889
  *pGroupInfo = p;
49,499,508✔
890

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

897
  return pPage;
49,499,508✔
898
}
899

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

907
  offset[0] = sizeof(int32_t) +
17,043✔
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) {
126,412✔
911
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
109,401✔
912

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

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

925
  return offset;
17,011✔
926
}
927

928
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
16,946✔
929
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
16,946✔
930
  for (int32_t i = 0; i < size; i++) {
110,861✔
931
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
93,913✔
932
    if (pGp && pGp->blockForNotLoaded) {
93,913!
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);
93,913✔
941
  }
942
  taosArrayClear(pInfo->sortedGroupArray);
16,948✔
943
  clearDiskbasedBuf(pInfo->pBuf);
16,948✔
944
}
16,948✔
945

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

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

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

957
static SSDataBlock* buildPartitionResultForNotLoadBlock(SDataGroupInfo* pGroupInfo) {
94,636✔
958
  if (pGroupInfo->blockForNotLoaded && pGroupInfo->offsetForNotLoaded < pGroupInfo->blockForNotLoaded->size) {
94,636!
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;
94,636✔
967
}
968

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

975
  if (pInfo->remainRows == 0) {
1,372,619✔
976
    blockDataCleanup(pInfo->binfo.pRes);
1,177,899✔
977
    SDataGroupInfo* pGroupInfo =
1,177,825✔
978
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
1,177,949✔
979
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
1,177,825✔
980
      if (pGroupInfo != NULL) {
111,679✔
981
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
94,636✔
982
        if (ret != NULL) return ret;
94,636!
983
      }
984
      // try next group data
985
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
111,679✔
986
        setOperatorCompleted(pOperator);
16,947✔
987
        clearPartitionOperator(pInfo);
16,946✔
988
        return NULL;
16,948✔
989
      }
990
      ++pInfo->groupIndex;
94,720✔
991

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

1000
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
1,160,797✔
1001
    if (pageId == NULL) {
1,160,509!
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);
1,160,509✔
1006
    if (page == NULL) {
1,160,351!
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) {
1,160,444!
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);
1,160,444✔
1026
    QUERY_CHECK_CODE(code, lino, _end);
1,160,456!
1027

1028
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
1,160,456✔
1029
    QUERY_CHECK_CODE(code, lino, _end);
1,160,965!
1030

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

1040
  if (pInfo->pOrderInfoArr) {
1,355,555✔
1041
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
575,320✔
1042
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
575,320✔
1043
    QUERY_CHECK_CODE(code, lino, _end);
575,336!
1044
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
575,336✔
1045
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
575,597✔
1046
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
575,597✔
1047
  }
1048

1049
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
1,355,832✔
1050
  QUERY_CHECK_CODE(code, lino, _end);
1,355,598!
1051

1052
_end:
1,355,598✔
1053
  if (code != TSDB_CODE_SUCCESS) {
1,355,598!
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;
1,355,598✔
1059
  return pInfo->binfo.pRes;
1,355,598✔
1060
}
1061

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

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

1074
  if (pOperator->status == OP_RES_TO_RETURN) {
1,372,591✔
1075
    (*ppRes) = buildPartitionResult(pOperator);
1,355,583✔
1076
    return code;
1,355,486✔
1077
  }
1078

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

1082
  while (1) {
201,745✔
1083
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
218,779✔
1084
    if (pBlock == NULL) {
218,928✔
1085
      break;
17,046✔
1086
    }
1087

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

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

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

1108
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
17,044✔
1109
  while (pGroupIter != NULL) {
137,757✔
1110
    SDataGroupInfo* pGroupInfo = pGroupIter;
120,711✔
1111
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
120,710✔
1112
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
120,710!
1113
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
120,710✔
1114
  }
1115

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

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

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

1127
_end:
17,046✔
1128
  if (code != TSDB_CODE_SUCCESS) {
17,046!
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);
17,046✔
1135
  return code;
17,045✔
1136
}
1137

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

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

1148
  taosArrayDestroy(pInfo->pGroupColVals);
17,044✔
1149
  taosMemoryFree(pInfo->keyBuf);
17,047!
1150

1151
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
17,047✔
1152
  for (int32_t i = 0; i < size; i++) {
43,840✔
1153
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
26,794✔
1154
    if (pGp) {
26,794!
1155
      taosArrayDestroy(pGp->pPageList);
26,794✔
1156
    }
1157
  }
1158
  taosArrayDestroy(pInfo->sortedGroupArray);
17,046✔
1159

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

1167
  taosHashCleanup(pInfo->pGroupSet);
17,041✔
1168
  taosMemoryFree(pInfo->columnOffset);
17,047!
1169

1170
  cleanupExprSupp(&pInfo->scalarSup);
17,047✔
1171
  destroyDiskbasedBuf(pInfo->pBuf);
17,047✔
1172
  taosArrayDestroy(pInfo->pOrderInfoArr);
17,045✔
1173
  taosMemoryFreeClear(param);
17,043!
1174
}
17,043✔
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,
17,013✔
1213
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1214
  QRY_PARAM_CHECK(pOptrInfo);
17,013!
1215

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

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

1233
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
17,041✔
1234

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

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

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

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

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

1263
  uint32_t defaultPgsz = 0;
17,041✔
1264
  int64_t  defaultBufsz = 0;
17,041✔
1265

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

1273
  if (!osTempSpaceAvailable()) {
17,045!
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);
17,036✔
1280
  if (code != TSDB_CODE_SUCCESS) {
17,044!
1281
    goto _error;
×
1282
  }
1283

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

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

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

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

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

1306
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
17,027✔
1307
  code = appendDownstream(pOperator, &downstream, 1);
17,025✔
1308
  if (code != TSDB_CODE_SUCCESS) {
17,033!
1309
    goto _error;
×
1310
  }
1311

1312
  *pOptrInfo = pOperator;
17,033✔
1313
  return TSDB_CODE_SUCCESS;
17,033✔
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,
38,347,750✔
1325
                                int32_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
1326
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
38,347,750✔
1327
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
38,347,750✔
1328
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
38,347,750✔
1329

1330
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
38,347,750✔
1331
                                                  false, pAggSup, false);
1332
  if (pResultRow == NULL || pTaskInfo->code != 0) {
38,549,737!
1333
    return pTaskInfo->code;
×
1334
  }
1335

1336
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
38,551,436✔
1337
}
1338

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

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

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

1374
  for (int32_t i = 0; i < tag->numOfExprs; i++) {
1,237!
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,237✔
1386
  if (code != TSDB_CODE_SUCCESS) {
1,237!
1387
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1388
    blockDataDestroy(pBlock);
×
1389
    return NULL;
×
1390
  }
1391
  return pBlock;
1,237✔
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) {
546,558✔
1400
  int32_t code = TSDB_CODE_SUCCESS;
546,558✔
1401
  int32_t lino = 0;
546,558✔
1402
  size_t  numOfCols = LIST_LENGTH(pNodeList);
546,558!
1403
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
546,558✔
1404
  if (pList == NULL) {
546,663!
UNCOV
1405
    code = terrno;
×
1406
    (*pArrayRes) = NULL;
×
1407
    QUERY_CHECK_CODE(code, lino, _end);
×
1408
  }
1409

1410
  for (int32_t i = 0; i < numOfCols; ++i) {
1,341,262✔
1411
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
794,296✔
1412
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
793,699!
1413

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

1417
      SColumn c = extractColumnFromColumnNode(pColNode);
793,785✔
1418
      void*   tmp = taosArrayPush(pList, &c);
794,599✔
1419
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
794,599!
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;
546,966✔
1436

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