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

taosdata / TDengine / #4860

22 Nov 2025 07:23AM UTC coverage: 64.335% (+0.06%) from 64.272%
#4860

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

0 of 79 new or added lines in 2 files covered. (0.0%)

559 existing lines in 108 files now uncovered.

154614 of 240326 relevant lines covered (64.34%)

112619890.95 hits per line

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

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

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

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

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

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

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

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

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
14,543,325✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,543,325✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,543,841✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,214,252✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,214,252✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,214,151✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,214,252✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,384✔
158
        if (code) {
57,316✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
10,327,947✔
166
  }
167

168
  return 0;
9,890,296✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
9,890,238✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
9,890,238✔
173
  pTaskInfo->id.queryId = queryId;
9,890,238✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
9,890,668✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
9,889,831✔
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,
139,179✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
139,179✔
209
    SExecTaskInfo* pTaskInfo = NULL;
23,314✔
210

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

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

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

227
  SSubplan* pPlan = NULL;
115,865✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
117,288✔
229
  if (code != TSDB_CODE_SUCCESS) {
117,397✔
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,377,955✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,260,558✔
249
    if (pSlotDesc->output) {
1,260,558✔
250
      ++(*numOfCols);
1,260,454✔
251
    }
252
  }
253

254
  return pTaskInfo;
117,397✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
389,414✔
258
  if (streamInserterParam == NULL) {
389,414✔
259
    return TSDB_CODE_SUCCESS;
166,226✔
260
  }
261

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

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

278
  return TSDB_CODE_SUCCESS;
223,188✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
223,188✔
329
    if (code) {
223,188✔
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,
389,039✔
334
         tstrerror(code));
335

336
_error:
139,185✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,291,611✔
348
  if (pInfo == NULL) {
5,291,611✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,291,611✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,291,611✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,291,611✔
354
    return false;
4,500✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,287,111✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,134,468✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
10,421,579✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,287,111✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
152,643✔
387
    setReadHandle(handle, pScanBaseInfo);
152,643✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,134,468✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,014,728✔
390
    setReadHandle(handle, pScanBaseInfo);
5,014,728✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
389,414✔
405

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

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

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
72,013✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
72,013✔
449

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

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

459
  locked = 1;
72,013✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
144,593✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
72,580✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
72,580✔
463

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

470
    tDecoderClear(&mr.coder);
72,580✔
471

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

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

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

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

517
      if (!qualified) {
62,132✔
518
        continue;
31,080✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
72,013✔
529

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

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

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

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

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

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

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

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

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

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

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

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

641
  return code;
72,232✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
288,426,983✔
650
    return TSDB_CODE_INVALID_PARA;
454✔
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
288,431,346✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
288,431,346✔
655
    return TSDB_CODE_SUCCESS;
170,047,655✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
118,363,712✔
665
  *tversion = pSchemaInfo->tversion;
118,392,041✔
666
  *rversion = pSchemaInfo->rversion;
118,386,544✔
667
  if (pSchemaInfo->dbname) {
118,387,385✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
118,391,963✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
118,401,436✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
118,393,953✔
674
  } else {
675
    tableName[0] = 0;
71✔
676
  }
677

678
  *tbGet = true;
118,401,642✔
679

680
  return TSDB_CODE_SUCCESS;
118,398,143✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
170,036,949✔
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) {
36,988,802✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
36,988,802✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
36,988,802✔
695
}
36,988,802✔
696

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

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

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

710
  readHandle->uid = 0;
171,918,268✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
171,938,507✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
171,828,362✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
85,507✔
714
    goto _error;
27,306✔
715
  }
716

717
  if (handle) {
171,772,300✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
171,686,513✔
719
    void*           pSinkManager = NULL;
171,710,928✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
171,563,477✔
721
    if (code != TSDB_CODE_SUCCESS) {
171,688,787✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
171,688,787✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
171,692,159✔
728
    if (code != TSDB_CODE_SUCCESS) {
171,578,097✔
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;
171,578,097✔
735
    if (readHandle->localExec) {
171,658,649✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
54,620✔
737
      if (code != TSDB_CODE_SUCCESS) {
54,620✔
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,
171,612,603✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
171,711,080✔
748
    if (code) {
171,603,487✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
625✔
750
    }
751
  }
752

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

756
_error:
8,886,917✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
171,926,382✔
759
}
760

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

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

773
  if (pLocal) {
230,380,043✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
224,896,128✔
775
  }
776

777
  taosArrayClear(pResList);
230,364,684✔
778

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

786
  if (pTaskInfo->cost.start == 0) {
230,370,437✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
167,700,241✔
788
  }
789

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

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
230,379,295✔
798
  if (ret != TSDB_CODE_SUCCESS) {
230,530,361✔
799
    pTaskInfo->code = ret;
181,711✔
800
    (void)cleanUpUdfs();
181,711✔
801

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

805
    return pTaskInfo->code;
181,711✔
806
  }
807

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

810
  int32_t      current = 0;
230,350,158✔
811
  SSDataBlock* pRes = NULL;
230,350,158✔
812
  int64_t      st = taosGetTimestampUs();
230,383,829✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
230,383,829✔
815
    pTaskInfo->paramSet = true;
36,988,802✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
36,988,802✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
193,389,000✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
230,205,106✔
822
  code = blockDataCheck(pRes);
230,205,106✔
823
  QUERY_CHECK_CODE(code, lino, _end);
230,204,871✔
824

825
  if (pRes == NULL) {
230,204,871✔
826
    st = taosGetTimestampUs();
41,975,116✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
230,207,553✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
230,208,484✔
831
    rowsThreshold = 4096;
229,441,444✔
832
  }
833

834
  int32_t blockIndex = 0;
230,205,535✔
835
  while (pRes != NULL) {
622,336,797✔
836
    SSDataBlock* p = NULL;
444,538,655✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
444,521,233✔
838
      SSDataBlock* p1 = NULL;
308,684,614✔
839
      code = createOneDataBlock(pRes, true, &p1);
308,684,196✔
840
      QUERY_CHECK_CODE(code, lino, _end);
308,675,678✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
308,675,678✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
308,686,775✔
844
      p = p1;
308,686,775✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
135,858,813✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
135,859,015✔
848

849
      p = *(SSDataBlock**)tmp;
135,859,015✔
850
      code = copyDataBlock(p, pRes);
135,859,217✔
851
      QUERY_CHECK_CODE(code, lino, _end);
135,859,424✔
852
    }
853

854
    blockIndex += 1;
444,547,286✔
855

856
    current += p->info.rows;
444,547,286✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
444,542,177✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
444,545,716✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
444,545,716✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
444,545,716✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
392,136,574✔
867
    QUERY_CHECK_CODE(code, lino, _end);
392,068,249✔
868
    code = blockDataCheck(pRes);
392,068,249✔
869
    QUERY_CHECK_CODE(code, lino, _end);
392,130,498✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
230,207,284✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
761,554✔
874
  }
875

876
  *hasMore = (pRes != NULL);
230,211,682✔
877
  uint64_t el = (taosGetTimestampUs() - st);
230,200,242✔
878

879
  pTaskInfo->cost.elapsedTime += el;
230,200,242✔
880
  if (NULL == pRes) {
230,197,725✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
177,788,583✔
882
  }
883

884
_end:
230,082,305✔
885
  (void)cleanUpUdfs();
230,197,639✔
886

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

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

897
  return pTaskInfo->code;
230,214,672✔
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,241,690✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
35,241,690✔
916
  int64_t        threadId = taosGetSelfPthreadId();
35,241,690✔
917
  int64_t        curOwner = 0;
35,244,186✔
918

919
  *pRes = NULL;
35,244,186✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
35,244,076✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
35,244,186✔
924
  if (isKilled) {
35,244,002✔
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,244,002✔
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,244,002✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
35,244,076✔
941

942
  if (pTaskInfo->cost.start == 0) {
35,244,002✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
92,716✔
944
  }
945

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

958
  int64_t st = taosGetTimestampUs();
35,243,416✔
959
  int32_t code = TSDB_CODE_SUCCESS;
35,243,416✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
35,243,416✔
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,242,866✔
965
  }
966
  if (code) {
35,238,408✔
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,238,408✔
972
  if (code) {
35,243,350✔
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,238,968✔
978

979
  pTaskInfo->cost.elapsedTime += el;
35,238,968✔
980
  if (NULL == *pRes) {
35,242,676✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,899,751✔
982
  }
983

984
  (void)cleanUpUdfs();
35,242,465✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
35,242,125✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
35,244,186✔
988

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
35,241,995✔
993
  return pTaskInfo->code;
35,244,085✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
57,678,849✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,678,849✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
57,678,836✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,679,038✔
1000

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

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

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
18,618✔
1067
  qStopTaskOperators(pTaskInfo);
18,618✔
1068

1069
  return TSDB_CODE_SUCCESS;
18,618✔
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) {
172,324,702✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
172,324,702✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
172,326,423✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
172,321,710✔
1130
  if (pSummary->pRecoder != NULL) {
172,323,869✔
1131
    qDebug(
120,631,373✔
1132
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1133
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1134
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1135
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1136
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1137
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1138
  } else {
1139
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
51,691,573✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
172,322,321✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
180,689,361✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
180,689,361✔
1146
  if (pTaskInfo == NULL) {
180,689,361✔
1147
    return;
8,368,464✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
172,320,897✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
172,325,306✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
172,325,832✔
1157
  doDestroyTask(pTaskInfo);
172,323,201✔
1158
}
1159

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

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

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

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

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

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

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

1236
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
142,876✔
1237
  pCond->suid = pMtInfo->suid;
142,931✔
1238
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
142,986✔
1239
  pCond->startVersion = -1;
143,041✔
1240
  pCond->endVersion = sContext->snapVersion;
142,876✔
1241

1242
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
911,652✔
1243
    SColumnInfo* pColInfo = &pCond->colList[i];
768,446✔
1244
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
768,391✔
1245
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
768,625✔
1246
    if (pMtInfo->pExtSchemas != NULL) {
768,460✔
1247
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,576✔
1248
    }
1249
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
768,625✔
1250
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
768,680✔
1251

1252
    pCond->pSlotList[i] = i;
768,666✔
1253
  }
1254

1255
  return TSDB_CODE_SUCCESS;
143,096✔
1256
}
1257

1258
void qStreamSetOpen(qTaskInfo_t tinfo) {
34,953,337✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
34,953,337✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
34,953,337✔
1261
  pOperator->status = OP_NOT_OPENED;
34,956,347✔
1262
}
34,956,259✔
1263

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

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

1274
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,368,334✔
1275
  const char*    id = GET_TASKID(pTaskInfo);
4,368,175✔
1276

1277
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,367,973✔
1278
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,091,898✔
1279
    if (pOperator == NULL || code != 0) {
4,091,783✔
1280
      return code;
142✔
1281
    }
1282

1283
    SStreamScanInfo* pInfo = pOperator->info;
4,091,641✔
1284
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,091,985✔
1285
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,091,285✔
1286
    walReaderVerifyOffset(pWalReader, pOffset);
4,092,066✔
1287
  }
1288
  // if pOffset equal to current offset, means continue consume
1289
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,367,626✔
1290
    return 0;
3,904,859✔
1291
  }
1292

1293
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
462,430✔
1294
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
316,155✔
1295
    if (pOperator == NULL || code != 0) {
316,155✔
1296
      return code;
×
1297
    }
1298

1299
    SStreamScanInfo* pInfo = pOperator->info;
316,260✔
1300
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
316,260✔
1301
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
316,155✔
1302
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
316,086✔
1303

1304
    if (pOffset->type == TMQ_OFFSET__LOG) {
315,928✔
1305
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
253,437✔
1306
      pScanBaseInfo->dataReader = NULL;
253,485✔
1307

1308
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
253,485✔
1309
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
253,380✔
1310
      walReaderVerifyOffset(pWalReader, pOffset);
253,437✔
1311
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
253,505✔
1312
      if (code < 0) {
253,505✔
1313
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
6,147✔
1314
        return code;
6,147✔
1315
      }
1316
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
62,520✔
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;
62,603✔
1320
      int64_t ts = pOffset->ts;
62,603✔
1321
      int32_t index = 0;
62,423✔
1322

1323
      // this value may be changed if new tables are created
1324
      taosRLockLatch(&pTaskInfo->lock);
62,423✔
1325
      int32_t numOfTables = 0;
62,755✔
1326
      code = tableListGetSize(pTableListInfo, &numOfTables);
62,755✔
1327
      if (code != TSDB_CODE_SUCCESS) {
62,534✔
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) {
62,534✔
1334
        if (numOfTables != 0) {
61,430✔
1335
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,368✔
1336
          if (!tmp) {
10,368✔
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,368✔
1342
          ts = INT64_MIN;
10,368✔
1343
          pScanInfo->currentTable = 0;
10,368✔
1344
        } else {
1345
          taosRUnLockLatch(&pTaskInfo->lock);
51,062✔
1346
          qError("no table in table list, %s", id);
50,979✔
1347
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
51,062✔
1348
        }
1349
      }
1350
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,472✔
1351

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

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

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

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

1408
      // restore the key value
1409
      pScanBaseInfo->cond.twindows.skey = oldSkey;
11,624✔
1410
    } else {
1411
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
83✔
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) {
146,275✔
1417
      SStreamRawScanInfo* pInfo = pOperator->info;
143,281✔
1418
      SSnapContext*       sContext = pInfo->sContext;
143,281✔
1419
      SOperatorInfo*      p = NULL;
143,281✔
1420

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

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

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

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

1442
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
143,212✔
1443
      tableListClear(pTableListInfo);
143,189✔
1444

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

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

1463
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
142,917✔
1464
      if (code != TSDB_CODE_SUCCESS) {
143,096✔
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);
143,096✔
1471
      if (!pList) {
143,096✔
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;
143,096✔
1477
      code = tableListGetSize(pTableListInfo, &size);
143,096✔
1478
      if (code != TSDB_CODE_SUCCESS) {
143,096✔
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,
286,192✔
1485
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
143,096✔
1486
      if (code != TSDB_CODE_SUCCESS) {
142,986✔
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);
142,986✔
1493
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
142,903✔
1494
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1495
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
142,917✔
1496
      pTaskInfo->streamInfo.schema = mtInfo.schema;
142,972✔
1497
      taosMemoryFreeClear(mtInfo.pExtSchemas);
143,027✔
1498

1499
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
143,027✔
1500
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
2,994✔
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,947✔
1511
      SStreamRawScanInfo* pInfo = pOperator->info;
1,947✔
1512
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,947✔
1513
      pInfo->dataReader = NULL;
1,947✔
1514
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,947✔
1515
    }
1516
  }
1517

1518
end:
404,928✔
1519
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
405,326✔
1520
  return 0;
405,326✔
1521
}
1522

1523
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
153,851,019✔
1524
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
153,851,019✔
1525
  if (pMsg->info.ahandle == NULL) {
153,861,722✔
1526
    qError("pMsg->info.ahandle is NULL");
562✔
1527
    return;
562✔
1528
  }
1529

1530
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
153,847,666✔
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};
153,851,638✔
1534

1535
  if (pMsg->contLen > 0) {
153,856,756✔
1536
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
153,840,915✔
1537
    if (buf.pData == NULL) {
153,841,933✔
1538
      pMsg->code = terrno;
×
1539
    } else {
1540
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
153,841,933✔
1541
    }
1542
  }
1543

1544
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
153,860,936✔
1545
  rpcFreeCont(pMsg->pCont);
153,863,262✔
1546
  destroySendMsgInfo(pSendInfo);
153,848,249✔
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,381,218✔
1588
  int32_t        code = TSDB_CODE_SUCCESS;
3,381,218✔
1589
  int32_t        lino = 0;
3,381,218✔
1590
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,381,218✔
1591

1592
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,381,720✔
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,377,556✔
1599
    STableScanInfo* pScanInfo = pOperator->info;
1,690,609✔
1600

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

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

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

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

1627
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,689,920✔
1628
  if (code == 0) {
1,690,009✔
1629
    *pList = pArray;
1,690,009✔
1630
  } else {
1631
    taosArrayDestroy(pArray);
×
1632
  }
1633
  return code;
1,689,409✔
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) {
38,295,503✔
1672
  int32_t code = 0;
38,295,503✔
1673

1674
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
38,295,503✔
1675
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
38,290,679✔
1676

1677
  if (pOper->fpSet.resetStateFn) {
38,291,504✔
1678
    code = pOper->fpSet.resetStateFn(pOper);
38,291,114✔
1679
  }
1680
  pOper->status = OP_NOT_OPENED;
38,288,266✔
1681
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
65,299,696✔
1682
    code = clearStatesForOperator(pOper->pDownstream[i]);
27,007,476✔
1683
  }
1684
  return code;
38,295,904✔
1685
}
1686

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

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

