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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

0.0
/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
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
×
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
×
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
×
46
  if (p == NULL) {
×
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
×
51
  p->cost.created = taosGetTimestampUs();
×
52

53
  p->execModel = model;
×
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
×
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
×
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
×
57
    doDestroyTask(p);
×
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
×
62
  taosInitRWLatch(&p->lock);
×
63

64
  p->id.vgId = vgId;
×
65
  p->id.queryId = queryId;
×
66
  p->id.taskId = taskId;
×
67
  p->id.str = taosMemoryMalloc(64);
×
68
  if (p->id.str == NULL) {
×
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
×
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
×
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
×
76
    doDestroyTask(p);
×
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
×
81
  return TSDB_CODE_SUCCESS;
×
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
×
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
×
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
×
94
  if (status == TASK_NOT_COMPLETED) {
×
95
    pTaskInfo->status = status;
×
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
×
99
    pTaskInfo->status |= status;
×
100
  }
101
}
×
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
×
105
  int32_t code = 0, lino = 0;
×
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
×
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
×
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
×
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
×
112

113
  ctx->queryId = pTaskInfo->id.queryId;
×
114
  ctx->taskId = pTaskInfo->id.taskId;
×
115
  ctx->idStr = pTaskInfo->id.str;
×
116
  ctx->pTaskInfo = pTaskInfo;
×
117
  ctx->subEndPoints = *subEndPoints;
×
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
×
119

120
  *subEndPoints = NULL;
×
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
×
123
  if (NULL == ctx->subResNodes) {
×
124
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
125
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
126
  }
127
  
128
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
×
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
×
131
  if (refId < 0) {
×
132
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
133
    TAOS_CHECK_EXIT(terrno);
×
134
  }
135
  
136
  ctx->subJobRefId = refId;
×
137

138
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
×
139

140
_exit:
×
141

142
  if (code) {
×
143
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
144
    pTaskInfo->pSubJobCtx = NULL;
×
145
    
146
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
147
  }
148

149
  return code;
×
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
×
155
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
156
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
×
157
  if (*pTaskInfo == NULL || code != 0) {
×
158
    nodesDestroyNode((SNode*)pPlan);
×
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
×
163

164
  if (NULL != sql) {
×
165
    (*pTaskInfo)->sql = taosStrdup(sql);
×
166
    if (NULL == (*pTaskInfo)->sql) {
×
167
      code = terrno;
×
168
      doDestroyTask(*pTaskInfo);
×
169
      (*pTaskInfo) = NULL;
×
170
      return code;
×
171
    }
172
  }
173

174
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
×
175
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
×
176

177
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
×
178
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
×
179
    if (code != TSDB_CODE_SUCCESS) {
×
180
      doDestroyTask(*pTaskInfo);
×
181
      (*pTaskInfo) = NULL;
×
182
      return code;
×
183
    }
184
  }
185
  
186
  setTaskScalarExtraInfo(*pTaskInfo);
×
187
  
188
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
×
189
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
×
190

191
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
×
192
    doDestroyTask(*pTaskInfo);
×
193
    (*pTaskInfo) = NULL;
×
194
  }
195
  return code;
×
196
}
197

198
void cleanupQueriedTableScanInfo(void* p) {
×
199
  SSchemaInfo* pSchemaInfo = p;
×
200

201
  taosMemoryFreeClear(pSchemaInfo->dbname);
×
202
  taosMemoryFreeClear(pSchemaInfo->tablename);
×
203
  tDeleteSchemaWrapper(pSchemaInfo->sw);
×
204
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
×
205
}
×
206

207
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
×
208
                                   SExecTaskInfo* pTaskInfo) {
209
  SMetaReader mr = {0};
×
210
  if (pHandle == NULL) {
×
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213

214
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
×
215

216
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
×
217
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
×
218
  if (code != TSDB_CODE_SUCCESS) {
×
219
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
220
           GET_TASKID(pTaskInfo));
221

222
    pAPI->metaReaderFn.clearReader(&mr);
×
223
    return code;
×
224
  }
225

226
  SSchemaInfo schemaInfo = {0};
×
227

228
  schemaInfo.tablename = taosStrdup(mr.me.name);
×
229
  schemaInfo.dbname = taosStrdup(dbName);
×
230
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
×
231
    pAPI->metaReaderFn.clearReader(&mr);
×
232
    cleanupQueriedTableScanInfo(&schemaInfo);
×
233
    return terrno;
×
234
  }
235

236
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
237
    schemaInfo.rversion = mr.me.colRef.version;
×
238
  }
