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

taosdata / TDengine / #4850

14 Nov 2025 08:06AM UTC coverage: 63.728% (-0.1%) from 63.829%
#4850

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

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

634 existing lines in 110 files now uncovered.

149066 of 233910 relevant lines covered (63.73%)

115676883.39 hits per line

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

67.78
/source/libs/executor/src/executor.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "taosdef.h"
32
#include "tarray.h"
33
#include "tdatablock.h"
34
#include "tref.h"
35
#include "trpc.h"
36
#include "tudf.h"
37
#include "wal.h"
38

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

43
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
677,696✔
44
  gExecInfo.dnode = pDnode;
677,696✔
45
  gExecInfo.getMnode = getMnode;
677,696✔
46
  gExecInfo.getDnodeId = getDnodeId;
677,696✔
47
  return;
677,696✔
48
}
49

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

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

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

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

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

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

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

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

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

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

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

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,209,788✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,209,788✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,209,889✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,136,570✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,136,671✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,136,640✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,136,536✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
56,438✔
158
        if (code) {
56,173✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,072,721✔
166
  }
167

168
  return 0;
10,651,858✔
169
}
170

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

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

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

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

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

194
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
195

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

203
  return code;
×
204
}
205

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

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,374,074✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,257,466✔
249
    if (pSlotDesc->output) {
1,257,466✔
250
      ++(*numOfCols);
1,257,360✔
251
    }
252
  }
253

254
  return pTaskInfo;
116,608✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
380,855✔
258
  if (streamInserterParam == NULL) {
380,855✔
259
    return TSDB_CODE_SUCCESS;
164,624✔
260
  }
261

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

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

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

278
  return TSDB_CODE_SUCCESS;
216,231✔
279
}
280

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

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

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

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

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

336
_error:
145,737✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
6,139,800✔
348
  if (pInfo == NULL) {
6,139,800✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,139,800✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,139,800✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,139,800✔
354
    return false;
4,776✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
6,135,024✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
6,001,406✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
12,136,430✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,135,024✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
133,618✔
387
    setReadHandle(handle, pScanBaseInfo);
133,618✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
6,001,406✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,883,030✔
390
    setReadHandle(handle, pScanBaseInfo);
5,883,030✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
380,855✔
405

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

421
  return code;
380,855✔
422
}
423

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

436
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
66,340✔
437

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

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

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

456
    tDecoderClear(&mr.coder);
67,249✔
457

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

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

484
      if (!qualified) {
58,476✔
485
        continue;
29,293✔
486
      }
487
    }
488

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

494
_end:
66,340✔
495

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

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

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

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

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

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

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

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

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

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

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

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

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

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

604
  return code;
66,565✔
605
}
606

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

612
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
302,765,515✔
613
    return TSDB_CODE_INVALID_PARA;
1,188✔
614
  }
615
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
302,765,355✔
616

617
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
302,765,355✔
618
    return TSDB_CODE_SUCCESS;
178,107,980✔
619
  }
620

621
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
124,663,270✔
622
  if (!pSchemaInfo) {
124,667,018✔
623
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
624
    return terrno;
×
625
  }
626

627
  *sversion = pSchemaInfo->sw->version;
124,667,018✔
628
  *tversion = pSchemaInfo->tversion;
124,668,002✔
629
  *rversion = pSchemaInfo->rversion;
124,675,333✔
630
  if (pSchemaInfo->dbname) {
124,666,063✔
631
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
124,662,782✔
632
  } else {
633
    dbName[0] = 0;
×
634
  }
635
  if (pSchemaInfo->tablename) {
124,667,253✔
636
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
124,666,385✔
637
  } else {
638
    tableName[0] = 0;
233✔
639
  }
640

641
  *tbGet = true;
124,673,402✔
642

643
  return TSDB_CODE_SUCCESS;
124,669,874✔
644
}
645

646
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
178,100,198✔
647

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

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

