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

taosdata / TDengine / #4865

26 Nov 2025 05:46AM UTC coverage: 64.495% (-0.05%) from 64.548%
#4865

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

767 of 945 new or added lines in 33 files covered. (81.16%)

3088 existing lines in 119 files now uncovered.

158096 of 245129 relevant lines covered (64.5%)

111671620.5 hits per line

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

67.96
/source/libs/executor/src/executor.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "taosdef.h"
32
#include "tarray.h"
33
#include "tdatablock.h"
34
#include "tref.h"
35
#include "trpc.h"
36
#include "tudf.h"
37
#include "wal.h"
38

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

43
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
673,695✔
44
  gExecInfo.dnode = pDnode;
673,695✔
45
  gExecInfo.getMnode = getMnode;
673,695✔
46
  gExecInfo.getDnodeId = getDnodeId;
673,695✔
47
  return;
673,695✔
48
}
49

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

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

64
static void initRefPool() {
627,905✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
627,905✔
66
  (void)atexit(cleanupRefPool);
627,905✔
67
}
627,905✔
68

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

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

87
    SStreamScanInfo* pInfo = pOperator->info;
×
88

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
14,976,673✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,976,673✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,978,338✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
3,670,377✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
3,670,356✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
3,670,384✔
156
      if (pScanInfo->base.dataReader != NULL) {
3,670,411✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
52,802✔
158
        if (code) {
52,827✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,305,770✔
166
  }
167

168
  return 0;
10,676,771✔
169
}
170

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

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

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

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

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

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

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

203
  return code;
×
204
}
205

206
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
134,405✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
134,405✔
209
    SExecTaskInfo* pTaskInfo = NULL;
20,944✔
210

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,358,011✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,243,549✔
249
    if (pSlotDesc->output) {
1,243,549✔
250
      ++(*numOfCols);
1,243,465✔
251
    }
252
  }
253

254
  return pTaskInfo;
114,462✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
658,204✔
258
  if (streamInserterParam == NULL) {
658,204✔
259
    return TSDB_CODE_SUCCESS;
364,488✔
260
  }
261

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

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

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

278
  return TSDB_CODE_SUCCESS;
293,716✔
279
}
280

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

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

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

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

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

336
_error:
138,924✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
6,354,752✔
348
  if (pInfo == NULL) {
6,354,752✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,354,752✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,354,752✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,354,752✔
354
    return false;
5,148✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
6,349,604✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
6,102,341✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
12,451,945✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

382
  void*           info = pOperator->info;
6,349,604✔
383
  STableScanBase* pScanBaseInfo = NULL;
6,349,604✔
384

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,349,604✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
247,263✔
387
    setReadHandle(handle, pScanBaseInfo);
247,263✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
6,102,341✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,616,495✔
390
    setReadHandle(handle, pScanBaseInfo);
5,616,495✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
658,204✔
405

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

421
  return code;
658,204✔
422
}
423

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

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

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
54,339✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
54,339✔
449

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

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

459
  locked = 1;
54,339✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
109,452✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
55,113✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
55,113✔
463

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

470
    tDecoderClear(&mr.coder);
55,113✔
471

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

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

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

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

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

517
      if (!qualified) {
47,348✔
518
        continue;
23,695✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
54,339✔
529

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

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

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

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

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

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

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

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

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

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

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

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

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

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

641
  return code;
54,951✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
285,593,218✔
650
    return TSDB_CODE_INVALID_PARA;
×
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
285,610,687✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
285,610,687✔
655
    return TSDB_CODE_SUCCESS;
168,409,918✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
117,191,980✔
665
  *tversion = pSchemaInfo->tversion;
117,214,787✔
666
  *rversion = pSchemaInfo->rversion;
117,208,084✔
667
  if (pSchemaInfo->dbname) {
117,199,036✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
117,207,678✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
117,226,640✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
117,218,127✔
674
  } else {
675
    tableName[0] = 0;
20✔
676
  }
677

678
  *tbGet = true;
117,224,708✔
679

680
  return TSDB_CODE_SUCCESS;
117,218,857✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
168,396,400✔
684

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

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

697
int32_t qExecutorInit(void) {
4,631,056✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,631,056✔
699
  return TSDB_CODE_SUCCESS;
4,632,057✔
700
}
701

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

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

710
  readHandle->uid = 0;
170,305,767✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
170,322,868✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
170,164,441✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
57,045✔
714
    goto _error;
26,995✔
715
  }
716

717
  if (handle) {
170,136,081✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
170,011,512✔
719
    void*           pSinkManager = NULL;
170,024,727✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
169,999,973✔
721
    if (code != TSDB_CODE_SUCCESS) {
169,959,353✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
169,959,353✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
170,062,920✔
728
    if (code != TSDB_CODE_SUCCESS) {
169,935,360✔
729
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
730
      taosMemoryFree(pSinkManager);
×
731
      goto _error;
×
732
    }
733

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

745
    // pSinkParam has been freed during create sinker.
746
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
169,976,590✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
170,116,166✔
748
    if (code) {
169,936,675✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
627✔
750
    }
751
  }
752

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

756
_error:
8,646,405✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
170,307,595✔
759
}
760

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

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

773
  if (pLocal) {
230,479,741✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
223,737,612✔
775
  }
776

777
  taosArrayClear(pResList);
230,457,241✔
778

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

786
  if (pTaskInfo->cost.start == 0) {
230,459,405✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
166,353,196✔
788
  }
