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

taosdata / TDengine / #4922

09 Jan 2026 08:13AM UTC coverage: 65.161% (-0.4%) from 65.541%
#4922

push

travis-ci

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

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

2171 existing lines in 120 files now uncovered.

197632 of 303297 relevant lines covered (65.16%)

117870313.81 hits per line

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

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

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

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

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

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

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

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

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

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

75
static void freeGroupKey(void* param) {
61,391,071✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
61,391,071✔
77
  taosMemoryFree(pKey->pData);
61,391,071✔
78
}
61,388,171✔
79

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

86
  cleanupBasicInfo(&pInfo->binfo);
43,897,679✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
43,896,801✔
88
  taosArrayDestroy(pInfo->pGroupCols);
43,895,424✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
43,896,903✔
90
  cleanupExprSupp(&pInfo->scalarSup);
43,896,378✔
91

92
  if (pInfo->pOperator != NULL) {
43,897,217✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
42,705,674✔
94
                      false);
95
    pInfo->pOperator = NULL;
42,705,191✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
43,894,783✔
99
  cleanupAggSup(&pInfo->aggSup);
43,896,683✔
100
  taosMemoryFreeClear(param);
43,895,835✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
50,781,945✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
50,781,945✔
105
  if ((*pGroupColVals) == NULL) {
50,766,680✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
50,770,029✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
122,237,487✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
71,463,937✔
112
    if (!pCol) {
71,470,190✔
113
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
114
      return terrno;
×
115
    }
116
    (*keyLen) += pCol->bytes;  // actual data + null_flag
71,470,190✔
117

118
    SGroupKeys key = {0};
71,460,266✔
119
    key.bytes = pCol->bytes;
71,459,218✔
120
    key.type = pCol->type;
71,458,904✔
121
    key.isNull = false;
71,455,928✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
71,455,928✔
123
    if (key.pData == NULL) {
71,450,260✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
71,450,260✔
128
    if (!tmp) {
71,473,003✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
50,773,550✔
134
  (*keyLen) += nullFlagSize;
50,773,550✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
50,775,924✔
137
  if ((*keyBuf) == NULL) {
50,764,974✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
50,758,930✔
142
}
143

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

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

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

161
    if (isNull || pkey->isNull) {
2,147,483,647✔
162
      return false;
23,279,771✔
163
    }
164

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

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

170
      if (memcmp(pkey->pData, val, dataLen) == 0) {
6,216✔
171
        continue;
888✔
172
      } else {
173
        return false;
5,328✔
174
      }
175
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
176
      if (IS_STR_DATA_BLOB(pkey->type)) {
1,915,826,678✔
177
        int32_t len = blobDataLen(val);
130✔
178
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
179
          continue;
×
180
        } else {
181
          return false;
×
182
        }
183
      } else {
184
        int32_t len = varDataLen(val);
1,916,490,762✔
185
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
1,916,503,113✔
186
          continue;
908,031,452✔
187
        } else {
188
          return false;
1,008,596,935✔
189
        }
190
      }
191
    } else {
192
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
2,147,483,647✔
193
        return false;
2,147,483,647✔
194
      }
195
    }
196
  }
197

198
  return true;
1,235,264,963✔
199
}
200

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

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

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

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

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

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

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

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

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

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

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

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

291
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
292
          int32_t dataLen = getJsonValueLen(data);
14,324✔
293
          memcpy(dest, data, dataLen);
14,324✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,438,039,111✔
UNCOV
296
            blobDataCopy(dest, data);
×
297
          } else {
298
            varDataCopy(dest, data);
1,440,516,901✔
299
          }
300
        } else {
301
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
302
        }
303
      } else {  // it is a NULL value
304
        pEntryInfo->isNullRes = 1;
1,153,750,901✔
305
      }
306

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

312
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
255,967,266✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
255,967,266✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
255,996,862✔
315

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

323
  int32_t len = 0;
255,994,790✔
324
  terrno = TSDB_CODE_SUCCESS;
255,994,790✔
325

326
  int32_t num = 0;
255,987,850✔
327
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
328
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
329
    if (!pInfo->isInit) {
2,147,483,647✔
330
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
29,996,717✔
331
      pInfo->isInit = true;
29,995,271✔
332
      num++;
29,995,853✔
333
      continue;
29,995,853✔
334
    }
