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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

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

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

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

64
static void initRefPool() {
639,444✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
639,444✔
66
  (void)atexit(cleanupRefPool);
639,444✔
67
}
639,444✔
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) {
16,982,444✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
16,982,444✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
16,982,555✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,478,191✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,478,222✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,478,404✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,478,189✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
58,199✔
158
        if (code) {
58,199✔
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);
12,503,813✔
166
  }
167

168
  return 0;
11,819,032✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
11,819,111✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
11,819,111✔
173
  pTaskInfo->id.queryId = queryId;
11,819,111✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
11,819,187✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
11,819,187✔
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,
145,185✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
145,185✔
209
    SExecTaskInfo* pTaskInfo = NULL;
23,672✔
210

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,416,836✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,294,821✔
249
    if (pSlotDesc->output) {
1,294,821✔
250
      ++(*numOfCols);
1,294,721✔
251
    }
252
  }
253

254
  return pTaskInfo;
122,015✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
740,779✔
258
  if (streamInserterParam == NULL) {
740,779✔
259
    return TSDB_CODE_SUCCESS;
430,585✔
260
  }
261

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

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

278
  return TSDB_CODE_SUCCESS;
310,194✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
310,194✔
329
    if (code) {
310,194✔
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,
740,779✔
334
         tstrerror(code));
335

336
_error:
143,031✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
6,605,564✔
348
  if (pInfo == NULL) {
6,605,564✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,605,564✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,605,564✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,605,564✔
354
    return false;
5,200✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
6,600,364✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
6,312,972✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
12,913,336✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

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

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,600,364✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
287,392✔
387
    setReadHandle(handle, pScanBaseInfo);
287,392✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
6,312,972✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,706,684✔
390
    setReadHandle(handle, pScanBaseInfo);
5,706,684✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
740,779✔
405

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

421
  return code;
740,779✔
422
}
423

424
typedef struct {
425
  tb_uid_t tableUid;
426
  tb_uid_t childUid;
427
  int8_t   check;
428
} STqPair;
429

430
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
69,997✔
431
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
432
  int32_t code = TSDB_CODE_SUCCESS;
69,997✔
433
  int32_t lino = 0;
69,997✔
434
  int8_t  locked = 0;
69,997✔
435
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
69,997✔
436

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
69,997✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
69,997✔
449

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

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

459
  locked = 1;
69,997✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
141,299✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
71,302✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
71,302✔
463

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

470
    tDecoderClear(&mr.coder);
71,302✔
471

472
    if (mr.me.type == TSDB_SUPER_TABLE) {
71,302✔
473
      continue;
×
474
    } else {
475
      if (type == TSDB_SUPER_TABLE) {
71,302✔
476
        // this new created child table does not belong to the scanned super table.
477
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
71,302✔
478
          continue;
×
479
        }
480
      } else {  // ordinary table
481
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
482
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
483
        if (mr.me.uid != uid) {
×
484
          continue;
×
485
        }
486
      }
487
    }
488

489
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
71,302✔
490
    if (pScanInfo->pTagCond != NULL) {
71,302✔
491
      // tb_uid_t id = mr.me.uid;
492
      item.check = 1;
61,983✔
493
    }
494
    if (taosArrayPush(tUid, &item) == NULL) {
71,302✔
495
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
496
    }
497
  }
498

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

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

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

517
      if (!qualified) {
61,983✔
518
        continue;
31,021✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
69,997✔
529

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

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

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

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

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

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

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

579
    bool   assignUid = false;
69,997✔
580
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
69,997✔
581
    char*  keyBuf = NULL;
69,997✔
582
    if (bufLen > 0) {
69,997✔
583
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
584
      keyBuf = taosMemoryMalloc(bufLen);
×
585
      if (keyBuf == NULL) {
×
586
        taosArrayDestroy(qa);
×
587
        return terrno;
×
588
      }
589
    }
590

591
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
69,997✔
592
    taosWLockLatch(&pTaskInfo->lock);
69,997✔
593

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

604
      if (bufLen > 0) {
40,281✔
605
        if (assignUid) {
×
606
          keyInfo.groupId = keyInfo.uid;
×
607
        } else {
608
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
609
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
610
          if (code != TSDB_CODE_SUCCESS) {
×
611
            taosMemoryFree(keyBuf);
×
612
            taosArrayDestroy(qa);
×
613
            taosWUnLockLatch(&pTaskInfo->lock);
×
614
            return code;
×
615
          }
616
        }
617
      }
618

619
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
40,281✔
620
      if (code != TSDB_CODE_SUCCESS) {
40,281✔
621
        taosMemoryFree(keyBuf);
×
622
        taosArrayDestroy(qa);
×
623
        taosWUnLockLatch(&pTaskInfo->lock);
×
624
        return code;
×
625
      }
626
    }
627

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

633
    taosArrayDestroy(qa);
69,997✔
634
  } else {  // remove the table id in current list
635
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
664✔
636
    taosWLockLatch(&pTaskInfo->lock);
664✔
637
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
664✔
638
    taosWUnLockLatch(&pTaskInfo->lock);
664✔
639
  }
640

641
  return code;
70,661✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
298,865,493✔
650
    return TSDB_CODE_INVALID_PARA;
×
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
298,872,241✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
298,872,241✔
655
    return TSDB_CODE_SUCCESS;
175,830,896✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
123,018,957✔
665
  *tversion = pSchemaInfo->tversion;
123,039,907✔
666
  *rversion = pSchemaInfo->rversion;
123,027,894✔
667
  if (pSchemaInfo->dbname) {
123,045,352✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
123,044,730✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
123,055,224✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
123,048,300✔
674
  } else {
UNCOV
675
    tableName[0] = 0;
×
676
  }
677

678
  *tbGet = true;
123,055,322✔
679

680
  return TSDB_CODE_SUCCESS;
123,048,358✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
175,827,151✔
684

685
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
686
  if (NULL == pParam) {
×
687
    return;
×
688
  }
689
  freeOperatorParam(pParam, OP_GET_PARAM);
×
690
}
691

692
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
38,015,955✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
38,015,955✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
38,015,955✔
695
}
38,015,955✔
696

697
int32_t qExecutorInit(void) {
4,708,629✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,708,629✔
699
  return TSDB_CODE_SUCCESS;
4,709,207✔
700
}
701

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

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

710
  readHandle->uid = 0;
177,770,030✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
177,783,608✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
177,681,475✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
89,810✔
714
    goto _error;
27,412✔
715
  }
716

717
  if (handle) {
177,622,966✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
177,539,053✔
719
    void*           pSinkManager = NULL;
177,568,910✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
177,383,398✔
721
    if (code != TSDB_CODE_SUCCESS) {
177,536,130✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
177,536,130✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
177,535,077✔
728
    if (code != TSDB_CODE_SUCCESS) {
177,434,243✔
729
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
730
      taosMemoryFree(pSinkManager);
×
731
      goto _error;
×
732
    }
733

734
    SDataSinkNode* pSink = NULL;
177,434,243✔
735
    if (readHandle->localExec) {
177,523,800✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
90,568✔
737
      if (code != TSDB_CODE_SUCCESS) {
90,568✔
738
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
739
               (*pTask)->id.str);
740
        taosMemoryFree(pSinkManager);
×
741
        goto _error;
×
742
      }
743
    }
744

745
    // pSinkParam has been freed during create sinker.
746
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
177,460,822✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
177,576,317✔
748
    if (code) {
177,425,847✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
629✔
750
    }
751
  }
752

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

756
_error:
9,013,495✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
177,776,502✔
759
}
760

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

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

773
  if (pLocal) {
241,572,035✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
234,512,443✔
775
  }
776

777
  taosArrayClear(pResList);
241,553,643✔
778

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

786
  if (pTaskInfo->cost.start == 0) {
241,563,194✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
173,768,631✔
788
  }
789

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

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
241,571,021✔
798
  if (ret != TSDB_CODE_SUCCESS) {
241,814,023✔
799
    pTaskInfo->code = ret;
269,699✔
800
    (void)cleanUpUdfs();
269,699✔
801

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

805
    return pTaskInfo->code;
269,699✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
241,544,324✔
809

810
  int32_t      current = 0;
241,545,303✔
811
  SSDataBlock* pRes = NULL;
241,545,303✔
812
  int64_t      st = taosGetTimestampUs();
241,580,725✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
241,580,725✔
815
    pTaskInfo->paramSet = true;
38,015,955✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
38,015,955✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
203,560,267✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
241,308,964✔
822
  code = blockDataCheck(pRes);
241,308,964✔
823
  QUERY_CHECK_CODE(code, lino, _end);
241,313,594✔
824

825
  if (pRes == NULL) {
241,313,594✔
826
    st = taosGetTimestampUs();
43,825,331✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
241,314,370✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
241,309,352✔
831
    rowsThreshold = 4096;
240,499,687✔
832
  }
833

834
  int32_t blockIndex = 0;
241,312,326✔
835
  while (pRes != NULL) {
631,460,470✔
836
    SSDataBlock* p = NULL;
446,286,971✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
446,267,883✔
838
      SSDataBlock* p1 = NULL;
309,450,902✔
839
      code = createOneDataBlock(pRes, true, &p1);
309,453,070✔
840
      QUERY_CHECK_CODE(code, lino, _end);
309,440,245✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
309,440,245✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
309,451,828✔
844
      p = p1;
309,451,828✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
136,836,980✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
136,835,878✔
848

849
      p = *(SSDataBlock**)tmp;
136,835,878✔
850
      code = copyDataBlock(p, pRes);
136,836,429✔
851
      QUERY_CHECK_CODE(code, lino, _end);
136,835,686✔
852
    }
853

854
    blockIndex += 1;
446,289,721✔
855

856
    current += p->info.rows;
446,289,721✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
446,292,040✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
446,290,847✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
446,290,847✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
446,290,847✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
390,149,979✔
867
    QUERY_CHECK_CODE(code, lino, _end);
390,100,286✔
868
    code = blockDataCheck(pRes);
390,100,286✔
869
    QUERY_CHECK_CODE(code, lino, _end);
390,148,577✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
241,314,367✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
809,850✔
874
  }
875

876
  *hasMore = (pRes != NULL);
241,317,421✔
877
  uint64_t el = (taosGetTimestampUs() - st);
241,298,733✔
878

879
  pTaskInfo->cost.elapsedTime += el;
241,298,733✔
880
  if (NULL == pRes) {
241,304,621✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
185,163,057✔
882
  }
883

884
_end:
241,152,633✔
885
  (void)cleanUpUdfs();
241,305,541✔
886

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

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

897
  return pTaskInfo->code;
241,321,406✔
898
}
899

900
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
901
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
902
  SArray*        pList = pTaskInfo->pResultBlockList;
×
903
  size_t         num = taosArrayGetSize(pList);
×
904
  for (int32_t i = 0; i < num; ++i) {
×
905
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
906
    if (p) {
×
907
      blockDataDestroy(*p);
×
908
    }
909
  }
910

911
  taosArrayClear(pTaskInfo->pResultBlockList);
