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

taosdata / TDengine / #4979

09 Mar 2026 09:42AM UTC coverage: 68.512% (+0.07%) from 68.439%
#4979

push

travis-ci

web-flow
doc: update user manual. (#34693)

211961 of 309380 relevant lines covered (68.51%)

134839524.93 hits per line

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

82.16
/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,
343,235,785✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
343,235,785✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
343,235,785✔
46
  if (p == NULL) {
343,238,653✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
343,238,653✔
51
  p->cost.created = taosGetTimestampUs();
343,241,324✔
52

53
  p->execModel = model;
343,221,519✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
343,231,001✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
343,202,059✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
343,209,545✔
57
    doDestroyTask(p);
26,870✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
343,192,960✔
62
  taosInitRWLatch(&p->lock);
343,218,278✔
63

64
  p->id.vgId = vgId;
343,200,257✔
65
  p->id.queryId = queryId;
343,203,612✔
66
  p->id.taskId = taskId;
343,221,860✔
67
  p->id.str = taosMemoryMalloc(64);
343,214,427✔
68
  if (p->id.str == NULL) {
343,161,059✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
343,192,459✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
343,184,034✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
343,124,951✔
76
    doDestroyTask(p);
×
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
343,161,285✔
81
  return TSDB_CODE_SUCCESS;
343,149,449✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,776,583,023✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,083,019,010✔
94
  if (status == TASK_NOT_COMPLETED) {
1,083,019,010✔
95
    pTaskInfo->status = status;
343,427,893✔
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);
739,591,117✔
99
    pTaskInfo->status |= status;
739,613,083✔
100
  }
101
}
1,083,040,546✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
37,311,485✔
105
  int32_t code = 0, lino = 0;
37,311,485✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
37,311,485✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
37,313,625✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
37,309,902✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
37,312,042✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
37,315,241✔
114
  ctx->taskId = pTaskInfo->id.taskId;
37,322,185✔
115
  ctx->idStr = pTaskInfo->id.str;
37,317,370✔
116
  ctx->pTaskInfo = pTaskInfo;
37,312,020✔
117
  ctx->subEndPoints = *subEndPoints;
37,310,415✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
37,308,286✔
119

120
  *subEndPoints = NULL;
37,323,255✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,323,790✔
123
  if (NULL == ctx->subResNodes) {
37,312,566✔
124
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
125
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
126
  }
127
  
128
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
37,310,961✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
37,309,356✔
131
  if (refId < 0) {
37,321,661✔
132
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
133
    TAOS_CHECK_EXIT(terrno);
×
134
  }
135
  
136
  ctx->subJobRefId = refId;
37,321,661✔
137

138
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
37,313,635✔
139

140
_exit:
37,121,515✔
141

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

149
  return code;
37,308,831✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
343,116,017✔
155
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
156
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
343,116,017✔
157
  if (*pTaskInfo == NULL || code != 0) {
343,030,136✔
158
    nodesDestroyNode((SNode*)pPlan);
114✔
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
343,057,288✔
163

164
  if (pHandle) {
343,044,031✔
165
    if (pHandle->pStateBackend) {
343,038,299✔
166
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
167
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
168
    }
169
  }
170

171
  if (NULL != sql) {
343,059,480✔
172
    (*pTaskInfo)->sql = taosStrdup(sql);
320,409,033✔
173
    if (NULL == (*pTaskInfo)->sql) {
320,438,440✔
174
      code = terrno;
×
175
      doDestroyTask(*pTaskInfo);
×
176
      (*pTaskInfo) = NULL;
×
177
      return code;
×
178
    }
179
  }
180

181
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
343,081,707✔
182
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
343,054,628✔
183

184
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
342,989,676✔
185
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
37,319,521✔
186
    if (code != TSDB_CODE_SUCCESS) {
37,307,772✔
187
      doDestroyTask(*pTaskInfo);
×
188
      (*pTaskInfo) = NULL;
×
189
      return code;
×
190
    }
191
  }
192
  
193
  setTaskScalarExtraInfo(*pTaskInfo);
342,954,042✔
194
  
195
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
342,946,630✔
196
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
342,954,311✔
197

198
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
342,877,100✔
199
    doDestroyTask(*pTaskInfo);
697,587✔
200
    (*pTaskInfo) = NULL;
457,989✔
201
  }
202
  return code;
342,671,710✔
203
}
204

205
void cleanupQueriedTableScanInfo(void* p) {
257,184,756✔
206
  SSchemaInfo* pSchemaInfo = p;
257,184,756✔
207

208
  taosMemoryFreeClear(pSchemaInfo->dbname);
257,184,756✔
209
  taosMemoryFreeClear(pSchemaInfo->tablename);
257,150,778✔
210
  tDeleteSchemaWrapper(pSchemaInfo->sw);
257,165,035✔
211
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
257,162,597✔
212
}
257,175,304✔
213

214
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
257,248,529✔
215
                                   SExecTaskInfo* pTaskInfo) {
216
  SMetaReader mr = {0};
257,248,529✔
217
  if (pHandle == NULL) {
257,252,999✔
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220

221
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
257,252,999✔
222

223
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
257,242,334✔
224
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
257,263,200✔
225
  if (code != TSDB_CODE_SUCCESS) {
256,955,859✔
226
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
224✔
227
           GET_TASKID(pTaskInfo));
228

229
    pAPI->metaReaderFn.clearReader(&mr);
224✔
230
    return code;
224✔
231
  }
232

233
  SSchemaInfo schemaInfo = {0};
256,955,635✔
234

235
  schemaInfo.tablename = taosStrdup(mr.me.name);
256,995,603✔
236
  schemaInfo.dbname = taosStrdup(dbName);
257,039,121✔
237
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
257,112,296✔
238
    pAPI->metaReaderFn.clearReader(&mr);
148,264✔
239
    cleanupQueriedTableScanInfo(&schemaInfo);
×
240
    return terrno;
×
241
  }
242

243
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
256,964,032✔
244
    schemaInfo.rversion = mr.me.colRef.version;
282,532✔
245
  }
246

247
  if (mr.me.type == TSDB_SUPER_TABLE) {
256,964,032✔
248
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
116,908,381✔
249
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
116,908,381✔
250
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
140,121,618✔
251
    tDecoderClear(&mr.coder);
98,507,676✔
252

253
    tb_uid_t suid = mr.me.ctbEntry.suid;
98,506,657✔
254
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
98,506,657✔
255
    if (code != TSDB_CODE_SUCCESS) {
98,505,449✔
256
      pAPI->metaReaderFn.clearReader(&mr);
×
257
      cleanupQueriedTableScanInfo(&schemaInfo);
×
258
      return code;
×
259
    }
260

261
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
98,505,090✔
262
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
98,505,090✔
263
  } else {
264
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
41,648,793✔
265
  }
266

267
  pAPI->metaReaderFn.clearReader(&mr);
257,062,264✔
268

269
  if (schemaInfo.sw == NULL) {
257,047,114✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
271
    return terrno;
×
272
  }
273

274
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
257,047,114✔
275
  if (schemaInfo.qsw == NULL) {
257,057,829✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
277
    return terrno;
×
278
  }
279

280
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
257,057,829✔
281
  if (p == NULL) {
257,118,196✔
282
    cleanupQueriedTableScanInfo(&schemaInfo);
×
283
    return terrno;
×
284
  }
285

286
  return code;
257,118,196✔
287
}
288

289
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
257,050,271✔
290
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
257,050,271✔
291
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
257,147,790✔
292

293
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
256,973,021✔
294
  if (pqSw == NULL) {
256,910,159✔
295
    return NULL;
×
296
  }
297

298
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
256,910,159✔
299
  if (pqSw->pSchema == NULL) {
256,776,041✔
300
    taosMemoryFree(pqSw);
×
301
    return NULL;
×
302
  }
303

304
  for (int32_t i = 0; i < numOfCols; ++i) {
1,162,433,840✔
305
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
905,267,343✔
306
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
905,371,293✔
307

308
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
905,393,891✔
309
    pSchema->colId = pColNode->colId;
905,471,069✔
310
    pSchema->type = pColNode->node.resType.type;
905,553,194✔
311
    pSchema->bytes = pColNode->node.resType.bytes;
905,524,423✔
312
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
905,456,595✔
313
  }
314

315
  // this the tags and pseudo function columns, we only keep the tag columns
316
  for (int32_t i = 0; i < numOfTags; ++i) {
442,395,809✔
317
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
185,221,590✔
318

319
    int32_t type = nodeType(pNode->pExpr);
185,290,245✔
320
    if (type == QUERY_NODE_COLUMN) {
185,316,109✔
321
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
142,835,066✔
322

323
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
142,827,052✔
324
      pSchema->colId = pColNode->colId;
142,751,298✔
325
      pSchema->type = pColNode->node.resType.type;
142,773,489✔
326
      pSchema->bytes = pColNode->node.resType.bytes;
142,753,210✔
327
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
142,718,255✔
328
    }
329
  }
330

331
  return pqSw;
257,174,219✔
332
}
333

334
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
343,141,719✔
335
  tDeleteSchemaWrapper(pStreamInfo->schema);
343,141,719✔
336
  tOffsetDestroy(&pStreamInfo->currentOffset);
343,161,373✔
337
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
343,055,780✔
338
  taosMemoryFree(pStreamInfo->stbFullName);
343,167,212✔
339
}
343,108,368✔
340

341
static void freeBlock(void* pParam) {
628,952,589✔
342
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
628,952,589✔
343
  blockDataDestroy(pBlock);
628,967,118✔
344
}
628,963,557✔
345

346

347
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
37,319,510✔
348
  if (pCtx->transporterId > 0) {
37,319,510✔
349
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
5,350✔
350
    if (ret != 0) {
5,350✔
351
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
352
    }
353
    pCtx->transporterId = -1;
5,350✔
354
  }
355

356
  if (pCtx->subEndPoints != NULL) {
37,321,126✔
357
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
37,322,720✔
358
    if (size > 0) {
37,319,531✔
359
      int32_t code = tsem_destroy(&pCtx->ready);
37,319,531✔
360
      if (code != TSDB_CODE_SUCCESS) {
37,322,196✔
361
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
362
      }
363
      taosArrayDestroy(pCtx->subResNodes);
37,322,196✔
364
    }
365
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
37,318,997✔
366
    pCtx->subEndPoints = NULL;
37,317,381✔
367
  }
368
  
369
  taosMemoryFreeClear(pCtx);  
37,317,381✔
370
}
37,319,531✔
371

372
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
343,242,407✔
373
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
343,242,407✔
374
  destroyOperator(pTaskInfo->pRoot);
343,242,407✔
375
  pTaskInfo->pRoot = NULL;
343,155,873✔
376

377
  if (pTaskInfo->pSubJobCtx) {
343,167,876✔
378
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
37,320,055✔
379
    if (code != TSDB_CODE_SUCCESS) {
37,321,650✔
380
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
381
    }
382
  }
383

384
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
343,199,047✔
385
  cleanupStreamInfo(&pTaskInfo->streamInfo);
343,145,442✔
386

387
  if (!pTaskInfo->localFetch.localExec) {
343,120,405✔
388
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
343,100,143✔
389
    pTaskInfo->pSubplan = NULL;
343,180,345✔
390
  }
391

392
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
343,210,922✔
393
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
343,156,356✔
394
  if (!pTaskInfo->paramSet) {
343,183,394✔
395
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
335,183,338✔
396
    pTaskInfo->pOpParam = NULL;
335,149,067✔
397
  }
398
  taosMemoryFreeClear(pTaskInfo->sql);
343,195,922✔
399
  taosMemoryFreeClear(pTaskInfo->id.str);
343,157,927✔
400
  taosMemoryFreeClear(pTaskInfo);
343,149,039✔
401
}
343,155,574✔
402

403
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
370,238,380✔
404
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
370,238,380✔
405
  if (ret < 0) {
370,238,380✔
406
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
407
  }
408
}
370,238,380✔
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