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

taosdata / TDengine / #4865

26 Nov 2025 05:46AM UTC coverage: 64.495% (-0.05%) from 64.548%
#4865

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

767 of 945 new or added lines in 33 files covered. (81.16%)

3088 existing lines in 119 files now uncovered.

158096 of 245129 relevant lines covered (64.5%)

111671620.5 hits per line

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

81.32
/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,
171,035,334✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
171,035,334✔
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
171,035,334✔
45
  if (p == NULL) {
171,028,080✔
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
171,028,080✔
50
  p->cost.created = taosGetTimestampUs();
171,041,171✔
51

52
  p->execModel = model;
171,038,528✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
171,036,380✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
171,034,184✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
171,033,029✔
UNCOV
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
171,035,175✔
61
  taosInitRWLatch(&p->lock);
171,033,678✔
62

63
  p->id.vgId = vgId;
171,030,179✔
64
  p->id.queryId = queryId;
171,031,043✔
65
  p->id.taskId = taskId;
171,032,445✔
66
  p->id.str = taosMemoryMalloc(64);
171,033,079✔
67
  if (p->id.str == NULL) {
171,024,820✔
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str, 64);
171,027,120✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
171,023,764✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
171,009,647✔
UNCOV
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
171,017,511✔
80
  return TSDB_CODE_SUCCESS;
171,011,706✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,122,065,166✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
532,066,535✔
91
  if (status == TASK_NOT_COMPLETED) {
532,066,535✔
92
    pTaskInfo->status = status;
171,128,568✔
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);
360,937,967✔
96
    pTaskInfo->status |= status;
360,940,436✔
97
  }
98
}
532,066,527✔
99

100
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
171,013,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);
171,013,704✔
103
  if (*pTaskInfo == NULL || code != 0) {
170,994,342✔
104
    nodesDestroyNode((SNode*)pPlan);
689✔
105
    return code;
×
106
  }
107

108
  (*pTaskInfo)->pSubplan = pPlan;
170,999,342✔
109

110
  if (pHandle) {
170,988,476✔
111
    if (pHandle->pStateBackend) {
170,988,629✔
112
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
113
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
114
    }
115
  }
116

117
  if (NULL != sql) {
170,995,520✔
118
    (*pTaskInfo)->sql = taosStrdup(sql);
168,442,619✔
119
    if (NULL == (*pTaskInfo)->sql) {
168,448,997✔
120
      code = terrno;
×
121
      doDestroyTask(*pTaskInfo);
×
122
      (*pTaskInfo) = NULL;
×
123
      return code;
×
124
    }
125
  }
126

127
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
171,001,657✔
128
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
170,993,288✔
129
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
170,976,756✔
130
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
170,988,668✔
131

132
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
170,920,326✔
133
    doDestroyTask(*pTaskInfo);
144,813✔
134
    (*pTaskInfo) = NULL;
26,995✔
135
  }
136
  return code;
170,826,207✔
137
}
138

139
void cleanupQueriedTableScanInfo(void* p) {
119,063,872✔
140
  SSchemaInfo* pSchemaInfo = p;
119,063,872✔
141

142
  taosMemoryFreeClear(pSchemaInfo->dbname);
119,063,872✔
143
  taosMemoryFreeClear(pSchemaInfo->tablename);
119,059,515✔
144
  tDeleteSchemaWrapper(pSchemaInfo->sw);
119,058,581✔
145
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
119,061,181✔
146
}
119,059,040✔
147

148
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
119,059,472✔
149
                                   SExecTaskInfo* pTaskInfo) {
150
  SMetaReader mr = {0};
119,059,472✔
151
  if (pHandle == NULL) {
119,064,715✔
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154

155
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
119,064,715✔
156

157
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
119,061,017✔
158
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
119,057,993✔
159
  if (code != TSDB_CODE_SUCCESS) {
118,899,531✔
UNCOV
160
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
161
           GET_TASKID(pTaskInfo));
162

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

167
  SSchemaInfo schemaInfo = {0};
118,899,531✔
168

169
  schemaInfo.tablename = taosStrdup(mr.me.name);
118,941,987✔
170
  schemaInfo.dbname = taosStrdup(dbName);
118,964,687✔
171
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
118,985,724✔
172
    pAPI->metaReaderFn.clearReader(&mr);
75,701✔
173
    cleanupQueriedTableScanInfo(&schemaInfo);
×
174
    return terrno;
×
175
  }
176

177
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
118,910,023✔
178
    schemaInfo.rversion = mr.me.colRef.version;
315,614✔
179
  }
