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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

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

81.65
/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,
351,193,247✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
351,193,247✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
351,193,247✔
45
  if (p == NULL) {
350,950,028✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
350,950,028✔
50
  p->cost.created = taosGetTimestampUs();
351,259,841✔
51

52
  p->execModel = model;
351,233,096✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
351,218,286✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
351,104,222✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
351,088,123✔
56
    doDestroyTask(p);
8✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
351,209,739✔
61
  taosInitRWLatch(&p->lock);
351,205,168✔
62

63
  p->id.vgId = vgId;
351,151,955✔
64
  p->id.queryId = queryId;
351,172,651✔
65
  p->id.taskId = taskId;
351,151,042✔
66
  p->id.str = taosMemoryMalloc(64);
351,174,437✔
67
  if (p->id.str == NULL) {
350,924,137✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
351,139,572✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
351,070,305✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
350,974,772✔
75
    doDestroyTask(p);
111✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
351,163,910✔
80
  return TSDB_CODE_SUCCESS;
351,162,005✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,741,477,962✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,074,866,832✔
93
  if (status == TASK_NOT_COMPLETED) {
1,074,866,832✔
94
    pTaskInfo->status = status;
351,107,144✔
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);
723,759,688✔
98
    pTaskInfo->status |= status;
723,793,249✔
99
  }
100
}
1,074,957,690✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
351,127,488✔
107
  ctx->idStr = pTaskInfo->id.str;
351,024,040✔
108
  ctx->pTaskInfo = pTaskInfo;
351,022,484✔
109
  ctx->subEndPoints = subEndPoints;
350,979,307✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
350,958,903✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
351,076,906✔
113
  if (subJobNum > 0) {
351,024,876✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
89,329,185✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
89,237,224✔
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);
89,232,518✔
121
    if (code) {
89,267,673✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
89,267,673✔
127

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

131
  return TSDB_CODE_SUCCESS;
350,910,745✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
351,078,150✔
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);
351,078,150✔
139
  if (*pTaskInfo == NULL || code != 0) {
350,968,003✔
140
    nodesDestroyNode((SNode*)pPlan);
92✔
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
351,035,492✔
145

146
  if (pHandle) {
351,059,293✔
147
    if (pHandle->pStateBackend) {
350,886,744✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
351,184,387✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
348,530,485✔
155
    if (NULL == (*pTaskInfo)->sql) {
348,595,244✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
351,199,363✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
351,102,554✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
350,857,482✔
167
  if (code != TSDB_CODE_SUCCESS) {
350,915,868✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
350,915,868✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
350,846,095✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
350,707,690✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
350,672,554✔
179
    doDestroyTask(*pTaskInfo);
7,642,586✔
180
    (*pTaskInfo) = NULL;
7,551,832✔
181
  }
182
  return code;
350,737,105✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
269,348,633✔
186
  SSchemaInfo* pSchemaInfo = p;
269,348,633✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
269,348,633✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
269,322,803✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
269,301,576✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
269,295,623✔
192
}
269,304,087✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
269,350,090✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
269,350,090✔
197
  if (pHandle == NULL) {
269,360,043✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
269,360,043✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
269,342,503✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
269,336,006✔
205
  if (code != TSDB_CODE_SUCCESS) {
269,071,361✔
UNCOV
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
269,071,361✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
269,070,475✔
216
  schemaInfo.dbname = taosStrdup(dbName);
269,093,134✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
269,107,493✔
218
    pAPI->metaReaderFn.clearReader(&mr);
205,809✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
268,901,714✔
224
    schemaInfo.rversion = mr.me.colRef.version;
1,827✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
268,901,714✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
123,394,297✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
123,394,297✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
145,669,003✔
231
    tDecoderClear(&mr.coder);
85,267,774✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
85,183,815✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
85,183,815✔
235
    if (code != TSDB_CODE_SUCCESS) {
85,189,785✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
85,169,938✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
85,169,938✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
60,410,542✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
268,974,777✔
248

249
  if (schemaInfo.sw == NULL) {
268,688,393✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
268,688,393✔
255
  if (schemaInfo.qsw == NULL) {
269,093,800✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
269,093,800✔
261
  if (p == NULL) {
269,115,662✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
269,115,662✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
268,884,477✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
268,884,477✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
269,082,150✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
268,929,175✔
274
  if (pqSw == NULL) {
268,713,086✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
268,713,086✔
279
  if (pqSw->pSchema == NULL) {
268,296,999✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,122,545,405✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
853,366,680✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
853,678,591✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
853,712,223✔
289
    pSchema->colId = pColNode->colId;
853,449,930✔
290
    pSchema->type = pColNode->node.resType.type;
853,789,613✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
853,750,272✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
853,667,862✔
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) {
403,290,972✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
134,104,401✔
298

299
    int32_t type = nodeType(pNode->pExpr);
134,168,500✔
300
    if (type == QUERY_NODE_COLUMN) {
134,183,648✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
99,234,829✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
99,221,462✔
304
      pSchema->colId = pColNode->colId;
99,207,390✔
305
      pSchema->type = pColNode->node.resType.type;
99,222,409✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
99,176,788✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
99,205,043✔
308
    }
309
  }
310

311
  return pqSw;
269,186,571✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
351,253,234✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
351,253,234✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
351,291,305✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
351,251,015✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
351,286,652✔
319
}
351,241,915✔
320

321
static void freeBlock(void* pParam) {
455,486,043✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
455,486,043✔
323
  blockDataDestroy(pBlock);
455,502,143✔
324
}
455,503,894✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
351,292,841✔
328
  if (pCtx->transporterId > 0) {
351,292,841✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
81,905✔
330
    if (ret != 0) {
81,905✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
81,905✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
351,304,391✔
336
}
351,284,698✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
351,213,060✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
351,213,060✔
340
  destroyOperator(pTaskInfo->pRoot);
351,212,698✔
341
  pTaskInfo->pRoot = NULL;
351,273,149✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
351,290,363✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
351,286,064✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
351,246,053✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
351,266,599✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
351,267,781✔
350
    pTaskInfo->pSubplan = NULL;
351,262,536✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
351,281,489✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
351,266,653✔
355
  if (!pTaskInfo->paramSet) {
351,256,496✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
350,873,393✔
357
    pTaskInfo->pOpParam = NULL;
350,833,568✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
351,250,454✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
351,247,200✔
361
  taosMemoryFreeClear(pTaskInfo);
351,231,514✔
362
}
351,198,964✔
363

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