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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

108937395.15 hits per line

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

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

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

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

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

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

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

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

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

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

74
static void freeGroupKey(void* param) {
22,943,712✔
75
  SGroupKeys* pKey = (SGroupKeys*)param;
22,943,712✔
76
  taosMemoryFree(pKey->pData);
22,943,712✔
77
}
22,943,243✔
78

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

85
  cleanupBasicInfo(&pInfo->binfo);
18,633,199✔
86
  taosMemoryFreeClear(pInfo->keyBuf);
18,632,881✔
87
  taosArrayDestroy(pInfo->pGroupCols);
18,633,304✔
88
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
18,632,881✔
89
  cleanupExprSupp(&pInfo->scalarSup);
18,633,009✔
90

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

97
  cleanupGroupResInfo(&pInfo->groupResInfo);
18,632,658✔
98
  cleanupAggSup(&pInfo->aggSup);
18,632,625✔
99
  taosMemoryFreeClear(param);
18,633,170✔
100
}
101

102
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
20,355,487✔
103
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
20,355,487✔
104
  if ((*pGroupColVals) == NULL) {
20,345,536✔
105
    return terrno;
×
106
  }
107

108
  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
20,345,739✔
109
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
45,420,139✔
110
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
25,068,250✔
111
    if (!pCol) {
25,067,744✔
112
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
113
      return terrno;
×
114
    }
115
    (*keyLen) += pCol->bytes;  // actual data + null_flag
25,067,744✔
116

117
    SGroupKeys key = {0};
25,062,366✔
118
    key.bytes = pCol->bytes;
25,056,875✔
119
    key.type = pCol->type;
25,072,012✔
120
    key.isNull = false;
25,067,204✔
121
    key.pData = taosMemoryCalloc(1, pCol->bytes);
25,067,204✔
122
    if (key.pData == NULL) {
25,054,513✔
123
      return terrno;
×
124
    }
125

126
    void* tmp = taosArrayPush((*pGroupColVals), &key);
25,054,513✔
127
    if (!tmp) {
25,070,295✔
128
      return terrno;
×
129
    }
130
  }
131

132
  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
20,351,889✔
133
  (*keyLen) += nullFlagSize;
20,351,889✔
134

135
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
20,361,708✔
136
  if ((*keyBuf) == NULL) {
20,341,176✔
137
    return terrno;
×
138
  }
139

140
  return TSDB_CODE_SUCCESS;
20,351,304✔
141
}
142

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

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

155
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
156
    if (pkey->isNull && isNull) {
2,147,483,647✔
157
      continue;
968,345,229✔
158
    }
159

160
    if (isNull || pkey->isNull) {
2,147,483,647✔
161
      return false;
1,032,476,767✔
162
    }
163

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

166
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
167
      int32_t dataLen = getJsonValueLen(val);
3,780✔
168

169
      if (memcmp(pkey->pData, val, dataLen) == 0) {
3,780✔
170
        continue;
540✔
171
      } else {
172
        return false;
3,240✔
173
      }
174
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
2,147,483,647✔
175
      if (IS_STR_DATA_BLOB(pkey->type)) {
1,845,997,951✔
176
        int32_t len = blobDataLen(val);
198,323✔
177
        if (len == blobDataLen(pkey->pData) && memcmp(blobDataVal(pkey->pData), blobDataVal(val), len) == 0) {
×
178
          continue;
×
179
        } else {
180
          return false;
×
181
        }
182
      } else {
183
        int32_t len = varDataLen(val);
1,846,939,730✔
184
        if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
1,847,091,324✔
185
          continue;
763,638,609✔
186
        } else {
187
          return false;
1,083,527,162✔
188
        }
189
      }
190
    } else {
191
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
2,147,483,647✔
192
        return false;
2,147,483,647✔
193
      }
194
    }
195
  }
196

197
  return true;
2,105,958,951✔
198
}
199

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

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

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

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

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

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

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

247
  char* isNull = (char*)pKey;
2,147,483,647✔
248
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
2,147,483,647✔
249
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
2,147,483,647✔
250
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
2,147,483,647✔
251
    if (pkey->isNull) {
2,147,483,647✔
252
      isNull[i] = 1;
1,635,826,825✔
253
      continue;
1,635,857,685✔
254
    }
255

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

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

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

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

290
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
291
          int32_t dataLen = getJsonValueLen(data);
9,768✔
292
          memcpy(dest, data, dataLen);
9,768✔
293
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
294
          if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
1,254,474,605✔
UNCOV
295
            blobDataCopy(dest, data);
×
296
          } else {
297
            varDataCopy(dest, data);
1,254,902,566✔
298
          }
299
        } else {
300
          memcpy(dest, data, pColInfoData->info.bytes);
2,147,483,647✔
301
        }
302
      } else {  // it is a NULL value
303
        pEntryInfo->isNullRes = 1;
1,128,467,575✔
304
      }
305

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

311
static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
234,249,226✔
312
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
234,249,226✔
313
  SGroupbyOperatorInfo* pInfo = pOperator->info;
234,271,253✔
314

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

322
  int32_t len = 0;
234,299,159✔
323
  terrno = TSDB_CODE_SUCCESS;
234,299,159✔
324

325
  int32_t num = 0;
234,291,387✔
326
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
327
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
328
    if (!pInfo->isInit) {
2,147,483,647✔
329
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
16,699,335✔
330
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
16,695,023✔
331
        T_LONG_JMP(pTaskInfo->env, terrno);
540✔
332
      }
333
      pInfo->isInit = true;
16,694,866✔
334
      num++;
16,696,849✔
335
      continue;
16,696,849✔
336
    }
337

338
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
2,147,483,647✔
339
    if (equal) {
2,147,483,647✔
340
      num++;
2,105,790,894✔
341
      continue;
2,105,790,894✔
342
    }
343

344
    // The first row of a new block does not belongs to the previous existed group
345
    if (j == 0) {
2,147,483,647✔
346
      num++;
214,209,072✔
347
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
214,209,072✔
348
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
214,196,324✔
349
        T_LONG_JMP(pTaskInfo->env, terrno);
×
350
      }
351
      continue;
214,171,871✔
352
    }
353

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

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

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

374
  if (num > 0) {
234,288,684✔
375
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
234,291,504✔
376
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
234,285,913✔
377
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
378
    if (ret != TSDB_CODE_SUCCESS) {
234,262,214✔
379
      T_LONG_JMP(pTaskInfo->env, ret);
×
380
    }
381

382
    int32_t rowIndex = pBlock->info.rows - num;
234,262,214✔
383
    ret = applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
234,262,021✔
384
                                          pOperator->exprSupp.numOfExprs);
385
    if (ret != TSDB_CODE_SUCCESS) {
234,248,346✔
386
      T_LONG_JMP(pTaskInfo->env, ret);
×
387
    }
388
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
234,248,346✔
389
  }
390
}
234,270,386✔
391

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

398
void doBuildResultDatablockByHash(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
2,063,841,099✔
399
                                  SDiskbasedBuf* pBuf) {
400
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,063,841,099✔
401
  SSHashObj*            pHashmap = pInfo->aggSup.pResultRowHashTable;
2,063,845,773✔
402
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,063,846,388✔
403

404
  SSDataBlock* pBlock = pInfo->binfo.pRes;
2,063,845,368✔
405

406
  // set output datablock version
407
  pBlock->info.version = pTaskInfo->version;
2,063,836,560✔
408

409
  blockDataCleanup(pBlock);
2,063,845,064✔
410
  if (!hasRemainResultByHash(pOperator)) {
2,063,843,596✔
411
    return;
1,926,954✔
412
  }
413

414
  pBlock->info.id.groupId = 0;
2,061,914,264✔
415
  if (!pInfo->binfo.mergeResultBlock) {
2,061,914,138✔
416
    doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
2,049,772,025✔
417
                             pHashmap, pOperator->resultInfo.threshold, false);
418
  } else {
419
    while (hasRemainResultByHash(pOperator)) {
24,271,248✔
420
      doCopyToSDataBlockByHash(pTaskInfo, pBlock, &pOperator->exprSupp, pInfo->aggSup.pResultBuf, &pInfo->groupResInfo,
12,142,705✔
421
                               pHashmap, pOperator->resultInfo.threshold, true);
422
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
12,142,705✔
423
        break;
14,162✔
424
      }
425
      pBlock->info.id.groupId = 0;
12,128,543✔
426
    }
427

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

433
static SSDataBlock* buildGroupResultDataBlockByHash(SOperatorInfo* pOperator) {
2,063,838,875✔
434
  int32_t               code = TSDB_CODE_SUCCESS;
2,063,838,875✔
435
  int32_t               lino = 0;
2,063,838,875✔
436
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,063,838,875✔
437
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,063,843,209✔
438
  SSDataBlock*          pRes = pInfo->binfo.pRes;
2,063,843,137✔
439

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

444
    code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
2,063,847,379✔
445
    QUERY_CHECK_CODE(code, lino, _end);
2,063,848,140✔
446

447
    if (!hasRemainResultByHash(pOperator)) {
2,063,848,140✔
448
      setOperatorCompleted(pOperator);
18,621,394✔
449
      // clean hash after completed
450
      tSimpleHashCleanup(pInfo->aggSup.pResultRowHashTable);
18,621,784✔
451
      pInfo->aggSup.pResultRowHashTable = NULL;
18,620,892✔
452
      break;
18,620,892✔
453
    }
454
    if (pRes->info.rows > 0) {
2,045,227,654✔
455
      break;
2,045,227,986✔
456
    }
457
  }
458

459
  pOperator->resultInfo.totalRows += pRes->info.rows;
2,063,848,878✔
460

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

469
static int32_t hashGroupbyAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
2,080,460,410✔
470
  int32_t               code = TSDB_CODE_SUCCESS;
2,080,460,410✔
471
  int32_t               lino = 0;
2,080,460,410✔
472
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
2,080,460,410✔
473
  SGroupbyOperatorInfo* pInfo = pOperator->info;
2,080,462,204✔
474
  SGroupResInfo*        pGroupResInfo = &pInfo->groupResInfo;
2,080,462,576✔
475
  int32_t               order = pInfo->binfo.inputTsOrder;
2,080,460,424✔
476
  int64_t               st = taosGetTimestampUs();
2,080,459,976✔
477

478
  QRY_PARAM_CHECK(ppRes);
2,080,459,976✔
479
  if (pOperator->status == OP_EXEC_DONE) {
2,080,460,094✔
480
    return code;
16,615,750✔
481
  }
482

483
  if (pOperator->status == OP_RES_TO_RETURN) {
2,063,842,648✔
484
    (*ppRes) = buildGroupResultDataBlockByHash(pOperator);
2,045,211,849✔
485
    return code;
2,045,215,792✔
486
  }
487

488
  while (1) {
234,241,488✔
489
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
252,871,310✔
490
    if (pBlock == NULL) {
252,894,062✔
491
      break;
18,630,791✔
492
    }
493

494
    pInfo->binfo.pRes->info.scanFlag = pBlock->info.scanFlag;
234,263,271✔
495

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

500
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
501
    if (pInfo->scalarSup.pExprInfo != NULL) {
234,284,775✔
502
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
42,399,819✔
503
                                   pInfo->scalarSup.numOfExprs, NULL, GET_STM_RTINFO(pOperator->pTaskInfo));
42,409,750✔
504
      QUERY_CHECK_CODE(code, lino, _end);
42,384,980✔
505
    }
506

507
    doHashGroupbyAgg(pOperator, pBlock);
234,260,783✔
508
  }
509

510
  pOperator->status = OP_RES_TO_RETURN;
18,630,791✔
511

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

517
  if (pGroupResInfo->pBuf) {
18,631,912✔
518
    taosMemoryFree(pGroupResInfo->pBuf);
×
519
    pGroupResInfo->pBuf = NULL;
×
520
  }
521

522
  pGroupResInfo->index = 0;
18,631,298✔
523
  pGroupResInfo->iter = 0;
18,631,231✔
524
  pGroupResInfo->dataPos = NULL;
18,631,845✔
525

526
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
18,631,231✔
527

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

537
  return code;
18,630,756✔
538
}
539

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

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

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

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

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

562
  pInfo->isInit = false;
×
563

564
  return code;
×
565
}
566

567
int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo,
18,621,937✔
568
                                SOperatorInfo** pOptrInfo) {
569
  QRY_PARAM_CHECK(pOptrInfo);
18,621,937✔
570

571
  int32_t               code = TSDB_CODE_SUCCESS;
18,626,452✔
572
  int32_t               lino = 0;
18,626,452✔
573
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
18,626,452✔
574
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
18,601,395✔
575
  if (pInfo == NULL || pOperator == NULL) {
18,600,458✔
576
    code = terrno;
767✔
577
    goto _error;
×
578
  }
579

580
  pOperator->pPhyNode = (SNode*)pAggNode;
18,599,691✔
581
  pOperator->exprSupp.hasWindowOrGroup = true;
18,604,150✔
582
  pOperator->exprSupp.hasWindow = false;
18,607,142✔
583

584
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
18,608,429✔
585
  if (pResBlock == NULL) {
18,628,379✔
UNCOV
586
    code = terrno;
×
587
    goto _error;
×
588
  }
589
  initBasicInfo(&pInfo->binfo, pResBlock);
18,628,379✔
590

591
  pInfo->pGroupCols = NULL;
18,623,872✔
592
  code = extractColumnInfo(pAggNode->pGroupKeys, &pInfo->pGroupCols);
18,623,115✔
593
  QUERY_CHECK_CODE(code, lino, _error);
18,618,560✔
594

595
  int32_t    numOfScalarExpr = 0;
18,618,560✔
596
  SExprInfo* pScalarExprInfo = NULL;
18,618,560✔
597
  if (pAggNode->pExprs != NULL) {
18,622,242✔
598
    code = createExprInfo(pAggNode->pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
12,290,627✔
599
    QUERY_CHECK_CODE(code, lino, _error);
12,286,838✔
600
  }
601

602
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
18,622,629✔
603
  QUERY_CHECK_CODE(code, lino, _error);
18,622,432✔
604

605
  initResultSizeInfo(&pOperator->resultInfo, 4096);
18,622,432✔
606
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
18,623,345✔
607
  QUERY_CHECK_CODE(code, lino, _error);
18,626,446✔
608

609
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
18,626,446✔
610
  QUERY_CHECK_CODE(code, lino, _error);
18,615,272✔
611

612
  int32_t    num = 0;
18,615,272✔
613
  SExprInfo* pExprInfo = NULL;
18,615,583✔
614

615
  code = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &pExprInfo, &num);
18,617,100✔
616
  QUERY_CHECK_CODE(code, lino, _error);
18,616,256✔
617

618
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
37,229,958✔
619
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
18,617,645✔
620
  QUERY_CHECK_CODE(code, lino, _error);
18,614,938✔
621

622
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
18,624,137✔
623
                            pTaskInfo->pStreamRuntimeInfo);
18,614,938✔
624
  QUERY_CHECK_CODE(code, lino, _error);
18,615,303✔
625

626
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
18,615,303✔
627
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
18,621,974✔
628

629
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
18,626,455✔
630
  pInfo->binfo.inputTsOrder = pAggNode->node.inputTsOrder;
18,623,839✔
631
  pInfo->binfo.outputTsOrder = pAggNode->node.outputTsOrder;
18,625,703✔
632

633
  pInfo->pOperator = pOperator;
18,622,201✔
634

635
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregateNext, NULL, destroyGroupOperatorInfo,
18,613,104✔
636
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
637
  setOperatorResetStateFn(pOperator, resetGroupOperState);
18,615,881✔
638
  code = appendDownstream(pOperator, &downstream, 1);
18,619,639✔
639
  QUERY_CHECK_CODE(code, lino, _error);
18,616,548✔
640

641
  *pOptrInfo = pOperator;
18,616,548✔
642
  return TSDB_CODE_SUCCESS;
18,619,165✔
643

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

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

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

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

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

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

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

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

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

708
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
8,858,684✔
709
  int32_t                 code = TSDB_CODE_SUCCESS;
8,858,684✔
710
  int32_t                 lino = 0;
8,858,684✔
711
  SPartitionOperatorInfo* pInfo = pOperator->info;
8,858,684✔
712
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
8,859,197✔
713

714
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
2,147,483,647✔
715
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
2,147,483,647✔
716
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
2,147,483,647✔
717

718
    SDataGroupInfo* pGroupInfo = NULL;
2,147,483,647✔
719
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
2,147,483,647✔
720
    if (pPage == NULL) {
2,147,483,647✔
UNCOV
721
      T_LONG_JMP(pTaskInfo->env, terrno);
×
722
    }
723

724
    pGroupInfo->numOfRows += 1;
2,147,483,647✔
725

726
    // group id
727
    if (pGroupInfo->groupId == 0) {
2,147,483,647✔
728
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
16,623,835✔
729
    }
730

731
    if (pBlock->info.dataLoad) {
2,147,483,647✔
732
      // number of rows
733
      int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
734

735
      size_t numOfCols = pOperator->exprSupp.numOfExprs;
2,147,483,647✔
736
      for (int32_t i = 0; i < numOfCols; ++i) {
2,147,483,647✔
737
        SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
2,147,483,647✔
738
        int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
2,147,483,647✔
739

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

742
        int32_t bytes = pColInfoData->info.bytes;
2,147,483,647✔
743
        int32_t startOffset = pInfo->columnOffset[i];
2,147,483,647✔
744

745
        int32_t* columnLen = NULL;
2,147,483,647✔
746
        int32_t  contentLen = 0;
2,147,483,647✔
747

748
        if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647✔
749
          int32_t* offset = (int32_t*)((char*)pPage + startOffset);
2,147,483,647✔
750
          columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
2,147,483,647✔
751
          char* data = (char*)((char*)columnLen + sizeof(int32_t));
2,147,483,647✔
752

753
          if (colDataIsNull_s(pColInfoData, j)) {
2,147,483,647✔
754
            offset[(*rows)] = -1;
710,519,297✔
755
            contentLen = 0;
710,521,087✔
756
          } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
757
            offset[*rows] = (*columnLen);
28,736✔
758
            char*   src = colDataGetData(pColInfoData, j);
28,277✔
759
            int32_t dataLen = getJsonValueLen(src);
28,736✔
760

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

765
            contentLen = dataLen;
28,736✔
766
          } else {
767
            if (IS_STR_DATA_BLOB(pColInfoData->info.type)) {
2,147,483,647✔
768
              offset[*rows] = (*columnLen);
22,116✔
UNCOV
769
              char* src = colDataGetData(pColInfoData, j);
×
770
              memcpy(data + (*columnLen), src, blobDataTLen(src));
×
771
              int32_t v = (data + (*columnLen) + blobDataTLen(src) - (char*)pPage);
×
772
              QUERY_CHECK_CONDITION((v > 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
773

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

782
              contentLen = varDataTLen(src);
2,147,483,647✔
783
            }
784
          }
785
        } else {
786
          char* bitmap = (char*)pPage + startOffset;
2,147,483,647✔
787
          columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
2,147,483,647✔
788
          char* data = (char*)columnLen + sizeof(int32_t);
2,147,483,647✔
789

790
          bool isNull = colDataIsNull_f(pColInfoData, j);
2,147,483,647✔
791
          if (isNull) {
2,147,483,647✔
792
            colDataSetNull_f(bitmap, (*rows));
2,147,483,647✔
793
          } else {
794
            memcpy(data + (*columnLen), colDataGetData(pColInfoData, j), bytes);
2,147,483,647✔
795
            QUERY_CHECK_CONDITION(((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf)), code,
2,147,483,647✔
796
                                  lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
797
          }
798
          contentLen = bytes;
2,147,483,647✔
799
        }
800

801
        (*columnLen) += contentLen;
2,147,483,647✔
802
      }
803

804
      (*rows) += 1;
2,147,483,647✔
805

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

826
_end:
8,723,550✔
827
  if (code != TSDB_CODE_SUCCESS) {
8,859,539✔
UNCOV
828
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
829
    T_LONG_JMP(pTaskInfo->env, code);
×
830
  }
831
}
8,859,539✔
832

833
void* getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len) {
2,147,483,647✔
834
  int32_t         code = TSDB_CODE_SUCCESS;
2,147,483,647✔
835
  int32_t         lino = 0;
2,147,483,647✔
836
  SDataGroupInfo* p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
2,147,483,647✔
837

838
  void* pPage = NULL;
2,147,483,647✔
839
  if (p == NULL) {  // it is a new group
2,147,483,647✔
840
    SDataGroupInfo gi = {0};
16,624,550✔
841
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
16,624,550✔
842
    QUERY_CHECK_NULL(gi.pPageList, code, lino, _end, terrno);
16,623,567✔
843

844
    code = taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));
16,623,567✔
845
    if (code == TSDB_CODE_DUP_KEY) {
16,625,500✔
UNCOV
846
      code = TSDB_CODE_SUCCESS;
×
847
    }
848
    QUERY_CHECK_CODE(code, lino, _end);
16,625,500✔
849

850
    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);
16,625,500✔
851

852
    int32_t pageId = 0;
16,625,808✔
853
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
16,624,890✔
854
    if (pPage == NULL) {
16,623,265✔
UNCOV
855
      return pPage;
×
856
    }
857

858
    void* tmp = taosArrayPush(p->pPageList, &pageId);
16,623,265✔
859
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
16,625,101✔
860

861
    *(int32_t*)pPage = 0;
16,625,101✔
862
  } else {
863
    int32_t* curId = taosArrayGetLast(p->pPageList);
2,147,483,647✔
864
    pPage = getBufPage(pInfo->pBuf, *curId);
2,147,483,647✔
865
    if (pPage == NULL) {
2,147,483,647✔
UNCOV
866
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
867
      return pPage;
×
868
    }
869

870
    int32_t* rows = (int32_t*)pPage;
2,147,483,647✔
871
    if (*rows >= pInfo->rowCapacity) {
2,147,483,647✔
872
      // release buffer
873
      releaseBufPage(pInfo->pBuf, pPage);
86,200,592✔
874

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

883
      void* tmp = taosArrayPush(p->pPageList, &pageId);
86,201,385✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
86,203,546✔
885

886
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
86,203,546✔
887
    }
888
  }
889

890
  *pGroupInfo = p;
2,147,483,647✔
891

892
_end:
2,147,483,647✔
893
  if (code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
UNCOV
894
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
895
    return NULL;
×
896
  }
897

898
  return pPage;
2,147,483,647✔
899
}
900

901
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
1,731,690✔
902
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
1,731,690✔
903
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
1,730,812✔
904
  if (!offset) {
1,731,271✔
UNCOV
905
    return NULL;
×
906
  }
907

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

911
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
5,885,853✔
912
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
4,155,041✔
913

914
    int32_t bytes = pColInfoData->info.bytes;
4,152,347✔
915
    int32_t payloadLen = bytes * rowCapacity;
4,153,684✔
916

917
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
4,153,684✔
918
      // offset segment + content length + payload
919
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
418,241✔
920
    } else {
921
      // bitmap + content length + payload
922
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
3,735,383✔
923
    }
924
  }
925

926
  return offset;
1,730,812✔
927
}
928

929
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
1,726,199✔
930
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
1,726,199✔
931
  for (int32_t i = 0; i < size; i++) {
18,283,905✔
932
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
16,557,706✔
933
    if (pGp && pGp->blockForNotLoaded) {
16,557,706✔
UNCOV
934
      for (int32_t i = 0; i < pGp->blockForNotLoaded->size; i++) {
×
935
        SSDataBlock** pBlock = taosArrayGet(pGp->blockForNotLoaded, i);
×
936
        if (pBlock) blockDataDestroy(*pBlock);
×
937
      }
UNCOV
938
      taosArrayClear(pGp->blockForNotLoaded);
×
939
      pGp->offsetForNotLoaded = 0;
×
940
    }
941
    taosArrayDestroy(pGp->pPageList);
16,557,706✔
942
  }
943
  taosArrayClear(pInfo->sortedGroupArray);
1,726,199✔
944
  clearDiskbasedBuf(pInfo->pBuf);
1,726,658✔
945
}
1,726,658✔
946

947
static int compareDataGroupInfo(const void* group1, const void* group2) {
148,871,031✔
948
  const SDataGroupInfo* pGroupInfo1 = group1;
148,871,031✔
949
  const SDataGroupInfo* pGroupInfo2 = group2;
148,871,031✔
950

951
  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
148,871,031✔
UNCOV
952
    return 0;
×
953
  }
954

955
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
148,871,031✔
956
}
957

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

970
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
110,382,699✔
971
  int32_t                 code = TSDB_CODE_SUCCESS;
110,382,699✔
972
  int32_t                 lino = 0;
110,382,699✔
973
  SPartitionOperatorInfo* pInfo = pOperator->info;
110,382,699✔
974
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
110,388,972✔
975

976
  if (pInfo->remainRows == 0) {
110,388,288✔
977
    blockDataCleanup(pInfo->binfo.pRes);
100,744,633✔
978
    SDataGroupInfo* pGroupInfo =
100,744,667✔
979
        (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
100,742,981✔
980
    if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
100,744,667✔
981
      if (pGroupInfo != NULL) {
18,316,828✔
982
        SSDataBlock* ret = buildPartitionResultForNotLoadBlock(pGroupInfo);
16,583,801✔
983
        if (ret != NULL) return ret;
16,582,484✔
984
      }
985
      // try next group data
986
      if (pInfo->groupIndex + 1 >= taosArrayGetSize(pInfo->sortedGroupArray)) {
18,315,511✔
987
        setOperatorCompleted(pOperator);
1,726,239✔
988
        clearPartitionOperator(pInfo);
1,727,117✔
989
        return NULL;
1,727,117✔
990
      }
991
      ++pInfo->groupIndex;
16,590,629✔
992

993
      pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
16,591,088✔
994
      if (pGroupInfo == NULL) {
16,590,190✔
UNCOV
995
        qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
996
        T_LONG_JMP(pTaskInfo->env, terrno);
×
997
      }
998
      pInfo->pageIndex = 0;
16,590,190✔
999
    }
1000

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

1026
    code = blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
99,016,256✔
1027
    QUERY_CHECK_CODE(code, lino, _end);
99,018,026✔
1028

1029
    code = blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
99,018,026✔
1030
    QUERY_CHECK_CODE(code, lino, _end);
99,004,693✔
1031

1032
    pInfo->pageIndex += 1;
99,004,693✔
1033
    releaseBufPage(pInfo->pBuf, page);
99,017,128✔
1034
    pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
99,016,844✔
1035
    pInfo->binfo.pRes->info.dataLoad = 1;
99,015,946✔
1036
    pInfo->orderedRows = 0;
99,016,292✔
1037
  } else if (pInfo->pOrderInfoArr == NULL) {
9,644,841✔
UNCOV
1038
    qError("Exception, remainRows not zero, but pOrderInfoArr is NULL");
×
1039
  }
1040

1041
  if (pInfo->pOrderInfoArr) {
108,660,634✔
1042
    pInfo->binfo.pRes->info.rows += pInfo->remainRows;
28,236,050✔
1043
    code = blockDataTrimFirstRows(pInfo->binfo.pRes, pInfo->orderedRows);
28,234,481✔
1044
    QUERY_CHECK_CODE(code, lino, _end);
28,231,745✔
1045
    pInfo->orderedRows = blockDataGetSortedRows(pInfo->binfo.pRes, pInfo->pOrderInfoArr);
28,231,745✔
1046
    pInfo->remainRows = pInfo->binfo.pRes->info.rows - pInfo->orderedRows;
28,236,221✔
1047
    pInfo->binfo.pRes->info.rows = pInfo->orderedRows;
28,236,221✔
1048
  }
1049

1050
  code = blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
108,660,593✔
1051
  QUERY_CHECK_CODE(code, lino, _end);
108,645,815✔
1052

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

1059
  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
108,645,815✔
1060
  return pInfo->binfo.pRes;
108,658,744✔
1061
}
1062

1063
static int32_t hashPartitionNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
110,383,778✔
1064
  if (pOperator->status == OP_EXEC_DONE) {
110,383,778✔
1065
    (*ppRes) = NULL;
474✔
1066
    return TSDB_CODE_SUCCESS;
474✔
1067
  }
1068

1069
  int32_t                 code = TSDB_CODE_SUCCESS;
110,385,835✔
1070
  int32_t                 lino = 0;
110,385,835✔
1071
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
110,385,835✔
1072
  SPartitionOperatorInfo* pInfo = pOperator->info;
110,386,706✔
1073
  SSDataBlock*            pRes = pInfo->binfo.pRes;
110,389,260✔
1074

1075
  if (pOperator->status == OP_RES_TO_RETURN) {
110,389,699✔
1076
    (*ppRes) = buildPartitionResult(pOperator);
108,653,884✔
1077
    return code;
108,654,847✔
1078
  }
1079

1080
  int64_t        st = taosGetTimestampUs();
1,733,945✔
1081
  SOperatorInfo* downstream = pOperator->pDownstream[0];
1,733,945✔
1082

1083
  while (1) {
8,859,120✔
1084
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
10,592,626✔
1085
    if (pBlock == NULL) {
10,592,746✔
1086
      break;
1,733,945✔
1087
    }
1088

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

1098
    terrno = TSDB_CODE_SUCCESS;
8,859,217✔
1099
    doHashPartition(pOperator, pBlock);
8,859,197✔
1100
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
8,858,490✔
UNCOV
1101
      code = terrno;
×
1102
      QUERY_CHECK_CODE(code, lino, _end);
×
1103
    }
1104
  }
1105

1106
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
1,733,945✔
1107
  QUERY_CHECK_NULL(groupArray, code, lino, _end, terrno);
1,733,486✔
1108

1109
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
1,733,486✔
1110
  while (pGroupIter != NULL) {
18,360,343✔
1111
    SDataGroupInfo* pGroupInfo = pGroupIter;
16,626,857✔
1112
    void*           tmp = taosArrayPush(groupArray, pGroupInfo);
16,626,857✔
1113
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
16,626,857✔
1114
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
16,626,857✔
1115
  }
1116

1117
  taosArraySort(groupArray, compareDataGroupInfo);
1,733,486✔
1118
  pInfo->sortedGroupArray = groupArray;
1,733,945✔
1119
  pInfo->groupIndex = -1;
1,733,945✔
1120
  taosHashClear(pInfo->pGroupSet);
1,733,945✔
1121

1122
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
1,733,945✔
1123

1124
  pOperator->status = OP_RES_TO_RETURN;
1,733,945✔
1125
  code = blockDataEnsureCapacity(pRes, 4096);
1,733,945✔
1126
  QUERY_CHECK_CODE(code, lino, _end);
1,733,945✔
1127

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

1135
  (*ppRes) = buildPartitionResult(pOperator);
1,733,945✔
1136
  return code;
1,729,934✔
1137
}
1138

1139
static void destroyPartitionOperatorInfo(void* param) {
1,733,945✔
1140
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
1,733,945✔
1141
  cleanupBasicInfo(&pInfo->binfo);
1,733,945✔
1142
  taosArrayDestroy(pInfo->pGroupCols);
1,733,945✔
1143

1144
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
3,871,100✔
1145
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
2,137,155✔
1146
    taosMemoryFree(key.pData);
2,137,155✔
1147
  }
1148

1149
  taosArrayDestroy(pInfo->pGroupColVals);
1,733,945✔
1150
  taosMemoryFree(pInfo->keyBuf);
1,733,945✔
1151

1152
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
1,733,945✔
1153
  for (int32_t i = 0; i < size; i++) {
1,801,739✔
1154
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
67,794✔
1155
    if (pGp) {
67,794✔
1156
      taosArrayDestroy(pGp->pPageList);
67,794✔
1157
    }
1158
  }
1159
  taosArrayDestroy(pInfo->sortedGroupArray);
1,733,945✔
1160

1161
  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
1,733,945✔
1162
  while (pGroupIter != NULL) {
1,733,506✔
UNCOV
1163
    SDataGroupInfo* pGroupInfo = pGroupIter;
×
1164
    taosArrayDestroy(pGroupInfo->pPageList);
×
1165
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
×
1166
  }
1167

1168
  taosHashCleanup(pInfo->pGroupSet);
1,733,506✔
1169
  taosMemoryFree(pInfo->columnOffset);
1,733,945✔
1170

1171
  cleanupExprSupp(&pInfo->scalarSup);
1,733,945✔
1172
  destroyDiskbasedBuf(pInfo->pBuf);
1,733,945✔
1173
  taosArrayDestroy(pInfo->pOrderInfoArr);
1,733,047✔
1174
  taosMemoryFreeClear(param);
1,733,486✔
1175
}
1,733,945✔
1176

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

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

UNCOV
1186
  clearPartitionOperator(pInfo);
×
1187

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

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

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

1213
int32_t createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
1,732,149✔
1214
                                           SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
1215
  QRY_PARAM_CHECK(pOptrInfo);
1,732,149✔
1216

1217
  int32_t                 code = TSDB_CODE_SUCCESS;
1,732,588✔
1218
  int32_t                 lino = 0;
1,732,588✔
1219
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
1,732,588✔
1220
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
1,728,098✔
1221
  if (pInfo == NULL || pOperator == NULL) {
1,729,056✔
UNCOV
1222
    pTaskInfo->code = code = terrno;
×
1223
    goto _error;
×
1224
  }
1225

1226
  pOperator->pPhyNode = pPartNode;
1,729,495✔
1227
  int32_t    numOfCols = 0;
1,730,852✔
1228
  SExprInfo* pExprInfo = NULL;
1,731,291✔
1229
  code = createExprInfo(pPartNode->pTargets, NULL, &pExprInfo, &numOfCols);
1,728,617✔
1230
  QUERY_CHECK_CODE(code, lino, _error);
1,733,945✔
1231
  pOperator->exprSupp.numOfExprs = numOfCols;
1,733,945✔
1232
  pOperator->exprSupp.pExprInfo = pExprInfo;
1,733,506✔
1233

1234
  pInfo->pGroupCols = makeColumnArrayFromList(pPartNode->pPartitionKeys);
1,733,506✔
1235

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

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

1248
  if (pPartNode->pExprs != NULL) {
1,731,231✔
1249
    int32_t    num = 0;
24,050✔
1250
    SExprInfo* pExprInfo1 = NULL;
24,050✔
1251
    code = createExprInfo(pPartNode->pExprs, NULL, &pExprInfo1, &num);
24,050✔
1252
    QUERY_CHECK_CODE(code, lino, _error);
24,050✔
1253

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

1258
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
1,732,568✔
1259
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
1,730,313✔
1260
  if (pInfo->pGroupSet == NULL) {
1,733,506✔
UNCOV
1261
    goto _error;
×
1262
  }
1263

1264
  uint32_t defaultPgsz = 0;
1,732,149✔
1265
  int64_t  defaultBufsz = 0;
1,731,710✔
1266

1267
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
1,733,506✔
1268
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
1,733,945✔
1269
  code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
1,733,486✔
1270
  if (code != TSDB_CODE_SUCCESS) {
1,731,690✔
UNCOV
1271
    goto _error;
×
1272
  }
1273

1274
  if (!osTempSpaceAvailable()) {
1,731,690✔
UNCOV
1275
    terrno = TSDB_CODE_NO_DISKSPACE;
×
1276
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
×
1277
    goto _error;
×
1278
  }
1279

1280
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
1,730,752✔
1281
  if (code != TSDB_CODE_SUCCESS) {
1,732,149✔
UNCOV
1282
    goto _error;
×
1283
  }
1284

1285
  pInfo->rowCapacity =
1,729,455✔
1286
      blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
1,729,914✔
1287
                                blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
1,732,149✔
1288
  if (pInfo->rowCapacity < 0) {
1,729,455✔
UNCOV
1289
    code = terrno;
×
1290
    goto _error;
×
1291
  }
1292

1293
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
1,729,894✔
1294
  QUERY_CHECK_NULL(pInfo->columnOffset, code, lino, _error, terrno);
1,731,670✔
1295

1296
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
1,727,200✔
1297
  if (code != TSDB_CODE_SUCCESS) {
1,731,251✔
UNCOV
1298
    goto _error;
×
1299
  }
1300

1301
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
1,731,251✔
1302
                  pTaskInfo);
1303

1304
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashPartitionNext, NULL, destroyPartitionOperatorInfo,
1,732,588✔
1305
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
1306

1307
  setOperatorResetStateFn(pOperator, resetPartitionOperState);
1,730,333✔
1308
  code = appendDownstream(pOperator, &downstream, 1);
1,732,129✔
1309
  if (code != TSDB_CODE_SUCCESS) {
1,730,772✔
UNCOV
1310
    goto _error;
×
1311
  }
1312

1313
  *pOptrInfo = pOperator;
1,730,772✔
1314
  return TSDB_CODE_SUCCESS;
1,730,772✔
1315

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

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

1331
  SResultRow* pResultRow = doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo,
2,147,483,647✔
1332
                                                  false, pAggSup, false);
1333
  if (pResultRow == NULL || pTaskInfo->code != 0) {
2,147,483,647✔
UNCOV
1334
    return pTaskInfo->code;
×
1335
  }
1336

1337
  return setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
2,147,483,647✔
1338
}
1339

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

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

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

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

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

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

1400
int32_t extractColumnInfo(SNodeList* pNodeList, SArray** pArrayRes) {
18,625,226✔
1401
  int32_t code = TSDB_CODE_SUCCESS;
18,625,226✔
1402
  int32_t lino = 0;
18,625,226✔
1403
  size_t  numOfCols = LIST_LENGTH(pNodeList);
18,625,226✔
1404
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
18,628,001✔
1405
  if (pList == NULL) {
18,612,523✔
UNCOV
1406
    code = terrno;
×
1407
    (*pArrayRes) = NULL;
×
1408
    QUERY_CHECK_CODE(code, lino, _end);
4,574✔
1409
  }
1410

1411
  for (int32_t i = 0; i < numOfCols; ++i) {
41,544,041✔
1412
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
22,930,899✔
1413
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
22,936,306✔
1414

1415
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
22,936,306✔
1416
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
22,935,946✔
1417

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

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

1436
  (*pArrayRes) = pList;
18,613,142✔
1437

1438
_end:
18,622,218✔
1439
  if (code != TSDB_CODE_SUCCESS) {
18,622,218✔
UNCOV
1440
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1441
  }
1442
  return code;
18,623,664✔
1443
}
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