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

taosdata / TDengine / #4931

16 Jan 2026 02:32AM UTC coverage: 66.749% (+0.03%) from 66.716%
#4931

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

1890 existing lines in 121 files now uncovered.

203303 of 304580 relevant lines covered (66.75%)

129941648.02 hits per line

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

80.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,
384,696,858✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
384,696,858✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
384,696,858✔
45
  if (p == NULL) {
384,559,987✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
384,559,987✔
50
  p->cost.created = taosGetTimestampUs();
384,758,423✔
51

52
  p->execModel = model;
384,752,413✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
384,745,088✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
384,665,796✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
384,657,706✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
384,701,418✔
61
  taosInitRWLatch(&p->lock);
384,740,525✔
62

63
  p->id.vgId = vgId;
384,687,954✔
64
  p->id.queryId = queryId;
384,666,314✔
65
  p->id.taskId = taskId;
384,711,886✔
66
  p->id.str = taosMemoryMalloc(64);
384,713,311✔
67
  if (p->id.str == NULL) {
384,601,583✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
384,657,673✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
384,617,912✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
384,532,967✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
384,607,579✔
80
  return TSDB_CODE_SUCCESS;
384,619,977✔
81
}
82

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

85
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
2,147,483,647✔
86

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

92
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,350,348,027✔
93
  if (status == TASK_NOT_COMPLETED) {
1,350,348,027✔
94
    pTaskInfo->status = status;
384,786,285✔
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);
965,561,742✔
98
    pTaskInfo->status |= status;
965,594,988✔
99
  }
100
}
1,350,533,166✔
101

102

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

106
  ctx->queryId = pTaskInfo->id.queryId;
384,604,436✔
107
  ctx->idStr = pTaskInfo->id.str;
384,516,136✔
108
  ctx->pTaskInfo = pTaskInfo;
384,510,975✔
109
  ctx->subEndPoints = subEndPoints;
384,356,994✔
110
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
384,417,941✔
111
  
112
  int32_t subJobNum = taosArrayGetSize(subEndPoints);
384,543,375✔
113
  if (subJobNum > 0) {
384,509,242✔
114
    pTaskInfo->subJobCtx.subResValues = taosArrayInit_s(POINTER_BYTES, subJobNum);
80,956,039✔
115
    if (NULL == pTaskInfo->subJobCtx.subResValues) {
80,940,195✔
116
      qError("%s taosArrayInit_s %d subJobValues failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
117
      return terrno;
×
118
    }
119
    
120
    int32_t code = tsem_init(&ctx->ready, 0, 0);
80,918,382✔
121
    if (code) {
80,900,150✔
122
      qError("%s tsem_init failed, error:%s", GET_TASKID(pTaskInfo), tstrerror(code));
×
123
      return code;
×
124
    }
125
    
126
    pTaskInfo->subJobCtx.hasSubJobs = true;
80,900,150✔
127

128
    qDebug("%s subJobCtx with %d endPoints inited", pTaskInfo->id.str, subJobNum);
80,946,437✔
129
  }
130

131
  return TSDB_CODE_SUCCESS;
384,443,577✔
132
}
133

134

135

136
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
384,578,762✔
137
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
138
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
384,578,762✔
139
  if (*pTaskInfo == NULL || code != 0) {
384,489,257✔
140
    nodesDestroyNode((SNode*)pPlan);
×
141
    return code;
×
142
  }
143

144
  (*pTaskInfo)->pSubplan = pPlan;
384,528,947✔
145

146
  if (pHandle) {
384,520,342✔
147
    if (pHandle->pStateBackend) {
384,376,802✔
148
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
149
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
150
    }
151
  }
152

153
  if (NULL != sql) {
384,566,835✔
154
    (*pTaskInfo)->sql = taosStrdup(sql);
381,918,334✔
155
    if (NULL == (*pTaskInfo)->sql) {
381,935,231✔
156
      code = terrno;
×
157
      doDestroyTask(*pTaskInfo);
×
158
      (*pTaskInfo) = NULL;
×
159
      return code;
×
160
    }
161
  }
