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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

83.04
/source/libs/executor/src/sortoperator.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 "executorInt.h"
17
#include "filter.h"
18
#include "operator.h"
19
#include "querytask.h"
20
#include "tdatablock.h"
21

22
typedef struct SSortOpGroupIdCalc {
23
  STupleHandle* pSavedTuple;
24
  SArray*       pSortColsArr;
25
  char*         keyBuf;
26
  int32_t       lastKeysLen; // default to be 0
27
  uint64_t      lastGroupId;
28
  bool          excludePKCol;
29
} SSortOpGroupIdCalc;
30

31
typedef struct SSortOperatorInfo {
32
  SOptrBasicInfo      binfo;
33
  uint32_t            sortBufSize;  // max buffer size for in-memory sort
34
  SArray*             pSortInfo;
35
  SSortHandle*        pSortHandle;
36
  SColMatchInfo       matchInfo;
37
  int32_t             bufPageSize;
38
  int64_t             startTs;      // sort start time
39
  uint64_t            sortElapsed;  // sort elapsed time, time to flush to disk not included.
40
  SLimitInfo          limitInfo;
41
  uint64_t            maxTupleLength;
42
  int64_t             maxRows;
43
  SSortOpGroupIdCalc* pGroupIdCalc;
44
} SSortOperatorInfo;
45

46
static int32_t doSort(SOperatorInfo* pOperator, SSDataBlock** pResBlock);
47
static int32_t doOpenSortOperator(SOperatorInfo* pOperator);
48
static int32_t getExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len);
49
static int32_t doGroupSort(SOperatorInfo* pOperator, SSDataBlock** pResBlock);
50

51
static void destroySortOperatorInfo(void* param);
52
static void calcSortOperMaxTupleLength(SSortOperatorInfo* pSortOperInfo, SNodeList* pSortKeys);
53

54
// Check whether the given slotId is the output of a Sort scalar pre-calculation expression.
55
// The planner's rewritePrecalcExprs pushes expression results (e.g. ts+1000) into extra slots
56
// in the child's output descriptor. This helper identifies such slots.
57
static bool sortIsExprResultSlot(const SOperatorInfo* pOperator, int32_t slotId) {
33,963,728✔
58
  if (pOperator == NULL || pOperator->exprSupp.pExprInfo == NULL || slotId < 0) {
33,963,728✔
59
    return false;
24,723,012✔
60
  }
61
  for (int32_t idx = 0; idx < pOperator->exprSupp.numOfExprs; ++idx) {
14,258,429✔
62
    if (pOperator->exprSupp.pExprInfo[idx].base.resSchema.slotId == slotId) {
9,261,483✔
63
      return true;
4,243,770✔
64
    }
65
  }
66
  return false;
4,996,946✔
67
}
68

69
// Find the slot index of the original (non-expression) primary timestamp column in the
70
// Sort's internal data block.  The internal block may contain both the original ts column
71
// and an expression-derived ts column (e.g. ts+1000).  We return the first TIMESTAMP
72
// column whose slot is NOT an expression result.
73
static int32_t sortFindOrigTsSlot(const SOperatorInfo* pOperator, const SSDataBlock* pBlock) {
4,243,770✔
74
  if (pBlock == NULL || pBlock->pDataBlock == NULL) {
4,243,770✔
75
    return -1;
×
76
  }
77
  int32_t colCount = (int32_t)taosArrayGetSize(pBlock->pDataBlock);
4,243,770✔
78
  for (int32_t idx = 0; idx < colCount; ++idx) {
4,243,770✔
79
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, idx);
4,243,770✔
80
    if (pCol != NULL && pCol->info.type == TSDB_DATA_TYPE_TIMESTAMP &&
4,243,770✔
81
        !sortIsExprResultSlot(pOperator, idx)) {
4,243,770✔
82
      return idx;
4,243,770✔
83
    }
84
  }
85
  return -1;
×
86
}
87

88
static void destroySortOpGroupIdCalc(SSortOpGroupIdCalc* pCalc);
89

90
static int32_t resetSortOperState(SOperatorInfo* pOper) {
359,935✔
91
  SSortOperatorInfo* pInfo = pOper->info;
359,935✔
92
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
359,935✔
93
  pOper->status = OP_NOT_OPENED;
359,935✔
94

95
  resetBasicOperatorState(&pInfo->binfo);
359,935✔
96
  destroySqlFunctionCtx(pOper->exprSupp.pCtx, pOper->exprSupp.pExprInfo, pOper->exprSupp.numOfExprs);
359,935✔
97
  taosMemoryFreeClear(pOper->exprSupp.rowEntryInfoOffset);
359,935✔
98
  pOper->exprSupp.pCtx =
359,935✔
99
      createSqlFunctionCtx(pOper->exprSupp.pExprInfo, pOper->exprSupp.numOfExprs, &pOper->exprSupp.rowEntryInfoOffset, &pTaskInfo->storageAPI.functionStore);
359,935✔
100

101
  tsortDestroySortHandle(pInfo->pSortHandle);
359,935✔
102
  pInfo->pSortHandle = NULL;
359,935✔
103

104
  if (pInfo->pGroupIdCalc) {
359,935✔
105
    pInfo->pGroupIdCalc->lastGroupId = 0;
×
106
    pInfo->pGroupIdCalc->lastKeysLen = 0;
×
107
  }
108

109
  return 0;
359,935✔
110
}
111

112
// todo add limit/offset impl
113
int32_t createSortOperatorInfo(SOperatorInfo* downstream, SSortPhysiNode* pSortNode, SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
35,408,100✔
114
  QRY_PARAM_CHECK(pOptrInfo);
35,408,100✔
115

116
  int32_t code = 0;
35,409,386✔
117
  int32_t lino = 0;
35,409,386✔
118

119
  SSortOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortOperatorInfo));
35,409,386✔
120
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
35,385,664✔
121
  if (pInfo == NULL || pOperator == NULL) {
35,392,850✔
UNCOV
122
    code = terrno;
×
123
    goto _error;
×
124
  }
125
  initOperatorCostInfo(pOperator);
35,394,645✔
126

127
  pOperator->pTaskInfo = pTaskInfo;
35,408,230✔
128
  SDataBlockDescNode* pDescNode = pSortNode->node.pOutputDataBlockDesc;
35,413,979✔
129

130
  int32_t numOfCols = 0;
35,403,880✔
131
  code = createExprInfo(pSortNode->pExprs, NULL, &pOperator->exprSupp.pExprInfo, &numOfCols);
35,407,632✔
132
  QUERY_CHECK_CODE(code, lino, _error);
35,397,718✔
133

134
  pOperator->exprSupp.numOfExprs = numOfCols;
35,397,718✔
135
  int32_t numOfOutputCols = 0;
35,401,863✔
136
  code =
