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

taosdata / TDengine / #4849

13 Nov 2025 07:17AM UTC coverage: 63.829% (+0.6%) from 63.27%
#4849

push

travis-ci

guanshengliang
test: enable auto upload

148928 of 233322 relevant lines covered (63.83%)

115593255.19 hits per line

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

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

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

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

64
static void initRefPool() {
626,960✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
626,960✔
66
  (void)atexit(cleanupRefPool);
626,960✔
67
}
626,960✔
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) {
14,916,739✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,916,739✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,916,648✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,158,448✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,158,448✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,158,448✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,158,253✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
54,393✔
158
        if (code) {
54,393✔
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,755,581✔
166
  }
167

168
  return 0;
10,192,476✔
169
}
170

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

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,194,167✔
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,
134,838✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
134,838✔
209
    SExecTaskInfo* pTaskInfo = NULL;
21,846✔
210

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,344,952✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,231,738✔
249
    if (pSlotDesc->output) {
1,231,738✔
250
      ++(*numOfCols);
1,231,648✔
251
    }
252
  }
253

254
  return pTaskInfo;
113,214✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
490,875✔
258
  if (streamInserterParam == NULL) {
490,875✔
259
    return TSDB_CODE_SUCCESS;
207,661✔
260
  }
261

262
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
283,214✔
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) {
283,214✔
268
    stError("insertParam: invalid db/table name");
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

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

278
  return TSDB_CODE_SUCCESS;
283,214✔
279
}
280

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

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

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

316
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
283,214✔
317
    if (NULL == pInserterParam) {
283,214✔
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);
283,214✔
323
    TSDB_CHECK_CODE(code, lino, _error);
283,214✔
324
    
325
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
283,214✔
326
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
283,214✔
327

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
283,214✔
329
    if (code) {
283,214✔
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,
489,642✔
334
         tstrerror(code));
335

336
_error:
177,859✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,552,355✔
348
  if (pInfo == NULL) {
5,552,355✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,552,355✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,552,355✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,552,355✔
354
    return false;
4,728✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,547,627✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,445,603✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
10,993,230✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

381
  void*           info = pOperator->info;
5,547,627✔
382
  STableScanBase* pScanBaseInfo = NULL;
5,547,627✔
383

384
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,547,627✔
385
    pScanBaseInfo = &((STableScanInfo*)info)->base;
102,024✔
386
    setReadHandle(handle, pScanBaseInfo);
102,024✔
387
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,445,603✔
388
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,340,468✔
389
    setReadHandle(handle, pScanBaseInfo);
5,340,468✔
390
  }
391

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

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

403
  *pTaskInfo = NULL;
490,464✔
404

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

420
  return code;
489,642✔
421
}
422

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

435
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
65,654✔
436

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

442
  // let's discard the tables those are not created according to the queried super table.
443
  SMetaReader mr = {0};
65,654✔
444
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
65,654✔
445
  for (int32_t i = 0; i < numOfUids; ++i) {
131,794✔
446
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
66,140✔
447
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
66,140✔
448

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

455
    tDecoderClear(&mr.coder);
66,140✔
456

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

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

483
      if (!qualified) {
55,962✔
484
        continue;
27,994✔
485
      }
486
    }
487

488
    // handle multiple partition
489
    void* tmp = taosArrayPush(qa, id);
37,146✔
490
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
37,146✔
491
  }
492

493
_end:
65,654✔
494

495
  pAPI->metaReaderFn.clearReader(&mr);
65,654✔
496
  (*ppArrayRes) = qa;
65,654✔
497

498
_error:
65,654✔
499
  if (code != TSDB_CODE_SUCCESS) {
65,654✔
500
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
501
  }
502
  return code;
65,654✔
503
}
504

505
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
66,078✔
506
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
66,078✔
507
  const char*    id = GET_TASKID(pTaskInfo);
66,078✔
508
  int32_t        code = 0;
66,078✔
509

510
  if (isAdd) {
66,078✔
511
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
65,654✔
512
  }
513

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

521
  SStreamScanInfo* pScanInfo = pInfo->info;
66,078✔
522
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
66,078✔
523
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
132,432✔
524
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
66,354✔
525
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
66,354✔
526
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
66,354✔
527
    }
528
  }
529

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

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

553
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
65,654✔
554
    taosWLockLatch(&pTaskInfo->lock);
65,654✔
555

556
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
102,800✔
557
      uint64_t* uid = taosArrayGet(qa, i);
37,146✔
558
      if (!uid) {
37,146✔
559
        taosMemoryFree(keyBuf);
×
560
        taosArrayDestroy(qa);
×
561
        taosWUnLockLatch(&pTaskInfo->lock);
×
562
        return terrno;
×
563
      }
564
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
37,146✔
565

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

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

590
    taosWUnLockLatch(&pTaskInfo->lock);
65,654✔
591
    if (keyBuf != NULL) {
65,654✔
592
      taosMemoryFree(keyBuf);
×
593
    }
594

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

603
  return code;
66,078✔
604
}
605

606
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
287,199,236✔
607
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
608
                                    int32_t idx, bool* tbGet) {
609
  *tbGet = false;
287,199,236✔
610

611
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
287,209,077✔
612
    return TSDB_CODE_INVALID_PARA;
5,443✔
613
  }
614
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
287,205,159✔
615

616
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
287,205,159✔
617
    return TSDB_CODE_SUCCESS;
169,119,274✔
618
  }
619

620
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
118,099,575✔
621
  if (!pSchemaInfo) {
118,101,987✔
622
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
623
    return terrno;
×
624
  }
625

626
  *sversion = pSchemaInfo->sw->version;
118,101,987✔
627
  *tversion = pSchemaInfo->tversion;
118,102,955✔
628
  *rversion = pSchemaInfo->rversion;
