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

taosdata / TDengine / #5003

26 Mar 2026 12:51PM UTC coverage: 72.317% (-0.4%) from 72.671%
#5003

push

travis-ci

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

513 of 851 new or added lines in 47 files covered. (60.28%)

561 existing lines in 135 files now uncovered.

253860 of 351039 relevant lines covered (72.32%)

132153090.56 hits per line

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

81.59
/source/libs/executor/src/querytask.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 "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

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

27
#include "executorInt.h"
28
#include "index.h"
29
#include "operator.h"
30
#include "query.h"
31
#include "querytask.h"
32
#include "storageapi.h"
33
#include "thash.h"
34
#include "ttypes.h"
35
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
352,351,831✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
352,351,831✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
352,351,831✔
46
  if (p == NULL) {
352,318,635✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
352,318,635✔
51
  p->cost.created = taosGetTimestampUs();
352,364,062✔
52

53
  p->execModel = model;
352,360,975✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
352,352,093✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
352,320,434✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
352,321,863✔
UNCOV
57
    doDestroyTask(p);
×
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
352,346,785✔
62
  taosInitRWLatch(&p->lock);
352,330,419✔
63

64
  p->id.vgId = vgId;
352,335,556✔
65
  p->id.queryId = queryId;
352,337,950✔
66
  p->id.taskId = taskId;
352,325,582✔
67
  p->id.str = taosMemoryMalloc(64);
352,338,189✔
68
  if (p->id.str == NULL) {
352,286,360✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
352,328,837✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
352,313,536✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
352,272,245✔
76
    doDestroyTask(p);
274✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
352,317,087✔
81
  return TSDB_CODE_SUCCESS;
352,319,481✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
17,888✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,816,684,244✔
87

88
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
97,331✔
89
  pTaskInfo->code = rspCode;
97,331✔
90
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
97,331✔
91
}
97,331✔
92

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,100,906,488✔
94
  if (status == TASK_NOT_COMPLETED) {
1,100,906,488✔
95
    pTaskInfo->status = status;
352,518,537✔
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
748,387,951✔
99
    pTaskInfo->status |= status;
748,415,302✔
100
  }
101
}
1,100,916,890✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
40,262,784✔
105
  int32_t code = 0, lino = 0;
40,262,784✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
40,262,784✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
40,259,989✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
40,257,779✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
40,263,902✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
40,264,461✔
114
  ctx->taskId = pTaskInfo->id.taskId;
40,260,574✔
115
  ctx->idStr = pTaskInfo->id.str;
40,265,032✔
116
  ctx->pTaskInfo = pTaskInfo;
40,254,972✔
117
  ctx->subEndPoints = *subEndPoints;
40,265,579✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
40,263,902✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
40,264,475✔
120

121
  *subEndPoints = NULL;
40,256,649✔
122
  
123
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
40,263,343✔
124
  if (NULL == ctx->subResNodes) {
40,262,225✔
125
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
126
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
127
  }
128
  
129
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
40,266,138✔
130

131
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
40,254,982✔
132
  if (refId < 0) {
40,262,796✔
133
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
134
    TAOS_CHECK_EXIT(terrno);
×
135
  }
136
  
137
  ctx->subJobRefId = refId;
40,262,796✔
138

139
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
40,258,883✔
140

141
_exit:
39,802,058✔
142

143
  if (code) {
40,261,107✔
144
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
145
    pTaskInfo->pSubJobCtx = NULL;
×
146
    
147
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
148
  }
149

150
  return code;
40,261,107✔
151
}
152

153

154

155
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
352,232,806✔
156
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints, bool enableExplain) {
157
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
352,232,806✔
158
  if (*pTaskInfo == NULL || code != 0) {
352,167,888✔
159
    nodesDestroyNode((SNode*)pPlan);
42✔
160
    return code;
×
161
  }
162

163
  (*pTaskInfo)->pSubplan = pPlan;
352,191,662✔
164

165
  if (NULL != sql) {
352,199,438✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
327,370,560✔
167
    if (NULL == (*pTaskInfo)->sql) {
327,411,693✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
352,246,062✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
352,186,904✔
177
  (*pTaskInfo)->enableExplain = enableExplain;
352,187,674✔
178

179
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
352,217,571✔
180
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
40,262,784✔
181
    if (code != TSDB_CODE_SUCCESS) {
40,261,666✔
182
      doDestroyTask(*pTaskInfo);
×
183
      (*pTaskInfo) = NULL;
×
184
      return code;
×
185
    }
186
  }
187
  
188
  setTaskScalarExtraInfo(*pTaskInfo);
352,234,241✔
189
  
190
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
352,166,793✔
191
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
352,105,772✔
192

193
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
351,970,424✔
194
    doDestroyTask(*pTaskInfo);
617,949✔
195
    (*pTaskInfo) = NULL;
478,667✔
196
  }
197
  return code;
351,964,192✔
198
}
199