137
      extractColMatchInfo(pSortNode->pTargets, pDescNode, &numOfOutputCols, COL_MATCH_FROM_SLOT_ID, &pInfo->matchInfo);
35,393,326✔
138
  if (code != TSDB_CODE_SUCCESS) {
35,409,425✔
139
    goto _error;
×
140
  }
141
  
142
  calcSortOperMaxTupleLength(pInfo, pSortNode->pSortKeys);
35,409,425✔
143
  pInfo->maxRows = -1;
35,407,595✔
144
  if (pSortNode->node.pLimit && ((SLimitNode*)pSortNode->node.pLimit)->limit) {
35,412,047✔
145
    SLimitNode* pLimit = (SLimitNode*)pSortNode->node.pLimit;
5,117,818✔
146
    if (pLimit->limit->datum.i > 0) {
5,117,442✔
147
      pInfo->maxRows = pLimit->limit->datum.i + (pLimit->offset ? pLimit->offset->datum.i : 0);
5,110,419✔
148
    }
149
  }
150

151
  pOperator->exprSupp.pCtx =
35,399,124✔
152
      createSqlFunctionCtx(pOperator->exprSupp.pExprInfo, numOfCols, &pOperator->exprSupp.rowEntryInfoOffset, &pTaskInfo->storageAPI.functionStore);
35,406,202✔
153
  QUERY_CHECK_NULL(pOperator->exprSupp.pCtx, code, lino, _error, terrno);
35,398,561✔
154
  initResultSizeInfo(&pOperator->resultInfo, 1024);
35,395,431✔
155
  code = filterInitFromNode((SNode*)pSortNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
35,400,841✔
156
                            pTaskInfo->pStreamRuntimeInfo);
35,398,470✔
157
  if (code != TSDB_CODE_SUCCESS) {
35,398,009✔
158
    goto _error;
×
159
  }
160

161
  pInfo->binfo.pRes = createDataBlockFromDescNode(pDescNode);
35,398,009✔
162
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
35,411,525✔
163

164
  pInfo->pSortInfo = createSortInfo(pSortNode->pSortKeys);
35,406,039✔
165
  TSDB_CHECK_NULL(pInfo->pSortInfo, code, lino, _error, terrno);
35,410,845✔
166

167
  if (pSortNode->calcGroupId) {
35,390,931✔
168
    int32_t keyLen;
118,472✔
169
    SSortOpGroupIdCalc* pGroupIdCalc = pInfo->pGroupIdCalc = taosMemoryCalloc(1, sizeof(SSortOpGroupIdCalc));
118,472✔
170
    if (!pGroupIdCalc) {
118,472✔
171
      code = terrno;
×
172
      goto _error;
×
173
    }
174
    SNodeList* pSortColsNodeArr = makeColsNodeArrFromSortKeys(pSortNode->pSortKeys);
118,472✔
175
    if (!pSortColsNodeArr) code = terrno;
118,472✔
176
    if (TSDB_CODE_SUCCESS == code) {
118,472✔
177
      pGroupIdCalc->pSortColsArr = makeColumnArrayFromList(pSortColsNodeArr);
118,472✔
178
      if (!pGroupIdCalc->pSortColsArr) code = terrno;
118,472✔
179
      nodesClearList(pSortColsNodeArr);
118,472✔
180
    }
181
    if (TSDB_CODE_SUCCESS == code) {
118,472✔
182
      // PK ts col should always at last, see partColOptCreateSort
183
      if (pSortNode->excludePkCol) taosArrayPop(pGroupIdCalc->pSortColsArr);
118,472✔
184
      code = extractKeysLen(pGroupIdCalc->pSortColsArr, &keyLen);
118,472✔
185
      QUERY_CHECK_CODE(code, lino, _error);
118,472✔
186
    }
187
    if (TSDB_CODE_SUCCESS == code) {
118,472✔
188
      pGroupIdCalc->lastKeysLen = 0;
118,472✔
189
      pGroupIdCalc->keyBuf = taosMemoryCalloc(1, keyLen);
118,472✔
190
      if (!pGroupIdCalc->keyBuf) {
118,472✔
191
        code = terrno;
×
192
      }
193
    }
194
  }
195
  if (code != TSDB_CODE_SUCCESS) goto _error;
35,405,774✔
196

197
  pInfo->binfo.inputTsOrder = pSortNode->node.inputTsOrder;
35,405,774✔
198
  pInfo->binfo.outputTsOrder = pSortNode->node.outputTsOrder;
35,403,242✔
199
  initLimitInfo(pSortNode->node.pLimit, pSortNode->node.pSlimit, &pInfo->limitInfo);
35,393,241✔
200

201
  setOperatorInfo(pOperator, "SortOperator", QUERY_NODE_PHYSICAL_PLAN_SORT, true, OP_NOT_OPENED, pInfo, pTaskInfo);
35,408,471✔
202

203

204
  // lazy evaluation for the following parameter since the input datablock is not known till now.
205
  //  pInfo->bufPageSize  = rowSize < 1024 ? 1024 * 2 : rowSize * 2;
206
  //  there are headers, so pageSize = rowSize + header pInfo->sortBufSize  = pInfo->bufPageSize * 16;
207
  // TODO dynamic set the available sort buffer
208

209
  pOperator->fpSet =
210
      createOperatorFpSet(doOpenSortOperator, doSort, NULL, destroySortOperatorInfo, optrDefaultBufFn, getExplainExecInfo, optrDefaultGetNextExtFn, NULL);
35,411,808✔
211

212
  setOperatorResetStateFn(pOperator, resetSortOperState);
35,401,006✔
213
  code = appendDownstream(pOperator, &downstream, 1);
35,397,233✔
214
  if (code != TSDB_CODE_SUCCESS) {
35,406,017✔
215
    goto _error;
×
216
  }
217

218
  *pOptrInfo = pOperator;
35,406,017✔
219
  return TSDB_CODE_SUCCESS;
35,406,017✔
220

UNCOV
221
_error:
×
222
  if (pInfo != NULL) {
×
223
    destroySortOperatorInfo(pInfo);
×
224
  }
225
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
226
  pTaskInfo->code = code;
×
227
  return code;
×
228
}
229

230
int32_t appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHandle) {
2,147,483,647✔
231
  int32_t code = 0;
2,147,483,647✔
232
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); ++i) {
2,147,483,647✔
233
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i);
2,147,483,647✔
234
    if (pColInfo == NULL) {
2,147,483,647✔
235
      return terrno;
×
236
    }
237

238
    bool isNull = tsortIsNullVal(pTupleHandle, i);
