• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 hits per line

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

68.91
/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 "tudf.h"
37
#include "wal.h"
38

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

43
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
702,939✔
44
  gExecInfo.dnode = pDnode;
702,939✔
45
  gExecInfo.getMnode = getMnode;
702,939✔
46
  gExecInfo.getDnodeId = getDnodeId;
702,939✔
47
  return;
702,939✔
48
}
49

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

59
static void cleanupRefPool() {
652,902✔
60
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
652,902✔
61
  taosCloseRef(ref);
652,902✔
62
}
652,902✔
63

64
static void initRefPool() {
652,902✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
652,902✔
66
  (void)atexit(cleanupRefPool);
652,902✔
67
}
652,902✔
68

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

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

87
    SStreamScanInfo* pInfo = pOperator->info;
×
88

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,751,126✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,751,126✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,751,871✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,292,215✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,292,215✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,292,122✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,292,122✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
55,496✔
158
        if (code) {
55,496✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,459,910✔
166
  }
167

168
  return 0;
10,728,042✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
10,727,527✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
10,727,527✔
173
  pTaskInfo->id.queryId = queryId;
10,727,527✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
10,727,714✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,727,949✔
178
}
179

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

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

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

194
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
195

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

203
  return code;
×
204
}
205

206
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
124,116✔
207
  if (msg == NULL) {  // create raw scan
124,116✔
208
    SExecTaskInfo* pTaskInfo = NULL;
22,395✔
209

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

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

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

226
  SSubplan* pPlan = NULL;
101,721✔
227
  int32_t   code = qStringToSubplan(msg, &pPlan);
102,163✔
228
  if (code != TSDB_CODE_SUCCESS) {
102,163✔
229
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

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

242
  return pTaskInfo;
102,163✔
243
}
244

245
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
714,524✔
246
  if (streamInserterParam == NULL) {
714,524✔
247
    return TSDB_CODE_SUCCESS;
393,921✔
248
  }
249

250
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
320,603✔
251
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
252
    return TSDB_CODE_INVALID_PARA;
×
253
  }
254

255
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
321,399✔
256
    stError("insertParam: invalid db/table name");
×
257
    return TSDB_CODE_INVALID_PARA;
×
258
  }
259

260
  if (streamInserterParam->suid <= 0 &&
321,001✔
261
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
104,452✔
262
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
398✔
263
    return TSDB_CODE_INVALID_PARA;
×
264
  }
265

266
  return TSDB_CODE_SUCCESS;
320,603✔
267
}
268

269
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
714,524✔
270
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
271
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
272
  if (pSubplan == NULL || pTaskInfo == NULL) {
714,524✔
273
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
×
274
    nodesDestroyNode((SNode *)pSubplan);
×
275
    return TSDB_CODE_INVALID_PARA;
×
276
  }
277
  int32_t lino = 0;
714,524✔
278
  int32_t code = checkInsertParam(streamInserterParam);
714,524✔
279
  if (code != TSDB_CODE_SUCCESS) {
714,524✔
280
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
281
    nodesDestroyNode((SNode *)pSubplan);
×
282
    return code;
×
283
  }
284
  SInserterParam* pInserterParam = NULL;
714,524✔
285
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
714,524✔
286
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
714,524✔
287
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
714,922✔
288

289
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
714,922✔
290
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
714,518✔
291
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
407✔
292
    goto _error;
407✔
293
  }
294

295
  if (streamInserterParam) {
714,111✔
296
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
321,001✔
297
    void*           pSinkManager = NULL;
321,001✔
298
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
320,593✔
299
    if (code != TSDB_CODE_SUCCESS) {
319,793✔
300
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
301
      goto _error;
×
302
    }
303

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

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

324
_error:
145,512✔
325

326
  if (code != TSDB_CODE_SUCCESS) {
714,922✔
327
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
407✔
328
    if (pInserterParam != NULL) {
407✔
329
      taosMemoryFree(pInserterParam);
×
330
    }
331
  }
332
  return code;
714,922✔
333
}
334

335
bool qNeedReset(qTaskInfo_t pInfo) {
5,729,801✔
336
  if (pInfo == NULL) {
5,729,801✔
337
    return false;
×
338
  }
339
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,729,801✔
340
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,729,801✔
341
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,729,801✔
342
    return false;
8,396✔
343
  }
344
  int32_t node = nodeType(pOperator->pPhyNode);
5,721,405✔
345
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,460,242✔
346
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,181,647✔
347
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
348
}
349

350
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
5,256,566✔
351
  if (pHandle == NULL || pScanBaseInfo == NULL) {
5,256,566✔
352
    return;
×
353
  }
354

355
  pScanBaseInfo->readHandle.uid = pHandle->uid;
5,256,566✔
356
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
5,256,566✔
357
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
5,256,973✔
358
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
5,256,973✔
359
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
5,256,973✔
360
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
5,256,566✔
361
}
362

363
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
5,721,405✔
364
  if (pInfo == NULL) {
5,721,405✔
365
    return TSDB_CODE_INVALID_PARA;
×
366
  }
367
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,721,405✔
368
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,721,405✔
369

370
  void*           info = pOperator->info;
5,721,405✔
371
  STableScanBase* pScanBaseInfo = NULL;
5,721,405✔
372

373
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,721,405✔
374
    pScanBaseInfo = &((STableScanInfo*)info)->base;
260,756✔
375
    setReadHandle(handle, pScanBaseInfo);
260,756✔
376
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,460,242✔
377
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,995,810✔
378
    setReadHandle(handle, pScanBaseInfo);
4,995,810✔
379
  }
380

381
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
5,721,003✔
382
  handle->winRange.ekey);
383
  return pOperator->fpSet.resetStateFn(pOperator);
5,721,405✔
384
}
385

386
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
714,922✔
387
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
388
  if (msg == NULL) {
714,922✔
389
    return TSDB_CODE_INVALID_PARA;
×
390
  }
391

392
  *pTaskInfo = NULL;
714,922✔
393

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

409
  return code;
714,515✔
410
}
411

412
typedef struct {
413
  tb_uid_t tableUid;
414
  tb_uid_t childUid;
415
  int8_t   check;
416
} STqPair;
417

418
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
55,257✔
419
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
420
  int32_t code = TSDB_CODE_SUCCESS;
55,257✔
421
  int32_t lino = 0;
55,257✔
422
  int8_t  locked = 0;
55,257✔
423
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
55,257✔
424

425
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
55,257✔
426

427
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
55,257✔
428
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
55,257✔
429

430
  int32_t numOfUids = taosArrayGetSize(tableIdList);
55,257✔
431
  if (numOfUids == 0) {
55,257✔
432
    (*ppArrayRes) = qa;
×
433
    goto _error;
×
434
  }
435

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
55,257✔
437

438
  uint64_t suid = 0;
55,257✔
439
  uint64_t uid = 0;
55,257✔
440
  int32_t  type = 0;
55,257✔
441
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
55,257✔
442

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

447
  locked = 1;
55,257✔
448
  for (int32_t i = 0; i < numOfUids; ++i) {
110,802✔
449
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
55,545✔
450
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
55,545✔
451

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

458
    tDecoderClear(&mr.coder);
55,545✔
459

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

477
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
55,545✔
478
    if (pScanInfo->pTagCond != NULL) {
55,545✔
479
      // tb_uid_t id = mr.me.uid;
480
      item.check = 1;
47,910✔
481
    }
482
    if (taosArrayPush(tUid, &item) == NULL) {
55,545✔
483
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
484
    }
485
  }
486

487
  pAPI->metaReaderFn.clearReader(&mr);
55,257✔
488
  locked = 0;
55,257✔
489

490
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
110,802✔
491
    bool     qualified = false;
55,545✔
492
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
55,545✔
493
    if (t == NULL) {
55,545✔
494
      continue;
×
495
    }
496

