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

taosdata / TDengine / #3612

14 Feb 2025 06:43AM UTC coverage: 63.513% (+0.06%) from 63.456%
#3612

push

travis-ci

web-flow
Merge pull request #29757 from taosdata/feat/TS-5486

feat: support interp function with time range and time interval

141465 of 286269 branches covered (49.42%)

Branch coverage included in aggregate %.

220292 of 283307 relevant lines covered (77.76%)

18570887.65 hits per line

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

76.9
/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,
8,259,669✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
8,259,669!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
8,259,669!
45
  if (p == NULL) {
8,259,925!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
8,259,925✔
50
  p->cost.created = taosGetTimestampUs();
8,263,123✔
51

52
  p->execModel = model;
8,263,123✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
8,263,123✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
8,268,984✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
8,261,104!
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
8,262,549✔
61
  taosInitRWLatch(&p->lock);
8,262,549✔
62

63
  p->id.vgId = vgId;
8,269,133✔
64
  p->id.queryId = queryId;
8,269,133✔
65
  p->id.taskId = taskId;
8,269,133✔
66
  p->id.str = taosMemoryMalloc(64);
8,269,133!
67
  if (p->id.str == NULL) {
8,268,548!
68
    doDestroyTask(p);
×
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str);
8,268,548✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
8,269,110✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
8,265,518!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
8,266,617✔
80
  return TSDB_CODE_SUCCESS;
8,266,617✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
47,326,630✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
25,671,214✔
91
  if (status == TASK_NOT_COMPLETED) {
25,671,214✔
92
    pTaskInfo->status = status;
8,261,613✔
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);
17,409,601✔
96
    pTaskInfo->status |= status;
17,409,601✔
97
  }
98
}
25,671,214✔
99

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

108
  if (pHandle) {
8,267,306✔
109
    if (pHandle->pStateBackend) {
8,264,404✔
110
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
7,590✔
111
    }
112
  }
113

114
  if (NULL != sql) {
8,267,306✔
115
    (*pTaskInfo)->sql = taosStrdup(sql);
8,130,644!
116
    if (NULL == (*pTaskInfo)->sql) {
8,120,206!
117
      code = terrno;
×
118
      nodesDestroyNode((SNode*)pPlan);
×
119
      doDestroyTask(*pTaskInfo);
×
120
      (*pTaskInfo) = NULL;
×
121
      return code;
×
122
    }
123
  }
124

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

130
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
8,257,740!
131
    doDestroyTask(*pTaskInfo);
×
132
    (*pTaskInfo) = NULL;
145✔
133
  }
134
  return code;
8,266,048✔
135
}
136

137
void cleanupQueriedTableScanInfo(void* p) {
4,858,599✔
138
  SSchemaInfo* pSchemaInfo = p;
4,858,599✔
139

140
  taosMemoryFreeClear(pSchemaInfo->dbname);
4,858,599!
141
  taosMemoryFreeClear(pSchemaInfo->tablename);
4,858,973!
142
  tDeleteSchemaWrapper(pSchemaInfo->sw);
4,858,986!
143
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
4,859,553✔
144
}
4,859,456✔
145

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

153
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
4,855,087✔
154

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

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

165
  SSchemaInfo schemaInfo = {0};
4,850,785✔
166

167
  schemaInfo.tablename = taosStrdup(mr.me.name);
4,850,785!
168
  schemaInfo.dbname = taosStrdup(dbName);
4,851,227✔
169
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
4,856,059!
170
    pAPI->metaReaderFn.clearReader(&mr);
×
171
    cleanupQueriedTableScanInfo(&schemaInfo);
×
172
    return terrno;
×
173
  }
174

175
  if (mr.me.type == TSDB_SUPER_TABLE) {
4,856,181✔
176
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4,135,699✔
177
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
4,135,699✔
178
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
719,973✔
179
    tDecoderClear(&mr.coder);
434,382✔
180

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

189
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
434,385✔
190
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
434,385✔
191
  } else {
192
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
280,789✔
193
  }
194

195
  pAPI->metaReaderFn.clearReader(&mr);
4,850,873✔
196

197
  if (schemaInfo.sw == NULL) {
4,856,202!
198
    cleanupQueriedTableScanInfo(&schemaInfo);
×
199
    return terrno;
×
200
  }
201

202
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
4,856,202✔
203
  if (schemaInfo.qsw == NULL) {
4,849,902!
204
    cleanupQueriedTableScanInfo(&schemaInfo);
×
205
    return terrno;
×
206
  }
207

208
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
4,849,902✔
209
  if (p == NULL) {
4,851,340!
210
    cleanupQueriedTableScanInfo(&schemaInfo);
×
211
    return terrno;
×
212
  }
213

214
  return code;
4,851,340✔
215
}
216

217
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
4,849,239✔
218
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
4,849,239!
219
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
4,849,239✔
220

221
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
4,849,239!
222
  if (pqSw == NULL) {
4,856,882!
223
    return NULL;
×
224
  }
225

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

232
  for (int32_t i = 0; i < numOfCols; ++i) {
28,107,801✔
233
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
23,279,123✔
234
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
23,256,781✔
235

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

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

247
    int32_t type = nodeType(pNode->pExpr);
6,919,298✔
248
    if (type == QUERY_NODE_COLUMN) {
6,919,298✔
249
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
6,200,587✔
250

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

259
  return pqSw;
4,821,818✔
260
}
261

262
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
8,272,592✔
263
  tDeleteSchemaWrapper(pStreamInfo->schema);
8,272,592✔
264
  tOffsetDestroy(&pStreamInfo->currentOffset);
8,272,480✔
265
  tDeleteSchemaWrapper(pStreamInfo->notifyResultSchema);
8,273,333!
266
  taosMemoryFree(pStreamInfo->stbFullName);
8,272,345✔
267
}
8,272,498✔
268

269
static void freeBlock(void* pParam) {
12,312,832✔
270
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
12,312,832✔
271
  blockDataDestroy(pBlock);
12,312,832✔
272
}
12,313,086✔
273

274
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
8,271,825✔
275
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
8,271,825✔
276
  destroyOperator(pTaskInfo->pRoot);
8,271,825✔
277
  pTaskInfo->pRoot = NULL;
8,273,114✔
278

279
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
8,273,114✔
280
  cleanupStreamInfo(&pTaskInfo->streamInfo);
8,273,586✔
281

282
  if (!pTaskInfo->localFetch.localExec) {
8,272,503✔
283
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
8,201,193✔
284
    pTaskInfo->pSubplan = NULL;
8,202,250✔
285
  }
286

287
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
8,273,560✔
288
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
8,273,402✔
289
  taosMemoryFreeClear(pTaskInfo->sql);
8,273,764!
290
  taosMemoryFreeClear(pTaskInfo->id.str);
8,273,779!
291
  taosMemoryFreeClear(pTaskInfo);
8,273,793!
292
}
8,273,810✔
293

294
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
8,323,113✔
295
  char* p = dst;
8,323,113✔
296

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

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

305
  p[offset] = 0;
8,327,184✔
306
}
8,327,184✔
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