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

taosdata / TDengine / #5059

17 May 2026 01:15AM UTC coverage: 73.443% (+0.06%) from 73.387%
#5059

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281870 of 383795 relevant lines covered (73.44%)

135516561.93 hits per line

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

71.4
/source/libs/executor/src/executor.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 "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "taosdef.h"
32
#include "tarray.h"
33
#include "tdatablock.h"
34
#include "tref.h"
35
#include "trpc.h"
36
#include "ttypes.h"
37
#include "tudf.h"
38
#include "wal.h"
39

40
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
41
int32_t             fetchObjRefPool = -1;
42
SGlobalExecInfo     gExecInfo = {0};
43

44
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
863,761,444✔
45
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
863,761,444✔
46
  gTaskScalarExtra.pSubJobCtx = pTaskInfo->pSubJobCtx;
863,761,444✔
47
  gTaskScalarExtra.fp = qFetchRemoteNode;
863,835,928✔
48
}
863,456,727✔
49

50
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
735,503✔
51
  gExecInfo.dnode = pDnode;
735,503✔
52
  gExecInfo.getMnode = getMnode;
735,503✔
53
  gExecInfo.getDnodeId = getDnodeId;
735,503✔
54
  return;
735,503✔
55
}
56

57
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
71,061✔
58
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
71,061✔
59
    qError("gExecInfo is not initialized");
238✔
60
    return TSDB_CODE_APP_ERROR;
×
61
  }
62
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
70,823✔
63
  return TSDB_CODE_SUCCESS;
71,299✔
64
}
65

66
void doDestroyFetchObj(void* param) {
165,382,874✔
67
  if (param == NULL) {
165,382,874✔
68
    return;
×
69
  }
70

71
  if (*(bool*)param) {
165,382,874✔
72
    doDestroyExchangeOperatorInfo(param);
120,065,869✔
73
  } else {
74
    destroySubJobCtx((STaskSubJobCtx *)param);
45,319,285✔
75
  }
76
}
77

78
static void cleanupRefPool() {
697,814✔
79
  int32_t ref = atomic_val_compare_exchange_32(&fetchObjRefPool, fetchObjRefPool, 0);
697,814✔
80
  taosCloseRef(ref);
697,814✔
81
}
697,814✔
82

83
static void initRefPool() {
697,814✔
84
  fetchObjRefPool = taosOpenRef(1024, doDestroyFetchObj);
697,814✔
85
  (void)atexit(cleanupRefPool);
697,814✔
86
}
697,814✔
87

88
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
×
89
  int32_t code = TSDB_CODE_SUCCESS;
×
90
  int32_t lino = 0;
×
91
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
92
    if (pOperator->numOfDownstream == 0) {
×
93
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
94
      return TSDB_CODE_APP_ERROR;
×
95
    }
96

97
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
98
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
99
      return TSDB_CODE_APP_ERROR;
×
100
    }
101
    pOperator->status = OP_NOT_OPENED;
×
102
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
×
103
  } else {
104
    pOperator->status = OP_NOT_OPENED;
×
105
    return TSDB_CODE_SUCCESS;
×
106
  }
107

108
_end:
109
  if (code != TSDB_CODE_SUCCESS) {
110
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
111
  }
112
  return code;
113
}
114

115
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
116
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
117
    if (pOperator->numOfDownstream == 0) {
×
118
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
119
      return TSDB_CODE_APP_ERROR;
×
120
    }
121

122
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
123
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
124
      return TSDB_CODE_APP_ERROR;
×
125
    }
126

127
    pOperator->status = OP_NOT_OPENED;
×
128
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
129
  }
130
  return 0;
×
131
}
132

133
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
363,781,679✔
134
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
363,781,679✔
135
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
363,812,393✔
136
    STmqQueryScanInfo* pTmqScanInfo = pOperator->info;
178,524,142✔
137
    if (pTmqScanInfo->pTableScanOp != NULL) {
178,528,480✔
138
      STableScanInfo* pScanInfo = pTmqScanInfo->pTableScanOp->info;
178,526,195✔
139
      if (pScanInfo->base.dataReader != NULL) {
178,527,371✔
140
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
322,285✔
141
        if (code) {
322,285✔
142
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
143
          return code;
×
144
        }
145
      }
146
    }
147
  } else {
148
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
185,279,097✔
149
  }
150

151
  return 0;
185,110,776✔
152
}
153

154
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
185,110,940✔
155
  SExecTaskInfo* pTaskInfo = tinfo;
185,110,940✔
156
  pTaskInfo->id.queryId = queryId;
185,110,940✔
157
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
185,117,776✔
158

159
  // set the idstr for tsdbReader
160
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
185,121,189✔
161
}
162

163
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
164
  SExecTaskInfo* pTaskInfo = tinfo;
×
165
  return pTaskInfo->status == OP_EXEC_DONE;
×
166
}
167

168
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
169
  if (tinfo == NULL) {
×
170
    return TSDB_CODE_APP_ERROR;
×
171
  }
172

173
  if (pBlocks == NULL || numOfBlocks == 0) {
×
174
    return TSDB_CODE_SUCCESS;
×
175
  }
176

177
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
178

179
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
180
  if (code != TSDB_CODE_SUCCESS) {
×
181
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
182
  } else {
183
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
184
  }
185

186
  return code;
×
187
}
188

189
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
542,989✔
190
  if (msg == NULL) {  // create raw scan
542,989✔
191
    SExecTaskInfo* pTaskInfo = NULL;
149,204✔
192

193
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
149,204✔
194
    if (NULL == pTaskInfo || code != 0) {
149,204✔
195
      return NULL;
×
196
    }
197

198
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
149,204✔
199
    if (NULL == pTaskInfo->pRoot || code != 0) {
149,204✔
200
      taosMemoryFree(pTaskInfo);
×
201
      return NULL;
×
202
    }
203

204
    pTaskInfo->storageAPI = pReaderHandle->api;
149,204✔
205
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
148,852✔
206
    return pTaskInfo;
149,204✔
207
  }
208

209
  SSubplan* pPlan = NULL;
393,785✔
210
  int32_t   code = qStringToSubplan(msg, &pPlan);
396,145✔
211
  if (code != TSDB_CODE_SUCCESS) {
395,376✔
212
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
213
    terrno = code;
×
214
    return NULL;
×
215
  }
216

217
  qTaskInfo_t pTaskInfo = NULL;
395,376✔
218
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0,
395,376✔
219
                         NULL, OPTR_EXEC_MODEL_QUEUE, NULL, false);
220
  if (code != TSDB_CODE_SUCCESS) {
396,145✔
221
    qDestroyTask(pTaskInfo);
×
222
    terrno = code;
×
223
    return NULL;
×
224
  }
225

226
  return pTaskInfo;
396,145✔
227
}
228

229
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
1,316,037✔
230
  if (streamInserterParam == NULL) {
1,316,037✔
231
    return TSDB_CODE_SUCCESS;
962,178✔
232
  }
233

234
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
353,859✔
235
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
236
    return TSDB_CODE_INVALID_PARA;
×
237
  }
238

239
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
353,859✔
240
    stError("insertParam: invalid db/table name");
×
241
    return TSDB_CODE_INVALID_PARA;
×
242
  }
243

244
  if (streamInserterParam->suid <= 0 &&
353,859✔
245
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
158,231✔
246
    stError("insertParam: invalid table name: %s, suid:%" PRIx64 "", streamInserterParam->tbname ? streamInserterParam->tbname : "(null)", streamInserterParam->suid);
×
247
    return TSDB_CODE_INVALID_PARA;
×
248
  }
249

250
  return TSDB_CODE_SUCCESS;
353,859✔
251
}
252

253
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
1,316,037✔
254
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
255
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
256
  if (pSubplan == NULL || pTaskInfo == NULL) {
1,316,037✔
257
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
×
258
    nodesDestroyNode((SNode *)pSubplan);
×
259
    return TSDB_CODE_INVALID_PARA;
×
260
  }
261
  int32_t lino = 0;
1,316,037✔
262
  int32_t code = checkInsertParam(streamInserterParam);
1,316,037✔
263
  if (code != TSDB_CODE_SUCCESS) {
1,316,037✔
264
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
265
    nodesDestroyNode((SNode *)pSubplan);
×
266
    return code;
×
267
  }
268
  SInserterParam* pInserterParam = NULL;
1,316,037✔
269
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
1,316,037✔
270
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
1,316,037✔
271
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
1,315,872✔
272

273
  int32_t subTaskNum = (int32_t)LIST_LENGTH(pSubplan->pSubQ);
1,315,872✔
274
  SArray* subEndPoints = taosArrayInit(subTaskNum, POINTER_BYTES);
1,316,037✔
275
  SDownstreamSourceNode* pSource = NULL;
1,316,037✔
276
  for (int32_t i = 0; i < subTaskNum; ++i) {
2,477,244✔
277
    SNode* pVal = nodesListGetNode(pSubplan->pSubQ, i);
1,161,207✔
278

279
    TSDB_CHECK_CODE(nodesCloneNode(pVal, (SNode**)&pSource), lino, _error);
1,161,207✔
280

281
    if (NULL == taosArrayPush(subEndPoints, &pSource)) {
2,322,414✔
282
      nodesDestroyNode((SNode *)pSource);
×
283
      code = terrno;
×
284
      goto _error;
×
285
    }
286
  }
287

288
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, &subEndPoints, false);
1,316,037✔
289
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
1,315,772✔
290
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
554✔
291
    goto _error;
554✔
292
  }
293

294
  if (streamInserterParam) {
1,315,218✔
295
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
353,594✔
296
    void*           pSinkManager = NULL;
353,859✔
297
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
353,859✔
298
    if (code != TSDB_CODE_SUCCESS) {
353,594✔
299
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
300
      goto _error;
×
301
    }
302

303
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
353,594✔
304
    if (NULL == pInserterParam) {
353,594✔
305
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
306
      code = terrno;
×
307
      goto _error;
×
308
    }
309
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
353,594✔
310
    TSDB_CHECK_CODE(code, lino, _error);
353,859✔
311
    
312
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
353,859✔
313
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
353,859✔
314

315
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
353,859✔
316
    if (code) {
353,859✔
317
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
318
    }
319
  }
320
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
1,315,483✔
321
         tstrerror(code));
322

323
_error:
1,315,076✔
324
  if (subEndPoints) {
1,316,037✔
325
    taosArrayDestroyP(subEndPoints, (FDelete)nodesDestroyNode);
1,040,776✔
326
  }
327

328
  if (code != TSDB_CODE_SUCCESS) {
1,316,314✔
329
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
554✔
330
    if (pInserterParam != NULL) {
554✔
331
      taosMemoryFree(pInserterParam);
×
332
    }
333
  }
