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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,987,570✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,987,570✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,988,074✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,399,401✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,399,401✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,399,482✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,399,273✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,739✔
158
        if (code) {
57,739✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,588,441✔
166
  }
167

168
  return 0;
10,856,793✔
169
}
170

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

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

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

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

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

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

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

203
  return code;
×
204
}
205

206
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
138,259✔
207
  if (msg == NULL) {  // create raw scan
138,259✔
208
    SExecTaskInfo* pTaskInfo = NULL;
23,382✔
209

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

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

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

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

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

242
  return pTaskInfo;
116,515✔
243
}
244

245
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
703,542✔
246
  if (streamInserterParam == NULL) {
703,542✔
247
    return TSDB_CODE_SUCCESS;
381,323✔
248
  }
249

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

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

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

266
  return TSDB_CODE_SUCCESS;
321,747✔
267
}
268

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

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

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

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

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

324
_error:
142,670✔
325

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

335
bool qNeedReset(qTaskInfo_t pInfo) {
5,763,437✔
336
  if (pInfo == NULL) {
5,763,437✔
337
    return false;
×
338
  }
339
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,763,437✔
340
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,763,437✔
341
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,763,437✔
342
    return false;
8,832✔
343
  }
344
  int32_t node = nodeType(pOperator->pPhyNode);
5,754,605✔
345
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,477,824✔
346
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,232,429✔
347
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
348
}
349

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

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

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

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

373
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,754,605✔
374
    pScanBaseInfo = &((STableScanInfo*)info)->base;
276,781✔
375
    setReadHandle(handle, pScanBaseInfo);
276,781✔
376
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,477,824✔
377
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,933,980✔
378
    setReadHandle(handle, pScanBaseInfo);
4,933,980✔
379
  }
380

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

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

392
  *pTaskInfo = NULL;
703,542✔
393

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

409
  return code;
703,542✔
410
}
411

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

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

425
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
59,264✔
426

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

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

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
59,264✔
437

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

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

447
  locked = 1;
59,264✔
448
  for (int32_t i = 0; i < numOfUids; ++i) {
118,978✔
449
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
59,714✔
450
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
59,714✔
451

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

458
    tDecoderClear(&mr.coder);
59,714✔
459

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

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

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

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

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

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

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

515
  // handle multiple partition
516

517
_end:
59,264✔
518

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

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

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

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

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

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

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

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

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

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

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

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

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

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

630
  return code;
59,906✔
631
}
632

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

638
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
264,656,544✔
639
    return TSDB_CODE_INVALID_PARA;
×
640
  }
641
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
264,661,683✔
642

643
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
264,661,683✔
644
    return TSDB_CODE_SUCCESS;
151,690,174✔
645
  }
646

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

653
  *sversion = pSchemaInfo->sw->version;
112,952,408✔
654
  *tversion = pSchemaInfo->tversion;
112,980,333✔
655
  *rversion = pSchemaInfo->rversion;
112,974,869✔
656
  if (pSchemaInfo->dbname) {
112,981,807✔
657
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
112,981,832✔
658
  } else {
659
    dbName[0] = 0;
×
660
  }
661
  if (pSchemaInfo->tablename) {
112,995,151✔
662
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
112,985,790✔
663
  } else {
664
    tableName[0] = 0;
3,765✔
665
  }
666

667
  *tbGet = true;
112,994,618✔
668

669
  return TSDB_CODE_SUCCESS;
112,988,418✔
670
}
671

672
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
151,682,348✔
673

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

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

686
int32_t qExecutorInit(void) {
4,720,898✔
687
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,720,898✔
688
  return TSDB_CODE_SUCCESS;
4,721,160✔
689
}
690

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

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

699
  readHandle->uid = 0;
153,545,413✔
700
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
153,560,089✔
701
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
153,470,720✔
702
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
90,986✔
703
    goto _error;
25,944✔
704
  }
705

706
  if (handle) {
153,398,863✔
707
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
153,325,221✔
708
    void*           pSinkManager = NULL;
153,350,757✔
709
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
153,169,867✔
710
    if (code != TSDB_CODE_SUCCESS) {
153,326,899✔
711
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
712
      goto _error;
×
713
    }
714

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

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

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

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

745
_error:
8,988,313✔
746
  // if failed to add ref for all tables in this query, abort current query
747
  return code;
153,550,707✔
748
}
749

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

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

762
  if (pLocal) {
171,744,366✔
763
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
165,577,487✔
764
  }
765

766
  taosArrayClear(pResList);
171,723,993✔
767

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

775
  if (pTaskInfo->cost.start == 0) {
171,732,250✔
776
    pTaskInfo->cost.start = taosGetTimestampUs();
151,984,568✔
777
  }
778

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

785
  // error occurs, record the error code and return to client
786
  int32_t ret = setjmp(pTaskInfo->env);
171,737,849✔
787
  if (ret != TSDB_CODE_SUCCESS) {
171,867,772✔
788
    pTaskInfo->code = ret;
157,774✔
789
    (void)cleanUpUdfs();
157,774✔
790

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

794
    return pTaskInfo->code;
157,774✔
795
  }
796

797
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
171,709,998✔
798

799
  int32_t      current = 0;
171,710,884✔
800
  SSDataBlock* pRes = NULL;
171,710,884✔
801
  int64_t      st = taosGetTimestampUs();
171,750,584✔
802

803
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
171,750,584✔
804
    pTaskInfo->paramSet = true;
695,061✔
805
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
695,061✔
806
  } else {
807
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
171,051,201✔
808
  }
809

810
  QUERY_CHECK_CODE(code, lino, _end);
171,590,477✔
811
  code = blockDataCheck(pRes);
171,590,477✔
812
  QUERY_CHECK_CODE(code, lino, _end);
171,589,341✔
813

814
  if (pRes == NULL) {
171,589,341✔
815
    st = taosGetTimestampUs();
29,659,089✔
816
  }
817

818
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
171,592,690✔
819
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
171,591,638✔
820
    rowsThreshold = 4096;
170,879,881✔
821
  }
822

823
  int32_t blockIndex = 0;
171,592,409✔
824
  while (pRes != NULL) {
553,539,644✔
825
    SSDataBlock* p = NULL;
396,020,070✔
826
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
395,998,443✔
827
      SSDataBlock* p1 = NULL;
291,695,841✔
828
      code = createOneDataBlock(pRes, true, &p1);
291,695,904✔
829
      QUERY_CHECK_CODE(code, lino, _end);
291,689,524✔
830

831
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
291,689,524✔
832
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
291,696,344✔
833
      p = p1;
291,696,344✔
834
    } else {
835
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
104,324,891✔
836
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
104,325,668✔
837

838
      p = *(SSDataBlock**)tmp;
104,325,668✔
839
      code = copyDataBlock(p, pRes);
104,325,460✔
840
      QUERY_CHECK_CODE(code, lino, _end);
104,324,723✔
841
    }
842

843
    blockIndex += 1;
396,018,056✔
844

845
    current += p->info.rows;
396,018,056✔
846
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
396,019,200✔
847
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
848
    void* tmp = taosArrayPush(pResList, &p);
396,020,075✔
849
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
396,020,075✔
850

851
    if (current >= rowsThreshold || processOneBlock) {
396,020,075✔
852
      break;
853
    }
854

855
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
381,946,893✔
856
    QUERY_CHECK_CODE(code, lino, _end);
381,935,518✔
857
    code = blockDataCheck(pRes);
381,935,518✔
858
    QUERY_CHECK_CODE(code, lino, _end);
381,894,121✔
859
  }
860

861
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
171,592,756✔
862
    pTaskInfo->pSubplan->rowsThreshold -= current;
708,429✔
863
  }
864

865
  *hasMore = (pRes != NULL);
171,595,083✔
866
  uint64_t el = (taosGetTimestampUs() - st);
171,574,073✔
867

868
  pTaskInfo->cost.elapsedTime += el;
171,574,073✔
869
  if (NULL == pRes) {
171,587,015✔
870
    *useconds = pTaskInfo->cost.elapsedTime;
157,513,833✔
871
  }
872

873
_end:
171,567,914✔
874
  (void)cleanUpUdfs();
171,580,094✔
875

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

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

886
  return pTaskInfo->code;
171,603,020✔
887
}
888

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

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

903
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
50,013,228✔
904
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
50,013,228✔
905
  int64_t        threadId = taosGetSelfPthreadId();
50,013,228✔
906
  int64_t        curOwner = 0;
50,013,783✔
907

908
  *pRes = NULL;
50,013,783✔
909

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

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

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

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

928
  pTaskInfo->owner = threadId;
50,013,938✔
929
  taosRUnLockLatch(&pTaskInfo->lock);
50,013,894✔
930

931
  if (pTaskInfo->cost.start == 0) {
50,014,116✔
932
    pTaskInfo->cost.start = taosGetTimestampUs();
94,359✔
933
  }
934

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

945
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
50,012,511✔
946

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

960
  code = blockDataCheck(*pRes);
49,962,706✔
961
  if (code) {
50,006,997✔
962
    pTaskInfo->code = code;
×
963
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
964
  }
965

966
  uint64_t el = (taosGetTimestampUs() - st);
49,969,538✔
967

968
  pTaskInfo->cost.elapsedTime += el;
49,969,538✔
969
  if (NULL == *pRes) {
50,000,669✔
970
    *useconds = pTaskInfo->cost.elapsedTime;
4,085,876✔
971
  }
972

973
  (void)cleanUpUdfs();
50,001,444✔
974

975
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
50,013,672✔
976
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
50,014,116✔
977

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

981
  atomic_store_64(&pTaskInfo->owner, 0);
50,013,615✔
982
  return pTaskInfo->code;
50,013,938✔
983
}
984

985
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
49,808,876✔
986
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
49,808,876✔
987
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
49,809,261✔
988
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
49,808,812✔
989

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

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

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

1007
  return 0;
×
1008
}
1009

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

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

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

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

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

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

1055
  setTaskKilled(pTaskInfo, rspCode);
40,194✔
1056
  qStopTaskOperators(pTaskInfo);
40,194✔
1057

1058
  return TSDB_CODE_SUCCESS;
40,194✔
1059
}
1060

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

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

1074
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1075

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

1082
        taosMsleep(200);
×
1083

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

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

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

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

1114
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
154,264,052✔
1115
  STaskCostInfo* pSummary = &pTaskInfo->cost;
154,264,052✔
1116
  int64_t        idleTime = pSummary->start - pSummary->created;
154,265,315✔
1117

1118
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
154,263,167✔
1119
  if (pSummary->pRecoder != NULL) {
154,263,897✔
1120
    qDebug(
112,116,914✔
1121
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1122
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1123
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1124
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1125
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1126
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1127
  } else {
1128
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
42,145,574✔
1129
           pSummary->elapsedTime / 1000.0);
1130
  }
1131
}
154,262,488✔
1132

1133
void qDestroyTask(qTaskInfo_t qTaskHandle) {
161,380,550✔
1134
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
161,380,550✔
1135
  if (pTaskInfo == NULL) {
161,380,550✔
1136
    return;
7,118,442✔
1137
  }
1138

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

1145
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
154,264,613✔
1146
  doDestroyTask(pTaskInfo);
154,264,606✔
1147
}
1148

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

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

1158
  while (1) {
115,448✔
1159
    uint16_t type = pOperator->operatorType;
231,963✔
1160
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
231,963✔
1161
      *scanner = pOperator->info;
116,515✔
1162
      break;
116,515✔
1163
    } else {
1164
      pOperator = pOperator->pDownstream[0];
115,448✔
1165
    }
1166
  }
1167
}
116,515✔
1168

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

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

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

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

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

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

1212
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
150,895✔
1213
  pCond->suid = pMtInfo->suid;
151,021✔
1214
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
150,965✔
1215
  pCond->startVersion = -1;
151,077✔
1216
  pCond->endVersion = sContext->snapVersion;
151,077✔
1217

1218
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
970,309✔
1219
    SColumnInfo* pColInfo = &pCond->colList[i];
819,414✔
1220
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
819,218✔
1221
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
819,400✔
1222
    if (pMtInfo->pExtSchemas != NULL) {
819,456✔
1223
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,660✔
1224
    }
1225
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
819,400✔
1226
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
819,358✔
1227

1228
    pCond->pSlotList[i] = i;
819,316✔
1229
  }
1230

1231
  return TSDB_CODE_SUCCESS;
151,077✔
1232
}
1233

1234
void qStreamSetOpen(qTaskInfo_t tinfo) {
49,707,389✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
49,707,389✔
1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
49,707,389✔
1237
  pOperator->status = OP_NOT_OPENED;
49,713,461✔
1238
}
49,713,836✔
1239

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

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

1250
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,562,978✔
1251
  const char*    id = GET_TASKID(pTaskInfo);
4,562,606✔
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,562,148✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,295,177✔
1255
    if (pOperator == NULL || code != 0) {
4,294,805✔
1256
      return code;
21✔
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
4,294,784✔
1260
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,294,713✔
1261
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,295,072✔
1262
    walReaderVerifyOffset(pWalReader, pOffset);
4,295,658✔
1263
  }
1264
  // if pOffset equal to current offset, means continue consume
1265
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,561,554✔
1266
    return 0;
4,120,152✔
1267
  }
1268

1269
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
441,568✔
1270
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
286,616✔
1271
    if (pOperator == NULL || code != 0) {
286,498✔
1272
      return code;
14✔
1273
    }
1274

1275
    SStreamScanInfo* pInfo = pOperator->info;
286,484✔
1276
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
286,566✔
1277
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
286,369✔
1278
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
286,383✔
1279

1280
    if (pOffset->type == TMQ_OFFSET__LOG) {
286,496✔
1281
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
239,988✔
1282
      pScanBaseInfo->dataReader = NULL;
239,964✔
1283

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

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

1309
      if (uid == 0) {
46,602✔
1310
        if (numOfTables != 0) {
45,253✔
1311
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
8,370✔
1312
          if (!tmp) {
8,370✔
1313
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1314
            taosRUnLockLatch(&pTaskInfo->lock);
×
1315
            return terrno;
×
1316
          }
1317
          if (tmp) uid = tmp->uid;
8,370✔
1318
          ts = INT64_MIN;
8,370✔
1319
          pScanInfo->currentTable = 0;
8,370✔
1320
        } else {
1321
          taosRUnLockLatch(&pTaskInfo->lock);
36,883✔
1322
          qError("no table in table list, %s", id);
36,862✔
1323
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
37,000✔
1324
        }
1325
      }
1326
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,719✔
1327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1418
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
151,209✔
1419
      tableListClear(pTableListInfo);
151,153✔
1420

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

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

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

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

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

1468
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
151,077✔
1469
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
151,077✔
1470
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1471
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
151,021✔
1472
      pTaskInfo->streamInfo.schema = mtInfo.schema;
151,021✔
1473
      taosMemoryFreeClear(mtInfo.pExtSchemas);
150,951✔
1474

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

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

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

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

1510
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
102,109,596✔
1511

1512
  if (pMsg->contLen > 0) {
102,126,179✔
1513
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
102,096,110✔
1514
    if (buf.pData == NULL) {
102,088,997✔
1515
      pMsg->code = terrno;
×
1516
    } else {
1517
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
102,088,997✔
1518
    }
1519
  }
1520

1521
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
102,127,294✔
1522
  rpcFreeCont(pMsg->pCont);
102,132,270✔
1523
  destroySendMsgInfo(pSendInfo);
102,103,452✔
1524
}
1525

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

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

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

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

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

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

1554
  taosArrayDestroy(plist);
×
1555

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

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

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

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

1578
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,718,918✔
1579
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,718,322✔
1580
  } else {
1581
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,718,926✔
1582
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,717,718✔
1583
    }
1584
  }
1585

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

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

1598
  *pList = NULL;