239

240
  if (mr.me.type == TSDB_SUPER_TABLE) {
×
241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
×
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
×
243
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
244
    tDecoderClear(&mr.coder);
×
245

246
    tb_uid_t suid = mr.me.ctbEntry.suid;
×
247
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
×
248
    if (code != TSDB_CODE_SUCCESS) {
×
249
      pAPI->metaReaderFn.clearReader(&mr);
×
250
      cleanupQueriedTableScanInfo(&schemaInfo);
×
251
      return code;
×
252
    }
253

254
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
×
255
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
×
256
  } else {
257
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
×
258
  }
259

260
  pAPI->metaReaderFn.clearReader(&mr);
×
261

262
  if (schemaInfo.sw == NULL) {
×
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
×
268
  if (schemaInfo.qsw == NULL) {
×
269
    cleanupQueriedTableScanInfo(&schemaInfo);
×
270
    return terrno;
×
271
  }
272

273
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
×
274
  if (p == NULL) {
×
275
    cleanupQueriedTableScanInfo(&schemaInfo);
×
276
    return terrno;
×
277
  }
278

279
  return code;
×
280
}
281

282
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
×
283
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
×
284
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
×
285

286
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
×
287
  if (pqSw == NULL) {
×
288
    return NULL;
×
289
  }
290

291
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
×
292
  if (pqSw->pSchema == NULL) {
×
293
    taosMemoryFree(pqSw);
×
294
    return NULL;
×
295
  }
296

297
  for (int32_t i = 0; i < numOfCols; ++i) {
×
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
×
299
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
×
300

301
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
×
302
    pSchema->colId = pColNode->colId;
×
303
    pSchema->type = pColNode->node.resType.type;
×
304
    pSchema->bytes = pColNode->node.resType.bytes;
×
305
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
×
306
  }
307

308
  // this the tags and pseudo function columns, we only keep the tag columns
309
  for (int32_t i = 0; i < numOfTags; ++i) {
×
310
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
×
311

312
    int32_t type = nodeType(pNode->pExpr);
×
313
    if (type == QUERY_NODE_COLUMN) {
×
314
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
×
315

316
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
×
317
      pSchema->colId = pColNode->colId;
×
318
      pSchema->type = pColNode->node.resType.type;
×
319
      pSchema->bytes = pColNode->node.resType.bytes;
×
320
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
×
321
    }
322
  }
323

324
  return pqSw;
×
325
}
326

327
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
×
328
  tDeleteSchemaWrapper(pTmqInfo->schema);
×
329
  tOffsetDestroy(&pTmqInfo->currentOffset);
×
330
}
×
331

332
static void freeBlock(void* pParam) {
×
333
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
×
334
  blockDataDestroy(pBlock);
×
335
}
×
336

337

338
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
×
339
  if (pCtx->transporterId > 0) {
×
340
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
×
341
    if (ret != 0) {
×
342
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
343
    }
344
    pCtx->transporterId = -1;
×
345
  }
346

347
  if (pCtx->subEndPoints != NULL) {
×
348
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
×
349
    if (size > 0) {
×
350
      int32_t code = tsem_destroy(&pCtx->ready);
×
351
      if (code != TSDB_CODE_SUCCESS) {
×
352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
353
      }
354
      taosArrayDestroy(pCtx->subResNodes);
×
355
    }
356
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
×
357
    pCtx->subEndPoints = NULL;
×
358
  }
359
  
360
  taosMemoryFreeClear(pCtx);  
×
361
}
×
362

363
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
×
364
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
×
365
  destroyOperator(pTaskInfo->pRoot);
×
366
  pTaskInfo->pRoot = NULL;
×
367

368
  if (pTaskInfo->pSubJobCtx) {
×
369
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
×
370
    if (code != TSDB_CODE_SUCCESS) {
×
371
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
372
    }
373
  }
374

375
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
×
376
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
×
377

378
  if (!pTaskInfo->localFetch.localExec) {
×
379
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
×
380
    pTaskInfo->pSubplan = NULL;
×
381
  }
382

383
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
×
384
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
×
385
  if (!pTaskInfo->paramSet) {
×
386
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
×
387
    pTaskInfo->pOpParam = NULL;
×
388
  }
389
  taosMemoryFreeClear(pTaskInfo->sql);
×
390
  taosMemoryFreeClear(pTaskInfo->id.str);
×
391
  taosMemoryFreeClear(pTaskInfo);
×
392
}
×
393

394
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
×
395
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
×
396
  if (ret < 0) {
×
397
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
398
  }
399
}
×
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