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

taosdata / TDengine / #4950

06 Feb 2026 07:29AM UTC coverage: 66.849% (-0.1%) from 66.973%
#4950

push

travis-ci

web-flow
merge: from main to 3.0 #34521

759 of 1081 new or added lines in 28 files covered. (70.21%)

1144 existing lines in 130 files now uncovered.

205692 of 307696 relevant lines covered (66.85%)

127112954.87 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,
269,293,911✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
269,293,911✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
269,293,911✔
45
  if (p == NULL) {
269,257,352✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
269,257,352✔
50
  p->cost.created = taosGetTimestampUs();
269,310,266✔
51

52
  p->execModel = model;
269,296,911✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
269,300,939✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
269,267,351✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
269,273,966✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
269,285,786✔
61
  taosInitRWLatch(&p->lock);
269,300,133✔
62

63
  p->id.vgId = vgId;
269,282,919✔
64
  p->id.queryId = queryId;
269,275,714✔
65
  p->id.taskId = taskId;
269,287,527✔
66
  p->id.str = taosMemoryMalloc(64);
269,287,007✔
67
  if (p->id.str == NULL) {
269,247,425✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
269,262,938✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
269,256,930✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
269,235,882✔
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
269,267,339✔
80
  return TSDB_CODE_SUCCESS;
269,267,952✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,693,549,177✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
860,603,828✔
93
  if (status == TASK_NOT_COMPLETED) {
860,603,828✔
94
    pTaskInfo->status = status;
269,441,455✔
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);
591,162,373✔
98
    pTaskInfo->status |= status;
591,175,246✔
99
  }
100
}
860,627,003✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
269,158,828✔
107
  ctx->taskId = pTaskInfo->id.taskId;
269,121,719✔
108
  ctx->idStr = pTaskInfo->id.str;
269,116,977✔
109
  ctx->pTaskInfo = pTaskInfo;
269,123,860✔
110
  ctx->subEndPoints = subEndPoints;
269,151,661✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
269,069,331✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
269,167,013✔
114
  if (subJobNum > 0) {
269,148,468✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
4,807,504✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
4,807,504✔
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);
4,806,987✔
122
    if (code) {
4,805,926✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
4,805,926✔
128

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

132
  return TSDB_CODE_SUCCESS;
269,123,394✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
269,171,537✔
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);
269,171,537✔
140
  if (*pTaskInfo == NULL || code != 0) {
269,140,628✔
141
    nodesDestroyNode((SNode*)pPlan);
6,948✔
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
269,141,885✔
146

147
  if (pHandle) {
269,156,639✔
148
    if (pHandle->pStateBackend) {
269,142,109✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
269,159,287✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
266,504,348✔
156
    if (NULL == (*pTaskInfo)->sql) {
266,531,322✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
269,177,604✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
269,152,880✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
269,101,577✔
168
  if (code != TSDB_CODE_SUCCESS) {
269,112,015✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
269,112,015✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
269,106,250✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
269,054,943✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
268,971,824✔
180
    doDestroyTask(*pTaskInfo);
620,107✔
181
    (*pTaskInfo) = NULL;
446,864✔
182
  }
183
  return code;
268,849,762✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
188,598,090✔
187
  SSchemaInfo* pSchemaInfo = p;
188,598,090✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
188,598,090✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
188,565,632✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
188,525,647✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
188,531,415✔
193
}
188,511,794✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
188,624,105✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
188,624,105✔
198
  if (pHandle == NULL) {
188,632,630✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
188,632,630✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
188,624,323✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
188,635,548✔
206
  if (code != TSDB_CODE_SUCCESS) {
188,505,952✔
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};
188,505,952✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
188,485,742✔
217
  schemaInfo.dbname = taosStrdup(dbName);
188,468,790✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
188,500,030✔
219
    pAPI->metaReaderFn.clearReader(&mr);
106,508✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
188,393,522✔
225
    schemaInfo.rversion = mr.me.colRef.version;
243,576✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
188,393,522✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
110,302,065✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
110,302,065✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
78,176,046✔
232
    tDecoderClear(&mr.coder);
37,269,412✔
233

234
    tb_uid_t suid = mr.me.ctbEntry.suid;
37,274,138✔
235
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
37,274,138✔
236
    if (code != TSDB_CODE_SUCCESS) {
37,271,493✔
237
      pAPI->metaReaderFn.clearReader(&mr);
×
238
      cleanupQueriedTableScanInfo(&schemaInfo);
×
239
      return code;
×
240
    }
241

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
37,272,959✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
37,272,959✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
40,930,284✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
188,505,308✔
249

250
  if (schemaInfo.sw == NULL) {
188,344,771✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
188,344,771✔
256
  if (schemaInfo.qsw == NULL) {
188,515,249✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
188,515,249✔
262
  if (p == NULL) {
188,560,852✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
188,560,852✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
188,461,689✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
188,461,689✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
188,517,143✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
188,449,780✔
275
  if (pqSw == NULL) {
188,382,294✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
188,382,294✔
280
  if (pqSw->pSchema == NULL) {
188,264,119✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
947,462,240✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
758,900,966✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
758,755,469✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
758,759,590✔
290
    pSchema->colId = pColNode->colId;
758,910,588✔
291
    pSchema->type = pColNode->node.resType.type;
759,038,573✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
758,978,457✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
758,986,790✔
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) {
344,832,414✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
156,297,556✔
299

300
    int32_t type = nodeType(pNode->pExpr);
156,303,432✔
301
    if (type == QUERY_NODE_COLUMN) {
156,323,912✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
117,564,680✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
117,547,864✔
305
      pSchema->colId = pColNode->colId;
117,501,051✔
306
      pSchema->type = pColNode->node.resType.type;
117,571,729✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
117,506,342✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
117,555,945✔
309
    }
310
  }
311

312
  return pqSw;
188,534,858✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
269,194,574✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
269,194,574✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
269,264,241✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
269,165,704✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
269,256,800✔
320
}
269,186,869✔
321

322
static void freeBlock(void* pParam) {
419,510,652✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
419,510,652✔
324
  blockDataDestroy(pBlock);
419,522,763✔
325
}
419,516,427✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
269,261,210✔
329
  if (pCtx->transporterId > 0) {
269,261,210✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
15,254✔
331
    if (ret != 0) {
15,254✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
15,254✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
269,273,620✔
337
}
269,274,460✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
269,302,045✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
269,302,045✔
341
  destroyOperator(pTaskInfo->pRoot);
269,302,045✔
342
  pTaskInfo->pRoot = NULL;
269,219,980✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
269,235,519✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
269,273,866✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
269,196,448✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
269,215,648✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
269,226,376✔
351
    pTaskInfo->pSubplan = NULL;
269,226,257✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
269,238,684✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
269,245,114✔
356
  if (!pTaskInfo->paramSet) {
269,223,678✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
262,416,205✔
358
    pTaskInfo->pOpParam = NULL;
262,374,976✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
269,239,028✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
269,220,551✔
362
  taosMemoryFreeClear(pTaskInfo);
269,210,811✔
363
}
269,147,424✔
364

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