789

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

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
230,477,121✔
798
  if (ret != TSDB_CODE_SUCCESS) {
230,616,329✔
799
    pTaskInfo->code = ret;
174,969✔
800
    (void)cleanUpUdfs();
174,969✔
801

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

805
    return pTaskInfo->code;
174,969✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
230,441,360✔
809

810
  int32_t      current = 0;
230,445,488✔
811
  SSDataBlock* pRes = NULL;
230,445,488✔
812
  int64_t      st = taosGetTimestampUs();
230,488,273✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
230,488,273✔
815
    pTaskInfo->paramSet = true;
37,457,635✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,457,635✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
193,024,523✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
230,311,966✔
822
  code = blockDataCheck(pRes);
230,311,966✔
823
  QUERY_CHECK_CODE(code, lino, _end);
230,313,574✔
824

825
  if (pRes == NULL) {
230,313,574✔
826
    st = taosGetTimestampUs();
42,663,863✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
230,313,667✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
230,315,622✔
831
    rowsThreshold = 4096;
229,625,770✔
832
  }
833

834
  int32_t blockIndex = 0;
230,309,623✔
835
  while (pRes != NULL) {
617,114,655✔
836
    SSDataBlock* p = NULL;
439,565,014✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
439,527,280✔
838
      SSDataBlock* p1 = NULL;
303,456,273✔
839
      code = createOneDataBlock(pRes, true, &p1);
303,458,623✔
840
      QUERY_CHECK_CODE(code, lino, _end);
303,436,525✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
303,436,525✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
303,448,436✔
844
      p = p1;
303,448,436✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
136,113,508✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
136,125,221✔
848

849
      p = *(SSDataBlock**)tmp;
136,125,221✔
850
      code = copyDataBlock(p, pRes);
136,125,594✔
851
      QUERY_CHECK_CODE(code, lino, _end);
136,110,546✔
852
    }
853

854
    blockIndex += 1;
439,559,557✔
855

856
    current += p->info.rows;
439,559,557✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
439,562,612✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
439,534,307✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
439,534,307✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
439,534,307✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
386,765,926✔
867
    QUERY_CHECK_CODE(code, lino, _end);
386,651,450✔
868
    code = blockDataCheck(pRes);
386,651,450✔
869
    QUERY_CHECK_CODE(code, lino, _end);
386,818,133✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
230,318,022✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
687,497✔
874
  }
875

876
  *hasMore = (pRes != NULL);
230,317,358✔
877
  uint64_t el = (taosGetTimestampUs() - st);
230,310,808✔
878

879
  pTaskInfo->cost.elapsedTime += el;
230,310,808✔
880
  if (NULL == pRes) {
230,310,807✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
177,542,426✔
882
  }
883

884
_end:
230,179,188✔
885
  (void)cleanUpUdfs();
230,303,796✔
886

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

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

897
  return pTaskInfo->code;
230,322,934✔
898
}
899

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

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

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

919
  *pRes = NULL;
38,917,171✔
920

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

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

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

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

939
  pTaskInfo->owner = threadId;
38,917,171✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
38,917,277✔
941

942
  if (pTaskInfo->cost.start == 0) {
38,917,536✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
91,380✔
944
  }
945

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

956
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
38,916,142✔
957

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

971
  code = blockDataCheck(*pRes);
38,906,707✔
972
  if (code) {
38,915,835✔
973
    pTaskInfo->code = code;
×
974
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
975
  }
976

977
  uint64_t el = (taosGetTimestampUs() - st);
38,910,003✔
978

979
  pTaskInfo->cost.elapsedTime += el;
38,910,003✔
980
  if (NULL == *pRes) {
38,914,363✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,332,229✔
982
  }
983

984
  (void)cleanUpUdfs();
38,915,106✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
38,917,536✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
38,917,466✔
988

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
38,917,127✔
993
  return pTaskInfo->code;
38,917,445✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
57,228,491✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,228,491✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
57,229,666✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,229,233✔
1000

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

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

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

1018
  return 0;
×
1019
}
1020

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

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

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

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
20,806✔
1067
  qStopTaskOperators(pTaskInfo);
20,806✔
1068

1069
  return TSDB_CODE_SUCCESS;
20,806✔
1070
}
1071

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

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

1085
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1086

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

1093
        taosMsleep(200);
×
1094

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

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

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

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

1125
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
170,979,665✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
170,979,665✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
170,981,119✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
170,979,925✔
1130
  if (pSummary->pRecoder != NULL) {
170,978,129✔
1131
    qDebug(
119,541,211✔
1132
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1133
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1134
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1135
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1136
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1137
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1138
  } else {
1139
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
51,435,199✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
170,976,410✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
185,296,816✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
185,296,816✔
1146
  if (pTaskInfo == NULL) {
185,296,816✔
1147
    return;
14,320,576✔
1148
  }
1149

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

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
170,980,342✔
1157
  doDestroyTask(pTaskInfo);
170,978,878✔
1158
}
1159

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

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

1169
  while (1) {
111,810✔
1170
    uint16_t type = pOperator->operatorType;
226,272✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
226,272✔
1172
      *scanner = pOperator->info;
114,462✔
1173
      break;
114,462✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
111,810✔
1176
    }
1177
  }
1178
}
114,462✔
1179

1180
static int32_t getOpratorIntervalInfo(SOperatorInfo* pOperator, int64_t* pWaterMark, SInterval* pInterval,
×
1181
                                      STimeWindow* pLastWindow, TSKEY* pRecInteral) {
1182
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1183
    return getOpratorIntervalInfo(pOperator->pDownstream[0], pWaterMark, pInterval, pLastWindow, pRecInteral);
×
1184
  }
1185
  SStreamScanInfo* pScanOp = (SStreamScanInfo*)pOperator->info;
×
1186
  *pWaterMark = pScanOp->twAggSup.waterMark;
×
1187
  *pInterval = pScanOp->interval;
×
1188
  *pLastWindow = pScanOp->lastScanRange;
×
1189
  *pRecInteral = pScanOp->recalculateInterval;
×
1190
  return TSDB_CODE_SUCCESS;
×
1191
}
1192

1193
void* qExtractReaderFromTmqScanner(void* scanner) {
114,462✔
1194
  SStreamScanInfo* pInfo = scanner;
114,462✔
1195
  return (void*)pInfo->tqReader;
114,462✔
1196
}
1197

1198
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
128,119✔
1199
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
128,119✔
1200
  return pTaskInfo->streamInfo.schema;
