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

taosdata / TDengine / #4848

12 Nov 2025 01:06AM UTC coverage: 63.27% (+0.6%) from 62.651%
#4848

push

travis-ci

web-flow
Merge f12882a7a into e27395247

33 of 36 new or added lines in 4 files covered. (91.67%)

2652 existing lines in 104 files now uncovered.

138980 of 219661 relevant lines covered (63.27%)

110230098.27 hits per line

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

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

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

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

64
static void initRefPool() {
623,718✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
623,718✔
66
  (void)atexit(cleanupRefPool);
623,718✔
67
}
623,718✔
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) {
×
UNCOV
74
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
UNCOV
75
      return TSDB_CODE_APP_ERROR;
×
76
    }
77

78
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
UNCOV
79
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
80
      return TSDB_CODE_APP_ERROR;
×
81
    }
UNCOV
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));
×
UNCOV
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};
×
UNCOV
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));
×
UNCOV
116
        void*        tmp = taosArrayPush(pInfo->pBlockLists, pReq);
×
117
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
118
      }
UNCOV
119
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
×
120
    }
121

UNCOV
122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

125
_end:
×
UNCOV
126
  if (code != TSDB_CODE_SUCCESS) {
×
127
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
128
  }
UNCOV
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) {
×
UNCOV
135
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
UNCOV
136
      return TSDB_CODE_APP_ERROR;
×
137
    }
138

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,080,897✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,080,897✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,081,804✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,077,724✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,077,724✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,077,724✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,077,724✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
54,307✔
158
        if (code) {
54,185✔
UNCOV
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
UNCOV
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,003,724✔
166
  }
167

168
  return 0;
10,377,136✔
169
}
170

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

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

180
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
UNCOV
181
  SExecTaskInfo* pTaskInfo = tinfo;
×
UNCOV
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) {
×
UNCOV
186
  if (tinfo == NULL) {
×
UNCOV
187
    return TSDB_CODE_APP_ERROR;
×
188
  }
189

UNCOV
190
  if (pBlocks == NULL || numOfBlocks == 0) {
×
UNCOV
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));
×
UNCOV
197
  if (code != TSDB_CODE_SUCCESS) {
×
198
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
199
  } else {
UNCOV
200
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
201
  }
202

UNCOV
203
  return code;
×
204
}
205

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

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

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

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

227
  SSubplan* pPlan = NULL;
116,807✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
116,807✔
229
  if (code != TSDB_CODE_SUCCESS) {
116,807✔
UNCOV
230
    terrno = code;
×
UNCOV
231
    return NULL;
×
232
  }
233

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,430,154✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,313,347✔
249
    if (pSlotDesc->output) {
1,313,347✔
250
      ++(*numOfCols);
1,313,253✔
251
    }
252
  }
253

254
  return pTaskInfo;
116,807✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
605,407✔
258
  if (streamInserterParam == NULL) {
605,407✔
259
    return TSDB_CODE_SUCCESS;
305,722✔
260
  }
261

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

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

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

278
  return TSDB_CODE_SUCCESS;
299,685✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
299,685✔
329
    if (code) {
299,298✔
UNCOV
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,
604,600✔
334
         tstrerror(code));
335

336
_error:
144,814✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,700,513✔
348
  if (pInfo == NULL) {
5,700,513✔
UNCOV
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,700,513✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,700,513✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,700,513✔
354
    return false;
4,740✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,695,773✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,556,356✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,252,129✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

381
  void*           info = pOperator->info;
5,695,773✔
382
  STableScanBase* pScanBaseInfo = NULL;
5,695,773✔
383

384
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,695,773✔
385
    pScanBaseInfo = &((STableScanInfo*)info)->base;
139,417✔
386
    setReadHandle(handle, pScanBaseInfo);
139,417✔
387
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,556,356✔
388
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,330,948✔
389
    setReadHandle(handle, pScanBaseInfo);
5,330,948✔
390
  }
391

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

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

403
  *pTaskInfo = NULL;
605,407✔
404

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

420
  return code;
604,600✔
421
}
422

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

435
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
64,004✔
436

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

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

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

455
    tDecoderClear(&mr.coder);
65,075✔
456

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

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

483
      if (!qualified) {
57,154✔
484
        continue;
28,604✔
485
      }
486
    }
487

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

493
_end:
64,004✔
494

495
  pAPI->metaReaderFn.clearReader(&mr);
64,004✔
496
  (*ppArrayRes) = qa;
64,004✔
497

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

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

510
  if (isAdd) {
64,623✔
511
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
64,004✔
512
  }
513

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

521
  SStreamScanInfo* pScanInfo = pInfo->info;
64,623✔
522
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
64,623✔
523
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
129,901✔
524
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
65,278✔
525
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
65,278✔
526
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
65,278✔
527
    }
528
  }
529

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

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

553
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
64,004✔
554
    taosWLockLatch(&pTaskInfo->lock);
64,004✔
555

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

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

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

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

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

603
  return code;
64,623✔
604
}
605

606
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
290,460,563✔
607
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
608
                                    int32_t idx, bool* tbGet) {
609
  *tbGet = false;
290,460,563✔
610

611
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
290,466,473✔
612
    return TSDB_CODE_INVALID_PARA;
2,254✔
613
  }
614
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
290,464,798✔
615

616
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
290,464,798✔
617
    return TSDB_CODE_SUCCESS;
171,201,073✔
618
  }
619

620
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
119,264,823✔
621
  if (!pSchemaInfo) {
119,269,831✔
UNCOV
622
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
623
    return terrno;
×
624
  }
625

626
  *sversion = pSchemaInfo->sw->version;
119,269,831✔
627
  *tversion = pSchemaInfo->tversion;
119,270,621✔
628
  *rversion = pSchemaInfo->rversion;
119,273,838✔
629
  if (pSchemaInfo->dbname) {
119,258,698✔
630
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
119,260,528✔
631
  } else {
UNCOV
632
    dbName[0] = 0;
×
633
  }
634
  if (pSchemaInfo->tablename) {
119,271,937✔
635
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
119,269,630✔
636
  } else {
637
    tableName[0] = 0;
183✔
638
  }
639

640
  *tbGet = true;
119,271,578✔
641

642
  return TSDB_CODE_SUCCESS;
119,263,836✔
643
}
644

645
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
171,194,778✔
646

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

654
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
37,755,088✔
655
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
37,755,088✔
656
  ((SExecTaskInfo*)tinfo)->paramSet = false;
37,755,088✔
657
}
37,755,088✔
658

659
int32_t qExecutorInit(void) {
4,588,317✔
660
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,588,317✔
661
  return TSDB_CODE_SUCCESS;
4,588,605✔
662
}
663

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

670
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
173,089,764✔
671

672
  readHandle->uid = 0;
173,107,652✔
673
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
173,116,307✔
674
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
173,069,750✔
675
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
131,898✔
676
    goto _error;
27,150✔
677
  }
678

679
  if (handle) {
172,951,940✔
680
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
172,897,683✔
681
    void*           pSinkManager = NULL;
172,915,570✔
682
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
172,820,889✔
683
    if (code != TSDB_CODE_SUCCESS) {
172,835,070✔
UNCOV
684
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
UNCOV
685
      goto _error;
×
686
    }
687

688
    void* pSinkParam = NULL;
172,835,070✔
689
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
172,907,826✔
690
    if (code != TSDB_CODE_SUCCESS) {
172,839,034✔
691
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
UNCOV
692
      taosMemoryFree(pSinkManager);
×
UNCOV
693
      goto _error;
×
694
    }
695

696
    SDataSinkNode* pSink = NULL;
172,839,034✔
697
    if (readHandle->localExec) {
172,895,585✔
698
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
71,838✔
699
      if (code != TSDB_CODE_SUCCESS) {
71,838✔
700
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
701
               (*pTask)->id.str);
UNCOV
702
        taosMemoryFree(pSinkManager);
×
UNCOV
703
        goto _error;
×
704
      }
705
    }
706

707
    // pSinkParam has been freed during create sinker.
708
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
172,831,394✔
709
                              (*pTask)->id.str, pSubplan->processOneBlock);
172,918,800✔
710
    if (code) {
172,866,568✔
711
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
619✔
712
    }
713
  }
714

715
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
173,037,634✔
716
         tstrerror(code));
717

718
_error:
9,078,081✔
719
  // if failed to add ref for all tables in this query, abort current query
720
  return code;
173,106,326✔
721
}
722

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

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

735
  if (pLocal) {
233,403,836✔
736
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
227,367,114✔
737
  }
738

739
  taosArrayClear(pResList);
233,401,371✔
740

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

748
  if (pTaskInfo->cost.start == 0) {
233,396,323✔
749
    pTaskInfo->cost.start = taosGetTimestampUs();
168,974,543✔
750
  }
751

752
  if (isTaskKilled(pTaskInfo)) {
233,413,984✔
753
    atomic_store_64(&pTaskInfo->owner, 0);
203✔
754
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
203✔
755
    return pTaskInfo->code;
203✔
756
  }
757

758
  // error occurs, record the error code and return to client
759
  int32_t ret = setjmp(pTaskInfo->env);
233,394,763✔
760
  if (ret != TSDB_CODE_SUCCESS) {
233,612,534✔
761
    pTaskInfo->code = ret;
231,163✔
762
    (void)cleanUpUdfs();
231,163✔
763

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

767
    return pTaskInfo->code;
231,163✔
768
  }
769

770
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
233,381,371✔
771

772
  int32_t      current = 0;
233,384,436✔
773
  SSDataBlock* pRes = NULL;
233,384,436✔
774
  int64_t      st = taosGetTimestampUs();
233,411,725✔
775

776
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
233,411,725✔
777
    pTaskInfo->paramSet = true;
37,755,088✔
778
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,755,088✔
779
  } else {
780
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
195,655,217✔
781
  }
782

783
  QUERY_CHECK_CODE(code, lino, _end);
233,176,975✔
784
  code = blockDataCheck(pRes);
233,176,975✔
785
  QUERY_CHECK_CODE(code, lino, _end);
233,181,759✔
786

787
  if (pRes == NULL) {
233,181,759✔
788
    st = taosGetTimestampUs();
42,812,528✔
789
  }
790

791
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
233,180,925✔
792
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
233,186,217✔
793
    rowsThreshold = 4096;
232,395,646✔
794
  }
795

796
  int32_t blockIndex = 0;
233,183,015✔
797
  while (pRes != NULL) {
613,956,536✔
798
    SSDataBlock* p = NULL;
434,412,902✔
799
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
434,405,380✔
800
      SSDataBlock* p1 = NULL;
297,454,672✔
801
      code = createOneDataBlock(pRes, true, &p1);
297,455,198✔
802
      QUERY_CHECK_CODE(code, lino, _end);
297,451,552✔
803

804
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
297,451,552✔
805
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
297,455,434✔
806
      p = p1;
297,455,434✔
807
    } else {
808
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
136,977,271✔
809
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
136,976,494✔
810

811
      p = *(SSDataBlock**)tmp;
136,976,494✔
812
      code = copyDataBlock(p, pRes);
136,976,103✔
813
      QUERY_CHECK_CODE(code, lino, _end);
136,977,922✔
814
    }
815

816
    blockIndex += 1;
434,431,641✔
817

818
    current += p->info.rows;
434,431,641✔
819
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
434,433,942✔
820
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
821
    void* tmp = taosArrayPush(pResList, &p);
434,433,871✔
822
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
434,433,871✔
823

824
    if (current >= rowsThreshold || processOneBlock) {
434,433,871✔
825
      break;
826
    }
827

828
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
380,795,227✔
829
    QUERY_CHECK_CODE(code, lino, _end);
380,786,678✔
830
    code = blockDataCheck(pRes);
380,786,678✔
831
    QUERY_CHECK_CODE(code, lino, _end);
380,714,326✔
832
  }
833

834
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
233,182,278✔
835
    pTaskInfo->pSubplan->rowsThreshold -= current;
787,426✔
836
  }
837

838
  *hasMore = (pRes != NULL);
233,176,732✔
839
  uint64_t el = (taosGetTimestampUs() - st);
233,172,881✔
840

841
  pTaskInfo->cost.elapsedTime += el;
233,172,881✔
842
  if (NULL == pRes) {
233,173,886✔
843
    *useconds = pTaskInfo->cost.elapsedTime;
179,534,675✔
844
  }
845

846
_end:
233,060,537✔
847
  (void)cleanUpUdfs();
233,172,446✔
848

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

853
  atomic_store_64(&pTaskInfo->owner, 0);
233,186,267✔
854
  if (code) {
233,187,765✔
UNCOV
855
    pTaskInfo->code = code;
×
UNCOV
856
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
857
  }
858

859
  return pTaskInfo->code;
233,187,765✔
860
}
861

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

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

876
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
39,102,680✔
877
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,102,680✔
878
  int64_t        threadId = taosGetSelfPthreadId();
39,102,680✔
879
  int64_t        curOwner = 0;
39,102,779✔
880

881
  *pRes = NULL;
39,102,779✔
882

883
  // todo extract method
884
  taosRLockLatch(&pTaskInfo->lock);
39,102,779✔
885
  bool isKilled = isTaskKilled(pTaskInfo);
39,102,983✔
886
  if (isKilled) {
39,102,782✔
887
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
888

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

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

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

901
  pTaskInfo->owner = threadId;
39,102,581✔
902
  taosRUnLockLatch(&pTaskInfo->lock);
39,102,479✔
903

904
  if (pTaskInfo->cost.start == 0) {
39,102,986✔
905
    pTaskInfo->cost.start = taosGetTimestampUs();
93,510✔
906
  }
907

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

918
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
39,101,969✔
919

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

933
  code = blockDataCheck(*pRes);
39,096,135✔
934
  if (code) {
39,102,269✔
UNCOV
935
    pTaskInfo->code = code;
×
UNCOV
936
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
937
  }
938

939
  uint64_t el = (taosGetTimestampUs() - st);
39,098,864✔
940

941
  pTaskInfo->cost.elapsedTime += el;
39,098,864✔
942
  if (NULL == *pRes) {
39,101,410✔
943
    *useconds = pTaskInfo->cost.elapsedTime;
3,753,021✔
944
  }
945

946
  (void)cleanUpUdfs();
39,101,532✔
947

948
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
39,103,085✔
949
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
39,103,085✔
950

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

954
  atomic_store_64(&pTaskInfo->owner, 0);
39,102,488✔
955
  return pTaskInfo->code;
39,103,085✔
956
}
957

958
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
58,219,836✔
959
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
58,219,836✔
960
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
58,220,032✔
961
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
58,220,032✔
962

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

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

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

UNCOV
980
  return 0;
×
981
}
982

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

992
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
39,194✔
993
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
39,194✔
994

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

1017
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
39,194✔
1018
}
39,194✔
1019

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

1026
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
39,194✔
1027

1028
  setTaskKilled(pTaskInfo, rspCode);
39,194✔
1029
  qStopTaskOperators(pTaskInfo);
39,194✔
1030

1031
  return TSDB_CODE_SUCCESS;
39,194✔
1032
}
1033

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

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

1047
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1048

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

1055
        taosMsleep(200);
×
1056

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

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

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

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

1087
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
173,717,095✔
1088
  STaskCostInfo* pSummary = &pTaskInfo->cost;
173,717,095✔
1089
  int64_t        idleTime = pSummary->start - pSummary->created;
173,718,488✔
1090

1091
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
173,718,275✔
1092
  if (pSummary->pRecoder != NULL) {
173,716,034✔
1093
    qDebug(
121,568,397✔
1094
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1095
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1096
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1097
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1098
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1099
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1100
  } else {
1101
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
52,146,870✔
1102
           pSummary->elapsedTime / 1000.0);
1103
  }
1104
}
173,715,267✔
1105

1106
void qDestroyTask(qTaskInfo_t qTaskHandle) {
178,850,564✔
1107
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
178,850,564✔
1108
  if (pTaskInfo == NULL) {
178,850,564✔
1109
    return;
5,135,724✔
1110
  }
1111

1112
  if (pTaskInfo->pRoot != NULL) {
173,714,840✔
1113
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
173,717,815✔
1114
  } else {
UNCOV
1115
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1116
  }
1117

1118
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
173,713,876✔
1119
  doDestroyTask(pTaskInfo);
173,716,657✔
1120
}
1121

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

1127
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
116,807✔
1128
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
116,807✔
1129
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
116,807✔
1130

1131
  while (1) {
114,061✔
1132
    uint16_t type = pOperator->operatorType;
230,868✔
1133
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
230,868✔
1134
      *scanner = pOperator->info;
116,807✔
1135
      break;
116,807✔
1136
    } else {
1137
      pOperator = pOperator->pDownstream[0];
114,061✔
1138
    }
1139
  }
1140
}
116,807✔
1141

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

1155
void* qExtractReaderFromTmqScanner(void* scanner) {
116,807✔
1156
  SStreamScanInfo* pInfo = scanner;
116,807✔
1157
  return (void*)pInfo->tqReader;
116,807✔
1158
}
1159

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

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

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

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

1186
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
140,423✔
1187
  memset(pCond, 0, sizeof(SQueryTableDataCond));
140,423✔
1188
  pCond->order = TSDB_ORDER_ASC;
140,423✔
1189
  pCond->numOfCols = pMtInfo->schema->nCols;
140,385✔
1190
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
140,294✔
1191
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
140,476✔
1192
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
140,529✔
1193
    taosMemoryFreeClear(pCond->colList);
144✔
UNCOV
1194
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1195
    return terrno;
×
1196
  }
1197

1198
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
140,304✔
1199
  pCond->suid = pMtInfo->suid;
140,423✔
1200
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
140,251✔
1201
  pCond->startVersion = -1;
140,438✔
1202
  pCond->endVersion = sContext->snapVersion;
140,400✔
1203

1204
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
897,707✔
1205
    SColumnInfo* pColInfo = &pCond->colList[i];
757,307✔
1206
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
757,115✔
1207
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
757,206✔
1208
    if (pMtInfo->pExtSchemas != NULL) {
757,413✔
1209
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,484✔
1210
    }
1211
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
757,312✔
1212
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
757,148✔
1213

1214
    pCond->pSlotList[i] = i;
757,259✔
1215
  }
1216

1217
  return TSDB_CODE_SUCCESS;
140,491✔
1218
}
1219

1220
void qStreamSetOpen(qTaskInfo_t tinfo) {
38,820,002✔
1221
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,820,002✔
1222
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
38,820,002✔
1223
  pOperator->status = OP_NOT_OPENED;
38,824,440✔
1224
}
38,823,650✔
1225

1226
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,021,492✔
1227
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,021,492✔
1228
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,021,492✔
1229
}
4,021,914✔
1230

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

1236
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,228,855✔
1237
  const char*    id = GET_TASKID(pTaskInfo);
4,228,667✔
1238

1239
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,228,603✔
1240
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
3,960,312✔
1241
    if (pOperator == NULL || code != 0) {
3,960,602✔
1242
      return code;
87✔
1243
    }
1244

1245
    SStreamScanInfo* pInfo = pOperator->info;
3,960,515✔
1246
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
3,959,980✔
1247
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
3,960,715✔
1248
    walReaderVerifyOffset(pWalReader, pOffset);
3,961,140✔
1249
  }
1250
  // if pOffset equal to current offset, means continue consume
1251
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,228,971✔
1252
    return 0;
3,792,356✔
1253
  }
1254

1255
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
436,048✔
1256
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
292,194✔
1257
    if (pOperator == NULL || code != 0) {
292,282✔
UNCOV
1258
      return code;
×
1259
    }
1260

1261
    SStreamScanInfo* pInfo = pOperator->info;
292,282✔
1262
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
292,141✔
1263
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
292,194✔
1264
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
292,068✔
1265

1266
    if (pOffset->type == TMQ_OFFSET__LOG) {
292,194✔
1267
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
231,836✔
1268
      pScanBaseInfo->dataReader = NULL;
231,783✔
1269

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

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

1295
      if (uid == 0) {
60,446✔
1296
        if (numOfTables != 0) {
59,190✔
1297
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
9,837✔
1298
          if (!tmp) {
9,837✔
1299
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1300
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1301
            return terrno;
×
1302
          }
1303
          if (tmp) uid = tmp->uid;
9,837✔
1304
          ts = INT64_MIN;
9,837✔
1305
          pScanInfo->currentTable = 0;
9,837✔
1306
        } else {
1307
          taosRUnLockLatch(&pTaskInfo->lock);
49,353✔
1308
          qError("no table in table list, %s", id);
49,216✔
1309
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
49,353✔
1310
        }
1311
      }
1312
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,093✔
1313

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

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

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

1332
      STableKeyInfo keyInfo = {.uid = uid};
11,093✔
1333
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
11,093✔
1334

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

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

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

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

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

1377
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1378
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
143,854✔
1379
      SStreamRawScanInfo* pInfo = pOperator->info;
140,673✔
1380
      SSnapContext*       sContext = pInfo->sContext;
140,764✔
1381
      SOperatorInfo*      p = NULL;
140,764✔
1382

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

1388
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
140,764✔
1389

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

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

1404
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
140,711✔
1405
      tableListClear(pTableListInfo);
140,605✔
1406

1407
      if (mtInfo.uid == 0) {
140,711✔
1408
        destroyMetaTableInfo(&mtInfo);
1409
        goto end;  // no data
182✔
1410
      }
1411

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

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

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

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

1454
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
140,244✔
1455
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
140,307✔
1456
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1457
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
140,244✔
1458
      pTaskInfo->streamInfo.schema = mtInfo.schema;
140,206✔
1459
      taosMemoryFreeClear(mtInfo.pExtSchemas);
140,519✔
1460

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

1480
end:
380,286✔
1481
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
380,595✔
1482
  return 0;
380,686✔
1483
}
1484

1485
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
156,959,327✔
1486
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
156,959,327✔
1487
  if (pMsg->info.ahandle == NULL) {
156,965,144✔
UNCOV
1488
    qError("pMsg->info.ahandle is NULL");
×
UNCOV
1489
    return;
×
1490
  }
1491

1492
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
156,946,844✔
1493
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1494

1495
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
156,949,001✔
1496

1497
  if (pMsg->contLen > 0) {
156,961,037✔
1498
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
156,947,524✔
1499
    if (buf.pData == NULL) {
156,941,600✔
UNCOV
1500
      pMsg->code = terrno;
×
1501
    } else {
1502
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
156,941,600✔
1503
    }
1504
  }
1505

1506
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
156,968,805✔
1507
  rpcFreeCont(pMsg->pCont);
156,967,604✔
1508
  destroySendMsgInfo(pSendInfo);
156,952,912✔
1509
}
1510

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

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

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

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

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

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

1539
  taosArrayDestroy(plist);
×
1540

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

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

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

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

1563
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,694,457✔
1564
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,695,663✔
1565
  } else {
1566
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,695,057✔
1567
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,695,657✔
1568
    }
1569
  }
1570

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

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

1583
  *pList = NULL;
1,695,657✔
1584
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,696,257✔
1585
  if (pArray == NULL) {
1,695,069✔
UNCOV
1586
    return terrno;
×
1587
  }
1588

1589
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,695,069✔
1590
  if (code == 0) {
1,694,475✔
1591
    *pList = pArray;
1,694,475✔
1592
  } else {
UNCOV
1593
    taosArrayDestroy(pArray);
×
1594
  }
1595
  return code;
1,694,475✔
1596
}
1597

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

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

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

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

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

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

1633
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
58,006,975✔
1634
  int32_t code = 0;
58,006,975✔
1635

1636
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
58,006,975✔
1637
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
58,005,859✔
1638

1639
  if (pOper->fpSet.resetStateFn) {
58,004,620✔
1640
    code = pOper->fpSet.resetStateFn(pOper);
58,005,436✔
1641
  }
1642
  pOper->status = OP_NOT_OPENED;
58,006,611✔
1643
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
101,054,255✔
1644
    code = clearStatesForOperator(pOper->pDownstream[i]);
43,040,185✔
1645
  }
1646
  return code;
58,014,429✔
1647
}
1648

1649
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
14,970,737✔
1650
  int32_t        code = 0;
14,970,737✔
1651
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
14,970,737✔
1652
  SOperatorInfo* pOper = pTaskInfo->pRoot;
14,970,737✔
1653
  pTaskInfo->code = TSDB_CODE_SUCCESS;
14,971,152✔
1654
  code = clearStatesForOperator(pOper);
14,971,152✔
1655
  return code;
14,970,729✔
1656
}
1657

1658
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
19,042,775✔
1659
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
19,042,775✔
1660
  int64_t        threadId = taosGetSelfPthreadId();
19,042,775✔
1661
  int64_t        curOwner = 0;
19,042,775✔
1662

1663
  *ppRes = NULL;
19,042,775✔
1664

1665
  // todo extract method
1666
  taosRLockLatch(&pTaskInfo->lock);
19,042,775✔
1667
  bool isKilled = isTaskKilled(pTaskInfo);
19,043,190✔
1668
  if (isKilled) {
19,042,799✔
1669
    // clearStreamBlock(pTaskInfo->pRoot);
1670
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1671

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

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

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

1684
  pTaskInfo->owner = threadId;
19,042,376✔
1685
  taosRUnLockLatch(&pTaskInfo->lock);
19,042,376✔
1686

1687
  if (pTaskInfo->cost.start == 0) {
19,043,190✔
1688
    pTaskInfo->cost.start = taosGetTimestampUs();
308,237✔
1689
  }
1690

1691
  // error occurs, record the error code and return to client
1692
  int32_t ret = setjmp(pTaskInfo->env);
19,043,190✔
1693
  if (ret != TSDB_CODE_SUCCESS) {
25,121,550✔
1694
    pTaskInfo->code = ret;
6,079,198✔
1695
    (void)cleanUpUdfs();
6,079,589✔
1696
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
6,079,589✔
1697
    atomic_store_64(&pTaskInfo->owner, 0);
6,079,589✔
1698
    return pTaskInfo->code;
6,079,589✔
1699
  }
1700

1701
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
19,042,352✔
1702

1703
  int64_t st = taosGetTimestampUs();
19,043,190✔
1704

1705
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
19,043,190✔
1706
  if (code) {
12,963,176✔
UNCOV
1707
    pTaskInfo->code = code;
×
UNCOV
1708
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1709
  } else {
1710
    *finished = *ppRes == NULL;
12,963,176✔
1711
    code = blockDataCheck(*ppRes);
12,963,601✔
1712
  }
1713
  if (code) {
12,963,601✔
UNCOV
1714
    pTaskInfo->code = code;
×
UNCOV
1715
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1716
  }
1717

1718
  uint64_t el = (taosGetTimestampUs() - st);
12,963,601✔
1719

1720
  pTaskInfo->cost.elapsedTime += el;
12,963,601✔
1721
  if (NULL == *ppRes) {
12,963,601✔
1722
    *useconds = pTaskInfo->cost.elapsedTime;
8,616,119✔
1723
  }
1724

1725
  (void)cleanUpUdfs();
12,963,601✔
1726

1727
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
12,963,601✔
1728
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
12,963,601✔
1729

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

1733
  atomic_store_64(&pTaskInfo->owner, 0);
12,963,601✔
1734
  return pTaskInfo->code;
12,963,601✔
1735
}
1736

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

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

1757
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
12,656,180✔
1758
  SReadHandle    pHandle = {.vnode = pVnode};
12,655,400✔
1759
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
12,654,640✔
1760

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

1769
end:
12,656,932✔
1770
  return 0;
12,657,331✔
1771
}
1772

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

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

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

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

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

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

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

1865
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
12,655,783✔
1866
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
12,656,158✔
1867
  STableListInfo* pList = pTableListInfo;
12,656,158✔
1868
  return pList->pTableList;
12,656,158✔
1869
}
1870
// int32_t qStreamGetTableListGroupNum(const void* pTableList, TdThreadRwlock* lock) { 
1871
//   (void)taosThreadRwlockRdlock(lock);
1872
//   int32_t code = ((STableListInfo*)pTableList)->numOfOuputGroups; 
1873
//   (void)taosThreadRwlockUnlock(lock);
1874
//   return code; 
1875
// }
1876

1877
// void    qStreamSetTableListGroupNum(void* pTableList, int32_t groupNum, TdThreadRwlock* lock) {
1878
//   (void)taosThreadRwlockWrlock(lock);
1879
//   ((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; 
1880
//   (void)taosThreadRwlockUnlock(lock);
1881
// }
1882

1883
// SArray* qStreamGetTableArrayList(const void* pTableList, TdThreadRwlock* lock) { 
1884
//   SArray* pList = NULL;
1885
//   (void)taosThreadRwlockRdlock(lock); 
1886
//   pList = ((STableListInfo*)pTableList)->pTableList;
1887
//   (void)taosThreadRwlockUnlock(lock);
1888
//   return pList;
1889
// }
1890

1891
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,467,779✔
1892

1893
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,532,800✔
1894
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,532,800✔
1895
  qDestroyTask(tInfo);
2,532,800✔
1896
}
2,532,800✔
1897

1898
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
738,327✔
1899
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
738,327✔
1900
}
1901

1902
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
750,314✔
1903
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1904
  int32_t      code = 0;
750,314✔
1905
  SNode*       pNode = 0;
750,314✔
1906
  SNodeList*   pList = NULL;
750,314✔
1907
  SExprInfo*   pExprInfo = NULL;
750,314✔
1908
  int32_t      numOfExprs = 1;
750,314✔
1909
  int32_t*     offset = 0;
750,314✔
1910
  STargetNode* pTargetNode = NULL;
750,314✔
1911
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
750,314✔
1912
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
750,314✔
1913

1914
  if (code == 0) {
750,314✔
1915
    pTargetNode->dataBlockId = 0;
750,314✔
1916
    pTargetNode->pExpr = pNode;
750,314✔
1917
    pTargetNode->slotId = 0;
750,314✔
1918
  }
1919
  if (code == 0) {
750,314✔
1920
    code = nodesMakeList(&pList);
750,314✔
1921
  }
1922
  if (code == 0) {
750,314✔
1923
    code = nodesListAppend(pList, (SNode*)pTargetNode);
750,314✔
1924
  }
1925
  if (code == 0) {
750,314✔
1926
    pNode = NULL;
750,314✔
1927
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
750,314✔
1928
  }
1929

1930
  if (code == 0) {
750,314✔
1931
    const char* pVal = NULL;
750,314✔
1932
    int32_t     len = 0;
750,314✔
1933
    SNode*      pSclNode = NULL;
750,314✔
1934
    switch (pExprInfo->pExpr->nodeType) {
750,314✔
1935
      case QUERY_NODE_FUNCTION:
750,314✔
1936
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
750,314✔
1937
        break;
750,314✔
UNCOV
1938
      case QUERY_NODE_OPERATOR:
×
UNCOV
1939
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
1940
        break;
×
UNCOV
1941
      default:
×
UNCOV
1942
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
1943
        break;
×
1944
    }
1945
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
750,314✔
1946
    SSDataBlock block = {0};
750,314✔
1947
    block.info.rows = 1;
750,314✔
1948
    SSDataBlock* pBlock = &block;
750,314✔
1949
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
750,314✔
1950
    if (tmp == NULL) {
750,314✔
UNCOV
1951
      code = terrno;
×
1952
    }
1953
    if (code == 0) {
750,314✔
1954
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
750,314✔
1955
    }
1956
    taosArrayDestroy(pBlockList);
750,314✔
1957
  }
1958
  nodesDestroyList(pList);
750,314✔
1959
  destroyExprInfo(pExprInfo, numOfExprs);
750,314✔
1960
  taosMemoryFreeClear(pExprInfo);
750,314✔
1961
  return code;
750,314✔
1962
}
1963

1964
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,289,458✔
1965
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,289,458✔
1966
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,289,458✔
1967
  int32_t        code = 0;
4,289,458✔
1968
  SNode*         pNode = NULL;
4,289,458✔
1969
  if (!pForceOutputCols) return 0;
4,289,458✔
1970
  if (!*pRes) {
11,987✔
1971
    code = createDataBlock(pRes);
11,987✔
1972
  }
1973

1974
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
11,987✔
1975
    int32_t idx = 0;
11,987✔
1976
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
47,571✔
1977
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
35,584✔
1978
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
35,584✔
1979
      colInfo.info.precision = pCol->type.precision;
35,584✔
1980
      colInfo.info.scale = pCol->type.scale;
35,584✔
1981
      code = blockDataAppendColInfo(*pRes, &colInfo);
35,584✔
1982
      if (code != 0) break;
35,584✔
1983
    }
1984
  }
1985

1986
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
11,987✔
1987
  if (code != TSDB_CODE_SUCCESS) {
11,987✔
UNCOV
1988
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
1989
    return code;
×
1990
  }
1991

1992
  // loop all exprs for force output, execute all exprs
1993
  int32_t idx = 0;
11,987✔
1994
  int32_t rowIdx = (*pRes)->info.rows;
11,987✔
1995
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
11,987✔
1996
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
11,987✔
1997
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
47,571✔
1998
    SScalarParam   dst = {0};
35,584✔
1999
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
35,584✔
2000
    code = nodesStringToNode(pCol->expr, &pNode);
35,584✔
2001
    if (code != 0) break;
35,584✔
2002
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
35,584✔
2003
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
35,584✔
2004
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
23,597✔
2005
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
23,597✔
2006
    } else {
2007
      dst.columnData = pInfo;
11,987✔
2008
      dst.numOfRows = rowIdx;
11,987✔
2009
      dst.colAlloced = false;
11,987✔
2010
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
11,987✔
2011
    }
2012
    ++idx;
35,584✔
2013
    // TODO sclFreeParam(&dst);
2014
    nodesDestroyNode(pNode);
35,584✔
2015
    if (code != 0) break;
35,584✔
2016
  }
2017
  if (code == TSDB_CODE_SUCCESS) {
11,987✔
2018
    (*pRes)->info.rows++;
11,987✔
2019
  }
2020
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
11,987✔
2021
  return code;
11,987✔
2022
}
2023

2024
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
271,779✔
2025
  int32_t      code = 0;
271,779✔
2026
  const char*  pVal = NULL;
271,779✔
2027
  SScalarParam dst = {0};
271,779✔
2028
  int32_t      len = 0;
271,779✔
2029
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
271,779✔
2030
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
271,779✔
2031
  // execute the expr
2032
  switch (pExpr->type) {
271,779✔
UNCOV
2033
    case QUERY_NODE_VALUE: {
×
UNCOV
2034
      SValueNode* pValue = (SValueNode*)pExpr;
×
UNCOV
2035
      int32_t     type = pValue->node.resType.type;
×
UNCOV
2036
      if (!IS_STR_DATA_TYPE(type)) {
×
UNCOV
2037
        qError("invalid sub tb expr with non-str type");
×
UNCOV
2038
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2039
        break;
×
2040
      }
UNCOV
2041
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
UNCOV
2042
      if (pTmp == NULL) {
×
UNCOV
2043
        qError("invalid sub tb expr with null value");
×
UNCOV
2044
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2045
        break;
×
2046
      }
UNCOV
2047
      pVal = varDataVal(pTmp);
×
UNCOV
2048
      len = varDataLen(pTmp);
×
UNCOV
2049
    } break;
×
2050
    case QUERY_NODE_FUNCTION: {
271,779✔
2051
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
271,779✔
2052
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
271,779✔
UNCOV
2053
        qError("invalid sub tb expr with non-str type func");
×
UNCOV
2054
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2055
        break;
×
2056
      }
2057
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
271,779✔
2058
      if (!pCol) {
271,779✔
UNCOV
2059
        code = terrno;
×
UNCOV
2060
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2061
        break;
×
2062
      }
2063

2064
      pCol->hasNull = true;
271,779✔
2065
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
271,779✔
2066
      pCol->info.colId = 0;
271,779✔
2067
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
271,779✔
2068
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
271,779✔
2069
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
271,779✔
2070
      code = colInfoDataEnsureCapacity(pCol, 1, true);
271,779✔
2071
      if (code != 0) {
271,779✔
UNCOV
2072
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2073
        taosMemoryFree(pCol);
×
UNCOV
2074
        break;
×
2075
      }
2076
      dst.columnData = pCol;
271,779✔
2077
      dst.numOfRows = 1;
271,779✔
2078
      dst.colAlloced = true;
271,779✔
2079
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
271,779✔
2080
      if (colDataIsNull_var(dst.columnData, 0)) {
271,779✔
UNCOV
2081
        qInfo("invalid sub tb expr with null value");
×
UNCOV
2082
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
2083
      }
2084
      if (code == 0) {
271,779✔
2085
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
271,779✔
2086
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
271,779✔
2087
      }
2088
    } break;
271,354✔
UNCOV
2089
    default:
×
UNCOV
2090
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2091
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2092
      break;
×
2093
  }
2094
  if (code == 0) {
271,354✔
2095
    if (!pVal || len == 0) {
271,779✔
2096
      qError("tbname generated with no characters which is not allowed");
425✔
UNCOV
2097
      code = TSDB_CODE_INVALID_PARA;
×
2098
    }
2099
    if(len > TSDB_TABLE_NAME_LEN - 1) {
271,354✔
2100
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
774✔
2101
      len = TSDB_TABLE_NAME_LEN - 1;
774✔
2102
    }
2103

2104
    memcpy(tbname, pVal, len);
271,354✔
2105
    tbname[len] = '\0';  // ensure null terminated
271,354✔
2106
    if (NULL != strchr(tbname, '.')) {
271,779✔
2107
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2108
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2109
    }
2110
  }
2111
  // TODO free dst
2112
  sclFreeParam(&dst);
271,354✔
2113
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
271,779✔
2114
  return code;
271,779✔
2115
}
2116

2117
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
299,685✔
2118
  if (pParam) {
299,685✔
2119
    if (pParam->tbname) {
299,685✔
2120
      taosMemFree(pParam->tbname);
299,685✔
2121
      pParam->tbname = NULL;
299,685✔
2122
    }
2123
    if (pParam->stbname) {
299,685✔
2124
      taosMemFree(pParam->stbname);
299,685✔
2125
      pParam->stbname = NULL;
299,685✔
2126
    }
2127
    if (pParam->dbFName) {
299,685✔
2128
      taosMemFree(pParam->dbFName);
299,685✔
2129
      pParam->dbFName = NULL;
299,685✔
2130
    }
2131
    if (pParam->pFields) {
299,685✔
2132
      taosArrayDestroy(pParam->pFields);
299,685✔
2133
      pParam->pFields = NULL;
299,685✔
2134
    }
2135
    if (pParam->pTagFields) {
299,685✔
2136
      taosArrayDestroy(pParam->pTagFields);
192,877✔
2137
      pParam->pTagFields = NULL;
192,877✔
2138
    }
2139
    taosMemFree(pParam);
299,685✔
2140
  }
2141
}
299,685✔
2142

2143
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
299,298✔
2144
  int32_t code = 0, lino = 0;
299,298✔
2145
  if (ppDst == NULL || pSrc == NULL) {
299,298✔
UNCOV
2146
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2147
  }
2148
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
299,298✔
2149
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
299,685✔
2150

2151
  (*ppDst)->suid = pSrc->suid;
299,685✔
2152
  (*ppDst)->sver = pSrc->sver;
299,685✔
2153
  (*ppDst)->tbType = pSrc->tbType;
299,685✔
2154
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
299,298✔
2155
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
299,685✔
2156

2157
  if (pSrc->stbname) {
299,298✔
2158
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
299,298✔
2159
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
299,685✔
2160
  }
2161

2162
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
299,298✔
2163
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
299,685✔
2164

2165
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
299,298✔
2166

2167
  if (pSrc->pFields && pSrc->pFields->size > 0) {
299,298✔
2168
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
299,685✔
2169
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
299,685✔
2170
  } else {
UNCOV
2171
    (*ppDst)->pFields = NULL;
×
2172
  }
2173
  
2174
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
299,685✔
2175
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
192,877✔
2176
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
192,877✔
2177
  } else {
2178
    (*ppDst)->pTagFields = NULL;
106,808✔
2179
  }
2180

2181
_exit:
299,298✔
2182

2183
  if (code != 0) {
299,298✔
UNCOV
2184
    if (*ppDst) {
×
UNCOV
2185
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2186
      *ppDst = NULL;
×
2187
    }
2188
    
UNCOV
2189
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2190
  }
2191
  return code;
299,298✔
2192
}
2193

2194
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
425✔
2195
  return doDropStreamTable(pMsgCb, pOutput, pReq);
425✔
2196
}
2197

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