334
  return code;
1,316,037✔
335
}
336

337
bool qNeedReset(qTaskInfo_t pInfo) {
4,546,465✔
338
  if (pInfo == NULL) {
4,546,465✔
339
    return false;
×
340
  }
341
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
4,546,465✔
342
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
4,546,465✔
343
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
4,546,465✔
344
    return false;
×
345
  }
346
  int32_t node = nodeType(pOperator->pPhyNode);
4,546,465✔
347
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
3,848,330✔
348
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
1,092,284✔
349
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
8,394,795✔
350
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
351
}
352

353
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
3,454,181✔
354
  if (pHandle == NULL || pScanBaseInfo == NULL) {
3,454,181✔
355
    return;
×
356
  }
357

358
  pScanBaseInfo->readHandle.uid = pHandle->uid;
3,454,181✔
359
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
3,454,181✔
360
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
3,454,181✔
361
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
3,454,181✔
362
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
3,454,181✔
363
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
3,454,181✔
364
}
365

366
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
4,546,465✔
367
  if (pInfo == NULL) {
4,546,465✔
368
    return TSDB_CODE_INVALID_PARA;
×
369
  }
370
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
4,546,465✔
371
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
4,546,465✔
372

373
  void*           info = pOperator->info;
4,546,465✔
374
  STableScanBase* pScanBaseInfo = NULL;
4,546,465✔
375

376
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
4,546,465✔
377
    pScanBaseInfo = &((STableScanInfo*)info)->base;
698,135✔
378
    setReadHandle(handle, pScanBaseInfo);
698,135✔
379
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
3,848,330✔
380
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,756,046✔
381
    setReadHandle(handle, pScanBaseInfo);
2,756,046✔
382
  }
383

384
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
4,546,465✔
385
  handle->winRange.ekey);
386
  return pOperator->fpSet.resetStateFn(pOperator);
4,546,465✔
387
}
388

389
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
1,316,037✔
390
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
391
  if (msg == NULL) {
1,316,037✔
392
    return TSDB_CODE_INVALID_PARA;
×
393
  }
394

395
  *pTaskInfo = NULL;
1,316,037✔
396

397
  SSubplan* pPlan = NULL;
1,316,037✔
398
  int32_t   code = qStringToSubplan(msg, &pPlan);
1,316,037✔
399
  if (code != TSDB_CODE_SUCCESS) {
1,316,037✔
400
    nodesDestroyNode((SNode *)pPlan);
×
401
    return code;
×
402
  }
403
  // todo: add stream inserter param
404
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
1,316,037✔
405
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
406
                               pInserterParams);
407
  if (code != TSDB_CODE_SUCCESS) {
1,316,037✔
408
    qDestroyTask(*pTaskInfo);
554✔
409
    return code;
554✔
410
  }
411

412
  return code;
1,315,483✔
413
}
414

415
typedef struct {
416
  tb_uid_t tableUid;
417
  tb_uid_t childUid;
418
  int8_t   check;
419
} STqPair;
420

421
static int32_t filterUnqualifiedTables(const STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
511,666✔
422
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
423
  int32_t code = TSDB_CODE_SUCCESS;
511,666✔
424
  int32_t lino = 0;
511,666✔
425
  int8_t  locked = 0;
511,666✔
426
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
511,666✔
427

428
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
511,666✔
429

430
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
511,666✔
431
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
511,666✔
432

433
  int32_t numOfUids = taosArrayGetSize(tableIdList);
511,666✔
434
  if (numOfUids == 0) {
511,666✔
435
    (*ppArrayRes) = qa;
×
436
    goto _error;
×
437
  }
438

439
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
511,666✔
440

441
  uint64_t suid = 0;
511,666✔
442
  uint64_t uid = 0;
511,666✔
443
  int32_t  type = 0;
511,666✔
444
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
511,666✔
445

446
  // let's discard the tables those are not created according to the queried super table.
447
  SMetaReader mr = {0};
511,666✔
448
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
511,666✔
449

450
  locked = 1;
511,666✔
451
  for (int32_t i = 0; i < numOfUids; ++i) {
1,274,459✔
452
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
762,793✔
453
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
762,793✔
454

455
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
762,793✔
456
    if (code != TSDB_CODE_SUCCESS) {
762,793✔
457
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
458
      continue;
×
459
    }
460

461
    tDecoderClear(&mr.coder);
762,793✔
462

463
    if (mr.me.type == TSDB_SUPER_TABLE) {
762,793✔
464
      continue;
×
465
    } else {
466
      if (type == TSDB_SUPER_TABLE) {
762,793✔
467
        // this new created child table does not belong to the scanned super table.
468
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
761,776✔
469
          continue;
18,984✔
470
        }
471
      } else {  // ordinary table
472
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
473
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
474
        if (mr.me.uid != uid) {
1,017✔
475
          continue;
1,017✔
476
        }
477
      }
478
    }
479

480
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
742,792✔
481
    if (pScanInfo->pTagCond != NULL) {
742,792✔
482
      // tb_uid_t id = mr.me.uid;
483
      item.check = 1;
423,837✔
484
    }
485
    if (taosArrayPush(tUid, &item) == NULL) {
742,792✔
486
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
487
    }
488
  }
489

490
  pAPI->metaReaderFn.clearReader(&mr);
511,666✔
491
  locked = 0;
511,666✔
492

493
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
1,254,458✔
494
    bool     qualified = false;
742,792✔
495
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
742,792✔
496
    if (t == NULL) {
742,792✔
497
      continue;
×
498
    }
499

500
    if (t->check == 1) {
742,792✔
501
      code = isQualifiedTable(t->childUid, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
423,837✔
502
      if (code != TSDB_CODE_SUCCESS) {
423,837✔
503
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", t->childUid, idstr);
×
504
        continue;
×
505
      }
506

507
      if (!qualified) {
423,837✔
508
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
212,088✔
509
        continue;
212,088✔
510
      }
511
    }
512

513
    void* tmp = taosArrayPush(qa, &t->tableUid);
530,704✔
514
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
530,704✔
515
  }
516

517
  // handle multiple partition
518

519
_end:
511,666✔
520

521
  if (locked) {
511,666✔
522
    pAPI->metaReaderFn.clearReader(&mr);
×
523
  }
524
  (*ppArrayRes) = qa;
511,666✔
525
_error:
511,666✔
526

527
  taosArrayDestroy(tUid);
511,666✔
528
  if (code != TSDB_CODE_SUCCESS) {
511,666✔
529
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
530
  }
531
  return code;
511,666✔
532
}
533

534
int32_t qDeleteTableListForQuerySub(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
535
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
536
  const char*    id = GET_TASKID(pTaskInfo);
×
537
  int32_t        code = 0;
×
538

539
  // traverse to the stream scanner node to add this table id
540
  SOperatorInfo* pInfo = NULL;
×
541
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
×
542
  if (code != 0 || pInfo == NULL) {
×
543
    return code;
×
544
  }
545

546
  STmqQueryScanInfo* pScanInfo = pInfo->info;
×
547
  qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
×
548
  taosWLockLatch(&pTaskInfo->lock);
×
549
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
550
  taosWUnLockLatch(&pTaskInfo->lock);
×
551

552
  return code;
×
553
}
554

555
static int32_t filterTableForTmqQuery(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* id, SStorageAPI* pAPI, SRWLatch* lock) {
511,666✔
556
  SArray* qa = NULL;
511,666✔
557
  int32_t code = filterUnqualifiedTables(pScanInfo, tableIdList, id, pAPI, &qa);
511,666✔
558
  if (code != TSDB_CODE_SUCCESS) {
511,666✔
559
    taosArrayDestroy(qa);
×
560
    return code;
×
561
  }
562
  int32_t numOfQualifiedTables = taosArrayGetSize(qa);
511,666✔
563
  qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
511,666✔
564
  pAPI->tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
511,666✔
565

566
  bool   assignUid = false;
511,666✔
567
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
511,666✔
568
  char*  keyBuf = NULL;
511,666✔
569
  if (bufLen > 0) {
511,666✔
570
    assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
571
    keyBuf = taosMemoryMalloc(bufLen);
×
572
    if (keyBuf == NULL) {
×
573
      taosArrayDestroy(qa);
×
574
      return terrno;
×
575
    }
576
  }
577

578
  STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
511,666✔
579
  taosWLockLatch(lock);
511,666✔
580

581
  for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
1,042,370✔
582
    uint64_t* uid = taosArrayGet(qa, i);
530,704✔
583
    if (!uid) {
530,704✔
584
      taosMemoryFree(keyBuf);
×
585
      taosArrayDestroy(qa);
×
586
      taosWUnLockLatch(lock);
×
587
      return terrno;
×
588
    }
589
    STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
530,704✔
590

591
    if (bufLen > 0) {
530,704✔
592
      if (assignUid) {
×
593
        keyInfo.groupId = keyInfo.uid;
×
594
      } else {
595
        code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
596
                                      &keyInfo.groupId, pAPI);
597
        if (code != TSDB_CODE_SUCCESS) {
×
598
          taosMemoryFree(keyBuf);
×
599
          taosArrayDestroy(qa);
×
600
          taosWUnLockLatch(lock);
×
601
          return code;
×
602
        }
603
      }
604
    }
605

606
    code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
530,704✔
607
    if (code != TSDB_CODE_SUCCESS) {
530,704✔
608
      taosMemoryFree(keyBuf);
×
609
      taosArrayDestroy(qa);
×
610
      taosWUnLockLatch(lock);
×
611
      return code;
×
612
    }
613
  }
614

615
  taosWUnLockLatch(lock);
511,666✔
616
  if (keyBuf != NULL) {
511,666✔
617
    taosMemoryFree(keyBuf);
×
618
  }
619

620
  taosArrayDestroy(qa);
511,666✔
621
  return 0;
511,666✔
622
}
623

624
static void qUpdateTableTagCache(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, col_id_t cid, SStorageAPI* api) {
513,119✔
625
  STqReader*   tqReader = pScanInfo->tqReader;
513,119✔
626
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
1,277,365✔
627
    int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
764,246✔
628
    api->tqReaderFn.tqUpdateTableTagCache(pScanInfo->tqReader, pScanInfo->pPseudoExpr, pScanInfo->numOfPseudoExpr, *uid, cid);
764,246✔
629
  }
630
}
513,119✔
631

