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

taosdata / TDengine / #4977

06 Mar 2026 09:48AM UTC coverage: 68.456% (+0.01%) from 68.446%
#4977

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

3178 existing lines in 120 files now uncovered.

211178 of 308486 relevant lines covered (68.46%)

135843202.71 hits per line

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

82.16
/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
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
348,392,795✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
348,392,795✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
348,392,795✔
46
  if (p == NULL) {
348,318,192✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
348,318,192✔
51
  p->cost.created = taosGetTimestampUs();
348,410,713✔
52

53
  p->execModel = model;
348,411,169✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
348,415,384✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
348,379,958✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
348,352,025✔
UNCOV
57
    doDestroyTask(p);
×
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
348,391,343✔
62
  taosInitRWLatch(&p->lock);
348,405,825✔
63

64
  p->id.vgId = vgId;
348,372,912✔
65
  p->id.queryId = queryId;
348,355,769✔
66
  p->id.taskId = taskId;
348,385,172✔
67
  p->id.str = taosMemoryMalloc(64);
348,386,484✔
68
  if (p->id.str == NULL) {
348,325,499✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
348,359,177✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
348,338,712✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
348,296,764✔
76
    doDestroyTask(p);
77✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
348,331,897✔
81
  return TSDB_CODE_SUCCESS;
348,340,747✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
5,380✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,798,875,193✔
87

88
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
84,979✔
89
  pTaskInfo->code = rspCode;
84,979✔
90
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
84,979✔
91
}
84,979✔
92

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,095,485,102✔
94
  if (status == TASK_NOT_COMPLETED) {
1,095,485,102✔
95
    pTaskInfo->status = status;
348,509,198✔
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
746,975,904✔
99
    pTaskInfo->status |= status;
746,990,858✔
100
  }
101
}
1,095,546,750✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
37,914,365✔
105
  int32_t code = 0, lino = 0;
37,914,365✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
37,914,365✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
37,911,760✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
37,907,960✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
37,911,743✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
37,913,289✔
114
  ctx->taskId = pTaskInfo->id.taskId;
37,917,038✔
115
  ctx->idStr = pTaskInfo->id.str;
37,913,827✔
116
  ctx->pTaskInfo = pTaskInfo;
37,911,222✔
117
  ctx->subEndPoints = *subEndPoints;
37,912,819✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
37,911,154✔
119

120
  *subEndPoints = NULL;
37,916,500✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,916,517✔
123
  if (NULL == ctx->subResNodes) {
37,913,340✔
124
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
125
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
126
  }
127
  
128
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
37,912,213✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
37,905,287✔
131
  if (refId < 0) {
37,912,836✔
132
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
133
    TAOS_CHECK_EXIT(terrno);
×
134
  }
135
  
136
  ctx->subJobRefId = refId;
37,912,836✔
137

138
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
37,909,608✔
139

140
_exit:
37,717,160✔
141

142
  if (code) {
37,909,036✔
143
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
144
    pTaskInfo->pSubJobCtx = NULL;
×
145
    
146
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
147
  }
148

149
  return code;
37,909,557✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
348,270,889✔
155
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
156
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
348,270,889✔
157
  if (*pTaskInfo == NULL || code != 0) {
348,204,161✔
158
    nodesDestroyNode((SNode*)pPlan);
2,567✔
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
348,213,155✔
163

164
  if (pHandle) {
348,253,946✔
165
    if (pHandle->pStateBackend) {
348,240,907✔
166
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
167
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
168
    }
169
  }
170

171
  if (NULL != sql) {
348,236,692✔
172
    (*pTaskInfo)->sql = taosStrdup(sql);
325,172,383✔
173
    if (NULL == (*pTaskInfo)->sql) {
325,210,404✔
174
      code = terrno;
×
175
      doDestroyTask(*pTaskInfo);
×
176
      (*pTaskInfo) = NULL;
×
177
      return code;
×
178
    }
179
  }
180

181
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
348,265,903✔
182
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
348,251,999✔
183

184
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
348,228,980✔
185
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
37,916,030✔
186
    if (code != TSDB_CODE_SUCCESS) {
37,910,129✔
187
      doDestroyTask(*pTaskInfo);
×
188
      (*pTaskInfo) = NULL;
×
189
      return code;
×
190
    }
191
  }
