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

taosdata / TDengine / #4926

13 Jan 2026 05:43AM UTC coverage: 66.053% (-0.05%) from 66.107%
#4926

push

travis-ci

web-flow
feat: [6654385780] show snap progress (#34203)

48 of 59 new or added lines in 7 files covered. (81.36%)

582 existing lines in 124 files now uncovered.

200362 of 303334 relevant lines covered (66.05%)

132283104.31 hits per line

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

75.9
/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) {
84,452,077✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
84,452,077✔
77
  taosMemoryFree(pKey->pData);
84,452,077✔
78
}
84,449,834✔
79

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

86
  cleanupBasicInfo(&pInfo->binfo);
54,641,151✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
54,642,039✔
88
  taosArrayDestroy(pInfo->pGroupCols);
54,640,117✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
54,640,283✔
90
  cleanupExprSupp(&pInfo->scalarSup);
54,638,175✔
91

92
  if (pInfo->pOperator != NULL) {
54,642,218✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
51,864,009✔
94
                      false);
95
    pInfo->pOperator = NULL;
51,864,392✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
54,641,154✔
99
  cleanupAggSup(&pInfo->aggSup);
54,636,262✔
100
  taosMemoryFreeClear(param);
54,635,434✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
63,821,127✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
63,821,127✔
105
  if ((*pGroupColVals) == NULL) {
63,783,290✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
63,805,066✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
162,578,025✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
98,744,234✔
112
    if (!pCol) {
98,767,242✔
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
98,767,242✔
117

118
    SGroupKeys key = {0};
98,762,860✔
119
    key.bytes = pCol->bytes;
98,760,952✔
120
    key.type = pCol->type;
98,718,218✔
121
    key.isNull = false;
98,739,625✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
98,739,625✔
123
    if (key.pData == NULL) {
98,732,793✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
98,732,793✔
128
    if (!tmp) {
98,769,834✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
63,833,791✔
134
  (*keyLen) += nullFlagSize;
63,833,791✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
63,821,777✔
137
  if ((*keyBuf) == NULL) {
63,795,071✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
63,803,987✔
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;
2,147,483,647✔
159
    }
160

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

170
      if (memcmp(pkey->pData, val, dataLen) == 0) {
6,314✔
171
        continue;
902✔
172
      } else {
173
        return false;
5,412✔
174
      }
175
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
176
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,147,483,647✔
UNCOV
177
        int32_t len = blobDataLen(val);
×
178
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
179
          continue;
×
180
        } else {
181
          return false;
×
182
        }
183
      } else {
184
        int32_t len = varDataLen(val);
2,147,483,647✔
185
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
2,147,483,647✔
186
          continue;
2,147,483,647✔
187
        } else {
188
          return false;
1,060,036,978✔
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;
2,147,483,647✔
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;
2,147,483,647✔
222
    } else {
223
      pkey->isNull = false;
2,147,483,647✔
224
      char* val = colDataGetData(pColInfoData, rowIndex);
2,147,483,647✔
225
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
226
        // if (tTagIsJson(val)) {
227
        //   terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
228
        //   return;
229
        // }
230
        int32_t dataLen = getJsonValueLen(val);
37,634✔
231
        memcpy(pkey->pData, val, dataLen);
37,634✔
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));
413✔
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;
2,147,483,647✔
254
      continue;
2,147,483,647✔
255
    }
256

257
    isNull[i] = 0;
2,147,483,647✔
258
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
259
      int32_t dataLen = getJsonValueLen(pkey->pData);
37,634✔
260
      memcpy(pStart, (pkey->pData), dataLen);
37,634✔
261
      pStart += dataLen;
37,634✔
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,562✔
293
          memcpy(dest, data, dataLen);
14,562✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,515,948,039✔
UNCOV
296
            blobDataCopy(dest, data);
