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

taosdata / TDengine / #4945

30 Jan 2026 06:19AM UTC coverage: 66.87% (+0.02%) from 66.849%
#4945

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1126 of 2018 new or added lines in 72 files covered. (55.8%)

13708 existing lines in 159 files now uncovered.

205277 of 306978 relevant lines covered (66.87%)

126353544.65 hits per line

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

80.82
/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,
259,796,925✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
259,796,925✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
259,796,925✔
45
  if (p == NULL) {
259,735,220✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
259,735,220✔
50
  p->cost.created = taosGetTimestampUs();
259,809,075✔
51

52
  p->execModel = model;
259,798,826✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
259,809,386✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
259,760,025✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
259,748,333✔
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
259,780,579✔
61
  taosInitRWLatch(&p->lock);
259,801,908✔
62

63
  p->id.vgId = vgId;
259,768,741✔
64
  p->id.queryId = queryId;
259,768,819✔
65
  p->id.taskId = taskId;
259,782,679✔
66
  p->id.str = taosMemoryMalloc(64);
259,780,819✔
67
  if (p->id.str == NULL) {
259,735,878✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
259,757,883✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
259,755,825✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
259,701,623✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
259,740,269✔
80
  return TSDB_CODE_SUCCESS;
259,750,716✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,654,842,656✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
831,019,630✔
93
  if (status == TASK_NOT_COMPLETED) {
831,019,630✔
94
    pTaskInfo->status = status;
259,926,538✔
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);
571,093,092✔
98
    pTaskInfo->status |= status;
571,123,591✔
99
  }
100
}
831,057,458✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
259,661,465✔
107
  ctx->taskId = pTaskInfo->id.taskId;
259,607,681✔
108
  ctx->idStr = pTaskInfo->id.str;
259,617,048✔
109
  ctx->pTaskInfo = pTaskInfo;
259,600,304✔
110
  ctx->subEndPoints = subEndPoints;
259,666,767✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
259,523,598✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
259,663,029✔
114
  if (subJobNum > 0) {
259,648,007✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
3,328,896✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
3,330,328✔
117
      qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
118
      return terrno;
×
119
    }
120
    
121
    int32_t code = tsem_init(&ctx->ready, 0, 0);
3,324,242✔
122
    if (code) {
3,325,316✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
3,325,316✔
128

129
    qDebug("%s subJobCtx with %d endPoints inited", pTaskInfo->id.str, subJobNum);
3,330,328✔
130
  }
131

132
  return TSDB_CODE_SUCCESS;
259,604,079✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
259,677,766✔
138
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
139
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
259,677,766✔
140
  if (*pTaskInfo == NULL || code != 0) {
259,611,948✔
141
    nodesDestroyNode((SNode*)pPlan);
184✔
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
259,620,123✔
146

147
  if (pHandle) {
259,638,077✔
148
    if (pHandle->pStateBackend) {
259,613,338✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
259,642,801✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
257,117,707✔
156
    if (NULL == (*pTaskInfo)->sql) {
257,141,939✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
259,631,506✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
259,655,303✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
259,587,979✔
168
  if (code != TSDB_CODE_SUCCESS) {
259,579,415✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
259,579,415✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
259,589,358✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
259,560,520✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
259,393,481✔
180
    doDestroyTask(*pTaskInfo);
445,179✔
181
    (*pTaskInfo) = NULL;
312,496✔
182
  }
183
  return code;
259,295,372✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
180,114,969✔
187
  SSchemaInfo* pSchemaInfo = p;
180,114,969✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
180,114,969✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
180,078,583✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
180,013,029✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
180,029,343✔
193
}
180,007,205✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
180,175,687✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
180,175,687✔
198
  if (pHandle == NULL) {
180,181,951✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
180,181,951✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
180,169,340✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
180,183,076✔
206
  if (code != TSDB_CODE_SUCCESS) {
179,981,623✔
207
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
208
           GET_TASKID(pTaskInfo));
209

210
    pAPI->metaReaderFn.clearReader(&mr);
×
211
    return code;
×
212
  }
213

214
  SSchemaInfo schemaInfo = {0};
179,981,623✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
179,967,960✔
217
  schemaInfo.dbname = taosStrdup(dbName);
180,032,778✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
180,017,821✔
219
    pAPI->metaReaderFn.clearReader(&mr);
124,306✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
179,893,515✔
225
    schemaInfo.rversion = mr.me.colRef.version;
280,482✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
179,893,515✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
109,174,854✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
109,174,854✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
70,777,043✔
232
    tDecoderClear(&mr.coder);
35,902,858✔
233

234
    tb_uid_t suid = mr.me.ctbEntry.suid;
35,918,612✔
235
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
35,918,612✔
236
    if (code != TSDB_CODE_SUCCESS) {
35,918,291✔
237
      pAPI->metaReaderFn.clearReader(&mr);
×
238
      cleanupQueriedTableScanInfo(&schemaInfo);
×
239
      return code;
×
240
    }
241

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
35,917,557✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
35,917,557✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
34,908,346✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
180,000,757✔
249

250
  if (schemaInfo.sw == NULL) {
179,871,047✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
179,871,047✔
256
  if (schemaInfo.qsw == NULL) {
180,020,901✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
180,020,901✔
262
  if (p == NULL) {
180,079,322✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
180,079,322✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
179,909,728✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
179,909,728✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
180,053,897✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
180,004,930✔
275
  if (pqSw == NULL) {
179,865,136✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
179,865,136✔
280
  if (pqSw->pSchema == NULL) {
179,701,862✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
912,907,958✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
732,855,766✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
732,930,870✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
732,935,913✔
290
    pSchema->colId = pColNode->colId;
732,897,841✔
291
    pSchema->type = pColNode->node.resType.type;
733,078,369✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
733,053,743✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
732,805,145✔
294
  }
295

296
  // this the tags and pseudo function columns, we only keep the tag columns
297
  for (int32_t i = 0; i < numOfTags; ++i) {
359,494,509✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
179,466,879✔
299

300
    int32_t type = nodeType(pNode->pExpr);
179,547,206✔
301
    if (type == QUERY_NODE_COLUMN) {
179,583,038✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
138,725,160✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
138,700,675✔
305
      pSchema->colId = pColNode->colId;
138,582,444✔
306
      pSchema->type = pColNode->node.resType.type;
138,718,598✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
138,629,028✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
138,689,785✔
309
    }
310
  }
311

312
  return pqSw;
180,027,630✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
259,674,375✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
259,674,375✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
259,727,468✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
259,598,780✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
259,724,256✔
320
}
259,628,592✔
321

322
static void freeBlock(void* pParam) {
387,224,478✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
387,224,478✔
324
  blockDataDestroy(pBlock);
387,238,863✔
325
}
387,231,836✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
259,736,224✔
329
  if (pCtx->transporterId > 0) {
259,736,224✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
5,370✔
331
    if (ret != 0) {
5,370✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
5,370✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
259,764,035✔
337
}
259,759,286✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
259,804,672✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
259,804,672✔
341
  destroyOperator(pTaskInfo->pRoot);
259,804,672✔
342
  pTaskInfo->pRoot = NULL;
259,675,390✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
259,701,417✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
259,766,856✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
259,658,746✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
259,673,346✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
259,694,468✔
351
    pTaskInfo->pSubplan = NULL;
259,668,894✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
259,682,825✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
259,705,101✔
356
  if (!pTaskInfo->paramSet) {
259,677,349✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
252,421,791✔
358
    pTaskInfo->pOpParam = NULL;
252,395,959✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
259,674,614✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
259,681,089✔
362
  taosMemoryFreeClear(pTaskInfo);
259,670,499✔
363
}
259,573,998✔
364

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