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

taosdata / TDengine / #4844

09 Nov 2025 03:44PM UTC coverage: 63.058% (-0.5%) from 63.514%
#4844

push

travis-ci

web-flow
test: minor changes (#33510)

117164 of 185804 relevant lines covered (63.06%)

115657269.29 hits per line

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

68.2
/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 "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

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

41
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
676,371✔
42
  gExecInfo.dnode = pDnode;
676,371✔
43
  gExecInfo.getMnode = getMnode;
676,371✔
44
  gExecInfo.getDnodeId = getDnodeId;
676,371✔
45
  return;
676,371✔
46
}
47

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

57
static void cleanupRefPool() {
630,302✔
58
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
630,302✔
59
  taosCloseRef(ref);
630,302✔
60
}
630,302✔
61

62
static void initRefPool() {
630,302✔
63
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
630,302✔
64
  (void)atexit(cleanupRefPool);
630,302✔
65
}
630,302✔
66

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

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

85
    SStreamScanInfo* pInfo = pOperator->info;
×
86

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

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

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

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

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

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

148
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,620,181✔
149
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,620,181✔
150
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,620,345✔
151
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,033,872✔
152
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,033,929✔
153
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,033,929✔
154
      if (pScanInfo->base.dataReader != NULL) {
4,033,829✔
155
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,084✔
156
        if (code) {
57,027✔
157
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
158
          return code;
×
159
        }
160
      }
161
    }
162
  } else {
163
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,586,404✔
164
  }
165

166
  return 0;
10,911,719✔
167
}
168

169
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
10,911,682✔
170
  SExecTaskInfo* pTaskInfo = tinfo;
10,911,682✔
171
  pTaskInfo->id.queryId = queryId;
10,911,682✔
172
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
10,911,682✔
173

174
  // set the idstr for tsdbReader
175
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,911,776✔
176
}
177

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

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

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

192
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
193

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

201
  return code;
×
202
}
203

204
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
137,627✔
205
                                     uint64_t id) {
206
  if (msg == NULL) {  // create raw scan
137,627✔
207
    SExecTaskInfo* pTaskInfo = NULL;
22,993✔
208

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

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

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

225
  SSubplan* pPlan = NULL;
114,634✔
226
  int32_t   code = qStringToSubplan(msg, &pPlan);
115,852✔
227
  if (code != TSDB_CODE_SUCCESS) {
116,284✔
228
    terrno = code;
×
229
    return NULL;
×
230
  }
231

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

240
  // extract the number of output columns
241
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
116,353✔
242
  *numOfCols = 0;
116,353✔
243

244
  SNode* pNode;
245
  FOREACH(pNode, pDescNode->pSlots) {
1,382,302✔
246
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,265,949✔
247
    if (pSlotDesc->output) {
1,265,949✔
248
      ++(*numOfCols);
1,265,841✔
249
    }
250
  }
251

252
  return pTaskInfo;
116,353✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
646,197✔
256
  if (streamInserterParam == NULL) {
646,197✔
257
    return TSDB_CODE_SUCCESS;
323,716✔
258
  }
259

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

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

270
  if (streamInserterParam->suid <= 0 &&
322,481✔
271
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
115,235✔
272
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
273
    return TSDB_CODE_INVALID_PARA;
×
274
  }
275

276
  return TSDB_CODE_SUCCESS;
322,481✔
277
}
278

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

299
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
646,197✔
300
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
646,197✔
301
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
412✔
302
    goto _error;
412✔
303
  }
304

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

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

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

334
_error:
176,183✔
335

336
  if (code != TSDB_CODE_SUCCESS) {
646,197✔
337
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
412✔
338
    if (pInserterParam != NULL) {
412✔
339
      taosMemoryFree(pInserterParam);
×
340
    }
341
  }
342
  return code;
646,197✔
343
}
344

345
bool qNeedReset(qTaskInfo_t pInfo) {
6,236,896✔
346
  if (pInfo == NULL) {
6,236,896✔
347
    return false;
×
348
  }
349
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,236,896✔
350
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,236,896✔
351
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,236,896✔
352
    return false;
4,752✔
353
  }
354
  int32_t node = nodeType(pOperator->pPhyNode);
6,232,144✔
355
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
6,084,405✔
356
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
12,316,549✔
357
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
358
}
359

360
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
6,002,462✔
361
  if (pHandle == NULL || pScanBaseInfo == NULL) {
6,002,462✔
362
    return;
×
363
  }
364

365
  pScanBaseInfo->readHandle.uid = pHandle->uid;
6,002,462✔
366
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
6,002,462✔
367
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
6,002,462✔
368
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
6,002,462✔
369
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
6,002,462✔
370
}
371

372
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
6,232,144✔
373
  if (pInfo == NULL) {
6,232,144✔
374
    return TSDB_CODE_INVALID_PARA;
×
375
  }
376
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,232,144✔
377
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,232,144✔
378

379
  void*           info = pOperator->info;
6,232,144✔
380
  STableScanBase* pScanBaseInfo = NULL;
6,232,144✔
381

382
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,232,144✔
383
    pScanBaseInfo = &((STableScanInfo*)info)->base;
147,739✔
384
    setReadHandle(handle, pScanBaseInfo);
147,739✔
385
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
6,084,405✔
386
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,854,723✔
387
    setReadHandle(handle, pScanBaseInfo);
5,854,723✔
388
  }
389

390
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
6,232,144✔
391
  handle->winRange.ekey);
392
  return pOperator->fpSet.resetStateFn(pOperator);
6,232,144✔
393
}
394

395
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
646,197✔
396
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
397
  if (msg == NULL) {
646,197✔
398
    return TSDB_CODE_INVALID_PARA;
×
399
  }
400

401
  *pTaskInfo = NULL;
646,197✔
402

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

418
  return code;
645,785✔
419
}
420

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

433
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
66,907✔
434

435
  uint64_t suid = 0;
66,907✔
436
  uint64_t uid = 0;
66,907✔
437
  int32_t  type = 0;
66,907✔
438
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
66,907✔
439

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

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

453
    tDecoderClear(&mr.coder);
67,600✔
454

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

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

481
      if (!qualified) {
59,689✔
482
        continue;
29,859✔
483
      }
484
    }
485

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

491
_end:
66,907✔
492