×
297
          } else {
298
            varDataCopy(dest, data);
1,519,954,539✔
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;
2,147,483,647✔
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) {
279,156,726✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
279,156,726✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
279,205,395✔
315

316
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
279,202,582✔
317
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
279,209,511✔
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;
279,214,771✔
324
  terrno = TSDB_CODE_SUCCESS;
279,214,771✔
325

326
  int32_t num = 0;
279,197,904✔
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);
37,533,151✔
331
      pInfo->isInit = true;
37,550,003✔
332
      num++;
37,552,741✔
333
      continue;
37,552,741✔
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++;
2,147,483,647✔
339
      continue;
2,147,483,647✔
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);
229,982,197✔
345
      num = 1;
229,984,828✔
346
      continue;
229,984,828✔
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) {
279,217,184✔
371
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
279,219,539✔
372
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
279,222,188✔
373
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
374
    if (ret != TSDB_CODE_SUCCESS) {
279,187,850✔
375
      T_LONG_JMP(pTaskInfo->env, ret);
×
376
    }
377

378
    int32_t rowIndex = pBlock->info.rows - num;
279,187,850✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
279,187,029✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
279,166,815✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
279,166,815✔
385
  }
386
}
279,200,407✔
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,
2,023,288,089✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,023,288,089✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,023,292,367✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,023,286,244✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
2,023,296,006✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
2,023,291,194✔
404

405
  blockDataCleanup(pBlock);
2,023,292,247✔
406
  if (!hasRemainResultByHash(pOperator)) {
2,023,296,185✔
407
    return;
13,320,376✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
2,009,974,087✔
411
  if (!pInfo->binfo.mergeResultBlock) {
2,009,974,548✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
1,981,362,597✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
57,201,528✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
28,609,281✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
28,608,787✔
419
        break;
15,552✔
420
      }
421
      pBlock->info.id.groupId = 0;
28,592,741✔
422
    }
423

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

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
1,972,476,430✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
1,972,476,430✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
63,492✔
432
    return true;  // limit reached, stop processing further rows
21,728✔
433
  }
434
  return false;
1,972,460,946✔
435
}
436

437
static int32_t doGroupResultSlimit(SSDataBlock* pRes, SLimitInfo* pLimitInfo) {
2,023,293,216✔
438
  int32_t code = TSDB_CODE_SUCCESS;
2,023,293,216✔
439
  int32_t lino = 0;
2,023,293,216✔
440

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
2,023,293,216✔
442
    return TSDB_CODE_SUCCESS;
14,079,499✔
443
  }
444

445
  if (pLimitInfo->remainGroupOffset > 0) {
2,009,213,881✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
67,804✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
7,528✔
448
      blockDataCleanup(pRes);
7,528✔
449
      return TSDB_CODE_SUCCESS;
7,528✔
450
    } else {
451
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
60,276✔
452
      QUERY_CHECK_CODE(code, lino, _end);
60,276✔
453
      pLimitInfo->remainGroupOffset = 0;
60,276✔
454
    }
455
  }
456

457
  if (pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
2,009,209,681✔
458
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
200,479✔
459
    if (pRes->info.rows > remainRows) {
200,479✔
460
      blockDataKeepFirstNRows(pRes, remainRows);
87,846✔
461
    }
462
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
200,479✔
463
  }
464

465
_end:
2,009,009,881✔
466
  if (code != TSDB_CODE_SUCCESS) {
2,009,210,360✔
467
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
468
  }
469
  return code;
2,009,207,320✔
470
}
471

472
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
2,023,288,593✔
473
  int32_t               code = TSDB_CODE_SUCCESS;
2,023,288,593✔
474
  int32_t               lino = 0;
2,023,288,593✔
475
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,023,288,593✔
476
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,023,293,226✔
477
  SSDataBlock*          pRes = pInfo->binfo.pRes;
2,023,276,842✔
478
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
2,023,295,114✔
479

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

484
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
2,023,293,672✔
485
    QUERY_CHECK_CODE(code, lino, _end);
2,023,296,187✔
486

487
    code = doGroupResultSlimit(pRes, pLimitInfo);
2,023,296,187✔
488
    QUERY_CHECK_CODE(code, lino, _end);
2,023,294,068✔
489