335

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

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

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

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

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

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

378
    int32_t rowIndex = pBlock->info.rows - num;
255,981,449✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
255,975,158✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
255,957,832✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
255,957,832✔
385
  }
386
}
255,970,913✔
387

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

394
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
1,832,886,812✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,832,886,812✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
1,832,890,685✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,832,889,364✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
1,832,890,709✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
1,832,892,627✔
404

405
  blockDataCleanup(pBlock);
1,832,888,620✔
406
  if (!hasRemainResultByHash(pOperator)) {
1,832,889,669✔
407
    return;
12,262,270✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
1,820,629,837✔
411
  if (!pInfo->binfo.mergeResultBlock) {
1,820,632,285✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
1,796,160,702✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
48,930,356✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
24,470,794✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
24,470,794✔
419
        break;
11,232✔
420
      }
421
      pBlock->info.id.groupId = 0;
24,459,562✔
422
    }
423

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

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
1,790,642,126✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
1,790,642,126✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
57,689✔
432
    return true;  // limit reached, stop processing further rows
17,935✔
433
  }
434
  return false;
1,790,623,851✔
435
}
436

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

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
1,832,898,600✔
442
    return TSDB_CODE_SUCCESS;
12,896,076✔
443
  }
444

445
  if (pLimitInfo && pLimitInfo->remainGroupOffset > 0) {
1,820,003,677✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
55,509✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
11,037✔
448
      blockDataCleanup(pRes);
11,037✔
449
    } else {
450
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
44,472✔
451
      QUERY_CHECK_CODE(code, lino, _end);
44,472✔
452
    }
453
  }
454

455
  pLimitInfo->remainGroupOffset = 0;
1,820,003,677✔
456
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
1,820,003,210✔
457
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
168,178✔
458
    if (pRes->info.rows > remainRows) {
168,178✔
459
      blockDataKeepFirstNRows(pRes, remainRows);
84,155✔
460
    }
461
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
168,178✔
462
  }
463

464
_end:
1,819,835,499✔
465
  if (code != TSDB_CODE_SUCCESS) {
1,820,003,677✔
466
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
467
  }
468
  return code;
1,820,003,166✔
469
}
470

471
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
1,832,888,568✔
472
  int32_t               code = TSDB_CODE_SUCCESS;
1,832,888,568✔
473
  int32_t               lino = 0;
1,832,888,568✔
474
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,832,888,568✔
475
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,832,892,665✔
476
  SSDataBlock*          pRes = pInfo->binfo.pRes;
1,832,890,782✔
477
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
1,832,888,602✔
478

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

483
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
1,832,898,824✔
484
    QUERY_CHECK_CODE(code, lino, _end);
1,832,899,418✔
485

486
    code = doGroupResultSlimit(pRes, pLimitInfo);
1,832,899,418✔
487
    QUERY_CHECK_CODE(code, lino, _end);
1,832,898,089✔
488

489
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
1,832,898,089✔
490
      setOperatorCompleted(pOperator);
42,274,240✔
491
      // clean hash after completed
492
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
42,275,323✔
493
      pInfo->aggSup.pResultRowHashTable = NULL;
42,274,024✔
494
      break;
42,274,024✔
495
    }
496

497
    if (pRes->info.rows > 0) {
1,790,623,340✔
498
      break;
1,790,624,191✔
499
    }
500
  }
501

502
  pOperator->resultInfo.totalRows += pRes->info.rows;
1,832,898,215✔
503

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

512
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,862,346,125✔
513
  int32_t               code = TSDB_CODE_SUCCESS;
1,862,346,125✔
514
  int32_t               lino = 0;
1,862,346,125✔
515
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,862,346,125✔
516
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,862,344,619✔
517
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
1,862,341,586✔
518
  int32_t               order = pInfo->binfo.inputTsOrder;
1,862,347,241✔
519
  int64_t               st = taosGetTimestampUs();
1,862,336,713✔
520

521
  QRY_PARAM_CHECK(ppRes);
1,862,336,713✔
522
  if (pOperator->status == OP_EXEC_DONE) {
1,862,335,601✔
523
    return code;
29,087,176✔
524
  }