632
int32_t qAddTableListForQuerySub(qTaskInfo_t tinfo, const SArray* tableIdList) {
509,632✔
633
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
509,632✔
634
  const char*    id = GET_TASKID(pTaskInfo);
509,632✔
635
  int32_t        code = 0;
509,632✔
636

637
  qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
509,632✔
638

639
  // traverse to the stream scanner node to add this table id
640
  SOperatorInfo* pInfo = NULL;
509,632✔
641
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
509,632✔
642
  if (code != 0 || pInfo == NULL) {
509,632✔
643
    return code;
×
644
  }
645

646
  STmqQueryScanInfo* pScanInfo = pInfo->info;
509,632✔
647
  qUpdateTableTagCache(pScanInfo, tableIdList, 0, &pTaskInfo->storageAPI);
509,632✔
648

649
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
509,632✔
650
}
651

652
// once of cids and cidListArray is NULL 
653
void qUpdateTableTagCacheForQuerySub(qTaskInfo_t tinfo, const SArray* tableIdList, SArray* cids, SArray* cidListArray) {
3,487✔
654
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,487✔
655
  const char*    id = GET_TASKID(pTaskInfo);
3,487✔
656
  int32_t        code = 0;
3,487✔
657

658
  // traverse to the stream scanner node to add this table id
659
  SOperatorInfo* pInfo = NULL;
3,487✔
660
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
3,487✔
661
  if (code != 0 || pInfo == NULL) {
3,487✔
662
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
663
    return;
×
664
  }
665

666
  STmqQueryScanInfo* pScanInfo = pInfo->info;
3,487✔
667
  for (int32_t i = 0; i < taosArrayGetSize(cids); ++i) {
3,487✔
668
    col_id_t* cid = (col_id_t*)taosArrayGet(cids, i);
×
669
    qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
×
670
  }
671

672
  for (int32_t i = 0; i < taosArrayGetSize(cidListArray); ++i) {
6,974✔
673
    SArray* cidsTmp = (SArray*)taosArrayGetP(cidListArray, i);
3,487✔
674
    for (int32_t j = 0; j < taosArrayGetSize(cidsTmp); ++j) {
6,974✔
675
      col_id_t* cid = (col_id_t*)taosArrayGet(cidsTmp, j);
3,487✔
676
      qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
3,487✔
677
    }
678
  }
679
}
680

681
int32_t qUpdateTableListForQuerySub(qTaskInfo_t tinfo, const SArray* tableIdList) {
2,034✔
682
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,034✔
683
  const char*    id = GET_TASKID(pTaskInfo);
2,034✔
684
  int32_t        code = 0;
2,034✔
685

686
  // traverse to the stream scanner node to add this table id
687
  SOperatorInfo* pInfo = NULL;
2,034✔
688
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
2,034✔
689
  if (code != 0 || pInfo == NULL) {
2,034✔
690
    return code;
×
691
  }
692

693
  STmqQueryScanInfo* pScanInfo = pInfo->info;
2,034✔
694

695
  qDebug("%s %d remove child tables from the stream scanner, %s", __func__, (int32_t)taosArrayGetSize(tableIdList), id);
2,034✔
696
  taosWLockLatch(&pTaskInfo->lock);
2,034✔
697
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
2,034✔
698
  taosWUnLockLatch(&pTaskInfo->lock);
2,034✔
699
  
700
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
2,034✔
701
}
702

703
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
690,447,312✔
704
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
705
                                    int32_t idx, bool* tbGet) {
706
  *tbGet = false;
690,447,312✔
707

708
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
690,479,130✔
709
    return TSDB_CODE_INVALID_PARA;
3,927✔
710
  }
711
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
690,475,295✔
712

713
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
690,475,295✔
714
    return TSDB_CODE_SUCCESS;
397,823,432✔
715
  }
716

717
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
292,623,808✔
718
  if (!pSchemaInfo) {
292,509,664✔
719
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
720
    return terrno;
×
721
  }
722

723
  *sversion = pSchemaInfo->sw->version;
292,509,664✔
724
  *tversion = pSchemaInfo->tversion;
292,684,884✔
725
  *rversion = pSchemaInfo->rversion;
292,648,156✔
726
  if (pSchemaInfo->dbname) {
292,738,111✔
727
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
292,731,210✔
728
  } else {
729
    dbName[0] = 0;
×
730
  }
731
  if (pSchemaInfo->tablename) {
292,832,629✔
732
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
292,770,478✔
733
  } else {
734
    tableName[0] = 0;
7,805✔
735
  }
736

737
  *tbGet = true;
292,836,417✔
738

739
  return TSDB_CODE_SUCCESS;
292,771,959✔
740
}
741

742
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
397,785,090✔
743

744
void qDestroyOperatorParam(SOperatorParam* pParam) {
140,921✔
745
  if (NULL == pParam) {
140,921✔
746
    return;
×
747
  }
748
  freeOperatorParam(pParam, OP_GET_PARAM);
140,921✔
749
}
750

751
/**
752
  @brief Update the operator param for the task.
753
  @note  Unlike setOperatorParam, this function will destroy the new param when
754
         operator type mismatch.
755
*/
756
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
48,512,674✔
757
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
48,512,674✔
758
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
48,512,674✔
759
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
48,512,674✔
760
    qError("%s, %s operator type mismatch, task operator type:%d, "
137,022✔
761
           "new param operator type:%d", GET_TASKID(pTask), __func__,
762
           pTask->pRoot->operatorType,
763
           pNewParam->opType);
764
    qDestroyOperatorParam((SOperatorParam*)pParam);
137,022✔
765
    return;
137,022✔
766
  }
767
  TSWAP(pParam, pTask->pOpParam);
48,377,606✔
768
  ((SExecTaskInfo*)tinfo)->paramSet = false;
48,375,016✔
769
}
770

771
int32_t qExecutorInit(void) {
5,513,479✔
772
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
5,513,479✔
773
  return TSDB_CODE_SUCCESS;
5,513,479✔
774
}
775

776
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
49,755,244✔
777
  int32_t        code = TSDB_CODE_SUCCESS;
49,755,244✔
778
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
49,755,244✔
779
  if (pTask->pWorkerCb) {
49,755,244✔
780
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
49,754,636✔
781
    if (code != TSDB_CODE_SUCCESS) {
49,755,845✔
782
      pTask->code = code;
×
783
      return pTask->code;
×
784
    }
785
  }
786

787
  code = tsem_wait(pSem);
49,757,062✔
788
  if (code != TSDB_CODE_SUCCESS) {
49,756,454✔
789
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
790
    pTask->code = code;
×
791
    return pTask->code;
×
792
  }
793

794
  if (pTask->pWorkerCb) {
49,756,454✔
795
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
49,756,454✔
796
    if (code != TSDB_CODE_SUCCESS) {
49,757,063✔
797
      pTask->code = code;
×
798
      return pTask->code;
×
799
    }
800
  }
801
  return TSDB_CODE_SUCCESS;
49,756,454✔
802
}
803

804
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
400,654,857✔
805
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
806
                        EOPTR_EXEC_MODEL model, SArray** subEndPoints, bool enableExplain) {
807
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
400,654,857✔
808
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
400,654,857✔
809

810
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d, enableExplain:%d", 
400,669,637✔
811
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints ? *subEndPoints : NULL), enableExplain);
812

813
  readHandle->uid = 0;
400,725,214✔
814
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints, enableExplain);
400,770,711✔
815
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
400,459,918✔
816
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
883,176✔
817
    goto _error;
521,206✔
818
  }
819
    
820
  if (handle) {
399,648,927✔
821
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
399,562,179✔
822
    void*           pSinkManager = NULL;
399,641,534✔
823
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
399,067,018✔
824
    if (code != TSDB_CODE_SUCCESS) {
399,121,877✔
825
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
826
      goto _error;
×
827
    }
828

829
    void* pSinkParam = NULL;
399,121,877✔
830
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
399,472,135✔
831
    if (code != TSDB_CODE_SUCCESS) {
399,486,973✔
832
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
833
      taosMemoryFree(pSinkManager);
×
834
      goto _error;
×
835
    }
836

837
    SDataSinkNode* pSink = NULL;
399,486,973✔
838
    if (readHandle->localExec) {
399,544,423✔
839
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
3,649✔
840
      if (code != TSDB_CODE_SUCCESS) {
3,649✔
841
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
842
               (*pTask)->id.str);
843
        taosMemoryFree(pSinkManager);
×
844
        goto _error;
×
845
      }
846
    }
847

848
    // pSinkParam has been freed during create sinker.
849
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
399,205,161✔
850
                              (*pTask)->id.str, pSubplan->processOneBlock);
399,488,926✔
851
    if (code) {
399,115,318✔
852
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
677✔
853
    }
854
  }
855

856
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
399,889,961✔
857
    taskId, pSubplan->id.queryId, tstrerror(code), (*pTask)->pSubJobCtx ? (int32_t)taosArrayGetSize((*pTask)->pSubJobCtx->subEndPoints) : 0);
858

859
_error:
143,725,111✔
860
  // if failed to add ref for all tables in this query, abort current query
861
  return code;
400,669,566✔
862
}
863

864
static void freeBlock(void* param) {
×
865
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
866
  blockDataDestroy(pBlock);
×
867
}
×
868

869
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
470,583,932✔
870
                     bool processOneBlock) {
871
  int32_t        code = TSDB_CODE_SUCCESS;
470,583,932✔
872
  int32_t        lino = 0;
470,583,932✔
873
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
470,583,932✔
874
  int64_t        threadId = taosGetSelfPthreadId();
470,583,932✔
875

876
  if (pLocal) {
470,552,340✔
877
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
461,772,925✔
878
  }
879

880
  taosArrayClear(pResList);
470,455,211✔
881

882
  int64_t curOwner = 0;
470,532,755✔
883
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
470,532,755✔
884
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
885
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
886
    return pTaskInfo->code;
×
887
  }
888

889
  if (pTaskInfo->cost.start == 0) {
470,475,116✔
890
    pTaskInfo->cost.start = taosGetTimestampUs();
390,452,072✔
891
  }
892

893
  if (isTaskKilled(pTaskInfo)) {
470,578,938✔
894
    atomic_store_64(&pTaskInfo->owner, 0);
×
895
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
896
    return pTaskInfo->code;
×
897
  }
898

899
  // error occurs, record the error code and return to client
900
  int32_t ret = setjmp(pTaskInfo->env);
470,494,253✔
901
  if (ret != TSDB_CODE_SUCCESS) {
472,895,560✔
902
    pTaskInfo->code = ret;
2,602,313✔
903
    (void)cleanUpUdfs();
2,602,313✔
904

905
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,602,313✔
906
    atomic_store_64(&pTaskInfo->owner, 0);
2,602,313✔
907

908
    return pTaskInfo->code;
2,602,313✔
909
  }
910

911
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
470,293,247✔
912

913
  int32_t      current = 0;
470,311,393✔
914
  SSDataBlock* pRes = NULL;
470,311,393✔
915
  int64_t      st = taosGetTimestampUs();
470,545,872✔
916

917
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
470,545,872✔
918
    pTaskInfo->paramSet = true;
