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

taosdata / TDengine / #4833

31 Oct 2025 03:37AM UTC coverage: 58.533% (-0.06%) from 58.59%
#4833

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149243 of 324176 branches covered (46.04%)

Branch coverage included in aggregate %.

198250 of 269498 relevant lines covered (73.56%)

237904970.78 hits per line

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

81.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,
1,221,960,771✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
1,221,960,771!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
1,221,960,771✔
45
  if (p == NULL) {
1,221,842,993!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
1,221,842,993✔
50
  p->cost.created = taosGetTimestampUs();
1,222,016,545✔
51

52
  p->execModel = model;
1,221,994,514✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
1,222,005,152✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
1,221,911,728✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
1,221,874,707✔
56
    doDestroyTask(p);
1,217✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
1,221,951,995✔
61
  taosInitRWLatch(&p->lock);
1,221,995,652✔
62

63
  p->id.vgId = vgId;
1,221,940,671✔
64
  p->id.queryId = queryId;
1,221,918,614✔
65
  p->id.taskId = taskId;
1,221,962,407✔
66
  p->id.str = taosMemoryMalloc(64);
1,221,966,699✔
67
  if (p->id.str == NULL) {
1,221,848,866!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
1,221,907,170✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
1,221,845,158✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
1,221,736,297!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
1,221,784,255✔
80
  return TSDB_CODE_SUCCESS;
1,221,823,217✔
81
}
82

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

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
2,147,483,647✔
91
  if (status == TASK_NOT_COMPLETED) {
2,147,483,647✔
92
    pTaskInfo->status = status;
1,222,207,598✔
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);
2,147,483,647✔
96
    pTaskInfo->status |= status;
2,147,483,647✔
97
  }
98
}
2,147,483,647✔
99

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

108
  (*pTaskInfo)->pSubplan = pPlan;
1,221,577,433✔
109

110
  if (pHandle) {
1,221,546,979✔
111
    if (pHandle->pStateBackend) {
1,221,411,627!
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
1,221,635,202✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
1,178,178,989✔
119
    if (NULL == (*pTaskInfo)->sql) {
1,178,233,929!
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
1,221,758,525✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
1,221,515,365✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
1,221,461,649✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
1,221,717,389✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
1,220,642,015✔
133
    doDestroyTask(*pTaskInfo);
×
134
    (*pTaskInfo) = NULL;
34,810✔
135
  }
136
  return code;
1,220,949,641✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
837,630,738✔
140
  SSchemaInfo* pSchemaInfo = p;
837,630,738✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
837,630,738✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
837,625,195✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
837,553,460!
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
837,587,331✔
146
}
837,574,157✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
837,637,254✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
837,637,254✔
151
  if (pHandle == NULL) {
837,667,005!
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
837,667,005✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
837,619,241✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
837,648,525✔
159
  if (code != TSDB_CODE_SUCCESS) {
836,827,390✔
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
3,877!
161
           GET_TASKID(pTaskInfo));
162

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

167
  SSchemaInfo schemaInfo = {0};
836,823,513✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
836,873,999✔
170
  schemaInfo.dbname = taosStrdup(dbName);
836,997,679✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
837,156,885✔
172
    pAPI->metaReaderFn.clearReader(&mr);
1,175,003✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
835,981,882!
178
    schemaInfo.rversion = mr.me.colRef.version;
812,496✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
835,981,882✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
552,589,670✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
552,589,670✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
283,543,314✔
185
    tDecoderClear(&mr.coder);
143,043,963✔
186

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

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
143,999,826✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
143,999,826✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
140,511,865✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
837,101,361✔
202

203
  if (schemaInfo.sw == NULL) {
836,659,834!
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
836,659,834✔
209
  if (schemaInfo.qsw == NULL) {
837,080,140!
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
837,080,140✔
215
  if (p == NULL) {
837,203,270!
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
837,203,270✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
836,843,057✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
836,843,057✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
837,082,569✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
837,110,103✔
228
  if (pqSw == NULL) {
836,427,569!
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
836,427,569✔
233
  if (pqSw->pSchema == NULL) {
835,825,919!
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
2,147,483,647✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
2,147,483,647✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
2,147,483,647✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
2,147,483,647✔
243
    pSchema->colId = pColNode->colId;
2,147,483,647✔
244
    pSchema->type = pColNode->node.resType.type;
2,147,483,647✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
2,147,483,647✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
2,147,483,647!
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) {
1,556,194,967✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
718,900,815✔
252

253
    int32_t type = nodeType(pNode->pExpr);
718,720,167✔
254
    if (type == QUERY_NODE_COLUMN) {
718,861,092✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
500,766,958✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
500,788,189✔
258
      pSchema->colId = pColNode->colId;
500,850,316✔
259
      pSchema->type = pColNode->node.resType.type;
500,561,839✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
500,748,462✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
500,606,243!
262
    }
263
  }
264

265
  return pqSw;
837,294,152✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
1,221,846,757✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
1,221,846,757✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
1,221,920,085✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
1,221,863,887!
272
  taosMemoryFree(pStreamInfo->stbFullName);
1,221,880,360✔
273
}
1,221,917,306✔
274

275
static void freeBlock(void* pParam) {
2,147,483,647✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
2,147,483,647✔
277
  blockDataDestroy(pBlock);
2,147,483,647✔
278
}
2,147,483,647✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
1,222,008,607✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
1,222,008,607✔
282
  destroyOperator(pTaskInfo->pRoot);
1,222,007,271✔
283
  pTaskInfo->pRoot = NULL;
1,221,899,950✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
1,221,934,615✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
1,221,840,506✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
1,221,907,200✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
1,186,740,953✔
290
    pTaskInfo->pSubplan = NULL;
1,186,686,147✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
1,221,791,244✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
1,221,898,689✔
295
  if (!pTaskInfo->paramSet) {
1,221,901,594✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
1,209,504,765✔
297
    pTaskInfo->pOpParam = NULL;
1,209,404,557✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
1,221,865,520!
300
  taosMemoryFreeClear(pTaskInfo->id.str);
1,221,866,310!
301
  taosMemoryFreeClear(pTaskInfo);
1,221,801,528!
302
}
1,221,671,462✔
303

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