128,119✔
1201
}
1202

1203
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
128,119✔
1204
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
128,119✔
1205
  return pTaskInfo->streamInfo.tbName;
128,119✔
1206
}
1207

1208
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
133,537✔
1209
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
133,537✔
1210
  return &pTaskInfo->streamInfo.btMetaRsp;
133,537✔
1211
}
1212

1213
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
3,753,287✔
1214
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,753,287✔
1215
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
3,753,287✔
1216
  return 0;
3,753,287✔
1217
  /*if (code != TSDB_CODE_SUCCESS) {
1218
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1219
    pTaskInfo->code = code;
1220
    T_LONG_JMP(pTaskInfo->env, code);
1221
  }*/
1222
}
1223

1224
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
124,397✔
1225
  memset(pCond, 0, sizeof(SQueryTableDataCond));
124,397✔
1226
  pCond->order = TSDB_ORDER_ASC;
124,397✔
1227
  pCond->numOfCols = pMtInfo->schema->nCols;
124,510✔
1228
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
124,952✔
1229
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
124,745✔
1230
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
124,839✔
1231
    taosMemoryFreeClear(pCond->colList);
348✔
1232
    taosMemoryFreeClear(pCond->pSlotList);
×
1233
    return terrno;
×
1234
  }
1235

1236
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
124,491✔
1237
  pCond->suid = pMtInfo->suid;
124,745✔
1238
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
124,867✔
1239
  pCond->startVersion = -1;
125,046✔
1240
  pCond->endVersion = sContext->snapVersion;
124,519✔
1241

1242
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
801,345✔
1243
    SColumnInfo* pColInfo = &pCond->colList[i];
676,384✔
1244
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
676,233✔
1245
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
676,431✔
1246
    if (pMtInfo->pExtSchemas != NULL) {
676,403✔
1247
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
6,768✔
1248
    }
1249
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
676,638✔
1250
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
676,770✔
1251

1252
    pCond->pSlotList[i] = i;
676,582✔
1253
  }
1254

1255
  return TSDB_CODE_SUCCESS;
125,093✔
1256
}
1257

1258
void qStreamSetOpen(qTaskInfo_t tinfo) {
38,643,679✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,643,679✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
38,643,679✔
1261
  pOperator->status = OP_NOT_OPENED;
38,661,366✔
1262
}
38,660,533✔
1263

1264
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
3,616,383✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,616,383✔
1266
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
3,616,383✔
1267
}
3,616,520✔
1268

1269
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
3,806,244✔
1270
  int32_t        code = TSDB_CODE_SUCCESS;
3,806,244✔
1271
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,806,244✔
1272
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
3,806,244✔
1273

1274
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
3,806,657✔
1275
  const char*    id = GET_TASKID(pTaskInfo);
3,806,657✔
1276

1277
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
3,806,255✔
1278
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,555,857✔
1279
    if (pOperator == NULL || code != 0) {
3,555,451✔
UNCOV
1280
      return code;
×
1281
    }
1282

1283
    SStreamScanInfo* pInfo = pOperator->info;
3,555,497✔
1284
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,555,583✔
1285
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,555,393✔
1286
    walReaderVerifyOffset(pWalReader, pOffset);
3,555,712✔
1287
  }
1288
  // if pOffset equal to current offset, means continue consume
1289
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
3,805,381✔
1290
    return 0;
3,403,924✔
1291
  }
1292

1293
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
401,605✔
1294
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
273,742✔
1295
    if (pOperator == NULL || code != 0) {
273,916✔
1296
      return code;
×
1297
    }
1298

1299
    SStreamScanInfo* pInfo = pOperator->info;
273,986✔
1300
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
273,686✔
1301
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
273,847✔
1302
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
273,986✔
1303

1304
    if (pOffset->type == TMQ_OFFSET__LOG) {
273,583✔
1305
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
214,284✔
1306
      pScanBaseInfo->dataReader = NULL;
214,101✔
1307

1308
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
214,101✔
1309
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
214,101✔
1310
      walReaderVerifyOffset(pWalReader, pOffset);
214,377✔
1311
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
214,422✔
1312
      if (code < 0) {
214,506✔
1313
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
4,504✔
1314
        return code;
4,504✔
1315
      }
1316
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
59,426✔
1317
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1318
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1319
      int64_t uid = pOffset->uid;
59,569✔
1320
      int64_t ts = pOffset->ts;
59,496✔
1321
      int32_t index = 0;
59,207✔
1322

1323
      // this value may be changed if new tables are created
1324
      taosRLockLatch(&pTaskInfo->lock);
59,207✔
1325
      int32_t numOfTables = 0;
59,350✔
1326
      code = tableListGetSize(pTableListInfo, &numOfTables);
59,350✔
1327
      if (code != TSDB_CODE_SUCCESS) {
59,360✔
1328
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1329
        taosRUnLockLatch(&pTaskInfo->lock);
×
1330
        return code;
×
1331
      }
1332

1333
      if (uid == 0) {
59,360✔
1334
        if (numOfTables != 0) {
58,293✔
1335
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
9,571✔
1336
          if (!tmp) {
9,502✔
1337
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1338
            taosRUnLockLatch(&pTaskInfo->lock);
×
1339
            return terrno;
×
1340
          }
1341
          if (tmp) uid = tmp->uid;
9,502✔
1342
          ts = INT64_MIN;
9,502✔
1343
          pScanInfo->currentTable = 0;
9,502✔
1344
        } else {
1345
          taosRUnLockLatch(&pTaskInfo->lock);
48,722✔
1346
          qError("no table in table list, %s", id);
48,865✔
1347
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
48,865✔
1348
        }
1349
      }
1350
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
10,638✔
1351

1352
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
10,634✔
1353
             pInfo->pTableScanOp->resultInfo.totalRows);
1354
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
10,634✔
1355

1356
      // start from current accessed position
1357
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1358
      // position, let's find it from the beginning.
1359
      index = tableListFind(pTableListInfo, uid, 0);
10,704✔
1360
      taosRUnLockLatch(&pTaskInfo->lock);
10,704✔
1361

1362
      if (index >= 0) {
10,704✔
1363
        pScanInfo->currentTable = index;
10,704✔
1364
      } else {
1365
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1366
               numOfTables, pScanInfo->currentTable, id);
1367
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1368
      }
1369

1370
      STableKeyInfo keyInfo = {.uid = uid};
10,704✔
1371
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
10,704✔
1372

1373
      // let's start from the next ts that returned to consumer.
1374
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
10,704✔
1375
        pScanBaseInfo->cond.twindows.skey = ts;
×
1376
      } else {
1377
        pScanBaseInfo->cond.twindows.skey = ts + 1;
10,704✔
1378
      }
1379
      pScanInfo->scanTimes = 0;
10,704✔
1380

1381
      if (pScanBaseInfo->dataReader == NULL) {
10,704✔
1382
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
18,682✔
1383
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1384
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
9,341✔
1385
        if (code != TSDB_CODE_SUCCESS) {
9,341✔
1386
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1387
          return code;
×
1388
        }
1389

1390
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
9,341✔
1391
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1392
      } else {
1393
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,363✔
1394
        if (code != TSDB_CODE_SUCCESS) {
1,293✔
1395
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1396
          return code;
×
1397
        }
1398

1399
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
1,293✔
1400
        if (code != TSDB_CODE_SUCCESS) {
1,293✔
1401
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1402
          return code;
×
1403
        }
1404
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
1,293✔
1405
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1406
      }
1407

1408
      // restore the key value
1409
      pScanBaseInfo->cond.twindows.skey = oldSkey;
10,729✔
1410
    } else {
1411
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1412
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1413
    }
1414

1415
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1416
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
127,863✔
1417
      SStreamRawScanInfo* pInfo = pOperator->info;
125,191✔
1418
      SSnapContext*       sContext = pInfo->sContext;
125,257✔
1419
      SOperatorInfo*      p = NULL;
125,257✔
1420

1421
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
125,257✔
1422
      if (code != 0) {
125,257✔
1423
        return code;
×
1424
      }
1425

1426
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
125,257✔
1427

1428
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
125,257✔
1429
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1430
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1431
      }
1432

1433
      SMetaTableInfo mtInfo = {0};
125,144✔
1434
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
125,144✔
1435
      if (code != 0) {
125,069✔
1436
        destroyMetaTableInfo(&mtInfo);
1437
        return code;
×
1438
      }
1439
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
125,069✔
1440
      pInfo->dataReader = NULL;
124,767✔
1441

1442
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
124,993✔
1443
      tableListClear(pTableListInfo);
124,956✔
1444

1445
      if (mtInfo.uid == 0) {
125,144✔
1446
        destroyMetaTableInfo(&mtInfo);
1447
        goto end;  // no data
164✔
1448
      }
1449

1450
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
124,980✔
1451
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
124,858✔
1452
      if (code != TSDB_CODE_SUCCESS) {
124,811✔
1453
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1454
        destroyMetaTableInfo(&mtInfo);
1455
        return code;
×
1456
      }
1457
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
124,811✔
1458
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1459
      } else {
1460
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
124,613✔
1461
      }
1462

1463
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
125,046✔
1464
      if (code != TSDB_CODE_SUCCESS) {
125,046✔
1465
        destroyMetaTableInfo(&mtInfo);
1466
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1467
        return code;
×
1468
      }
1469

1470
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
125,046✔
1471
      if (!pList) {
125,046✔
1472
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1473
        destroyMetaTableInfo(&mtInfo);
1474
        return code;
×
1475
      }
1476
      int32_t size = 0;
125,046✔
1477
      code = tableListGetSize(pTableListInfo, &size);
125,046✔
1478
      if (code != TSDB_CODE_SUCCESS) {
125,093✔
1479
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1480
        destroyMetaTableInfo(&mtInfo);
1481
        return code;
×
1482
      }
1483

1484
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
250,186✔
1485
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
125,093✔
1486
      if (code != TSDB_CODE_SUCCESS) {
124,679✔
1487
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1488
        destroyMetaTableInfo(&mtInfo);
1489
        return code;
×
1490
      }
1491

1492
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
124,679✔
1493
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
125,093✔
1494
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1495
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
124,623✔
1496
      pTaskInfo->streamInfo.schema = mtInfo.schema;
124,754✔
1497
      taosMemoryFreeClear(mtInfo.pExtSchemas);
124,688✔
1498

1499
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
124,688✔
1500
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
2,672✔
1501
      SStreamRawScanInfo* pInfo = pOperator->info;
931✔
1502
      SSnapContext*       sContext = pInfo->sContext;
931✔
1503
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
931✔
1504
      if (code != 0) {
931✔
1505
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1506
        return code;
×
1507
      }
1508
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
931✔
1509
             id);
1510
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
1,675✔
1511
      SStreamRawScanInfo* pInfo = pOperator->info;
1,675✔
1512
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,675✔
1513
      pInfo->dataReader = NULL;
1,675✔
1514
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,675✔
1515
    }
1516
  }
1517

1518
end:
348,181✔
1519
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
348,544✔
1520
  return 0;
348,569✔
1521
}
1522

1523
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
157,615,692✔
1524
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
157,615,692✔
1525
  if (pMsg->info.ahandle == NULL) {
157,624,788✔
1526
    qError("pMsg->info.ahandle is NULL");
×
1527
    return;
×
1528
  }
1529

1530
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
157,607,287✔
1531
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1532

1533
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
157,613,572✔
1534

1535
  if (pMsg->contLen > 0) {
157,621,429✔
1536
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
157,612,775✔
1537
    if (buf.pData == NULL) {
157,603,139✔
1538
      pMsg->code = terrno;
×
1539
    } else {
1540
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
157,603,139✔
1541
    }
1542
  }
1543

1544
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
157,612,405✔
1545
  rpcFreeCont(pMsg->pCont);
157,624,251✔
1546
  destroySendMsgInfo(pSendInfo);
157,596,842✔
1547
}
1548

1549
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1550
  int32_t        code = TSDB_CODE_SUCCESS;
×
1551
  int32_t        lino = 0;
×
1552
  SExecTaskInfo* pTaskInfo = tinfo;
×
1553
  SArray*        plist = NULL;
×
1554

1555
  code = getTableListInfo(pTaskInfo, &plist);
×
1556
  if (code || plist == NULL) {
×
1557
    return NULL;
×
1558
  }
1559

1560
  // only extract table in the first elements
1561
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1562

1563
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1564
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1565

1566
  int32_t numOfTables = 0;
×
1567
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1568
  QUERY_CHECK_CODE(code, lino, _end);
×
1569

1570
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1571
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1572
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1573
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1574
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1575
  }
1576

1577
  taosArrayDestroy(plist);
×
1578

1579
_end:
×
1580
  if (code != TSDB_CODE_SUCCESS) {
×
1581
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1582
    T_LONG_JMP(pTaskInfo->env, code);
×
1583
  }
1584
  return pUidList;
×
1585
}
1586

1587
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,377,934✔
1588
  int32_t        code = TSDB_CODE_SUCCESS;
3,377,934✔
1589
  int32_t        lino = 0;
3,377,934✔
1590
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,377,934✔
1591

1592
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,377,934✔
1593
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1594
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1595

1596
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1597
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1598
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,376,740✔
1599
    STableScanInfo* pScanInfo = pOperator->info;
1,688,670✔
1600

1601
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,689,267✔
1602
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,688,667✔
1603
  } else {
1604
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,689,267✔
1605
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,688,670✔
1606
    }
1607
  }
1608

1609
_end:
×
1610
  if (code != TSDB_CODE_SUCCESS) {
3,377,334✔
1611
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1612
  }
1613
  return code;
3,377,334✔
1614
}
1615

1616
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,689,864✔
1617
  if (pList == NULL) {
1,689,864✔
1618
    return TSDB_CODE_INVALID_PARA;
×
1619
  }
1620

1621
  *pList = NULL;
1,689,864✔
1622
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,689,864✔
1623
  if (pArray == NULL) {
1,689,267✔
1624
    return terrno;
×
1625
  }
1626

1627
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,689,267✔
1628
  if (code == 0) {
1,688,667✔
1629
    *pList = pArray;
1,688,667✔
1630
  } else {
1631
    taosArrayDestroy(pArray);
×
1632
  }
1633
  return code;
1,688,667✔
1634
}
1635

1636
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1637
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1638
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1639
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1640
  }
1641
  return 0;
×
1642
}
1643

1644
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1645
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1646
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1647
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1648
  }
1649
  return 0;
×
1650
}
1651

1652
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1653
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1654

1655
  int32_t code = pTaskInfo->code;
×
1656
  pTaskInfo->code = 0;
×
1657
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1658
}
×
1659

1660
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1661
  int32_t code = 0;
×
1662
  return code;
×
1663
}
1664

1665
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1666
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1667
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1668
  return code;
×
1669
}
1670

1671
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
40,736,743✔
1672
  int32_t code = 0;
40,736,743✔
1673

1674
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
40,736,743✔
1675
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
40,732,834✔
1676

1677
  if (pOper->fpSet.resetStateFn) {
40,733,253✔
1678
    code = pOper->fpSet.resetStateFn(pOper);
40,737,081✔
1679
  }
1680
  pOper->status = OP_NOT_OPENED;
40,725,453✔
1681
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
68,679,320✔
1682
    code = clearStatesForOperator(pOper->pDownstream[i]);
27,943,417✔
1683
  }
1684
  return code;
40,737,832✔
1685
}
1686

1687
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
12,792,567✔
1688
  int32_t        code = 0;
12,792,567✔
1689
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
12,792,567✔
1690
  SOperatorInfo* pOper = pTaskInfo->pRoot;
12,792,567✔
1691
  pTaskInfo->code = TSDB_CODE_SUCCESS;
12,792,567✔
1692
  code = clearStatesForOperator(pOper);
12,792,567✔
1693
  return code;
12,792,567✔
1694
}
1695

1696
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
17,437,243✔
1697
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
17,437,243✔
1698
  int64_t        threadId = taosGetSelfPthreadId();
17,437,243✔
1699
  int64_t        curOwner = 0;
17,437,243✔
1700

1701
  *ppRes = NULL;
17,437,243✔
1702

1703
  // todo extract method
1704
  taosRLockLatch(&pTaskInfo->lock);
17,436,847✔
1705
  bool isKilled = isTaskKilled(pTaskInfo);
17,436,844✔
1706
  if (isKilled) {
17,436,444✔
1707
    // clearStreamBlock(pTaskInfo->pRoot);
1708
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1709

1710
    taosRUnLockLatch(&pTaskInfo->lock);
×
1711
    return pTaskInfo->code;
×
1712
  }
1713

1714
  if (pTaskInfo->owner != 0) {
17,436,444✔
1715
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1716
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1717

1718
    taosRUnLockLatch(&pTaskInfo->lock);
×
1719
    return pTaskInfo->code;
×
1720
  }
1721

1722
  pTaskInfo->owner = threadId;
17,436,444✔
1723
  taosRUnLockLatch(&pTaskInfo->lock);
17,437,243✔
1724

1725
  if (pTaskInfo->cost.start == 0) {
17,436,805✔
1726
    pTaskInfo->cost.start = taosGetTimestampUs();
302,266✔
1727
  }
1728

1729
  // error occurs, record the error code and return to client
1730
  int32_t ret = setjmp(pTaskInfo->env);
17,437,201✔
1731
  if (ret != TSDB_CODE_SUCCESS) {
19,944,731✔
1732
    pTaskInfo->code = ret;
2,508,741✔
1733
    (void)cleanUpUdfs();
2,508,741✔
1734
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,508,741✔
1735
    atomic_store_64(&pTaskInfo->owner, 0);
2,508,741✔
1736
    return pTaskInfo->code;
2,508,741✔
1737
  }
1738

1739
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,435,990✔
1740

1741
  int64_t st = taosGetTimestampUs();
17,437,200✔
1742

1743
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,437,200✔
1744
  if (code) {
14,928,859✔
1745
    pTaskInfo->code = code;
×
1746
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1747
  } else {
1748
    *finished = *ppRes == NULL;
14,928,859✔
1749
    code = blockDataCheck(*ppRes);
14,928,859✔
1750
  }
1751
  if (code) {
14,928,859✔
1752
    pTaskInfo->code = code;
×
1753
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1754
  }
1755

1756
  uint64_t el = (taosGetTimestampUs() - st);
14,928,859✔
1757

1758
  pTaskInfo->cost.elapsedTime += el;
14,928,859✔
1759
  if (NULL == *ppRes) {
14,928,859✔
1760
    *useconds = pTaskInfo->cost.elapsedTime;
9,746,153✔
1761
  }
1762

1763
  (void)cleanUpUdfs();
14,928,437✔
1764

1765
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
14,928,859✔
1766
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
14,928,859✔
1767

1768
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
14,928,859✔
1769
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1770

1771
  atomic_store_64(&pTaskInfo->owner, 0);
14,928,859✔
1772
  return pTaskInfo->code;
14,928,859✔
1773
}
1774

1775
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1776
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1777
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1778
// }
1779

1780
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
306,603✔
1781
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1782
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1783
  int32_t code = 0;                                        
306,603✔
1784
  if (*pTableListInfo != NULL) {
306,603✔
1785
    qDebug("table list already exists, no need to create again");
×
1786
    goto end;
×
1787
  }
1788
  STableListInfo* pList = tableListCreate();
306,995✔
1789
  if (pList == NULL) {
307,755✔
1790
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1791
    code = terrno;
×
1792
    goto end;
×
1793
  }
1794

1795
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
307,755✔
1796
  SReadHandle    pHandle = {.vnode = pVnode};
307,755✔
1797
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
307,755✔
1798

1799
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
307,755✔
1800
  if (code != 0) {
307,755✔
1801
    tableListDestroy(pList);
×
1802
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1803
    goto end;
×
1804
  }
1805
  *pTableListInfo = pList;
307,755✔
1806

1807
end:
307,755✔
1808
  return 0;
307,755✔
1809
}
1810

1811
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
60,312✔
1812
  bool   listAdded = false;
60,312✔
1813
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
60,312✔
1814
  if (code == 0 && !listAdded) {
60,312✔
1815
    int32_t numOfTables = taosArrayGetSize(pUidList);
51,342✔
1816
    for (int i = 0; i < numOfTables; i++) {
102,684✔
1817
      void* tmp = taosArrayGet(pUidList, i);
51,342✔
1818
      if (tmp == NULL) {
51,342✔
1819
        return terrno;
×
1820
      }
1821
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
51,342✔
1822

1823
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
51,342✔
1824
      if (p == NULL) {
51,342✔
1825
        return terrno;
×
1826
      }
1827
    }
1828
  }
1829
  return code;
60,312✔
1830
}
1831

1832
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
59,955✔
1833
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1834
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1835
  int32_t code = TSDB_CODE_SUCCESS;
59,955✔
1836
  STableListInfo* pList = tableListCreate();
59,955✔
1837
  if (pList == NULL) {
60,312✔
1838
    code = terrno;
×
1839
    goto end;
×
1840
  }
1841
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
60,312✔
1842
  if (uidListCopy == NULL) {
59,911✔
1843
    code = terrno;
×
1844
    goto end;
×
1845
  }
1846
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
59,911✔
1847
  SReadHandle    pHandle = {.vnode = pVnode};
60,312✔
1848

1849
  pList->idInfo.suid = suid;
59,911✔
1850
  pList->idInfo.tableType = TD_SUPER_TABLE;
60,312✔
1851
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
59,911✔
1852
  if (code != TSDB_CODE_SUCCESS) {
60,312✔
1853
    goto end;
×
1854
  }                                              
1855
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
60,312✔
1856
  if (code != TSDB_CODE_SUCCESS) {
60,312✔
1857
    goto end;
×
1858
  }
1859
  *tableList = pList->pTableList;
60,312✔
1860
  pList->pTableList = NULL;
60,312✔
1861

1862
  taosArrayClear(uidList);
60,312✔
1863
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
120,624✔
1864
    void* tmp = taosArrayGet(uidListCopy, i);
60,312✔
1865
    if (tmp == NULL) {
60,312✔
1866
      continue;
×
1867
    }
1868
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
60,312✔
1869
    if (slot == NULL) {
60,312✔
1870
      if (taosArrayPush(uidList, tmp) == NULL) {
6,072✔
1871
        code = terrno;
×
1872
        goto end;
×
1873
      }
1874
    }
1875
  }
1876
end:
60,312✔
1877
  taosArrayDestroy(uidListCopy);
60,312✔
1878
  tableListDestroy(pList);
60,312✔
1879
  return code;
60,312✔
1880
}
1881

1882
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1883
//   int32_t index = -1;
1884
//   (void)taosThreadRwlockRdlock(lock);
1885
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1886
//     index = 0;
1887
//     goto end;
1888
//   }
1889
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1890
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1891

1892
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1893
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1894
//       index = i;
1895
//       goto end;
1896
//     }
1897
//   }
1898
// end:
1899
//   (void)taosThreadRwlockUnlock(lock);
1900
//   return index;
1901
// }
1902

1903
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
307,755✔
1904
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
307,755✔
1905
  STableListInfo* pList = pTableListInfo;
307,755✔
1906
  return pList->pTableList;