490
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
2,023,294,068✔
491
      setOperatorCompleted(pOperator);
50,832,132✔
492
      // clean hash after completed
493
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
50,836,276✔
494
      pInfo->aggSup.pResultRowHashTable = NULL;
50,833,090✔
495
      break;
50,834,539✔
496
    }
497

498
    if (pRes->info.rows > 0) {
1,972,460,541✔
499
      break;
1,972,460,356✔
500
    }
501
  }
502

503
  pOperator->resultInfo.totalRows += pRes->info.rows;
2,023,294,895✔
504

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

513
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
2,060,492,949✔
514
  int32_t               code = TSDB_CODE_SUCCESS;
2,060,492,949✔
515
  int32_t               lino = 0;
2,060,492,949✔
516
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,060,492,949✔
517
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,060,503,844✔
518
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
2,060,498,831✔
519
  int32_t               order = pInfo->binfo.inputTsOrder;
2,060,503,233✔
520
  int64_t               st = taosGetTimestampUs();
2,060,471,603✔
521

522
  QRY_PARAM_CHECK(ppRes);
2,060,471,603✔
523
  if (pOperator->status == OP_EXEC_DONE) {
2,060,495,415✔
524
    return code;
36,356,238✔
525
  }
526

527
  if (pOperator->status == OP_RES_TO_RETURN) {
2,024,137,802✔
528
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
1,972,390,792✔
529
    return code;
1,972,390,547✔
530
  }
531

532
  while (1) {
279,172,304✔
533
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
330,913,331✔
534
    if (pBlock == NULL) {
330,932,020✔
535
      break;
50,904,340✔
536
    }
537

538
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
280,027,680✔
539

540
    // the pDataBlock are always the same one, no need to call this again
541
    code = setInputDataBlock(&pOperator->exprSupp, pBlock, order, pBlock->info.scanFlag, true);
280,073,510✔
542
    QUERY_CHECK_CODE(code, lino, _end);
280,062,442✔
543

544
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
545
    if (pInfo->scalarSup.pExprInfo != NULL) {
280,062,442✔
546
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
48,341,126✔
547
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
48,346,692✔
548
      QUERY_CHECK_CODE(code, lino, _end);
48,319,560✔
549
    }
550

551
    doHashGroupbyAgg(pOperator, pBlock);
279,172,386✔
552
  }
553

554
  pOperator->status = OP_RES_TO_RETURN;
50,904,340✔
555

556
  // initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
557
  if (pGroupResInfo->pRows != NULL) {
50,903,879✔
558
    taosArrayDestroy(pGroupResInfo->pRows);
×
559
  }
560

561
  if (pGroupResInfo->pBuf) {
50,903,323✔
562
    taosMemoryFree(pGroupResInfo->pBuf);
×
563
    pGroupResInfo->pBuf = NULL;
×
564
  }
565

566
  pGroupResInfo->index = 0;
50,901,973✔
567
  pGroupResInfo->iter = 0;
50,902,862✔
568
  pGroupResInfo->dataPos = NULL;
50,902,818✔
569

570
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
50,902,350✔
571

572
_end:
51,765,926✔
573
  if (code != TSDB_CODE_SUCCESS) {
51,765,926✔
574
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
864,621✔
575
    pTaskInfo->code = code;
864,621✔
576
    T_LONG_JMP(pTaskInfo->env, code);
864,621✔
577
  } else {
578
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
50,901,305✔
579
  }
580

581
  return code;
50,898,678✔
582
}
583

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

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

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

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

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

606
  pInfo->isInit = false;
×
607

608
  return code;
×
609
}
610