192
  
193
  setTaskScalarExtraInfo(*pTaskInfo);
348,195,836✔
194
  
195
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
348,177,076✔
196
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
348,102,368✔
197

198
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
348,000,294✔
199
    doDestroyTask(*pTaskInfo);
487,574✔
200
    (*pTaskInfo) = NULL;
462,020✔
201
  }
202
  return code;
348,063,321✔
203
}
204

205
void cleanupQueriedTableScanInfo(void* p) {
261,793,518✔
206
  SSchemaInfo* pSchemaInfo = p;
261,793,518✔
207

208
  taosMemoryFreeClear(pSchemaInfo->dbname);
261,793,518✔
209
  taosMemoryFreeClear(pSchemaInfo->tablename);
261,764,809✔
210
  tDeleteSchemaWrapper(pSchemaInfo->sw);
261,686,377✔
211
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
261,688,016✔
212
}
261,699,645✔
213

214
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
261,846,912✔
215
                                   SExecTaskInfo* pTaskInfo) {
216
  SMetaReader mr = {0};
261,846,912✔
217
  if (pHandle == NULL) {
261,854,838✔
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220

221
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
261,854,838✔
222

223
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
261,849,078✔
224
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
261,854,131✔
225
  if (code != TSDB_CODE_SUCCESS) {
261,619,149✔
226
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
226✔
227
           GET_TASKID(pTaskInfo));
228

229
    pAPI->metaReaderFn.clearReader(&mr);
226✔
230
    return code;
226✔
231
  }
232

233
  SSchemaInfo schemaInfo = {0};
261,618,923✔
234

235
  schemaInfo.tablename = taosStrdup(mr.me.name);
261,607,950✔
236
  schemaInfo.dbname = taosStrdup(dbName);
261,628,472✔
237
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
261,716,751✔
238
    pAPI->metaReaderFn.clearReader(&mr);
145,865✔
239
    cleanupQueriedTableScanInfo(&schemaInfo);
×
240
    return terrno;
×
241
  }
242

243
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
261,570,886✔
244
    schemaInfo.rversion = mr.me.colRef.version;
465,946✔
245
  }
246

247
  if (mr.me.type == TSDB_SUPER_TABLE) {
261,570,886✔
248
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
115,466,084✔
249
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
115,466,084✔
250
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
146,120,161✔
251
    tDecoderClear(&mr.coder);
99,893,547✔
252

253
    tb_uid_t suid = mr.me.ctbEntry.suid;
99,938,932✔
254
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
99,938,932✔
255
    if (code != TSDB_CODE_SUCCESS) {
99,937,429✔
256
      pAPI->metaReaderFn.clearReader(&mr);
×
257
      cleanupQueriedTableScanInfo(&schemaInfo);
×
258
      return code;
×
259
    }
260

261
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
99,936,991✔
262
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
99,936,991✔
263
  } else {
264
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
46,257,093✔
265
  }
266

267
  pAPI->metaReaderFn.clearReader(&mr);
261,660,168✔
268

269
  if (schemaInfo.sw == NULL) {
261,504,329✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
271
    return terrno;
×
272
  }
273

274
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
261,504,329✔
275
  if (schemaInfo.qsw == NULL) {
261,643,842✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
277
    return terrno;
×
278
  }
279

280
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
261,643,842✔
281
  if (p == NULL) {
261,690,966✔
282
    cleanupQueriedTableScanInfo(&schemaInfo);
×
283
    return terrno;
×
284
  }
285

286
  return code;
261,690,966✔
287
}
288

289
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
261,552,971✔
290
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
261,552,971✔
291
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
261,716,233✔
292

293
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
261,642,595✔
294
  if (pqSw == NULL) {
261,532,191✔
295
    return NULL;
×
296
  }
297

298
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
261,532,191✔
299
  if (pqSw->pSchema == NULL) {
261,318,154✔
300
    taosMemoryFree(pqSw);
×
301
    return NULL;
×
302
  }
303

