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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

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

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
8,368,355✔
45
  if (p == NULL) {
8,367,968!
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
8,367,968✔
50
  p->cost.created = taosGetTimestampUs();
8,371,453✔
51

52
  p->execModel = model;
8,371,453✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
8,371,453✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
8,375,272✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
8,367,992!
56
    doDestroyTask(p);
×
57
    return terrno;
×
58
  }
59

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

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

72
  buildTaskId(taskId, queryId, p->id.str);
8,373,562✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
8,372,949✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
8,371,377!
75
    doDestroyTask(p);
×
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
8,371,834✔
80
  return TSDB_CODE_SUCCESS;
8,371,834✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
54,547,412✔
84

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

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
25,902,383✔
91
  if (status == TASK_NOT_COMPLETED) {
25,902,383✔
92
    pTaskInfo->status = status;
8,370,150✔
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,532,233✔
96
    pTaskInfo->status |= status;
17,532,233✔
97
  }
98
}
25,902,383✔
99

100
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
8,368,016✔
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,368,016✔
103
  if (*pTaskInfo == NULL || code != 0) {
8,371,760!
104
    nodesDestroyNode((SNode*)pPlan);
×
105
    taosMemoryFree(sql);
×
106
    return code;
×
107
  }
108

109
  if (pHandle) {
8,372,269✔
110
    if (pHandle->pStateBackend) {
8,370,514✔
111
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
7,144✔
112
    }
113
  }
114

115
  TSWAP((*pTaskInfo)->sql, sql);
8,372,269✔
116

117
  (*pTaskInfo)->pSubplan = pPlan;
8,372,269✔
118
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
8,372,269✔
119
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
8,372,269✔
120
                        pPlan->dbFName, &((*pTaskInfo)->pRoot));
8,372,269✔
121

122
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
8,363,606!
123
    doDestroyTask(*pTaskInfo);
×
124
    (*pTaskInfo) = NULL;
146✔
125
  }
126
  return code;
8,371,255✔
127
}
128

129
void cleanupQueriedTableScanInfo(void* p) {
5,006,058✔
130
  SSchemaInfo* pSchemaInfo = p;
5,006,058✔
131

132
  taosMemoryFreeClear(pSchemaInfo->dbname);
5,006,058!
133
  taosMemoryFreeClear(pSchemaInfo->tablename);
5,006,090!
134
  tDeleteSchemaWrapper(pSchemaInfo->sw);
5,006,094!
135
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
5,006,251✔
136
}
5,006,261✔
137

138
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
5,003,218✔
139
                                   SExecTaskInfo* pTaskInfo) {
140
  SMetaReader mr = {0};
5,003,218✔
141
  if (pHandle == NULL) {
5,003,218!
142
    return TSDB_CODE_INVALID_PARA;
×
143
  }
144

145
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
5,003,218✔
146

147
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
5,003,218✔
148
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
5,005,687✔
149
  if (code != TSDB_CODE_SUCCESS) {
4,999,519✔
150
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
120!
151
           GET_TASKID(pTaskInfo));
152

153
    pAPI->metaReaderFn.clearReader(&mr);
120✔
154
    return code;
120✔
155
  }
156

157
  SSchemaInfo schemaInfo = {0};
4,999,399✔
158

159
  schemaInfo.tablename = taosStrdup(mr.me.name);
4,999,399✔
160
  schemaInfo.dbname = taosStrdup(dbName);
4,995,471✔
161
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
5,003,155!
162
    pAPI->metaReaderFn.clearReader(&mr);
×
163
    cleanupQueriedTableScanInfo(&schemaInfo);
×
164
    return terrno;
×
165
  }
166

167
  if (mr.me.type == TSDB_SUPER_TABLE) {
5,003,377✔
168
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4,237,630✔
169
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
4,237,630✔
170
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
773,522✔
171
    tDecoderClear(&mr.coder);
468,648✔
172

173
    tb_uid_t suid = mr.me.ctbEntry.suid;
468,657✔
174
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
468,657✔
175
    if (code != TSDB_CODE_SUCCESS) {
468,657!
176
      pAPI->metaReaderFn.clearReader(&mr);
×
177
      cleanupQueriedTableScanInfo(&schemaInfo);
×
178
      return code;
×
179
    }
180

181
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
468,668✔
182
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
468,668✔
183
  } else {
184
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
298,124✔
185
  }
186