×
912
}
×
913

914
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
41,206,768✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
41,206,768✔
916
  int64_t        threadId = taosGetSelfPthreadId();
41,206,768✔
917
  int64_t        curOwner = 0;
41,207,101✔
918

919
  *pRes = NULL;
41,207,101✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
41,207,101✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
41,207,545✔
924
  if (isKilled) {
41,207,375✔
925
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
926

927
    taosRUnLockLatch(&pTaskInfo->lock);
×
928
    return pTaskInfo->code;
×
929
  }
930

931
  if (pTaskInfo->owner != 0) {
41,207,375✔
932
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
933
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
934

935
    taosRUnLockLatch(&pTaskInfo->lock);
×
936
    return pTaskInfo->code;
×
937
  }
938

939
  pTaskInfo->owner = threadId;
41,206,939✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
41,206,939✔
941

942
  if (pTaskInfo->cost.start == 0) {
41,207,656✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
96,689✔
944
  }
945

946
  // error occurs, record the error code and return to client
947
  int32_t ret = setjmp(pTaskInfo->env);
41,207,545✔
948
  if (ret != TSDB_CODE_SUCCESS) {
41,206,464✔
949
    pTaskInfo->code = ret;
×
950
    (void)cleanUpUdfs();
×
951
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
952
    atomic_store_64(&pTaskInfo->owner, 0);
×
953
    return pTaskInfo->code;
×
954
  }
955

956
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
41,206,464✔
957

958
  int64_t st = taosGetTimestampUs();
41,206,990✔
959
  int32_t code = TSDB_CODE_SUCCESS;
41,206,990✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
41,206,990✔
961
    pTaskInfo->paramSet = true;
×
962
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
963
  } else {
964
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
41,206,768✔
965
  }
966
  if (code) {
41,191,617✔
967
    pTaskInfo->code = code;
×
968
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
969
  }
970

971
  code = blockDataCheck(*pRes);
41,191,617✔
972
  if (code) {
41,206,597✔
973
    pTaskInfo->code = code;
×
974
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
975
  }
976

977
  uint64_t el = (taosGetTimestampUs() - st);
41,198,239✔
978

979
  pTaskInfo->cost.elapsedTime += el;
41,198,239✔
980
  if (NULL == *pRes) {
41,204,507✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
4,145,344✔
982
  }
983

984
  (void)cleanUpUdfs();
41,205,057✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
41,207,101✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
41,207,656✔
988

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
41,207,212✔
993
  return pTaskInfo->code;
41,207,656✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
60,671,988✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
60,671,988✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
60,671,922✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
60,672,125✔
1000

1001
  if (!tmp) {
60,671,988✔
1002
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1003
    return terrno;
×
1004
  }
1005
  return TSDB_CODE_SUCCESS;
60,671,988✔
1006
}
1007

1008
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1009
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1010
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1011

1012
  if (key->refId < pInfo->refId) {
×
1013
    return -1;
×
1014
  } else if (key->refId > pInfo->refId) {
×
1015
    return 1;
×
1016
  }
1017

1018
  return 0;
×
1019
}
1020

1021
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1022
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1023
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1024
  if (idx >= 0) {
×
1025
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1026
  }
1027
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1028
}
×
1029

1030
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
42,879✔
1031
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
42,879✔
1032

1033
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
42,879✔
1034
  for (int32_t i = 0; i < num; ++i) {
48,357✔
1035
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
5,478✔
1036
    if (!pStop) {
5,478✔
1037
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1038
      continue;
×
1039
    }
1040
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
5,478✔
1041
    if (pExchangeInfo) {
5,478✔
1042
      int32_t code = tsem_post(&pExchangeInfo->ready);
5,478✔
1043
      if (code != TSDB_CODE_SUCCESS) {
5,478✔
1044
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1045
      } else {
1046
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
5,478✔
1047
      }
1048
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
5,478✔
1049
      if (code != TSDB_CODE_SUCCESS) {
5,478✔
1050
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1051
      }
1052
    }
1053
  }
1054

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
42,879✔
1067
  qStopTaskOperators(pTaskInfo);
42,879✔
1068

1069
  return TSDB_CODE_SUCCESS;
42,879✔
1070
}
1071

1072
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1073
  int64_t        st = taosGetTimestampMs();
×
1074
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1075
  if (pTaskInfo == NULL) {
×
1076
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1077
  }
1078

1079
  if (waitDuration > 0) {
×
1080
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1081
  } else {
1082
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1083
  }
1084

1085
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1086

1087
  if (waitDuration > 0) {
×
1088
    while (1) {
1089
      taosWLockLatch(&pTaskInfo->lock);
×
1090
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1091
        taosWUnLockLatch(&pTaskInfo->lock);
×
1092

1093
        taosMsleep(200);
×
1094

1095
        int64_t d = taosGetTimestampMs() - st;
×
1096
        if (d >= waitDuration && waitDuration >= 0) {
×
1097
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1098
          return TSDB_CODE_SUCCESS;
×
1099
        }
1100
      } else {  // not running now
1101
        pTaskInfo->code = rspCode;
×
1102
        taosWUnLockLatch(&pTaskInfo->lock);
×
1103
        return TSDB_CODE_SUCCESS;
×
1104
      }
1105
    }
1106
  }
1107

1108
  int64_t et = taosGetTimestampMs() - st;
×
1109
  if (et < waitDuration) {
×
1110
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1111
    return TSDB_CODE_SUCCESS;
×
1112
  }
1113
  return TSDB_CODE_SUCCESS;
×
1114
}
1115

1116
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1117
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1118
  if (NULL == pTaskInfo) {
×
1119
    return false;
×
1120
  }
1121

1122
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1123
}
1124

1125
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
178,522,076✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
178,522,076✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
178,523,330✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
178,521,479✔
1130
  if (pSummary->pRecoder != NULL) {
178,520,217✔
1131
    qDebug(
125,220,628✔
1132
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1133
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1134
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1135
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1136
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1137
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1138
  } else {
1139
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
53,297,865✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
178,518,342✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
193,118,116✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
193,118,116✔
1146
  if (pTaskInfo == NULL) {
193,118,116✔
1147
    return;
14,606,826✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
178,511,290✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
178,522,181✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
178,522,737✔
1157
  doDestroyTask(pTaskInfo);
178,521,978✔
1158
}
1159

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

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

1169
  while (1) {
119,228✔
1170
    uint16_t type = pOperator->operatorType;
241,243✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
241,243✔
1172
      *scanner = pOperator->info;
122,015✔
1173
      break;
122,015✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
119,228✔
1176
    }
1177
  }
1178
}
122,015✔
1179

1180
static int32_t getOpratorIntervalInfo(SOperatorInfo* pOperator, int64_t* pWaterMark, SInterval* pInterval,
×
1181
                                      STimeWindow* pLastWindow, TSKEY* pRecInteral) {
1182
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1183
    return getOpratorIntervalInfo(pOperator->pDownstream[0], pWaterMark, pInterval, pLastWindow, pRecInteral);
×
1184
  }
1185
  SStreamScanInfo* pScanOp = (SStreamScanInfo*)pOperator->info;
×
1186
  *pWaterMark = pScanOp->twAggSup.waterMark;
×
1187
  *pInterval = pScanOp->interval;
×
1188
  *pLastWindow = pScanOp->lastScanRange;
×
1189
  *pRecInteral = pScanOp->recalculateInterval;
×
1190
  return TSDB_CODE_SUCCESS;
×
1191
}
1192

1193
void* qExtractReaderFromTmqScanner(void* scanner) {
122,015✔
1194
  SStreamScanInfo* pInfo = scanner;
122,015✔
1195
  return (void*)pInfo->tqReader;
122,015✔
1196
}
1197

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

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

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

1213
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,584,942✔
1214
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,584,942✔
1215
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,584,942✔
1216
  return 0;
4,584,942✔
1217
  /*if (code != TSDB_CODE_SUCCESS) {
1218
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1219
    pTaskInfo->code = code;
1220
    T_LONG_JMP(pTaskInfo->env, code);
1221
  }*/
1222
}
1223

1224
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
151,841✔
1225
  memset(pCond, 0, sizeof(SQueryTableDataCond));
151,841✔
1226
  pCond->order = TSDB_ORDER_ASC;
151,841✔
1227
  pCond->numOfCols = pMtInfo->schema->nCols;
151,841✔
1228
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
151,959✔
1229
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
151,829✔
1230
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
151,900✔
1231
    taosMemoryFreeClear(pCond->colList);
118✔
1232
    taosMemoryFreeClear(pCond->pSlotList);
×
1233
    return terrno;
×
1234
  }
1235

1236
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
151,782✔
1237
  pCond->suid = pMtInfo->suid;
151,841✔
1238
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
151,900✔
1239
  pCond->startVersion = -1;
151,829✔
1240
  pCond->endVersion = sContext->snapVersion;
151,841✔
1241

1242
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
966,409✔
1243
    SColumnInfo* pColInfo = &pCond->colList[i];
814,639✔
1244
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
814,545✔
1245
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
814,663✔
1246
    if (pMtInfo->pExtSchemas != NULL) {
814,639✔
1247
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,738✔
1248
    }
1249
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
814,639✔
1250
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
814,651✔
1251

1252
    pCond->pSlotList[i] = i;
814,580✔
1253
  }
1254

1255
  return TSDB_CODE_SUCCESS;
151,959✔
1256
}
1257

1258
void qStreamSetOpen(qTaskInfo_t tinfo) {
40,897,766✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
40,897,766✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
40,897,766✔
1261
  pOperator->status = OP_NOT_OPENED;
40,900,598✔
1262
}
40,900,899✔
1263

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

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

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

1277
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,642,020✔
1278
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,353,980✔
1279
    if (pOperator == NULL || code != 0) {
4,353,589✔
1280
      return code;
×
1281
    }
1282

1283
    SStreamScanInfo* pInfo = pOperator->info;
4,353,761✔
1284
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,353,791✔
1285
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,353,297✔
1286
    walReaderVerifyOffset(pWalReader, pOffset);
4,353,980✔
1287
  }
1288
  // if pOffset equal to current offset, means continue consume
1289
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,641,529✔
1290
    return 0;
4,203,160✔
1291
  }
1292

1293
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
438,142✔
1294
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
283,014✔
1295
    if (pOperator == NULL || code != 0) {
282,959✔
1296
      return code;
×
1297
    }
1298

1299
    SStreamScanInfo* pInfo = pOperator->info;
283,112✔
1300
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
282,875✔
1301
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
282,959✔
1302
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
282,575✔
1303

1304
    if (pOffset->type == TMQ_OFFSET__LOG) {
282,179✔
1305
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
218,947✔
1306
      pScanBaseInfo->dataReader = NULL;
218,750✔
1307

1308
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
218,643✔
1309
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
218,643✔
1310
      walReaderVerifyOffset(pWalReader, pOffset);
218,971✔
1311
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
218,869✔
1312
      if (code < 0) {
218,971✔
1313
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
4,749✔
1314
        return code;
4,641✔
1315
      }
1316
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
63,688✔
1317
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1318
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1319
      int64_t uid = pOffset->uid;
64,183✔
1320
      int64_t ts = pOffset->ts;
64,024✔
1321
      int32_t index = 0;
63,613✔
1322

1323
      // this value may be changed if new tables are created
1324
      taosRLockLatch(&pTaskInfo->lock);
63,613✔
1325
      int32_t numOfTables = 0;
64,114✔
1326
      code = tableListGetSize(pTableListInfo, &numOfTables);
64,114✔
1327
      if (code != TSDB_CODE_SUCCESS) {
63,868✔
1328
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1329
        taosRUnLockLatch(&pTaskInfo->lock);
×
1330
        return code;
×
1331
      }
1332

1333
      if (uid == 0) {
63,868✔
1334
        if (numOfTables != 0) {
62,778✔
1335
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,374✔
1336
          if (!tmp) {
10,374✔
1337
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1338
            taosRUnLockLatch(&pTaskInfo->lock);
×
1339
            return terrno;
×
1340
          }
1341
          if (tmp) uid = tmp->uid;
10,374✔
1342
          ts = INT64_MIN;
10,299✔
1343
          pScanInfo->currentTable = 0;
10,299✔
1344
        } else {
1345
          taosRUnLockLatch(&pTaskInfo->lock);
52,404✔
1346
          qError("no table in table list, %s", id);
52,488✔
1347
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
52,572✔
1348
        }
1349
      }
1350
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,464✔
1351

1352
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
11,695✔
1353
             pInfo->pTableScanOp->resultInfo.totalRows);
1354
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
11,695✔
1355

1356
      // start from current accessed position
1357
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1358
      // position, let's find it from the beginning.
1359
      index = tableListFind(pTableListInfo, uid, 0);
11,695✔
1360
      taosRUnLockLatch(&pTaskInfo->lock);
11,695✔
1361

1362
      if (index >= 0) {
11,695✔
1363
        pScanInfo->currentTable = index;
11,695✔
1364
      } else {
1365
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1366
               numOfTables, pScanInfo->currentTable, id);
1367
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1368
      }
1369

1370
      STableKeyInfo keyInfo = {.uid = uid};
11,695✔
1371
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
11,695✔
1372

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

1381
      if (pScanBaseInfo->dataReader == NULL) {
11,695✔
1382
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
20,578✔
1383
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1384
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
10,289✔
1385
        if (code != TSDB_CODE_SUCCESS) {
10,087✔
1386
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1387
          return code;
×
1388
        }
1389

1390
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
10,087✔
1391
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1392
      } else {
1393
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,406✔
1394
        if (code != TSDB_CODE_SUCCESS) {
1,406✔
1395
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1396
          return code;
×
1397
        }
1398

1399
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
1,406✔
1400
        if (code != TSDB_CODE_SUCCESS) {
1,406✔
1401
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1402
          return code;
×
1403
        }
1404
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
1,406✔
1405
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1406
      }
1407

1408
      // restore the key value
1409
      pScanBaseInfo->cond.twindows.skey = oldSkey;
11,636✔
1410
    } else {
1411
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1412
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1413
    }
1414

1415
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1416
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
155,128✔
1417
      SStreamRawScanInfo* pInfo = pOperator->info;
152,147✔
1418
      SSnapContext*       sContext = pInfo->sContext;
152,147✔
1419
      SOperatorInfo*      p = NULL;
152,147✔
1420

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

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

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

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

1442
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
151,958✔
1443
      tableListClear(pTableListInfo);
151,804✔
1444

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

1450
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
151,900✔
1451
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
151,900✔
1452
      if (code != TSDB_CODE_SUCCESS) {
151,935✔
1453
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1454
        destroyMetaTableInfo(&mtInfo);
1455
        return code;
×
1456
      }
1457
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
151,935✔
1458
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1459
      } else {
1460
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
151,935✔
1461
      }
1462

1463
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
151,876✔
1464
      if (code != TSDB_CODE_SUCCESS) {
151,959✔
1465
        destroyMetaTableInfo(&mtInfo);
1466
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1467
        return code;
×
1468
      }
1469

1470
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
151,959✔
1471
      if (!pList) {
151,959✔
1472
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1473
        destroyMetaTableInfo(&mtInfo);
1474
        return code;
×
1475
      }
1476
      int32_t size = 0;
151,959✔
1477
      code = tableListGetSize(pTableListInfo, &size);
151,959✔
1478
      if (code != TSDB_CODE_SUCCESS) {
151,959✔
1479
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1480
        destroyMetaTableInfo(&mtInfo);
1481
        return code;
×
1482
      }
1483

1484
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
303,918✔
1485
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
151,959✔
1486
      if (code != TSDB_CODE_SUCCESS) {
151,782✔
1487
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1488
        destroyMetaTableInfo(&mtInfo);
1489
        return code;
×
1490
      }
1491

1492
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
151,782✔
1493
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
151,723✔
1494
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1495
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
151,664✔
1496
      pTaskInfo->streamInfo.schema = mtInfo.schema;
151,782✔
1497
      taosMemoryFreeClear(mtInfo.pExtSchemas);
151,782✔
1498

1499
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
151,782✔
1500
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
2,841✔
1501
      SStreamRawScanInfo* pInfo = pOperator->info;
1,053✔
1502
      SSnapContext*       sContext = pInfo->sContext;
1,053✔
1503
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,053✔
1504
      if (code != 0) {
982✔
1505
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1506
        return code;
×
1507
      }
1508
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
982✔
1509
             id);
1510
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
1,859✔
1511
      SStreamRawScanInfo* pInfo = pOperator->info;
1,999✔
1512
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
1,999✔
1513
      pInfo->dataReader = NULL;
1,999✔
1514
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
1,999✔
1515
    }
1516
  }
1517

1518
end:
380,718✔
1519
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
380,960✔
1520
  return 0;
381,047✔
1521
}
1522

1523
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
164,418,640✔
1524
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
164,418,640✔
1525
  if (pMsg->info.ahandle == NULL) {
164,429,436✔
1526
    qError("pMsg->info.ahandle is NULL");
585✔
1527
    return;
585✔
1528
  }
1529

1530
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
164,417,127✔
1531
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1532

1533
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
164,424,904✔
1534

1535
  if (pMsg->contLen > 0) {
164,424,138✔
1536
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
164,413,394✔
1537
    if (buf.pData == NULL) {
164,404,565✔
1538
      pMsg->code = terrno;
×
1539
    } else {
1540
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
164,404,565✔
1541
    }
1542
  }
1543

1544
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
164,414,992✔
1545
  rpcFreeCont(pMsg->pCont);
164,432,652✔
1546
  destroySendMsgInfo(pSendInfo);
164,410,587✔
1547
}
1548

1549
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1550
  int32_t        code = TSDB_CODE_SUCCESS;
×
1551
  int32_t        lino = 0;
×
1552
  SExecTaskInfo* pTaskInfo = tinfo;
×
1553
  SArray*        plist = NULL;
×
1554

1555
  code = getTableListInfo(pTaskInfo, &plist);
×
1556
  if (code || plist == NULL) {
×
1557
    return NULL;
×
1558
  }
1559

1560
  // only extract table in the first elements
1561
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1562

1563
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1564
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1565

1566
  int32_t numOfTables = 0;
×
1567
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1568
  QUERY_CHECK_CODE(code, lino, _end);
×
1569

1570
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1571
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1572
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1573
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1574
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1575
  }
1576

1577
  taosArrayDestroy(plist);
×
1578

1579
_end:
×
1580
  if (code != TSDB_CODE_SUCCESS) {
×
1581
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1582
    T_LONG_JMP(pTaskInfo->env, code);
×
1583
  }
1584
  return pUidList;
×
1585
}
1586

1587
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,417,702✔
1588
  int32_t        code = TSDB_CODE_SUCCESS;
3,417,702✔
1589
  int32_t        lino = 0;
3,417,702✔
1590
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,417,702✔
1591

1592
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,417,702✔
1593
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1594
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1595

1596
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1597
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1598
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,416,492✔
1599
    STableScanInfo* pScanInfo = pOperator->info;
1,707,944✔
1600

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

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

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

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

1627
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,709,756✔
1628
  if (code == 0) {
1,709,756✔
1629
    *pList = pArray;
1,709,756✔
1630
  } else {
1631
    taosArrayDestroy(pArray);
×
1632
  }
1633
  return code;
1,709,756✔
1634
}
1635

1636
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1637
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1638
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1639
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1640
  }
1641
  return 0;
×
1642
}
1643

1644
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1645
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1646
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1647
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1648
  }
1649
  return 0;
×
1650
}
1651

1652
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1653
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1654

1655
  int32_t code = pTaskInfo->code;
×
1656
  pTaskInfo->code = 0;
×
1657
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1658
}
×
1659

1660
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1661
  int32_t code = 0;
×
1662
  return code;
×
1663
}
1664

1665
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1666
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1667
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1668
  return code;
×
1669
}
1670

1671
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
40,647,512✔
1672
  int32_t code = 0;
40,647,512✔
1673

1674
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
40,647,512✔
1675
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
40,646,763✔
1676

1677
  if (pOper->fpSet.resetStateFn) {
40,647,115✔
1678
    code = pOper->fpSet.resetStateFn(pOper);
40,648,688✔
1679
  }
1680
  pOper->status = OP_NOT_OPENED;
40,643,537✔
1681
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
68,371,881✔
1682
    code = clearStatesForOperator(pOper->pDownstream[i]);
27,720,773✔
1683
  }
1684
  return code;
40,652,278✔
1685
}
1686

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

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

1701
  *ppRes = NULL;
17,561,869✔
1702

1703
  // todo extract method
1704
  taosRLockLatch(&pTaskInfo->lock);
17,561,869✔
1705
  bool isKilled = isTaskKilled(pTaskInfo);
17,561,869✔
1706
  if (isKilled) {
17,561,869✔
1707
    // clearStreamBlock(pTaskInfo->pRoot);
1708
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1709

1710
    taosRUnLockLatch(&pTaskInfo->lock);
×
1711
    return pTaskInfo->code;
×
1712
  }
1713

1714
  if (pTaskInfo->owner != 0) {
17,561,869✔
1715
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1716
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1717

1718
    taosRUnLockLatch(&pTaskInfo->lock);
×
1719
    return pTaskInfo->code;
×
1720
  }
1721

1722
  pTaskInfo->owner = threadId;
17,561,869✔
1723
  taosRUnLockLatch(&pTaskInfo->lock);
17,561,869✔
1724

1725
  if (pTaskInfo->cost.start == 0) {
17,561,869✔
1726
    pTaskInfo->cost.start = taosGetTimestampUs();
318,136✔
1727
  }
1728

1729
  // error occurs, record the error code and return to client
1730
  int32_t ret = setjmp(pTaskInfo->env);
