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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

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

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

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 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,
295,785,380✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
295,785,380✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
295,785,380✔
45
  if (p == NULL) {
295,675,181✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
295,675,181✔
50
  p->cost.created = taosGetTimestampUs();
295,816,788✔
51

52
  p->execModel = model;
295,803,713✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
295,802,746✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
295,733,539✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
295,749,831✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
295,770,718✔
61
  taosInitRWLatch(&p->lock);
295,787,683✔
62

63
  p->id.vgId = vgId;
295,780,685✔
64
  p->id.queryId = queryId;
295,786,201✔
65
  p->id.taskId = taskId;
295,792,066✔
66
  p->id.str = taosMemoryMalloc(64);
295,791,031✔
67
  if (p->id.str == NULL) {
295,691,266✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
295,742,476✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
295,718,530✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
295,639,439✔
75
    doDestroyTask(p);
357✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
295,706,141✔
80
  return TSDB_CODE_SUCCESS;
295,716,081✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,610,451,099✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
921,429,136✔
93
  if (status == TASK_NOT_COMPLETED) {
921,429,136✔
94
    pTaskInfo->status = status;
295,798,149✔
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);
625,630,987✔
98
    pTaskInfo->status |= status;
625,664,878✔
99
  }
100
}
921,486,224✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
295,613,410✔
107
  ctx->idStr = pTaskInfo->id.str;
295,589,240✔
108
  ctx->pTaskInfo = pTaskInfo;
295,591,081✔
109
  ctx->subEndPoints = subEndPoints;
295,582,097✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
295,510,169✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
295,643,898✔
113
  if (subJobNum > 0) {
295,636,299✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
59,661,489✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
59,637,147✔
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);
59,615,274✔
121
    if (code) {
59,599,174✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
59,599,174✔
127

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

131
  return TSDB_CODE_SUCCESS;
295,569,474✔
132
}
133

134

135

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

144
  (*pTaskInfo)->pSubplan = pPlan;
295,627,996✔
145

146
  if (pHandle) {
295,621,715✔
147
    if (pHandle->pStateBackend) {
295,625,969✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
295,637,717✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
293,162,446✔
155
    if (NULL == (*pTaskInfo)->sql) {
293,180,721✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
295,666,836✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
295,640,172✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
295,609,032✔
167
  if (code != TSDB_CODE_SUCCESS) {
295,562,937✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
295,562,937✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
295,561,723✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
295,481,644✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
295,465,072✔
179
    doDestroyTask(*pTaskInfo);
4,043,376✔
180
    (*pTaskInfo) = NULL;
4,032,487✔
181
  }
182
  return code;
295,451,433✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
230,828,979✔
186
  SSchemaInfo* pSchemaInfo = p;
230,828,979✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
230,828,979✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
230,813,853✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
230,800,556✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
230,828,149✔
192
}
230,814,018✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
230,821,685✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
230,821,685✔
197
  if (pHandle == NULL) {
230,833,718✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
230,833,718✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
230,826,058✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
230,841,923✔
205
  if (code != TSDB_CODE_SUCCESS) {
230,511,698✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
204✔
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
230,511,494✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
230,561,795✔
216
  schemaInfo.dbname = taosStrdup(dbName);
230,660,691✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
230,672,673✔
218
    pAPI->metaReaderFn.clearReader(&mr);
155,664✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

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

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
230,517,009✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
94,318,630✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
94,318,630✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
136,237,043✔
231
    tDecoderClear(&mr.coder);
79,403,585✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
79,445,344✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
79,445,344✔
235
    if (code != TSDB_CODE_SUCCESS) {
79,442,682✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
79,428,731✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
79,428,731✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
56,865,691✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
230,613,052✔
248

249
  if (schemaInfo.sw == NULL) {
230,631,885✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
230,631,885✔
255
  if (schemaInfo.qsw == NULL) {
230,672,611✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
230,672,611✔
261
  if (p == NULL) {
230,745,000✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
230,745,000✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
230,600,290✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
230,600,290✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
230,703,489✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
230,601,228✔
274
  if (pqSw == NULL) {
230,438,098✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
230,438,098✔
279
  if (pqSw->pSchema == NULL) {
230,232,854✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,000,757,814✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
769,996,968✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
770,162,154✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
770,196,850✔
289
    pSchema->colId = pColNode->colId;
770,143,561✔
290
    pSchema->type = pColNode->node.resType.type;
770,294,890✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
770,250,108✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
770,175,498✔
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) {
345,706,407✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
114,933,535✔
298

299
    int32_t type = nodeType(pNode->pExpr);
114,998,106✔
300
    if (type == QUERY_NODE_COLUMN) {
115,015,467✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
83,419,723✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
83,411,206✔
304
      pSchema->colId = pColNode->colId;
83,370,576✔
305
      pSchema->type = pColNode->node.resType.type;
83,383,783✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
83,364,140✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
83,332,939✔
308
    }
309
  }
310

311
  return pqSw;
230,772,872✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
295,791,309✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
295,791,309✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
295,798,809✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
295,755,887✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
295,784,281✔
319
}
295,790,182✔
320

321
static void freeBlock(void* pParam) {
406,175,822✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
406,175,822✔
323
  blockDataDestroy(pBlock);
406,179,391✔
324
}
406,165,476✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
295,811,224✔
328
  if (pCtx->transporterId > 0) {
295,811,224✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
5,060✔
330
    if (ret != 0) {
5,060✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
5,060✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
295,818,834✔
336
}
295,823,532✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
295,772,397✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
295,772,397✔
340
  destroyOperator(pTaskInfo->pRoot);
295,771,921✔
341
  pTaskInfo->pRoot = NULL;
295,800,923✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
295,813,883✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
295,825,755✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
295,794,935✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
295,776,766✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
295,774,146✔
350
    pTaskInfo->pSubplan = NULL;
295,781,775✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
295,813,316✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
295,808,237✔
355
  if (!pTaskInfo->paramSet) {
295,802,024✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
295,518,914✔
357
    pTaskInfo->pOpParam = NULL;
295,463,087✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
295,754,236✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
295,783,646✔
361
  taosMemoryFreeClear(pTaskInfo);
295,740,056✔
362
}
295,701,424✔
363

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