200
void cleanupQueriedTableScanInfo(void* p) {
267,313,608✔
201
  SSchemaInfo* pSchemaInfo = p;
267,313,608✔
202

203
  taosMemoryFreeClear(pSchemaInfo->dbname);
267,313,608✔
204
  taosMemoryFreeClear(pSchemaInfo->tablename);
267,277,359✔
205
  tDeleteSchemaWrapper(pSchemaInfo->sw);
267,248,791✔
206
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
267,228,180✔
207
}
267,221,865✔
208

209
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
267,341,400✔
210
                                   SExecTaskInfo* pTaskInfo) {
211
  SMetaReader mr = {0};
267,341,400✔
212
  if (pHandle == NULL) {
267,347,503✔
213
    return TSDB_CODE_INVALID_PARA;
×
214
  }
215

216
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
267,347,503✔
217

218
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
267,337,724✔
219
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
267,341,308✔
220
  if (code != TSDB_CODE_SUCCESS) {
267,205,921✔
UNCOV
221
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
222
           GET_TASKID(pTaskInfo));
223

UNCOV
224
    pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
225
    return code;
×
226
  }
227

228
  SSchemaInfo schemaInfo = {0};
267,205,921✔
229

230
  schemaInfo.tablename = taosStrdup(mr.me.name);
267,208,941✔
231
  schemaInfo.dbname = taosStrdup(dbName);
267,176,386✔
232
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
267,225,505✔
233
    pAPI->metaReaderFn.clearReader(&mr);
87,896✔
234
    cleanupQueriedTableScanInfo(&schemaInfo);
×
235
    return terrno;
×
236
  }
237

238
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
267,137,609✔
239
    schemaInfo.rversion = mr.me.colRef.version;
427,190✔
240
  }
241

242
  if (mr.me.type == TSDB_SUPER_TABLE) {
267,137,609✔
243
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
115,378,393✔
244
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
115,378,393✔
245
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
151,808,130✔
246
    tDecoderClear(&mr.coder);
105,403,331✔
247

248
    tb_uid_t suid = mr.me.ctbEntry.suid;
105,414,064✔
249
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
105,414,064✔
250
    if (code != TSDB_CODE_SUCCESS) {
105,413,683✔
251
      pAPI->metaReaderFn.clearReader(&mr);
×
252
      cleanupQueriedTableScanInfo(&schemaInfo);
×
253
      return code;
×
254
    }
255

256
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
105,413,928✔
257
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
105,413,928✔
258
  } else {
259
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
46,425,775✔
260
  }
261

262
  pAPI->metaReaderFn.clearReader(&mr);
267,218,096✔
263

264
  if (schemaInfo.sw == NULL) {
267,092,997✔
265
    cleanupQueriedTableScanInfo(&schemaInfo);
×
266
    return terrno;
×
267
  }
268

269
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
267,092,997✔
270
  if (schemaInfo.qsw == NULL) {
267,208,328✔
271
    cleanupQueriedTableScanInfo(&schemaInfo);
×
272
    return terrno;
×
273
  }
274

275
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
267,208,328✔
276
  if (p == NULL) {
267,240,754✔
277
    cleanupQueriedTableScanInfo(&schemaInfo);
×
278
    return terrno;
×
279
  }
280

281
  return code;
267,240,754✔
282
}
283

284
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
267,123,461✔
285
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
267,123,461✔
286
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
267,236,085✔
287

288
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
267,202,634✔
289
  if (pqSw == NULL) {
267,114,710✔
290
    return NULL;
×
291
  }
292

293
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
267,114,710✔
294
  if (pqSw->pSchema == NULL) {
266,963,857✔
295
    taosMemoryFree(pqSw);
×
296
    return NULL;
×
297
  }
