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

taosdata / TDengine / #4864

26 Nov 2025 05:46AM UTC coverage: 64.548% (+0.009%) from 64.539%
#4864

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

769 of 945 new or added lines in 33 files covered. (81.38%)

3006 existing lines in 116 files now uncovered.

158227 of 245129 relevant lines covered (64.55%)

111826500.07 hits per line

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

68.32
/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,178✔
44
  gExecInfo.dnode = pDnode;
673,178✔
45
  gExecInfo.getMnode = getMnode;
673,178✔
46
  gExecInfo.getDnodeId = getDnodeId;
673,178✔
47
  return;
673,178✔
48
}
49

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

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

64
static void initRefPool() {
627,571✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
627,571✔
66
  (void)atexit(cleanupRefPool);
627,571✔
67
}
627,571✔
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,716,706✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,716,706✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,717,106✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
3,765,075✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
3,765,075✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
3,765,059✔
156
      if (pScanInfo->base.dataReader != NULL) {
3,764,781✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
52,882✔
158
        if (code) {
52,825✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
10,951,903✔
166
  }
167

168
  return 0;
10,316,637✔
169
}
170

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

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

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,356,758✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,242,942✔
249
    if (pSlotDesc->output) {
1,242,942✔
250
      ++(*numOfCols);
1,242,858✔
251
    }
252
  }
253

254
  return pTaskInfo;
113,816✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
670,157✔
258
  if (streamInserterParam == NULL) {
670,157✔
259
    return TSDB_CODE_SUCCESS;
377,484✔
260
  }
261

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

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

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

278
  return TSDB_CODE_SUCCESS;
292,673✔
279
}
280

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

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

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

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

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

336
_error:
144,454✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,886,665✔
348
  if (pInfo == NULL) {
5,886,665✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,886,665✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,886,665✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,886,244✔
354
    return false;
4,764✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,881,507✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,636,980✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,518,881✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,881,901✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
244,921✔
387
    setReadHandle(handle, pScanBaseInfo);
244,921✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,636,980✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,146,356✔
390
    setReadHandle(handle, pScanBaseInfo);
5,146,356✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
670,157✔
405

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

421
  return code;
669,760✔
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,
57,660✔
431
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
432
  int32_t code = TSDB_CODE_SUCCESS;
57,660✔
433
  int32_t lino = 0;
57,660✔
434
  int8_t  locked = 0;
57,660✔
435
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
57,660✔
436

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
57,660✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
57,660✔
449

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

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

459
  locked = 1;
57,660✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
116,184✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
58,524✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
58,524✔
463

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

470
    tDecoderClear(&mr.coder);
58,524✔
471

472
    if (mr.me.type == TSDB_SUPER_TABLE) {
58,524✔
473
      continue;
×
474
    } else {
475
      if (type == TSDB_SUPER_TABLE) {
58,524✔
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) {
58,524✔
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};
58,524✔
490
    if (pScanInfo->pTagCond != NULL) {
58,524✔
491
      // tb_uid_t id = mr.me.uid;
492
      item.check = 1;
50,077✔
493
    }
494
    if (taosArrayPush(tUid, &item) == NULL) {
58,524✔
495
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
496
    }
497
  }
498

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

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

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

517
      if (!qualified) {
50,077✔
518
        continue;
25,050✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
57,660✔
529

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

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

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

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

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

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

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

579
    bool   assignUid = false;
57,660✔
580
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
57,660✔
581
    char*  keyBuf = NULL;
57,660✔
582
    if (bufLen > 0) {
57,660✔
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;
57,660✔
592
    taosWLockLatch(&pTaskInfo->lock);
57,660✔
593

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

604
      if (bufLen > 0) {
33,474✔
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);
33,474✔
620
      if (code != TSDB_CODE_SUCCESS) {
33,474✔
621
        taosMemoryFree(keyBuf);
×
622
        taosArrayDestroy(qa);
×
623
        taosWUnLockLatch(&pTaskInfo->lock);
×
624
        return code;
×
625
      }
626
    }
627

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

633
    taosArrayDestroy(qa);
57,660✔
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);
608✔
636
    taosWLockLatch(&pTaskInfo->lock);
608✔
637
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
608✔
638
    taosWUnLockLatch(&pTaskInfo->lock);
608✔
639
  }
640

641
  return code;
58,268✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
287,627,204✔
UNCOV
650
    return TSDB_CODE_INVALID_PARA;