493
  pAPI->metaReaderFn.clearReader(&mr);
66,907✔
494
  (*ppArrayRes) = qa;
66,907✔
495

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

503
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
67,125✔
504
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
67,125✔
505
  const char*    id = GET_TASKID(pTaskInfo);
67,125✔
506
  int32_t        code = 0;
67,125✔
507

508
  if (isAdd) {
67,125✔
509
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
66,907✔
510
  }
511

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

519
  SStreamScanInfo* pScanInfo = pInfo->info;
67,125✔
520
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
67,125✔
521
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
134,943✔
522
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
67,818✔
523
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
67,818✔
524
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
67,818✔
525
    }
526
  }
527

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

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

551
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
66,907✔
552
    taosWLockLatch(&pTaskInfo->lock);
66,907✔
553

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

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

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

588
    taosWUnLockLatch(&pTaskInfo->lock);
66,907✔
589
    if (keyBuf != NULL) {
66,907✔
590
      taosMemoryFree(keyBuf);
×
591
    }
592

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

601
  return code;
67,125✔
602
}
603

604
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
291,820,565✔
605
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
606
                                    int32_t idx, bool* tbGet) {
607
  *tbGet = false;
291,820,565✔
608

609
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
291,826,310✔
610
    return TSDB_CODE_INVALID_PARA;
401✔
611
  }
612
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
291,826,405✔
613

614
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
291,826,405✔
615
    return TSDB_CODE_SUCCESS;
171,776,008✔
616
  }
617

618
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
120,050,699✔
619
  if (!pSchemaInfo) {
120,034,557✔
620
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
621
    return terrno;
×
622
  }
623

624
  *sversion = pSchemaInfo->sw->version;
120,034,557✔
625
  *tversion = pSchemaInfo->tversion;
120,057,932✔
626
  *rversion = pSchemaInfo->rversion;
120,050,189✔
627
  if (pSchemaInfo->dbname) {
120,058,707✔
628
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
120,054,200✔
629
  } else {
630
    dbName[0] = 0;
×
631
  }
632
  if (pSchemaInfo->tablename) {
120,068,711✔
633
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
120,059,614✔
634
  } else {
635
    tableName[0] = 0;
794✔
636
  }
637

638
  *tbGet = true;
120,065,894✔
639

640
  return TSDB_CODE_SUCCESS;
120,062,687✔
641
}
642

643
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
171,772,541✔
644

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

652
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
37,469,800✔
653
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
37,469,800✔
654
  ((SExecTaskInfo*)tinfo)->paramSet = false;
37,469,800✔
655
}
37,469,800✔
656

657
int32_t qExecutorInit(void) {
4,637,639✔
658
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,637,639✔
659
  return TSDB_CODE_SUCCESS;
4,637,545✔
660
}
661

662
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
173,601,042✔
663
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
664
                        EOPTR_EXEC_MODEL model) {
665
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
173,601,042✔
666
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
173,601,042✔
667

668
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
173,602,259✔
669

670
  readHandle->uid = 0;
173,629,422✔
671
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
173,640,103✔
672
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
173,542,073✔
673
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
130,861✔
674
    goto _error;
27,212✔
675
  }
676

677
  if (handle) {
173,432,661✔
678
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
173,381,958✔
679
    void*           pSinkManager = NULL;
173,416,253✔
680
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
173,241,431✔
681
    if (code != TSDB_CODE_SUCCESS) {
173,400,374✔
682
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
683
      goto _error;
×
684
    }
685

686
    void* pSinkParam = NULL;
173,400,374✔
687
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
173,407,064✔
688
    if (code != TSDB_CODE_SUCCESS) {
173,283,929✔
689
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
690
      taosMemoryFree(pSinkManager);
×
691
      goto _error;
×
692
    }
693

694
    SDataSinkNode* pSink = NULL;
173,283,929✔
695
    if (readHandle->localExec) {
173,367,798✔
696
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
10,780✔
697
      if (code != TSDB_CODE_SUCCESS) {
10,780✔
698
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
699
               (*pTask)->id.str);
700
        taosMemoryFree(pSinkManager);
×
701
        goto _error;
×
702
      }
703
    }
704

705
    // pSinkParam has been freed during create sinker.
706
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
173,315,885✔
707
                              (*pTask)->id.str, pSubplan->processOneBlock);
173,430,226✔
708
    if (code) {
173,289,195✔
709
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
620✔
710
    }
711
  }
712

713
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
173,461,359✔
714
         tstrerror(code));
715

716
_error:
8,991,648✔
717
  // if failed to add ref for all tables in this query, abort current query
718
  return code;
173,630,539✔
719
}
720

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

726
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
233,662,882✔
727
                     bool processOneBlock) {
728
  int32_t        code = TSDB_CODE_SUCCESS;
233,662,882✔
729
  int32_t        lino = 0;
233,662,882✔
730
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
233,662,882✔
731
  int64_t        threadId = taosGetSelfPthreadId();
233,662,882✔
732

733
  if (pLocal) {
233,659,937✔
734
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
227,071,299✔
735
  }
736

737
  taosArrayClear(pResList);
233,645,707✔
738

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

746
  if (pTaskInfo->cost.start == 0) {
233,646,839✔
747
    pTaskInfo->cost.start = taosGetTimestampUs();
169,571,408✔
748
  }
749

750
  if (isTaskKilled(pTaskInfo)) {
233,660,844✔
751
    atomic_store_64(&pTaskInfo->owner, 0);
×
752
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
753
    return pTaskInfo->code;
×
754
  }
755

756
  // error occurs, record the error code and return to client
757
  int32_t ret = setjmp(pTaskInfo->env);
233,659,616✔
758
  if (ret != TSDB_CODE_SUCCESS) {
233,869,025✔
759
    pTaskInfo->code = ret;
234,715✔
760
    (void)cleanUpUdfs();
234,715✔
761

762
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
234,715✔
763
    atomic_store_64(&pTaskInfo->owner, 0);
234,715✔
764

765
    return pTaskInfo->code;
234,715✔
766
  }
767

768
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
233,634,310✔
769

770
  int32_t      current = 0;
233,634,338✔
771
  SSDataBlock* pRes = NULL;
233,634,338✔
772
  int64_t      st = taosGetTimestampUs();
233,659,019✔
773

774
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
233,659,019✔
775
    pTaskInfo->paramSet = true;
37,469,800✔
776
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,469,800✔
777
  } else {
778
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
196,187,062✔
779
  }