48,373,291✔
919
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
48,373,332✔
920
  } else {
921
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
422,158,845✔
922
  }
923

924
  QUERY_CHECK_CODE(code, lino, _end);
467,971,953✔
925
  code = blockDataCheck(pRes);
467,971,953✔
926
  QUERY_CHECK_CODE(code, lino, _end);
468,031,802✔
927

928
  if (pRes == NULL) {
468,031,802✔
929
    st = taosGetTimestampUs();
101,042,545✔
930
  }
931

932
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
468,040,336✔
933
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
468,010,371✔
934
    rowsThreshold = 4096;
467,284,488✔
935
  }
936

937
  int32_t blockIndex = 0;
467,982,958✔
938
  while (pRes != NULL) {
1,340,811,598✔
939
    SSDataBlock* p = NULL;
941,086,237✔
940
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
940,981,651✔
941
      SSDataBlock* p1 = NULL;
766,116,815✔
942
      code = createOneDataBlock(pRes, true, &p1);
766,115,174✔
943
      QUERY_CHECK_CODE(code, lino, _end);
766,105,654✔
944

945
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
766,105,654✔
946
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
766,184,579✔
947
      p = p1;
766,184,579✔
948
    } else if (processOneBlock) {
175,140,291✔
949
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
26,533,703✔
950
      if (tmp) {
26,533,703✔
951
        blockDataDestroy(*tmp);
26,533,703✔
952
        *tmp = NULL;
26,533,703✔
953
      }
954
      SSDataBlock* p1 = NULL;
26,533,703✔
955
      code = createOneDataBlock(pRes, true, &p1);
26,533,703✔
956
      QUERY_CHECK_CODE(code, lino, _end);
26,533,703✔
957

958
      *tmp = p1;
26,533,703✔
959
      p = p1;
26,533,703✔
960
    } else {
961
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
148,606,588✔
962
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
148,604,812✔
963

964
      p = *(SSDataBlock**)tmp;
148,604,812✔
965
      code = copyDataBlock(p, pRes);
148,604,812✔
966
      QUERY_CHECK_CODE(code, lino, _end);
148,607,113✔
967
    }
968

969
    blockIndex += 1;
941,323,344✔
970

971
    current += p->info.rows;
941,323,344✔
972
    QUERY_CHECK_CONDITION((p->info.rows > 0), code, lino, _end,
941,326,543✔
973
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
974
    void* tmp = taosArrayPush(pResList, &p);
941,313,124✔
975
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
941,313,124✔
976

977
    if (current >= rowsThreshold || processOneBlock) {
941,313,124✔
978
      break;
979
    }
980

981
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
872,988,666✔
982
    QUERY_CHECK_CODE(code, lino, _end);
872,795,041✔
983
    code = blockDataCheck(pRes);
872,795,041✔
984
    QUERY_CHECK_CODE(code, lino, _end);
873,030,815✔
985
  }
986

987
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
468,049,435✔
988
    pTaskInfo->pSubplan->rowsThreshold -= current;
619,187✔
989
  }
990

991
  *hasMore = (pRes != NULL);
468,062,214✔
992
  uint64_t el = (taosGetTimestampUs() - st);
468,037,257✔
993

994
  pTaskInfo->cost.elapsedTime += el;
468,037,257✔
995
  if (NULL == pRes) {
468,028,696✔
996
    *useconds = pTaskInfo->cost.elapsedTime;
399,703,656✔
997
  }
998

999
_end:
468,266,003✔
1000
  (void)cleanUpUdfs();
468,060,961✔
1001

1002
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
468,097,656✔
1003
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
468,097,074✔
1004
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
1005

1006
  atomic_store_64(&pTaskInfo->owner, 0);
468,099,517✔
1007
  if (code) {
468,097,238✔
1008
    pTaskInfo->code = code;
×
1009
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1010
  }
1011

1012
  return pTaskInfo->code;
468,097,238✔
1013
}
1014

1015
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
1016
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1017
  SArray*        pList = pTaskInfo->pResultBlockList;
×
1018
  size_t         num = taosArrayGetSize(pList);
×
1019
  for (int32_t i = 0; i < num; ++i) {
×
1020
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
1021
    if (p) {
×
1022
      blockDataDestroy(*p);
×
1023
    }
1024
  }
1025

1026
  taosArrayClear(pTaskInfo->pResultBlockList);
×
1027
}
×
1028

1029
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
171,876,175✔
1030
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
171,876,175✔
1031
  int64_t        threadId = taosGetSelfPthreadId();
171,876,175✔
1032
  int64_t        curOwner = 0;
171,874,607✔
1033

1034
  *pRes = NULL;
171,874,607✔
1035

1036
  // todo extract method
1037
  taosRLockLatch(&pTaskInfo->lock);
171,874,244✔
1038
  bool isKilled = isTaskKilled(pTaskInfo);
171,875,910✔
1039
  if (isKilled) {
171,875,096✔
1040
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1041

1042
    taosRUnLockLatch(&pTaskInfo->lock);
×
1043
    return pTaskInfo->code;
×
1044
  }
1045

1046
  if (pTaskInfo->owner != 0) {
171,875,096✔
1047
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1048
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1049

1050
    taosRUnLockLatch(&pTaskInfo->lock);
×
1051
    return pTaskInfo->code;
×
1052
  }
1053

1054
  pTaskInfo->owner = threadId;
171,873,084✔
1055
  taosRUnLockLatch(&pTaskInfo->lock);
171,874,806✔
1056

1057
  if (pTaskInfo->cost.start == 0) {
171,875,689✔
1058
    pTaskInfo->cost.start = taosGetTimestampUs();
290,336✔
1059
  }
1060

1061
  // error occurs, record the error code and return to client
1062
  int32_t ret = setjmp(pTaskInfo->env);
171,876,096✔
1063
  if (ret != TSDB_CODE_SUCCESS) {
171,867,451✔
1064
    pTaskInfo->code = ret;
339✔
1065
    (void)cleanUpUdfs();
339✔
1066
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
339✔
1067
    atomic_store_64(&pTaskInfo->owner, 0);
339✔
1068
    return pTaskInfo->code;
339✔
1069
  }
1070

1071
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
171,867,112✔
1072

1073
  int64_t st = taosGetTimestampUs();
171,874,184✔
1074
  int32_t code = TSDB_CODE_SUCCESS;
171,874,184✔
1075
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
171,874,184✔
1076
    pTaskInfo->paramSet = true;
×
1077
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1078
  } else {
1079
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
171,874,127✔
1080
  }
1081
  if (code) {
171,869,435✔
1082
    pTaskInfo->code = code;
×
1083
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1084
  }
1085

1086
  code = blockDataCheck(*pRes);
171,869,435✔
1087
  if (code) {
171,871,042✔
1088
    pTaskInfo->code = code;
×
1089
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1090
  }
1091

1092
  uint64_t el = (taosGetTimestampUs() - st);
171,869,530✔
1093

1094
  pTaskInfo->cost.elapsedTime += el;
171,869,530✔
1095
  if (NULL == *pRes) {
171,871,794✔
1096
    *useconds = pTaskInfo->cost.elapsedTime;
134,038,921✔
1097
  }
1098

1099
  (void)cleanUpUdfs();
171,868,717✔
1100

1101
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
171,877,402✔
1102
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
171,878,183✔
1103

1104
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
171,877,776✔
1105
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1106

1107
  atomic_store_64(&pTaskInfo->owner, 0);
171,875,134✔
1108
  return pTaskInfo->code;
171,877,365✔
1109
}
1110

1111
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
120,065,416✔
1112
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
120,065,416✔
1113
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
120,066,349✔
1114
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
120,066,767✔
1115

1116
  if (!tmp) {
120,066,314✔
1117
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1118
    return terrno;
×
1119
  }
1120
  return TSDB_CODE_SUCCESS;
120,066,314✔
1121
}
1122

1123
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1124
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1125
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1126

1127
  if (key->refId < pInfo->refId) {
×
1128
    return -1;
×
1129
  } else if (key->refId > pInfo->refId) {
×
1130
    return 1;
×
1131
  }
1132

1133
  return 0;
×
1134
}
1135

1136
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1137
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1138
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1139
  if (idx >= 0) {
×
1140
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1141
  }
1142
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1143
}
×
1144

1145
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
98,281✔
1146
  if (pTaskInfo->pSubJobCtx) {
98,281✔
1147
    pTaskInfo->pSubJobCtx->code = pTaskInfo->code;
28,623✔
1148
    int32_t code = tsem_post(&pTaskInfo->pSubJobCtx->ready);
28,623✔
1149
  }
1150
  
1151
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
98,281✔
1152

1153
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
98,281✔
1154
  for (int32_t i = 0; i < num; ++i) {
102,540✔
1155
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
4,259✔
1156
    if (!pStop) {
4,259✔
1157
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1158
      continue;
×
1159
    }
1160
    SExchangeInfo* pExchangeInfo = taosAcquireRef(fetchObjRefPool, pStop->refId);
4,259✔
1161
    if (pExchangeInfo) {
4,259✔
1162
      int32_t code = tsem_post(&pExchangeInfo->ready);
4,259✔
1163
      if (code != TSDB_CODE_SUCCESS) {
4,259✔
1164
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1165
      } else {
1166
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
4,259✔
1167
      }
1168
      code = taosReleaseRef(fetchObjRefPool, pStop->refId);
4,259✔
1169
      if (code != TSDB_CODE_SUCCESS) {
4,259✔
1170
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1171
      }
1172
    }
1173
  }
1174

1175
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
98,281✔
1176
}
98,281✔
1177

1178
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
98,281✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
98,281✔
1180
  if (pTaskInfo == NULL) {
98,281✔
1181
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1182
  }
1183

1184
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
98,281✔
1185

1186
  setTaskKilled(pTaskInfo, rspCode);
98,281✔
1187
  qStopTaskOperators(pTaskInfo);
98,281✔
1188

1189
  return TSDB_CODE_SUCCESS;
98,281✔
1190
}
1191

1192
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1193
  int64_t        st = taosGetTimestampMs();
×
1194
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1195
  if (pTaskInfo == NULL) {
×
1196
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1197
  }
1198

1199
  if (waitDuration > 0) {
×
1200
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1201
  } else {
1202
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1203
  }
1204

1205
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1206

1207
  if (waitDuration > 0) {
×
1208
    while (1) {
1209
      taosWLockLatch(&pTaskInfo->lock);
×
1210
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1211
        taosWUnLockLatch(&pTaskInfo->lock);
×
1212

1213
        taosMsleep(200);
×
1214

1215
        int64_t d = taosGetTimestampMs() - st;
×
1216
        if (d >= waitDuration && waitDuration >= 0) {
×
1217
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1218
          return TSDB_CODE_SUCCESS;
×
1219
        }
1220
      } else {  // not running now
1221
        pTaskInfo->code = rspCode;
×
1222
        taosWUnLockLatch(&pTaskInfo->lock);
×
1223
        return TSDB_CODE_SUCCESS;
×
1224
      }
1225
    }