298

299
  for (int32_t i = 0; i < numOfCols; ++i) {
1,202,647,693✔
300
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
935,380,551✔
301
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
935,477,358✔
302

303
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
935,497,986✔
304
    pSchema->colId = pColNode->colId;
935,482,864✔
305
    pSchema->type = pColNode->node.resType.type;
935,624,801✔
306
    pSchema->bytes = pColNode->node.resType.bytes;
935,581,212✔
307
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
935,533,447✔
308
  }
309

310
  // this the tags and pseudo function columns, we only keep the tag columns
311
  for (int32_t i = 0; i < numOfTags; ++i) {
435,435,099✔
312
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
168,167,871✔
313

314
    int32_t type = nodeType(pNode->pExpr);
168,213,746✔
315
    if (type == QUERY_NODE_COLUMN) {
168,252,111✔
316
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
127,316,632✔
317

318
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
127,310,766✔
319
      pSchema->colId = pColNode->colId;
127,264,660✔
320
      pSchema->type = pColNode->node.resType.type;
127,319,980✔
321
      pSchema->bytes = pColNode->node.resType.bytes;
127,273,433✔
322
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
127,297,242✔
323
    }
324
  }
325

326
  return pqSw;
267,267,228✔
327
}
328

329
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
352,263,818✔
330
  tDeleteSchemaWrapper(pTmqInfo->schema);
352,263,818✔
331
  tOffsetDestroy(&pTmqInfo->currentOffset);
352,307,121✔
332
}
352,196,838✔
333

334
static void freeBlock(void* pParam) {
647,961,624✔
335
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
647,961,624✔
336
  blockDataDestroy(pBlock);
647,975,121✔
337
}
647,964,669✔
338

339

340
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
40,265,591✔
341
  if (pCtx->transporterId > 0) {
40,265,591✔
342
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
7,267✔
343
    if (ret != 0) {
7,267✔
344
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
345
    }
346
    pCtx->transporterId = -1;
7,267✔
347
  }
348

349
  if (pCtx->subEndPoints != NULL) {
40,267,827✔
350
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
40,265,034✔
351
    if (size > 0) {
40,263,914✔
352
      int32_t code = tsem_destroy(&pCtx->ready);
40,265,020✔
353
      if (code != TSDB_CODE_SUCCESS) {
40,264,475✔
354
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
355
      }
356
      taosArrayDestroy(pCtx->subResNodes);
40,264,475✔
357
    }
358
    if (pCtx->isStream) {
40,256,703✔
359
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
235,697✔
360
    } else {
361
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
40,022,110✔
362
    }
363
    pCtx->subEndPoints = NULL;
40,260,588✔
364
  }
365
  
366
  taosMemoryFreeClear(pCtx);  
40,261,704✔
367
}
40,260,025✔
368

369
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
352,347,447✔
370
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
352,347,447✔
371
  destroyOperator(pTaskInfo->pRoot);
352,347,039✔
372
  pTaskInfo->pRoot = NULL;
352,252,736✔
373

374
  if (pTaskInfo->pSubJobCtx) {
352,292,519✔
375
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
40,264,461✔
376
    if (code != TSDB_CODE_SUCCESS) {
40,266,697✔
377
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
378
    }
379
  }
380

381
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
352,312,914✔
382
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
352,249,194✔
383

384
  if (!pTaskInfo->localFetch.localExec) {
352,186,039✔
385
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
352,276,167✔
386
    pTaskInfo->pSubplan = NULL;
352,292,427✔
387
  }
388

389
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
352,229,844✔
390
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
352,258,700✔
391
  if (!pTaskInfo->paramSet) {
352,300,669✔
392
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
345,960,645✔
393
    pTaskInfo->pOpParam = NULL;
345,955,475✔
394
  }
395
  taosMemoryFreeClear(pTaskInfo->sql);
352,249,175✔
396
  taosMemoryFreeClear(pTaskInfo->id.str);
352,282,339✔
397
  taosMemoryFreeClear(pTaskInfo);
352,229,677✔
398
}
352,212,656✔
399

400
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
368,107,413✔
401
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
368,107,413✔
402
  if (ret < 0) {
368,107,413✔
403
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
404
  }
405
}
368,107,413✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc