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

taosdata / TDengine / #4918

08 Jan 2026 11:50AM UTC coverage: 65.916% (+0.5%) from 65.42%
#4918

push

travis-ci

web-flow
merge: from main to 3.0 branch #34215

16 of 22 new or added lines in 3 files covered. (72.73%)

788 existing lines in 116 files now uncovered.

204221 of 309822 relevant lines covered (65.92%)

126504701.11 hits per line

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

81.19
/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,
425,334,440✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
425,334,440✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
425,334,440✔
45
  if (p == NULL) {
424,981,596✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
424,981,596✔
50
  p->cost.created = taosGetTimestampUs();
425,451,751✔
51

52
  p->execModel = model;
425,422,111✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
425,435,918✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
425,216,575✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
425,134,412✔
56
    doDestroyTask(p);
43✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
425,289,065✔
61
  taosInitRWLatch(&p->lock);
425,412,613✔
62

63
  p->id.vgId = vgId;
425,311,415✔
64
  p->id.queryId = queryId;
425,284,049✔
65
  p->id.taskId = taskId;
425,341,205✔
66
  p->id.str = taosMemoryMalloc(64);
425,355,672✔
67
  if (p->id.str == NULL) {
425,091,120✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
425,263,604✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
425,199,891✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
424,958,951✔
75
    doDestroyTask(p);
78✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
425,250,929✔
80
  return TSDB_CODE_SUCCESS;
425,272,041✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
2,028,498,855✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,294,999,974✔
93
  if (status == TASK_NOT_COMPLETED) {
1,294,999,974✔
94
    pTaskInfo->status = status;
425,199,059✔
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);
869,800,915✔
98
    pTaskInfo->status |= status;
869,882,820✔
99
  }
100
}
1,295,267,037✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
425,316,402✔
107
  ctx->idStr = pTaskInfo->id.str;
425,191,531✔
108
  ctx->pTaskInfo = pTaskInfo;
425,147,794✔
109
  ctx->subEndPoints = subEndPoints;
425,325,037✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
424,925,783✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
425,252,095✔
113
  if (subJobNum > 0) {
425,206,206✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
127,963,744✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
127,953,974✔
116
      qError("%s taosArrayInit_s %d subJobValues failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
117
      return terrno;
×
118
    }
119
    
120
    int32_t code = tsem_init(&ctx->ready, 0, 0);
127,811,906✔
121
    if (code) {
127,776,983✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
127,776,983✔
127

128
    qDebug("%s subJobCtx with %d endPoints inited", pTaskInfo->id.str, subJobNum);
127,952,288✔
129
  }
130

131
  return TSDB_CODE_SUCCESS;
425,104,914✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
425,221,888✔
137
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
138
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
425,221,888✔
139
  if (*pTaskInfo == NULL || code != 0) {
425,104,660✔
UNCOV
140
    nodesDestroyNode((SNode*)pPlan);
×
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
425,162,307✔
145

146
  if (pHandle) {
425,186,916✔
147
    if (pHandle->pStateBackend) {
424,961,053✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
425,253,074✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
422,736,467✔
155
    if (NULL == (*pTaskInfo)->sql) {
422,721,529✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
425,347,826✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
425,161,851✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
425,242,159✔
167
  if (code != TSDB_CODE_SUCCESS) {
424,950,827✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
424,950,827✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
425,081,424✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
425,091,685✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
424,877,612✔
179
    doDestroyTask(*pTaskInfo);
12,677,955✔
180
    (*pTaskInfo) = NULL;
12,638,393✔
181
  }
182
  return code;
424,967,044✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
329,239,414✔
186
  SSchemaInfo* pSchemaInfo = p;
329,239,414✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
329,239,414✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
329,214,020✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
329,184,152✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
329,223,912✔
192
}
329,206,283✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
329,255,584✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
329,255,584✔
197
  if (pHandle == NULL) {
329,268,323✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
329,268,323✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
329,242,768✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
329,271,448✔
205
  if (code != TSDB_CODE_SUCCESS) {
328,764,343✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
328,764,343✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
328,875,272✔
216
  schemaInfo.dbname = taosStrdup(dbName);
328,734,085✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
328,840,996✔
218
    pAPI->metaReaderFn.clearReader(&mr);
299,969✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
328,541,027✔
224
    schemaInfo.rversion = mr.me.colRef.version;
1,836✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
328,541,027✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
145,603,289✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
145,603,289✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
182,936,326✔
231
    tDecoderClear(&mr.coder);
121,138,240✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
121,400,730✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
121,400,730✔
235
    if (code != TSDB_CODE_SUCCESS) {
121,393,961✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
121,428,873✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
121,428,873✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
61,794,747✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
328,826,909✔
248

249
  if (schemaInfo.sw == NULL) {
328,435,962✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
328,435,962✔
255
  if (schemaInfo.qsw == NULL) {
328,829,723✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
328,829,723✔
261
  if (p == NULL) {
329,044,636✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
329,044,636✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
328,754,432✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
328,754,432✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
329,014,055✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
328,950,673✔
274
  if (pqSw == NULL) {
328,498,617✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
328,498,617✔
279
  if (pqSw->pSchema == NULL) {
328,096,102✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,289,086,380✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
960,018,037✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
960,060,447✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
960,233,834✔
289
    pSchema->colId = pColNode->colId;
960,064,170✔
290
    pSchema->type = pColNode->node.resType.type;
960,585,461✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
960,542,492✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
960,642,015✔
293
  }
294

295
  // this the tags and pseudo function columns, we only keep the tag columns
296
  for (int32_t i = 0; i < numOfTags; ++i) {
495,305,227✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
166,251,561✔
298

299
    int32_t type = nodeType(pNode->pExpr);
166,218,876✔
300
    if (type == QUERY_NODE_COLUMN) {
166,236,662✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
127,697,163✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
127,696,433✔
304
      pSchema->colId = pColNode->colId;
127,714,849✔
305
      pSchema->type = pColNode->node.resType.type;
127,645,573✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
127,691,746✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
127,649,115✔
308
    }
309
  }
310

311
  return pqSw;
329,053,666✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
425,420,452✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
425,420,452✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
425,459,009✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
425,413,034✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
425,446,469✔
319
}
425,473,455✔
320

321
static void freeBlock(void* pParam) {
560,878,069✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
560,878,069✔
323
  blockDataDestroy(pBlock);
560,885,202✔
324
}
560,850,565✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
425,457,007✔
328
  if (pCtx->transporterId > 0) {
425,457,007✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
142,071✔
330
    if (ret != 0) {
142,071✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
142,071✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
425,497,947✔
336
}
425,482,399✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
425,403,534✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
425,403,534✔
340
  destroyOperator(pTaskInfo->pRoot);
425,402,543✔
341
  pTaskInfo->pRoot = NULL;
425,452,318✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
425,486,332✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
425,456,452✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
425,423,133✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
425,472,707✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
425,438,002✔
350
    pTaskInfo->pSubplan = NULL;
425,437,325✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
425,482,864✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
425,449,637✔
355
  if (!pTaskInfo->paramSet) {
425,427,843✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
423,479,393✔
357
    pTaskInfo->pOpParam = NULL;
423,463,612✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
425,442,927✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
425,409,384✔
361
  taosMemoryFreeClear(pTaskInfo);
425,401,944✔
362
}
425,360,385✔
363

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