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

taosdata / TDengine / #4939

26 Jan 2026 07:26AM UTC coverage: 66.931% (+0.1%) from 66.8%
#4939

push

travis-ci

web-flow
fix: interp func order (#34402)

* fix: interp func order

* fix: iterp sort

5 of 5 new or added lines in 1 file covered. (100.0%)

464 existing lines in 112 files now uncovered.

204592 of 305676 relevant lines covered (66.93%)

126181350.71 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,
300,403,403✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
300,403,403✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
300,403,403✔
45
  if (p == NULL) {
300,356,950✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
300,356,950✔
50
  p->cost.created = taosGetTimestampUs();
300,489,089✔
51

52
  p->execModel = model;
300,478,273✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
300,477,092✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
300,433,061✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
300,364,230✔
56
    doDestroyTask(p);
6,460✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
300,402,336✔
61
  taosInitRWLatch(&p->lock);
300,415,369✔
62

63
  p->id.vgId = vgId;
300,473,856✔
64
  p->id.queryId = queryId;
300,477,500✔
65
  p->id.taskId = taskId;
300,483,854✔
66
  p->id.str = taosMemoryMalloc(64);
300,479,817✔
67
  if (p->id.str == NULL) {
300,378,466✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
300,432,607✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
300,369,360✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
300,304,069✔
75
    doDestroyTask(p);
5✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
300,370,191✔
80
  return TSDB_CODE_SUCCESS;
300,366,831✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,759,140,133✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
938,815,430✔
93
  if (status == TASK_NOT_COMPLETED) {
938,815,430✔
94
    pTaskInfo->status = status;
300,552,177✔
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);
638,263,253✔
98
    pTaskInfo->status |= status;
638,281,646✔
99
  }
100
}
938,861,977✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
300,252,842✔
107
  ctx->taskId = pTaskInfo->id.taskId;
300,193,574✔
108
  ctx->idStr = pTaskInfo->id.str;
300,141,096✔
109
  ctx->pTaskInfo = pTaskInfo;
300,180,312✔
110
  ctx->subEndPoints = subEndPoints;
300,119,949✔
111
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
300,135,549✔
112
  
113
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
300,251,610✔
114
  if (subJobNum > 0) {
300,266,372✔
115
    pTaskInfo->subJobCtx.subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,500,313✔
116
    if (NULL == pTaskInfo->subJobCtx.subResNodes) {
37,483,060✔
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);
37,479,462✔
122
    if (code) {
37,456,121✔
123
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
124
      return code;
×
125
    }
126
    
127
    pTaskInfo->subJobCtx.hasSubJobs = true;
37,456,121✔
128

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

132
  return TSDB_CODE_SUCCESS;
300,151,579✔
133
}
134

135

136

137
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
300,289,710✔
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);
300,289,710✔
140
  if (*pTaskInfo == NULL || code != 0) {
300,209,280✔
UNCOV
141
    nodesDestroyNode((SNode*)pPlan);
×
142
    return code;
×
143
  }
144

145
  (*pTaskInfo)->pSubplan = pPlan;
300,265,000✔
146

147
  if (pHandle) {
300,248,684✔
148
    if (pHandle->pStateBackend) {
300,285,987✔
149
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
150
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
151
    }
152
  }
153

154
  if (NULL != sql) {
300,220,596✔
155
    (*pTaskInfo)->sql = taosStrdup(sql);
297,740,112✔
156
    if (NULL == (*pTaskInfo)->sql) {
297,676,727✔
157
      code = terrno;
×
158
      doDestroyTask(*pTaskInfo);
×
159
      (*pTaskInfo) = NULL;
×
160
      return code;
×
161
    }
162
  }
163

164
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
300,199,620✔
165
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
300,162,615✔
166

167
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
300,222,682✔
168
  if (code != TSDB_CODE_SUCCESS) {
300,127,920✔
169
    doDestroyTask(*pTaskInfo);
×
170
    (*pTaskInfo) = NULL;
×
171
    return code;
×
172
  }
173

174
  setTaskScalarExtraInfo(*pTaskInfo);
300,127,920✔
175
  
176
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
300,094,314✔
177
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
299,959,092✔
178

179
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
299,596,216✔
180
    doDestroyTask(*pTaskInfo);
3,908,127✔
181
    (*pTaskInfo) = NULL;
4,195,324✔
182
  }
183
  return code;
299,975,504✔
184
}
185

186
void cleanupQueriedTableScanInfo(void* p) {
211,037,353✔
187
  SSchemaInfo* pSchemaInfo = p;
211,037,353✔
188

189
  taosMemoryFreeClear(pSchemaInfo->dbname);
211,037,353✔
190
  taosMemoryFreeClear(pSchemaInfo->tablename);
210,988,128✔
191
  tDeleteSchemaWrapper(pSchemaInfo->sw);
210,942,007✔
192
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
210,952,836✔
193
}
210,957,427✔
194

