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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

68.57
/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 "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

37
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
38
int32_t             exchangeObjRefPool = -1;
39
SGlobalExecInfo     gExecInfo = {0};
40

41
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
676,512✔
42
  gExecInfo.dnode = pDnode;
676,512✔
43
  gExecInfo.getMnode = getMnode;
676,512✔
44
  gExecInfo.getDnodeId = getDnodeId;
676,512✔
45
  return;
676,512✔
46
}
47

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

57
static void cleanupRefPool() {
630,490✔
58
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
630,490✔
59
  taosCloseRef(ref);
630,490✔
60
}
630,490✔
61

62
static void initRefPool() {
630,490✔
63
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
630,490✔
64
  (void)atexit(cleanupRefPool);
630,490✔
65
}
630,490✔
66

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

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

85
    SStreamScanInfo* pInfo = pOperator->info;
×
86

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

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

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

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

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

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

148
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,393,758✔
149
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,393,758✔
150
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,394,207✔
151
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,185,714✔
152
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,185,714✔
153
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,185,320✔
154
      if (pScanInfo->base.dataReader != NULL) {
4,185,769✔
155
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
56,326✔
156
        if (code) {
56,271✔
157
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
158
          return code;
×
159
        }
160
      }
161
    }
162
  } else {
163
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,208,487✔
164
  }
165

166
  return 0;
10,657,242✔
167
}
168

169
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
10,656,682✔
170
  SExecTaskInfo* pTaskInfo = tinfo;
10,656,682✔
171
  pTaskInfo->id.queryId = queryId;
10,656,682✔
172
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
10,656,785✔
173

174
  // set the idstr for tsdbReader
175
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,657,367✔
176
}
177

178
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
179
  SExecTaskInfo* pTaskInfo = tinfo;
×
180
  return pTaskInfo->status == OP_EXEC_DONE;
×
181
}
182

183
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
184
  if (tinfo == NULL) {
×
185
    return TSDB_CODE_APP_ERROR;
×
186
  }
187

188
  if (pBlocks == NULL || numOfBlocks == 0) {
×
189
    return TSDB_CODE_SUCCESS;
×
190
  }
191

192
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
193

194
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
195
  if (code != TSDB_CODE_SUCCESS) {
×
196
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
197
  } else {
198
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
199
  }
200

201
  return code;
×
202
}
203

204
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
138,663✔
205
                                     uint64_t id) {
206
  if (msg == NULL) {  // create raw scan
138,663✔
207
    SExecTaskInfo* pTaskInfo = NULL;
23,191✔
208

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

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

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

225
  SSubplan* pPlan = NULL;
115,472✔
226
  int32_t   code = qStringToSubplan(msg, &pPlan);
116,400✔
227
  if (code != TSDB_CODE_SUCCESS) {
116,478✔
228
    terrno = code;
×
229
    return NULL;
×
230
  }
231

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

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

244
  SNode* pNode;
245
  FOREACH(pNode, pDescNode->pSlots) {
1,382,971✔
246
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,266,493✔
247
    if (pSlotDesc->output) {
1,266,493✔
248
      ++(*numOfCols);
1,266,385✔
249
    }
250
  }
251

252
  return pTaskInfo;
116,478✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
481,235✔
256
  if (streamInserterParam == NULL) {
481,235✔
257
    return TSDB_CODE_SUCCESS;
204,682✔
258
  }
259

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

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

270
  if (streamInserterParam->suid <= 0 &&
276,553✔
271
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
86,952✔
272
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
273
    return TSDB_CODE_INVALID_PARA;
×
274
  }
275

276
  return TSDB_CODE_SUCCESS;
276,553✔
277
}
278

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

299
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
481,235✔
300
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
481,235✔
301
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
810✔
302
    goto _error;
810✔
303
  }
304

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

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

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

334
_error:
180,204✔
335

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

345
bool qNeedReset(qTaskInfo_t pInfo) {
5,995,925✔
346
  if (pInfo == NULL) {
5,995,925✔
347
    return false;
×
348
  }
349
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,995,925✔
350
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,995,925✔
351
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,995,925✔
352
    return false;
4,752✔
353
  }
354
  int32_t node = nodeType(pOperator->pPhyNode);
5,991,173✔
355
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,888,537✔
356
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,879,710✔
357
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
358
}
359

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

365
  pScanBaseInfo->readHandle.uid = pHandle->uid;
5,881,846✔
366
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
5,881,846✔
367
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
5,881,846✔
368
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
5,881,846✔
369
}
5,881,846✔
370

371
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
372
  if (pInfo == NULL) {
5,991,173✔
373
    return TSDB_CODE_INVALID_PARA;
5,991,173✔
UNCOV
374
  }
×
375
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
376
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,991,173✔
377

5,991,173✔
378
  void*           info = pOperator->info;
379
  STableScanBase* pScanBaseInfo = NULL;
5,991,173✔
380

5,991,173✔
381
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
382
    pScanBaseInfo = &((STableScanInfo*)info)->base;
5,991,173✔
383
    setReadHandle(handle, pScanBaseInfo);
102,636✔
384
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
102,636✔
385
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,888,537✔
386
    setReadHandle(handle, pScanBaseInfo);
5,779,210✔
387
  }
5,779,210✔
388

389
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
390
  handle->winRange.ekey);
5,991,173✔
391
  return pOperator->fpSet.resetStateFn(pOperator);
392
}
5,991,173✔
393

394
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
395
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
481,235✔
396
  if (msg == NULL) {
397
    return TSDB_CODE_INVALID_PARA;
481,235✔
UNCOV
398
  }
×
399

400
  *pTaskInfo = NULL;
401

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

417
  return code;
418
}
480,425✔
419

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

432
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
433

66,495✔
434
  uint64_t suid = 0;
435
  uint64_t uid = 0;
66,495✔
436
  int32_t  type = 0;
66,495✔
437
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
66,495✔
438

66,495✔
439
  // let's discard the tables those are not created according to the queried super table.
440
  SMetaReader mr = {0};
441
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
66,495✔
442
  for (int32_t i = 0; i < numOfUids; ++i) {
66,495✔
443
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
134,052✔
444
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
67,557✔
445

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

452
    tDecoderClear(&mr.coder);
453

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

471
    if (pScanInfo->pTagCond != NULL) {
472
      bool          qualified = false;
67,557✔
473
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
58,718✔
474
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
58,718✔
475
      if (code != TSDB_CODE_SUCCESS) {
58,718✔
476
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
58,718✔
UNCOV
477
        continue;
×
478
      }
×
479

480
      if (!qualified) {
481
        continue;
58,718✔
482
      }
29,386✔
483
    }
484

485
    // handle multiple partition
486
    void* tmp = taosArrayPush(qa, id);
487
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
38,171✔
488
  }
38,171✔
489

490
_end:
491

66,495✔
492
  pAPI->metaReaderFn.clearReader(&mr);
493
  (*ppArrayRes) = qa;
66,495✔
494

