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

taosdata / TDengine / #4958

09 Feb 2026 01:16AM UTC coverage: 66.842% (+0.005%) from 66.837%
#4958

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

205670 of 307696 relevant lines covered (66.84%)

127690353.27 hits per line

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

80.82
/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,
279,019,838✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
279,019,838✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
279,019,838✔
45
  if (p == NULL) {
278,970,734✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
278,970,734✔
50
  p->cost.created = taosGetTimestampUs();
279,032,288✔
51

52
  p->execModel = model;
279,023,869✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
279,019,020✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
278,990,923✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
278,999,653✔
56
    doDestroyTask(p);
129✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
279,000,547✔
61
  taosInitRWLatch(&p->lock);
279,012,228✔
62

63
  p->id.vgId = vgId;
278,989,376✔
64
  p->id.queryId = queryId;
278,986,939✔
65
  p->id.taskId = taskId;
278,986,470✔
66
  p->id.str = taosMemoryMalloc(64);
279,007,090✔
67
  if (p->id.str == NULL) {
278,958,739✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
278,984,272✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
278,971,868✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
278,928,509✔
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
278,967,447✔
80
  return TSDB_CODE_SUCCESS;
278,951,637✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,736,110,273✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
889,206,890✔
93
  if (status == TASK_NOT_COMPLETED) {
889,206,890✔
94
    pTaskInfo->status = status;
279,157,847✔
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);
610,049,043✔
98
    pTaskInfo->status |= status;
610,077,397✔
99
  }
100
}
889,235,674✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
278,792,196✔
107
  ctx->taskId = pTaskInfo->id.taskId;
278,780,396✔
108
  ctx->idStr = pTaskInfo->id.str;
278,797,974✔
109
  ctx->pTaskInfo = pTaskInfo;
278,784,890✔
110
  ctx->subEndPoints = subEndPoints;
278,811,158✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
278,751,024✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
278,875,216✔
114
  if (subJobNum > 0) {
278,857,779✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
4,809,044✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
4,807,998✔
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,801,686✔
122
    if (code) {
4,804,316✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
4,804,316✔
128

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

132
  return TSDB_CODE_SUCCESS;
278,831,968✔
133
}
134

135

136

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

145
  (*pTaskInfo)->pSubplan = pPlan;
278,842,744✔
146

147
  if (pHandle) {
278,840,788✔
148
    if (pHandle->pStateBackend) {
278,818,208✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
278,850,649✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
276,232,854✔
156
    if (NULL == (*pTaskInfo)->sql) {
276,239,412✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
278,857,628✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
278,819,635✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
278,799,455✔
168
  if (code != TSDB_CODE_SUCCESS) {
278,815,934✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
278,815,934✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
278,783,966✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
278,719,626✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
278,633,755✔
180
    doDestroyTask(*pTaskInfo);
612,730✔
181
    (*pTaskInfo) = NULL;
446,846✔
182
  }
183
  return code;
278,346,862✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
197,386,578✔
187
  SSchemaInfo* pSchemaInfo = p;
197,386,578✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
197,386,578✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
197,358,226✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
197,362,222✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
197,376,019✔
193
}
197,357,310✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
197,427,378✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
197,427,378✔
198
  if (pHandle == NULL) {
197,434,727✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
197,434,727✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
197,429,392✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
197,447,909✔
206
  if (code != TSDB_CODE_SUCCESS) {
197,151,081✔
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};
197,151,081✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
197,161,342✔
217
  schemaInfo.dbname = taosStrdup(dbName);
197,237,886✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
197,289,986✔
219
    pAPI->metaReaderFn.clearReader(&mr);
163,326✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
197,126,660✔
225
    schemaInfo.rversion = mr.me.colRef.version;
241,394✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
197,126,660✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
119,149,019✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
119,149,019✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
78,053,442✔
232
    tDecoderClear(&mr.coder);
36,966,283✔
233

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

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
36,988,184✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
36,988,184✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
41,129,857✔
246
  }
247

248
  pAPI->metaReaderFn.clearReader(&mr);
197,267,060✔
249

250
  if (schemaInfo.sw == NULL) {
197,199,683✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
197,199,683✔
256
  if (schemaInfo.qsw == NULL) {
197,255,461✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
197,255,461✔
262
  if (p == NULL) {
197,358,371✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
197,358,371✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
197,240,137✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
197,240,137✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
197,310,272✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
197,200,927✔
275
  if (pqSw == NULL) {
197,117,653✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
197,117,653✔
280
  if (pqSw->pSchema == NULL) {
196,988,585✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
993,637,385✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
796,296,279✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
796,189,910✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
796,189,587✔
290
    pSchema->colId = pColNode->colId;
796,358,480✔
291
    pSchema->type = pColNode->node.resType.type;
796,530,847✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
796,391,218✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
796,464,281✔
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) {
378,149,961✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
180,784,896✔
299

300
    int32_t type = nodeType(pNode->pExpr);
180,784,738✔
301
    if (type == QUERY_NODE_COLUMN) {
180,835,546✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
139,687,750✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
139,686,937✔
305
      pSchema->colId = pColNode->colId;
139,626,266✔
306
      pSchema->type = pColNode->node.resType.type;
139,673,520✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
139,632,265✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
139,595,327✔
309
    }
310
  }
311

312
  return pqSw;
197,365,065✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
278,885,406✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
278,885,406✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
278,950,055✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
278,807,855✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
278,929,940✔
320
}
278,893,642✔
321

322
static void freeBlock(void* pParam) {
423,676,151✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
423,676,151✔
324
  blockDataDestroy(pBlock);
423,681,042✔
325
}
423,673,778✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
278,957,531✔
329
  if (pCtx->transporterId > 0) {
278,957,531✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
9,468✔
331
    if (ret != 0) {
9,468✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
9,468✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
279,000,497✔
337
}
278,979,265✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
279,012,871✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
279,012,871✔
341
  destroyOperator(pTaskInfo->pRoot);
279,012,871✔
342
  pTaskInfo->pRoot = NULL;
278,931,547✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
278,937,524✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
278,983,526✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
278,920,357✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
278,896,341✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
278,889,746✔
351
    pTaskInfo->pSubplan = NULL;
278,902,613✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
278,964,118✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
278,958,977✔
356
  if (!pTaskInfo->paramSet) {
278,946,846✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
271,962,381✔
358
    pTaskInfo->pOpParam = NULL;
271,876,098✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
278,898,257✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
278,897,622✔
362
  taosMemoryFreeClear(pTaskInfo);
278,861,534✔
363
}
278,794,315✔
364

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