1226
  }
1227

1228
  int64_t et = taosGetTimestampMs() - st;
×
1229
  if (et < waitDuration) {
×
1230
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1231
    return TSDB_CODE_SUCCESS;
×
1232
  }
1233
  return TSDB_CODE_SUCCESS;
×
1234
}
1235

1236
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1237
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1238
  if (NULL == pTaskInfo) {
×
1239
    return false;
×
1240
  }
1241

1242
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1243
}
1244

1245
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
401,715,359✔
1246
  STaskCostInfo* pSummary = &pTaskInfo->cost;
401,715,359✔
1247
  int64_t        idleTime = pSummary->start - pSummary->created;
401,724,457✔
1248

1249
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
401,722,352✔
1250
  if (pSummary->pRecoder != NULL) {
401,689,025✔
1251
    qDebug(
295,286,236✔
1252
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1253
        "createGroupIdMap:%.2f ms, total blocks:%" PRId64
1254
        ",load block SMA:%" PRId64 ", load data block:%" PRId64 ", check rows:%" PRId64,
1255
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0,
1256
        pSummary->extractListTime, pSummary->groupIdMapTime, pRecorder->totalBlocks,
1257
        pRecorder->smaLoadBlocks, pRecorder->fileLoadBlocks, pRecorder->checkRows);
1258
  } else {
1259
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
106,421,383✔
1260
           pSummary->elapsedTime / 1000.0);
1261
  }
1262
}
401,707,157✔
1263

1264

1265
void qDestroyTask(qTaskInfo_t qTaskHandle) {
412,661,489✔
1266
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
412,661,489✔
1267
  if (pTaskInfo == NULL) {
412,661,489✔
1268
    return;
10,956,532✔
1269
  }
1270

1271
  if (pTaskInfo->pRoot != NULL) {
401,704,957✔
1272
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
401,724,589✔
1273
  } else {
1274
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1275
  }
1276

1277
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
401,725,417✔
1278
  doDestroyTask(pTaskInfo);
401,709,450✔
1279
}
1280

1281
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,474,305✔
1282
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,474,305✔
1283
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,474,305✔
1284
}
1285

1286
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
396,145✔
1287
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
396,145✔
1288
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
396,145✔
1289

1290
  while (1) {
386,429✔
1291
    uint16_t type = pOperator->operatorType;
782,574✔
1292
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
782,574✔
1293
      *scanner = pOperator->info;
396,145✔
1294
      break;
396,145✔
1295
    } else {
1296
      pOperator = pOperator->pDownstream[0];
386,429✔
1297
    }
1298
  }
1299
}
396,145✔
1300

1301
void* qExtractReaderFromTmqScanner(void* scanner) {
396,145✔
1302
  STmqQueryScanInfo* pInfo = scanner;
396,145✔
1303
  return (void*)pInfo->tqReader;
396,145✔
1304
}
1305

1306
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
896,873✔
1307
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
896,873✔
1308
  return pTaskInfo->tmqInfo.schema;
896,873✔
1309
}
1310

1311
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
896,873✔
1312
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
896,873✔
1313
  return pTaskInfo->tmqInfo.tbName;
896,873✔
1314
}
1315

1316
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
984,858✔
1317
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
984,858✔
1318
  return &pTaskInfo->tmqInfo.btMetaRsp;
984,858✔
1319
}
1320

1321
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
171,035,590✔
1322
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
171,035,590✔
1323
  tOffsetCopy(pOffset, &pTaskInfo->tmqInfo.currentOffset);
171,035,590✔
1324
  return 0;
171,032,463✔
1325
}
1326

1327
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
937,249✔
1328
  memset(pCond, 0, sizeof(SQueryTableDataCond));
937,249✔
1329
  pCond->order = TSDB_ORDER_ASC;
937,249✔
1330
  pCond->numOfCols = pMtInfo->schema->nCols;
939,427✔
1331
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
939,064✔
1332
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
937,975✔
1333
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
938,338✔
1334
    taosMemoryFreeClear(pCond->colList);
×
1335
    taosMemoryFreeClear(pCond->pSlotList);
×
1336
    return terrno;
×
1337
  }
1338

1339
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
937,612✔
1340
  pCond->suid = pMtInfo->suid;
938,701✔
1341
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
937,612✔
1342
  pCond->startVersion = -1;
939,064✔
1343
  pCond->endVersion = sContext->snapVersion;
938,701✔
1344

1345
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
5,955,374✔
1346
    SColumnInfo* pColInfo = &pCond->colList[i];
5,015,584✔
1347
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
5,014,858✔
1348
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
5,016,310✔
1349
    if (pMtInfo->pExtSchemas != NULL) {
5,015,584✔
1350
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
55,680✔
1351
    }
1352
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
5,015,947✔
1353
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
5,014,132✔
1354

1355
    pCond->pSlotList[i] = i;
5,017,762✔
1356
  }
1357

1358
  return TSDB_CODE_SUCCESS;
939,064✔
1359
}
1360

1361
void qStreamSetOpen(qTaskInfo_t tinfo) {
169,951,998✔
1362
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
169,951,998✔
1363
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
169,951,998✔
1364
  pOperator->status = OP_NOT_OPENED;
169,988,890✔
1365
}
169,943,339✔
1366

1367
void qStreamSetParams(qTaskInfo_t tinfo, int8_t sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
170,004,549✔
1368
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
170,004,549✔
1369
  pTaskInfo->tmqInfo.sourceExcluded = sourceExcluded;
170,004,549✔
1370
  pTaskInfo->tmqInfo.minPollRows = minPollRows;
170,030,590✔
1371
  pTaskInfo->tmqInfo.timeout = timeout;
169,819,482✔
1372
  pTaskInfo->tmqInfo.enableReplay = enableReplay;
169,861,084✔
1373
}
169,904,429✔
1374

1375
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
179,484,841✔
1376
  int32_t        code = TSDB_CODE_SUCCESS;
179,484,841✔
1377
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
179,484,841✔
1378
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
179,484,841✔
1379

1380
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
179,523,625✔
1381
  const char*    id = GET_TASKID(pTaskInfo);
179,504,011✔
1382

1383
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
179,487,637✔
1384
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
172,358,112✔
1385
    if (pOperator == NULL || code != 0) {
172,299,654✔
1386
      return code;
×
1387
    }
1388

1389
    STmqQueryScanInfo* pInfo = pOperator->info;
172,304,075✔
1390
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
172,299,621✔
1391
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
172,330,943✔
1392
    walReaderVerifyOffset(pWalReader, pOffset);
172,353,230✔
1393
  }
1394
  // if pOffset equal to current offset, means continue consume
1395
  if (tOffsetEqual(pOffset, &pTaskInfo->tmqInfo.currentOffset)) {
179,490,110✔
1396
    return 0;
169,700,052✔
1397
  }
1398

1399
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
9,768,401✔
1400
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
8,805,894✔
1401
    if (pOperator == NULL || code != 0) {
8,804,198✔
1402
      return code;
1,203✔
1403
    }
1404

1405
    STmqQueryScanInfo* pInfo = pOperator->info;
8,802,995✔
1406
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
8,805,643✔
1407
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
8,801,088✔
1408
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
8,801,163✔
1409

1410
    if (pOffset->type == TMQ_OFFSET__LOG) {
8,802,114✔
1411
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
4,992,795✔
1412
      pScanBaseInfo->dataReader = NULL;
4,986,244✔
1413

1414
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,986,671✔
1415
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,989,084✔
1416
      walReaderVerifyOffset(pWalReader, pOffset);
4,985,556✔
1417
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
4,995,666✔
1418
      if (code < 0) {
4,991,218✔
1419
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
4,743,900✔
1420
        return code;
4,747,936✔
1421
      }
1422
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
3,810,606✔
1423
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1424
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1425
      int64_t uid = pOffset->uid;
3,814,366✔
1426
      int64_t ts = pOffset->ts;
3,812,123✔
1427
      int32_t index = 0;
3,810,259✔
1428

1429
      // this value may be changed if new tables are created
1430
      taosRLockLatch(&pTaskInfo->lock);
3,810,259✔
1431
      int32_t numOfTables = 0;
3,813,921✔
1432
      code = tableListGetSize(pTableListInfo, &numOfTables);
3,813,921✔
1433
      if (code != TSDB_CODE_SUCCESS) {
3,809,864✔
1434
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1435
        taosRUnLockLatch(&pTaskInfo->lock);
×
1436
        return code;
×
1437
      }
1438

1439
      if (uid == 0) {
3,809,864✔
1440
        if (numOfTables != 0) {
3,802,067✔
1441
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
47,669✔
1442
          if (!tmp) {
48,043✔
1443
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1444
            taosRUnLockLatch(&pTaskInfo->lock);
×
1445
            return terrno;
×
1446
          }
1447
          if (tmp) uid = tmp->uid;
48,043✔
1448
          ts = INT64_MIN;
47,669✔
1449
          pScanInfo->currentTable = 0;
47,669✔
1450
        } else {
1451
          taosRUnLockLatch(&pTaskInfo->lock);
3,754,398✔
1452
          qError("no table in table list, %s", id);
3,756,600✔
1453
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
3,760,718✔
1454
        }
1455
      }
1456
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
55,466✔
1457

1458
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
55,845✔
1459
             pInfo->pTableScanOp->resultInfo.totalRows);
1460
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
55,845✔
1461

1462
      // start from current accessed position
1463
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1464
      // position, let's find it from the beginning.
1465
      index = tableListFind(pTableListInfo, uid, 0);
55,845✔
1466
      taosRUnLockLatch(&pTaskInfo->lock);
55,845✔
1467

1468
      if (index >= 0) {
55,845✔
1469
        pScanInfo->currentTable = index;
55,845✔
1470
      } else {
1471
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1472
               numOfTables, pScanInfo->currentTable, id);
1473
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1474
      }
1475

1476
      STableKeyInfo keyInfo = {.uid = uid};
55,845✔
1477
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
55,845✔
1478

1479
      // let's start from the next ts that returned to consumer.
1480
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
55,845✔
1481
        pScanBaseInfo->cond.twindows.skey = ts;
1,106✔
1482
      } else {
1483
        pScanBaseInfo->cond.twindows.skey = ts + 1;
54,739✔
1484
      }
1485
      pScanInfo->scanTimes = 0;
55,845✔
1486

