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

taosdata / TDengine / #4937

23 Jan 2026 09:40AM UTC coverage: 66.794% (+0.05%) from 66.746%
#4937

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204171 of 305671 relevant lines covered (66.79%)

127025939.33 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

36
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
37

38
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
301,972,922✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
301,972,922✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
301,972,922✔
45
  if (p == NULL) {
301,918,073✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
301,918,073✔
50
  p->cost.created = taosGetTimestampUs();
302,026,982✔
51

52
  p->execModel = model;
302,006,559✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
301,999,204✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
301,972,648✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
301,937,929✔
56
    doDestroyTask(p);
15,478✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
301,955,625✔
61
  taosInitRWLatch(&p->lock);
301,962,221✔
62

63
  p->id.vgId = vgId;
301,991,422✔
64
  p->id.queryId = queryId;
301,999,757✔
65
  p->id.taskId = taskId;
302,007,782✔
66
  p->id.str = taosMemoryMalloc(64);
302,006,584✔
67
  if (p->id.str == NULL) {
301,916,614✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
301,977,604✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
301,938,756✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
301,871,770✔
75
    doDestroyTask(p);
199✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
301,934,043✔
80
  return TSDB_CODE_SUCCESS;
301,935,314✔
81
}
82

83
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
81,853✔
84

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,818,621,494✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
946,275,715✔
93
  if (status == TASK_NOT_COMPLETED) {
946,275,715✔
94
    pTaskInfo->status = status;
302,037,001✔
95
  } else {
96
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
97
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
644,238,714✔
98
    pTaskInfo->status |= status;
644,266,134✔
99
  }
100
}
946,322,567✔
101

102

103
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray* subEndPoints, SReadHandle* readHandle) {
301,754,784✔
104
  STaskSubJobCtx* ctx = &pTaskInfo->subJobCtx;
301,754,784✔
105

106
  ctx->queryId = pTaskInfo->id.queryId;
301,818,313✔
107
  ctx->taskId = pTaskInfo->id.taskId;
301,754,692✔
108
  ctx->idStr = pTaskInfo->id.str;
301,721,831✔
109
  ctx->pTaskInfo = pTaskInfo;
301,735,064✔
110
  ctx->subEndPoints = subEndPoints;
301,689,023✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
301,724,020✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
301,813,629✔
114
  if (subJobNum > 0) {
301,823,881✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
29,524,958✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
29,511,774✔
117
      qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
118
      return terrno;
×
119
    }
120
    
121
    int32_t code = tsem_init(&ctx->ready, 0, 0);
29,504,536✔
122
    if (code) {
29,491,202✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
29,491,202✔
128

129
    qDebug("%s subJobCtx with %d endPoints inited", pTaskInfo->id.str, subJobNum);
29,493,270✔
130
  }
131

132
  return TSDB_CODE_SUCCESS;
301,730,924✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
301,852,483✔
138
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
139
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
301,852,483✔
140
  if (*pTaskInfo == NULL || code != 0) {
301,776,683✔
141
    nodesDestroyNode((SNode*)pPlan);
×
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
301,811,247✔
146

147
  if (pHandle) {
301,807,035✔
148
    if (pHandle->pStateBackend) {
301,835,095✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
301,772,731✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
299,337,165✔
156
    if (NULL == (*pTaskInfo)->sql) {
299,297,877✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
301,768,331✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
301,742,624✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
301,774,509✔
168
  if (code != TSDB_CODE_SUCCESS) {
301,705,337✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
301,705,337✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
301,685,583✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
301,576,652✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
301,171,303✔
180
    doDestroyTask(*pTaskInfo);
2,801,719✔
181
    (*pTaskInfo) = NULL;
3,069,135✔
182
  }
183
  return code;
301,566,584✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
215,933,166✔
187
  SSchemaInfo* pSchemaInfo = p;
215,933,166✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
215,933,166✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
215,913,749✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
215,871,834✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
215,889,338✔
193
}
215,885,056✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
216,035,108✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
216,035,108✔
198
  if (pHandle == NULL) {
216,044,649✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
216,044,649✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
216,029,493✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
216,049,988✔
206
  if (code != TSDB_CODE_SUCCESS) {
215,573,556✔
207
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
208
           GET_TASKID(pTaskInfo));
209

210
    pAPI->metaReaderFn.clearReader(&mr);
×
211
    return code;
×
212
  }
213

214
  SSchemaInfo schemaInfo = {0};
215,573,556✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
215,639,789✔
217
  schemaInfo.dbname = taosStrdup(dbName);
215,661,755✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
215,748,598✔
219
    pAPI->metaReaderFn.clearReader(&mr);
314,802✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
215,433,796✔
225
    schemaInfo.rversion = mr.me.colRef.version;
275,719✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
215,433,796✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
127,630,219✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
127,630,219✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
87,910,053✔
232
    tDecoderClear(&mr.coder);
46,405,822✔
233

234
    tb_uid_t suid = mr.me.ctbEntry.suid;
46,541,583✔
235
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
46,541,583✔
236
    if (code != TSDB_CODE_SUCCESS) {
46,514,611✔
237
      pAPI->metaReaderFn.clearReader(&mr);
×
238
      cleanupQueriedTableScanInfo(&schemaInfo);
×
239
      return code;
×
240
    }
241

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
46,530,062✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
46,530,062✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
41,500,086✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
215,660,367✔
249

250
  if (schemaInfo.sw == NULL) {
215,541,239✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
215,541,239✔
256
  if (schemaInfo.qsw == NULL) {
215,637,130✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
215,637,130✔
262
  if (p == NULL) {
215,843,516✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
215,843,516✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
215,595,711✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
215,595,711✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
215,822,467✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
215,716,049✔
275
  if (pqSw == NULL) {
215,494,296✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
215,494,296✔
280
  if (pqSw->pSchema == NULL) {
215,241,892✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
1,023,303,477✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
807,448,476✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
807,719,254✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
807,833,245✔
290
    pSchema->colId = pColNode->colId;
807,467,027✔
291
    pSchema->type = pColNode->node.resType.type;
807,767,267✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
807,768,254✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
807,762,853✔
294
  }
295

296
  // this the tags and pseudo function columns, we only keep the tag columns
297
  for (int32_t i = 0; i < numOfTags; ++i) {
401,408,811✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
185,592,856✔
299

300
    int32_t type = nodeType(pNode->pExpr);
185,625,590✔
301
    if (type == QUERY_NODE_COLUMN) {
185,704,795✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
144,201,211✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
144,214,929✔
305
      pSchema->colId = pColNode->colId;
144,217,081✔
306
      pSchema->type = pColNode->node.resType.type;
144,047,252✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
144,174,628✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
144,064,379✔
309
    }
310
  }
311

312
  return pqSw;
215,815,955✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
301,852,048✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
301,852,048✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
301,893,046✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
301,650,839✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
301,853,669✔
320
}
301,822,662✔
321

322
static void freeBlock(void* pParam) {
474,577,257✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
474,577,257✔
324
  blockDataDestroy(pBlock);
474,599,663✔
325
}
474,620,406✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
301,931,655✔
329
  if (pCtx->transporterId > 0) {
301,931,655✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
59,294✔
331
    if (ret != 0) {
59,294✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
59,294✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
301,965,228✔
337
}
301,937,618✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
301,982,817✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
301,982,817✔
341
  destroyOperator(pTaskInfo->pRoot);
301,983,179✔
342
  pTaskInfo->pRoot = NULL;
301,914,241✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
301,900,033✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
301,893,671✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
301,860,642✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
301,809,123✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
301,821,231✔
351
    pTaskInfo->pSubplan = NULL;
301,869,077✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
301,882,278✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
301,922,799✔
356
  if (!pTaskInfo->paramSet) {
301,924,491✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
295,298,412✔
358
    pTaskInfo->pOpParam = NULL;
295,151,001✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
301,811,641✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
301,900,050✔
362
  taosMemoryFreeClear(pTaskInfo);
301,878,800✔
363
}
301,756,219✔
364

365
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
325,954,161✔
366
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
325,954,161✔
367
  if (ret < 0) {
325,954,161✔
368
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
369
  }
370
}
325,954,161✔
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