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

taosdata / TDengine / #5000

22 Mar 2026 10:21AM UTC coverage: 72.307% (-0.003%) from 72.31%
#5000

push

travis-ci

web-flow
feat(subq/some): some/any/exists for stream subq (#34860)

50 of 68 new or added lines in 1 file covered. (73.53%)

614 existing lines in 138 files now uncovered.

253462 of 350536 relevant lines covered (72.31%)

134798164.58 hits per line

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

82.01
/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,
368,710,332✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
368,710,332✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
368,710,332✔
46
  if (p == NULL) {
368,742,225✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
368,742,225✔
51
  p->cost.created = taosGetTimestampUs();
368,726,430✔
52

53
  p->execModel = model;
368,721,547✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
368,722,165✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
368,634,291✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
368,647,076✔
57
    doDestroyTask(p);
421✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
368,683,889✔
62
  taosInitRWLatch(&p->lock);
368,760,471✔
63

64
  p->id.vgId = vgId;
368,661,049✔
65
  p->id.queryId = queryId;
368,664,294✔
66
  p->id.taskId = taskId;
368,673,729✔
67
  p->id.str = taosMemoryMalloc(64);
368,684,022✔
68
  if (p->id.str == NULL) {
368,506,311✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
368,639,635✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
368,573,155✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
368,373,559✔
76
    doDestroyTask(p);
5,092✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
368,437,675✔
81
  return TSDB_CODE_SUCCESS;
368,468,776✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
8,768✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,955,848,119✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,164,714,986✔
94
  if (status == TASK_NOT_COMPLETED) {
1,164,714,986✔
95
    pTaskInfo->status = status;
368,919,145✔
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);
795,795,841✔
99
    pTaskInfo->status |= status;
795,902,187✔
100
  }
101
}
1,164,816,791✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
41,472,944✔
105
  int32_t code = 0, lino = 0;
41,472,944✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
41,472,944✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
41,466,368✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
41,459,244✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
41,461,984✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
41,464,724✔
114
  ctx->taskId = pTaskInfo->id.taskId;
41,470,184✔
115
  ctx->idStr = pTaskInfo->id.str;
41,464,176✔
116
  ctx->pTaskInfo = pTaskInfo;
41,465,262✔
117
  ctx->subEndPoints = *subEndPoints;
41,463,618✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
41,452,122✔
119
  ctx->isStream = pTaskInfo && IS_STREAM_MODE(pTaskInfo);
41,477,856✔
120

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

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

139
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
41,464,726✔
140

141
_exit:
40,992,208✔
142

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

150
  return code;
41,460,340✔
151
}
152

153

154

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

163
  (*pTaskInfo)->pSubplan = pPlan;
368,405,401✔
164

165
  if (NULL != sql) {
368,328,352✔
166
    (*pTaskInfo)->sql = taosStrdup(sql);
342,864,741✔
167
    if (NULL == (*pTaskInfo)->sql) {
342,785,617✔
168
      code = terrno;
×
169
      doDestroyTask(*pTaskInfo);
×
170
      (*pTaskInfo) = NULL;
×
171
      return code;
×
172
    }
173
  }
174

175
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
368,341,633✔
176
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
368,402,072✔
177
  (*pTaskInfo)->enableExplain = enableExplain;
368,378,133✔
178

179
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
368,298,753✔
180
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
41,475,116✔
181
    if (code != TSDB_CODE_SUCCESS) {
41,461,994✔
182
      doDestroyTask(*pTaskInfo);
×
183
      (*pTaskInfo) = NULL;
×
184
      return code;
×
185
    }
186
  }
187
  
188
  setTaskScalarExtraInfo(*pTaskInfo);
368,397,383✔
189
  
190
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
368,214,814✔
191
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
368,227,597✔
192

193
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
367,609,738✔
194
    doDestroyTask(*pTaskInfo);
1,225,635✔
195
    (*pTaskInfo) = NULL;
470,352✔
196
  }
197
  return code;