66,495✔
495
_error:
496
  if (code != TSDB_CODE_SUCCESS) {
66,495✔
497
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
66,495✔
UNCOV
498
  }
×
499
  return code;
500
}
66,495✔
501

502
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
503
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
66,930✔
504
  const char*    id = GET_TASKID(pTaskInfo);
66,930✔
505
  int32_t        code = 0;
66,930✔
506

66,930✔
507
  if (isAdd) {
508
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
66,930✔
509
  }
66,495✔
510

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

518
  SStreamScanInfo* pScanInfo = pInfo->info;
519
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
66,930✔
520
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
66,930✔
521
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
134,708✔
522
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
67,778✔
523
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
67,778✔
524
    }
67,778✔
525
  }
526

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

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

550
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
551
    taosWLockLatch(&pTaskInfo->lock);
66,495✔
552

66,495✔
553
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
554
      uint64_t* uid = taosArrayGet(qa, i);
104,666✔
555
      if (!uid) {
38,171✔
556
        taosMemoryFree(keyBuf);
38,171✔
UNCOV
557
        taosArrayDestroy(qa);
×
558
        taosWUnLockLatch(&pTaskInfo->lock);
×
559
        return terrno;
×
560
      }
×
561
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
562

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

578
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
579
      if (code != TSDB_CODE_SUCCESS) {
38,171✔
580
        taosMemoryFree(keyBuf);
38,171✔
UNCOV
581
        taosArrayDestroy(qa);
×
582
        taosWUnLockLatch(&pTaskInfo->lock);
×
583
        return code;
×
584
      }
×
585
    }
586

587
    taosWUnLockLatch(&pTaskInfo->lock);
588
    if (keyBuf != NULL) {
66,495✔
589
      taosMemoryFree(keyBuf);
66,495✔
UNCOV
590
    }
×
591

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

600
  return code;
601
}
66,930✔
602

603
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
604
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
287,866,016✔
605
                                    int32_t idx, bool* tbGet) {
606
  *tbGet = false;
607

287,866,016✔
608
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
609
    return TSDB_CODE_INVALID_PARA;
287,870,117✔
610
  }
2✔
611
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
612

287,872,224✔
613
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
614
    return TSDB_CODE_SUCCESS;
287,872,224✔
615
  }
169,580,020✔
616

617
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
618
  if (!pSchemaInfo) {
118,292,431✔
619
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
118,278,738✔
UNCOV
620
    return terrno;
×
621
  }
×
622

623
  *sversion = pSchemaInfo->sw->version;
624
  *tversion = pSchemaInfo->tversion;
118,278,738✔
625
  *rversion = pSchemaInfo->rversion;
118,302,253✔
626
  if (pSchemaInfo->dbname) {
118,295,348✔
627
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
118,306,130✔
628
  } else {
118,295,229✔
629
    dbName[0] = 0;
UNCOV
630
  }
×
631
  if (pSchemaInfo->tablename) {
632
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
118,316,437✔
633
  } else {
118,303,654✔
634
    tableName[0] = 0;
635
  }
141✔
636

637
  *tbGet = true;
638

118,317,643✔
639
  return TSDB_CODE_SUCCESS;
640
}
118,307,325✔
641

642
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
643

169,575,655✔
644
void qDestroyOperatorParam(SOperatorParam* pParam) {
UNCOV
645
  if (NULL == pParam) {
×
646
    return;
×
647
  }
×
648
  freeOperatorParam(pParam, OP_GET_PARAM);
UNCOV
649
}
×
650

651
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
652
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
37,557,244✔
653
  ((SExecTaskInfo*)tinfo)->paramSet = false;
37,557,244✔
654
}
37,557,244✔
655

37,557,244✔
656
int32_t qExecutorInit(void) {
657
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,619,428✔
658
  return TSDB_CODE_SUCCESS;
4,619,428✔
659
}
4,621,188✔
660

661
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
662
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
171,486,684✔
663
                        EOPTR_EXEC_MODEL model) {
664
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
665
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
171,486,684✔
666

171,486,684✔
667
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
668

171,493,350✔
669
  readHandle->uid = 0;
670
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
171,513,631✔
671
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
171,525,905✔
672
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
171,430,600✔
673
    goto _error;
130,052✔
674
  }
27,200✔
675

676
  if (handle) {
677
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
171,326,273✔
678
    void*           pSinkManager = NULL;
171,265,195✔
679
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
171,293,083✔
680
    if (code != TSDB_CODE_SUCCESS) {
171,123,348✔
681
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
171,282,855✔
UNCOV
682
      goto _error;
×
683
    }
×
684

685
    void* pSinkParam = NULL;
686
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
171,282,855✔
687
    if (code != TSDB_CODE_SUCCESS) {
171,286,138✔
688
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
171,180,178✔
UNCOV
689
      taosMemoryFree(pSinkManager);
×
690
      goto _error;
×
691
    }
×
692

693
    SDataSinkNode* pSink = NULL;
694
    if (readHandle->localExec) {
171,180,178✔
695
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
171,251,756✔
696
      if (code != TSDB_CODE_SUCCESS) {
90,893✔
697
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
90,893✔
UNCOV
698
               (*pTask)->id.str);
×
699
        taosMemoryFree(pSinkManager);
UNCOV
700
        goto _error;
×
701
      }
×
702
    }
703

704
    // pSinkParam has been freed during create sinker.
705
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
706
                              (*pTask)->id.str, pSubplan->processOneBlock);
171,197,439✔
707
    if (code) {
171,306,399✔
708
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
171,159,851✔
709
    }
619✔
710
  }
711

712
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
713
         tstrerror(code));
171,345,306✔
714

715
_error:
716
  // if failed to add ref for all tables in this query, abort current query
8,965,928✔
717
  return code;
718
}
171,513,693✔
719

720
static void freeBlock(void* param) {
UNCOV
721
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
722
  blockDataDestroy(pBlock);
×
723
}
×
724

×
725
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
726
                     bool processOneBlock) {
231,407,878✔
727
  int32_t        code = TSDB_CODE_SUCCESS;
728
  int32_t        lino = 0;
231,407,878✔
729
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
231,407,878✔
730
  int64_t        threadId = taosGetSelfPthreadId();
231,407,878✔
731

231,407,878✔
732
  if (pLocal) {
733
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
231,402,580✔
734
  }
225,182,822✔
735

736
  taosArrayClear(pResList);
737

231,379,147✔
738
  int64_t curOwner = 0;
739
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
231,393,044✔
740
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
231,393,044✔
UNCOV
741
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
742
    return pTaskInfo->code;
×
743
  }
×
744

745
  if (pTaskInfo->cost.start == 0) {
746
    pTaskInfo->cost.start = taosGetTimestampUs();
231,392,803✔
747
  }
167,328,659✔
748

749
  if (isTaskKilled(pTaskInfo)) {
750
    atomic_store_64(&pTaskInfo->owner, 0);
231,404,339✔
751
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
122✔
752
    return pTaskInfo->code;
122✔
753
  }
122✔
754

