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

taosdata / TDengine / #4935

22 Jan 2026 06:38AM UTC coverage: 66.708% (+0.02%) from 66.691%
#4935

push

travis-ci

web-flow
merge: from main to 3.0 #34371

121 of 271 new or added lines in 17 files covered. (44.65%)

9066 existing lines in 149 files now uncovered.

203884 of 305637 relevant lines covered (66.71%)

125811266.68 hits per line

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

80.82
/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,
303,231,031✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
303,231,031✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
303,231,031✔
45
  if (p == NULL) {
303,144,565✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
303,144,565✔
50
  p->cost.created = taosGetTimestampUs();
303,214,562✔
51

52
  p->execModel = model;
303,266,865✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
303,206,988✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
303,244,772✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
303,150,362✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
303,219,336✔
61
  taosInitRWLatch(&p->lock);
303,208,773✔
62

63
  p->id.vgId = vgId;
303,242,169✔
64
  p->id.queryId = queryId;
303,246,920✔
65
  p->id.taskId = taskId;
303,248,902✔
66
  p->id.str = taosMemoryMalloc(64);
303,244,842✔
67
  if (p->id.str == NULL) {
303,159,441✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
303,233,499✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
303,176,403✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
303,103,491✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
303,182,681✔
80
  return TSDB_CODE_SUCCESS;
303,199,213✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,710,971,770✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
946,590,567✔
93
  if (status == TASK_NOT_COMPLETED) {
946,590,567✔
94
    pTaskInfo->status = status;
303,276,017✔
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);
643,314,550✔
98
    pTaskInfo->status |= status;
643,327,446✔
99
  }
100
}
946,626,256✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
303,063,044✔
107
  ctx->taskId = pTaskInfo->id.taskId;
302,980,443✔
108
  ctx->idStr = pTaskInfo->id.str;
302,983,316✔
109
  ctx->pTaskInfo = pTaskInfo;
302,993,855✔
110
  ctx->subEndPoints = subEndPoints;
302,911,493✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
302,951,673✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
303,060,399✔
114
  if (subJobNum > 0) {
303,074,231✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
35,696,633✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
35,679,093✔
117
      qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
UNCOV
118
      return terrno;
×
119
    }
120
    
121
    int32_t code = tsem_init(&ctx->ready, 0, 0);
35,677,943✔
122
    if (code) {
35,665,998✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
UNCOV
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
35,665,998✔
128

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

132
  return TSDB_CODE_SUCCESS;
302,981,079✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
303,101,443✔
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);
303,101,443✔
140
  if (*pTaskInfo == NULL || code != 0) {
303,005,765✔
141
    nodesDestroyNode((SNode*)pPlan);
8✔
UNCOV
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
303,024,887✔
146

147
  if (pHandle) {
303,041,522✔
148
    if (pHandle->pStateBackend) {
303,052,724✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
UNCOV
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
303,055,669✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
300,607,558✔
156
    if (NULL == (*pTaskInfo)->sql) {
300,527,900✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
UNCOV
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
302,946,839✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
302,964,063✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
303,035,187✔
168
  if (code != TSDB_CODE_SUCCESS) {
302,943,693✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
UNCOV
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
302,943,693✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
302,930,584✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
302,777,935✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
302,608,499✔
180
    doDestroyTask(*pTaskInfo);
3,737,393✔
181
    (*pTaskInfo) = NULL;
3,777,320✔
182
  }
183
  return code;
302,756,801✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
213,886,129✔
187
  SSchemaInfo* pSchemaInfo = p;
213,886,129✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
213,886,129✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
213,880,400✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
213,817,418✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
213,858,599✔
193
}
213,842,151✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
214,002,678✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
214,002,678✔
198
  if (pHandle == NULL) {
214,019,439✔
UNCOV
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
214,019,439✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
213,994,437✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
214,006,408✔
206
  if (code != TSDB_CODE_SUCCESS) {
213,411,099✔
UNCOV
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);
×
UNCOV
211
    return code;
×
212
  }
213

214
  SSchemaInfo schemaInfo = {0};
213,411,099✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
213,522,522✔
217
  schemaInfo.dbname = taosStrdup(dbName);
213,630,158✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
213,725,220✔
219
    pAPI->metaReaderFn.clearReader(&mr);
367,864✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
213,357,356✔
225
    schemaInfo.rversion = mr.me.colRef.version;
274,607✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
213,357,356✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
127,292,163✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
127,292,163✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
86,198,525✔
232
    tDecoderClear(&mr.coder);
45,795,468✔
233

234
    tb_uid_t suid = mr.me.ctbEntry.suid;
45,900,398✔
235
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
45,900,398✔
236
    if (code != TSDB_CODE_SUCCESS) {
45,888,856✔
237
      pAPI->metaReaderFn.clearReader(&mr);
×
238
      cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
239
      return code;
×
240
    }
241

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
45,892,714✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
45,892,714✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
40,400,426✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
213,585,303✔
249

250
  if (schemaInfo.sw == NULL) {
213,455,419✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
213,455,419✔
256
  if (schemaInfo.qsw == NULL) {
213,617,951✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
213,617,951✔
262
  if (p == NULL) {
213,706,204✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
264
    return terrno;
×
265
  }
266

267
  return code;
213,706,204✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
213,507,781✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
213,507,781✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
213,764,069✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
213,637,034✔
275
  if (pqSw == NULL) {
213,418,823✔
UNCOV
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
213,418,823✔
280
  if (pqSw->pSchema == NULL) {
213,103,852✔
281
    taosMemoryFree(pqSw);
×
UNCOV
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
1,008,996,875✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
795,185,103✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
795,403,244✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
795,518,526✔
290
    pSchema->colId = pColNode->colId;
795,284,713✔
291
    pSchema->type = pColNode->node.resType.type;
795,504,361✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
795,358,923✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
795,545,477✔
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) {
392,157,717✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
178,362,194✔
299

300
    int32_t type = nodeType(pNode->pExpr);
178,379,868✔
301
    if (type == QUERY_NODE_COLUMN) {
178,461,386✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
137,529,058✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
137,535,702✔
305
      pSchema->colId = pColNode->colId;
137,530,146✔
306
      pSchema->type = pColNode->node.resType.type;
137,461,387✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
137,480,015✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
137,445,524✔
309
    }
310
  }
311

312
  return pqSw;
213,795,523✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
303,105,253✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
303,105,253✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
303,159,644✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
302,944,654✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
303,124,003✔
320
}
303,016,201✔
321

322
static void freeBlock(void* pParam) {
436,387,204✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
436,387,204✔
324
  blockDataDestroy(pBlock);
436,408,393✔
325
}
436,424,430✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
303,185,453✔
329
  if (pCtx->transporterId > 0) {
303,185,453✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
66,884✔
331
    if (ret != 0) {
66,884✔
UNCOV
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
66,884✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
303,226,545✔
337
}
303,175,414✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
303,230,906✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
303,230,906✔
341
  destroyOperator(pTaskInfo->pRoot);
303,230,906✔
342
  pTaskInfo->pRoot = NULL;
303,129,125✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
303,155,600✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
303,135,327✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
303,097,219✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
303,011,647✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
303,038,498✔
351
    pTaskInfo->pSubplan = NULL;
303,121,246✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
303,117,553✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
303,164,299✔
356
  if (!pTaskInfo->paramSet) {
303,156,116✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
295,975,480✔
358
    pTaskInfo->pOpParam = NULL;
295,814,151✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
302,993,989✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
303,137,995✔
362
  taosMemoryFreeClear(pTaskInfo);
303,145,157✔
363
}
302,997,298✔
364

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