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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

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%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

81.28
/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,
349,797,632✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
349,797,632✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
349,797,632✔
46
  if (p == NULL) {
349,767,067✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
349,767,067✔
51
  p->cost.created = taosGetTimestampUs();
349,817,978✔
52

53
  p->execModel = model;
349,812,742✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
349,799,622✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
349,759,488✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
349,751,479✔
57
    doDestroyTask(p);
20✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
349,793,373✔
62
  taosInitRWLatch(&p->lock);
349,782,841✔
63

64
  p->id.vgId = vgId;
349,785,760✔
65
  p->id.queryId = queryId;
349,794,276✔
66
  p->id.taskId = taskId;
349,788,728✔
67
  p->id.str = taosMemoryMalloc(64);
349,783,544✔
68
  if (p->id.str == NULL) {
349,718,545✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
349,777,922✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
349,736,803✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
349,708,956✔
76
    doDestroyTask(p);
76✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
349,758,569✔
81
  return TSDB_CODE_SUCCESS;
349,751,788✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,754,784,188✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,103,851,996✔
94
  if (status == TASK_NOT_COMPLETED) {
1,103,851,996✔
95
    pTaskInfo->status = status;
349,962,689✔
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);
753,889,307✔
99
    pTaskInfo->status |= status;
753,901,008✔
100
  }
101
}
1,103,873,215✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
38,873,296✔
105
  int32_t code = 0, lino = 0;
38,873,296✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
38,873,296✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
38,874,394✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
38,869,501✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
38,871,686✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
38,873,858✔
114
  ctx->taskId = pTaskInfo->id.taskId;
38,872,762✔
115
  ctx->idStr = pTaskInfo->id.str;
38,876,054✔
116
  ctx->pTaskInfo = pTaskInfo;
38,871,111✔
117
  ctx->subEndPoints = *subEndPoints;
38,877,128✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
38,872,211✔
119

120
  *subEndPoints = NULL;
38,877,128✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
38,878,226✔
123
  if (NULL == ctx->subResNodes) {
38,877,677✔
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));
38,870,586✔
129

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

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

140
_exit:
38,653,875✔
141

142
  if (code) {
38,870,551✔
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;
38,868,355✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
349,664,603✔
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);
349,664,603✔
157
  if (*pTaskInfo == NULL || code != 0) {
349,601,734✔
UNCOV
158
    nodesDestroyNode((SNode*)pPlan);
×
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
349,626,592✔
163

164
  if (NULL != sql) {
349,628,051✔
165
    (*pTaskInfo)->sql = taosStrdup(sql);
326,026,330✔
166
    if (NULL == (*pTaskInfo)->sql) {
326,029,698✔
167
      code = terrno;
×
168
      doDestroyTask(*pTaskInfo);
×
169
      (*pTaskInfo) = NULL;
×
170
      return code;
×
171
    }
172
  }
173

174
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
349,630,414✔
175
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
349,482,220✔
176

177
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
349,627,877✔
178
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
38,874,945✔
179
    if (code != TSDB_CODE_SUCCESS) {
38,870,538✔
180
      doDestroyTask(*pTaskInfo);
×
181
      (*pTaskInfo) = NULL;
×
182
      return code;
×
183
    }
184
  }
185
  
186
  setTaskScalarExtraInfo(*pTaskInfo);
349,598,764✔
187
  
188
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
349,592,821✔
189
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
349,578,772✔
190

191
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
349,448,713✔
192
    doDestroyTask(*pTaskInfo);
819,945✔
193
    (*pTaskInfo) = NULL;
470,340✔
194
  }
195
  return code;
349,275,253✔
196
}
197

198
void cleanupQueriedTableScanInfo(void* p) {
261,088,328✔
199
  SSchemaInfo* pSchemaInfo = p;
261,088,328✔
200

201
  taosMemoryFreeClear(pSchemaInfo->dbname);
261,088,328✔
202
  taosMemoryFreeClear(pSchemaInfo->tablename);
261,084,886✔
203
  tDeleteSchemaWrapper(pSchemaInfo->sw);
261,036,813✔
204
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
261,034,831✔
205
}
261,038,162✔
206

207
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
261,137,237✔
208
                                   SExecTaskInfo* pTaskInfo) {
209
  SMetaReader mr = {0};
261,137,237✔
210
  if (pHandle == NULL) {
261,144,514✔
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213

214
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
261,144,514✔
215

216
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
261,134,002✔
217
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
261,142,552✔
218
  if (code != TSDB_CODE_SUCCESS) {
260,871,278✔
UNCOV
219
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
220
           GET_TASKID(pTaskInfo));
221

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

226
  SSchemaInfo schemaInfo = {0};
260,871,278✔
227

228
  schemaInfo.tablename = taosStrdup(mr.me.name);
260,941,853✔
229
  schemaInfo.dbname = taosStrdup(dbName);
260,954,219✔
230
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
260,985,812✔
231
    pAPI->metaReaderFn.clearReader(&mr);
137,590✔
232
    cleanupQueriedTableScanInfo(&schemaInfo);
×
233
    return terrno;
×
234
  }
235

236
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
260,848,316✔
237
    schemaInfo.rversion = mr.me.colRef.version;
286,650✔
238
  }
239

240
  if (mr.me.type == TSDB_SUPER_TABLE) {
260,848,316✔
241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
115,842,607✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
115,842,607✔
243
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
145,059,477✔
244
    tDecoderClear(&mr.coder);
102,076,721✔
245

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

254
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
102,165,911✔
255
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
102,165,911✔
256
  } else {
257
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
42,986,065✔
258
  }
259

260
  pAPI->metaReaderFn.clearReader(&mr);
260,994,583✔
261

262
  if (schemaInfo.sw == NULL) {
260,857,069✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
260,857,069✔
268
  if (schemaInfo.qsw == NULL) {
260,953,586✔
269
    cleanupQueriedTableScanInfo(&schemaInfo);
×
270
    return terrno;
×
271
  }
272

273
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
260,953,586✔
274
  if (p == NULL) {
261,044,357✔
275
    cleanupQueriedTableScanInfo(&schemaInfo);
×
276
    return terrno;
×
277
  }
278

279
  return code;
261,044,357✔
280
}
281

282
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
260,915,695✔
283
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
260,915,695✔
284
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
261,073,289✔
285

286
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
261,005,985✔
287
  if (pqSw == NULL) {
260,862,537✔
288
    return NULL;
×
289
  }
290

291
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
260,862,537✔
292
  if (pqSw->pSchema == NULL) {
260,678,817✔
293
    taosMemoryFree(pqSw);
×
294
    return NULL;
×
295
  }
296

297
  for (int32_t i = 0; i < numOfCols; ++i) {
1,175,535,013✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
914,482,796✔
299
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
914,378,693✔
300

301
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
914,419,384✔
302
    pSchema->colId = pColNode->colId;
914,491,596✔
303
    pSchema->type = pColNode->node.resType.type;
914,607,467✔
304
    pSchema->bytes = pColNode->node.resType.bytes;
914,542,707✔
305
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
914,642,892✔
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) {
446,094,656✔
310
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
185,029,986✔
311

312
    int32_t type = nodeType(pNode->pExpr);
184,993,612✔
313
    if (type == QUERY_NODE_COLUMN) {
185,025,302✔
314
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
142,128,244✔
315

316
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
142,132,499✔
317
      pSchema->colId = pColNode->colId;
142,157,650✔
318
      pSchema->type = pColNode->node.resType.type;
142,087,659✔
319
      pSchema->bytes = pColNode->node.resType.bytes;
142,121,845✔
320
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
142,092,806✔
321
    }
322
  }
323

324
  return pqSw;
261,064,670✔
325
}
326

327
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
349,693,378✔
328
  tDeleteSchemaWrapper(pTmqInfo->schema);
349,693,378✔
329
  tOffsetDestroy(&pTmqInfo->currentOffset);
349,747,397✔
330
}
349,656,932✔
331

332
static void freeBlock(void* pParam) {
622,389,836✔
333
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
622,389,836✔
334
  blockDataDestroy(pBlock);
622,401,728✔
335
}
622,408,554✔
336

337

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

347
  if (pCtx->subEndPoints != NULL) {
38,876,017✔
348
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
38,874,392✔
349
    if (size > 0) {
38,874,370✔
350
      int32_t code = tsem_destroy(&pCtx->ready);
38,874,919✔
351
      if (code != TSDB_CODE_SUCCESS) {
38,872,185✔
352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
353
      }
354
      taosArrayDestroy(pCtx->subResNodes);
38,872,185✔
355
    }
356
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
38,871,087✔
357
    pCtx->subEndPoints = NULL;
38,875,468✔
358
  }
359
  
360
  taosMemoryFreeClear(pCtx);  
38,870,549✔
361
}
38,871,098✔
362

363
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
349,805,447✔
364
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
349,805,447✔
365
  destroyOperator(pTaskInfo->pRoot);
349,804,943✔
366
  pTaskInfo->pRoot = NULL;
349,706,678✔
367

368
  if (pTaskInfo->pSubJobCtx) {
349,724,039✔
369
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
38,877,137✔
370
    if (code != TSDB_CODE_SUCCESS) {
38,872,734✔
371
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
372
    }
373
  }
374

375
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
349,729,916✔
376
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
349,683,502✔
377

378
  if (!pTaskInfo->localFetch.localExec) {
349,648,924✔
379
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
349,667,876✔
380
    pTaskInfo->pSubplan = NULL;
349,717,867✔
381
  }
382

383
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
349,733,782✔
384
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
349,737,613✔
385
  if (!pTaskInfo->paramSet) {
349,740,989✔
386
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
341,515,995✔
387
    pTaskInfo->pOpParam = NULL;
341,497,410✔
388
  }
389
  taosMemoryFreeClear(pTaskInfo->sql);
349,712,263✔
390
  taosMemoryFreeClear(pTaskInfo->id.str);
349,706,407✔
391
  taosMemoryFreeClear(pTaskInfo);
349,713,934✔
392
}
349,688,698✔
393

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