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

taosdata / TDengine / #4891

20 Dec 2025 03:05AM UTC coverage: 65.549% (+2.7%) from 62.824%
#4891

push

travis-ci

web-flow
merge: from main to 3.0 branch #33992

182743 of 278788 relevant lines covered (65.55%)

104071702.81 hits per line

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

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

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

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

64
static void initRefPool() {
652,828✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
652,828✔
66
  (void)atexit(cleanupRefPool);
652,828✔
67
}
652,828✔
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,973,559✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,973,559✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,973,559✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
3,919,938✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
3,919,938✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
3,919,938✔
156
      if (pScanInfo->base.dataReader != NULL) {
3,919,938✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
55,350✔
158
        if (code) {
55,260✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,053,605✔
166
  }
167

168
  return 0;
10,282,849✔
169
}
170

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

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

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

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

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

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

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

203
  return code;
×
204
}
205

206
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
133,671✔
207
  if (msg == NULL) {  // create raw scan
133,671✔
208
    SExecTaskInfo* pTaskInfo = NULL;
22,443✔
209

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

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

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

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

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

242
  return pTaskInfo;
111,998✔
243
}
244

245
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
776,408✔
246
  if (streamInserterParam == NULL) {
776,408✔
247
    return TSDB_CODE_SUCCESS;
447,159✔
248
  }
249

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

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

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

266
  return TSDB_CODE_SUCCESS;
329,249✔
267
}
268

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

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

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

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

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

324
_error:
141,833✔
325

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

335
bool qNeedReset(qTaskInfo_t pInfo) {
5,595,336✔
336
  if (pInfo == NULL) {
5,595,336✔
337
    return false;
×
338
  }
339
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,595,336✔
340
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,595,336✔
341
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,595,336✔
342
    return false;
8,376✔
343
  }
344
  int32_t node = nodeType(pOperator->pPhyNode);
5,586,960✔
345
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,305,751✔
346
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
10,892,711✔
347
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
348
}
349

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

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

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

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

373
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,586,960✔
374
    pScanBaseInfo = &((STableScanInfo*)info)->base;
281,209✔
375
    setReadHandle(handle, pScanBaseInfo);
281,209✔
376
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,305,751✔
377
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,811,092✔
378
    setReadHandle(handle, pScanBaseInfo);
4,811,092✔
379
  }
380

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

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

392
  *pTaskInfo = NULL;
776,542✔
393

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

409
  return code;
776,136✔
410
}
411

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

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

425
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
54,774✔
426

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

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

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
54,774✔
437

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

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

447
  locked = 1;
54,774✔
448
  for (int32_t i = 0; i < numOfUids; ++i) {
110,367✔
449
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
55,593✔
450
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
55,593✔
451

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

458
    tDecoderClear(&mr.coder);
55,593✔
459

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

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

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

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

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

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

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

515
  // handle multiple partition
516

517
_end:
54,774✔
518

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

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

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

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

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

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

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

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

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

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

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

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

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

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

630
  return code;
55,397✔
631
}
632

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

638
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
266,557,719✔
639
    return TSDB_CODE_INVALID_PARA;
96✔
640
  }
641
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
266,558,741✔
642

643
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
266,558,741✔
644
    return TSDB_CODE_SUCCESS;
152,592,225✔
645
  }
646

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

653
  *sversion = pSchemaInfo->sw->version;
113,968,252✔
654
  *tversion = pSchemaInfo->tversion;
113,992,054✔
655
  *rversion = pSchemaInfo->rversion;
113,984,955✔
656
  if (pSchemaInfo->dbname) {
113,975,821✔
657
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
113,971,795✔
658
  } else {
659
    dbName[0] = 0;
×
660
  }
661
  if (pSchemaInfo->tablename) {
113,989,283✔
662
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
113,974,709✔
663
  } else {
664
    tableName[0] = 0;
1,929✔
665
  }
666

667
  *tbGet = true;
113,993,850✔
668

669
  return TSDB_CODE_SUCCESS;
113,991,094✔
670
}
671

672
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
152,576,178✔
673

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

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

686
int32_t qExecutorInit(void) {
4,689,147✔
687
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,689,147✔
688
  return TSDB_CODE_SUCCESS;
4,689,634✔
689
}
690

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

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

699
  readHandle->uid = 0;
154,445,608✔
700
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
154,457,380✔
701
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
154,355,708✔
702
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
195,508✔
703
    goto _error;
25,754✔
704
  }
705

706
  if (handle) {
154,198,352✔
707
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
154,098,690✔
708
    void*           pSinkManager = NULL;
154,083,167✔
709
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
154,130,733✔
710
    if (code != TSDB_CODE_SUCCESS) {
154,103,896✔
711
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
712
      goto _error;
×
713
    }
714

715
    void* pSinkParam = NULL;
154,103,896✔
716
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
154,132,379✔
717
    if (code != TSDB_CODE_SUCCESS) {
154,092,677✔
718
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
719
      taosMemoryFree(pSinkManager);
×
720
      goto _error;
×
721
    }
722

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

734
    // pSinkParam has been freed during create sinker.
735
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
154,065,508✔
736
                              (*pTask)->id.str, pSubplan->processOneBlock);
154,239,771✔
737
    if (code) {
154,113,625✔
738
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
640✔
739
    }
740
  }
741

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

745
_error:
8,844,779✔
746
  // if failed to add ref for all tables in this query, abort current query
747
  return code;
154,424,669✔
748
}
749

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

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

762
  if (pLocal) {
172,261,529✔
763
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
166,194,458✔
764
  }
765

766
  taosArrayClear(pResList);
172,245,985✔
767

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

775
  if (pTaskInfo->cost.start == 0) {
172,240,936✔
776
    pTaskInfo->cost.start = taosGetTimestampUs();
152,975,325✔
777
  }
778

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

785
  // error occurs, record the error code and return to client
786
  int32_t ret = setjmp(pTaskInfo->env);
172,252,100✔
787
  if (ret != TSDB_CODE_SUCCESS) {
172,481,375✔
788
    pTaskInfo->code = ret;
250,690✔
789
    (void)cleanUpUdfs();
250,690✔
790

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

794
    return pTaskInfo->code;
250,690✔
795
  }
796

797
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
172,230,685✔
798

799
  int32_t      current = 0;
172,231,740✔
800
  SSDataBlock* pRes = NULL;
172,231,740✔
801
  int64_t      st = taosGetTimestampUs();
172,260,779✔
802

803
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
172,260,779✔
804
    pTaskInfo->paramSet = true;
718,864✔
805
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
718,864✔
806
  } else {
807
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
171,537,929✔
808
  }
809

810
  QUERY_CHECK_CODE(code, lino, _end);
172,015,247✔
811
  code = blockDataCheck(pRes);
172,015,247✔
812
  QUERY_CHECK_CODE(code, lino, _end);
172,011,881✔
813

814
  if (pRes == NULL) {
172,011,881✔
815
    st = taosGetTimestampUs();
29,357,776✔
816
  }
817

818
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
172,007,403✔
819
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
172,015,756✔
820
    rowsThreshold = 4096;
171,371,423✔
821
  }
822

823
  int32_t blockIndex = 0;
172,006,724✔
824
  while (pRes != NULL) {
550,168,768✔
825
    SSDataBlock* p = NULL;
391,939,405✔
826
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
391,928,845✔
827
      SSDataBlock* p1 = NULL;
289,176,482✔
828
      code = createOneDataBlock(pRes, true, &p1);
289,178,467✔
829
      QUERY_CHECK_CODE(code, lino, _end);
289,180,832✔
830

831
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
289,180,832✔
832
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
289,181,455✔
833
      p = p1;
289,181,455✔
834
    } else {
835
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
102,771,550✔
836
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
102,771,511✔
837

838
      p = *(SSDataBlock**)tmp;
102,771,511✔
839
      code = copyDataBlock(p, pRes);
102,771,304✔
840
      QUERY_CHECK_CODE(code, lino, _end);
102,769,772✔
841
    }
842

843
    blockIndex += 1;
391,947,535✔
844

845
    current += p->info.rows;
391,947,535✔
846
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
391,942,714✔
847
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
848
    void* tmp = taosArrayPush(pResList, &p);
391,954,006✔
849
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
391,954,006✔
850

851
    if (current >= rowsThreshold || processOneBlock) {
391,954,006✔
852
      break;
853
    }
854

855
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
378,168,563✔
856
    QUERY_CHECK_CODE(code, lino, _end);
378,126,887✔
857
    code = blockDataCheck(pRes);
378,126,887✔
858
    QUERY_CHECK_CODE(code, lino, _end);
378,171,021✔
859
  }
860

861
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
172,014,806✔
862
    pTaskInfo->pSubplan->rowsThreshold -= current;