525

526
  if (pOperator->status == OP_RES_TO_RETURN) {
1,833,246,510✔
527
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
1,790,618,573✔
528
    return code;
1,790,622,539✔
529
  }
530

531
  while (1) {
255,955,789✔
532
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
298,591,220✔
533
    if (pBlock == NULL) {
298,609,178✔
534
      break;
42,275,909✔
535
    }
536

537
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
256,333,269✔
538

539
    // the pDataBlock are always the same one, no need to call this again
540
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
256,368,083✔
541
    QUERY_CHECK_CODE(code, lino, _end);
256,340,310✔
542

543
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
544
    if (pInfo->scalarSup.pExprInfo != NULL) {
256,340,310✔
545
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
44,549,817✔
546
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
44,559,984✔
547
      QUERY_CHECK_CODE(code, lino, _end);
44,541,834✔
548
    }
549

550
    doHashGroupbyAgg(pOperator, pBlock);
255,959,613✔
551
  }
552

553
  pOperator->status = OP_RES_TO_RETURN;
42,275,909✔
554

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

560
  if (pGroupResInfo->pBuf) {
42,274,517✔
561
    taosMemoryFree(pGroupResInfo->pBuf);
×
562
    pGroupResInfo->pBuf = NULL;
×
563
  }
564

565
  pGroupResInfo->index = 0;
42,274,224✔
566
  pGroupResInfo->iter = 0;
42,275,483✔
567
  pGroupResInfo->dataPos = NULL;
42,274,182✔
568

569
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
42,274,303✔
570

571
_end:
42,640,604✔
572
  if (code != TSDB_CODE_SUCCESS) {
42,640,604✔
573
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
366,980✔
574
    pTaskInfo->code = code;
366,980✔
575
    T_LONG_JMP(pTaskInfo->env, code);
366,980✔
576
  } else {
577
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
42,273,624✔
578
  }
579

580
  return code;
42,274,099✔
581
}
582

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

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

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

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

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

605
  pInfo->isInit = false;
×
606

607
  return code;
×
608
}
609

610
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
43,883,801✔
611
                                SOperatorInfo** pOptrInfo) {
612
  QRY_PARAM_CHECK(pOptrInfo);
43,883,801✔
613

614
  int32_t               code = TSDB_CODE_SUCCESS;
43,888,702✔
615
  int32_t               lino = 0;
43,888,702✔
616
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
43,888,702✔
617
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
43,862,800✔
618
  if (pInfo == NULL || pOperator == NULL) {
43,861,048✔
619
    code = terrno;
1,674✔
620
    goto _error;
×
621
  }
622

623
  pOperator->pPhyNode = (SNode*)pAggNode;
43,859,374✔
624
  pOperator->exprSupp.hasWindowOrGroup = true;
43,864,085✔
625
  pOperator->exprSupp.hasWindow = false;
43,873,638✔
626

627
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
43,880,677✔
628
  if (pResBlock == NULL) {
43,891,580✔
629
    code = terrno;
×
630
    goto _error;
×
631
  }
632
  initBasicInfo(&pInfo->binfo, pResBlock);
43,891,580✔
633

634
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
43,885,613✔
635

636
  pInfo->pGroupCols = NULL;
43,891,828✔
637
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
43,891,631✔
638
  QUERY_CHECK_CODE(code, lino, _error);
43,871,931✔
639

640
  int32_t    numOfScalarExpr = 0;
43,871,931✔
641
  SExprInfo* pScalarExprInfo = NULL;
43,877,719✔
642
  if (pAggNode->pExprs != NULL) {
43,884,189✔
643
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
20,946,782✔
644
    QUERY_CHECK_CODE(code, lino, _error);
20,950,555✔
645
  }
646

647
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
42,686,782✔
648
  QUERY_CHECK_CODE(code, lino, _error);
42,676,280✔
649

650
  initResultSizeInfo(&pOperator->resultInfo, 4096);
42,676,280✔
651
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
42,688,918✔
652
  QUERY_CHECK_CODE(code, lino, _error);
42,698,181✔
653

654
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
42,698,181✔
655
  QUERY_CHECK_CODE(code, lino, _error);
42,679,741✔
656

657
  int32_t    num = 0;
42,679,741✔
658
  SExprInfo* pExprInfo = NULL;
42,682,050✔
659

660
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
42,682,301✔
661
  QUERY_CHECK_CODE(code, lino, _error);
42,684,610✔
662

663
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
85,373,653✔
664
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
42,683,616✔
665
  QUERY_CHECK_CODE(code, lino, _error);
42,679,101✔
666

667
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
42,690,739✔
668
                            pTaskInfo->pStreamRuntimeInfo);
