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

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 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,
155,009,005✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
155,009,005✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
155,009,005✔
45
  if (p == NULL) {
155,002,550✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
155,002,550✔
50
  p->cost.created = taosGetTimestampUs();
155,008,408✔
51

52
  p->execModel = model;
155,006,852✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
155,009,363✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
155,006,625✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
155,002,679✔
56
    doDestroyTask(p);
3,424✔
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
155,001,361✔
61
  taosInitRWLatch(&p->lock);
155,003,749✔
62

63
  p->id.vgId = vgId;
155,001,733✔
64
  p->id.queryId = queryId;
155,001,769✔
65
  p->id.taskId = taskId;
155,003,526✔
66
  p->id.str = taosMemoryMalloc(64);
155,005,263✔
67
  if (p->id.str == NULL) {
154,995,076✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
154,998,235✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
155,000,503✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
154,984,551✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
154,988,954✔
80
  return TSDB_CODE_SUCCESS;
154,985,242✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
979,746,224✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
485,997,131✔
91
  if (status == TASK_NOT_COMPLETED) {
485,997,131✔
92
    pTaskInfo->status = status;
155,104,180✔
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);
330,892,951✔
96
    pTaskInfo->status |= status;
330,898,471✔
97
  }
98
}
486,000,078✔
99

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

108
  (*pTaskInfo)->pSubplan = pPlan;
154,969,076✔
109

110
  if (pHandle) {
154,961,654✔
111
    if (pHandle->pStateBackend) {
154,963,429✔
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
154,968,251✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
152,418,331✔
119
    if (NULL == (*pTaskInfo)->sql) {
152,425,641✔
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
154,979,953✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
154,967,149✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
154,939,910✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
154,948,014✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
154,866,518✔
133
    doDestroyTask(*pTaskInfo);
113,282✔
134
    (*pTaskInfo) = NULL;
26,233✔
135
  }
136
  return code;
154,806,330✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
115,710,254✔
140
  SSchemaInfo* pSchemaInfo = p;
115,710,254✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
115,710,254✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
115,707,954✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
115,705,237✔
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
115,707,052✔
146
}
115,709,130✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
115,712,780✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
115,712,780✔
151
  if (pHandle == NULL) {
115,713,706✔
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
115,713,706✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
115,712,235✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
115,719,476✔
159
  if (code != TSDB_CODE_SUCCESS) {
115,608,720✔
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
407✔
161
           GET_TASKID(pTaskInfo));
162

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

167
  SSchemaInfo schemaInfo = {0};
115,608,313✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
115,616,764✔
170
  schemaInfo.dbname = taosStrdup(dbName);
115,624,601✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
115,644,529✔
172
    pAPI->metaReaderFn.clearReader(&mr);
73,291✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
115,571,238✔
178
    schemaInfo.rversion = mr.me.colRef.version;
12,240✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
115,571,238✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
63,956,637✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
63,956,637✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
51,665,997✔
185
    tDecoderClear(&mr.coder);
32,107,406✔
186

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

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
32,110,658✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
32,110,658✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
19,566,842✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
115,634,137✔
202

203
  if (schemaInfo.sw == NULL) {
115,648,230✔
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
115,648,230✔
209
  if (schemaInfo.qsw == NULL) {
115,635,910✔
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
115,635,910✔
215
  if (p == NULL) {
115,651,392✔
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
115,651,392✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
115,611,057✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
115,611,057✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
115,663,794✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
115,604,657✔
228
  if (pqSw == NULL) {
115,549,585✔
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
115,549,585✔
233
  if (pqSw->pSchema == NULL) {
115,492,922✔
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
725,917,263✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
610,223,707✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
610,066,325✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
610,106,572✔
243
    pSchema->colId = pColNode->colId;
610,047,281✔
244
    pSchema->type = pColNode->node.resType.type;
610,251,462✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
610,251,360✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
610,262,361✔
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) {
188,043,266✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
72,346,269✔
252

253
    int32_t type = nodeType(pNode->pExpr);
72,351,564✔
254
    if (type == QUERY_NODE_COLUMN) {
72,357,977✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
59,966,024✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
59,972,395✔
258
      pSchema->colId = pColNode->colId;
59,944,699✔
259
      pSchema->type = pColNode->node.resType.type;
59,934,154✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
59,912,599✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
59,920,039✔
262
    }
263
  }
264

265
  return pqSw;
115,696,997✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
154,986,305✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
154,986,305✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
154,988,492✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
154,971,926✔
272
  taosMemoryFree(pStreamInfo->stbFullName);
154,985,370✔
273
}
154,989,784✔
274

275
static void freeBlock(void* pParam) {
298,406,799✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
298,406,799✔
277
  blockDataDestroy(pBlock);
298,407,870✔
278
}
298,419,179✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
155,007,934✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
155,007,934✔
282
  destroyOperator(pTaskInfo->pRoot);
155,007,492✔
283
  pTaskInfo->pRoot = NULL;
154,990,116✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
154,996,367✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
154,990,705✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
154,985,473✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
154,977,328✔
290
    pTaskInfo->pSubplan = NULL;
154,992,597✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
154,998,026✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
154,998,143✔
295
  if (!pTaskInfo->paramSet) {
154,997,364✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
154,665,537✔
297
    pTaskInfo->pOpParam = NULL;
154,644,088✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
154,984,912✔
300
  taosMemoryFreeClear(pTaskInfo->id.str);
154,988,941✔
301
  taosMemoryFreeClear(pTaskInfo);
154,975,880✔
302
}
154,961,960✔
303

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