304
  for (int32_t i = 0; i < numOfCols; ++i) {
1,175,901,971✔
305
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
914,159,730✔
306
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
914,142,140✔
307

308
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
914,151,023✔
309
    pSchema->colId = pColNode->colId;
914,344,580✔
310
    pSchema->type = pColNode->node.resType.type;
914,413,668✔
311
    pSchema->bytes = pColNode->node.resType.bytes;
914,396,932✔
312
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
914,151,435✔
313
  }
314

315
  // this the tags and pseudo function columns, we only keep the tag columns
316
  for (int32_t i = 0; i < numOfTags; ++i) {
428,259,312✔
317
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
166,570,205✔
318

319
    int32_t type = nodeType(pNode->pExpr);
166,567,124✔
320
    if (type == QUERY_NODE_COLUMN) {
166,589,657✔
321
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
125,703,508✔
322

323
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
125,693,269✔
324
      pSchema->colId = pColNode->colId;
125,664,642✔
325
      pSchema->type = pColNode->node.resType.type;
125,715,537✔
326
      pSchema->bytes = pColNode->node.resType.bytes;
125,672,672✔
327
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
125,698,506✔
328
    }
329
  }
330

331
  return pqSw;
261,689,107✔
332
}
333

334
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
348,322,154✔
335
  tDeleteSchemaWrapper(pStreamInfo->schema);
348,322,154✔
336
  tOffsetDestroy(&pStreamInfo->currentOffset);
348,345,549✔
337
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
348,218,887✔
338
  taosMemoryFree(pStreamInfo->stbFullName);
348,338,681✔
339
}
348,296,655✔
340

341
static void freeBlock(void* pParam) {
636,761,226✔
342
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
636,761,226✔
343
  blockDataDestroy(pBlock);
636,776,245✔
344
}
636,746,927✔
345

346

347
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
37,912,281✔
348
  if (pCtx->transporterId > 0) {
37,912,281✔
349
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
2,152✔
350
    if (ret != 0) {
2,152✔
351
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
352
    }
353
    pCtx->transporterId = -1;
2,152✔
354
  }
355

356
  if (pCtx->subEndPoints != NULL) {
37,916,551✔
357
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
37,917,072✔
358
    if (size > 0) {
37,917,089✔
359
      int32_t code = tsem_destroy(&pCtx->ready);
37,917,610✔
360
      if (code != TSDB_CODE_SUCCESS) {
37,916,568✔
361
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
362
      }
363
      taosArrayDestroy(pCtx->subResNodes);
37,916,568✔
364
    }
365
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
37,912,315✔
366
    pCtx->subEndPoints = NULL;
37,910,701✔
367
  }
368
  
369
  taosMemoryFreeClear(pCtx);  
37,909,642✔
370
}
37,910,701✔
371

372
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
348,404,649✔
373
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
348,404,649✔
374
  destroyOperator(pTaskInfo->pRoot);
348,404,261✔
375
  pTaskInfo->pRoot = NULL;
348,276,531✔
376

377
  if (pTaskInfo->pSubJobCtx) {
348,316,159✔
378
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
37,917,644✔
379
    if (code != TSDB_CODE_SUCCESS) {
37,917,593✔
380
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
381
    }
382
  }
383

384
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
348,334,448✔
385
  cleanupStreamInfo(&pTaskInfo->streamInfo);
348,298,789✔
386

387
  if (!pTaskInfo->localFetch.localExec) {
348,315,702✔
388
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
348,306,436✔
389
    pTaskInfo->pSubplan = NULL;
348,331,861✔
390
  }
391

392
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
348,355,946✔
393
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
348,310,469✔
394
  if (!pTaskInfo->paramSet) {
348,336,519✔
395
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
340,488,846✔
396
    pTaskInfo->pOpParam = NULL;
340,440,053✔
397
  }
398
  taosMemoryFreeClear(pTaskInfo->sql);
348,330,207✔
399
  taosMemoryFreeClear(pTaskInfo->id.str);
348,332,429✔
400
  taosMemoryFreeClear(pTaskInfo);
348,301,568✔
401
}
348,263,335✔
402

403
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
375,740,782✔
404
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
375,740,782✔
405
  if (ret < 0) {
375,740,782✔
406
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
407
  }
408
}
375,740,782✔
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