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

taosdata / TDengine / #5009

29 Mar 2026 04:32AM UTC coverage: 72.26% (+0.02%) from 72.241%
#5009

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253662 of 351039 relevant lines covered (72.26%)

131649114.75 hits per line

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

82.43
/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,
361,026,176✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
361,026,176✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
361,026,176✔
46
  if (p == NULL) {
360,997,705✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
360,997,705✔
51
  p->cost.created = taosGetTimestampUs();
361,045,777✔
52

53
  p->execModel = model;
361,040,017✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
361,038,396✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
360,996,430✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
360,997,023✔
57
    doDestroyTask(p);
×
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
361,030,311✔
62
  taosInitRWLatch(&p->lock);
361,018,810✔
63

64
  p->id.vgId = vgId;
361,007,093✔
65
  p->id.queryId = queryId;
361,014,826✔
66
  p->id.taskId = taskId;
361,015,588✔
67
  p->id.str = taosMemoryMalloc(64);
361,022,064✔
68
  if (p->id.str == NULL) {
360,959,657✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
361,016,331✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
360,995,698✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
360,966,020✔
76
    doDestroyTask(p);
26✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
361,002,256✔
81
  return TSDB_CODE_SUCCESS;
361,005,362✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,831,917,188✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,126,154,170✔
94
  if (status == TASK_NOT_COMPLETED) {
1,126,154,170✔
95
    pTaskInfo->status = status;
361,194,094✔
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);
764,960,076✔
99
    pTaskInfo->status |= status;
764,980,308✔
100
  }
101
}
1,126,179,844✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
40,125,963✔
105
  int32_t code = 0, lino = 0;
40,125,963✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
40,125,963✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
40,127,614✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
40,123,740✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
40,125,404✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
40,126,509✔
114
  ctx->taskId = pTaskInfo->id.taskId;
40,125,365✔
115
  ctx->idStr = pTaskInfo->id.str;
40,130,396✔
116
  ctx->pTaskInfo = pTaskInfo;
40,124,819✔
117
  ctx->subEndPoints = *subEndPoints;
40,129,850✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
40,128,745✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
40,129,291✔
120

121
  *subEndPoints = NULL;
40,128,173✔
122
  
123
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
40,129,304✔
124
  if (NULL == ctx->subResNodes) {
40,126,509✔
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));
40,129,837✔
130

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

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

141
_exit:
39,705,194✔
142

143
  if (code) {
40,125,963✔
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;
40,125,404✔
151
}
152

153

154

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

163
  (*pTaskInfo)->pSubplan = pPlan;
360,880,888✔
164

165
  if (NULL != sql) {
360,902,581✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
336,293,995✔
167
    if (NULL == (*pTaskInfo)->sql) {
336,303,573✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
360,898,674✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
360,889,914✔
177
  (*pTaskInfo)->enableExplain = enableExplain;
360,875,309✔
178

179
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
360,906,682✔
180
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
40,128,173✔
181
    if (code != TSDB_CODE_SUCCESS) {
40,125,404✔
182
      doDestroyTask(*pTaskInfo);
×
183
      (*pTaskInfo) = NULL;
×
184
      return code;
×
185
    }
186
  }
187
  
188
  setTaskScalarExtraInfo(*pTaskInfo);
360,908,082✔
189
  
190
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
360,868,152✔
191
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
360,815,618✔
192

193
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
360,716,298✔
194
    doDestroyTask(*pTaskInfo);
572,384✔
195
    (*pTaskInfo) = NULL;
477,238✔
196
  }
197
  return code;
360,738,708✔
198
}
199

200
void cleanupQueriedTableScanInfo(void* p) {
276,321,620✔
201
  SSchemaInfo* pSchemaInfo = p;
276,321,620✔
202

203
  taosMemoryFreeClear(pSchemaInfo->dbname);
276,321,620✔
204
  taosMemoryFreeClear(pSchemaInfo->tablename);
276,294,250✔
205
  tDeleteSchemaWrapper(pSchemaInfo->sw);
276,244,483✔
206
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
276,255,488✔
207
}
276,237,999✔
208

209
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
276,348,635✔
210
                                   SExecTaskInfo* pTaskInfo) {
211
  SMetaReader mr = {0};
276,348,635✔
212
  if (pHandle == NULL) {
276,357,359✔
213
    return TSDB_CODE_INVALID_PARA;
×
214
  }
215

216
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
276,357,359✔
217

218
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
276,344,174✔
219
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
276,353,808✔
220
  if (code != TSDB_CODE_SUCCESS) {
276,227,408✔
221
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
240✔
222
           GET_TASKID(pTaskInfo));
223

224
    pAPI->metaReaderFn.clearReader(&mr);
240✔
225
    return code;
240✔
226
  }
227

228
  SSchemaInfo schemaInfo = {0};
276,227,168✔
229

230
  schemaInfo.tablename = taosStrdup(mr.me.name);
276,228,742✔
231
  schemaInfo.dbname = taosStrdup(dbName);
276,218,181✔
232
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
276,249,323✔
233
    pAPI->metaReaderFn.clearReader(&mr);
77,472✔
234
    cleanupQueriedTableScanInfo(&schemaInfo);
×
235
    return terrno;
×
236
  }
237

238
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
276,172,113✔
239
    schemaInfo.rversion = mr.me.colRef.version;
427,678✔
240
  }
241

