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

taosdata / TDengine / #4982

12 Mar 2026 05:32AM UTC coverage: 68.587% (+0.1%) from 68.488%
#4982

push

travis-ci

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

merge: from main to 3.0 branch

279 of 443 new or added lines in 34 files covered. (62.98%)

2352 existing lines in 132 files now uncovered.

212163 of 309332 relevant lines covered (68.59%)

135254549.82 hits per line

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

82.55
/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,
350,264,803✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
350,264,803✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
350,264,803✔
46
  if (p == NULL) {
350,251,315✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
350,251,315✔
51
  p->cost.created = taosGetTimestampUs();
350,279,262✔
52

53
  p->execModel = model;
350,247,634✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
350,250,103✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
350,204,982✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
350,176,499✔
57
    doDestroyTask(p);
2,915✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
350,190,171✔
62
  taosInitRWLatch(&p->lock);
350,235,804✔
63

64
  p->id.vgId = vgId;
350,194,670✔
65
  p->id.queryId = queryId;
350,195,165✔
66
  p->id.taskId = taskId;
350,217,604✔
67
  p->id.str = taosMemoryMalloc(64);
350,205,207✔
68
  if (p->id.str == NULL) {
350,082,414✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
350,175,659✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
350,111,374✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
349,929,383✔
UNCOV
76
    doDestroyTask(p);
×
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
350,056,163✔
81
  return TSDB_CODE_SUCCESS;
350,018,559✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,809,444,228✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,107,339,991✔
94
  if (status == TASK_NOT_COMPLETED) {
1,107,339,991✔
95
    pTaskInfo->status = status;
350,443,437✔
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);
756,896,554✔
99
    pTaskInfo->status |= status;
756,938,647✔
100
  }
101
}
1,107,388,607✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
39,242,853✔
105
  int32_t code = 0, lino = 0;
39,242,853✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
39,242,853✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
39,242,871✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
39,232,278✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
39,234,393✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
39,238,632✔
114
  ctx->taskId = pTaskInfo->id.taskId;
39,243,402✔
115
  ctx->idStr = pTaskInfo->id.str;
39,240,756✔
116
  ctx->pTaskInfo = pTaskInfo;
39,237,039✔
117
  ctx->subEndPoints = *subEndPoints;
39,237,579✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
39,234,933✔
119

120
  *subEndPoints = NULL;
39,249,738✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
39,250,269✔
123
  if (NULL == ctx->subResNodes) {
39,235,437✔
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));
39,229,632✔
129

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

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

140
_exit:
39,022,470✔
141

142
  if (code) {
39,231,774✔
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;
39,230,712✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
350,140,118✔
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);
350,140,118✔
157
  if (*pTaskInfo == NULL || code != 0) {
349,892,427✔
158
    nodesDestroyNode((SNode*)pPlan);
9,871✔
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
349,905,751✔
163

164
  if (NULL != sql) {
349,974,611✔
165
    (*pTaskInfo)->sql = taosStrdup(sql);
325,922,227✔
166
    if (NULL == (*pTaskInfo)->sql) {
326,077,358✔
167
      code = terrno;
×
UNCOV
168
      doDestroyTask(*pTaskInfo);
×
UNCOV
169
      (*pTaskInfo) = NULL;
×
UNCOV
170
      return code;
×
171
    }
172
  }
173

174
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
350,006,455✔
175
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
349,950,897✔
176

177
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
349,843,174✔
178
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
39,243,897✔
179
    if (code != TSDB_CODE_SUCCESS) {
39,236,526✔
UNCOV
180
      doDestroyTask(*pTaskInfo);
×
UNCOV
181
      (*pTaskInfo) = NULL;
×
UNCOV
182
      return code;
×
183
    }
184
  }
185
  
186
  setTaskScalarExtraInfo(*pTaskInfo);
349,784,830✔
187
  
188
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
349,940,167✔
189
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
349,766,852✔
190

191
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
349,261,373✔
192
    doDestroyTask(*pTaskInfo);
1,673,536✔
193
    (*pTaskInfo) = NULL;
455,273✔
194
  }
195
  return code;
348,221,872✔
196
}
197

198
void cleanupQueriedTableScanInfo(void* p) {
259,607,089✔
199
  SSchemaInfo* pSchemaInfo = p;
259,607,089✔
200

201
  taosMemoryFreeClear(pSchemaInfo->dbname);
259,607,089✔
202
  taosMemoryFreeClear(pSchemaInfo->tablename);
259,517,470✔
203
  tDeleteSchemaWrapper(pSchemaInfo->sw);
259,409,911✔
204
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
259,498,147✔
205
}
259,490,029✔
206

207
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
259,710,994✔
208
                                   SExecTaskInfo* pTaskInfo) {
209
  SMetaReader mr = {0};
259,710,994✔
210
  if (pHandle == NULL) {
259,741,499✔
UNCOV
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213

214
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
259,741,499✔
215

216
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
259,724,271✔
217
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
259,728,776✔
218
  if (code != TSDB_CODE_SUCCESS) {
258,471,245✔
219
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
226✔
220
           GET_TASKID(pTaskInfo));
221

222
    pAPI->metaReaderFn.clearReader(&mr);
226✔
223
    return code;
226✔
224
  }
225

226
  SSchemaInfo schemaInfo = {0};
258,471,019✔
227

228
  schemaInfo.tablename = taosStrdup(mr.me.name);
258,672,485✔
229
  schemaInfo.dbname = taosStrdup(dbName);
258,916,727✔
230
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
259,039,968✔
231
    pAPI->metaReaderFn.clearReader(&mr);
468,148✔
UNCOV
232
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
233
    return terrno;
×
234
  }
235

236
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
258,571,820✔
237
    schemaInfo.rversion = mr.me.colRef.version;
287,744✔
238
  }
239

240
  if (mr.me.type == TSDB_SUPER_TABLE) {
258,571,820✔
241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
113,661,481✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
113,661,481✔
243
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
145,086,436✔
244
    tDecoderClear(&mr.coder);
102,652,605✔
245

246
    tb_uid_t suid = mr.me.ctbEntry.suid;
102,845,928✔
247
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
102,845,928✔
248
    if (code != TSDB_CODE_SUCCESS) {
102,837,956✔
UNCOV
249
      pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
250
      cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
251
      return code;
×
252
    }
253

254
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
102,843,192✔
255
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
102,843,192✔
256
  } else {
257
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
42,522,737✔
258
  }
259

260
  pAPI->metaReaderFn.clearReader(&mr);
259,027,410✔
261

262
  if (schemaInfo.sw == NULL) {
258,864,448✔
UNCOV
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
264
    return terrno;
×
265
  }
266

267
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
258,864,448✔
268
  if (schemaInfo.qsw == NULL) {
259,262,660✔
UNCOV
269
    cleanupQueriedTableScanInfo(&schemaInfo);
×
270
    return terrno;
×
271
  }
272

273
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
259,262,660✔
274
  if (p == NULL) {
259,170,478✔
UNCOV
275
    cleanupQueriedTableScanInfo(&schemaInfo);
×
276
    return terrno;
×
277
  }
278

279
  return code;
259,170,478✔
280
}
281

282
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
258,974,726✔
283
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
258,974,726✔
284
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
259,226,631✔
285

286
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
258,778,290✔
287
  if (pqSw == NULL) {
258,485,960✔
UNCOV
288
    return NULL;
×
289
  }
290

291
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
258,485,960✔
292
  if (pqSw->pSchema == NULL) {
257,948,633✔
UNCOV
293
    taosMemoryFree(pqSw);
×
UNCOV
294
    return NULL;
×
295
  }
296

297
  for (int32_t i = 0; i < numOfCols; ++i) {
1,169,180,021✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
909,717,537✔
299
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
910,044,254✔
300

301
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
910,146,881✔
302
    pSchema->colId = pColNode->colId;
910,382,957✔
303
    pSchema->type = pColNode->node.resType.type;
910,543,346✔
304
    pSchema->bytes = pColNode->node.resType.bytes;
910,263,507✔
305
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
910,091,501✔
306
  }
307

308
  // this the tags and pseudo function columns, we only keep the tag columns
309
  for (int32_t i = 0; i < numOfTags; ++i) {
425,380,937✔
310
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
165,873,240✔
311

312
    int32_t type = nodeType(pNode->pExpr);
166,087,480✔
313
    if (type == QUERY_NODE_COLUMN) {
166,210,234✔
314
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
124,393,004✔
315

316
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
124,335,383✔
317
      pSchema->colId = pColNode->colId;
124,178,905✔
318
      pSchema->type = pColNode->node.resType.type;
124,246,381✔
319
      pSchema->bytes = pColNode->node.resType.bytes;
124,223,330✔
320
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
124,062,595✔
321
    }
322
  }
323

324
  return pqSw;
259,507,697✔
325
}
326

327
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
349,979,436✔
328
  tDeleteSchemaWrapper(pTmqInfo->schema);
349,979,436✔
329
  tOffsetDestroy(&pTmqInfo->currentOffset);
350,072,492✔
330
}
349,595,326✔
331

332
static void freeBlock(void* pParam) {
657,093,872✔
333
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
657,093,872✔
334
  blockDataDestroy(pBlock);
657,124,467✔
335
}
657,082,728✔
336

337

338
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
39,240,720✔
339
  if (pCtx->transporterId > 0) {
39,240,720✔
340
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
3,717✔
341
    if (ret != 0) {
3,717✔
UNCOV
342
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
343
    }
344
    pCtx->transporterId = -1;
3,717✔
345
  }
346

347
  if (pCtx->subEndPoints != NULL) {
39,244,959✔
348
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
39,247,614✔
349
    if (size > 0) {
39,246,561✔
350
      int32_t code = tsem_destroy(&pCtx->ready);
39,246,561✔
351
      if (code != TSDB_CODE_SUCCESS) {
39,243,375✔
UNCOV
352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
353
      }
354
      taosArrayDestroy(pCtx->subResNodes);
39,243,375✔
355
    }
356
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
39,233,295✔
357
    pCtx->subEndPoints = NULL;
39,238,065✔
358
  }
359
  
360
  taosMemoryFreeClear(pCtx);  
39,239,136✔
361
}
39,233,835✔
362

363
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
350,296,904✔
364
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
350,296,904✔
365
  destroyOperator(pTaskInfo->pRoot);
350,297,210✔
366
  pTaskInfo->pRoot = NULL;
349,953,623✔
367

368
  if (pTaskInfo->pSubJobCtx) {
350,014,309✔
369
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
39,247,074✔
370
    if (code != TSDB_CODE_SUCCESS) {
39,244,950✔
UNCOV
371
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
372
    }
373
  }
374

375
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
350,124,865✔
376
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
349,936,932✔
377

378
  if (!pTaskInfo->localFetch.localExec) {
349,585,720✔
379
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
349,830,985✔
380
    pTaskInfo->pSubplan = NULL;
350,055,443✔
381
  }
382

383
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
350,014,308✔
384
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
349,979,412✔
385
  if (!pTaskInfo->paramSet) {
350,041,896✔
386
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
341,247,499✔
387
    pTaskInfo->pOpParam = NULL;
341,014,035✔
388
  }
389
  taosMemoryFreeClear(pTaskInfo->sql);
349,667,792✔
390
  taosMemoryFreeClear(pTaskInfo->id.str);
349,903,132✔
391
  taosMemoryFreeClear(pTaskInfo);
349,651,484✔
392
}
349,661,696✔
393

394
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
376,896,233✔
395
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
376,896,233✔
396
  if (ret < 0) {
376,896,233✔
UNCOV
397
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
398
  }
399
}
376,896,233✔
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