42,679,101✔
669
  QUERY_CHECK_CODE(code, lino, _error);
42,679,396✔
670

671
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
42,679,396✔
672
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
42,681,830✔
673

674
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
42,692,045✔
675
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
42,680,067✔
676
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
42,676,751✔
677

678
  pInfo->pOperator = pOperator;
42,667,281✔
679

680
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
42,682,885✔
681
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
682
  setOperatorResetStateFn(pOperator, resetGroupOperState);
42,669,492✔
683
  code = appendDownstream(pOperator, &downstream, 1);
42,670,430✔
684
  QUERY_CHECK_CODE(code, lino, _error);
42,676,381✔
685

686
  *pOptrInfo = pOperator;
42,676,381✔
687
  return TSDB_CODE_SUCCESS;
42,671,207✔
688

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

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

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

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

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

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

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

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

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

753
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
20,181,953✔
754
  int32_t                 code = TSDB_CODE_SUCCESS;
20,181,953✔
755
  int32_t                 lino = 0;
20,181,953✔
756
  SPartitionOperatorInfo* pInfo = pOperator->info;
20,181,953✔
757
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
20,184,599✔
758

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

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

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

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

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

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

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

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

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

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

798
          if (colDataIsNull_s(pColInfoData, j)) {
2,147,483,647✔
799
            offset[(*rows)] = -1;
306,364,002✔
800
            contentLen = 0;
307,714,782✔
801
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
802
            offset[*rows] = (*columnLen);
22,304✔
803
            char*   src = colDataGetData(pColInfoData, j);
22,304✔
804
            int32_t dataLen = getJsonValueLen(src);
21,964✔
805

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

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

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

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

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

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

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

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

871
_end:
23,354,224✔
872
  if (code != TSDB_CODE_SUCCESS) {
20,186,363✔
873
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
874
    T_LONG_JMP(pTaskInfo->env, code);
×
875
  }
876
}
20,186,363✔
877

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

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

889
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
15,965,272✔
890
    if (code == TSDB_CODE_DUP_KEY) {
15,969,775✔
891
      code = TSDB_CODE_SUCCESS;
×
892
    }
893
    QUERY_CHECK_CODE(code, lino, _end);
15,969,775✔
894

895
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
15,969,775✔
896

897
    int32_t pageId = 0;
15,970,216✔
898
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
15,970,216✔
899
    if (pPage == NULL) {
15,969,466✔
900
      return pPage;
×
901
    }
902

903
    void* tmp = taosArrayPush(p->pPageList, &pageId);
15,969,466✔
904
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
15,969,025✔
905

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

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

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

928
      void* tmp = taosArrayPush(p->pPageList, &pageId);
120,672,845✔
929
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
120,723,609✔
930

931
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
120,723,609✔
932
    }
933
  }
934

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

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

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

946
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
8,086,198✔
947
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
8,086,198✔
948
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
8,086,942✔
949
  if (!offset) {
8,085,377✔
950
    return NULL;
×
951
  }
952

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

956
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
14,360,079✔
957
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
6,275,113✔
958

959
    int32_t bytes = pColInfoData->info.bytes;
6,273,078✔
960
    int32_t payloadLen = bytes * rowCapacity;
6,273,078✔
961

962
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
6,273,078✔
963
      // offset segment + content length + payload
964
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
801,232✔
965
    } else {
966
      // bitmap + content length + payload
967
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
5,468,934✔
968
    }
969
  }
970

971
  return offset;
8,084,966✔
972
}
973