497
    if (t->check == 1) {
55,545✔
498
      STableKeyInfo info = {.groupId = 0, .uid = t->childUid};
47,910✔
499
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
47,910✔
500
      if (code != TSDB_CODE_SUCCESS) {
47,910✔
501
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
502
        continue;
×
503
      }
504

505
      if (!qualified) {
47,910✔
506
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", info.uid, idstr);
23,955✔
507
        continue;
23,955✔
508
      }
509
    }
510

511
    void* tmp = taosArrayPush(qa, &t->tableUid);
31,590✔
512
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
31,590✔
513
  }
514

515
  // handle multiple partition
516

517
_end:
55,257✔
518

519
  if (locked) {
55,257✔
520
    pAPI->metaReaderFn.clearReader(&mr);
×
521
  }
522
  (*ppArrayRes) = qa;
55,257✔
523
_error:
55,257✔
524

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

532
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
55,877✔
533
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
55,877✔
534
  const char*    id = GET_TASKID(pTaskInfo);
55,877✔
535
  int32_t        code = 0;
55,877✔
536

537
  if (isAdd) {
55,877✔
538
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
55,257✔
539
  }
540

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

548
  SStreamScanInfo* pScanInfo = pInfo->info;
55,877✔
549
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
55,877✔
550
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
111,614✔
551
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
55,737✔
552
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
55,737✔
553
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
55,737✔
554
    }
555
  }
556

557
  if (isAdd) {  // add new table id
55,877✔
558
    SArray* qa = NULL;
55,257✔
559
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
55,257✔
560
    if (code != TSDB_CODE_SUCCESS) {
55,257✔
561
      taosArrayDestroy(qa);
×
562
      return code;
×
563
    }
564
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
55,257✔
565
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
55,257✔
566
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
55,257✔
567

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

580
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
55,257✔
581
    taosWLockLatch(&pTaskInfo->lock);
55,257✔
582

583
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
86,847✔
584
      uint64_t* uid = taosArrayGet(qa, i);
31,590✔
585
      if (!uid) {
31,590✔
586
        taosMemoryFree(keyBuf);
×
587
        taosArrayDestroy(qa);
×
588
        taosWUnLockLatch(&pTaskInfo->lock);
×
589
        return terrno;
×
590
      }
591
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
31,590✔
592

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

608
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
31,590✔
609
      if (code != TSDB_CODE_SUCCESS) {
31,590✔
610
        taosMemoryFree(keyBuf);
×
611
        taosArrayDestroy(qa);
×
612
        taosWUnLockLatch(&pTaskInfo->lock);
×
613
        return code;
×
614
      }
615
    }
616

617
    taosWUnLockLatch(&pTaskInfo->lock);
55,257✔
618
    if (keyBuf != NULL) {
55,257✔
619
      taosMemoryFree(keyBuf);
×
620
    }
621

622
    taosArrayDestroy(qa);
55,257✔
623
  } else {  // remove the table id in current list
624
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
620✔
625
    taosWLockLatch(&pTaskInfo->lock);
620✔
626
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
620✔
627
    taosWUnLockLatch(&pTaskInfo->lock);
620✔
628
  }
629

630
  return code;
55,877✔
631
}
632

633
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
266,141,120✔
634
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
635
                                    int32_t idx, bool* tbGet) {
636
  *tbGet = false;
266,141,120✔
637

638
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
266,162,907✔
UNCOV
639
    return TSDB_CODE_INVALID_PARA;
×
640
  }
641
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
266,169,181✔
642

643
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
266,169,181✔
644
    return TSDB_CODE_SUCCESS;
152,375,631✔
645
  }
646

647
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
113,797,530✔
648
  if (!pSchemaInfo) {
113,781,589✔
649
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
650
    return terrno;
×
651
  }
652

653
  *sversion = pSchemaInfo->sw->version;
113,781,589✔
654
  *tversion = pSchemaInfo->tversion;
113,810,313✔
655
  *rversion = pSchemaInfo->rversion;
113,801,037✔
656
  if (pSchemaInfo->dbname) {
113,777,778✔
657
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
113,793,905✔
658
  } else {
659
    dbName[0] = 0;
×
660
  }
661
  if (pSchemaInfo->tablename) {
113,822,647✔
662
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
113,805,975✔
663
  } else {
664
    tableName[0] = 0;
430✔
665
  }
666

667
  *tbGet = true;
113,819,972✔
668

669
  return TSDB_CODE_SUCCESS;
113,795,510✔
670
}
671

672
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
152,353,753✔
673

674
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
675
  if (NULL == pParam) {
×
676
    return;
×
677
  }
678
  freeOperatorParam(pParam, OP_GET_PARAM);
×
679
}
680

681
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
684,353✔
682
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
684,353✔
683
  ((SExecTaskInfo*)tinfo)->paramSet = false;
684,353✔
684
}
684,353✔
685

686
int32_t qExecutorInit(void) {
4,692,024✔
687
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,692,024✔
688
  return TSDB_CODE_SUCCESS;
4,692,024✔
689
}
690

691
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
154,187,899✔
692
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
693
                        EOPTR_EXEC_MODEL model) {
694
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
154,187,899✔
695
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
154,187,899✔
696

697
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
154,191,290✔
698

699
  readHandle->uid = 0;
154,216,440✔
700
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
154,225,236✔
701
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
154,061,954✔
702
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
1,629✔
703
    goto _error;
25,826✔
704
  }
705

706
  if (handle) {
153,981,486✔
707
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
153,885,779✔
708
    void*           pSinkManager = NULL;
153,908,711✔
709
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
153,910,524✔
710
    if (code != TSDB_CODE_SUCCESS) {
153,867,005✔
711
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
712
      goto _error;
×
713
    }
714

715
    void* pSinkParam = NULL;
153,867,005✔
716
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
153,977,733✔
717
    if (code != TSDB_CODE_SUCCESS) {
153,851,388✔
718
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
719
      taosMemoryFree(pSinkManager);
×
720
      goto _error;
×
721
    }
722

723
    SDataSinkNode* pSink = NULL;
153,851,388✔
724
    if (readHandle->localExec) {
153,960,198✔
725
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
5,500✔
726
      if (code != TSDB_CODE_SUCCESS) {
5,500✔
727
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
728
               (*pTask)->id.str);
729
        taosMemoryFree(pSinkManager);
×
730
        goto _error;
×
731
      }
732
    }
733

734
    // pSinkParam has been freed during create sinker.
735
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
153,889,719✔
736
                              (*pTask)->id.str, pSubplan->processOneBlock);
154,007,966✔
737
    if (code) {
153,884,937✔
738
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
643✔
739
    }
740
  }
741

742
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
154,029,072✔
743
         tstrerror(code));
744

745
_error:
8,927,788✔
746
  // if failed to add ref for all tables in this query, abort current query
747
  return code;
154,186,987✔
748
}
749

750
static void freeBlock(void* param) {
×
751
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
752
  blockDataDestroy(pBlock);
×
753
}
×
754

755
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
172,518,485✔
756
                     bool processOneBlock) {
757
  int32_t        code = TSDB_CODE_SUCCESS;
172,518,485✔
758
  int32_t        lino = 0;
172,518,485✔
759
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
172,518,485✔
760
  int64_t        threadId = taosGetSelfPthreadId();
172,518,485✔
761

762
  if (pLocal) {
172,516,515✔
763
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
166,365,282✔
764
  }
765

766
  taosArrayClear(pResList);
172,487,034✔
767

768
  int64_t curOwner = 0;
172,504,645✔
769
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
172,504,645✔
770
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
771
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
772
    return pTaskInfo->code;
×
773
  }
774

775
  if (pTaskInfo->cost.start == 0) {
172,504,267✔
776
    pTaskInfo->cost.start = taosGetTimestampUs();
152,703,453✔
777
  }
778

779
  if (isTaskKilled(pTaskInfo)) {
172,511,927✔
780
    atomic_store_64(&pTaskInfo->owner, 0);
113✔
781
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
113✔
782
    return pTaskInfo->code;
113✔
783
  }
784

785
  // error occurs, record the error code and return to client
786
  int32_t ret = setjmp(pTaskInfo->env);
172,504,569✔
787
  if (ret != TSDB_CODE_SUCCESS) {
172,721,973✔
788
    pTaskInfo->code = ret;
242,579✔
789
    (void)cleanUpUdfs();
242,579✔
790

791
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
242,579✔
792
    atomic_store_64(&pTaskInfo->owner, 0);
242,579✔
793

794
    return pTaskInfo->code;
242,579✔
795
  }
796

797
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
172,479,394✔
798

799
  int32_t      current = 0;
172,480,086✔
800
  SSDataBlock* pRes = NULL;
172,480,086✔
801
  int64_t      st = taosGetTimestampUs();
172,504,456✔
802

803
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
172,504,456✔
804
    pTaskInfo->paramSet = true;
684,353✔
805
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
684,353✔
806
  } else {
807
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
171,829,638✔
808
  }
809

810
  QUERY_CHECK_CODE(code, lino, _end);
172,274,036✔
811
  code = blockDataCheck(pRes);
172,274,036✔
812
  QUERY_CHECK_CODE(code, lino, _end);
172,275,836✔
813

814
  if (pRes == NULL) {
172,275,836✔
815
    st = taosGetTimestampUs();
29,367,631✔
816
  }
817

818
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
172,272,844✔
819
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
172,272,914✔
820
    rowsThreshold = 4096;
171,618,147✔
821
  }
822

823
  int32_t blockIndex = 0;
172,267,796✔
824
  while (pRes != NULL) {
559,707,465✔
825
    SSDataBlock* p = NULL;
401,592,996✔
826
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
401,620,622✔
827
      SSDataBlock* p1 = NULL;
298,245,401✔
828
      code = createOneDataBlock(pRes, true, &p1);
298,246,310✔
829
      QUERY_CHECK_CODE(code, lino, _end);
298,237,834✔
830

831
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
298,237,834✔
832
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
298,245,187✔
833
      p = p1;
298,245,187✔
834
    } else {
835
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
103,397,837✔
836
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
103,398,500✔
837

838
      p = *(SSDataBlock**)tmp;
103,398,500✔
839
      code = copyDataBlock(p, pRes);
103,399,275✔
840
      QUERY_CHECK_CODE(code, lino, _end);
103,395,135✔
841
    }
842

843
    blockIndex += 1;
401,640,413✔
844

845
    current += p->info.rows;
401,640,413✔
846
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
401,641,355✔
847
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
848
    void* tmp = taosArrayPush(pResList, &p);
401,642,974✔
849
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
401,642,974✔
850

851
    if (current >= rowsThreshold || processOneBlock) {
401,642,974✔
852
      break;
853
    }
854

855
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
387,481,254✔
856
    QUERY_CHECK_CODE(code, lino, _end);
387,450,712✔
857
    code = blockDataCheck(pRes);
387,450,712✔
858
    QUERY_CHECK_CODE(code, lino, _end);
387,482,342✔
859
  }
860

861
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
172,276,189✔
862
    pTaskInfo->pSubplan->rowsThreshold -= current;
645,310✔
863
  }
864

865
  *hasMore = (pRes != NULL);
172,278,113✔
866
  uint64_t el = (taosGetTimestampUs() - st);
172,271,102✔
867

868
  pTaskInfo->cost.elapsedTime += el;
172,271,102✔
869
  if (NULL == pRes) {
172,258,225✔
870
    *useconds = pTaskInfo->cost.elapsedTime;
158,096,505✔
871
  }
872

873
_end:
172,289,946✔
874
  (void)cleanUpUdfs();
172,252,079✔
875

876
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
172,287,621✔
877
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
172,287,509✔
878
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
879

880
  atomic_store_64(&pTaskInfo->owner, 0);
172,288,305✔
881
  if (code) {
172,288,417✔
882
    pTaskInfo->code = code;
×
883
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
884
  }
885

886
  return pTaskInfo->code;
172,288,417✔
887
}
888

889
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
890
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
891
  SArray*        pList = pTaskInfo->pResultBlockList;
×
892
  size_t         num = taosArrayGetSize(pList);
×
893
  for (int32_t i = 0; i < num; ++i) {
×
894
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
895
    if (p) {
×
896
      blockDataDestroy(*p);
×
897
    }
898
  }
899

900
  taosArrayClear(pTaskInfo->pResultBlockList);
×
901
}
×
902

903
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
38,912,220✔
904
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,912,220✔
905
  int64_t        threadId = taosGetSelfPthreadId();
38,912,220✔
906
  int64_t        curOwner = 0;
38,913,645✔
907

908
  *pRes = NULL;
38,913,645✔
909

910
  // todo extract method
911
  taosRLockLatch(&pTaskInfo->lock);
38,913,645✔
912
  bool isKilled = isTaskKilled(pTaskInfo);
38,913,645✔
913
  if (isKilled) {
38,913,645✔
914
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
915

916
    taosRUnLockLatch(&pTaskInfo->lock);
×
917
    return pTaskInfo->code;
×
918
  }
919

920
  if (pTaskInfo->owner != 0) {
38,913,645✔
921
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
922
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
923

924
    taosRUnLockLatch(&pTaskInfo->lock);
×
925
    return pTaskInfo->code;
×
926
  }
927

928
  pTaskInfo->owner = threadId;
38,913,645✔
929
  taosRUnLockLatch(&pTaskInfo->lock);
38,913,645✔
930

931
  if (pTaskInfo->cost.start == 0) {
38,913,645✔
932
    pTaskInfo->cost.start = taosGetTimestampUs();
81,230✔
933
  }
934

935
  // error occurs, record the error code and return to client
936
  int32_t ret = setjmp(pTaskInfo->env);
38,913,645✔
937
  if (ret != TSDB_CODE_SUCCESS) {
38,913,538✔
938
    pTaskInfo->code = ret;
×
939
    (void)cleanUpUdfs();
×
940
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
941
    atomic_store_64(&pTaskInfo->owner, 0);
×
942
    return pTaskInfo->code;
×
943
  }
944

945
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
38,913,538✔
946

947
  int64_t st = taosGetTimestampUs();
38,913,645✔
948
  int32_t code = TSDB_CODE_SUCCESS;
38,913,645✔
949
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
38,913,645✔
950
    pTaskInfo->paramSet = true;
×
951
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
952
  } else {
953
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
38,913,645✔
954
  }
955
  if (code) {
38,873,420✔
956
    pTaskInfo->code = code;
×
957
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
958
  }
959

960
  code = blockDataCheck(*pRes);
38,873,420✔
961
  if (code) {
38,913,480✔
962
    pTaskInfo->code = code;
×
963
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
964
  }
965

966
  uint64_t el = (taosGetTimestampUs() - st);
38,886,077✔
967

968
  pTaskInfo->cost.elapsedTime += el;
38,886,077✔
969
  if (NULL == *pRes) {
38,907,041✔
970
    *useconds = pTaskInfo->cost.elapsedTime;
4,045,861✔
971
  }
972

973
  (void)cleanUpUdfs();
38,908,382✔
974

975
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
38,913,645✔
976
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
38,913,645✔
977

978
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
38,913,645✔
979
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
980

981
  atomic_store_64(&pTaskInfo->owner, 0);
38,913,645✔
982
  return pTaskInfo->code;
38,913,645✔
983
}
984

985
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
49,731,046✔
986
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
49,731,046✔
987
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
49,731,589✔
988
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
49,731,723✔
989

990
  if (!tmp) {
49,731,046✔
991
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
992
    return terrno;
×
993
  }
994
  return TSDB_CODE_SUCCESS;
49,731,046✔
995
}
996

997
int32_t stopInfoComp(void const* lp, void const* rp) {
×
998
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
999
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1000

1001
  if (key->refId < pInfo->refId) {
×
1002
    return -1;
×
1003
  } else if (key->refId > pInfo->refId) {
×
1004
    return 1;
×
1005
  }
1006

1007
  return 0;
×
1008
}
1009

1010
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1011
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1012
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1013
  if (idx >= 0) {
×
1014
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1015
  }
1016
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1017
}
×
1018

1019
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
18,349✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
18,349✔
1021

1022
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
18,349✔
1023
  for (int32_t i = 0; i < num; ++i) {
19,281✔
1024
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
932✔
1025
    if (!pStop) {
932✔
1026
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1027
      continue;
×
1028
    }
1029
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
932✔
1030
    if (pExchangeInfo) {
932✔
1031
      int32_t code = tsem_post(&pExchangeInfo->ready);
932✔
1032
      if (code != TSDB_CODE_SUCCESS) {
932✔
1033
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1034
      } else {
1035
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
932✔
1036
      }
1037
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
932✔
1038
      if (code != TSDB_CODE_SUCCESS) {
932✔
1039
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1040
      }
1041
    }
1042
  }
1043

1044
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
18,349✔
1045
}
18,349✔
1046

1047
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
18,349✔
1048
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
18,349✔
1049
  if (pTaskInfo == NULL) {
18,349✔
1050
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1051
  }
1052

1053
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
18,349✔
1054

1055
  setTaskKilled(pTaskInfo, rspCode);
18,349✔
1056
  qStopTaskOperators(pTaskInfo);
18,349✔
1057

1058
  return TSDB_CODE_SUCCESS;
18,349✔
1059
}
1060

1061
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1062
  int64_t        st = taosGetTimestampMs();
×
1063
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1064
  if (pTaskInfo == NULL) {
×
1065
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1066
  }
1067

1068
  if (waitDuration > 0) {
×
1069
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1070
  } else {
1071
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1072
  }
1073

1074
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1075

1076
  if (waitDuration > 0) {
×
1077
    while (1) {
1078
      taosWLockLatch(&pTaskInfo->lock);
×
1079
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1080
        taosWUnLockLatch(&pTaskInfo->lock);
×
1081

1082
        taosMsleep(200);
×
1083

1084
        int64_t d = taosGetTimestampMs() - st;
×
1085
        if (d >= waitDuration && waitDuration >= 0) {
×
1086
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1087
          return TSDB_CODE_SUCCESS;
×
1088
        }
1089
      } else {  // not running now
1090
        pTaskInfo->code = rspCode;
×
1091
        taosWUnLockLatch(&pTaskInfo->lock);
×
1092
        return TSDB_CODE_SUCCESS;
×
1093
      }
1094
    }
1095
  }
1096

1097
  int64_t et = taosGetTimestampMs() - st;
×
1098
  if (et < waitDuration) {
×
1099
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1100
    return TSDB_CODE_SUCCESS;
×
1101
  }
1102
  return TSDB_CODE_SUCCESS;
×
1103
}
1104

1105
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1106
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1107
  if (NULL == pTaskInfo) {
×
1108
    return false;
×
1109
  }
1110

1111
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1112
}
1113

1114
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
154,936,305✔
1115
  STaskCostInfo* pSummary = &pTaskInfo->cost;
154,936,305✔
1116
  int64_t        idleTime = pSummary->start - pSummary->created;
154,937,207✔
1117

1118
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
154,933,942✔
1119
  if (pSummary->pRecoder != NULL) {
154,934,027✔
1120
    qDebug(
112,957,994✔
1121
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1122
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1123
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1124
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1125
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1126
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1127
  } else {
1128
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
41,972,226✔
1129
           pSummary->elapsedTime / 1000.0);
1130
  }
1131
}
154,930,220✔
1132

1133
void qDestroyTask(qTaskInfo_t qTaskHandle) {
162,462,196✔
1134
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
162,462,196✔
1135
  if (pTaskInfo == NULL) {
162,462,196✔
1136
    return;
7,526,031✔
1137
  }
1138

1139
  if (pTaskInfo->pRoot != NULL) {
154,936,165✔
1140
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
154,936,347✔
1141
  } else {
1142
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1143
  }
1144

1145
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
154,935,934✔
1146
  doDestroyTask(pTaskInfo);
154,935,724✔
1147
}
1148

1149
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,639,945✔
1150
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,639,945✔
1151
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,639,945✔
1152
}
1153

1154
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
102,163✔
1155
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
102,163✔
1156
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
102,163✔
1157

1158
  while (1) {
101,153✔
1159
    uint16_t type = pOperator->operatorType;
203,316✔
1160
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
203,316✔
1161
      *scanner = pOperator->info;
102,163✔
1162
      break;
102,163✔
1163
    } else {
1164
      pOperator = pOperator->pDownstream[0];
101,153✔
1165
    }
1166
  }
1167
}
102,163✔
1168

1169
void* qExtractReaderFromTmqScanner(void* scanner) {
102,163✔
1170
  SStreamScanInfo* pInfo = scanner;
102,163✔
1171
  return (void*)pInfo->tqReader;
102,163✔
1172
}
1173

1174
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
127,291✔
1175
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
127,291✔
1176
  return pTaskInfo->streamInfo.schema;
127,291✔
1177
}
1178

1179
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
127,291✔
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
127,291✔
1181
  return pTaskInfo->streamInfo.tbName;
127,291✔
1182
}
1183

1184
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
132,976✔
1185
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
132,976✔
1186
  return &pTaskInfo->streamInfo.btMetaRsp;
132,976✔
1187
}
1188

1189
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,381,683✔
1190
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,381,683✔
1191
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,381,683✔
1192
  return 0;
4,381,683✔
1193
  /*if (code != TSDB_CODE_SUCCESS) {
1194
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1195
    pTaskInfo->code = code;
1196
    T_LONG_JMP(pTaskInfo->env, code);
1197
  }*/
1198
}
1199

1200
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
123,872✔
1201
  memset(pCond, 0, sizeof(SQueryTableDataCond));
123,872✔
1202
  pCond->order = TSDB_ORDER_ASC;
123,872✔
1203
  pCond->numOfCols = pMtInfo->schema->nCols;
123,896✔
1204
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
123,919✔
1205
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
123,872✔
1206
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
123,919✔
1207
    taosMemoryFreeClear(pCond->colList);
70✔
1208
    taosMemoryFreeClear(pCond->pSlotList);
×
1209
    return terrno;
×
1210
  }
1211

1212
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
123,849✔
1213
  pCond->suid = pMtInfo->suid;
123,825✔
1214
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
123,886✔
1215
  pCond->startVersion = -1;
123,933✔
1216
  pCond->endVersion = sContext->snapVersion;
123,863✔
1217

1218
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
790,700✔
1219
    SColumnInfo* pColInfo = &pCond->colList[i];
666,626✔
1220
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
666,603✔
1221
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
666,650✔
1222
    if (pMtInfo->pExtSchemas != NULL) {
666,532✔
1223
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,537✔
1224
    }
1225
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
666,322✔
1226
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
666,532✔
1227

1228
    pCond->pSlotList[i] = i;
666,612✔
1229
  }
1230

1231
  return TSDB_CODE_SUCCESS;
124,027✔
1232
}
1233

1234
void qStreamSetOpen(qTaskInfo_t tinfo) {
38,656,997✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,656,997✔
1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
38,656,997✔
1237
  pOperator->status = OP_NOT_OPENED;
38,659,968✔
1238
}
38,657,867✔
1239

1240
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,245,388✔
1241
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,245,388✔
1242
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,245,388✔
1243
}
4,245,295✔
1244

1245
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,428,155✔
1246
  int32_t        code = TSDB_CODE_SUCCESS;
4,428,155✔
1247
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,428,155✔
1248
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,428,155✔
1249

1250
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,428,342✔
1251
  const char*    id = GET_TASKID(pTaskInfo);
4,428,155✔
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,427,527✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,187,883✔
1255
    if (pOperator == NULL || code != 0) {
4,187,523✔
1256
      return code;
×
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
4,187,847✔
1260
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,187,910✔
1261
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,188,313✔
1262
    walReaderVerifyOffset(pWalReader, pOffset);
4,188,314✔
1263
  }
1264
  // if pOffset equal to current offset, means continue consume
1265
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,427,397✔
1266
    return 0;
4,038,971✔
1267
  }
1268

1269
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
388,750✔
1270
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
261,397✔
1271
    if (pOperator == NULL || code != 0) {
261,360✔
1272
      return code;
×
1273
    }
1274

1275
    SStreamScanInfo* pInfo = pOperator->info;
261,360✔
1276
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
261,439✔
1277
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
261,326✔
1278
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
261,261✔
1279

1280
    if (pOffset->type == TMQ_OFFSET__LOG) {
261,261✔
1281
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
212,637✔
1282
      pScanBaseInfo->dataReader = NULL;
212,397✔
1283

1284
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
212,397✔
1285
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
212,384✔
1286
      walReaderVerifyOffset(pWalReader, pOffset);
212,384✔
1287
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
212,637✔
1288
      if (code < 0) {
212,637✔
1289
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
6,836✔
1290
        return code;
6,836✔
1291
      }
1292
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
48,751✔
1293
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1294
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1295
      int64_t uid = pOffset->uid;
48,830✔
1296
      int64_t ts = pOffset->ts;
48,751✔
1297
      int32_t index = 0;
48,830✔
1298

1299
      // this value may be changed if new tables are created
1300
      taosRLockLatch(&pTaskInfo->lock);
48,830✔
1301
      int32_t numOfTables = 0;
48,758✔
1302
      code = tableListGetSize(pTableListInfo, &numOfTables);
48,758✔
1303
      if (code != TSDB_CODE_SUCCESS) {
48,909✔
1304
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1305
        taosRUnLockLatch(&pTaskInfo->lock);
×
1306
        return code;
×
1307
      }
1308

1309
      if (uid == 0) {
48,909✔
1310
        if (numOfTables != 0) {
47,454✔
1311
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
7,835✔
1312
          if (!tmp) {
7,835✔
1313
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1314
            taosRUnLockLatch(&pTaskInfo->lock);
×
1315
            return terrno;
×
1316
          }
1317
          if (tmp) uid = tmp->uid;
7,835✔
1318
          ts = INT64_MIN;
7,835✔
1319
          pScanInfo->currentTable = 0;
7,835✔
1320
        } else {
1321
          taosRUnLockLatch(&pTaskInfo->lock);
39,619✔
1322
          qError("no table in table list, %s", id);
39,619✔
1323
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
39,619✔
1324
        }
1325
      }
1326
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,290✔
1327

1328
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
9,290✔
1329
             pInfo->pTableScanOp->resultInfo.totalRows);
1330
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
9,290✔
1331

1332
      // start from current accessed position
1333
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1334
      // position, let's find it from the beginning.
1335
      index = tableListFind(pTableListInfo, uid, 0);
9,290✔
1336
      taosRUnLockLatch(&pTaskInfo->lock);
9,290✔
1337

1338
      if (index >= 0) {
9,290✔
1339
        pScanInfo->currentTable = index;
9,290✔
1340
      } else {
1341
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1342
               numOfTables, pScanInfo->currentTable, id);
1343
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1344
      }
1345

1346
      STableKeyInfo keyInfo = {.uid = uid};
9,290✔
1347
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
9,290✔
1348

1349
      // let's start from the next ts that returned to consumer.
1350
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
9,290✔
1351
        pScanBaseInfo->cond.twindows.skey = ts;
217✔
1352
      } else {
1353
        pScanBaseInfo->cond.twindows.skey = ts + 1;
9,014✔
1354
      }
1355
      pScanInfo->scanTimes = 0;
9,231✔
1356

1357
      if (pScanBaseInfo->dataReader == NULL) {
9,290✔
1358
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
15,454✔
1359
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1360
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
7,727✔
1361
        if (code != TSDB_CODE_SUCCESS) {
7,727✔
1362
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1363
          return code;
×
1364
        }
1365

1366
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
7,727✔
1367
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1368
      } else {
1369
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,563✔
1370
        if (code != TSDB_CODE_SUCCESS) {
1,563✔
1371
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1372
          return code;
×
1373
        }
1374

1375
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
1,563✔
1376
        if (code != TSDB_CODE_SUCCESS) {
1,563✔
1377
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1378
          return code;
×
1379
        }
1380
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
1,563✔
1381
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1382
      }
1383

1384
      // restore the key value
1385
      pScanBaseInfo->cond.twindows.skey = oldSkey;
9,290✔
1386
    } else {
1387
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
79✔
1388
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1389
    }
1390

1391
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1392
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
127,353✔
1393
      SStreamRawScanInfo* pInfo = pOperator->info;
124,203✔
1394
      SSnapContext*       sContext = pInfo->sContext;
124,203✔
1395
      SOperatorInfo*      p = NULL;
124,203✔
1396

1397
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
124,203✔
1398
      if (code != 0) {
124,156✔
1399
        return code;
×
1400
      }
1401

1402
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
124,156✔
1403

1404
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
124,203✔
1405
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1406
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1407
      }
1408

1409
      SMetaTableInfo mtInfo = {0};
124,156✔
1410
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
124,203✔
1411
      if (code != 0) {
124,109✔
1412
        destroyMetaTableInfo(&mtInfo);
1413
        return code;
×
1414
      }
1415
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
124,109✔
1416
      pInfo->dataReader = NULL;
124,109✔
1417

1418
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
124,109✔
1419
      tableListClear(pTableListInfo);
124,156✔
1420

1421
      if (mtInfo.uid == 0) {
124,203✔
1422
        destroyMetaTableInfo(&mtInfo);
1423
        goto end;  // no data
176✔
1424
      }
1425

1426
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
124,027✔
1427
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
123,966✔
1428
      if (code != TSDB_CODE_SUCCESS) {
123,849✔
1429
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1430
        destroyMetaTableInfo(&mtInfo);
1431
        return code;
×
1432
      }
1433
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
123,849✔
1434
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
371✔
1435
      } else {
1436
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
123,656✔
1437
      }
1438

1439
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
123,933✔
1440
      if (code != TSDB_CODE_SUCCESS) {
124,027✔
1441
        destroyMetaTableInfo(&mtInfo);
1442
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1443
        return code;
×
1444
      }
1445

1446
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
124,027✔
1447
      if (!pList) {
124,027✔
1448
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1449
        destroyMetaTableInfo(&mtInfo);
1450
        return code;
×
1451
      }
1452
      int32_t size = 0;
124,027✔
1453
      code = tableListGetSize(pTableListInfo, &size);
124,027✔
1454
      if (code != TSDB_CODE_SUCCESS) {
124,027✔
1455
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1456
        destroyMetaTableInfo(&mtInfo);
1457
        return code;
×
1458
      }
1459

1460
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
248,054✔
1461
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
124,027✔
1462
      if (code != TSDB_CODE_SUCCESS) {
123,980✔
1463
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1464
        destroyMetaTableInfo(&mtInfo);
1465
        return code;
×
1466
      }
1467

1468
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
123,980✔
1469
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
123,980✔
1470
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1471
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
123,980✔
1472
      pTaskInfo->streamInfo.schema = mtInfo.schema;
123,933✔
1473
      taosMemoryFreeClear(mtInfo.pExtSchemas);
124,027✔
1474

1475
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
124,027✔
1476
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,150✔
1477
      SStreamRawScanInfo* pInfo = pOperator->info;
1,202✔
1478
      SSnapContext*       sContext = pInfo->sContext;
1,202✔
1479
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,202✔
1480
      if (code != 0) {
1,202✔
1481
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1482
        return code;
×
1483
      }
1484
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
1,202✔
1485
             id);
1486
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
1,995✔
1487
      SStreamRawScanInfo* pInfo = pOperator->info;
1,995✔
1488
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,995✔
1489
      pInfo->dataReader = NULL;
1,995✔
1490
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,995✔
1491
    }
1492
  }
1493

1494
end:
342,485✔
1495
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
342,491✔
1496
  return 0;
342,491✔
1497
}
1498

1499
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
103,649,229✔
1500
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
103,649,229✔
1501
  if (pMsg->info.ahandle == NULL) {
103,659,278✔
1502
    rpcFreeCont(pMsg->pCont);
×
1503
    qError("pMsg->info.ahandle is NULL");
×
1504
    return;
×
1505
  }