780

781
  QUERY_CHECK_CODE(code, lino, _end);
233,425,773✔
782
  code = blockDataCheck(pRes);
233,425,773✔
783
  QUERY_CHECK_CODE(code, lino, _end);
233,429,009✔
784

785
  if (pRes == NULL) {
233,429,009✔
786
    st = taosGetTimestampUs();
43,325,329✔
787
  }
788

789
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
233,430,380✔
790
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
233,430,500✔
791
    rowsThreshold = 4096;
232,730,092✔
792
  }
793

794
  int32_t blockIndex = 0;
233,430,730✔
795
  while (pRes != NULL) {
614,982,992✔
796
    SSDataBlock* p = NULL;
434,350,992✔
797
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
434,328,985✔
798
      SSDataBlock* p1 = NULL;
299,899,860✔
799
      code = createOneDataBlock(pRes, true, &p1);
299,900,064✔
800
      QUERY_CHECK_CODE(code, lino, _end);
299,896,908✔
801

802
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
299,896,908✔
803
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
299,900,495✔
804
      p = p1;
299,900,495✔
805
    } else {
806
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
134,472,414✔
807
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
134,472,006✔
808

809
      p = *(SSDataBlock**)tmp;
134,472,006✔
810
      code = copyDataBlock(p, pRes);
134,471,803✔
811
      QUERY_CHECK_CODE(code, lino, _end);
134,446,543✔
812
    }
813

814
    blockIndex += 1;
434,347,376✔
815

816
    current += p->info.rows;
434,347,376✔
817
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
434,370,729✔
818
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
819
    void* tmp = taosArrayPush(pResList, &p);
434,372,576✔
820
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
434,372,576✔
821

822
    if (current >= rowsThreshold || processOneBlock) {
434,372,576✔
823
      break;
824
    }
825

826
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
381,572,753✔
827
    QUERY_CHECK_CODE(code, lino, _end);
381,554,659✔
828
    code = blockDataCheck(pRes);
381,554,659✔
829
    QUERY_CHECK_CODE(code, lino, _end);
381,577,600✔
830
  }
831

832
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
233,431,270✔
833
    pTaskInfo->pSubplan->rowsThreshold -= current;
693,493✔
834
  }
835

836
  *hasMore = (pRes != NULL);
233,431,229✔
837
  uint64_t el = (taosGetTimestampUs() - st);
233,420,250✔
838

839
  pTaskInfo->cost.elapsedTime += el;
233,420,250✔
840
  if (NULL == pRes) {
233,421,489✔
841
    *useconds = pTaskInfo->cost.elapsedTime;
180,621,666✔
842
  }
843

844
_end:
233,357,386✔
845
  (void)cleanUpUdfs();
233,422,706✔
846

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

851
  atomic_store_64(&pTaskInfo->owner, 0);
233,438,301✔
852
  if (code) {
233,438,301✔
853
    pTaskInfo->code = code;
×
854
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
855
  }
856

857
  return pTaskInfo->code;
233,438,301✔
858
}
859

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

871
  taosArrayClear(pTaskInfo->pResultBlockList);
×
872
}
×
873

874
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
41,892,385✔
875
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
41,892,385✔
876
  int64_t        threadId = taosGetSelfPthreadId();
41,892,385✔
877
  int64_t        curOwner = 0;
41,892,563✔
878

879
  *pRes = NULL;
41,892,563✔
880

881
  // todo extract method
882
  taosRLockLatch(&pTaskInfo->lock);
41,892,563✔
883
  bool isKilled = isTaskKilled(pTaskInfo);
41,892,781✔
884
  if (isKilled) {
41,892,781✔
885
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
886

887
    taosRUnLockLatch(&pTaskInfo->lock);
×
888
    return pTaskInfo->code;
×
889
  }
890

891
  if (pTaskInfo->owner != 0) {
41,892,781✔
892
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
893
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
894

895
    taosRUnLockLatch(&pTaskInfo->lock);
×
896
    return pTaskInfo->code;
×
897
  }
898

899
  pTaskInfo->owner = threadId;
41,892,672✔
900
  taosRUnLockLatch(&pTaskInfo->lock);
41,892,494✔
901

902
  if (pTaskInfo->cost.start == 0) {
41,892,781✔
903
    pTaskInfo->cost.start = taosGetTimestampUs();
92,334✔
904
  }
905

906
  // error occurs, record the error code and return to client
907
  int32_t ret = setjmp(pTaskInfo->env);
41,892,781✔
908
  if (ret != TSDB_CODE_SUCCESS) {
41,889,809✔
909
    pTaskInfo->code = ret;
×
910
    (void)cleanUpUdfs();
×
911
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
912
    atomic_store_64(&pTaskInfo->owner, 0);
×
913
    return pTaskInfo->code;
×
914
  }
915

916
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
41,889,809✔
917

918
  int64_t st = taosGetTimestampUs();
41,891,840✔
919
  int32_t code = TSDB_CODE_SUCCESS;
41,891,840✔
920
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
41,891,840✔
921
    pTaskInfo->paramSet = true;
×
922
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
923
  } else {
924
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
41,891,840✔
925
  }
926
  if (code) {
41,874,239✔
927
    pTaskInfo->code = code;
×
928
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
929
  }
930

931
  code = blockDataCheck(*pRes);
41,874,239✔
932
  if (code) {
41,890,663✔
933
    pTaskInfo->code = code;
×
934
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
935
  }
936

937
  uint64_t el = (taosGetTimestampUs() - st);
41,879,123✔
938

939
  pTaskInfo->cost.elapsedTime += el;
41,879,123✔
940
  if (NULL == *pRes) {
41,888,457✔
941
    *useconds = pTaskInfo->cost.elapsedTime;
3,715,848✔
942
  }
943

944
  (void)cleanUpUdfs();
41,889,000✔
945

946
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
41,892,236✔
947
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
41,892,781✔
948

949
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
41,892,236✔
950
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
951

952
  atomic_store_64(&pTaskInfo->owner, 0);
41,891,762✔
953
  return pTaskInfo->code;
41,892,781✔
954
}
955