974
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
6,981,385✔
975
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
6,981,385✔
976
  for (int32_t i = 0; i < size; i++) {
20,775,089✔
977
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
13,793,334✔
978
    if (pGp && pGp->blockForNotLoaded) {
13,793,691✔
979
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
980
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
981
        if (pBlock) blockDataDestroy(*pBlock);
×
982
      }
983
      taosArrayClear(pGp->blockForNotLoaded);
×
984
      pGp->offsetForNotLoaded = 0;
×
985
    }
986
    taosArrayDestroy(pGp->pPageList);
13,794,048✔
987
  }
988
  taosArrayClear(pInfo->sortedGroupArray);
6,981,755✔
989
  clearDiskbasedBuf(pInfo->pBuf);
6,981,385✔
990
}
6,981,385✔
991

992
static int compareDataGroupInfo(const void* group1, const void* group2) {
53,057,836✔
993
  const SDataGroupInfo* pGroupInfo1 = group1;
53,057,836✔
994
  const SDataGroupInfo* pGroupInfo2 = group2;
53,057,836✔
995

996
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
53,057,836✔
997
    return 0;
×
998
  }
999

1000
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
53,057,836✔
1001
}
1002

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

1015
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
154,553,577✔
1016
  int32_t                 code = TSDB_CODE_SUCCESS;
154,553,577✔
1017
  int32_t                 lino = 0;
154,553,577✔
1018
  SPartitionOperatorInfo* pInfo = pOperator->info;
154,553,577✔
1019
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
154,560,127✔
1020

1021
  if (pInfo->remainRows == 0) {
154,560,953✔
1022
    blockDataCleanup(pInfo->binfo.pRes);
132,734,858✔
1023
    SDataGroupInfo* pGroupInfo =
132,738,099✔
1024
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
132,737,658✔
1025
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
132,738,099✔
1026
      if (pGroupInfo != NULL) {
21,565,558✔
1027
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
13,845,576✔
1028
        if (ret != NULL) return ret;
13,844,758✔
1029
      }
1030
      // try next group data
1031
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
21,564,740✔
1032
        setOperatorCompleted(pOperator);
6,981,398✔
1033
        clearPartitionOperator(pInfo);
6,981,398✔
1034
        return NULL;
6,981,385✔
1035
      }
1036
      ++pInfo->groupIndex;
14,584,625✔
1037

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

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

1071
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
125,749,361✔
1072
    QUERY_CHECK_CODE(code, lino, _end);
125,755,021✔
1073

1074
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
125,755,021✔
1075
    QUERY_CHECK_CODE(code, lino, _end);
125,757,667✔
1076

1077
    pInfo->pageIndex += 1;
125,757,667✔
1078
    releaseBufPage(pInfo->pBuf, page);
125,757,667✔
1079
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
125,755,021✔
1080
    pInfo->binfo.pRes->info.dataLoad = 1;
125,754,627✔
1081
    pInfo->orderedRows = 0;
125,753,409✔
1082
  } else if (pInfo->pOrderInfoArr == NULL) {
21,828,729✔
1083
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1084
  }
1085

1086
  if (pInfo->pOrderInfoArr) {
147,583,044✔
1087
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
66,607,541✔
1088
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
66,609,305✔
1089
    QUERY_CHECK_CODE(code, lino, _end);
66,603,572✔
1090
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
66,603,572✔
1091
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
66,610,628✔
1092
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
66,610,628✔
1093
  }
1094

1095
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
147,579,516✔
1096
  QUERY_CHECK_CODE(code, lino, _end);
147,576,074✔
1097

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

1104
  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
147,576,074✔
1105
  return pInfo->binfo.pRes;
147,581,735✔
1106
}
1107

1108
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
154,917,723✔
1109
  if (pOperator->status == OP_EXEC_DONE) {
154,917,723✔
1110
    (*ppRes) = NULL;
369✔
1111
    return TSDB_CODE_SUCCESS;
369✔
1112
  }
1113

1114
  int32_t                 code = TSDB_CODE_SUCCESS;
154,928,050✔
1115
  int32_t                 lino = 0;
154,928,050✔
1116
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
154,928,050✔
1117
  SPartitionOperatorInfo* pInfo = pOperator->info;
154,931,482✔
1118
  SSDataBlock*            pRes = pInfo->binfo.pRes;
154,927,788✔
1119