×
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
287,644,555✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
287,644,555✔
655
    return TSDB_CODE_SUCCESS;
169,390,202✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
118,244,871✔
665
  *tversion = pSchemaInfo->tversion;
118,266,150✔
666
  *rversion = pSchemaInfo->rversion;
118,257,990✔
667
  if (pSchemaInfo->dbname) {
118,250,462✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
118,256,742✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
118,277,066✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
118,266,650✔
674
  } else {
675
    tableName[0] = 0;
1,147✔
676
  }
677

678
  *tbGet = true;
118,275,078✔
679

680
  return TSDB_CODE_SUCCESS;
118,269,128✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
169,378,474✔
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,421,299✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
37,421,299✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
37,421,299✔
695
}
37,421,299✔
696

697
int32_t qExecutorInit(void) {
4,615,843✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,615,843✔
699
  return TSDB_CODE_SUCCESS;
4,616,567✔
700
}
701

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

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

710
  readHandle->uid = 0;
171,288,162✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
171,303,719✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
171,145,170✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
100,818✔
714
    goto _error;
26,985✔
715
  }
716

717
  if (handle) {
171,085,471✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
170,981,324✔
719
    void*           pSinkManager = NULL;
170,996,736✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
170,967,979✔
721
    if (code != TSDB_CODE_SUCCESS) {
170,939,179✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
170,939,179✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
171,023,060✔
728
    if (code != TSDB_CODE_SUCCESS) {
170,919,557✔
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;
170,919,557✔
735
    if (readHandle->localExec) {
171,048,835✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
77,912✔
737
      if (code != TSDB_CODE_SUCCESS) {
77,912✔
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,
170,962,955✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
171,096,913✔
748
    if (code) {
170,918,138✔
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,
171,089,377✔
754
         tstrerror(code));
755

756
_error:
8,702,494✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
171,285,888✔
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,819,337✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
230,819,337✔
769
  int32_t        lino = 0;
230,819,337✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
230,819,337✔
771
  int64_t        threadId = taosGetSelfPthreadId();
230,819,337✔
772

773
  if (pLocal) {
230,812,210✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
224,522,986✔
775
  }
776

777
  taosArrayClear(pResList);
230,792,878✔
778

779
  int64_t curOwner = 0;
230,803,583✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
230,803,583✔
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,801,149✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
167,351,650✔
788
  }
789

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

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
230,812,864✔
798
  if (ret != TSDB_CODE_SUCCESS) {
230,958,777✔
799
    pTaskInfo->code = ret;
178,253✔
800
    (void)cleanUpUdfs();
178,253✔
801

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

805
    return pTaskInfo->code;
178,253✔
806
  }
807

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

810
  int32_t      current = 0;
230,783,305✔
811
  SSDataBlock* pRes = NULL;
230,783,305✔
812
  int64_t      st = taosGetTimestampUs();
230,819,540✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
230,819,540✔
815
    pTaskInfo->paramSet = true;
37,421,299✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,421,299✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
193,390,503✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
230,645,087✔
822
  code = blockDataCheck(pRes);
230,645,087✔
823
  QUERY_CHECK_CODE(code, lino, _end);
230,641,629✔
824

825
  if (pRes == NULL) {
230,641,629✔
826
    st = taosGetTimestampUs();
42,745,697✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
230,642,733✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
230,645,836✔
831
    rowsThreshold = 4096;
229,941,636✔
832
  }
833

834
  int32_t blockIndex = 0;
230,640,719✔
835
  while (pRes != NULL) {
617,267,934✔
836
    SSDataBlock* p = NULL;
439,198,046✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
439,179,230✔
838
      SSDataBlock* p1 = NULL;
303,883,266✔
839
      code = createOneDataBlock(pRes, true, &p1);
303,885,125✔
840
      QUERY_CHECK_CODE(code, lino, _end);
303,879,701✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
303,879,701✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
303,876,406✔
844
      p = p1;
303,876,406✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
135,324,392✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
135,322,892✔
848

849
      p = *(SSDataBlock**)tmp;
135,322,892✔
850
      code = copyDataBlock(p, pRes);
135,322,296✔
851
      QUERY_CHECK_CODE(code, lino, _end);
135,323,986✔
852
    }
853

854
    blockIndex += 1;
439,201,094✔
855

856
    current += p->info.rows;
439,201,094✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
439,202,348✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
439,202,027✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
439,202,027✔
861

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

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
386,623,280✔
867
    QUERY_CHECK_CODE(code, lino, _end);
386,617,099✔
868
    code = blockDataCheck(pRes);
386,617,099✔
869
    QUERY_CHECK_CODE(code, lino, _end);
386,629,369✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
230,648,136✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
700,821✔
874
  }
875

876
  *hasMore = (pRes != NULL);
230,647,125✔
877
  uint64_t el = (taosGetTimestampUs() - st);
230,636,705✔
878

879
  pTaskInfo->cost.elapsedTime += el;
230,636,705✔
880
  if (NULL == pRes) {
230,634,549✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
178,056,846✔
882
  }
883

884
_end:
230,510,335✔
885
  (void)cleanUpUdfs();
230,635,628✔
886

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

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

897
  return pTaskInfo->code;
230,654,477✔
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) {
33,202,016✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
33,202,016✔
916
  int64_t        threadId = taosGetSelfPthreadId();
33,202,016✔
917
  int64_t        curOwner = 0;
33,201,375✔
918

919
  *pRes = NULL;
33,201,375✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
33,202,107✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
33,202,839✔
924
  if (isKilled) {
33,202,214✔
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) {
33,202,214✔
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;
33,202,214✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
33,202,214✔
941

942
  if (pTaskInfo->cost.start == 0) {
33,202,946✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
91,384✔
944
  }
945

946
  // error occurs, record the error code and return to client
947
  int32_t ret = setjmp(pTaskInfo->env);
33,202,946✔
948
  if (ret != TSDB_CODE_SUCCESS) {
33,201,849✔
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));
33,201,849✔
957

958
  int64_t st = taosGetTimestampUs();
33,202,839✔
959
  int32_t code = TSDB_CODE_SUCCESS;
33,202,839✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
33,202,839✔
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);
33,202,625✔
965
  }
966
  if (code) {
33,197,450✔
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);
33,197,450✔
972
  if (code) {
33,202,217✔
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);
33,191,600✔
978

979
  pTaskInfo->cost.elapsedTime += el;
33,191,600✔
980
  if (NULL == *pRes) {
33,201,654✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,440,069✔
982
  }
983

984
  (void)cleanUpUdfs();
33,201,810✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
33,200,952✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
33,203,087✔
988

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
33,202,124✔
993
  return pTaskInfo->code;
33,202,946✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
57,767,936✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,767,936✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
57,768,627✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,768,627✔
1000

1001
  if (!tmp) {
57,768,369✔
1002
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1003
    return terrno;
×
1004
  }
1005
  return TSDB_CODE_SUCCESS;
57,768,369✔
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) {
25,955✔
1031
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
25,955✔
1032

1033
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
25,955✔
1034
  for (int32_t i = 0; i < num; ++i) {
27,954✔
1035
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
1,999✔
1036
    if (!pStop) {
1,999✔
1037
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1038
      continue;
×
1039
    }
1040
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
1,999✔
1041
    if (pExchangeInfo) {
1,999✔
1042
      int32_t code = tsem_post(&pExchangeInfo->ready);
1,999✔
1043
      if (code != TSDB_CODE_SUCCESS) {
1,999✔
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);
1,999✔
1047
      }
1048
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
1,999✔
1049
      if (code != TSDB_CODE_SUCCESS) {
1,999✔
1050
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1051
      }
1052
    }
1053
  }
