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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

81.93
/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
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
344,675,593✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
344,675,593✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
344,675,593✔
46
  if (p == NULL) {
344,649,818✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
344,649,818✔
51
  p->cost.created = taosGetTimestampUs();
344,683,929✔
52

53
  p->execModel = model;
344,683,792✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
344,686,059✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
344,666,634✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
344,650,778✔
57
    doDestroyTask(p);
1✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
344,666,302✔
62
  taosInitRWLatch(&p->lock);
344,676,652✔
63

64
  p->id.vgId = vgId;
344,665,508✔
65
  p->id.queryId = queryId;
344,660,281✔
66
  p->id.taskId = taskId;
344,668,212✔
67
  p->id.str = taosMemoryMalloc(64);
344,665,359✔
68
  if (p->id.str == NULL) {
344,637,196✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
344,639,770✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
344,642,549✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
344,601,839✔
76
    doDestroyTask(p);
35✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
344,630,231✔
81
  return TSDB_CODE_SUCCESS;
344,641,862✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
19,584✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,744,154,739✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,089,333,839✔
94
  if (status == TASK_NOT_COMPLETED) {
1,089,333,839✔
95
    pTaskInfo->status = status;
344,841,390✔
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
744,492,449✔
99
    pTaskInfo->status |= status;
744,500,060✔
100
  }
101
}
1,089,355,776✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
39,548,748✔
105
  int32_t code = 0, lino = 0;
39,548,748✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
39,548,748✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
39,541,676✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
39,545,473✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
39,541,121✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
39,550,369✔
114
  ctx->taskId = pTaskInfo->id.taskId;
39,547,105✔
115
  ctx->idStr = pTaskInfo->id.str;
39,542,209✔
116
  ctx->pTaskInfo = pTaskInfo;
39,547,649✔
117
  ctx->subEndPoints = *subEndPoints;
39,544,929✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
39,549,281✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
39,547,649✔
120

121
  *subEndPoints = NULL;
39,550,369✔
122
  
123
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
39,548,737✔
124
  if (NULL == ctx->subResNodes) {
39,542,764✔
125
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
126
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
127
  }
128
  
129
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
39,544,396✔
130

131
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
39,549,292✔
132
  if (refId < 0) {
39,546,561✔
133
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
134
    TAOS_CHECK_EXIT(terrno);
×
135
  }
136
  
137
  ctx->subJobRefId = refId;
39,546,561✔
138

139
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
39,544,385✔
140

141
_exit:
39,269,464✔
142

143
  if (code) {
39,543,841✔
144
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
145
    pTaskInfo->pSubJobCtx = NULL;
×
146
    
147
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
148
  }
149

150
  return code;
39,549,281✔
151
}
152

153

154

155
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
344,565,402✔
156
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
157
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
344,565,402✔
158
  if (*pTaskInfo == NULL || code != 0) {
344,519,185✔
159
    nodesDestroyNode((SNode*)pPlan);
345✔
160
    return code;
×
161
  }
162

163
  (*pTaskInfo)->pSubplan = pPlan;
344,530,923✔
164

165
  if (NULL != sql) {
344,550,970✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
320,247,305✔
167
    if (NULL == (*pTaskInfo)->sql) {
320,261,279✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
344,560,013✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
344,553,021✔
177

178
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
344,532,690✔
179
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
39,548,204✔
180
    if (code != TSDB_CODE_SUCCESS) {
39,549,281✔
181
      doDestroyTask(*pTaskInfo);
×
182
      (*pTaskInfo) = NULL;
×
183
      return code;
×
184
    }
185
  }
186
  
187
  setTaskScalarExtraInfo(*pTaskInfo);
344,511,726✔
188
  
189
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
344,545,517✔
190
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
344,527,098✔
191

192
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
344,404,071✔
193
    doDestroyTask(*pTaskInfo);
485,075✔
194
    (*pTaskInfo) = NULL;
463,270✔
195
  }