755
  // error occurs, record the error code and return to client
756
  int32_t ret = setjmp(pTaskInfo->env);
757
  if (ret != TSDB_CODE_SUCCESS) {
231,398,901✔
758
    pTaskInfo->code = ret;
231,592,922✔
759
    (void)cleanUpUdfs();
226,673✔
760

226,673✔
761
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
762
    atomic_store_64(&pTaskInfo->owner, 0);
226,673✔
763

226,673✔
764
    return pTaskInfo->code;
765
  }
226,673✔
766

767
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
768

231,366,249✔
769
  int32_t      current = 0;
770
  SSDataBlock* pRes = NULL;
231,367,346✔
771
  int64_t      st = taosGetTimestampUs();
231,367,346✔
772

231,404,297✔
773
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
774
    pTaskInfo->paramSet = true;
231,404,297✔
775
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,557,244✔
776
  } else {
37,557,244✔
777
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
778
  }
193,846,677✔
779

780
  QUERY_CHECK_CODE(code, lino, _end);
781
  code = blockDataCheck(pRes);
231,181,971✔
782
  QUERY_CHECK_CODE(code, lino, _end);
231,181,971✔
783

231,187,025✔
784
  if (pRes == NULL) {
785
    st = taosGetTimestampUs();
231,187,025✔
786
  }
42,295,050✔
787

788
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
789
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
231,187,864✔
790
    rowsThreshold = 4096;
231,188,533✔
791
  }
230,374,669✔
792

793
  int32_t blockIndex = 0;
794
  while (pRes != NULL) {
231,187,537✔
795
    SSDataBlock* p = NULL;
614,937,266✔
796
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
436,732,960✔
797
      SSDataBlock* p1 = NULL;
436,715,126✔
798
      code = createOneDataBlock(pRes, true, &p1);
301,536,854✔
799
      QUERY_CHECK_CODE(code, lino, _end);
301,537,634✔
800

301,534,862✔
801
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
802
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
301,534,862✔
803
      p = p1;
301,538,093✔
804
    } else {
301,538,093✔
805
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
806
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
135,228,214✔
807

135,236,055✔
808
      p = *(SSDataBlock**)tmp;
809
      code = copyDataBlock(p, pRes);
135,236,055✔
810
      QUERY_CHECK_CODE(code, lino, _end);
135,237,273✔
811
    }
135,234,348✔
812

813
    blockIndex += 1;
814

436,772,747✔
815
    current += p->info.rows;
816
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
436,772,747✔
817
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
436,772,396✔
818
    void* tmp = taosArrayPush(pResList, &p);
819
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
436,766,988✔
820

436,766,988✔
821
    if (current >= rowsThreshold || processOneBlock) {
822
      break;
436,766,988✔
823
    }
824

825
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
826
    QUERY_CHECK_CODE(code, lino, _end);
383,784,374✔
827
    code = blockDataCheck(pRes);
383,736,305✔
828
    QUERY_CHECK_CODE(code, lino, _end);
383,736,305✔
829
  }
383,797,160✔
830

831
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
832
    pTaskInfo->pSubplan->rowsThreshold -= current;
231,186,920✔
833
  }
806,098✔
834

835
  *hasMore = (pRes != NULL);
836
  uint64_t el = (taosGetTimestampUs() - st);
231,189,365✔
837

231,179,671✔
838
  pTaskInfo->cost.elapsedTime += el;
839
  if (NULL == pRes) {
231,179,671✔
840
    *useconds = pTaskInfo->cost.elapsedTime;
231,179,901✔
841
  }
178,196,678✔
842

843
_end:
844
  (void)cleanUpUdfs();
231,058,792✔
845

231,179,415✔
846
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
847
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
231,193,992✔
848
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
231,193,992✔
849

850
  atomic_store_64(&pTaskInfo->owner, 0);
851
  if (code) {
231,193,992✔
852
    pTaskInfo->code = code;
231,193,992✔
UNCOV
853
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
854
  }
×
855

856
  return pTaskInfo->code;
857
}
231,193,992✔
858

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

870
  taosArrayClear(pTaskInfo->pResultBlockList);
UNCOV
871
}
×
872

×
873
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
874
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
42,057,982✔
875
  int64_t        threadId = taosGetSelfPthreadId();
42,057,982✔
876
  int64_t        curOwner = 0;
42,057,982✔
877

42,057,655✔
878
  *pRes = NULL;
879

42,057,655✔
880
  // todo extract method
881
  taosRLockLatch(&pTaskInfo->lock);
882
  bool isKilled = isTaskKilled(pTaskInfo);
42,057,655✔
883
  if (isKilled) {
42,057,873✔
884
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
42,057,873✔
UNCOV
885

×
886
    taosRUnLockLatch(&pTaskInfo->lock);
UNCOV
887
    return pTaskInfo->code;
×
888
  }
×
889

890
  if (pTaskInfo->owner != 0) {
891
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
42,057,873✔
UNCOV
892
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
893

×
894
    taosRUnLockLatch(&pTaskInfo->lock);
UNCOV
895
    return pTaskInfo->code;
×
896
  }
×
897

898
  pTaskInfo->owner = threadId;
899
  taosRUnLockLatch(&pTaskInfo->lock);
42,057,672✔
900

42,057,781✔
901
  if (pTaskInfo->cost.start == 0) {
902
    pTaskInfo->cost.start = taosGetTimestampUs();
42,057,982✔
903
  }
92,325✔
904

905
  // error occurs, record the error code and return to client
906
  int32_t ret = setjmp(pTaskInfo->env);
907
  if (ret != TSDB_CODE_SUCCESS) {
42,057,982✔
908
    pTaskInfo->code = ret;
42,055,637✔
UNCOV
909
    (void)cleanUpUdfs();
×
910
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
911
    atomic_store_64(&pTaskInfo->owner, 0);
×
912
    return pTaskInfo->code;
×
913
  }
×
914

915
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
916

42,055,637✔
917
  int64_t st = taosGetTimestampUs();
918
  int32_t code = TSDB_CODE_SUCCESS;
42,057,546✔
919
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
42,057,546✔
920
    pTaskInfo->paramSet = true;
42,057,546✔
UNCOV
921
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
922
  } else {
×
923
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
924
  }
42,057,546✔
925
  if (code) {
926
    pTaskInfo->code = code;
42,045,618✔
UNCOV
927
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
928
  }
×
929

930
  code = blockDataCheck(*pRes);
931
  if (code) {
42,045,618✔
932
    pTaskInfo->code = code;
42,056,151✔
UNCOV
933
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
934
  }
×
935

936
  uint64_t el = (taosGetTimestampUs() - st);
937

42,047,509✔
938
  pTaskInfo->cost.elapsedTime += el;
939
  if (NULL == *pRes) {
42,047,509✔
940
    *useconds = pTaskInfo->cost.elapsedTime;
42,053,380✔
941
  }
3,863,706✔
942

943
  (void)cleanUpUdfs();
944

42,053,040✔
945
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
946
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
42,057,219✔
947

