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

taosdata / TDengine / #4963

09 Feb 2026 01:16AM UTC coverage: 66.867% (-0.005%) from 66.872%
#4963

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

205748 of 307696 relevant lines covered (66.87%)

127038261.06 hits per line

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

82.19
/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,
266,988,075✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
266,988,075✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
266,988,075✔
45
  if (p == NULL) {
266,933,479✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
266,933,479✔
50
  p->cost.created = taosGetTimestampUs();
266,998,277✔
51

52
  p->execModel = model;
266,984,955✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
266,979,018✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
266,964,235✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
266,955,568✔
56
    doDestroyTask(p);
434✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
266,967,501✔
61
  taosInitRWLatch(&p->lock);
266,983,392✔
62

63
  p->id.vgId = vgId;
266,949,964✔
64
  p->id.queryId = queryId;
266,951,422✔
65
  p->id.taskId = taskId;
266,942,292✔
66
  p->id.str = taosMemoryMalloc(64);
266,975,162✔
67
  if (p->id.str == NULL) {
266,925,362✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
266,955,270✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
266,945,434✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
266,913,808✔
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
266,942,343✔
80
  return TSDB_CODE_SUCCESS;
266,927,474✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,687,776,419✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
855,668,508✔
93
  if (status == TASK_NOT_COMPLETED) {
855,668,508✔
94
    pTaskInfo->status = status;
267,126,099✔
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);
588,542,409✔
98
    pTaskInfo->status |= status;
588,561,737✔
99
  }
100
}
855,688,205✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
266,779,281✔
107
  ctx->taskId = pTaskInfo->id.taskId;
266,749,561✔
108
  ctx->idStr = pTaskInfo->id.str;
266,773,877✔
109
  ctx->pTaskInfo = pTaskInfo;
266,764,782✔
110
  ctx->subEndPoints = subEndPoints;
266,783,457✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
266,744,369✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
266,855,556✔
114
  if (subJobNum > 0) {
266,839,576✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
4,815,880✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
4,807,448✔
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,809,556✔
122
    if (code) {
4,807,975✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
4,807,975✔
128

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

132
  return TSDB_CODE_SUCCESS;
266,811,456✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
266,869,959✔
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);
266,869,959✔
140
  if (*pTaskInfo == NULL || code != 0) {
266,805,703✔
141
    nodesDestroyNode((SNode*)pPlan);
×
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
266,823,469✔
146

147
  if (pHandle) {
266,817,968✔
148
    if (pHandle->pStateBackend) {
266,818,057✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
266,806,604✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
264,213,159✔
156
    if (NULL == (*pTaskInfo)->sql) {
264,221,650✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
266,810,469✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
266,813,953✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
266,772,563✔
168
  if (code != TSDB_CODE_SUCCESS) {
266,798,951✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
266,798,951✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
266,771,555✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
266,688,966✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
266,650,677✔
180
    doDestroyTask(*pTaskInfo);
613,083✔
181
    (*pTaskInfo) = NULL;
448,396✔
182
  }
183
  return code;
266,335,389✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
187,459,830✔
187
  SSchemaInfo* pSchemaInfo = p;
187,459,830✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
187,459,830✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
187,435,025✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
187,447,155✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
187,437,119✔
193
}
187,429,911✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
187,510,447✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
187,510,447✔
198
  if (pHandle == NULL) {
187,515,555✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
187,515,555✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
187,499,726✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
187,524,919✔
206
  if (code != TSDB_CODE_SUCCESS) {
187,234,283✔
207
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
222✔
208
           GET_TASKID(pTaskInfo));
209

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

214
  SSchemaInfo schemaInfo = {0};
187,234,061✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
187,256,611✔
217
  schemaInfo.dbname = taosStrdup(dbName);
187,307,214✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
187,377,045✔
219
    pAPI->metaReaderFn.clearReader(&mr);
160,113✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
187,216,932✔
225
    schemaInfo.rversion = mr.me.colRef.version;
241,626✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
187,216,932✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
109,419,172✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
109,419,172✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
77,911,619✔
232
    tDecoderClear(&mr.coder);
37,077,027✔
233

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

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
37,052,479✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
37,052,479✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
40,862,831✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
187,334,482✔
249

250
  if (schemaInfo.sw == NULL) {
187,264,759✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
187,264,759✔
256
  if (schemaInfo.qsw == NULL) {
187,329,280✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
187,329,280✔
262
  if (p == NULL) {
187,434,699✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
187,434,699✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
187,330,750✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
187,330,750✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
187,383,919✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
187,285,250✔
275
  if (pqSw == NULL) {
187,218,611✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
187,218,611✔
280
  if (pqSw->pSchema == NULL) {
187,072,481✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
951,876,564✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
764,440,048✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
764,321,459✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
764,321,790✔
290
    pSchema->colId = pColNode->colId;
764,422,184✔
291
    pSchema->type = pColNode->node.resType.type;
764,642,158✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
764,520,197✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
764,542,991✔
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) {
344,273,517✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
156,829,305✔
299

300
    int32_t type = nodeType(pNode->pExpr);
156,836,638✔
301
    if (type == QUERY_NODE_COLUMN) {
156,865,951✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
117,531,820✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
117,521,048✔
305
      pSchema->colId = pColNode->colId;
117,461,273✔
306
      pSchema->type = pColNode->node.resType.type;
117,519,273✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
117,497,082✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
117,448,560✔
309
    }
310
  }
311

312
  return pqSw;
187,444,212✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
266,869,995✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
266,869,995✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
266,928,657✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
266,782,399✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
266,923,881✔
320
}
266,855,453✔
321

322
static void freeBlock(void* pParam) {
396,350,372✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
396,350,372✔
324
  blockDataDestroy(pBlock);
396,354,118✔
325
}
396,351,734✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
266,942,021✔
329
  if (pCtx->transporterId > 0) {
266,942,021✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
11,594✔
331
    if (ret != 0) {
11,594✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
11,594✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
266,961,066✔
337
}
266,950,128✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
266,987,541✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
266,987,541✔
341
  destroyOperator(pTaskInfo->pRoot);
266,987,539✔
342
  pTaskInfo->pRoot = NULL;
266,889,994✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
266,905,815✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
266,955,885✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
266,900,889✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
266,857,490✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
266,850,448✔
351
    pTaskInfo->pSubplan = NULL;
266,895,188✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
266,941,769✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
266,942,200✔
356
  if (!pTaskInfo->paramSet) {
266,934,544✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
260,289,262✔
358
    pTaskInfo->pOpParam = NULL;
260,197,420✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
266,872,155✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
266,876,319✔
362
  taosMemoryFreeClear(pTaskInfo);
266,872,348✔
363
}
266,807,997✔
364

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