956
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
58,623,218✔
957
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
58,623,218✔
958
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
58,624,122✔
959
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
58,624,122✔
960

961
  if (!tmp) {
58,622,911✔
962
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
963
    return terrno;
×
964
  }
965
  return TSDB_CODE_SUCCESS;
58,622,911✔
966
}
967

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

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

978
  return 0;
×
979
}
980

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

990
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
28,382✔
991
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
28,382✔
992

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

1015
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
28,382✔
1016
}
28,382✔
1017

1018
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
28,382✔
1019
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
28,382✔
1020
  if (pTaskInfo == NULL) {
28,382✔
1021
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1022
  }
1023

1024
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
28,382✔
1025

1026
  setTaskKilled(pTaskInfo, rspCode);
28,382✔
1027
  qStopTaskOperators(pTaskInfo);
28,382✔
1028

1029
  return TSDB_CODE_SUCCESS;
28,382✔
1030
}
1031

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

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

1045
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1046

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

1053
        taosMsleep(200);
×
1054

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

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

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

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

1085
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
174,282,504✔
1086
  STaskCostInfo* pSummary = &pTaskInfo->cost;
174,282,504✔
1087
  int64_t        idleTime = pSummary->start - pSummary->created;
174,282,595✔
1088

1089
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
174,278,004✔
1090
  if (pSummary->pRecoder != NULL) {
174,279,449✔
1091
    qDebug(
122,385,352✔
1092
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1093
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1094
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1095
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1096
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1097
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1098
  } else {
1099
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
51,896,415✔
1100
           pSummary->elapsedTime / 1000.0);
1101
  }
1102
}
174,281,767✔
1103

1104
void qDestroyTask(qTaskInfo_t qTaskHandle) {
179,584,673✔
1105
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
179,584,673✔
1106
  if (pTaskInfo == NULL) {
179,584,673✔
1107
    return;
5,307,776✔
1108
  }
1109

1110
  if (pTaskInfo->pRoot != NULL) {
174,276,897✔
1111
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
174,283,400✔
1112
  } else {
1113
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1114
  }
1115

1116
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
174,284,194✔
1117
  doDestroyTask(pTaskInfo);
174,282,894✔
1118
}
1119

1120
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,646,853✔
1121
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,646,853✔
1122
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,646,853✔
1123
}
1124

1125
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
116,353✔
1126
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
116,353✔
1127
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
116,353✔
1128

1129
  while (1) {
113,556✔
1130
    uint16_t type = pOperator->operatorType;
229,909✔
1131
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
229,909✔
1132
      *scanner = pOperator->info;
116,353✔
1133
      break;
116,353✔
1134
    } else {
1135
      pOperator = pOperator->pDownstream[0];
113,556✔
1136
    }
1137
  }
1138
}
116,353✔
1139

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

1153
void* qExtractReaderFromTmqScanner(void* scanner) {
116,353✔
1154
  SStreamScanInfo* pInfo = scanner;
116,353✔
1155
  return (void*)pInfo->tqReader;
116,353✔
1156
}
1157

1158
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
154,221✔
1159
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
154,221✔
1160
  return pTaskInfo->streamInfo.schema;
154,221✔
1161
}
1162

1163
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
154,161✔
1164
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
154,161✔
1165
  return pTaskInfo->streamInfo.tbName;
154,161✔
1166
}
1167

1168
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
161,666✔
1169
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
161,666✔
1170
  return &pTaskInfo->streamInfo.btMetaRsp;
161,666✔
1171
}
1172

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

1184
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
151,863✔
1185
  memset(pCond, 0, sizeof(SQueryTableDataCond));
151,863✔
1186
  pCond->order = TSDB_ORDER_ASC;
151,863✔
1187
  pCond->numOfCols = pMtInfo->schema->nCols;
152,857✔
1188
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
153,037✔
1189
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
152,488✔
1190
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
152,617✔
1191
    taosMemoryFreeClear(pCond->colList);
660✔
1192
    taosMemoryFreeClear(pCond->pSlotList);
×
1193
    return terrno;
×
1194
  }
1195

1196
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
151,957✔
1197
  pCond->suid = pMtInfo->suid;
152,557✔
1198
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
152,703✔
1199
  pCond->startVersion = -1;
152,565✔
1200
  pCond->endVersion = sContext->snapVersion;
152,137✔
1201

1202
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
970,277✔
1203
    SColumnInfo* pColInfo = &pCond->colList[i];
817,343✔
1204
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
817,437✔
1205
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
818,062✔
1206
    if (pMtInfo->pExtSchemas != NULL) {
817,197✔
1207
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,494✔
1208
    }
1209
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
818,106✔
1210
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
817,926✔
1211

1212
    pCond->pSlotList[i] = i;
817,472✔
1213
  }
1214

1215
  return TSDB_CODE_SUCCESS;
153,037✔
1216
}
1217

1218
void qStreamSetOpen(qTaskInfo_t tinfo) {
41,579,204✔
1219
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
41,579,204✔
1220
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
41,579,204✔
1221
  pOperator->status = OP_NOT_OPENED;
41,582,152✔
1222
}
41,581,601✔
1223

1224
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
3,976,055✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,976,055✔
1226
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
3,976,055✔
1227
}
3,976,198✔
1228

1229
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,197,796✔
1230
  int32_t        code = TSDB_CODE_SUCCESS;
4,197,796✔
1231
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,197,796✔
1232
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,197,796✔
1233

1234
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,198,347✔
1235
  const char*    id = GET_TASKID(pTaskInfo);
4,198,287✔
1236

1237
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,198,043✔
1238
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,912,741✔
1239
    if (pOperator == NULL || code != 0) {
3,912,685✔
1240
      return code;
×
1241
    }
1242

1243
    SStreamScanInfo* pInfo = pOperator->info;
3,913,039✔
1244
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,913,032✔
1245
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,912,593✔
1246
    walReaderVerifyOffset(pWalReader, pOffset);
3,913,238✔
1247
  }
1248
  // if pOffset equal to current offset, means continue consume
1249
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,198,046✔
1250
    return 0;
3,755,009✔
1251
  }
1252

1253
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
441,605✔
1254
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
285,384✔
1255
    if (pOperator == NULL || code != 0) {
285,179✔
1256
      return code;
×
1257
    }