1120
  if (pOperator->status == OP_RES_TO_RETURN) {
154,930,223✔
1121
    (*ppRes) = buildPartitionResult(pOperator);
146,844,644✔
1122
    return code;
146,845,585✔
1123
  }
1124

1125
  int64_t        st = taosGetTimestampUs();
8,086,431✔
1126
  SOperatorInfo* downstream = pOperator->pDownstream[0];
8,086,431✔
1127

1128
  while (1) {
20,186,804✔
1129
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
28,273,746✔
1130
    if (pBlock == NULL) {
27,901,400✔
1131
      break;
7,719,517✔
1132
    }
1133

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

1143
    terrno = TSDB_CODE_SUCCESS;
20,186,363✔
1144
    doHashPartition(pOperator, pBlock);
20,185,922✔
1145
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
20,187,245✔
1146
      code = terrno;
×
UNCOV
1147
      QUERY_CHECK_CODE(code, lino, _end);
×
1148
    }
1149
  }
1150

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

1154
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
7,719,517✔
1155
  while (pGroupIter != NULL) {
23,690,174✔
1156
    SDataGroupInfo* pGroupInfo = pGroupIter;
15,970,192✔
1157
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
15,970,657✔
1158
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
15,970,657✔
1159
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
15,970,657✔
1160
  }
1161

1162
  taosArraySort(groupArray, compareDataGroupInfo);
7,719,982✔
1163
  pInfo->sortedGroupArray = groupArray;
7,719,982✔
1164
  pInfo->groupIndex = -1;
7,719,612✔
1165
  taosHashClear(pInfo->pGroupSet);
7,719,612✔
1166

1167
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
7,719,982✔
1168

1169
  pOperator->status = OP_RES_TO_RETURN;
7,719,982✔
1170
  code = blockDataEnsureCapacity(pRes, 4096);
7,719,982✔
1171
  QUERY_CHECK_CODE(code, lino, _end);
7,719,982✔
1172

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

1180
  (*ppRes) = buildPartitionResult(pOperator);
7,719,982✔
1181
  return code;
7,718,127✔
1182
}
1183

1184
static void destroyPartitionOperatorInfo(void* param) {
8,461,227✔
1185
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
8,461,227✔
1186
  cleanupBasicInfo(&pInfo->binfo);
8,461,227✔
1187
  taosArrayDestroy(pInfo->pGroupCols);
8,461,227✔
1188

1189
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
18,538,887✔
1190
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
10,078,000✔
1191
    taosMemoryFree(key.pData);
10,078,000✔
1192
  }
1193

1194
  taosArrayDestroy(pInfo->pGroupColVals);
8,461,567✔
1195
  taosMemoryFree(pInfo->keyBuf);
8,461,227✔
1196

1197
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
8,461,227✔
1198
  for (int32_t i = 0; i < size; i++) {
10,638,163✔
1199
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
2,176,596✔
1200
    if (pGp) {
2,176,596✔
1201
      taosArrayDestroy(pGp->pPageList);
2,176,596✔
1202
    }
1203
  }
1204
  taosArrayDestroy(pInfo->sortedGroupArray);
8,461,567✔
1205

1206
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
8,461,567✔
1207
  while (pGroupIter != NULL) {
8,461,227✔
UNCOV
1208
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
UNCOV
1209
    taosArrayDestroy(pGroupInfo->pPageList);
×
UNCOV
1210
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1211
  }
1212

1213
  taosHashCleanup(pInfo->pGroupSet);
8,461,227✔
1214
  taosMemoryFree(pInfo->columnOffset);
8,461,227✔
1215

1216
  cleanupExprSupp(&pInfo->scalarSup);
8,461,567✔
1217
  destroyDiskbasedBuf(pInfo->pBuf);
8,461,567✔
1218
  taosArrayDestroy(pInfo->pOrderInfoArr);
8,461,227✔
1219
  taosMemoryFreeClear(param);
8,461,567✔
1220
}
8,461,567✔
1221

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

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

1231
  clearPartitionOperator(pInfo);
×
1232

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

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

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

1258
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
8,459,037✔
1259
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1260
  QRY_PARAM_CHECK(pOptrInfo);
8,459,037✔
1261

