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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

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

76.09
/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) {
66,626,328✔
76
  SGroupKeys* pKey = (SGroupKeys*)param;
66,626,328✔
77
  taosMemoryFree(pKey->pData);
66,626,328✔
78
}
66,625,954✔
79

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

86
  cleanupBasicInfo(&pInfo->binfo);
46,987,803✔
87
  taosMemoryFreeClear(pInfo->keyBuf);
46,987,558✔
88
  taosArrayDestroy(pInfo->pGroupCols);
46,988,087✔
89
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
46,987,952✔
90
  cleanupExprSupp(&pInfo->scalarSup);
46,987,997✔
91

92
  if (pInfo->pOperator != NULL) {
46,987,668✔
93
    cleanupResultInfo(pInfo->pOperator->pTaskInfo, &pInfo->pOperator->exprSupp, &pInfo->groupResInfo, &pInfo->aggSup,
45,312,255✔
94
                      false);
95
    pInfo->pOperator = NULL;
45,311,577✔
96
  }
97

98
  cleanupGroupResInfo(&pInfo->groupResInfo);
46,986,990✔
99
  cleanupAggSup(&pInfo->aggSup);
46,983,424✔
100
  taosMemoryFreeClear(param);
46,984,021✔
101
}
102

103
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
53,990,002✔
104
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
53,990,002✔
105
  if ((*pGroupColVals) == NULL) {
53,949,525✔
106
    return terrno;
×
107
  }
108

109
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
53,967,832✔
110
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
131,289,646✔
111
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
77,324,911✔
112
    if (!pCol) {
77,328,649✔
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
77,328,649✔
117

118
    SGroupKeys key = {0};
77,314,202✔
119
    key.bytes = pCol->bytes;
77,315,614✔
120
    key.type = pCol->type;
77,287,584✔
121
    key.isNull = false;
77,298,976✔
122
    key.pData = taosMemoryCalloc(1, pCol->bytes);
77,298,976✔
123
    if (key.pData == NULL) {
77,316,466✔
124
      return terrno;
×
125
    }
126

127
    void* tmp = taosArrayPush((*pGroupColVals), &key);
77,316,466✔
128
    if (!tmp) {
77,322,454✔
129
      return terrno;
×
130
    }
131
  }
132

133
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
53,964,735✔
134
  (*keyLen) += nullFlagSize;
53,964,735✔
135

136
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
53,980,073✔
137
  if ((*keyBuf) == NULL) {
53,952,930✔
138
    return terrno;
×
139
  }
140

141
  return TSDB_CODE_SUCCESS;
53,975,264✔
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;
234,455,043✔
159
    }
160

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

170
      if (memcmp(pkey->pData, val, dataLen) == 0) {
6,230✔
171
        continue;
890✔
172
      } else {
173
        return false;
5,340✔
174
      }
175
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
176
      if (IS_STR_DATA_BLOB(pkey->type)) {
2,005,061,563✔
177
        int32_t len = blobDataLen(val);
42✔
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,004,341,271✔
185
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
2,004,296,769✔
186
          continue;
933,801,215✔
187
        } else {
188
          return false;
1,069,984,946✔
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,286,213,705✔
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,641,918,605✔
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);
25,806✔
231
        memcpy(pkey->pData, val, dataLen);
25,806✔
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));
16,588✔
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,641,098,950✔
254
      continue;
1,641,122,587✔
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);
25,806✔
260
      memcpy(pStart, (pkey->pData), dataLen);
25,806✔
261
      pStart += dataLen;
25,806✔
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);
30✔
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,350✔
293
          memcpy(dest, data, dataLen);
14,350✔
294
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
295
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,505,469,807✔
296
            blobDataCopy(dest, data);
35✔
297
          } else {
298
            varDataCopy(dest, data);
1,505,101,860✔
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,200,312,069✔
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) {
258,002,315✔
313
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
258,002,315✔
314
  SGroupbyOperatorInfo* pInfo = pOperator->info;
258,036,286✔
315

316
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
258,026,039✔
317
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
258,019,341✔
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;
258,035,253✔
324
  terrno = TSDB_CODE_SUCCESS;
258,035,253✔
325

326
  int32_t num = 0;
258,022,377✔
327
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
328
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
329
    if (!pInfo->isInit) {
2,147,483,647✔
330
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
32,197,822✔
331
      pInfo->isInit = true;
32,221,664✔
332
      num++;
32,226,513✔
333
      continue;
32,226,513✔
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,285,050,261✔
339
      continue;
1,285,050,261✔
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,191,253✔
345
      num = 1;
218,181,258✔
346
      continue;
218,181,258✔
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) {
258,029,533✔
371
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
258,031,918✔
372
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
258,023,110✔
373
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
374
    if (ret != TSDB_CODE_SUCCESS) {
258,018,256✔
375
      T_LONG_JMP(pTaskInfo->env, ret);
×
376
    }
377

378
    int32_t rowIndex = pBlock->info.rows - num;
258,018,256✔
379
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
258,021,505✔
380
                                          pOperator->exprSupp.numOfExprs);
381
    if (ret != TSDB_CODE_SUCCESS) {
258,000,910✔
382
      T_LONG_JMP(pTaskInfo->env, ret);
×
383
    }
384
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
258,000,910✔
385
  }
386
}
258,008,751✔
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,888,513,062✔
395
                                  SDiskbasedBuf* pBuf) {
396
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,888,513,062✔
397
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
1,888,521,647✔
398
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,888,518,732✔
399

400
  SSDataBlock* pBlock = pInfo->binfo.pRes;
1,888,518,077✔
401

402
  // set output datablock version
403
  pBlock->info.version = pTaskInfo->version;
1,888,519,593✔
404

405
  blockDataCleanup(pBlock);
1,888,522,717✔
406
  if (!hasRemainResultByHash(pOperator)) {
1,888,525,970✔
407
    return;
12,466,229✔
408
  }
409

410
  pBlock->info.id.groupId = 0;
1,876,058,475✔
411
  if (!pInfo->binfo.mergeResultBlock) {
1,876,059,165✔
412
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
1,851,151,324✔
413
                             pHashmap, pOperator->resultInfo.threshold, false);
414
  } else {
415
    while (hasRemainResultByHash(pOperator)) {
49,802,868✔
416
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
24,907,042✔
417
                               pHashmap, pOperator->resultInfo.threshold, true);
418
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
24,907,042✔
419
        break;
11,216✔
420
      }
421
      pBlock->info.id.groupId = 0;
24,895,826✔
422
    }
423

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

429
static bool slimitReached(SLimitInfo* pLimitInfo) {
1,843,821,664✔
430
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 &&
1,843,821,664✔
431
      pLimitInfo->numOfOutputGroups >= pLimitInfo->slimit.limit) {
51,690✔
432
    return true;  // limit reached, stop processing further rows
18,866✔
433
  }
434
  return false;
1,843,801,115✔
435
}
436

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

441
  if (pRes == NULL || pRes->info.rows == 0 || !pLimitInfo) {
1,888,528,893✔
442
    return TSDB_CODE_SUCCESS;
13,114,531✔
443
  }
444

445
  if (pLimitInfo && pLimitInfo->remainGroupOffset > 0) {
1,875,412,716✔
446
    if (pRes->info.rows <= pLimitInfo->remainGroupOffset) {
54,848✔
447
      pLimitInfo->remainGroupOffset -= pRes->info.rows;
12,737✔
448
      blockDataCleanup(pRes);
12,737✔
449
    } else {
450
      code = blockDataTrimFirstRows(pRes, pLimitInfo->remainGroupOffset);
42,111✔
451
      QUERY_CHECK_CODE(code, lino, _end);
42,111✔
452
    }
453
  }
454

455
  pLimitInfo->remainGroupOffset = 0;
1,875,414,152✔
456
  if (pLimitInfo && pLimitInfo->slimit.limit >= 0 && pRes->info.rows > 0) {
1,875,413,393✔
457
    int32_t remainRows = pLimitInfo->slimit.limit - pLimitInfo->numOfOutputGroups;
160,462✔
458
    if (pRes->info.rows > remainRows) {
160,462✔
459
      blockDataKeepFirstNRows(pRes, remainRows);
67,355✔
460
    }
461
    pLimitInfo->numOfOutputGroups += pRes->info.rows;
160,462✔
462
  }
463

464
_end:
1,875,252,066✔
465
  if (code != TSDB_CODE_SUCCESS) {
1,875,412,528✔
NEW
466
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
467
  }
468
  return code;
1,875,411,328✔
469
}
470

471
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
1,888,510,030✔
472
  int32_t               code = TSDB_CODE_SUCCESS;
1,888,510,030✔
473
  int32_t               lino = 0;
1,888,510,030✔
474
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,888,510,030✔
475
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,888,517,437✔
476
  SSDataBlock*          pRes = pInfo->binfo.pRes;
1,888,510,333✔
477
  SLimitInfo*           pLimitInfo = &pInfo->limitInfo;
1,888,522,191✔
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,888,520,803✔
482

483
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
1,888,522,956✔
484
    QUERY_CHECK_CODE(code, lino, _end);
1,888,526,852✔
485

486
    code = doGroupResultSlimit(pRes, pLimitInfo);
1,888,526,852✔
487
    QUERY_CHECK_CODE(code, lino, _end);
1,888,526,900✔
488

489
    if (!hasRemainResultByHash(pOperator) || slimitReached(pLimitInfo)) {
1,888,526,900✔
490
      setOperatorCompleted(pOperator);
44,723,091✔
491
      // clean hash after completed
492
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
44,723,436✔
493
      pInfo->aggSup.pResultRowHashTable = NULL;
44,723,149✔
494
      break;
44,723,149✔
495
    }
496

497
    if (pRes->info.rows > 0) {
1,843,802,567✔
498
      break;
1,843,803,525✔
499
    }
500
  }
501

502
  pOperator->resultInfo.totalRows += pRes->info.rows;
1,888,526,674✔
503

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

512
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,920,357,245✔
513
  int32_t               code = TSDB_CODE_SUCCESS;
1,920,357,245✔
514
  int32_t               lino = 0;
1,920,357,245✔
515
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
1,920,357,245✔
516
  SGroupbyOperatorInfo* pInfo = pOperator->info;
1,920,361,124✔
517
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
1,920,358,108✔
518
  int32_t               order = pInfo->binfo.inputTsOrder;
1,920,366,026✔
519
  int64_t               st = taosGetTimestampUs();
1,920,333,753✔
520

521
  QRY_PARAM_CHECK(ppRes);
1,920,333,753✔
522
  if (pOperator->status == OP_EXEC_DONE) {
1,920,346,620✔
523
    return code;
31,323,336✔
524
  }
525

526
  if (pOperator->status == OP_RES_TO_RETURN) {
1,889,019,671✔
527
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
1,843,796,635✔
528
    return code;
1,843,802,466✔
529
  }
530

531
  while (1) {
258,002,537✔
532
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
303,220,953✔
533
    if (pBlock == NULL) {
303,247,802✔
534
      break;
44,724,773✔
535
    }
536

537
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
258,523,029✔
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);
258,552,725✔
541
    QUERY_CHECK_CODE(code, lino, _end);
258,545,435✔
542

543
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
544
    if (pInfo->scalarSup.pExprInfo != NULL) {
258,545,435✔
545
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
45,550,057✔
546
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
45,549,235✔
547
      QUERY_CHECK_CODE(code, lino, _end);
45,537,855✔
548
    }
549

550
    doHashGroupbyAgg(pOperator, pBlock);
258,015,428✔
551
  }
552

553
  pOperator->status = OP_RES_TO_RETURN;
44,724,773✔
554

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

560
  if (pGroupResInfo->pBuf) {
44,724,773✔
561
    taosMemoryFree(pGroupResInfo->pBuf);
×
562
    pGroupResInfo->pBuf = NULL;
×
563
  }
564

565
  pGroupResInfo->index = 0;
44,722,964✔
566
  pGroupResInfo->iter = 0;
44,723,811✔
567
  pGroupResInfo->dataPos = NULL;
44,723,976✔
568

569
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
44,722,753✔
570

571
_end:
45,246,103✔
572
  if (code != TSDB_CODE_SUCCESS) {
45,246,103✔
573
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
523,263✔
574
    pTaskInfo->code = code;
523,263✔
575
    T_LONG_JMP(pTaskInfo->env, code);
523,263✔
576
  } else {
577
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
44,722,840✔
578
  }
579

580
  return code;
44,720,442✔
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,
46,965,268✔
611
                                SOperatorInfo** pOptrInfo) {
612
  QRY_PARAM_CHECK(pOptrInfo);
46,965,268✔
613

614
  int32_t               code = TSDB_CODE_SUCCESS;
46,970,274✔
615
  int32_t               lino = 0;
46,970,274✔
616
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
46,970,274✔
617
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
46,905,443✔
618
  if (pInfo == NULL || pOperator == NULL) {
46,907,474✔
619
    code = terrno;
520✔
620
    goto _error;
×
621
  }
622

623
  pOperator->pPhyNode = (SNode*)pAggNode;
46,906,954✔
624
  pOperator->exprSupp.hasWindowOrGroup = true;
46,908,719✔
625
  pOperator->exprSupp.hasWindow = false;
46,929,046✔
626

627
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
46,943,299✔
628
  if (pResBlock == NULL) {
46,984,536✔
629
    code = terrno;
×
630
    goto _error;
×
631
  }
632
  initBasicInfo(&pInfo->binfo, pResBlock);
46,984,536✔
633

634
  initLimitInfo(pAggNode->node.pLimit, pAggNode->node.pSlimit, &pInfo->limitInfo);
46,970,660✔
635

636
  pInfo->pGroupCols = NULL;
46,970,446✔
637
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
46,966,715✔
638
  QUERY_CHECK_CODE(code, lino, _error);
46,965,257✔
639

640
  int32_t    numOfScalarExpr = 0;
46,965,257✔
641
  SExprInfo* pScalarExprInfo = NULL;
46,972,392✔
642
  if (pAggNode->pExprs != NULL) {
46,967,920✔
643
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
22,184,321✔
644
    QUERY_CHECK_CODE(code, lino, _error);
22,199,220✔
645
  }
646

647
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
45,278,062✔
648
  QUERY_CHECK_CODE(code, lino, _error);
45,286,528✔
649

650
  initResultSizeInfo(&pOperator->resultInfo, 4096);
45,286,528✔
651
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
45,295,121✔
652
  QUERY_CHECK_CODE(code, lino, _error);
45,302,465✔
653

654
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
45,302,465✔
655
  QUERY_CHECK_CODE(code, lino, _error);
45,291,047✔
656

657
  int32_t    num = 0;
45,291,047✔
658
  SExprInfo* pExprInfo = NULL;
45,292,183✔
659

660
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
45,293,252✔
661
  QUERY_CHECK_CODE(code, lino, _error);
45,276,660✔
662

663
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
90,575,943✔
664
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
45,281,362✔
665
  QUERY_CHECK_CODE(code, lino, _error);
45,295,176✔
666

667
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
45,296,207✔
668
                            pTaskInfo->pStreamRuntimeInfo);
45,295,176✔
669
  QUERY_CHECK_CODE(code, lino, _error);
45,295,539✔
670

671
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
45,295,539✔
672
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
45,285,018✔
673

674
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
45,290,466✔
675
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
45,294,048✔
676
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
45,276,959✔
677

678
  pInfo->pOperator = pOperator;
45,291,703✔
679

680
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
45,295,595✔
681
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
682
  setOperatorResetStateFn(pOperator, resetGroupOperState);
45,273,751✔
683
  code = appendDownstream(pOperator, &downstream, 1);
45,293,664✔
684
  QUERY_CHECK_CODE(code, lino, _error);
45,258,234✔
685

686
  *pOptrInfo = pOperator;
45,258,234✔
687
  return TSDB_CODE_SUCCESS;
45,281,055✔
688

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

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

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

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

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

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

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

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

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

753
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
21,475,619✔
754
  int32_t                 code = TSDB_CODE_SUCCESS;
21,475,619✔
755
  int32_t                 lino = 0;
21,475,619✔
756
  SPartitionOperatorInfo* pInfo = pOperator->info;
21,475,619✔
757
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
21,479,672✔
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);
17,524,350✔
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;
309,998,625✔
800
            contentLen = 0;
310,679,766✔
801
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
802
            offset[*rows] = (*columnLen);
11,456✔
803
            char*   src = colDataGetData(pColInfoData, j);
11,456✔
804
            int32_t dataLen = getJsonValueLen(src);
11,456✔
805

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

810
            contentLen = dataLen;
11,456✔
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));
788,269,465✔
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:
22,615,751✔
872
  if (code != TSDB_CODE_SUCCESS) {
21,491,051✔
873
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
874
    T_LONG_JMP(pTaskInfo->env, code);
×
875
  }
876
}
21,491,051✔
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};
17,507,071✔
886
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
17,507,071✔
887
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
17,476,796✔
888

889
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
17,476,796✔
890
    if (code == TSDB_CODE_DUP_KEY) {
17,519,220✔
891
      code = TSDB_CODE_SUCCESS;
×
892
    }
893
    QUERY_CHECK_CODE(code, lino, _end);
17,519,220✔
894

895
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
17,519,220✔
896

897
    int32_t pageId = 0;
17,526,845✔
898
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
17,527,129✔
899
    if (pPage == NULL) {
17,506,009✔
900
      return pPage;
×
901
    }
902

903
    void* tmp = taosArrayPush(p->pPageList, &pageId);
17,506,009✔
904
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
17,513,558✔
905

906
    *(int32_t*)pPage = 0;
17,513,558✔
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);
122,647,743✔
919

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

928
      void* tmp = taosArrayPush(p->pPageList, &pageId);
122,659,864✔
929
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
122,662,109✔
930

931
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
122,662,109✔
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,685,577✔
947
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
8,685,577✔
948
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
8,685,273✔
949
  if (!offset) {
8,670,735✔
950
    return NULL;
×
951
  }
952

953
  offset[0] = sizeof(int32_t) +
8,670,735✔
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,292,012✔
957
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
5,624,117✔
958

959
    int32_t bytes = pColInfoData->info.bytes;
5,620,544✔
960
    int32_t payloadLen = bytes * rowCapacity;
5,620,260✔
961

962
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
5,620,260✔
963
      // offset segment + content length + payload
964
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
735,442✔
965
    } else {
966
      // bitmap + content length + payload
967
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
4,888,678✔
968
    }
969
  }
970

971
  return offset;
8,667,895✔
972
}
973

974
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
7,377,629✔
975
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
7,377,629✔
976
  for (int32_t i = 0; i < size; i++) {
22,564,563✔
977
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
15,187,650✔
978
    if (pGp && pGp->blockForNotLoaded) {
15,188,008✔
979
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
980
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
981
        if (pBlock) blockDataDestroy(*pBlock);
×
982
      }
983
      taosArrayClear(pGp->blockForNotLoaded);
×
984
      pGp->offsetForNotLoaded = 0;
×
985
    }
986
    taosArrayDestroy(pGp->pPageList);
15,188,008✔
987
  }
988
  taosArrayClear(pInfo->sortedGroupArray);
7,376,913✔
989
  clearDiskbasedBuf(pInfo->pBuf);
7,377,271✔
990
}
7,377,271✔
991

992
static int compareDataGroupInfo(const void* group1, const void* group2) {
55,632,246✔
993
  const SDataGroupInfo* pGroupInfo1 = group1;
55,632,246✔
994
  const SDataGroupInfo* pGroupInfo2 = group2;
55,632,246✔
995

996
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
55,632,246✔
997
    return 0;
×
998
  }
999

1000
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
55,632,246✔
1001
}
1002

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

1015
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
158,667,077✔
1016
  int32_t                 code = TSDB_CODE_SUCCESS;
158,667,077✔
1017
  int32_t                 lino = 0;
158,667,077✔
1018
  SPartitionOperatorInfo* pInfo = pOperator->info;
158,667,077✔
1019
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
158,676,176✔
1020

1021
  if (pInfo->remainRows == 0) {
158,670,788✔
1022
    blockDataCleanup(pInfo->binfo.pRes);
136,454,030✔
1023
    SDataGroupInfo* pGroupInfo =
136,456,587✔
1024
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
136,457,394✔
1025
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
136,456,587✔
1026
      if (pGroupInfo != NULL) {
23,413,588✔
1027
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
15,243,668✔
1028
        if (ret != NULL) return ret;
15,242,742✔
1029
      }
1030
      // try next group data
1031
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
23,412,662✔
1032
        setOperatorCompleted(pOperator);
7,377,271✔
1033
        clearPartitionOperator(pInfo);
7,377,629✔
1034
        return NULL;
7,377,271✔
1035
      }
1036
      ++pInfo->groupIndex;
16,036,601✔
1037

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

1046
    int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
129,078,015✔
1047
    if (pageId == NULL) {
129,076,173✔
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);
129,076,173✔
1052
    if (page == NULL) {
129,076,237✔
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) {
129,076,237✔
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);
129,077,731✔
1072
    QUERY_CHECK_CODE(code, lino, _end);
129,078,748✔
1073

1074
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
129,078,748✔
1075
    QUERY_CHECK_CODE(code, lino, _end);
129,080,425✔
1076

1077
    pInfo->pageIndex += 1;
129,080,425✔
1078
    releaseBufPage(pInfo->pBuf, page);
129,080,425✔
1079
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
129,077,300✔
1080
    pInfo->binfo.pRes->info.dataLoad = 1;
129,077,779✔
1081
    pInfo->orderedRows = 0;
129,075,149✔
1082
  } else if (pInfo->pOrderInfoArr == NULL) {
22,222,356✔
1083
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1084
  }
1085

1086
  if (pInfo->pOrderInfoArr) {
151,300,987✔
1087
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
68,237,980✔
1088
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
68,234,929✔
1089
    QUERY_CHECK_CODE(code, lino, _end);
68,229,138✔
1090
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
68,229,138✔
1091
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
68,237,201✔
1092
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
68,238,997✔
1093
  }
1094

1095
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
151,291,109✔
1096
  QUERY_CHECK_CODE(code, lino, _end);
151,283,330✔
1097

1098
_end:
151,283,330✔
1099
  if (code != TSDB_CODE_SUCCESS) {
151,283,330✔
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;
151,283,330✔
1105
  return pInfo->binfo.pRes;
151,297,633✔
1106
}
1107

1108
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
159,180,153✔
1109
  if (pOperator->status == OP_EXEC_DONE) {
159,180,153✔
1110
    (*ppRes) = NULL;
368✔
1111
    return TSDB_CODE_SUCCESS;
368✔
1112
  }
1113

1114
  int32_t                 code = TSDB_CODE_SUCCESS;
159,136,725✔
1115
  int32_t                 lino = 0;
159,136,725✔
1116
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
159,136,725✔
1117
  SPartitionOperatorInfo* pInfo = pOperator->info;
159,164,948✔
1118
  SSDataBlock*            pRes = pInfo->binfo.pRes;
159,174,340✔
1119

1120
  if (pOperator->status == OP_RES_TO_RETURN) {
159,188,344✔
1121
    (*ppRes) = buildPartitionResult(pOperator);
150,507,841✔
1122
    return code;
150,508,604✔
1123
  }
1124

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

1128
  while (1) {
21,490,318✔
1129
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
30,154,575✔
1130
    if (pBlock == NULL) {
29,642,128✔
1131
      break;
8,169,920✔
1132
    }
1133

1134
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
21,472,208✔
1135
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
1136
    if (pInfo->scalarSup.pExprInfo != NULL) {
21,480,424✔
1137
      code =
1138
          projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
934,532✔
1139
                                pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
933,680✔
1140
      QUERY_CHECK_CODE(code, lino, _end);
933,680✔
1141
    }
1142

1143
    terrno = TSDB_CODE_SUCCESS;
21,471,125✔
1144
    doHashPartition(pOperator, pBlock);
21,475,473✔
1145
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
21,490,602✔
1146
      code = terrno;
×
1147
      QUERY_CHECK_CODE(code, lino, _end);
×
1148
    }
1149
  }
1150

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

1154
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
8,169,920✔
1155
  while (pGroupIter != NULL) {
25,699,547✔
1156
    SDataGroupInfo* pGroupInfo = pGroupIter;
17,529,627✔
1157
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
17,529,627✔
1158
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
17,529,627✔
1159
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
17,529,627✔
1160
  }
1161

1162
  taosArraySort(groupArray, compareDataGroupInfo);
8,169,920✔
1163
  pInfo->sortedGroupArray = groupArray;
8,169,920✔
1164
  pInfo->groupIndex = -1;
8,169,920✔
1165
  taosHashClear(pInfo->pGroupSet);
8,169,920✔
1166

1167
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
8,169,920✔
1168

1169
  pOperator->status = OP_RES_TO_RETURN;
8,169,920✔
1170
  code = blockDataEnsureCapacity(pRes, 4096);
8,169,920✔
1171
  QUERY_CHECK_CODE(code, lino, _end);
8,169,636✔
1172

1173
_end:
8,169,636✔
1174
  if (code != TSDB_CODE_SUCCESS) {
8,169,636✔
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);
8,169,636✔
1181
  return code;
8,164,227✔
1182
}
1183

1184
static void destroyPartitionOperatorInfo(void* param) {
9,214,872✔
1185
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
9,214,872✔
1186
  cleanupBasicInfo(&pInfo->binfo);
9,214,872✔
1187
  taosArrayDestroy(pInfo->pGroupCols);
9,214,588✔
1188

1189
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
19,935,247✔
1190
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
10,721,301✔
1191
    taosMemoryFree(key.pData);
10,721,585✔
1192
  }
1193

1194
  taosArrayDestroy(pInfo->pGroupColVals);
9,214,588✔
1195
  taosMemoryFree(pInfo->keyBuf);
9,214,230✔
1196

1197
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
9,214,230✔
1198
  for (int32_t i = 0; i < size; i++) {
11,555,849✔
1199
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
2,341,619✔
1200
    if (pGp) {
2,341,335✔
1201
      taosArrayDestroy(pGp->pPageList);
2,341,335✔
1202
    }
1203
  }
1204
  taosArrayDestroy(pInfo->sortedGroupArray);
9,214,230✔
1205

1206
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
9,213,736✔
1207
  while (pGroupIter != NULL) {
9,214,588✔
1208
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1209
    taosArrayDestroy(pGroupInfo->pPageList);
×
1210
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1211
  }
1212

1213
  taosHashCleanup(pInfo->pGroupSet);
9,214,588✔
1214
  taosMemoryFree(pInfo->columnOffset);
9,214,020✔
1215

1216
  cleanupExprSupp(&pInfo->scalarSup);
9,214,304✔
1217
  destroyDiskbasedBuf(pInfo->pBuf);
9,214,588✔
1218
  taosArrayDestroy(pInfo->pOrderInfoArr);
9,214,304✔
1219
  taosMemoryFreeClear(param);
9,214,588✔
1220
}
9,214,588✔
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,
9,203,899✔
1259
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1260
  QRY_PARAM_CHECK(pOptrInfo);
9,203,899✔
1261

1262
  int32_t                 code = TSDB_CODE_SUCCESS;
9,207,756✔
1263
  int32_t                 lino = 0;
9,207,756✔
1264
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
9,207,756✔
1265
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
9,162,058✔
1266
  if (pInfo == NULL || pOperator == NULL) {
9,152,386✔
UNCOV
1267
    pTaskInfo->code = code = terrno;
×
1268
    goto _error;
×
1269
  }
1270

1271
  pOperator->pPhyNode = pPartNode;
9,154,658✔
1272
  int32_t    numOfCols = 0;
9,166,602✔
1273
  SExprInfo* pExprInfo = NULL;
9,170,517✔
1274
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
9,165,182✔
1275
  QUERY_CHECK_CODE(code, lino, _error);
9,188,292✔
1276
  pOperator->exprSupp.numOfExprs = numOfCols;
9,188,292✔
1277
  pOperator->exprSupp.pExprInfo = pExprInfo;
9,187,666✔
1278

1279
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
9,185,455✔
1280

1281
  if (pPartNode->needBlockOutputTsOrder) {
9,186,172✔
1282
    SBlockOrderInfo order = {.order = ORDER_ASC, .pColData = NULL, .nullFirst = false, .slotId = pPartNode->tsSlotId};
1,977,743✔
1283
    pInfo->pOrderInfoArr = taosArrayInit(1, sizeof(SBlockOrderInfo));
1,985,353✔
1284
    if (!pInfo->pOrderInfoArr) {
1,977,969✔
1285
      pTaskInfo->code = terrno;
×
1286
      goto _error;
×
1287
    }
1288

1289
    void* tmp = taosArrayPush(pInfo->pOrderInfoArr, &order);
1,981,661✔
1290
    QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
1,985,069✔
1291
  }
1292

1293
  if (pPartNode->pExprs != NULL) {
9,194,124✔
1294
    int32_t    num = 0;
1,891,799✔
1295
    SExprInfo* pExprInfo1 = NULL;
1,892,083✔
1296
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
1,890,663✔
1297
    QUERY_CHECK_CODE(code, lino, _error);
1,894,071✔
1298

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

1303
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
8,686,916✔
1304
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
8,644,594✔
1305
  if (pInfo->pGroupSet == NULL) {
8,691,118✔
1306
    goto _error;
×
1307
  }
1308

1309
  uint32_t defaultPgsz = 0;
8,690,834✔
1310
  int64_t  defaultBufsz = 0;
8,689,414✔
1311

1312
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
8,688,278✔
1313
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
8,693,106✔
1314
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
8,686,916✔
1315
  if (code != TSDB_CODE_SUCCESS) {
8,682,714✔
1316
    goto _error;
×
1317
  }
1318

1319
  if (!osTempSpaceAvailable()) {
8,682,714✔
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,676,124✔
1326
  if (code != TSDB_CODE_SUCCESS) {
8,683,815✔
1327
    goto _error;
×
1328
  }
1329

1330
  pInfo->rowCapacity =
8,689,130✔
1331
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
8,675,921✔
1332
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
8,683,815✔
1333
  if (pInfo->rowCapacity < 0) {
8,689,130✔
1334
    code = terrno;
×
1335
    goto _error;
×
1336
  }
1337

1338
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
8,668,808✔
1339
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
8,665,113✔
1340

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

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

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

1352
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
8,674,123✔
1353
  code = appendDownstream(pOperator, &downstream, 1);
8,669,376✔
1354
  if (code != TSDB_CODE_SUCCESS) {
8,666,052✔
1355
    goto _error;
×
1356
  }
1357

1358
  *pOptrInfo = pOperator;
8,666,052✔
1359
  return TSDB_CODE_SUCCESS;
8,666,052✔
1360

1361
_error:
521,766✔
1362
  if (pInfo != NULL) {
521,766✔
1363
    destroyPartitionOperatorInfo(pInfo);
521,766✔
1364
  }
1365
  pTaskInfo->code = code;
521,766✔
1366
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
521,766✔
1367
  TAOS_RETURN(code);
521,766✔
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) {
309,496✔
1386
  int32_t      code = TSDB_CODE_SUCCESS;
309,496✔
1387
  int32_t      lino = 0;
309,496✔
1388
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
309,496✔
1389
  if (!pBlock) {
309,496✔
1390
    return NULL;
×
1391
  }
1392
  pBlock->info.hasVarCol = false;
309,496✔
1393
  pBlock->info.id.groupId = 0;
309,496✔
1394
  pBlock->info.rows = 0;
309,496✔
1395
  pBlock->info.type = STREAM_CREATE_CHILD_TABLE;
309,496✔
1396
  pBlock->info.watermark = INT64_MIN;
309,496✔
1397

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

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

1420
  for (int32_t i = 0; i < tag->numOfExprs; i++) {
309,496✔
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:
309,496✔
1432
  if (code != TSDB_CODE_SUCCESS) {
309,496✔
1433
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1434
    blockDataDestroy(pBlock);
×
1435
    return NULL;
×
1436
  }
1437
  return pBlock;
309,496✔
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) {
46,966,340✔
1446
  int32_t code = TSDB_CODE_SUCCESS;
46,966,340✔
1447
  int32_t lino = 0;
46,966,340✔
1448
  size_t  numOfCols = LIST_LENGTH(pNodeList);
46,966,340✔
1449
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
46,967,985✔
1450
  if (pList == NULL) {
46,931,616✔
1451
    code = terrno;
×
1452
    (*pArrayRes) = NULL;
×
1453
    QUERY_CHECK_CODE(code, lino, _end);
17,796✔
1454
  }
1455

1456
  for (int32_t i = 0; i < numOfCols; ++i) {
115,799,677✔
1457
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
68,869,458✔
1458
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
68,891,619✔
1459

1460
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
68,891,619✔
1461
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
68,890,743✔
1462

1463
      SColumn c = extractColumnFromColumnNode(pColNode);
68,891,617✔
1464
      void*   tmp = taosArrayPush(pList, &c);
68,890,113✔
1465
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
68,890,113✔
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;
46,930,219✔
1482

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