42,058,091✔
948
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
949
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
42,058,091✔
950

951
  atomic_store_64(&pTaskInfo->owner, 0);
952
  return pTaskInfo->code;
42,057,001✔
953
}
42,058,091✔
954

955
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
956
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,447,545✔
957
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
57,447,545✔
958
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,447,862✔
959

57,448,613✔
960
  if (!tmp) {
961
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
57,447,428✔
UNCOV
962
    return terrno;
×
963
  }
×
964
  return TSDB_CODE_SUCCESS;
965
}
57,447,428✔
966

967
int32_t stopInfoComp(void const* lp, void const* rp) {
UNCOV
968
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
969
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
970

×
971
  if (key->refId < pInfo->refId) {
UNCOV
972
    return -1;
×
973
  } else if (key->refId > pInfo->refId) {
×
974
    return 1;
×
975
  }
×
976

977
  return 0;
UNCOV
978
}
×
979

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

×
989
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
990
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
26,729✔
991

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

1014
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
1015
}
26,729✔
1016

26,729✔
1017
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
1018
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
26,729✔
1019
  if (pTaskInfo == NULL) {
26,729✔
1020
    return TSDB_CODE_QRY_INVALID_QHANDLE;
26,729✔
UNCOV
1021
  }
×
1022

1023
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
1024

26,729✔
1025
  setTaskKilled(pTaskInfo, rspCode);
1026
  qStopTaskOperators(pTaskInfo);
26,729✔
1027

26,729✔
1028
  return TSDB_CODE_SUCCESS;
1029
}
26,729✔
1030

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

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

1044
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
UNCOV
1045

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

×
1052
        taosMsleep(200);
UNCOV
1053

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

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

1075
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
UNCOV
1076
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1077
  if (NULL == pTaskInfo) {
×
1078
    return false;
×
1079
  }
×
1080

1081
  return 0 != atomic_load_64(&pTaskInfo->owner);
UNCOV
1082
}
×
1083

1084
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
1085
  STaskCostInfo* pSummary = &pTaskInfo->cost;
172,003,148✔
1086
  int64_t        idleTime = pSummary->start - pSummary->created;
172,003,148✔
1087

172,004,324✔
1088
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
1089
  if (pSummary->pRecoder != NULL) {
172,000,391✔
1090
    qDebug(
171,999,957✔
1091
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
120,642,692✔
1092
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1093
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1094
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1095
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1096
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1097
  } else {
1098
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
1099
           pSummary->elapsedTime / 1000.0);
51,359,400✔
1100
  }
1101
}
1102

172,002,092✔
1103
void qDestroyTask(qTaskInfo_t qTaskHandle) {
1104
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
175,768,946✔
1105
  if (pTaskInfo == NULL) {
175,768,946✔
1106
    return;
175,768,946✔
1107
  }
3,769,308✔
1108

1109
  if (pTaskInfo->pRoot != NULL) {
1110
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
171,999,638✔
1111
  } else {
172,004,320✔
1112
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
UNCOV
1113
  }
×
1114

1115
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
1116
  doDestroyTask(pTaskInfo);
172,005,870✔
1117
}
172,002,713✔
1118

1119
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
1120
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,646,347✔
1121
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,646,347✔
1122
}
2,646,347✔
1123

1124
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
1125
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
116,478✔
1126
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
116,478✔
1127

116,478✔
1128
  while (1) {
1129
    uint16_t type = pOperator->operatorType;
113,669✔
1130
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
230,147✔
1131
      *scanner = pOperator->info;
230,147✔
1132
      break;
116,478✔
1133
    } else {
116,478✔
1134
      pOperator = pOperator->pDownstream[0];
1135
    }
113,669✔
1136
  }
1137
}
1138

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

1152
void* qExtractReaderFromTmqScanner(void* scanner) {
1153
  SStreamScanInfo* pInfo = scanner;
116,478✔
1154
  return (void*)pInfo->tqReader;
116,478✔
1155
}
116,478✔
1156

1157
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
1158
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
149,390✔
1159
  return pTaskInfo->streamInfo.schema;
149,390✔
1160
}
149,390✔
1161

1162
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
1163
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
149,390✔
1164
  return pTaskInfo->streamInfo.tbName;
149,390✔
1165
}
149,390✔
1166

1167
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
1168
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
157,524✔
1169
  return &pTaskInfo->streamInfo.btMetaRsp;
157,524✔
1170
}
157,524✔
1171

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

1183
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
1184
  memset(pCond, 0, sizeof(SQueryTableDataCond));
148,592✔
1185
  pCond->order = TSDB_ORDER_ASC;
148,592✔
1186
  pCond->numOfCols = pMtInfo->schema->nCols;
148,592✔
1187
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
148,802✔
1188
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
149,034✔
1189
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
148,860✔
1190
    taosMemoryFreeClear(pCond->colList);
149,034✔
1191
    taosMemoryFreeClear(pCond->pSlotList);
174✔
UNCOV
1192
    return terrno;
×
1193
  }
×
1194

1195
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
1196
  pCond->suid = pMtInfo->suid;
148,860✔
1197
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
148,918✔
1198
  pCond->startVersion = -1;
148,882✔
1199
  pCond->endVersion = sContext->snapVersion;
148,918✔
1200

148,860✔
1201
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
1202
    SColumnInfo* pColInfo = &pCond->colList[i];
947,456✔
1203
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
798,364✔
1204
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
798,248✔
1205
    if (pMtInfo->pExtSchemas != NULL) {
798,422✔
1206
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
798,157✔
1207
    }
7,494✔
1208
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
1209
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
798,353✔
1210

798,273✔
1211
    pCond->pSlotList[i] = i;
1212
  }
798,400✔
1213

1214
  return TSDB_CODE_SUCCESS;
1215
}
149,034✔
1216

1217
void qStreamSetOpen(qTaskInfo_t tinfo) {
1218
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
41,753,690✔
1219
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
41,753,690✔
1220
  pOperator->status = OP_NOT_OPENED;
41,753,690✔
1221
}
41,757,283✔
1222

41,757,166✔
1223
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
1224
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,129,091✔
1225
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,129,091✔
1226
}
4,129,091✔
1227

4,129,071✔
1228
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
1229
  int32_t        code = TSDB_CODE_SUCCESS;
4,345,758✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,345,758✔
1231
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,345,758✔
1232

4,345,758✔
1233
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
1234
  const char*    id = GET_TASKID(pTaskInfo);
4,346,033✔
1235

4,346,132✔
1236
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
1237
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,345,340✔
1238
    if (pOperator == NULL || code != 0) {
4,065,401✔
1239
      return code;
4,065,459✔
UNCOV
1240
    }
×
1241

1242
    SStreamScanInfo* pInfo = pOperator->info;
1243
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,065,623✔
1244
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,065,605✔
1245
    walReaderVerifyOffset(pWalReader, pOffset);
4,065,089✔
1246
  }
4,065,770✔
1247
  // if pOffset equal to current offset, means continue consume
