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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

81.65
/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,
368,940,324✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
368,940,324✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
368,940,324✔
45
  if (p == NULL) {
368,596,308✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
368,596,308✔
50
  p->cost.created = taosGetTimestampUs();
369,060,340✔
51

52
  p->execModel = model;
369,031,546✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
369,033,475✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
368,877,407✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
368,839,183✔
56
    doDestroyTask(p);
125✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
368,908,729✔
61
  taosInitRWLatch(&p->lock);
369,034,451✔
62

63
  p->id.vgId = vgId;
368,922,955✔
64
  p->id.queryId = queryId;
368,880,109✔
65
  p->id.taskId = taskId;
368,976,924✔
66
  p->id.str = taosMemoryMalloc(64);
368,970,901✔
67
  if (p->id.str == NULL) {
368,734,821✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
368,794,349✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
368,766,316✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
368,652,780✔
75
    doDestroyTask(p);
175✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
368,793,708✔
80
  return TSDB_CODE_SUCCESS;
368,868,436✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,843,812,503✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,133,161,680✔
93
  if (status == TASK_NOT_COMPLETED) {
1,133,161,680✔
94
    pTaskInfo->status = status;
368,824,967✔
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);
764,336,713✔
98
    pTaskInfo->status |= status;
764,406,120✔
99
  }
100
}
1,133,352,991✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
368,900,358✔
107
  ctx->idStr = pTaskInfo->id.str;
368,766,252✔
108
  ctx->pTaskInfo = pTaskInfo;
368,737,734✔
109
  ctx->subEndPoints = subEndPoints;
368,914,949✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
368,539,553✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
368,820,083✔
113
  if (subJobNum > 0) {
368,748,546✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
101,611,996✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
101,582,350✔
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);
101,469,760✔
121
    if (code) {
101,420,259✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
101,420,259✔
127

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

131
  return TSDB_CODE_SUCCESS;
368,631,016✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
368,812,924✔
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);
368,812,924✔
139
  if (*pTaskInfo == NULL || code != 0) {
368,695,696✔
140
    nodesDestroyNode((SNode*)pPlan);
63✔
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
368,756,159✔
145

146
  if (pHandle) {
368,780,472✔
147
    if (pHandle->pStateBackend) {
368,514,189✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
368,860,432✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
366,216,705✔
155
    if (NULL == (*pTaskInfo)->sql) {
366,164,593✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
368,970,763✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
368,642,327✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
368,918,867✔
167
  if (code != TSDB_CODE_SUCCESS) {
368,567,389✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
368,567,389✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
368,682,005✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
368,636,261✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
368,427,715✔
179
    doDestroyTask(*pTaskInfo);
7,823,989✔
180
    (*pTaskInfo) = NULL;
7,403,886✔
181
  }
182
  return code;
368,251,502✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
285,386,410✔
186
  SSchemaInfo* pSchemaInfo = p;
285,386,410✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
285,386,410✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
285,332,855✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
285,297,582✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
285,361,517✔
192
}
285,348,363✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
285,398,817✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
285,398,817✔
197
  if (pHandle == NULL) {
285,419,700✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
285,419,700✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
285,395,548✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
285,406,083✔
205
  if (code != TSDB_CODE_SUCCESS) {
284,661,996✔
UNCOV
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
284,661,996✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
284,918,960✔
216
  schemaInfo.dbname = taosStrdup(dbName);
284,910,797✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
284,894,643✔
218
    pAPI->metaReaderFn.clearReader(&mr);
289,869✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
284,604,774✔
224
    schemaInfo.rversion = mr.me.colRef.version;
1,854✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
284,604,774✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
122,612,645✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
122,612,645✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
162,059,190✔
231
    tDecoderClear(&mr.coder);
85,078,348✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
85,330,651✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
85,330,651✔
235
    if (code != TSDB_CODE_SUCCESS) {
85,324,122✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
85,334,945✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
85,334,945✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
76,958,530✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
284,906,120✔
248

249
  if (schemaInfo.sw == NULL) {
284,571,605✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
284,571,605✔
255
  if (schemaInfo.qsw == NULL) {
284,935,997✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
284,935,997✔
261
  if (p == NULL) {
285,180,805✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
285,180,805✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
284,858,038✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
284,858,038✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
285,175,004✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
285,040,576✔
274
  if (pqSw == NULL) {
284,566,481✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
284,566,481✔
279
  if (pqSw->pSchema == NULL) {
284,101,622✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,149,459,372✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
864,313,774✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
864,862,883✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
865,010,226✔
289
    pSchema->colId = pColNode->colId;
864,541,314✔
290
    pSchema->type = pColNode->node.resType.type;
864,955,294✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
864,664,012✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
865,171,652✔
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) {
422,943,076✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
137,784,998✔
298

299
    int32_t type = nodeType(pNode->pExpr);
137,806,219✔
300
    if (type == QUERY_NODE_COLUMN) {
137,839,712✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
103,188,974✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
103,190,998✔
304
      pSchema->colId = pColNode->colId;
103,201,319✔
305
      pSchema->type = pColNode->node.resType.type;
103,151,683✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
103,174,574✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
103,155,548✔
308
    }
309
  }
310

311
  return pqSw;
285,158,078✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
369,040,786✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
369,040,786✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
369,070,023✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
368,982,920✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
369,042,984✔
319
}
369,076,898✔
320

321
static void freeBlock(void* pParam) {
446,120,865✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
446,120,865✔
323
  blockDataDestroy(pBlock);
446,130,659✔
324
}
446,122,755✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
369,059,517✔
328
  if (pCtx->transporterId > 0) {
369,059,517✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
44,688✔
330
    if (ret != 0) {
44,688✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
44,688✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
369,099,911✔
336
}
369,089,039✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
368,991,687✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
368,991,687✔
340
  destroyOperator(pTaskInfo->pRoot);
368,991,009✔
341
  pTaskInfo->pRoot = NULL;
369,061,700✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
369,102,676✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
369,055,224✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
369,019,686✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
369,075,650✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
369,019,773✔
350
    pTaskInfo->pSubplan = NULL;
369,064,704✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
369,136,310✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
369,053,457✔
355
  if (!pTaskInfo->paramSet) {
369,073,837✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
368,639,227✔
357
    pTaskInfo->pOpParam = NULL;
368,565,212✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
369,061,129✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
369,016,496✔
361
  taosMemoryFreeClear(pTaskInfo);
368,958,224✔
362
}
368,880,639✔
363

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