1,718,926✔
1599
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,719,530✔
1600
  if (pArray == NULL) {
1,718,918✔
1601
    return terrno;
×
1602
  }
1603

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

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

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

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

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

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

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

1648
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
45,945,402✔
1649
  int32_t code = 0;
45,945,402✔
1650

1651
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
45,945,402✔
1652
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
45,943,750✔
1653

1654
  if (pOper->fpSet.resetStateFn) {
45,943,750✔
1655
    code = pOper->fpSet.resetStateFn(pOper);
45,944,158✔
1656
  }
1657
  pOper->status = OP_NOT_OPENED;
45,942,660✔
1658
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
78,951,332✔
1659
    code = clearStatesForOperator(pOper->pDownstream[i]);
33,001,078✔
1660
  }
1661
  return code;
45,950,658✔
1662
}
1663

1664
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
12,945,665✔
1665
  int32_t        code = 0;
12,945,665✔
1666
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
12,945,665✔
1667
  SOperatorInfo* pOper = pTaskInfo->pRoot;
12,945,665✔
1668
  pTaskInfo->code = TSDB_CODE_SUCCESS;
12,945,665✔
1669
  code = clearStatesForOperator(pOper);
12,945,665✔
1670
  return code;
12,945,665✔
1671
}
1672

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

1678
  *ppRes = NULL;
16,902,470✔
1679

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

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

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

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

1699
  pTaskInfo->owner = threadId;
16,902,470✔
1700
  taosRUnLockLatch(&pTaskInfo->lock);
16,902,470✔
1701

1702
  if (pTaskInfo->cost.start == 0) {
16,902,066✔
1703
    pTaskInfo->cost.start = taosGetTimestampUs();
330,635✔
1704
  }
1705

1706
  // error occurs, record the error code and return to client
1707
  int32_t ret = setjmp(pTaskInfo->env);
16,902,066✔
1708
  if (ret != TSDB_CODE_SUCCESS) {
20,681,113✔
1709
    pTaskInfo->code = ret;
3,779,047✔
1710
    (void)cleanUpUdfs();
3,779,047✔
1711
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,779,047✔
1712
    atomic_store_64(&pTaskInfo->owner, 0);
3,779,047✔
1713
    return pTaskInfo->code;
3,779,047✔
1714
  }
1715

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

1718
  int64_t st = taosGetTimestampUs();
16,902,470✔
1719

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

1733
  uint64_t el = (taosGetTimestampUs() - st);
13,123,423✔
1734

1735
  pTaskInfo->cost.elapsedTime += el;
13,123,423✔
1736
  if (NULL == *ppRes) {
13,123,423✔
1737
    *useconds = pTaskInfo->cost.elapsedTime;
8,721,306✔
1738
  }
1739

1740
  (void)cleanUpUdfs();
13,123,423✔
1741

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

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

1748
  atomic_store_64(&pTaskInfo->owner, 0);
13,123,423✔
1749
  return pTaskInfo->code;
13,123,423✔
1750
}
1751

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

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

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

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

1784
end:
316,729✔
1785
  return 0;
316,729✔
1786
}
1787

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2146
  (*ppDst)->suid = pSrc->suid;
322,219✔
2147
  (*ppDst)->sver = pSrc->sver;
322,219✔
2148
  (*ppDst)->tbType = pSrc->tbType;
322,219✔
2149
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
322,219✔
2150
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
322,219✔
2151

2152
  if (pSrc->stbname) {
322,219✔
2153
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
322,219✔
2154
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
321,811✔
2155
  }
2156

2157
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
322,219✔
2158
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
322,219✔
2159

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

2162
  if (pSrc->pFields && pSrc->pFields->size > 0) {
322,219✔
2163
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
322,219✔
2164
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
321,811✔
2165
  } else {
2166
    (*ppDst)->pFields = NULL;
×
2167
  }
2168
  
2169
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
322,219✔
2170
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
220,126✔
2171
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
220,126✔
2172
  } else {
2173
    (*ppDst)->pTagFields = NULL;
101,685✔
2174
  }
2175

2176
_exit:
322,219✔
2177

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

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

2193
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2194
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc