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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

54.74
/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 "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

37
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
38
int32_t             exchangeObjRefPool = -1;
39
SGlobalExecInfo     gExecInfo = {0};
40

41
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
1,800✔
42
  gExecInfo.dnode = pDnode;
1,800✔
43
  gExecInfo.getMnode = getMnode;
1,800✔
44
  gExecInfo.getDnodeId = getDnodeId;
1,800✔
45
  return;
1,800✔
46
}
47

48
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
151✔
49
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
151!
50
    qError("gExecInfo is not initialized");
×
51
    return TSDB_CODE_APP_ERROR;
×
52
  }
53
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
151✔
54
  return TSDB_CODE_SUCCESS;
151✔
55
}
56

57
static void cleanupRefPool() {
1,690✔
58
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
1,690✔
59
  taosCloseRef(ref);
1,690✔
60
}
1,690✔
61

62
static void initRefPool() {
1,690✔
63
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
1,690✔
64
  (void)atexit(cleanupRefPool);
1,690✔
65
}
1,690✔
66

67
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
×
68
  int32_t code = TSDB_CODE_SUCCESS;
×
69
  int32_t lino = 0;
×
70
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
71
    if (pOperator->numOfDownstream == 0) {
×
72
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
73
      return TSDB_CODE_APP_ERROR;
×
74
    }
75

76
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
77
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
78
      return TSDB_CODE_APP_ERROR;
×
79
    }
80
    pOperator->status = OP_NOT_OPENED;
×
81
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
×
82
  } else {
83
    pOperator->status = OP_NOT_OPENED;
×
84

85
    SStreamScanInfo* pInfo = pOperator->info;
×
86

87
    if (type == STREAM_INPUT__MERGED_SUBMIT) {
×
88
      for (int32_t i = 0; i < numOfBlocks; i++) {
×
89
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
×
90
        void*        tmp = taosArrayPush(pInfo->pBlockLists, pReq);
×
91
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
92
      }
93
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
×
94
    } else if (type == STREAM_INPUT__DATA_SUBMIT) {
×
95
      void* tmp = taosArrayPush(pInfo->pBlockLists, &input);
×
96
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
97
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
×
98
    } else if (type == STREAM_INPUT__DATA_BLOCK) {
×
99
      for (int32_t i = 0; i < numOfBlocks; ++i) {
×
100
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
×
101
        SPackedData  tmp = {.pDataBlock = pDataBlock};
×
102
        void*        tmpItem = taosArrayPush(pInfo->pBlockLists, &tmp);
×
103
        QUERY_CHECK_NULL(tmpItem, code, lino, _end, terrno);
×
104
      }
105
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
×
106
    } else if (type == STREAM_INPUT__CHECKPOINT) {
×
107
      SPackedData tmp = {.pDataBlock = input};
×
108
      void*       tmpItem = taosArrayPush(pInfo->pBlockLists, &tmp);
×
109
      QUERY_CHECK_NULL(tmpItem, code, lino, _end, terrno);
×
110
      pInfo->blockType = STREAM_INPUT__CHECKPOINT;
×
111
    } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
×
112
      for (int32_t i = 0; i < numOfBlocks; ++i) {
×
113
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
×
114
        void*        tmp = taosArrayPush(pInfo->pBlockLists, pReq);
×
115
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
116
      }
117
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
×
118
    }
119

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

123
_end:
×
124
  if (code != TSDB_CODE_SUCCESS) {
×
125
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
126
  }
127
  return code;
×
128
}
129

130
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
131
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
132
    if (pOperator->numOfDownstream == 0) {
×
133
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
134
      return TSDB_CODE_APP_ERROR;
×
135
    }
136

137
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
138
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
139
      return TSDB_CODE_APP_ERROR;
×
140
    }
141

142
    pOperator->status = OP_NOT_OPENED;
×
143
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
144
  }
145
  return 0;
×
146
}
147

148
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
80,674✔
149
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
80,674✔
150
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
80,674✔
151
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
30,559✔
152
    if (pStreamScanInfo->pTableScanOp != NULL) {
30,559!
153
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
30,559✔
154
      if (pScanInfo->base.dataReader != NULL) {
30,559✔
155
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
733✔
156
        if (code) {
733!
157
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
158
          return code;
×
159
        }
160
      }
161
    }
162
  } else {
163
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
50,115✔
164
  }
165

166
  return 0;
48,525✔
167
}
168

169
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
48,525✔
170
  SExecTaskInfo* pTaskInfo = tinfo;
48,525✔
171
  pTaskInfo->id.queryId = queryId;
48,525✔
172
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
48,525✔
173

174
  // set the idstr for tsdbReader
175
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
48,525✔
176
}
177

178
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
179
  SExecTaskInfo* pTaskInfo = tinfo;
×
180
  return pTaskInfo->status == OP_EXEC_DONE;
×
181
}
182

183
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
184
  if (tinfo == NULL) {
×
185
    return TSDB_CODE_APP_ERROR;
×
186
  }
187

188
  if (pBlocks == NULL || numOfBlocks == 0) {
×
189
    return TSDB_CODE_SUCCESS;
×
190
  }
191

192
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
193

194
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
195
  if (code != TSDB_CODE_SUCCESS) {
×
196
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
197
  } else {
198
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
199
  }
200

201
  return code;
×
202
}
203

204
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
1,294✔
205
                                     uint64_t id) {
206
  if (msg == NULL) {  // create raw scan
1,294✔
207
    SExecTaskInfo* pTaskInfo = NULL;
317✔
208

209
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
317✔
210
    if (NULL == pTaskInfo || code != 0) {
317!
211
      return NULL;
×
212
    }
213

214
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
317✔
215
    if (NULL == pTaskInfo->pRoot || code != 0) {
317!
216
      taosMemoryFree(pTaskInfo);
×
217
      return NULL;
×
218
    }
219

220
    pTaskInfo->storageAPI = pReaderHandle->api;
317✔
221
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
317!
222
    return pTaskInfo;
317✔
223
  }
224

225
  SSubplan* pPlan = NULL;
977✔
226
  int32_t   code = qStringToSubplan(msg, &pPlan);
977✔
227
  if (code != TSDB_CODE_SUCCESS) {
975!
228
    terrno = code;
×
229
    return NULL;
×
230
  }
231

232
  qTaskInfo_t pTaskInfo = NULL;
975✔
233
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE);
975✔
234
  if (code != TSDB_CODE_SUCCESS) {
977!
235
    qDestroyTask(pTaskInfo);
×
236
    terrno = code;
×
237
    return NULL;
×
238
  }
239

240
  // extract the number of output columns
241
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
977✔
242
  *numOfCols = 0;
977✔
243

244
  SNode* pNode;
245
  FOREACH(pNode, pDescNode->pSlots) {
8,815!
246
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
7,838✔
247
    if (pSlotDesc->output) {
7,838✔
248
      ++(*numOfCols);
7,836✔
249
    }
250
  }
251

252
  return pTaskInfo;
977✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
1,584✔
256
  if (streamInserterParam == NULL) {
1,584✔
257
    return TSDB_CODE_SUCCESS;
812✔
258
  }
259

260
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
772!
261
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
262
    return TSDB_CODE_INVALID_PARA;
×
263
  }
264

265
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
772!
266
    stError("insertParam: invalid db/table name");
×
267
    return TSDB_CODE_INVALID_PARA;
×
268
  }
269

270
  if (streamInserterParam->suid <= 0 &&
772✔
271
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
278!
272
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
273
    return TSDB_CODE_INVALID_PARA;
×
274
  }
275

276
  return TSDB_CODE_SUCCESS;
772✔
277
}
278

279
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
1,584✔
280
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
281
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
282
  if (pSubplan == NULL || pTaskInfo == NULL) {
1,584!
283
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
×
284
    nodesDestroyNode((SNode *)pSubplan);
×
285
    return TSDB_CODE_INVALID_PARA;
×
286
  }
287
  int32_t lino = 0;
1,584✔
288
  int32_t code = checkInsertParam(streamInserterParam);
1,584✔
289
  if (code != TSDB_CODE_SUCCESS) {
1,584!
290
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
291
    nodesDestroyNode((SNode *)pSubplan);
×
292
    return code;
×
293
  }
294
  SInserterParam* pInserterParam = NULL;
1,584✔
295
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
1,584✔
296
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
1,584✔
297
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
1,584✔
298

299
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
1,584✔
300
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
1,584!
301
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
2!
302
    goto _error;
2✔
303
  }
304

305
  if (streamInserterParam) {
1,582✔
306
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
772✔
307
    void*           pSinkManager = NULL;
772✔
308
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
772✔
309
    if (code != TSDB_CODE_SUCCESS) {
772!
310
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
311
      goto _error;
×
312
    }
313

314
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
772!
315
    if (NULL == pInserterParam) {
772!
316
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
317
      code = terrno;
×
318
      goto _error;
×
319
    }
320
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
772✔
321
    TSDB_CHECK_CODE(code, lino, _error);
772!
322
    
323
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
772✔
324
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
772✔
325

326
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
772✔
327
    if (code) {
772!
328
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
329
    }
330
  }
331
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
1,582✔
332
         tstrerror(code));
333

334
_error:
423✔
335

336
  if (code != TSDB_CODE_SUCCESS) {
1,584✔
337
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2!
338
    if (pInserterParam != NULL) {
2!
339
      taosMemoryFree(pInserterParam);
×
340
    }
341
  }
342
  return code;
1,584✔
343
}
344

345
bool qNeedReset(qTaskInfo_t pInfo) {
16,396✔
346
  if (pInfo == NULL) {
16,396!
347
    return false;
×
348
  }
349
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
16,396✔
350
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
16,396✔
351
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
16,396!
352
    return false;
12✔
353
  }
354
  int32_t node = nodeType(pOperator->pPhyNode);
16,384✔
355
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
15,473✔
356
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
31,857!
357
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
358
}
359

360
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
14,411✔
361
  if (pHandle == NULL || pScanBaseInfo == NULL) {
14,411!
362
    return;
×
363
  }
364

365
  pScanBaseInfo->readHandle.uid = pHandle->uid;
14,411✔
366
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
14,411✔
367
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
14,411✔
368
}
369

370
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
16,384✔
371
  if (pInfo == NULL) {
16,384!
372
    return TSDB_CODE_INVALID_PARA;
×
373
  }
374
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
16,384✔
375
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
16,384✔
376

377
  void*           info = pOperator->info;
16,384✔
378
  STableScanBase* pScanBaseInfo = NULL;
16,384✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
16,384✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
911✔
382
    setReadHandle(handle, pScanBaseInfo);
911✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
15,473✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
13,500✔
385
    setReadHandle(handle, pScanBaseInfo);
13,500✔
386
  }
387

388
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
16,384✔
389
  handle->winRange.ekey);
390
  return pOperator->fpSet.resetStateFn(pOperator);
16,384✔
391
}
392

393
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
1,584✔
394
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
395
  if (msg == NULL) {
1,584!
396
    return TSDB_CODE_INVALID_PARA;
×
397
  }
398

399
  *pTaskInfo = NULL;
1,584✔
400

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

416
  return code;
1,582✔
417
}
418

419
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
313✔
420
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
421
  int32_t code = TSDB_CODE_SUCCESS;
313✔
422
  int32_t lino = 0;
313✔
423
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
313✔
424
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
313!
425
  int32_t numOfUids = taosArrayGetSize(tableIdList);
313✔
426
  if (numOfUids == 0) {
313!
427
    (*ppArrayRes) = qa;
×
428
    goto _error;
×
429
  }
430

431
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
313✔
432

433
  uint64_t suid = 0;
313✔
434
  uint64_t uid = 0;
313✔
435
  int32_t  type = 0;
313✔
436
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
313✔
437

438
  // let's discard the tables those are not created according to the queried super table.
439
  SMetaReader mr = {0};
313✔
440
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
313✔
441
  for (int32_t i = 0; i < numOfUids; ++i) {
680✔
442
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
367✔
443
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
367!
444

445
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
367✔
446
    if (code != TSDB_CODE_SUCCESS) {
367!
447
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
448
      continue;
×
449
    }
450

451
    tDecoderClear(&mr.coder);
367✔
452

453
    if (mr.me.type == TSDB_SUPER_TABLE) {
367!
454
      continue;
×
455
    } else {
456
      if (type == TSDB_SUPER_TABLE) {
367!
457
        // this new created child table does not belong to the scanned super table.
458
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
367!
459
          continue;
10✔
460
        }
461
      } else {  // ordinary table
462
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
463
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
464
        if (mr.me.uid != uid) {
×
465
          continue;
×
466
        }
467
      }
468
    }
469

470
    if (pScanInfo->pTagCond != NULL) {
357✔
471
      bool          qualified = false;
178✔
472
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
178✔
473
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
178✔
474
      if (code != TSDB_CODE_SUCCESS) {
178!
475
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
476
        continue;
88✔
477
      }
478

479
      if (!qualified) {
178✔
480
        continue;
88✔
481
      }
482
    }
483

484
    // handle multiple partition
485
    void* tmp = taosArrayPush(qa, id);
269✔
486
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
269!
487
  }
488

489
_end:
313✔
490

491
  pAPI->metaReaderFn.clearReader(&mr);
313✔
492
  (*ppArrayRes) = qa;
313✔
493

494
_error:
313✔
495
  if (code != TSDB_CODE_SUCCESS) {
313!
496
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
497
  }
498
  return code;
313✔
499
}
500

501
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
316✔
502
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
316✔
503
  const char*    id = GET_TASKID(pTaskInfo);
316✔
504
  int32_t        code = 0;
316✔
505

506
  if (isAdd) {
316✔
507
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
313!
508
  }
509

510
  // traverse to the stream scanner node to add this table id
511
  SOperatorInfo* pInfo = NULL;
316✔
512
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
316✔
513
  if (code != 0 || pInfo == NULL) {
316!
514
    return code;
×
515
  }
516

517
  SStreamScanInfo* pScanInfo = pInfo->info;
316✔
518
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
316!
519
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
686✔
520
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
370✔
521
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
370✔
522
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
370✔
523
    }
524
  }
525

526
  if (isAdd) {  // add new table id
316✔
527
    SArray* qa = NULL;
313✔
528
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
313✔
529
    if (code != TSDB_CODE_SUCCESS) {
313!
530
      taosArrayDestroy(qa);
×
531
      return code;
×
532
    }
533
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
313✔
534
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
313!
535
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
313✔
536

537
    bool   assignUid = false;
313✔
538
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
313!
539
    char*  keyBuf = NULL;
313✔
540
    if (bufLen > 0) {
313!
541
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
542
      keyBuf = taosMemoryMalloc(bufLen);
×
543
      if (keyBuf == NULL) {
×
544
        taosArrayDestroy(qa);
×
545
        return terrno;
×
546
      }
547
    }
548

549
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
313✔
550
    taosWLockLatch(&pTaskInfo->lock);
313✔
551

552
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
582✔
553
      uint64_t* uid = taosArrayGet(qa, i);
269✔
554
      if (!uid) {
269!
555
        taosMemoryFree(keyBuf);
×
556
        taosArrayDestroy(qa);
×
557
        taosWUnLockLatch(&pTaskInfo->lock);
×
558
        return terrno;
×
559
      }
560
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
269✔
561

562
      if (bufLen > 0) {
269!
563
        if (assignUid) {
×
564
          keyInfo.groupId = keyInfo.uid;
×
565
        } else {
566
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
567
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
568
          if (code != TSDB_CODE_SUCCESS) {
×
569
            taosMemoryFree(keyBuf);
×
570
            taosArrayDestroy(qa);
×
571
            taosWUnLockLatch(&pTaskInfo->lock);
×
572
            return code;
×
573
          }
574
        }
575
      }
576

577
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
269✔
578
      if (code != TSDB_CODE_SUCCESS) {
269!
579
        taosMemoryFree(keyBuf);
×
580
        taosArrayDestroy(qa);
×
581
        taosWUnLockLatch(&pTaskInfo->lock);
×
582
        return code;
×
583
      }
584
    }
585

586
    taosWUnLockLatch(&pTaskInfo->lock);
313✔
587
    if (keyBuf != NULL) {
313!
588
      taosMemoryFree(keyBuf);
×
589
    }
590

591
    taosArrayDestroy(qa);
313✔
592
  } else {  // remove the table id in current list
593
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
3!
594
    taosWLockLatch(&pTaskInfo->lock);
3✔
595
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
3✔
596
    taosWUnLockLatch(&pTaskInfo->lock);
3✔
597
  }
598

599
  return code;
316✔
600
}
601

602
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
866,231✔
603
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
604
                                    int32_t idx, bool* tbGet) {
605
  *tbGet = false;
866,231✔
606

607
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
866,231!
608
    return TSDB_CODE_INVALID_PARA;
×
609
  }
610
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
866,316✔
611

612
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
866,316✔
613
    return TSDB_CODE_SUCCESS;
500,297✔
614
  }
615

616
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
366,034✔
617
  if (!pSchemaInfo) {
366,039!
618
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
619
    return terrno;
×
620
  }
621

622
  *sversion = pSchemaInfo->sw->version;
366,053✔
623
  *tversion = pSchemaInfo->tversion;
366,053✔
624
  *rversion = pSchemaInfo->rversion;
366,053✔
625
  if (pSchemaInfo->dbname) {
366,053!
626
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
366,053✔
627
  } else {
628
    dbName[0] = 0;
×
629
  }
630
  if (pSchemaInfo->tablename) {
366,053!
631
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
366,063✔
632
  } else {
633
    tableName[0] = 0;
×
634
  }
635

636
  *tbGet = true;
366,053✔
637

638
  return TSDB_CODE_SUCCESS;
366,053✔
639
}
640

641
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
500,294✔
642

643
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
644
  if (NULL == pParam) {
×
645
    return;
×
646
  }
647
  freeOperatorParam(pParam, OP_GET_PARAM);
×
648
}
649

650
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
31,186✔
651
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
31,186✔
652
  ((SExecTaskInfo*)tinfo)->paramSet = false;
31,186✔
653
}
31,186✔
654

655
int32_t qExecutorInit(void) {
10,943✔
656
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
10,943✔
657
  return TSDB_CODE_SUCCESS;
10,950✔
658
}
659

660
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
506,306✔
661
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
662
                        EOPTR_EXEC_MODEL model) {
663
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
506,306✔
664
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
506,306✔
665

666
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
506,434✔
667

668
  readHandle->uid = 0;
506,809✔
669
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
506,809✔
670
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
506,622!
671
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
672
    goto _error;
41✔
673
  }
674

675
  if (handle) {
506,742✔
676
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
505,787✔
677
    void*           pSinkManager = NULL;
505,787✔
678
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
505,787✔
679
    if (code != TSDB_CODE_SUCCESS) {
505,815!
680
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
681
      goto _error;
×
682
    }
683

684
    void* pSinkParam = NULL;
505,815✔
685
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
505,815✔
686
    if (code != TSDB_CODE_SUCCESS) {
505,573!
687
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
688
      taosMemoryFree(pSinkManager);
×
689
      goto _error;
×
690
    }
691

692
    SDataSinkNode* pSink = NULL;
505,573✔
693
    if (readHandle->localExec) {
505,573!
694
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
×
695
      if (code != TSDB_CODE_SUCCESS) {
×
696
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
61!
697
               (*pTask)->id.str);
698
        taosMemoryFree(pSinkManager);
61!
699
        goto _error;
×
700
      }
701
    }
702

703
    // pSinkParam has been freed during create sinker.
704
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
505,512✔
705
                              (*pTask)->id.str, pSubplan->processOneBlock);
505,512!
706
    if (code) {
505,540✔
707
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
1!
708
    }
709
  }
710

711
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
506,423✔
712
         tstrerror(code));
713

714
_error:
49,370✔
715
  // if failed to add ref for all tables in this query, abort current query
716
  return code;
506,834✔
717
}
718

719
static void freeBlock(void* param) {
×
720
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
721
  blockDataDestroy(pBlock);
×
722
}
×
723

724
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
588,108✔
725
                     bool processOneBlock) {
726
  int32_t        code = TSDB_CODE_SUCCESS;
588,108✔
727
  int32_t        lino = 0;
588,108✔
728
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
588,108✔
729
  int64_t        threadId = taosGetSelfPthreadId();
588,108✔
730

731
  if (pLocal) {
588,106✔
732
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
570,854✔
733
  }
734

735
  taosArrayClear(pResList);
588,106✔
736

737
  int64_t curOwner = 0;
588,060✔
738
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
588,060!
739
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
740
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
741
    return pTaskInfo->code;
×
742
  }
743

744
  if (pTaskInfo->cost.start == 0) {
588,133✔
745
    pTaskInfo->cost.start = taosGetTimestampUs();
496,320✔
746
  }
747

748
  if (isTaskKilled(pTaskInfo)) {
588,109✔
749
    atomic_store_64(&pTaskInfo->owner, 0);
3✔
750
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
3!
751
    return pTaskInfo->code;
3✔
752
  }
753

754
  // error occurs, record the error code and return to client
755
  int32_t ret = setjmp(pTaskInfo->env);
588,100✔
756
  if (ret != TSDB_CODE_SUCCESS) {
589,342✔
757
    pTaskInfo->code = ret;
1,218✔
758
    (void)cleanUpUdfs();
1,218✔
759

760
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,218✔
761
    atomic_store_64(&pTaskInfo->owner, 0);
1,218✔
762

763
    return pTaskInfo->code;
1,218✔
764
  }
765

766
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
588,124✔
767

768
  int32_t      current = 0;
588,124✔
769
  SSDataBlock* pRes = NULL;
588,124✔
770
  int64_t      st = taosGetTimestampUs();
588,138✔
771

772
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
588,138!
773
    pTaskInfo->paramSet = true;
31,186✔
774
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
31,186✔
775
  } else {
776
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
556,952✔
777
  }
778

779
  QUERY_CHECK_CODE(code, lino, _end);
586,911!
780
  code = blockDataCheck(pRes);
586,911✔
781
  QUERY_CHECK_CODE(code, lino, _end);
586,915!
782

783
  if (pRes == NULL) {
586,915✔
784
    st = taosGetTimestampUs();
112,699✔
785
  }
786

787
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
586,919✔
788
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
586,919!
789
    rowsThreshold = 4096;
584,373✔
790
  }
791

792
  int32_t blockIndex = 0;
586,919✔
793
  while (pRes != NULL) {
1,519,420✔
794
    SSDataBlock* p = NULL;
1,005,081✔
795
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
1,005,081✔
796
      SSDataBlock* p1 = NULL;
785,229✔
797
      code = createOneDataBlock(pRes, true, &p1);
785,229✔
798
      QUERY_CHECK_CODE(code, lino, _end);
785,333!
799

800
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
785,333✔
801
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
785,279!
802
      p = p1;
785,279✔
803
    } else {
804
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
219,911✔
805
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
219,840!
806

807
      p = *(SSDataBlock**)tmp;
219,840✔
808
      code = copyDataBlock(p, pRes);
219,840✔
809
      QUERY_CHECK_CODE(code, lino, _end);
220,260!
810
    }
811

812
    blockIndex += 1;
1,005,539✔
813

814
    current += p->info.rows;
1,005,539✔
815
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
1,005,539!
816
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
817
    void* tmp = taosArrayPush(pResList, &p);
1,005,455✔
818
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,005,455!
819

820
    if (current >= rowsThreshold || processOneBlock) {
1,005,455✔
821
      break;
822
    }
823

824
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
932,892✔
825
    QUERY_CHECK_CODE(code, lino, _end);
932,842!
826
    code = blockDataCheck(pRes);
932,842✔
827
    QUERY_CHECK_CODE(code, lino, _end);
932,501!
828
  }
829

830
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
586,902✔
831
    pTaskInfo->pSubplan->rowsThreshold -= current;
2,546✔
832
  }
833

834
  *hasMore = (pRes != NULL);
586,902✔
835
  uint64_t el = (taosGetTimestampUs() - st);
586,925✔
836

837
  pTaskInfo->cost.elapsedTime += el;
586,925✔
838
  if (NULL == pRes) {
586,925✔
839
    *useconds = pTaskInfo->cost.elapsedTime;
514,329✔
840
  }
841

842
_end:
72,596✔
843
  (void)cleanUpUdfs();
586,925✔
844

845
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
586,944✔
846
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
586,944✔
847
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
848

849
  atomic_store_64(&pTaskInfo->owner, 0);
586,943✔
850
  if (code) {
586,944!
851
    pTaskInfo->code = code;
×
852
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
853
  }
854

855
  return pTaskInfo->code;
586,944✔
856
}
857

858
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
859
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
860
  SArray*        pList = pTaskInfo->pResultBlockList;
×
861
  size_t         num = taosArrayGetSize(pList);
×
862
  for (int32_t i = 0; i < num; ++i) {
×
863
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
864
    if (p) {
×
865
      blockDataDestroy(*p);
×
866
    }
867
  }
868

869
  taosArrayClear(pTaskInfo->pResultBlockList);
×
870
}
×
871

872
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
252,068✔
873
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
252,068✔
874
  int64_t        threadId = taosGetSelfPthreadId();
252,068✔
875
  int64_t        curOwner = 0;
252,068✔
876

877
  *pRes = NULL;
252,068✔
878

879
  // todo extract method
880
  taosRLockLatch(&pTaskInfo->lock);
252,068✔
881
  bool isKilled = isTaskKilled(pTaskInfo);
252,068✔
882
  if (isKilled) {
252,069!
883
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
884

885
    taosRUnLockLatch(&pTaskInfo->lock);
×
886
    return pTaskInfo->code;
×
887
  }
888

889
  if (pTaskInfo->owner != 0) {
252,069!
890
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
891
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
892

893
    taosRUnLockLatch(&pTaskInfo->lock);
×
894
    return pTaskInfo->code;
×
895
  }
896

897
  pTaskInfo->owner = threadId;
252,069✔
898
  taosRUnLockLatch(&pTaskInfo->lock);
252,069✔
899

900
  if (pTaskInfo->cost.start == 0) {
252,069✔
901
    pTaskInfo->cost.start = taosGetTimestampUs();
692✔
902
  }
903

904
  // error occurs, record the error code and return to client
905
  int32_t ret = setjmp(pTaskInfo->env);
252,069✔
906
  if (ret != TSDB_CODE_SUCCESS) {
252,068!
907
    pTaskInfo->code = ret;
×
908
    (void)cleanUpUdfs();
×
909
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
910
    atomic_store_64(&pTaskInfo->owner, 0);
×
911
    return pTaskInfo->code;
×
912
  }
913

914
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
252,068!
915

916
  int64_t st = taosGetTimestampUs();
252,074✔
917
  int32_t code = TSDB_CODE_SUCCESS;
252,074✔
918
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
252,074!
919
    pTaskInfo->paramSet = true;
×
920
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
921
  } else {
922
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
252,074✔
923
  }
924
  if (code) {
252,037!
925
    pTaskInfo->code = code;
×
926
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
927
  }
928

929
  code = blockDataCheck(*pRes);
252,037✔
930
  if (code) {
252,045!
931
    pTaskInfo->code = code;
×
932
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
933
  }
934

935
  uint64_t el = (taosGetTimestampUs() - st);
252,048✔
936

937
  pTaskInfo->cost.elapsedTime += el;
252,048✔
938
  if (NULL == *pRes) {
252,048✔
939
    *useconds = pTaskInfo->cost.elapsedTime;
30,324✔
940
  }
941

942
  (void)cleanUpUdfs();
252,048✔
943

944
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
252,074✔
945
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
252,074✔
946

947
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
252,074!
948
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
949

950
  atomic_store_64(&pTaskInfo->owner, 0);
252,069✔
951
  return pTaskInfo->code;
252,072✔
952
}
953

954
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
183,724✔
955
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
183,724✔
956
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
183,725✔
957
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
183,723✔
958

959
  if (!tmp) {
183,724!
960
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
961
    return terrno;
×
962
  }
963
  return TSDB_CODE_SUCCESS;
183,724✔
964
}
965

966
int32_t stopInfoComp(void const* lp, void const* rp) {
×
967
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
968
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
969

970
  if (key->refId < pInfo->refId) {
×
971
    return -1;
×
972
  } else if (key->refId > pInfo->refId) {
×
973
    return 1;
×
974
  }
975

976
  return 0;
×
977
}
978

979
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
980
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
981
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
982
  if (idx >= 0) {
×
983
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
984
  }
985
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
986
}
×
987

988
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
1,041✔
989
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
1,041✔
990

991
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
1,041✔
992
  for (int32_t i = 0; i < num; ++i) {
1,044✔
993
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
3✔
994
    if (!pStop) {
3!
995
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
996
      continue;
×
997
    }
998
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
3✔
999
    if (pExchangeInfo) {
3!
1000
      int32_t code = tsem_post(&pExchangeInfo->ready);
3✔
1001
      if (code != TSDB_CODE_SUCCESS) {
3!
1002
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1003
      } else {
1004
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
3!
1005
      }
1006
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
3✔
1007
      if (code != TSDB_CODE_SUCCESS) {
3!
1008
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1009
      }
1010
    }
1011
  }
1012

1013
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
1,041✔
1014
}
1,041✔
1015

1016
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
1,041✔
1017
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
1,041✔
1018
  if (pTaskInfo == NULL) {
1,041!
1019
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1020
  }
1021

1022
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
1,041!
1023

1024
  setTaskKilled(pTaskInfo, rspCode);
1,041✔
1025
  qStopTaskOperators(pTaskInfo);
1,041✔
1026

1027
  return TSDB_CODE_SUCCESS;
1,041✔
1028
}
1029

1030
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1031
  int64_t        st = taosGetTimestampMs();
×
1032
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1033
  if (pTaskInfo == NULL) {
×
1034
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1035
  }
1036

1037
  if (waitDuration > 0) {
×
1038
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1039
  } else {
1040
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1041
  }
1042

1043
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1044

1045
  if (waitDuration > 0) {
×
1046
    while (1) {
1047
      taosWLockLatch(&pTaskInfo->lock);
×
1048
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1049
        taosWUnLockLatch(&pTaskInfo->lock);
×
1050

1051
        taosMsleep(200);
×
1052

1053
        int64_t d = taosGetTimestampMs() - st;
×
1054
        if (d >= waitDuration && waitDuration >= 0) {
×
1055
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1056
          return TSDB_CODE_SUCCESS;
×
1057
        }
1058
      } else {  // not running now
1059
        pTaskInfo->code = rspCode;
×
1060
        taosWUnLockLatch(&pTaskInfo->lock);
×
1061
        return TSDB_CODE_SUCCESS;
×
1062
      }
1063
    }
1064
  }
1065

1066
  int64_t et = taosGetTimestampMs() - st;
×
1067
  if (et < waitDuration) {
×
1068
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1069
    return TSDB_CODE_SUCCESS;
×
1070
  }
1071
  return TSDB_CODE_SUCCESS;
×
1072
}
1073

1074
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1075
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1076
  if (NULL == pTaskInfo) {
×
1077
    return false;
×
1078
  }
1079

1080
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1081
}
1082

1083
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
508,773✔
1084
  STaskCostInfo* pSummary = &pTaskInfo->cost;
508,773✔
1085
  int64_t        idleTime = pSummary->start - pSummary->created;
508,773✔
1086

1087
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
508,773✔
1088
  if (pSummary->pRecoder != NULL) {
508,773✔
1089
    qDebug(
361,557✔
1090
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1091
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1092
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1093
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1094
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1095
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1096
  } else {
1097
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
147,216✔
1098
           pSummary->elapsedTime / 1000.0);
1099
  }
1100
}
508,773✔
1101

1102
void qDestroyTask(qTaskInfo_t qTaskHandle) {
521,769✔
1103
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
521,769✔
1104
  if (pTaskInfo == NULL) {
521,769✔
1105
    return;
12,995✔
1106
  }
1107

1108
  if (pTaskInfo->pRoot != NULL) {
508,774!
1109
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
508,776✔
1110
  } else {
1111
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1112
  }
1113

1114
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
508,774✔
1115
  doDestroyTask(pTaskInfo);
508,774✔
1116
}
1117

1118
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
8,328✔
1119
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
8,328✔
1120
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
8,328✔
1121
}
1122

1123
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
977✔
1124
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
977✔
1125
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
977✔
1126