1248
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
1249
    return 0;
4,345,744✔
1250
  }
3,886,738✔
1251

1252
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1253
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
457,896✔
1254
    if (pOperator == NULL || code != 0) {
305,696✔
1255
      return code;
305,777✔
UNCOV
1256
    }
×
1257

1258
    SStreamScanInfo* pInfo = pOperator->info;
1259
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
305,965✔
1260
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
305,858✔
1261
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
305,884✔
1262

305,699✔
1263
    if (pOffset->type == TMQ_OFFSET__LOG) {
1264
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
305,345✔
1265
      pScanBaseInfo->dataReader = NULL;
244,813✔
1266

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

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

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

11,319✔
1311
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
1312
             pInfo->pTableScanOp->resultInfo.totalRows);
11,562✔
1313
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
1314

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

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

1329
      STableKeyInfo keyInfo = {.uid = uid};
1330
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
11,562✔
1331

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

11,562✔
1340
      if (pScanBaseInfo->dataReader == NULL) {
1341
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
11,562✔
1342
                                                             &keyInfo, 1, pScanInfo->pResBlock,
20,106✔
1343
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
1344
        if (code != TSDB_CODE_SUCCESS) {
10,053✔
1345
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
10,053✔
UNCOV
1346
          return code;
×
1347
        }
×
1348

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

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

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

1374
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1375
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
1376
      SStreamRawScanInfo* pInfo = pOperator->info;
152,200✔
1377
      SSnapContext*       sContext = pInfo->sContext;
149,216✔
1378
      SOperatorInfo*      p = NULL;
149,216✔
1379

149,216✔
1380
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
1381
      if (code != 0) {
149,216✔
1382
        return code;
149,216✔
UNCOV
1383
      }
×
1384

1385
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
1386

149,216✔
1387
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
1388
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
149,216✔
UNCOV
1389
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1390
      }
×
1391

1392
      SMetaTableInfo mtInfo = {0};
1393
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
149,216✔
1394
      if (code != 0) {
149,216✔
1395
        destroyMetaTableInfo(&mtInfo);
149,158✔
1396
        return code;
UNCOV
1397
      }
×
1398
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1399
      pInfo->dataReader = NULL;
149,158✔
1400

149,031✔
1401
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
1402
      tableListClear(pTableListInfo);
149,031✔
1403

148,962✔
1404
      if (mtInfo.uid == 0) {
1405
        destroyMetaTableInfo(&mtInfo);
149,089✔
1406
        goto end;  // no data
1407
      }
182✔
1408

1409
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
1410
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
148,907✔
1411
      if (code != TSDB_CODE_SUCCESS) {
148,918✔
1412
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
148,976✔
UNCOV
1413
        destroyMetaTableInfo(&mtInfo);
×
1414
        return code;
UNCOV
1415
      }
×
1416
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
1417
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
148,976✔
UNCOV
1418
      } else {
×
1419
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
1420
      }
148,708✔
1421

1422
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
1423
      if (code != TSDB_CODE_SUCCESS) {
148,846✔
1424
        destroyMetaTableInfo(&mtInfo);
149,034✔
1425
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
UNCOV
1426
        return code;
×
1427
      }
×
1428

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

1443
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1444
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
298,068✔
1445
      if (code != TSDB_CODE_SUCCESS) {
149,034✔
1446
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
148,432✔
UNCOV
1447
        destroyMetaTableInfo(&mtInfo);
×
1448
        return code;
UNCOV
1449
      }
×
1450

1451
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
1452
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
148,432✔
1453
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
148,907✔
1454
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
1455
      pTaskInfo->streamInfo.schema = mtInfo.schema;
148,432✔
1456
      taosMemoryFreeClear(mtInfo.pExtSchemas);
148,791✔
1457

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

1477
end:
1478
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
401,479✔
1479
  return 0;
401,885✔
1480
}
401,885✔
1481

1482
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
1483
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
155,059,469✔
1484
  if (pMsg->info.ahandle == NULL) {
155,059,469✔
1485
    qError("pMsg->info.ahandle is NULL");
155,068,110✔
1486
    return;
554✔
1487
  }
554✔
1488

1489
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
1490
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
155,059,015✔
1491

1492
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
1493

155,064,555✔
1494
  if (pMsg->contLen > 0) {
1495
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
155,068,040✔
1496
    if (buf.pData == NULL) {
155,048,231✔
1497
      pMsg->code = terrno;
155,047,939✔
UNCOV
1498
    } else {
×
1499
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
1500
    }
155,047,939✔
1501
  }
1502

1503
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
1504
  rpcFreeCont(pMsg->pCont);
155,072,054✔
1505
  destroySendMsgInfo(pSendInfo);
155,074,044✔
1506
}
155,054,936✔
1507

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

×
1514
  code = getTableListInfo(pTaskInfo, &plist);
UNCOV
1515
  if (code || plist == NULL) {
×
1516
    return NULL;
×
1517
  }
×
1518

1519
  // only extract table in the first elements
1520
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
UNCOV
1521

×
1522
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
UNCOV
1523
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1524

×
1525
  int32_t numOfTables = 0;
UNCOV
1526
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1527
  QUERY_CHECK_CODE(code, lino, _end);
×
1528

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

1536
  taosArrayDestroy(plist);
UNCOV
1537

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

1546
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
1547
  int32_t        code = TSDB_CODE_SUCCESS;
3,413,310✔
1548
  int32_t        lino = 0;
3,413,310✔
1549
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,413,310✔
1550

3,413,310✔
1551
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
1552
    SStreamScanInfo* pScanInfo = pOperator->info;
3,413,301✔
UNCOV
1553
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1554

×
1555
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
UNCOV
1556
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1557
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
×
1558
    STableScanInfo* pScanInfo = pOperator->info;
3,409,728✔
1559

1,705,764✔
1560
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1561
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,706,961✔
1562
  } else {
1,708,761✔
1563
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1564
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,705,152✔
1565
    }
1,706,349✔
1566
  }
1567

1568
_end:
UNCOV
1569
  if (code != TSDB_CODE_SUCCESS) {
×
1570
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
3,415,119✔
UNCOV
1571
  }
×
1572
  return code;
1573
}
3,413,904✔
1574

1575
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1576
  if (pList == NULL) {
1,706,952✔
1577
    return TSDB_CODE_INVALID_PARA;
1,706,952✔
UNCOV
1578
  }
×
1579

1580
  *pList = NULL;
1581
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,706,952✔
1582
  if (pArray == NULL) {
1,706,961✔
1583
    return terrno;
1,706,952✔
UNCOV
1584
  }
×
1585

1586
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1587
  if (code == 0) {
1,706,952✔
1588
    *pList = pArray;
1,708,752✔
1589
  } else {
1,708,752✔
1590
    taosArrayDestroy(pArray);
UNCOV
1591
  }
×
1592
  return code;
1593
}
1,706,952✔
1594

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

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