660
int32_t qExecutorInit(void) {
4,637,810✔
661
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,637,810✔
662
  return TSDB_CODE_SUCCESS;
4,638,515✔
663
}
664

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

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

673
  readHandle->uid = 0;
180,032,972✔
674
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
180,057,060✔
675
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
179,893,104✔
676
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
154,800✔
677
    goto _error;
27,135✔
678
  }
679

680
  if (handle) {
179,773,510✔
681
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
179,757,674✔
682
    void*           pSinkManager = NULL;
179,808,885✔
683
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
179,579,099✔
684
    if (code != TSDB_CODE_SUCCESS) {
179,593,010✔
685
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
686
      goto _error;
×
687
    }
688

689
    void* pSinkParam = NULL;
179,593,010✔
690
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
179,681,317✔
691
    if (code != TSDB_CODE_SUCCESS) {
179,619,428✔
692
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
693
      taosMemoryFree(pSinkManager);
×
694
      goto _error;
×
695
    }
696

697
    SDataSinkNode* pSink = NULL;
179,619,428✔
698
    if (readHandle->localExec) {
179,752,913✔
699
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
81,841✔
700
      if (code != TSDB_CODE_SUCCESS) {
81,841✔
701
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
702
               (*pTask)->id.str);
703
        taosMemoryFree(pSinkManager);
×
704
        goto _error;
×
705
      }
706
    }
707

708
    // pSinkParam has been freed during create sinker.
709
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
179,590,998✔
710
                              (*pTask)->id.str, pSubplan->processOneBlock);
179,823,904✔
711
    if (code) {
179,600,859✔
712
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
629✔
713
    }
714
  }
715

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

719
_error:
8,918,154✔
720
  // if failed to add ref for all tables in this query, abort current query
721
  return code;
180,041,490✔
722
}
723

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

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

736
  if (pLocal) {
239,776,001✔
737
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
233,450,776✔
738
  }
739

740
  taosArrayClear(pResList);
239,768,102✔
741

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

749
  if (pTaskInfo->cost.start == 0) {
239,749,542✔
750
    pTaskInfo->cost.start = taosGetTimestampUs();
175,808,007✔
751
  }
752

753
  if (isTaskKilled(pTaskInfo)) {
239,784,986✔
UNCOV
754
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
755
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
756
    return pTaskInfo->code;
×
757
  }
758

759
  // error occurs, record the error code and return to client
760
  int32_t ret = setjmp(pTaskInfo->env);
239,765,589✔
761
  if (ret != TSDB_CODE_SUCCESS) {
239,935,548✔
762
    pTaskInfo->code = ret;
192,520✔
763
    (void)cleanUpUdfs();
192,520✔
764

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

768
    return pTaskInfo->code;
192,520✔
769
  }
770

771
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
239,743,028✔
772

773
  int32_t      current = 0;
239,744,329✔
774
  SSDataBlock* pRes = NULL;
239,744,329✔
775
  int64_t      st = taosGetTimestampUs();
239,774,601✔
776

777
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
239,774,601✔
778
    pTaskInfo->paramSet = true;
37,059,392✔
779
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
37,059,392✔
780
  } else {
781
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
202,721,039✔
782
  }
783

784
  QUERY_CHECK_CODE(code, lino, _end);
239,586,844✔
785
  code = blockDataCheck(pRes);
239,586,844✔
786
  QUERY_CHECK_CODE(code, lino, _end);
239,585,239✔
787

788
  if (pRes == NULL) {
239,585,239✔
789
    st = taosGetTimestampUs();
46,257,358✔
790
  }
791

792
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
239,582,612✔
793
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
239,596,439✔
794
    rowsThreshold = 4096;
238,888,792✔
795
  }
796

797
  int32_t blockIndex = 0;