187
  pAPI->metaReaderFn.clearReader(&mr);
5,004,422✔
188

189
  if (schemaInfo.sw == NULL) {
5,003,151!
190
    cleanupQueriedTableScanInfo(&schemaInfo);
×
191
    return terrno;
×
192
  }
193

194
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
5,003,151✔
195
  if (schemaInfo.qsw == NULL) {
5,002,881!
196
    cleanupQueriedTableScanInfo(&schemaInfo);
×
197
    return terrno;
×
198
  }
199

200
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
5,002,881✔
201
  if (p == NULL) {
4,999,319!
202
    cleanupQueriedTableScanInfo(&schemaInfo);
×
203
    return terrno;
×
204
  }
205

206
  return code;
4,999,319✔
207
}
208

209
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
4,998,740✔
210
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
4,998,740!
211
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
4,998,740✔
212

213
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
4,998,740✔
214
  if (pqSw == NULL) {
5,005,294!
215
    return NULL;
×
216
  }
217

218
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
5,005,294✔
219
  if (pqSw->pSchema == NULL) {
5,000,181!
220
    taosMemoryFree(pqSw);
×
221
    return NULL;
×
222
  }
223

224
  for (int32_t i = 0; i < numOfCols; ++i) {
31,075,837✔
225
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
26,102,581✔
226
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
26,075,023✔
227

228
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
26,075,023✔
229
    pSchema->colId = pColNode->colId;
26,075,023✔
230
    pSchema->type = pColNode->node.resType.type;
26,075,023✔
231
    pSchema->bytes = pColNode->node.resType.bytes;
26,075,023✔
232
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
26,075,023✔
233
  }
234

235
  // this the tags and pseudo function columns, we only keep the tag columns
236
  for (int32_t i = 0; i < numOfTags; ++i) {
12,049,025✔
237
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
7,085,018✔
238

239
    int32_t type = nodeType(pNode->pExpr);
7,075,769✔
240
    if (type == QUERY_NODE_COLUMN) {
7,075,769✔
241
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
6,328,210✔
242

243
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
6,328,210✔
244
      pSchema->colId = pColNode->colId;
6,328,210✔
245
      pSchema->type = pColNode->node.resType.type;
6,328,210✔
246
      pSchema->bytes = pColNode->node.resType.bytes;
6,328,210✔
247
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
6,328,210✔
248
    }
249
  }
250

251
  return pqSw;
4,964,007✔
252
}
253

254
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
8,375,340✔
255
  tDeleteSchemaWrapper(pStreamInfo->schema);
8,375,340✔
256
  tOffsetDestroy(&pStreamInfo->currentOffset);
8,375,544✔
257
}
8,374,942✔
258

259
static void freeBlock(void* pParam) {
9,987,394✔
260
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
9,987,394✔
261
  blockDataDestroy(pBlock);
9,987,394✔
262
}
9,988,063✔
263

264
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
8,372,752✔
265
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
8,372,752✔
266
  destroyOperator(pTaskInfo->pRoot);
8,372,752✔
267
  pTaskInfo->pRoot = NULL;
8,375,462✔
268

269
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
8,375,462✔
270
  cleanupStreamInfo(&pTaskInfo->streamInfo);
8,375,450✔
271

272
  if (!pTaskInfo->localFetch.localExec) {
8,374,826✔
273
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
8,302,692✔
274
    pTaskInfo->pSubplan = NULL;
8,303,324✔
275
  }
276

277
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
8,375,458✔
278
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
8,375,662✔
279
  taosMemoryFreeClear(pTaskInfo->sql);
8,375,718✔
280
  taosMemoryFreeClear(pTaskInfo->id.str);
8,375,813✔
281
  taosMemoryFreeClear(pTaskInfo);
8,375,951✔
282
}
8,375,937✔
283

284
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
8,444,664✔
285
  char* p = dst;
8,444,664✔
286

287
  int32_t offset = 6;
8,444,664✔
288
  memcpy(p, "TID:0x", offset);
8,444,664✔
289
  offset += tintToHex(taskId, &p[offset]);
8,444,664✔
290

291
  memcpy(&p[offset], " QID:0x", 7);
8,447,224✔
292
  offset += 7;
8,447,224✔
293
  offset += tintToHex(queryId, &p[offset]);
8,447,224✔
294

295
  p[offset] = 0;
8,446,656✔
296
}
8,446,656✔
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

© 2025 Coveralls, Inc