367,039,290✔
198
}
199

200
void cleanupQueriedTableScanInfo(void* p) {
272,473,016✔
201
  SSchemaInfo* pSchemaInfo = p;
272,473,016✔
202

203
  taosMemoryFreeClear(pSchemaInfo->dbname);
272,473,016✔
204
  taosMemoryFreeClear(pSchemaInfo->tablename);
272,400,725✔
205
  tDeleteSchemaWrapper(pSchemaInfo->sw);
272,464,704✔
206
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
272,426,793✔
207
}
272,402,116✔
208

209
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
272,582,847✔
210
                                   SExecTaskInfo* pTaskInfo) {
211
  SMetaReader mr = {0};
272,582,847✔
212
  if (pHandle == NULL) {
272,594,002✔
213
    return TSDB_CODE_INVALID_PARA;
×
214
  }
215

216
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
272,594,002✔
217

218
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
272,597,425✔
219
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
272,630,648✔
220
  if (code != TSDB_CODE_SUCCESS) {
271,658,612✔
UNCOV
221
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
222
           GET_TASKID(pTaskInfo));
223

UNCOV
224
    pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
225
    return code;
×
226
  }
227

228
  SSchemaInfo schemaInfo = {0};
271,658,612✔
229

230
  schemaInfo.tablename = taosStrdup(mr.me.name);
271,648,498✔
231
  schemaInfo.dbname = taosStrdup(dbName);
271,808,877✔
232
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
272,107,466✔
233
    pAPI->metaReaderFn.clearReader(&mr);
1,024,623✔
234
    cleanupQueriedTableScanInfo(&schemaInfo);
×
235
    return terrno;
×
236
  }
237

238
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
271,082,843✔
239
    schemaInfo.rversion = mr.me.colRef.version;
296,444✔
240
  }
241

242
  if (mr.me.type == TSDB_SUPER_TABLE) {
271,082,843✔
243
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
119,966,984✔
244
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
119,966,984✔
245
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
151,432,506✔
246
    tDecoderClear(&mr.coder);
106,758,406✔
247

248
    tb_uid_t suid = mr.me.ctbEntry.suid;
107,236,879✔
249
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
107,236,879✔
250
    if (code != TSDB_CODE_SUCCESS) {
107,234,700✔
251
      pAPI->metaReaderFn.clearReader(&mr);
×
252
      cleanupQueriedTableScanInfo(&schemaInfo);
×
253
      return code;
×
254
    }
255

256
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
107,236,849✔
257
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
107,236,849✔
258
  } else {
259
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
44,751,428✔
260
  }
261

262
  pAPI->metaReaderFn.clearReader(&mr);
271,955,261✔
263

264
  if (schemaInfo.sw == NULL) {
271,974,471✔
265
    cleanupQueriedTableScanInfo(&schemaInfo);
×
266
    return terrno;
×
267
  }
268

269
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
271,974,471✔
270
  if (schemaInfo.qsw == NULL) {
272,135,263✔
271
    cleanupQueriedTableScanInfo(&schemaInfo);
×
272
    return terrno;
×
273
  }
274

275
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
272,135,263✔
276
  if (p == NULL) {
272,093,556✔
277
    cleanupQueriedTableScanInfo(&schemaInfo);
×
278
    return terrno;
×
279
  }
280

281
  return code;
272,093,556✔
282
}
283

284
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
271,862,768✔
285
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
271,862,768✔
286
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
272,181,167✔
287

288
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
271,778,104✔
289
  if (pqSw == NULL) {
271,296,970✔
290
    return NULL;
×
291
  }
292

293
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
271,296,970✔
294
  if (pqSw->pSchema == NULL) {
270,761,979✔
295
    taosMemoryFree(pqSw);
×
296
    return NULL;
×
297
  }
298