239,590,229✔
798
  while (pRes != NULL) {
628,800,058✔
799
    SSDataBlock* p = NULL;
442,044,884✔
800
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
442,054,288✔
801
      SSDataBlock* p1 = NULL;
305,506,921✔
802
      code = createOneDataBlock(pRes, true, &p1);
305,509,561✔
803
      QUERY_CHECK_CODE(code, lino, _end);
305,499,820✔
804

805
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
305,499,820✔
806
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
305,503,110✔
807
      p = p1;
305,503,110✔
808
    } else {
809
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
136,590,781✔
810
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
136,591,216✔
811

812
      p = *(SSDataBlock**)tmp;
136,591,216✔
813
      code = copyDataBlock(p, pRes);
136,591,216✔
814
      QUERY_CHECK_CODE(code, lino, _end);
136,581,694✔
815
    }
816

817
    blockIndex += 1;
442,076,274✔
818

819
    current += p->info.rows;
442,076,274✔
820
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
442,078,028✔
821
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
822
    void* tmp = taosArrayPush(pResList, &p);
442,074,952✔
823
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
442,074,952✔
824

825
    if (current >= rowsThreshold || processOneBlock) {
442,074,952✔
826
      break;
827
    }
828

829
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
389,245,795✔
830
    QUERY_CHECK_CODE(code, lino, _end);
389,216,321✔
831
    code = blockDataCheck(pRes);
389,216,321✔
832
    QUERY_CHECK_CODE(code, lino, _end);
389,246,622✔
833
  }
834

835
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
239,584,331✔
836
    pTaskInfo->pSubplan->rowsThreshold -= current;
700,664✔
837
  }
838

839
  *hasMore = (pRes != NULL);
239,584,929✔
840
  uint64_t el = (taosGetTimestampUs() - st);
239,566,369✔
841

842
  pTaskInfo->cost.elapsedTime += el;
239,566,369✔
843
  if (NULL == pRes) {
239,577,106✔
844
    *useconds = pTaskInfo->cost.elapsedTime;
186,747,384✔
845
  }
846

847
_end:
239,498,790✔
848
  (void)cleanUpUdfs();
239,587,253✔
849

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

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

860
  return pTaskInfo->code;
239,600,725✔
861
}
862

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

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

877
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
35,927,417✔
878
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
35,927,417✔
879
  int64_t        threadId = taosGetSelfPthreadId();
35,927,417✔
880
  int64_t        curOwner = 0;
35,928,046✔
881

882
  *pRes = NULL;
35,928,046✔
883

884
  // todo extract method
885
  taosRLockLatch(&pTaskInfo->lock);
35,928,046✔
886
  bool isKilled = isTaskKilled(pTaskInfo);
35,927,719✔
887
  if (isKilled) {
35,927,703✔
888
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
889

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

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

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

902
  pTaskInfo->owner = threadId;
35,927,392✔
903
  taosRUnLockLatch(&pTaskInfo->lock);
35,927,610✔
904

905
  if (pTaskInfo->cost.start == 0) {
35,928,474✔
906
    pTaskInfo->cost.start = taosGetTimestampUs();
91,857✔
907
  }
908

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

919
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
35,925,922✔
920

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

934
  code = blockDataCheck(*pRes);
35,916,800✔
935
  if (code) {
35,926,889✔
936
    pTaskInfo->code = code;
×
937
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
938
  }
939

940
  uint64_t el = (taosGetTimestampUs() - st);
35,920,339✔
941

942
  pTaskInfo->cost.elapsedTime += el;
35,920,339✔
943
  if (NULL == *pRes) {
35,922,972✔
944
    *useconds = pTaskInfo->cost.elapsedTime;
3,797,745✔
945
  }
946

947
  (void)cleanUpUdfs();
35,923,180✔
948

949
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
35,928,474✔
950
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
35,928,474✔
951

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

955
  atomic_store_64(&pTaskInfo->owner, 0);
35,928,521✔
956
  return pTaskInfo->code;
35,928,384✔
957
}
958

959
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
60,648,969✔
960
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
60,648,969✔
961
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
60,649,461✔
962
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
60,649,461✔
963

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

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

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

981
  return 0;
×
982
}
983

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

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

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

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

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

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

1029
  setTaskKilled(pTaskInfo, rspCode);