633,004✔
863
  }
864

865
  *hasMore = (pRes != NULL);
172,014,875✔
866
  uint64_t el = (taosGetTimestampUs() - st);
172,009,759✔
867

868
  pTaskInfo->cost.elapsedTime += el;
172,009,759✔
869
  if (NULL == pRes) {
171,993,269✔
870
    *useconds = pTaskInfo->cost.elapsedTime;
158,208,436✔
871
  }
872

873
_end:
171,993,997✔
874
  (void)cleanUpUdfs();
172,002,544✔
875

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

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

886
  return pTaskInfo->code;
172,029,453✔
887
}
888

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

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

903
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
47,309,145✔
904
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,309,145✔
905
  int64_t        threadId = taosGetSelfPthreadId();
47,309,145✔
906
  int64_t        curOwner = 0;
47,309,131✔
907

908
  *pRes = NULL;
47,309,131✔
909

910
  // todo extract method
911
  taosRLockLatch(&pTaskInfo->lock);
47,309,022✔
912
  bool isKilled = isTaskKilled(pTaskInfo);
47,308,804✔
913
  if (isKilled) {
47,308,843✔
914
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
915

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

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

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

928
  pTaskInfo->owner = threadId;
47,308,478✔
929
  taosRUnLockLatch(&pTaskInfo->lock);
47,308,553✔
930

931
  if (pTaskInfo->cost.start == 0) {
47,308,843✔
932
    pTaskInfo->cost.start = taosGetTimestampUs();
91,145✔
933
  }
934

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

945
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
47,308,254✔
946

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

960
  code = blockDataCheck(*pRes);
47,288,539✔
961
  if (code) {
47,305,806✔
962
    pTaskInfo->code = code;
×
963
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
964
  }
965

966
  uint64_t el = (taosGetTimestampUs() - st);
47,300,019✔
967

968
  pTaskInfo->cost.elapsedTime += el;
47,300,019✔
969
  if (NULL == *pRes) {
47,302,866✔
970
    *useconds = pTaskInfo->cost.elapsedTime;
3,599,319✔
971
  }
972

973
  (void)cleanUpUdfs();
47,303,772✔
974

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

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

981
  atomic_store_64(&pTaskInfo->owner, 0);
47,309,299✔
982
  return pTaskInfo->code;
47,309,145✔
983
}
984

985
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
49,939,469✔
986
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
49,939,469✔
987
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
49,940,042✔
988
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
49,940,037✔
989

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

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

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

1007
  return 0;
×
1008
}
1009

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

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

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

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

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

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

1055
  setTaskKilled(pTaskInfo, rspCode);
16,347✔
1056
  qStopTaskOperators(pTaskInfo);
16,347✔
1057

1058
  return TSDB_CODE_SUCCESS;
16,347✔
1059
}
1060

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

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

1074
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1075

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

1082
        taosMsleep(200);
×
1083

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

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

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

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

1114
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
155,230,842✔
1115
  STaskCostInfo* pSummary = &pTaskInfo->cost;
155,230,842✔
1116
  int64_t        idleTime = pSummary->start - pSummary->created;
155,232,808✔
1117

1118
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
155,230,744✔
1119
  if (pSummary->pRecoder != NULL) {
155,232,158✔
1120
    qDebug(
113,145,947✔
1121
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1122
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1123
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1124
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1125
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1126
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1127
  } else {
1128
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
42,080,841✔
1129
           pSummary->elapsedTime / 1000.0);
1130
  }
1131
}
155,226,788✔
1132

1133
void qDestroyTask(qTaskInfo_t qTaskHandle) {
162,747,722✔
1134
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
162,747,722✔
1135
  if (pTaskInfo == NULL) {
162,747,722✔
1136
    return;
7,517,899✔
1137
  }
1138

1139
  if (pTaskInfo->pRoot != NULL) {
155,229,823✔
1140
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
155,233,089✔
1141
  } else {
1142
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1143
  }
1144

1145
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
155,233,801✔
1146
  doDestroyTask(pTaskInfo);
155,230,956✔
1147
}
1148

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

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

1158
  while (1) {
110,997✔
1159
    uint16_t type = pOperator->operatorType;
222,995✔
1160
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
222,995✔
1161
      *scanner = pOperator->info;
111,998✔
1162
      break;
111,998✔
1163
    } else {
1164
      pOperator = pOperator->pDownstream[0];
110,997✔
1165
    }
1166
  }
1167
}
111,998✔
1168

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

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

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

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

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

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

1212
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
137,358✔
1213
  pCond->suid = pMtInfo->suid;
137,543✔
1214
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
137,358✔
1215
  pCond->startVersion = -1;
137,477✔
1216
  pCond->endVersion = sContext->snapVersion;
137,358✔
1217

1218
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
876,070✔
1219
    SColumnInfo* pColInfo = &pCond->colList[i];
738,606✔
1220
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
738,778✔
1221
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
738,791✔
1222
    if (pMtInfo->pExtSchemas != NULL) {
738,606✔
1223
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
6,932✔
1224
    }
1225
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
738,606✔
1226
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
738,553✔
1227

1228
    pCond->pSlotList[i] = i;
738,606✔
1229
  }
1230

1231
  return TSDB_CODE_SUCCESS;
137,596✔
1232
}
1233

1234
void qStreamSetOpen(qTaskInfo_t tinfo) {
47,022,647✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,022,647✔
1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
47,022,647✔
1237
  pOperator->status = OP_NOT_OPENED;
47,027,556✔
1238
}
47,027,290✔
1239

1240
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
3,877,295✔
1241
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,877,295✔
1242
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
3,877,295✔
1243
}
3,877,936✔
1244

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

1250
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,069,896✔
1251
  const char*    id = GET_TASKID(pTaskInfo);
4,069,412✔
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,069,196✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,820,333✔
1255
    if (pOperator == NULL || code != 0) {
3,820,405✔
1256
      return code;
18✔
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
3,820,387✔
1260
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,820,473✔
1261
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,820,172✔
1262
    walReaderVerifyOffset(pWalReader, pOffset);
3,820,400✔
1263
  }
1264
  // if pOffset equal to current offset, means continue consume
1265
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,069,177✔
1266
    return 0;
3,659,770✔
1267
  }
1268

1269
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
408,791✔
1270
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
267,879✔
1271
    if (pOperator == NULL || code != 0) {
267,890✔
1272
      return code;
×
1273
    }
1274

1275
    SStreamScanInfo* pInfo = pOperator->info;
267,951✔
1276
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
267,951✔
1277
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
268,073✔
1278
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
267,936✔
1279

1280
    if (pOffset->type == TMQ_OFFSET__LOG) {
267,936✔
1281
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
223,405✔
1282
      pScanBaseInfo->dataReader = NULL;
222,964✔
1283

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

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

1309
      if (uid == 0) {
44,458✔
1310
        if (numOfTables != 0) {
43,146✔
1311
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
7,900✔
1312
          if (!tmp) {
7,900✔
1313
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1314
            taosRUnLockLatch(&pTaskInfo->lock);
×
1315
            return terrno;
×
1316
          }
1317
          if (tmp) uid = tmp->uid;
7,900✔
1318
          ts = INT64_MIN;
7,900✔
1319
          pScanInfo->currentTable = 0;
7,900✔
1320
        } else {
1321
          taosRUnLockLatch(&pTaskInfo->lock);
35,246✔
1322
          qError("no table in table list, %s", id);
35,531✔
1323
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
35,531✔
1324
        }
1325
      }
1326
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,212✔
1327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1418
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
137,434✔
1419
      tableListClear(pTableListInfo);
137,500✔
1420

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

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

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

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

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

1468
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
137,159✔
1469
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
137,477✔
1470
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1471
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
137,053✔
1472
      pTaskInfo->streamInfo.schema = mtInfo.schema;
137,477✔
1473
      taosMemoryFreeClear(mtInfo.pExtSchemas);
137,477✔
1474

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

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

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

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

1510
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
103,668,659✔
1511

1512
  if (pMsg->contLen > 0) {
103,673,912✔
1513
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
103,659,204✔
1514
    if (buf.pData == NULL) {
103,660,271✔
1515
      pMsg->code = terrno;
×
1516
    } else {
1517
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
103,660,271✔
1518
    }
1519
  }
1520

1521
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
103,677,313✔
1522
  rpcFreeCont(pMsg->pCont);
103,686,007✔
1523
  destroySendMsgInfo(pSendInfo);
103,659,568✔
1524
}
1525

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

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

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

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

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

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

1554
  taosArrayDestroy(plist);
×
1555

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

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

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

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

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

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

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

1598
  *pList = NULL;
1,714,950✔
1599
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,714,950✔
1600
  if (pArray == NULL) {
1,715,560✔
1601
    return terrno;
×
1602
  }
1603

1604
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,715,560✔
1605
  if (code == 0) {
1,713,123✔
1606
    *pList = pArray;
1,713,123✔
1607
  } else {
1608
    taosArrayDestroy(pArray);
×
1609
  }
1610
  return code;
1,711,903✔
1611
}
1612

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

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

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

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

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

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

1648
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
36,473,987✔
1649
  int32_t code = 0;
36,473,987✔
1650

1651
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
36,473,987✔
1652
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
36,472,332✔
1653

1654
  if (pOper->fpSet.resetStateFn) {
36,471,542✔
1655
    code = pOper->fpSet.resetStateFn(pOper);
36,474,005✔
1656
  }
1657
  pOper->status = OP_NOT_OPENED;
36,469,705✔
1658
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
61,273,854✔
1659
    code = clearStatesForOperator(pOper->pDownstream[i]);
24,795,259✔
1660
  }
1661
  return code;
36,478,450✔
1662
}
1663

1664
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
11,679,492✔
1665
  int32_t        code = 0;
11,679,492✔
1666
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,679,492✔
1667
  SOperatorInfo* pOper = pTaskInfo->pRoot;
11,679,492✔
1668
  pTaskInfo->code = TSDB_CODE_SUCCESS;
11,679,898✔
1669
  code = clearStatesForOperator(pOper);
11,679,898✔
1670
  return code;
11,680,304✔
1671
}
1672

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

1678
  *ppRes = NULL;
16,197,351✔
1679

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

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

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

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

1699
  pTaskInfo->owner = threadId;
16,198,629✔
1700
  taosRUnLockLatch(&pTaskInfo->lock);
16,199,069✔
1701

1702
  if (pTaskInfo->cost.start == 0) {
16,198,603✔
1703
    pTaskInfo->cost.start = taosGetTimestampUs();
337,969✔
1704
  }
1705

1706
  // error occurs, record the error code and return to client
1707
  int32_t ret = setjmp(pTaskInfo->env);
16,198,603✔
1708
  if (ret != TSDB_CODE_SUCCESS) {
18,041,916✔
1709
    pTaskInfo->code = ret;
1,845,404✔
1710
    (void)cleanUpUdfs();
1,845,404✔
1711
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,845,404✔
1712
    atomic_store_64(&pTaskInfo->owner, 0);
1,845,404✔
1713
    return pTaskInfo->code;
1,845,404✔
1714
  }
1715

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

1718
  int64_t st = taosGetTimestampUs();
16,198,663✔
1719

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

1733
  uint64_t el = (taosGetTimestampUs() - st);
14,353,259✔
1734

1735
  pTaskInfo->cost.elapsedTime += el;
14,353,259✔
1736
  if (NULL == *ppRes) {
14,353,259✔
1737
    *useconds = pTaskInfo->cost.elapsedTime;
9,281,939✔
1738
  }
1739

1740
  (void)cleanUpUdfs();
14,352,853✔
1741

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

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

1748
  atomic_store_64(&pTaskInfo->owner, 0);
14,353,665✔
1749
  return pTaskInfo->code;
14,353,665✔
1750
}
1751

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

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

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

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

1784
end:
331,195✔
1785
  return 0;
331,195✔
1786
}
1787

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

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

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

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

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

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

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

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

1886
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
2,133,097✔
1887

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2146
  (*ppDst)->suid = pSrc->suid;
329,249✔
2147
  (*ppDst)->sver = pSrc->sver;
328,435✔
2148
  (*ppDst)->tbType = pSrc->tbType;
328,435✔
2149
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
328,842✔
2150
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
329,249✔
2151

2152
  if (pSrc->stbname) {
328,435✔
2153
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
328,842✔
2154
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
329,249✔
2155
  }
2156

2157
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
328,842✔
2158
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
329,249✔
2159

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

2162
  if (pSrc->pFields && pSrc->pFields->size > 0) {
329,249✔
2163
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
328,843✔
2164
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
329,249✔
2165
  } else {
2166
    (*ppDst)->pFields = NULL;
×
2167
  }
2168
  
2169
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
329,249✔
2170
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
212,355✔
2171
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
212,355✔
2172
  } else {
2173
    (*ppDst)->pTagFields = NULL;
116,894✔
2174
  }
2175

2176
_exit:
329,249✔
2177

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

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

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

© 2026 Coveralls, Inc