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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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

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

UNCOV
44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
×
UNCOV
45
  if (p == NULL) {
×
46
    return terrno;
×
47
  }
48

UNCOV
49
  setTaskStatus(p, TASK_NOT_COMPLETED);
×
UNCOV
50
  p->cost.created = taosGetTimestampUs();
×
51

UNCOV
52
  p->execModel = model;
×
UNCOV
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
×
UNCOV
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
×
UNCOV
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
×
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

UNCOV
60
  p->storageAPI = *pAPI;
×
UNCOV
61
  taosInitRWLatch(&p->lock);
×
62

UNCOV
63
  p->id.vgId = vgId;
×
UNCOV
64
  p->id.queryId = queryId;
×
UNCOV
65
  p->id.taskId = taskId;
×
UNCOV
66
  p->id.str = taosMemoryMalloc(64);
×
UNCOV
67
  if (p->id.str == NULL) {
×
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

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

UNCOV
79
  *pTaskInfo = p;
×
UNCOV
80
  return TSDB_CODE_SUCCESS;
×
81
}
82

UNCOV
83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
×
84

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

UNCOV
90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
×
UNCOV
91
  if (status == TASK_NOT_COMPLETED) {
×
UNCOV
92
    pTaskInfo->status = status;
×
93
  } else {
94
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
UNCOV
95
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
×
UNCOV
96
    pTaskInfo->status |= status;
×
97
  }
UNCOV
98
}
×
99

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

UNCOV
108
  if (pHandle) {
×
UNCOV
109
    if (pHandle->pStateBackend) {
×
UNCOV
110
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
×
111
    }
112
  }
113

UNCOV
114
  if (NULL != sql) {
×
UNCOV
115
    (*pTaskInfo)->sql = taosStrdup(sql);
×
UNCOV
116
    if (NULL == (*pTaskInfo)->sql) {
×
117
      code = terrno;
×
118
      nodesDestroyNode((SNode*)pPlan);
×
119
      doDestroyTask(*pTaskInfo);
×
120
      (*pTaskInfo) = NULL;
×
121
      return code;
×
122
    }
123
  }
124

UNCOV
125
  (*pTaskInfo)->pSubplan = pPlan;
×
UNCOV
126
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
×
UNCOV
127
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
×
UNCOV
128
                        pPlan->dbFName, &((*pTaskInfo)->pRoot));
×
129

UNCOV
130
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
×
131
    doDestroyTask(*pTaskInfo);
×
UNCOV
132
    (*pTaskInfo) = NULL;
×
133
  }
UNCOV
134
  return code;
×
135
}
136

UNCOV
137
void cleanupQueriedTableScanInfo(void* p) {
×
UNCOV
138
  SSchemaInfo* pSchemaInfo = p;
×
139

UNCOV
140
  taosMemoryFreeClear(pSchemaInfo->dbname);
×
UNCOV
141
  taosMemoryFreeClear(pSchemaInfo->tablename);
×
UNCOV
142
  tDeleteSchemaWrapper(pSchemaInfo->sw);
×
UNCOV
143
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
×
UNCOV
144
}
×
145

UNCOV
146
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
×
147
                                   SExecTaskInfo* pTaskInfo) {
UNCOV
148
  SMetaReader mr = {0};
×
UNCOV
149
  if (pHandle == NULL) {
×
150
    return TSDB_CODE_INVALID_PARA;
×
151
  }
152

UNCOV
153
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
×
154

UNCOV
155
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
×
UNCOV
156
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
×
UNCOV
157
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
158
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
159
           GET_TASKID(pTaskInfo));
160

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

UNCOV
165
  SSchemaInfo schemaInfo = {0};
×
166

UNCOV
167
  schemaInfo.tablename = taosStrdup(mr.me.name);
×
UNCOV
168
  schemaInfo.dbname = taosStrdup(dbName);
×
UNCOV
169
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
×
UNCOV
170
    pAPI->metaReaderFn.clearReader(&mr);
×
171
    cleanupQueriedTableScanInfo(&schemaInfo);
×
172
    return terrno;
×
173
  }
174

UNCOV
175
  if (mr.me.type == TSDB_SUPER_TABLE) {
×
UNCOV
176
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
×
UNCOV
177
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
×
UNCOV
178
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
×
UNCOV
179
    tDecoderClear(&mr.coder);
×
180

UNCOV
181
    tb_uid_t suid = mr.me.ctbEntry.suid;
×
UNCOV
182
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
×
UNCOV
183
    if (code != TSDB_CODE_SUCCESS) {
×
184
      pAPI->metaReaderFn.clearReader(&mr);
×
185
      cleanupQueriedTableScanInfo(&schemaInfo);
×
186
      return code;
×
187
    }
188

UNCOV
189
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
×
UNCOV
190
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
×
191
  } else {
UNCOV
192
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
×
193
  }
