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

taosdata / TDengine / #4868

26 Nov 2025 05:46AM UTC coverage: 64.629% (+0.2%) from 64.473%
#4868

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

770 of 945 new or added lines in 33 files covered. (81.48%)

3010 existing lines in 120 files now uncovered.

158425 of 245129 relevant lines covered (64.63%)

113048242.66 hits per line

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

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

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

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

64
static void initRefPool() {
639,523✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
639,523✔
66
  (void)atexit(cleanupRefPool);
639,523✔
67
}
639,523✔
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) {
15,773,128✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,773,128✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,773,912✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,270,336✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,270,336✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,270,172✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,269,536✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,820✔
158
        if (code) {
57,749✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,503,403✔
166
  }
167

168
  return 0;
10,869,520✔
169
}
170

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

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,869,354✔
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,
144,252✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
144,252✔
209
    SExecTaskInfo* pTaskInfo = NULL;
23,843✔
210

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

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

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

227
  SSubplan* pPlan = NULL;
120,409✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
121,250✔
229
  if (code != TSDB_CODE_SUCCESS) {
121,321✔
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,410,505✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,289,184✔
249
    if (pSlotDesc->output) {
1,289,184✔
250
      ++(*numOfCols);
1,289,076✔
251
    }
252
  }
253

254
  return pTaskInfo;
121,321✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
651,381✔
258
  if (streamInserterParam == NULL) {
651,381✔
259
    return TSDB_CODE_SUCCESS;
356,905✔
260
  }
261

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

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

278
  return TSDB_CODE_SUCCESS;
294,476✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
294,476✔
329
    if (code) {
294,335✔
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,
650,962✔
334
         tstrerror(code));
335

336
_error:
145,340✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,953,034✔
348
  if (pInfo == NULL) {
5,953,034✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,953,034✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,953,034✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,953,034✔
354
    return false;
4,800✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,948,234✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,688,301✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,636,535✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,948,234✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
259,933✔
387
    setReadHandle(handle, pScanBaseInfo);
259,933✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,688,301✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,200,849✔
390
    setReadHandle(handle, pScanBaseInfo);
5,200,849✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
651,381✔
405

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

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

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
67,402✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
67,402✔
449

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

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

459
  locked = 1;
67,402✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
135,839✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
68,437✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
68,437✔
463

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

470
    tDecoderClear(&mr.coder);
68,437✔
471

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

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

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

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

517
      if (!qualified) {
59,510✔
518
        continue;
29,783✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
67,402✔
529

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

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

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

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

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

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

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

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

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

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

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

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

641
  return code;
67,625✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
297,862,802✔
UNCOV
650
    return TSDB_CODE_INVALID_PARA;
×
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
297,870,296✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
297,870,296✔
655
    return TSDB_CODE_SUCCESS;
177,060,837✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
120,784,818✔
665
  *tversion = pSchemaInfo->tversion;
120,804,903✔
666
  *rversion = pSchemaInfo->rversion;
120,799,371✔
667
  if (pSchemaInfo->dbname) {
120,812,113✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
120,812,113✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
120,820,112✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
120,813,842✔
674
  } else {
675
    tableName[0] = 0;
416✔
676
  }
677

678
  *tbGet = true;
120,820,889✔
679

680
  return TSDB_CODE_SUCCESS;
120,814,443✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
177,057,964✔
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) {
47,696,284✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
47,696,284✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
47,696,284✔
695
}
47,696,284✔
696

697
int32_t qExecutorInit(void) {
4,698,742✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,698,742✔
699
  return TSDB_CODE_SUCCESS;
4,699,085✔
700
}
701

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

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

710
  readHandle->uid = 0;
178,953,696✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
178,974,909✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
178,855,453✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
105,254✔
714
    goto _error;
27,419✔
715
  }
716

