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

taosdata / TDengine / #4295

14 Jun 2025 08:14AM UTC coverage: 62.777% (-0.1%) from 62.881%
#4295

push

travis-ci

web-flow
refactor: update container_build.sh to include branch option for taosadapter (#31367)

157612 of 320085 branches covered (49.24%)

Branch coverage included in aggregate %.

243488 of 318844 relevant lines covered (76.37%)

6438123.12 hits per line

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

77.26
/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,
2,008,551✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
2,008,551!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
2,008,551!
45
  if (p == NULL) {
2,008,587!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
2,008,587✔
50
  p->cost.created = taosGetTimestampUs();
2,009,840✔
51

52
  p->execModel = model;
2,009,840✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
2,009,840✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
2,011,933✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
2,009,587!
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
2,009,788✔
61
  taosInitRWLatch(&p->lock);
2,009,788✔
62

63
  p->id.vgId = vgId;
2,011,879✔
64
  p->id.queryId = queryId;
2,011,879✔
65
  p->id.taskId = taskId;
2,011,879✔
66
  p->id.str = taosMemoryMalloc(64);
2,011,879!
67
  if (p->id.str == NULL) {
2,011,492!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str);
2,011,492✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
2,011,540✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
2,011,432!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
2,011,679✔
80
  return TSDB_CODE_SUCCESS;
2,011,679✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
10,511,588✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
6,066,952✔
91
  if (status == TASK_NOT_COMPLETED) {
6,066,952✔
92
    pTaskInfo->status = status;
2,009,635✔
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);
4,057,317✔
96
    pTaskInfo->status |= status;
4,057,317✔
97
  }
98
}
6,066,952✔
99

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

108
  if (pHandle) {
2,011,641!
109
    if (pHandle->pStateBackend) {
2,011,762✔
110
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
3,601✔
111
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
3,601✔
112
    }
113
  }
114

115
  if (NULL != sql) {
2,011,641✔
116
    (*pTaskInfo)->sql = taosStrdup(sql);
1,924,402!
117
    if (NULL == (*pTaskInfo)->sql) {
1,921,955!
118
      code = terrno;
×
119
      nodesDestroyNode((SNode*)pPlan);
×
120
      doDestroyTask(*pTaskInfo);
×
121
      (*pTaskInfo) = NULL;
×
122
      return code;
×
123
    }
124
  }
125

126
  (*pTaskInfo)->pSubplan = pPlan;
2,009,194✔
127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
2,009,194✔
128
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
2,009,194✔
129
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
2,009,194✔
130

131
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
2,009,923!
132
    doDestroyTask(*pTaskInfo);
×
133
    (*pTaskInfo) = NULL;
366✔
134
  }
135
  return code;
2,011,778✔
136
}
137

138
void cleanupQueriedTableScanInfo(void* p) {
1,376,249✔
139
  SSchemaInfo* pSchemaInfo = p;
1,376,249✔
140

141
  taosMemoryFreeClear(pSchemaInfo->dbname);
1,376,249!
142
  taosMemoryFreeClear(pSchemaInfo->tablename);
1,376,469!
143
  tDeleteSchemaWrapper(pSchemaInfo->sw);
1,376,461!
144
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
1,376,706✔
145
}
1,376,705✔
146

147
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
1,375,527✔
148
                                   SExecTaskInfo* pTaskInfo) {
149
  SMetaReader mr = {0};
1,375,527✔
150
  if (pHandle == NULL) {
1,375,527!
151
    return TSDB_CODE_INVALID_PARA;
×
152
  }
153

154
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
1,375,527✔
155

156
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
1,375,527✔
157
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
1,376,525✔
158
  if (code != TSDB_CODE_SUCCESS) {
1,373,562✔
159
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
116!
160
           GET_TASKID(pTaskInfo));
161

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

166
  SSchemaInfo schemaInfo = {0};
1,373,446✔
167

168
  schemaInfo.tablename = taosStrdup(mr.me.name);
1,373,446!
169
  schemaInfo.dbname = taosStrdup(dbName);
1,375,072✔
170
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
1,376,099!
171
    pAPI->metaReaderFn.clearReader(&mr);
×
172
    cleanupQueriedTableScanInfo(&schemaInfo);
×
173
    return terrno;
×
174
  }
175

176
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
1,376,128!
177
    schemaInfo.rversion = mr.me.colRef.version;
×
178
  }
179

180
  if (mr.me.type == TSDB_SUPER_TABLE) {
1,376,128✔
181
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
842,991✔
182
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
842,991✔
183
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
532,891✔
184
    tDecoderClear(&mr.coder);
294,067✔
185

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

194
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
293,093✔
195
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
293,093✔
196
  } else {
197
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
238,895✔
198
  }
199

200
  pAPI->metaReaderFn.clearReader(&mr);
1,374,979✔
201

202
  if (schemaInfo.sw == NULL) {
1,375,997!
203
    cleanupQueriedTableScanInfo(&schemaInfo);
×
204
    return terrno;
×
205
  }
206

207
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
1,375,997✔
208
  if (schemaInfo.qsw == NULL) {
1,375,694!
209
    cleanupQueriedTableScanInfo(&schemaInfo);
×
210
    return terrno;
×
211
  }
212

213
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
1,375,694✔
214
  if (p == NULL) {
1,374,477!
215
    cleanupQueriedTableScanInfo(&schemaInfo);
×
216
    return terrno;
×
217
  }
218

219
  return code;
1,374,477✔
220
}
221

222
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
1,373,751✔
223
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
1,373,751!
224
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
1,373,751✔
225

226
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
1,373,751!
227
  if (pqSw == NULL) {
1,376,070!
228
    return NULL;
×
229
  }
230

231
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
1,376,070!
232
  if (pqSw->pSchema == NULL) {
1,375,106!
233
    taosMemoryFree(pqSw);
×
234
    return NULL;
×
235
  }
236

237
  for (int32_t i = 0; i < numOfCols; ++i) {
7,228,965✔
238
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
5,852,142✔
239
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
5,853,859✔
240

241
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
5,853,859✔
242
    pSchema->colId = pColNode->colId;
5,853,859✔
243
    pSchema->type = pColNode->node.resType.type;
5,853,859✔
244
    pSchema->bytes = pColNode->node.resType.bytes;
5,853,859✔
245
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
5,853,859✔
246
  }
247

248
  // this the tags and pseudo function columns, we only keep the tag columns
249
  for (int32_t i = 0; i < numOfTags; ++i) {
2,426,371✔
250
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
1,049,357✔
251

252
    int32_t type = nodeType(pNode->pExpr);
1,049,548✔
253
    if (type == QUERY_NODE_COLUMN) {
1,049,548✔
254
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
818,013✔
255

256
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
818,013✔
257
      pSchema->colId = pColNode->colId;
818,013✔
258
      pSchema->type = pColNode->node.resType.type;
818,013✔
259
      pSchema->bytes = pColNode->node.resType.bytes;
818,013✔
260
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
818,013✔
261
    }
262
  }
263

264
  return pqSw;
1,377,014✔
265
}
266

267
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
2,012,489✔
268
  tDeleteSchemaWrapper(pStreamInfo->schema);
2,012,489✔
269
  tOffsetDestroy(&pStreamInfo->currentOffset);
2,012,475✔
270
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
2,012,608!
271
  taosMemoryFree(pStreamInfo->stbFullName);
2,012,284✔
272
}
2,012,592✔
273

274
static void freeBlock(void* pParam) {
4,097,598✔
275
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
4,097,598✔
276
  blockDataDestroy(pBlock);
4,097,598✔
277
}
4,097,773✔
278

279
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
2,012,057✔
280
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
2,012,057✔
281
  destroyOperator(pTaskInfo->pRoot);
2,012,057✔
282
  pTaskInfo->pRoot = NULL;
2,012,529✔
283

284
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
2,012,529✔
285
  cleanupStreamInfo(&pTaskInfo->streamInfo);
2,012,670✔
286

287
  if (!pTaskInfo->localFetch.localExec) {
2,012,589✔
288
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
1,937,515✔
289
    pTaskInfo->pSubplan = NULL;
1,937,569✔
290
  }
291

292
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
2,012,643✔
293
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
2,012,833✔
294
  if (!pTaskInfo->paramSet) {
2,012,816✔
295
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
2,011,637✔
296
    pTaskInfo->pOpParam = NULL;
2,011,326✔
297
  }
298
  taosMemoryFreeClear(pTaskInfo->sql);
2,012,505!
299
  taosMemoryFreeClear(pTaskInfo->id.str);
2,012,852!
300
  taosMemoryFreeClear(pTaskInfo);
2,012,855!
301
}
2,012,870✔
302

303
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
2,055,034✔
304
  char* p = dst;
2,055,034✔
305

306
  int32_t offset = 6;
2,055,034✔
307
  memcpy(p, "TID:0x", offset);
2,055,034✔
308
  offset += tintToHex(taskId, &p[offset]);
2,055,034✔
309

310
  memcpy(&p[offset], " QID:0x", 7);
2,055,555✔
311
  offset += 7;
2,055,555✔
312
  offset += tintToHex(queryId, &p[offset]);
2,055,555✔
313

314
  p[offset] = 0;
2,055,653✔
315
}
2,055,653✔
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