611
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
54,609,082✔
612
                                SOperatorInfo** pOptrInfo) {
613
  QRY_PARAM_CHECK(pOptrInfo);
54,609,082✔
614

615
  int32_t               code = TSDB_CODE_SUCCESS;
54,623,865✔
616
  int32_t               lino = 0;
54,623,865✔
617
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
54,623,865✔
618
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
54,518,227✔
619
  if (pInfo == NULL || pOperator == NULL) {
54,533,588✔
620
    code = terrno;
61✔
621
    goto _error;
×
622
  }
623

624
  pOperator->pPhyNode = (SNode*)pAggNode;
54,534,449✔
625
  pOperator->exprSupp.hasWindowOrGroup = true;
54,537,181✔
626
  pOperator->exprSupp.hasWindow = false;
54,556,079✔
627

628
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
54,573,516✔
629
  if (pResBlock == NULL) {
54,646,157✔
630
    code = terrno;
×
631
    goto _error;
×
632
  }
633
  initBasicInfo(&pInfo->binfo, pResBlock);
54,646,157✔
634

635
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
54,618,599✔
636

637
  pInfo->pGroupCols = NULL;
54,639,204✔
638
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
54,639,235✔
639
  QUERY_CHECK_CODE(code, lino, _error);
54,608,156✔
640

641
  int32_t    numOfScalarExpr = 0;
54,608,156✔
642
  SExprInfo* pScalarExprInfo = NULL;
54,621,600✔
643
  if (pAggNode->pExprs != NULL) {
54,616,021✔
644
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
25,175,539✔
645
    QUERY_CHECK_CODE(code, lino, _error);
25,188,513✔
646
  }
647

648
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
51,816,285✔
649
  QUERY_CHECK_CODE(code, lino, _error);
51,842,982✔
650

651
  initResultSizeInfo(&pOperator->resultInfo, 4096);
51,842,982✔
652
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
51,849,237✔
653
  QUERY_CHECK_CODE(code, lino, _error);
51,849,846✔
654

655
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
51,849,846✔
656
  QUERY_CHECK_CODE(code, lino, _error);
51,848,573✔
657

658
  int32_t    num = 0;
51,848,573✔
659
  SExprInfo* pExprInfo = NULL;
51,849,526✔
660

661
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
51,850,000✔
662
  QUERY_CHECK_CODE(code, lino, _error);
51,836,857✔
663

664
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
103,684,102✔
665
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
51,837,317✔
666
  QUERY_CHECK_CODE(code, lino, _error);
51,843,367✔
667

668
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
51,843,993✔
669
                            pTaskInfo->pStreamRuntimeInfo);
51,843,367✔
670
  QUERY_CHECK_CODE(code, lino, _error);
51,841,607✔
671

672
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
51,841,607✔
673
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
51,846,565✔
674

675
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
51,846,286✔
676
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
51,842,005✔
677
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
51,846,255✔
678

679
  pInfo->pOperator = pOperator;
51,838,859✔
680

681
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
51,851,594✔
682
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
683
  setOperatorResetStateFn(pOperator, resetGroupOperState);
51,817,367✔
684
  code = appendDownstream(pOperator, &downstream, 1);
51,842,568✔
685
  QUERY_CHECK_CODE(code, lino, _error);
51,811,985✔
686

687
  *pOptrInfo = pOperator;
51,811,985✔
688
  return TSDB_CODE_SUCCESS;
51,826,767✔
689

690
_error:
2,778,594✔
691
  if (pInfo != NULL) destroyGroupOperatorInfo(pInfo);
2,778,594✔
692
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
2,778,133✔
693
  pTaskInfo->code = code;
2,778,133✔
694
  return code;
2,778,133✔
695
}
696

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

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

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

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

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

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

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

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

754
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
27,670,367✔
755
  int32_t                 code = TSDB_CODE_SUCCESS;
27,670,367✔
756
  int32_t                 lino = 0;
27,670,367✔
757
  SPartitionOperatorInfo* pInfo = pOperator->info;
27,670,367✔
758
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
27,682,678✔
759

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

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

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

772
    // group id
773
    if (pGroupInfo->groupId == 0) {
2,147,483,647✔
774
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
83,410,532✔
775
    }
776

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

872
_end:
30,639,310✔
873
  if (code != TSDB_CODE_SUCCESS) {
27,696,614✔
874
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
875
    T_LONG_JMP(pTaskInfo->env, code);
×
876
  }
877
}
27,696,614✔
878

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

884
  void* pPage = NULL;
