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

taosdata / TDengine / #4896

24 Dec 2025 07:36AM UTC coverage: 65.929% (+0.4%) from 65.513%
#4896

push

travis-ci

web-flow
enh: [TS-7591] Some code refactor and add more log. (#34022)

326 of 537 new or added lines in 4 files covered. (60.71%)

370 existing lines in 111 files now uncovered.

185828 of 281861 relevant lines covered (65.93%)

116309824.55 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,
275,601,179✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
275,601,179✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
275,601,179✔
45
  if (p == NULL) {
275,542,177✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
275,542,177✔
50
  p->cost.created = taosGetTimestampUs();
275,641,792✔
51

52
  p->execModel = model;
275,630,472✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
275,630,562✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
275,588,250✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
275,577,969✔
56
    doDestroyTask(p);
8✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
275,612,251✔
61
  taosInitRWLatch(&p->lock);
275,609,233✔
62

63
  p->id.vgId = vgId;
275,602,782✔
64
  p->id.queryId = queryId;
275,607,527✔
65
  p->id.taskId = taskId;
275,595,333✔
66
  p->id.str = taosMemoryMalloc(64);
275,612,764✔
67
  if (p->id.str == NULL) {
275,536,795✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
275,600,245✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
275,565,875✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
275,561,604✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
275,598,766✔
80
  return TSDB_CODE_SUCCESS;
275,598,817✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,316,106,354✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
859,990,056✔
93
  if (status == TASK_NOT_COMPLETED) {
859,990,056✔
94
    pTaskInfo->status = status;
275,660,183✔
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);
584,329,873✔
98
    pTaskInfo->status |= status;
584,342,524✔
99
  }
100
}
860,026,280✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
275,596,282✔
107
  ctx->idStr = pTaskInfo->id.str;
275,559,293✔
108
  ctx->pTaskInfo = pTaskInfo;
275,565,495✔
109
  ctx->subEndPoints = subEndPoints;
275,544,737✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
275,550,452✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
275,580,295✔
113
  if (subJobNum > 0) {
275,569,921✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
73,798,150✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
73,780,123✔
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);
73,770,917✔
121
    if (code) {
73,783,072✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
73,783,072✔
127

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

131
  return TSDB_CODE_SUCCESS;
275,542,751✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
275,581,146✔
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);
275,581,146✔
139
  if (*pTaskInfo == NULL || code != 0) {
275,543,891✔
140
    nodesDestroyNode((SNode*)pPlan);
21✔
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
275,565,307✔
145

146
  if (pHandle) {
275,572,712✔
147
    if (pHandle->pStateBackend) {
275,538,149✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
275,598,980✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
272,895,783✔
155
    if (NULL == (*pTaskInfo)->sql) {
272,918,455✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
275,612,707✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
275,585,541✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
275,544,483✔
167
  if (code != TSDB_CODE_SUCCESS) {
275,543,261✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
275,543,261✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
275,547,389✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
275,467,476✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
275,480,759✔
179
    doDestroyTask(*pTaskInfo);
4,998,556✔
180
    (*pTaskInfo) = NULL;
4,973,776✔
181
  }
182
  return code;
275,496,026✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
219,822,614✔
186
  SSchemaInfo* pSchemaInfo = p;
219,822,614✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
219,822,614✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
219,815,756✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
219,810,419✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
219,807,314✔
192
}
219,812,800✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
219,827,099✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
219,827,099✔
197
  if (pHandle == NULL) {
219,830,883✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
219,830,883✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
219,824,303✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
219,830,543✔
205
  if (code != TSDB_CODE_SUCCESS) {
219,766,329✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
821✔
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
219,765,508✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
219,763,094✔
216
  schemaInfo.dbname = taosStrdup(dbName);
219,718,626✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
219,723,371✔
218
    pAPI->metaReaderFn.clearReader(&mr);
40,157✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
219,683,214✔
224
    schemaInfo.rversion = mr.me.colRef.version;
12,456✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
219,683,214✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
70,383,592✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
70,383,592✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
149,336,379✔
231
    tDecoderClear(&mr.coder);
92,194,940✔
232

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

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
92,191,388✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
92,191,388✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
57,153,355✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
219,728,335✔
248

249
  if (schemaInfo.sw == NULL) {
219,639,156✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
219,639,156✔
255
  if (schemaInfo.qsw == NULL) {
219,759,814✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
219,759,814✔
261
  if (p == NULL) {
219,768,997✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
219,768,997✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
219,709,814✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
219,709,814✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
219,755,815✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
219,758,922✔
274
  if (pqSw == NULL) {
219,674,900✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
219,674,900✔
279
  if (pqSw->pSchema == NULL) {
219,548,016✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,023,395,806✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
803,592,334✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
803,561,763✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
803,589,090✔
289
    pSchema->colId = pColNode->colId;
803,614,870✔
290
    pSchema->type = pColNode->node.resType.type;
803,674,049✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
803,694,024✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
803,671,509✔
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) {
299,882,712✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
80,088,083✔
298

299
    int32_t type = nodeType(pNode->pExpr);
80,108,935✔
300
    if (type == QUERY_NODE_COLUMN) {
80,115,852✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
65,524,630✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
65,517,524✔
304
      pSchema->colId = pColNode->colId;
65,488,988✔
305
      pSchema->type = pColNode->node.resType.type;
65,521,678✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
65,487,022✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
65,514,416✔
308
    }
309
  }
310

311
  return pqSw;
219,794,629✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
275,622,596✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
275,622,596✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
275,626,562✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
275,615,339✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
275,623,716✔
319
}
275,621,296✔
320

321
static void freeBlock(void* pParam) {
359,998,170✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
359,998,170✔
323
  blockDataDestroy(pBlock);
360,001,581✔
324
}
359,999,790✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
275,626,766✔
328
  if (pCtx->transporterId > 0) {
275,626,766✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
3,621✔
330
    if (ret != 0) {
3,621✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
3,621✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
275,637,631✔
336
}
275,636,127✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
275,596,118✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
275,596,118✔
340
  destroyOperator(pTaskInfo->pRoot);
275,596,118✔
341
  pTaskInfo->pRoot = NULL;
275,620,307✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
275,630,378✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
275,637,803✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
275,619,288✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
275,636,806✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
275,636,609✔
350
    pTaskInfo->pSubplan = NULL;
275,612,456✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
275,623,849✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
275,634,065✔
355
  if (!pTaskInfo->paramSet) {
275,618,563✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
272,880,956✔
357
    pTaskInfo->pOpParam = NULL;
272,874,266✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
275,617,835✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
275,616,288✔
361
  taosMemoryFreeClear(pTaskInfo);
275,613,054✔
362
}
275,602,602✔
363

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