2,147,483,647✔
239
    if (isNull) {
2,147,483,647✔
240
      colDataSetNULL(pColInfo, pBlock->info.rows);
2,147,483,647✔
241
    } else {
242
      char* pData = NULL;
2,147,483,647✔
243
      tsortGetValue(pTupleHandle, i, (void**) &pData);
2,147,483,647✔
244

245
      if (pData != NULL) {
2,147,483,647✔
246
        code = colDataSetVal(pColInfo, pBlock->info.rows, pData, false);
2,147,483,647✔
247
        if (code) {
2,147,483,647✔
248
          return code;
×
249
        }
250
      }
251
    }
252
  }
253

254
  pBlock->info.dataLoad = 1;
2,147,483,647✔
255

256
  SDataBlockInfo info = {0};
2,147,483,647✔
257
  tsortGetBlockInfo(pTupleHandle, &info);
2,147,483,647✔
258

259
  pBlock->info.scanFlag = info.scanFlag;
2,147,483,647✔
260
  pBlock->info.rows += 1;
2,147,483,647✔
261
  return code;
2,147,483,647✔
262
}
263

264
/**
265
 * @brief get next tuple with group id attached, here assume that all tuples are sorted by group keys
266
 * @param [in, out] pBlock the output block, the group id will be saved in it
267
 * @retval NULL if next group tuple arrived and this new group tuple will be saved in pInfo.pSavedTuple
268
 */
269
static int32_t nextTupleWithGroupId(SSortHandle* pHandle, SSortOperatorInfo* pInfo, SSDataBlock* pBlock,
2,147,483,647✔
270
                                    STupleHandle** pTupleHandle) {
271
  QRY_PARAM_CHECK(pTupleHandle);
2,147,483,647✔
272

273
  int32_t       code = 0;
2,147,483,647✔
274
  STupleHandle* retTuple = pInfo->pGroupIdCalc->pSavedTuple;
2,147,483,647✔
275
  if (!retTuple) {
2,147,483,647✔
276
    code = tsortNextTuple(pHandle, &retTuple);
2,147,483,647✔
277
    if (code) {
2,147,483,647✔
278
      qError("failed to get next tuple, code:%s", tstrerror(code));
×
279
      return code;
×
280
    }
281
  }
282

283
  if (retTuple) {
2,147,483,647✔
284
    int32_t newGroup;
285
    if (pInfo->pGroupIdCalc->pSavedTuple) {
2,147,483,647✔
286
      newGroup = true;
1,672,664✔
287
      pInfo->pGroupIdCalc->pSavedTuple = NULL;
1,672,664✔
288
    } else {
289
      newGroup = tsortCompAndBuildKeys(pInfo->pGroupIdCalc->pSortColsArr, pInfo->pGroupIdCalc->keyBuf,
2,147,483,647✔
290
                                       &pInfo->pGroupIdCalc->lastKeysLen, retTuple);
2,147,483,647✔
291
    }
292

293
    bool emptyBlock = (pBlock->info.rows == 0);
2,147,483,647✔
294
    if (newGroup) {
2,147,483,647✔
295
      if (!emptyBlock) {
3,464,804✔
296
        // new group arrived, and we have already copied some tuples for cur group, save the new group tuple, return
297
        // NULL. Note that the keyBuf and lastKeysLen has been updated to new value
298
        pInfo->pGroupIdCalc->pSavedTuple = retTuple;
1,673,668✔
299
        retTuple = NULL;
1,673,668✔
300
      } else {
301
        // new group with empty block
302
        pInfo->pGroupIdCalc->lastGroupId = pBlock->info.id.groupId =
1,791,136✔
303
            calcGroupId(pInfo->pGroupIdCalc->keyBuf, pInfo->pGroupIdCalc->lastKeysLen);
1,791,136✔
304
      }
305
    } else {
306
      if (emptyBlock) {
2,147,483,647✔
307
        // new block but not new group, assign last group id to it
308
        pBlock->info.id.groupId = pInfo->pGroupIdCalc->lastGroupId;
1,027,594✔
309
      } else {
310
        // not new group and not empty block and ret NOT NULL, just return the tuple
311
      }
312
    }
313
  }
314

315
  *pTupleHandle = retTuple;
2,147,483,647✔
316
  return code;
2,147,483,647✔
317
}
318

319
static int32_t getSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, int32_t capacity, SArray* pColMatchInfo,
86,450,601✔
320
                                  const SOperatorInfo* pOperator, SSortOperatorInfo* pInfo, SSDataBlock** pResBlock) {
321
  QRY_PARAM_CHECK(pResBlock);
86,450,601✔
322
  blockDataCleanup(pDataBlock);
86,450,601✔
323

324
  int32_t       lino = 0;
86,450,601✔
325
  int32_t       code = 0;
86,450,601✔
326
  STupleHandle* pTupleHandle = NULL;
86,450,601✔
327
  SSDataBlock*  p = NULL;
86,450,601✔
328

329
  code = tsortGetSortedDataBlock(pHandle, &p);
86,450,601✔
330
  if (p == NULL || (code != 0)) {
86,451,182✔
331
    return code;
6,762,841✔
332
  }
333

334
  code = blockDataEnsureCapacity(p, capacity);
79,688,341✔
335
  QUERY_CHECK_CODE(code, lino, _error);
79,688,341✔
336

337
  while (1) {
338
    if (pInfo->pGroupIdCalc) {
2,147,483,647✔
339
      code = nextTupleWithGroupId(pHandle, pInfo, p, &pTupleHandle);
2,147,483,647✔
340
    } else {
341
      code = tsortNextTuple(pHandle, &pTupleHandle);
2,147,483,647✔
342
    }
343

344
    TSDB_CHECK_CODE(code, lino, _error);
2,147,483,647✔
345
    if (pTupleHandle == NULL) {
2,147,483,647✔
346
      break;
57,378,735✔
347
    }
348

349
    code = appendOneRowToDataBlock(p, pTupleHandle);
2,147,483,647✔
350
    QUERY_CHECK_CODE(code, lino, _error);
2,147,483,647✔
351

352
    if (p->info.rows >= capacity) {
2,147,483,647✔
353
      break;
22,309,606✔
354
    }
355
  }
356

357
  QUERY_CHECK_CODE(code, lino, _error);
79,688,341✔
358

359
  if (p->info.rows > 0) {
79,688,341✔
360
    code = blockDataEnsureCapacity(pDataBlock, capacity);
52,553,667✔
361
    QUERY_CHECK_CODE(code, lino, _error);
52,553,239✔
362

363
    // todo extract function to handle this
364
    int32_t numOfCols = taosArrayGetSize(pColMatchInfo);
52,553,239✔
365
    for (int32_t i = 0; i < numOfCols; ++i) {
292,245,922✔
366
      SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, i);
239,692,683✔
367
      QUERY_CHECK_NULL(pmInfo, code, lino, _error, terrno);
239,692,683✔
368

369
      int32_t srcSlotId = pmInfo->srcSlotId;
239,692,683✔
370

371
      // Fix: when the planner's setListSlotId resolves Sort pTargets by name, it may
372
      // mistakenly bind the primary timestamp column to the scalar expression result slot
373
      // (e.g. slot for ts+1000) instead of the original ts slot, because pushdownDataBlockSlots
374
      // added the expression slot with the same column name.  This causes the downstream
375
      // Project operator to apply the expression again (ts+1000 becomes ts+2000).
376
      // Detect this case and fall back to the original timestamp slot.
377
      if (pmInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID &&
239,692,683✔
378
          pmInfo->dataType.type == TSDB_DATA_TYPE_TIMESTAMP &&
59,556,287✔
379
          sortIsExprResultSlot(pOperator, srcSlotId)) {
29,719,538✔
380
        int32_t origSlot = sortFindOrigTsSlot(pOperator, p);
4,243,770✔
381
        if (origSlot >= 0 && origSlot != srcSlotId) {
4,243,770✔
382
          srcSlotId = origSlot;
4,243,770✔
383
        }
384
      }
385

386
      SColumnInfoData* pSrc = taosArrayGet(p->pDataBlock, srcSlotId);
239,692,683✔
387
      QUERY_CHECK_NULL(pSrc, code, lino, _error, terrno);
239,692,683✔
388

389
      SColumnInfoData* pDst = taosArrayGet(pDataBlock->pDataBlock, pmInfo->dstSlotId);
239,692,683✔
390
      QUERY_CHECK_NULL(pDst, code, lino, _error, terrno);
239,692,683✔
391

392
      code = colDataAssign(pDst, pSrc, p->info.rows, &pDataBlock->info);
239,692,683✔
393
      QUERY_CHECK_CODE(code, lino, _error);
239,692,255✔
394
    }
395

396
    pDataBlock->info.dataLoad = 1;
52,553,239✔
397
    pDataBlock->info.rows = p->info.rows;
52,553,239✔
398
    pDataBlock->info.scanFlag = p->info.scanFlag;
52,553,097✔
399
    // propagate both C-group id and baseGId from upstream
400
    pDataBlock->info.id.groupId = p->info.id.groupId;
52,553,667✔
401
    pDataBlock->info.id.baseGId = p->info.id.baseGId;
52,553,097✔
402
  }