2,147,483,647✔
885
  if (p == NULL) {  // it is a new group
2,147,483,647✔
886
    SDataGroupInfo gi = {0};
83,411,817✔
887
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
83,411,817✔
888
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
83,376,945✔
889

890
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
83,376,945✔
891
    if (code == TSDB_CODE_DUP_KEY) {
83,407,378✔
892
      code = TSDB_CODE_SUCCESS;
×
893
    }
894
    QUERY_CHECK_CODE(code, lino, _end);
83,407,378✔
895

896
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
83,407,378✔
897

898
    int32_t pageId = 0;
83,419,811✔
899
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
83,420,272✔
900
    if (pPage == NULL) {
83,405,165✔
901
      return pPage;
×
902
    }
903

904
    void* tmp = taosArrayPush(p->pPageList, &pageId);
83,405,165✔
905
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
83,415,662✔
906

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

916
    int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
917
    if (*rows >= pInfo->rowCapacity) {
2,147,483,647✔
918
      // release buffer
919
      releaseBufPage(pInfo->pBuf, pPage);
503,075,762✔
920

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

929
      void* tmp = taosArrayPush(p->pPageList, &pageId);
503,081,637✔
930
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
503,082,116✔
931

932
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
503,082,116✔
933
    }
934
  }
935

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

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

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

947
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
11,961,247✔
948
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
11,961,247✔
949
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
11,970,502✔
950
  if (!offset) {
11,945,514✔
951
    return NULL;
×
952
  }
953

954
  offset[0] = sizeof(int32_t) +
11,945,514✔
955
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
956

957
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
21,531,559✔
958
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
9,577,282✔
959

960
    int32_t bytes = pColInfoData->info.bytes;
9,576,518✔
961
    int32_t payloadLen = bytes * rowCapacity;
9,580,005✔
962

963
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
9,580,005✔
964
      // offset segment + content length + payload
965
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
1,688,251✔
966
    } else {
967
      // bitmap + content length + payload
968
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
7,893,646✔
969
    }
970
  }
971

972
  return offset;
11,954,277✔
973
}
974

975
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
9,705,820✔
976
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
9,705,820✔
977
  for (int32_t i = 0; i < size; i++) {
59,037,381✔
978
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
49,332,022✔
979
    if (pGp && pGp->blockForNotLoaded) {
49,332,483✔
980
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
981
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
982
        if (pBlock) blockDataDestroy(*pBlock);
×
983
      }
984
      taosArrayClear(pGp->blockForNotLoaded);
×
985
      pGp->offsetForNotLoaded = 0;
×
986
    }
987
    taosArrayDestroy(pGp->pPageList);
49,332,483✔
988
  }
989
  taosArrayClear(pInfo->sortedGroupArray);
9,705,359✔
990
  clearDiskbasedBuf(pInfo->pBuf);
9,705,359✔
991
}
9,705,820✔
992

993
static int compareDataGroupInfo(const void* group1, const void* group2) {
660,137,177✔
994
  const SDataGroupInfo* pGroupInfo1 = group1;
660,137,177✔
995
  const SDataGroupInfo* pGroupInfo2 = group2;
660,137,177✔
996

997
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
660,137,177✔
998
    return 0;
×
999
  }
1000

1001
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
660,137,546✔
1002
}
1003

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

1016
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
566,453,389✔
1017
  int32_t                 code = TSDB_CODE_SUCCESS;
566,453,389✔
1018
  int32_t                 lino = 0;
566,453,389✔
1019
  SPartitionOperatorInfo* pInfo = pOperator->info;
566,453,389✔
1020
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
566,467,030✔
1021

1022
  if (pInfo->remainRows == 0) {
566,463,774✔
1023
    blockDataCleanup(pInfo->binfo.pRes);
541,950,300✔
1024
    SDataGroupInfo* pGroupInfo =
541,955,230✔
1025
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
541,955,805✔
1026
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
541,955,230✔
1027
      if (pGroupInfo != NULL) {
61,092,148✔
1028
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
49,989,077✔
1029
        if (ret != NULL) return ret;
49,987,240✔
1030
      }
1031
      // try next group data
1032
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
61,090,311✔
1033
        setOperatorCompleted(pOperator);
9,705,820✔
1034
        clearPartitionOperator(pInfo);
9,705,820✔
1035
        return NULL;
9,705,820✔
1036
      }