17,561,869✔
1731
  if (ret != TSDB_CODE_SUCCESS) {
19,878,805✔
1732
    pTaskInfo->code = ret;
2,317,334✔
1733
    (void)cleanUpUdfs();
2,317,334✔
1734
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,317,334✔
1735
    atomic_store_64(&pTaskInfo->owner, 0);
2,317,334✔
1736
    return pTaskInfo->code;
2,317,334✔
1737
  }
1738

1739
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,561,471✔
1740

1741
  int64_t st = taosGetTimestampUs();
17,561,869✔
1742

1743
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,561,869✔
1744
  if (code) {
15,243,674✔
1745
    pTaskInfo->code = code;
×
1746
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1747
  } else {
1748
    *finished = *ppRes == NULL;
15,243,674✔
1749
    code = blockDataCheck(*ppRes);
15,244,535✔
1750
  }
1751
  if (code) {
15,244,535✔
1752
    pTaskInfo->code = code;
×
1753
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1754
  }
1755

1756
  uint64_t el = (taosGetTimestampUs() - st);
15,244,535✔
1757

1758
  pTaskInfo->cost.elapsedTime += el;
15,244,535✔
1759
  if (NULL == *ppRes) {
15,244,535✔
1760
    *useconds = pTaskInfo->cost.elapsedTime;
9,971,636✔
1761
  }
1762

1763
  (void)cleanUpUdfs();
15,244,535✔
1764

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

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

1771
  atomic_store_64(&pTaskInfo->owner, 0);
15,244,535✔
1772
  return pTaskInfo->code;
15,244,535✔
1773
}
1774

1775
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1776
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1777
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1778
// }
1779

1780
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
312,132✔
1781
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1782
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1783
  int32_t code = 0;                                        
312,132✔
1784
  if (*pTableListInfo != NULL) {
312,132✔
1785
    qDebug("table list already exists, no need to create again");
×
1786
    goto end;
×
1787
  }
1788
  STableListInfo* pList = tableListCreate();
312,632✔
1789
  if (pList == NULL) {
312,632✔
1790
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1791
    code = terrno;
×
1792
    goto end;
×
1793
  }
1794

1795
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
312,632✔
1796
  SReadHandle    pHandle = {.vnode = pVnode};
312,632✔
1797
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
312,632✔
1798

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

1807
end:
312,632✔
1808
  return 0;
312,632✔
1809
}
1810

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

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

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

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

1862
  taosArrayClear(uidList);
60,862✔
1863
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
121,724✔
1864
    void* tmp = taosArrayGet(uidListCopy, i);
60,862✔
1865
    if (tmp == NULL) {
60,862✔
1866
      continue;
×
1867
    }
1868
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
60,862✔
1869
    if (slot == NULL) {
60,862✔
1870
      if (taosArrayPush(uidList, tmp) == NULL) {
6,311✔
1871
        code = terrno;
×
1872
        goto end;
×
1873
      }
1874
    }
1875
  }
1876
end:
60,862✔
1877
  taosArrayDestroy(uidListCopy);
60,862✔
1878
  tableListDestroy(pList);
60,862✔
1879
  return code;
60,862✔
1880
}
1881

1882
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1883
//   int32_t index = -1;
1884
//   (void)taosThreadRwlockRdlock(lock);
1885
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1886
//     index = 0;
1887
//     goto end;
1888
//   }
1889
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1890
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1891

1892
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1893
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1894
//       index = i;
1895
//       goto end;
1896
//     }
1897
//   }
1898
// end:
1899
//   (void)taosThreadRwlockUnlock(lock);
1900
//   return index;
1901
// }
1902

1903
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
312,632✔
1904
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
312,632✔
1905
  STableListInfo* pList = pTableListInfo;
312,632✔
1906
  return pList->pTableList;
312,632✔
1907
}
1908

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

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

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

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

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

1948
  if (code == 0) {
873,897✔
1949
    const char* pVal = NULL;
873,897✔
1950
    int32_t     len = 0;
873,897✔
1951
    SNode*      pSclNode = NULL;
873,897✔
1952
    switch (pExprInfo->pExpr->nodeType) {
873,897✔
1953
      case QUERY_NODE_FUNCTION:
873,522✔
1954
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
873,522✔
1955
        break;
873,897✔
1956
      case QUERY_NODE_OPERATOR:
×
1957
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1958
        break;
×
1959
      default:
×
1960
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1961
        break;
×
1962
    }
1963
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
873,897✔
1964
    SSDataBlock block = {0};
873,897✔
1965
    block.info.rows = 1;
873,897✔
1966
    SSDataBlock* pBlock = &block;
873,897✔
1967
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
873,897✔
1968
    if (tmp == NULL) {
873,897✔
1969
      code = terrno;
×
1970
    }
1971
    if (code == 0) {
873,897✔
1972
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
873,897✔
1973
    }
1974
    taosArrayDestroy(pBlockList);
873,897✔
1975
  }
1976
  nodesDestroyList(pList);
873,493✔
1977
  destroyExprInfo(pExprInfo, numOfExprs);
873,897✔
1978
  taosMemoryFreeClear(pExprInfo);
873,897✔
1979
  return code;
873,897✔
1980
}
1981

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

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

2004
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
61,192✔
2005
  if (code != TSDB_CODE_SUCCESS) {
61,192✔
2006
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2007
    return code;
×
2008
  }
2009

2010
  // loop all exprs for force output, execute all exprs
2011
  int32_t idx = 0;
61,192✔
2012
  int32_t rowIdx = (*pRes)->info.rows;
61,192✔
2013
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
61,192✔
2014
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
61,192✔
2015
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
244,376✔
2016
    SScalarParam   dst = {0};