1054

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
25,955✔
1067
  qStopTaskOperators(pTaskInfo);
25,955✔
1068

1069
  return TSDB_CODE_SUCCESS;
25,955✔
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) {
171,969,706✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
171,969,706✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
171,971,101✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
171,969,060✔
1130
  if (pSummary->pRecoder != NULL) {
171,967,842✔
1131
    qDebug(
120,575,551✔
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,391,033✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
171,968,924✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
186,213,693✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
186,213,693✔
1146
  if (pTaskInfo == NULL) {
186,213,693✔
1147
    return;
14,252,534✔
1148
  }
1149

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

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
171,970,375✔
1157
  doDestroyTask(pTaskInfo);
171,968,563✔
1158
}
1159

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

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

1169
  while (1) {
111,165✔
1170
    uint16_t type = pOperator->operatorType;
224,981✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
224,981✔
1172
      *scanner = pOperator->info;
113,816✔
1173
      break;
113,816✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
111,165✔
1176
    }
1177
  }
1178
}
113,816✔
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) {
113,816✔
1194
  SStreamScanInfo* pInfo = scanner;
113,816✔
1195
  return (void*)pInfo->tqReader;
113,816✔
1196
}
1197

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

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

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

1213
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
3,851,207✔
1214
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,851,207✔
1215
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
3,851,207✔
1216
  return 0;