403

404
  blockDataDestroy(p);
79,687,771✔
405
  *pResBlock = (pDataBlock->info.rows > 0) ? pDataBlock : NULL;
79,685,370✔
406
  return code;
79,686,390✔
407

408
  _error:
×
409
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
410

411
  blockDataDestroy(p);
×
412
  return code;
×
413
}
414

415
int32_t loadNextDataBlock(void* param, SSDataBlock** ppBlock) {
2,147,483,647✔
416
  SOperatorInfo* pOperator = (SOperatorInfo*)param;
2,147,483,647✔
417
  int32_t        code = pOperator->fpSet.getNextFn(pOperator, ppBlock);
2,147,483,647✔
418
  if (code) {
2,147,483,647✔
419
    qError("failed to get next data block from upstream, %s code:%s", __func__, tstrerror(code));
×
420
  } else {
421
    code = blockDataCheck(*ppBlock);
2,147,483,647✔
422
    if (code) {
2,147,483,647✔
423
      qError("failed to check block data, %s code:%s", __func__, tstrerror(code));
×
424
    }
425
  }
426
  return code;
2,147,483,647✔
427
}
428

429
// todo refactor: merged with fetch fp
430
void applyScalarFunction(SSDataBlock* pBlock, void* param) {
2,147,483,647✔
431
  SOperatorInfo*     pOperator = param;
2,147,483,647✔
432
  SSortOperatorInfo* pSort = pOperator->info;
2,147,483,647✔
433
  if (pOperator->exprSupp.pExprInfo != NULL && pOperator->exprSupp.numOfExprs > 0) {
2,147,483,647✔
434
    int32_t code = projectApplyFunctions(pOperator->exprSupp.pExprInfo, pBlock, pBlock, pOperator->exprSupp.pCtx,
12,463,678✔
435
                                         pOperator->exprSupp.numOfExprs, NULL,
436
                                         GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
6,231,839✔
437
    if (code != TSDB_CODE_SUCCESS) {
6,232,414✔
438
      T_LONG_JMP(pOperator->pTaskInfo->env, code);
×
439
    }
440
  }
441
}
2,147,483,647✔
442

443
int32_t doOpenSortOperator(SOperatorInfo* pOperator) {
86,526,799✔
444
  SSortOperatorInfo* pInfo = pOperator->info;
86,526,799✔
445
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
86,526,810✔
446
  int32_t            code = TSDB_CODE_SUCCESS;
86,528,103✔
447
  int32_t            lino = 0;
86,528,103✔
448
  SSortSource* pSource =NULL;
86,528,103✔
449

450
  if (pOperator->exprSupp.pFilterInfo != NULL) {
86,528,103✔
451
    filterSetExecContext(pOperator->exprSupp.pFilterInfo, pTaskInfo, isTaskKilled);
1,220✔
452
  }
453

454
  if (OPTR_IS_OPENED(pOperator)) {
86,524,058✔
455
    return code;
51,039,872✔
456
  }
457

458
  //  pInfo->binfo.pRes is not equalled to the input datablock.
459
  pInfo->pSortHandle = NULL;
35,486,750✔
460
  code =
461
      tsortCreateSortHandle(pInfo->pSortInfo, SORT_SINGLESOURCE_SORT, -1, -1, NULL, pTaskInfo->id.str, pInfo->maxRows,
70,967,949✔
462
                            pInfo->maxTupleLength, tsPQSortMemThreshold * 1024 * 1024, &pInfo->pSortHandle);
35,478,950✔
463
  QUERY_CHECK_CODE(code, lino, _end);
35,473,343✔
464

465
  tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock, applyScalarFunction, pOperator);
35,473,343✔
466

467
  pSource = taosMemoryCalloc(1, sizeof(SSortSource));
35,472,844✔
468
  QUERY_CHECK_NULL(pSource, code, lino, _end, terrno);
35,474,466✔
469

470
  pSource->param = pOperator->pDownstream[0];
35,474,466✔
471
  pSource->onlyRef = true;
35,477,347✔
472

473
  code = tsortAddSource(pInfo->pSortHandle, pSource);
35,485,199✔
474
  QUERY_CHECK_CODE(code, lino, _end);
35,476,844✔
475
  pSource = NULL;
35,476,844✔
476

477
  code = tsortOpen(pInfo->pSortHandle);
35,476,844✔
478
  QUERY_CHECK_CODE(code, lino, _end);
35,367,094✔
479
  pOperator->status = OP_RES_TO_RETURN;
35,366,569✔
480
  OPTR_SET_OPENED(pOperator);
35,365,988✔
481

482
_end:
35,366,513✔
483
  if (pSource) {
35,366,513✔
484
    taosMemoryFree(pSource);
×
485
  }
486
  if (code != TSDB_CODE_SUCCESS) {
35,367,094✔
487
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
525✔
488
    pTaskInfo->code = code;
525✔
489
    T_LONG_JMP(pTaskInfo->env, code);
525✔
490
  }
491
  return code;
35,366,569✔
492
}
493