242
  if (mr.me.type == TSDB_SUPER_TABLE) {
276,172,113✔
243
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
125,798,205✔
244
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
125,798,205✔
245
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
150,393,868✔
246
    tDecoderClear(&mr.coder);
105,096,325✔
247

248
    tb_uid_t suid = mr.me.ctbEntry.suid;
105,131,403✔
249
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
105,131,403✔
250
    if (code != TSDB_CODE_SUCCESS) {
105,130,699✔
251
      pAPI->metaReaderFn.clearReader(&mr);
×
252
      cleanupQueriedTableScanInfo(&schemaInfo);
×
253
      return code;
×
254
    }
255

256
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
105,134,661✔
257
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
105,134,661✔
258
  } else {
259
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
45,309,692✔
260
  }
261

262
  pAPI->metaReaderFn.clearReader(&mr);
276,242,558✔
263

264
  if (schemaInfo.sw == NULL) {
276,101,388✔
265
    cleanupQueriedTableScanInfo(&schemaInfo);
×
266
    return terrno;
×
267
  }
268

269
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
276,101,388✔
270
  if (schemaInfo.qsw == NULL) {
276,234,291✔
271
    cleanupQueriedTableScanInfo(&schemaInfo);
×
272
    return terrno;
×
273
  }
274

275
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
276,234,291✔
276
  if (p == NULL) {
276,266,844✔
277
    cleanupQueriedTableScanInfo(&schemaInfo);
×
278
    return terrno;
×
279
  }
280

281
  return code;
276,266,844✔
282
}
283

284
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
276,178,899✔
285
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
276,178,899✔
286
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
276,263,094✔
287

288
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
276,236,769✔
289
  if (pqSw == NULL) {
276,156,075✔
290
    return NULL;
×
291
  }
292

293
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
276,156,075✔
294
  if (pqSw->pSchema == NULL) {
276,025,632✔
295
    taosMemoryFree(pqSw);
×
296
    return NULL;
×
297
  }
298

299
  for (int32_t i = 0; i < numOfCols; ++i) {
1,256,142,338✔
300
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
979,854,204✔
301
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
979,899,348✔
302

303
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
979,914,016✔
304
    pSchema->colId = pColNode->colId;
979,902,069✔
305
    pSchema->type = pColNode->node.resType.type;
980,019,426✔
306
    pSchema->bytes = pColNode->node.resType.bytes;
979,997,973✔
307
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
979,918,587✔
308
  }
309

310
  // this the tags and pseudo function columns, we only keep the tag columns
311
  for (int32_t i = 0; i < numOfTags; ++i) {
469,631,298✔
312
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
193,326,778✔
313

314
    int32_t type = nodeType(pNode->pExpr);
193,377,100✔
315
    if (type == QUERY_NODE_COLUMN) {
193,404,358✔
316
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
149,357,861✔
317

318
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
149,334,940✔
319
      pSchema->colId = pColNode->colId;
149,310,285✔
320
      pSchema->type = pColNode->node.resType.type;
149,353,270✔
321
      pSchema->bytes = pColNode->node.resType.bytes;
149,338,282✔
322
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
149,327,027✔
323
    }
324
  }
325

326
  return pqSw;
276,304,520✔
327
}
328

329
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
360,961,651✔
330
  tDeleteSchemaWrapper(pTmqInfo->schema);
360,961,651✔
331
  tOffsetDestroy(&pTmqInfo->currentOffset);
360,994,481✔
332
}
360,915,845✔
333

334
static void freeBlock(void* pParam) {
693,172,128✔
335
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
693,172,128✔
336
  blockDataDestroy(pBlock);
693,181,153✔
337
}
693,157,207✔
338

339

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

349
  if (pCtx->subEndPoints != NULL) {
40,130,396✔
350
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
40,128,732✔
351
    if (size > 0) {
40,128,173✔
352
      int32_t code = tsem_destroy(&pCtx->ready);
40,128,173✔
353
      if (code != TSDB_CODE_SUCCESS) {
40,127,915✔
354
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
355
      }
356
      taosArrayDestroy(pCtx->subResNodes);
40,127,915✔
357
    }
358
    if (pCtx->isStream) {
40,126,548✔
359
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
199,304✔
360
    } else {
361
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
39,922,785✔
362
    }
363
    pCtx->subEndPoints = NULL;
40,120,932✔
364
  }
365
  
366
  taosMemoryFreeClear(pCtx);  
40,125,133✔
367
}
40,120,962✔
368

369
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
361,031,810✔
370
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
361,031,810✔
371
  destroyOperator(pTaskInfo->pRoot);
361,031,810✔
372
  pTaskInfo->pRoot = NULL;
360,958,580✔
373

374
  if (pTaskInfo->pSubJobCtx) {
360,980,288✔
375
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
40,129,291✔
376
    if (code != TSDB_CODE_SUCCESS) {
40,126,496✔
377
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
378
    }
379
  }
380

381
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
360,993,164✔
382
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
360,948,919✔
383

384
  if (!pTaskInfo->localFetch.localExec) {
360,904,628✔
385
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
360,967,844✔
386
    pTaskInfo->pSubplan = NULL;
360,979,055✔
387
  }
388

389
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
360,932,877✔
390
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
360,967,812✔
391
  if (!pTaskInfo->paramSet) {
360,983,782✔
392
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
354,658,719✔
393
    pTaskInfo->pOpParam = NULL;
354,660,955✔
394
  }
395
  taosMemoryFreeClear(pTaskInfo->sql);
360,949,272✔
396
  taosMemoryFreeClear(pTaskInfo->id.str);
360,981,449✔
397
  taosMemoryFreeClear(pTaskInfo);
360,931,538✔
398
}
360,907,798✔
399

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