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

taosdata / TDengine / #4965

09 Feb 2026 01:16AM UTC coverage: 66.884% (-0.02%) from 66.908%
#4965

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205798 of 307696 relevant lines covered (66.88%)

126382200.96 hits per line

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

81.28
/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,
265,620,814✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
265,620,814✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
265,620,814✔
45
  if (p == NULL) {
265,584,043✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
265,584,043✔
50
  p->cost.created = taosGetTimestampUs();
265,640,186✔
51

52
  p->execModel = model;
265,631,508✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
265,638,270✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
265,609,321✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
265,606,365✔
56
    doDestroyTask(p);
3,460✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
265,614,148✔
61
  taosInitRWLatch(&p->lock);
265,626,057✔
62

63
  p->id.vgId = vgId;
265,609,218✔
64
  p->id.queryId = queryId;
265,599,119✔
65
  p->id.taskId = taskId;
265,612,413✔
66
  p->id.str = taosMemoryMalloc(64);
265,619,008✔
67
  if (p->id.str == NULL) {
265,577,942✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
265,597,398✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
265,579,108✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
265,555,898✔
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
265,587,089✔
80
  return TSDB_CODE_SUCCESS;
265,591,106✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,709,131,963✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
852,530,549✔
93
  if (status == TASK_NOT_COMPLETED) {
852,530,549✔
94
    pTaskInfo->status = status;
265,774,042✔
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);
586,756,507✔
98
    pTaskInfo->status |= status;
586,767,421✔
99
  }
100
}
852,560,697✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
265,481,602✔
107
  ctx->taskId = pTaskInfo->id.taskId;
265,440,616✔
108
  ctx->idStr = pTaskInfo->id.str;
265,447,401✔
109
  ctx->pTaskInfo = pTaskInfo;
265,443,670✔
110
  ctx->subEndPoints = subEndPoints;
265,484,758✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
265,406,483✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
265,478,269✔
114
  if (subJobNum > 0) {
265,470,308✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
4,866,033✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
4,867,629✔
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);
4,864,437✔
122
    if (code) {
4,860,181✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
4,860,181✔
128

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

132
  return TSDB_CODE_SUCCESS;
265,454,383✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
265,498,654✔
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);
265,498,654✔
140
  if (*pTaskInfo == NULL || code != 0) {
265,447,822✔
141
    nodesDestroyNode((SNode*)pPlan);
1✔
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
265,460,661✔
146

147
  if (pHandle) {
265,482,555✔
148
    if (pHandle->pStateBackend) {
265,460,342✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
265,488,722✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
262,745,393✔
156
    if (NULL == (*pTaskInfo)->sql) {
262,779,730✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
265,518,067✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
265,483,298✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
265,449,832✔
168
  if (code != TSDB_CODE_SUCCESS) {
265,435,652✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
265,435,652✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
265,426,572✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
265,368,558✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
265,273,576✔
180
    doDestroyTask(*pTaskInfo);
565,870✔
181
    (*pTaskInfo) = NULL;
453,828✔
182
  }
183
  return code;
265,206,282✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
185,676,596✔
187
  SSchemaInfo* pSchemaInfo = p;
185,676,596✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
185,676,596✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
185,654,611✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
185,617,637✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
185,607,109✔
193
}
185,593,049✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
185,708,822✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
185,708,822✔
198
  if (pHandle == NULL) {
185,720,029✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
185,720,029✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
185,712,306✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
185,714,823✔
206
  if (code != TSDB_CODE_SUCCESS) {
185,566,002✔
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};
185,566,002✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
185,557,634✔
217
  schemaInfo.dbname = taosStrdup(dbName);
185,545,234✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
185,583,254✔
219
    pAPI->metaReaderFn.clearReader(&mr);
110,841✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
185,472,413✔
225
    schemaInfo.rversion = mr.me.colRef.version;
243,380✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
185,472,413✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
106,057,643✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
106,057,643✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
79,460,642✔
232
    tDecoderClear(&mr.coder);
37,763,163✔
233

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

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
37,798,342✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
37,798,342✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
41,706,225✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
185,562,210✔
249

250
  if (schemaInfo.sw == NULL) {
185,486,481✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
185,486,481✔
256
  if (schemaInfo.qsw == NULL) {
185,567,029✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
185,567,029✔
262
  if (p == NULL) {
185,635,627✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
185,635,627✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
185,521,330✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
185,521,330✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
185,621,306✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
185,564,408✔
275
  if (pqSw == NULL) {
185,480,559✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
185,480,559✔
280
  if (pqSw->pSchema == NULL) {
185,343,226✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
922,585,065✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
736,948,426✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
736,852,448✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
736,859,533✔
290
    pSchema->colId = pColNode->colId;
736,990,837✔
291
    pSchema->type = pColNode->node.resType.type;
737,135,483✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
737,070,005✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
737,085,498✔
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) {
319,279,904✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
133,649,194✔
299

300
    int32_t type = nodeType(pNode->pExpr);
133,659,967✔
301
    if (type == QUERY_NODE_COLUMN) {
133,683,177✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
97,279,899✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
97,246,921✔
305
      pSchema->colId = pColNode->colId;
97,207,492✔
306
      pSchema->type = pColNode->node.resType.type;
97,275,919✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
97,220,594✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
97,255,945✔
309
    }
310
  }
311

312
  return pqSw;
185,630,710✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
265,529,428✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
265,529,428✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
265,577,545✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
265,476,665✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
265,575,607✔
320
}
265,507,749✔
321

322
static void freeBlock(void* pParam) {
432,232,843✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
432,232,843✔
324
  blockDataDestroy(pBlock);
432,239,639✔
325
}
432,241,567✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
265,588,415✔
329
  if (pCtx->transporterId > 0) {
265,588,415✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
5,852✔
331
    if (ret != 0) {
5,852✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
5,852✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
265,614,847✔
337
}
265,595,589✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
265,631,947✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
265,631,947✔
341
  destroyOperator(pTaskInfo->pRoot);
265,631,947✔
342
  pTaskInfo->pRoot = NULL;
265,539,794✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
265,552,679✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
265,599,246✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
265,538,623✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
265,532,560✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
265,551,496✔
351
    pTaskInfo->pSubplan = NULL;
265,529,891✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
265,553,572✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
265,570,298✔
356
  if (!pTaskInfo->paramSet) {
265,547,380✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
258,817,871✔
358
    pTaskInfo->pOpParam = NULL;
258,779,747✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
265,561,953✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
265,545,333✔
362
  taosMemoryFreeClear(pTaskInfo);
265,523,030✔
363
}
265,446,755✔
364

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