196
  return code;
344,436,426✔
197
}
198

199
void cleanupQueriedTableScanInfo(void* p) {
259,882,853✔
200
  SSchemaInfo* pSchemaInfo = p;
259,882,853✔
201

202
  taosMemoryFreeClear(pSchemaInfo->dbname);
259,882,853✔
203
  taosMemoryFreeClear(pSchemaInfo->tablename);
259,849,645✔
204
  tDeleteSchemaWrapper(pSchemaInfo->sw);
259,814,295✔
205
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
259,816,768✔
206
}
259,814,838✔
207

208
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
259,914,395✔
209
                                   SExecTaskInfo* pTaskInfo) {
210
  SMetaReader mr = {0};
259,914,395✔
211
  if (pHandle == NULL) {
259,921,676✔
212
    return TSDB_CODE_INVALID_PARA;
×
213
  }
214

215
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
259,921,676✔
216

217
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
259,918,347✔
218
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
259,917,789✔
219
  if (code != TSDB_CODE_SUCCESS) {
259,807,428✔
UNCOV
220
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
221
           GET_TASKID(pTaskInfo));
222

UNCOV
223
    pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
224
    return code;
×
225
  }
226

227
  SSchemaInfo schemaInfo = {0};
259,807,428✔
228

229
  schemaInfo.tablename = taosStrdup(mr.me.name);
259,806,920✔
230
  schemaInfo.dbname = taosStrdup(dbName);
259,750,594✔
231
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
259,818,393✔
232
    pAPI->metaReaderFn.clearReader(&mr);
78,528✔
233
    cleanupQueriedTableScanInfo(&schemaInfo);
×
234
    return terrno;
×
235
  }
236

237
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
259,739,865✔
238
    schemaInfo.rversion = mr.me.colRef.version;
290,232✔
239
  }
240

241
  if (mr.me.type == TSDB_SUPER_TABLE) {
259,739,865✔
242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
112,138,149✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
112,138,149✔
244
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
147,681,954✔
245
    tDecoderClear(&mr.coder);
103,747,721✔
246

247
    tb_uid_t suid = mr.me.ctbEntry.suid;
103,714,240✔
248
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
103,714,240✔
249
    if (code != TSDB_CODE_SUCCESS) {
103,715,968✔
250
      pAPI->metaReaderFn.clearReader(&mr);
×
251
      cleanupQueriedTableScanInfo(&schemaInfo);
×
252
      return code;
×
253
    }
254

255
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
103,713,001✔
256
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
103,713,001✔
257
  } else {
258
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
43,948,083✔
259
  }
260

261
  pAPI->metaReaderFn.clearReader(&mr);
259,799,233✔
262

263
  if (schemaInfo.sw == NULL) {
259,730,088✔
264
    cleanupQueriedTableScanInfo(&schemaInfo);
×
265
    return terrno;
×
266
  }
267

268
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
259,730,088✔
269
  if (schemaInfo.qsw == NULL) {
259,816,182✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
271
    return terrno;
×
272
  }
273

274
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
259,816,182✔
275
  if (p == NULL) {
259,853,600✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
277
    return terrno;
×
278
  }
279

280
  return code;
259,853,600✔
281
}
282

283
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
259,754,098✔
284
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
259,754,098✔
285
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
259,817,625✔
286

287
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
259,804,223✔
288
  if (pqSw == NULL) {
259,724,394✔
289
    return NULL;
×
290
  }
291

292
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
259,724,394✔
293
  if (pqSw->pSchema == NULL) {
259,623,493✔
294
    taosMemoryFree(pqSw);
×
295
    return NULL;
×
296
  }
297

298
  for (int32_t i = 0; i < numOfCols; ++i) {
1,164,037,520✔
299
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
904,171,202✔
300
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
904,150,297✔
301

302
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
904,157,220✔
303
    pSchema->colId = pColNode->colId;
904,086,620✔
304
    pSchema->type = pColNode->node.resType.type;
904,245,769✔
305
    pSchema->bytes = pColNode->node.resType.bytes;
904,230,884✔
306
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
904,167,376✔
307
  }