31,292✔
1030
  qStopTaskOperators(pTaskInfo);
31,292✔
1031

1032
  return TSDB_CODE_SUCCESS;
31,292✔
1033
}
1034

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

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

1048
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1049

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

1056
        taosMsleep(200);
×
1057

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

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

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

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

1088
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
180,434,356✔
1089
  STaskCostInfo* pSummary = &pTaskInfo->cost;
180,434,356✔
1090
  int64_t        idleTime = pSummary->start - pSummary->created;
180,436,315✔
1091

1092
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
180,437,728✔
1093
  if (pSummary->pRecoder != NULL) {
180,434,146✔
1094
    qDebug(
126,917,247✔
1095
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1096
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1097
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1098
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1099
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1100
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1101
  } else {
1102
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
53,514,313✔
1103
           pSummary->elapsedTime / 1000.0);
1104
  }
1105
}
180,431,560✔
1106

1107
void qDestroyTask(qTaskInfo_t qTaskHandle) {
188,383,903✔
1108
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
188,383,903✔
1109
  if (pTaskInfo == NULL) {
188,383,903✔
1110
    return;
7,952,775✔
1111
  }
1112

1113
  if (pTaskInfo->pRoot != NULL) {
180,431,128✔
1114
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
180,435,990✔
1115
  } else {
1116
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1117
  }
1118

1119
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
180,434,361✔
1120
  doDestroyTask(pTaskInfo);
180,435,207✔
1121
}
1122

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

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

1132
  while (1) {
113,762✔
1133
    uint16_t type = pOperator->operatorType;
230,370✔
1134
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
230,370✔
1135
      *scanner = pOperator->info;
116,608✔
1136
      break;
116,608✔
1137
    } else {
1138
      pOperator = pOperator->pDownstream[0];
113,762✔
1139
    }
1140
  }
1141
}
116,608✔
1142

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

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

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

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

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

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

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

1199
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
143,613✔
1200
  pCond->suid = pMtInfo->suid;
143,613✔
1201
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
143,439✔
1202
  pCond->startVersion = -1;
143,734✔
1203
  pCond->endVersion = sContext->snapVersion;
143,666✔
1204

1205
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
924,160✔
1206
    SColumnInfo* pColInfo = &pCond->colList[i];
780,781✔
1207
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
780,615✔
1208
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
781,023✔
1209
    if (pMtInfo->pExtSchemas != NULL) {
781,023✔
1210
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,416✔
1211
    }
1212
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
781,038✔
1213
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
780,645✔
1214

1215
    pCond->pSlotList[i] = i;
780,872✔
1216
  }
1217

1218
  return TSDB_CODE_SUCCESS;
143,681✔
1219
}
1220

1221
void qStreamSetOpen(qTaskInfo_t tinfo) {
35,639,350✔
1222
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
35,639,350✔
1223
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
35,639,350✔
1224
  pOperator->status = OP_NOT_OPENED;
35,642,663✔
1225
}
35,640,885✔
1226

1227
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,077,328✔
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,077,328✔
1229
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,077,328✔
1230
}
4,078,211✔
1231

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

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

1240
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,291,525✔
1241
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,015,568✔
1242
    if (pOperator == NULL || code != 0) {
4,014,914✔
1243
      return code;
107✔
1244
    }
1245

1246
    SStreamScanInfo* pInfo = pOperator->info;
4,014,807✔
1247
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,014,600✔
1248
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,015,214✔
1249
    walReaderVerifyOffset(pWalReader, pOffset);
4,015,770✔
1250
  }
1251
  // if pOffset equal to current offset, means continue consume
1252
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,290,699✔
1253
    return 0;
3,842,548✔
1254
  }
1255

1256
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
448,524✔
1257
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
301,396✔
1258
    if (pOperator == NULL || code != 0) {
301,540✔
UNCOV
1259
      return code;
×
1260
    }
1261

1262
    SStreamScanInfo* pInfo = pOperator->info;
