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

taosdata / TDengine / #4853

14 Nov 2025 08:06AM UTC coverage: 63.951% (+0.1%) from 63.812%
#4853

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

2781 existing lines in 25 files now uncovered.

150719 of 235679 relevant lines covered (63.95%)

117936996.02 hits per line

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

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

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

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

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

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

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

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

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,562,541✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,562,541✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,563,831✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,125,334✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,125,334✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,125,311✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,125,430✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
52,844✔
158
        if (code) {
52,796✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,437,117✔
166
  }
167

168
  return 0;
10,854,629✔
169
}
170

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

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

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

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

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

227
  SSubplan* pPlan = NULL;
109,703✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
110,309✔
229
  if (code != TSDB_CODE_SUCCESS) {
110,377✔
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,327,790✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,217,413✔
249
    if (pSlotDesc->output) {
1,217,413✔
250
      ++(*numOfCols);
1,217,333✔
251
    }
252
  }
253

254
  return pTaskInfo;
110,377✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
620,930✔
258
  if (streamInserterParam == NULL) {
620,930✔
259
    return TSDB_CODE_SUCCESS;
351,243✔
260
  }
261

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

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

278
  return TSDB_CODE_SUCCESS;
269,687✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
269,687✔
329
    if (code) {
269,687✔
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,
620,930✔
334
         tstrerror(code));
335

336
_error:
138,796✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
6,113,405✔
348
  if (pInfo == NULL) {
6,113,405✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,113,405✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,113,405✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,113,405✔
354
    return false;
4,680✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
6,108,725✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,875,737✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,984,462✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,108,725✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
232,988✔
387
    setReadHandle(handle, pScanBaseInfo);
232,988✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,875,737✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,387,858✔
390
    setReadHandle(handle, pScanBaseInfo);
5,387,858✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
620,539✔
405

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

421
  return code;
620,930✔
422
}
423

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

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
59,432✔
437

438
  uint64_t suid = 0;
59,432✔
439
  uint64_t uid = 0;
59,432✔
440
  int32_t  type = 0;
59,432✔
441
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
59,432✔
442

443
  // let's discard the tables those are not created according to the queried super table.
444
  SMetaReader mr = {0};
59,432✔
445
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
59,432✔
446
  for (int32_t i = 0; i < numOfUids; ++i) {
119,791✔
447
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
60,359✔
448
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
60,359✔
449

450
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
60,359✔
451
    if (code != TSDB_CODE_SUCCESS) {
60,359✔
UNCOV
452
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
453
      continue;
×
454
    }
455

456
    tDecoderClear(&mr.coder);
60,359✔
457

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

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

484
      if (!qualified) {
50,543✔
485
        continue;
25,259✔
486
      }
487
    }
488

489
    // handle multiple partition
490
    void* tmp = taosArrayPush(qa, id);
34,160✔
491
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
34,160✔
492
  }
493

494
_end:
59,432✔
495

496
  pAPI->metaReaderFn.clearReader(&mr);
59,432✔
497
  (*ppArrayRes) = qa;
59,432✔
498

499
_error:
59,432✔
500
  if (code != TSDB_CODE_SUCCESS) {
59,432✔
UNCOV
501
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
502
  }
503
  return code;
59,432✔
504
}
505

506
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
59,619✔
507
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
59,619✔
508
  const char*    id = GET_TASKID(pTaskInfo);
59,619✔
509
  int32_t        code = 0;
59,619✔
510

511
  if (isAdd) {
59,619✔
512
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
59,432✔
513
  }
514

515
  // traverse to the stream scanner node to add this table id
516
  SOperatorInfo* pInfo = NULL;
59,619✔
517
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
59,619✔
518
  if (code != 0 || pInfo == NULL) {
59,619✔
UNCOV
519
    return code;
×
520
  }
521

522
  SStreamScanInfo* pScanInfo = pInfo->info;
59,619✔
523
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
59,619✔
524
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
120,165✔
525
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
60,546✔
526
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
60,546✔
527
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
60,546✔
528
    }
529
  }
530

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

542
    bool   assignUid = false;
59,432✔
543
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
59,432✔
544
    char*  keyBuf = NULL;
59,432✔
545
    if (bufLen > 0) {
59,432✔
UNCOV
546
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
547
      keyBuf = taosMemoryMalloc(bufLen);
×
548
      if (keyBuf == NULL) {
×
549
        taosArrayDestroy(qa);
×
550
        return terrno;
×
551
      }
552
    }
553

554
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
59,432✔
555
    taosWLockLatch(&pTaskInfo->lock);
59,432✔
556

557
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
93,592✔
558
      uint64_t* uid = taosArrayGet(qa, i);
34,160✔
559
      if (!uid) {
34,160✔
UNCOV
560
        taosMemoryFree(keyBuf);
×
561
        taosArrayDestroy(qa);
×
562
        taosWUnLockLatch(&pTaskInfo->lock);
×
563
        return terrno;
×
564
      }
565
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
34,160✔
566

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

582
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
34,160✔
583
      if (code != TSDB_CODE_SUCCESS) {
34,160✔
UNCOV
584
        taosMemoryFree(keyBuf);
×
585
        taosArrayDestroy(qa);
×
586
        taosWUnLockLatch(&pTaskInfo->lock);
×
587
        return code;
×
588
      }
589
    }
590

591
    taosWUnLockLatch(&pTaskInfo->lock);
59,432✔
592
    if (keyBuf != NULL) {
59,432✔
UNCOV
593
      taosMemoryFree(keyBuf);
×
594
    }
595

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

604
  return code;
59,619✔
605
}
606

607
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
310,799,510✔
608
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
609
                                    int32_t idx, bool* tbGet) {
610
  *tbGet = false;
310,799,510✔
611

612
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
310,806,071✔
UNCOV
613
    return TSDB_CODE_INVALID_PARA;
×
614
  }
615
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
310,820,742✔
616

617
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
310,820,742✔
618
    return TSDB_CODE_SUCCESS;
182,233,650✔
619
  }
620

621
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
128,596,601✔
622
  if (!pSchemaInfo) {
128,586,416✔
UNCOV
623
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
624
    return terrno;
×
625
  }
626

627
  *sversion = pSchemaInfo->sw->version;
128,586,416✔
628
  *tversion = pSchemaInfo->tversion;
128,599,934✔
629
  *rversion = pSchemaInfo->rversion;
128,596,981✔
630
  if (pSchemaInfo->dbname) {
128,594,695✔
631
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
128,595,528✔
632
  } else {
UNCOV
633
    dbName[0] = 0;
×
634
  }
635
  if (pSchemaInfo->tablename) {
128,607,379✔
636
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
128,593,223✔
637
  } else {
638
    tableName[0] = 0;
5,578✔
639
  }
640

641
  *tbGet = true;
128,606,834✔
642

643
  return TSDB_CODE_SUCCESS;
