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

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

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

81.19
/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,
367,680,118✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
367,680,118✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
367,680,118✔
45
  if (p == NULL) {
367,455,726✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
367,455,726✔
50
  p->cost.created = taosGetTimestampUs();
367,782,319✔
51

52
  p->execModel = model;
367,773,893✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
367,694,132✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
367,569,074✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
367,616,501✔
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
367,749,077✔
61
  taosInitRWLatch(&p->lock);
367,726,580✔
62

63
  p->id.vgId = vgId;
367,672,045✔
64
  p->id.queryId = queryId;
367,689,545✔
65
  p->id.taskId = taskId;
367,694,990✔
66
  p->id.str = taosMemoryMalloc(64);
367,705,286✔
67
  if (p->id.str == NULL) {
367,439,997✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
367,667,573✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
367,570,735✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
367,505,225✔
75
    doDestroyTask(p);
330✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
367,674,265✔
80
  return TSDB_CODE_SUCCESS;
367,678,491✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,757,638,993✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,119,110,579✔
93
  if (status == TASK_NOT_COMPLETED) {
1,119,110,579✔
94
    pTaskInfo->status = status;
367,638,159✔
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);
751,472,420✔
98
    pTaskInfo->status |= status;
751,506,163✔
99
  }
100
}
1,119,186,815✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
367,636,131✔
107
  ctx->idStr = pTaskInfo->id.str;
367,547,912✔
108
  ctx->pTaskInfo = pTaskInfo;
367,584,163✔
109
  ctx->subEndPoints = subEndPoints;
367,528,111✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
367,475,856✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
367,578,894✔
113
  if (subJobNum > 0) {
367,555,020✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
90,612,972✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
90,528,888✔
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);
90,456,796✔
121
    if (code) {
90,554,531✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
90,554,531✔
127

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

131
  return TSDB_CODE_SUCCESS;
367,439,957✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
367,567,387✔
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);
367,567,387✔
139
  if (*pTaskInfo == NULL || code != 0) {
367,487,719✔
140
    nodesDestroyNode((SNode*)pPlan);
16✔
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
367,547,036✔
145

146
  if (pHandle) {
367,570,192✔
147
    if (pHandle->pStateBackend) {
367,442,654✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
367,617,928✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
365,026,698✔
155
    if (NULL == (*pTaskInfo)->sql) {
365,073,390✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
367,641,772✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
367,547,050✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
367,488,943✔
167
  if (code != TSDB_CODE_SUCCESS) {
367,433,652✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
367,433,652✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
367,411,381✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
367,201,984✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
367,310,631✔
179
    doDestroyTask(*pTaskInfo);
9,118,144✔
180
    (*pTaskInfo) = NULL;
8,934,335✔
181
  }
182
  return code;
367,224,100✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
285,586,950✔
186
  SSchemaInfo* pSchemaInfo = p;
285,586,950✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
285,586,950✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
285,578,549✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
285,540,272✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
285,529,541✔
192
}
285,535,859✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
285,593,611✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
285,593,611✔
197
  if (pHandle == NULL) {
285,611,368✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
285,611,368✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
285,603,687✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
285,596,774✔
205
  if (code != TSDB_CODE_SUCCESS) {
285,332,104✔
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};
285,332,104✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
285,332,195✔
216
  schemaInfo.dbname = taosStrdup(dbName);
285,244,570✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
285,255,850✔
218
    pAPI->metaReaderFn.clearReader(&mr);
197,605✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

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

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
285,058,245✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
143,657,812✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
143,657,812✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
141,433,235✔
231
    tDecoderClear(&mr.coder);
81,772,979✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
81,956,407✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
81,956,407✔
235
    if (code != TSDB_CODE_SUCCESS) {
81,961,332✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
81,940,330✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
81,940,330✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
59,673,982✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
285,272,124✔
248

249
  if (schemaInfo.sw == NULL) {
284,825,645✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
284,825,645✔
255
  if (schemaInfo.qsw == NULL) {
285,356,204✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
285,356,204✔
261
  if (p == NULL) {
285,395,768✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
285,395,768✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
285,115,344✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
285,115,344✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
285,333,565✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
285,293,538✔
274
  if (pqSw == NULL) {
285,019,573✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
285,019,573✔
279
  if (pqSw->pSchema == NULL) {
284,672,852✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,180,944,417✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
895,509,877✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
895,790,375✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
895,816,469✔
289
    pSchema->colId = pColNode->colId;
895,823,430✔
290
    pSchema->type = pColNode->node.resType.type;
895,986,793✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
895,891,101✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
895,949,588✔
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) {
433,997,390✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
148,568,020✔
298

299
    int32_t type = nodeType(pNode->pExpr);
148,615,486✔
300
    if (type == QUERY_NODE_COLUMN) {
148,633,979✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
112,564,574✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
112,551,662✔
304
      pSchema->colId = pColNode->colId;
112,519,171✔
305
      pSchema->type = pColNode->node.resType.type;
112,563,654✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
112,533,719✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
112,553,726✔
308
    }
309
  }
310

311
  return pqSw;
285,429,370✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
367,742,749✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
367,742,749✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
367,775,400✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
367,766,437✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
367,759,665✔
319
}
367,740,146✔
320

321
static void freeBlock(void* pParam) {
492,116,044✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
492,116,044✔
323
  blockDataDestroy(pBlock);
492,126,130✔
324
}
492,134,730✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
367,771,242✔
328
  if (pCtx->transporterId > 0) {
367,771,242✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
153,507✔
330
    if (ret != 0) {
153,507✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
153,507✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
367,781,886✔
336
}
367,776,741✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
367,735,331✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
367,735,331✔
340
  destroyOperator(pTaskInfo->pRoot);
367,734,852✔
341
  pTaskInfo->pRoot = NULL;
367,745,618✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
367,770,246✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
367,781,962✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
367,738,068✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
367,768,438✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
367,773,642✔
350
    pTaskInfo->pSubplan = NULL;
367,720,962✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
367,749,834✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
367,763,052✔
355
  if (!pTaskInfo->paramSet) {
367,743,998✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
367,488,170✔
357
    pTaskInfo->pOpParam = NULL;
367,446,244✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
367,732,544✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
367,717,355✔
361
  taosMemoryFreeClear(pTaskInfo);
367,712,548✔
362
}
367,650,931✔
363

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