3,851,207✔
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) {
130,402✔
1225
  memset(pCond, 0, sizeof(SQueryTableDataCond));
130,402✔
1226
  pCond->order = TSDB_ORDER_ASC;
130,402✔
1227
  pCond->numOfCols = pMtInfo->schema->nCols;
130,739✔
1228
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
130,904✔
1229
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
130,524✔
1230
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
130,689✔
1231
    taosMemoryFreeClear(pCond->colList);
265✔
1232
    taosMemoryFreeClear(pCond->pSlotList);
×
1233
    return terrno;
×
1234
  }
1235

1236
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
130,539✔
1237
  pCond->suid = pMtInfo->suid;
130,754✔
1238
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
130,689✔
1239
  pCond->startVersion = -1;
130,689✔
1240
  pCond->endVersion = sContext->snapVersion;
130,674✔
1241

1242
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
834,969✔
1243
    SColumnInfo* pColInfo = &pCond->colList[i];
704,243✔
1244
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
704,243✔
1245
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
704,228✔
1246
    if (pMtInfo->pExtSchemas != NULL) {
704,078✔
1247
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
6,602✔
1248
    }
1249
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
704,308✔
1250
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
704,395✔
1251

1252
    pCond->pSlotList[i] = i;
704,575✔
1253
  }
1254

1255
  return TSDB_CODE_SUCCESS;
130,904✔
1256
}
1257

1258
void qStreamSetOpen(qTaskInfo_t tinfo) {
32,932,465✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
32,932,465✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
32,932,465✔
1261
  pOperator->status = OP_NOT_OPENED;
32,935,223✔
1262
}
32,934,893✔
1263

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

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

1274
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
3,907,092✔
1275
  const char*    id = GET_TASKID(pTaskInfo);
3,906,953✔
1276

1277
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
3,906,492✔
1278
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,650,289✔
1279
    if (pOperator == NULL || code != 0) {
3,650,070✔
1280
      return code;
6✔
1281
    }
1282

1283
    SStreamScanInfo* pInfo = pOperator->info;
3,650,198✔
1284
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,650,286✔
1285
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,649,986✔
1286
    walReaderVerifyOffset(pWalReader, pOffset);
3,650,559✔
1287
  }
1288
  // if pOffset equal to current offset, means continue consume
1289
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
3,906,402✔
1290
    return 0;
3,498,136✔
1291
  }
1292

1293
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
407,984✔
1294
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
274,264✔
1295
    if (pOperator == NULL || code != 0) {
274,188✔
1296
      return code;
×
1297
    }
1298

1299
    SStreamScanInfo* pInfo = pOperator->info;
274,249✔
1300
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
274,051✔
1301
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
274,039✔
1302
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
273,843✔
1303

1304
    if (pOffset->type == TMQ_OFFSET__LOG) {
274,111✔
1305
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
214,602✔
1306
      pScanBaseInfo->dataReader = NULL;
214,158✔
1307

1308
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
214,158✔
1309
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
214,602✔
1310
      walReaderVerifyOffset(pWalReader, pOffset);
214,422✔
1311
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
214,602✔
1312
      if (code < 0) {
214,602✔
1313
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
6,781✔
1314
        return code;
6,781✔
1315
      }
1316
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
59,563✔
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,883✔
1320
      int64_t ts = pOffset->ts;
59,774✔
1321
      int32_t index = 0;
59,252✔
1322

1323
      // this value may be changed if new tables are created
1324
      taosRLockLatch(&pTaskInfo->lock);
59,252✔
1325
      int32_t numOfTables = 0;
59,610✔
1326
      code = tableListGetSize(pTableListInfo, &numOfTables);
59,610✔
1327
      if (code != TSDB_CODE_SUCCESS) {
59,088✔
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,088✔
1334
        if (numOfTables != 0) {
58,264✔
1335
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
9,472✔
1336
          if (!tmp) {
9,472✔
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,472✔
1342
          ts = INT64_MIN;
9,472✔
1343
          pScanInfo->currentTable = 0;
9,472✔
1344
        } else {
1345
          taosRUnLockLatch(&pTaskInfo->lock);
48,792✔
1346
          qError("no table in table list, %s", id);
49,067✔
1347
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
49,127✔
1348
        }
1349
      }
1350
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
10,296✔
1351

1352
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
10,756✔
1353
             pInfo->pTableScanOp->resultInfo.totalRows);
1354
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
10,756✔
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,756✔
1360
      taosRUnLockLatch(&pTaskInfo->lock);
10,756✔
1361

1362
      if (index >= 0) {
10,756✔
1363
        pScanInfo->currentTable = index;
10,756✔
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,756✔
1371
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
10,756✔
1372

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

1381
      if (pScanBaseInfo->dataReader == NULL) {
10,756✔
1382
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
18,628✔
1383
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1384
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
9,314✔
1385
        if (code != TSDB_CODE_SUCCESS) {
9,314✔
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,314✔
1391
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1392
      } else {
1393
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,442✔
1394
        if (code != TSDB_CODE_SUCCESS) {
1,442✔
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,442✔
1400
        if (code != TSDB_CODE_SUCCESS) {
1,442✔
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,442✔
1405
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1406
      }
1407

1408
      // restore the key value
1409
      pScanBaseInfo->cond.twindows.skey = oldSkey;
10,874✔
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) {
133,720✔
1417
      SStreamRawScanInfo* pInfo = pOperator->info;
131,065✔
1418
      SSnapContext*       sContext = pInfo->sContext;
131,065✔
1419
      SOperatorInfo*      p = NULL;
131,065✔
1420

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

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

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

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

1442
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
130,900✔
1443
      tableListClear(pTableListInfo);
130,965✔
1444

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

1450
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
130,904✔
1451
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
130,639✔
1452
      if (code != TSDB_CODE_SUCCESS) {
130,639✔
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)) {
130,639✔
1458
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1459
      } else {
1460
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
130,524✔
1461
      }
1462

1463
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
130,702✔
1464
      if (code != TSDB_CODE_SUCCESS) {
130,904✔
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);
130,904✔
1471
      if (!pList) {
130,904✔
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;
130,904✔
1477
      code = tableListGetSize(pTableListInfo, &size);
130,904✔
1478
      if (code != TSDB_CODE_SUCCESS) {
130,904✔
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,
261,808✔
1485
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
130,904✔
1486
      if (code != TSDB_CODE_SUCCESS) {
130,524✔
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);
130,524✔
1493
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
130,804✔
1494
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1495
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
130,539✔
1496
      pTaskInfo->streamInfo.schema = mtInfo.schema;
130,704✔
1497
      taosMemoryFreeClear(mtInfo.pExtSchemas);
130,654✔
1498

1499
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
130,654✔
1500
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
2,605✔
1501
      SStreamRawScanInfo* pInfo = pOperator->info;
943✔
1502
      SSnapContext*       sContext = pInfo->sContext;
943✔
1503
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
943✔
1504
      if (code != 0) {
943✔
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,
943✔
1509
             id);
1510
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
1,712✔
1511
      SStreamRawScanInfo* pInfo = pOperator->info;
1,712✔
1512
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,712✔
1513
      pInfo->dataReader = NULL;
1,712✔
1514
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,712✔
1515
    }
1516
  }
1517

1518
end:
351,915✔
1519
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
352,096✔
1520
  return 0;
352,297✔
1521
}
1522

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

1530
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
157,726,754✔
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,734,714✔
1534

1535
  if (pMsg->contLen > 0) {
157,740,258✔
1536
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
157,729,740✔
1537
    if (buf.pData == NULL) {
157,719,404✔
1538
      pMsg->code = terrno;
×
1539
    } else {
1540
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
157,719,404✔
1541
    }
1542
  }
1543

1544
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
157,733,041✔
1545
  rpcFreeCont(pMsg->pCont);
157,747,662✔
1546
  destroySendMsgInfo(pSendInfo);
157,712,519✔
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,384,418✔
1588
  int32_t        code = TSDB_CODE_SUCCESS;
3,384,418✔
1589
  int32_t        lino = 0;
3,384,418✔
1590
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,384,418✔
1591

1592
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,384,418✔
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,383,059✔
1599
    STableScanInfo* pScanInfo = pOperator->info;
1,692,209✔
1600

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

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

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

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

1627
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,692,209✔
1628
  if (code == 0) {
1,692,054✔
1629
    *pList = pArray;
1,692,054✔
1630
  } else {
1631
    taosArrayDestroy(pArray);
×
1632
  }
1633
  return code;
1,692,054✔
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) {
51,033,909✔
1672
  int32_t code = 0;
51,033,909✔
1673

1674
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
51,033,909✔
1675
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
51,029,446✔
1676

1677
  if (pOper->fpSet.resetStateFn) {
51,030,234✔
1678
    code = pOper->fpSet.resetStateFn(pOper);
51,032,735✔
1679
  }
1680
  pOper->status = OP_NOT_OPENED;
51,028,867✔
1681
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
87,933,749✔
1682
    code = clearStatesForOperator(pOper->pDownstream[i]);
36,894,973✔
1683
  }
1684
  return code;
51,042,229✔
1685
}
1686

1687
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
14,142,329✔
1688
  int32_t        code = 0;
14,142,329✔
1689
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
14,142,329✔
1690
  SOperatorInfo* pOper = pTaskInfo->pRoot;
14,142,329✔
1691
  pTaskInfo->code = TSDB_CODE_SUCCESS;
14,142,329✔
1692
  code = clearStatesForOperator(pOper);
14,141,935✔
1693
  return code;
14,141,916✔
1694
}
1695

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

