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

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

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

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 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,
7,238,453✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
7,238,453!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
7,238,453!
45
  if (p == NULL) {
7,238,307!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
7,238,307✔
50
  p->cost.created = taosGetTimestampUs();
7,244,667✔
51

52
  p->execModel = model;
7,244,667✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
7,244,667✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
7,248,697✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
7,239,533!
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
7,240,426✔
61
  taosInitRWLatch(&p->lock);
7,240,426✔
62

63
  p->id.vgId = vgId;
7,250,405✔
64
  p->id.queryId = queryId;
7,250,405✔
65
  p->id.taskId = taskId;
7,250,405✔
66
  p->id.str = taosMemoryMalloc(64);
7,250,405!
67
  if (p->id.str == NULL) {
7,247,064!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str);
7,247,064✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
7,250,452✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
7,244,689!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
7,245,094✔
80
  return TSDB_CODE_SUCCESS;
7,245,094✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
33,648,513✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
22,826,000✔
91
  if (status == TASK_NOT_COMPLETED) {
22,826,000✔
92
    pTaskInfo->status = status;
7,239,697✔
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);
15,586,303✔
96
    pTaskInfo->status |= status;
15,586,303✔
97
  }
98
}
22,826,000✔
99

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

108
  if (pHandle) {
7,245,666!
109
    if (pHandle->pStateBackend) {
7,246,151✔
110
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
6,380✔
111
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
6,380✔
112
    }
113
  }
114

115
  if (NULL != sql) {
7,245,666✔
116
    (*pTaskInfo)->sql = taosStrdup(sql);
7,084,126!
117
    if (NULL == (*pTaskInfo)->sql) {
7,073,160!
118
      code = terrno;
×
119
      nodesDestroyNode((SNode*)pPlan);
×
120
      doDestroyTask(*pTaskInfo);
×
121
      (*pTaskInfo) = NULL;
×
122
      return code;
×
123
    }
124
  }
125

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

131
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
7,240,755!
132
    doDestroyTask(*pTaskInfo);
×
133
    (*pTaskInfo) = NULL;
367✔
134
  }
135
  return code;
7,249,057✔
136
}
137

138
void cleanupQueriedTableScanInfo(void* p) {
4,334,286✔
139
  SSchemaInfo* pSchemaInfo = p;
4,334,286✔
140

141
  taosMemoryFreeClear(pSchemaInfo->dbname);
4,334,286!
142
  taosMemoryFreeClear(pSchemaInfo->tablename);
4,334,847!
143
  tDeleteSchemaWrapper(pSchemaInfo->sw);
4,334,845!
144
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
4,335,277!
145
}
4,335,267✔
146

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

154
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
4,331,684✔
155

156
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
4,331,684✔
157
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
4,334,600✔
158
  if (code != TSDB_CODE_SUCCESS) {
4,323,177✔
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};
4,323,057✔
167

168
  schemaInfo.tablename = taosStrdup(mr.me.name);
4,323,057!
169
  schemaInfo.dbname = taosStrdup(dbName);
4,327,661✔
170
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
4,331,486!
UNCOV
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) {
4,331,672!
177
    schemaInfo.rversion = mr.me.colRef.version;
×
178
  }
179

180
  if (mr.me.type == TSDB_SUPER_TABLE) {
4,331,672✔
181
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
3,493,408✔
182
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
3,493,408✔
183
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
838,078!
184
    tDecoderClear(&mr.coder);
529,042✔
185

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

194
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
524,128✔
195
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
524,128✔
196
  } else {
197
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
309,105✔
198
  }
199

200
  pAPI->metaReaderFn.clearReader(&mr);
4,326,641✔
201

202
  if (schemaInfo.sw == NULL) {
4,329,620!
203
    cleanupQueriedTableScanInfo(&schemaInfo);
×
204
    return terrno;
×
205
  }
206

207
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
4,329,620✔
208
  if (schemaInfo.qsw == NULL) {
4,333,061!
209
    cleanupQueriedTableScanInfo(&schemaInfo);
×
210
    return terrno;
×
211
  }
212

213
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
4,333,061✔
214
  if (p == NULL) {
4,326,197!
215
    cleanupQueriedTableScanInfo(&schemaInfo);
×
216
    return terrno;
×
217
  }
218

219
  return code;
4,326,197✔
220
}
221

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

226
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
4,324,741!
227
  if (pqSw == NULL) {
4,332,050!
228
    return NULL;
×
229
  }
230

231
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
4,332,050!
232
  if (pqSw->pSchema == NULL) {
4,327,172!
233
    taosMemoryFree(pqSw);
×
234
    return NULL;
×
235
  }
236

237
  for (int32_t i = 0; i < numOfCols; ++i) {
25,269,996✔
238
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
20,946,266✔
239
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
20,942,824✔
240

241
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
20,942,824✔
242
    pSchema->colId = pColNode->colId;
20,942,824✔
243
    pSchema->type = pColNode->node.resType.type;
20,942,824✔
244
    pSchema->bytes = pColNode->node.resType.bytes;
20,942,824✔
245
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
20,942,824✔
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) {
10,260,944✔
250
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
5,938,637✔
251

252
    int32_t type = nodeType(pNode->pExpr);
5,937,214✔
253
    if (type == QUERY_NODE_COLUMN) {
5,937,214✔
254
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
5,325,994✔
255

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

264
  return pqSw;
4,322,307✔
265
}
266

267
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
7,252,919✔
268
  tDeleteSchemaWrapper(pStreamInfo->schema);
7,252,919✔
269
  tOffsetDestroy(&pStreamInfo->currentOffset);
7,252,961✔
270
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
7,253,573!
271
  taosMemoryFree(pStreamInfo->stbFullName);
7,252,692✔
272
}
7,252,751✔
273

274
static void freeBlock(void* pParam) {
10,680,294✔
275
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
10,680,294✔
276
  blockDataDestroy(pBlock);
10,680,294✔
277
}
10,680,671✔
278

279
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
7,251,499✔
280
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
7,251,499✔
281
  destroyOperator(pTaskInfo->pRoot);
7,251,499✔
282
  pTaskInfo->pRoot = NULL;
7,253,534✔
283

284
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
7,253,534✔
285
  cleanupStreamInfo(&pTaskInfo->streamInfo);
7,253,393✔
286

287
  if (!pTaskInfo->localFetch.localExec) {
7,252,760✔
288
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
7,178,204✔
289
    pTaskInfo->pSubplan = NULL;
7,179,020✔
290
  }
291

292
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
7,253,576✔
293
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
7,253,974✔
294
  if (!pTaskInfo->paramSet) {
7,253,970✔
295
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
7,252,767✔
296
    pTaskInfo->pOpParam = NULL;
7,251,984✔
297
  }
298
  taosMemoryFreeClear(pTaskInfo->sql);
7,253,187!
299
  taosMemoryFreeClear(pTaskInfo->id.str);
7,253,873!
300
  taosMemoryFreeClear(pTaskInfo);
7,253,998!
301
}
7,253,994✔
302

303
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
7,287,880✔
304
  char* p = dst;
7,287,880✔
305

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

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

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