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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 hits per line

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

81.32
/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,
153,823,826✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
153,823,826✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
153,823,826✔
45
  if (p == NULL) {
153,816,337✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
153,816,337✔
50
  p->cost.created = taosGetTimestampUs();
153,827,236✔
51

52
  p->execModel = model;
153,825,054✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
153,825,834✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
153,820,520✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
153,816,721✔
56
    doDestroyTask(p);
861✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
153,823,257✔
61
  taosInitRWLatch(&p->lock);
153,826,073✔
62

63
  p->id.vgId = vgId;
153,822,443✔
64
  p->id.queryId = queryId;
153,819,847✔
65
  p->id.taskId = taskId;
153,823,831✔
66
  p->id.str = taosMemoryMalloc(64);
153,823,386✔
67
  if (p->id.str == NULL) {
153,810,430✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
153,817,713✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
153,814,489✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
153,796,204✔
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
153,813,359✔
80
  return TSDB_CODE_SUCCESS;
153,808,660✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
987,598,474✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
481,143,446✔
91
  if (status == TASK_NOT_COMPLETED) {
481,143,446✔
92
    pTaskInfo->status = status;
153,918,594✔
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);
327,224,852✔
96
    pTaskInfo->status |= status;
327,234,134✔
97
  }
98
}
481,153,721✔
99

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

108
  (*pTaskInfo)->pSubplan = pPlan;
153,788,282✔
109

110
  if (pHandle) {
153,787,331✔
111
    if (pHandle->pStateBackend) {
153,772,572✔
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
153,794,134✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
151,246,071✔
119
    if (NULL == (*pTaskInfo)->sql) {
151,243,060✔
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
153,800,083✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
153,777,677✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
153,776,924✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
153,801,900✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
153,660,104✔
133
    doDestroyTask(*pTaskInfo);
15,124✔
134
    (*pTaskInfo) = NULL;
25,850✔
135
  }
136
  return code;
153,711,323✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
114,500,864✔
140
  SSchemaInfo* pSchemaInfo = p;
114,500,864✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
114,500,864✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
114,500,753✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
114,490,526✔
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
114,491,889✔
146
}
114,488,129✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
114,500,543✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
114,500,543✔
151
  if (pHandle == NULL) {
114,501,314✔
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
114,501,314✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
114,499,878✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
114,503,913✔
159
  if (code != TSDB_CODE_SUCCESS) {
114,436,313✔
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
161
           GET_TASKID(pTaskInfo));
162

163
    pAPI->metaReaderFn.clearReader(&mr);
×
164
    return code;
×
165
  }
166

167
  SSchemaInfo schemaInfo = {0};
114,436,313✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
114,429,798✔
170
  schemaInfo.dbname = taosStrdup(dbName);
114,418,328✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
114,435,167✔
172
    pAPI->metaReaderFn.clearReader(&mr);
112,631✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
114,322,536✔
178
    schemaInfo.rversion = mr.me.colRef.version;
12,276✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
114,322,536✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
63,305,796✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
63,305,796✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
51,026,546✔
185
    tDecoderClear(&mr.coder);
32,127,439✔
186

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

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
32,227,060✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
32,227,060✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
18,898,032✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
114,430,888✔
202

203
  if (schemaInfo.sw == NULL) {
114,378,199✔
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
114,378,199✔
209
  if (schemaInfo.qsw == NULL) {
114,429,731✔
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
114,429,731✔
215
  if (p == NULL) {
114,440,781✔
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
114,440,781✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
114,419,117✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
114,419,117✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
114,448,571✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
114,438,458✔
228
  if (pqSw == NULL) {
114,346,259✔
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
114,346,259✔
233
  if (pqSw->pSchema == NULL) {
114,296,185✔
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
732,153,252✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
617,681,847✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
617,573,100✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
617,604,311✔
243
    pSchema->colId = pColNode->colId;
617,615,833✔
244
    pSchema->type = pColNode->node.resType.type;
617,747,673✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
617,700,594✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
617,800,013✔
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) {
189,830,707✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
75,360,013✔
252

253
    int32_t type = nodeType(pNode->pExpr);
75,338,718✔
254
    if (type == QUERY_NODE_COLUMN) {
75,343,225✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
62,991,564✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
62,990,962✔
258
      pSchema->colId = pColNode->colId;
63,009,496✔
259
      pSchema->type = pColNode->node.resType.type;
62,975,019✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
62,994,348✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
62,989,517✔
262
    }
263
  }
264

265
  return pqSw;
114,470,694✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
153,809,155✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
153,809,155✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
153,816,797✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
153,793,972✔
272
  taosMemoryFree(pStreamInfo->stbFullName);
153,811,658✔
273
}
153,812,277✔
274

275
static void freeBlock(void* pParam) {
290,949,839✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
290,949,839✔
277
  blockDataDestroy(pBlock);
290,951,594✔
278
}
290,953,192✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
153,827,120✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
153,827,120✔
282
  destroyOperator(pTaskInfo->pRoot);
153,826,296✔
283
  pTaskInfo->pRoot = NULL;
153,814,417✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
153,816,254✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
153,800,999✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
153,810,127✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
153,806,951✔
290
    pTaskInfo->pSubplan = NULL;
153,808,315✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
153,807,056✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
153,808,421✔
295
  if (!pTaskInfo->paramSet) {
153,811,883✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
153,465,804✔
297
    pTaskInfo->pOpParam = NULL;
153,455,679✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
153,810,593✔
300
  taosMemoryFreeClear(pTaskInfo->id.str);
153,805,276✔
301
  taosMemoryFreeClear(pTaskInfo);
153,804,174✔
302
}
153,787,481✔
303

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