195
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
211,180,850✔
196
                                   SExecTaskInfo* pTaskInfo) {
197
  SMetaReader mr = {0};
211,180,850✔
198
  if (pHandle == NULL) {
211,192,027✔
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201

202
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
211,192,027✔
203

204
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
211,175,497✔
205
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
211,200,115✔
206
  if (code != TSDB_CODE_SUCCESS) {
210,629,979✔
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};
210,629,979✔
215

216
  schemaInfo.tablename = taosStrdup(mr.me.name);
210,686,911✔
217
  schemaInfo.dbname = taosStrdup(dbName);
210,764,701✔
218
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
210,857,634✔
219
    pAPI->metaReaderFn.clearReader(&mr);
335,282✔
220
    cleanupQueriedTableScanInfo(&schemaInfo);
×
221
    return terrno;
×
222
  }
223

224
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
210,522,352✔
225
    schemaInfo.rversion = mr.me.colRef.version;
273,359✔
226
  }
227

228
  if (mr.me.type == TSDB_SUPER_TABLE) {
210,522,352✔
229
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
117,423,073✔
230
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
117,423,073✔
231
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
93,199,874✔
232
    tDecoderClear(&mr.coder);
52,755,508✔
233

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

242
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
52,877,851✔
243
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
52,877,851✔
244
  } else {
245
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
40,442,503✔
246
  }
247

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

250
  if (schemaInfo.sw == NULL) {
210,585,891✔
251
    cleanupQueriedTableScanInfo(&schemaInfo);
×
252
    return terrno;
×
253
  }
254

255
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
210,585,891✔
256
  if (schemaInfo.qsw == NULL) {
210,717,943✔
257
    cleanupQueriedTableScanInfo(&schemaInfo);
×
258
    return terrno;
×
259
  }
260

261
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
210,717,943✔
262
  if (p == NULL) {
210,871,629✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  return code;
210,871,629✔
268
}
269

270
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
210,646,318✔
271
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
210,646,318✔
272
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
210,933,720✔
273

274
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
210,805,711✔
275
  if (pqSw == NULL) {
210,663,552✔
276
    return NULL;
×
277
  }
278

279
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
210,663,552✔
280
  if (pqSw->pSchema == NULL) {
210,262,503✔
281
    taosMemoryFree(pqSw);
×
282
    return NULL;
×
283
  }
284

285
  for (int32_t i = 0; i < numOfCols; ++i) {
971,163,606✔
286
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
760,216,640✔
287
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
760,547,407✔
288

289
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
760,700,501✔
290
    pSchema->colId = pColNode->colId;
760,257,381✔
291
    pSchema->type = pColNode->node.resType.type;
760,595,624✔
292
    pSchema->bytes = pColNode->node.resType.bytes;
760,591,228✔
293
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
760,659,446✔
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) {
350,845,593✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
139,926,535✔
299

300
    int32_t type = nodeType(pNode->pExpr);
139,937,926✔
301
    if (type == QUERY_NODE_COLUMN) {
140,047,258✔
302
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
103,561,227✔
303

304
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
103,574,907✔
305
      pSchema->colId = pColNode->colId;
103,577,539✔
306
      pSchema->type = pColNode->node.resType.type;
103,389,864✔
307
      pSchema->bytes = pColNode->node.resType.bytes;
103,522,258✔
308
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
103,448,225✔
309
    }
310
  }
311

312
  return pqSw;
210,919,058✔
313
}
314

315
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
300,256,331✔
316
  tDeleteSchemaWrapper(pStreamInfo->schema);
300,256,331✔
317
  tOffsetDestroy(&pStreamInfo->currentOffset);
300,281,395✔
318
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
300,072,579✔
319
  taosMemoryFree(pStreamInfo->stbFullName);
300,267,803✔
320
}
300,216,098✔
321

322
static void freeBlock(void* pParam) {
413,168,423✔
323
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
413,168,423✔
324
  blockDataDestroy(pBlock);
413,192,298✔
325
}
413,207,396✔
326

327

328
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
300,347,621✔
329
  if (pCtx->transporterId > 0) {
300,347,621✔
330
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
64,312✔
331
    if (ret != 0) {
64,312✔
332
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
333
    }
334
    pCtx->transporterId = -1;
64,312✔
335
  }
336
  taosArrayDestroy(pCtx->subResNodes);
300,411,888✔
337
}
300,371,947✔
338

339
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
300,415,144✔
340
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
300,415,144✔
341
  destroyOperator(pTaskInfo->pRoot);
300,414,663✔
342
  pTaskInfo->pRoot = NULL;
300,301,702✔
343

344
  destroySubJobCtx(&pTaskInfo->subJobCtx);
300,294,923✔
345

346
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
300,299,881✔
347
  cleanupStreamInfo(&pTaskInfo->streamInfo);
300,272,263✔
348

349
  if (!pTaskInfo->localFetch.localExec) {
300,207,444✔
350
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
300,233,965✔
351
    pTaskInfo->pSubplan = NULL;
300,254,661✔
352
  }
353

354
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
300,265,002✔
355
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
300,357,272✔
356
  if (!pTaskInfo->paramSet) {
300,357,068✔
357
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
293,134,210✔
358
    pTaskInfo->pOpParam = NULL;
292,937,116✔
359
  }
360
  taosMemoryFreeClear(pTaskInfo->sql);
300,194,345✔
361
  taosMemoryFreeClear(pTaskInfo->id.str);
300,286,573✔
362
  taosMemoryFreeClear(pTaskInfo);
300,269,050✔
363
}
300,115,882✔
364

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