308

309
  // this the tags and pseudo function columns, we only keep the tag columns
310
  for (int32_t i = 0; i < numOfTags; ++i) {
402,441,563✔
311
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
142,597,220✔
312

313
    int32_t type = nodeType(pNode->pExpr);
142,635,516✔
314
    if (type == QUERY_NODE_COLUMN) {
142,662,937✔
315
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
103,603,687✔
316

317
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
103,590,576✔
318
      pSchema->colId = pColNode->colId;
103,565,680✔
319
      pSchema->type = pColNode->node.resType.type;
103,609,716✔
320
      pSchema->bytes = pColNode->node.resType.bytes;
103,567,623✔
321
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
103,595,632✔
322
    }
323
  }
324

325
  return pqSw;
259,844,343✔
326
}
327

328
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
344,614,357✔
329
  tDeleteSchemaWrapper(pTmqInfo->schema);
344,614,357✔
330
  tOffsetDestroy(&pTmqInfo->currentOffset);
344,636,752✔
331
}
344,574,606✔
332

333
static void freeBlock(void* pParam) {
632,143,766✔
334
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
632,143,766✔
335
  blockDataDestroy(pBlock);
632,152,773✔
336
}
632,137,151✔
337

338

339
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
39,550,369✔
340
  if (pCtx->transporterId > 0) {
39,550,369✔
341
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
8,704✔
342
    if (ret != 0) {
8,704✔
343
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
344
    }
345
    pCtx->transporterId = -1;
8,704✔
346
  }
347

348
  if (pCtx->subEndPoints != NULL) {
39,550,913✔
349
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
39,549,825✔
350
    if (size > 0) {
39,550,913✔
351
      int32_t code = tsem_destroy(&pCtx->ready);
39,551,457✔
352
      if (code != TSDB_CODE_SUCCESS) {
39,549,281✔
353
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
354
      }
355
      taosArrayDestroy(pCtx->subResNodes);
39,549,281✔
356
    }
357
    if (pCtx->isStream) {
39,546,028✔
358
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
60,669✔
359
    } else {
360
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
39,485,359✔
361
    }
362
    pCtx->subEndPoints = NULL;
39,545,495✔
363
  }
364
  
365
  taosMemoryFreeClear(pCtx);  
39,548,215✔
366
}
39,547,671✔
367

368
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
344,679,256✔
369
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
344,679,256✔
370
  destroyOperator(pTaskInfo->pRoot);
344,679,256✔
371
  pTaskInfo->pRoot = NULL;
344,586,700✔
372

373
  if (pTaskInfo->pSubJobCtx) {
344,603,571✔
374
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
39,549,292✔
375
    if (code != TSDB_CODE_SUCCESS) {
39,550,913✔
376
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
377
    }
378
  }
379

380
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
344,617,973✔
381
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
344,587,505✔
382

383
  if (!pTaskInfo->localFetch.localExec) {
344,565,386✔
384
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
344,612,816✔
385
    pTaskInfo->pSubplan = NULL;
344,607,403✔
386
  }
387

388
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
344,517,338✔
389
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
344,606,888✔
390
  if (!pTaskInfo->paramSet) {
344,617,644✔
391
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
337,539,465✔
392
    pTaskInfo->pOpParam = NULL;
337,500,421✔
393
  }
394
  taosMemoryFreeClear(pTaskInfo->sql);
344,598,469✔
395
  taosMemoryFreeClear(pTaskInfo->id.str);
344,635,251✔
396
  taosMemoryFreeClear(pTaskInfo);
344,596,281✔
397
}
344,571,945✔
398

399
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
375,023,944✔
400
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
375,023,944✔
401
  if (ret < 0) {
375,023,944✔
402
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
403
  }
404
}
375,023,944✔
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