1262
  int32_t                 code = TSDB_CODE_SUCCESS;
8,460,108✔
1263
  int32_t                 lino = 0;
8,460,108✔
1264
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
8,460,108✔
1265
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
8,457,082✔
1266
  if (pInfo == NULL || pOperator == NULL) {
8,457,279✔
UNCOV
1267
    pTaskInfo->code = code = terrno;
×
1268
    goto _error;
×
1269
  }
1270

1271
  pOperator->pPhyNode = pPartNode;
8,458,177✔
1272
  int32_t    numOfCols = 0;
8,457,820✔
1273
  SExprInfo* pExprInfo = NULL;
8,457,433✔
1274
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
8,456,411✔
1275
  QUERY_CHECK_CODE(code, lino, _error);
8,459,523✔
1276
  pOperator->exprSupp.numOfExprs = numOfCols;
8,459,523✔
1277
  pOperator->exprSupp.pExprInfo = pExprInfo;
8,459,523✔
1278

1279
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
8,459,677✔
1280

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

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

1293
  if (pPartNode->pExprs != NULL) {
8,459,632✔
1294
    int32_t    num = 0;
1,639,015✔
1295
    SExprInfo* pExprInfo1 = NULL;
1,639,015✔
1296
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
1,639,015✔
1297
    QUERY_CHECK_CODE(code, lino, _error);
1,639,015✔
1298

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

1303
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
8,086,585✔
1304
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
8,085,330✔
1305
  if (pInfo->pGroupSet == NULL) {
8,086,942✔
1306
    goto _error;
×
1307
  }
1308

1309
  uint32_t defaultPgsz = 0;
8,086,942✔
1310
  int64_t  defaultBufsz = 0;
8,086,942✔
1311

1312
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
8,086,942✔
1313
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
8,086,942✔
1314
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
8,086,074✔
1315
  if (code != TSDB_CODE_SUCCESS) {
8,085,751✔
1316
    goto _error;
×
1317
  }
1318

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

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

1330
  pInfo->rowCapacity =
8,085,775✔
1331
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
8,085,565✔
1332
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
8,086,602✔
1333
  if (pInfo->rowCapacity < 0) {
8,086,602✔
1334
    code = terrno;
×
1335
    goto _error;
×
1336
  }
1337

1338
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
8,085,401✔
1339
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
8,084,836✔
1340

1341
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
8,083,393✔
1342
  if (code != TSDB_CODE_SUCCESS) {
8,083,652✔
1343
    goto _error;
×
1344
  }
1345

1346
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
8,083,652✔
1347
                  pTaskInfo);
1348

1349
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
8,085,922✔
1350
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1351

1352
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
8,083,295✔
1353
  code = appendDownstream(pOperator, &downstream, 1);
8,084,836✔
1354
  if (code != TSDB_CODE_SUCCESS) {
8,080,330✔
1355
    goto _error;
×
1356
  }
1357

1358
  *pOptrInfo = pOperator;
8,080,330✔
1359
  return TSDB_CODE_SUCCESS;
8,079,973✔
1360

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

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

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

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

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

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

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

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

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

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

1445
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
43,884,508✔
1446
  int32_t code = TSDB_CODE_SUCCESS;
43,884,508✔
1447
  int32_t lino = 0;
43,884,508✔
1448
  size_t  numOfCols = LIST_LENGTH(pNodeList);
43,884,508✔
1449
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
43,884,592✔
1450
  if (pList == NULL) {
43,875,896✔
1451
    code = terrno;
×
1452
    (*pArrayRes) = NULL;
×
1453
    QUERY_CHECK_CODE(code, lino, _end);
5,702✔
1454
  }
1455

1456
  for (int32_t i = 0; i < numOfCols; ++i) {
106,885,581✔
1457
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
63,010,770✔
1458
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
63,020,607✔
1459

1460
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
63,020,607✔
1461
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
63,022,134✔
1462

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

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

1481
  (*pArrayRes) = pList;
43,874,811✔
1482

1483
_end:
43,886,460✔
1484
  if (code != TSDB_CODE_SUCCESS) {
43,886,460✔
1485
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1486
  }
1487
  return code;
43,876,083✔
1488
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc