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

taosdata / TDengine / #4910

30 Dec 2025 10:52AM UTC coverage: 65.864% (+0.3%) from 65.542%
#4910

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

999 existing lines in 108 files now uncovered.

194877 of 295877 relevant lines covered (65.86%)

121300574.4 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,
363,227,542✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
363,227,542✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
363,227,542✔
45
  if (p == NULL) {
362,975,301✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
362,975,301✔
50
  p->cost.created = taosGetTimestampUs();
363,334,122✔
51

52
  p->execModel = model;
363,335,327✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
363,326,001✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
363,175,914✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
363,092,891✔
56
    doDestroyTask(p);
58✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
363,276,546✔
61
  taosInitRWLatch(&p->lock);
363,261,378✔
62

63
  p->id.vgId = vgId;
363,227,674✔
64
  p->id.queryId = queryId;
363,252,351✔
65
  p->id.taskId = taskId;
363,272,732✔
66
  p->id.str = taosMemoryMalloc(64);
363,284,965✔
67
  if (p->id.str == NULL) {
363,029,337✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
363,227,214✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
363,084,956✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
362,986,242✔
75
    doDestroyTask(p);
89✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
363,200,588✔
80
  return TSDB_CODE_SUCCESS;
363,217,732✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,792,411,989✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,115,576,419✔
93
  if (status == TASK_NOT_COMPLETED) {
1,115,576,419✔
94
    pTaskInfo->status = status;
363,149,343✔
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);
752,427,076✔
98
    pTaskInfo->status |= status;
752,482,640✔
99
  }
100
}
1,115,702,810✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
363,207,152✔
107
  ctx->idStr = pTaskInfo->id.str;
363,076,805✔
108
  ctx->pTaskInfo = pTaskInfo;
363,070,077✔
109
  ctx->subEndPoints = subEndPoints;
363,056,023✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
363,009,456✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
363,135,372✔
113
  if (subJobNum > 0) {
363,098,882✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
86,991,731✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
86,914,041✔
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,847,499✔
121
    if (code) {
86,910,072✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
86,910,072✔
127

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

131
  return TSDB_CODE_SUCCESS;
363,006,633✔
132
}
133

134

135

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

144
  (*pTaskInfo)->pSubplan = pPlan;
363,073,744✔
145

146
  if (pHandle) {
363,138,935✔
147
    if (pHandle->pStateBackend) {
362,986,547✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
363,218,692✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
360,616,689✔
155
    if (NULL == (*pTaskInfo)->sql) {
360,689,600✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
363,272,575✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
363,145,206✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
363,158,679✔
167
  if (code != TSDB_CODE_SUCCESS) {
362,970,766✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
362,970,766✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
362,970,274✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
362,752,375✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
362,696,698✔
179
    doDestroyTask(*pTaskInfo);
8,395,913✔
180
    (*pTaskInfo) = NULL;
8,549,376✔
181
  }
182
  return code;
362,995,376✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
280,552,052✔
186
  SSchemaInfo* pSchemaInfo = p;
280,552,052✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
280,552,052✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
280,528,549✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
280,507,669✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
280,484,345✔
192
}
280,490,816✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
280,586,045✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
280,586,045✔
197
  if (pHandle == NULL) {
280,604,241✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
280,604,241✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
280,590,692✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
280,583,451✔
205
  if (code != TSDB_CODE_SUCCESS) {
280,245,177✔
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};
280,245,177✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
280,254,599✔
216
  schemaInfo.dbname = taosStrdup(dbName);
280,304,372✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
280,320,636✔
218
    pAPI->metaReaderFn.clearReader(&mr);
191,376✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

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

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
280,129,260✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
142,683,571✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
142,683,571✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
137,430,662✔
231
    tDecoderClear(&mr.coder);
80,012,733✔
232

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

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

247
  pAPI->metaReaderFn.clearReader(&mr);
280,192,533✔
248

249
  if (schemaInfo.sw == NULL) {
279,993,407✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
279,993,407✔
255
  if (schemaInfo.qsw == NULL) {
280,271,076✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
280,271,076✔
261
  if (p == NULL) {
280,393,809✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
280,393,809✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
280,086,939✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
280,086,939✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
280,358,827✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
280,309,538✔
274
  if (pqSw == NULL) {
280,009,056✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
280,009,056✔
279
  if (pqSw->pSchema == NULL) {
279,508,574✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,160,973,288✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
880,557,764✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
880,950,731✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
880,998,809✔
289
    pSchema->colId = pColNode->colId;
880,957,543✔
290
    pSchema->type = pColNode->node.resType.type;
881,194,156✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
881,127,870✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
880,904,671✔
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) {
450,495,851✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
170,068,585✔
298

299
    int32_t type = nodeType(pNode->pExpr);
170,124,954✔
300
    if (type == QUERY_NODE_COLUMN) {
170,151,052✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
131,988,811✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
131,955,684✔
304
      pSchema->colId = pColNode->colId;
131,926,617✔
305
      pSchema->type = pColNode->node.resType.type;
131,973,841✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
131,935,712✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
131,952,397✔
308
    }
309
  }
310

311
  return pqSw;
280,427,266✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
363,259,585✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
363,259,585✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
363,306,558✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
363,247,203✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
363,277,513✔
319
}
363,257,477✔
320

321
static void freeBlock(void* pParam) {
501,666,803✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
501,666,803✔
323
  blockDataDestroy(pBlock);
501,681,901✔
324
}
501,668,906✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
363,307,856✔
328
  if (pCtx->transporterId > 0) {
363,307,856✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
133,674✔
330
    if (ret != 0) {
133,674✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
133,674✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
363,334,305✔
336
}
363,333,048✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
363,282,831✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
363,282,831✔
340
  destroyOperator(pTaskInfo->pRoot);
363,282,831✔
341
  pTaskInfo->pRoot = NULL;
363,286,628✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
363,318,825✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
363,339,841✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
363,262,251✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
363,293,712✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
363,317,363✔
350
    pTaskInfo->pSubplan = NULL;
363,258,127✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
363,283,609✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
363,325,309✔
355
  if (!pTaskInfo->paramSet) {
363,271,884✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
361,920,592✔
357
    pTaskInfo->pOpParam = NULL;
361,855,352✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
363,239,117✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
363,319,463✔
361
  taosMemoryFreeClear(pTaskInfo);
363,213,879✔
362
}
363,135,277✔
363

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