1127
  while (1) {
954✔
1128
    uint16_t type = pOperator->operatorType;
1,931✔
1129
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
1,931✔
1130
      *scanner = pOperator->info;
977✔
1131
      break;
977✔
1132
    } else {
1133
      pOperator = pOperator->pDownstream[0];
954✔
1134
    }
1135
  }
1136
}
977✔
1137

1138
static int32_t getOpratorIntervalInfo(SOperatorInfo* pOperator, int64_t* pWaterMark, SInterval* pInterval,
×
1139
                                      STimeWindow* pLastWindow, TSKEY* pRecInteral) {
1140
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1141
    return getOpratorIntervalInfo(pOperator->pDownstream[0], pWaterMark, pInterval, pLastWindow, pRecInteral);
×
1142
  }
1143
  SStreamScanInfo* pScanOp = (SStreamScanInfo*)pOperator->info;
×
1144
  *pWaterMark = pScanOp->twAggSup.waterMark;
×
1145
  *pInterval = pScanOp->interval;
×
1146
  *pLastWindow = pScanOp->lastScanRange;
×
1147
  *pRecInteral = pScanOp->recalculateInterval;
×
1148
  return TSDB_CODE_SUCCESS;
×
1149
}
1150

1151
void* qExtractReaderFromTmqScanner(void* scanner) {
977✔
1152
  SStreamScanInfo* pInfo = scanner;
977✔
1153
  return (void*)pInfo->tqReader;
977✔
1154
}
1155

1156
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
2,532✔
1157
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,532✔
1158
  return pTaskInfo->streamInfo.schema;
2,532✔
1159
}
1160

1161
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
2,532✔
1162
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,532✔
1163
  return pTaskInfo->streamInfo.tbName;
2,532✔
1164
}
1165

1166
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
2,639✔
1167
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,639✔
1168
  return &pTaskInfo->streamInfo.btMetaRsp;
2,639✔
1169
}
1170

1171
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
32,555✔
1172
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
32,555✔
1173
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
32,555✔
1174
  return 0;
32,555✔
1175
  /*if (code != TSDB_CODE_SUCCESS) {
1176
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1177
    pTaskInfo->code = code;
1178
    T_LONG_JMP(pTaskInfo->env, code);
1179
  }*/
1180
}
1181

1182
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
2,514✔
1183
  memset(pCond, 0, sizeof(SQueryTableDataCond));
2,514✔
1184
  pCond->order = TSDB_ORDER_ASC;
2,514✔
1185
  pCond->numOfCols = pMtInfo->schema->nCols;
2,514✔
1186
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
2,514!
1187
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
2,514!
1188
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
2,514!
1189
    taosMemoryFreeClear(pCond->colList);
×
1190
    taosMemoryFreeClear(pCond->pSlotList);
×
1191
    return terrno;
×
1192
  }
1193

1194
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
2,514✔
1195
  pCond->suid = pMtInfo->suid;
2,514✔
1196
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
2,514✔
1197
  pCond->startVersion = -1;
2,514✔
1198
  pCond->endVersion = sContext->snapVersion;
2,514✔
1199

1200
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
15,834✔
1201
    SColumnInfo* pColInfo = &pCond->colList[i];
13,319✔
1202
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
13,319✔
1203
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
13,319✔
1204
    if (pMtInfo->pExtSchemas != NULL) {
13,319✔
1205
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
162✔
1206
    }
1207
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
13,320✔
1208
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
13,320✔
1209

1210
    pCond->pSlotList[i] = i;
13,320✔
1211
  }
1212

1213
  return TSDB_CODE_SUCCESS;
2,515✔
1214
}
1215

1216
void qStreamSetOpen(qTaskInfo_t tinfo) {
246,990✔
1217
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
246,990✔
1218
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
246,990✔
1219
  pOperator->status = OP_NOT_OPENED;
246,990✔
1220
}
246,990✔
1221

1222
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
29,857✔
1223
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
29,857✔
1224
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
29,857✔
1225
}
29,857✔
1226

1227
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
33,245✔
1228
  int32_t        code = TSDB_CODE_SUCCESS;
33,245✔
1229
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
33,245✔
1230
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
33,245✔
1231

1232
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
33,245✔
1233
  const char*    id = GET_TASKID(pTaskInfo);
33,245✔
1234

1235
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
33,245✔
1236
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
28,994✔
1237
    if (pOperator == NULL || code != 0) {
28,994!
1238
      return code;
×
1239
    }
1240

1241
    SStreamScanInfo* pInfo = pOperator->info;
28,994✔
1242
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
28,994✔
1243
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
28,994✔
1244
    walReaderVerifyOffset(pWalReader, pOffset);
28,994✔
1245
  }
1246
  // if pOffset equal to current offset, means continue consume
1247
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
33,245✔
1248
    return 0;
27,838✔
1249
  }
1250

1251
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
5,407✔
1252
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
2,840✔
1253
    if (pOperator == NULL || code != 0) {
2,840!
1254
      return code;
×
1255
    }
1256

1257
    SStreamScanInfo* pInfo = pOperator->info;
2,840✔
1258
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
2,840✔
1259
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
2,840✔
1260
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
2,840✔
1261

1262
    if (pOffset->type == TMQ_OFFSET__LOG) {
2,840✔
1263
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
2,022✔
1264
      pScanBaseInfo->dataReader = NULL;
2,022✔
1265

1266
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
2,022✔
1267
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
2,022✔
1268
      walReaderVerifyOffset(pWalReader, pOffset);
2,022✔
1269
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
2,022✔
1270
      if (code < 0) {
2,022✔
1271
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
26!
1272
        return code;
26✔
1273
      }
1274
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
818!
1275
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1276
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1277
      int64_t uid = pOffset->uid;
818✔
1278
      int64_t ts = pOffset->ts;
818✔
1279
      int32_t index = 0;
818✔
1280

1281
      // this value may be changed if new tables are created
1282
      taosRLockLatch(&pTaskInfo->lock);
818✔
1283
      int32_t numOfTables = 0;
818✔
1284
      code = tableListGetSize(pTableListInfo, &numOfTables);
818✔
1285
      if (code != TSDB_CODE_SUCCESS) {
818!
1286
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1287
        taosRUnLockLatch(&pTaskInfo->lock);
×
1288
        return code;
676✔
1289
      }
1290

1291
      if (uid == 0) {
818✔
1292
        if (numOfTables != 0) {
798✔
1293
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
122✔
1294
          if (!tmp) {
122!
1295
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1296
            taosRUnLockLatch(&pTaskInfo->lock);
×
1297
            return terrno;
×
1298
          }
1299
          if (tmp) uid = tmp->uid;
122!
1300
          ts = INT64_MIN;
122✔
1301
          pScanInfo->currentTable = 0;
122✔
1302
        } else {
1303
          taosRUnLockLatch(&pTaskInfo->lock);
676✔
1304
          qError("no table in table list, %s", id);
676!
1305
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
676✔
1306
        }
1307
      }
1308
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
142✔
1309

1310
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
142!
1311
             pInfo->pTableScanOp->resultInfo.totalRows);
1312
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
142✔
1313

1314
      // start from current accessed position
1315
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1316
      // position, let's find it from the beginning.
1317
      index = tableListFind(pTableListInfo, uid, 0);
142✔
1318
      taosRUnLockLatch(&pTaskInfo->lock);
142✔
1319

1320
      if (index >= 0) {
142!
1321
        pScanInfo->currentTable = index;
142✔
1322
      } else {
1323
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1324
               numOfTables, pScanInfo->currentTable, id);
1325
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1326
      }
1327

1328
      STableKeyInfo keyInfo = {.uid = uid};
142✔
1329
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
142✔
1330

1331
      // let's start from the next ts that returned to consumer.
1332
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
142!
1333
        pScanBaseInfo->cond.twindows.skey = ts;
×
1334
      } else {
1335
        pScanBaseInfo->cond.twindows.skey = ts + 1;
142✔
1336
      }
1337
      pScanInfo->scanTimes = 0;
142✔
1338

1339
      if (pScanBaseInfo->dataReader == NULL) {
142✔
1340
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
122✔
1341
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1342
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
122✔
1343
        if (code != TSDB_CODE_SUCCESS) {
122!
1344
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1345
          return code;
×
1346
        }
1347

1348
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
122!
1349
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1350
      } else {
1351
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
20✔
1352
        if (code != TSDB_CODE_SUCCESS) {
20!
1353
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1354
          return code;
×
1355
        }
1356

1357
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
20✔
1358
        if (code != TSDB_CODE_SUCCESS) {
20!
1359
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1360
          return code;
×
1361
        }
1362
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
20!
1363
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1364
      }
1365

1366
      // restore the key value
1367
      pScanBaseInfo->cond.twindows.skey = oldSkey;
142✔
1368
    } else {
1369
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1370
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1371
    }
1372

1373
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1374
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
2,567✔
1375
      SStreamRawScanInfo* pInfo = pOperator->info;
2,518✔
1376
      SSnapContext*       sContext = pInfo->sContext;
2,518✔
1377
      SOperatorInfo*      p = NULL;
2,518✔
1378

1379
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
2,518✔
1380
      if (code != 0) {
2,518!
1381
        return code;
×
1382
      }
1383

1384
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
2,518✔
1385

1386
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
2,518!
1387
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1388
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1389
      }
1390

1391
      SMetaTableInfo mtInfo = {0};
2,517✔
1392
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
2,517✔
1393
      if (code != 0) {
2,518!
1394
        destroyMetaTableInfo(&mtInfo);
1395
        return code;
×
1396
      }
1397
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,518✔
1398
      pInfo->dataReader = NULL;
2,518✔
1399

1400
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,518✔
1401
      tableListClear(pTableListInfo);
2,518✔
1402

1403
      if (mtInfo.uid == 0) {
2,518✔
1404
        destroyMetaTableInfo(&mtInfo);
1405
        goto end;  // no data
4✔
1406
      }
1407

1408
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
2,514✔
1409
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
2,514✔
1410
      if (code != TSDB_CODE_SUCCESS) {
2,514!
1411
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1412
        destroyMetaTableInfo(&mtInfo);
1413
        return code;
×
1414
      }
1415
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
2,514!
1416
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1417
      } else {
1418
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
2,514✔
1419
      }
1420

1421
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
2,514✔
1422
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1423
        destroyMetaTableInfo(&mtInfo);
1424
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1425
        return code;
×
1426
      }
1427

1428
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
2,513✔
1429
      if (!pList) {
2,513!
1430
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1431
        destroyMetaTableInfo(&mtInfo);
1432
        return code;
×
1433
      }
1434
      int32_t size = 0;
2,513✔
1435
      code = tableListGetSize(pTableListInfo, &size);
2,513✔
1436
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1437
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1438
        destroyMetaTableInfo(&mtInfo);
1439
        return code;
×
1440
      }
1441

1442
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
2,513✔
1443
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
2,513✔
1444
      if (code != TSDB_CODE_SUCCESS) {
2,514!
1445
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1446
        destroyMetaTableInfo(&mtInfo);
1447
        return code;
×
1448
      }
1449

1450
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,514✔
1451
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
2,514✔
1452
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1453
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
2,514✔
1454
      pTaskInfo->streamInfo.schema = mtInfo.schema;
2,514✔
1455
      taosMemoryFreeClear(mtInfo.pExtSchemas);
2,514!
1456

1457
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
2,514!
1458
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
49✔
1459
      SStreamRawScanInfo* pInfo = pOperator->info;
18✔
1460
      SSnapContext*       sContext = pInfo->sContext;
18✔
1461
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
18✔
1462
      if (code != 0) {
18!
1463
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1464
        return code;
×
1465
      }
1466
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
18!
1467
             id);
1468
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
31!
1469
      SStreamRawScanInfo* pInfo = pOperator->info;
31✔
1470
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
31✔
1471
      pInfo->dataReader = NULL;
31✔
1472
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
31!
1473
    }
1474
  }
1475

1476
end:
×
1477
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
4,705✔
1478
  return 0;
4,705✔
1479
}
1480

1481
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
364,785✔
1482
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
364,785✔
1483
  if (pMsg->info.ahandle == NULL) {
364,785!
1484
    qError("pMsg->info.ahandle is NULL");
×
1485
    return;
×
1486
  }
1487

1488
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
364,785✔
1489
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1490

1491
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
364,787✔
1492

1493
  if (pMsg->contLen > 0) {
364,787✔
1494
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
364,780!
1495
    if (buf.pData == NULL) {
364,771!
1496
      pMsg->code = terrno;
×
1497
    } else {
1498
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
364,771✔
1499
    }
1500
  }
1501

1502
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
364,778✔
1503
  rpcFreeCont(pMsg->pCont);
364,847✔
1504
  destroySendMsgInfo(pSendInfo);
364,818✔
1505
}
1506

1507
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1508
  int32_t        code = TSDB_CODE_SUCCESS;
×
1509
  int32_t        lino = 0;
×
1510
  SExecTaskInfo* pTaskInfo = tinfo;
×
1511
  SArray*        plist = NULL;
×
1512

1513
  code = getTableListInfo(pTaskInfo, &plist);
×
1514
  if (code || plist == NULL) {
×
1515
    return NULL;
×
1516
  }
1517

1518
  // only extract table in the first elements
1519
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1520

1521
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1522
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1523

1524
  int32_t numOfTables = 0;
×
1525
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1526
  QUERY_CHECK_CODE(code, lino, _end);
×
1527

1528
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1529
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1530
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1531
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1532
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1533
  }
1534

1535
  taosArrayDestroy(plist);
×
1536

1537
_end:
×
1538
  if (code != TSDB_CODE_SUCCESS) {
×
1539
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1540
    T_LONG_JMP(pTaskInfo->env, code);
×
1541
  }
1542
  return pUidList;
×
1543
}
1544

1545
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
11,160✔
1546
  int32_t        code = TSDB_CODE_SUCCESS;
11,160✔
1547
  int32_t        lino = 0;
11,160✔
1548
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
11,160✔
1549

1550
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
11,160!
1551
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1552
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1553

1554
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1555
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1556
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
11,160✔
1557
    STableScanInfo* pScanInfo = pOperator->info;
5,580✔
1558

1559
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
5,580✔
1560
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
5,581!
1561
  } else {
1562
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
5,580!
1563
      code = extractTableList(pList, pOperator->pDownstream[0]);
5,580✔
1564
    }
1565
  }
1566

1567
_end:
×
1568
  if (code != TSDB_CODE_SUCCESS) {
11,162!
1569
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1570
  }
1571
  return code;
11,162✔
1572
}
1573

1574
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
5,580✔
1575
  if (pList == NULL) {
5,580!
1576
    return TSDB_CODE_INVALID_PARA;
×
1577
  }
1578

1579
  *pList = NULL;
5,580✔
1580
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
5,580✔
1581
  if (pArray == NULL) {
5,581!
1582
    return terrno;
×
1583
  }
1584

1585
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
5,581✔
1586
  if (code == 0) {
5,581!
1587
    *pList = pArray;
5,581✔
1588
  } else {
1589
    taosArrayDestroy(pArray);
×
1590
  }
1591
  return code;
5,581✔
1592
}
1593

1594
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1595
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1596
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1597
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1598
  }
1599
  return 0;
×
1600
}
1601

1602
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1603
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1604
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1605
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1606
  }
1607
  return 0;
×
1608
}
1609

1610
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1611
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1612

1613
  int32_t code = pTaskInfo->code;
×
1614
  pTaskInfo->code = 0;
×
1615
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1616
}
×
1617

1618
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1619
  int32_t code = 0;
×
1620
  return code;
×
1621
}
1622

1623
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1624
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1625
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1626
  return code;
×
1627
}
1628

1629
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
162,555✔
1630
  int32_t code = 0;
162,555✔
1631

1632
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
162,555✔
1633
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
162,541✔
1634

1635
  if (pOper->fpSet.resetStateFn) {
162,539✔
1636
    code = pOper->fpSet.resetStateFn(pOper);
162,537✔
1637
  }
1638
  pOper->status = OP_NOT_OPENED;
162,554✔
1639
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
284,292!
1640
    code = clearStatesForOperator(pOper->pDownstream[i]);
121,723✔
1641
  }
1642
  return code;
162,569✔
1643
}
1644

1645
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
40,847✔
1646
  int32_t        code = 0;
40,847✔
1647
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
40,847✔
1648
  SOperatorInfo* pOper = pTaskInfo->pRoot;