307,755✔
1907
}
1908

1909
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,634,267✔
1910

1911
void streamDestroyExecTask(qTaskInfo_t tInfo) {
6,193,760✔
1912
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
6,193,760✔
1913
  qDestroyTask(tInfo);
6,193,760✔
1914
}
6,193,760✔
1915

1916
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
821,941✔
1917
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
821,941✔
1918
}
1919

1920
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
864,123✔
1921
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1922
  int32_t      code = 0;
864,123✔
1923
  SNode*       pNode = 0;
864,123✔
1924
  SNodeList*   pList = NULL;
864,123✔
1925
  SExprInfo*   pExprInfo = NULL;
864,123✔
1926
  int32_t      numOfExprs = 1;
864,123✔
1927
  int32_t*     offset = 0;
864,123✔
1928
  STargetNode* pTargetNode = NULL;
864,123✔
1929
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
864,123✔
1930
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
864,123✔
1931

1932
  if (code == 0) {
864,123✔
1933
    pTargetNode->dataBlockId = 0;
864,123✔
1934
    pTargetNode->pExpr = pNode;
864,123✔
1935
    pTargetNode->slotId = 0;
864,123✔
1936
  }
1937
  if (code == 0) {
864,123✔
1938
    code = nodesMakeList(&pList);
864,123✔
1939
  }
1940
  if (code == 0) {
864,123✔
1941
    code = nodesListAppend(pList, (SNode*)pTargetNode);
864,123✔
1942
  }
1943
  if (code == 0) {
864,123✔
1944
    pNode = NULL;
864,123✔
1945
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
864,123✔
1946
  }
1947

1948
  if (code == 0) {
864,123✔
1949
    const char* pVal = NULL;
864,123✔
1950
    int32_t     len = 0;
864,123✔
1951
    SNode*      pSclNode = NULL;
864,123✔
1952
    switch (pExprInfo->pExpr->nodeType) {
864,123✔
1953
      case QUERY_NODE_FUNCTION:
864,123✔
1954
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
864,123✔
1955
        break;
864,123✔
1956
      case QUERY_NODE_OPERATOR:
×
1957
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1958
        break;
×
1959
      default:
×
1960
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1961
        break;
×
1962
    }
1963
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
864,123✔
1964
    SSDataBlock block = {0};
864,123✔
1965
    block.info.rows = 1;
864,123✔
1966
    SSDataBlock* pBlock = &block;
864,123✔
1967
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
864,123✔
1968
    if (tmp == NULL) {
864,123✔
1969
      code = terrno;
×
1970
    }
1971
    if (code == 0) {
864,123✔
1972
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
864,123✔
1973
    }
1974
    taosArrayDestroy(pBlockList);
864,123✔
1975
  }
1976
  nodesDestroyList(pList);
864,123✔
1977
  destroyExprInfo(pExprInfo, numOfExprs);
864,123✔
1978
  taosMemoryFreeClear(pExprInfo);
864,123✔
1979
  return code;
864,123✔
1980
}
1981

1982
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,583,119✔
1983
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,583,119✔
1984
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,583,119✔
1985
  int32_t        code = 0;
4,583,119✔
1986
  SNode*         pNode = NULL;
4,583,119✔
1987
  if (!pForceOutputCols) return 0;
4,583,119✔
1988
  if (!*pRes) {
42,182✔
1989
    code = createDataBlock(pRes);
42,182✔
1990
  }
1991

1992
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
42,182✔
1993
    int32_t idx = 0;
42,182✔
1994
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
168,346✔
1995
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
126,164✔
1996
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
126,164✔
1997
      colInfo.info.precision = pCol->type.precision;
126,164✔
1998
      colInfo.info.scale = pCol->type.scale;
126,164✔
1999
      code = blockDataAppendColInfo(*pRes, &colInfo);
126,164✔
2000
      if (code != 0) break;
126,164✔
2001
    }
2002
  }
2003

2004
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
42,182✔
2005
  if (code != TSDB_CODE_SUCCESS) {
42,182✔
2006
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2007
    return code;
×
2008
  }
2009

2010
  // loop all exprs for force output, execute all exprs
2011
  int32_t idx = 0;
42,182✔
2012
  int32_t rowIdx = (*pRes)->info.rows;
42,182✔
2013
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
42,182✔
2014
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
42,182✔
2015
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
168,346✔
2016
    SScalarParam   dst = {0};
126,164✔
2017
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
126,164✔
2018
    code = nodesStringToNode(pCol->expr, &pNode);
126,164✔
2019
    if (code != 0) break;
126,164✔
2020
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
126,164✔
2021
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
126,164✔
2022
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
83,982✔
2023
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
83,982✔
2024
    } else {
2025
      dst.columnData = pInfo;
42,182✔
2026
      dst.numOfRows = rowIdx;
42,182✔
2027
      dst.colAlloced = false;
42,182✔
2028
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
42,182✔
2029
    }
2030
    ++idx;
126,164✔
2031
    // TODO sclFreeParam(&dst);
2032
    nodesDestroyNode(pNode);
126,164✔
2033
    if (code != 0) break;
126,164✔
2034
  }
2035
  if (code == TSDB_CODE_SUCCESS) {
42,182✔
2036
    (*pRes)->info.rows++;
42,182✔
2037
  }
2038
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
42,182✔
2039
  return code;
42,182✔
2040
}
2041

2042
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
313,691✔
2043
  int32_t      code = 0;
313,691✔
2044
  const char*  pVal = NULL;
313,691✔
2045
  SScalarParam dst = {0};
313,691✔
2046
  int32_t      len = 0;
313,691✔
2047
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
313,691✔
2048
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
313,691✔
2049
  // execute the expr
2050
  switch (pExpr->type) {
313,691✔
2051
    case QUERY_NODE_VALUE: {
×
2052
      SValueNode* pValue = (SValueNode*)pExpr;
×
2053
      int32_t     type = pValue->node.resType.type;
×
2054
      if (!IS_STR_DATA_TYPE(type)) {
×
2055
        qError("invalid sub tb expr with non-str type");
×
2056
        code = TSDB_CODE_INVALID_PARA;
×
2057
        break;
×
2058
      }
2059
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2060
      if (pTmp == NULL) {
×
2061
        qError("invalid sub tb expr with null value");
×
2062
        code = TSDB_CODE_INVALID_PARA;
×
2063
        break;
×
2064
      }
2065
      pVal = varDataVal(pTmp);
×
2066
      len = varDataLen(pTmp);
×
2067
    } break;
×
2068
    case QUERY_NODE_FUNCTION: {
313,691✔
2069
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
313,691✔
2070
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
313,691✔
2071
        qError("invalid sub tb expr with non-str type func");
×
2072
        code = TSDB_CODE_INVALID_PARA;
×
2073
        break;
×
2074
      }
2075
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
313,691✔
2076
      if (!pCol) {
313,691✔
2077
        code = terrno;
×
2078
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2079
        break;
×
2080
      }
2081

2082
      pCol->hasNull = true;
313,691✔
2083
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
313,691✔
2084
      pCol->info.colId = 0;
313,691✔
2085
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
313,691✔
2086
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
313,691✔
2087
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
313,691✔
2088
      code = colInfoDataEnsureCapacity(pCol, 1, true);
313,691✔
2089
      if (code != 0) {
313,691✔
2090
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2091
        taosMemoryFree(pCol);
×
2092
        break;
×
2093
      }
2094
      dst.columnData = pCol;
313,691✔
2095
      dst.numOfRows = 1;
313,691✔
2096
      dst.colAlloced = true;
313,691✔
2097
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
313,691✔
2098
      if (colDataIsNull_var(dst.columnData, 0)) {
313,691✔
2099
        qInfo("invalid sub tb expr with null value");
2,187✔
2100
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
1,785✔
2101
      }
2102
      if (code == 0) {
313,289✔
2103
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
311,504✔
2104
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
311,906✔
2105
      }
2106
    } break;
313,691✔
2107
    default:
×
2108
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2109
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2110
      break;
×
2111
  }
2112
  if (code == 0) {
313,691✔
2113
    if (!pVal || len == 0) {
311,504✔
2114
      qError("tbname generated with no characters which is not allowed");
×
2115
      code = TSDB_CODE_INVALID_PARA;
×
2116
    }
2117
    if(len > TSDB_TABLE_NAME_LEN - 1) {
311,906✔
2118
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
764✔
2119
      len = TSDB_TABLE_NAME_LEN - 1;
764✔
2120
    }
2121

2122
    memcpy(tbname, pVal, len);
311,906✔
2123
    tbname[len] = '\0';  // ensure null terminated
311,906✔
2124
    if (NULL != strchr(tbname, '.')) {
311,906✔
2125
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2126
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2127
    }
2128
  }
2129
  // TODO free dst
2130
  sclFreeParam(&dst);
314,093✔
2131
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
313,691✔
2132
  return code;
313,691✔
2133
}
2134

2135
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
293,716✔
2136
  if (pParam) {
293,716✔
2137
    if (pParam->tbname) {
293,716✔
2138
      taosMemFree(pParam->tbname);
293,716✔
2139
      pParam->tbname = NULL;
293,716✔
2140
    }
2141
    if (pParam->stbname) {
293,716✔
2142
      taosMemFree(pParam->stbname);
293,716✔
2143
      pParam->stbname = NULL;
293,716✔
2144
    }
2145
    if (pParam->dbFName) {
293,716✔
2146
      taosMemFree(pParam->dbFName);
293,716✔
2147
      pParam->dbFName = NULL;
293,716✔
2148
    }
2149
    if (pParam->pFields) {
293,716✔
2150
      taosArrayDestroy(pParam->pFields);
293,716✔
2151
      pParam->pFields = NULL;
293,716✔
2152
    }
2153
    if (pParam->pTagFields) {
293,716✔
2154
      taosArrayDestroy(pParam->pTagFields);
193,056✔
2155
      pParam->pTagFields = NULL;
193,056✔
2156
    }
2157
    taosMemFree(pParam);
293,716✔
2158
  }
2159
}
293,716✔
2160

2161
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
293,716✔
2162
  int32_t code = 0, lino = 0;
293,716✔
2163
  if (ppDst == NULL || pSrc == NULL) {
293,716✔
2164
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2165
  }
2166
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
293,716✔
2167
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
293,716✔
2168

2169
  (*ppDst)->suid = pSrc->suid;
293,716✔
2170
  (*ppDst)->sver = pSrc->sver;
293,716✔
2171
  (*ppDst)->tbType = pSrc->tbType;
293,716✔
2172
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
293,716✔
2173
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
293,716✔
2174

2175
  if (pSrc->stbname) {
293,716✔
2176
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
293,716✔
2177
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
293,716✔
2178
  }
2179

2180
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
293,716✔
2181
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
293,716✔
2182

2183
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
293,316✔
2184

2185
  if (pSrc->pFields && pSrc->pFields->size > 0) {
293,716✔
2186
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
293,716✔
2187
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
293,716✔
2188
  } else {
UNCOV
2189
    (*ppDst)->pFields = NULL;
×
2190
  }
2191
  
2192
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
293,321✔
2193
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
193,056✔
2194
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
193,056✔
2195
  } else {
2196
    (*ppDst)->pTagFields = NULL;
100,660✔
2197
  }
2198

2199
_exit:
293,716✔
2200

2201
  if (code != 0) {
293,716✔
2202
    if (*ppDst) {
×
2203
      destroyStreamInserterParam(*ppDst);
×
2204
      *ppDst = NULL;
×
2205
    }
2206
    
2207
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2208
  }
2209
  return code;
293,321✔
2210
}
2211

2212
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
423✔
2213
  return doDropStreamTable(pMsgCb, pOutput, pReq);
423✔
2214
}
2215

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