180

181
  if (mr.me.type == TSDB_SUPER_TABLE) {
118,910,023✔
182
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
62,604,734✔
183
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
62,604,734✔
184
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
56,345,456✔
185
    tDecoderClear(&mr.coder);
31,830,068✔
186

187
    tb_uid_t suid = mr.me.ctbEntry.suid;
31,831,543✔
188
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
31,831,543✔
189
    if (code != TSDB_CODE_SUCCESS) {
31,830,387✔
190
      pAPI->metaReaderFn.clearReader(&mr);
×
191
      cleanupQueriedTableScanInfo(&schemaInfo);
×
192
      return code;
×
193
    }
194

195
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
31,830,147✔
196
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
31,830,147✔
197
  } else {
198
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
24,519,963✔
199
  }
200

201
  pAPI->metaReaderFn.clearReader(&mr);
118,954,844✔
202

203
  if (schemaInfo.sw == NULL) {
118,944,984✔
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
118,944,984✔
209
  if (schemaInfo.qsw == NULL) {
118,957,398✔
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
118,957,398✔
215
  if (p == NULL) {
118,989,320✔
216
    cleanupQueriedTableScanInfo(&schemaInfo);
×
217
    return terrno;
×
218
  }
219

220
  return code;
118,989,320✔
221
}
222

223
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
118,966,973✔
224
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
118,966,973✔
225
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
119,004,146✔
226

227
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
118,940,357✔
228
  if (pqSw == NULL) {
118,897,924✔
229
    return NULL;
×
230
  }
231

232
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
118,897,924✔
233
  if (pqSw->pSchema == NULL) {
118,808,514✔
234
    taosMemoryFree(pqSw);
×
235
    return NULL;
×
236
  }
237

238
  for (int32_t i = 0; i < numOfCols; ++i) {
724,191,495✔
239
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
605,149,304✔
240
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
605,063,318✔
241

242
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
605,100,428✔
243
    pSchema->colId = pColNode->colId;
605,089,557✔
244
    pSchema->type = pColNode->node.resType.type;
605,226,854✔
245
    pSchema->bytes = pColNode->node.resType.bytes;
605,212,267✔
246
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
605,236,235✔
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) {
193,984,998✔
251
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
74,949,284✔
252

253
    int32_t type = nodeType(pNode->pExpr);
74,931,125✔
254
    if (type == QUERY_NODE_COLUMN) {
74,939,446✔
255
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
63,330,332✔
256

257
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
63,340,994✔
258
      pSchema->colId = pColNode->colId;
63,322,516✔
259
      pSchema->type = pColNode->node.resType.type;
63,327,396✔
260
      pSchema->bytes = pColNode->node.resType.bytes;
63,272,508✔
261
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
63,328,829✔
262
    }
263
  }
264

265
  return pqSw;
119,035,714✔
266
}
267

268
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
171,018,095✔
269
  tDeleteSchemaWrapper(pStreamInfo->schema);
171,018,095✔
270
  tOffsetDestroy(&pStreamInfo->currentOffset);
171,026,140✔
271
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
171,006,290✔
272
  taosMemoryFree(pStreamInfo->stbFullName);
171,017,146✔
273
}
171,019,264✔
274

275
static void freeBlock(void* pParam) {
303,529,333✔
276
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
303,529,333✔
277
  blockDataDestroy(pBlock);
303,530,905✔
278
}
303,533,687✔
279

280
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
171,039,585✔
281
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
171,039,585✔
282
  destroyOperator(pTaskInfo->pRoot);
171,039,585✔
283
  pTaskInfo->pRoot = NULL;
171,024,349✔
284

285
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
171,030,286✔
286
  cleanupStreamInfo(&pTaskInfo->streamInfo);
171,017,978✔
287

288
  if (!pTaskInfo->localFetch.localExec) {
171,015,640✔
289
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
170,939,160✔
290
    pTaskInfo->pSubplan = NULL;
170,952,275✔
291
  }
292

293
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
171,028,420✔
294
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
171,024,782✔
295
  if (!pTaskInfo->paramSet) {
171,025,172✔
296
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
166,229,649✔
297
    pTaskInfo->pOpParam = NULL;
166,214,896✔
298
  }
299
  taosMemoryFreeClear(pTaskInfo->sql);
171,020,335✔
300
  taosMemoryFreeClear(pTaskInfo->id.str);
171,016,915✔
301
  taosMemoryFreeClear(pTaskInfo);
171,007,309✔
302
}
170,988,618✔
303

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