128,605,237✔
644
}
645

646
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
182,222,057✔
647

UNCOV
648
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
649
  if (NULL == pParam) {
×
650
    return;
×
651
  }
UNCOV
652
  freeOperatorParam(pParam, OP_GET_PARAM);
×
653
}
654

655
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
36,685,050✔
656
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
36,685,050✔
657
  ((SExecTaskInfo*)tinfo)->paramSet = false;
36,685,050✔
658
}
36,685,050✔
659

660
int32_t qExecutorInit(void) {
4,596,100✔
661
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,596,100✔
662
  return TSDB_CODE_SUCCESS;
4,596,100✔
663
}
664

665
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
184,114,336✔
666
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
667
                        EOPTR_EXEC_MODEL model) {
668
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
184,114,336✔
669
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
184,114,336✔
670

671
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
184,112,584✔
672

673
  readHandle->uid = 0;
184,138,298✔
674
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
184,157,172✔
675
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
184,017,886✔
676
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
170,258✔
677
    goto _error;
26,765✔
678
  }
679

680
  if (handle) {
183,881,012✔
681
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
183,795,182✔
682
    void*           pSinkManager = NULL;
183,811,986✔
683
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
183,862,930✔
684
    if (code != TSDB_CODE_SUCCESS) {
183,733,520✔
UNCOV
685
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
686
      goto _error;
×
687
    }
688

689
    void* pSinkParam = NULL;
183,733,520✔
690
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
183,685,819✔
691
    if (code != TSDB_CODE_SUCCESS) {
183,771,711✔
UNCOV
692
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
693
      taosMemoryFree(pSinkManager);
×
694
      goto _error;
×
695
    }
696

697
    SDataSinkNode* pSink = NULL;
183,771,711✔
698
    if (readHandle->localExec) {
183,885,808✔
699
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
61,651✔
700
      if (code != TSDB_CODE_SUCCESS) {
61,651✔
UNCOV
701
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
702
               (*pTask)->id.str);
UNCOV
703
        taosMemoryFree(pSinkManager);
×
704
        goto _error;
×
705
      }
706
    }
707

708
    // pSinkParam has been freed during create sinker.
709
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
183,741,655✔
710
                              (*pTask)->id.str, pSubplan->processOneBlock);
183,946,135✔
711
    if (code) {
183,764,390✔
712
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
622✔
713
    }
714
  }
715

716
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
183,867,352✔
717
         tstrerror(code));
718

719
_error:
8,538,142✔
720
  // if failed to add ref for all tables in this query, abort current query
721
  return code;
184,134,814✔
722
}
723

UNCOV
724
static void freeBlock(void* param) {
×
725
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
726
  blockDataDestroy(pBlock);
×
727
}
×
728

729
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
253,005,160✔
730
                     bool processOneBlock) {
731
  int32_t        code = TSDB_CODE_SUCCESS;
253,005,160✔
732
  int32_t        lino = 0;
253,005,160✔
733
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
253,005,160✔
734
  int64_t        threadId = taosGetSelfPthreadId();
253,005,160✔
735

736
  if (pLocal) {
253,002,384✔
737
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
246,515,330✔
738
  }
739

740
  taosArrayClear(pResList);
252,987,479✔
741

742
  int64_t curOwner = 0;
252,996,477✔
743
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
252,996,477✔
UNCOV
744
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
745
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
746
    return pTaskInfo->code;
×
747
  }
748

749
  if (pTaskInfo->cost.start == 0) {
252,978,911✔
750
    pTaskInfo->cost.start = taosGetTimestampUs();
180,196,226✔
751
  }
752

753
  if (isTaskKilled(pTaskInfo)) {
252,994,775✔
754
    atomic_store_64(&pTaskInfo->owner, 0);
595✔
755
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
595✔
756
    return pTaskInfo->code;
595✔
757
  }
758

759
  // error occurs, record the error code and return to client
760
  int32_t ret = setjmp(pTaskInfo->env);
252,991,050✔
761
  if (ret != TSDB_CODE_SUCCESS) {
253,258,111✔
762
    pTaskInfo->code = ret;
283,277✔
763
    (void)cleanUpUdfs();
283,277✔
764

765
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
283,277✔
766
    atomic_store_64(&pTaskInfo->owner, 0);
283,277✔
767

768
    return pTaskInfo->code;
283,277✔
769
  }
770

771
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
252,974,834✔
772

773
  int32_t      current = 0;
252,977,805✔
774
  SSDataBlock* pRes = NULL;
252,977,805✔
775
  int64_t      st = taosGetTimestampUs();
252,998,698✔
776

777
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
252,998,698✔
778
    pTaskInfo->paramSet = true;
36,685,050✔
779
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
36,685,050✔
780
  } else {
781
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
216,306,065✔
782
  }
783

784
  QUERY_CHECK_CODE(code, lino, _end);
252,717,820✔
785
  code = blockDataCheck(pRes);
252,717,820✔
786
  QUERY_CHECK_CODE(code, lino, _end);
252,722,318✔
787

788
  if (pRes == NULL) {
252,722,318✔
789
    st = taosGetTimestampUs();
45,997,542✔
790
  }
791

792
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
252,720,694✔
793
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
252,726,624✔
794
    rowsThreshold = 4096;
251,993,305✔
795
  }
796

797
  int32_t blockIndex = 0;
252,719,891✔
798
  while (pRes != NULL) {
655,906,021✔
799
    SSDataBlock* p = NULL;
465,004,045✔
800
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
465,011,301✔
801
      SSDataBlock* p1 = NULL;
321,109,832✔
802
      code = createOneDataBlock(pRes, true, &p1);
321,111,210✔
803
      QUERY_CHECK_CODE(code, lino, _end);
321,105,722✔
804

805
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
321,105,722✔
806
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
321,105,476✔
807
      p = p1;
321,105,476✔
808
    } else {
809
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
143,921,841✔
810
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
143,920,567✔
811

812
      p = *(SSDataBlock**)tmp;
143,920,567✔
813
      code = copyDataBlock(p, pRes);
143,921,577✔
814
      QUERY_CHECK_CODE(code, lino, _end);
143,919,497✔
815
    }
816

817
    blockIndex += 1;
465,023,265✔
818

819
    current += p->info.rows;
465,023,265✔
820
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
465,022,616✔
821
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
822
    void* tmp = taosArrayPush(pResList, &p);
465,024,371✔
823
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
465,024,371✔
824

825
    if (current >= rowsThreshold || processOneBlock) {
465,024,371✔
826
      break;
827
    }
828

829
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
403,203,221✔
830
    QUERY_CHECK_CODE(code, lino, _end);
403,186,560✔
831
    code = blockDataCheck(pRes);
403,186,560✔
832
    QUERY_CHECK_CODE(code, lino, _end);
403,207,988✔
833
  }
834