1258

1259
    SStreamScanInfo* pInfo = pOperator->info;
285,630✔
1260
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
285,460✔
1261
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
285,179✔
1262
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
285,302✔
1263

1264
    if (pOffset->type == TMQ_OFFSET__LOG) {
285,129✔
1265
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
224,318✔
1266
      pScanBaseInfo->dataReader = NULL;
224,160✔
1267

1268
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
224,230✔
1269
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
224,318✔
1270
      walReaderVerifyOffset(pWalReader, pOffset);
224,213✔
1271
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
224,387✔
1272
      if (code < 0) {
224,387✔
1273
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,688✔
1274
        return code;
7,688✔
1275
      }
1276
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
60,868✔
1277
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1278
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1279
      int64_t uid = pOffset->uid;
61,319✔
1280
      int64_t ts = pOffset->ts;
61,183✔
1281
      int32_t index = 0;
60,868✔
1282

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

1293
      if (uid == 0) {
60,807✔
1294
        if (numOfTables != 0) {
59,484✔
1295
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,313✔
1296
          if (!tmp) {
10,209✔
1297
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1298
            taosRUnLockLatch(&pTaskInfo->lock);
×
1299
            return terrno;
×
1300
          }
1301
          if (tmp) uid = tmp->uid;
10,209✔
1302
          ts = INT64_MIN;
10,209✔
1303
          pScanInfo->currentTable = 0;
10,209✔
1304
        } else {
1305
          taosRUnLockLatch(&pTaskInfo->lock);
49,171✔
1306
          qError("no table in table list, %s", id);
49,616✔
1307
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
49,698✔
1308
        }
1309
      }
1310
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,560✔
1311

1312
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
11,621✔
1313
             pInfo->pTableScanOp->resultInfo.totalRows);
1314
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
11,621✔
1315

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

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

1330
      STableKeyInfo keyInfo = {.uid = uid};
11,621✔
1331
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
11,621✔
1332

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

1341
      if (pScanBaseInfo->dataReader == NULL) {
11,621✔
1342
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
20,218✔
1343
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1344
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
10,109✔
1345
        if (code != TSDB_CODE_SUCCESS) {
10,033✔
1346
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1347
          return code;
×
1348
        }
1349

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

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

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

1375
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1376
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
156,221✔
1377
      SStreamRawScanInfo* pInfo = pOperator->info;
153,219✔
1378
      SSnapContext*       sContext = pInfo->sContext;
153,219✔
1379
      SOperatorInfo*      p = NULL;
153,219✔
1380

1381
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
153,219✔
1382
      if (code != 0) {
153,219✔
1383
        return code;
×
1384
      }
1385

1386
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
153,219✔
1387

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

1393
      SMetaTableInfo mtInfo = {0};
153,125✔
1394
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
153,125✔
1395
      if (code != 0) {
152,979✔
1396
        destroyMetaTableInfo(&mtInfo);
1397
        return code;
×
1398
      }
1399
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
152,979✔
1400
      pInfo->dataReader = NULL;
152,472✔
1401

1402
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
152,378✔
1403
      tableListClear(pTableListInfo);
152,085✔
1404

1405
      if (mtInfo.uid == 0) {
153,159✔
1406
        destroyMetaTableInfo(&mtInfo);
1407
        goto end;  // no data
182✔
1408
      }
1409

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

1423
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
152,763✔
1424
      if (code != TSDB_CODE_SUCCESS) {
153,037✔
1425
        destroyMetaTableInfo(&mtInfo);
1426
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1427
        return code;
×
1428
      }
1429

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

1444
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
306,074✔
1445
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
153,037✔
1446
      if (code != TSDB_CODE_SUCCESS) {
151,983✔
1447
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1448
        destroyMetaTableInfo(&mtInfo);
1449
        return code;
×
1450
      }
1451

1452
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
151,983✔
1453
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
152,797✔
1454
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1455
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
151,897✔
1456
      pTaskInfo->streamInfo.schema = mtInfo.schema;
152,394✔
1457
      taosMemoryFreeClear(mtInfo.pExtSchemas);
152,548✔
1458

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

1478
end:
384,137✔
1479
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
383,677✔
1480
  return 0;
384,541✔
1481
}
1482

1483
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
158,263,623✔
1484
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
158,263,623✔
1485
  if (pMsg->info.ahandle == NULL) {
158,274,953✔
1486
    qError("pMsg->info.ahandle is NULL");
×
1487
    return;
×
1488
  }
1489

1490
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
158,266,218✔
1491
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1492

1493
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
158,271,067✔
1494

1495
  if (pMsg->contLen > 0) {
158,270,784✔
1496
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
158,250,972✔
1497
    if (buf.pData == NULL) {
158,250,538✔
1498
      pMsg->code = terrno;
×
1499
    } else {
1500
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
158,250,538✔
1501
    }
1502
  }
1503

1504
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
158,281,483✔
1505
  rpcFreeCont(pMsg->pCont);
158,277,855✔
1506
  destroySendMsgInfo(pSendInfo);
158,259,016✔
1507
}
1508

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

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

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

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

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

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

1537
  taosArrayDestroy(plist);
×
1538

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

1547
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,408,339✔
1548
  int32_t        code = TSDB_CODE_SUCCESS;
3,408,339✔
1549
  int32_t        lino = 0;
3,408,339✔
1550
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,408,339✔
1551

1552
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,408,947✔
1553
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1554
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1555

1556
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1557
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1558
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,407,718✔
1559
    STableScanInfo* pScanInfo = pOperator->info;
1,702,352✔
1560

1561
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,702,960✔
1562
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,705,379✔
1563
  } else {
1564
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,705,379✔
1565
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,705,379✔
1566
    }
1567
  }
1568

1569
_end:
×
1570
  if (code != TSDB_CODE_SUCCESS) {
3,410,758✔
1571
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1572
  }
1573
  return code;
3,408,934✔
1574
}
1575

1576
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,705,379✔
1577
  if (pList == NULL) {
1,705,379✔
1578
    return TSDB_CODE_INVALID_PARA;
×
1579
  }
1580

1581
  *pList = NULL;
1,705,379✔
1582
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,705,987✔
1583
  if (pArray == NULL) {
1,704,176✔
1584
    return terrno;
×
1585
  }
1586

