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

taosdata / TDengine / #4807

17 Oct 2025 06:47AM UTC coverage: 61.121% (+0.03%) from 61.094%
#4807

push

travis-ci

web-flow
Merge pull request #33289 from taosdata/3.0

enh: Code Optimization (#33283)

155421 of 324369 branches covered (47.91%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2727 existing lines in 120 files now uncovered.

207582 of 269535 relevant lines covered (77.01%)

127069383.69 hits per line

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

81.54
/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,
220,931,792✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
220,931,792!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
220,931,792✔
45
  if (p == NULL) {
220,920,525!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
220,920,525✔
50
  p->cost.created = taosGetTimestampUs();
220,936,315✔
51

52
  p->execModel = model;
220,935,190✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
220,933,727✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
220,920,643✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
220,924,974!
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
220,920,808✔
61
  taosInitRWLatch(&p->lock);
220,921,505✔
62

63
  p->id.vgId = vgId;
220,924,318✔
64
  p->id.queryId = queryId;
220,926,627✔
65
  p->id.taskId = taskId;
220,929,890✔
66
  p->id.str = taosMemoryMalloc(64);
220,921,998✔
67
  if (p->id.str == NULL) {
220,898,609!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
220,918,559✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
220,901,114✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
220,893,852!
75
    doDestroyTask(p);
10,469✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
220,899,086✔
80
  return TSDB_CODE_SUCCESS;
220,900,559✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,307,657,185✔
84

85
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
45,037✔
86
  pTaskInfo->code = rspCode;
45,037✔
87
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
45,037✔
88
}
45,037✔
89

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
682,696,586✔
91
  if (status == TASK_NOT_COMPLETED) {
682,696,586✔
92
    pTaskInfo->status = status;
221,052,900✔
93
  } else {
94
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
95
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
461,643,686✔
96
    pTaskInfo->status |= status;
461,659,692✔
97
  }
98
}
682,717,420✔
99

100
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
220,906,391✔
101
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model) {
102
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
220,906,391✔
103
  if (*pTaskInfo == NULL || code != 0) {
220,868,602!
104
    nodesDestroyNode((SNode*)pPlan);
13,357✔
105
    return code;
×
106
  }
107

108
  (*pTaskInfo)->pSubplan = pPlan;
220,864,381✔
109

110
  if (pHandle) {
220,884,085✔
111
    if (pHandle->pStateBackend) {
220,860,674!
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
220,880,834✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
210,260,853!
119
    if (NULL == (*pTaskInfo)->sql) {
210,296,221!
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
220,891,701✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
220,882,716✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
220,875,741✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
220,860,805✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
220,700,626!
133
    doDestroyTask(*pTaskInfo);
21,598✔
134
    (*pTaskInfo) = NULL;
15,530✔
135
  }
136
  return code;
220,725,007✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
161,569,815✔
140
  SSchemaInfo* pSchemaInfo = p;
161,569,815✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
161,569,815✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
161,566,443✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
161,556,504!
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
161,546,404✔
146
}
161,542,461✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
161,593,366✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
161,593,366✔
151
  if (pHandle == NULL) {
161,596,268!
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
161,596,268✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
161,591,643✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
161,598,531✔
159
  if (code != TSDB_CODE_SUCCESS) {
161,461,634✔
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
4,971!
161
           GET_TASKID(pTaskInfo));
162

163
    pAPI->metaReaderFn.clearReader(&mr);
4,971✔
164
    return code;
4,971✔
165
  }
166

167
  SSchemaInfo schemaInfo = {0};
161,456,663✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
161,457,788✔
170
  schemaInfo.dbname = taosStrdup(dbName);
161,402,199✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
161,453,160✔
172
    pAPI->metaReaderFn.clearReader(&mr);
104,724✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
161,348,436!
178
    schemaInfo.rversion = mr.me.colRef.version;
228,012✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
161,348,436✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
94,322,035✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
94,322,035✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
67,196,396✔
185
    tDecoderClear(&mr.coder);
39,254,028✔
186

187
    tb_uid_t suid = mr.me.ctbEntry.suid;
39,182,456✔
188
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
39,182,456✔
189
    if (code != TSDB_CODE_SUCCESS) {
39,181,529!
190
      pAPI->metaReaderFn.clearReader(&mr);
×
191
      cleanupQueriedTableScanInfo(&schemaInfo);
×
192
      return code;
×
193
    }
194

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
39,179,679✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
39,179,679✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
27,947,841✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
161,449,555✔
202

203
  if (schemaInfo.sw == NULL) {
161,331,178!
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
161,331,178✔
209
  if (schemaInfo.qsw == NULL) {
161,438,129!
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
161,438,129✔
215
  if (p == NULL) {
161,481,429!
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
161,481,429✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
161,436,314✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
161,436,314✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
161,480,955✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
161,413,518✔
228
  if (pqSw == NULL) {
161,356,664!
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
161,356,664✔
233
  if (pqSw->pSchema == NULL) {
161,214,449!
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
902,722,560✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
741,196,814✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
741,009,779✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
741,021,500✔
243
    pSchema->colId = pColNode->colId;
740,976,406✔
244
    pSchema->type = pColNode->node.resType.type;
741,297,556✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
741,276,796✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
741,183,848!
247
  }
248

249
  // this the tags and pseudo function columns, we only keep the tag columns
250
  for (int32_t i = 0; i < numOfTags; ++i) {
275,523,934✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
114,031,047✔
252

253
    int32_t type = nodeType(pNode->pExpr);
114,008,424✔
254
    if (type == QUERY_NODE_COLUMN) {
114,031,681✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
95,236,042✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
95,235,530✔
258
      pSchema->colId = pColNode->colId;
95,198,176✔
259
      pSchema->type = pColNode->node.resType.type;
95,272,883✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
95,193,863✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
95,260,026!
262
    }
263
  }
264

265
  return pqSw;
161,492,887✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
220,882,491✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
220,882,491✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
220,901,340✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
220,869,356!
272
  taosMemoryFree(pStreamInfo->stbFullName);
220,888,623✔
273
}
220,880,859✔
274

275
static void freeBlock(void* pParam) {
357,700,145✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
357,700,145✔
277
  blockDataDestroy(pBlock);
357,709,608✔
278
}
357,712,406✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
220,933,027✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
220,933,027✔
282
  destroyOperator(pTaskInfo->pRoot);
220,933,027✔
283
  pTaskInfo->pRoot = NULL;
220,880,544✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
220,898,714✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
220,866,342✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
220,896,596✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
220,900,882✔
290
    pTaskInfo->pSubplan = NULL;
220,896,698✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
220,894,970✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
220,907,108✔
295
  if (!pTaskInfo->paramSet) {
220,899,236✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
216,232,863✔
297
    pTaskInfo->pOpParam = NULL;
216,205,228✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
220,895,978!
300
  taosMemoryFreeClear(pTaskInfo->id.str);
220,881,895✔
301
  taosMemoryFreeClear(pTaskInfo);
220,893,601✔
302
}
220,864,510✔
303

304
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
232,102,330✔
305
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
232,102,330!
306
  if (ret < 0) {
232,102,330!
307
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
308
  }
309
}
232,102,330✔
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