1037
      ++pInfo->groupIndex;
51,385,874✔
1038

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

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

1072
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
532,245,767✔
1073
    QUERY_CHECK_CODE(code, lino, _end);
532,248,490✔
1074

1075
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
532,248,490✔
1076
    QUERY_CHECK_CODE(code, lino, _end);
532,247,596✔
1077

1078
    pInfo->pageIndex += 1;
532,247,596✔
1079
    releaseBufPage(pInfo->pBuf, page);
532,246,667✔
1080
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
532,245,830✔
1081
    pInfo->binfo.pRes->info.dataLoad = 1;
532,246,220✔
1082
    pInfo->orderedRows = 0;
532,245,745✔
1083
  } else if (pInfo->pOrderInfoArr == NULL) {
24,517,191✔
1084
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1085
  }
1086

1087
  if (pInfo->pOrderInfoArr) {
556,767,078✔
1088
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
76,283,829✔
1089
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
76,282,907✔
1090
    QUERY_CHECK_CODE(code, lino, _end);
76,266,053✔
1091
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
76,266,053✔
1092
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
76,283,368✔
1093
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
76,281,105✔
1094
  }
1095

1096
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
556,749,445✔
1097
  QUERY_CHECK_CODE(code, lino, _end);
556,745,381✔
1098

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

1105
  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
556,745,381✔
1106
  return pInfo->binfo.pRes;
556,751,323✔
1107
}
1108

1109
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
567,330,043✔
1110
  if (pOperator->status == OP_EXEC_DONE) {
567,330,043✔
1111
    (*ppRes) = NULL;
11,834✔
1112
    return TSDB_CODE_SUCCESS;
11,834✔
1113
  }
1114

1115
  int32_t                 code = TSDB_CODE_SUCCESS;
567,300,506✔
1116
  int32_t                 lino = 0;
567,300,506✔
1117
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
567,300,506✔
1118
  SPartitionOperatorInfo* pInfo = pOperator->info;
567,309,129✔
1119
  SSDataBlock*            pRes = pInfo->binfo.pRes;
567,312,959✔
1120

1121
  if (pOperator->status == OP_RES_TO_RETURN) {
567,315,998✔
1122
    (*ppRes) = buildPartitionResult(pOperator);
555,361,650✔
1123
    return code;
555,363,447✔
1124
  }
1125

1126
  int64_t        st = taosGetTimestampUs();
11,934,228✔
1127
  SOperatorInfo* downstream = pOperator->pDownstream[0];
11,934,228✔
1128

1129
  while (1) {
27,697,068✔
1130
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
39,637,806✔
1131
    if (pBlock == NULL) {
38,769,729✔
1132
      break;
11,103,532✔
1133
    }
1134

1135
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
27,666,197✔
1136
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1137
    if (pInfo->scalarSup.pExprInfo != NULL) {
27,682,245✔
1138
      code =
1139
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
1,900,649✔
1140
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
1,900,649✔
1141
      QUERY_CHECK_CODE(code, lino, _end);
1,900,649✔
1142
    }
1143

1144
    terrno = TSDB_CODE_SUCCESS;
27,678,553✔
1145
    doHashPartition(pOperator, pBlock);
27,674,411✔
1146
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
27,696,614✔
1147
      code = terrno;
×
UNCOV
1148
      QUERY_CHECK_CODE(code, lino, _end);
×
1149
    }
1150
  }
1151

1152
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
11,103,532✔
1153
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
11,101,688✔
1154

1155
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
11,101,688✔
1156
  while (pGroupIter != NULL) {
94,525,187✔
1157
    SDataGroupInfo* pGroupInfo = pGroupIter;
83,421,655✔
1158
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
83,421,194✔
1159
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
83,421,194✔
1160
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
83,421,194✔
1161
  }