1587
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,704,176✔
1588
  if (code == 0) {
1,705,379✔
1589
    *pList = pArray;
1,705,379✔
1590
  } else {
1591
    taosArrayDestroy(pArray);
×
1592
  }
1593
  return code;
1,705,379✔
1594
}
1595

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

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

1612
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1613
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1614

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

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

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

1631
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
53,878,161✔
1632
  int32_t code = 0;
53,878,161✔
1633

1634
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
53,878,161✔
1635
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
53,874,259✔
1636

1637
  if (pOper->fpSet.resetStateFn) {
53,874,259✔
1638
    code = pOper->fpSet.resetStateFn(pOper);
53,874,259✔
1639
  }
1640
  pOper->status = OP_NOT_OPENED;
53,875,385✔
1641
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
93,013,429✔
1642
    code = clearStatesForOperator(pOper->pDownstream[i]);
39,129,895✔
1643
  }
1644
  return code;
53,883,508✔
1645
}
1646

1647
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
14,747,061✔
1648
  int32_t        code = 0;
14,747,061✔
1649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
14,747,061✔
1650
  SOperatorInfo* pOper = pTaskInfo->pRoot;
14,747,061✔
1651
  pTaskInfo->code = TSDB_CODE_SUCCESS;
14,747,061✔
1652
  code = clearStatesForOperator(pOper);
14,747,061✔
1653
  return code;
14,746,646✔
1654
}
1655

1656
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
19,256,181✔
1657
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
19,256,181✔
1658
  int64_t        threadId = taosGetSelfPthreadId();
19,256,181✔
1659
  int64_t        curOwner = 0;
19,256,596✔
1660

1661
  *ppRes = NULL;
19,256,596✔
1662

1663
  // todo extract method
1664
  taosRLockLatch(&pTaskInfo->lock);
19,256,596✔
1665
  bool isKilled = isTaskKilled(pTaskInfo);
19,257,379✔
1666
  if (isKilled) {
19,256,985✔
1667
    // clearStreamBlock(pTaskInfo->pRoot);
1668
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1669

1670
    taosRUnLockLatch(&pTaskInfo->lock);
×
1671
    return pTaskInfo->code;
×
1672
  }
1673

1674
  if (pTaskInfo->owner != 0) {
19,256,985✔
1675
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1676
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1677

1678
    taosRUnLockLatch(&pTaskInfo->lock);
×
1679
    return pTaskInfo->code;
×
1680
  }
1681

1682
  pTaskInfo->owner = threadId;
19,256,570✔
1683
  taosRUnLockLatch(&pTaskInfo->lock);
19,256,985✔
1684

1685
  if (pTaskInfo->cost.start == 0) {
19,257,379✔
1686
    pTaskInfo->cost.start = taosGetTimestampUs();
331,163✔
1687
  }
1688

1689
  // error occurs, record the error code and return to client
1690
  int32_t ret = setjmp(pTaskInfo->env);
19,256,591✔
1691
  if (ret != TSDB_CODE_SUCCESS) {
24,355,315✔
1692
    pTaskInfo->code = ret;
5,099,134✔
1693
    (void)cleanUpUdfs();
5,099,134✔
1694
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
5,099,134✔
1695
    atomic_store_64(&pTaskInfo->owner, 0);
5,099,134✔
1696
    return pTaskInfo->code;
5,099,134✔
1697
  }
1698

1699
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
19,256,181✔
1700

1701
  int64_t st = taosGetTimestampUs();
19,257,379✔
1702

1703
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
19,257,379✔
1704
  if (code) {
14,157,816✔
1705
    pTaskInfo->code = code;
×
1706
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1707
  } else {
1708
    *finished = *ppRes == NULL;
14,157,816✔
1709
    code = blockDataCheck(*ppRes);
14,158,245✔
1710
  }
1711
  if (code) {
14,158,245✔
1712
    pTaskInfo->code = code;
×
1713
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1714
  }
1715

1716
  uint64_t el = (taosGetTimestampUs() - st);
14,158,245✔
1717

1718
  pTaskInfo->cost.elapsedTime += el;
14,158,245✔
1719
  if (NULL == *ppRes) {
14,157,845✔
1720
    *useconds = pTaskInfo->cost.elapsedTime;
9,334,963✔
1721
  }
1722

1723
  (void)cleanUpUdfs();
14,157,845✔
1724

1725
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
14,158,245✔
1726
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
14,158,245✔
1727

1728
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
14,158,245✔
1729
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1730

1731
  atomic_store_64(&pTaskInfo->owner, 0);
14,158,674✔
1732
  return pTaskInfo->code;
14,158,245✔
1733
}
1734

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

1740
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
5,284,114✔
1741
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1742
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1743
  STableListInfo* pList = tableListCreate();
5,284,114✔
1744
  if (pList == NULL) {
5,286,406✔
1745
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1746
    return terrno;
×
1747
  }
1748

1749
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
5,286,406✔
1750
  SReadHandle    pHandle = {.vnode = pVnode};
5,286,017✔
1751
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
5,285,632✔
1752

1753
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
5,286,031✔
1754
  if (code != 0) {
5,287,198✔
1755
    tableListDestroy(pList);
×
1756
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1757
    return code;
×
1758
  }
1759
  *pTableListInfo = pList;
5,287,198✔
1760
  return 0;
5,287,198✔
1761
}
1762

1763
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
12,987,396✔
1764
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
12,987,396✔
1765
    return TSDB_CODE_INVALID_PARA;
7✔
1766
  }
1767
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
12,988,550✔
1768
    *size = 0;
8,830,223✔
1769
    *pKeyInfo = NULL;
8,830,223✔
1770
    return 0;
8,830,223✔
1771
  }
1772
  if (currentGroupId == -1) {
4,158,318✔
1773
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
434,576✔
1774
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
434,576✔
1775
    return 0;
434,576✔
1776
  }
1777
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
3,723,742✔
1778
}
1779

1780
int32_t  qStreamSetTableList(void** pTableListInfo, uint64_t uid, uint64_t gid){
4,224,807✔
1781
  if (*pTableListInfo == NULL) {
4,224,807✔
1782
    *pTableListInfo = tableListCreate();
563,260✔
1783
    if (*pTableListInfo == NULL) {
563,260✔
1784
      return terrno;
×
1785
    }
1786
  }
1787
  return tableListAddTableInfo(*pTableListInfo, uid, gid);
4,224,807✔
1788
}
1789

1790
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
10,891,972✔
1791
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
10,891,972✔
1792
    return 0;
8,774,670✔
1793
  }
1794
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
2,999,290✔
1795
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
2,886,019✔
1796

1797
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
2,886,413✔
1798
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
2,886,413✔
1799
      return i;
2,005,973✔
1800
    }
1801
  }
1802
  return -1;
112,877✔
1803
}
1804

1805
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
6,119,746✔
1806

1807
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
6,329,400✔
1808

1809
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
1,945,032✔
1810
void    qStreamSetTableListGroupNum(const void* pTableList, int32_t groupNum) {((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; }
563,260✔
1811
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
304,211✔
1812

1813
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,605,869✔
1814

1815
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,634,745✔
1816
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,634,745✔
1817
  qDestroyTask(tInfo);
2,634,745✔
1818
}
2,634,745✔
1819

1820
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
807,293✔
1821
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
807,293✔
1822
}
1823

1824
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
819,732✔
1825
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1826
  int32_t      code = 0;
819,732✔
1827
  SNode*       pNode = 0;
819,732✔
1828
  SNodeList*   pList = NULL;
819,732✔
1829
  SExprInfo*   pExprInfo = NULL;
819,732✔
1830
  int32_t      numOfExprs = 1;
819,732✔
1831
  int32_t*     offset = 0;
819,732✔
1832
  STargetNode* pTargetNode = NULL;
819,732✔
1833
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
819,732✔
1834
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
819,732✔
1835

1836
  if (code == 0) {
819,732✔
1837
    pTargetNode->dataBlockId = 0;
819,732✔
1838
    pTargetNode->pExpr = pNode;
819,732✔
1839
    pTargetNode->slotId = 0;
819,732✔
1840
  }
1841
  if (code == 0) {
819,732✔
1842
    code = nodesMakeList(&pList);
819,732✔
1843
  }
1844
  if (code == 0) {
819,732✔
1845
    code = nodesListAppend(pList, (SNode*)pTargetNode);
819,732✔
1846
  }
1847
  if (code == 0) {
819,732✔
1848
    pNode = NULL;
819,732✔
1849
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
819,732✔
1850
  }
1851

1852
  if (code == 0) {
819,732✔
1853
    const char* pVal = NULL;
819,732✔
1854
    int32_t     len = 0;
819,732✔
1855
    SNode*      pSclNode = NULL;
819,732✔
1856
    switch (pExprInfo->pExpr->nodeType) {
819,732✔
1857
      case QUERY_NODE_FUNCTION:
819,732✔
1858
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
819,732✔
1859
        break;
819,732✔
1860
      case QUERY_NODE_OPERATOR:
×
1861
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1862
        break;
×
1863
      default:
×
1864
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1865
        break;
×
1866
    }
1867
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
819,732✔
1868
    SSDataBlock block = {0};
819,732✔
1869
    block.info.rows = 1;
819,732✔
1870
    SSDataBlock* pBlock = &block;
819,732✔
1871
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
819,732✔
1872
    if (tmp == NULL) {
819,732✔
1873
      code = terrno;
×
1874
    }
1875
    if (code == 0) {
819,732✔
1876
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
819,732✔
1877
    }
1878
    taosArrayDestroy(pBlockList);
819,732✔
1879
  }
1880
  nodesDestroyList(pList);
819,732✔
1881
  destroyExprInfo(pExprInfo, numOfExprs);
819,732✔
1882
  taosMemoryFreeClear(pExprInfo);
819,732✔
1883
  return code;
819,732✔
1884
}
1885

1886
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,532,917✔
1887
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,532,917✔
1888
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,532,917✔
1889
  int32_t        code = 0;
4,532,917✔
1890
  SNode*         pNode = NULL;
4,532,917✔
1891
  if (!pForceOutputCols) return 0;
4,532,917✔
1892
  if (!*pRes) {
12,439✔
1893
    code = createDataBlock(pRes);
12,439✔
1894
  }
1895

1896
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
12,439✔
1897
    int32_t idx = 0;
12,439✔
1898
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
49,376✔
1899
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
36,937✔
1900
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
36,937✔
1901
      colInfo.info.precision = pCol->type.precision;
36,937✔
1902
      colInfo.info.scale = pCol->type.scale;
36,937✔
1903
      code = blockDataAppendColInfo(*pRes, &colInfo);
36,937✔
1904
      if (code != 0) break;
36,937✔
1905
    }
1906
  }
1907

1908
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
12,439✔
1909
  if (code != TSDB_CODE_SUCCESS) {
12,439✔
1910
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1911
    return code;
×
1912
  }
1913

1914
  // loop all exprs for force output, execute all exprs
1915
  int32_t idx = 0;
12,439✔
1916
  int32_t rowIdx = (*pRes)->info.rows;
12,439✔
1917
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
12,439✔
1918
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
12,439✔
1919
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
49,376✔
1920
    SScalarParam   dst = {0};
36,937✔
1921
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
36,937✔
1922
    code = nodesStringToNode(pCol->expr, &pNode);
36,937✔
1923
    if (code != 0) break;
36,937✔
1924
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
36,937✔
1925
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
36,937✔
1926
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
24,498✔
1927
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
24,498✔
1928
    } else {
1929
      dst.columnData = pInfo;
12,439✔
1930
      dst.numOfRows = rowIdx;
12,439✔
1931
      dst.colAlloced = false;
12,439✔
1932
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
12,439✔
1933
    }
1934
    ++idx;
36,937✔
1935
    // TODO sclFreeParam(&dst);
1936
    nodesDestroyNode(pNode);
36,937✔
1937
    if (code != 0) break;
36,937✔
1938
  }
1939
  if (code == TSDB_CODE_SUCCESS) {
12,439✔
1940
    (*pRes)->info.rows++;
12,439✔
1941
  }
1942
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
12,439✔
1943
  return code;
12,439✔
1944
}
1945

1946
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
294,500✔
1947
  int32_t      code = 0;
294,500✔
1948
  const char*  pVal = NULL;
294,500✔
1949
  SScalarParam dst = {0};
294,500✔
1950
  int32_t      len = 0;
294,500✔
1951
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
294,500✔
1952
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
294,500✔
1953
  // execute the expr
1954
  switch (pExpr->type) {
294,500✔
1955
    case QUERY_NODE_VALUE: {
×
1956
      SValueNode* pValue = (SValueNode*)pExpr;
×
1957
      int32_t     type = pValue->node.resType.type;
×
1958
      if (!IS_STR_DATA_TYPE(type)) {
×
1959
        qError("invalid sub tb expr with non-str type");
×
1960
        code = TSDB_CODE_INVALID_PARA;
×
1961
        break;
×
1962
      }
1963
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
1964
      if (pTmp == NULL) {
×
1965
        qError("invalid sub tb expr with null value");
×
1966
        code = TSDB_CODE_INVALID_PARA;
×
1967
        break;
×
1968
      }
1969
      pVal = varDataVal(pTmp);
×
1970
      len = varDataLen(pTmp);
×
1971
    } break;
×
1972
    case QUERY_NODE_FUNCTION: {
294,500✔
1973
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
294,500✔
1974
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
294,500✔
1975
        qError("invalid sub tb expr with non-str type func");
×
1976
        code = TSDB_CODE_INVALID_PARA;
×
1977
        break;
×
1978
      }
1979
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
294,500✔
1980
      if (!pCol) {
294,500✔
1981
        code = terrno;
×
1982
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
1983
        break;
×
1984
      }
1985

1986
      pCol->hasNull = true;
294,500✔
1987
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
294,500✔
1988
      pCol->info.colId = 0;
294,500✔
1989
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
294,500✔
1990
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
294,500✔
1991
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
294,500✔
1992
      code = colInfoDataEnsureCapacity(pCol, 1, true);
294,500✔
1993
      if (code != 0) {
294,500✔
1994
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
1995
        taosMemoryFree(pCol);
×
1996
        break;
×
1997
      }
1998
      dst.columnData = pCol;
294,500✔
1999
      dst.numOfRows = 1;
294,500✔
2000
      dst.colAlloced = true;
294,500✔
2001
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
294,500✔
2002
      if (colDataIsNull_var(dst.columnData, 0)) {
294,500✔
2003
        qInfo("invalid sub tb expr with null value");
×
2004
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
2005
      }
2006
      if (code == 0) {
294,500✔
2007
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
294,500✔
2008
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
294,500✔
2009
      }
2010
    } break;
294,500✔
2011
    default:
×
2012
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2013
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2014
      break;
×
2015
  }
2016
  if (code == 0) {
294,500✔
2017
    if (!pVal || len == 0) {
294,500✔
2018
      qError("tbname generated with no characters which is not allowed");
×
2019
      code = TSDB_CODE_INVALID_PARA;
×
2020
    }
2021
    if(len > TSDB_TABLE_NAME_LEN - 1) {
294,500✔
2022
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
760✔
2023
      len = TSDB_TABLE_NAME_LEN - 1;
760✔
2024
    }
2025

2026
    memcpy(tbname, pVal, len);
294,500✔
2027
    tbname[len] = '\0';  // ensure null terminated
294,500✔
2028
    if (NULL != strchr(tbname, '.')) {
294,500✔
2029
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2030
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2031
    }
2032
  }
2033
  // TODO free dst
2034
  sclFreeParam(&dst);
294,500✔
2035
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
294,500✔
2036
  return code;
294,500✔
2037
}
2038

2039
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
322,481✔
2040
  if (pParam) {
322,481✔
2041
    if (pParam->tbname) {
322,481✔
2042
      taosMemFree(pParam->tbname);
322,481✔
2043
      pParam->tbname = NULL;
322,481✔
2044
    }
2045
    if (pParam->stbname) {
322,481✔
2046
      taosMemFree(pParam->stbname);
322,481✔
2047
      pParam->stbname = NULL;
322,481✔
2048
    }
2049
    if (pParam->dbFName) {
322,481✔
2050
      taosMemFree(pParam->dbFName);
322,481✔
2051
      pParam->dbFName = NULL;
322,481✔
2052
    }
2053
    if (pParam->pFields) {
322,481✔
2054
      taosArrayDestroy(pParam->pFields);
322,481✔
2055
      pParam->pFields = NULL;
322,481✔
2056
    }
2057
    if (pParam->pTagFields) {
322,481✔
2058
      taosArrayDestroy(pParam->pTagFields);
207,246✔
2059
      pParam->pTagFields = NULL;
207,246✔
2060
    }
2061
    taosMemFree(pParam);
322,481✔
2062
  }
2063
}
322,481✔
2064

2065
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
322,481✔
2066
  int32_t code = 0, lino = 0;
322,481✔
2067
  if (ppDst == NULL || pSrc == NULL) {
322,481✔
2068
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2069
  }
2070
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
322,481✔
2071
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
322,481✔
2072

2073
  (*ppDst)->suid = pSrc->suid;
322,481✔
2074
  (*ppDst)->sver = pSrc->sver;
322,481✔
2075
  (*ppDst)->tbType = pSrc->tbType;
322,481✔
2076
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
322,481✔
2077
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
322,481✔
2078

2079
  if (pSrc->stbname) {
322,481✔
2080
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
322,481✔
2081
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
322,481✔
2082
  }
2083

2084
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
322,481✔
2085
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
322,481✔
2086

2087
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
322,481✔
2088

2089
  if (pSrc->pFields && pSrc->pFields->size > 0) {
322,481✔
2090
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
322,481✔
2091
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
322,481✔
2092
  } else {
2093
    (*ppDst)->pFields = NULL;
×
2094
  }
2095
  
2096
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
322,481✔
2097
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
207,246✔
2098
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
207,246✔
2099
  } else {
2100
    (*ppDst)->pTagFields = NULL;
115,235✔
2101
  }
2102

2103
_exit:
322,481✔
2104

2105
  if (code != 0) {
322,481✔
2106
    if (*ppDst) {
×
2107
      destroyStreamInserterParam(*ppDst);
×
2108
      *ppDst = NULL;
×
2109
    }
2110
    
2111
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2112
  }
2113
  return code;
322,481✔
2114
}
2115

2116
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
429✔
2117
  return doDropStreamTable(pMsgCb, pOutput, pReq);
429✔
2118
}
2119

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