162

163
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
384,660,063✔
164
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
384,583,168✔
165

166
  code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
384,606,446✔
167
  if (code != TSDB_CODE_SUCCESS) {
384,404,589✔
168
    doDestroyTask(*pTaskInfo);
×
169
    (*pTaskInfo) = NULL;
×
170
    return code;
×
171
  }
172

173
  setTaskScalarExtraInfo(*pTaskInfo);
384,404,589✔
174
  
175
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
384,436,280✔
176
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
384,280,371✔
177

178
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
384,333,414✔
179
    doDestroyTask(*pTaskInfo);
6,631,070✔
180
    (*pTaskInfo) = NULL;
6,551,485✔
181
  }
182
  return code;
384,338,365✔
183
}
184

185
void cleanupQueriedTableScanInfo(void* p) {
288,568,560✔
186
  SSchemaInfo* pSchemaInfo = p;
288,568,560✔
187

188
  taosMemoryFreeClear(pSchemaInfo->dbname);
288,568,560✔
189
  taosMemoryFreeClear(pSchemaInfo->tablename);
288,474,765✔
190
  tDeleteSchemaWrapper(pSchemaInfo->sw);
288,306,677✔
191
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
288,346,281✔
192
}
288,362,128✔
193

194
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
288,738,496✔
195
                                   SExecTaskInfo* pTaskInfo) {
196
  SMetaReader mr = {0};
288,738,496✔
197
  if (pHandle == NULL) {
288,750,999✔
198
    return TSDB_CODE_INVALID_PARA;
×
199
  }
200

201
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
288,750,999✔
202

203
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
288,732,637✔
204
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
288,738,594✔
205
  if (code != TSDB_CODE_SUCCESS) {
288,420,759✔
206
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
207
           GET_TASKID(pTaskInfo));
208

209
    pAPI->metaReaderFn.clearReader(&mr);
×
210
    return code;
×
211
  }
212

213
  SSchemaInfo schemaInfo = {0};
288,420,759✔
214

215
  schemaInfo.tablename = taosStrdup(mr.me.name);
288,418,597✔
216
  schemaInfo.dbname = taosStrdup(dbName);
288,475,167✔
217
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
288,527,722✔
218
    pAPI->metaReaderFn.clearReader(&mr);
424,192✔
219
    cleanupQueriedTableScanInfo(&schemaInfo);
×
220
    return terrno;
×
221
  }
222

223
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
288,103,530✔
224
    schemaInfo.rversion = mr.me.colRef.version;
282,349✔
225
  }
226

227
  if (mr.me.type == TSDB_SUPER_TABLE) {
288,103,530✔
228
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
117,040,240✔
229
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
117,040,240✔
230
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
171,088,427✔
231
    tDecoderClear(&mr.coder);
102,960,911✔
232

233
    tb_uid_t suid = mr.me.ctbEntry.suid;
103,319,406✔
234
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
103,319,406✔
235
    if (code != TSDB_CODE_SUCCESS) {
103,317,599✔
236
      pAPI->metaReaderFn.clearReader(&mr);
×
237
      cleanupQueriedTableScanInfo(&schemaInfo);
×
238
      return code;
×
239
    }
240

241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
103,324,119✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
103,324,119✔
243
  } else {
244
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
68,125,225✔
245
  }
246

247
  pAPI->metaReaderFn.clearReader(&mr);
288,489,584✔
248

249
  if (schemaInfo.sw == NULL) {
288,311,048✔
250
    cleanupQueriedTableScanInfo(&schemaInfo);
×
251
    return terrno;
×
252
  }
253

254
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
288,311,048✔
255
  if (schemaInfo.qsw == NULL) {
288,435,178✔
256
    cleanupQueriedTableScanInfo(&schemaInfo);
×
257
    return terrno;
×
258
  }
259

260
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
288,435,178✔
261
  if (p == NULL) {
288,562,268✔
262
    cleanupQueriedTableScanInfo(&schemaInfo);
×
263
    return terrno;
×
264
  }
265

266
  return code;
288,562,268✔
267
}
268

269
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
288,405,883✔
270
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
288,405,883✔
271
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
288,635,315✔
272

273
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
288,506,220✔
274
  if (pqSw == NULL) {
288,195,886✔
275
    return NULL;
×
276
  }
277

278
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
288,195,886✔
279
  if (pqSw->pSchema == NULL) {
287,889,612✔
280
    taosMemoryFree(pqSw);
×
281
    return NULL;
×
282
  }
283

284
  for (int32_t i = 0; i < numOfCols; ++i) {
1,226,420,327✔
285
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
937,780,833✔
286
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
938,147,187✔
287

288
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
938,303,857✔
289
    pSchema->colId = pColNode->colId;
938,063,559✔
290
    pSchema->type = pColNode->node.resType.type;
938,301,029✔
291
    pSchema->bytes = pColNode->node.resType.bytes;
938,335,138✔
292
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
938,398,143✔
293
  }
294

295
  // this the tags and pseudo function columns, we only keep the tag columns
296
  for (int32_t i = 0; i < numOfTags; ++i) {
479,690,102✔
297
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
191,043,969✔
298

299
    int32_t type = nodeType(pNode->pExpr);
191,080,138✔
300
    if (type == QUERY_NODE_COLUMN) {
191,107,977✔
301
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
148,599,893✔
302

303
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
148,608,954✔
304
      pSchema->colId = pColNode->colId;
148,619,657✔
305
      pSchema->type = pColNode->node.resType.type;
148,544,298✔
306
      pSchema->bytes = pColNode->node.resType.bytes;
148,562,130✔
307
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
148,541,067✔
308
    }
309
  }
310

311
  return pqSw;
288,646,133✔
312
}
313

314
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
384,458,721✔
315
  tDeleteSchemaWrapper(pStreamInfo->schema);
384,458,721✔
316
  tOffsetDestroy(&pStreamInfo->currentOffset);
384,561,248✔
317
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
384,402,013✔
318
  taosMemoryFree(pStreamInfo->stbFullName);
384,516,195✔
319
}
384,528,546✔
320

321
static void freeBlock(void* pParam) {
501,469,341✔
322
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
501,469,341✔
323
  blockDataDestroy(pBlock);
501,505,200✔
324
}
501,476,538✔
325

326

327
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
384,612,326✔
328
  if (pCtx->transporterId > 0) {
384,612,326✔
329
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
10,011✔
330
    if (ret != 0) {
10,011✔
331
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
332
    }
333
    pCtx->transporterId = -1;
10,011✔
334
  }
335
  taosArrayDestroy(pCtx->subResValues);
384,681,872✔
336
}
384,605,303✔
337

338
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
384,655,408✔
339
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
384,655,408✔
340
  destroyOperator(pTaskInfo->pRoot);
384,655,408✔
341
  pTaskInfo->pRoot = NULL;
384,449,410✔
342

343
  destroySubJobCtx(&pTaskInfo->subJobCtx);
384,502,757✔
344

345
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
384,536,442✔
346
  cleanupStreamInfo(&pTaskInfo->streamInfo);
384,319,862✔
347

348
  if (!pTaskInfo->localFetch.localExec) {
384,530,498✔
349
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
384,454,014✔
350
    pTaskInfo->pSubplan = NULL;
384,444,199✔
351
  }
352

353
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
384,563,164✔
354
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
384,425,157✔
355
  if (!pTaskInfo->paramSet) {
384,432,546✔
356
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
376,766,156✔
357
    pTaskInfo->pOpParam = NULL;
376,693,559✔
358
  }
359
  taosMemoryFreeClear(pTaskInfo->sql);
384,422,947✔
360
  taosMemoryFreeClear(pTaskInfo->id.str);
384,487,847✔
361
  taosMemoryFreeClear(pTaskInfo);
384,418,746✔
362
}
384,298,277✔
363

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