299
  for (int32_t i = 0; i < numOfCols; ++i) {
1,225,199,991✔
300
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
952,681,912✔
301
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
952,722,685✔
302

303
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
952,998,791✔
304
    pSchema->colId = pColNode->colId;
952,936,453✔
305
    pSchema->type = pColNode->node.resType.type;
953,183,469✔
306
    pSchema->bytes = pColNode->node.resType.bytes;
953,265,048✔
307
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
953,049,006✔
308
  }
309

310
  // this the tags and pseudo function columns, we only keep the tag columns
311
  for (int32_t i = 0; i < numOfTags; ++i) {
432,972,991✔
312
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
160,448,035✔
313

314
    int32_t type = nodeType(pNode->pExpr);
160,587,276✔
315
    if (type == QUERY_NODE_COLUMN) {
160,695,977✔
316
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
118,238,212✔
317

318
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
118,239,366✔
319
      pSchema->colId = pColNode->colId;
118,076,578✔
320
      pSchema->type = pColNode->node.resType.type;
118,056,028✔
321
      pSchema->bytes = pColNode->node.resType.bytes;
117,876,081✔
322
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
118,021,428✔
323
    }
324
  }
325

326
  return pqSw;
272,524,956✔
327
}
328

329
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
368,389,880✔
330
  tDeleteSchemaWrapper(pTmqInfo->schema);
368,389,880✔
331
  tOffsetDestroy(&pTmqInfo->currentOffset);
368,494,551✔
332
}
368,309,418✔
333

334
static void freeBlock(void* pParam) {
707,143,096✔
335
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
707,143,096✔
336
  blockDataDestroy(pBlock);
707,178,341✔
337
}
707,184,716✔
338

339

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

349
  if (pCtx->subEndPoints != NULL) {
41,474,030✔
350
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
41,470,764✔
351
    if (size > 0) {
41,471,868✔
352
      int32_t code = tsem_destroy(&pCtx->ready);
41,471,868✔
353
      if (code != TSDB_CODE_SUCCESS) {
41,472,976✔
354
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
355
      }
356
      taosArrayDestroy(pCtx->subResNodes);
41,472,976✔
357
    }
358
    if (pCtx->isStream) {
41,468,331✔
359
      taosArrayDestroyP(pCtx->subEndPoints, (FDelete)nodesDestroyNode);
255,876✔
360
    } else {
361
      taosArrayDestroyP(pCtx->subEndPoints, NULL);
41,212,993✔
362
    }
363
    pCtx->subEndPoints = NULL;
41,465,055✔
364
  }
365
  
366
  taosMemoryFreeClear(pCtx);  
41,471,061✔
367
}
41,471,587✔
368

369
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
368,767,532✔
370
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
368,767,532✔
371
  destroyOperator(pTaskInfo->pRoot);
368,767,532✔
372
  pTaskInfo->pRoot = NULL;
368,486,436✔
373

374
  if (pTaskInfo->pSubJobCtx) {
368,592,963✔
375
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
41,476,521✔
376
    if (code != TSDB_CODE_SUCCESS) {
41,475,734✔
377
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
378
    }
379
  }
380

381
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
368,591,149✔
382
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
368,468,429✔
383

384
  if (!pTaskInfo->localFetch.localExec) {
368,310,963✔
385
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
368,402,697✔
386
    pTaskInfo->pSubplan = NULL;
368,557,665✔
387
  }
388

389
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
368,564,333✔
390
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
368,588,113✔
391
  if (!pTaskInfo->paramSet) {
368,521,402✔
392
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
358,656,054✔
393
    pTaskInfo->pOpParam = NULL;
358,349,036✔
394
  }
395
  taosMemoryFreeClear(pTaskInfo->sql);
368,296,546✔
396
  taosMemoryFreeClear(pTaskInfo->id.str);
368,356,707✔
397
  taosMemoryFreeClear(pTaskInfo);
368,554,107✔
398
}
368,397,570✔
399

400
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
381,842,881✔
401
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
381,842,881✔
402
  if (ret < 0) {
381,842,881✔
403
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
404
  }
405
}
381,842,881✔
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