717
  if (handle) {
178,781,946✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
178,710,511✔
719
    void*           pSinkManager = NULL;
178,740,176✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
178,558,531✔
721
    if (code != TSDB_CODE_SUCCESS) {
178,707,339✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
178,707,339✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
178,699,599✔
728
    if (code != TSDB_CODE_SUCCESS) {
178,556,282✔
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;
178,556,282✔
735
    if (readHandle->localExec) {
178,698,385✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
66,813✔
737
      if (code != TSDB_CODE_SUCCESS) {
66,813✔
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,
178,593,457✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
178,754,351✔
748
    if (code) {
178,575,480✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
629✔
750
    }
751
  }
752

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

756
_error:
9,025,317✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
178,969,291✔
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,
247,616,749✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
247,616,749✔
769
  int32_t        lino = 0;
247,616,749✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
247,616,749✔
771
  int64_t        threadId = taosGetSelfPthreadId();
247,616,749✔
772

773
  if (pLocal) {
247,611,458✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
241,268,806✔
775
  }
776

777
  taosArrayClear(pResList);
247,593,554✔
778

779
  int64_t curOwner = 0;
247,600,961✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
247,600,961✔
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) {
247,600,368✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
174,000,063✔
788
  }
789

790
  if (isTaskKilled(pTaskInfo)) {
247,617,772✔
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);
247,604,621✔
798
  if (ret != TSDB_CODE_SUCCESS) {
247,763,173✔
799
    pTaskInfo->code = ret;
180,503✔
800
    (void)cleanUpUdfs();
180,503✔
801

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

805
    return pTaskInfo->code;
180,503✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
247,582,670✔
809

810
  int32_t      current = 0;
247,586,282✔
811
  SSDataBlock* pRes = NULL;
247,586,282✔
812
  int64_t      st = taosGetTimestampUs();
247,618,195✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
247,618,195✔
815
    pTaskInfo->paramSet = true;
47,696,284✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
47,696,284✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
199,915,525✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
247,430,261✔
822
  code = blockDataCheck(pRes);
247,430,261✔
823
  QUERY_CHECK_CODE(code, lino, _end);
247,434,751✔
824

825
  if (pRes == NULL) {
247,434,751✔
826
    st = taosGetTimestampUs();
47,796,630✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
247,436,333✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
247,437,824✔
831
    rowsThreshold = 4096;
246,617,091✔
832
  }
833

834
  int32_t blockIndex = 0;
247,435,356✔
835
  while (pRes != NULL) {
647,348,110✔
836
    SSDataBlock* p = NULL;
460,731,032✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
460,692,762✔
838
      SSDataBlock* p1 = NULL;
312,771,051✔
839
      code = createOneDataBlock(pRes, true, &p1);
312,769,825✔
840
      QUERY_CHECK_CODE(code, lino, _end);
312,770,916✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
312,770,916✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
312,772,657✔
844
      p = p1;
312,772,657✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
147,964,270✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
147,963,510✔
848

849
      p = *(SSDataBlock**)tmp;
147,963,510✔
850
      code = copyDataBlock(p, pRes);
147,963,702✔
851
      QUERY_CHECK_CODE(code, lino, _end);
147,963,631✔
852
    }
853

854
    blockIndex += 1;
460,736,112✔
855

856
    current += p->info.rows;
460,736,112✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
460,736,778✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
460,738,602✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
460,738,602✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
460,738,602✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
399,915,907✔
867
    QUERY_CHECK_CODE(code, lino, _end);
399,900,868✔
868
    code = blockDataCheck(pRes);
399,900,868✔
869
    QUERY_CHECK_CODE(code, lino, _end);
399,915,520✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
247,439,773✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
815,909✔
874
  }
875

876
  *hasMore = (pRes != NULL);
247,440,193✔
877
  uint64_t el = (taosGetTimestampUs() - st);
247,427,179✔
878

879
  pTaskInfo->cost.elapsedTime += el;
247,427,179✔
880
  if (NULL == pRes) {
247,430,577✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
186,607,882✔
882
  }
883

884
_end:
247,309,337✔
885
  (void)cleanUpUdfs();
247,428,247✔
886

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

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

897
  return pTaskInfo->code;
247,446,593✔
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) {
35,948,644✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
35,948,644✔
916
  int64_t        threadId = taosGetSelfPthreadId();
35,948,644✔
917
  int64_t        curOwner = 0;
35,948,977✔
918

919
  *pRes = NULL;
35,948,977✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
35,948,977✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
35,949,310✔
924
  if (isKilled) {
35,949,199✔
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) {
35,949,199✔
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;
35,948,793✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
35,949,199✔
941

942
  if (pTaskInfo->cost.start == 0) {
35,949,310✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
96,411✔
944
  }
945

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

958
  int64_t st = taosGetTimestampUs();
35,948,311✔
959
  int32_t code = TSDB_CODE_SUCCESS;
35,948,311✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
35,948,311✔
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);
35,948,422✔
965
  }
966
  if (code) {
35,938,426✔
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);
35,938,426✔
972
  if (code) {
35,947,802✔
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);
35,941,718✔
978

979
  pTaskInfo->cost.elapsedTime += el;
35,941,718✔
980
  if (NULL == *pRes) {
35,947,017✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,921,214✔
982
  }
983

984
  (void)cleanUpUdfs();
35,946,862✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
35,948,570✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
35,949,199✔
988

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
35,949,350✔
993
  return pTaskInfo->code;
35,949,310✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
59,655,304✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
59,655,304✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
59,656,006✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
59,656,125✔
1000

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

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

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
27,961✔
1067
  qStopTaskOperators(pTaskInfo);
27,961✔
1068

1069
  return TSDB_CODE_SUCCESS;
27,961✔
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) {
179,624,128✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
179,624,128✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
179,625,800✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
179,623,668✔
1130
  if (pSummary->pRecoder != NULL) {
179,620,741✔
1131
    qDebug(
124,309,959✔
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,
55,312,199✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
179,623,958✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
194,137,725✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
194,137,725✔
1146
  if (pTaskInfo == NULL) {
194,137,725✔
1147
    return;
14,521,724✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
179,616,001✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
179,625,671✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
179,626,113✔
1157
  doDestroyTask(pTaskInfo);
179,624,765✔
1158
}
1159

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

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

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

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

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

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

1213
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,372,755✔
1214
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,372,755✔
1215
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,372,755✔
1216
  return 0;
4,372,755✔
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) {
150,766✔
1225
  memset(pCond, 0, sizeof(SQueryTableDataCond));
150,766✔
1226
  pCond->order = TSDB_ORDER_ASC;
150,766✔
1227
  pCond->numOfCols = pMtInfo->schema->nCols;
151,087✔
1228
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
151,087✔
1229
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
150,723✔
1230
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
151,028✔
1231
    taosMemoryFreeClear(pCond->colList);
118✔
1232
    taosMemoryFreeClear(pCond->pSlotList);
×
1233
    return terrno;
×
1234
  }
1235

1236
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
150,802✔
1237
  pCond->suid = pMtInfo->suid;
150,920✔
1238
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
150,900✔
1239
  pCond->startVersion = -1;
150,864✔
1240
  pCond->endVersion = sContext->snapVersion;
150,861✔
1241

1242
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
959,580✔
1243
    SColumnInfo* pColInfo = &pCond->colList[i];
808,523✔
1244
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
808,290✔
1245
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
808,798✔
1246
    if (pMtInfo->pExtSchemas != NULL) {
808,595✔
1247
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,576✔
1248
    }
1249
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
808,798✔
1250
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
808,808✔
1251

1252
    pCond->pSlotList[i] = i;
808,749✔
1253
  }
1254

1255
  return TSDB_CODE_SUCCESS;
151,156✔
1256
}
1257

1258
void qStreamSetOpen(qTaskInfo_t tinfo) {
35,634,863✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
35,634,863✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
35,634,863✔
1261
  pOperator->status = OP_NOT_OPENED;
35,642,659✔
1262
}
35,642,585✔
1263

1264
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,209,179✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,209,179✔
1266
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,209,179✔
1267
}
4,209,852✔
1268

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

1274
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,432,351✔
1275
  const char*    id = GET_TASKID(pTaskInfo);
4,432,060✔
1276

1277
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,431,857✔
1278
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,145,563✔
1279
    if (pOperator == NULL || code != 0) {
4,144,756✔
1280
      return code;
×
1281
    }
1282

1283
    SStreamScanInfo* pInfo = pOperator->info;
4,145,320✔
1284
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,145,158✔
1285
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,145,247✔
1286
    walReaderVerifyOffset(pWalReader, pOffset);
4,145,988✔
1287
  }
1288
  // if pOffset equal to current offset, means continue consume
1289
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,430,932✔
1290
    return 0;
3,950,329✔
1291
  }
1292

1293
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
480,840✔
1294
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
326,467✔
1295
    if (pOperator == NULL || code != 0) {
326,454✔
1296
      return code;
×
1297
    }
1298

1299
    SStreamScanInfo* pInfo = pOperator->info;
326,607✔
1300
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
326,483✔
1301
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
326,523✔
1302
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
326,519✔
1303

1304
    if (pOffset->type == TMQ_OFFSET__LOG) {
326,328✔
1305
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
262,058✔
1306
      pScanBaseInfo->dataReader = NULL;
262,217✔
1307

1308
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
262,217✔
1309
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
262,109✔
1310
      walReaderVerifyOffset(pWalReader, pOffset);
262,129✔
1311
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
262,217✔
1312
      if (code < 0) {
262,109✔
1313
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,580✔
1314
        return code;
7,688✔
1315
      }
1316
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
64,251✔
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;
64,390✔
1320
      int64_t ts = pOffset->ts;
64,390✔
1321
      int32_t index = 0;
64,237✔
1322

1323
      // this value may be changed if new tables are created
1324
      taosRLockLatch(&pTaskInfo->lock);
64,237✔
1325
      int32_t numOfTables = 0;
64,335✔
1326
      code = tableListGetSize(pTableListInfo, &numOfTables);
64,335✔
1327
      if (code != TSDB_CODE_SUCCESS) {
64,153✔
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) {
64,153✔
1334
        if (numOfTables != 0) {
62,910✔
1335
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,506✔
1336
          if (!tmp) {
10,437✔
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;
10,437✔
1342
          ts = INT64_MIN;
10,437✔
1343
          pScanInfo->currentTable = 0;
10,437✔
1344
        } else {
1345
          taosRUnLockLatch(&pTaskInfo->lock);
52,404✔
1346
          qError("no table in table list, %s", id);
52,389✔
1347
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
52,557✔
1348
        }
1349
      }
1350
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,749✔
1351

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

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

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

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

1408
      // restore the key value
1409
      pScanBaseInfo->cond.twindows.skey = oldSkey;
11,833✔
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) {
154,373✔
1417
      SStreamRawScanInfo* pInfo = pOperator->info;
151,341✔
1418
      SSnapContext*       sContext = pInfo->sContext;
151,341✔
1419
      SOperatorInfo*      p = NULL;
151,341✔
1420

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

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

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

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

1442
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
150,928✔
1443
      tableListClear(pTableListInfo);
150,908✔
1444

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

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

1463
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
151,156✔
1464
      if (code != TSDB_CODE_SUCCESS) {
151,156✔
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);
151,156✔
1471
      if (!pList) {
151,156✔
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;
151,156✔
1477
      code = tableListGetSize(pTableListInfo, &size);
151,156✔
1478
      if (code != TSDB_CODE_SUCCESS) {
151,156✔
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,
302,312✔
1485
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
151,156✔
1486
      if (code != TSDB_CODE_SUCCESS) {
150,792✔
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);
150,792✔
1493
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
150,861✔
1494
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1495
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
150,733✔
1496
      pTaskInfo->streamInfo.schema = mtInfo.schema;
150,979✔
1497
      taosMemoryFreeClear(mtInfo.pExtSchemas);
150,979✔
1498

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

1518
end:
420,335✔
1519
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
420,735✔
1520
  return 0;
420,735✔
1521
}
1522

1523
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
172,700,623✔
1524
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
172,700,623✔
1525
  if (pMsg->info.ahandle == NULL) {
172,712,816✔
1526
    qError("pMsg->info.ahandle is NULL");
170✔
1527
    return;
170✔
1528
  }
1529

1530
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
172,701,327✔
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};
172,706,853✔
1534

1535
  if (pMsg->contLen > 0) {
172,709,333✔
1536
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
172,699,939✔
1537
    if (buf.pData == NULL) {
172,686,747✔
1538
      pMsg->code = terrno;
×
1539
    } else {
1540
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
172,686,747✔
1541
    }
1542
  }
1543

1544
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
172,700,730✔
1545
  rpcFreeCont(pMsg->pCont);
172,715,824✔
1546
  destroySendMsgInfo(pSendInfo);
172,689,384✔
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,856✔
1588
  int32_t        code = TSDB_CODE_SUCCESS;
3,384,856✔
1589
  int32_t        lino = 0;
3,384,856✔
1590
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,384,856✔
1591

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

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

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

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

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

1627
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,693,026✔
1628
  if (code == 0) {
1,693,030✔
1629
    *pList = pArray;
1,693,030✔
1630
  } else {
1631
    taosArrayDestroy(pArray);
×
1632
  }
1633
  return code;
1,693,030✔
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) {
49,866,172✔
1672
  int32_t code = 0;
49,866,172✔
1673

1674
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
49,866,172✔
1675
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
49,866,607✔
1676

1677
  if (pOper->fpSet.resetStateFn) {
49,866,626✔
1678
    code = pOper->fpSet.resetStateFn(pOper);
49,867,832✔
1679
  }
1680
  pOper->status = OP_NOT_OPENED;
49,857,672✔
1681
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
85,718,135✔
1682
    code = clearStatesForOperator(pOper->pDownstream[i]);
35,848,395✔
1683
  }
1684
  return code;
49,873,340✔
1685
}
1686

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

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

1701
  *ppRes = NULL;
18,461,564✔
1702

1703
  // todo extract method
1704
  taosRLockLatch(&pTaskInfo->lock);
18,461,564✔
1705
  bool isKilled = isTaskKilled(pTaskInfo);
18,461,895✔
1706
  if (isKilled) {
18,461,895✔
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,461,895✔
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,461,895✔
1723
  taosRUnLockLatch(&pTaskInfo->lock);
18,461,895✔
1724

1725
  if (pTaskInfo->cost.start == 0) {
18,461,895✔
1726
    pTaskInfo->cost.start = taosGetTimestampUs();
302,434✔
1727
  }
1728

1729
  // error occurs, record the error code and return to client
1730
  int32_t ret = setjmp(pTaskInfo->env);
18,461,895✔
1731
  if (ret != TSDB_CODE_SUCCESS) {
22,635,254✔
1732
    pTaskInfo->code = ret;
4,176,581✔
1733
    (void)cleanUpUdfs();
4,176,581✔
1734
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
4,176,980✔
1735
    atomic_store_64(&pTaskInfo->owner, 0);
4,176,980✔
1736
    return pTaskInfo->code;
4,176,980✔
1737
  }
1738

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

1741
  int64_t st = taosGetTimestampUs();
18,461,895✔
1742

1743
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
18,461,895✔
1744
  if (code) {
14,284,510✔
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,284,510✔
1749
    code = blockDataCheck(*ppRes);
14,284,483✔
1750
  }
1751
  if (code) {
14,284,915✔
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,284,915✔
1757

1758
  pTaskInfo->cost.elapsedTime += el;
14,284,915✔
1759
  if (NULL == *ppRes) {
14,284,915✔
1760
    *useconds = pTaskInfo->cost.elapsedTime;
9,313,507✔
1761
  }
1762

1763
  (void)cleanUpUdfs();
14,284,915✔
1764

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

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

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

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

1807
end:
314,981✔
1808
  return 0;
314,981✔
1809
}
1810

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2122
    memcpy(tbname, pVal, len);
306,648✔
2123
    tbname[len] = '\0';  // ensure null terminated
306,648✔
2124
    if (NULL != strchr(tbname, '.')) {
306,648✔
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);
308,074✔
2131
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
308,448✔
2132
  return code;
308,448✔
2133
}
2134

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

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

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

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

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

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

2185
  if (pSrc->pFields && pSrc->pFields->size > 0) {
294,476✔
2186
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
294,476✔
2187
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
294,476✔
2188
  } else {
2189
    (*ppDst)->pFields = NULL;
×
2190
  }
2191
  
2192
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
294,476✔
2193
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
193,080✔
2194
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
193,080✔
2195
  } else {
2196
    (*ppDst)->pTagFields = NULL;
101,396✔
2197
  }
2198

2199
_exit:
294,476✔
2200

2201
  if (code != 0) {
294,476✔
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;
294,476✔
2210
}
2211

2212
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
433✔
2213
  return doDropStreamTable(pMsgCb, pOutput, pReq);
433✔
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