118,109,287✔
629
  if (pSchemaInfo->dbname) {
118,087,976✔
630
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
118,090,398✔
631
  } else {
632
    dbName[0] = 0;
×
633
  }
634
  if (pSchemaInfo->tablename) {
118,102,981✔
635
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
118,102,191✔
636
  } else {
637
    tableName[0] = 0;
245✔
638
  }
639

640
  *tbGet = true;
118,106,515✔
641

642
  return TSDB_CODE_SUCCESS;
118,095,940✔
643
}
644

645
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
169,112,226✔
646

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

654
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
37,199,064✔
655
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
37,199,064✔
656
  ((SExecTaskInfo*)tinfo)->paramSet = false;
37,199,064✔
657
}
37,199,064✔
658

659
int32_t qExecutorInit(void) {
4,613,189✔
660
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,613,189✔
661
  return TSDB_CODE_SUCCESS;
4,613,502✔
662
}
663

664
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
171,003,534✔
665
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
666
                        EOPTR_EXEC_MODEL model) {
667
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
171,003,534✔
668
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
171,003,534✔
669

670
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
171,000,577✔
671

672
  readHandle->uid = 0;
171,021,028✔
673
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
171,042,916✔
674
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
170,953,663✔
675
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
178,245✔
676
    goto _error;
26,670✔
677
  }
678

679
  if (handle) {
170,800,686✔
680
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
170,770,505✔
681
    void*           pSinkManager = NULL;
170,807,896✔
682
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
170,659,695✔
683
    if (code != TSDB_CODE_SUCCESS) {
170,679,322✔
684
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
685
      goto _error;
×
686
    }
687

688
    void* pSinkParam = NULL;
170,679,322✔
689
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
170,783,387✔
690
    if (code != TSDB_CODE_SUCCESS) {
170,673,420✔
691
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
692
      taosMemoryFree(pSinkManager);
×
693
      goto _error;
×
694
    }
695

696
    SDataSinkNode* pSink = NULL;
170,673,420✔
697
    if (readHandle->localExec) {
170,770,384✔
698
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
87,372✔
699
      if (code != TSDB_CODE_SUCCESS) {
87,372✔
700
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
701
               (*pTask)->id.str);
702
        taosMemoryFree(pSinkManager);
×
703
        goto _error;
×
704
      }
705
    }
706

707
    // pSinkParam has been freed during create sinker.
708
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
170,654,107✔
709
                              (*pTask)->id.str, pSubplan->processOneBlock);
170,825,937✔
710
    if (code) {
170,712,867✔
711
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
620✔
712
    }
713
  }
714

715
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
170,920,902✔
716
         tstrerror(code));
717

718
_error:
8,808,508✔
719
  // if failed to add ref for all tables in this query, abort current query
720
  return code;
171,028,739✔
721
}
722

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

728
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
229,750,896✔
729
                     bool processOneBlock) {
730
  int32_t        code = TSDB_CODE_SUCCESS;
229,750,896✔
731
  int32_t        lino = 0;
229,750,896✔
732
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
229,750,896✔
733
  int64_t        threadId = taosGetSelfPthreadId();
229,750,896✔
734

735
  if (pLocal) {
229,744,545✔
736
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
223,961,888✔
737
  }
738

739
  taosArrayClear(pResList);
229,738,726✔
740

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

748
  if (pTaskInfo->cost.start == 0) {
229,739,863✔
749
    pTaskInfo->cost.start = taosGetTimestampUs();
166,909,329✔
750
  }
751

752
  if (isTaskKilled(pTaskInfo)) {
229,755,229✔
753
    atomic_store_64(&pTaskInfo->owner, 0);
236✔
754
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
236✔
755
    return pTaskInfo->code;
236✔
756
  }
757

758
  // error occurs, record the error code and return to client
759
  int32_t ret = setjmp(pTaskInfo->env);
229,732,512✔
760
  if (ret != TSDB_CODE_SUCCESS) {
229,863,735✔
761
    pTaskInfo->code = ret;
148,312✔
762
    (void)cleanUpUdfs();
148,312✔
763

764
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
148,312✔
765
    atomic_store_64(&pTaskInfo->owner, 0);
148,312✔
766

767
    return pTaskInfo->code;
148,312✔
768
  }
769

770
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
229,715,423✔
771

772
  int32_t      current = 0;
229,717,653✔
773
  SSDataBlock* pRes = NULL;
229,717,653✔
774
  int64_t      st = taosGetTimestampUs();
229,754,676✔
775

776
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
229,754,676✔
777
    pTaskInfo->paramSet = true;
37,199,064✔
778
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,199,064✔
779
  } else {
780
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
192,554,074✔
781
  }
782

783
  QUERY_CHECK_CODE(code, lino, _end);
229,599,997✔
784
  code = blockDataCheck(pRes);
229,599,997✔
785
  QUERY_CHECK_CODE(code, lino, _end);
229,601,045✔
786

787
  if (pRes == NULL) {
229,601,045✔
788
    st = taosGetTimestampUs();
42,288,651✔
789
  }
790

791
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
229,601,464✔
792
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
229,611,684✔
793
    rowsThreshold = 4096;
228,929,604✔
794
  }
795

796
  int32_t blockIndex = 0;
229,605,485✔
797
  while (pRes != NULL) {
612,554,973✔
798
    SSDataBlock* p = NULL;
435,188,588✔
799
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
435,189,230✔
800
      SSDataBlock* p1 = NULL;
301,365,027✔
801
      code = createOneDataBlock(pRes, true, &p1);
301,365,481✔
802
      QUERY_CHECK_CODE(code, lino, _end);
301,351,529✔
803

804
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
301,351,529✔
805
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
301,366,233✔
806
      p = p1;
301,366,233✔
807
    } else {
808
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
133,847,863✔
809
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
133,846,509✔
810

811
      p = *(SSDataBlock**)tmp;
133,846,509✔
812
      code = copyDataBlock(p, pRes);
133,841,434✔
813
      QUERY_CHECK_CODE(code, lino, _end);
133,849,121✔
814
    }
815

816
    blockIndex += 1;
435,210,117✔
817

818
    current += p->info.rows;
435,210,117✔
819
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
435,214,045✔
820
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
821
    void* tmp = taosArrayPush(pResList, &p);
435,202,059✔
822
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
435,202,059✔
823

824
    if (current >= rowsThreshold || processOneBlock) {
435,202,059✔
825
      break;
826
    }
827

828
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
382,960,276✔
829
    QUERY_CHECK_CODE(code, lino, _end);
382,909,457✔
830
    code = blockDataCheck(pRes);
382,909,457✔
831
    QUERY_CHECK_CODE(code, lino, _end);
382,967,083✔
832
  }
833

834
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
229,608,168✔
835
    pTaskInfo->pSubplan->rowsThreshold -= current;
675,188✔
836
  }
837

838
  *hasMore = (pRes != NULL);
229,605,385✔
839
  uint64_t el = (taosGetTimestampUs() - st);
229,598,263✔
840

841
  pTaskInfo->cost.elapsedTime += el;
229,598,263✔
842
  if (NULL == pRes) {
229,600,298✔
843
    *useconds = pTaskInfo->cost.elapsedTime;
177,357,973✔
844
  }
845

846
_end:
229,470,600✔
847
  (void)cleanUpUdfs();
229,594,862✔
848

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

853
  atomic_store_64(&pTaskInfo->owner, 0);
229,615,705✔
854
  if (code) {
229,615,705✔
855
    pTaskInfo->code = code;
×
856
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
857
  }
858

859
  return pTaskInfo->code;
229,615,705✔
860
}
861

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

873
  taosArrayClear(pTaskInfo->pResultBlockList);
×
874
}
×
875

876
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
37,813,942✔
877
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
37,813,942✔
878
  int64_t        threadId = taosGetSelfPthreadId();
37,813,942✔
879
  int64_t        curOwner = 0;
37,814,263✔
880

881
  *pRes = NULL;
37,814,263✔
882

883
  // todo extract method
884
  taosRLockLatch(&pTaskInfo->lock);
37,814,263✔
885
  bool isKilled = isTaskKilled(pTaskInfo);
37,814,370✔
886
  if (isKilled) {
37,814,370✔
887
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
888

889
    taosRUnLockLatch(&pTaskInfo->lock);
×
890
    return pTaskInfo->code;
×
891
  }
892

893
  if (pTaskInfo->owner != 0) {
37,814,370✔
894
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
895
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
896

897
    taosRUnLockLatch(&pTaskInfo->lock);
×
898
    return pTaskInfo->code;
×
899
  }
900

901
  pTaskInfo->owner = threadId;
37,814,370✔
902
  taosRUnLockLatch(&pTaskInfo->lock);
37,814,370✔
903

904
  if (pTaskInfo->cost.start == 0) {
37,814,477✔
905
    pTaskInfo->cost.start = taosGetTimestampUs();
90,229✔
906
  }
907

908
  // error occurs, record the error code and return to client
909
  int32_t ret = setjmp(pTaskInfo->env);
37,814,370✔
910
  if (ret != TSDB_CODE_SUCCESS) {
37,813,316✔
911
    pTaskInfo->code = ret;
×
912
    (void)cleanUpUdfs();
×
913
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
914
    atomic_store_64(&pTaskInfo->owner, 0);
×
915
    return pTaskInfo->code;
×
916
  }
917

918
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
37,813,316✔
919

920
  int64_t st = taosGetTimestampUs();
37,814,263✔
921
  int32_t code = TSDB_CODE_SUCCESS;
37,814,263✔
922
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
37,814,263✔
923
    pTaskInfo->paramSet = true;
×
924
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
925
  } else {
926
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
37,814,263✔
927
  }
928
  if (code) {
37,809,376✔
929
    pTaskInfo->code = code;
×
930
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
931
  }
932

933
  code = blockDataCheck(*pRes);
37,809,376✔
934
  if (code) {
37,813,744✔
935
    pTaskInfo->code = code;
×
936
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
937
  }
938

939
  uint64_t el = (taosGetTimestampUs() - st);
37,810,781✔
940

941
  pTaskInfo->cost.elapsedTime += el;
37,810,781✔
942
  if (NULL == *pRes) {
37,812,414✔
943
    *useconds = pTaskInfo->cost.elapsedTime;
3,857,522✔
944
  }
945

946
  (void)cleanUpUdfs();
37,812,883✔
947

948
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
37,814,477✔
949
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
37,814,477✔
950

951
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
37,814,408✔
952
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
953

954
  atomic_store_64(&pTaskInfo->owner, 0);
37,814,505✔
955
  return pTaskInfo->code;
37,814,477✔
956
}
957

958
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
57,797,155✔
959
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,797,155✔
960
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
57,797,271✔
961
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,797,271✔
962

963
  if (!tmp) {
57,797,155✔
964
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
965
    return terrno;
×
966
  }
967
  return TSDB_CODE_SUCCESS;
57,797,155✔
968
}
969

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

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

980
  return 0;
×
981
}
982

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

992
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
20,879✔
993
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
20,879✔
994

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

1017
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
20,879✔
1018
}
20,879✔
1019

1020
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
20,879✔
1021
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
20,879✔
1022
  if (pTaskInfo == NULL) {
20,879✔
1023
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1024
  }
1025

1026
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
20,879✔
1027

1028
  setTaskKilled(pTaskInfo, rspCode);
20,879✔
1029
  qStopTaskOperators(pTaskInfo);