301,540✔
1263
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
301,540✔
1264
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
301,405✔
1265
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
301,277✔
1266

1267
    if (pOffset->type == TMQ_OFFSET__LOG) {
301,488✔
1268
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
239,318✔
1269
      pScanBaseInfo->dataReader = NULL;
239,242✔
1270

1271
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
239,294✔
1272
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
239,242✔
1273
      walReaderVerifyOffset(pWalReader, pOffset);
239,297✔
1274
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
239,370✔
1275
      if (code < 0) {
239,370✔
1276
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,542✔
1277
        return code;
7,542✔
1278
      }
1279
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
62,183✔
1280
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1281
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1282
      int64_t uid = pOffset->uid;
62,253✔
1283
      int64_t ts = pOffset->ts;
62,170✔
1284
      int32_t index = 0;
61,882✔
1285

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

1296
      if (uid == 0) {
62,170✔
1297
        if (numOfTables != 0) {
61,035✔
1298
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,231✔
1299
          if (!tmp) {
10,231✔
1300
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1301
            taosRUnLockLatch(&pTaskInfo->lock);
×
1302
            return terrno;
×
1303
          }
1304
          if (tmp) uid = tmp->uid;
10,231✔
1305
          ts = INT64_MIN;
10,231✔
1306
          pScanInfo->currentTable = 0;
10,231✔
1307
        } else {
1308
          taosRUnLockLatch(&pTaskInfo->lock);
50,804✔
1309
          qError("no table in table list, %s", id);
50,653✔
1310
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
50,887✔
1311
        }
1312
      }
1313
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,366✔
1314

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1405
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
143,553✔
1406
      tableListClear(pTableListInfo);
143,674✔
1407

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

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

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

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

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

1455
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
143,613✔
1456
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
143,598✔
1457
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1458
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
143,424✔
1459
      pTaskInfo->streamInfo.schema = mtInfo.schema;
143,492✔
1460
      taosMemoryFreeClear(mtInfo.pExtSchemas);
143,666✔
1461

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

1481
end:
389,912✔
1482
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
390,322✔
1483
  return 0;
390,322✔
1484
}
1485

1486
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
160,078,750✔
1487
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
160,078,750✔
1488
  if (pMsg->info.ahandle == NULL) {
160,086,493✔
1489
    qError("pMsg->info.ahandle is NULL");
567✔
1490
    return;
567✔
1491
  }
1492

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

1496
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
160,070,879✔
1497

1498
  if (pMsg->contLen > 0) {
160,087,024✔
1499
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
160,078,293✔
1500
    if (buf.pData == NULL) {
160,067,604✔
1501
      pMsg->code = terrno;
×
1502
    } else {
1503
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
160,067,604✔
1504
    }
1505
  }
1506

1507
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
160,086,703✔
1508
  rpcFreeCont(pMsg->pCont);
160,093,876✔
1509
  destroySendMsgInfo(pSendInfo);
160,064,006✔
1510
}
1511

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

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

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

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

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

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

1540
  taosArrayDestroy(plist);
×
1541

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

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

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

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

1564
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,717,981✔
1565
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,718,562✔
1566
  } else {
1567
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,713,125✔
1568
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,715,540✔
1569
    }
1570
  }
1571

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

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

1584
  *pList = NULL;
1,716,147✔
1585
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,716,754✔
1586
  if (pArray == NULL) {
1,714,352✔
1587
    return terrno;
×
1588
  }
1589

1590
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,714,352✔
1591
  if (code == 0) {
1,717,968✔
1592
    *pList = pArray;
1,717,968✔
1593
  } else {
1594
    taosArrayDestroy(pArray);
×
1595
  }
1596
  return code;
1,717,968✔
1597
}
1598

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

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

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

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

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

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

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

1637
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
43,050,804✔
1638
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
43,046,083✔
1639

1640
  if (pOper->fpSet.resetStateFn) {
43,046,083✔
1641
    code = pOper->fpSet.resetStateFn(pOper);
43,048,077✔
1642
  }