494
int32_t doSort(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
86,562,721✔
495
  QRY_PARAM_CHECK(pResBlock);
86,562,721✔
496
  int32_t code = TSDB_CODE_SUCCESS;
86,561,159✔
497
  int32_t lino = 0;
86,561,159✔
498
  if (pOperator->status == OP_EXEC_DONE) {
86,561,159✔
499
    return code;
35,769✔
500
  }
501

502
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
86,527,533✔
503
  SSortOperatorInfo* pInfo = pOperator->info;
86,527,522✔
504

505
  code = pOperator->fpSet._openFn(pOperator);
86,525,005✔
506
  QUERY_CHECK_CODE(code, lino, _end);
86,406,013✔
507

508
  // multi-group case not handle here
509
  SSDataBlock* pBlock = NULL;
86,406,013✔
510
  while (1) {
43,020✔
511
    if (tsortIsClosed(pInfo->pSortHandle)) {
86,449,614✔
512
      code = TSDB_CODE_TSC_QUERY_CANCELLED;
×
513
      QUERY_CHECK_CODE(code, lino, _end);
×
514
    }
515

516
    recordOpExecBeforeDownstream(pOperator);
86,450,754✔
517
    code = getSortedBlockData(pInfo->pSortHandle, pInfo->binfo.pRes, pOperator->resultInfo.capacity,
86,450,222✔
518
                                pInfo->matchInfo.pList, pOperator, pInfo, &pBlock);
519
    recordOpExecAfterDownstream(pOperator, pBlock ? pBlock->info.rows : 0);
86,450,334✔
520
    QUERY_CHECK_CODE(code, lino, _end);
86,448,946✔
521
    if (pBlock == NULL) {
86,448,946✔
522
      setOperatorCompleted(pOperator);
33,897,515✔
523
      return code;
33,896,810✔
524
    }
525

526
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, &pInfo->matchInfo, NULL);
52,551,431✔
527
    QUERY_CHECK_CODE(code, lino, _end);
52,552,669✔
528

529
    if (blockDataGetNumOfRows(pBlock) == 0) {
52,552,669✔
530
      continue;
×
531
    }
532

533
    // there are bugs?
534
    bool limitReached = applyLimitOffset(&pInfo->limitInfo, pBlock, pTaskInfo);
52,552,808✔
535
    if (limitReached) {
52,552,819✔
536
      resetLimitInfoForNextGroup(&pInfo->limitInfo);
3,162,005✔
537
    }
538

539
    if (pBlock->info.rows > 0) {
52,552,819✔
540
      break;
52,510,647✔
541
    }
542
  }
543

544
  *pResBlock = blockDataGetNumOfRows(pBlock) > 0 ? pBlock : NULL;
52,510,647✔
545
_end:
52,509,807✔
546
  if (code != TSDB_CODE_SUCCESS) {
52,509,274✔
547
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
37✔
548
    pTaskInfo->code = code;
37✔
549
    T_LONG_JMP(pTaskInfo->env, code);
×
550
  }
551
  return code;
52,510,077✔
552
}
553

554
void destroySortOperatorInfo(void* param) {
35,410,938✔
555
  SSortOperatorInfo* pInfo = (SSortOperatorInfo*)param;
35,410,938✔
556
  blockDataDestroy(pInfo->binfo.pRes);
35,410,938✔
557
  pInfo->binfo.pRes = NULL;
35,410,833✔
558

559
  tsortDestroySortHandle(pInfo->pSortHandle);
35,411,820✔
560
  taosArrayDestroy(pInfo->pSortInfo);
35,411,885✔
561
  taosArrayDestroy(pInfo->matchInfo.pList);
35,414,054✔
562
  destroySortOpGroupIdCalc(pInfo->pGroupIdCalc);
35,414,028✔
563
  taosMemoryFreeClear(param);
35,410,297✔
564
}
35,414,203✔
565

566
int32_t getExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
104,674✔
567
  SSortExecInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortExecInfo));
104,674✔
568
  if (pInfo == NULL) {
104,674✔
569
    return terrno;
×
570
  }
571

572
  SSortOperatorInfo* pOperatorInfo = (SSortOperatorInfo*)pOptr->info;
104,674✔
573

574
  *pInfo = tsortGetSortExecInfo(pOperatorInfo->pSortHandle);
104,674✔
575
  *pOptrExplain = pInfo;
104,674✔
576
  *len = sizeof(SSortExecInfo);
104,674✔
577
  return TSDB_CODE_SUCCESS;
104,674✔
578
}
579

580
static void calcSortOperMaxTupleLength(SSortOperatorInfo* pSortOperInfo, SNodeList* pSortKeys) {
35,408,165✔
581
  SColMatchInfo* pColItem = &pSortOperInfo->matchInfo;
35,408,165✔
582
  size_t         size = taosArrayGetSize(pColItem->pList);
35,414,123✔
583
  for (size_t i = 0; i < size; ++i) {
142,454,391✔
584
    SColMatchItem* pInfo = taosArrayGet(pColItem->pList, i);
107,050,609✔
585
    if (pInfo == NULL) {
107,038,196✔
586
      continue;
×
587
    }
588

589
    pSortOperInfo->maxTupleLength += pInfo->dataType.bytes;
107,038,196✔
590
  }
591

592
  size = LIST_LENGTH(pSortKeys);
35,403,782✔
593
  for (size_t i = 0; i < size; ++i) {
85,743,388✔
594
    SOrderByExprNode* pOrderExprNode = (SOrderByExprNode*)nodesListGetNode(pSortKeys, i);
50,335,159✔
595
    pSortOperInfo->maxTupleLength += ((SColumnNode*)pOrderExprNode->pExpr)->node.resType.bytes;
50,338,811✔
596
  }
597
}
35,408,229✔
598

599
static void destroySortOpGroupIdCalc(SSortOpGroupIdCalc* pCalc) {
35,411,298✔
600
  if (pCalc) {
35,411,298✔
601
    taosArrayDestroy(pCalc->pSortColsArr);
118,472✔
602
    taosMemoryFree(pCalc->keyBuf);
118,472✔
603
    taosMemoryFree(pCalc);
118,472✔
604
  }
605
}
35,411,298✔
606

607
//=====================================================================================
608
// Group Sort Operator
609
typedef enum EChildOperatorStatus { CHILD_OP_NEW_GROUP, CHILD_OP_SAME_GROUP, CHILD_OP_FINISHED } EChildOperatorStatus;
610