40,847✔
1649
  pTaskInfo->code = TSDB_CODE_SUCCESS;
40,847✔
1650
  code = clearStatesForOperator(pOper);
40,847✔
1651
  return code;
40,845✔
1652
}
1653

1654
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
51,618✔
1655
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
51,618✔
1656
  int64_t        threadId = taosGetSelfPthreadId();
51,618✔
1657
  int64_t        curOwner = 0;
51,618✔
1658

1659
  *ppRes = NULL;
51,618✔
1660

1661
  // todo extract method
1662
  taosRLockLatch(&pTaskInfo->lock);
51,618✔
1663
  bool isKilled = isTaskKilled(pTaskInfo);
51,619✔
1664
  if (isKilled) {
51,619!
1665
    // clearStreamBlock(pTaskInfo->pRoot);
1666
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1667

1668
    taosRUnLockLatch(&pTaskInfo->lock);
×
1669
    return pTaskInfo->code;
×
1670
  }
1671

1672
  if (pTaskInfo->owner != 0) {
51,619!
1673
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1674
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1675

1676
    taosRUnLockLatch(&pTaskInfo->lock);
×
1677
    return pTaskInfo->code;
×
1678
  }
1679

1680
  pTaskInfo->owner = threadId;
51,619✔
1681
  taosRUnLockLatch(&pTaskInfo->lock);
51,619✔
1682

1683
  if (pTaskInfo->cost.start == 0) {
51,619✔
1684
    pTaskInfo->cost.start = taosGetTimestampUs();
792✔
1685
  }
1686

1687
  // error occurs, record the error code and return to client
1688
  int32_t ret = setjmp(pTaskInfo->env);
51,619✔
1689
  if (ret != TSDB_CODE_SUCCESS) {
69,059✔
1690
    pTaskInfo->code = ret;
17,443✔
1691
    (void)cleanUpUdfs();
17,443✔
1692
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
17,443!
1693
    atomic_store_64(&pTaskInfo->owner, 0);
17,443✔
1694
    return pTaskInfo->code;
17,443✔
1695
  }
1696

1697
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
51,616✔
1698

1699
  int64_t st = taosGetTimestampUs();
51,619✔
1700

1701
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
51,619✔
1702
  if (code) {
34,175!
1703
    pTaskInfo->code = code;
×
1704
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1705
  } else {
1706
    *finished = *ppRes == NULL;
34,175✔
1707
    code = blockDataCheck(*ppRes);
34,175✔
1708
  }
1709
  if (code) {
34,173!
1710
    pTaskInfo->code = code;
×
1711
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1712
  }
1713

1714
  uint64_t el = (taosGetTimestampUs() - st);
34,172✔
1715

1716
  pTaskInfo->cost.elapsedTime += el;
34,172✔
1717
  if (NULL == *ppRes) {
34,172✔
1718
    *useconds = pTaskInfo->cost.elapsedTime;
22,666✔
1719
  }
1720

1721
  (void)cleanUpUdfs();
34,172✔
1722

1723
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
34,176✔
1724
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
34,176✔
1725

1726
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
34,176✔
1727
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1728

1729
  atomic_store_64(&pTaskInfo->owner, 0);
34,176✔
1730
  return pTaskInfo->code;
34,176✔
1731
}
1732

1733
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1734
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1735
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1736
// }
1737

1738
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
13,095✔
1739
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1740
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1741
  STableListInfo* pList = tableListCreate();
13,095✔
1742
  if (pList == NULL) {
13,094!
1743
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1744
    return terrno;
×
1745
  }
1746

1747
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
13,094✔
1748
  SReadHandle    pHandle = {.vnode = pVnode};
13,094✔
1749
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
13,094✔
1750

1751
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
13,094✔
1752
  if (code != 0) {
13,098!
1753
    tableListDestroy(pList);
×
1754
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1755
    return code;
×
1756
  }
1757
  *pTableListInfo = pList;
13,098✔
1758
  return 0;
13,098✔
1759
}
1760

1761
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
32,735✔
1762
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
32,735!
1763
    return TSDB_CODE_INVALID_PARA;
×
1764
  }
1765
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
32,735✔
1766
    *size = 0;
22,843✔
1767
    *pKeyInfo = NULL;
22,843✔
1768
    return 0;
22,843✔
1769
  }
1770
  if (currentGroupId == -1) {
9,892✔
1771
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
903✔
1772
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
903✔
1773
    return 0;
903✔
1774
  }
1775
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
8,989✔
1776
}
1777

1778
int32_t  qStreamSetTableList(void** pTableListInfo, uint64_t uid, uint64_t gid){
7,275✔
1779
  if (*pTableListInfo == NULL) {
7,275✔
1780
    *pTableListInfo = tableListCreate();
1,191✔
1781
    if (*pTableListInfo == NULL) {
1,191!
1782
      return terrno;
×
1783
    }
1784
  }
1785
  return tableListAddTableInfo(*pTableListInfo, uid, gid);
7,275✔
1786
}
1787

1788
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
28,306✔
1789
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
28,306✔
1790
    return 0;
22,700✔
1791
  }
1792
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
8,412✔
1793
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
8,039✔
1794

1795
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
8,039✔
1796
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
8,039!
1797
      return i;
5,233✔
1798
    }
1799
  }
1800
  return -1;
373✔
1801
}
1802

1803
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
15,131✔
1804

1805
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
14,308✔
1806

1807
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
4,234✔
1808
void    qStreamSetTableListGroupNum(const void* pTableList, int32_t groupNum) {((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; }
1,191✔
1809
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
790✔
1810

1811
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
4,235✔
1812

1813
void streamDestroyExecTask(qTaskInfo_t tInfo) {
6,330✔
1814
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
6,330✔
1815
  qDestroyTask(tInfo);
6,330✔
1816
}
6,330✔
1817

1818
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
1,951✔
1819
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
1,951✔
1820
}
1821

1822
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
1,976✔
1823
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1824
  int32_t      code = 0;
1,976✔
1825
  SNode*       pNode = 0;
1,976✔
1826
  SNodeList*   pList = NULL;
1,976✔
1827
  SExprInfo*   pExprInfo = NULL;
1,976✔
1828
  int32_t      numOfExprs = 1;
1,976✔
1829
  int32_t*     offset = 0;
1,976✔
1830
  STargetNode* pTargetNode = NULL;
1,976✔
1831
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
1,976✔
1832
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
1,976!
1833

1834
  if (code == 0) {
1,976!
1835
    pTargetNode->dataBlockId = 0;
1,976✔
1836
    pTargetNode->pExpr = pNode;
1,976✔
1837
    pTargetNode->slotId = 0;
1,976✔
1838
  }
1839
  if (code == 0) {
1,976!
1840
    code = nodesMakeList(&pList);
1,976✔
1841
  }
1842
  if (code == 0) {
1,976!
1843
    code = nodesListAppend(pList, (SNode*)pTargetNode);
1,976✔
1844
  }
1845
  if (code == 0) {
1,976!
1846
    pNode = NULL;
1,976✔
1847
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
1,976✔
1848
  }
1849

1850
  if (code == 0) {
1,976!
1851
    const char* pVal = NULL;
1,976✔
1852
    int32_t     len = 0;
1,976✔
1853
    SNode*      pSclNode = NULL;
1,976✔
1854
    switch (pExprInfo->pExpr->nodeType) {
1,976!
1855
      case QUERY_NODE_FUNCTION:
1,976✔
1856
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
1,976✔
1857
        break;
1,976✔
1858
      case QUERY_NODE_OPERATOR:
×
1859
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1860
        break;
×
1861
      default:
×
1862
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1863
        break;
×
1864
    }
1865
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
1,976✔
1866
    SSDataBlock block = {0};
1,976✔
1867
    block.info.rows = 1;
1,976✔
1868
    SSDataBlock* pBlock = &block;
1,976✔
1869
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
1,976✔
1870
    if (tmp == NULL) {
1,976!
1871
      code = terrno;
×
1872
    }
1873
    if (code == 0) {
1,976!
1874
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
1,976✔
1875
    }
1876
    taosArrayDestroy(pBlockList);
1,975✔
1877
  }
1878
  nodesDestroyList(pList);
1,976✔
1879
  destroyExprInfo(pExprInfo, numOfExprs);
1,976✔
1880
  taosMemoryFreeClear(pExprInfo);
1,976!
1881
  return code;
1,975✔
1882
}
1883

1884
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
11,155✔
1885
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,155✔
1886
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
11,155✔
1887
  int32_t        code = 0;
11,155✔
1888
  SNode*         pNode = NULL;
11,155✔
1889
  if (!pForceOutputCols) return 0;
11,155✔
1890
  if (!*pRes) {
25!
1891
    code = createDataBlock(pRes);
25✔
1892
  }
1893

1894
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
25!
1895
    int32_t idx = 0;
25✔
1896
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
99✔
1897
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
74✔
1898
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
74✔
1899
      colInfo.info.precision = pCol->type.precision;
74✔
1900
      colInfo.info.scale = pCol->type.scale;
74✔
1901
      code = blockDataAppendColInfo(*pRes, &colInfo);
74✔
1902
      if (code != 0) break;
74!
1903
    }
1904
  }
1905

1906
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
25✔
1907
  if (code != TSDB_CODE_SUCCESS) {
25!
1908
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1909
    return code;
×
1910
  }
1911

1912
  // loop all exprs for force output, execute all exprs
1913
  int32_t idx = 0;
25✔
1914
  int32_t rowIdx = (*pRes)->info.rows;
25✔
1915
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
25✔
1916
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
25✔
1917
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
99✔
1918
    SScalarParam   dst = {0};
74✔
1919
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
74✔
1920
    code = nodesStringToNode(pCol->expr, &pNode);
74✔
1921
    if (code != 0) break;
74!
1922
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
74✔
1923
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
74✔
1924
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
49✔
1925
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
49✔
1926
    } else {
1927
      dst.columnData = pInfo;
25✔
1928
      dst.numOfRows = rowIdx;
25✔
1929
      dst.colAlloced = false;
25✔
1930
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
25✔
1931
    }
1932
    ++idx;
74✔
1933
    // TODO sclFreeParam(&dst);
1934
    nodesDestroyNode(pNode);
74✔
1935
    if (code != 0) break;
74!
1936
  }
1937
  if (code == TSDB_CODE_SUCCESS) {
25!
1938
    (*pRes)->info.rows++;
25✔
1939
  }
1940
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
25✔
1941
  return code;
25✔
1942
}
1943

1944
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
709✔
1945
  int32_t      code = 0;
709✔
1946
  const char*  pVal = NULL;
709✔
1947
  SScalarParam dst = {0};
709✔
1948
  int32_t      len = 0;
709✔
1949
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
709✔
1950
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
709✔
1951
  // execute the expr
1952
  switch (pExpr->type) {
709!
1953
    case QUERY_NODE_VALUE: {
×
1954
      SValueNode* pValue = (SValueNode*)pExpr;
×
1955
      int32_t     type = pValue->node.resType.type;
×
1956
      if (!IS_STR_DATA_TYPE(type)) {
×
1957
        qError("invalid sub tb expr with non-str type");
×
1958
        code = TSDB_CODE_INVALID_PARA;
×
1959
        break;
×
1960
      }
1961
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
1962
      if (pTmp == NULL) {
×
1963
        qError("invalid sub tb expr with null value");
×
1964
        code = TSDB_CODE_INVALID_PARA;
×
1965
        break;
×
1966
      }
1967
      pVal = varDataVal(pTmp);
×
1968
      len = varDataLen(pTmp);
×
1969
    } break;
×
1970
    case QUERY_NODE_FUNCTION: {
709✔
1971
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
709✔
1972
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
709!
1973
        qError("invalid sub tb expr with non-str type func");
×
1974
        code = TSDB_CODE_INVALID_PARA;
×
1975
        break;
×
1976
      }
1977
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
709!
1978
      if (!pCol) {
709!
1979
        code = terrno;
×
1980
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
1981
        break;
×
1982
      }
1983

1984
      pCol->hasNull = true;
709✔
1985
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
709✔
1986
      pCol->info.colId = 0;
709✔
1987
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
709✔
1988
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
709✔
1989
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
709✔
1990
      code = colInfoDataEnsureCapacity(pCol, 1, true);
709✔
1991
      if (code != 0) {
709!
1992
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
1993
        taosMemoryFree(pCol);
×
1994
        break;
×
1995
      }
1996
      dst.columnData = pCol;
709✔
1997
      dst.numOfRows = 1;
709✔
1998
      dst.colAlloced = true;
709✔
1999
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
709✔
2000
      if (colDataIsNull_var(dst.columnData, 0)) {
708!
2001
        qInfo("invalid sub tb expr with null value");
×
2002
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
2003
      }
2004
      if (code == 0) {
708!
2005
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
708✔
2006
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
708✔
2007
      }
2008
    } break;
708✔
2009
    default:
×
2010
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2011
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2012
      break;
×
2013
  }
2014
  if (code == 0) {
708!
2015
    if (!pVal || len == 0) {
708!
2016
      qError("tbname generated with no characters which is not allowed");
×
2017
      code = TSDB_CODE_INVALID_PARA;
×
2018
    }
2019
    if(len > TSDB_TABLE_NAME_LEN - 1) {
708✔
2020
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
2!
2021
      len = TSDB_TABLE_NAME_LEN - 1;
2✔
2022
    }
2023

2024
    memcpy(tbname, pVal, len);
708✔
2025
    tbname[len] = '\0';  // ensure null terminated
708✔
2026
    if (NULL != strchr(tbname, '.')) {
708!
2027
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2028
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2029
    }
2030
  }
2031
  // TODO free dst
2032
  sclFreeParam(&dst);
708✔
2033
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
709✔
2034
  return code;
709✔
2035
}
2036

2037
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
772✔
2038
  if (pParam) {
772!
2039
    if (pParam->tbname) {
772!
2040
      taosMemFree(pParam->tbname);
772✔
2041
      pParam->tbname = NULL;
772✔
2042
    }
2043
    if (pParam->stbname) {
772!
2044
      taosMemFree(pParam->stbname);
772✔
2045
      pParam->stbname = NULL;
772✔
2046
    }
2047
    if (pParam->dbFName) {
772!
2048
      taosMemFree(pParam->dbFName);
772✔
2049
      pParam->dbFName = NULL;
772✔
2050
    }
2051
    if (pParam->pFields) {
772!
2052
      taosArrayDestroy(pParam->pFields);
772✔
2053
      pParam->pFields = NULL;
772✔
2054
    }
2055
    if (pParam->pTagFields) {
772✔
2056
      taosArrayDestroy(pParam->pTagFields);
494✔
2057
      pParam->pTagFields = NULL;
494✔
2058
    }
2059
    taosMemFree(pParam);
772✔
2060
  }
2061
}
772✔
2062

2063
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
771✔
2064
  int32_t code = 0, lino = 0;
771✔
2065
  if (ppDst == NULL || pSrc == NULL) {
771!
2066
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2067
  }
2068
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
771!
2069
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
772!
2070

2071
  (*ppDst)->suid = pSrc->suid;
772✔
2072
  (*ppDst)->sver = pSrc->sver;
772✔
2073
  (*ppDst)->tbType = pSrc->tbType;
772✔
2074
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
772!
2075
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
772!
2076

2077
  if (pSrc->stbname) {
772!
2078
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
772!
2079
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
772!
2080
  }
2081

2082
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
772!
2083
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
772!
2084

2085
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
772✔
2086

2087
  if (pSrc->pFields && pSrc->pFields->size > 0) {
772!
2088
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
772✔
2089
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
772!
2090
  } else {
2091
    (*ppDst)->pFields = NULL;
×
2092
  }
2093
  
2094
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
772!
2095
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
494✔
2096
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
494!
2097
  } else {
2098
    (*ppDst)->pTagFields = NULL;
278✔
2099
  }
2100

2101
_exit:
772✔
2102

2103
  if (code != 0) {
772!
2104
    if (*ppDst) {
×
2105
      destroyStreamInserterParam(*ppDst);
×
2106
      *ppDst = NULL;
×
2107
    }
2108
    
2109
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2110
  }
2111
  return code;
772✔
2112
}
2113

2114
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
1✔
2115
  return doDropStreamTable(pMsgCb, pOutput, pReq);
1✔
2116
}
2117

2118
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2119
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2120
}
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