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

taosdata / TDengine / #4838

08 Nov 2025 04:37AM UTC coverage: 71.256% (+12.3%) from 58.963%
#4838

push

travis-ci

web-flow
test: adjust source list (#33506)

243241 of 341361 relevant lines covered (71.26%)

281946921.97 hits per line

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

68.57
/source/libs/executor/src/executor.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

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

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

48
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
529,534✔
49
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
529,534✔
50
    qError("gExecInfo is not initialized");
2,286✔
51
    return TSDB_CODE_APP_ERROR;
×
52
  }
53
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
527,248✔
54
  return TSDB_CODE_SUCCESS;
528,082✔
55
}
56

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

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

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

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

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

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

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

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

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

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

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

148
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
429,925,021✔
149
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
429,925,021✔
150
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
429,933,774✔
151
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
183,937,361✔
152
    if (pStreamScanInfo->pTableScanOp != NULL) {
183,940,378✔
153
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
183,941,034✔
154
      if (pScanInfo->base.dataReader != NULL) {
183,941,034✔
155
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
2,517,447✔
156
        if (code) {
2,513,433✔
157
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
158
          return code;
×
159
        }
160
      }
161
    }
162
  } else {
163
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
245,978,748✔
164
  }
165

166
  return 0;
240,931,146✔
167
}
168

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

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

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

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

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

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

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

201
  return code;
×
202
}
203

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

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

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

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

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

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

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

244
  SNode* pNode;
245
  FOREACH(pNode, pDescNode->pSlots) {
28,922,105✔
246
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
25,743,542✔
247
    if (pSlotDesc->output) {
25,743,542✔
248
      ++(*numOfCols);
25,737,670✔
249
    }
250
  }
251

252
  return pTaskInfo;
3,178,563✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
4,536,564✔
256
  if (streamInserterParam == NULL) {
4,536,564✔
257
    return TSDB_CODE_SUCCESS;
1,950,699✔
258
  }
259

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

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

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

276
  return TSDB_CODE_SUCCESS;
2,585,865✔
277
}
278

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

299
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
4,536,564✔
300
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
4,534,390✔
301
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
7,957✔
302
    goto _error;
8,712✔
303
  }
304

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

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

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

334
_error:
1,629,095✔
335

336
  if (code != TSDB_CODE_SUCCESS) {
4,535,794✔
337
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
8,712✔
338
    if (pInserterParam != NULL) {
8,712✔
339
      taosMemoryFree(pInserterParam);
×
340
    }
341
  }
342
  return code;
4,537,995✔
343
}
344

345
bool qNeedReset(qTaskInfo_t pInfo) {
52,513,647✔
346
  if (pInfo == NULL) {
52,513,647✔
347
    return false;
×
348
  }
349
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
52,513,647✔
350
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
52,513,647✔
351
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
52,513,647✔
352
    return false;
43,620✔
353
  }
354
  int32_t node = nodeType(pOperator->pPhyNode);
52,470,027✔
355
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
51,274,869✔
356
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
103,744,896✔
357
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
358
}
359

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

365
  pScanBaseInfo->readHandle.uid = pHandle->uid;
51,456,337✔
366
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
51,456,337✔
367
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
51,456,337✔
368
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
51,456,337✔
369
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
51,456,337✔
370
}
371

372
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
52,470,027✔
373
  if (pInfo == NULL) {
52,470,027✔
374
    return TSDB_CODE_INVALID_PARA;
×
375
  }
376
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
52,470,027✔
377
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
52,470,027✔
378

379
  void*           info = pOperator->info;
52,470,027✔
380
  STableScanBase* pScanBaseInfo = NULL;
52,470,027✔
381

382
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
52,470,027✔
383
    pScanBaseInfo = &((STableScanInfo*)info)->base;
1,195,158✔
384
    setReadHandle(handle, pScanBaseInfo);
1,195,158✔
385
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
51,274,869✔
386
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
50,261,179✔
387
    setReadHandle(handle, pScanBaseInfo);
50,261,179✔
388
  }
389

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

395
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
4,533,048✔
396
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
397
  if (msg == NULL) {
4,533,048✔
398
    return TSDB_CODE_INVALID_PARA;
×
399
  }
400

401
  *pTaskInfo = NULL;
4,533,048✔
402

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

418
  return code;
4,529,283✔
419
}
420

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

433
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
3,636,311✔
434

435
  uint64_t suid = 0;
3,636,311✔
436
  uint64_t uid = 0;
3,636,311✔
437
  int32_t  type = 0;
3,636,311✔
438
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
3,636,311✔
439

440
  // let's discard the tables those are not created according to the queried super table.
441
  SMetaReader mr = {0};
3,636,311✔
442
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
3,636,311✔
443
  for (int32_t i = 0; i < numOfUids; ++i) {
7,437,277✔
444
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
3,800,966✔
445
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
3,800,966✔
446

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

453
    tDecoderClear(&mr.coder);
3,800,966✔
454

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

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

481
      if (!qualified) {
3,275,748✔
482
        continue;
1,636,980✔
483
      }
484
    }
485

486
    // handle multiple partition
487
    void* tmp = taosArrayPush(qa, id);
2,152,586✔
488
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,152,586✔
489
  }
490

491
_end:
3,636,311✔
492

493
  pAPI->metaReaderFn.clearReader(&mr);
3,636,311✔
494
  (*ppArrayRes) = qa;
3,636,311✔
495

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

503
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
3,657,497✔
504
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,657,497✔
505
  const char*    id = GET_TASKID(pTaskInfo);
3,657,497✔
506
  int32_t        code = 0;
3,657,497✔
507

508
  if (isAdd) {
3,657,497✔
509
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
3,636,311✔
510
  }
511

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

519
  SStreamScanInfo* pScanInfo = pInfo->info;
3,657,497✔
520
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
3,657,497✔
521
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
7,467,409✔
522
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
3,809,912✔
523
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
3,809,912✔
524
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
3,809,912✔
525
    }
526
  }
527

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

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

551
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
3,636,311✔
552
    taosWLockLatch(&pTaskInfo->lock);
3,636,311✔
553

554
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
5,788,897✔
555
      uint64_t* uid = taosArrayGet(qa, i);
2,152,586✔
556
      if (!uid) {
2,152,586✔
557
        taosMemoryFree(keyBuf);
×
558
        taosArrayDestroy(qa);
×
559
        taosWUnLockLatch(&pTaskInfo->lock);
×
560
        return terrno;
×
561
      }
562
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
2,152,586✔
563

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

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

588
    taosWUnLockLatch(&pTaskInfo->lock);
3,636,311✔
589
    if (keyBuf != NULL) {
3,636,311✔
590
      taosMemoryFree(keyBuf);
×
591
    }
592

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

601
  return code;
3,657,497✔
602
}
603

604
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
2,147,483,647✔
605
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
606
                                    int32_t idx, bool* tbGet) {
607
  *tbGet = false;
2,147,483,647✔
608

609
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
2,147,483,647✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,147,483,647✔
613

614
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
2,147,483,647✔
615
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
616
  }
617

618
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
1,670,460,093✔
619
  if (!pSchemaInfo) {
1,670,056,368✔
620
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
621
    return terrno;
×
622
  }
623

624
  *sversion = pSchemaInfo->sw->version;
1,670,056,368✔
625
  *tversion = pSchemaInfo->tversion;
1,670,716,829✔
626
  *rversion = pSchemaInfo->rversion;
1,670,552,368✔
627
  if (pSchemaInfo->dbname) {
1,670,763,883✔
628
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
1,670,559,288✔
629
  } else {
630
    dbName[0] = 0;
×
631
  }
632
  if (pSchemaInfo->tablename) {
1,671,019,173✔
633
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
1,670,749,337✔
634
  } else {
635
    tableName[0] = 0;
24,190✔
636
  }
637

638
  *tbGet = true;
1,671,023,418✔
639

640
  return TSDB_CODE_SUCCESS;
1,670,894,457✔
641
}
642

643
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
2,147,483,647✔
644

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

652
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
259,039,296✔
653
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
259,039,296✔
654
  ((SExecTaskInfo*)tinfo)->paramSet = false;
259,039,296✔
655
}
259,039,296✔
656

657
int32_t qExecutorInit(void) {
44,168,439✔
658
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
44,168,439✔
659
  return TSDB_CODE_SUCCESS;
44,172,349✔
660
}
661

662
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
2,147,483,647✔
663
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
664
                        EOPTR_EXEC_MODEL model) {
665
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
2,147,483,647✔
666
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
2,147,483,647✔
667

668
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
2,147,483,647✔
669

670
  readHandle->uid = 0;
2,147,483,647✔
671
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
2,147,483,647✔
672
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
2,147,483,647✔
673
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
1,770,210✔
674
    goto _error;
215,894✔
675
  }
676

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

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

694
    SDataSinkNode* pSink = NULL;
2,147,483,647✔
695
    if (readHandle->localExec) {
2,147,483,647✔
696
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
7,455,135✔
697
      if (code != TSDB_CODE_SUCCESS) {
7,455,493✔
698
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
699
               (*pTask)->id.str);
700
        taosMemoryFree(pSinkManager);
×
701
        goto _error;
×
702
      }
703
    }
704

705
    // pSinkParam has been freed during create sinker.
706
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
2,147,483,647✔
707
                              (*pTask)->id.str, pSubplan->processOneBlock);
2,147,483,647✔
708
    if (code) {
2,147,483,647✔
709
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
4,086✔
710
    }
711
  }
712

713
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
2,147,483,647✔
714
         tstrerror(code));
715

716
_error:
208,272,391✔
717
  // if failed to add ref for all tables in this query, abort current query
718
  return code;
2,147,483,647✔
719
}
720

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

726
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
2,147,483,647✔
727
                     bool processOneBlock) {
728
  int32_t        code = TSDB_CODE_SUCCESS;
2,147,483,647✔
729
  int32_t        lino = 0;
2,147,483,647✔
730
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,147,483,647✔
731
  int64_t        threadId = taosGetSelfPthreadId();
2,147,483,647✔
732

733
  if (pLocal) {
2,147,483,647✔
734
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
2,147,483,647✔
735
  }
736

737
  taosArrayClear(pResList);
2,147,483,647✔
738

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

746
  if (pTaskInfo->cost.start == 0) {
2,147,483,647✔
747
    pTaskInfo->cost.start = taosGetTimestampUs();
2,147,483,647✔
748
  }
749

750
  if (isTaskKilled(pTaskInfo)) {
2,147,483,647✔
751
    atomic_store_64(&pTaskInfo->owner, 0);
2,756✔
752
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
2,756✔
753
    return pTaskInfo->code;
2,756✔
754
  }
755

756
  // error occurs, record the error code and return to client
757
  int32_t ret = setjmp(pTaskInfo->env);
2,147,483,647✔
758
  if (ret != TSDB_CODE_SUCCESS) {
2,147,483,647✔
759
    pTaskInfo->code = ret;
1,159,961✔
760
    (void)cleanUpUdfs();
1,159,961✔
761

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

765
    return pTaskInfo->code;
1,159,961✔
766
  }
767

768
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
2,147,483,647✔
769

770
  int32_t      current = 0;
2,147,483,647✔
771
  SSDataBlock* pRes = NULL;
2,147,483,647✔
772
  int64_t      st = taosGetTimestampUs();
2,147,483,647✔
773

774
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
2,147,483,647✔
775
    pTaskInfo->paramSet = true;
259,039,296✔
776
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
259,039,296✔
777
  } else {
778
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
2,147,483,647✔
779
  }
780

781
  QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
782
  code = blockDataCheck(pRes);
2,147,483,647✔
783
  QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
784

785
  if (pRes == NULL) {
2,147,483,647✔
786
    st = taosGetTimestampUs();
537,189,032✔
787
  }
788

789
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
2,147,483,647✔
790
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
2,147,483,647✔
791
    rowsThreshold = 4096;
2,147,483,647✔
792
  }
793

794
  int32_t blockIndex = 0;
2,147,483,647✔
795
  while (pRes != NULL) {
2,147,483,647✔
796
    SSDataBlock* p = NULL;
2,147,483,647✔
797
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
2,147,483,647✔
798
      SSDataBlock* p1 = NULL;
2,147,483,647✔
799
      code = createOneDataBlock(pRes, true, &p1);
2,147,483,647✔
800
      QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
801

802
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
2,147,483,647✔
803
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,147,483,647✔
804
      p = p1;
2,147,483,647✔
805
    } else {
806
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
1,755,838,119✔
807
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,755,831,233✔
808

809
      p = *(SSDataBlock**)tmp;
1,755,831,233✔
810
      code = copyDataBlock(p, pRes);
1,755,824,872✔
811
      QUERY_CHECK_CODE(code, lino, _end);
1,755,781,432✔
812
    }
813

814
    blockIndex += 1;
2,147,483,647✔
815

816
    current += p->info.rows;
2,147,483,647✔
817
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
2,147,483,647✔
818
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
819
    void* tmp = taosArrayPush(pResList, &p);
2,147,483,647✔
820
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,147,483,647✔
821

822
    if (current >= rowsThreshold || processOneBlock) {
2,147,483,647✔
823
      break;
824
    }
825

826
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
2,147,483,647✔
827
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
828
    code = blockDataCheck(pRes);
2,147,483,647✔
829
    QUERY_CHECK_CODE(code, lino, _end);
2,147,483,647✔
830
  }
831

832
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
2,147,483,647✔
833
    pTaskInfo->pSubplan->rowsThreshold -= current;
16,101,830✔
834
  }
835

836
  *hasMore = (pRes != NULL);
2,147,483,647✔
837
  uint64_t el = (taosGetTimestampUs() - st);
2,147,483,647✔
838

839
  pTaskInfo->cost.elapsedTime += el;
2,147,483,647✔
840
  if (NULL == pRes) {
2,147,483,647✔
841
    *useconds = pTaskInfo->cost.elapsedTime;
2,147,483,647✔
842
  }
843

844
_end:
2,147,483,647✔
845
  (void)cleanUpUdfs();
2,147,483,647✔
846

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

851
  atomic_store_64(&pTaskInfo->owner, 0);
2,147,483,647✔
852
  if (code) {
2,147,483,647✔
853
    pTaskInfo->code = code;
×
854
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
855
  }
856

857
  return pTaskInfo->code;
2,147,483,647✔
858
}
859

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

871
  taosArrayClear(pTaskInfo->pResultBlockList);
×
872
}
×
873

874
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
1,066,663,845✔
875
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,066,663,845✔
876
  int64_t        threadId = taosGetSelfPthreadId();
1,066,663,845✔
877
  int64_t        curOwner = 0;
1,066,674,523✔
878

879
  *pRes = NULL;
1,066,674,523✔
880

881
  // todo extract method
882
  taosRLockLatch(&pTaskInfo->lock);
1,066,675,671✔
883
  bool isKilled = isTaskKilled(pTaskInfo);
1,066,675,711✔
884
  if (isKilled) {
1,066,673,375✔
885
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
886

887
    taosRUnLockLatch(&pTaskInfo->lock);
×
888
    return pTaskInfo->code;
×
889
  }
890

891
  if (pTaskInfo->owner != 0) {
1,066,673,375✔
892
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
893
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
894

895
    taosRUnLockLatch(&pTaskInfo->lock);
×
896
    return pTaskInfo->code;
×
897
  }
898

899
  pTaskInfo->owner = threadId;
1,066,672,609✔
900
  taosRUnLockLatch(&pTaskInfo->lock);
1,066,673,360✔
901

902
  if (pTaskInfo->cost.start == 0) {
1,066,637,421✔
903
    pTaskInfo->cost.start = taosGetTimestampUs();
2,296,049✔
904
  }
905

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

916
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
1,066,648,963✔
917

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

931
  code = blockDataCheck(*pRes);
1,066,412,800✔
932
  if (code) {
1,066,633,568✔
933
    pTaskInfo->code = code;
×
934
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
935
  }
936

937
  uint64_t el = (taosGetTimestampUs() - st);
1,066,435,809✔
938

939
  pTaskInfo->cost.elapsedTime += el;
1,066,435,809✔
940
  if (NULL == *pRes) {
1,066,583,914✔
941
    *useconds = pTaskInfo->cost.elapsedTime;
179,972,504✔
942
  }
943

944
  (void)cleanUpUdfs();
1,066,589,308✔
945

946
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
1,066,477,778✔
947
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
1,066,681,780✔
948

949
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
1,066,673,744✔
950
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
951

952
  atomic_store_64(&pTaskInfo->owner, 0);
1,066,660,730✔
953
  return pTaskInfo->code;
1,066,680,632✔
954
}
955

956
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
887,316,921✔
957
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
887,316,921✔
958
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
887,332,195✔
959
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
887,335,711✔
960

961
  if (!tmp) {
887,319,645✔
962
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
963
    return terrno;
×
964
  }
965
  return TSDB_CODE_SUCCESS;
887,319,645✔
966
}
967

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

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

978
  return 0;
×
979
}
980

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

990
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
290,388✔
991
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
290,388✔
992

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

1015
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
290,388✔
1016
}
290,388✔
1017

1018
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
290,388✔
1019
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
290,388✔
1020
  if (pTaskInfo == NULL) {
290,388✔
1021
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1022
  }
1023

1024
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
290,388✔
1025

1026
  setTaskKilled(pTaskInfo, rspCode);
290,388✔
1027
  qStopTaskOperators(pTaskInfo);
290,388✔
1028

1029
  return TSDB_CODE_SUCCESS;
290,388✔
1030
}
1031

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

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

1045
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1046

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

1053
        taosMsleep(200);
×
1054

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

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

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

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

1085
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
2,147,483,647✔
1086
  STaskCostInfo* pSummary = &pTaskInfo->cost;
2,147,483,647✔
1087
  int64_t        idleTime = pSummary->start - pSummary->created;
2,147,483,647✔
1088

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

1104
void qDestroyTask(qTaskInfo_t qTaskHandle) {
2,147,483,647✔
1105
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
2,147,483,647✔
1106
  if (pTaskInfo == NULL) {
2,147,483,647✔
1107
    return;
36,237,061✔
1108
  }
1109

1110
  if (pTaskInfo->pRoot != NULL) {
2,147,483,647✔
1111
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
2,147,483,647✔
1112
  } else {
1113
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1114
  }
1115

1116
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
2,147,483,647✔
1117
  doDestroyTask(pTaskInfo);
2,147,483,647✔
1118
}
1119

1120
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
42,051,785✔
1121
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
42,051,785✔
1122
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
42,051,785✔
1123
}
1124

1125
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
3,178,563✔
1126
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,178,563✔
1127
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
3,178,563✔
1128

1129
  while (1) {
3,107,638✔
1130
    uint16_t type = pOperator->operatorType;
6,286,201✔
1131
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
6,286,201✔
1132
      *scanner = pOperator->info;
3,178,563✔
1133
      break;
3,178,563✔
1134
    } else {
1135
      pOperator = pOperator->pDownstream[0];
3,107,638✔
1136
    }
1137
  }
1138
}
3,178,563✔
1139

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

1153
void* qExtractReaderFromTmqScanner(void* scanner) {
3,178,563✔
1154
  SStreamScanInfo* pInfo = scanner;
3,178,563✔
1155
  return (void*)pInfo->tqReader;
3,178,563✔
1156
}
1157

1158
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
7,245,171✔
1159
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
7,245,171✔
1160
  return pTaskInfo->streamInfo.schema;
7,245,171✔
1161
}
1162

1163
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
7,243,312✔
1164
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
7,243,312✔
1165
  return pTaskInfo->streamInfo.tbName;
7,243,312✔
1166
}
1167

1168
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
7,977,762✔
1169
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
7,977,762✔
1170
  return &pTaskInfo->streamInfo.btMetaRsp;
7,977,762✔
1171
}
1172

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

1184
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
7,621,818✔
1185
  memset(pCond, 0, sizeof(SQueryTableDataCond));
7,621,818✔
1186
  pCond->order = TSDB_ORDER_ASC;
7,621,818✔
1187
  pCond->numOfCols = pMtInfo->schema->nCols;
7,633,285✔
1188
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
7,638,089✔
1189
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
7,627,708✔
1190
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
7,629,567✔
1191
    taosMemoryFreeClear(pCond->colList);
11,467✔
1192
    taosMemoryFreeClear(pCond->pSlotList);
×
1193
    return terrno;
×
1194
  }
1195

1196
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
7,623,677✔
1197
  pCond->suid = pMtInfo->suid;
7,631,113✔
1198
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
7,635,144✔
1199
  pCond->startVersion = -1;
7,635,144✔
1200
  pCond->endVersion = sContext->snapVersion;
7,629,567✔
1201

1202
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
48,516,649✔
1203
    SColumnInfo* pColInfo = &pCond->colList[i];
40,871,124✔
1204
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
40,871,897✔
1205
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
40,878,539✔
1206
    if (pMtInfo->pExtSchemas != NULL) {
40,870,790✔
1207
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
475,622✔
1208
    }
1209
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
40,883,343✔
1210
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
40,885,223✔
1211

1212
    pCond->pSlotList[i] = i;
40,874,821✔
1213
  }
1214

1215
  return TSDB_CODE_SUCCESS;
7,641,034✔
1216
}
1217

1218
void qStreamSetOpen(qTaskInfo_t tinfo) {
1,051,506,464✔
1219
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,051,506,464✔
1220
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
1,051,506,464✔
1221
  pOperator->status = OP_NOT_OPENED;
1,051,663,661✔
1222
}
1,051,650,028✔
1223

1224
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
182,006,678✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
182,006,678✔
1226
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
182,006,678✔
1227
}
182,017,047✔
1228

1229
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
192,041,899✔
1230
  int32_t        code = TSDB_CODE_SUCCESS;
192,041,899✔
1231
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
192,041,899✔
1232
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
192,041,899✔
1233

1234
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
192,064,826✔
1235
  const char*    id = GET_TASKID(pTaskInfo);
192,051,875✔
1236

1237
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
192,042,197✔
1238
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
179,100,097✔
1239
    if (pOperator == NULL || code != 0) {
179,106,433✔
1240
      return code;
25✔
1241
    }
1242

1243
    SStreamScanInfo* pInfo = pOperator->info;
179,113,898✔
1244
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
179,116,987✔
1245
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
179,109,202✔
1246
    walReaderVerifyOffset(pWalReader, pOffset);
179,116,083✔
1247
  }
1248
  // if pOffset equal to current offset, means continue consume
1249
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
192,049,670✔
1250
    return 0;
174,646,693✔
1251
  }
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
17,379,025✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
9,568,187✔
1255
    if (pOperator == NULL || code != 0) {
9,567,082✔
1256
      return code;
×
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
9,578,607✔
1260
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
9,570,775✔
1261
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
9,571,921✔
1262
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
9,576,819✔
1263

1264
    if (pOffset->type == TMQ_OFFSET__LOG) {
9,566,695✔
1265
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
7,302,239✔
1266
      pScanBaseInfo->dataReader = NULL;
7,295,309✔
1267

1268
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
7,297,421✔
1269
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
7,296,222✔
1270
      walReaderVerifyOffset(pWalReader, pOffset);
7,293,649✔
1271
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
7,301,456✔
1272
      if (code < 0) {
7,303,286✔
1273
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
92,630✔
1274
        return code;
92,630✔
1275
      }
1276
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
2,271,481✔
1277
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1278
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1279
      int64_t uid = pOffset->uid;
2,277,460✔
1280
      int64_t ts = pOffset->ts;
2,274,465✔
1281
      int32_t index = 0;
2,266,428✔
1282

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

1293
      if (uid == 0) {
2,266,964✔
1294
        if (numOfTables != 0) {
2,214,246✔
1295
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
400,979✔
1296
          if (!tmp) {
400,979✔
1297
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1298
            taosRUnLockLatch(&pTaskInfo->lock);
×
1299
            return terrno;
×
1300
          }
1301
          if (tmp) uid = tmp->uid;
400,979✔
1302
          ts = INT64_MIN;
400,979✔
1303
          pScanInfo->currentTable = 0;
400,979✔
1304
        } else {
1305
          taosRUnLockLatch(&pTaskInfo->lock);
1,813,267✔
1306
          qError("no table in table list, %s", id);
1,814,037✔
1307
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
1,818,132✔
1308
        }
1309
      }
1310
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
451,820✔
1311

1312
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
459,328✔
1313
             pInfo->pTableScanOp->resultInfo.totalRows);
1314
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
459,328✔
1315

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

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

1330
      STableKeyInfo keyInfo = {.uid = uid};
459,328✔
1331
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
459,328✔
1332

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

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

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

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

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

1375
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1376
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
7,810,838✔
1377
      SStreamRawScanInfo* pInfo = pOperator->info;
7,652,768✔
1378
      SSnapContext*       sContext = pInfo->sContext;
7,652,768✔
1379
      SOperatorInfo*      p = NULL;
7,652,768✔
1380

1381
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
7,652,768✔
1382
      if (code != 0) {
7,652,768✔
1383
        return code;
×
1384
      }
1385

1386
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
7,652,768✔
1387

1388
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
7,652,768✔
1389
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1390
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1391
      }
1392

1393
      SMetaTableInfo mtInfo = {0};
7,650,873✔
1394
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
7,652,768✔
1395
      if (code != 0) {
7,645,332✔
1396
        destroyMetaTableInfo(&mtInfo);
1397
        return code;
×
1398
      }
1399
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
7,645,332✔
1400
      pInfo->dataReader = NULL;
7,630,415✔
1401

1402
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
7,634,921✔
1403
      tableListClear(pTableListInfo);
7,630,854✔
1404

1405
      if (mtInfo.uid == 0) {
7,652,768✔
1406
        destroyMetaTableInfo(&mtInfo);
1407
        goto end;  // no data
11,734✔
1408
      }
1409

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

1423
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
7,637,316✔
1424
      if (code != TSDB_CODE_SUCCESS) {
7,641,034✔
1425
        destroyMetaTableInfo(&mtInfo);
1426
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1427
        return code;
×
1428
      }
1429

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

1444
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
15,282,068✔
1445
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
7,641,034✔
1446
      if (code != TSDB_CODE_SUCCESS) {
7,631,739✔
1447
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1448
        destroyMetaTableInfo(&mtInfo);
1449
        return code;
×
1450
      }
1451

1452
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
7,631,739✔
1453
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
7,634,350✔
1454
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1455
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
7,625,040✔
1456
      pTaskInfo->streamInfo.schema = mtInfo.schema;
7,633,583✔
1457
      taosMemoryFreeClear(mtInfo.pExtSchemas);
7,635,457✔
1458

1459
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
7,635,457✔
1460
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
155,857✔
1461
      SStreamRawScanInfo* pInfo = pOperator->info;
53,243✔
1462
      SSnapContext*       sContext = pInfo->sContext;
53,243✔
1463
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
53,243✔
1464
      if (code != 0) {
48,435✔
1465
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1466
        return code;
×
1467
      }
1468
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
48,435✔
1469
             id);
1470
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
102,614✔
1471
      SStreamRawScanInfo* pInfo = pOperator->info;
106,699✔
1472
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
106,699✔
1473
      pInfo->dataReader = NULL;
106,699✔
1474
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
106,699✔
1475
    }
1476
  }
1477

1478
end:
15,482,350✔
1479
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
15,481,632✔
1480
  return 0;
15,482,694✔
1481
}
1482

1483
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
1,838,327,373✔
1484
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
1,838,327,373✔
1485
  if (pMsg->info.ahandle == NULL) {
1,838,503,480✔
1486
    qError("pMsg->info.ahandle is NULL");
×
1487
    return;
×
1488
  }
1489

1490
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
1,838,265,058✔
1491
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1492

1493
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
1,838,320,845✔
1494

1495
  if (pMsg->contLen > 0) {
1,838,419,244✔
1496
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
1,838,006,595✔
1497
    if (buf.pData == NULL) {
1,838,048,425✔
1498
      pMsg->code = terrno;
×
1499
    } else {
1500
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
1,838,048,425✔
1501
    }
1502
  }
1503

1504
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
1,838,624,201✔
1505
  rpcFreeCont(pMsg->pCont);
1,838,615,279✔
1506
  destroySendMsgInfo(pSendInfo);
1,838,290,800✔
1507
}
1508

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

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

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

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

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

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

1537
  taosArrayDestroy(plist);
×
1538

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

1547
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
26,569,988✔
1548
  int32_t        code = TSDB_CODE_SUCCESS;
26,569,988✔
1549
  int32_t        lino = 0;
26,569,988✔
1550
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
26,569,988✔
1551

1552
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
26,577,298✔
1553
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1554
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1555

1556
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1557
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1558
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
26,527,571✔
1559
    STableScanInfo* pScanInfo = pOperator->info;
13,265,650✔
1560

1561
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
13,270,568✔
1562
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
13,283,452✔
1563
  } else {
1564
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
13,274,061✔
1565
      code = extractTableList(pList, pOperator->pDownstream[0]);
13,294,277✔
1566
    }
1567
  }
1568

1569
_end:
×
1570
  if (code != TSDB_CODE_SUCCESS) {
26,553,906✔
1571
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1572
  }
1573
  return code;
26,553,014✔
1574
}
1575

1576
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
13,289,473✔
1577
  if (pList == NULL) {
13,289,473✔
1578
    return TSDB_CODE_INVALID_PARA;
×
1579
  }
1580

1581
  *pList = NULL;
13,289,473✔
1582
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
13,295,033✔
1583
  if (pArray == NULL) {
13,284,547✔
1584
    return terrno;
×
1585
  }
1586

1587
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
13,284,547✔
1588
  if (code == 0) {
13,278,959✔
1589
    *pList = pArray;
13,278,959✔
1590
  } else {
1591
    taosArrayDestroy(pArray);
×
1592
  }
1593
  return code;
13,283,771✔
1594
}
1595

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

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

1612
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1613
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1614

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

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

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

1631
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
543,469,609✔
1632
  int32_t code = 0;
543,469,609✔
1633

1634
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
543,469,609✔
1635
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
543,405,726✔
1636

1637
  if (pOper->fpSet.resetStateFn) {
543,420,433✔
1638
    code = pOper->fpSet.resetStateFn(pOper);
543,433,475✔
1639
  }
1640
  pOper->status = OP_NOT_OPENED;
543,361,919✔
1641
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
948,660,521✔
1642
    code = clearStatesForOperator(pOper->pDownstream[i]);
405,172,104✔
1643
  }
1644
  return code;
543,532,012✔
1645
}
1646

1647
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
138,283,529✔
1648
  int32_t        code = 0;
138,283,529✔
1649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
138,283,529✔
1650
  SOperatorInfo* pOper = pTaskInfo->pRoot;
138,283,529✔
1651
  pTaskInfo->code = TSDB_CODE_SUCCESS;
138,283,529✔
1652
  code = clearStatesForOperator(pOper);
138,280,653✔
1653
  return code;
138,282,070✔
1654
}
1655

1656
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
173,642,137✔
1657
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
173,642,137✔
1658
  int64_t        threadId = taosGetSelfPthreadId();
173,642,137✔
1659
  int64_t        curOwner = 0;
173,639,901✔
1660

1661
  *ppRes = NULL;
173,639,901✔
1662

1663
  // todo extract method
1664
  taosRLockLatch(&pTaskInfo->lock);
173,639,901✔
1665
  bool isKilled = isTaskKilled(pTaskInfo);
173,642,137✔
1666
  if (isKilled) {
173,639,943✔
1667
    // clearStreamBlock(pTaskInfo->pRoot);
1668
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1669

1670
    taosRUnLockLatch(&pTaskInfo->lock);
×
1671
    return pTaskInfo->code;
×
1672
  }
1673

1674
  if (pTaskInfo->owner != 0) {
173,639,943✔
1675
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1676
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1677

1678
    taosRUnLockLatch(&pTaskInfo->lock);
×
1679
    return pTaskInfo->code;
×
1680
  }
1681

1682
  pTaskInfo->owner = threadId;
173,637,722✔
1683
  taosRUnLockLatch(&pTaskInfo->lock);
173,639,916✔
1684

1685
  if (pTaskInfo->cost.start == 0) {
173,641,666✔
1686
    pTaskInfo->cost.start = taosGetTimestampUs();
2,667,152✔
1687
  }
1688

1689
  // error occurs, record the error code and return to client
1690
  int32_t ret = setjmp(pTaskInfo->env);
173,641,666✔
1691
  if (ret != TSDB_CODE_SUCCESS) {
233,200,864✔
1692
    pTaskInfo->code = ret;
59,573,054✔
1693
    (void)cleanUpUdfs();
59,573,054✔
1694
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
59,573,054✔
1695
    atomic_store_64(&pTaskInfo->owner, 0);
59,573,054✔
1696
    return pTaskInfo->code;
59,573,054✔
1697
  }
1698

1699
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
173,627,810✔
1700

1701
  int64_t st = taosGetTimestampUs();
173,641,396✔
1702

1703
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
173,641,396✔
1704
  if (code) {
114,070,546✔
1705
    pTaskInfo->code = code;
×
1706
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1707
  } else {
1708
    *finished = *ppRes == NULL;
114,070,546✔
1709
    code = blockDataCheck(*ppRes);
114,070,546✔
1710
  }
1711
  if (code) {
114,065,550✔
1712
    pTaskInfo->code = code;
×
1713
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1714
  }
1715

1716
  uint64_t el = (taosGetTimestampUs() - st);
114,066,949✔
1717

1718
  pTaskInfo->cost.elapsedTime += el;
114,066,949✔
1719
  if (NULL == *ppRes) {
114,066,949✔
1720
    *useconds = pTaskInfo->cost.elapsedTime;
76,755,143✔
1721
  }
1722

1723
  (void)cleanUpUdfs();
114,066,949✔
1724

1725
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
114,070,546✔
1726
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
114,070,546✔
1727

1728
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
114,070,546✔
1729
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1730

1731
  atomic_store_64(&pTaskInfo->owner, 0);
114,070,546✔
1732
  return pTaskInfo->code;
114,070,546✔
1733
}
1734

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

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

1749
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
12,082,857✔
1750
  SReadHandle    pHandle = {.vnode = pVnode};
12,083,023✔
1751
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
12,079,219✔
1752

1753
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
12,081,565✔
1754
  if (code != 0) {
12,085,184✔
1755
    tableListDestroy(pList);
×
1756
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1757
    return code;
×
1758
  }
1759
  *pTableListInfo = pList;
12,085,184✔
1760
  return 0;
12,085,184✔
1761
}
1762

1763
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
110,510,397✔
1764
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
110,510,397✔
1765
    return TSDB_CODE_INVALID_PARA;
×
1766
  }
1767
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
110,518,967✔
1768
    *size = 0;
82,682,179✔
1769
    *pKeyInfo = NULL;
82,682,179✔
1770
    return 0;
82,682,179✔
1771
  }
1772
  if (currentGroupId == -1) {
27,846,285✔
1773
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
2,471,937✔
1774
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
2,471,937✔
1775
    return 0;
2,471,937✔
1776
  }
1777
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
25,374,348✔
1778
}
1779

1780
int32_t  qStreamSetTableList(void** pTableListInfo, uint64_t uid, uint64_t gid){
8,573,980✔
1781
  if (*pTableListInfo == NULL) {
8,573,980✔
1782
    *pTableListInfo = tableListCreate();
2,699,172✔
1783
    if (*pTableListInfo == NULL) {
2,699,172✔
1784
      return terrno;
×
1785
    }
1786
  }
1787
  return tableListAddTableInfo(*pTableListInfo, uid, gid);
8,573,980✔
1788
}
1789

1790
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
100,429,905✔
1791
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
100,429,905✔
1792
    return 0;
82,132,408✔
1793
  }
1794
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
20,550,931✔
1795
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
20,232,371✔
1796

1797
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
20,232,371✔
1798
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
20,232,371✔
1799
      return i;
17,990,450✔
1800
    }
1801
  }
1802
  return -1;
318,560✔
1803
}
1804

1805
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
15,434,087✔
1806

1807
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
50,381,726✔
1808

1809
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
8,442,557✔
1810
void    qStreamSetTableListGroupNum(const void* pTableList, int32_t groupNum) {((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; }
2,699,172✔
1811
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
1,944,565✔
1812

1813
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
13,335,746✔
1814

1815
void streamDestroyExecTask(qTaskInfo_t tInfo) {
21,242,935✔
1816
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
21,242,935✔
1817
  qDestroyTask(tInfo);
21,242,935✔
1818
}
21,242,935✔
1819

1820
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
6,833,076✔
1821
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
6,833,076✔
1822
}
1823

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

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

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

1886
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
39,630,879✔
1887
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
39,630,879✔
1888
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
39,630,879✔
1889
  int32_t        code = 0;
39,630,879✔
1890
  SNode*         pNode = NULL;
39,630,879✔
1891
  if (!pForceOutputCols) return 0;
39,630,879✔
1892
  if (!*pRes) {
3,614✔
1893
    code = createDataBlock(pRes);
3,614✔
1894
  }
1895

1896
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
3,614✔
1897
    int32_t idx = 0;
3,614✔
1898
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
10,842✔
1899
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
7,228✔
1900
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
7,228✔
1901
      colInfo.info.precision = pCol->type.precision;
7,228✔
1902
      colInfo.info.scale = pCol->type.scale;
7,228✔
1903
      code = blockDataAppendColInfo(*pRes, &colInfo);
7,228✔
1904
      if (code != 0) break;
7,228✔
1905
    }
1906
  }
1907

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

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

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

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

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

2039
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
2,587,296✔
2040
  if (pParam) {
2,587,296✔
2041
    if (pParam->tbname) {
2,587,296✔
2042
      taosMemFree(pParam->tbname);
2,587,296✔
2043
      pParam->tbname = NULL;
2,587,296✔
2044
    }
2045
    if (pParam->stbname) {
2,587,296✔
2046
      taosMemFree(pParam->stbname);
2,587,296✔
2047
      pParam->stbname = NULL;
2,587,296✔
2048
    }
2049
    if (pParam->dbFName) {
2,587,296✔
2050
      taosMemFree(pParam->dbFName);
2,587,296✔
2051
      pParam->dbFName = NULL;
2,587,296✔
2052
    }
2053
    if (pParam->pFields) {
2,587,296✔
2054
      taosArrayDestroy(pParam->pFields);
2,587,296✔
2055
      pParam->pFields = NULL;
2,587,296✔
2056
    }
2057
    if (pParam->pTagFields) {
2,587,296✔
2058
      taosArrayDestroy(pParam->pTagFields);
1,823,959✔
2059
      pParam->pTagFields = NULL;
1,823,959✔
2060
    }
2061
    taosMemFree(pParam);
2,587,296✔
2062
  }
2063
}
2,587,296✔
2064

2065
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
2,585,871✔
2066
  int32_t code = 0, lino = 0;
2,585,871✔
2067
  if (ppDst == NULL || pSrc == NULL) {
2,585,871✔
2068
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2069
  }
2070
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
2,585,871✔
2071
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
2,585,116✔
2072

2073
  (*ppDst)->suid = pSrc->suid;
2,583,691✔
2074
  (*ppDst)->sver = pSrc->sver;
2,585,116✔
2075
  (*ppDst)->tbType = pSrc->tbType;
2,585,871✔
2076
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
2,585,871✔
2077
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
2,587,296✔
2078

2079
  if (pSrc->stbname) {
2,585,871✔
2080
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
2,585,116✔
2081
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
2,585,871✔
2082
  }
2083

2084
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
2,588,051✔
2085
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
2,587,269✔
2086

2087
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
2,583,664✔
2088

2089
  if (pSrc->pFields && pSrc->pFields->size > 0) {
2,587,269✔
2090
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
2,582,922✔
2091
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
2,585,116✔
2092
  } else {
2093
    (*ppDst)->pFields = NULL;
2,180✔
2094
  }
2095
  
2096
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
2,582,895✔
2097
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
1,821,765✔
2098
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
1,823,959✔
2099
  } else {
2100
    (*ppDst)->pTagFields = NULL;
763,310✔
2101
  }
2102

2103
_exit:
2,587,269✔
2104

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

2116
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
3,694✔
2117
  return doDropStreamTable(pMsgCb, pOutput, pReq);
3,694✔
2118
}
2119

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