1487
      if (pScanBaseInfo->dataReader == NULL) {
55,845✔
1488
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
95,112✔
1489
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1490
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
47,556✔
1491
        if (code != TSDB_CODE_SUCCESS) {
47,556✔
1492
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1493
          return code;
×
1494
        }
1495

1496
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
47,556✔
1497
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1498
      } else {
1499
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
8,289✔
1500
        if (code != TSDB_CODE_SUCCESS) {
8,289✔
1501
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1502
          return code;
×
1503
        }
1504

1505
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
8,289✔
1506
        if (code != TSDB_CODE_SUCCESS) {
8,289✔
1507
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1508
          return code;
×
1509
        }
1510
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
8,289✔
1511
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1512
      }
1513

1514
      // restore the key value
1515
      pScanBaseInfo->cond.twindows.skey = oldSkey;
55,845✔
1516
    } else {
1517
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1518
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1519
    }
1520

1521
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1522
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
962,507✔
1523
      STmqRawScanInfo* pInfo = pOperator->info;
940,495✔
1524
      SSnapContext*       sContext = pInfo->sContext;
940,495✔
1525
      SOperatorInfo*      p = NULL;
940,858✔
1526

1527
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
940,858✔
1528
      if (code != 0) {
940,858✔
1529
        return code;
×
1530
      }
1531

1532
      STableListInfo* pTableListInfo = ((STmqRawScanInfo*)(p->info))->pTableListInfo;
940,858✔
1533

1534
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
940,858✔
1535
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1536
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1537
      }
1538

1539
      SMetaTableInfo mtInfo = {0};
940,858✔
1540
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
940,858✔
1541
      if (code != 0) {
940,495✔
1542
        destroyMetaTableInfo(&mtInfo);
1543
        return code;
×
1544
      }
1545
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
940,495✔
1546
      pInfo->dataReader = NULL;
939,769✔
1547

1548
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
940,495✔
1549
      tableListClear(pTableListInfo);
939,769✔
1550

1551
      if (mtInfo.uid == 0) {
940,858✔
1552
        destroyMetaTableInfo(&mtInfo);
1553
        goto end;  // no data
1,431✔
1554
      }
1555

1556
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
939,427✔
1557
      code = initQueryTableDataCondForTmq(&pTaskInfo->tmqInfo.tableCond, sContext, &mtInfo);
937,975✔
1558
      if (code != TSDB_CODE_SUCCESS) {
937,975✔
1559
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1560
        destroyMetaTableInfo(&mtInfo);
1561
        return code;
×
1562
      }
1563
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
937,975✔
1564
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts;
1,564✔
1565
      } else {
1566
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts + 1;
936,411✔
1567
      }
1568

1569
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
937,975✔
1570
      if (code != TSDB_CODE_SUCCESS) {
939,427✔
1571
        destroyMetaTableInfo(&mtInfo);
1572
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1573
        return code;
×
1574
      }
1575

1576
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
939,427✔
1577
      if (!pList) {
939,427✔
1578
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1579
        destroyMetaTableInfo(&mtInfo);
1580
        return code;
×
1581
      }
1582
      int32_t size = 0;
939,427✔
1583
      code = tableListGetSize(pTableListInfo, &size);
939,427✔
1584
      if (code != TSDB_CODE_SUCCESS) {
939,427✔
1585
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1586
        destroyMetaTableInfo(&mtInfo);
1587
        return code;
×
1588
      }
1589

1590
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->tmqInfo.tableCond, pList, size,
1,878,854✔
1591
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
939,427✔
1592
      if (code != TSDB_CODE_SUCCESS) {
937,240✔
1593
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1594
        destroyMetaTableInfo(&mtInfo);
1595
        return code;
×
1596
      }
1597

1598
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
937,240✔
1599
      tstrncpy(pTaskInfo->tmqInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
937,585✔
1600
      //      pTaskInfo->tmqInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1601
      tDeleteSchemaWrapper(pTaskInfo->tmqInfo.schema);
937,975✔
1602
      pTaskInfo->tmqInfo.schema = mtInfo.schema;
937,612✔
1603
      taosMemoryFreeClear(mtInfo.pExtSchemas);
938,338✔
1604

1605
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
938,338✔
1606
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
22,012✔
1607
      STmqRawScanInfo* pInfo = pOperator->info;
7,676✔
1608
      SSnapContext*       sContext = pInfo->sContext;
7,676✔
1609
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
7,676✔
1610
      if (code != 0) {
7,676✔
1611
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1612
        return code;
×
1613
      }
1614
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
7,676✔
1615
             id);
1616
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
14,336✔
1617
      STmqRawScanInfo* pInfo = pOperator->info;
14,336✔
1618
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
14,336✔
1619
      pInfo->dataReader = NULL;
14,336✔
1620
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
14,336✔
1621
    }
1622
  }
1623

1624
end:
1,265,919✔
1625
  tOffsetCopy(&pTaskInfo->tmqInfo.currentOffset, pOffset);
1,266,445✔
1626
  return 0;
1,266,445✔
1627
}
1628

1629
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
323,951,782✔
1630
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
323,951,782✔
1631
  if (pMsg->info.ahandle == NULL) {
324,009,804✔
1632
    rpcFreeCont(pMsg->pCont);
×
1633
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
×
1634
    return;
×
1635
  }
1636

1637
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
323,908,342✔
1638
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1639

1640
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
323,932,407✔
1641

1642
  if (pMsg->contLen > 0) {
323,991,865✔
1643
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
323,828,043✔
1644
    if (buf.pData == NULL) {
323,769,776✔
1645
      pMsg->code = terrno;
×
1646
    } else {
1647
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
323,769,776✔
1648
    }
1649
  }
1650

1651
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
323,997,228✔
1652
  rpcFreeCont(pMsg->pCont);
324,035,499✔
1653
  destroySendMsgInfo(pSendInfo);
323,980,307✔
1654
}
1655

1656
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1657
  int32_t        code = TSDB_CODE_SUCCESS;
×
1658
  int32_t        lino = 0;
×
1659
  SExecTaskInfo* pTaskInfo = tinfo;
×
1660
  SArray*        plist = NULL;
×
1661

1662
  code = getTableListInfo(pTaskInfo, &plist);
×
1663
  if (code || plist == NULL) {
×
1664
    return NULL;
×
1665
  }
1666

1667
  // only extract table in the first elements
1668
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1669

1670
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1671
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1672

1673
  int32_t numOfTables = 0;
×
1674
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1675
  QUERY_CHECK_CODE(code, lino, _end);
×
1676

1677
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1678
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1679
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1680
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1681
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1682
  }
1683

1684
  taosArrayDestroy(plist);
×
1685

1686
_end:
×
1687
  if (code != TSDB_CODE_SUCCESS) {
×
1688
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1689
    T_LONG_JMP(pTaskInfo->env, code);
×
1690
  }
1691
  return pUidList;
×
1692
}
1693

1694
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
4,006,326✔
1695
  int32_t        code = TSDB_CODE_SUCCESS;
4,006,326✔
1696
  int32_t        lino = 0;
4,006,326✔
1697
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
4,006,326✔
1698

1699
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
4,005,686✔
1700
    STmqQueryScanInfo* pScanInfo = pOperator->info;
×
1701
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1702

1703
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1704
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1705
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
4,004,409✔
1706
    STableScanInfo* pScanInfo = pOperator->info;
2,002,206✔
1707

1708
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
2,002,206✔
1709
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,003,483✔
1710
  } else {
1711
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
2,003,483✔
1712
      code = extractTableList(pList, pOperator->pDownstream[0]);
2,003,483✔
1713
    }
1714
  }
1715

1716
_end:
×
1717
  if (code != TSDB_CODE_SUCCESS) {
4,005,049✔
1718
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1719
  }
1720
  return code;
4,005,049✔
1721
}
1722

1723
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
2,003,483✔
1724
  if (pList == NULL) {
2,003,483✔
1725
    return TSDB_CODE_INVALID_PARA;
×
1726
  }
1727

1728
  *pList = NULL;
2,003,483✔
1729
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
2,003,483✔
1730
  if (pArray == NULL) {
2,003,483✔
1731
    return terrno;
×
1732
  }
1733

1734
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
2,003,483✔
1735
  if (code == 0) {
2,002,206✔
1736
    *pList = pArray;
2,002,206✔
1737
  } else {
1738
    taosArrayDestroy(pArray);
×
1739
  }
1740
  return code;
2,002,206✔
1741
}
1742

1743
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1744
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1745
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1746
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1747
  }
1748
  return 0;
×
1749
}
1750

1751
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1752
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1753
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1754
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1755
  }
1756
  return 0;
×
1757
}
1758

1759
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1760
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1761

1762
  int32_t code = pTaskInfo->code;
×
1763
  pTaskInfo->code = 0;
×
1764
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1765
}
×
1766

1767
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1768
  int32_t code = 0;
×
1769
  return code;
×
1770
}
1771

1772
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1773
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1774
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1775
  return code;
×
1776
}
1777

1778
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
35,292,107✔
1779
  int32_t code = 0;
35,292,107✔
1780

1781
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
35,292,107✔
1782
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
35,291,264✔
1783

1784
  if (pOper->fpSet.resetStateFn) {
35,290,497✔
1785
    code = pOper->fpSet.resetStateFn(pOper);
35,289,588✔
1786
  }
1787
  pOper->status = OP_NOT_OPENED;
35,288,234✔
1788
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
60,223,951✔
1789
    code = clearStatesForOperator(pOper->pDownstream[i]);
24,932,111✔
1790
  }
1791
  return code;
35,294,219✔
1792
}
1793

1794
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
10,358,367✔
1795
  int32_t        code = 0;
10,358,367✔
1796
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,358,367✔
1797
  SOperatorInfo* pOper = pTaskInfo->pRoot;
10,358,367✔
1798
  pTaskInfo->code = TSDB_CODE_SUCCESS;
10,358,090✔
1799
  code = clearStatesForOperator(pOper);
10,358,367✔
1800
  return code;
10,358,367✔
1801
}
1802