611
typedef struct SGroupSortOperatorInfo {
612
  SOptrBasicInfo       binfo;
613
  SArray*              pSortInfo;
614
  SColMatchInfo        matchInfo;
615
  int64_t              startTs;
616
  uint64_t             sortElapsed;
617
  bool                 hasGroupId;
618
  uint64_t             currGroupId;
619
  uint64_t             currBaseGId;
620
  SSDataBlock*         prefetchedSortInput;
621
  SSortHandle*         pCurrSortHandle;
622
  EChildOperatorStatus childOpStatus;
623
  SSortExecInfo        sortExecInfo;
624
} SGroupSortOperatorInfo;
625

626
int32_t getGroupSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, int32_t capacity, SArray* pColMatchInfo,
154,752✔
627
                                SGroupSortOperatorInfo* pInfo, SSDataBlock** pResBlock) {
628
  QRY_PARAM_CHECK(pResBlock);
154,752✔
629

630
  blockDataCleanup(pDataBlock);
154,752✔
631
  int32_t code = blockDataEnsureCapacity(pDataBlock, capacity);
154,752✔
632
  if (code) {
154,752✔
633
    return code;
×
634
  }
635

636
  SSDataBlock* p = NULL;
154,752✔
637
  code = tsortGetSortedDataBlock(pHandle, &p);
154,752✔
638
  if (p == NULL || (code != 0)) {
154,752✔
639
    return code;
×
640
  }
641

642
  code = blockDataEnsureCapacity(p, capacity);
154,752✔
643
  if (code) {
154,752✔
644
    return code;
×
645
  }
646

647
  while (1) {
8,191,800✔
648
    STupleHandle* pTupleHandle = NULL;
8,346,552✔
649
    code = tsortNextTuple(pHandle, &pTupleHandle);
8,346,552✔
650
    if (pTupleHandle == NULL || code != 0) {
8,346,552✔
651
      break;
652
    }
653

654
    code = appendOneRowToDataBlock(p, pTupleHandle);
8,191,800✔
655
    if (code) {
8,191,800✔
656
      break;
×
657
    }
658

659
    if (p->info.rows >= capacity) {
8,191,800✔
660
      break;
×
661
    }
662
  }
663

664
  if (p->info.rows > 0) {
154,752✔
665
    int32_t numOfCols = taosArrayGetSize(pColMatchInfo);
77,376✔
666
    for (int32_t i = 0; i < numOfCols; ++i) {
232,128✔
667
      SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, i);
154,752✔
668
      if (pmInfo == NULL) {
154,752✔
669
        return terrno;
×
670
      }
671

672
      SColumnInfoData* pSrc = taosArrayGet(p->pDataBlock, pmInfo->srcSlotId);
154,752✔
673
      if (pSrc == NULL) {
154,752✔
674
        return terrno;
×
675
      }
676

677
      SColumnInfoData* pDst = taosArrayGet(pDataBlock->pDataBlock, pmInfo->dstSlotId);
154,752✔
678
      if (pDst == NULL) {
154,752✔
679
        return terrno;
×
680
      }
681

682
      code = colDataAssign(pDst, pSrc, p->info.rows, &pDataBlock->info);
154,752✔
683
      if (code) {
154,752✔
684
        return code;
×
685
      }
686
    }
687

688
    pDataBlock->info.rows = p->info.rows;
77,376✔
689
    pDataBlock->info.capacity = p->info.rows;
77,376✔
690
    pDataBlock->info.scanFlag = p->info.scanFlag;
77,376✔
691
    // propagate ids for the current group
692
    pDataBlock->info.id.groupId = pInfo->currGroupId;
77,376✔
693
    pDataBlock->info.id.baseGId = pInfo->currBaseGId;
77,376✔
694
  }
695

696
  blockDataDestroy(p);
154,752✔
697
  *pResBlock = (pDataBlock->info.rows > 0) ? pDataBlock : NULL;
154,752✔
698
  return code;
154,752✔
699
}
700

701
typedef struct SGroupSortSourceParam {
702
  SOperatorInfo*          childOpInfo;
703
  SGroupSortOperatorInfo* grpSortOpInfo;
704
} SGroupSortSourceParam;
705

706
int32_t fetchNextGroupSortDataBlock(void* param, SSDataBlock** ppBlock) {
154,283✔
707
  int32_t                 code = 0;
154,283✔
708
  int32_t                 lino = 0;
154,283✔
709
  SGroupSortSourceParam*  source = param;
154,283✔
710
  SGroupSortOperatorInfo* grpSortOpInfo = source->grpSortOpInfo;
154,283✔
711
  SSDataBlock*            block = NULL;
154,752✔
712

713
  QRY_PARAM_CHECK(ppBlock);
154,752✔
714

715
  if (grpSortOpInfo->prefetchedSortInput) {
154,752✔
716
    block = grpSortOpInfo->prefetchedSortInput;
77,376✔
717
    grpSortOpInfo->prefetchedSortInput = NULL;
77,376✔
718
    *ppBlock = block;
77,376✔
719
  } else {
720
    SOperatorInfo* childOp = source->childOpInfo;
77,376✔
721
    code = childOp->fpSet.getNextFn(childOp, &block);
77,376✔
722
    QUERY_CHECK_CODE(code, lino, _end);
77,376✔
723

724
    if (block != NULL) {
77,376✔
725
      code = blockDataCheck(block);
16,680✔
726
      QUERY_CHECK_CODE(code, lino, _end);
16,680✔
727
      if (block->info.id.groupId == grpSortOpInfo->currGroupId) {
16,680✔
728
        grpSortOpInfo->childOpStatus = CHILD_OP_SAME_GROUP;
×
729
        *ppBlock = block;
×
730
      } else {
731
        grpSortOpInfo->childOpStatus = CHILD_OP_NEW_GROUP;
16,680✔
732
        grpSortOpInfo->prefetchedSortInput = block;
16,680✔
733
      }
734
    } else {
735
      grpSortOpInfo->childOpStatus = CHILD_OP_FINISHED;
60,696✔
736
    }
737
  }
738

739
  return code;
154,752✔
740
_end:
×
741
  if (code != 0) {
×
742
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
743
  }
744
  return code;
×
745
}
746