1162

1163
  taosArraySort(groupArray, compareDataGroupInfo);
11,103,532✔
1164
  pInfo->sortedGroupArray = groupArray;
11,103,532✔
1165
  pInfo->groupIndex = -1;
11,103,532✔
1166
  taosHashClear(pInfo->pGroupSet);
11,103,532✔
1167

1168
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
11,103,532✔
1169

1170
  pOperator->status = OP_RES_TO_RETURN;
11,103,532✔
1171
  code = blockDataEnsureCapacity(pRes, 4096);
11,103,532✔
1172
  QUERY_CHECK_CODE(code, lino, _end);
11,102,610✔
1173

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

1181
  (*ppRes) = buildPartitionResult(pOperator);
11,102,610✔
1182
  return code;
11,095,766✔
1183
}
1184

1185
static void destroyPartitionOperatorInfo(void* param) {
12,866,087✔
1186
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
12,866,087✔
1187
  cleanupBasicInfo(&pInfo->binfo);
12,866,087✔
1188
  taosArrayDestroy(pInfo->pGroupCols);
12,864,243✔
1189

1190
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
27,194,199✔
1191
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
14,332,064✔
1192
    taosMemoryFree(key.pData);
14,333,057✔
1193
  }
1194

1195
  taosArrayDestroy(pInfo->pGroupColVals);
12,865,274✔
1196
  taosMemoryFree(pInfo->keyBuf);
12,864,775✔
1197

1198
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
12,863,576✔
1199
  for (int32_t i = 0; i < size; i++) {
46,954,798✔
1200
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
34,089,633✔
1201
    if (pGp) {
34,088,257✔
1202
      taosArrayDestroy(pGp->pPageList);
34,088,257✔
1203
    }
1204
  }
1205
  taosArrayDestroy(pInfo->sortedGroupArray);
12,865,165✔
1206

1207
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
12,860,626✔
1208
  while (pGroupIter != NULL) {
12,865,626✔
UNCOV
1209
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
UNCOV
1210
    taosArrayDestroy(pGroupInfo->pPageList);
×
UNCOV
1211
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1212
  }
1213

1214
  taosHashCleanup(pInfo->pGroupSet);
12,865,626✔
1215
  taosMemoryFree(pInfo->columnOffset);
12,865,236✔
1216

1217
  cleanupExprSupp(&pInfo->scalarSup);
12,865,626✔
1218
  destroyDiskbasedBuf(pInfo->pBuf);
12,866,548✔
1219
  taosArrayDestroy(pInfo->pOrderInfoArr);
12,861,548✔
1220
  taosMemoryFreeClear(param);
12,862,470✔
1221
}
12,863,782✔
1222

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

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

1232
  clearPartitionOperator(pInfo);
×
1233

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

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

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

1259
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
12,842,876✔
1260
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1261
  QRY_PARAM_CHECK(pOptrInfo);
12,842,876✔
1262

1263
  int32_t                 code = TSDB_CODE_SUCCESS;
12,853,841✔
1264
  int32_t                 lino = 0;
12,853,841✔
1265
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
12,853,841✔
1266
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
12,775,653✔
1267
  if (pInfo == NULL || pOperator == NULL) {
12,772,939✔
1268
    pTaskInfo->code = code = terrno;
×
1269
    goto _error;
×
1270
  }
1271

1272
  pOperator->pPhyNode = pPartNode;
12,776,683✔
1273
  int32_t    numOfCols = 0;
12,796,506✔
1274
  SExprInfo* pExprInfo = NULL;
12,804,712✔
1275
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
12,795,021✔
1276
  QUERY_CHECK_CODE(code, lino, _error);
12,835,029✔
1277
  pOperator->exprSupp.numOfExprs = numOfCols;
12,835,029✔
1278
  pOperator->exprSupp.pExprInfo = pExprInfo;
12,832,780✔
1279

1280
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
12,830,534✔
1281