194

UNCOV
195
  pAPI->metaReaderFn.clearReader(&mr);
×
196

UNCOV
197
  if (schemaInfo.sw == NULL) {
×
198
    cleanupQueriedTableScanInfo(&schemaInfo);
×
199
    return terrno;
×
200
  }
201

UNCOV
202
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
×
UNCOV
203
  if (schemaInfo.qsw == NULL) {
×
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

UNCOV
208
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
×
UNCOV
209
  if (p == NULL) {
×
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

UNCOV
214
  return code;
×
215
}
216

UNCOV
217
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
×
UNCOV
218
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
×
UNCOV
219
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
×
220

UNCOV
221
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
×
UNCOV
222
  if (pqSw == NULL) {
×
223
    return NULL;
×
224
  }
225

UNCOV
226
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
×
UNCOV
227
  if (pqSw->pSchema == NULL) {
×
228
    taosMemoryFree(pqSw);
×
229
    return NULL;
×
230
  }
231

UNCOV
232
  for (int32_t i = 0; i < numOfCols; ++i) {
×
UNCOV
233
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
×
UNCOV
234
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
×
235

UNCOV
236
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
×
UNCOV
237
    pSchema->colId = pColNode->colId;
×
UNCOV
238
    pSchema->type = pColNode->node.resType.type;
×
UNCOV
239
    pSchema->bytes = pColNode->node.resType.bytes;
×
UNCOV
240
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
×
241
  }
242

243
  // this the tags and pseudo function columns, we only keep the tag columns
UNCOV
244
  for (int32_t i = 0; i < numOfTags; ++i) {
×
UNCOV
245
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
×
246

UNCOV
247
    int32_t type = nodeType(pNode->pExpr);
×
UNCOV
248
    if (type == QUERY_NODE_COLUMN) {
×
UNCOV
249
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
×
250

UNCOV
251
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
×
UNCOV
252
      pSchema->colId = pColNode->colId;
×
UNCOV
253
      pSchema->type = pColNode->node.resType.type;
×
UNCOV
254
      pSchema->bytes = pColNode->node.resType.bytes;
×
UNCOV
255
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
×
256
    }
257
  }
258

UNCOV
259
  return pqSw;
×
260
}
261

UNCOV
262
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
×
UNCOV
263
  tDeleteSchemaWrapper(pStreamInfo->schema);
×
UNCOV
264
  tOffsetDestroy(&pStreamInfo->currentOffset);
×
UNCOV
265
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
×
UNCOV
266
  taosMemoryFree(pStreamInfo->stbFullName);
×
UNCOV
267
}
×
268

UNCOV
269
static void freeBlock(void* pParam) {
×
UNCOV
270
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
×
UNCOV
271
  blockDataDestroy(pBlock);
×
UNCOV
272
}
×
273

UNCOV
274
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
×
UNCOV
275
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
×
UNCOV
276
  destroyOperator(pTaskInfo->pRoot);
×
UNCOV
277
  pTaskInfo->pRoot = NULL;
×
278

UNCOV
279
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
×
UNCOV
280
  cleanupStreamInfo(&pTaskInfo->streamInfo);
×
281

UNCOV
282
  if (!pTaskInfo->localFetch.localExec) {
×
UNCOV
283
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
×
UNCOV
284
    pTaskInfo->pSubplan = NULL;
×
285
  }
286

UNCOV
287
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
×
UNCOV
288
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
×
UNCOV
289
  taosMemoryFreeClear(pTaskInfo->sql);
×
UNCOV
290
  taosMemoryFreeClear(pTaskInfo->id.str);
×
UNCOV
291
  taosMemoryFreeClear(pTaskInfo);
×
UNCOV
292
}
×
293

UNCOV
294
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
×
UNCOV
295
  char* p = dst;
×
296

UNCOV
297
  int32_t offset = 6;
×
UNCOV
298
  memcpy(p, "TID:0x", offset);
×
UNCOV
299
  offset += tintToHex(taskId, &p[offset]);
×
300

UNCOV
301
  memcpy(&p[offset], " QID:0x", 7);
×
UNCOV
302
  offset += 7;
×
UNCOV
303
  offset += tintToHex(queryId, &p[offset]);
×
304

UNCOV
305
  p[offset] = 0;
×
UNCOV
306
}
×
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