1803
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, bool* finished) {
16,761,862✔
1804
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
16,761,862✔
1805
  int64_t        threadId = taosGetSelfPthreadId();
16,761,862✔
1806
  int64_t        curOwner = 0;
16,761,862✔
1807

1808
  *ppRes = NULL;
16,761,862✔
1809

1810
  // todo extract method
1811
  taosRLockLatch(&pTaskInfo->lock);
16,761,862✔
1812
  bool isKilled = isTaskKilled(pTaskInfo);
16,761,862✔
1813
  if (isKilled) {
16,761,862✔
1814
    // clearStreamBlock(pTaskInfo->pRoot);
1815
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1816

1817
    taosRUnLockLatch(&pTaskInfo->lock);
×
1818
    return pTaskInfo->code;
×
1819
  }
1820

1821
  if (pTaskInfo->owner != 0) {
16,761,862✔
1822
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1823
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1824

1825
    taosRUnLockLatch(&pTaskInfo->lock);
×
1826
    return pTaskInfo->code;
×
1827
  }
1828

1829
  pTaskInfo->owner = threadId;
16,761,308✔
1830
  taosRUnLockLatch(&pTaskInfo->lock);
16,761,862✔
1831

1832
  if (pTaskInfo->cost.start == 0) {
16,761,862✔
1833
    pTaskInfo->cost.start = taosGetTimestampUs();
582,864✔
1834
  }
1835

1836
  // error occurs, record the error code and return to client
1837
  int32_t ret = setjmp(pTaskInfo->env);
16,761,290✔
1838
  if (ret != TSDB_CODE_SUCCESS) {
18,626,010✔
1839
    pTaskInfo->code = ret;
1,864,425✔
1840
    (void)cleanUpUdfs();
1,864,425✔
1841
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,864,425✔
1842
    atomic_store_64(&pTaskInfo->owner, 0);
1,864,425✔
1843
    return pTaskInfo->code;
1,864,425✔
1844
  }
1845

1846
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
16,761,585✔
1847

1848
  int64_t st = taosGetTimestampUs();
16,761,862✔
1849

1850
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
16,761,862✔
1851
  if (code) {
14,895,785✔
1852
    pTaskInfo->code = code;
×
1853
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1854
  } else {
1855
    *finished = *ppRes == NULL;
14,895,785✔
1856
    code = blockDataCheck(*ppRes);
14,897,437✔
1857
  }
1858
  if (code) {
14,897,154✔
1859
    pTaskInfo->code = code;
×
1860
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1861
  }
1862

1863
  uint64_t el = (taosGetTimestampUs() - st);
14,896,850✔
1864

1865
  pTaskInfo->cost.elapsedTime += el;
14,896,850✔
1866

1867
  (void)cleanUpUdfs();
14,897,437✔
1868

1869
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
14,897,437✔
1870
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
14,897,437✔
1871

1872
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
14,897,437✔
1873
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1874

1875
  atomic_store_64(&pTaskInfo->owner, 0);
14,897,437✔
1876
  return pTaskInfo->code;
14,897,437✔
1877
}
1878

1879
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1880
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1881
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1882
// }
1883

1884
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
370,121✔
1885
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1886
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1887
  int32_t code = 0;                                        
370,121✔
1888
  if (*pTableListInfo != NULL) {
370,121✔
1889
    qDebug("table list already exists, no need to create again");
×
1890
    goto end;
×
1891
  }
1892
  STableListInfo* pList = tableListCreate();
370,858✔
1893
  if (pList == NULL) {
370,602✔
1894
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1895
    code = terrno;
×
1896
    goto end;
×
1897
  }
1898

1899
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
370,602✔
1900
  SReadHandle    pHandle = {.vnode = pVnode};
370,602✔
1901
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
370,858✔
1902

1903
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
370,613✔
1904
  if (code != 0) {
370,858✔
1905
    tableListDestroy(pList);
×
1906
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1907
    goto end;
×
1908
  }
1909
  *pTableListInfo = pList;
370,858✔
1910

1911
end:
370,858✔
1912
  return 0;
370,858✔
1913
}
1914

1915
static int32_t doFilterTableByTagCond(void* pVnode, STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
36,680✔
1916
  int32_t code = doFilterByTagCond(pListInfo->idInfo.suid, pUidList, -1, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, NULL);
36,680✔
1917
  if (code != 0) {
36,680✔
1918
    return code;
×
1919
  }
1920
  int32_t numOfTables = taosArrayGetSize(pUidList);
36,680✔
1921
  for (int i = 0; i < numOfTables; i++) {
73,360✔
1922
    void* tmp = taosArrayGet(pUidList, i);
36,680✔
1923
    if (tmp == NULL) {
36,680✔
1924
      return terrno;
×
1925
    }
1926
    STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
36,680✔
1927

1928
    void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
36,680✔
1929
    if (p == NULL) {
36,680✔
1930
      return terrno;
×
1931
    }
1932
  }
1933
  return code;
36,680✔
1934
}
1935

1936
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
36,461✔
1937
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1938
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1939
  int32_t code = TSDB_CODE_SUCCESS;
36,461✔
1940
  SArray* uidListCopy = NULL;
36,461✔
1941
  STableListInfo* pList = tableListCreate();
36,461✔
1942
  if (pList == NULL) {
36,680✔
1943
    code = terrno;
×
1944
    goto end;
×
1945
  }
1946
  uidListCopy = taosArrayDup(uidList, NULL);
36,680✔
1947
  if (uidListCopy == NULL) {
36,680✔
1948
    code = terrno;
×
1949
    goto end;
×
1950
  }
1951
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
36,680✔
1952
  SReadHandle    pHandle = {.vnode = pVnode};
36,680✔
1953

1954
  pList->idInfo.suid = suid;
36,680✔
1955
  pList->idInfo.tableType = TD_SUPER_TABLE;
36,680✔
1956
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
36,680✔
1957
  if (code != TSDB_CODE_SUCCESS) {
36,680✔
1958
    goto end;
×
1959
  }                                              
1960
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap,
36,680✔
1961
                                     false);
1962
  if (code != TSDB_CODE_SUCCESS) {
36,680✔
1963
    goto end;
×
1964
  }
1965
  *tableList = pList->pTableList;
36,680✔
1966
  pList->pTableList = NULL;
36,680✔
1967

1968
  taosArrayClear(uidList);
36,680✔
1969
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
73,360✔
1970
    void* tmp = taosArrayGet(uidListCopy, i);
36,680✔
1971
    if (tmp == NULL) {
36,680✔
1972
      continue;
×
1973
    }
1974
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
36,680✔
1975
    if (slot == NULL) {
36,680✔
1976
      if (taosArrayPush(uidList, tmp) == NULL) {
×
1977
        code = terrno;
×
1978
        goto end;
×
1979
      }
1980
    }
1981
  }
1982
end:
36,680✔
1983
  taosArrayDestroy(uidListCopy);
36,680✔
1984
  tableListDestroy(pList);
36,680✔
1985
  return code;
36,680✔
1986
}
1987

1988
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1989
//   int32_t index = -1;
1990
//   (void)taosThreadRwlockRdlock(lock);
1991
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1992
//     index = 0;
1993
//     goto end;
1994
//   }
1995
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1996
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1997

1998
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1999
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
2000
//       index = i;
2001
//       goto end;
2002
//     }
2003
//   }
2004
// end:
2005
//   (void)taosThreadRwlockUnlock(lock);
2006
//   return index;
2007
// }
2008

2009
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
370,858✔
2010
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
370,858✔
2011
  STableListInfo* pList = pTableListInfo;
370,858✔
2012
  return pList->pTableList;
370,858✔
2013
}
2014

2015
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
2,049,005✔
2016

2017
void streamDestroyExecTask(qTaskInfo_t tInfo) {
4,410,460✔
2018
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
4,410,460✔
2019
  qDestroyTask(tInfo);
4,410,460✔
2020
}
4,410,460✔
2021

2022
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
1,085,889✔
2023
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
1,085,889✔
2024
}
2025

2026
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
1,218,296✔
2027
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
2028
  int32_t      code = 0;
1,218,296✔
2029
  SNode*       pNode = NULL;
1,218,296✔
2030
  SNodeList*   pList = NULL;
1,218,296✔
2031
  SExprInfo*   pExprInfo = NULL;
1,218,296✔
2032
  int32_t      numOfExprs = 1;
1,218,296✔
2033
  int32_t*     offset = NULL;
1,218,071✔
2034
  STargetNode* pTargetNode = NULL;
1,218,071✔
2035
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
1,218,296✔
2036
  if (code == 0) {
1,218,036✔
2037
    code = nodesCloneNode(pExpr, &pNode);
1,218,036✔
2038
  }
2039

2040
  if (code == 0) {
1,218,296✔
2041
    pTargetNode->dataBlockId = 0;
1,218,296✔
2042
    pTargetNode->pExpr = pNode;
1,218,296✔
2043
    pTargetNode->slotId = 0;
1,218,296✔
2044
  }
2045
  if (code == 0) {
1,218,296✔
2046
    code = nodesMakeList(&pList);
1,218,296✔
2047
  }
2048
  if (code == 0) {
1,218,296✔
2049
    code = nodesListAppend(pList, (SNode*)pTargetNode);
1,218,296✔
2050
  }
2051
  if (code == 0) {
1,218,296✔
2052
    pNode = NULL;
1,218,296✔
2053
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
1,218,296✔
2054
  }
2055

2056
  if (code == 0) {
1,218,071✔
2057
    const char* pVal = NULL;
1,218,071✔
2058
    int32_t     len = 0;
1,218,071✔
2059
    SNode*      pSclNode = NULL;
1,218,071✔
2060
    switch (pExprInfo->pExpr->nodeType) {
1,218,071✔
2061
      case QUERY_NODE_FUNCTION:
1,218,296✔
2062
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
1,218,296✔
2063
        break;
1,218,296✔
2064
      case QUERY_NODE_OPERATOR:
×
2065
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
2066
        break;
×
2067
      default:
×
2068
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2069
        break;
×
2070
    }
2071
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
1,218,296✔
2072
    SSDataBlock block = {0};
1,218,296✔
2073
    block.info.rows = 1;
1,218,296✔
2074
    SSDataBlock* pBlock = &block;
1,218,296✔
2075
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
1,218,071✔
2076
    if (tmp == NULL) {
1,218,071✔
2077
      code = terrno;
×
2078
    }
2079
    if (code == 0) {
1,218,071✔
2080
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
1,218,296✔
2081
      gTaskScalarExtra.pStreamRange = NULL;
1,218,296✔
2082
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
1,218,071✔
2083
    }
2084
    taosArrayDestroy(pBlockList);
1,217,812✔
2085
  }
2086
  nodesDestroyList(pList);
1,218,037✔
2087
  destroyExprInfo(pExprInfo, numOfExprs);
1,218,296✔
2088
  taosMemoryFreeClear(pExprInfo);
1,218,037✔
2089
  return code;
1,218,037✔
2090
}
2091

2092
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,744,517✔
2093
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,744,517✔
2094
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,744,517✔
2095
  int32_t        code = 0;
2,744,517✔
2096
  SNode*         pNode = NULL;
2,744,517✔
2097
  if (!pForceOutputCols) return 0;
2,744,517✔
2098
  if (!*pRes) {
132,407✔
2099
    code = createDataBlock(pRes);
132,407✔
2100
  }
2101

2102
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
132,407✔
2103
    int32_t idx = 0;
