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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

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

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

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

64
static void initRefPool() {
636,173✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
636,173✔
66
  (void)atexit(cleanupRefPool);
636,173✔
67
}
636,173✔
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) {
13,764,109✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
13,764,109✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
13,769,409✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
3,421,044✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
3,421,044✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
3,421,151✔
156
      if (pScanInfo->base.dataReader != NULL) {
3,421,001✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
54,087✔
158
        if (code) {
54,016✔
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);
10,347,106✔
166
  }
167

168
  return 0;
9,641,987✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
9,643,119✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
9,643,119✔
173
  pTaskInfo->id.queryId = queryId;
9,643,119✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
9,643,234✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
9,642,792✔
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, int32_t* numOfCols,
132,307✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
132,307✔
209
    SExecTaskInfo* pTaskInfo = NULL;
21,292✔
210

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

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

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

227
  SSubplan* pPlan = NULL;
111,015✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
111,015✔
229
  if (code != TSDB_CODE_SUCCESS) {
111,231✔
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

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

242
  // extract the number of output columns
243
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
111,231✔
244
  *numOfCols = 0;
111,231✔
245

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,263,338✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,152,107✔
249
    if (pSlotDesc->output) {
1,152,107✔
250
      ++(*numOfCols);
1,152,025✔
251
    }
252
  }
253

254
  return pTaskInfo;
111,231✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
673,269✔
258
  if (streamInserterParam == NULL) {
673,269✔
259
    return TSDB_CODE_SUCCESS;
362,596✔
260
  }
261

262
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
310,673✔
263
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
264
    return TSDB_CODE_INVALID_PARA;
×
265
  }
266

267
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
310,673✔
268
    stError("insertParam: invalid db/table name");
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

272
  if (streamInserterParam->suid <= 0 &&
310,673✔
273
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
103,955✔
274
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
275
    return TSDB_CODE_INVALID_PARA;
×
276
  }
277

278
  return TSDB_CODE_SUCCESS;
310,673✔
279
}
280

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

301
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
672,497✔
302
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
673,269✔
303
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
1,172✔
304
    goto _error;
400✔
305
  }
306

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

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

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

336
_error:
139,864✔
337

338
  if (code != TSDB_CODE_SUCCESS) {
673,269✔
339
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
400✔
340
    if (pInserterParam != NULL) {
400✔
341
      taosMemoryFree(pInserterParam);
×
342
    }
343
  }
344
  return code;
673,269✔
345
}
346

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,556,796✔
348
  if (pInfo == NULL) {
5,556,796✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,556,796✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,556,796✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,556,796✔
354
    return false;
5,200✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,551,596✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,290,146✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
10,841,742✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

362
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
5,110,260✔
363
  if (pHandle == NULL || pScanBaseInfo == NULL) {
5,110,260✔
364
    return;
×
365
  }
366

367
  pScanBaseInfo->readHandle.uid = pHandle->uid;
5,110,260✔
368
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
5,110,260✔
369
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
5,110,260✔
370
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
5,110,260✔
371
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
5,110,260✔
372
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
5,110,260✔
373
}
374

375
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
5,551,596✔
376
  if (pInfo == NULL) {
5,551,596✔
377
    return TSDB_CODE_INVALID_PARA;
×
378
  }
379
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,551,596✔
380
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,551,596✔
381

382
  void*           info = pOperator->info;
5,551,596✔
383
  STableScanBase* pScanBaseInfo = NULL;
5,551,596✔
384

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,551,596✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
261,450✔
387
    setReadHandle(handle, pScanBaseInfo);
261,450✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,290,146✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,848,810✔
390
    setReadHandle(handle, pScanBaseInfo);
4,848,810✔
391
  }
392

393
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
5,551,596✔
394
  handle->winRange.ekey);
395
  return pOperator->fpSet.resetStateFn(pOperator);
5,551,596✔
396
}
397

398
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
673,269✔
399
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
400
  if (msg == NULL) {
673,269✔
401
    return TSDB_CODE_INVALID_PARA;
×
402
  }
403

404
  *pTaskInfo = NULL;
673,269✔
405

406
  SSubplan* pPlan = NULL;
673,269✔
407
  int32_t   code = qStringToSubplan(msg, &pPlan);
673,269✔
408
  if (code != TSDB_CODE_SUCCESS) {
673,269✔
409
    nodesDestroyNode((SNode *)pPlan);
×
410
    return code;
×
411
  }
412
  // todo: add stream inserter param
413
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
673,269✔
414
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
415
                               pInserterParams);
416
  if (code != TSDB_CODE_SUCCESS) {
673,269✔
417
    qDestroyTask(*pTaskInfo);
400✔
418
    return code;
400✔
419
  }
420

421
  return code;
672,869✔
422
}
423

424
typedef struct {
425
  tb_uid_t tableUid;
426
  tb_uid_t childUid;
427
  int8_t   check;
428
} STqPair;
429

430
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
62,258✔
431
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
432
  int32_t code = TSDB_CODE_SUCCESS;
62,258✔
433
  int32_t lino = 0;
62,258✔
434
  int8_t  locked = 0;
62,258✔
435
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
62,258✔
436

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
62,258✔
438

439
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
62,258✔
440
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
62,258✔
441

442
  int32_t numOfUids = taosArrayGetSize(tableIdList);
62,258✔
443
  if (numOfUids == 0) {
62,258✔
444
    (*ppArrayRes) = qa;
×
445
    goto _error;
×
446
  }
447

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
62,258✔
449

450
  uint64_t suid = 0;
62,258✔
451
  uint64_t uid = 0;
62,258✔
452
  int32_t  type = 0;
62,258✔
453
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
62,258✔
454

455
  // let's discard the tables those are not created according to the queried super table.
456
  SMetaReader mr = {0};
62,258✔
457
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
62,258✔
458

459
  locked = 1;
62,258✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
124,921✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
62,663✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
62,663✔
463

464
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
62,663✔
465
    if (code != TSDB_CODE_SUCCESS) {
62,663✔
466
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
467
      continue;
×
468
    }
469

470
    tDecoderClear(&mr.coder);
62,663✔
471

472
    if (mr.me.type == TSDB_SUPER_TABLE) {
62,663✔
473
      continue;
×
474
    } else {
475
      if (type == TSDB_SUPER_TABLE) {
62,663✔
476
        // this new created child table does not belong to the scanned super table.
477
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
62,663✔
478
          continue;
×
479
        }
480
      } else {  // ordinary table
481
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
482
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
483
        if (mr.me.uid != uid) {
×
484
          continue;
×
485
        }
486
      }
487
    }
488

489
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
62,663✔
490
    if (pScanInfo->pTagCond != NULL) {
62,663✔
491
      // tb_uid_t id = mr.me.uid;
492
      item.check = 1;
54,630✔
493
    }
494
    if (taosArrayPush(tUid, &item) == NULL) {
62,663✔
495
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
496
    }
497
  }
498

499
  pAPI->metaReaderFn.clearReader(&mr);
62,258✔
500
  locked = 0;
62,258✔
501

502
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
124,921✔
503
    bool     qualified = false;
62,663✔
504
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
62,663✔
505
    if (t == NULL) {
62,663✔
506
      continue;
×
507
    }
508

509
    if (t->check == 1) {
62,663✔
510
      STableKeyInfo info = {.groupId = 0, .uid = t->childUid};
54,630✔
511
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
54,630✔
512
      if (code != TSDB_CODE_SUCCESS) {
54,630✔
513
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
514
        continue;
×
515
      }
516

517
      if (!qualified) {
54,630✔
518
        continue;
27,302✔
519
      }
520
    }
521

522
    void* tmp = taosArrayPush(qa, &t->tableUid);
35,361✔
523
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
35,361✔
524
  }
525

526
  // handle multiple partition
527

528
_end:
62,258✔
529

530
  if (locked) {
62,258✔
531
    pAPI->metaReaderFn.clearReader(&mr);
×
532
  }
533
  (*ppArrayRes) = qa;
62,258✔
534
_error:
62,258✔
535

536
  taosArrayDestroy(tUid);
62,258✔
537
  if (code != TSDB_CODE_SUCCESS) {
62,258✔
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
62,258✔
541
}
542

543
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
62,885✔
544
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
62,885✔
545
  const char*    id = GET_TASKID(pTaskInfo);
62,885✔
546
  int32_t        code = 0;
62,885✔
547

548
  if (isAdd) {
62,885✔
549
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
62,258✔
550
  }
551

552
  // traverse to the stream scanner node to add this table id
553
  SOperatorInfo* pInfo = NULL;
62,885✔
554
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
62,885✔
555
  if (code != 0 || pInfo == NULL) {
62,885✔
556
    return code;
×
557
  }
558

559
  SStreamScanInfo* pScanInfo = pInfo->info;
62,885✔
560
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
62,885✔
561
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
125,747✔
562
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
62,862✔
563
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
62,862✔
564
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
62,862✔
565
    }
566
  }
567

568
  if (isAdd) {  // add new table id
62,885✔
569
    SArray* qa = NULL;
62,258✔
570
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
62,258✔
571
    if (code != TSDB_CODE_SUCCESS) {
62,258✔
572
      taosArrayDestroy(qa);
×
573
      return code;
×
574
    }
575
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
62,258✔
576
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
62,258✔
577
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
62,258✔
578

579
    bool   assignUid = false;
62,258✔
580
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
62,258✔
581
    char*  keyBuf = NULL;
62,258✔
582
    if (bufLen > 0) {
62,258✔
583
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
584
      keyBuf = taosMemoryMalloc(bufLen);
×
585
      if (keyBuf == NULL) {
×
586
        taosArrayDestroy(qa);
×
587
        return terrno;
×
588
      }
589
    }
590

591
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
62,258✔
592
    taosWLockLatch(&pTaskInfo->lock);
62,258✔
593

594
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
97,619✔
595
      uint64_t* uid = taosArrayGet(qa, i);
35,361✔
596
      if (!uid) {
35,361✔
597
        taosMemoryFree(keyBuf);
×
598
        taosArrayDestroy(qa);
×
599
        taosWUnLockLatch(&pTaskInfo->lock);
×
600
        return terrno;
×
601
      }
602
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
35,361✔
603

604
      if (bufLen > 0) {
35,361✔
605
        if (assignUid) {
×
606
          keyInfo.groupId = keyInfo.uid;
×
607
        } else {
608
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
609
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
610
          if (code != TSDB_CODE_SUCCESS) {
×
611
            taosMemoryFree(keyBuf);
×
612
            taosArrayDestroy(qa);
×
613
            taosWUnLockLatch(&pTaskInfo->lock);
×
614
            return code;
×
615
          }
616
        }
617
      }
618

619
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
35,361✔
620
      if (code != TSDB_CODE_SUCCESS) {
35,361✔
621
        taosMemoryFree(keyBuf);
×
622
        taosArrayDestroy(qa);
×
623
        taosWUnLockLatch(&pTaskInfo->lock);
×
624
        return code;
×
625
      }
626
    }
627

628
    taosWUnLockLatch(&pTaskInfo->lock);
62,258✔
629
    if (keyBuf != NULL) {
62,258✔
630
      taosMemoryFree(keyBuf);
×
631
    }
632

633
    taosArrayDestroy(qa);
62,258✔
634
  } else {  // remove the table id in current list
635
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
627✔
636
    taosWLockLatch(&pTaskInfo->lock);
627✔
637
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
627✔
638
    taosWUnLockLatch(&pTaskInfo->lock);
627✔
639
  }
640

641
  return code;
62,885✔
642
}
643

644
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
258,498,334✔
645
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
646
                                    int32_t idx, bool* tbGet) {
647
  *tbGet = false;
258,498,334✔
648

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
258,515,332✔
650
    return TSDB_CODE_INVALID_PARA;
×
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
258,527,069✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
258,527,069✔
655
    return TSDB_CODE_SUCCESS;
148,268,179✔
656
  }
657

658
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
110,268,142✔
659
  if (!pSchemaInfo) {
110,250,215✔
660
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
661
    return terrno;
×
662
  }
663

664
  *sversion = pSchemaInfo->sw->version;
110,250,215✔
665
  *tversion = pSchemaInfo->tversion;
110,280,277✔
666
  *rversion = pSchemaInfo->rversion;
110,274,267✔
667
  if (pSchemaInfo->dbname) {
110,255,753✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
110,253,627✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
110,291,469✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
110,275,207✔
674
  } else {
675
    tableName[0] = 0;
2,688✔
676
  }
677

678
  *tbGet = true;
110,291,051✔
679

680
  return TSDB_CODE_SUCCESS;
110,276,401✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
148,252,777✔
684

685
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
686
  if (NULL == pParam) {
×
687
    return;
×
688
  }
689
  freeOperatorParam(pParam, OP_GET_PARAM);
×
690
}
691

692
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
663,187✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
663,187✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
663,187✔
695
}
663,187✔
696

697
int32_t qExecutorInit(void) {
4,643,946✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,643,946✔
699
  return TSDB_CODE_SUCCESS;
4,643,970✔
700
}
701

702
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
150,096,627✔
703
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
704
                        EOPTR_EXEC_MODEL model) {
705
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
150,096,627✔
706
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
150,096,627✔
707

708
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
150,110,234✔
709

710
  readHandle->uid = 0;
150,136,500✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
150,143,317✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
149,970,813✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
180,595✔
714
    goto _error;
25,598✔
715
  }
716

717
  if (handle) {
149,822,462✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
149,824,439✔
719
    void*           pSinkManager = NULL;
149,837,633✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
149,736,844✔
721
    if (code != TSDB_CODE_SUCCESS) {
149,728,450✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
149,728,450✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
149,872,302✔
728
    if (code != TSDB_CODE_SUCCESS) {
149,737,950✔
729
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
730
      taosMemoryFree(pSinkManager);
×
731
      goto _error;
×
732
    }
733

734
    SDataSinkNode* pSink = NULL;
149,737,950✔
735
    if (readHandle->localExec) {
149,866,137✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
45,694✔
737
      if (code != TSDB_CODE_SUCCESS) {
45,694✔
738
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
739
               (*pTask)->id.str);
740
        taosMemoryFree(pSinkManager);
×
741
        goto _error;
×
742
      }
743
    }
744

745
    // pSinkParam has been freed during create sinker.
746
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
149,717,201✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
149,938,520✔
748
    if (code) {
149,799,710✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
628✔
750
    }
751
  }
752

753
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
149,940,697✔
754
         tstrerror(code));
755

756
_error:
8,621,712✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
150,117,735✔
759
}
760

761
static void freeBlock(void* param) {
×
762
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
763
  blockDataDestroy(pBlock);
×
764
}
×
765

766
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
167,864,396✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
167,864,396✔
769
  int32_t        lino = 0;
167,864,396✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
167,864,396✔
771
  int64_t        threadId = taosGetSelfPthreadId();
167,864,396✔
772

773
  if (pLocal) {
167,855,531✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
161,913,804✔
775
  }
776

777
  taosArrayClear(pResList);
167,841,621✔
778

779
  int64_t curOwner = 0;
167,841,205✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
167,841,205✔
781
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
782
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
783
    return pTaskInfo->code;
×
784
  }
785

786
  if (pTaskInfo->cost.start == 0) {
167,843,199✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
148,625,609✔
788
  }
789

790
  if (isTaskKilled(pTaskInfo)) {
167,858,207✔
791
    atomic_store_64(&pTaskInfo->owner, 0);
×
792
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
793
    return pTaskInfo->code;
×
794
  }
795

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
167,858,476✔
798
  if (ret != TSDB_CODE_SUCCESS) {
167,981,770✔
799
    pTaskInfo->code = ret;
164,183✔
800
    (void)cleanUpUdfs();
164,183✔
801

802
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
164,183✔
803
    atomic_store_64(&pTaskInfo->owner, 0);
164,183✔
804

805
    return pTaskInfo->code;
164,183✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
167,817,587✔
809

810
  int32_t      current = 0;
167,820,862✔
811
  SSDataBlock* pRes = NULL;
167,820,862✔
812
  int64_t      st = taosGetTimestampUs();
167,861,736✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
167,861,736✔
815
    pTaskInfo->paramSet = true;
663,187✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
663,187✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
167,194,869✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
167,701,868✔
822
  code = blockDataCheck(pRes);
167,701,868✔
823
  QUERY_CHECK_CODE(code, lino, _end);
167,696,824✔
824

825
  if (pRes == NULL) {
167,696,824✔
826
    st = taosGetTimestampUs();
28,595,856✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
167,695,674✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
167,697,240✔
831
    rowsThreshold = 4096;
167,037,455✔
832
  }
833

834
  int32_t blockIndex = 0;
167,689,505✔
835
  while (pRes != NULL) {
539,550,118✔
836
    SSDataBlock* p = NULL;
385,611,721✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
385,507,207✔
838
      SSDataBlock* p1 = NULL;
284,049,379✔
839
      code = createOneDataBlock(pRes, true, &p1);
284,050,996✔
840
      QUERY_CHECK_CODE(code, lino, _end);
284,039,108✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
284,039,108✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
284,048,419✔
844
      p = p1;
284,048,419✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
101,608,416✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
101,610,881✔
848

849
      p = *(SSDataBlock**)tmp;
101,610,881✔
850
      code = copyDataBlock(p, pRes);
101,610,881✔
851
      QUERY_CHECK_CODE(code, lino, _end);
101,614,003✔
852
    }
853

854
    blockIndex += 1;
385,660,101✔
855

856
    current += p->info.rows;
385,660,101✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
385,627,155✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
385,649,460✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
385,649,460✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
385,649,460✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
371,891,818✔
867
    QUERY_CHECK_CODE(code, lino, _end);
371,719,246✔
868
    code = blockDataCheck(pRes);
371,719,246✔
869
    QUERY_CHECK_CODE(code, lino, _end);
371,907,366✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
167,696,039✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
655,818✔
874
  }
875

876
  *hasMore = (pRes != NULL);
167,699,897✔
877
  uint64_t el = (taosGetTimestampUs() - st);
167,690,513✔
878

879
  pTaskInfo->cost.elapsedTime += el;
167,690,513✔
880
  if (NULL == pRes) {
167,684,984✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
153,927,342✔
882
  }
883

884
_end:
167,630,866✔
885
  (void)cleanUpUdfs();
167,676,560✔
886

887
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
167,713,910✔
888
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
167,713,910✔
889
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
890

891
  atomic_store_64(&pTaskInfo->owner, 0);
167,713,910✔
892
  if (code) {
167,713,910✔
893
    pTaskInfo->code = code;
×
894
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
895
  }
896

897
  return pTaskInfo->code;
167,713,910✔
898
}
899

900
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
901
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
902
  SArray*        pList = pTaskInfo->pResultBlockList;
×
903
  size_t         num = taosArrayGetSize(pList);
×
904
  for (int32_t i = 0; i < num; ++i) {
×
905
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
906
    if (p) {
×
907
      blockDataDestroy(*p);
×
908
    }
909
  }
910

911
  taosArrayClear(pTaskInfo->pResultBlockList);
×
912
}
×
913

914
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
37,169,126✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
37,169,126✔
916
  int64_t        threadId = taosGetSelfPthreadId();
37,169,126✔
917
  int64_t        curOwner = 0;
37,169,234✔
918

919
  *pRes = NULL;
37,169,234✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
37,169,234✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
37,169,450✔
924
  if (isKilled) {
37,169,450✔
925
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
926

927
    taosRUnLockLatch(&pTaskInfo->lock);
×
928
    return pTaskInfo->code;
×
929
  }
930

931
  if (pTaskInfo->owner != 0) {
37,169,450✔
932
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
933
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
934

935
    taosRUnLockLatch(&pTaskInfo->lock);
×
936
    return pTaskInfo->code;
×
937
  }
938

939
  pTaskInfo->owner = threadId;
37,168,320✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
37,169,450✔
941

942
  if (pTaskInfo->cost.start == 0) {
37,169,558✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
89,834✔
944
  }
945

946
  // error occurs, record the error code and return to client
947
  int32_t ret = setjmp(pTaskInfo->env);
37,169,558✔
948
  if (ret != TSDB_CODE_SUCCESS) {
37,169,260✔
949
    pTaskInfo->code = ret;
×
950
    (void)cleanUpUdfs();
×
951
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
952
    atomic_store_64(&pTaskInfo->owner, 0);
×
953
    return pTaskInfo->code;
×
954
  }
955

956
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
37,169,260✔
957

958
  int64_t st = taosGetTimestampUs();
37,168,910✔
959
  int32_t code = TSDB_CODE_SUCCESS;
37,168,910✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
37,168,910✔
961
    pTaskInfo->paramSet = true;
×
962
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
963
  } else {
964
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
37,168,586✔
965
  }
966
  if (code) {
37,163,775✔
967
    pTaskInfo->code = code;
×
968
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
969
  }
970

971
  code = blockDataCheck(*pRes);
37,163,775✔
972
  if (code) {
37,168,101✔
973
    pTaskInfo->code = code;
×
974
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
975
  }
976

977
  uint64_t el = (taosGetTimestampUs() - st);
37,162,034✔
978

979
  pTaskInfo->cost.elapsedTime += el;
37,162,034✔
980
  if (NULL == *pRes) {
37,166,134✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,104,307✔
982
  }
983

984
  (void)cleanUpUdfs();
37,166,365✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
37,169,342✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
37,169,558✔
988

989
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
37,169,558✔
990
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
991

992
  atomic_store_64(&pTaskInfo->owner, 0);
37,169,450✔
993
  return pTaskInfo->code;
37,169,450✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
48,479,642✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
48,479,642✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
48,480,016✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
48,480,347✔
1000

1001
  if (!tmp) {
48,479,441✔
1002
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1003
    return terrno;
×
1004
  }
1005
  return TSDB_CODE_SUCCESS;
48,479,441✔
1006
}
1007

1008
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1009
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1010
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1011

1012
  if (key->refId < pInfo->refId) {
×
1013
    return -1;
×
1014
  } else if (key->refId > pInfo->refId) {
×
1015
    return 1;
×
1016
  }
1017

1018
  return 0;
×
1019
}
1020

1021
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1022
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1023
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1024
  if (idx >= 0) {
×
1025
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1026
  }
1027
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1028
}
×
1029

1030
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
9,869✔
1031
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
9,869✔
1032

1033
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
9,869✔
1034
  for (int32_t i = 0; i < num; ++i) {
12,407✔
1035
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
2,538✔
1036
    if (!pStop) {
2,538✔
1037
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1038
      continue;
×
1039
    }
1040
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
2,538✔
1041
    if (pExchangeInfo) {
2,538✔
1042
      int32_t code = tsem_post(&pExchangeInfo->ready);
2,538✔
1043
      if (code != TSDB_CODE_SUCCESS) {
2,538✔
1044
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1045
      } else {
1046
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
2,538✔
1047
      }
1048
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
2,538✔
1049
      if (code != TSDB_CODE_SUCCESS) {
2,538✔
1050
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1051
      }
1052
    }
1053
  }
1054

1055
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
9,869✔
1056
}
9,869✔
1057

1058
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
9,869✔
1059
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
9,869✔
1060
  if (pTaskInfo == NULL) {
9,869✔
1061
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1062
  }
1063

1064
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
9,869✔
1065

1066
  setTaskKilled(pTaskInfo, rspCode);
9,869✔
1067
  qStopTaskOperators(pTaskInfo);
9,869✔
1068

1069
  return TSDB_CODE_SUCCESS;
9,869✔
1070
}
1071

1072
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1073
  int64_t        st = taosGetTimestampMs();
×
1074
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1075
  if (pTaskInfo == NULL) {
×
1076
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1077
  }
1078

1079
  if (waitDuration > 0) {
×
1080
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1081
  } else {
1082
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1083
  }
1084

1085
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1086

1087
  if (waitDuration > 0) {
×
1088
    while (1) {
1089
      taosWLockLatch(&pTaskInfo->lock);
×
1090
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1091
        taosWUnLockLatch(&pTaskInfo->lock);
×
1092

1093
        taosMsleep(200);
×
1094

1095
        int64_t d = taosGetTimestampMs() - st;
×
1096
        if (d >= waitDuration && waitDuration >= 0) {
×
1097
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1098
          return TSDB_CODE_SUCCESS;
×
1099
        }
1100
      } else {  // not running now
1101
        pTaskInfo->code = rspCode;
×
1102
        taosWUnLockLatch(&pTaskInfo->lock);
×
1103
        return TSDB_CODE_SUCCESS;
×
1104
      }
1105
    }
1106
  }
1107

1108
  int64_t et = taosGetTimestampMs() - st;
×
1109
  if (et < waitDuration) {
×
1110
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1111
    return TSDB_CODE_SUCCESS;
×
1112
  }
1113
  return TSDB_CODE_SUCCESS;
×
1114
}
1115

1116
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1117
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1118
  if (NULL == pTaskInfo) {
×
1119
    return false;
×
1120
  }
1121

1122
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1123
}
1124

1125
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
150,814,859✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
150,814,859✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
150,814,897✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
150,812,051✔
1130
  if (pSummary->pRecoder != NULL) {
150,809,821✔
1131
    qDebug(
109,462,933✔
1132
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1133
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1134
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1135
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1136
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1137
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1138
  } else {
1139
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
41,344,865✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
150,807,798✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
158,254,335✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
158,254,335✔
1146
  if (pTaskInfo == NULL) {
158,254,335✔
1147
    return;
7,441,027✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
150,813,308✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
150,813,901✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
150,814,321✔
1157
  doDestroyTask(pTaskInfo);
150,814,221✔
1158
}
1159

1160
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,603,870✔
1161
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,603,870✔
1162
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,603,870✔
1163
}
1164

1165
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
111,162✔
1166
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
111,162✔
1167
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
111,162✔
1168

1169
  while (1) {
110,548✔
1170
    uint16_t type = pOperator->operatorType;
221,779✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
221,779✔
1172
      *scanner = pOperator->info;
111,231✔
1173
      break;
111,231✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
110,548✔
1176
    }
1177
  }
1178
}
111,231✔
1179

1180
void* qExtractReaderFromTmqScanner(void* scanner) {
111,231✔
1181
  SStreamScanInfo* pInfo = scanner;
111,231✔
1182
  return (void*)pInfo->tqReader;
111,231✔
1183
}
1184

1185
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
138,948✔
1186
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
138,948✔
1187
  return pTaskInfo->streamInfo.schema;
138,948✔
1188
}
1189

1190
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
138,948✔
1191
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
138,948✔
1192
  return pTaskInfo->streamInfo.tbName;
138,948✔
1193
}
1194

1195
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
144,082✔
1196
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
144,082✔
1197
  return &pTaskInfo->streamInfo.btMetaRsp;
144,082✔
1198
}
1199

1200
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
3,523,251✔
1201
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,523,251✔
1202
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
3,523,251✔
1203
  return 0;
3,523,343✔
1204
  /*if (code != TSDB_CODE_SUCCESS) {
1205
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1206
    pTaskInfo->code = code;
1207
    T_LONG_JMP(pTaskInfo->env, code);
1208
  }*/
1209
}
1210

1211
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
135,598✔
1212
  memset(pCond, 0, sizeof(SQueryTableDataCond));
135,598✔
1213
  pCond->order = TSDB_ORDER_ASC;
135,598✔
1214
  pCond->numOfCols = pMtInfo->schema->nCols;
135,651✔
1215
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
135,651✔
1216
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
135,651✔
1217
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
135,598✔
1218
    taosMemoryFreeClear(pCond->colList);
×
1219
    taosMemoryFreeClear(pCond->pSlotList);
×
1220
    return terrno;
×
1221
  }
1222

1223
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
135,651✔
1224
  pCond->suid = pMtInfo->suid;
135,713✔
1225
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
135,651✔
1226
  pCond->startVersion = -1;
135,651✔
1227
  pCond->endVersion = sContext->snapVersion;
135,651✔
1228

1229
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
862,480✔
1230
    SColumnInfo* pColInfo = &pCond->colList[i];
726,944✔
1231
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
726,838✔
1232
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
726,944✔
1233
    if (pMtInfo->pExtSchemas != NULL) {
726,882✔
1234
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
6,924✔
1235
    }
1236
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
726,891✔
1237
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
726,891✔
1238

1239
    pCond->pSlotList[i] = i;
726,838✔
1240
  }
1241

1242
  return TSDB_CODE_SUCCESS;
135,766✔
1243
}
1244

1245
void qStreamSetOpen(qTaskInfo_t tinfo) {
36,888,463✔
1246
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
36,888,463✔
1247
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
36,888,463✔
1248
  pOperator->status = OP_NOT_OPENED;
36,891,763✔
1249
}
36,891,507✔
1250

1251
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
3,376,090✔
1252
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,376,090✔
1253
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
3,376,090✔
1254
}
3,376,302✔
1255

1256
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
3,567,175✔
1257
  int32_t        code = TSDB_CODE_SUCCESS;
3,567,175✔
1258
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,567,175✔
1259
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
3,567,175✔
1260

1261
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
3,567,797✔
1262
  const char*    id = GET_TASKID(pTaskInfo);
3,567,438✔
1263

1264
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
3,567,207✔
1265
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,319,511✔
1266
    if (pOperator == NULL || code != 0) {
3,318,368✔
1267
      return code;
×
1268
    }
1269

1270
    SStreamScanInfo* pInfo = pOperator->info;
3,318,417✔
1271
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,318,553✔
1272
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,318,719✔
1273
    walReaderVerifyOffset(pWalReader, pOffset);
3,320,160✔
1274
  }
1275
  // if pOffset equal to current offset, means continue consume
1276
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
3,567,088✔
1277
    return 0;
3,177,885✔
1278
  }
1279

1280
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
388,796✔
1281
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
250,170✔
1282
    if (pOperator == NULL || code != 0) {
250,274✔
1283
      return code;
×
1284
    }
1285

1286
    SStreamScanInfo* pInfo = pOperator->info;
250,274✔
1287
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
249,766✔
1288
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
249,789✔
1289
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
249,904✔
1290

1291
    if (pOffset->type == TMQ_OFFSET__LOG) {
250,064✔
1292
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
203,236✔
1293
      pScanBaseInfo->dataReader = NULL;
202,695✔
1294

1295
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
202,695✔
1296
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
202,695✔
1297
      walReaderVerifyOffset(pWalReader, pOffset);
202,773✔
1298
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
203,236✔
1299
      if (code < 0) {
203,236✔
1300
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
6,192✔
1301
        return code;
6,192✔
1302
      }
1303
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
47,039✔
1304
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1305
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1306
      int64_t uid = pOffset->uid;
47,251✔
1307
      int64_t ts = pOffset->ts;
47,113✔
1308
      int32_t index = 0;
47,105✔
1309

1310
      // this value may be changed if new tables are created
1311
      taosRLockLatch(&pTaskInfo->lock);
47,105✔
1312
      int32_t numOfTables = 0;
47,251✔
1313
      code = tableListGetSize(pTableListInfo, &numOfTables);
47,179✔
1314
      if (code != TSDB_CODE_SUCCESS) {
46,971✔
1315
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1316
        taosRUnLockLatch(&pTaskInfo->lock);
×
1317
        return code;
×
1318
      }
1319

1320
      if (uid == 0) {
46,971✔
1321
        if (numOfTables != 0) {
45,729✔
1322
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
7,531✔
1323
          if (!tmp) {
7,603✔
1324
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1325
            taosRUnLockLatch(&pTaskInfo->lock);
×
1326
            return terrno;
×
1327
          }
1328
          if (tmp) uid = tmp->uid;
7,603✔
1329
          ts = INT64_MIN;
7,603✔
1330
          pScanInfo->currentTable = 0;
7,603✔
1331
        } else {
1332
          taosRUnLockLatch(&pTaskInfo->lock);
38,198✔
1333
          qError("no table in table list, %s", id);
38,262✔
1334
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
38,336✔
1335
        }
1336
      }
1337
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
8,703✔
1338

1339
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
8,915✔
1340
             pInfo->pTableScanOp->resultInfo.totalRows);
1341
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
8,915✔
1342

1343
      // start from current accessed position
1344
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1345
      // position, let's find it from the beginning.
1346
      index = tableListFind(pTableListInfo, uid, 0);
8,915✔
1347
      taosRUnLockLatch(&pTaskInfo->lock);
8,915✔
1348

1349
      if (index >= 0) {
8,915✔
1350
        pScanInfo->currentTable = index;
8,915✔
1351
      } else {
1352
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1353
               numOfTables, pScanInfo->currentTable, id);
1354
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1355
      }
1356

1357
      STableKeyInfo keyInfo = {.uid = uid};
8,915✔
1358
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
8,915✔
1359

1360
      // let's start from the next ts that returned to consumer.
1361
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
8,915✔
1362
        pScanBaseInfo->cond.twindows.skey = ts;
×
1363
      } else {
1364
        pScanBaseInfo->cond.twindows.skey = ts + 1;
8,915✔
1365
      }
1366
      pScanInfo->scanTimes = 0;
8,915✔
1367

1368
      if (pScanBaseInfo->dataReader == NULL) {
8,915✔
1369
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
15,208✔
1370
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1371
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
7,604✔
1372
        if (code != TSDB_CODE_SUCCESS) {
7,534✔
1373
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1374
          return code;
×
1375
        }
1376

1377
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
7,534✔
1378
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1379
      } else {
1380
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,311✔
1381
        if (code != TSDB_CODE_SUCCESS) {
1,311✔
1382
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1383
          return code;
×
1384
        }
1385

1386
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
1,311✔
1387
        if (code != TSDB_CODE_SUCCESS) {
1,311✔
1388
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1389
          return code;
×
1390
        }
1391
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
1,311✔
1392
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1393
      }
1394

1395
      // restore the key value
1396
      pScanBaseInfo->cond.twindows.skey = oldSkey;
8,845✔
1397
    } else {
1398
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1399
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1400
    }
1401

1402
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1403
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
138,626✔
1404
      SStreamRawScanInfo* pInfo = pOperator->info;
135,932✔
1405
      SSnapContext*       sContext = pInfo->sContext;
135,932✔
1406
      SOperatorInfo*      p = NULL;
135,932✔
1407

1408
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
135,932✔
1409
      if (code != 0) {
135,932✔
1410
        return code;
×
1411
      }
1412

1413
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
135,932✔
1414

1415
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
135,932✔
1416
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1417
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1418
      }
1419

1420
      SMetaTableInfo mtInfo = {0};
135,932✔
1421
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
135,932✔
1422
      if (code != 0) {
135,870✔
1423
        destroyMetaTableInfo(&mtInfo);
1424
        return code;
×
1425
      }
1426
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
135,870✔
1427
      pInfo->dataReader = NULL;
135,817✔
1428

1429
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
135,932✔
1430
      tableListClear(pTableListInfo);
135,870✔
1431

1432
      if (mtInfo.uid == 0) {
135,932✔
1433
        destroyMetaTableInfo(&mtInfo);
1434
        goto end;  // no data
166✔
1435
      }
1436

1437
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
135,766✔
1438
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
135,713✔
1439
      if (code != TSDB_CODE_SUCCESS) {
135,704✔
1440
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1441
        destroyMetaTableInfo(&mtInfo);
1442
        return code;
×
1443
      }
1444
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
135,704✔
1445
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1446
      } else {
1447
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
135,704✔
1448
      }
1449

1450
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
135,651✔
1451
      if (code != TSDB_CODE_SUCCESS) {
135,766✔
1452
        destroyMetaTableInfo(&mtInfo);
1453
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1454
        return code;
×
1455
      }
1456

1457
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
135,766✔
1458
      if (!pList) {
135,766✔
1459
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1460
        destroyMetaTableInfo(&mtInfo);
1461
        return code;
×
1462
      }
1463
      int32_t size = 0;
135,766✔
1464
      code = tableListGetSize(pTableListInfo, &size);
135,766✔
1465
      if (code != TSDB_CODE_SUCCESS) {
135,766✔
1466
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1467
        destroyMetaTableInfo(&mtInfo);
1468
        return code;
×
1469
      }
1470

1471
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
271,532✔
1472
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
135,766✔
1473
      if (code != TSDB_CODE_SUCCESS) {
135,713✔
1474
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478

1479
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
135,713✔
1480
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
135,589✔
1481
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1482
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
135,651✔
1483
      pTaskInfo->streamInfo.schema = mtInfo.schema;
135,651✔
1484
      taosMemoryFreeClear(mtInfo.pExtSchemas);
135,704✔
1485

1486
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
135,704✔
1487
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
2,694✔
1488
      SStreamRawScanInfo* pInfo = pOperator->info;
952✔
1489
      SSnapContext*       sContext = pInfo->sContext;
952✔
1490
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
952✔
1491
      if (code != 0) {
952✔
1492
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1493
        return code;
×
1494
      }
1495
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
952✔
1496
             id);
1497
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
1,742✔
1498
      SStreamRawScanInfo* pInfo = pOperator->info;
1,742✔
1499
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,742✔
1500
      pInfo->dataReader = NULL;
1,742✔
1501
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,742✔
1502
    }
1503
  }
1504

1505
end:
344,209✔
1506
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
344,488✔
1507
  return 0;
344,585✔
1508
}
1509

1510
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
100,195,685✔
1511
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
100,195,685✔
1512
  if (pMsg->info.ahandle == NULL) {
100,204,962✔
1513
    qError("pMsg->info.ahandle is NULL");
×
1514
    return;
×
1515
  }
1516

1517
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
100,188,906✔
1518
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1519

1520
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
100,197,644✔
1521

1522
  if (pMsg->contLen > 0) {
100,196,784✔
1523
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
100,187,216✔
1524
    if (buf.pData == NULL) {
100,178,622✔
1525
      pMsg->code = terrno;
×
1526
    } else {
1527
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
100,178,622✔
1528
    }
1529
  }
1530

1531
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
100,203,554✔
1532
  rpcFreeCont(pMsg->pCont);
100,208,275✔
1533
  destroySendMsgInfo(pSendInfo);
100,178,999✔
1534
}
1535

1536
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1537
  int32_t        code = TSDB_CODE_SUCCESS;
×
1538
  int32_t        lino = 0;
×
1539
  SExecTaskInfo* pTaskInfo = tinfo;
×
1540
  SArray*        plist = NULL;
×
1541

1542
  code = getTableListInfo(pTaskInfo, &plist);
×
1543
  if (code || plist == NULL) {
×
1544
    return NULL;
×
1545
  }
1546

1547
  // only extract table in the first elements
1548
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1549

1550
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1551
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1552

1553
  int32_t numOfTables = 0;
×
1554
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1555
  QUERY_CHECK_CODE(code, lino, _end);
×
1556

1557
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1558
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1559
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1560
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1561
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1562
  }
1563

1564
  taosArrayDestroy(plist);
×
1565

1566
_end:
×
1567
  if (code != TSDB_CODE_SUCCESS) {
×
1568
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1569
    T_LONG_JMP(pTaskInfo->env, code);
×
1570
  }
1571
  return pUidList;
×
1572
}
1573

1574
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,376,722✔
1575
  int32_t        code = TSDB_CODE_SUCCESS;
3,376,722✔
1576
  int32_t        lino = 0;
3,376,722✔
1577
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,376,722✔
1578

1579
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,376,722✔
1580
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1581
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1582

1583
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1584
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1585
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,375,530✔
1586
    STableScanInfo* pScanInfo = pOperator->info;
1,687,765✔
1587

1588
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,687,765✔
1589
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,688,361✔
1590
  } else {
1591
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,688,957✔
1592
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,688,361✔
1593
    }
1594
  }
1595

1596
_end:
×
1597
  if (code != TSDB_CODE_SUCCESS) {
3,376,722✔
1598
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1599
  }
1600
  return code;
3,376,722✔
1601
}
1602

1603
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,688,957✔
1604
  if (pList == NULL) {
1,688,957✔
1605
    return TSDB_CODE_INVALID_PARA;
×
1606
  }
1607

1608
  *pList = NULL;
1,688,957✔
1609
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,688,957✔
1610
  if (pArray == NULL) {
1,688,361✔
1611
    return terrno;
×
1612
  }
1613

1614
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,688,361✔
1615
  if (code == 0) {
1,688,361✔
1616
    *pList = pArray;
1,688,361✔
1617
  } else {
1618
    taosArrayDestroy(pArray);
×
1619
  }
1620
  return code;
1,688,361✔
1621
}
1622

1623
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1624
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1625
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1626
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1627
  }
1628
  return 0;
×
1629
}
1630

1631
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1632
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1633
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1634
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1635
  }
1636
  return 0;
×
1637
}
1638

1639
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1640
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1641

1642
  int32_t code = pTaskInfo->code;
×
1643
  pTaskInfo->code = 0;
×
1644
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1645
}
×
1646

1647
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1648
  int32_t code = 0;
×
1649
  return code;
×
1650
}
1651

1652
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1653
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1654
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1655
  return code;
×
1656
}
1657

1658
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
33,458,147✔
1659
  int32_t code = 0;
33,458,147✔
1660

1661
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
33,458,147✔
1662
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
33,459,680✔
1663

1664
  if (pOper->fpSet.resetStateFn) {
33,458,512✔
1665
    code = pOper->fpSet.resetStateFn(pOper);
33,457,722✔
1666
  }
1667
  pOper->status = OP_NOT_OPENED;
33,455,177✔
1668
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
55,862,368✔
1669
    code = clearStatesForOperator(pOper->pDownstream[i]);
22,401,098✔
1670
  }
1671
  return code;
33,462,857✔
1672
}
1673

1674
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
11,058,111✔
1675
  int32_t        code = 0;
11,058,111✔
1676
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,058,111✔
1677
  SOperatorInfo* pOper = pTaskInfo->pRoot;
11,058,111✔
1678
  pTaskInfo->code = TSDB_CODE_SUCCESS;
11,058,111✔
1679
  code = clearStatesForOperator(pOper);
11,058,111✔
1680
  return code;
11,058,510✔
1681
}
1682

1683
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
15,317,386✔
1684
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
15,317,386✔
1685
  int64_t        threadId = taosGetSelfPthreadId();
15,317,386✔
1686
  int64_t        curOwner = 0;
15,316,987✔
1687

1688
  *ppRes = NULL;
15,316,987✔
1689

1690
  // todo extract method
1691
  taosRLockLatch(&pTaskInfo->lock);
15,316,987✔
1692
  bool isKilled = isTaskKilled(pTaskInfo);
15,317,386✔
1693
  if (isKilled) {
15,316,961✔
1694
    // clearStreamBlock(pTaskInfo->pRoot);
1695
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1696

1697
    taosRUnLockLatch(&pTaskInfo->lock);
×
1698
    return pTaskInfo->code;
×
1699
  }
1700

1701
  if (pTaskInfo->owner != 0) {
15,316,961✔
1702
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1703
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1704

1705
    taosRUnLockLatch(&pTaskInfo->lock);
×
1706
    return pTaskInfo->code;
×
1707
  }
1708

1709
  pTaskInfo->owner = threadId;
15,317,386✔
1710
  taosRUnLockLatch(&pTaskInfo->lock);
15,317,386✔
1711

1712
  if (pTaskInfo->cost.start == 0) {
15,316,983✔
1713
    pTaskInfo->cost.start = taosGetTimestampUs();
318,505✔
1714
  }
1715

1716
  // error occurs, record the error code and return to client
1717
  int32_t ret = setjmp(pTaskInfo->env);
15,316,983✔
1718
  if (ret != TSDB_CODE_SUCCESS) {
16,833,329✔
1719
    pTaskInfo->code = ret;
1,517,114✔
1720
    (void)cleanUpUdfs();
1,517,114✔
1721
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,517,114✔
1722
    atomic_store_64(&pTaskInfo->owner, 0);
1,517,114✔
1723
    return pTaskInfo->code;
1,517,114✔
1724
  }
1725

1726
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
15,316,215✔
1727

1728
  int64_t st = taosGetTimestampUs();
15,316,584✔
1729

1730
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
15,316,584✔
1731
  if (code) {
13,800,272✔
1732
    pTaskInfo->code = code;
×
1733
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1734
  } else {
1735
    *finished = *ppRes == NULL;
13,800,272✔
1736
    code = blockDataCheck(*ppRes);
13,800,272✔
1737
  }
1738
  if (code) {
13,799,870✔
1739
    pTaskInfo->code = code;
×
1740
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1741
  }
1742

1743
  uint64_t el = (taosGetTimestampUs() - st);
13,800,272✔
1744

1745
  pTaskInfo->cost.elapsedTime += el;
13,800,272✔
1746
  if (NULL == *ppRes) {
13,800,272✔
1747
    *useconds = pTaskInfo->cost.elapsedTime;
9,015,883✔
1748
  }
1749

1750
  (void)cleanUpUdfs();
13,799,873✔
1751

1752
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
13,800,272✔
1753
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
13,800,272✔
1754

1755
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
13,800,272✔
1756
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1757

1758
  atomic_store_64(&pTaskInfo->owner, 0);
13,800,272✔
1759
  return pTaskInfo->code;
13,800,272✔
1760
}
1761

1762
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1763
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1764
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1765
// }
1766

1767
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
327,889✔
1768
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1769
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1770
  int32_t code = 0;                                        
327,889✔
1771
  if (*pTableListInfo != NULL) {
327,889✔
1772
    qDebug("table list already exists, no need to create again");
×
1773
    goto end;
×
1774
  }
1775
  STableListInfo* pList = tableListCreate();
327,889✔
1776
  if (pList == NULL) {
327,889✔
1777
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1778
    code = terrno;
×
1779
    goto end;
×
1780
  }
1781

1782
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
327,889✔
1783
  SReadHandle    pHandle = {.vnode = pVnode};
327,889✔
1784
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
327,889✔
1785

1786
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
327,889✔
1787
  if (code != 0) {
327,889✔
1788
    tableListDestroy(pList);
×
1789
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1790
    goto end;
×
1791
  }
1792
  *pTableListInfo = pList;
327,889✔
1793

1794
end:
327,889✔
1795
  return 0;
327,889✔
1796
}
1797

1798
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
108,372✔
1799
  bool   listAdded = false;
108,372✔
1800
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
108,372✔
1801
  if (code == 0 && !listAdded) {
108,372✔
1802
    int32_t numOfTables = taosArrayGetSize(pUidList);
99,362✔
1803
    for (int i = 0; i < numOfTables; i++) {
198,724✔
1804
      void* tmp = taosArrayGet(pUidList, i);
99,362✔
1805
      if (tmp == NULL) {
99,362✔
1806
        return terrno;
×
1807
      }
1808
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
99,362✔
1809

1810
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
99,362✔
1811
      if (p == NULL) {
99,362✔
1812
        return terrno;
×
1813
      }
1814
    }
1815
  }
1816
  return code;
108,372✔
1817
}
1818

1819
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
108,372✔
1820
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1821
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1822
  int32_t code = TSDB_CODE_SUCCESS;
108,372✔
1823
  STableListInfo* pList = tableListCreate();
108,372✔
1824
  if (pList == NULL) {
108,372✔
1825
    code = terrno;
×
1826
    goto end;
×
1827
  }
1828
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
108,372✔
1829
  if (uidListCopy == NULL) {
108,372✔
1830
    code = terrno;
×
1831
    goto end;
×
1832
  }
1833
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
108,372✔
1834
  SReadHandle    pHandle = {.vnode = pVnode};
108,372✔
1835

1836
  pList->idInfo.suid = suid;
108,372✔
1837
  pList->idInfo.tableType = TD_SUPER_TABLE;
108,372✔
1838
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
108,372✔
1839
  if (code != TSDB_CODE_SUCCESS) {
108,372✔
1840
    goto end;
×
1841
  }                                              
1842
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
108,372✔
1843
  if (code != TSDB_CODE_SUCCESS) {
108,372✔
1844
    goto end;
×
1845
  }
1846
  *tableList = pList->pTableList;
108,372✔
1847
  pList->pTableList = NULL;
108,372✔
1848

1849
  taosArrayClear(uidList);
108,372✔
1850
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
216,744✔
1851
    void* tmp = taosArrayGet(uidListCopy, i);
108,372✔
1852
    if (tmp == NULL) {
108,372✔
1853
      continue;
×
1854
    }
1855
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
108,372✔
1856
    if (slot == NULL) {
108,372✔
1857
      if (taosArrayPush(uidList, tmp) == NULL) {
6,093✔
1858
        code = terrno;
×
1859
        goto end;
×
1860
      }
1861
    }
1862
  }
1863
end:
108,372✔
1864
  taosArrayDestroy(uidListCopy);
108,372✔
1865
  tableListDestroy(pList);
108,372✔
1866
  return code;
108,372✔
1867
}
1868

1869
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1870
//   int32_t index = -1;
1871
//   (void)taosThreadRwlockRdlock(lock);
1872
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1873
//     index = 0;
1874
//     goto end;
1875
//   }
1876
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1877
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1878

1879
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1880
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1881
//       index = i;
1882
//       goto end;
1883
//     }
1884
//   }
1885
// end:
1886
//   (void)taosThreadRwlockUnlock(lock);
1887
//   return index;
1888
// }
1889

1890
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
327,889✔
1891
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
327,889✔
1892
  STableListInfo* pList = pTableListInfo;
327,889✔
1893
  return pList->pTableList;
327,889✔
1894
}
1895

1896
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,890,423✔
1897

1898
void streamDestroyExecTask(qTaskInfo_t tInfo) {
3,325,810✔
1899
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
3,325,810✔
1900
  qDestroyTask(tInfo);
3,325,810✔
1901
}
3,325,385✔
1902

1903
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
872,197✔
1904
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
872,197✔
1905
}
1906

1907
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
905,000✔
1908
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1909
  int32_t      code = 0;
905,000✔
1910
  SNode*       pNode = 0;
905,000✔
1911
  SNodeList*   pList = NULL;
905,000✔
1912
  SExprInfo*   pExprInfo = NULL;
905,000✔
1913
  int32_t      numOfExprs = 1;
905,000✔
1914
  int32_t*     offset = 0;
905,000✔
1915
  STargetNode* pTargetNode = NULL;
905,000✔
1916
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
905,000✔
1917
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
905,000✔
1918

1919
  if (code == 0) {
905,000✔
1920
    pTargetNode->dataBlockId = 0;
905,000✔
1921
    pTargetNode->pExpr = pNode;
905,000✔
1922
    pTargetNode->slotId = 0;
905,000✔
1923
  }
1924
  if (code == 0) {
905,000✔
1925
    code = nodesMakeList(&pList);
905,000✔
1926
  }
1927
  if (code == 0) {
905,000✔
1928
    code = nodesListAppend(pList, (SNode*)pTargetNode);
905,000✔
1929
  }
1930
  if (code == 0) {
905,000✔
1931
    pNode = NULL;
905,000✔
1932
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
905,000✔
1933
  }
1934

1935
  if (code == 0) {
905,000✔
1936
    const char* pVal = NULL;
905,000✔
1937
    int32_t     len = 0;
905,000✔
1938
    SNode*      pSclNode = NULL;
905,000✔
1939
    switch (pExprInfo->pExpr->nodeType) {
905,000✔
1940
      case QUERY_NODE_FUNCTION:
905,000✔
1941
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
905,000✔
1942
        break;
905,000✔
1943
      case QUERY_NODE_OPERATOR:
×
1944
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1945
        break;
×
1946
      default:
×
1947
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1948
        break;
×
1949
    }
1950
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
905,000✔
1951
    SSDataBlock block = {0};
905,000✔
1952
    block.info.rows = 1;
905,000✔
1953
    SSDataBlock* pBlock = &block;
905,000✔
1954
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
905,000✔
1955
    if (tmp == NULL) {
905,000✔
1956
      code = terrno;
×
1957
    }
1958
    if (code == 0) {
905,000✔
1959
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
905,000✔
1960
    }
1961
    taosArrayDestroy(pBlockList);
904,114✔
1962
  }
1963
  nodesDestroyList(pList);
904,600✔
1964
  destroyExprInfo(pExprInfo, numOfExprs);
904,514✔
1965
  taosMemoryFreeClear(pExprInfo);
904,514✔
1966
  return code;
904,028✔
1967
}
1968

1969
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,250,351✔
1970
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,250,351✔
1971
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,250,351✔
1972
  int32_t        code = 0;
4,250,341✔
1973
  SNode*         pNode = NULL;
4,250,341✔
1974
  if (!pForceOutputCols) return 0;
4,250,766✔
1975
  if (!*pRes) {
32,803✔
1976
    code = createDataBlock(pRes);
32,803✔
1977
  }
1978

1979
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
32,803✔
1980
    int32_t idx = 0;
32,803✔
1981
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
130,833✔
1982
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
98,030✔
1983
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
98,030✔
1984
      colInfo.info.precision = pCol->type.precision;
98,030✔
1985
      colInfo.info.scale = pCol->type.scale;
98,030✔
1986
      code = blockDataAppendColInfo(*pRes, &colInfo);
98,030✔
1987
      if (code != 0) break;
98,030✔
1988
    }
1989
  }
1990

1991
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
32,803✔
1992
  if (code != TSDB_CODE_SUCCESS) {
32,803✔
1993
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1994
    return code;
×
1995
  }
1996

1997
  // loop all exprs for force output, execute all exprs
1998
  int32_t idx = 0;
32,803✔
1999
  int32_t rowIdx = (*pRes)->info.rows;
32,803✔
2000
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
32,803✔
2001
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
32,803✔
2002
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
130,833✔
2003
    SScalarParam   dst = {0};
98,030✔
2004
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
98,030✔
2005
    code = nodesStringToNode(pCol->expr, &pNode);
98,030✔
2006
    if (code != 0) break;
98,030✔
2007
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
98,030✔
2008
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
98,030✔
2009
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
65,227✔
2010
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
65,227✔
2011
    } else {
2012
      dst.columnData = pInfo;
32,803✔
2013
      dst.numOfRows = rowIdx;
32,803✔
2014
      dst.colAlloced = false;
32,803✔
2015
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
32,803✔
2016
    }
2017
    ++idx;
98,030✔
2018
    // TODO sclFreeParam(&dst);
2019
    nodesDestroyNode(pNode);
98,030✔
2020
    if (code != 0) break;
98,030✔
2021
  }
2022
  if (code == TSDB_CODE_SUCCESS) {
32,803✔
2023
    (*pRes)->info.rows++;
32,803✔
2024
  }
2025
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
32,803✔
2026
  return code;
32,803✔
2027
}
2028

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

2069
      pCol->hasNull = true;
332,183✔
2070
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
332,183✔
2071
      pCol->info.colId = 0;
332,183✔
2072
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
332,183✔
2073
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
332,183✔
2074
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
332,183✔
2075
      code = colInfoDataEnsureCapacity(pCol, 1, true);
332,183✔
2076
      if (code != 0) {
332,183✔
2077
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2078
        taosMemoryFree(pCol);
×
2079
        break;
×
2080
      }
2081
      dst.columnData = pCol;
332,183✔
2082
      dst.numOfRows = 1;
332,183✔
2083
      dst.colAlloced = true;
332,183✔
2084
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
332,183✔
2085
      if (colDataIsNull_var(dst.columnData, 0)) {
331,697✔
2086
        qInfo("invalid sub tb expr with null value");
7,167✔
2087
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
6,740✔
2088
      }
2089
      if (code == 0) {
331,367✔
2090
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
325,054✔
2091
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
324,957✔
2092
      }
2093
    } break;
331,756✔
2094
    default:
×
2095
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2096
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2097
      break;
×
2098
  }
2099
  if (code == 0) {
331,756✔
2100
    if (!pVal || len == 0) {
324,690✔
2101
      qError("tbname generated with no characters which is not allowed");
×
2102
      code = TSDB_CODE_INVALID_PARA;
×
2103
    }
2104
    if(len > TSDB_TABLE_NAME_LEN - 1) {
325,443✔
2105
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
800✔
2106
      len = TSDB_TABLE_NAME_LEN - 1;
800✔
2107
    }
2108

2109
    memcpy(tbname, pVal, len);
325,443✔
2110
    tbname[len] = '\0';  // ensure null terminated
325,443✔
2111
    if (NULL != strchr(tbname, '.')) {
324,530✔
2112
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2113
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2114
    }
2115
  }
2116
  // TODO free dst
2117
  sclFreeParam(&dst);
331,596✔
2118
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
331,697✔
2119
  return code;
331,697✔
2120
}
2121

2122
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
310,673✔
2123
  if (pParam) {
310,673✔
2124
    if (pParam->tbname) {
310,673✔
2125
      taosMemFree(pParam->tbname);
310,673✔
2126
      pParam->tbname = NULL;
310,673✔
2127
    }
2128
    if (pParam->stbname) {
310,673✔
2129
      taosMemFree(pParam->stbname);
310,673✔
2130
      pParam->stbname = NULL;
310,673✔
2131
    }
2132
    if (pParam->dbFName) {
310,673✔
2133
      taosMemFree(pParam->dbFName);
310,673✔
2134
      pParam->dbFName = NULL;
310,673✔
2135
    }
2136
    if (pParam->pFields) {
310,673✔
2137
      taosArrayDestroy(pParam->pFields);
310,673✔
2138
      pParam->pFields = NULL;
310,673✔
2139
    }
2140
    if (pParam->pTagFields) {
310,673✔
2141
      taosArrayDestroy(pParam->pTagFields);
206,718✔
2142
      pParam->pTagFields = NULL;
206,718✔
2143
    }
2144
    taosMemFree(pParam);
310,673✔
2145
  }
2146
}
310,673✔
2147

2148
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
310,673✔
2149
  int32_t code = 0, lino = 0;
310,673✔
2150
  if (ppDst == NULL || pSrc == NULL) {
310,673✔
2151
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2152
  }
2153
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
310,673✔
2154
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
310,673✔
2155

2156
  (*ppDst)->suid = pSrc->suid;
310,673✔
2157
  (*ppDst)->sver = pSrc->sver;
310,673✔
2158
  (*ppDst)->tbType = pSrc->tbType;
310,673✔
2159
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
310,673✔
2160
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
309,901✔
2161

2162
  if (pSrc->stbname) {
309,901✔
2163
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
310,673✔
2164
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
310,673✔
2165
  }
2166

2167
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
310,673✔
2168
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
310,673✔
2169

2170
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
309,901✔
2171

2172
  if (pSrc->pFields && pSrc->pFields->size > 0) {
310,673✔
2173
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
310,280✔
2174
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
310,673✔
2175
  } else {
2176
    (*ppDst)->pFields = NULL;
393✔
2177
  }
2178
  
2179
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
310,673✔
2180
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
206,718✔
2181
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
206,325✔
2182
  } else {
2183
    (*ppDst)->pTagFields = NULL;
103,955✔
2184
  }
2185

2186
_exit:
310,673✔
2187

2188
  if (code != 0) {
310,673✔
2189
    if (*ppDst) {
×
2190
      destroyStreamInserterParam(*ppDst);
×
2191
      *ppDst = NULL;
×
2192
    }
2193
    
2194
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2195
  }
2196
  return code;
309,901✔
2197
}
2198

2199
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
427✔
2200
  return doDropStreamTable(pMsgCb, pOutput, pReq);
427✔
2201
}
2202

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

© 2026 Coveralls, Inc