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

taosdata / TDengine / #4069

12 May 2025 05:35AM UTC coverage: 63.048% (+0.5%) from 62.547%
#4069

push

travis-ci

web-flow
Merge pull request #31053 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

157521 of 317858 branches covered (49.56%)

Branch coverage included in aggregate %.

374 of 573 new or added lines in 31 files covered. (65.27%)

4949 existing lines in 87 files now uncovered.

242707 of 316936 relevant lines covered (76.58%)

18229906.31 hits per line

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

76.09
/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,
8,590,763✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
8,590,763!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
8,590,763!
45
  if (p == NULL) {
8,591,523!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
8,591,523✔
50
  p->cost.created = taosGetTimestampUs();
8,593,477✔
51

52
  p->execModel = model;
8,593,477✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
8,593,477✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
8,601,729✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
8,592,425!
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
8,593,476✔
61
  taosInitRWLatch(&p->lock);
8,593,476✔
62

63
  p->id.vgId = vgId;
8,601,898✔
64
  p->id.queryId = queryId;
8,601,898✔
65
  p->id.taskId = taskId;
8,601,898✔
66
  p->id.str = taosMemoryMalloc(64);
8,601,898!
67
  if (p->id.str == NULL) {
8,598,122!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str);
8,598,122✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
8,601,669✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
8,597,808!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
8,598,674✔
80
  return TSDB_CODE_SUCCESS;
8,598,674✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
51,650,839✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
26,887,192✔
91
  if (status == TASK_NOT_COMPLETED) {
26,887,192✔
92
    pTaskInfo->status = status;
8,592,794✔
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);
18,294,398✔
96
    pTaskInfo->status |= status;
18,294,398✔
97
  }
98
}
26,887,192✔
99

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

108
  if (pHandle) {
8,599,425!
109
    if (pHandle->pStateBackend) {
8,601,166✔
110
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
6,389✔
111
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
6,389✔
112
    }
113
  }
114

115
  if (NULL != sql) {
8,599,425✔
116
    (*pTaskInfo)->sql = taosStrdup(sql);
8,435,686!
117
    if (NULL == (*pTaskInfo)->sql) {
8,422,845!
118
      code = terrno;
×
119
      nodesDestroyNode((SNode*)pPlan);
×
120
      doDestroyTask(*pTaskInfo);
×
121
      (*pTaskInfo) = NULL;
×
122
      return code;
×
123
    }
124
  }
125

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

131
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
8,589,936!
132
    doDestroyTask(*pTaskInfo);
×
133
    (*pTaskInfo) = NULL;
368✔
134
  }
135
  return code;
8,599,465✔
136
}
137

138
void cleanupQueriedTableScanInfo(void* p) {
5,119,295✔
139
  SSchemaInfo* pSchemaInfo = p;
5,119,295✔
140

141
  taosMemoryFreeClear(pSchemaInfo->dbname);
5,119,295!
142
  taosMemoryFreeClear(pSchemaInfo->tablename);
5,119,646!
143
  tDeleteSchemaWrapper(pSchemaInfo->sw);
5,119,642!
144
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
5,119,980!
145
}
5,119,999✔
146

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

154
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
5,114,794✔
155

156
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
5,114,794✔
157
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
5,119,408✔
158
  if (code != TSDB_CODE_SUCCESS) {
5,110,993✔
159
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
120!
160
           GET_TASKID(pTaskInfo));
161

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

166
  SSchemaInfo schemaInfo = {0};
5,110,873✔
167

168
  schemaInfo.tablename = taosStrdup(mr.me.name);
5,110,873!
169
  schemaInfo.dbname = taosStrdup(dbName);
5,110,235✔
170
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
5,115,893!
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) {
5,116,283!
NEW
177
    schemaInfo.rversion = mr.me.colRef.version;
×
178
  }
179

180
  if (mr.me.type == TSDB_SUPER_TABLE) {
5,116,283✔
181
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4,236,390✔
182
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
4,236,390✔
183
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
879,607!
184
    tDecoderClear(&mr.coder);
562,218✔
185

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

194
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
557,724✔
195
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
557,724✔
196
  } else {
197
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
317,443✔
198
  }
199

200
  pAPI->metaReaderFn.clearReader(&mr);
5,111,557✔
201

202
  if (schemaInfo.sw == NULL) {
5,116,243!
UNCOV
203
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
204
    return terrno;
×
205
  }
206

207
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
5,116,243✔
208
  if (schemaInfo.qsw == NULL) {
5,115,799!
UNCOV
209
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
210
    return terrno;
×
211
  }
212

213
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
5,115,799✔
214
  if (p == NULL) {
5,107,773!
UNCOV
215
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
216
    return terrno;
×
217
  }
218

219
  return code;
5,107,773✔
220
}
221

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

226
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
5,110,288!
227
  if (pqSw == NULL) {
5,116,824!
UNCOV
228
    return NULL;
×
229
  }
230

231
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
5,116,824!
232
  if (pqSw->pSchema == NULL) {
5,111,775!
UNCOV
233
    taosMemoryFree(pqSw);
×
UNCOV
234
    return NULL;
×
235
  }
236

237
  for (int32_t i = 0; i < numOfCols; ++i) {
31,937,734✔
238
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
26,852,742✔
239
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
26,825,959✔
240

241
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
26,825,959✔
242
    pSchema->colId = pColNode->colId;
26,825,959✔
243
    pSchema->type = pColNode->node.resType.type;
26,825,959✔
244
    pSchema->bytes = pColNode->node.resType.bytes;
26,825,959✔
245
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
26,825,959✔
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) {
11,942,711✔
250
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
6,864,747✔
251

252
    int32_t type = nodeType(pNode->pExpr);
6,857,719✔
253
    if (type == QUERY_NODE_COLUMN) {
6,857,719✔
254
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
6,204,559✔
255

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

264
  return pqSw;
5,077,964✔
265
}
266

267
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
8,605,131✔
268
  tDeleteSchemaWrapper(pStreamInfo->schema);
8,605,131✔
269
  tOffsetDestroy(&pStreamInfo->currentOffset);
8,605,102✔
270
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
8,605,339!
271
  taosMemoryFree(pStreamInfo->stbFullName);
8,604,594✔
272
}
8,604,256✔
273

274
static void freeBlock(void* pParam) {
14,481,458✔
275
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
14,481,458✔
276
  blockDataDestroy(pBlock);
14,481,458✔
277
}
14,481,941✔
278

279
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
8,603,357✔
280
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
8,603,357✔
281
  destroyOperator(pTaskInfo->pRoot);
8,603,357✔
282
  pTaskInfo->pRoot = NULL;
8,605,493✔
283

284
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
8,605,493✔
285
  cleanupStreamInfo(&pTaskInfo->streamInfo);
8,605,430✔
286

287
  if (!pTaskInfo->localFetch.localExec) {
8,604,214✔
288
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
8,529,560✔
289
    pTaskInfo->pSubplan = NULL;
8,530,733✔
290
  }
291

292
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
8,605,387✔
293
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
8,605,763✔
294
  if (!pTaskInfo->paramSet) {
8,605,834✔
295
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
8,604,746✔
296
    pTaskInfo->pOpParam = NULL;
8,604,132✔
297
  }
298
  taosMemoryFreeClear(pTaskInfo->sql);
8,605,220!
299
  taosMemoryFreeClear(pTaskInfo->id.str);
8,605,709!
300
  taosMemoryFreeClear(pTaskInfo);
8,605,726!
301
}
8,605,775✔
302

303
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
8,905,742✔
304
  char* p = dst;
8,905,742✔
305

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

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

314
  p[offset] = 0;
8,910,966✔
315
}
8,910,966✔
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