747
int32_t beginSortGroup(SOperatorInfo* pOperator) {
77,376✔
748
  SGroupSortOperatorInfo* pInfo = pOperator->info;
77,376✔
749
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
77,376✔
750

751
  //  pInfo->binfo.pRes is not equalled to the input datablock.
752
  pInfo->pCurrSortHandle = NULL;
77,376✔
753

754
  int32_t code = tsortCreateSortHandle(pInfo->pSortInfo, SORT_SINGLESOURCE_SORT, -1, -1, NULL, pTaskInfo->id.str, 0, 0,
77,376✔
755
                                       0, &pInfo->pCurrSortHandle);
756
  if (code) {
77,376✔
757
    return code;
×
758
  }
759

760
  tsortSetFetchRawDataFp(pInfo->pCurrSortHandle, fetchNextGroupSortDataBlock, applyScalarFunction, pOperator);
77,376✔
761

762
  SSortSource*           ps = taosMemoryCalloc(1, sizeof(SSortSource));
77,376✔
763
  SGroupSortSourceParam* param = taosMemoryCalloc(1, sizeof(SGroupSortSourceParam));
77,376✔
764
  if (ps == NULL || param == NULL) {
77,376✔
765
    taosMemoryFree(ps);
×
766
    taosMemoryFree(param);
×
767
    return terrno;
×
768
  }
769

770
  param->childOpInfo = pOperator->pDownstream[0];
77,376✔
771
  param->grpSortOpInfo = pInfo;
77,376✔
772

773
  ps->param = param;
77,376✔
774
  ps->onlyRef = false;
77,376✔
775
  code = tsortAddSource(pInfo->pCurrSortHandle, ps);
77,376✔
776
  if (code != 0) {
77,376✔
777
    return code;
×
778
  }
779

780
  code = tsortOpen(pInfo->pCurrSortHandle);
77,376✔
781
  return code;
77,376✔
782
}
783

784
int32_t finishSortGroup(SOperatorInfo* pOperator) {
77,376✔
785
  SGroupSortOperatorInfo* pInfo = pOperator->info;
77,376✔
786

787
  SSortExecInfo sortExecInfo = tsortGetSortExecInfo(pInfo->pCurrSortHandle);
77,376✔
788

789
  pInfo->sortExecInfo.sortMethod = sortExecInfo.sortMethod;
77,376✔
790
  pInfo->sortExecInfo.sortBuffer = sortExecInfo.sortBuffer;
77,376✔
791
  pInfo->sortExecInfo.loops += sortExecInfo.loops;
77,376✔
792
  pInfo->sortExecInfo.readBytes += sortExecInfo.readBytes;
77,376✔
793
  pInfo->sortExecInfo.writeBytes += sortExecInfo.writeBytes;
77,376✔
794

795
  tsortDestroySortHandle(pInfo->pCurrSortHandle);
77,376✔
796
  pInfo->pCurrSortHandle = NULL;
77,376✔
797

798
  return TSDB_CODE_SUCCESS;
77,376✔
799
}
800

801
int32_t doGroupSort(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
160,254✔
802
  QRY_PARAM_CHECK(pResBlock);
160,254✔
803
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
160,254✔
804
  SGroupSortOperatorInfo* pInfo = pOperator->info;
160,254✔
805
  int32_t                 code = TSDB_CODE_SUCCESS;
159,848✔
806
  int32_t                 lino = 0;
159,848✔
807

808
  if (pOperator->status == OP_EXEC_DONE) {
159,848✔
809
    return code;
×
810
  }
811

812
  code = pOperator->fpSet._openFn(pOperator);
160,254✔
813
  QUERY_CHECK_CODE(code, lino, _end);
159,848✔
814

815
  if (!pInfo->hasGroupId) {
159,848✔
816
    pInfo->hasGroupId = true;
82,878✔
817

818
    pInfo->prefetchedSortInput = getNextBlockFromDownstream(pOperator, 0);
82,878✔
819
    if (pInfo->prefetchedSortInput == NULL) {
82,878✔
820
      setOperatorCompleted(pOperator);
22,182✔
821
      return code;
22,182✔
822
    }
823

824
    pInfo->currGroupId = pInfo->prefetchedSortInput->info.id.groupId;
60,696✔
825
    pInfo->currBaseGId = pInfo->prefetchedSortInput->info.id.baseGId;
60,696✔
826
    pInfo->childOpStatus = CHILD_OP_NEW_GROUP;
60,696✔
827
    code = beginSortGroup(pOperator);
60,696✔
828
    QUERY_CHECK_CODE(code, lino, _end);
60,696✔
829
  }
830

831
  SSDataBlock* pBlock = NULL;
137,666✔
832
  while (pInfo->pCurrSortHandle != NULL) {
154,752✔
833
    if (tsortIsClosed(pInfo->pCurrSortHandle)) {
154,752✔
834
      code = TSDB_CODE_TSC_QUERY_CANCELLED;
×
835
      QUERY_CHECK_CODE(code, lino, _end);
×
836
    }
837

838
    // beginSortGroup would fetch all child blocks of pInfo->currGroupId;
839
    if (pInfo->childOpStatus == CHILD_OP_SAME_GROUP) {
154,752✔
840
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
841
      QUERY_CHECK_CODE(code, lino, _end);
×
842
    }
843

844
    recordOpExecBeforeDownstream(pOperator);
154,752✔
845
    code = getGroupSortedBlockData(pInfo->pCurrSortHandle, pInfo->binfo.pRes, pOperator->resultInfo.capacity,
154,752✔
846
                                     pInfo->matchInfo.pList, pInfo, &pBlock);
847
    recordOpExecAfterDownstream(pOperator, pBlock ? pBlock->info.rows : 0);
154,752✔
848
    QUERY_CHECK_CODE(code, lino, _end);
154,752✔
849
    if (pBlock != NULL) {
154,752✔
850
      // keep both ids aligned with current group
851
      pBlock->info.id.groupId = pInfo->currGroupId;
77,376✔
852
      pBlock->info.id.baseGId = pInfo->currBaseGId;
77,376✔
853
      // baseGId follows upstream; if upstream is empty here, preserve current
854
      // (no-op if not set). We cannot reconstruct baseGId here; rely on upstream propagation.
855
      pOperator->resultInfo.totalRows += pBlock->info.rows;
77,376✔
856
      *pResBlock = pBlock;
77,376✔
857
      return code;
77,376✔
858
    } else {
859
      if (pInfo->childOpStatus == CHILD_OP_NEW_GROUP) {
77,376✔
860
        (void) finishSortGroup(pOperator);
16,680✔
861
        pInfo->currGroupId = pInfo->prefetchedSortInput->info.id.groupId;
16,680✔
862
        pInfo->currBaseGId = pInfo->prefetchedSortInput->info.id.baseGId;
16,680✔
863
        code = beginSortGroup(pOperator);
16,680✔
864
        QUERY_CHECK_CODE(code, lino, _end);
16,680✔
865
      } else if (pInfo->childOpStatus == CHILD_OP_FINISHED) {
60,696✔
866
        (void) finishSortGroup(pOperator);
60,696✔
867
        setOperatorCompleted(pOperator);
60,696✔
868
        return code;
60,696✔
869
      }
870
    }
871
  }
872

873
_end:
×
874
  if (code != TSDB_CODE_SUCCESS) {
×
875
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
876
    pTaskInfo->code = code;
×
877
    T_LONG_JMP(pTaskInfo->env, code);
×
878
  }
879
  return code;
×
880
}
881

