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

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

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

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

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
349,972,330✔
46
  if (p == NULL) {
349,974,035✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
349,974,035✔
51
  p->cost.created = taosGetTimestampUs();
349,987,965✔
52

53
  p->execModel = model;
349,970,385✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
349,977,631✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
349,952,249✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
349,964,784✔
57
    doDestroyTask(p);
27,730✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
349,945,254✔
62
  taosInitRWLatch(&p->lock);
349,972,104✔
63

64
  p->id.vgId = vgId;
349,955,920✔
65
  p->id.queryId = queryId;
349,959,597✔
66
  p->id.taskId = taskId;
349,967,886✔
67
  p->id.str = taosMemoryMalloc(64);
349,975,294✔
68
  if (p->id.str == NULL) {
349,908,362✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
349,946,212✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
349,937,347✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
349,893,817✔
76
    doDestroyTask(p);
761✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
349,916,947✔
81
  return TSDB_CODE_SUCCESS;
349,917,684✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,815,726,730✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,104,033,974✔
94
  if (status == TASK_NOT_COMPLETED) {
1,104,033,974✔
95
    pTaskInfo->status = status;
350,161,515✔
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,872,459✔
99
    pTaskInfo->status |= status;
753,890,231✔
100
  }
101
}
1,104,046,654✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
38,547,171✔
105
  int32_t code = 0, lino = 0;
38,547,171✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
38,547,171✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
38,548,267✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
38,543,883✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
38,545,539✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
38,547,719✔
114
  ctx->taskId = pTaskInfo->id.taskId;
38,544,443✔
115
  ctx->idStr = pTaskInfo->id.str;
38,546,075✔
116
  ctx->pTaskInfo = pTaskInfo;
38,547,183✔
117
  ctx->subEndPoints = *subEndPoints;
38,546,087✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
38,543,335✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
38,548,803✔
120

121
  *subEndPoints = NULL;
38,546,623✔
122
  
123
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
38,545,539✔
124
  if (NULL == ctx->subResNodes) {
38,545,527✔
125
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
UNCOV
126
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
127
  }
128
  
129
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
38,545,003✔
130

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

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

141
_exit:
38,278,640✔
142

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

150
  return code;
38,543,347✔
151
}
152

153

154

155
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
349,845,743✔
156
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
157
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
349,845,743✔
158
  if (*pTaskInfo == NULL || code != 0) {
349,778,730✔
159
    nodesDestroyNode((SNode*)pPlan);
160✔
UNCOV
160
    return code;
×
161
  }
162

163
  (*pTaskInfo)->pSubplan = pPlan;
349,794,733✔
164

165
  if (NULL != sql) {
349,798,060✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
326,324,502✔
167
    if (NULL == (*pTaskInfo)->sql) {
326,343,603✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
UNCOV
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
349,790,288✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
349,818,660✔
177

178
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
349,739,190✔
179
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
38,549,363✔
180
    if (code != TSDB_CODE_SUCCESS) {
38,543,359✔
181
      doDestroyTask(*pTaskInfo);
×
182
      (*pTaskInfo) = NULL;
×
UNCOV
183
      return code;
×
184
    }
185
  }
186
  
187
  setTaskScalarExtraInfo(*pTaskInfo);
349,731,520✔
188
  
189
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
349,754,657✔
190
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
349,756,509✔
191

192
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
349,643,676✔
193
    doDestroyTask(*pTaskInfo);
580,094✔
194
    (*pTaskInfo) = NULL;
469,362✔
195
  }
196
  return code;
349,463,874✔
197
}
198

199
void cleanupQueriedTableScanInfo(void* p) {
260,858,452✔
200
  SSchemaInfo* pSchemaInfo = p;
260,858,452✔
201

202
  taosMemoryFreeClear(pSchemaInfo->dbname);
260,858,452✔
203
  taosMemoryFreeClear(pSchemaInfo->tablename);
260,808,254✔
204
  tDeleteSchemaWrapper(pSchemaInfo->sw);
260,803,577✔
205
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
260,837,735✔
206
}
260,831,661✔
207

208
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
260,903,355✔
209
                                   SExecTaskInfo* pTaskInfo) {
210
  SMetaReader mr = {0};
260,903,355✔
211
  if (pHandle == NULL) {
260,907,701✔
UNCOV
212
    return TSDB_CODE_INVALID_PARA;
×
213
  }
214

215
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
260,907,701✔
216

217
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
260,894,092✔
218
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
260,909,164✔
219
  if (code != TSDB_CODE_SUCCESS) {
260,634,607✔
UNCOV
220
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
221
           GET_TASKID(pTaskInfo));
222

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

227
  SSchemaInfo schemaInfo = {0};
260,634,607✔
228

229
  schemaInfo.tablename = taosStrdup(mr.me.name);
260,688,795✔
230
  schemaInfo.dbname = taosStrdup(dbName);
260,731,896✔
231
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
260,773,583✔
232
    pAPI->metaReaderFn.clearReader(&mr);
127,186✔
233
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
234
    return terrno;
×
235
  }
236

237
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
260,646,397✔
238
    schemaInfo.rversion = mr.me.colRef.version;
289,768✔
239
  }
240

241
  if (mr.me.type == TSDB_SUPER_TABLE) {
260,646,397✔
242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
116,000,633✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
116,000,633✔
244
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
144,717,023✔
245
    tDecoderClear(&mr.coder);
101,447,162✔
246

247
    tb_uid_t suid = mr.me.ctbEntry.suid;
101,449,591✔
248
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
101,449,591✔
249
    if (code != TSDB_CODE_SUCCESS) {
101,446,937✔
250
      pAPI->metaReaderFn.clearReader(&mr);
×
251
      cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
252
      return code;
×
253
    }
254

255
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
101,448,925✔
256
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
101,448,925✔
257
  } else {
258
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
43,299,726✔
259
  }
260

261
  pAPI->metaReaderFn.clearReader(&mr);
260,749,284✔
262

263
  if (schemaInfo.sw == NULL) {
260,631,008✔
264
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
265
    return terrno;
×
266
  }
267

268
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
260,631,008✔
269
  if (schemaInfo.qsw == NULL) {
260,735,943✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
271
    return terrno;
×
272
  }
273

274
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
260,735,943✔
275
  if (p == NULL) {
260,811,559✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
277
    return terrno;
×
278
  }
279

280
  return code;
260,811,559✔
281
}
282

283
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
260,747,515✔
284
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
260,747,515✔
285
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
260,801,031✔
286

287
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
260,615,925✔
288
  if (pqSw == NULL) {
260,604,884✔
UNCOV
289
    return NULL;
×
290
  }
291

292
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
260,604,884✔
293
  if (pqSw->pSchema == NULL) {
260,524,754✔
294
    taosMemoryFree(pqSw);
×
UNCOV
295
    return NULL;
×
296
  }
297

298
  for (int32_t i = 0; i < numOfCols; ++i) {
1,177,642,631✔
299
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
916,810,888✔
300
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
916,872,626✔
301

302
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
916,890,425✔
303
    pSchema->colId = pColNode->colId;
916,896,937✔
304
    pSchema->type = pColNode->node.resType.type;
916,947,236✔
305
    pSchema->bytes = pColNode->node.resType.bytes;
916,935,582✔
306
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
916,905,861✔
307
  }
308

309
  // this the tags and pseudo function columns, we only keep the tag columns
310
  for (int32_t i = 0; i < numOfTags; ++i) {
445,332,873✔
311
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
184,512,014✔
312

313
    int32_t type = nodeType(pNode->pExpr);
184,570,388✔
314
    if (type == QUERY_NODE_COLUMN) {
184,625,431✔
315
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
141,375,050✔
316

317
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
141,359,998✔
318
      pSchema->colId = pColNode->colId;
141,305,367✔
319
      pSchema->type = pColNode->node.resType.type;
141,316,889✔
320
      pSchema->bytes = pColNode->node.resType.bytes;
141,312,827✔
321
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
141,259,815✔
322
    }
323
  }
324

325
  return pqSw;
260,820,859✔
326
}
327

328
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
349,905,131✔
329
  tDeleteSchemaWrapper(pTmqInfo->schema);
349,905,131✔
330
  tOffsetDestroy(&pTmqInfo->currentOffset);
349,929,976✔
331
}
349,778,442✔
332

333
static void freeBlock(void* pParam) {
618,893,013✔
334
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
618,893,013✔
335
  blockDataDestroy(pBlock);
618,903,253✔
336
}
618,920,549✔
337

338

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

348
  if (pCtx->subEndPoints != NULL) {
38,548,289✔
349
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
38,548,255✔
350
    if (size > 0) {
38,549,909✔
351
      int32_t code = tsem_destroy(&pCtx->ready);
38,550,457✔
352
      if (code != TSDB_CODE_SUCCESS) {
38,549,899✔
UNCOV
353
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
354
      }
355
      taosArrayDestroy(pCtx->subResNodes);
38,549,899✔
356
    }
357
    if (pCtx->isStream) {
38,548,277✔
358
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
49,380✔
359
    } else {
360
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
38,497,813✔
361
    }
362
    pCtx->subEndPoints = NULL;
38,543,929✔
363
  }
364
  
365
  taosMemoryFreeClear(pCtx);  
38,546,645✔
366
}
38,547,743✔
367

368
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
349,978,331✔
369
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
349,978,331✔
370
  destroyOperator(pTaskInfo->pRoot);
349,978,331✔
371
  pTaskInfo->pRoot = NULL;
349,902,963✔
372

373
  if (pTaskInfo->pSubJobCtx) {
349,915,423✔
374
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
38,548,815✔
375
    if (code != TSDB_CODE_SUCCESS) {
38,547,183✔
376
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
377
    }
378
  }
379

380
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
349,939,773✔
381
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
349,893,778✔
382

383
  if (!pTaskInfo->localFetch.localExec) {
349,769,978✔
384
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
349,812,600✔
385
    pTaskInfo->pSubplan = NULL;
349,928,360✔
386
  }
387

388
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
349,962,564✔
389
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
349,912,838✔
390
  if (!pTaskInfo->paramSet) {
349,925,706✔
391
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
341,693,795✔
392
    pTaskInfo->pOpParam = NULL;
341,612,799✔
393
  }
394
  taosMemoryFreeClear(pTaskInfo->sql);
349,862,861✔
395
  taosMemoryFreeClear(pTaskInfo->id.str);
349,883,584✔
396
  taosMemoryFreeClear(pTaskInfo);
349,877,664✔
397
}
349,882,493✔
398

399
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
374,595,846✔
400
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
374,595,846✔
401
  if (ret < 0) {
374,595,846✔
UNCOV
402
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
403
  }
404
}
374,595,846✔
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