132,407✔
2104
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
529,381✔
2105
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
396,974✔
2106
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
396,974✔
2107
      colInfo.info.precision = pCol->type.precision;
396,974✔
2108
      colInfo.info.scale = pCol->type.scale;
396,974✔
2109
      code = blockDataAppendColInfo(*pRes, &colInfo);
396,974✔
2110
      if (code != 0) break;
396,974✔
2111
    }
2112
  }
2113

2114
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
132,407✔
2115
  if (code != TSDB_CODE_SUCCESS) {
132,407✔
2116
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2117
    return code;
×
2118
  }
2119

2120
  // loop all exprs for force output, execute all exprs
2121
  int32_t idx = 0;
132,407✔
2122
  int32_t rowIdx = (*pRes)->info.rows;
132,407✔
2123
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
132,407✔
2124
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
132,407✔
2125
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
529,381✔
2126
    SScalarParam   dst = {0};
396,974✔
2127
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
396,974✔
2128
    code = nodesStringToNode(pCol->expr, &pNode);
396,974✔
2129
    if (code != 0) break;
396,974✔
2130
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
396,974✔
2131
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
396,974✔
2132
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
264,567✔
2133
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
264,567✔
2134
    } else {
2135
      dst.columnData = pInfo;
132,407✔
2136
      dst.numOfRows = rowIdx;
132,407✔
2137
      dst.colAlloced = false;
132,407✔
2138
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
132,407✔
2139
    }
2140
    ++idx;
396,974✔
2141
    // TODO sclFreeParam(&dst);
2142
    nodesDestroyNode(pNode);
396,974✔
2143
    if (code != 0) break;
396,974✔
2144
  }
2145
  if (code == TSDB_CODE_SUCCESS) {
132,407✔
2146
    (*pRes)->info.rows++;
132,407✔
2147
  }
2148
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
132,407✔
2149
  return code;
132,407✔
2150
}
2151

2152
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
384,131✔
2153
  int32_t      code = 0;
384,131✔
2154
  const char*  pVal = NULL;
384,131✔
2155
  SScalarParam dst = {0};
384,131✔
2156
  int32_t      len = 0;
384,131✔
2157
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
384,131✔
2158
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
384,131✔
2159
  if (pStreamRuntimeInfo->outNormalTable != NULL) {
384,131✔
2160
    tstrncpy(tbname, pStreamRuntimeInfo->outNormalTable, TSDB_DB_NAME_LEN + TSDB_TABLE_NAME_LEN + 1);
×
2161
    return code;
×
2162
  }
2163
  // execute the expr
2164
  switch (pExpr->type) {
384,131✔
2165
    case QUERY_NODE_VALUE: {
×
2166
      SValueNode* pValue = (SValueNode*)pExpr;
×
2167
      int32_t     type = pValue->node.resType.type;
×
2168
      if (!IS_STR_DATA_TYPE(type)) {
×
2169
        qError("invalid sub tb expr with non-str type");
×
2170
        code = TSDB_CODE_INVALID_PARA;
×
2171
        break;
×
2172
      }
2173
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2174
      if (pTmp == NULL) {
×
2175
        qError("invalid sub tb expr with null value");
×
2176
        code = TSDB_CODE_INVALID_PARA;
×
2177
        break;
×
2178
      }
2179
      pVal = varDataVal(pTmp);
×
2180
      len = varDataLen(pTmp);
×
2181
    } break;
×
2182
    case QUERY_NODE_FUNCTION: {
384,356✔
2183
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
384,356✔
2184
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
384,356✔
2185
        qError("invalid sub tb expr with non-str type func");
×
2186
        code = TSDB_CODE_INVALID_PARA;
×
2187
        break;
×
2188
      }
2189
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
384,131✔
2190
      if (!pCol) {
384,356✔
2191
        code = terrno;
×
2192
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2193
        break;
×
2194
      }
2195

2196
      pCol->hasNull = true;
384,356✔
2197
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
384,356✔
2198
      pCol->info.colId = 0;
384,356✔
2199
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
384,356✔
2200
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
384,356✔
2201
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
384,356✔
2202
      code = colInfoDataEnsureCapacity(pCol, 1, true);
384,356✔
2203
      if (code != 0) {
384,356✔
2204
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2205
        taosMemoryFree(pCol);
×
2206
        break;
×
2207
      }
2208
      dst.columnData = pCol;
384,356✔
2209
      dst.numOfRows = 1;
384,356✔
2210
      dst.colAlloced = true;
384,356✔
2211
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
384,356✔
2212
      if (colDataIsNull_var(dst.columnData, 0)) {
384,097✔
2213
        qInfo("invalid sub tb expr with null value");
3,510✔
2214
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,285✔
2215
      }
2216
      if (code == 0) {
383,872✔
2217
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
380,587✔
2218
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
380,349✔
2219
      }
2220
    } break;
383,859✔
2221
    default:
×
2222
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2223
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2224
      break;
×
2225
  }
2226
  if (code == 0) {
383,859✔
2227
    if (!pVal || len == 0) {
380,587✔
2228
      qError("tbname generated with no characters which is not allowed");
×
2229
      code = TSDB_CODE_INVALID_PARA;
×
2230
    }
2231
    if(len > TSDB_TABLE_NAME_LEN - 1) {
380,587✔
2232
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
552✔
2233
      len = TSDB_TABLE_NAME_LEN - 1;
552✔
2234
    }
2235

2236
    memcpy(tbname, pVal, len);
380,587✔
2237
    tbname[len] = '\0';  // ensure null terminated
380,587✔
2238
    if (NULL != strchr(tbname, '.')) {
380,812✔
2239
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2240
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2241
    }
2242
  }
2243
  // TODO free dst
2244
  sclFreeParam(&dst);
384,084✔
2245
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
384,097✔
2246
  return code;
383,818✔
2247
}
2248

2249
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
353,859✔
2250
  if (pParam) {
353,859✔
2251
    if (pParam->tbname) {
353,859✔
2252
      taosMemFree(pParam->tbname);
353,859✔
2253
      pParam->tbname = NULL;
353,859✔
2254
    }
2255
    if (pParam->stbname) {
353,859✔
2256
      taosMemFree(pParam->stbname);
353,859✔
2257
      pParam->stbname = NULL;
353,859✔
2258
    }
2259
    if (pParam->dbFName) {
353,859✔
2260
      taosMemFree(pParam->dbFName);
353,859✔
2261
      pParam->dbFName = NULL;
353,859✔
2262
    }
2263
    if (pParam->pFields) {
353,859✔
2264
      taosArrayDestroy(pParam->pFields);
353,859✔
2265
      pParam->pFields = NULL;
353,859✔
2266
    }
2267
    if (pParam->pTagFields) {
353,859✔
2268
      taosArrayDestroy(pParam->pTagFields);
195,628✔
2269
      pParam->pTagFields = NULL;
195,628✔
2270
    }
2271
    if (pParam->colCids) {
353,859✔
2272
      taosArrayDestroy(pParam->colCids);
2,340✔
2273
      pParam->colCids = NULL;
2,340✔
2274
    }
2275
    if (pParam->tagCids) {
353,859✔
2276
      taosArrayDestroy(pParam->tagCids);
1,317✔
2277
      pParam->tagCids = NULL;
1,317✔
2278
    }
2279
    taosMemFree(pParam);
353,859✔
2280
  }
2281
}
353,859✔
2282

2283
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
353,594✔
2284
  int32_t code = 0, lino = 0;
353,594✔
2285
  if (ppDst == NULL || pSrc == NULL) {
353,594✔
2286
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2287
  }
2288
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
353,594✔
2289
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
353,859✔
2290

2291
  (*ppDst)->suid = pSrc->suid;
353,594✔
2292
  (*ppDst)->sver = pSrc->sver;
353,859✔
2293
  (*ppDst)->tbType = pSrc->tbType;
353,859✔
2294
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
353,594✔
2295
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
353,859✔
2296

2297
  if (pSrc->stbname) {
353,859✔
2298
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
353,859✔
2299
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
353,859✔
2300
  }
2301

2302
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
353,859✔
2303
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
353,859✔
2304

2305
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
353,594✔
2306

2307
  if (pSrc->pFields && pSrc->pFields->size > 0) {
353,594✔
2308
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
353,859✔
2309
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
353,859✔
2310
  } else {
2311
    (*ppDst)->pFields = NULL;
×
2312
  }
2313
  
2314
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
353,859✔
2315
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
195,628✔
2316
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
195,628✔
2317
  } else {
2318
    (*ppDst)->pTagFields = NULL;
158,231✔
2319
  }
2320

2321
  if (pSrc->colCids && pSrc->colCids->size > 0) {
353,859✔
2322
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
2,340✔
2323
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
2,340✔
2324
  } else {
2325
    (*ppDst)->colCids = NULL;
351,519✔
2326
  }
2327

2328
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
353,859✔
2329
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,317✔
2330
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,317✔
2331
  } else {
2332
    (*ppDst)->tagCids = NULL;
352,542✔
2333
  }
2334

2335
_exit:
353,859✔
2336

2337
  if (code != 0) {
353,859✔
2338
    if (*ppDst) {
×
2339
      destroyStreamInserterParam(*ppDst);
×
2340
      *ppDst = NULL;
×
2341
    }
2342
    
2343
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2344
  }
2345
  return code;
353,859✔
2346
}
2347

2348
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
×
2349
  return doDropStreamTable(pMsgCb, pOutput, pReq);
×
2350
}
2351

2352
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2353
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2354
}
2355

2356
int32_t qFilterTableList(void* pVnode, SArray* uidList, int64_t version, SNode* node, void* pTaskInfo, uint64_t suid) {
77,103✔
2357
  int32_t         code = TSDB_CODE_SUCCESS;
77,103✔
2358

2359
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
77,103✔
2360
  code = doFilterByTagCond(suid, uidList, version, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
77,103✔
2361
  if (code != TSDB_CODE_SUCCESS) {
77,103✔
2362
    goto end;
×
2363
  }
2364
end:
77,103✔
2365
  return code;
77,103✔
2366
}
2367

2368
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2369
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2370
    return true;
2,147,483,647✔
2371
  }
2372

2373
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2374
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2375
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2376
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2377
      return durationSatisfied;
2,147,483,647✔
2378
    case TRUE_FOR_COUNT_ONLY:
59,936,855✔
2379
      return countSatisfied;
59,936,855✔
2380
    case TRUE_FOR_AND:
59,936,855✔
2381
      return durationSatisfied && countSatisfied;
59,936,855✔
2382
    case TRUE_FOR_OR:
59,940,560✔
2383
      return durationSatisfied || countSatisfied;
59,940,560✔
2384
    default:
×
2385
      return true;
×
2386
  }
2387
}
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