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

taosdata / TDengine / #5057

17 May 2026 01:15AM UTC coverage: 73.406% (+0.02%) from 73.384%
#5057

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281727 of 383795 relevant lines covered (73.41%)

136101761.85 hits per line

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

82.7
/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) {
36,717,119✔
58
  if (pOperator == NULL || pOperator->exprSupp.pExprInfo == NULL || slotId < 0) {
36,717,119✔
59
    return false;
26,967,589✔
60
  }
61
  for (int32_t idx = 0; idx < pOperator->exprSupp.numOfExprs; ++idx) {
15,047,290✔
62
    if (pOperator->exprSupp.pExprInfo[idx].base.resSchema.slotId == slotId) {
9,771,572✔
63
      return true;
4,473,812✔
64
    }
65
  }
66
  return false;
5,275,718✔
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,473,812✔
74
  if (pBlock == NULL || pBlock->pDataBlock == NULL) {
4,473,812✔
75
    return -1;
×
76
  }
77
  int32_t colCount = (int32_t)taosArrayGetSize(pBlock->pDataBlock);
4,473,812✔
78
  for (int32_t idx = 0; idx < colCount; ++idx) {
4,473,812✔
79
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, idx);
4,473,812✔
80
    if (pCol != NULL && pCol->info.type == TSDB_DATA_TYPE_TIMESTAMP &&
4,473,812✔
81
        !sortIsExprResultSlot(pOperator, idx)) {
4,473,812✔
82
      return idx;
4,473,812✔
83
    }
84
  }
85
  return -1;
×
86
}
87

88
static void destroySortOpGroupIdCalc(SSortOpGroupIdCalc* pCalc);
89

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

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

101
  tsortDestroySortHandle(pInfo->pSortHandle);
383,911✔
102
  pInfo->pSortHandle = NULL;
383,911✔
103

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

109
  return 0;
383,911✔
110
}
111

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

116
  int32_t code = 0;
37,056,028✔
117
  int32_t lino = 0;
37,056,028✔
118

119
  SSortOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortOperatorInfo));
37,056,028✔
120
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
37,022,733✔
121
  if (pInfo == NULL || pOperator == NULL) {
37,036,734✔
122
    code = terrno;
×
123
    goto _error;
×
124
  }
125
  initOperatorCostInfo(pOperator);
37,036,915✔
126

127
  pOperator->pTaskInfo = pTaskInfo;
37,059,788✔
128
  SDataBlockDescNode* pDescNode = pSortNode->node.pOutputDataBlockDesc;
37,059,054✔
129

130
  int32_t numOfCols = 0;
37,053,637✔
131
  code = createExprInfo(pSortNode->pExprs, NULL, &pOperator->exprSupp.pExprInfo, &numOfCols);
37,060,087✔
132
  QUERY_CHECK_CODE(code, lino, _error);
37,039,205✔
133

134
  pOperator->exprSupp.numOfExprs = numOfCols;
37,039,205✔
135
  int32_t numOfOutputCols = 0;
37,028,606✔
136
  code =
137
      extractColMatchInfo(pSortNode->pTargets, pDescNode, &numOfOutputCols, COL_MATCH_FROM_SLOT_ID, &pInfo->matchInfo);
37,043,597✔
138
  if (code != TSDB_CODE_SUCCESS) {
37,051,799✔
139
    goto _error;
×
140
  }
141
  
142
  calcSortOperMaxTupleLength(pInfo, pSortNode->pSortKeys);
37,051,799✔
143
  pInfo->maxRows = -1;
37,047,557✔
144
  if (pSortNode->node.pLimit && ((SLimitNode*)pSortNode->node.pLimit)->limit) {
37,047,577✔
145
    SLimitNode* pLimit = (SLimitNode*)pSortNode->node.pLimit;
5,414,629✔
146
    if (pLimit->limit->datum.i > 0) {
5,416,094✔
147
      pInfo->maxRows = pLimit->limit->datum.i + (pLimit->offset ? pLimit->offset->datum.i : 0);
5,407,910✔
148
    }
149
  }
150

151
  pOperator->exprSupp.pCtx =
37,043,321✔
152
      createSqlFunctionCtx(pOperator->exprSupp.pExprInfo, numOfCols, &pOperator->exprSupp.rowEntryInfoOffset, &pTaskInfo->storageAPI.functionStore);
37,044,340✔
153
  QUERY_CHECK_NULL(pOperator->exprSupp.pCtx, code, lino, _error, terrno);
37,040,586✔
154
  initResultSizeInfo(&pOperator->resultInfo, 1024);
37,034,493✔
155
  code = filterInitFromNode((SNode*)pSortNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
37,043,545✔
156
                            pTaskInfo->pStreamRuntimeInfo);
37,045,378✔
157
  if (code != TSDB_CODE_SUCCESS) {
37,036,035✔
158
    goto _error;
×
159
  }
160

161
  pInfo->binfo.pRes = createDataBlockFromDescNode(pDescNode);
37,036,035✔
162
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
37,061,699✔
163

164
  pInfo->pSortInfo = createSortInfo(pSortNode->pSortKeys);
37,053,254✔
165
  TSDB_CHECK_NULL(pInfo->pSortInfo, code, lino, _error, terrno);
37,048,055✔
166

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

197
  pInfo->binfo.inputTsOrder = pSortNode->node.inputTsOrder;
37,052,219✔
198
  pInfo->binfo.outputTsOrder = pSortNode->node.outputTsOrder;
37,042,809✔
199
  initLimitInfo(pSortNode->node.pLimit, pSortNode->node.pSlimit, &pInfo->limitInfo);
37,043,926✔
200

201
  setOperatorInfo(pOperator, "SortOperator", QUERY_NODE_PHYSICAL_PLAN_SORT, true, OP_NOT_OPENED, pInfo, pTaskInfo);
37,050,670✔
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);
37,053,779✔
211

212
  setOperatorResetStateFn(pOperator, resetSortOperState);
37,047,610✔
213
  code = appendDownstream(pOperator, &downstream, 1);
37,047,560✔
214
  if (code != TSDB_CODE_SUCCESS) {
37,029,365✔
215
    goto _error;
×
216
  }
217

218
  *pOptrInfo = pOperator;
37,029,365✔
219
  return TSDB_CODE_SUCCESS;
37,032,008✔
220

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,808,112✔
287
      pInfo->pGroupIdCalc->pSavedTuple = NULL;
1,808,112✔
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,744,678✔
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,808,654✔
299
        retTuple = NULL;
1,808,654✔
300
      } else {
301
        // new group with empty block
302
        pInfo->pGroupIdCalc->lastGroupId = pBlock->info.id.groupId =
1,936,024✔
303
            calcGroupId(pInfo->pGroupIdCalc->keyBuf, pInfo->pGroupIdCalc->lastKeysLen);
1,936,024✔
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,105,680✔
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,
91,642,899✔
320
                                  const SOperatorInfo* pOperator, SSortOperatorInfo* pInfo, SSDataBlock** pResBlock) {
321
  QRY_PARAM_CHECK(pResBlock);
91,642,899✔
322
  blockDataCleanup(pDataBlock);
91,642,881✔
323

324
  int32_t       lino = 0;
91,644,673✔
325
  int32_t       code = 0;
91,644,673✔
326
  STupleHandle* pTupleHandle = NULL;
91,644,673✔
327
  SSDataBlock*  p = NULL;
91,644,237✔
328

329
  code = tsortGetSortedDataBlock(pHandle, &p);
91,644,673✔
330
  if (p == NULL || (code != 0)) {
91,644,839✔
331
    return code;
7,029,453✔
332
  }
333

334
  code = blockDataEnsureCapacity(p, capacity);
84,615,386✔
335
  QUERY_CHECK_CODE(code, lino, _error);
84,615,984✔
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;
60,153,882✔
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;
24,462,538✔
354
    }
355
  }
356

357
  QUERY_CHECK_CODE(code, lino, _error);
84,616,420✔
358

359
  if (p->info.rows > 0) {
84,616,420✔
360
    code = blockDataEnsureCapacity(pDataBlock, capacity);
56,218,297✔
361
    QUERY_CHECK_CODE(code, lino, _error);
56,220,085✔
362

363
    // todo extract function to handle this
364
    int32_t numOfCols = taosArrayGetSize(pColMatchInfo);
56,220,085✔
365
    for (int32_t i = 0; i < numOfCols; ++i) {
314,254,518✔
366
      SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, i);
258,036,204✔
367
      QUERY_CHECK_NULL(pmInfo, code, lino, _error, terrno);
258,037,395✔
368

369
      int32_t srcSlotId = pmInfo->srcSlotId;
258,037,395✔
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 &&
258,036,203✔
378
          pmInfo->dataType.type == TSDB_DATA_TYPE_TIMESTAMP &&
64,616,776✔
379
          sortIsExprResultSlot(pOperator, srcSlotId)) {
32,243,307✔
380
        int32_t origSlot = sortFindOrigTsSlot(pOperator, p);
4,473,812✔
381
        if (origSlot >= 0 && origSlot != srcSlotId) {
4,473,812✔
382
          srcSlotId = origSlot;
4,473,812✔
383
        }
384
      }
385

386
      SColumnInfoData* pSrc = taosArrayGet(p->pDataBlock, srcSlotId);
258,037,394✔
387
      QUERY_CHECK_NULL(pSrc, code, lino, _error, terrno);
258,035,604✔
388

389
      SColumnInfoData* pDst = taosArrayGet(pDataBlock->pDataBlock, pmInfo->dstSlotId);
258,035,604✔
390
      QUERY_CHECK_NULL(pDst, code, lino, _error, terrno);
258,036,793✔
391

392
      code = colDataAssign(pDst, pSrc, p->info.rows, &pDataBlock->info);
258,036,793✔
393
      QUERY_CHECK_CODE(code, lino, _error);
258,034,433✔
394
    }
395

396
    pDataBlock->info.dataLoad = 1;
56,218,314✔
397
    pDataBlock->info.rows = p->info.rows;
56,217,701✔
398
    pDataBlock->info.scanFlag = p->info.scanFlag;
56,217,105✔
399
    // propagate both C-group id and baseGId from upstream
400
    pDataBlock->info.id.groupId = p->info.id.groupId;
56,215,913✔
401
    pDataBlock->info.id.baseGId = p->info.id.baseGId;
56,215,346✔
402
  }
403

404
  blockDataDestroy(p);
84,613,440✔
405
  *pResBlock = (pDataBlock->info.rows > 0) ? pDataBlock : NULL;
84,609,929✔
406
  return code;
84,609,304✔
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,
13,136,806✔
435
                                         pOperator->exprSupp.numOfExprs, NULL,
436
                                         GET_STM_RTINFO(pOperator->pTaskInfo), pOperator->pTaskInfo);
6,568,403✔
437
    if (code != TSDB_CODE_SUCCESS) {
6,568,403✔
438
      T_LONG_JMP(pOperator->pTaskInfo->env, code);
×
439
    }
440
  }
441
}
2,147,483,647✔
442

443
int32_t doOpenSortOperator(SOperatorInfo* pOperator) {
91,727,187✔
444
  SSortOperatorInfo* pInfo = pOperator->info;
91,727,187✔
445
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
91,731,180✔
446
  int32_t            code = TSDB_CODE_SUCCESS;
91,723,119✔
447
  int32_t            lino = 0;
91,723,119✔
448
  SSortSource* pSource =NULL;
91,723,119✔
449

450
  if (pOperator->exprSupp.pFilterInfo != NULL) {
91,723,119✔
451
    filterSetExecContext(pOperator->exprSupp.pFilterInfo, pTaskInfo, isTaskKilled);
1,274✔
452
  }
453

454
  if (OPTR_IS_OPENED(pOperator)) {
91,723,697✔
455
    return code;
54,582,162✔
456
  }
457

458
  //  pInfo->binfo.pRes is not equalled to the input datablock.
459
  pInfo->pSortHandle = NULL;
37,127,438✔
460
  code =
461
      tsortCreateSortHandle(pInfo->pSortInfo, SORT_SINGLESOURCE_SORT, -1, -1, NULL, pTaskInfo->id.str, pInfo->maxRows,
74,271,194✔
462
                            pInfo->maxTupleLength, tsPQSortMemThreshold * 1024 * 1024, &pInfo->pSortHandle);
37,140,809✔
463
  QUERY_CHECK_CODE(code, lino, _end);
37,121,988✔
464

465
  tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock, applyScalarFunction, pOperator);
37,121,988✔
466

467
  pSource = taosMemoryCalloc(1, sizeof(SSortSource));