1643
  pOper->status = OP_NOT_OPENED;
43,036,855✔
1644
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
73,464,321✔
1645
    code = clearStatesForOperator(pOper->pDownstream[i]);
30,414,308✔
1646
  }
1647
  return code;
43,054,381✔
1648
}
1649

1650
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
12,633,637✔
1651
  int32_t        code = 0;
12,633,637✔
1652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
12,633,637✔
1653
  SOperatorInfo* pOper = pTaskInfo->pRoot;
12,633,637✔
1654
  pTaskInfo->code = TSDB_CODE_SUCCESS;
12,634,885✔
1655
  code = clearStatesForOperator(pOper);
12,634,885✔
1656
  return code;
12,634,885✔
1657
}
1658

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

1664
  *ppRes = NULL;
16,572,316✔
1665

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

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

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

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

1685
  pTaskInfo->owner = threadId;
16,572,316✔
1686
  taosRUnLockLatch(&pTaskInfo->lock);
16,572,316✔
1687

1688
  if (pTaskInfo->cost.start == 0) {
16,572,316✔
1689
    pTaskInfo->cost.start = taosGetTimestampUs();
224,915✔
1690
  }
1691

1692
  // error occurs, record the error code and return to client
1693
  int32_t ret = setjmp(pTaskInfo->env);
16,572,316✔
1694
  if (ret != TSDB_CODE_SUCCESS) {
20,232,672✔
1695
    pTaskInfo->code = ret;
3,661,577✔
1696
    (void)cleanUpUdfs();
3,661,577✔
1697
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,661,183✔
1698
    atomic_store_64(&pTaskInfo->owner, 0);
3,661,183✔
1699
    return pTaskInfo->code;
3,661,577✔
1700
  }
1701

1702
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
16,571,095✔
1703

1704
  int64_t st = taosGetTimestampUs();
16,571,918✔
1705

1706
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
16,571,918✔
1707
  if (code) {
12,910,739✔
1708
    pTaskInfo->code = code;
×
1709
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1710
  } else {
1711
    *finished = *ppRes == NULL;
12,910,739✔
1712
    code = blockDataCheck(*ppRes);
12,910,739✔
1713
  }
1714
  if (code) {
12,910,739✔
1715
    pTaskInfo->code = code;
×
1716
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1717
  }
1718

1719
  uint64_t el = (taosGetTimestampUs() - st);
12,910,739✔
1720

1721
  pTaskInfo->cost.elapsedTime += el;
12,910,739✔
1722
  if (NULL == *ppRes) {
12,910,739✔
1723
    *useconds = pTaskInfo->cost.elapsedTime;
8,712,211✔
1724
  }
1725

1726
  (void)cleanUpUdfs();
12,910,739✔
1727

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

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

1734
  atomic_store_64(&pTaskInfo->owner, 0);
12,910,739✔
1735
  return pTaskInfo->code;
12,910,739✔
1736
}
1737

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

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

1758
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
10,581,588✔
1759
  SReadHandle    pHandle = {.vnode = pVnode};
10,581,588✔
1760
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
10,581,197✔
1761

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

1770
end:
10,581,944✔
1771
  return 0;
10,582,342✔
1772
}
1773

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

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

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

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

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

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

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

1866
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
10,581,168✔
1867
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
10,582,342✔
1868
  STableListInfo* pList = pTableListInfo;
10,582,342✔
1869
  return pList->pTableList;
10,582,342✔
1870
}
1871

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2148
  if (pSrc->pFields && pSrc->pFields->size > 0) {
216,231✔
2149
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
216,231✔
2150
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
216,231✔
2151
  } else {
2152
    (*ppDst)->pFields = NULL;
×
2153
  }
2154
  
2155
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
216,231✔
2156
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
156,607✔
2157
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
156,607✔
2158
  } else {
2159
    (*ppDst)->pTagFields = NULL;
59,624✔
2160
  }
2161

2162
_exit:
216,231✔
2163

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

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

2179
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2180
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc