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

taosdata / TDengine / #4980

10 Mar 2026 08:57AM UTC coverage: 68.492% (-0.02%) from 68.512%
#4980

push

travis-ci

web-flow
fix: add retry while exec ci case test_stable_keep_compact.py. (#34729)

211901 of 309380 relevant lines covered (68.49%)

135171938.8 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,
341,024,121✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
341,024,121✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
341,024,121✔
46
  if (p == NULL) {
341,014,161✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
341,014,161✔
51
  p->cost.created = taosGetTimestampUs();
341,018,349✔
52

53
  p->execModel = model;
340,998,390✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
341,012,038✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
340,991,722✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
340,994,290✔
57
    doDestroyTask(p);
33,993✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
340,968,826✔
62
  taosInitRWLatch(&p->lock);
340,997,315✔
63

64
  p->id.vgId = vgId;
340,986,692✔
65
  p->id.queryId = queryId;
340,985,611✔
66
  p->id.taskId = taskId;
340,999,101✔
67
  p->id.str = taosMemoryMalloc(64);
340,992,279✔
68
  if (p->id.str == NULL) {
340,938,029✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
340,973,872✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
340,956,417✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
340,900,810✔
76
    doDestroyTask(p);
69✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
340,939,626✔
81
  return TSDB_CODE_SUCCESS;
340,932,262✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,823,481,985✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,076,957,021✔
94
  if (status == TASK_NOT_COMPLETED) {
1,076,957,021✔
95
    pTaskInfo->status = status;
341,191,334✔
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,765,687✔
99
    pTaskInfo->status |= status;
735,771,576✔
100
  }
101
}
1,076,970,972✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
37,404,588✔
105
  int32_t code = 0, lino = 0;
37,404,588✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
37,404,588✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
37,405,113✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
37,398,705✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
37,400,853✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
37,398,705✔
114
  ctx->taskId = pTaskInfo->id.taskId;
37,403,526✔
115
  ctx->idStr = pTaskInfo->id.str;
37,402,989✔
116
  ctx->pTaskInfo = pTaskInfo;
37,404,063✔
117
  ctx->subEndPoints = *subEndPoints;
37,399,242✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
37,399,767✔
119

120
  *subEndPoints = NULL;
37,409,934✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,409,397✔
123
  if (NULL == ctx->subResNodes) {
37,400,280✔
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,397,082✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
37,400,280✔
131
  if (refId < 0) {
37,404,600✔
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,404,600✔
137

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

140
_exit:
37,204,664✔
141

142
  if (code) {
37,396,032✔
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,393,371✔
150
}
151

152

153

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

162
  (*pTaskInfo)->pSubplan = pPlan;
340,818,410✔
163

164
  if (pHandle) {
340,823,615✔
165
    if (pHandle->pStateBackend) {
340,808,746✔
166
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
167
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
168
    }
169
  }
170

171
  if (NULL != sql) {
340,843,875✔
172
    (*pTaskInfo)->sql = taosStrdup(sql);
318,107,556✔
173
    if (NULL == (*pTaskInfo)->sql) {
318,135,790✔
174
      code = terrno;
×
175
      doDestroyTask(*pTaskInfo);
×
176
      (*pTaskInfo) = NULL;
×
177
      return code;
×
178
    }
179
  }
180

181
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
340,863,809✔
182
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
340,824,514✔
183

184
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
340,780,826✔
185
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
37,406,712✔
186
    if (code != TSDB_CODE_SUCCESS) {
37,388,538✔
187
      doDestroyTask(*pTaskInfo);
×
188
      (*pTaskInfo) = NULL;
×
189
      return code;
×
190
    }
191
  }
192
  
193
  setTaskScalarExtraInfo(*pTaskInfo);
340,729,929✔
194
  
195
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
340,715,647✔
196
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
340,737,417✔
197

198
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
340,682,766✔
199
    doDestroyTask(*pTaskInfo);
670,613✔
200
    (*pTaskInfo) = NULL;
459,086✔
201
  }
202
  return code;
340,464,199✔
203
}
204

205
void cleanupQueriedTableScanInfo(void* p) {
255,606,165✔
206
  SSchemaInfo* pSchemaInfo = p;
255,606,165✔
207

208
  taosMemoryFreeClear(pSchemaInfo->dbname);
255,606,165✔
209
  taosMemoryFreeClear(pSchemaInfo->tablename);
255,572,362✔
210
  tDeleteSchemaWrapper(pSchemaInfo->sw);
255,561,244✔
211
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
255,570,977✔
212
}
255,594,571✔
213

214
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
255,665,848✔
215
                                   SExecTaskInfo* pTaskInfo) {
216
  SMetaReader mr = {0};
255,665,848✔
217
  if (pHandle == NULL) {
255,668,041✔
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220

221
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
255,668,041✔
222

223
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
255,663,624✔
224
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
255,669,703✔
225
  if (code != TSDB_CODE_SUCCESS) {
255,385,734✔
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};
255,385,510✔
234

235
  schemaInfo.tablename = taosStrdup(mr.me.name);
255,436,827✔
236
  schemaInfo.dbname = taosStrdup(dbName);
255,484,091✔
237
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
255,548,661✔
238
    pAPI->metaReaderFn.clearReader(&mr);
147,537✔
239
    cleanupQueriedTableScanInfo(&schemaInfo);
×
240
    return terrno;
×
241
  }
242

243
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
255,401,125✔
244
    schemaInfo.rversion = mr.me.colRef.version;
282,062✔
245
  }
246

247
  if (mr.me.type == TSDB_SUPER_TABLE) {
255,401,125✔
248
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
115,154,943✔
249
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
115,154,943✔
250
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
140,313,365✔
251
    tDecoderClear(&mr.coder);
98,639,103✔
252

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

261
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
98,627,431✔
262
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
98,627,431✔
263
  } else {
264
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
41,703,507✔
265
  }
266

267
  pAPI->metaReaderFn.clearReader(&mr);
255,485,881✔
268

269
  if (schemaInfo.sw == NULL) {
255,483,787✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
271
    return terrno;
×
272
  }
273

274
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
255,483,787✔
275
  if (schemaInfo.qsw == NULL) {
255,472,203✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
277
    return terrno;
×
278
  }
279

280
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
255,472,203✔
281
  if (p == NULL) {
255,532,481✔
282
    cleanupQueriedTableScanInfo(&schemaInfo);
×
283
    return terrno;
×
284
  }
285

286
  return code;
255,532,481✔
287
}
288

289
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
255,480,929✔
290
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
255,480,929✔
291
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
255,574,448✔
292

293
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
255,392,489✔
294
  if (pqSw == NULL) {
255,330,260✔
295
    return NULL;
×
296
  }
297

298
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
255,330,260✔
299
  if (pqSw->pSchema == NULL) {
255,195,189✔
300
    taosMemoryFree(pqSw);
×
301
    return NULL;
×
302
  }
303

304
  for (int32_t i = 0; i < numOfCols; ++i) {
1,135,362,959✔
305
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
879,780,342✔
306
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
879,892,097✔
307

308
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
879,924,420✔
309
    pSchema->colId = pColNode->colId;
879,969,815✔
310
    pSchema->type = pColNode->node.resType.type;
880,037,376✔
311
    pSchema->bytes = pColNode->node.resType.bytes;
880,034,533✔
312
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
879,943,690✔
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) {
400,915,607✔
317
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
145,325,770✔
318

319
    int32_t type = nodeType(pNode->pExpr);
145,358,623✔
320
    if (type == QUERY_NODE_COLUMN) {
145,384,063✔
321
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
107,105,271✔
322

323
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
107,096,014✔
324
      pSchema->colId = pColNode->colId;
107,043,179✔
325
      pSchema->type = pColNode->node.resType.type;
107,048,649✔
326
      pSchema->bytes = pColNode->node.resType.bytes;
107,031,849✔
327
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
106,999,708✔
328
    }
329
  }
330

331
  return pqSw;
255,589,837✔
332
}
333

334
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
340,917,893✔
335
  tDeleteSchemaWrapper(pStreamInfo->schema);
340,917,893✔
336
  tOffsetDestroy(&pStreamInfo->currentOffset);
340,952,008✔
337
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
340,856,226✔
338
  taosMemoryFree(pStreamInfo->stbFullName);
340,957,268✔
339
}
340,888,407✔
340

341
static void freeBlock(void* pParam) {
653,308,628✔
342
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
653,308,628✔
343
  blockDataDestroy(pBlock);
653,323,427✔
344
}
653,326,276✔
345

346

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

356
  if (pCtx->subEndPoints != NULL) {
37,405,137✔
357
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
37,408,335✔
358
    if (size > 0) {
37,405,101✔
359
      int32_t code = tsem_destroy(&pCtx->ready);
37,405,101✔
360
      if (code != TSDB_CODE_SUCCESS) {
37,403,526✔
361
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
362
      }
363
      taosArrayDestroy(pCtx->subResNodes);
37,403,526✔
364
    }
365
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
37,399,230✔
366
    pCtx->subEndPoints = NULL;
37,399,755✔
367
  }
368
  
369
  taosMemoryFreeClear(pCtx);  
37,401,891✔
370
}
37,404,576✔
371

372
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
341,018,056✔
373
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
341,018,056✔
374
  destroyOperator(pTaskInfo->pRoot);
341,018,056✔
375
  pTaskInfo->pRoot = NULL;
340,931,338✔
376

377
  if (pTaskInfo->pSubJobCtx) {
340,945,626✔
378
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
37,404,075✔
379
    if (code != TSDB_CODE_SUCCESS) {
37,407,822✔
380
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
381
    }
382
  }
383

384
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
340,979,371✔
385
  cleanupStreamInfo(&pTaskInfo->streamInfo);
340,901,863✔
386

387
  if (!pTaskInfo->localFetch.localExec) {
340,893,792✔
388
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
340,895,754✔
389
    pTaskInfo->pSubplan = NULL;
340,962,018✔
390
  }
391

392
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
340,983,551✔
393
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
340,942,935✔
394
  if (!pTaskInfo->paramSet) {
340,969,930✔
395
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
332,973,470✔
396
    pTaskInfo->pOpParam = NULL;
332,937,544✔
397
  }
398
  taosMemoryFreeClear(pTaskInfo->sql);
340,978,552✔
399
  taosMemoryFreeClear(pTaskInfo->id.str);
340,924,888✔
400
  taosMemoryFreeClear(pTaskInfo);
340,921,305✔
401
}
340,937,385✔
402

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