183,184✔
2017
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
183,184✔
2018
    code = nodesStringToNode(pCol->expr, &pNode);
183,184✔
2019
    if (code != 0) break;
183,184✔
2020
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
183,184✔
2021
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
183,184✔
2022
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
121,992✔
2023
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
121,992✔
2024
    } else {
2025
      dst.columnData = pInfo;
61,192✔
2026
      dst.numOfRows = rowIdx;
61,192✔
2027
      dst.colAlloced = false;
61,192✔
2028
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
61,192✔
2029
    }
2030
    ++idx;
183,184✔
2031
    // TODO sclFreeParam(&dst);
2032
    nodesDestroyNode(pNode);
183,184✔
2033
    if (code != 0) break;
183,184✔
2034
  }
2035
  if (code == TSDB_CODE_SUCCESS) {
61,192✔
2036
    (*pRes)->info.rows++;
61,192✔
2037
  }
2038
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
61,192✔
2039
  return code;
61,192✔
2040
}
2041

2042
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
309,212✔
2043
  int32_t      code = 0;
309,212✔
2044
  const char*  pVal = NULL;
309,212✔
2045
  SScalarParam dst = {0};
309,212✔
2046
  int32_t      len = 0;
309,212✔
2047
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
309,212✔
2048
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
309,212✔
2049
  // execute the expr
2050
  switch (pExpr->type) {
309,212✔
2051
    case QUERY_NODE_VALUE: {
×
2052
      SValueNode* pValue = (SValueNode*)pExpr;
×
2053
      int32_t     type = pValue->node.resType.type;
×
2054
      if (!IS_STR_DATA_TYPE(type)) {
×
2055
        qError("invalid sub tb expr with non-str type");
×
2056
        code = TSDB_CODE_INVALID_PARA;
×
2057
        break;
×
2058
      }
2059
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2060
      if (pTmp == NULL) {
×
2061
        qError("invalid sub tb expr with null value");
×
2062
        code = TSDB_CODE_INVALID_PARA;
×
2063
        break;
×
2064
      }
2065
      pVal = varDataVal(pTmp);
×
2066
      len = varDataLen(pTmp);
×
2067
    } break;
×
2068
    case QUERY_NODE_FUNCTION: {
309,212✔
2069
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
309,212✔
2070
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
309,212✔
2071
        qError("invalid sub tb expr with non-str type func");
×
2072
        code = TSDB_CODE_INVALID_PARA;
×
2073
        break;
×
2074
      }
2075
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
309,212✔
2076
      if (!pCol) {
309,212✔
2077
        code = terrno;
×
2078
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2079
        break;
×
2080
      }
2081

2082
      pCol->hasNull = true;
309,212✔
2083
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
309,212✔
2084
      pCol->info.colId = 0;
309,212✔
2085
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
309,212✔
2086
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
309,212✔
2087
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
309,212✔
2088
      code = colInfoDataEnsureCapacity(pCol, 1, true);
309,212✔
2089
      if (code != 0) {
309,212✔
2090
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2091
        taosMemoryFree(pCol);
×
2092
        break;
×
2093
      }
2094
      dst.columnData = pCol;
309,212✔
2095
      dst.numOfRows = 1;
309,212✔
2096
      dst.colAlloced = true;
309,212✔
2097
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
309,212✔
2098
      if (colDataIsNull_var(dst.columnData, 0)) {
309,212✔
2099
        qInfo("invalid sub tb expr with null value");
1,790✔
2100
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
1,790✔
2101
      }
2102
      if (code == 0) {
309,212✔
2103
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
307,422✔
2104
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
307,422✔
2105
      }
2106
    } break;
308,837✔
2107
    default:
×
2108
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2109
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2110
      break;
×
2111
  }
2112
  if (code == 0) {
308,837✔
2113
    if (!pVal || len == 0) {
307,422✔
2114
      qError("tbname generated with no characters which is not allowed");
×
2115
      code = TSDB_CODE_INVALID_PARA;
×
2116
    }
2117
    if(len > TSDB_TABLE_NAME_LEN - 1) {
307,422✔
2118
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
782✔
2119
      len = TSDB_TABLE_NAME_LEN - 1;
782✔
2120
    }
2121

2122
    memcpy(tbname, pVal, len);
307,422✔
2123
    tbname[len] = '\0';  // ensure null terminated
307,422✔
2124
    if (NULL != strchr(tbname, '.')) {
307,047✔
2125
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2126
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2127
    }
2128
  }
2129
  // TODO free dst
2130
  sclFreeParam(&dst);
308,462✔
2131
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
309,212✔
2132
  return code;
309,212✔
2133
}
2134

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

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

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

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

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

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

2185
  if (pSrc->pFields && pSrc->pFields->size > 0) {
310,194✔
2186
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
310,194✔
2187
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
310,194✔
2188
  } else {
2189
    (*ppDst)->pFields = NULL;
×
2190
  }
2191
  
2192
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
310,194✔
2193
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
196,103✔
2194
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
196,103✔
2195
  } else {
2196
    (*ppDst)->pTagFields = NULL;
114,091✔
2197
  }
2198

2199
_exit:
310,194✔
2200

2201
  if (code != 0) {
310,194✔
2202
    if (*ppDst) {
×
2203
      destroyStreamInserterParam(*ppDst);
×
2204
      *ppDst = NULL;
×
2205
    }
2206
    
2207
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2208
  }
2209
  return code;
310,194✔
2210
}
2211

2212
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
432✔
2213
  return doDropStreamTable(pMsgCb, pOutput, pReq);
432✔
2214
}
2215

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

© 2026 Coveralls, Inc