1282
  if (pPartNode->needBlockOutputTsOrder) {
12,822,556✔
1283
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
2,673,246✔
1284
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
2,683,908✔
1285
    if (!pInfo->pOrderInfoArr) {
2,676,012✔
1286
      pTaskInfo->code = terrno;
×
1287
      goto _error;
×
1288
    }
1289

1290
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
2,676,532✔
1291
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
2,685,752✔
1292
  }
1293

1294
  if (pPartNode->pExprs != NULL) {
12,840,525✔
1295
    int32_t    num = 0;
2,646,410✔
1296
    SExprInfo* pExprInfo1 = NULL;
2,648,715✔
1297
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
2,644,566✔
1298
    QUERY_CHECK_CODE(code, lino, _error);
2,650,098✔
1299

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

1304
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
11,965,801✔
1305
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
11,952,376✔
1306
  if (pInfo->pGroupSet == NULL) {
11,975,429✔
1307
    goto _error;
×
1308
  }
1309

1310
  uint32_t defaultPgsz = 0;
11,976,386✔
1311
  int64_t  defaultBufsz = 0;
11,979,169✔
1312

1313
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
11,967,627✔
1314
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
11,982,748✔
1315
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
11,966,226✔
1316
  if (code != TSDB_CODE_SUCCESS) {
11,962,999✔
1317
    goto _error;
×
1318
  }
1319

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

1326
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
11,955,197✔
1327
  if (code != TSDB_CODE_SUCCESS) {
11,963,921✔
1328
    goto _error;
×
1329
  }
1330

1331
  pInfo->rowCapacity =
11,965,892✔
1332
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
11,962,221✔
1333
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
11,963,921✔
1334
  if (pInfo->rowCapacity < 0) {
11,968,088✔
1335
    code = terrno;
×
1336
    goto _error;
×
1337
  }
1338

1339
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
11,935,647✔
1340
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
11,942,467✔
1341

1342
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
11,942,146✔
1343
  if (code != TSDB_CODE_SUCCESS) {
11,942,001✔
1344
    goto _error;
×
1345
  }
1346

1347
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
11,942,001✔
1348
                  pTaskInfo);
1349

1350
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
11,962,112✔
1351
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1352

1353
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
11,954,391✔
1354
  code = appendDownstream(pOperator, &downstream, 1);
11,963,150✔
1355
  if (code != TSDB_CODE_SUCCESS) {
11,948,762✔
1356
    goto _error;
×
1357
  }
1358

1359
  *pOptrInfo = pOperator;
11,948,762✔
1360
  return TSDB_CODE_SUCCESS;
11,953,741✔
1361

1362
_error:
877,228✔
1363
  if (pInfo != NULL) {
885,644✔
1364
    destroyPartitionOperatorInfo(pInfo);
885,644✔
1365
  }
1366
  pTaskInfo->code = code;
885,644✔
1367
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
885,644✔
1368
  TAOS_RETURN(code);
884,722✔
1369
}
1370

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

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

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

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

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

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

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

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

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

1446
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
54,610,217✔
1447
  int32_t code = TSDB_CODE_SUCCESS;
54,610,217✔
1448
  int32_t lino = 0;
54,610,217✔
1449
  size_t  numOfCols = LIST_LENGTH(pNodeList);
54,610,217✔
1450
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
54,620,017✔
1451
  if (pList == NULL) {
54,581,337✔
1452
    code = terrno;
×
1453
    (*pArrayRes) = NULL;
×
1454
    QUERY_CHECK_CODE(code, lino, _end);
26,597✔
1455
  }
1456

1457
  for (int32_t i = 0; i < numOfCols; ++i) {
142,768,241✔
1458
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
88,195,247✔
1459
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
88,234,445✔
1460

1461
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
88,234,445✔
1462
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
88,233,585✔
1463

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

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

1482
  (*pArrayRes) = pList;
54,572,994✔
1483

1484
_end:
54,649,513✔
1485
  if (code != TSDB_CODE_SUCCESS) {
54,649,513✔
1486
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1487
  }
1488
  return code;
54,590,733✔
1489
}
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