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

taosdata / TDengine / #4978

06 Mar 2026 09:48AM UTC coverage: 68.439% (-0.02%) from 68.456%
#4978

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%)

3130 existing lines in 120 files now uncovered.

211124 of 308486 relevant lines covered (68.44%)

136029500.43 hits per line

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

81.33
/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,
342,625,596✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
342,625,596✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
342,625,596✔
46
  if (p == NULL) {
342,574,485✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
342,574,485✔
51
  p->cost.created = taosGetTimestampUs();
342,641,129✔
52

53
  p->execModel = model;
342,637,519✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
342,637,230✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
342,609,111✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
342,601,740✔
57
    doDestroyTask(p);
605✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
342,624,597✔
62
  taosInitRWLatch(&p->lock);
342,638,326✔
63

64
  p->id.vgId = vgId;
342,612,533✔
65
  p->id.queryId = queryId;
342,605,344✔
66
  p->id.taskId = taskId;
342,623,390✔
67
  p->id.str = taosMemoryMalloc(64);
342,630,406✔
68
  if (p->id.str == NULL) {
342,569,599✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
342,603,036✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
342,582,652✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
342,529,977✔
76
    doDestroyTask(p);
15,170✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
342,553,325✔
81
  return TSDB_CODE_SUCCESS;
342,590,719✔
82
}
83

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

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,849,050,807✔
87

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

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,082,723,144✔
94
  if (status == TASK_NOT_COMPLETED) {
1,082,723,144✔
95
    pTaskInfo->status = status;
342,754,045✔
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);
739,969,099✔
99
    pTaskInfo->status |= status;
739,978,578✔
100
  }
101
}
1,082,775,818✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
37,837,478✔
105
  int32_t code = 0, lino = 0;
37,837,478✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
37,837,478✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
37,835,370✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
37,834,322✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
37,835,374✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
37,835,382✔
114
  ctx->taskId = pTaskInfo->id.taskId;
37,841,156✔
115
  ctx->idStr = pTaskInfo->id.str;
37,834,860✔
116
  ctx->pTaskInfo = pTaskInfo;
37,839,582✔
117
  ctx->subEndPoints = *subEndPoints;
37,835,896✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
37,835,378✔
119

120
  *subEndPoints = NULL;
37,842,196✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
37,842,196✔
123
  if (NULL == ctx->subResNodes) {
37,836,956✔
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,835,378✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
37,831,704✔
131
  if (refId < 0) {
37,838,004✔
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,838,004✔
137

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

140
_exit:
37,642,898✔
141

142
  if (code) {
37,832,230✔
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,833,274✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
342,500,743✔
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);
342,500,743✔
157
  if (*pTaskInfo == NULL || code != 0) {
342,444,317✔
158
    nodesDestroyNode((SNode*)pPlan);
36✔
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
342,455,172✔
163

164
  if (pHandle) {
342,460,624✔
165
    if (pHandle->pStateBackend) {
342,453,856✔
166
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
167
      (*pTaskInfo)->streamInfo.pOtherState = pHandle->pOtherBackend;
×
168
    }
169
  }
170

171
  if (NULL != sql) {
342,466,335✔
172
    (*pTaskInfo)->sql = taosStrdup(sql);
319,387,787✔
173
    if (NULL == (*pTaskInfo)->sql) {
319,412,348✔
174
      code = terrno;
×
175
      doDestroyTask(*pTaskInfo);
×
176
      (*pTaskInfo) = NULL;
×
177
      return code;
×
178
    }
179
  }
180

181
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
342,469,359✔
182
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
342,482,521✔
183

184
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
342,465,827✔
185
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
37,840,630✔
186
    if (code != TSDB_CODE_SUCCESS) {
37,833,270✔
187
      doDestroyTask(*pTaskInfo);
×
188
      (*pTaskInfo) = NULL;
×
189
      return code;
×
190
    }
191
  }
192
  
193
  setTaskScalarExtraInfo(*pTaskInfo);
342,449,474✔
194
  
195
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
342,430,667✔
196
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
342,368,773✔
197

198
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
342,293,108✔
199
    doDestroyTask(*pTaskInfo);
463,039✔
200
    (*pTaskInfo) = NULL;
450,264✔
201
  }
202
  return code;
342,342,077✔
203
}
204