1506

1507
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
103,637,493✔
1508
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1509

1510
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
103,647,706✔
1511

1512
  if (pMsg->contLen > 0) {
103,657,813✔
1513
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
103,642,508✔
1514
    if (buf.pData == NULL) {
103,637,855✔
1515
      pMsg->code = terrno;
×
1516
    } else {
1517
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
103,637,855✔
1518
    }
1519
  }
1520

1521
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
103,657,799✔
1522
  rpcFreeCont(pMsg->pCont);
103,663,656✔
1523
  destroySendMsgInfo(pSendInfo);
103,640,324✔
1524
}
1525

1526
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1527
  int32_t        code = TSDB_CODE_SUCCESS;
×
1528
  int32_t        lino = 0;
×
1529
  SExecTaskInfo* pTaskInfo = tinfo;
×
1530
  SArray*        plist = NULL;
×
1531

1532
  code = getTableListInfo(pTaskInfo, &plist);
×
1533
  if (code || plist == NULL) {
×
1534
    return NULL;
×
1535
  }
1536

1537
  // only extract table in the first elements
1538
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1539

1540
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1541
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1542

1543
  int32_t numOfTables = 0;
×
1544
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1545
  QUERY_CHECK_CODE(code, lino, _end);
×
1546

1547
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1548
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1549
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1550
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1551
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1552
  }
1553

1554
  taosArrayDestroy(plist);
×
1555

1556
_end:
×
1557
  if (code != TSDB_CODE_SUCCESS) {
×
1558
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1559
    T_LONG_JMP(pTaskInfo->env, code);
×
1560
  }
1561
  return pUidList;
×
1562
}
1563

1564
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,430,391✔
1565
  int32_t        code = TSDB_CODE_SUCCESS;
3,430,391✔
1566
  int32_t        lino = 0;
3,430,391✔
1567
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,430,391✔
1568

1569
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,430,391✔
1570
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1571
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1572

1573
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1574
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1575
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,429,174✔
1576
    STableScanInfo* pScanInfo = pOperator->info;
1,714,432✔
1577

1578
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,714,432✔
1579
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,715,804✔
1580
  } else {
1581
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,715,959✔
1582
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,713,981✔
1583
    }
1584
  }
1585

1586
_end:
×
1587
  if (code != TSDB_CODE_SUCCESS) {
3,430,386✔
1588
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1589
  }
1590
  return code;
3,430,386✔
1591
}
1592

1593
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,715,959✔
1594
  if (pList == NULL) {
1,715,959✔
1595
    return TSDB_CODE_INVALID_PARA;
×
1596
  }
1597

1598
  *pList = NULL;
1,715,959✔
1599
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,715,959✔
1600
  if (pArray == NULL) {
1,715,348✔
1601
    return terrno;
×
1602
  }
1603

1604
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,715,348✔
1605
  if (code == 0) {
1,714,582✔
1606
    *pList = pArray;
1,714,582✔
1607
  } else {
1608
    taosArrayDestroy(pArray);
×
1609
  }
1610
  return code;
1,714,582✔
1611
}
1612

1613
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1614
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1615
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1616
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1617
  }
1618
  return 0;
×
1619
}
1620

1621
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1622
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1623
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1624
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1625
  }
1626
  return 0;
×
1627
}
1628

1629
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1630
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1631

1632
  int32_t code = pTaskInfo->code;
×
1633
  pTaskInfo->code = 0;
×
1634
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1635
}
×
1636

1637
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1638
  int32_t code = 0;
×
1639
  return code;
×
1640
}
1641

1642
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1643
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1644
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1645
  return code;
×
1646
}
1647

1648
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
36,543,750✔
1649
  int32_t code = 0;
36,543,750✔
1650

1651
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
36,543,750✔
1652
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
36,542,315✔
1653

1654
  if (pOper->fpSet.resetStateFn) {
36,542,715✔
1655
    code = pOper->fpSet.resetStateFn(pOper);
36,542,340✔
1656
  }
1657
  pOper->status = OP_NOT_OPENED;
36,541,090✔
1658
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
61,288,898✔
1659
    code = clearStatesForOperator(pOper->pDownstream[i]);
24,745,775✔
1660
  }
1661
  return code;
36,545,504✔
1662
}
1663

1664
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
11,798,498✔
1665
  int32_t        code = 0;
11,798,498✔
1666
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,798,498✔
1667
  SOperatorInfo* pOper = pTaskInfo->pRoot;
11,798,498✔
1668
  pTaskInfo->code = TSDB_CODE_SUCCESS;
11,798,088✔
1669
  code = clearStatesForOperator(pOper);
11,797,272✔
1670
  return code;
11,798,498✔
1671
}
1672

1673
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
16,358,704✔
1674
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
16,358,704✔
1675
  int64_t        threadId = taosGetSelfPthreadId();
16,358,704✔
1676
  int64_t        curOwner = 0;
16,358,704✔
1677

1678
  *ppRes = NULL;
16,358,704✔
1679

1680
  // todo extract method
1681
  taosRLockLatch(&pTaskInfo->lock);
16,358,704✔
1682
  bool isKilled = isTaskKilled(pTaskInfo);
16,358,704✔
1683
  if (isKilled) {
16,358,704✔
1684
    // clearStreamBlock(pTaskInfo->pRoot);
1685
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1686

1687
    taosRUnLockLatch(&pTaskInfo->lock);
×
1688
    return pTaskInfo->code;
×
1689
  }
1690

1691
  if (pTaskInfo->owner != 0) {
16,358,704✔
1692
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1693
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1694

1695
    taosRUnLockLatch(&pTaskInfo->lock);
×
1696
    return pTaskInfo->code;
×
1697
  }
1698

1699
  pTaskInfo->owner = threadId;
16,358,296✔
1700
  taosRUnLockLatch(&pTaskInfo->lock);
16,358,704✔
1701

1702
  if (pTaskInfo->cost.start == 0) {
16,358,704✔
1703
    pTaskInfo->cost.start = taosGetTimestampUs();
329,739✔
1704
  }
1705

1706
  // error occurs, record the error code and return to client
1707
  int32_t ret = setjmp(pTaskInfo->env);
16,358,704✔
1708
  if (ret != TSDB_CODE_SUCCESS) {
18,191,863✔
1709
    pTaskInfo->code = ret;
1,833,988✔
1710
    (void)cleanUpUdfs();
1,833,988✔
1711
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,833,988✔
1712
    atomic_store_64(&pTaskInfo->owner, 0);
1,833,988✔
1713
    return pTaskInfo->code;
1,833,988✔
1714
  }
1715

1716
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
16,357,875✔
1717

1718
  int64_t st = taosGetTimestampUs();
16,358,704✔
1719

1720
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
16,358,704✔
1721
  if (code) {
14,523,894✔
1722
    pTaskInfo->code = code;
×
1723
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1724
  } else {
1725
    *finished = *ppRes == NULL;
14,523,894✔
1726
    code = blockDataCheck(*ppRes);
14,524,309✔
1727
  }
1728
  if (code) {
14,524,716✔
1729
    pTaskInfo->code = code;
×
1730
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1731
  }
1732

1733
  uint64_t el = (taosGetTimestampUs() - st);
14,524,716✔
1734

1735
  pTaskInfo->cost.elapsedTime += el;
14,524,716✔
1736
  if (NULL == *ppRes) {
14,524,306✔
1737
    *useconds = pTaskInfo->cost.elapsedTime;
9,412,067✔
1738
  }
1739

1740
  (void)cleanUpUdfs();
14,524,716✔
1741

1742
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
14,524,716✔
1743
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
14,524,716✔
1744

1745
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
14,524,716✔
1746
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1747

1748
  atomic_store_64(&pTaskInfo->owner, 0);
14,524,716✔
1749
  return pTaskInfo->code;
14,523,943✔
1750
}
1751

1752
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1753
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1754
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1755
// }
1756

1757
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
330,486✔
1758
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1759
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1760
  int32_t code = 0;                                        
330,486✔
1761
  if (*pTableListInfo != NULL) {
330,486✔
1762
    qDebug("table list already exists, no need to create again");
×
1763
    goto end;
×
1764
  }
1765
  STableListInfo* pList = tableListCreate();
330,486✔
1766
  if (pList == NULL) {
330,486✔
1767
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1768
    code = terrno;
×
1769
    goto end;
×
1770
  }
1771

1772
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
330,486✔
1773
  SReadHandle    pHandle = {.vnode = pVnode};
330,486✔
1774
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
330,486✔
1775

1776
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
330,486✔
1777
  if (code != 0) {
330,486✔
1778
    tableListDestroy(pList);
×
1779
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1780
    goto end;
×
1781
  }
1782
  *pTableListInfo = pList;
330,486✔
1783

1784
end:
330,486✔
1785
  return 0;
330,486✔
1786
}
1787

1788
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
141,196✔
1789
  bool   listAdded = false;
141,196✔
1790
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
141,196✔
1791
  if (code == 0 && !listAdded) {
141,196✔
1792
    int32_t numOfTables = taosArrayGetSize(pUidList);
132,020✔
1793
    for (int i = 0; i < numOfTables; i++) {
264,040✔
1794
      void* tmp = taosArrayGet(pUidList, i);
132,020✔
1795
      if (tmp == NULL) {
132,020✔
1796
        return terrno;
×
1797
      }
1798
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
132,020✔
1799

1800
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
132,020✔
1801
      if (p == NULL) {
132,020✔
1802
        return terrno;
×
1803
      }
1804
    }
1805
  }
1806
  return code;
141,196✔
1807
}
1808

1809
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
141,196✔
1810
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1811
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1812
  int32_t code = TSDB_CODE_SUCCESS;
141,196✔
1813
  STableListInfo* pList = tableListCreate();
141,196✔
1814
  if (pList == NULL) {
141,196✔
1815
    code = terrno;
×
1816
    goto end;
×
1817
  }
1818
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
141,196✔
1819
  if (uidListCopy == NULL) {
141,196✔
1820
    code = terrno;
×
1821
    goto end;
×
1822
  }
1823
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
141,196✔
1824
  SReadHandle    pHandle = {.vnode = pVnode};
141,196✔
1825

1826
  pList->idInfo.suid = suid;
141,196✔
1827
  pList->idInfo.tableType = TD_SUPER_TABLE;
141,196✔
1828
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
141,196✔
1829
  if (code != TSDB_CODE_SUCCESS) {
141,196✔
1830
    goto end;
×
1831
  }                                              
1832
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
141,196✔
1833
  if (code != TSDB_CODE_SUCCESS) {
141,196✔
1834
    goto end;
×
1835
  }
1836
  *tableList = pList->pTableList;
141,196✔
1837
  pList->pTableList = NULL;
141,196✔
1838

1839
  taosArrayClear(uidList);
141,196✔
1840
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
282,392✔
1841
    void* tmp = taosArrayGet(uidListCopy, i);
141,196✔
1842
    if (tmp == NULL) {
141,196✔
1843
      continue;
×
1844
    }
1845
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
141,196✔
1846
    if (slot == NULL) {
141,196✔
1847
      if (taosArrayPush(uidList, tmp) == NULL) {
6,202✔
1848
        code = terrno;
×
1849
        goto end;
×
1850
      }
1851
    }
1852
  }
1853
end:
141,196✔
1854
  taosArrayDestroy(uidListCopy);
141,196✔
1855
  tableListDestroy(pList);
141,196✔
1856
  return code;
141,196✔
1857
}
1858

1859
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1860
//   int32_t index = -1;
1861
//   (void)taosThreadRwlockRdlock(lock);
1862
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1863
//     index = 0;
1864
//     goto end;
1865
//   }
1866
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1867
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1868

1869
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1870
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1871
//       index = i;
1872
//       goto end;
1873
//     }
1874
//   }
1875
// end:
1876
//   (void)taosThreadRwlockUnlock(lock);
1877
//   return index;
1878
// }
1879

1880
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
330,486✔
1881
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
330,486✔
1882
  STableListInfo* pList = pTableListInfo;
330,486✔
1883
  return pList->pTableList;
330,486✔
1884
}
1885

1886
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,891,531✔
1887

1888
void streamDestroyExecTask(qTaskInfo_t tInfo) {
3,336,980✔
1889
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
3,336,980✔
1890
  qDestroyTask(tInfo);
3,336,980✔
1891
}
3,336,980✔
1892

1893
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
917,413✔
1894
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
917,413✔
1895
}
1896

1897
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
954,125✔
1898
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1899
  int32_t      code = 0;
954,125✔
1900
  SNode*       pNode = 0;
954,125✔
1901
  SNodeList*   pList = NULL;
954,125✔
1902
  SExprInfo*   pExprInfo = NULL;
954,125✔
1903
  int32_t      numOfExprs = 1;
954,125✔
1904
  int32_t*     offset = 0;
954,125✔
1905
  STargetNode* pTargetNode = NULL;
954,125✔
1906
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
954,125✔
1907
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
954,125✔
1908

1909
  if (code == 0) {
954,125✔
1910
    pTargetNode->dataBlockId = 0;
954,125✔
1911
    pTargetNode->pExpr = pNode;
954,125✔
1912
    pTargetNode->slotId = 0;
954,125✔
1913
  }
1914
  if (code == 0) {
954,125✔
1915
    code = nodesMakeList(&pList);
953,715✔
1916
  }
1917
  if (code == 0) {
954,127✔
1918
    code = nodesListAppend(pList, (SNode*)pTargetNode);
953,717✔
1919
  }
1920
  if (code == 0) {
954,535✔
1921
    pNode = NULL;
954,125✔
1922
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
954,125✔
1923
  }
1924

1925
  if (code == 0) {
954,535✔
1926
    const char* pVal = NULL;
954,125✔
1927
    int32_t     len = 0;
954,125✔
1928
    SNode*      pSclNode = NULL;
954,125✔
1929
    switch (pExprInfo->pExpr->nodeType) {
954,125✔
1930
      case QUERY_NODE_FUNCTION:
954,125✔
1931
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
954,125✔
1932
        break;
954,125✔
1933
      case QUERY_NODE_OPERATOR:
×
1934
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1935
        break;
×
1936
      default:
×
1937
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1938
        break;
×
1939
    }
1940
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
954,125✔
1941
    SSDataBlock block = {0};
954,125✔
1942
    block.info.rows = 1;
954,125✔
1943
    SSDataBlock* pBlock = &block;
954,125✔
1944
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
954,125✔
1945
    if (tmp == NULL) {
954,125✔
1946
      code = terrno;
×
1947
    }
1948
    if (code == 0) {
954,125✔
1949
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
954,125✔
1950
    }
1951
    taosArrayDestroy(pBlockList);
954,125✔
1952
  }
1953
  nodesDestroyList(pList);
954,535✔
1954
  destroyExprInfo(pExprInfo, numOfExprs);
954,125✔
1955
  taosMemoryFreeClear(pExprInfo);
954,125✔
1956
  return code;
954,125✔
1957
}
1958

1959
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,318,685✔
1960
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,318,685✔
1961
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,318,685✔
1962
  int32_t        code = 0;
4,318,685✔
1963
  SNode*         pNode = NULL;
4,318,685✔
1964
  if (!pForceOutputCols) return 0;
4,318,685✔
1965
  if (!*pRes) {
36,712✔
1966
    code = createDataBlock(pRes);
36,712✔
1967
  }
1968

1969
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
36,712✔
1970
    int32_t idx = 0;
36,712✔
1971
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
146,467✔
1972
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
109,755✔
1973
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
109,755✔
1974
      colInfo.info.precision = pCol->type.precision;
109,755✔
1975
      colInfo.info.scale = pCol->type.scale;
109,755✔
1976
      code = blockDataAppendColInfo(*pRes, &colInfo);
109,755✔
1977
      if (code != 0) break;
109,755✔
1978
    }
1979
  }
1980

1981
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
36,712✔
1982
  if (code != TSDB_CODE_SUCCESS) {
36,712✔
1983
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1984
    return code;
×
1985
  }
1986

1987
  // loop all exprs for force output, execute all exprs
1988
  int32_t idx = 0;
36,712✔
1989
  int32_t rowIdx = (*pRes)->info.rows;
36,712✔
1990
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
36,712✔
1991
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
36,712✔
1992
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
146,467✔
1993
    SScalarParam   dst = {0};
109,755✔
1994
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
109,755✔
1995
    code = nodesStringToNode(pCol->expr, &pNode);
109,755✔
1996
    if (code != 0) break;
109,755✔
1997
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
109,755✔
1998
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
109,755✔
1999
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
73,043✔
2000
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
73,043✔
2001
    } else {
2002
      dst.columnData = pInfo;
36,712✔
2003
      dst.numOfRows = rowIdx;
36,712✔
2004
      dst.colAlloced = false;
36,712✔
2005
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
36,712✔
2006
    }
2007
    ++idx;
109,755✔
2008
    // TODO sclFreeParam(&dst);
2009
    nodesDestroyNode(pNode);
109,755✔
2010
    if (code != 0) break;
109,755✔
2011
  }
2012
  if (code == TSDB_CODE_SUCCESS) {
36,712✔
2013
    (*pRes)->info.rows++;
36,712✔
2014
  }
2015
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
36,712✔
2016
  return code;
36,712✔
2017
}
2018

2019
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
348,923✔
2020
  int32_t      code = 0;
348,923✔
2021
  const char*  pVal = NULL;
348,923✔
2022
  SScalarParam dst = {0};
348,923✔
2023
  int32_t      len = 0;
348,923✔
2024
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
348,923✔
2025
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
348,923✔
2026
  // execute the expr
2027
  switch (pExpr->type) {
348,923✔
2028
    case QUERY_NODE_VALUE: {
×
2029
      SValueNode* pValue = (SValueNode*)pExpr;
×
2030
      int32_t     type = pValue->node.resType.type;
×
2031
      if (!IS_STR_DATA_TYPE(type)) {
×
2032
        qError("invalid sub tb expr with non-str type");
×
2033
        code = TSDB_CODE_INVALID_PARA;
×
2034
        break;
×
2035
      }
2036
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2037
      if (pTmp == NULL) {
×
2038
        qError("invalid sub tb expr with null value");
×
2039
        code = TSDB_CODE_INVALID_PARA;
×
2040
        break;
×
2041
      }
2042
      pVal = varDataVal(pTmp);
×
2043
      len = varDataLen(pTmp);
×
2044
    } break;
×
2045
    case QUERY_NODE_FUNCTION: {
348,923✔
2046
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
348,923✔
2047
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
348,923✔
2048
        qError("invalid sub tb expr with non-str type func");
×
2049
        code = TSDB_CODE_INVALID_PARA;
×
2050
        break;
×
2051
      }
2052
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
348,923✔
2053
      if (!pCol) {
348,923✔
2054
        code = terrno;
×
2055
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2056
        break;
×
2057
      }
2058

2059
      pCol->hasNull = true;
348,923✔
2060
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
348,923✔
2061
      pCol->info.colId = 0;
348,923✔
2062
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
348,923✔
2063
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
348,923✔
2064
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
348,923✔
2065
      code = colInfoDataEnsureCapacity(pCol, 1, true);
348,923✔
2066
      if (code != 0) {
348,923✔
2067
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2068
        taosMemoryFree(pCol);
×
2069
        break;
×
2070
      }
2071
      dst.columnData = pCol;
348,923✔
2072
      dst.numOfRows = 1;
348,923✔
2073
      dst.colAlloced = true;
348,923✔
2074
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
348,923✔
2075
      if (colDataIsNull_var(dst.columnData, 0)) {
348,923✔
2076
        qInfo("invalid sub tb expr with null value");
7,270✔
2077
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
6,860✔
2078
      }
2079
      if (code == 0) {
348,923✔
2080
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
342,063✔
2081
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
341,653✔
2082
      }
2083
    } break;
348,923✔
2084
    default:
×
2085
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2086
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2087
      break;
×
2088
  }
2089
  if (code == 0) {
348,923✔
2090
    if (!pVal || len == 0) {
342,063✔
2091
      qError("tbname generated with no characters which is not allowed");
×
2092
      code = TSDB_CODE_INVALID_PARA;
×
2093
    }
2094
    if(len > TSDB_TABLE_NAME_LEN - 1) {
342,063✔
2095
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
816✔
2096
      len = TSDB_TABLE_NAME_LEN - 1;
816✔
2097
    }
2098

2099
    memcpy(tbname, pVal, len);
342,063✔
2100
    tbname[len] = '\0';  // ensure null terminated
342,063✔
2101
    if (NULL != strchr(tbname, '.')) {
342,063✔
2102
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2103
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2104
    }
2105
  }
2106
  // TODO free dst
2107
  sclFreeParam(&dst);
348,923✔
2108
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
348,923✔
2109
  return code;
348,923✔
2110
}
2111

2112
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
321,001✔
2113
  if (pParam) {
321,001✔
2114
    if (pParam->tbname) {
321,001✔
2115
      taosMemFree(pParam->tbname);
321,001✔
2116
      pParam->tbname = NULL;
321,001✔
2117
    }
2118
    if (pParam->stbname) {
321,001✔
2119
      taosMemFree(pParam->stbname);
321,001✔
2120
      pParam->stbname = NULL;
321,001✔
2121
    }
2122
    if (pParam->dbFName) {
321,001✔
2123
      taosMemFree(pParam->dbFName);
321,001✔
2124
      pParam->dbFName = NULL;
321,001✔
2125
    }
2126
    if (pParam->pFields) {
321,001✔
2127
      taosArrayDestroy(pParam->pFields);
321,001✔
2128
      pParam->pFields = NULL;
321,001✔
2129
    }
2130
    if (pParam->pTagFields) {
321,001✔
2131
      taosArrayDestroy(pParam->pTagFields);
216,549✔
2132
      pParam->pTagFields = NULL;
216,549✔
2133
    }
2134
    taosMemFree(pParam);
321,001✔
2135
  }
2136
}
321,001✔
2137

2138
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
320,570✔
2139
  int32_t code = 0, lino = 0;
320,570✔
2140
  if (ppDst == NULL || pSrc == NULL) {
320,570✔
2141
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2142
  }
2143
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
320,570✔
2144
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
320,593✔
2145

2146
  (*ppDst)->suid = pSrc->suid;
320,597✔
2147
  (*ppDst)->sver = pSrc->sver;
321,001✔
2148
  (*ppDst)->tbType = pSrc->tbType;
319,793✔
2149
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
320,570✔
2150
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
320,593✔
2151

2152
  if (pSrc->stbname) {
320,166✔
2153
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
321,001✔
2154
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
320,593✔
2155
  }
2156

2157
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
318,569✔
2158
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
320,597✔
2159

2160
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
320,220✔
2161

2162
  if (pSrc->pFields && pSrc->pFields->size > 0) {
319,350✔
2163
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
320,224✔
2164
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
320,570✔
2165
  } else {
UNCOV
2166
    (*ppDst)->pFields = NULL;
×
2167
  }
2168
  
2169
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
320,162✔
2170
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
216,145✔
2171
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
216,549✔
2172
  } else {
2173
    (*ppDst)->pTagFields = NULL;
104,110✔
2174
  }
2175

2176
_exit:
320,628✔
2177

2178
  if (code != 0) {
320,628✔
2179
    if (*ppDst) {
×
2180
      destroyStreamInserterParam(*ppDst);
×
2181
      *ppDst = NULL;
×
2182
    }
2183
    
2184
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2185
  }
2186
  return code;
321,001✔
2187
}
2188

2189
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
436✔
2190
  return doDropStreamTable(pMsgCb, pOutput, pReq);
436✔
2191
}
2192

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