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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

80.73
/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,
302,725,602✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
302,725,602✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
302,725,602✔
45
  if (p == NULL) {
302,645,962✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
302,645,962✔
50
  p->cost.created = taosGetTimestampUs();
302,758,500✔
51

52
  p->execModel = model;
302,752,900✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
302,753,461✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
302,704,985✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
302,696,094✔
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
302,728,369✔
61
  taosInitRWLatch(&p->lock);
302,748,889✔
62

63
  p->id.vgId = vgId;
302,724,879✔
64
  p->id.queryId = queryId;
302,712,741✔
65
  p->id.taskId = taskId;
302,741,265✔
66
  p->id.str = taosMemoryMalloc(64);
302,738,150✔
67
  if (p->id.str == NULL) {
302,684,727✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
302,721,819✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
302,689,850✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
302,614,788✔
75
    doDestroyTask(p);
175✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
302,680,656✔
80
  return TSDB_CODE_SUCCESS;
302,680,717✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,583,677,005✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
940,444,897✔
93
  if (status == TASK_NOT_COMPLETED) {
940,444,897✔
94
    pTaskInfo->status = status;
302,768,950✔
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);
637,675,947✔
98
    pTaskInfo->status |= status;
637,700,805✔
99
  }
100
}
940,548,433✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
302,634,328✔
107
  ctx->idStr = pTaskInfo->id.str;
302,620,186✔
108
  ctx->pTaskInfo = pTaskInfo;
302,582,941✔
109
  ctx->subEndPoints = subEndPoints;
302,633,351✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
302,527,406✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
302,595,903✔
113
  if (subJobNum > 0) {
302,583,724✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
60,917,579✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
60,912,867✔
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);
60,892,949✔
121
    if (code) {
60,881,216✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
60,881,216✔
127

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

131
  return TSDB_CODE_SUCCESS;
302,559,601✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
302,613,257✔
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);
302,613,257✔
139
  if (*pTaskInfo == NULL || code != 0) {
302,559,528✔
140
    nodesDestroyNode((SNode*)pPlan);
×
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
302,580,588✔
145

146
  if (pHandle) {
302,596,580✔
147
    if (pHandle->pStateBackend) {
302,523,121✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
302,624,575✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
300,134,210✔
155
    if (NULL == (*pTaskInfo)->sql) {
300,144,378✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
302,669,938✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
302,584,241✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
302,641,401✔
167
  if (code != TSDB_CODE_SUCCESS) {
302,536,041✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
302,536,041✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
302,557,783✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
302,552,356✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
302,447,450✔
179
    doDestroyTask(*pTaskInfo);
4,217,187✔
180
    (*pTaskInfo) = NULL;
4,159,530✔
181
  }
182
  return code;
302,480,934✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
237,317,820✔
186
  SSchemaInfo* pSchemaInfo = p;
237,317,820✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
237,317,820✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
237,311,426✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
237,287,880✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
237,303,301✔
192
}
237,293,515✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
237,312,370✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
237,312,370✔
197
  if (pHandle == NULL) {
237,323,408✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
237,323,408✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
237,309,906✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
237,322,570✔
205
  if (code != TSDB_CODE_SUCCESS) {
237,142,851✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
207
           GET_TASKID(pTaskInfo));
208

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

213
  SSchemaInfo schemaInfo = {0};
237,142,851✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
237,150,347✔
216
  schemaInfo.dbname = taosStrdup(dbName);
237,162,729✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
237,185,538✔
218
    pAPI->metaReaderFn.clearReader(&mr);
120,758✔
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,064,780✔
224
    schemaInfo.rversion = mr.me.colRef.version;
11,016✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
237,064,780✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
97,991,037✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
97,991,037✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
139,168,924✔
231
    tDecoderClear(&mr.coder);
81,146,321✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
81,161,706✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
81,161,706✔
235
    if (code != TSDB_CODE_SUCCESS) {
81,177,878✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
81,170,475✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
81,170,475✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
58,023,159✔
245
  }
246

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

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

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
237,060,429✔
255
  if (schemaInfo.qsw == NULL) {
237,158,873✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
237,158,873✔
261
  if (p == NULL) {
237,207,831✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
237,207,831✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
237,082,071✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
237,082,071✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
237,246,466✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
237,193,213✔
274
  if (pqSw == NULL) {
237,035,542✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
237,035,542✔
279
  if (pqSw->pSchema == NULL) {
236,847,050✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,008,652,629✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
771,393,583✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
771,350,682✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
771,425,692✔
289
    pSchema->colId = pColNode->colId;
771,396,057✔
290
    pSchema->type = pColNode->node.resType.type;
771,572,669✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
771,560,263✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
771,654,069✔
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) {
369,012,132✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
131,740,879✔
298

299
    int32_t type = nodeType(pNode->pExpr);
131,731,405✔
300
    if (type == QUERY_NODE_COLUMN) {
131,738,538✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
98,051,742✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
98,056,078✔
304
      pSchema->colId = pColNode->colId;
98,075,095✔
305
      pSchema->type = pColNode->node.resType.type;
98,035,874✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
98,047,606✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
98,056,749✔
308
    }
309
  }
310

311
  return pqSw;
237,271,253✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
302,730,570✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
302,730,570✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
302,753,422✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
302,731,356✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
302,748,064✔
319
}
302,745,067✔
320

321
static void freeBlock(void* pParam) {
416,706,609✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
416,706,609✔
323
  blockDataDestroy(pBlock);
416,712,844✔
324
}
416,717,905✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
302,755,671✔
328
  if (pCtx->transporterId > 0) {
302,755,671✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
2,544✔
330
    if (ret != 0) {
2,544✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
2,544✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
302,769,390✔
336
}
302,766,204✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
302,712,374✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
302,712,374✔
340
  destroyOperator(pTaskInfo->pRoot);
302,711,425✔
341
  pTaskInfo->pRoot = NULL;
302,751,203✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
302,765,271✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
302,754,471✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
302,738,379✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
302,741,940✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
302,734,928✔
350
    pTaskInfo->pSubplan = NULL;
302,753,900✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
302,765,622✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
302,754,164✔
355
  if (!pTaskInfo->paramSet) {
302,753,450✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
302,484,631✔
357
    pTaskInfo->pOpParam = NULL;
302,462,371✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
302,746,420✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
302,734,629✔
361
  taosMemoryFreeClear(pTaskInfo);
302,722,930✔
362
}
302,694,082✔
363

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