205
void cleanupQueriedTableScanInfo(void* p) {
259,739,143✔
206
  SSchemaInfo* pSchemaInfo = p;
259,739,143✔
207

208
  taosMemoryFreeClear(pSchemaInfo->dbname);
259,739,143✔
209
  taosMemoryFreeClear(pSchemaInfo->tablename);
259,715,160✔
210
  tDeleteSchemaWrapper(pSchemaInfo->sw);
259,654,115✔
211
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
259,663,275✔
212
}
259,661,173✔
213

214
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
259,776,710✔
215
                                   SExecTaskInfo* pTaskInfo) {
216
  SMetaReader mr = {0};
259,776,710✔
217
  if (pHandle == NULL) {
259,783,168✔
218
    return TSDB_CODE_INVALID_PARA;
×
219
  }
220

221
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
259,783,168✔
222

223
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
259,779,808✔
224
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
259,788,132✔
225
  if (code != TSDB_CODE_SUCCESS) {
259,626,646✔
UNCOV
226
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
227
           GET_TASKID(pTaskInfo));
228

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

233
  SSchemaInfo schemaInfo = {0};
259,626,646✔
234

235
  schemaInfo.tablename = taosStrdup(mr.me.name);
259,606,797✔
236
  schemaInfo.dbname = taosStrdup(dbName);
259,602,911✔
237
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
259,648,862✔
238
    pAPI->metaReaderFn.clearReader(&mr);
83,817✔
239
    cleanupQueriedTableScanInfo(&schemaInfo);
×
240
    return terrno;
×
241
  }
242

243
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
259,565,045✔
244
    schemaInfo.rversion = mr.me.colRef.version;
471,850✔
245
  }
246

247
  if (mr.me.type == TSDB_SUPER_TABLE) {
259,565,045✔
248
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
113,504,680✔
249
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
113,504,680✔
250
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
146,113,334✔
251
    tDecoderClear(&mr.coder);
100,033,308✔
252

253
    tb_uid_t suid = mr.me.ctbEntry.suid;
100,033,497✔
254
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
100,033,497✔
255
    if (code != TSDB_CODE_SUCCESS) {
100,034,050✔
256
      pAPI->metaReaderFn.clearReader(&mr);
×
257
      cleanupQueriedTableScanInfo(&schemaInfo);
×
258
      return code;
×
259
    }
260

261
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
100,034,441✔
262
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
100,034,441✔
263
  } else {
264
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
46,107,738✔
265
  }
266

267
  pAPI->metaReaderFn.clearReader(&mr);
259,646,859✔
268

269
  if (schemaInfo.sw == NULL) {
259,496,184✔
270
    cleanupQueriedTableScanInfo(&schemaInfo);
×
271
    return terrno;
×
272
  }
273

274
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
259,496,184✔
275
  if (schemaInfo.qsw == NULL) {
259,646,727✔
276
    cleanupQueriedTableScanInfo(&schemaInfo);
×
277
    return terrno;
×
278
  }
279

280
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
259,646,727✔
281
  if (p == NULL) {
259,676,032✔
282
    cleanupQueriedTableScanInfo(&schemaInfo);
×
283
    return terrno;
×
284
  }
285

286
  return code;
259,676,032✔
287
}
288

289
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
259,557,752✔
290
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
259,557,752✔
291
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
259,669,511✔
292

293
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
259,603,347✔
294
  if (pqSw == NULL) {
259,538,321✔
295
    return NULL;
×
296
  }
297

