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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

68.84
/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) {
706,933✔
44
  gExecInfo.dnode = pDnode;
706,933✔
45
  gExecInfo.getMnode = getMnode;
706,933✔
46
  gExecInfo.getDnodeId = getDnodeId;
706,933✔
47
  return;
706,933✔
48
}
49

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

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

64
static void initRefPool() {
657,591✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
657,591✔
66
  (void)atexit(cleanupRefPool);
657,591✔
67
}
657,591✔
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) {
16,742,780✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
16,742,780✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
16,743,460✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,753,116✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,753,116✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,753,200✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,753,200✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,623✔
158
        if (code) {
57,623✔
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,989,336✔
166
  }
167

168
  return 0;
11,228,046✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
11,228,052✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
11,228,052✔
173
  pTaskInfo->id.queryId = queryId;
11,228,052✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
11,228,110✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
11,227,700✔
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) {
139,498✔
207
  if (msg == NULL) {  // create raw scan
139,498✔
208
    SExecTaskInfo* pTaskInfo = NULL;
23,893✔
209

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

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

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

226
  SSubplan* pPlan = NULL;
115,605✔
227
  int32_t   code = qStringToSubplan(msg, &pPlan);
116,594✔
228
  if (code != TSDB_CODE_SUCCESS) {
115,764✔
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;
115,764✔
235
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE);
115,764✔
236
  if (code != TSDB_CODE_SUCCESS) {
116,673✔
237
    qDestroyTask(pTaskInfo);
×
238
    terrno = code;
×
239
    return NULL;
×
240
  }
241

242
  return pTaskInfo;
116,673✔
243
}
244

245
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
737,054✔
246
  if (streamInserterParam == NULL) {
737,054✔
247
    return TSDB_CODE_SUCCESS;
403,280✔
248
  }
249

250
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
333,774✔
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) {
333,774✔
256
    stError("insertParam: invalid db/table name");
×
257
    return TSDB_CODE_INVALID_PARA;
×
258
  }
259

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

266
  return TSDB_CODE_SUCCESS;
333,774✔
267
}
268

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

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

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

304
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
333,774✔
305
    if (NULL == pInserterParam) {
333,774✔
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);
333,774✔
311
    TSDB_CHECK_CODE(code, lino, _error);
333,774✔
312
    
313
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
333,774✔
314
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
333,774✔
315

316
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
333,774✔
317
    if (code) {
333,774✔
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,
736,205✔
322
         tstrerror(code));
323

324
_error:
145,394✔
325

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

335
bool qNeedReset(qTaskInfo_t pInfo) {
5,747,057✔
336
  if (pInfo == NULL) {
5,747,057✔
337
    return false;
×
338
  }
339
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,747,057✔
340
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,747,057✔
341
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,747,057✔
342
    return false;
8,436✔
343
  }
344
  int32_t node = nodeType(pOperator->pPhyNode);
5,738,621✔
345
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,443,143✔
346
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,181,764✔
347
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
348
}
349

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

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

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

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

373
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,738,621✔
374
    pScanBaseInfo = &((STableScanInfo*)info)->base;
295,478✔
375
    setReadHandle(handle, pScanBaseInfo);
295,478✔
376
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,443,143✔
377
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,723,545✔
378
    setReadHandle(handle, pScanBaseInfo);
4,723,545✔
379
  }
380

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

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

392
  *pTaskInfo = NULL;
736,651✔
393

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

409
  return code;
736,645✔
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,
61,036✔
419
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
420
  int32_t code = TSDB_CODE_SUCCESS;
61,036✔
421
  int32_t lino = 0;
61,036✔
422
  int8_t  locked = 0;
61,036✔
423
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
61,036✔
424

425
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
61,036✔
426

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

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

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
61,036✔
437

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

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

447
  locked = 1;
61,036✔
448
  for (int32_t i = 0; i < numOfUids; ++i) {
122,450✔
449
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
61,414✔
450
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
61,414✔
451

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

458
    tDecoderClear(&mr.coder);
61,414✔
459

460
    if (mr.me.type == TSDB_SUPER_TABLE) {
61,414✔
461
      continue;
×
462
    } else {
463
      if (type == TSDB_SUPER_TABLE) {
61,414✔
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) {
61,414✔
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};
61,414✔
478
    if (pScanInfo->pTagCond != NULL) {
61,414✔
479
      // tb_uid_t id = mr.me.uid;
480
      item.check = 1;
53,146✔
481
    }
482
    if (taosArrayPush(tUid, &item) == NULL) {
61,414✔
483
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
484
    }
485
  }
486

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

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

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

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

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

515
  // handle multiple partition
516

517
_end:
61,036✔
518

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

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

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

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

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

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

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

568
    bool   assignUid = false;
61,036✔
569
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
61,036✔
570
    char*  keyBuf = NULL;
61,036✔
571
    if (bufLen > 0) {
61,036✔
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;
61,036✔
581
    taosWLockLatch(&pTaskInfo->lock);
61,036✔
582

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

593
      if (bufLen > 0) {
34,816✔
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);
34,816✔
609
      if (code != TSDB_CODE_SUCCESS) {
34,816✔
610
        taosMemoryFree(keyBuf);
×
611
        taosArrayDestroy(qa);
×
612
        taosWUnLockLatch(&pTaskInfo->lock);
×
613
        return code;
×
614
      }
615
    }
616

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

622
    taosArrayDestroy(qa);
61,036✔
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);
209✔
625
    taosWLockLatch(&pTaskInfo->lock);
209✔
626
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
209✔
627
    taosWUnLockLatch(&pTaskInfo->lock);
209✔
628
  }
629

630
  return code;
61,245✔
631
}
632

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

638
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
267,494,471✔
639
    return TSDB_CODE_INVALID_PARA;
2,520✔
640
  }
641
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
267,492,534✔
642

643
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
267,492,534✔
644
    return TSDB_CODE_SUCCESS;
154,057,657✔
645
  }
646

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

653
  *sversion = pSchemaInfo->sw->version;
113,447,936✔
654
  *tversion = pSchemaInfo->tversion;
113,443,643✔
655
  *rversion = pSchemaInfo->rversion;
113,451,541✔
656
  if (pSchemaInfo->dbname) {
113,437,371✔
657
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
113,436,022✔
658
  } else {
659
    dbName[0] = 0;
×
660
  }
661
  if (pSchemaInfo->tablename) {
113,448,728✔
662
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
113,447,692✔
663
  } else {
664
    tableName[0] = 0;
×
665
  }
666

667
  *tbGet = true;
113,451,759✔
668

669
  return TSDB_CODE_SUCCESS;
113,448,378✔
670
}
671

672
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
154,051,500✔
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) {
6,408,449✔
682
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
6,408,449✔
683
  ((SExecTaskInfo*)tinfo)->paramSet = false;
6,408,449✔
684
}
6,408,449✔
685

686
int32_t qExecutorInit(void) {
4,716,406✔
687
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,716,406✔
688
  return TSDB_CODE_SUCCESS;
4,717,128✔
689
}
690

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

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

699
  readHandle->uid = 0;
155,919,994✔
700
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
155,937,133✔
701
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
155,839,958✔
702
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
172,203✔
703
    goto _error;
26,100✔
704
  }
705

706
  if (handle) {
155,685,620✔
707
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
155,679,980✔
708
    void*           pSinkManager = NULL;
155,708,792✔
709
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
155,555,726✔
710
    if (code != TSDB_CODE_SUCCESS) {
155,559,100✔
711
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
712
      goto _error;
×
713
    }
714

715
    void* pSinkParam = NULL;
155,559,100✔
716
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
155,649,446✔
717
    if (code != TSDB_CODE_SUCCESS) {
155,552,481✔
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;
155,552,481✔
724
    if (readHandle->localExec) {
155,683,250✔
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,
155,542,043✔
736
                              (*pTask)->id.str, pSubplan->processOneBlock);
155,731,670✔
737
    if (code) {
155,583,487✔
738
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
645✔
739
    }
740
  }
741

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

745
_error:
11,549,154✔
746
  // if failed to add ref for all tables in this query, abort current query
747
  return code;
155,927,778✔
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,
178,633,633✔
756
                     bool processOneBlock) {
757
  int32_t        code = TSDB_CODE_SUCCESS;
178,633,633✔
758
  int32_t        lino = 0;
178,633,633✔
759
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
178,633,633✔
760
  int64_t        threadId = taosGetSelfPthreadId();
178,633,633✔
761

762
  if (pLocal) {
178,626,197✔
763
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
172,451,373✔
764
  }
765

766
  taosArrayClear(pResList);
178,620,361✔
767

768
  int64_t curOwner = 0;
178,623,076✔
769
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
178,623,076✔
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) {
178,611,715✔
776
    pTaskInfo->cost.start = taosGetTimestampUs();
154,014,966✔
777
  }
778

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

785
  // error occurs, record the error code and return to client
786
  int32_t ret = setjmp(pTaskInfo->env);
178,613,198✔
787
  if (ret != TSDB_CODE_SUCCESS) {
178,757,687✔
788
    pTaskInfo->code = ret;
163,287✔
789
    (void)cleanUpUdfs();
163,287✔
790

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

794
    return pTaskInfo->code;
163,287✔
795
  }
796

797
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
178,594,400✔
798

799
  int32_t      current = 0;
178,596,096✔
800
  SSDataBlock* pRes = NULL;
178,596,096✔
801
  int64_t      st = taosGetTimestampUs();
178,630,144✔
802

803
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
178,630,144✔
804
    pTaskInfo->paramSet = true;
6,408,449✔
805
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
6,408,449✔
806
  } else {
807
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
172,226,744✔
808
  }
809

810
  QUERY_CHECK_CODE(code, lino, _end);
178,472,246✔
811
  code = blockDataCheck(pRes);
178,472,246✔
812
  QUERY_CHECK_CODE(code, lino, _end);
178,470,755✔
813

814
  if (pRes == NULL) {
178,470,755✔
815
    st = taosGetTimestampUs();
30,975,461✔
816
  }
817

818
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
178,466,802✔
819
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
178,476,836✔
820
    rowsThreshold = 4096;
177,758,493✔
821
  }
822

823
  int32_t blockIndex = 0;
178,473,353✔
824
  while (pRes != NULL) {
573,014,812✔
825
    SSDataBlock* p = NULL;
412,914,739✔
826
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
412,922,485✔
827
      SSDataBlock* p1 = NULL;
305,927,664✔
828
      code = createOneDataBlock(pRes, true, &p1);
305,938,763✔
829
      QUERY_CHECK_CODE(code, lino, _end);
305,925,382✔
830

831
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
305,925,382✔
832
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
305,945,064✔
833
      p = p1;
305,945,064✔
834
    } else {
835
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
107,045,627✔
836
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
107,043,599✔
837

838
      p = *(SSDataBlock**)tmp;
107,043,599✔
839
      code = copyDataBlock(p, pRes);
107,044,703✔
840
      QUERY_CHECK_CODE(code, lino, _end);
107,044,759✔
841
    }
842

843
    blockIndex += 1;
412,977,109✔
844

845
    current += p->info.rows;
412,977,109✔
846
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
412,969,316✔
847
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
848
    void* tmp = taosArrayPush(pResList, &p);
412,982,839✔
849
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
412,982,839✔
850

851
    if (current >= rowsThreshold || processOneBlock) {
412,982,839✔
852
      break;
853
    }
854

855
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
394,617,452✔
856
    QUERY_CHECK_CODE(code, lino, _end);
394,445,788✔
857
    code = blockDataCheck(pRes);
394,445,788✔
858
    QUERY_CHECK_CODE(code, lino, _end);
394,627,231✔
859
  }
860

861
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
178,465,460✔
862
    pTaskInfo->pSubplan->rowsThreshold -= current;
716,636✔
863
  }
864

865
  *hasMore = (pRes != NULL);
178,464,513✔
866
  uint64_t el = (taosGetTimestampUs() - st);
178,436,718✔
867

868
  pTaskInfo->cost.elapsedTime += el;
178,436,718✔
869
  if (NULL == pRes) {
178,459,912✔
870
    *useconds = pTaskInfo->cost.elapsedTime;
160,094,525✔
871
  }
872

873
_end:
178,523,171✔
874
  (void)cleanUpUdfs();
178,447,367✔
875

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

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

886
  return pTaskInfo->code;
178,483,851✔
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) {
51,592,084✔
904
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
51,592,084✔
905
  int64_t        threadId = taosGetSelfPthreadId();
51,592,084✔
906
  int64_t        curOwner = 0;
51,592,417✔
907

908
  *pRes = NULL;
51,592,417✔
909

910
  // todo extract method
911
  taosRLockLatch(&pTaskInfo->lock);
51,592,417✔
912
  bool isKilled = isTaskKilled(pTaskInfo);
51,592,696✔
913
  if (isKilled) {
51,592,474✔
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) {
51,592,474✔
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;
51,591,958✔
929
  taosRUnLockLatch(&pTaskInfo->lock);
51,592,585✔
930

931
  if (pTaskInfo->cost.start == 0) {
51,592,696✔
932
    pTaskInfo->cost.start = taosGetTimestampUs();
94,392✔
933
  }
934

935
  // error occurs, record the error code and return to client
936
  int32_t ret = setjmp(pTaskInfo->env);
51,592,696✔
937
  if (ret != TSDB_CODE_SUCCESS) {
51,591,067✔
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));
51,591,067✔
946

947
  int64_t st = taosGetTimestampUs();
51,591,808✔
948
  int32_t code = TSDB_CODE_SUCCESS;
51,591,808✔
949
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
51,591,808✔
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);
51,591,697✔
954
  }
955
  if (code) {
51,564,836✔
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);
51,564,836✔
961
  if (code) {
51,591,736✔
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);
51,578,519✔
967

968
  pTaskInfo->cost.elapsedTime += el;
51,578,519✔
969
  if (NULL == *pRes) {
51,582,452✔
970
    *useconds = pTaskInfo->cost.elapsedTime;
4,393,520✔
971
  }
972

973
  (void)cleanUpUdfs();
51,583,851✔
974

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

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

981
  atomic_store_64(&pTaskInfo->owner, 0);
51,592,547✔
982
  return pTaskInfo->code;
51,592,585✔
983
}
984

985
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
50,874,559✔
986
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
50,874,559✔
987
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
50,874,813✔
988
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
50,874,813✔
989

990
  if (!tmp) {
50,874,559✔
991
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
992
    return terrno;
×
993
  }
994
  return TSDB_CODE_SUCCESS;
50,874,559✔
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) {
33,346✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
33,346✔
1021

1022
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
33,346✔
1023
  for (int32_t i = 0; i < num; ++i) {
54,247✔
1024
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
20,901✔
1025
    if (!pStop) {
20,901✔
1026
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1027
      continue;
×
1028
    }
1029
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
20,901✔
1030
    if (pExchangeInfo) {
20,901✔
1031
      int32_t code = tsem_post(&pExchangeInfo->ready);
20,901✔
1032
      if (code != TSDB_CODE_SUCCESS) {
20,901✔
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,901✔
1036
      }
1037
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
20,901✔
1038
      if (code != TSDB_CODE_SUCCESS) {
20,901✔
1039
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1040
      }
1041
    }
1042
  }
1043

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

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

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

1055
  setTaskKilled(pTaskInfo, rspCode);
33,346✔
1056
  qStopTaskOperators(pTaskInfo);
33,346✔
1057

1058
  return TSDB_CODE_SUCCESS;
33,346✔
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) {
156,672,207✔
1115
  STaskCostInfo* pSummary = &pTaskInfo->cost;
156,672,207✔
1116
  int64_t        idleTime = pSummary->start - pSummary->created;
156,671,534✔
1117

1118
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
156,672,938✔
1119
  if (pSummary->pRecoder != NULL) {
156,668,188✔
1120
    qDebug(
112,627,242✔
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,
44,035,902✔
1129
           pSummary->elapsedTime / 1000.0);
1130
  }
1131
}
156,664,226✔
1132

1133
void qDestroyTask(qTaskInfo_t qTaskHandle) {
164,134,681✔
1134
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
164,134,681✔
1135
  if (pTaskInfo == NULL) {
164,134,681✔
1136
    return;
7,467,315✔
1137
  }
1138

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

1145
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
156,671,993✔
1146
  doDestroyTask(pTaskInfo);
156,670,984✔
1147
}
1148

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

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

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

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

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

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

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

1189
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,875,389✔
1190
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,875,389✔
1191
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,875,389✔
1192
  return 0;
4,875,332✔
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) {
152,482✔
1201
  memset(pCond, 0, sizeof(SQueryTableDataCond));
152,482✔
1202
  pCond->order = TSDB_ORDER_ASC;
152,482✔
1203
  pCond->numOfCols = pMtInfo->schema->nCols;
152,601✔
1204
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
152,912✔
1205
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
152,500✔
1206
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
152,881✔
1207
    taosMemoryFreeClear(pCond->colList);
780✔
1208
    taosMemoryFreeClear(pCond->pSlotList);
×
1209
    return terrno;
×
1210
  }
1211

1212
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
152,355✔
1213
  pCond->suid = pMtInfo->suid;
152,539✔
1214
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
152,500✔
1215
  pCond->startVersion = -1;
152,228✔
1216
  pCond->endVersion = sContext->snapVersion;
152,368✔
1217

1218
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
980,984✔
1219
    SColumnInfo* pColInfo = &pCond->colList[i];
828,365✔
1220
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
828,536✔
1221
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
828,541✔
1222
    if (pMtInfo->pExtSchemas != NULL) {
828,264✔
1223
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,652✔
1224
    }
1225
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
828,378✔
1226
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
828,295✔
1227

1228
    pCond->pSlotList[i] = i;
828,580✔
1229
  }
1230

1231
  return TSDB_CODE_SUCCESS;
153,052✔
1232
}
1233

1234
void qStreamSetOpen(qTaskInfo_t tinfo) {
51,284,430✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
51,284,430✔
1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
51,284,430✔
1237
  pOperator->status = OP_NOT_OPENED;
51,291,051✔
1238
}
51,290,279✔
1239

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

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

1250
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,918,257✔
1251
  const char*    id = GET_TASKID(pTaskInfo);
4,917,875✔
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,917,975✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,646,735✔
1255
    if (pOperator == NULL || code != 0) {
4,646,217✔
1256
      return code;
×
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
4,646,450✔
1260
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,646,554✔
1261
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,646,144✔
1262
    walReaderVerifyOffset(pWalReader, pOffset);
4,646,401✔
1263
  }
1264
  // if pOffset equal to current offset, means continue consume
1265
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,917,407✔
1266
    return 0;
4,471,618✔
1267
  }
1268

1269
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
446,112✔
1270
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
289,147✔
1271
    if (pOperator == NULL || code != 0) {
289,606✔
1272
      return code;
×
1273
    }
1274

1275
    SStreamScanInfo* pInfo = pOperator->info;
289,606✔
1276
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
289,135✔
1277
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
289,434✔
1278
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
289,434✔
1279

1280
    if (pOffset->type == TMQ_OFFSET__LOG) {
289,582✔
1281
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
240,935✔
1282
      pScanBaseInfo->dataReader = NULL;
240,679✔
1283

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

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

1309
      if (uid == 0) {
48,335✔
1310
        if (numOfTables != 0) {
47,372✔
1311
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
8,382✔
1312
          if (!tmp) {
8,308✔
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,308✔
1318
          ts = INT64_MIN;
8,308✔
1319
          pScanInfo->currentTable = 0;
8,308✔
1320
        } else {
1321
          taosRUnLockLatch(&pTaskInfo->lock);
38,990✔
1322
          qError("no table in table list, %s", id);
38,990✔
1323
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
39,074✔
1324
        }
1325
      }
1326
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,345✔
1327

1328
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
9,609✔
1329
             pInfo->pTableScanOp->resultInfo.totalRows);
1330
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
9,681✔
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,681✔
1336
      taosRUnLockLatch(&pTaskInfo->lock);
9,681✔
1337

1338
      if (index >= 0) {
9,681✔
1339
        pScanInfo->currentTable = index;
9,681✔
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,681✔
1347
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
9,681✔
1348

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

1357
      if (pScanBaseInfo->dataReader == NULL) {
9,681✔
1358
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
16,592✔
1359
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1360
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
8,296✔
1361
        if (code != TSDB_CODE_SUCCESS) {
8,296✔
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,296✔
1367
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1368
      } else {
1369
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,385✔
1370
        if (code != TSDB_CODE_SUCCESS) {
1,385✔
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,385✔
1376
        if (code != TSDB_CODE_SUCCESS) {
1,385✔
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,385✔
1381
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1382
      }
1383

1384
      // restore the key value
1385
      pScanBaseInfo->cond.twindows.skey = oldSkey;
9,681✔
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) {
156,965✔
1393
      SStreamRawScanInfo* pInfo = pOperator->info;
153,236✔
1394
      SSnapContext*       sContext = pInfo->sContext;
153,236✔
1395
      SOperatorInfo*      p = NULL;
153,236✔
1396

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

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

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

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

1418
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
152,482✔
1419
      tableListClear(pTableListInfo);
152,259✔
1420

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

1426
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
152,925✔
1427
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
152,697✔
1428
      if (code != TSDB_CODE_SUCCESS) {
152,399✔
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)) {
152,399✔
1434
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
382✔
1435
      } else {
1436
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
151,877✔
1437
      }
1438

1439
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
152,728✔
1440
      if (code != TSDB_CODE_SUCCESS) {
153,052✔
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);
153,052✔
1447
      if (!pList) {
153,052✔
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;
153,052✔
1453
      code = tableListGetSize(pTableListInfo, &size);
153,052✔
1454
      if (code != TSDB_CODE_SUCCESS) {
153,052✔
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,
306,104✔
1461
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
153,052✔
1462
      if (code != TSDB_CODE_SUCCESS) {
152,311✔
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);
152,311✔
1469
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
152,868✔
1470
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1471
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
152,171✔
1472
      pTaskInfo->streamInfo.schema = mtInfo.schema;
152,526✔
1473
      taosMemoryFreeClear(mtInfo.pExtSchemas);
152,627✔
1474

1475
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
152,627✔
1476
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,729✔
1477
      SStreamRawScanInfo* pInfo = pOperator->info;
1,234✔
1478
      SSnapContext*       sContext = pInfo->sContext;
1,234✔
1479
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,234✔
1480
      if (code != 0) {
1,234✔
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,234✔
1485
             id);
1486
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
2,495✔
1487
      SStreamRawScanInfo* pInfo = pOperator->info;
2,495✔
1488
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,495✔
1489
      pInfo->dataReader = NULL;
2,495✔
1490
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
2,495✔
1491
    }
1492
  }
1493

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

1499
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
109,359,726✔
1500
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
109,359,726✔
1501
  if (pMsg->info.ahandle == NULL) {
109,368,577✔
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, 
109,349,862✔
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};
109,358,606✔
1511

1512
  if (pMsg->contLen > 0) {
109,360,496✔
1513
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
109,333,709✔
1514
    if (buf.pData == NULL) {
109,324,318✔
1515
      pMsg->code = terrno;
×
1516
    } else {
1517
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
109,324,318✔
1518
    }
1519
  }
1520

1521
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
109,370,268✔
1522
  rpcFreeCont(pMsg->pCont);
109,376,958✔
1523
  destroySendMsgInfo(pSendInfo);
109,351,002✔
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,447,684✔
1565
  int32_t        code = TSDB_CODE_SUCCESS;
3,447,684✔
1566
  int32_t        lino = 0;
3,447,684✔
1567
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,447,684✔
1568

1569
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,448,900✔
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,445,149✔
1576
    STableScanInfo* pScanInfo = pOperator->info;
1,722,827✔
1577

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

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

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

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

1604
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,724,146✔
1605
  if (code == 0) {
1,723,431✔
1606
    *pList = pArray;
1,723,431✔
1607
  } else {
1608
    taosArrayDestroy(pArray);
×
1609
  }
1610
  return code;
1,722,823✔
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) {
48,283,582✔
1649
  int32_t code = 0;
48,283,582✔
1650

1651
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
48,283,582✔
1652
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
48,280,705✔
1653

1654
  if (pOper->fpSet.resetStateFn) {
48,280,743✔
1655
    code = pOper->fpSet.resetStateFn(pOper);
48,283,611✔
1656
  }
1657
  pOper->status = OP_NOT_OPENED;
48,275,385✔
1658
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
83,125,397✔
1659
    code = clearStatesForOperator(pOper->pDownstream[i]);
34,840,154✔
1660
  }
1661
  return code;
48,286,045✔
1662
}
1663

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

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

1678
  *ppRes = NULL;
17,826,016✔
1679

1680
  // todo extract method
1681
  taosRLockLatch(&pTaskInfo->lock);
17,826,016✔
1682
  bool isKilled = isTaskKilled(pTaskInfo);
17,826,016✔
1683
  if (isKilled) {
17,826,016✔
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) {
17,826,016✔
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;
17,825,576✔
1700
  taosRUnLockLatch(&pTaskInfo->lock);
17,825,576✔
1701

1702
  if (pTaskInfo->cost.start == 0) {
17,826,016✔
1703
    pTaskInfo->cost.start = taosGetTimestampUs();
341,842✔
1704
  }
1705

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

1716
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,824,346✔
1717

1718
  int64_t st = taosGetTimestampUs();
17,826,016✔
1719

1720
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,826,016✔
1721
  if (code) {
13,914,658✔
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,914,658✔
1726
    code = blockDataCheck(*ppRes);
13,914,658✔
1727
  }
1728
  if (code) {
13,914,249✔
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,914,658✔
1734

1735
  pTaskInfo->cost.elapsedTime += el;
13,914,658✔
1736
  if (NULL == *ppRes) {
13,914,658✔
1737
    *useconds = pTaskInfo->cost.elapsedTime;
9,029,009✔
1738
  }
1739

1740
  (void)cleanUpUdfs();
13,914,658✔
1741

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1925
  if (code == 0) {
906,206✔
1926
    const char* pVal = NULL;
906,206✔
1927
    int32_t     len = 0;
906,206✔
1928
    SNode*      pSclNode = NULL;
906,206✔
1929
    switch (pExprInfo->pExpr->nodeType) {
906,206✔
1930
      case QUERY_NODE_FUNCTION:
906,206✔
1931
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
906,206✔
1932
        break;
906,206✔
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);
906,206✔
1941
    SSDataBlock block = {0};
906,206✔
1942
    block.info.rows = 1;
906,206✔
1943
    SSDataBlock* pBlock = &block;
906,206✔
1944
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
906,206✔
1945
    if (tmp == NULL) {
906,206✔
1946
      code = terrno;
×
1947
    }
1948
    if (code == 0) {
906,206✔
1949
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
906,206✔
1950
    }
1951
    taosArrayDestroy(pBlockList);
906,206✔
1952
  }
1953
  nodesDestroyList(pList);
906,206✔
1954
  destroyExprInfo(pExprInfo, numOfExprs);
906,206✔
1955
  taosMemoryFreeClear(pExprInfo);
905,797✔
1956
  return code;
905,388✔
1957
}
1958

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

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

1981
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
22,779✔
1982
  if (code != TSDB_CODE_SUCCESS) {
22,779✔
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;
22,779✔
1989
  int32_t rowIdx = (*pRes)->info.rows;
22,779✔
1990
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
22,779✔
1991
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
22,779✔
1992
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
90,725✔
1993
    SScalarParam   dst = {0};
67,946✔
1994
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
67,946✔
1995
    code = nodesStringToNode(pCol->expr, &pNode);
67,946✔
1996
    if (code != 0) break;
67,946✔
1997
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
67,946✔
1998
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
67,946✔
1999
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
45,167✔
2000
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
45,167✔
2001
    } else {
2002
      dst.columnData = pInfo;
22,779✔
2003
      dst.numOfRows = rowIdx;
22,779✔
2004
      dst.colAlloced = false;
22,779✔
2005
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
22,779✔
2006
    }
2007
    ++idx;
67,946✔
2008
    // TODO sclFreeParam(&dst);
2009
    nodesDestroyNode(pNode);
67,946✔
2010
    if (code != 0) break;
67,946✔
2011
  }
2012
  if (code == TSDB_CODE_SUCCESS) {
22,779✔
2013
    (*pRes)->info.rows++;
22,779✔
2014
  }
2015
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
22,779✔
2016
  return code;
22,779✔
2017
}
2018

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

2059
      pCol->hasNull = true;
348,211✔
2060
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
348,211✔
2061
      pCol->info.colId = 0;
348,211✔
2062
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
348,211✔
2063
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
348,211✔
2064
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
348,211✔
2065
      code = colInfoDataEnsureCapacity(pCol, 1, true);
348,211✔
2066
      if (code != 0) {
348,211✔
2067
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2068
        taosMemoryFree(pCol);
×
2069
        break;
×
2070
      }
2071
      dst.columnData = pCol;
348,211✔
2072
      dst.numOfRows = 1;
348,211✔
2073
      dst.colAlloced = true;
348,211✔
2074
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
348,211✔
2075
      if (colDataIsNull_var(dst.columnData, 0)) {
348,211✔
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) {
348,211✔
2080
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
342,856✔
2081
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
342,038✔
2082
      }
2083
    } break;
347,802✔
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) {
347,802✔
2090
    if (!pVal || len == 0) {
342,447✔
2091
      qError("tbname generated with no characters which is not allowed");
×
2092
      code = TSDB_CODE_INVALID_PARA;
×
2093
    }
2094
    if(len > TSDB_TABLE_NAME_LEN - 1) {
342,447✔
2095
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
818✔
2096
      len = TSDB_TABLE_NAME_LEN - 1;
818✔
2097
    }
2098

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

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

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

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

2152
  if (pSrc->stbname) {
333,369✔
2153
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
333,774✔
2154
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
333,774✔
2155
  }
2156

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

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

2162
  if (pSrc->pFields && pSrc->pFields->size > 0) {
333,774✔
2163
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
333,364✔
2164
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
333,774✔
2165
  } else {
2166
    (*ppDst)->pFields = NULL;
410✔
2167
  }
2168
  
2169
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
333,774✔
2170
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
226,296✔
2171
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
226,296✔
2172
  } else {
2173
    (*ppDst)->pTagFields = NULL;
107,478✔
2174
  }
2175

2176
_exit:
333,774✔
2177

2178
  if (code != 0) {
333,774✔
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;
333,774✔
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