1611
void qResetTaskCode(qTaskInfo_t tinfo) {
UNCOV
1612
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1613

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

×
1619
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
UNCOV
1620
  int32_t code = 0;
×
1621
  return code;
×
1622
}
×
1623

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

1630
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
1631
  int32_t code = 0;
50,097,631✔
1632

50,097,631✔
1633
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
1634
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
50,097,631✔
1635

50,095,212✔
1636
  if (pOper->fpSet.resetStateFn) {
1637
    code = pOper->fpSet.resetStateFn(pOper);
50,094,420✔
1638
  }
50,095,621✔
1639
  pOper->status = OP_NOT_OPENED;
1640
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
50,090,307✔
1641
    code = clearStatesForOperator(pOper->pDownstream[i]);
86,501,410✔
1642
  }
36,401,238✔
1643
  return code;
1644
}
50,100,341✔
1645

1646
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
1647
  int32_t        code = 0;
13,696,393✔
1648
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
13,696,393✔
1649
  SOperatorInfo* pOper = pTaskInfo->pRoot;
13,696,393✔
1650
  pTaskInfo->code = TSDB_CODE_SUCCESS;
13,696,393✔
1651
  code = clearStatesForOperator(pOper);
13,696,393✔
1652
  return code;
13,696,000✔
1653
}
13,696,393✔
1654

1655
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
1656
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
17,557,066✔
1657
  int64_t        threadId = taosGetSelfPthreadId();
17,557,066✔
1658
  int64_t        curOwner = 0;
17,557,066✔
1659

17,557,480✔
1660
  *ppRes = NULL;
1661

17,557,480✔
1662
  // todo extract method
1663
  taosRLockLatch(&pTaskInfo->lock);
1664
  bool isKilled = isTaskKilled(pTaskInfo);
17,557,480✔
1665
  if (isKilled) {
17,557,880✔
1666
    // clearStreamBlock(pTaskInfo->pRoot);
17,558,273✔
1667
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
UNCOV
1668

×
1669
    taosRUnLockLatch(&pTaskInfo->lock);
UNCOV
1670
    return pTaskInfo->code;
×
1671
  }
×
1672

1673
  if (pTaskInfo->owner != 0) {
1674
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
17,558,273✔
UNCOV
1675
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1676

×
1677
    taosRUnLockLatch(&pTaskInfo->lock);
UNCOV
1678
    return pTaskInfo->code;
×
1679
  }
×
1680

1681
  pTaskInfo->owner = threadId;
1682
  taosRUnLockLatch(&pTaskInfo->lock);
17,557,880✔
1683

17,557,880✔
1684
  if (pTaskInfo->cost.start == 0) {
1685
    pTaskInfo->cost.start = taosGetTimestampUs();
17,557,880✔
1686
  }
286,749✔
1687

1688
  // error occurs, record the error code and return to client
1689
  int32_t ret = setjmp(pTaskInfo->env);
1690
  if (ret != TSDB_CODE_SUCCESS) {
17,557,880✔
1691
    pTaskInfo->code = ret;
22,470,813✔
1692
    (void)cleanUpUdfs();
4,914,511✔
1693
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
4,914,904✔
1694
    atomic_store_64(&pTaskInfo->owner, 0);
4,914,904✔
1695
    return pTaskInfo->code;
4,914,904✔
1696
  }
4,914,904✔
1697

1698
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
1699

17,556,302✔
1700
  int64_t st = taosGetTimestampUs();
1701

17,558,273✔
1702
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
1703
  if (code) {
17,558,273✔
1704
    pTaskInfo->code = code;
12,642,583✔
UNCOV
1705
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1706
  } else {
×
1707
    *finished = *ppRes == NULL;
1708
    code = blockDataCheck(*ppRes);
12,642,583✔
1709
  }
12,642,577✔
1710
  if (code) {
1711
    pTaskInfo->code = code;
12,643,369✔
UNCOV
1712
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1713
  }
×
1714

1715
  uint64_t el = (taosGetTimestampUs() - st);
1716

12,642,177✔
1717
  pTaskInfo->cost.elapsedTime += el;
1718
  if (NULL == *ppRes) {
12,642,177✔
1719
    *useconds = pTaskInfo->cost.elapsedTime;
12,643,369✔
1720
  }
8,569,044✔
1721

1722
  (void)cleanUpUdfs();
1723

12,643,369✔
1724
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
1725
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
12,643,369✔
1726

12,643,369✔
1727
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
1728
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
12,643,369✔
1729

1730
  atomic_store_64(&pTaskInfo->owner, 0);
1731
  return pTaskInfo->code;
12,643,369✔
1732
}
12,643,369✔
1733

1734
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1735
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1736
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1737
// }
1738

1739
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
1740
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1,188,728✔
1741
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1742
  int32_t code = 0;                                        
1743
  if (*pTableListInfo != NULL) {
1,188,728✔
1744
    qDebug("table list already exists, no need to create again");
1,190,574✔
NEW
1745
    goto end;
×
NEW
1746
  }
×
1747
  STableListInfo* pList = tableListCreate();
1748
  if (pList == NULL) {
1749
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
1,190,574✔
1750
    code = terrno;
1,190,178✔
1751
    goto end;
1,190,158✔
1752
  }
1753

1,191,288✔
1754
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
1,190,887✔
UNCOV
1755
  SReadHandle    pHandle = {.vnode = pVnode};
×
UNCOV
1756
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
×
UNCOV
1757

×
1758
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
1759
  if (code != 0) {
1,190,887✔
1760
    tableListDestroy(pList);
1,191,288✔
1761
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
1762
    goto end;
1763
  }
11,856,877✔
1764
  *pTableListInfo = pList;
11,856,877✔
NEW
1765

×
1766
end:
1767
  return 0;
11,858,018✔
1768
}
8,876,521✔
1769

8,876,521✔
1770
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
8,876,521✔
1771
  bool   listAdded = false;
1772
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
2,981,876✔
1773
  if (code == 0 && !listAdded) {
275,289✔
1774
    int32_t numOfTables = taosArrayGetSize(pUidList);
275,289✔
1775
    for (int i = 0; i < numOfTables; i++) {
275,289✔
1776
      void* tmp = taosArrayGet(pUidList, i);
1777
      if (tmp == NULL) {
2,706,587✔
1778
        return terrno;
1779
      }
1780
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
943,858✔
1781

943,858✔
1782
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
297,605✔
1783
      if (p == NULL) {
297,605✔
NEW
1784
        return terrno;
×
1785
      }
1786
    }
1787
  }
943,858✔
1788
  return code;
1789
}
1790

10,792,892✔
1791
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
10,792,892✔
1792
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
8,821,182✔
1793
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1794
  int32_t code = TSDB_CODE_SUCCESS;
2,248,096✔
1795
  STableListInfo* pList = tableListCreate();
2,208,526✔
1796
  if (pList == NULL) {
1797
    code = terrno;
2,208,526✔
1798
    goto end;
2,208,526✔
1799
  }
1,936,419✔
1800
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
1801
  if (uidListCopy == NULL) {
1802
    code = terrno;
39,570✔
1803
    goto end;
1804
  }
