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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

83.52
/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,
156,237,159✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
156,237,159✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
156,237,159✔
45
  if (p == NULL) {
156,230,820✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
156,230,820✔
50
  p->cost.created = taosGetTimestampUs();
156,239,913✔
51

52
  p->execModel = model;
156,239,023✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
156,238,736✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
156,233,659✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
156,230,004✔
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
156,235,659✔
61
  taosInitRWLatch(&p->lock);
156,233,104✔
62

63
  p->id.vgId = vgId;
156,229,052✔
64
  p->id.queryId = queryId;
156,229,944✔
65
  p->id.taskId = taskId;
156,232,368✔
66
  p->id.str = taosMemoryMalloc(64);
156,230,418✔
67
  if (p->id.str == NULL) {
156,221,709✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
156,230,642✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
156,225,565✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
156,217,428✔
75
    doDestroyTask(p);
2,156✔
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
156,226,666✔
80
  return TSDB_CODE_SUCCESS;
156,226,231✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,021,788,687✔
84

85
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
33,295✔
86
  pTaskInfo->code = rspCode;
33,295✔
87
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
33,295✔
88
}
33,295✔
89

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
490,911,254✔
91
  if (status == TASK_NOT_COMPLETED) {
490,911,254✔
92
    pTaskInfo->status = status;
156,333,777✔
93
  } else {
94
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
95
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
334,577,477✔
96
    pTaskInfo->status |= status;
334,581,855✔
97
  }
98
}
490,922,618✔
99

100
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
156,211,860✔
101
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model) {
102
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
156,211,860✔
103
  if (*pTaskInfo == NULL || code != 0) {
156,196,612✔
104
    nodesDestroyNode((SNode*)pPlan);
24✔
105
    return code;
×
106
  }
107

108
  (*pTaskInfo)->pSubplan = pPlan;
156,199,111✔
109

110
  if (pHandle) {
156,204,210✔
111
    if (pHandle->pStateBackend) {
156,194,886✔
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
156,206,153✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
153,617,876✔
119
    if (NULL == (*pTaskInfo)->sql) {
153,626,075✔
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
156,196,789✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
156,198,149✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
156,201,124✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
156,194,305✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
156,092,060✔
133
    doDestroyTask(*pTaskInfo);
22,533✔
134
    (*pTaskInfo) = NULL;
26,276✔
135
  }
136
  return code;
156,121,962✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
114,972,552✔
140
  SSchemaInfo* pSchemaInfo = p;
114,972,552✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
114,972,552✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
114,969,694✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
114,958,355✔
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
114,959,289✔
146
}
114,955,995✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
114,981,395✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
114,981,395✔
151
  if (pHandle == NULL) {
114,982,811✔
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
114,982,811✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
114,979,593✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
114,980,246✔
159
  if (code != TSDB_CODE_SUCCESS) {
114,906,457✔
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
408✔
161
           GET_TASKID(pTaskInfo));
162

163
    pAPI->metaReaderFn.clearReader(&mr);
408✔
164
    return code;
408✔
165
  }
166

167
  SSchemaInfo schemaInfo = {0};
114,906,049✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
114,906,840✔
170
  schemaInfo.dbname = taosStrdup(dbName);
114,883,183✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
114,906,510✔
172
    pAPI->metaReaderFn.clearReader(&mr);
52,193✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
114,854,317✔
178
    schemaInfo.rversion = mr.me.colRef.version;
12,276✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
114,854,317✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
63,666,932✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
63,666,932✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
51,210,645✔
185
    tDecoderClear(&mr.coder);
32,295,986✔
186

187
    tb_uid_t suid = mr.me.ctbEntry.suid;
32,317,792✔
188
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
32,317,792✔
189
    if (code != TSDB_CODE_SUCCESS) {
32,317,267✔
190
      pAPI->metaReaderFn.clearReader(&mr);
×
191
      cleanupQueriedTableScanInfo(&schemaInfo);
×
192
      return code;
×
193
    }
194

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
32,317,475✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
32,317,475✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
18,923,337✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
114,907,744✔
202

203
  if (schemaInfo.sw == NULL) {
114,833,023✔
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
114,833,023✔
209
  if (schemaInfo.qsw == NULL) {
114,907,265✔
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
114,907,265✔
215
  if (p == NULL) {
114,933,934✔
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
114,933,934✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
114,887,804✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
114,887,804✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
114,919,917✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
114,899,949✔
228
  if (pqSw == NULL) {
114,845,476✔
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
114,845,476✔
233
  if (pqSw->pSchema == NULL) {
114,773,486✔
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
726,367,394✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
611,420,842✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
611,442,298✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
611,449,082✔
243
    pSchema->colId = pColNode->colId;
611,454,828✔
244
    pSchema->type = pColNode->node.resType.type;
611,508,553✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
611,513,370✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
611,479,357✔
247
  }
248

249
  // this the tags and pseudo function columns, we only keep the tag columns
250
  for (int32_t i = 0; i < numOfTags; ++i) {
186,343,203✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
71,413,501✔
252

253
    int32_t type = nodeType(pNode->pExpr);
71,441,347✔
254
    if (type == QUERY_NODE_COLUMN) {
71,454,392✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
58,875,773✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
58,866,421✔
258
      pSchema->colId = pColNode->colId;
58,840,949✔
259
      pSchema->type = pColNode->node.resType.type;
58,877,074✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
58,839,011✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
58,868,857✔
262
    }
263
  }
264

265
  return pqSw;
114,929,702✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
156,221,518✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
156,221,518✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
156,222,615✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
156,209,346✔
272
  taosMemoryFree(pStreamInfo->stbFullName);
156,219,826✔
273
}
156,201,706✔
274

275
static void freeBlock(void* pParam) {
305,025,740✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
305,025,740✔
277
  blockDataDestroy(pBlock);
305,032,362✔
278
}
305,027,325✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
156,239,799✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
156,239,799✔
282
  destroyOperator(pTaskInfo->pRoot);
156,239,480✔
283
  pTaskInfo->pRoot = NULL;
156,222,015✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
156,230,413✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
156,214,570✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
156,211,307✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
156,209,985✔
290
    pTaskInfo->pSubplan = NULL;
156,220,006✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
156,221,961✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
156,216,585✔
295
  if (!pTaskInfo->paramSet) {
156,219,220✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
154,866,248✔
297
    pTaskInfo->pOpParam = NULL;
154,855,829✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
156,219,033✔
300
  taosMemoryFreeClear(pTaskInfo->id.str);
156,209,689✔
301
  taosMemoryFreeClear(pTaskInfo);
156,210,946✔
302
}
156,192,638✔
303

304
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
167,344,049✔
305
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
167,344,049✔
306
  if (ret < 0) {
167,344,049✔
307
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
308
  }
309
}
167,344,049✔
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