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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 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,
305,062,065✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
305,062,065✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
305,062,065✔
45
  if (p == NULL) {
304,976,457✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
304,976,457✔
50
  p->cost.created = taosGetTimestampUs();
305,101,605✔
51

52
  p->execModel = model;
305,100,418✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
305,068,624✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
305,023,814✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
305,026,678✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
305,077,080✔
61
  taosInitRWLatch(&p->lock);
305,076,379✔
62

63
  p->id.vgId = vgId;
305,065,675✔
64
  p->id.queryId = queryId;
305,071,197✔
65
  p->id.taskId = taskId;
305,074,368✔
66
  p->id.str = taosMemoryMalloc(64);
305,070,211✔
67
  if (p->id.str == NULL) {
304,970,041✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
305,049,582✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
305,012,004✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
304,997,795✔
75
    doDestroyTask(p);
270✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
305,048,985✔
80
  return TSDB_CODE_SUCCESS;
305,047,994✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,647,295,069✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
951,497,242✔
93
  if (status == TASK_NOT_COMPLETED) {
951,497,242✔
94
    pTaskInfo->status = status;
305,094,117✔
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);
646,403,125✔
98
    pTaskInfo->status |= status;
646,419,948✔
99
  }
100
}
951,565,140✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
304,962,915✔
107
  ctx->idStr = pTaskInfo->id.str;
304,927,146✔
108
  ctx->pTaskInfo = pTaskInfo;
304,950,813✔
109
  ctx->subEndPoints = subEndPoints;
304,912,305✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
304,911,354✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
304,945,367✔
113
  if (subJobNum > 0) {
304,926,801✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
61,969,913✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
61,961,938✔
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);
61,942,565✔
121
    if (code) {
61,960,020✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
61,960,020✔
127

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

131
  return TSDB_CODE_SUCCESS;
304,895,756✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
304,944,888✔
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);
304,944,888✔
139
  if (*pTaskInfo == NULL || code != 0) {
304,906,833✔
140
    nodesDestroyNode((SNode*)pPlan);
397✔
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
304,927,772✔
145

146
  if (pHandle) {
304,947,531✔
147
    if (pHandle->pStateBackend) {
304,883,695✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
304,979,088✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
302,432,855✔
155
    if (NULL == (*pTaskInfo)->sql) {
302,464,720✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
304,999,951✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
304,954,251✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
304,915,211✔
167
  if (code != TSDB_CODE_SUCCESS) {
304,883,811✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
304,883,811✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
304,899,752✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
304,808,208✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
304,777,735✔
179
    doDestroyTask(*pTaskInfo);
4,227,318✔
180
    (*pTaskInfo) = NULL;
4,182,136✔
181
  }
182
  return code;
304,783,779✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
238,069,547✔
186
  SSchemaInfo* pSchemaInfo = p;
238,069,547✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
238,069,547✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
238,064,467✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
238,050,466✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
238,049,705✔
192
}
238,044,595✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
238,072,289✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
238,072,289✔
197
  if (pHandle == NULL) {
238,081,254✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
238,081,254✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
238,073,991✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
238,076,557✔
205
  if (code != TSDB_CODE_SUCCESS) {
237,952,480✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
201✔
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
237,952,279✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
237,954,033✔
216
  schemaInfo.dbname = taosStrdup(dbName);
237,928,556✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
237,937,642✔
218
    pAPI->metaReaderFn.clearReader(&mr);
96,177✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
237,841,495✔
224
    schemaInfo.rversion = mr.me.colRef.version;
1,824✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
237,841,495✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
104,545,015✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
104,545,015✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
133,322,684✔
231
    tDecoderClear(&mr.coder);
78,302,770✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
78,342,172✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
78,342,172✔
235
    if (code != TSDB_CODE_SUCCESS) {
78,353,994✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
78,328,173✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
78,328,173✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
55,029,545✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
237,902,733✔
248

249
  if (schemaInfo.sw == NULL) {
237,768,793✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
237,768,793✔
255
  if (schemaInfo.qsw == NULL) {
237,958,678✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
237,958,678✔
261
  if (p == NULL) {
237,986,570✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
237,986,570✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
237,853,915✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
237,853,915✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
237,970,118✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
237,944,283✔
274
  if (pqSw == NULL) {
237,799,531✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
237,799,531✔
279
  if (pqSw->pSchema == NULL) {
237,658,684✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
860,693,499✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
622,681,630✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
622,811,236✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
622,819,090✔
289
    pSchema->colId = pColNode->colId;
622,814,032✔
290
    pSchema->type = pColNode->node.resType.type;
622,885,013✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
622,821,379✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
622,838,089✔
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) {
382,852,984✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
144,847,825✔
298

299
    int32_t type = nodeType(pNode->pExpr);
144,893,509✔
300
    if (type == QUERY_NODE_COLUMN) {
144,907,545✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
110,138,529✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
110,120,001✔
304
      pSchema->colId = pColNode->colId;
110,089,858✔
305
      pSchema->type = pColNode->node.resType.type;
110,136,625✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
110,102,551✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
110,119,053✔
308
    }
309
  }
310

311
  return pqSw;
238,005,159✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
305,080,631✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
305,080,631✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
305,087,174✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
305,086,162✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
305,087,386✔
319
}
305,076,007✔
320

321
static void freeBlock(void* pParam) {
427,224,526✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
427,224,526✔
323
  blockDataDestroy(pBlock);
427,230,909✔
324
}
427,231,701✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
305,090,069✔
328
  if (pCtx->transporterId > 0) {
305,090,069✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
8,144✔
330
    if (ret != 0) {
8,144✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
8,144✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
305,096,177✔
336
}
305,100,656✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
305,078,735✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
305,078,735✔
340
  destroyOperator(pTaskInfo->pRoot);
305,078,319✔
341
  pTaskInfo->pRoot = NULL;
305,086,864✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
305,092,323✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
305,103,732✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
305,082,005✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
305,088,689✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
305,092,773✔
350
    pTaskInfo->pSubplan = NULL;
305,060,129✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
305,071,355✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
305,087,397✔
355
  if (!pTaskInfo->paramSet) {
305,067,548✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
304,085,997✔
357
    pTaskInfo->pOpParam = NULL;
304,072,045✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
305,070,508✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
305,071,927✔
361
  taosMemoryFreeClear(pTaskInfo);
305,061,119✔
362
}
305,042,043✔
363

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