37,109,522✔
468
  QUERY_CHECK_NULL(pSource, code, lino, _end, terrno);
37,118,528✔
469

470
  pSource->param = pOperator->pDownstream[0];
37,118,528✔
471
  pSource->onlyRef = true;
37,130,869✔
472

473
  code = tsortAddSource(pInfo->pSortHandle, pSource);
37,133,296✔
474
  QUERY_CHECK_CODE(code, lino, _end);
37,133,801✔
475
  pSource = NULL;
37,133,801✔
476

477
  code = tsortOpen(pInfo->pSortHandle);
37,133,801✔
478
  QUERY_CHECK_CODE(code, lino, _end);
37,014,288✔
479
  pOperator->status = OP_RES_TO_RETURN;
37,013,733✔
480
  OPTR_SET_OPENED(pOperator);
37,013,733✔
481

482
_end:
37,014,288✔
483
  if (pSource) {
37,014,288✔
484
    taosMemoryFree(pSource);
×
485
  }
486
  if (code != TSDB_CODE_SUCCESS) {
37,014,288✔
487
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
555✔
488
    pTaskInfo->code = code;
555✔
489
    T_LONG_JMP(pTaskInfo->env, code);
555✔
490
  }
491
  return code;
37,013,733✔
492
}
493

494
int32_t doSort(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
91,765,541✔
495
  QRY_PARAM_CHECK(pResBlock);
91,765,541✔
496
  int32_t code = TSDB_CODE_SUCCESS;
91,759,453✔
497
  int32_t lino = 0;
91,759,453✔
498
  if (pOperator->status == OP_EXEC_DONE) {
91,759,453✔
499
    return code;
37,776✔
500
  }
501

502
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
91,710,528✔
503
  SSortOperatorInfo* pInfo = pOperator->info;
91,714,790✔
504

505
  code = pOperator->fpSet._openFn(pOperator);
91,728,553✔
506
  QUERY_CHECK_CODE(code, lino, _end);
91,598,417✔
507

508
  // multi-group case not handle here
509
  SSDataBlock* pBlock = NULL;
91,598,417✔
510
  while (1) {
43,872✔
511
    if (tsortIsClosed(pInfo->pSortHandle)) {
91,642,289✔
512
      code = TSDB_CODE_TSC_QUERY_CANCELLED;
×
513
      QUERY_CHECK_CODE(code, lino, _end);
×
514
    }
515

516
    recordOpExecBeforeDownstream(pOperator);
91,643,771✔
517
    code = getSortedBlockData(pInfo->pSortHandle, pInfo->binfo.pRes, pOperator->resultInfo.capacity,
91,643,157✔
518
                                pInfo->matchInfo.pList, pOperator, pInfo, &pBlock);
519
    recordOpExecAfterDownstream(pOperator, pBlock ? pBlock->info.rows : 0);
91,638,786✔
520
    QUERY_CHECK_CODE(code, lino, _end);
91,641,725✔
521
    if (pBlock == NULL) {
91,641,725✔
522
      setOperatorCompleted(pOperator);
35,425,788✔
523
      return code;
35,425,788✔
524
    }
525

526
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, &pInfo->matchInfo, NULL);
56,215,937✔
527
    QUERY_CHECK_CODE(code, lino, _end);
56,215,383✔
528

529
    if (blockDataGetNumOfRows(pBlock) == 0) {
56,215,383✔
530
      continue;
×
531
    }
532

533
    // there are bugs?
534
    bool limitReached = applyLimitOffset(&pInfo->limitInfo, pBlock, pTaskInfo);
56,216,574✔
535
    if (limitReached) {
56,215,345✔
536
      resetLimitInfoForNextGroup(&pInfo->limitInfo);
3,364,927✔
537
    }
538

539
    if (pBlock->info.rows > 0) {
56,215,345✔
540
      break;
56,172,673✔
541
    }
542
  }
543

544
  *pResBlock = blockDataGetNumOfRows(pBlock) > 0 ? pBlock : NULL;
56,172,673✔
545
_end:
56,172,077✔
546
  if (code != TSDB_CODE_SUCCESS) {
56,167,360✔
547
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
548
    pTaskInfo->code = code;
×
549
    T_LONG_JMP(pTaskInfo->env, code);
×
550
  }
551
  return code;
56,170,914✔
552
}
553

554
void destroySortOperatorInfo(void* param) {
37,058,509✔
555
  SSortOperatorInfo* pInfo = (SSortOperatorInfo*)param;
37,058,509✔
556
  blockDataDestroy(pInfo->binfo.pRes);
37,058,509✔
557
  pInfo->binfo.pRes = NULL;
37,057,743✔
558

559
  tsortDestroySortHandle(pInfo->pSortHandle);
37,056,413✔
560
  taosArrayDestroy(pInfo->pSortInfo);
37,059,412✔
561
  taosArrayDestroy(pInfo->matchInfo.pList);
37,056,963✔
562
  destroySortOpGroupIdCalc(pInfo->pGroupIdCalc);
37,059,734✔
563
  taosMemoryFreeClear(param);
37,057,241✔
564
}
37,060,801✔
565

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

572
  SSortOperatorInfo* pOperatorInfo = (SSortOperatorInfo*)pOptr->info;
111,540✔
573

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

580
static void calcSortOperMaxTupleLength(SSortOperatorInfo* pSortOperInfo, SNodeList* pSortKeys) {
37,053,284✔
581
  SColMatchInfo* pColItem = &pSortOperInfo->matchInfo;
37,053,284✔
582
  size_t         size = taosArrayGetSize(pColItem->pList);
37,058,173✔
583
  for (size_t i = 0; i < size; ++i) {
148,706,870✔
584
    SColMatchItem* pInfo = taosArrayGet(pColItem->pList, i);
111,667,581✔
585
    if (pInfo == NULL) {
111,664,160✔
586
      continue;
×
587
    }
588

589
    pSortOperInfo->maxTupleLength += pInfo->dataType.bytes;
111,664,160✔
590
  }
591

592
  size = LIST_LENGTH(pSortKeys);
37,039,289✔
593
  for (size_t i = 0; i < size; ++i) {
89,538,435✔
594
    SOrderByExprNode* pOrderExprNode = (SOrderByExprNode*)nodesListGetNode(pSortKeys, i);
52,489,622✔
595
    pSortOperInfo->maxTupleLength += ((SColumnNode*)pOrderExprNode->pExpr)->node.resType.bytes;
52,482,969✔
596
  }
597
}
37,048,813✔
598

599
static void destroySortOpGroupIdCalc(SSortOpGroupIdCalc* pCalc) {
37,060,771✔
600
  if (pCalc) {
37,060,771✔
601
    taosArrayDestroy(pCalc->pSortColsArr);
127,912✔
602
    taosMemoryFree(pCalc->keyBuf);
127,912✔
603
    taosMemoryFree(pCalc);
127,912✔
604
  }
605
}
37,060,771✔
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,
165,468✔
627
                                SGroupSortOperatorInfo* pInfo, SSDataBlock** pResBlock) {
628
  QRY_PARAM_CHECK(pResBlock);
165,468✔
629

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

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

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

647
  while (1) {
8,762,032✔
648
    STupleHandle* pTupleHandle = NULL;
8,927,500✔
649
    code = tsortNextTuple(pHandle, &pTupleHandle);
8,926,933✔
650
    if (pTupleHandle == NULL || code != 0) {
8,929,201✔
651
      break;
652
    }
653

654
    code = appendOneRowToDataBlock(p, pTupleHandle);
8,763,733✔
655
    if (code) {
8,760,331✔
656
      break;
×
657
    }
658

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

664
  if (p->info.rows > 0) {
164,901✔
665
    int32_t numOfCols = taosArrayGetSize(pColMatchInfo);
82,734✔
666
    for (int32_t i = 0; i < numOfCols; ++i) {
248,202✔
667
      SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, i);
165,468✔
668
      if (pmInfo == NULL) {
165,468✔
669
        return terrno;
×
670
      }
671

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

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

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

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

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

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

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

713
  QRY_PARAM_CHECK(ppBlock);
164,901✔
714

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

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

739
  return code;
165,468✔
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) {
82,734✔
748
  SGroupSortOperatorInfo* pInfo = pOperator->info;
82,734✔
749
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;
82,734✔
750

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

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

760
  tsortSetFetchRawDataFp(pInfo->pCurrSortHandle, fetchNextGroupSortDataBlock, applyScalarFunction, pOperator);
82,734✔
761

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

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

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

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

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

787
  SSortExecInfo sortExecInfo = tsortGetSortExecInfo(pInfo->pCurrSortHandle);
82,734✔
788

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

795
  tsortDestroySortHandle(pInfo->pCurrSortHandle);
82,734✔
796
  pInfo->pCurrSortHandle = NULL;
82,734✔
797

798
  return TSDB_CODE_SUCCESS;
82,734✔
799
}
800

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

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

812
  code = pOperator->fpSet._openFn(pOperator);
169,916✔
813
  QUERY_CHECK_CODE(code, lino, _end);
170,483✔
814

815
  if (!pInfo->hasGroupId) {
170,483✔
816
    pInfo->hasGroupId = true;
87,749✔
817

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

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

831
  SSDataBlock* pBlock = NULL;
147,948✔
832
  while (pInfo->pCurrSortHandle != NULL) {
165,468✔
833
    if (tsortIsClosed(pInfo->pCurrSortHandle)) {
165,468✔
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) {
165,468✔
840
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
841
      QUERY_CHECK_CODE(code, lino, _end);
×
842
    }
843

844
    recordOpExecBeforeDownstream(pOperator);
165,468✔
845
    code = getGroupSortedBlockData(pInfo->pCurrSortHandle, pInfo->binfo.pRes, pOperator->resultInfo.capacity,
165,468✔
846
                                     pInfo->matchInfo.pList, pInfo, &pBlock);
847
    recordOpExecAfterDownstream(pOperator, pBlock ? pBlock->info.rows : 0);
165,468✔
848
    QUERY_CHECK_CODE(code, lino, _end);
165,468✔
849
    if (pBlock != NULL) {
165,468✔
850
      // keep both ids aligned with current group
851
      pBlock->info.id.groupId = pInfo->currGroupId;
82,734✔
852
      pBlock->info.id.baseGId = pInfo->currBaseGId;
82,734✔
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;
82,734✔
856
      *pResBlock = pBlock;
82,734✔
857
      return code;
82,734✔
858
    } else {
859
      if (pInfo->childOpStatus == CHILD_OP_NEW_GROUP) {
82,734✔
860
        (void) finishSortGroup(pOperator);
17,520✔
861
        pInfo->currGroupId = pInfo->prefetchedSortInput->info.id.groupId;
17,520✔
862
        pInfo->currBaseGId = pInfo->prefetchedSortInput->info.id.baseGId;
17,520✔
863
        code = beginSortGroup(pOperator);
17,520✔
864
        QUERY_CHECK_CODE(code, lino, _end);
17,520✔
865
      } else if (pInfo->childOpStatus == CHILD_OP_FINISHED) {
65,214✔
866
        (void) finishSortGroup(pOperator);
65,214✔
867
        setOperatorCompleted(pOperator);
65,214✔
868
        return code;
65,214✔
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) {
87,749✔
890
  SGroupSortOperatorInfo* pInfo = (SGroupSortOperatorInfo*)param;
87,749✔
891
  blockDataDestroy(pInfo->binfo.pRes);
87,749✔
892
  pInfo->binfo.pRes = NULL;
87,749✔
893

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

897
  tsortDestroySortHandle(pInfo->pCurrSortHandle);
87,749✔
898
  pInfo->pCurrSortHandle = NULL;
87,749✔
899

900
  taosMemoryFreeClear(param);
87,749✔
901
}
87,749✔
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,
87,749✔
927
                                    SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
928
  QRY_PARAM_CHECK(pOptrInfo);
87,749✔
929
  int32_t code = 0;
87,749✔
930
  int32_t lino = 0;
87,749✔
931

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

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

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

948
  pSup->pExprInfo = pExprInfo;
87,749✔
949
  pSup->numOfExprs = numOfCols;
87,749✔
950

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

956
  pInfo->binfo.pRes = createDataBlockFromDescNode(pDescNode);
87,165✔
957
  QUERY_CHECK_NULL(pInfo->binfo.pRes, code, lino, _error, terrno);
87,749✔
958

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

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

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

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

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

983
  *pOptrInfo = pOperator;
87,165✔
984
  return TSDB_CODE_SUCCESS;
87,165✔
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