20,879✔
1030

1031
  return TSDB_CODE_SUCCESS;
20,879✔
1032
}
1033

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

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

1047
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1048

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

1055
        taosMsleep(200);
×
1056

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

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

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

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

1087
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
171,526,621✔
1088
  STaskCostInfo* pSummary = &pTaskInfo->cost;
171,526,621✔
1089
  int64_t        idleTime = pSummary->start - pSummary->created;
171,529,715✔
1090

1091
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
171,529,478✔
1092
  if (pSummary->pRecoder != NULL) {
171,525,583✔
1093
    qDebug(
120,431,980✔
1094
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1095
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1096
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1097
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1098
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1099
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1100
  } else {
1101
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
51,091,560✔
1102
           pSummary->elapsedTime / 1000.0);
1103
  }
1104
}
171,523,853✔
1105

1106
void qDestroyTask(qTaskInfo_t qTaskHandle) {
175,321,085✔
1107
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
175,321,085✔
1108
  if (pTaskInfo == NULL) {
175,321,085✔
1109
    return;
3,795,779✔
1110
  }
1111

1112
  if (pTaskInfo->pRoot != NULL) {
171,525,306✔
1113
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
171,526,080✔
1114
  } else {
1115
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1116
  }
1117

1118
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
171,526,349✔
1119
  doDestroyTask(pTaskInfo);
171,528,153✔
1120
}
1121

1122
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,611,338✔
1123
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,611,338✔
1124
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,611,338✔
1125
}
1126

1127
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
113,144✔
1128
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
113,144✔
1129
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
113,144✔
1130

1131
  while (1) {
110,502✔
1132
    uint16_t type = pOperator->operatorType;
223,716✔
1133
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
223,716✔
1134
      *scanner = pOperator->info;
113,214✔
1135
      break;
113,214✔
1136
    } else {
1137
      pOperator = pOperator->pDownstream[0];
110,502✔
1138
    }
1139
  }
1140
}
113,214✔
1141

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

1155
void* qExtractReaderFromTmqScanner(void* scanner) {
113,214✔
1156
  SStreamScanInfo* pInfo = scanner;
113,214✔
1157
  return (void*)pInfo->tqReader;
113,214✔
1158
}
1159

1160
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
136,101✔
1161
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
136,101✔
1162
  return pTaskInfo->streamInfo.schema;
136,101✔
1163
}
1164

1165
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
136,101✔
1166
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
136,101✔
1167
  return pTaskInfo->streamInfo.tbName;
136,101✔
1168
}
1169

1170
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
151,727✔
1171
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
151,727✔
1172
  return &pTaskInfo->streamInfo.btMetaRsp;
151,727✔
1173
}
1174

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

1186
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
143,627✔
1187
  memset(pCond, 0, sizeof(SQueryTableDataCond));
143,627✔
1188
  pCond->order = TSDB_ORDER_ASC;
143,627✔
1189
  pCond->numOfCols = pMtInfo->schema->nCols;
143,264✔
1190
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
143,627✔
1191
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
143,559✔
1192
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
143,612✔
1193
    taosMemoryFreeClear(pCond->colList);
333✔
1194
    taosMemoryFreeClear(pCond->pSlotList);
×
1195
    return terrno;
×
1196
  }
1197

1198
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
143,347✔
1199
  pCond->suid = pMtInfo->suid;
143,355✔
1200
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
143,627✔
1201
  pCond->startVersion = -1;
143,733✔
1202
  pCond->endVersion = sContext->snapVersion;
143,597✔
1203

1204
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
924,802✔
1205
    SColumnInfo* pColInfo = &pCond->colList[i];
780,895✔
1206
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
780,857✔
1207
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
780,894✔
1208
    if (pMtInfo->pExtSchemas != NULL) {
781,198✔
1209
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,490✔
1210
    }
1211
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
780,850✔
1212
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
781,131✔
1213

1214
    pCond->pSlotList[i] = i;
780,918✔
1215
  }
1216

1217
  return TSDB_CODE_SUCCESS;
143,733✔
1218
}
1219

1220
void qStreamSetOpen(qTaskInfo_t tinfo) {
37,529,151✔
1221
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
37,529,151✔
1222
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
37,529,151✔
1223
  pOperator->status = OP_NOT_OPENED;
37,531,559✔
1224
}
37,530,213✔
1225

1226
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,105,357✔
1227
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,105,357✔
1228
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,105,357✔
1229
}
4,105,473✔
1230

1231
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,312,302✔
1232
  int32_t        code = TSDB_CODE_SUCCESS;
4,312,302✔
1233
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,312,302✔
1234
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,312,302✔
1235

1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,312,654✔
1237
  const char*    id = GET_TASKID(pTaskInfo);
4,311,851✔
1238

1239
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,312,411✔
1240
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,042,335✔
1241
    if (pOperator == NULL || code != 0) {
4,041,111✔
1242
      return code;
×
1243
    }
1244

1245
    SStreamScanInfo* pInfo = pOperator->info;
4,041,111✔
1246
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,040,465✔
1247
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,042,488✔
1248
    walReaderVerifyOffset(pWalReader, pOffset);
4,042,818✔
1249
  }
1250
  // if pOffset equal to current offset, means continue consume
1251
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,312,210✔
1252
    return 0;
3,868,996✔
1253
  }
1254

1255
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
442,340✔
1256
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
295,277✔
1257
    if (pOperator == NULL || code != 0) {
295,909✔
1258
      return code;
104✔
1259
    }
1260

1261
    SStreamScanInfo* pInfo = pOperator->info;
295,805✔
1262
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
295,481✔
1263
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
295,417✔
1264
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
294,733✔
1265

1266
    if (pOffset->type == TMQ_OFFSET__LOG) {
295,445✔
1267
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
237,440✔
1268
      pScanBaseInfo->dataReader = NULL;
236,680✔
1269

1270
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
236,784✔
1271
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
236,784✔
1272
      walReaderVerifyOffset(pWalReader, pOffset);
237,316✔
1273
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
237,440✔
1274
      if (code < 0) {
237,440✔
1275
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
4,629✔
1276
        return code;
4,629✔
1277
      }
1278
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
58,565✔
1279
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1280
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1281
      int64_t uid = pOffset->uid;
58,709✔
1282
      int64_t ts = pOffset->ts;
58,785✔
1283
      int32_t index = 0;
58,565✔
1284

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

1295
      if (uid == 0) {
58,353✔
1296
        if (numOfTables != 0) {
57,757✔
1297
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,027✔
1298
          if (!tmp) {
10,027✔
1299
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1300
            taosRUnLockLatch(&pTaskInfo->lock);
×
1301
            return terrno;
×
1302
          }
1303
          if (tmp) uid = tmp->uid;
10,027✔
1304
          ts = INT64_MIN;
10,027✔
1305
          pScanInfo->currentTable = 0;
10,027✔
1306
        } else {
1307
          taosRUnLockLatch(&pTaskInfo->lock);
47,730✔
1308
          qError("no table in table list, %s", id);
47,798✔
1309
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
47,942✔
1310
        }
1311
      }
1312
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
10,623✔
1313

1314
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
10,850✔
1315
             pInfo->pTableScanOp->resultInfo.totalRows);
1316
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
10,850✔
1317

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

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

1332
      STableKeyInfo keyInfo = {.uid = uid};
10,919✔
1333
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
10,919✔
1334

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

1343
      if (pScanBaseInfo->dataReader == NULL) {
10,919✔
1344
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
19,462✔
1345
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1346
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
9,731✔
1347
        if (code != TSDB_CODE_SUCCESS) {
9,731✔
1348
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1349
          return code;
×
1350
        }
1351

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

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

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

1377
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1378
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
147,063✔
1379
      SStreamRawScanInfo* pInfo = pOperator->info;
143,914✔
1380
      SSnapContext*       sContext = pInfo->sContext;
143,914✔
1381
      SOperatorInfo*      p = NULL;
143,914✔
1382

1383
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
143,914✔
1384
      if (code != 0) {
143,861✔
1385
        return code;
×
1386
      }
1387

1388
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
143,861✔
1389

1390
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
143,914✔
1391
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1392
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1393
      }
1394

1395
      SMetaTableInfo mtInfo = {0};
143,861✔
1396
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
143,861✔
1397
      if (code != 0) {
143,861✔
1398
        destroyMetaTableInfo(&mtInfo);
1399
        return code;
×
1400
      }
1401
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
143,861✔
1402
      pInfo->dataReader = NULL;
143,725✔
1403

1404
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
143,725✔
1405
      tableListClear(pTableListInfo);
143,755✔
1406

1407
      if (mtInfo.uid == 0) {
143,914✔
1408
        destroyMetaTableInfo(&mtInfo);
1409
        goto end;  // no data
181✔
1410
      }
1411

1412
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
143,733✔
1413
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
143,491✔
1414
      if (code != TSDB_CODE_SUCCESS) {
143,574✔
1415
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1416
        destroyMetaTableInfo(&mtInfo);
1417
        return code;
×
1418
      }
1419
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
143,574✔
1420
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1421
      } else {
1422
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
143,491✔
1423
      }
1424

1425
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
143,627✔
1426
      if (code != TSDB_CODE_SUCCESS) {
143,733✔
1427
        destroyMetaTableInfo(&mtInfo);
1428
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1429
        return code;
×
1430
      }
1431

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

1446
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
287,466✔
1447
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
143,733✔
1448
      if (code != TSDB_CODE_SUCCESS) {
143,468✔
1449
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1450
        destroyMetaTableInfo(&mtInfo);
1451
        return code;
×
1452
      }
1453

1454
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
143,468✔
1455
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
143,453✔
1456
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1457
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
143,415✔
1458
      pTaskInfo->streamInfo.schema = mtInfo.schema;
143,468✔
1459
      taosMemoryFreeClear(mtInfo.pExtSchemas);
143,733✔
1460

1461
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
143,733✔
1462
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,149✔
1463
      SStreamRawScanInfo* pInfo = pOperator->info;
1,026✔
1464
      SSnapContext*       sContext = pInfo->sContext;
1,026✔
1465
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,026✔
1466
      if (code != 0) {
1,026✔
1467
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1468
        return code;
×
1469
      }
1470
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
1,026✔
1471
             id);
1472
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
2,176✔
1473
      SStreamRawScanInfo* pInfo = pOperator->info;
2,176✔
1474
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,176✔
1475
      pInfo->dataReader = NULL;
2,176✔
1476
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
2,176✔
1477
    }
1478
  }
1479

1480
end:
390,458✔
1481
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
390,776✔
1482
  return 0;
390,846✔
1483
}
1484

1485
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
155,469,697✔
1486
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
155,469,697✔
1487
  if (pMsg->info.ahandle == NULL) {
155,474,585✔
1488
    qError("pMsg->info.ahandle is NULL");
×
1489
    return;
×
1490
  }
1491

1492
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
155,451,329✔
1493
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1494

1495
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
155,459,334✔
1496

1497
  if (pMsg->contLen > 0) {
155,476,717✔
1498
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
155,457,416✔
1499
    if (buf.pData == NULL) {
155,454,392✔
1500
      pMsg->code = terrno;
×
1501
    } else {
1502
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
155,454,392✔
1503
    }
1504
  }
1505

1506
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
155,485,318✔
1507
  rpcFreeCont(pMsg->pCont);
155,482,218✔
1508
  destroySendMsgInfo(pSendInfo);
155,466,565✔
1509
}
1510

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

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

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

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

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

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

1539
  taosArrayDestroy(plist);
×
1540

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

1549
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,384,440✔
1550
  int32_t        code = TSDB_CODE_SUCCESS;
3,384,440✔
1551
  int32_t        lino = 0;
3,384,440✔
1552
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,384,440✔
1553

1554
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,386,238✔
1555
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1556
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1557

1558
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1559
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1560
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,385,636✔
1561
    STableScanInfo* pScanInfo = pOperator->info;
1,693,420✔
1562

1563
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,694,022✔
1564
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,692,826✔
1565
  } else {
1566
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,692,216✔
1567
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,691,020✔
1568
    }
1569
  }
1570

1571
_end:
×
1572
  if (code != TSDB_CODE_SUCCESS) {
3,382,650✔
1573
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1574
  }
1575
  return code;
3,383,244✔
1576
}
1577

1578
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,692,141✔
1579
  if (pList == NULL) {
1,692,141✔
1580
    return TSDB_CODE_INVALID_PARA;
×
1581
  }
1582

1583
  *pList = NULL;
1,692,141✔
1584
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,693,420✔
1585
  if (pArray == NULL) {
1,692,826✔
1586
    return terrno;
×
1587
  }
1588

1589
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,692,826✔
1590
  if (code == 0) {
1,691,028✔
1591
    *pList = pArray;
1,691,028✔
1592
  } else {
1593
    taosArrayDestroy(pArray);
×
1594
  }
1595
  return code;
1,691,028✔
1596
}
1597

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

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

1614
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1615
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1616

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

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

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

1633
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
44,863,612✔
1634
  int32_t code = 0;
44,863,612✔
1635

1636
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
44,863,612✔
1637
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
44,860,464✔
1638

1639
  if (pOper->fpSet.resetStateFn) {
44,862,028✔
1640
    code = pOper->fpSet.resetStateFn(pOper);
44,862,810✔
1641
  }
1642
  pOper->status = OP_NOT_OPENED;
44,858,090✔
1643
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
76,672,823✔
1644
    code = clearStatesForOperator(pOper->pDownstream[i]);
31,800,624✔
1645
  }
1646
  return code;
44,871,057✔
1647
}
1648

1649
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
13,059,863✔
1650
  int32_t        code = 0;
13,059,863✔
1651
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
13,059,863✔
1652
  SOperatorInfo* pOper = pTaskInfo->pRoot;
13,059,863✔
1653
  pTaskInfo->code = TSDB_CODE_SUCCESS;
13,059,863✔
1654
  code = clearStatesForOperator(pOper);
13,059,863✔
1655
  return code;
13,059,441✔
1656
}
1657

1658
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
17,433,906✔
1659
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
17,433,906✔
1660
  int64_t        threadId = taosGetSelfPthreadId();
17,433,906✔
1661
  int64_t        curOwner = 0;
17,434,328✔
1662

1663
  *ppRes = NULL;
17,434,328✔
1664

1665
  // todo extract method
1666
  taosRLockLatch(&pTaskInfo->lock);
17,434,328✔
1667
  bool isKilled = isTaskKilled(pTaskInfo);
17,434,719✔
1668
  if (isKilled) {
17,434,328✔
1669
    // clearStreamBlock(pTaskInfo->pRoot);
1670
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1671

1672
    taosRUnLockLatch(&pTaskInfo->lock);
×
1673
    return pTaskInfo->code;
×
1674
  }
1675

1676
  if (pTaskInfo->owner != 0) {
17,434,328✔
1677
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1678
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1679

1680
    taosRUnLockLatch(&pTaskInfo->lock);
×
1681
    return pTaskInfo->code;
×
1682
  }
1683

1684
  pTaskInfo->owner = threadId;
17,434,328✔
1685
  taosRUnLockLatch(&pTaskInfo->lock);
17,434,297✔
1686

1687
  if (pTaskInfo->cost.start == 0) {
17,434,719✔
1688
    pTaskInfo->cost.start = taosGetTimestampUs();
291,844✔
1689
  }
1690

1691
  // error occurs, record the error code and return to client
1692
  int32_t ret = setjmp(pTaskInfo->env);
17,434,719✔
1693
  if (ret != TSDB_CODE_SUCCESS) {
21,361,277✔
1694
    pTaskInfo->code = ret;
3,927,392✔
1695
    (void)cleanUpUdfs();
3,927,392✔
1696
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,927,392✔
1697
    atomic_store_64(&pTaskInfo->owner, 0);
3,927,392✔
1698
    return pTaskInfo->code;
3,927,392✔
1699
  }
1700

1701
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,433,885✔
1702

1703
  int64_t st = taosGetTimestampUs();
17,434,719✔
1704

1705
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,434,719✔
1706
  if (code) {
13,507,327✔
1707
    pTaskInfo->code = code;
×
1708
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1709
  } else {
1710
    *finished = *ppRes == NULL;
13,507,327✔
1711
    code = blockDataCheck(*ppRes);
13,507,327✔
1712
  }
1713
  if (code) {
13,507,327✔
1714
    pTaskInfo->code = code;
×
1715
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1716
  }
1717

1718
  uint64_t el = (taosGetTimestampUs() - st);
13,507,327✔
1719

1720
  pTaskInfo->cost.elapsedTime += el;
13,507,327✔
1721
  if (NULL == *ppRes) {
13,507,327✔
1722
    *useconds = pTaskInfo->cost.elapsedTime;
8,928,134✔
1723
  }
1724

1725
  (void)cleanUpUdfs();
13,507,327✔
1726

1727
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
13,507,327✔
1728
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
13,507,327✔
1729

1730
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
13,507,327✔
1731
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1732

1733
  atomic_store_64(&pTaskInfo->owner, 0);
13,507,327✔
1734
  return pTaskInfo->code;
13,507,327✔
1735
}
1736

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

1742
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
10,875,776✔
1743
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1744
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1745
  int32_t code = 0;                                        
10,875,776✔
1746
  if (*pTableListInfo != NULL) {
10,875,776✔
1747
    qDebug("table list already exists, no need to create again");
×
1748
    goto end;
×
1749
  }
1750
  STableListInfo* pList = tableListCreate();
10,879,698✔
1751
  if (pList == NULL) {
10,880,457✔
1752
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1753
    code = terrno;
×
1754
    goto end;
×
1755
  }
1756

1757
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
10,880,457✔
1758
  SReadHandle    pHandle = {.vnode = pVnode};
10,881,231✔
1759
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
10,878,165✔
1760

1761
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
10,881,614✔
1762
  if (code != 0) {
10,881,623✔
1763
    tableListDestroy(pList);
×
1764
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1765
    goto end;
×
1766
  }
1767
  *pTableListInfo = pList;
10,881,623✔
1768

1769
end:
10,882,388✔
1770
  return 0;
10,882,388✔
1771
}
1772

1773
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
13,331✔
1774
  bool   listAdded = false;
13,331✔
1775
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
13,331✔
1776
  if (code == 0 && !listAdded) {
13,331✔
1777
    int32_t numOfTables = taosArrayGetSize(pUidList);
5,047✔
1778
    for (int i = 0; i < numOfTables; i++) {
10,094✔
1779
      void* tmp = taosArrayGet(pUidList, i);
5,047✔
1780
      if (tmp == NULL) {
5,047✔
1781
        return terrno;
×
1782
      }
1783
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
5,047✔
1784

1785
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
5,047✔
1786
      if (p == NULL) {
5,047✔
1787
        return terrno;
×
1788
      }
1789
    }
1790
  }
1791
  return code;
13,331✔
1792
}
1793

1794
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
13,331✔
1795
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1796
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1797
  int32_t code = TSDB_CODE_SUCCESS;
13,331✔
1798
  STableListInfo* pList = tableListCreate();
13,331✔
1799
  if (pList == NULL) {
13,331✔
1800
    code = terrno;
×
1801
    goto end;
×
1802
  }
1803
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
13,331✔
1804
  if (uidListCopy == NULL) {
13,331✔
1805
    code = terrno;
×
1806
    goto end;
×
1807
  }
1808
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
13,331✔
1809
  SReadHandle    pHandle = {.vnode = pVnode};
13,331✔
1810

1811
  pList->idInfo.suid = suid;
13,331✔
1812
  pList->idInfo.tableType = TD_SUPER_TABLE;
13,331✔
1813
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
13,331✔
1814
  if (code != TSDB_CODE_SUCCESS) {
13,331✔
1815
    goto end;
×
1816
  }                                              
1817
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
13,331✔
1818
  if (code != TSDB_CODE_SUCCESS) {
13,331✔
1819
    goto end;
×
1820
  }
1821
  *tableList = pList->pTableList;
13,331✔
1822
  pList->pTableList = NULL;
13,331✔
1823

1824
  taosArrayClear(uidList);
13,331✔
1825
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
26,662✔
1826
    void* tmp = taosArrayGet(uidListCopy, i);
13,331✔
1827
    if (tmp == NULL) {
13,331✔
1828
      continue;
×
1829
    }
1830
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
13,331✔
1831
    if (slot == NULL) {
13,331✔
1832
      if (taosArrayPush(uidList, tmp) == NULL) {
6,197✔
1833
        code = terrno;
×
1834
        goto end;
×
1835
      }
1836
    }
1837
  }
1838
end:
13,331✔
1839
  taosArrayDestroy(uidListCopy);
13,331✔
1840
  tableListDestroy(pList);
13,331✔
1841
  return code;
13,331✔
1842
}
1843

1844
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1845
//   int32_t index = -1;
1846
//   (void)taosThreadRwlockRdlock(lock);
1847
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1848
//     index = 0;
1849
//     goto end;
1850
//   }
1851
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1852
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1853

1854
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1855
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1856
//       index = i;
1857
//       goto end;
1858
//     }
1859
//   }
1860
// end:
1861
//   (void)taosThreadRwlockUnlock(lock);
1862
//   return index;
1863
// }
1864

1865
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
10,880,467✔
1866
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
10,882,388✔
1867
  STableListInfo* pList = pTableListInfo;
10,882,388✔
1868
  return pList->pTableList;
10,882,388✔
1869
}
1870
// int32_t qStreamGetTableListGroupNum(const void* pTableList, TdThreadRwlock* lock) { 
1871
//   (void)taosThreadRwlockRdlock(lock);
1872
//   int32_t code = ((STableListInfo*)pTableList)->numOfOuputGroups; 
1873
//   (void)taosThreadRwlockUnlock(lock);
1874
//   return code; 
1875
// }
1876

1877
// void    qStreamSetTableListGroupNum(void* pTableList, int32_t groupNum, TdThreadRwlock* lock) {
1878
//   (void)taosThreadRwlockWrlock(lock);
1879
//   ((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; 
1880
//   (void)taosThreadRwlockUnlock(lock);
1881
// }
1882

1883
// SArray* qStreamGetTableArrayList(const void* pTableList, TdThreadRwlock* lock) { 
1884
//   SArray* pList = NULL;
1885
//   (void)taosThreadRwlockRdlock(lock); 
1886
//   pList = ((STableListInfo*)pTableList)->pTableList;
1887
//   (void)taosThreadRwlockUnlock(lock);
1888
//   return pList;
1889
// }
1890

1891
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,379,901✔
1892

1893
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,301,525✔
1894
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,301,525✔
1895
  qDestroyTask(tInfo);
2,301,525✔
1896
}
2,301,525✔
1897

1898
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
731,580✔
1899
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
731,580✔
1900
}
1901

1902
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
731,966✔
1903
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1904
  int32_t      code = 0;
731,966✔
1905
  SNode*       pNode = 0;
731,966✔
1906
  SNodeList*   pList = NULL;
731,966✔
1907
  SExprInfo*   pExprInfo = NULL;
731,966✔
1908
  int32_t      numOfExprs = 1;
731,966✔
1909
  int32_t*     offset = 0;
731,966✔
1910
  STargetNode* pTargetNode = NULL;
731,966✔
1911
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
731,966✔
1912
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
731,568✔
1913

1914
  if (code == 0) {
731,966✔
1915
    pTargetNode->dataBlockId = 0;
731,966✔
1916
    pTargetNode->pExpr = pNode;
731,966✔
1917
    pTargetNode->slotId = 0;
731,966✔
1918
  }
1919
  if (code == 0) {
731,966✔
1920
    code = nodesMakeList(&pList);
731,966✔
1921
  }
1922
  if (code == 0) {
731,966✔
1923
    code = nodesListAppend(pList, (SNode*)pTargetNode);
731,966✔
1924
  }
1925
  if (code == 0) {
731,966✔
1926
    pNode = NULL;
731,966✔
1927
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
731,966✔
1928
  }
1929

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

1964
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,369,165✔
1965
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,369,165✔
1966
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,369,165✔
1967
  int32_t        code = 0;
4,369,165✔
1968
  SNode*         pNode = NULL;
4,369,165✔
1969
  if (!pForceOutputCols) return 0;
4,369,165✔
1970
  if (!*pRes) {
386✔
1971
    code = createDataBlock(pRes);
386✔
1972
  }
1973

1974
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
386✔
1975
    int32_t idx = 0;
386✔
1976
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
1,158✔
1977
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
772✔
1978
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
772✔
1979
      colInfo.info.precision = pCol->type.precision;
772✔
1980
      colInfo.info.scale = pCol->type.scale;
772✔
1981
      code = blockDataAppendColInfo(*pRes, &colInfo);
772✔
1982
      if (code != 0) break;
772✔
1983
    }
1984
  }
1985

1986
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
386✔
1987
  if (code != TSDB_CODE_SUCCESS) {
386✔
1988
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1989
    return code;
×
1990
  }
1991

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

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

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

2104
    memcpy(tbname, pVal, len);
263,818✔
2105
    tbname[len] = '\0';  // ensure null terminated
263,818✔
2106
    if (NULL != strchr(tbname, '.')) {
263,424✔
2107
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2108
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2109
    }
2110
  }
2111
  // TODO free dst
2112
  sclFreeParam(&dst);
263,424✔
2113
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
263,822✔
2114
  return code;
263,822✔
2115
}
2116

2117
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
283,214✔
2118
  if (pParam) {
283,214✔
2119
    if (pParam->tbname) {
283,214✔
2120
      taosMemFree(pParam->tbname);
283,214✔
2121
      pParam->tbname = NULL;
283,214✔
2122
    }
2123
    if (pParam->stbname) {
283,214✔
2124
      taosMemFree(pParam->stbname);
283,214✔
2125
      pParam->stbname = NULL;
283,214✔
2126
    }
2127
    if (pParam->dbFName) {
283,214✔
2128
      taosMemFree(pParam->dbFName);
283,214✔
2129
      pParam->dbFName = NULL;
283,214✔
2130
    }
2131
    if (pParam->pFields) {
283,214✔
2132
      taosArrayDestroy(pParam->pFields);
283,214✔
2133
      pParam->pFields = NULL;
283,214✔
2134
    }
2135
    if (pParam->pTagFields) {
283,214✔
2136
      taosArrayDestroy(pParam->pTagFields);
194,192✔
2137
      pParam->pTagFields = NULL;
194,192✔
2138
    }
2139
    taosMemFree(pParam);
283,214✔
2140
  }
2141
}
283,214✔
2142

2143
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
283,214✔
2144
  int32_t code = 0, lino = 0;
283,214✔
2145
  if (ppDst == NULL || pSrc == NULL) {
283,214✔
2146
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2147
  }
2148
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
283,214✔
2149
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
282,837✔
2150

2151
  (*ppDst)->suid = pSrc->suid;
282,837✔
2152
  (*ppDst)->sver = pSrc->sver;
283,214✔
2153
  (*ppDst)->tbType = pSrc->tbType;
282,837✔
2154
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
283,214✔
2155
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
283,214✔
2156

2157
  if (pSrc->stbname) {
283,214✔
2158
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
283,214✔
2159
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
283,037✔
2160
  }
2161

2162
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
282,660✔
2163
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
283,037✔
2164

2165
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
283,037✔
2166

2167
  if (pSrc->pFields && pSrc->pFields->size > 0) {
283,037✔
2168
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
283,214✔
2169
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
282,837✔
2170
  } else {
2171
    (*ppDst)->pFields = NULL;
×
2172
  }
2173
  
2174
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
282,660✔
2175
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
194,192✔
2176
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
194,192✔
2177
  } else {
2178
    (*ppDst)->pTagFields = NULL;
88,098✔
2179
  }
2180

2181
_exit:
283,037✔
2182

2183
  if (code != 0) {
283,037✔
2184
    if (*ppDst) {
×
2185
      destroyStreamInserterParam(*ppDst);
×
2186
      *ppDst = NULL;
×
2187
    }
2188
    
2189
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2190
  }
2191
  return code;
283,214✔
2192
}
2193

2194
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
427✔
2195
  return doDropStreamTable(pMsgCb, pOutput, pReq);
427✔
2196
}
2197

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