1701
  *ppRes = NULL;
14,744,072✔
1702

1703
  // todo extract method
1704
  taosRLockLatch(&pTaskInfo->lock);
14,744,072✔
1705
  bool isKilled = isTaskKilled(pTaskInfo);
14,744,473✔
1706
  if (isKilled) {
14,743,644✔
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) {
14,743,644✔
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;
14,744,045✔
1723
  taosRUnLockLatch(&pTaskInfo->lock);
14,743,649✔
1724

1725
  if (pTaskInfo->cost.start == 0) {
14,744,473✔
1726
    pTaskInfo->cost.start = taosGetTimestampUs();
231,114✔
1727
  }
1728

1729
  // error occurs, record the error code and return to client
1730
  int32_t ret = setjmp(pTaskInfo->env);
14,744,473✔
1731
  if (ret != TSDB_CODE_SUCCESS) {
17,876,966✔
1732
    pTaskInfo->code = ret;
3,135,319✔
1733
    (void)cleanUpUdfs();
3,135,319✔
1734
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,135,319✔
1735
    atomic_store_64(&pTaskInfo->owner, 0);
3,134,922✔
1736
    return pTaskInfo->code;
3,135,319✔
1737
  }
1738

1739
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
14,741,647✔
1740

1741
  int64_t st = taosGetTimestampUs();
14,743,675✔
1742

1743
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
14,743,675✔
1744
  if (code) {
11,609,154✔
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;
11,609,154✔
1749
    code = blockDataCheck(*ppRes);
11,609,154✔
1750
  }
1751
  if (code) {
11,609,154✔
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);
11,609,154✔
1757

1758
  pTaskInfo->cost.elapsedTime += el;
11,609,154✔
1759
  if (NULL == *ppRes) {
11,609,154✔
1760
    *useconds = pTaskInfo->cost.elapsedTime;
7,892,814✔
1761
  }
1762

1763
  (void)cleanUpUdfs();
11,609,154✔
1764

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

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

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

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

1807
end:
10,610,276✔
1808
  return 0;
10,610,276✔
1809
}
1810

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2082
      pCol->hasNull = true;
232,714✔
2083
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
232,714✔
2084
      pCol->info.colId = 0;
232,714✔
2085
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
232,714✔
2086
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
232,714✔
2087
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
232,714✔
2088
      code = colInfoDataEnsureCapacity(pCol, 1, true);
232,714✔
2089
      if (code != 0) {
232,714✔
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;
232,714✔
2095
      dst.numOfRows = 1;
232,714✔
2096
      dst.colAlloced = true;
232,714✔
2097
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
232,714✔
2098
      if (colDataIsNull_var(dst.columnData, 0)) {
232,714✔
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) {
232,714✔
2103
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
230,914✔
2104
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
230,914✔
2105
      }
2106
    } break;
232,714✔
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) {
232,714✔
2113
    if (!pVal || len == 0) {
230,914✔
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) {
230,914✔
2118
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
780✔
2119
      len = TSDB_TABLE_NAME_LEN - 1;
780✔
2120
    }
2121

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

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

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

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

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

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

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

2185
  if (pSrc->pFields && pSrc->pFields->size > 0) {
223,188✔
2186
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
223,188✔
2187
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
223,188✔
2188
  } else {
2189
    (*ppDst)->pFields = NULL;
×
2190
  }
2191
  
2192
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
223,188✔
2193
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
163,763✔
2194
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
163,763✔
2195
  } else {
2196
    (*ppDst)->pTagFields = NULL;
59,425✔
2197
  }
2198

2199
_exit:
223,188✔
2200

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

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