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

taosdata / TDengine / #4984

13 Mar 2026 03:38AM UTC coverage: 68.643% (-0.01%) from 68.653%
#4984

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

3048 existing lines in 150 files now uncovered.

212713 of 309883 relevant lines covered (68.64%)

135561814.23 hits per line

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

82.13
/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,
340,030,805✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
340,030,805✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
340,030,805✔
46
  if (p == NULL) {
339,988,160✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
339,988,160✔
51
  p->cost.created = taosGetTimestampUs();
340,038,637✔
52

53
  p->execModel = model;
340,034,462✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
340,018,403✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
339,994,814✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
339,987,736✔
UNCOV
57
    doDestroyTask(p);
×
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
340,022,694✔
62
  taosInitRWLatch(&p->lock);
340,011,715✔
63

64
  p->id.vgId = vgId;
340,005,292✔
65
  p->id.queryId = queryId;
340,011,712✔
66
  p->id.taskId = taskId;
340,007,739✔
67
  p->id.str = taosMemoryMalloc(64);
340,012,539✔
68
  if (p->id.str == NULL) {
339,958,808✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
340,008,178✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
339,974,731✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
339,949,790✔
76
    doDestroyTask(p);
425✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
339,984,073✔
81
  return TSDB_CODE_SUCCESS;
339,980,318✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,777,967,708✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,075,950,915✔
94
  if (status == TASK_NOT_COMPLETED) {
1,075,950,915✔
95
    pTaskInfo->status = status;
340,174,231✔
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);
735,776,684✔
99
    pTaskInfo->status |= status;
735,785,538✔
100
  }
101
}
1,075,966,133✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
37,943,247✔
105
  int32_t code = 0, lino = 0;
37,943,247✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
37,943,247✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
37,942,159✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
37,943,239✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
37,943,787✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
37,945,399✔
114
  ctx->taskId = pTaskInfo->id.taskId;
37,943,239✔
115
  ctx->idStr = pTaskInfo->id.str;
37,946,479✔
116
  ctx->pTaskInfo = pTaskInfo;
37,942,159✔
117
  ctx->subEndPoints = *subEndPoints;
37,944,327✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
37,943,787✔
119

120
  *subEndPoints = NULL;
37,946,479✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,947,019✔
123
  if (NULL == ctx->subResNodes) {
37,944,867✔
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,942,707✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
37,940,015✔
131
  if (refId < 0) {
37,944,319✔
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,944,319✔
137

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

140
_exit:
37,731,012✔
141

142
  if (code) {
37,943,255✔
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,942,715✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
339,905,763✔
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);
339,905,763✔
157
  if (*pTaskInfo == NULL || code != 0) {
339,835,953✔
158
    nodesDestroyNode((SNode*)pPlan);
×
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
339,854,366✔
163

164
  if (NULL != sql) {
339,862,429✔
165
    (*pTaskInfo)->sql = taosStrdup(sql);
316,791,804✔
166
    if (NULL == (*pTaskInfo)->sql) {
316,785,235✔
167
      code = terrno;
×
168
      doDestroyTask(*pTaskInfo);
×
169
      (*pTaskInfo) = NULL;
×
170
      return code;
×
171
    }
172
  }
173

174
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
339,850,353✔
175
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
339,723,909✔
176

177
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
339,866,137✔
178
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
37,943,787✔
179
    if (code != TSDB_CODE_SUCCESS) {
37,944,327✔
180
      doDestroyTask(*pTaskInfo);
×
181
      (*pTaskInfo) = NULL;
×
182
      return code;
×
183
    }
184
  }
185
  
186
  setTaskScalarExtraInfo(*pTaskInfo);
339,845,682✔
187
  
188
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
339,820,678✔
189
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
339,817,332✔
190

191
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
339,686,012✔
192
    doDestroyTask(*pTaskInfo);
754,579✔
193
    (*pTaskInfo) = NULL;
460,974✔
194
  }
195
  return code;
339,563,196✔
196
}
197

198
void cleanupQueriedTableScanInfo(void* p) {
253,442,369✔
199
  SSchemaInfo* pSchemaInfo = p;
253,442,369✔
200

201
  taosMemoryFreeClear(pSchemaInfo->dbname);
253,442,369✔
202
  taosMemoryFreeClear(pSchemaInfo->tablename);
253,452,255✔
203
  tDeleteSchemaWrapper(pSchemaInfo->sw);
253,379,822✔
204
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
253,395,539✔
205
}
253,397,086✔
206

207
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
253,495,934✔
208
                                   SExecTaskInfo* pTaskInfo) {
209
  SMetaReader mr = {0};
253,495,934✔
210
  if (pHandle == NULL) {
253,501,867✔
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213

214
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
253,501,867✔
215

216
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
253,493,799✔
217
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
253,497,855✔
218
  if (code != TSDB_CODE_SUCCESS) {
253,268,496✔
219
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
232✔
220
           GET_TASKID(pTaskInfo));
221

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

226
  SSchemaInfo schemaInfo = {0};
253,268,264✔
227

228
  schemaInfo.tablename = taosStrdup(mr.me.name);
253,326,514✔
229
  schemaInfo.dbname = taosStrdup(dbName);
253,300,057✔
230
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
253,368,667✔
231
    pAPI->metaReaderFn.clearReader(&mr);
140,531✔
232
    cleanupQueriedTableScanInfo(&schemaInfo);
×
233
    return terrno;
×
234
  }
235

236
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
253,228,136✔
237
    schemaInfo.rversion = mr.me.colRef.version;
284,020✔
238
  }
239

240
  if (mr.me.type == TSDB_SUPER_TABLE) {
253,228,136✔
241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
110,366,821✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
110,366,821✔
243
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
142,882,176✔
244
    tDecoderClear(&mr.coder);
99,907,470✔
245

246
    tb_uid_t suid = mr.me.ctbEntry.suid;
100,016,910✔
247
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
100,016,910✔
248
    if (code != TSDB_CODE_SUCCESS) {
100,018,563✔
249
      pAPI->metaReaderFn.clearReader(&mr);
×
250
      cleanupQueriedTableScanInfo(&schemaInfo);
×
251
      return code;
×
252
    }
253

254
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
100,018,576✔
255
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
100,018,576✔
256
  } else {
257
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
42,970,754✔
258
  }
259

260
  pAPI->metaReaderFn.clearReader(&mr);
253,356,151✔
261

262
  if (schemaInfo.sw == NULL) {
253,242,968✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
253,242,968✔
268
  if (schemaInfo.qsw == NULL) {
253,300,433✔
269
    cleanupQueriedTableScanInfo(&schemaInfo);
×
270
    return terrno;
×
271
  }
272

273
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
253,300,433✔
274
  if (p == NULL) {
253,401,040✔
275
    cleanupQueriedTableScanInfo(&schemaInfo);
×
276
    return terrno;
×
277
  }
278

279
  return code;
253,401,040✔
280
}
281

282
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
253,285,878✔
283
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
253,285,878✔
284
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
253,420,712✔
285

286
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
253,375,989✔
287
  if (pqSw == NULL) {
253,225,743✔
288
    return NULL;
×
289
  }
290

291
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
253,225,743✔
292
  if (pqSw->pSchema == NULL) {
253,084,338✔
293
    taosMemoryFree(pqSw);
×
294
    return NULL;
×
295
  }
296

297
  for (int32_t i = 0; i < numOfCols; ++i) {
1,129,244,105✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
875,817,738✔
299
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
875,676,660✔
300

301
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
875,750,928✔
302
    pSchema->colId = pColNode->colId;
875,821,707✔
303
    pSchema->type = pColNode->node.resType.type;
875,935,758✔
304
    pSchema->bytes = pColNode->node.resType.bytes;
875,923,185✔
305
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
876,000,508✔
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) {
393,662,466✔
310
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
140,245,873✔
311

312
    int32_t type = nodeType(pNode->pExpr);
140,203,601✔
313
    if (type == QUERY_NODE_COLUMN) {
140,233,404✔
314
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
102,304,332✔
315

316
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
102,313,257✔
317
      pSchema->colId = pColNode->colId;
102,326,921✔
318
      pSchema->type = pColNode->node.resType.type;
102,276,960✔
319
      pSchema->bytes = pColNode->node.resType.bytes;
102,298,081✔
320
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
102,288,859✔
321
    }
322
  }
323

324
  return pqSw;
253,416,593✔
325
}
326

327
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
339,920,572✔
328
  tDeleteSchemaWrapper(pTmqInfo->schema);
339,920,572✔
329
  tOffsetDestroy(&pTmqInfo->currentOffset);
339,967,717✔
330
}
339,872,954✔
331

332
static void freeBlock(void* pParam) {
652,122,799✔
333
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
652,122,799✔
334
  blockDataDestroy(pBlock);
652,135,863✔
335
}
652,132,529✔
336

337

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

347
  if (pCtx->subEndPoints != NULL) {
37,942,707✔
348
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
37,944,867✔
349
    if (size > 0) {
37,938,411✔
350
      int32_t code = tsem_destroy(&pCtx->ready);
37,939,491✔
351
      if (code != TSDB_CODE_SUCCESS) {
37,941,087✔
352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
353
      }
354
      taosArrayDestroy(pCtx->subResNodes);
37,941,087✔
355
    }
356
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
37,938,951✔
357
    pCtx->subEndPoints = NULL;
37,942,159✔
358
  }
359
  
360
  taosMemoryFreeClear(pCtx);  
37,944,311✔
361
}
37,944,327✔
362

363
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
340,028,783✔
364
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
340,028,783✔
365
  destroyOperator(pTaskInfo->pRoot);
340,026,470✔
366
  pTaskInfo->pRoot = NULL;
339,923,317✔
367

368
  if (pTaskInfo->pSubJobCtx) {
339,941,311✔
369
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
37,942,167✔
370
    if (code != TSDB_CODE_SUCCESS) {
37,944,319✔
371
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
372
    }
373
  }
374

375
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
339,961,336✔
376
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
339,932,170✔
377

378
  if (!pTaskInfo->localFetch.localExec) {
339,862,591✔
379
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
339,887,178✔
380
    pTaskInfo->pSubplan = NULL;
339,942,724✔
381
  }
382

383
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
339,968,074✔
384
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
339,946,722✔
385
  if (!pTaskInfo->paramSet) {
339,941,007✔
386
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
331,850,070✔
387
    pTaskInfo->pOpParam = NULL;
331,837,146✔
388
  }
389
  taosMemoryFreeClear(pTaskInfo->sql);
339,935,586✔
390
  taosMemoryFreeClear(pTaskInfo->id.str);
339,934,418✔
391
  taosMemoryFreeClear(pTaskInfo);
339,940,366✔
392
}
339,910,873✔
393

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