1805
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
1,557,156✔
1806
  SReadHandle    pHandle = {.vnode = pVnode};
1807

5,435,194✔
1808
  pList->idInfo.suid = suid;
1809
  pList->idInfo.tableType = TD_SUPER_TABLE;
888,709✔
1810
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
297,605✔
1811
  if (code != TSDB_CODE_SUCCESS) {
140,311✔
1812
    goto end;
1813
  }                                              
1,394,032✔
1814
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
1815
  if (code != TSDB_CODE_SUCCESS) {
2,249,785✔
1816
    goto end;
2,249,785✔
1817
  }
2,249,785✔
1818
  *tableList = pList->pTableList;
2,249,785✔
1819
  pList->pTableList = NULL;
1820

720,583✔
1821
  taosArrayClear(uidList);
720,583✔
1822
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
1823
    void* tmp = taosArrayGet(uidListCopy, i);
1824
    if (tmp == NULL) {
720,970✔
1825
      continue;
1826
    }
720,970✔
1827
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
720,970✔
1828
    if (slot == NULL) {
720,970✔
1829
      if (taosArrayPush(uidList, tmp) == NULL) {
720,970✔
1830
        code = terrno;
720,970✔
1831
        goto end;
720,970✔
1832
      }
720,970✔
1833
    }
720,970✔
1834
  }
720,970✔
1835
end:
1836
  taosArrayDestroy(uidListCopy);
720,970✔
1837
  tableListDestroy(pList);
720,970✔
1838
  return code;
720,970✔
1839
}
720,970✔
1840

1841
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
720,970✔
1842
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
720,970✔
1843
  STableListInfo* pList = pTableListInfo;
1844
  return pList->pTableList;
720,970✔
1845
}
720,970✔
1846

1847
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
720,970✔
1848

720,970✔
1849
void streamDestroyExecTask(qTaskInfo_t tInfo) {
720,970✔
1850
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
1851
  qDestroyTask(tInfo);
1852
}
720,970✔
1853

720,970✔
1854
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
720,970✔
1855
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
720,970✔
1856
}
720,970✔
1857

720,970✔
1858
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
720,970✔
1859
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
720,970✔
UNCOV
1860
  int32_t      code = 0;
×
UNCOV
1861
  SNode*       pNode = 0;
×
UNCOV
1862
  SNodeList*   pList = NULL;
×
UNCOV
1863
  SExprInfo*   pExprInfo = NULL;
×
UNCOV
1864
  int32_t      numOfExprs = 1;
×
UNCOV
1865
  int32_t*     offset = 0;
×
1866
  STargetNode* pTargetNode = NULL;
1867
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
720,970✔
1868
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
720,970✔
1869

720,970✔
1870
  if (code == 0) {
720,970✔
1871
    pTargetNode->dataBlockId = 0;
720,970✔
1872
    pTargetNode->pExpr = pNode;
720,970✔
UNCOV
1873
    pTargetNode->slotId = 0;
×
1874
  }
1875
  if (code == 0) {
720,970✔
1876
    code = nodesMakeList(&pList);
720,970✔
1877
  }
1878
  if (code == 0) {
720,970✔
1879
    code = nodesListAppend(pList, (SNode*)pTargetNode);
1880
  }
720,642✔
1881
  if (code == 0) {
720,970✔
1882
    pNode = NULL;
720,970✔
1883
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
720,970✔
1884
  }
1885

1886
  if (code == 0) {
4,515,453✔
1887
    const char* pVal = NULL;
4,515,453✔
1888
    int32_t     len = 0;
4,515,453✔
1889
    SNode*      pSclNode = NULL;
4,515,453✔
1890
    switch (pExprInfo->pExpr->nodeType) {
4,515,453✔
1891
      case QUERY_NODE_FUNCTION:
4,515,453✔
1892
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
387✔
1893
        break;
387✔
1894
      case QUERY_NODE_OPERATOR:
1895
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
1896
        break;
387✔
1897
      default:
387✔
1898
        code = TSDB_CODE_OPS_NOT_SUPPORT;
1,161✔
1899
        break;
774✔
1900
    }
774✔
1901
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
774✔
1902
    SSDataBlock block = {0};
774✔
1903
    block.info.rows = 1;
774✔
1904
    SSDataBlock* pBlock = &block;
774✔
1905
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
1906
    if (tmp == NULL) {
1907
      code = terrno;
1908
    }
387✔
1909
    if (code == 0) {
387✔
UNCOV
1910
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
×
UNCOV
1911
    }
×
1912
    taosArrayDestroy(pBlockList);
1913
  }
1914
  nodesDestroyList(pList);
1915
  destroyExprInfo(pExprInfo, numOfExprs);
387✔
1916
  taosMemoryFreeClear(pExprInfo);
387✔
1917
  return code;
387✔
1918
}
387✔
1919

1,161✔
1920
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
774✔
1921
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
774✔
1922
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
774✔
1923
  int32_t        code = 0;
774✔
1924
  SNode*         pNode = NULL;
774✔
1925
  if (!pForceOutputCols) return 0;
774✔
1926
  if (!*pRes) {
387✔
1927
    code = createDataBlock(pRes);
387✔
1928
  }
1929

387✔
1930
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
387✔
1931
    int32_t idx = 0;
387✔
1932
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
387✔
1933
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
1934
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
774✔
1935
      colInfo.info.precision = pCol->type.precision;
1936
      colInfo.info.scale = pCol->type.scale;
774✔
1937
      code = blockDataAppendColInfo(*pRes, &colInfo);
774✔
1938
      if (code != 0) break;
1939
    }
387✔
1940
  }
387✔
1941

1942
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
387✔
1943
  if (code != TSDB_CODE_SUCCESS) {
387✔
1944
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
1945
    return code;
1946
  }
260,393✔
1947

260,393✔
1948
  // loop all exprs for force output, execute all exprs
260,393✔
1949
  int32_t idx = 0;
260,393✔
1950
  int32_t rowIdx = (*pRes)->info.rows;
260,393✔
1951
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
260,393✔
1952
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
260,393✔
1953
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
1954
    SScalarParam   dst = {0};
260,393✔
UNCOV
1955
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
×
UNCOV
1956
    code = nodesStringToNode(pCol->expr, &pNode);
×
UNCOV
1957
    if (code != 0) break;
×
UNCOV
1958
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
×
UNCOV
1959
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
×
UNCOV
1960
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
×
UNCOV
1961
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
×
1962
    } else {
UNCOV
1963
      dst.columnData = pInfo;
×
UNCOV
1964
      dst.numOfRows = rowIdx;
×
UNCOV
1965
      dst.colAlloced = false;
×
UNCOV
1966
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
×
UNCOV
1967
    }
×
1968
    ++idx;
UNCOV
1969
    // TODO sclFreeParam(&dst);
×
UNCOV
1970
    nodesDestroyNode(pNode);
×
UNCOV
1971
    if (code != 0) break;
×
1972
  }
260,393✔
1973
  if (code == TSDB_CODE_SUCCESS) {
260,393✔
1974
    (*pRes)->info.rows++;
260,393✔
UNCOV
1975
  }
×
UNCOV
1976
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
×
UNCOV
1977
  return code;
×
1978
}
1979

260,393✔
1980
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
260,393✔
UNCOV
1981
  int32_t      code = 0;
×
UNCOV
1982
  const char*  pVal = NULL;
×
UNCOV
1983
  SScalarParam dst = {0};
×
1984
  int32_t      len = 0;
1985
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
1986
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
260,393✔
1987
  // execute the expr
260,393✔
1988
  switch (pExpr->type) {
260,393✔
1989
    case QUERY_NODE_VALUE: {
260,393✔
1990
      SValueNode* pValue = (SValueNode*)pExpr;
260,393✔
1991
      int32_t     type = pValue->node.resType.type;
260,393✔
1992
      if (!IS_STR_DATA_TYPE(type)) {
260,393✔
1993
        qError("invalid sub tb expr with non-str type");
260,393✔
1994
        code = TSDB_CODE_INVALID_PARA;
×
1995
        break;
×
1996
      }
×
1997
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
1998
      if (pTmp == NULL) {
260,393✔
1999
        qError("invalid sub tb expr with null value");
260,393✔
2000
        code = TSDB_CODE_INVALID_PARA;
260,393✔
2001
        break;
260,393✔
2002
      }
260,393✔
2003
      pVal = varDataVal(pTmp);
×
UNCOV
2004
      len = varDataLen(pTmp);
×
2005
    } break;
2006
    case QUERY_NODE_FUNCTION: {
260,393✔
2007
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
260,393✔
2008
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
260,393✔
2009
        qError("invalid sub tb expr with non-str type func");
2010
        code = TSDB_CODE_INVALID_PARA;
260,393✔
2011
        break;
×
2012
      }
×
2013
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
UNCOV
2014
      if (!pCol) {
×
2015
        code = terrno;
2016
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
260,393✔
2017
        break;
260,393✔
2018
      }
×
2019

×
2020
      pCol->hasNull = true;
2021
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
260,393✔
2022
      pCol->info.colId = 0;
774✔
2023
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
774✔
2024
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
2025
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
2026
      code = colInfoDataEnsureCapacity(pCol, 1, true);
260,393✔
2027
      if (code != 0) {
260,393✔
2028
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
260,020✔
UNCOV
2029
        taosMemoryFree(pCol);
×
2030
        break;
×
2031
      }
2032
      dst.columnData = pCol;
2033
      dst.numOfRows = 1;
2034
      dst.colAlloced = true;
260,020✔
2035
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
260,393✔
2036
      if (colDataIsNull_var(dst.columnData, 0)) {
260,393✔
2037
        qInfo("invalid sub tb expr with null value");
2038
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2039
      }
276,553✔
2040
      if (code == 0) {
276,553✔
2041
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
276,553✔
2042
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
276,553✔
2043
      }
276,553✔
2044
    } break;
2045
    default:
276,553✔
2046
      qError("wrong subtable expr with type: %d", pExpr->type);
276,553✔
2047
      code = TSDB_CODE_OPS_NOT_SUPPORT;
276,553✔
2048
      break;
2049
  }
276,553✔
2050
  if (code == 0) {
276,553✔
2051
    if (!pVal || len == 0) {
276,553✔
2052
      qError("tbname generated with no characters which is not allowed");
2053
      code = TSDB_CODE_INVALID_PARA;
276,553✔
2054
    }
276,553✔
2055
    if(len > TSDB_TABLE_NAME_LEN - 1) {
276,553✔
2056
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
2057
      len = TSDB_TABLE_NAME_LEN - 1;
276,553✔
2058
    }
189,601✔
2059

189,601✔
2060
    memcpy(tbname, pVal, len);
2061
    tbname[len] = '\0';  // ensure null terminated
276,553✔
2062
    if (NULL != strchr(tbname, '.')) {
2063
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
276,553✔
2064
      qError("tbname generated with invalid characters, '.' is not allowed");
2065
    }
276,553✔
2066
  }
276,553✔
2067
  // TODO free dst
276,553✔
UNCOV
2068
  sclFreeParam(&dst);
×
2069
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
2070
  return code;
276,553✔
2071
}
276,553✔
2072

2073
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
276,553✔
2074
  if (pParam) {
276,553✔
2075
    if (pParam->tbname) {
276,553✔
2076
      taosMemFree(pParam->tbname);
276,553✔
2077
      pParam->tbname = NULL;
276,553✔
2078
    }
2079
    if (pParam->stbname) {
276,553✔
2080
      taosMemFree(pParam->stbname);
276,553✔
2081
      pParam->stbname = NULL;
276,553✔
2082
    }
2083
    if (pParam->dbFName) {
2084
      taosMemFree(pParam->dbFName);
276,553✔
2085
      pParam->dbFName = NULL;
276,553✔
2086
    }
2087
    if (pParam->pFields) {
276,553✔
2088
      taosArrayDestroy(pParam->pFields);
2089
      pParam->pFields = NULL;
276,553✔
2090
    }
276,553✔
2091
    if (pParam->pTagFields) {
276,553✔
2092
      taosArrayDestroy(pParam->pTagFields);
UNCOV
2093
      pParam->pTagFields = NULL;
×
2094
    }
2095
    taosMemFree(pParam);
2096
  }
276,553✔
2097
}
189,601✔
2098

189,601✔
2099
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
2100
  int32_t code = 0, lino = 0;
86,952✔
2101
  if (ppDst == NULL || pSrc == NULL) {
2102
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
2103
  }
276,553✔
2104
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
2105
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
276,553✔
UNCOV
2106

×
UNCOV
2107
  (*ppDst)->suid = pSrc->suid;
×
UNCOV
2108
  (*ppDst)->sver = pSrc->sver;
×
2109
  (*ppDst)->tbType = pSrc->tbType;
2110
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
UNCOV
2111
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
×
2112

2113
  if (pSrc->stbname) {
276,553✔
2114
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
2115
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
2116
  }
428✔
2117

428✔
2118
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
2119
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
UNCOV
2120

×
UNCOV
2121
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
×
2122

2123
  if (pSrc->pFields && pSrc->pFields->size > 0) {
2124
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
2125
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
2126
  } else {
2127
    (*ppDst)->pFields = NULL;
2128
  }
2129
  
2130
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
2131
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
2132
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
2133
  } else {
2134
    (*ppDst)->pTagFields = NULL;
2135
  }
2136

2137
_exit:
2138

2139
  if (code != 0) {
2140
    if (*ppDst) {
2141
      destroyStreamInserterParam(*ppDst);
2142
      *ppDst = NULL;
2143
    }
2144
    
2145
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2146
  }
2147
  return code;
2148
}
2149

2150
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
2151
  return doDropStreamTable(pMsgCb, pOutput, pReq);
2152
}
2153

2154
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
2155
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
2156
}
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