882
int32_t getGroupSortExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
×
883
  SGroupSortOperatorInfo* pInfo = (SGroupSortOperatorInfo*)pOptr->info;
×
884
  *pOptrExplain = &pInfo->sortExecInfo;
×
885
  *len = sizeof(SSortExecInfo);
×
886
  return TSDB_CODE_SUCCESS;
×
887
}
888

889
void destroyGroupSortOperatorInfo(void* param) {
82,878✔
890
  SGroupSortOperatorInfo* pInfo = (SGroupSortOperatorInfo*)param;
82,878✔
891
  blockDataDestroy(pInfo->binfo.pRes);
82,878✔
892
  pInfo->binfo.pRes = NULL;
82,878✔
893

894
  taosArrayDestroy(pInfo->pSortInfo);
82,878✔
895
  taosArrayDestroy(pInfo->matchInfo.pList);
82,878✔
896

897
  tsortDestroySortHandle(pInfo->pCurrSortHandle);
82,878✔
898
  pInfo->pCurrSortHandle = NULL;
82,878✔
899

900
  taosMemoryFreeClear(param);
82,878✔
901
}
82,878✔
902

903
static int32_t resetGroupSortOperState(SOperatorInfo* pOper) {
×
904
  SGroupSortOperatorInfo* pInfo = pOper->info;
×
905
  SExecTaskInfo*           pTaskInfo = pOper->pTaskInfo;
×
906
  pOper->status = OP_NOT_OPENED;
×
907

908
  pInfo->currGroupId = 0;
×
909
  pInfo->hasGroupId = false;
×
910
  pInfo->prefetchedSortInput = NULL;
×
911
  pInfo->childOpStatus = CHILD_OP_NEW_GROUP;
×
912
  pInfo->sortExecInfo = (SSortExecInfo){0};
×
913
  
914
  resetBasicOperatorState(&pInfo->binfo);
×
915
  destroySqlFunctionCtx(pOper->exprSupp.pCtx, pOper->exprSupp.pExprInfo, pOper->exprSupp.numOfExprs);
×
916
  taosMemoryFreeClear(pOper->exprSupp.rowEntryInfoOffset);
×
917
  pOper->exprSupp.pCtx =
×
918
      createSqlFunctionCtx(pOper->exprSupp.pExprInfo, pOper->exprSupp.numOfExprs, &pOper->exprSupp.rowEntryInfoOffset, &pTaskInfo->storageAPI.functionStore);
×
919

920
  tsortDestroySortHandle(pInfo->pCurrSortHandle);
×
921
  pInfo->pCurrSortHandle = NULL;
×
922

923
  return 0;
×
924
}
925

926
int32_t createGroupSortOperatorInfo(SOperatorInfo* downstream, SGroupSortPhysiNode* pSortPhyNode,
82,878✔
927
                                    SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
928
  QRY_PARAM_CHECK(pOptrInfo);
82,878✔
929
  int32_t code = 0;
82,878✔
930
  int32_t lino = 0;
82,878✔
931

932
  SGroupSortOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupSortOperatorInfo));
82,878✔
933
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
82,878✔
934
  if (pInfo == NULL || pOperator == NULL) {
82,878✔
935
    code = terrno;
×
936
    goto _error;
×
937
  }
938
  initOperatorCostInfo(pOperator);
82,878✔
939

940
  SExprSupp*          pSup = &pOperator->exprSupp;
82,878✔
941
  SDataBlockDescNode* pDescNode = pSortPhyNode->node.pOutputDataBlockDesc;
82,878✔
942

943
  int32_t    numOfCols = 0;
82,878✔
944
  SExprInfo* pExprInfo = NULL;
82,878✔
945
  code = createExprInfo(pSortPhyNode->pExprs, NULL, &pExprInfo, &numOfCols);
82,878✔
946
  QUERY_CHECK_CODE(code, lino, _error);
82,878✔
947

948
  pSup->pExprInfo = pExprInfo;
82,878✔
949
  pSup->numOfExprs = numOfCols;
82,878✔
950

951
  initResultSizeInfo(&pOperator->resultInfo, 1024);
82,878✔
952
  pOperator->exprSupp.pCtx = createSqlFunctionCtx(pExprInfo, numOfCols, &pOperator->exprSupp.rowEntryInfoOffset,
82,878✔
953
                                                  &pTaskInfo->storageAPI.functionStore);
954
  QUERY_CHECK_NULL(pOperator->exprSupp.pCtx, code, lino, _error, terrno);
82,878✔
955

956
  pInfo->binfo.pRes = createDataBlockFromDescNode(pDescNode);
82,472✔
957
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
82,878✔
958

959
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
82,878✔
960
  TSDB_CHECK_CODE(code, lino, _error);
82,878✔
961

962
  pInfo->binfo.inputTsOrder = pSortPhyNode->node.inputTsOrder;
82,878✔
963
  pInfo->binfo.outputTsOrder = pSortPhyNode->node.outputTsOrder;
82,878✔
964

965
  int32_t numOfOutputCols = 0;
82,472✔
966
  code = extractColMatchInfo(pSortPhyNode->pTargets, pDescNode, &numOfOutputCols, COL_MATCH_FROM_SLOT_ID,
82,878✔
967
                             &pInfo->matchInfo);
968
  TSDB_CHECK_CODE(code, lino, _error);
82,878✔
969

970
  pInfo->pSortInfo = createSortInfo(pSortPhyNode->pSortKeys);
82,878✔
971
  setOperatorInfo(pOperator, "GroupSortOperator", QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT, false, OP_NOT_OPENED, pInfo,
82,878✔
972
                  pTaskInfo);
973
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, doGroupSort, NULL, destroyGroupSortOperatorInfo,
82,878✔
974
                                         optrDefaultBufFn, getGroupSortExplainExecInfo, optrDefaultGetNextExtFn, NULL);
975

976
  setOperatorResetStateFn(pOperator, resetGroupSortOperState);
82,878✔
977
                                         
978
  code = appendDownstream(pOperator, &downstream, 1);
82,878✔
979
  if (code != TSDB_CODE_SUCCESS) {
82,878✔
980
    goto _error;
×
981
  }
982

983
  *pOptrInfo = pOperator;
82,878✔
984
  return TSDB_CODE_SUCCESS;
82,878✔
985

986
_error:
×
987
  pTaskInfo->code = code;
×
988
  if (pInfo != NULL) {
×
989
    destroyGroupSortOperatorInfo(pInfo);
×
990
  }
991
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
992
  return code;
×
993
}
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