298
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
259,538,321✔
299
  if (pqSw->pSchema == NULL) {
259,367,977✔
300
    taosMemoryFree(pqSw);
×
301
    return NULL;
×
302
  }
303

304
  for (int32_t i = 0; i < numOfCols; ++i) {
1,179,187,672✔
305
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
919,480,782✔
306
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
919,406,528✔
307

308
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
919,418,647✔
309
    pSchema->colId = pColNode->colId;
919,593,132✔
310
    pSchema->type = pColNode->node.resType.type;
919,652,473✔
311
    pSchema->bytes = pColNode->node.resType.bytes;
919,668,076✔
312
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
919,437,371✔
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) {
444,713,034✔
317
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
185,037,577✔
318

319
    int32_t type = nodeType(pNode->pExpr);
185,037,360✔
320
    if (type == QUERY_NODE_COLUMN) {
185,067,624✔
321
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
142,879,565✔
322

323
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
142,864,665✔
324
      pSchema->colId = pColNode->colId;
142,832,752✔
325
      pSchema->type = pColNode->node.resType.type;
142,885,513✔
326
      pSchema->bytes = pColNode->node.resType.bytes;
142,826,697✔
327
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
142,864,636✔
328
    }
329
  }
330

331
  return pqSw;
259,675,457✔
332
}
333

334
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
342,563,796✔
335
  tDeleteSchemaWrapper(pStreamInfo->schema);
342,563,796✔
336
  tOffsetDestroy(&pStreamInfo->currentOffset);
342,587,860✔
337
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
342,484,578✔
338
  taosMemoryFree(pStreamInfo->stbFullName);
342,583,749✔
339
}
342,537,438✔
340

341
static void freeBlock(void* pParam) {
657,030,133✔
342
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
657,030,133✔
343
  blockDataDestroy(pBlock);
657,041,367✔
344
}
657,033,596✔
345

346

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

356
  if (pCtx->subEndPoints != NULL) {
37,840,622✔
357
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
37,840,622✔
358
    if (size > 0) {
37,842,726✔
359
      int32_t code = tsem_destroy(&pCtx->ready);
37,842,726✔
360
      if (code != TSDB_CODE_SUCCESS) {
37,841,670✔
361
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
362
      }
363
      taosArrayDestroy(pCtx->subResNodes);
37,841,670✔
364
    }
365
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
37,842,200✔
366
    pCtx->subEndPoints = NULL;
37,839,574✔
367
  }
368
  
369
  taosMemoryFreeClear(pCtx);  
37,839,574✔
370
}
37,841,148✔
371

372
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
342,637,373✔
373
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
342,637,373✔
374
  destroyOperator(pTaskInfo->pRoot);
342,632,351✔
375
  pTaskInfo->pRoot = NULL;
342,521,120✔
376

377
  if (pTaskInfo->pSubJobCtx) {
342,548,716✔
378
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
37,840,626✔
379
    if (code != TSDB_CODE_SUCCESS) {
37,841,674✔
380
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
381
    }
382
  }
383

384
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
342,568,985✔
385
  cleanupStreamInfo(&pTaskInfo->streamInfo);
342,524,931✔
386

387
  if (!pTaskInfo->localFetch.localExec) {
342,554,623✔
388
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
342,553,073✔
389
    pTaskInfo->pSubplan = NULL;
342,559,390✔
390
  }
391

392
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
342,571,180✔
393
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
342,550,552✔
394
  if (!pTaskInfo->paramSet) {
342,574,915✔
395
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
335,326,034✔
396
    pTaskInfo->pOpParam = NULL;
335,276,914✔
397
  }
398
  taosMemoryFreeClear(pTaskInfo->sql);
342,548,326✔
399
  taosMemoryFreeClear(pTaskInfo->id.str);
342,577,036✔
400
  taosMemoryFreeClear(pTaskInfo);
342,521,885✔
401
}
342,487,339✔
402

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