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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

82.57
/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,
354,221,179✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
354,221,179✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
354,221,179✔
45
  if (p == NULL) {
353,958,383✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
353,958,383✔
50
  p->cost.created = taosGetTimestampUs();
354,313,556✔
51

52
  p->execModel = model;
354,300,826✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
354,267,786✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
354,177,714✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
354,163,712✔
56
    doDestroyTask(p);
157✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
354,247,689✔
61
  taosInitRWLatch(&p->lock);
354,267,904✔
62

63
  p->id.vgId = vgId;
354,237,291✔
64
  p->id.queryId = queryId;
354,232,403✔
65
  p->id.taskId = taskId;
354,257,382✔
66
  p->id.str = taosMemoryMalloc(64);
354,274,668✔
67
  if (p->id.str == NULL) {
354,004,645✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
354,166,626✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
354,104,533✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
353,957,060✔
75
    doDestroyTask(p);
394✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
354,070,502✔
80
  return TSDB_CODE_SUCCESS;
354,154,800✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,810,578,769✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,081,367,498✔
93
  if (status == TASK_NOT_COMPLETED) {
1,081,367,498✔
94
    pTaskInfo->status = status;
354,149,521✔
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);
727,217,977✔
98
    pTaskInfo->status |= status;
727,224,624✔
99
  }
100
}
1,081,425,399✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
354,045,175✔
107
  ctx->idStr = pTaskInfo->id.str;
354,065,904✔
108
  ctx->pTaskInfo = pTaskInfo;
353,992,194✔
109
  ctx->subEndPoints = subEndPoints;
354,084,029✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
353,945,785✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
354,170,264✔
113
  if (subJobNum > 0) {
354,142,353✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
86,386,515✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
86,269,970✔
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);
86,161,334✔
121
    if (code) {
86,172,930✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
86,172,930✔
127

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

131
  return TSDB_CODE_SUCCESS;
353,831,079✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
354,099,605✔
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);
354,099,605✔
139
  if (*pTaskInfo == NULL || code != 0) {
354,011,461✔
UNCOV
140
    nodesDestroyNode((SNode*)pPlan);
×
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
354,052,259✔
145

146
  if (pHandle) {
353,995,132✔
147
    if (pHandle->pStateBackend) {
353,934,486✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
354,033,186✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
351,503,881✔
155
    if (NULL == (*pTaskInfo)->sql) {
351,599,181✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
354,110,884✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
354,065,346✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
354,057,387✔
167
  if (code != TSDB_CODE_SUCCESS) {
353,688,679✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
353,688,679✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
353,839,958✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
353,904,394✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
353,811,727✔
179
    doDestroyTask(*pTaskInfo);
8,738,179✔
180
    (*pTaskInfo) = NULL;
8,467,631✔
181
  }
182
  return code;
353,560,772✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
274,875,821✔
186
  SSchemaInfo* pSchemaInfo = p;
274,875,821✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
274,875,821✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
274,843,614✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
274,846,379✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
274,876,705✔
192
}
274,870,693✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
274,816,652✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
274,816,652✔
197
  if (pHandle == NULL) {
274,845,599✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
274,845,599✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
274,820,022✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
274,920,684✔
205
  if (code != TSDB_CODE_SUCCESS) {
274,546,267✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
206✔
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
274,546,061✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
274,387,678✔
216
  schemaInfo.dbname = taosStrdup(dbName);
274,507,357✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
274,558,643✔
218
    pAPI->metaReaderFn.clearReader(&mr);
277,747✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
274,280,921✔
224
    schemaInfo.rversion = mr.me.colRef.version;
1,830✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
274,280,921✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
136,452,891✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
136,452,891✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
137,906,112✔
231
    tDecoderClear(&mr.coder);
80,019,201✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
80,034,098✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
80,034,098✔
235
    if (code != TSDB_CODE_SUCCESS) {
80,057,392✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
80,014,241✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
80,014,241✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
57,897,142✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
274,364,274✔
248

249
  if (schemaInfo.sw == NULL) {
274,377,766✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
274,377,766✔
255
  if (schemaInfo.qsw == NULL) {
274,599,967✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
274,599,967✔
261
  if (p == NULL) {
274,607,626✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
274,607,626✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
274,361,487✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
274,361,487✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
274,682,289✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
274,336,678✔
274
  if (pqSw == NULL) {
274,147,765✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
274,147,765✔
279
  if (pqSw->pSchema == NULL) {
273,666,087✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,131,439,494✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
856,652,931✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
856,600,169✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
856,737,193✔
289
    pSchema->colId = pColNode->colId;
856,679,488✔
290
    pSchema->type = pColNode->node.resType.type;
857,061,873✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
857,095,125✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
856,999,087✔
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) {
400,319,713✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
125,534,395✔
298

299
    int32_t type = nodeType(pNode->pExpr);
125,530,813✔
300
    if (type == QUERY_NODE_COLUMN) {
125,550,346✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
93,068,102✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
93,072,388✔
304
      pSchema->colId = pColNode->colId;
93,052,042✔
305
      pSchema->type = pColNode->node.resType.type;
93,029,648✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
92,984,473✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
93,012,877✔
308
    }
309
  }
310

311
  return pqSw;
274,785,318✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
354,312,197✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
354,312,197✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
354,310,406✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
354,279,176✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
354,308,831✔
319
}
354,362,560✔
320

321
static void freeBlock(void* pParam) {
451,412,561✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
451,412,561✔
323
  blockDataDestroy(pBlock);
451,416,256✔
324
}
451,389,544✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
354,348,335✔
328
  if (pCtx->transporterId > 0) {
354,348,335✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
137,957✔
330
    if (ret != 0) {
137,957✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
137,957✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
354,373,807✔
336
}
354,370,708✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
354,281,422✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
354,281,422✔
340
  destroyOperator(pTaskInfo->pRoot);
354,279,685✔
341
  pTaskInfo->pRoot = NULL;
354,329,277✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
354,380,358✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
354,380,412✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
354,320,450✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
354,358,159✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
354,267,352✔
350
    pTaskInfo->pSubplan = NULL;
354,341,429✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
354,444,142✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
354,348,247✔
355
  if (!pTaskInfo->paramSet) {
354,348,051✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
354,082,840✔
357
    pTaskInfo->pOpParam = NULL;
354,012,989✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
354,320,919✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
354,284,484✔
361
  taosMemoryFreeClear(pTaskInfo);
354,237,441✔
362
}
354,193,591✔
363

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