1701
  *ppRes = NULL;
18,448,575✔
1702

1703
  // todo extract method
1704
  taosRLockLatch(&pTaskInfo->lock);
18,448,933✔
1705
  bool isKilled = isTaskKilled(pTaskInfo);
18,449,327✔
1706
  if (isKilled) {
18,449,727✔
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) {
18,449,727✔
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;
18,448,933✔
1723
  taosRUnLockLatch(&pTaskInfo->lock);
18,449,333✔
1724

1725
  if (pTaskInfo->cost.start == 0) {
18,449,728✔
1726
    pTaskInfo->cost.start = taosGetTimestampUs();
301,229✔
1727
  }
1728

1729
  // error occurs, record the error code and return to client
1730
  int32_t ret = setjmp(pTaskInfo->env);
18,449,728✔
1731
  if (ret != TSDB_CODE_SUCCESS) {
22,864,925✔
1732
    pTaskInfo->code = ret;
4,417,979✔
1733
    (void)cleanUpUdfs();
4,418,373✔
1734
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
4,418,373✔
1735
    atomic_store_64(&pTaskInfo->owner, 0);
4,418,373✔
1736
    return pTaskInfo->code;
4,418,373✔
1737
  }
1738

1739
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
18,446,946✔
1740

1741
  int64_t st = taosGetTimestampUs();
18,449,727✔
1742

1743
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
18,449,727✔
1744
  if (code) {
14,031,755✔
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,031,755✔
1749
    code = blockDataCheck(*ppRes);
14,031,755✔
1750
  }
1751
  if (code) {
14,031,325✔
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,031,755✔
1757

1758
  pTaskInfo->cost.elapsedTime += el;
14,031,755✔
1759
  if (NULL == *ppRes) {
14,031,755✔
1760
    *useconds = pTaskInfo->cost.elapsedTime;
9,183,787✔
1761
  }
1762

1763
  (void)cleanUpUdfs();
14,031,755✔
1764

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

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

1771
  atomic_store_64(&pTaskInfo->owner, 0);
14,031,755✔
1772
  return pTaskInfo->code;
14,031,755✔
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,
302,689✔
1781
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1782
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1783
  int32_t code = 0;                                        
302,689✔
1784
  if (*pTableListInfo != NULL) {
302,689✔
1785
    qDebug("table list already exists, no need to create again");
×
1786
    goto end;
×
1787
  }
1788
  STableListInfo* pList = tableListCreate();
304,200✔
1789
  if (pList == NULL) {
304,200✔
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};
304,200✔
1796
  SReadHandle    pHandle = {.vnode = pVnode};
304,200✔
1797
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
304,200✔
1798

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

1807
end:
304,200✔
1808
  return 0;
304,200✔
1809
}
1810

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

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

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

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

1862
  taosArrayClear(uidList);
60,368✔
1863
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
120,736✔
1864
    void* tmp = taosArrayGet(uidListCopy, i);
60,368✔
1865
    if (tmp == NULL) {
60,368✔
1866
      continue;
×
1867
    }
1868
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
60,368✔
1869
    if (slot == NULL) {
60,368✔
1870
      if (taosArrayPush(uidList, tmp) == NULL) {
6,111✔
1871
        code = terrno;
×
1872
        goto end;
×
1873
      }
1874
    }
1875
  }
1876
end:
60,368✔
1877
  taosArrayDestroy(uidListCopy);
60,368✔
1878
  tableListDestroy(pList);
60,368✔
1879
  return code;
60,368✔
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); }
304,200✔
1904
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
304,200✔
1905
  STableListInfo* pList = pTableListInfo;
304,200✔
1906
  return pList->pTableList;
304,200✔
1907
}
1908

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

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

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

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

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

1948
  if (code == 0) {
832,240✔
1949
    const char* pVal = NULL;
832,240✔
1950
    int32_t     len = 0;
832,240✔
1951
    SNode*      pSclNode = NULL;
832,240✔
1952
    switch (pExprInfo->pExpr->nodeType) {
832,240✔
1953
      case QUERY_NODE_FUNCTION:
832,240✔
1954
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
832,240✔
1955
        break;
832,240✔
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);
832,240✔
1964
    SSDataBlock block = {0};
832,240✔
1965
    block.info.rows = 1;
832,240✔
1966
    SSDataBlock* pBlock = &block;
832,240✔
1967
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
832,240✔
1968
    if (tmp == NULL) {
832,240✔
1969
      code = terrno;
×
1970
    }
1971
    if (code == 0) {
832,240✔
1972
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
832,240✔
1973
    }
1974
    taosArrayDestroy(pBlockList);
832,240✔
1975
  }
1976
  nodesDestroyList(pList);
832,240✔
1977
  destroyExprInfo(pExprInfo, numOfExprs);
831,835✔
1978
  taosMemoryFreeClear(pExprInfo);
832,240✔
1979
  return code;
832,240✔
1980
}
1981

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

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

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

2042
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
302,028✔
2043
  int32_t      code = 0;
302,028✔
2044
  const char*  pVal = NULL;
302,028✔
2045
  SScalarParam dst = {0};
302,028✔
2046
  int32_t      len = 0;
302,028✔
2047
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
302,028✔
2048
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
302,028✔
2049
  // execute the expr
2050
  switch (pExpr->type) {
302,028✔
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: {
302,028✔
2069
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
302,028✔
2070
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
302,028✔
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));
302,028✔
2076
      if (!pCol) {
302,028✔
2077
        code = terrno;
×
2078
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2079
        break;
×
2080
      }
2081

2082
      pCol->hasNull = true;
302,028✔
2083
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
302,028✔
2084
      pCol->info.colId = 0;
302,028✔
2085
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
302,028✔
2086
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
302,028✔
2087
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
302,028✔
2088
      code = colInfoDataEnsureCapacity(pCol, 1, true);
302,028✔
2089
      if (code != 0) {
302,028✔
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;
302,028✔
2095
      dst.numOfRows = 1;
302,028✔
2096
      dst.colAlloced = true;
302,028✔
2097
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
302,028✔
2098
      if (colDataIsNull_var(dst.columnData, 0)) {
302,028✔
2099
        qInfo("invalid sub tb expr with null value");
1,785✔
2100
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
1,785✔
2101
      }
2102
      if (code == 0) {
302,028✔
2103
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
300,243✔
2104
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
300,243✔
2105
      }
2106
    } break;
301,623✔
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) {
301,623✔
2113
    if (!pVal || len == 0) {
300,243✔
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) {
300,243✔
2118
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
762✔
2119
      len = TSDB_TABLE_NAME_LEN - 1;
762✔
2120
    }
2121

2122
    memcpy(tbname, pVal, len);
300,243✔
2123
    tbname[len] = '\0';  // ensure null terminated
300,243✔
2124
    if (NULL != strchr(tbname, '.')) {
299,838✔
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);
301,218✔
2131
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
301,623✔
2132
  return code;
301,623✔
2133
}
2134

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

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

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

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

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

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

2185
  if (pSrc->pFields && pSrc->pFields->size > 0) {
292,281✔
2186
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
292,673✔
2187
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
292,673✔
2188
  } else {
2189
    (*ppDst)->pFields = NULL;
392✔
2190
  }
2191
  
2192
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
292,673✔
2193
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
192,538✔
2194
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
192,538✔
2195
  } else {
2196
    (*ppDst)->pTagFields = NULL;
100,135✔
2197
  }
2198

2199
_exit:
292,673✔
2200

2201
  if (code != 0) {
292,673✔
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;
292,673✔
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