835
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
252,722,697✔
836
    pTaskInfo->pSubplan->rowsThreshold -= current;
726,957✔
837
  }
838

839
  *hasMore = (pRes != NULL);
252,725,008✔
840
  uint64_t el = (taosGetTimestampUs() - st);
252,713,336✔
841

842
  pTaskInfo->cost.elapsedTime += el;
252,713,336✔
843
  if (NULL == pRes) {
252,715,239✔
844
    *useconds = pTaskInfo->cost.elapsedTime;
190,895,376✔
845
  }
846

847
_end:
252,624,812✔
848
  (void)cleanUpUdfs();
252,719,803✔
849

850
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
252,735,660✔
851
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
252,735,660✔
852
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
853

854
  atomic_store_64(&pTaskInfo->owner, 0);
252,735,231✔
855
  if (code) {
252,735,460✔
UNCOV
856
    pTaskInfo->code = code;
×
857
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
858
  }
859

860
  return pTaskInfo->code;
252,735,460✔
861
}
862

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

UNCOV
874
  taosArrayClear(pTaskInfo->pResultBlockList);
×
875
}
×
876

877
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
47,555,028✔
878
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,555,028✔
879
  int64_t        threadId = taosGetSelfPthreadId();
47,555,028✔
880
  int64_t        curOwner = 0;
47,556,603✔
881

882
  *pRes = NULL;
47,556,603✔
883

884
  // todo extract method
885
  taosRLockLatch(&pTaskInfo->lock);
47,556,603✔
886
  bool isKilled = isTaskKilled(pTaskInfo);
47,556,393✔
887
  if (isKilled) {
47,556,393✔
UNCOV
888
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
889

UNCOV
890
    taosRUnLockLatch(&pTaskInfo->lock);
×
891
    return pTaskInfo->code;
×
892
  }
893

894
  if (pTaskInfo->owner != 0) {
47,556,393✔
UNCOV
895
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
896
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
897

UNCOV
898
    taosRUnLockLatch(&pTaskInfo->lock);
×
899
    return pTaskInfo->code;
×
900
  }
901

902
  pTaskInfo->owner = threadId;
47,555,402✔
903
  taosRUnLockLatch(&pTaskInfo->lock);
47,556,498✔
904

905
  if (pTaskInfo->cost.start == 0) {
47,556,813✔
906
    pTaskInfo->cost.start = taosGetTimestampUs();
87,486✔
907
  }
908

909
  // error occurs, record the error code and return to client
910
  int32_t ret = setjmp(pTaskInfo->env);
47,556,813✔
911
  if (ret != TSDB_CODE_SUCCESS) {
47,554,155✔
UNCOV
912
    pTaskInfo->code = ret;
×
913
    (void)cleanUpUdfs();
×
914
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
915
    atomic_store_64(&pTaskInfo->owner, 0);
×
916
    return pTaskInfo->code;
×
917
  }
918

919
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
47,554,155✔
920

921
  int64_t st = taosGetTimestampUs();
47,554,788✔
922
  int32_t code = TSDB_CODE_SUCCESS;
47,554,788✔
923
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
47,554,788✔
UNCOV
924
    pTaskInfo->paramSet = true;
×
925
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
926
  } else {
927
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
47,554,544✔
928
  }
929
  if (code) {
47,543,554✔
UNCOV
930
    pTaskInfo->code = code;
×
931
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
932
  }
933

934
  code = blockDataCheck(*pRes);
47,543,554✔
935
  if (code) {
47,554,794✔
UNCOV
936
    pTaskInfo->code = code;
×
937
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
938
  }
939

940
  uint64_t el = (taosGetTimestampUs() - st);
47,548,098✔
941

942
  pTaskInfo->cost.elapsedTime += el;
47,548,098✔
943
  if (NULL == *pRes) {
47,548,617✔
944
    *useconds = pTaskInfo->cost.elapsedTime;
3,688,198✔
945
  }
946

947
  (void)cleanUpUdfs();
47,550,705✔
948

949
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
47,556,918✔
950
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
47,557,023✔
951

952
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
47,556,708✔
953
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
954

955
  atomic_store_64(&pTaskInfo->owner, 0);
47,555,658✔
956
  return pTaskInfo->code;
47,556,603✔
957
}
958

959
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
64,660,742✔
960
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
64,660,742✔
961
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
64,660,849✔
962
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
64,660,742✔
963

964
  if (!tmp) {
64,660,742✔
UNCOV
965
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
966
    return terrno;
×
967
  }
968
  return TSDB_CODE_SUCCESS;
64,660,742✔
969
}
970

UNCOV
971
int32_t stopInfoComp(void const* lp, void const* rp) {
×
972
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
973
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
974

UNCOV
975
  if (key->refId < pInfo->refId) {
×
976
    return -1;
×
977
  } else if (key->refId > pInfo->refId) {
×
978
    return 1;
×
979
  }
980

UNCOV
981
  return 0;
×
982
}
983

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

993
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
46,674✔
994
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
46,674✔
995

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

1018
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
46,674✔
1019
}
46,674✔
1020

1021
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
46,674✔
1022
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
46,674✔
1023
  if (pTaskInfo == NULL) {
46,674✔
UNCOV
1024
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1025
  }
1026

1027
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
46,674✔
1028

1029
  setTaskKilled(pTaskInfo, rspCode);
46,674✔
1030
  qStopTaskOperators(pTaskInfo);
46,674✔
1031

1032
  return TSDB_CODE_SUCCESS;
46,674✔
1033
}
1034

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

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

UNCOV
1048
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1049

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

UNCOV
1056
        taosMsleep(200);
×
1057

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

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

UNCOV
1079
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1080
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1081
  if (NULL == pTaskInfo) {
×
1082
    return false;
×
1083
  }
1084

UNCOV
1085
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1086
}
1087

1088
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
184,773,438✔
1089
  STaskCostInfo* pSummary = &pTaskInfo->cost;
184,773,438✔
1090
  int64_t        idleTime = pSummary->start - pSummary->created;
184,772,560✔
1091

1092
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
184,771,743✔
1093
  if (pSummary->pRecoder != NULL) {
184,770,900✔
1094
    qDebug(
130,670,840✔
1095
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1096
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1097
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1098
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1099
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1100
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1101
  } else {
1102
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
54,098,308✔
1103
           pSummary->elapsedTime / 1000.0);
1104
  }
1105
}
184,769,148✔
1106

1107
void qDestroyTask(qTaskInfo_t qTaskHandle) {
198,168,163✔
1108
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
198,168,163✔
1109
  if (pTaskInfo == NULL) {
198,168,163✔
1110
    return;
13,398,634✔
1111
  }
1112

1113
  if (pTaskInfo->pRoot != NULL) {
184,769,529✔
1114
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
184,773,478✔
1115
  } else {
UNCOV
1116
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1117
  }
1118

1119
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
184,773,749✔
1120
  doDestroyTask(pTaskInfo);
184,771,797✔
1121
}
1122

1123
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,579,437✔
1124
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,579,437✔
1125
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,579,437✔
1126
}
1127

1128
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
110,377✔
1129
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
110,377✔
1130
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
110,377✔
1131

1132
  while (1) {
107,731✔
1133
    uint16_t type = pOperator->operatorType;
218,108✔
1134
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
218,108✔
1135
      *scanner = pOperator->info;
110,377✔
1136
      break;
110,377✔
1137
    } else {
1138
      pOperator = pOperator->pDownstream[0];
107,731✔
1139
    }
1140
  }
1141
}
110,377✔
1142

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

1156
void* qExtractReaderFromTmqScanner(void* scanner) {
110,377✔
1157
  SStreamScanInfo* pInfo = scanner;
110,377✔
1158
  return (void*)pInfo->tqReader;
110,377✔
1159
}
1160

1161
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
120,754✔
1162
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
120,754✔
1163
  return pTaskInfo->streamInfo.schema;
120,754✔
1164
}
1165

1166
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
120,754✔
1167
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
120,754✔
1168
  return pTaskInfo->streamInfo.tbName;
120,754✔
1169
}
1170

1171
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
125,919✔
1172
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
125,919✔
1173
  return &pTaskInfo->streamInfo.btMetaRsp;
125,919✔
1174
}
1175

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

1187
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
117,567✔
1188
  memset(pCond, 0, sizeof(SQueryTableDataCond));
117,567✔
1189
  pCond->order = TSDB_ORDER_ASC;
117,567✔
1190
  pCond->numOfCols = pMtInfo->schema->nCols;
117,455✔
1191
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
117,567✔
1192
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
117,586✔
1193
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
117,604✔
UNCOV
1194
    taosMemoryFreeClear(pCond->colList);
×
1195
    taosMemoryFreeClear(pCond->pSlotList);
×
1196
    return terrno;
×
1197
  }
1198

1199
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
117,560✔
1200
  pCond->suid = pMtInfo->suid;
117,604✔
1201
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
117,560✔
1202
  pCond->startVersion = -1;
117,517✔
1203
  pCond->endVersion = sContext->snapVersion;
117,604✔
1204

1205
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
753,973✔
1206
    SColumnInfo* pColInfo = &pCond->colList[i];
636,605✔
1207
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
636,455✔
1208
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
636,562✔
1209
    if (pMtInfo->pExtSchemas != NULL) {
636,581✔
1210
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,004✔
1211
    }
1212
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
636,625✔
1213
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
636,350✔
1214

1215
    pCond->pSlotList[i] = i;
636,544✔
1216
  }
1217

1218
  return TSDB_CODE_SUCCESS;
117,692✔
1219
}
1220

1221
void qStreamSetOpen(qTaskInfo_t tinfo) {
47,307,958✔
1222
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,307,958✔
1223
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
47,307,958✔
1224
  pOperator->status = OP_NOT_OPENED;
47,313,985✔
1225
}
47,314,309✔
1226

1227
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,072,692✔
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,072,692✔
1229
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,072,692✔
1230
}
4,073,340✔
1231

1232
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,252,908✔
1233
  int32_t        code = TSDB_CODE_SUCCESS;
4,252,908✔
1234
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,252,908✔
1235
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,252,908✔
1236

1237
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,253,736✔
1238
  const char*    id = GET_TASKID(pTaskInfo);
4,253,633✔
1239

1240
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,252,453✔
1241
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,011,833✔
1242
    if (pOperator == NULL || code != 0) {
4,010,237✔
UNCOV
1243
      return code;
×
1244
    }
1245

1246
    SStreamScanInfo* pInfo = pOperator->info;
4,010,860✔
1247
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,010,657✔
1248
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,010,307✔
1249
    walReaderVerifyOffset(pWalReader, pOffset);
4,011,745✔
1250
  }
1251
  // if pOffset equal to current offset, means continue consume
1252
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,251,747✔
1253
    return 0;
3,835,798✔
1254
  }
1255

1256
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
415,936✔
1257
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
295,511✔
1258
    if (pOperator == NULL || code != 0) {
295,224✔
1259
      return code;
324✔
1260
    }
1261

1262
    SStreamScanInfo* pInfo = pOperator->info;
294,900✔
1263
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
295,536✔
1264
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
295,500✔
1265
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
295,048✔
1266

1267
    if (pOffset->type == TMQ_OFFSET__LOG) {
294,954✔
1268
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
237,476✔
1269
      pScanBaseInfo->dataReader = NULL;
237,476✔
1270

1271
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
237,476✔
1272
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
237,476✔
1273
      walReaderVerifyOffset(pWalReader, pOffset);
237,362✔
1274
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
237,476✔
1275
      if (code < 0) {
237,476✔
1276
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
3,771✔
1277
        return code;
3,771✔
1278
      }
1279
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
58,249✔
1280
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1281
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1282
      int64_t uid = pOffset->uid;
58,222✔
1283
      int64_t ts = pOffset->ts;
57,909✔
1284
      int32_t index = 0;
57,902✔
1285

1286
      // this value may be changed if new tables are created
1287
      taosRLockLatch(&pTaskInfo->lock);
57,902✔
1288
      int32_t numOfTables = 0;
58,157✔
1289
      code = tableListGetSize(pTableListInfo, &numOfTables);
58,185✔
1290
      if (code != TSDB_CODE_SUCCESS) {
58,163✔
UNCOV
1291
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1292
        taosRUnLockLatch(&pTaskInfo->lock);
×
1293
        return code;
×
1294
      }
1295

1296
      if (uid == 0) {
58,163✔
1297
        if (numOfTables != 0) {
57,001✔
1298
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
9,391✔
1299
          if (!tmp) {
9,323✔
UNCOV
1300
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1301
            taosRUnLockLatch(&pTaskInfo->lock);
×
1302
            return terrno;
×
1303
          }
1304
          if (tmp) uid = tmp->uid;
9,323✔
1305
          ts = INT64_MIN;
9,323✔
1306
          pScanInfo->currentTable = 0;
9,323✔
1307
        } else {
1308
          taosRUnLockLatch(&pTaskInfo->lock);
47,610✔
1309
          qError("no table in table list, %s", id);
47,835✔
1310
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
47,835✔
1311
        }
1312
      }
1313
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
10,485✔
1314

1315
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
10,478✔
1316
             pInfo->pTableScanOp->resultInfo.totalRows);
1317
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
10,478✔
1318

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

1325
      if (index >= 0) {
10,478✔
1326
        pScanInfo->currentTable = index;
10,478✔
1327
      } else {
UNCOV
1328
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1329
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1330
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1331
      }
1332

1333
      STableKeyInfo keyInfo = {.uid = uid};
10,478✔
1334
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
10,430✔
1335

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

1344
      if (pScanBaseInfo->dataReader == NULL) {
10,478✔
1345
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
18,526✔
1346
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1347
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
9,263✔
1348
        if (code != TSDB_CODE_SUCCESS) {
9,263✔
UNCOV
1349
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1350
          return code;
×
1351
        }
1352

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

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

1371
      // restore the key value
1372
      pScanBaseInfo->cond.twindows.skey = oldSkey;
10,546✔
1373
    } else {
UNCOV
1374
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1375
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1376
    }
1377

1378
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1379
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
120,425✔
1380
      SStreamRawScanInfo* pInfo = pOperator->info;
117,859✔
1381
      SSnapContext*       sContext = pInfo->sContext;
117,859✔
1382
      SOperatorInfo*      p = NULL;
117,859✔
1383

1384
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
117,859✔
1385
      if (code != 0) {
117,859✔
UNCOV
1386
        return code;
×
1387
      }
1388

1389
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
117,859✔
1390

1391
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
117,859✔
UNCOV
1392
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1393
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1394
      }
1395

1396
      SMetaTableInfo mtInfo = {0};
117,815✔
1397
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
117,815✔
1398
      if (code != 0) {
117,647✔
1399
        destroyMetaTableInfo(&mtInfo);
UNCOV
1400
        return code;
×
1401
      }
1402
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
117,647✔
1403
      pInfo->dataReader = NULL;
117,621✔
1404

1405
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
117,621✔
1406
      tableListClear(pTableListInfo);
117,771✔
1407

1408
      if (mtInfo.uid == 0) {
117,859✔
1409
        destroyMetaTableInfo(&mtInfo);
1410
        goto end;  // no data
167✔
1411
      }
1412

1413
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
117,692✔
1414
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
117,567✔
1415
      if (code != TSDB_CODE_SUCCESS) {
117,692✔
UNCOV
1416
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1417
        destroyMetaTableInfo(&mtInfo);
UNCOV
1418
        return code;
×
1419
      }
1420
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
117,692✔
UNCOV
1421
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1422
      } else {
1423
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
117,604✔
1424
      }
1425

1426
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
117,499✔
1427
      if (code != TSDB_CODE_SUCCESS) {
117,692✔
1428
        destroyMetaTableInfo(&mtInfo);
UNCOV
1429
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1430
        return code;
×
1431
      }
1432

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

1447
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
235,384✔
1448
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
117,692✔
1449
      if (code != TSDB_CODE_SUCCESS) {
117,454✔
UNCOV
1450
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1451
        destroyMetaTableInfo(&mtInfo);
UNCOV
1452
        return code;
×
1453
      }
1454

1455
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
117,454✔
1456
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
117,524✔
1457
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1458
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
117,286✔
1459
      pTaskInfo->streamInfo.schema = mtInfo.schema;
117,330✔
1460
      taosMemoryFreeClear(mtInfo.pExtSchemas);
117,462✔
1461

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

1481
end:
364,266✔
1482
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
364,652✔
1483
  return 0;
364,652✔
1484
}
1485

1486
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
170,217,084✔
1487
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
170,217,084✔
1488
  if (pMsg->info.ahandle == NULL) {
170,225,106✔
1489
    qError("pMsg->info.ahandle is NULL");
1,435✔
1490
    return;
1,435✔
1491
  }
1492

1493
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
170,202,603✔
1494
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1495

1496
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
170,214,625✔
1497

1498
  if (pMsg->contLen > 0) {
170,223,066✔
1499
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
170,210,780✔
1500
    if (buf.pData == NULL) {
170,200,933✔
UNCOV
1501
      pMsg->code = terrno;
×
1502
    } else {
1503
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
170,200,933✔
1504
    }
1505
  }
1506

1507
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
170,218,331✔
1508
  rpcFreeCont(pMsg->pCont);
170,226,649✔
1509
  destroySendMsgInfo(pSendInfo);
170,203,906✔
1510
}
1511

UNCOV
1512
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1513
  int32_t        code = TSDB_CODE_SUCCESS;
×
1514
  int32_t        lino = 0;
×
1515
  SExecTaskInfo* pTaskInfo = tinfo;
×
1516
  SArray*        plist = NULL;
×
1517

UNCOV
1518
  code = getTableListInfo(pTaskInfo, &plist);
×
1519
  if (code || plist == NULL) {
×
1520
    return NULL;
×
1521
  }
1522

1523
  // only extract table in the first elements
UNCOV
1524
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1525

UNCOV
1526
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1527
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1528

UNCOV
1529
  int32_t numOfTables = 0;
×
1530
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1531
  QUERY_CHECK_CODE(code, lino, _end);
×
1532

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

UNCOV
1540
  taosArrayDestroy(plist);
×
1541

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

1550
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,421,120✔
1551
  int32_t        code = TSDB_CODE_SUCCESS;
3,421,120✔
1552
  int32_t        lino = 0;
3,421,120✔
1553
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,421,120✔
1554

1555
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,421,713✔
UNCOV
1556
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1557
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1558

UNCOV
1559
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1560
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1561
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,419,926✔
1562
    STableScanInfo* pScanInfo = pOperator->info;
1,709,370✔
1563

1564
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,710,556✔
1565
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,710,539✔
1566
  } else {
1567
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,712,936✔
1568
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,710,556✔
1569
    }
1570
  }
1571

UNCOV
1572
_end:
×
1573
  if (code != TSDB_CODE_SUCCESS) {
3,420,485✔
UNCOV
1574
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1575
  }
1576
  return code;
3,419,299✔
1577
}
1578

1579
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,712,343✔
1580
  if (pList == NULL) {
1,712,343✔
UNCOV
1581
    return TSDB_CODE_INVALID_PARA;
×
1582
  }
1583

1584
  *pList = NULL;
1,712,343✔
1585
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,712,936✔
1586
  if (pArray == NULL) {
1,710,539✔
UNCOV
1587
    return terrno;
×
1588
  }
1589

1590
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,710,539✔
1591
  if (code == 0) {
1,709,946✔
1592
    *pList = pArray;
1,709,946✔
1593
  } else {
UNCOV
1594
    taosArrayDestroy(pArray);
×
1595
  }
1596
  return code;
1,709,946✔
1597
}
1598

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

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

UNCOV
1615
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1616
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1617

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

UNCOV
1623
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1624
  int32_t code = 0;
×
1625
  return code;
×
1626
}
1627

UNCOV
1628
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1629
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1630
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1631
  return code;
×
1632
}
1633

1634
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
43,378,985✔
1635
  int32_t code = 0;
43,378,985✔
1636

1637
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
43,378,985✔
1638
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
43,376,210✔
1639

1640
  if (pOper->fpSet.resetStateFn) {
43,375,028✔
1641
    code = pOper->fpSet.resetStateFn(pOper);
43,375,457✔
1642
  }
1643
  pOper->status = OP_NOT_OPENED;
43,367,723✔
1644
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
73,727,147✔
1645
    code = clearStatesForOperator(pOper->pDownstream[i]);
30,354,773✔
1646
  }
1647
  return code;
43,378,539✔
1648
}
1649

1650
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
13,023,820✔
1651
  int32_t        code = 0;
13,023,820✔
1652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
13,023,820✔
1653
  SOperatorInfo* pOper = pTaskInfo->pRoot;
13,023,820✔
1654
  pTaskInfo->code = TSDB_CODE_SUCCESS;
13,023,820✔
1655
  code = clearStatesForOperator(pOper);
13,023,820✔
1656
  return code;
13,023,428✔
1657
}
1658

1659
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
17,385,906✔
1660
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
17,385,906✔
1661
  int64_t        threadId = taosGetSelfPthreadId();
17,385,906✔
1662
  int64_t        curOwner = 0;
17,385,906✔
1663

1664
  *ppRes = NULL;
17,385,906✔
1665

1666
  // todo extract method
1667
  taosRLockLatch(&pTaskInfo->lock);
17,385,906✔
1668
  bool isKilled = isTaskKilled(pTaskInfo);
17,385,906✔
1669
  if (isKilled) {
17,385,508✔
1670
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1671
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1672

UNCOV
1673
    taosRUnLockLatch(&pTaskInfo->lock);
×
1674
    return pTaskInfo->code;
×
1675
  }
1676

1677
  if (pTaskInfo->owner != 0) {
17,385,508✔
UNCOV
1678
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1679
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1680

UNCOV
1681
    taosRUnLockLatch(&pTaskInfo->lock);
×
1682
    return pTaskInfo->code;
×
1683
  }
1684

1685
  pTaskInfo->owner = threadId;
17,385,153✔
1686
  taosRUnLockLatch(&pTaskInfo->lock);
17,385,153✔
1687

1688
  if (pTaskInfo->cost.start == 0) {
17,385,906✔
1689
    pTaskInfo->cost.start = taosGetTimestampUs();
278,903✔
1690
  }
1691

1692
  // error occurs, record the error code and return to client
1693
  int32_t ret = setjmp(pTaskInfo->env);
17,385,906✔
1694
  if (ret != TSDB_CODE_SUCCESS) {
20,452,081✔
1695
    pTaskInfo->code = ret;
3,067,326✔
1696
    (void)cleanUpUdfs();
3,067,326✔
1697
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,067,326✔
1698
    atomic_store_64(&pTaskInfo->owner, 0);
3,067,326✔
1699
    return pTaskInfo->code;
3,067,326✔
1700
  }
1701

1702
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,384,755✔
1703

1704
  int64_t st = taosGetTimestampUs();
17,385,906✔
1705

1706
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,385,906✔
1707
  if (code) {
14,318,580✔
UNCOV
1708
    pTaskInfo->code = code;
×
1709
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1710
  } else {
1711
    *finished = *ppRes == NULL;
14,318,580✔
1712
    code = blockDataCheck(*ppRes);
14,318,580✔
1713
  }
1714
  if (code) {
14,318,182✔
UNCOV
1715
    pTaskInfo->code = code;
×
1716
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1717
  }
1718

1719
  uint64_t el = (taosGetTimestampUs() - st);
14,318,580✔
1720

1721
  pTaskInfo->cost.elapsedTime += el;
14,318,580✔
1722
  if (NULL == *ppRes) {
14,318,580✔
1723
    *useconds = pTaskInfo->cost.elapsedTime;
9,418,610✔
1724
  }
1725

1726
  (void)cleanUpUdfs();
14,318,580✔
1727

1728
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
14,318,580✔
1729
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
14,318,580✔
1730

1731
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
14,318,580✔
1732
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1733

1734
  atomic_store_64(&pTaskInfo->owner, 0);
14,318,580✔
1735
  return pTaskInfo->code;
14,318,580✔
1736
}
1737

1738
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1739
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1740
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1741
// }
1742

1743
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
16,529,517✔
1744
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1745
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1746
  int32_t code = 0;                                        
16,529,517✔
1747
  if (*pTableListInfo != NULL) {
16,529,517✔
UNCOV
1748
    qDebug("table list already exists, no need to create again");
×
1749
    goto end;
×
1750
  }
1751
  STableListInfo* pList = tableListCreate();
16,531,179✔
1752
  if (pList == NULL) {
16,533,111✔
UNCOV
1753
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1754
    code = terrno;
×
1755
    goto end;
×
1756
  }
1757

1758
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
16,533,111✔
1759
  SReadHandle    pHandle = {.vnode = pVnode};
16,533,885✔
1760
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
16,531,967✔
1761

1762
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
16,533,880✔
1763
  if (code != 0) {
16,535,037✔
UNCOV
1764
    tableListDestroy(pList);
×
1765
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1766
    goto end;
×
1767
  }
1768
  *pTableListInfo = pList;
16,535,037✔
1769

1770
end:
16,535,424✔
1771
  return 0;
16,535,424✔
1772
}
1773

1774
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
12,918✔
1775
  bool   listAdded = false;
12,918✔
1776
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
12,918✔
1777
  if (code == 0 && !listAdded) {
12,918✔
1778
    int32_t numOfTables = taosArrayGetSize(pUidList);
4,878✔
1779
    for (int i = 0; i < numOfTables; i++) {
9,756✔
1780
      void* tmp = taosArrayGet(pUidList, i);
4,878✔
1781
      if (tmp == NULL) {
4,878✔
UNCOV
1782
        return terrno;
×
1783
      }
1784
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
4,878✔
1785

1786
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
4,878✔
1787
      if (p == NULL) {
4,878✔
UNCOV
1788
        return terrno;
×
1789
      }
1790
    }
1791
  }
1792
  return code;
12,918✔
1793
}
1794

1795
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
12,918✔
1796
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1797
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1798
  int32_t code = TSDB_CODE_SUCCESS;
12,918✔
1799
  STableListInfo* pList = tableListCreate();
12,918✔
1800
  if (pList == NULL) {
12,918✔
UNCOV
1801
    code = terrno;
×
1802
    goto end;
×
1803
  }
1804
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
12,918✔
1805
  if (uidListCopy == NULL) {
12,918✔
UNCOV
1806
    code = terrno;
×
1807
    goto end;
×
1808
  }
1809
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
12,918✔
1810
  SReadHandle    pHandle = {.vnode = pVnode};
12,918✔
1811

1812
  pList->idInfo.suid = suid;
12,918✔
1813
  pList->idInfo.tableType = TD_SUPER_TABLE;
12,918✔
1814
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
12,918✔
1815
  if (code != TSDB_CODE_SUCCESS) {
12,918✔
UNCOV
1816
    goto end;
×
1817
  }                                              
1818
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
12,918✔
1819
  if (code != TSDB_CODE_SUCCESS) {
12,918✔
UNCOV
1820
    goto end;
×
1821
  }
1822
  *tableList = pList->pTableList;
12,918✔
1823
  pList->pTableList = NULL;
12,918✔
1824

1825
  taosArrayClear(uidList);
12,918✔
1826
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
25,836✔
1827
    void* tmp = taosArrayGet(uidListCopy, i);
12,918✔
1828
    if (tmp == NULL) {
12,918✔
UNCOV
1829
      continue;
×
1830
    }
1831
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
12,918✔
1832
    if (slot == NULL) {
12,918✔
1833
      if (taosArrayPush(uidList, tmp) == NULL) {
6,010✔
UNCOV
1834
        code = terrno;
×
1835
        goto end;
×
1836
      }
1837
    }
1838
  }
1839
end:
12,918✔
1840
  taosArrayDestroy(uidListCopy);
12,918✔
1841
  tableListDestroy(pList);
12,918✔
1842
  return code;
12,918✔
1843
}
1844

1845
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1846
//   int32_t index = -1;
1847
//   (void)taosThreadRwlockRdlock(lock);
1848
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1849
//     index = 0;
1850
//     goto end;
1851
//   }
1852
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1853
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1854

1855
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1856
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1857
//       index = i;
1858
//       goto end;
1859
//     }
1860
//   }
1861
// end:
1862
//   (void)taosThreadRwlockUnlock(lock);
1863
//   return index;
1864
// }
1865

1866
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
16,534,172✔
1867
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
16,535,037✔
1868
  STableListInfo* pList = pTableListInfo;
16,535,037✔
1869
  return pList->pTableList;
16,535,037✔
1870
}
1871

1872
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,721,429✔
1873

1874
void streamDestroyExecTask(qTaskInfo_t tInfo) {
5,662,600✔
1875
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
5,662,600✔
1876
  qDestroyTask(tInfo);
5,662,600✔
1877
}
5,662,600✔
1878

1879
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
831,601✔
1880
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
831,601✔
1881
}
1882

1883
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
871,536✔
1884
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1885
  int32_t      code = 0;
871,536✔
1886
  SNode*       pNode = 0;
871,536✔
1887
  SNodeList*   pList = NULL;
871,536✔
1888
  SExprInfo*   pExprInfo = NULL;
871,536✔
1889
  int32_t      numOfExprs = 1;
871,536✔
1890
  int32_t*     offset = 0;
871,536✔
1891
  STargetNode* pTargetNode = NULL;
871,536✔
1892
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
871,536✔
1893
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
871,536✔
1894

1895
  if (code == 0) {
871,536✔
1896
    pTargetNode->dataBlockId = 0;
871,536✔
1897
    pTargetNode->pExpr = pNode;
871,536✔
1898
    pTargetNode->slotId = 0;
871,536✔
1899
  }
1900
  if (code == 0) {
871,536✔
1901
    code = nodesMakeList(&pList);
871,536✔
1902
  }
1903
  if (code == 0) {
871,536✔
1904
    code = nodesListAppend(pList, (SNode*)pTargetNode);
871,536✔
1905
  }
1906
  if (code == 0) {
871,536✔
1907
    pNode = NULL;
871,536✔
1908
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
871,536✔
1909
  }
1910

1911
  if (code == 0) {
871,536✔
1912
    const char* pVal = NULL;
871,536✔
1913
    int32_t     len = 0;
871,536✔
1914
    SNode*      pSclNode = NULL;
871,536✔
1915
    switch (pExprInfo->pExpr->nodeType) {
871,536✔
1916
      case QUERY_NODE_FUNCTION:
871,536✔
1917
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
871,536✔
1918
        break;
871,536✔
UNCOV
1919
      case QUERY_NODE_OPERATOR:
×
UNCOV
1920
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
1921
        break;
×
UNCOV
1922
      default:
×
UNCOV
1923
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
1924
        break;
×
1925
    }
1926
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
871,536✔
1927
    SSDataBlock block = {0};
871,536✔
1928
    block.info.rows = 1;
871,536✔
1929
    SSDataBlock* pBlock = &block;
871,536✔
1930
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
871,536✔
1931
    if (tmp == NULL) {
871,536✔
UNCOV
1932
      code = terrno;
×
1933
    }
1934
    if (code == 0) {
871,536✔
1935
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
871,536✔
1936
    }
1937
    taosArrayDestroy(pBlockList);
871,536✔
1938
  }
1939
  nodesDestroyList(pList);
871,536✔
1940
  destroyExprInfo(pExprInfo, numOfExprs);
871,536✔
1941
  taosMemoryFreeClear(pExprInfo);
871,139✔
1942
  return code;
871,536✔
1943
}
1944

1945
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,540,789✔
1946
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,540,789✔
1947
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,540,789✔
1948
  int32_t        code = 0;
4,540,789✔
1949
  SNode*         pNode = NULL;
4,540,789✔
1950
  if (!pForceOutputCols) return 0;
4,540,789✔
1951
  if (!*pRes) {
39,538✔
1952
    code = createDataBlock(pRes);
39,538✔
1953
  }
1954

1955
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
39,538✔
1956
    int32_t idx = 0;
39,538✔
1957
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
157,770✔
1958
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
118,232✔
1959
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
118,232✔
1960
      colInfo.info.precision = pCol->type.precision;
118,232✔
1961
      colInfo.info.scale = pCol->type.scale;
118,232✔
1962
      code = blockDataAppendColInfo(*pRes, &colInfo);
118,232✔
1963
      if (code != 0) break;
118,232✔
1964
    }
1965
  }
1966

1967
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
39,538✔
1968
  if (code != TSDB_CODE_SUCCESS) {
39,538✔
UNCOV
1969
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
1970
    return code;
×
1971
  }
1972

1973
  // loop all exprs for force output, execute all exprs
1974
  int32_t idx = 0;
39,538✔
1975
  int32_t rowIdx = (*pRes)->info.rows;
39,538✔
1976
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
39,538✔
1977
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
39,538✔
1978
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
157,770✔
1979
    SScalarParam   dst = {0};
118,232✔
1980
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
118,232✔
1981
    code = nodesStringToNode(pCol->expr, &pNode);
118,232✔
1982
    if (code != 0) break;
118,232✔
1983
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
118,232✔
1984
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
118,232✔
1985
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
78,694✔
1986
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
78,694✔
1987
    } else {
1988
      dst.columnData = pInfo;
39,538✔
1989
      dst.numOfRows = rowIdx;
39,538✔
1990
      dst.colAlloced = false;
39,538✔
1991
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
39,538✔
1992
    }
1993
    ++idx;
118,232✔
1994
    // TODO sclFreeParam(&dst);
1995
    nodesDestroyNode(pNode);
118,232✔
1996
    if (code != 0) break;
118,232✔
1997
  }
1998
  if (code == TSDB_CODE_SUCCESS) {
39,538✔
1999
    (*pRes)->info.rows++;
39,538✔
2000
  }
2001
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
39,538✔
2002
  return code;
39,538✔
2003
}
2004

2005
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
311,281✔
2006
  int32_t      code = 0;
311,281✔
2007
  const char*  pVal = NULL;
311,281✔
2008
  SScalarParam dst = {0};
311,281✔
2009
  int32_t      len = 0;
311,281✔
2010
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
311,281✔
2011
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
311,281✔
2012
  // execute the expr
2013
  switch (pExpr->type) {
311,281✔
UNCOV
2014
    case QUERY_NODE_VALUE: {
×
2015
      SValueNode* pValue = (SValueNode*)pExpr;
×
2016
      int32_t     type = pValue->node.resType.type;
×
2017
      if (!IS_STR_DATA_TYPE(type)) {
×
UNCOV
2018
        qError("invalid sub tb expr with non-str type");
×
UNCOV
2019
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2020
        break;
×
2021
      }
2022
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2023
      if (pTmp == NULL) {
×
UNCOV
2024
        qError("invalid sub tb expr with null value");
×
UNCOV
2025
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2026
        break;
×
2027
      }
UNCOV
2028
      pVal = varDataVal(pTmp);
×
UNCOV
2029
      len = varDataLen(pTmp);
×
UNCOV
2030
    } break;
×
2031
    case QUERY_NODE_FUNCTION: {
311,281✔
2032
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
311,281✔
2033
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
311,281✔
2034
        qError("invalid sub tb expr with non-str type func");
×
2035
        code = TSDB_CODE_INVALID_PARA;
×
2036
        break;
×
2037
      }
2038
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
311,281✔
2039
      if (!pCol) {
311,281✔
UNCOV
2040
        code = terrno;
×
UNCOV
2041
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2042
        break;
×
2043
      }
2044

2045
      pCol->hasNull = true;
311,281✔
2046
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
311,281✔
2047
      pCol->info.colId = 0;
311,281✔
2048
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
311,281✔
2049
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
311,281✔
2050
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
311,281✔
2051
      code = colInfoDataEnsureCapacity(pCol, 1, true);
311,281✔
2052
      if (code != 0) {
311,281✔
2053
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2054
        taosMemoryFree(pCol);
×
UNCOV
2055
        break;
×
2056
      }
2057
      dst.columnData = pCol;
311,281✔
2058
      dst.numOfRows = 1;
311,281✔
2059
      dst.colAlloced = true;
311,281✔
2060
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
311,281✔
2061
      if (colDataIsNull_var(dst.columnData, 0)) {
311,281✔
2062
        qInfo("invalid sub tb expr with null value");
397✔
UNCOV
2063
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
2064
      }
2065
      if (code == 0) {
310,884✔
2066
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
310,884✔
2067
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
310,884✔
2068
      }
2069
    } break;
311,281✔
2070
    default:
×
UNCOV
2071
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2072
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2073
      break;
×
2074
  }
2075
  if (code == 0) {
311,281✔
2076
    if (!pVal || len == 0) {
310,884✔
UNCOV
2077
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2078
      code = TSDB_CODE_INVALID_PARA;
×
2079
    }
2080
    if(len > TSDB_TABLE_NAME_LEN - 1) {
310,884✔
2081
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
770✔
2082
      len = TSDB_TABLE_NAME_LEN - 1;
770✔
2083
    }
2084

2085
    memcpy(tbname, pVal, len);
310,884✔
2086
    tbname[len] = '\0';  // ensure null terminated
310,884✔
2087
    if (NULL != strchr(tbname, '.')) {
311,281✔
UNCOV
2088
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
UNCOV
2089
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2090
    }
2091
  }
2092
  // TODO free dst
2093
  sclFreeParam(&dst);
311,678✔
2094
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
311,281✔
2095
  return code;
311,281✔
2096
}
2097

2098
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
269,687✔
2099
  if (pParam) {
269,687✔
2100
    if (pParam->tbname) {
269,687✔
2101
      taosMemFree(pParam->tbname);
269,687✔
2102
      pParam->tbname = NULL;
269,687✔
2103
    }
2104
    if (pParam->stbname) {
269,687✔
2105
      taosMemFree(pParam->stbname);
269,687✔
2106
      pParam->stbname = NULL;
269,687✔
2107
    }
2108
    if (pParam->dbFName) {
269,687✔
2109
      taosMemFree(pParam->dbFName);
269,687✔
2110
      pParam->dbFName = NULL;
269,687✔
2111
    }
2112
    if (pParam->pFields) {
269,687✔
2113
      taosArrayDestroy(pParam->pFields);
269,687✔
2114
      pParam->pFields = NULL;
269,687✔
2115
    }
2116
    if (pParam->pTagFields) {
269,687✔
2117
      taosArrayDestroy(pParam->pTagFields);
175,527✔
2118
      pParam->pTagFields = NULL;
175,527✔
2119
    }
2120
    taosMemFree(pParam);
269,687✔
2121
  }
2122
}
269,687✔
2123

2124
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
269,687✔
2125
  int32_t code = 0, lino = 0;
269,687✔
2126
  if (ppDst == NULL || pSrc == NULL) {
269,687✔
UNCOV
2127
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2128
  }
2129
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
269,687✔
2130
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
269,297✔
2131

2132
  (*ppDst)->suid = pSrc->suid;
269,324✔
2133
  (*ppDst)->sver = pSrc->sver;
269,324✔
2134
  (*ppDst)->tbType = pSrc->tbType;
269,324✔
2135
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
269,687✔
2136
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
269,297✔
2137

2138
  if (pSrc->stbname) {
268,934✔
2139
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
268,934✔
2140
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
269,687✔
2141
  }
2142

2143
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
269,687✔
2144
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
269,687✔
2145

2146
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
269,324✔
2147

2148
  if (pSrc->pFields && pSrc->pFields->size > 0) {
269,324✔
2149
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
269,297✔
2150
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
269,687✔
2151
  } else {
UNCOV
2152
    (*ppDst)->pFields = NULL;
×
2153
  }
2154
  
2155
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
269,324✔
2156
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
175,527✔
2157
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
175,527✔
2158
  } else {
2159
    (*ppDst)->pTagFields = NULL;
93,743✔
2160
  }
2161

2162
_exit:
269,687✔
2163

2164
  if (code != 0) {
269,687✔
UNCOV
2165
    if (*ppDst) {
×
UNCOV
2166
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2167
      *ppDst = NULL;
×
2168
    }
2169
    
UNCOV
2170
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2171
  }
2172
  return code;
268,907✔
2173
}
2174

2175
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
418✔
2176
  return doDropStreamTable(pMsgCb, pOutput, pReq);
418✔
2177
}
2178

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