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

taosdata / TDengine / #4898

26 Dec 2025 09:58AM UTC coverage: 65.061% (-0.7%) from 65.717%
#4898

push

travis-ci

web-flow
feat: support encryption of configuration files, data files and metadata files (#33801)

350 of 1333 new or added lines in 31 files covered. (26.26%)

2796 existing lines in 159 files now uncovered.

184024 of 282850 relevant lines covered (65.06%)

113940470.33 hits per line

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

68.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 setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
626,061,068✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
626,061,068✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
626,061,068✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
626,111,072✔
47
}
625,936,318✔
48

49
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
552,277✔
50
  gExecInfo.dnode = pDnode;
552,277✔
51
  gExecInfo.getMnode = getMnode;
552,277✔
52
  gExecInfo.getDnodeId = getDnodeId;
552,277✔
53
  return;
552,277✔
54
}
55

56
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
38,578✔
57
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
38,578✔
58
    qError("gExecInfo is not initialized");
203✔
59
    return TSDB_CODE_APP_ERROR;
×
60
  }
61
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
38,375✔
62
  return TSDB_CODE_SUCCESS;
38,375✔
63
}
64

65
static void cleanupRefPool() {
522,552✔
66
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
522,552✔
67
  taosCloseRef(ref);
522,552✔
68
}
522,552✔
69

70
static void initRefPool() {
522,552✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
522,552✔
72
  (void)atexit(cleanupRefPool);
522,552✔
73
}
522,552✔
74

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

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

93
    SStreamScanInfo* pInfo = pOperator->info;
×
94

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

131
_end:
×
132
  if (code != TSDB_CODE_SUCCESS) {
×
133
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
134
  }
135
  return code;
×
136
}
137

138
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
139
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
140
    if (pOperator->numOfDownstream == 0) {
×
141
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
142
      return TSDB_CODE_APP_ERROR;
×
143
    }
144

145
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
146
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
147
      return TSDB_CODE_APP_ERROR;
×
148
    }
149

150
    pOperator->status = OP_NOT_OPENED;
×
151
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
152
  }
153
  return 0;
×
154
}
155

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
42,404,609✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
42,404,609✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
42,405,074✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
19,169,764✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
19,169,764✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
19,169,089✔
162
      if (pScanInfo->base.dataReader != NULL) {
19,169,688✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
254,948✔
164
        if (code) {
254,948✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
23,235,552✔
172
  }
173

174
  return 0;
22,793,861✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
22,793,367✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
22,793,367✔
179
  pTaskInfo->id.queryId = queryId;
22,793,367✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
22,794,001✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
22,793,046✔
184
}
185

186
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
187
  SExecTaskInfo* pTaskInfo = tinfo;
×
188
  return pTaskInfo->status == OP_EXEC_DONE;
×
189
}
190

191
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
192
  if (tinfo == NULL) {
×
193
    return TSDB_CODE_APP_ERROR;
×
194
  }
195

196
  if (pBlocks == NULL || numOfBlocks == 0) {
×
197
    return TSDB_CODE_SUCCESS;
×
198
  }
199

200
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
201

202
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
203
  if (code != TSDB_CODE_SUCCESS) {
×
204
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
205
  } else {
206
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
207
  }
208

209
  return code;
×
210
}
211

212
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
411,883✔
213
  if (msg == NULL) {  // create raw scan
411,883✔
214
    SExecTaskInfo* pTaskInfo = NULL;
104,283✔
215

216
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
104,283✔
217
    if (NULL == pTaskInfo || code != 0) {
103,992✔
218
      return NULL;
×
219
    }
220

221
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
103,992✔
222
    if (NULL == pTaskInfo->pRoot || code != 0) {
104,283✔
223
      taosMemoryFree(pTaskInfo);
×
224
      return NULL;
×
225
    }
226

227
    pTaskInfo->storageAPI = pReaderHandle->api;
104,283✔
228
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
104,283✔
229
    return pTaskInfo;
104,283✔
230
  }
231

232
  SSubplan* pPlan = NULL;
307,600✔
233
  int32_t   code = qStringToSubplan(msg, &pPlan);
308,773✔
234
  if (code != TSDB_CODE_SUCCESS) {
309,072✔
235
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
236
    terrno = code;
×
237
    return NULL;
×
238
  }
239

240
  qTaskInfo_t pTaskInfo = NULL;
309,072✔
241
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
309,072✔
242
  if (code != TSDB_CODE_SUCCESS) {
309,072✔
243
    qDestroyTask(pTaskInfo);
×
244
    terrno = code;
×
245
    return NULL;
×
246
  }
247

248
  return pTaskInfo;
309,072✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
462,248✔
252
  if (streamInserterParam == NULL) {
462,248✔
253
    return TSDB_CODE_SUCCESS;
270,260✔
254
  }
255

256
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
191,988✔
257
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
258
    return TSDB_CODE_INVALID_PARA;
×
259
  }
260

261
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
191,988✔
262
    stError("insertParam: invalid db/table name");
×
263
    return TSDB_CODE_INVALID_PARA;
×
264
  }
265

266
  if (streamInserterParam->suid <= 0 &&
191,988✔
267
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
69,905✔
268
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

272
  return TSDB_CODE_SUCCESS;
191,988✔
273
}
274

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

295
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, NULL);
462,248✔
296
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
462,248✔
UNCOV
297
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
298
    goto _error;
×
299
  }
300

301
  if (streamInserterParam) {
462,248✔
302
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
191,988✔
303
    void*           pSinkManager = NULL;
191,988✔
304
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
191,988✔
305
    if (code != TSDB_CODE_SUCCESS) {
191,988✔
306
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
307
      goto _error;
×
308
    }
309

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
191,988✔
323
    if (code) {
191,988✔
324
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
325
    }
326
  }
327
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
462,248✔
328
         tstrerror(code));
329

330
_error:
75,879✔
331

332
  if (code != TSDB_CODE_SUCCESS) {
462,248✔
333
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
334
    if (pInserterParam != NULL) {
×
335
      taosMemoryFree(pInserterParam);
×
336
    }
337
  }
338
  return code;
462,248✔
339
}
340

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,166,591✔
342
  if (pInfo == NULL) {
3,166,591✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,166,591✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,166,591✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,166,591✔
348
    return false;
4,364✔
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,162,227✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,991,914✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
6,154,141✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
354
}
355

356
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
2,859,967✔
357
  if (pHandle == NULL || pScanBaseInfo == NULL) {
2,859,967✔
358
    return;
×
359
  }
360

361
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,859,967✔
362
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,859,967✔
363
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,859,967✔
364
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,859,967✔
365
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,859,967✔
366
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,859,967✔
367
}
368

369
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
3,162,227✔
370
  if (pInfo == NULL) {
3,162,227✔
371
    return TSDB_CODE_INVALID_PARA;
×
372
  }
373
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,162,227✔
374
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,162,227✔
375

376
  void*           info = pOperator->info;
3,162,227✔
377
  STableScanBase* pScanBaseInfo = NULL;
3,162,227✔
378

379
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,162,227✔
380
    pScanBaseInfo = &((STableScanInfo*)info)->base;
170,313✔
381
    setReadHandle(handle, pScanBaseInfo);
170,313✔
382
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,991,914✔
383
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,689,654✔
384
    setReadHandle(handle, pScanBaseInfo);
2,689,654✔
385
  }
386

387
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
3,162,227✔
388
  handle->winRange.ekey);
389
  return pOperator->fpSet.resetStateFn(pOperator);
3,162,227✔
390
}
391

392
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
461,358✔
393
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
394
  if (msg == NULL) {
461,358✔
395
    return TSDB_CODE_INVALID_PARA;
×
396
  }
397

398
  *pTaskInfo = NULL;
461,358✔
399

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

415
  return code;
462,248✔
416
}
417

418
typedef struct {
419
  tb_uid_t tableUid;
420
  tb_uid_t childUid;
421
  int8_t   check;
422
} STqPair;
423

424
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
338,372✔
425
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
426
  int32_t code = TSDB_CODE_SUCCESS;
338,372✔
427
  int32_t lino = 0;
338,372✔
428
  int8_t  locked = 0;
338,372✔
429
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
338,372✔
430

431
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
338,372✔
432

433
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
338,372✔
434
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
338,372✔
435

436
  int32_t numOfUids = taosArrayGetSize(tableIdList);
338,372✔
437
  if (numOfUids == 0) {
338,372✔
438
    (*ppArrayRes) = qa;
×
439
    goto _error;
×
440
  }
441

442
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
338,372✔
443

444
  uint64_t suid = 0;
338,372✔
445
  uint64_t uid = 0;
338,372✔
446
  int32_t  type = 0;
338,372✔
447
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
338,372✔
448

449
  // let's discard the tables those are not created according to the queried super table.
450
  SMetaReader mr = {0};
338,372✔
451
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
338,372✔
452

453
  locked = 1;
338,372✔
454
  for (int32_t i = 0; i < numOfUids; ++i) {
688,111✔
455
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
349,739✔
456
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
349,739✔
457

458
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
349,739✔
459
    if (code != TSDB_CODE_SUCCESS) {
349,739✔
460
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
461
      continue;
×
462
    }
463

464
    tDecoderClear(&mr.coder);
349,739✔
465

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

483
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
349,739✔
484
    if (pScanInfo->pTagCond != NULL) {
349,739✔
485
      // tb_uid_t id = mr.me.uid;
486
      item.check = 1;
306,950✔
487
    }
488
    if (taosArrayPush(tUid, &item) == NULL) {
349,739✔
489
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
490
    }
491
  }
492

493
  pAPI->metaReaderFn.clearReader(&mr);
338,372✔
494
  locked = 0;
338,372✔
495

496
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
688,111✔
497
    bool     qualified = false;
349,739✔
498
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
349,739✔
499
    if (t == NULL) {
349,739✔
500
      continue;
×
501
    }
502

503
    if (t->check == 1) {
349,739✔
504
      code = isQualifiedTable(t->childUid, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
306,950✔
505
      if (code != TSDB_CODE_SUCCESS) {
306,950✔
506
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", t->childUid, idstr);
×
507
        continue;
×
508
      }
509

510
      if (!qualified) {
306,950✔
511
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
153,493✔
512
        continue;
153,493✔
513
      }
514
    }
515

516
    void* tmp = taosArrayPush(qa, &t->tableUid);
196,246✔
517
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
196,246✔
518
  }
519

520
  // handle multiple partition
521

522
_end:
338,372✔
523

524
  if (locked) {
338,372✔
525
    pAPI->metaReaderFn.clearReader(&mr);
×
526
  }
527
  (*ppArrayRes) = qa;
338,372✔
528
_error:
338,372✔
529

530
  taosArrayDestroy(tUid);
338,372✔
531
  if (code != TSDB_CODE_SUCCESS) {
338,372✔
532
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
533
  }
534
  return code;
338,372✔
535
}
536

537
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
340,318✔
538
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
340,318✔
539
  const char*    id = GET_TASKID(pTaskInfo);
340,318✔
540
  int32_t        code = 0;
340,318✔
541

542
  if (isAdd) {
340,318✔
543
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
338,372✔
544
  }
545

546
  // traverse to the stream scanner node to add this table id
547
  SOperatorInfo* pInfo = NULL;
340,318✔
548
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
340,318✔
549
  if (code != 0 || pInfo == NULL) {
340,318✔
550
    return code;
×
551
  }
552

553
  SStreamScanInfo* pScanInfo = pInfo->info;
340,318✔
554
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
340,318✔
555
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
690,671✔
556
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
350,353✔
557
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
350,353✔
558
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
350,353✔
559
    }
560
  }
561

562
  if (isAdd) {  // add new table id
340,318✔
563
    SArray* qa = NULL;
338,372✔
564
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
338,372✔
565
    if (code != TSDB_CODE_SUCCESS) {
338,372✔
566
      taosArrayDestroy(qa);
×
567
      return code;
×
568
    }
569
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
338,372✔
570
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
338,372✔
571
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
338,372✔
572

573
    bool   assignUid = false;
338,372✔
574
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
338,372✔
575
    char*  keyBuf = NULL;
338,372✔
576
    if (bufLen > 0) {
338,372✔
577
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
578
      keyBuf = taosMemoryMalloc(bufLen);
×
579
      if (keyBuf == NULL) {
×
580
        taosArrayDestroy(qa);
×
581
        return terrno;
×
582
      }
583
    }
584

585
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
338,372✔
586
    taosWLockLatch(&pTaskInfo->lock);
338,372✔
587

588
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
534,618✔
589
      uint64_t* uid = taosArrayGet(qa, i);
196,246✔
590
      if (!uid) {
196,246✔
591
        taosMemoryFree(keyBuf);
×
592
        taosArrayDestroy(qa);
×
593
        taosWUnLockLatch(&pTaskInfo->lock);
×
594
        return terrno;
×
595
      }
596
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
196,246✔
597

598
      if (bufLen > 0) {
196,246✔
599
        if (assignUid) {
×
600
          keyInfo.groupId = keyInfo.uid;
×
601
        } else {
602
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
603
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
604
          if (code != TSDB_CODE_SUCCESS) {
×
605
            taosMemoryFree(keyBuf);
×
606
            taosArrayDestroy(qa);
×
607
            taosWUnLockLatch(&pTaskInfo->lock);
×
608
            return code;
×
609
          }
610
        }
611
      }
612

613
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
196,246✔
614
      if (code != TSDB_CODE_SUCCESS) {
196,246✔
615
        taosMemoryFree(keyBuf);
×
616
        taosArrayDestroy(qa);
×
617
        taosWUnLockLatch(&pTaskInfo->lock);
×
618
        return code;
×
619
      }
620
    }
621

622
    taosWUnLockLatch(&pTaskInfo->lock);
338,372✔
623
    if (keyBuf != NULL) {
338,372✔
624
      taosMemoryFree(keyBuf);
×
625
    }
626

627
    taosArrayDestroy(qa);
338,372✔
628
  } else {  // remove the table id in current list
629
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
1,946✔
630
    taosWLockLatch(&pTaskInfo->lock);
1,946✔
631
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,946✔
632
    taosWUnLockLatch(&pTaskInfo->lock);
1,946✔
633
  }
634

635
  return code;
340,318✔
636
}
637

638
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
540,877,130✔
639
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
640
                                    int32_t idx, bool* tbGet) {
641
  *tbGet = false;
540,877,130✔
642

643
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
541,007,759✔
644
    return TSDB_CODE_INVALID_PARA;
57,282✔
645
  }
646
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
540,950,579✔
647

648
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
540,950,579✔
649
    return TSDB_CODE_SUCCESS;
303,211,194✔
650
  }
651

652
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
237,846,472✔
653
  if (!pSchemaInfo) {
237,734,936✔
654
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
655
    return terrno;
×
656
  }
657

658
  *sversion = pSchemaInfo->sw->version;
237,734,936✔
659
  *tversion = pSchemaInfo->tversion;
237,903,126✔
660
  *rversion = pSchemaInfo->rversion;
237,866,813✔
661
  if (pSchemaInfo->dbname) {
237,775,220✔
662
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
237,806,901✔
663
  } else {
664
    dbName[0] = 0;
×
665
  }
666
  if (pSchemaInfo->tablename) {
237,957,112✔
667
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
237,891,884✔
668
  } else {
669
    tableName[0] = 0;
3,511✔
670
  }
671

672
  *tbGet = true;
237,949,110✔
673

674
  return TSDB_CODE_SUCCESS;
237,876,549✔
675
}
676

677
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
303,128,945✔
678

679
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
680
  if (NULL == pParam) {
×
681
    return;
×
682
  }
683
  freeOperatorParam(pParam, OP_GET_PARAM);
×
684
}
685

686
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
550,704✔
687
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
550,704✔
688
  ((SExecTaskInfo*)tinfo)->paramSet = false;
550,704✔
689
}
550,704✔
690

691
int32_t qExecutorInit(void) {
4,093,222✔
692
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,093,222✔
693
  return TSDB_CODE_SUCCESS;
4,093,740✔
694
}
695

696
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
37,952,995✔
697
  int32_t        code = TSDB_CODE_SUCCESS;
37,952,995✔
698
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
37,952,995✔
699
  if (pTask->pWorkerCb) {
37,952,995✔
700
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
37,953,651✔
701
    if (code != TSDB_CODE_SUCCESS) {
37,954,979✔
702
      pTask->code = code;
×
703
      return pTask->code;
×
704
    }
705
  }
706

707
  code = tsem_wait(pSem);
37,956,868✔
708
  if (code != TSDB_CODE_SUCCESS) {
37,954,545✔
709
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
710
    pTask->code = code;
×
711
    return pTask->code;
×
712
  }
713

714
  if (pTask->pWorkerCb) {
37,954,545✔
715
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
37,955,997✔
716
    if (code != TSDB_CODE_SUCCESS) {
37,956,868✔
717
      pTask->code = code;
×
718
      return pTask->code;
×
719
    }
720
  }
721
  return TSDB_CODE_SUCCESS;
37,955,924✔
722
}
723

724
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
309,150,458✔
725
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
726
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
727
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
309,150,458✔
728
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
309,150,458✔
729

730
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
309,155,062✔
731
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
732

733
  readHandle->uid = 0;
309,178,255✔
734
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
309,207,398✔
735
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
309,058,807✔
736
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
4,189,450✔
737
    goto _error;
4,050,628✔
738
  }
739
    
740
  if (handle) {
304,937,649✔
741
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
304,670,632✔
742
    void*           pSinkManager = NULL;
304,736,253✔
743
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
304,628,071✔
744
    if (code != TSDB_CODE_SUCCESS) {
304,457,174✔
745
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
746
      goto _error;
×
747
    }
748

749
    void* pSinkParam = NULL;
304,457,174✔
750
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
304,474,460✔
751
    if (code != TSDB_CODE_SUCCESS) {
304,520,666✔
752
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
753
      taosMemoryFree(pSinkManager);
×
754
      goto _error;
×
755
    }
756

757
    SDataSinkNode* pSink = NULL;
304,520,666✔
758
    if (readHandle->localExec) {
304,636,772✔
759
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,816✔
760
      if (code != TSDB_CODE_SUCCESS) {
2,816✔
761
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
762
               (*pTask)->id.str);
763
        taosMemoryFree(pSinkManager);
×
764
        goto _error;
×
765
      }
766
    }
767

768
    // pSinkParam has been freed during create sinker.
769
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
304,478,458✔
770
                              (*pTask)->id.str, pSubplan->processOneBlock);
304,751,745✔
771
    if (code) {
304,522,685✔
772
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
560✔
773
    }
774
  }
775

776
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
304,960,724✔
777
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
778

779
_error:
104,731,462✔
780
  // if failed to add ref for all tables in this query, abort current query
781
  return code;
309,090,950✔
782
}
783

784
static void freeBlock(void* param) {
×
785
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
786
  blockDataDestroy(pBlock);
×
787
}
×
788

789
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
319,928,490✔
790
                     bool processOneBlock) {
791
  int32_t        code = TSDB_CODE_SUCCESS;
319,928,490✔
792
  int32_t        lino = 0;
319,928,490✔
793
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
319,928,490✔
794
  int64_t        threadId = taosGetSelfPthreadId();
319,928,490✔
795

796
  if (pLocal) {
319,897,983✔
797
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
316,468,748✔
798
  }
799

800
  taosArrayClear(pResList);
319,821,652✔
801

802
  int64_t curOwner = 0;
319,841,658✔
803
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
319,841,658✔
804
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
805
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
806
    return pTaskInfo->code;
×
807
  }
808

809
  if (pTaskInfo->cost.start == 0) {
319,839,114✔
810
    pTaskInfo->cost.start = taosGetTimestampUs();
300,804,585✔
811
  }
812

813
  if (isTaskKilled(pTaskInfo)) {
319,942,814✔
814
    atomic_store_64(&pTaskInfo->owner, 0);
920✔
815
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
920✔
816
    return pTaskInfo->code;
920✔
817
  }
818

819
  // error occurs, record the error code and return to client
820
  int32_t ret = setjmp(pTaskInfo->env);
319,830,842✔
821
  if (ret != TSDB_CODE_SUCCESS) {
323,309,152✔
822
    pTaskInfo->code = ret;
3,582,710✔
823
    (void)cleanUpUdfs();
3,582,710✔
824

825
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
3,582,710✔
826
    atomic_store_64(&pTaskInfo->owner, 0);
3,582,710✔
827

828
    return pTaskInfo->code;
3,582,710✔
829
  }
830

831
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
319,726,442✔
832

833
  int32_t      current = 0;
319,728,620✔
834
  SSDataBlock* pRes = NULL;
319,728,620✔
835
  int64_t      st = taosGetTimestampUs();
319,875,241✔
836

837
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
319,875,241✔
838
    pTaskInfo->paramSet = true;
550,704✔
839
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
550,704✔
840
  } else {
841
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
319,277,831✔
842
  }
843

844
  QUERY_CHECK_CODE(code, lino, _end);
316,369,336✔
845
  code = blockDataCheck(pRes);
316,369,336✔
846
  QUERY_CHECK_CODE(code, lino, _end);
316,354,687✔
847

848
  if (pRes == NULL) {
316,354,687✔
849
    st = taosGetTimestampUs();
79,909,146✔
850
  }
851

852
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
316,353,282✔
853
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
316,387,624✔
854
    rowsThreshold = 4096;
314,618,754✔
855
  }
856

857
  int32_t blockIndex = 0;
316,357,464✔
858
  while (pRes != NULL) {
802,420,140✔
859
    SSDataBlock* p = NULL;
502,011,853✔
860
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
501,972,462✔
861
      SSDataBlock* p1 = NULL;
416,431,533✔
862
      code = createOneDataBlock(pRes, true, &p1);
416,434,668✔
863
      QUERY_CHECK_CODE(code, lino, _end);
416,401,351✔
864

865
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
416,401,351✔
866
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
416,458,972✔
867
      p = p1;
416,458,972✔
868
    } else {
869
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
85,574,326✔
870
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
85,573,867✔
871

872
      p = *(SSDataBlock**)tmp;
85,573,867✔
873
      code = copyDataBlock(p, pRes);
85,573,867✔
874
      QUERY_CHECK_CODE(code, lino, _end);
85,574,102✔
875
    }
876

877
    blockIndex += 1;
502,029,789✔
878

879
    current += p->info.rows;
502,029,789✔
880
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
502,030,702✔
881
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
882
    void* tmp = taosArrayPush(pResList, &p);
502,030,218✔
883
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
502,030,218✔
884

885
    if (current >= rowsThreshold || processOneBlock) {
502,030,218✔
886
      break;
887
    }
888

889
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
486,052,626✔
890
    QUERY_CHECK_CODE(code, lino, _end);
486,054,098✔
891
    code = blockDataCheck(pRes);
486,054,098✔
892
    QUERY_CHECK_CODE(code, lino, _end);
486,070,142✔
893
  }
894

895
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
316,385,879✔
896
    pTaskInfo->pSubplan->rowsThreshold -= current;
1,707,564✔
897
  }
898

899
  *hasMore = (pRes != NULL);
316,388,931✔
900
  uint64_t el = (taosGetTimestampUs() - st);
316,340,008✔
901

902
  pTaskInfo->cost.elapsedTime += el;
316,340,008✔
903
  if (NULL == pRes) {
316,333,409✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
300,355,817✔
905
  }
906

907
_end:
316,275,912✔
908
  (void)cleanUpUdfs();
316,312,134✔
909

910
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
316,428,950✔
911
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
316,429,425✔
912
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
913

914
  atomic_store_64(&pTaskInfo->owner, 0);
316,429,089✔
915
  if (code) {
316,428,902✔
916
    pTaskInfo->code = code;
×
917
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
918
  }
919

920
  return pTaskInfo->code;
316,428,902✔
921
}
922

923
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
924
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
925
  SArray*        pList = pTaskInfo->pResultBlockList;
×
926
  size_t         num = taosArrayGetSize(pList);
×
927
  for (int32_t i = 0; i < num; ++i) {
×
928
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
929
    if (p) {
×
930
      blockDataDestroy(*p);
×
931
    }
932
  }
933

934
  taosArrayClear(pTaskInfo->pResultBlockList);
×
935
}
×
936

937
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
119,879,833✔
938
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
119,879,833✔
939
  int64_t        threadId = taosGetSelfPthreadId();
119,879,833✔
940
  int64_t        curOwner = 0;
119,880,164✔
941

942
  *pRes = NULL;
119,880,164✔
943

944
  // todo extract method
945
  taosRLockLatch(&pTaskInfo->lock);
119,880,164✔
946
  bool isKilled = isTaskKilled(pTaskInfo);
119,880,495✔
947
  if (isKilled) {
119,880,164✔
948
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
949

950
    taosRUnLockLatch(&pTaskInfo->lock);
×
951
    return pTaskInfo->code;
×
952
  }
953

954
  if (pTaskInfo->owner != 0) {
119,880,164✔
955
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
956
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
957

958
    taosRUnLockLatch(&pTaskInfo->lock);
×
959
    return pTaskInfo->code;
×
960
  }
961

962
  pTaskInfo->owner = threadId;
119,880,164✔
963
  taosRUnLockLatch(&pTaskInfo->lock);
119,880,164✔
964

965
  if (pTaskInfo->cost.start == 0) {
119,880,495✔
966
    pTaskInfo->cost.start = taosGetTimestampUs();
216,963✔
967
  }
968

969
  // error occurs, record the error code and return to client
970
  int32_t ret = setjmp(pTaskInfo->env);
119,880,495✔
971
  if (ret != TSDB_CODE_SUCCESS) {
119,872,136✔
972
    pTaskInfo->code = ret;
×
973
    (void)cleanUpUdfs();
×
974
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
975
    atomic_store_64(&pTaskInfo->owner, 0);
×
976
    return pTaskInfo->code;
×
977
  }
978

979
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
119,872,136✔
980

981
  int64_t st = taosGetTimestampUs();
119,877,516✔
982
  int32_t code = TSDB_CODE_SUCCESS;
119,877,516✔
983
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
119,877,516✔
984
    pTaskInfo->paramSet = true;
×
985
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
986
  } else {
987
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
119,877,845✔
988
  }
989
  if (code) {
119,837,456✔
990
    pTaskInfo->code = code;
×
991
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
992
  }
993

994
  code = blockDataCheck(*pRes);
119,837,456✔
995
  if (code) {
119,876,705✔
996
    pTaskInfo->code = code;
×
997
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
998
  }
999

1000
  uint64_t el = (taosGetTimestampUs() - st);
119,861,535✔
1001

1002
  pTaskInfo->cost.elapsedTime += el;
119,861,535✔
1003
  if (NULL == *pRes) {
119,863,734✔
1004
    *useconds = pTaskInfo->cost.elapsedTime;
18,696,916✔
1005
  }
1006

1007
  (void)cleanUpUdfs();
119,865,927✔
1008

1009
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
119,880,147✔
1010
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
119,880,147✔
1011

1012
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
119,880,826✔
1013
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1014

1015
  atomic_store_64(&pTaskInfo->owner, 0);
119,880,114✔
1016
  return pTaskInfo->code;
119,880,826✔
1017
}
1018

1019
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
90,589,151✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
90,589,151✔
1021
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
90,589,151✔
1022
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
90,589,151✔
1023

1024
  if (!tmp) {
90,589,151✔
1025
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1026
    return terrno;
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
90,589,151✔
1029
}
1030

1031
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1032
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1033
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1034

1035
  if (key->refId < pInfo->refId) {
×
1036
    return -1;
×
1037
  } else if (key->refId > pInfo->refId) {
×
1038
    return 1;
×
1039
  }
1040

1041
  return 0;
×
1042
}
1043

1044
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1045
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1046
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1047
  if (idx >= 0) {
×
1048
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1049
  }
1050
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1051
}
×
1052

1053
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
38,127✔
1054
  if (pTaskInfo->subJobCtx.hasSubJobs) {
38,127✔
1055
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
18,220✔
1056
    if (pTaskInfo->subJobCtx.param) {
18,220✔
1057
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
6,108✔
1058
    }
1059
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
18,220✔
1060
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
18,220✔
1061
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
18,220✔
1062
  }
1063
  
1064
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
38,127✔
1065

1066
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
38,127✔
1067
  for (int32_t i = 0; i < num; ++i) {
39,216✔
1068
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
1,089✔
1069
    if (!pStop) {
1,089✔
1070
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1071
      continue;
×
1072
    }
1073
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
1,089✔
1074
    if (pExchangeInfo) {
1,089✔
1075
      int32_t code = tsem_post(&pExchangeInfo->ready);
1,089✔
1076
      if (code != TSDB_CODE_SUCCESS) {
1,089✔
1077
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1078
      } else {
1079
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
1,089✔
1080
      }
1081
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
1,089✔
1082
      if (code != TSDB_CODE_SUCCESS) {
1,089✔
1083
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1084
      }
1085
    }
1086
  }
1087

1088
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
38,127✔
1089
}
38,127✔
1090

1091
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
38,127✔
1092
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
38,127✔
1093
  if (pTaskInfo == NULL) {
38,127✔
1094
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1095
  }
1096

1097
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
38,127✔
1098

1099
  setTaskKilled(pTaskInfo, rspCode);
38,127✔
1100
  qStopTaskOperators(pTaskInfo);
38,127✔
1101

1102
  return TSDB_CODE_SUCCESS;
38,127✔
1103
}
1104

1105
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1106
  int64_t        st = taosGetTimestampMs();
×
1107
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1108
  if (pTaskInfo == NULL) {
×
1109
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1110
  }
1111

1112
  if (waitDuration > 0) {
×
1113
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1114
  } else {
1115
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1116
  }
1117

1118
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1119

1120
  if (waitDuration > 0) {
×
1121
    while (1) {
1122
      taosWLockLatch(&pTaskInfo->lock);
×
1123
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1124
        taosWUnLockLatch(&pTaskInfo->lock);
×
1125

1126
        taosMsleep(200);
×
1127

1128
        int64_t d = taosGetTimestampMs() - st;
×
1129
        if (d >= waitDuration && waitDuration >= 0) {
×
1130
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1131
          return TSDB_CODE_SUCCESS;
×
1132
        }
1133
      } else {  // not running now
1134
        pTaskInfo->code = rspCode;
×
1135
        taosWUnLockLatch(&pTaskInfo->lock);
×
1136
        return TSDB_CODE_SUCCESS;
×
1137
      }
1138
    }
1139
  }
1140

1141
  int64_t et = taosGetTimestampMs() - st;
×
1142
  if (et < waitDuration) {
×
1143
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1144
    return TSDB_CODE_SUCCESS;
×
1145
  }
1146
  return TSDB_CODE_SUCCESS;
×
1147
}
1148

1149
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1150
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1151
  if (NULL == pTaskInfo) {
×
1152
    return false;
×
1153
  }
1154

1155
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1156
}
1157

1158
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
305,669,561✔
1159
  STaskCostInfo* pSummary = &pTaskInfo->cost;
305,669,561✔
1160
  int64_t        idleTime = pSummary->start - pSummary->created;
305,700,679✔
1161

1162
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
305,704,771✔
1163
  if (pSummary->pRecoder != NULL) {
305,688,573✔
1164
    qDebug(
232,133,112✔
1165
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1166
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1167
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1168
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1169
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1170
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1171
  } else {
1172
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
73,548,131✔
1173
           pSummary->elapsedTime / 1000.0);
1174
  }
1175
}
305,680,677✔
1176

1177

1178
void qDestroyTask(qTaskInfo_t qTaskHandle) {
314,754,331✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
314,754,331✔
1180
  if (pTaskInfo == NULL) {
314,754,331✔
1181
    return;
9,045,511✔
1182
  }
1183

1184
  if (pTaskInfo->pRoot != NULL) {
305,708,820✔
1185
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
305,741,558✔
1186
  } else {
1187
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1188
  }
1189

1190
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
305,742,339✔
1191
  doDestroyTask(pTaskInfo);
305,713,253✔
1192
}
1193

1194
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
4,231,193✔
1195
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,231,193✔
1196
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
4,231,193✔
1197
}
1198

1199
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
309,072✔
1200
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
309,072✔
1201
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
309,072✔
1202

1203
  while (1) {
303,372✔
1204
    uint16_t type = pOperator->operatorType;
612,444✔
1205
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
612,444✔
1206
      *scanner = pOperator->info;
309,072✔
1207
      break;
309,072✔
1208
    } else {
1209
      pOperator = pOperator->pDownstream[0];
303,372✔
1210
    }
1211
  }
1212
}
309,072✔
1213

1214
void* qExtractReaderFromTmqScanner(void* scanner) {
309,072✔
1215
  SStreamScanInfo* pInfo = scanner;
309,072✔
1216
  return (void*)pInfo->tqReader;
309,072✔
1217
}
1218

1219
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
714,350✔
1220
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
714,350✔
1221
  return pTaskInfo->streamInfo.schema;
714,350✔
1222
}
1223

1224
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
714,631✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
714,631✔
1226
  return pTaskInfo->streamInfo.tbName;
714,631✔
1227
}
1228

1229
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
745,923✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
745,923✔
1231
  return &pTaskInfo->streamInfo.btMetaRsp;
745,923✔
1232
}
1233

1234
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
19,743,528✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,743,528✔
1236
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
19,743,528✔
1237
  return 0;
19,743,528✔
1238
  /*if (code != TSDB_CODE_SUCCESS) {
1239
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1240
    pTaskInfo->code = code;
1241
    T_LONG_JMP(pTaskInfo->env, code);
1242
  }*/
1243
}
1244

1245
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
707,573✔
1246
  memset(pCond, 0, sizeof(SQueryTableDataCond));
707,573✔
1247
  pCond->order = TSDB_ORDER_ASC;
707,573✔
1248
  pCond->numOfCols = pMtInfo->schema->nCols;
707,543✔
1249
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
707,272✔
1250
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
707,553✔
1251
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
707,834✔
1252
    taosMemoryFreeClear(pCond->colList);
1,144✔
1253
    taosMemoryFreeClear(pCond->pSlotList);
×
1254
    return terrno;
×
1255
  }
1256

1257
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
706,981✔
1258
  pCond->suid = pMtInfo->suid;
707,573✔
1259
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
707,282✔
1260
  pCond->startVersion = -1;
708,416✔
1261
  pCond->endVersion = sContext->snapVersion;
706,991✔
1262

1263
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,474,493✔
1264
    SColumnInfo* pColInfo = &pCond->colList[i];
3,766,067✔
1265
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,764,923✔
1266
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,765,495✔
1267
    if (pMtInfo->pExtSchemas != NULL) {
3,764,933✔
1268
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
43,840✔
1269
    }
1270
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,765,495✔
1271
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,766,057✔
1272

1273
    pCond->pSlotList[i] = i;
3,766,067✔
1274
  }
1275

1276
  return TSDB_CODE_SUCCESS;
708,416✔
1277
}
1278

1279
void qStreamSetOpen(qTaskInfo_t tinfo) {
118,427,895✔
1280
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
118,427,895✔
1281
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
118,427,895✔
1282
  pOperator->status = OP_NOT_OPENED;
118,444,274✔
1283
}
118,443,231✔
1284

1285
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
18,979,045✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
18,979,045✔
1287
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
18,979,045✔
1288
}
18,979,361✔
1289

1290
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
19,927,258✔
1291
  int32_t        code = TSDB_CODE_SUCCESS;
19,927,258✔
1292
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,927,258✔
1293
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
19,927,258✔
1294

1295
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
19,928,441✔
1296
  const char*    id = GET_TASKID(pTaskInfo);
19,927,498✔
1297

1298
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
19,926,605✔
1299
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
18,694,458✔
1300
    if (pOperator == NULL || code != 0) {
18,692,335✔
UNCOV
1301
      return code;
×
1302
    }
1303

1304
    SStreamScanInfo* pInfo = pOperator->info;
18,693,003✔
1305
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
18,692,428✔
1306
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
18,691,497✔
1307
    walReaderVerifyOffset(pWalReader, pOffset);
18,694,301✔
1308
  }
1309
  // if pOffset equal to current offset, means continue consume
1310
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
19,925,200✔
1311
    return 0;
18,127,882✔
1312
  }
1313

1314
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,796,318✔
1315
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,073,794✔
1316
    if (pOperator == NULL || code != 0) {
1,073,758✔
1317
      return code;
×
1318
    }
1319

1320
    SStreamScanInfo* pInfo = pOperator->info;
1,073,804✔
1321
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,073,804✔
1322
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,072,701✔
1323
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,073,474✔
1324

1325
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,071,994✔
1326
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
853,934✔
1327
      pScanBaseInfo->dataReader = NULL;
853,953✔
1328

1329
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
853,953✔
1330
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
853,708✔
1331
      walReaderVerifyOffset(pWalReader, pOffset);
853,321✔
1332
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
854,198✔
1333
      if (code < 0) {
854,198✔
1334
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,057✔
1335
        return code;
11,057✔
1336
      }
1337
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
219,552✔
1338
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1339
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1340
      int64_t uid = pOffset->uid;
219,851✔
1341
      int64_t ts = pOffset->ts;
219,245✔
1342
      int32_t index = 0;
219,835✔
1343

1344
      // this value may be changed if new tables are created
1345
      taosRLockLatch(&pTaskInfo->lock);
219,835✔
1346
      int32_t numOfTables = 0;
220,138✔
1347
      code = tableListGetSize(pTableListInfo, &numOfTables);
220,138✔
1348
      if (code != TSDB_CODE_SUCCESS) {
219,560✔
1349
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1350
        taosRUnLockLatch(&pTaskInfo->lock);
×
1351
        return code;
×
1352
      }
1353

1354
      if (uid == 0) {
219,560✔
1355
        if (numOfTables != 0) {
213,756✔
1356
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
36,836✔
1357
          if (!tmp) {
36,836✔
1358
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1359
            taosRUnLockLatch(&pTaskInfo->lock);
×
1360
            return terrno;
×
1361
          }
1362
          if (tmp) uid = tmp->uid;
36,836✔
1363
          ts = INT64_MIN;
36,836✔
1364
          pScanInfo->currentTable = 0;
36,836✔
1365
        } else {
1366
          taosRUnLockLatch(&pTaskInfo->lock);
176,920✔
1367
          qError("no table in table list, %s", id);
177,498✔
1368
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
177,498✔
1369
        }
1370
      }
1371
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
42,640✔
1372

1373
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
42,640✔
1374
             pInfo->pTableScanOp->resultInfo.totalRows);
1375
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
42,640✔
1376

1377
      // start from current accessed position
1378
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1379
      // position, let's find it from the beginning.
1380
      index = tableListFind(pTableListInfo, uid, 0);
42,640✔
1381
      taosRUnLockLatch(&pTaskInfo->lock);
42,640✔
1382

1383
      if (index >= 0) {
42,640✔
1384
        pScanInfo->currentTable = index;
42,640✔
1385
      } else {
1386
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1387
               numOfTables, pScanInfo->currentTable, id);
1388
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1389
      }
1390

1391
      STableKeyInfo keyInfo = {.uid = uid};
42,640✔
1392
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
42,640✔
1393

1394
      // let's start from the next ts that returned to consumer.
1395
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
42,640✔
1396
        pScanBaseInfo->cond.twindows.skey = ts;
862✔
1397
      } else {
1398
        pScanBaseInfo->cond.twindows.skey = ts + 1;
41,778✔
1399
      }
1400
      pScanInfo->scanTimes = 0;
42,640✔
1401

1402
      if (pScanBaseInfo->dataReader == NULL) {
42,640✔
1403
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
73,454✔
1404
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1405
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
36,727✔
1406
        if (code != TSDB_CODE_SUCCESS) {
36,727✔
1407
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1408
          return code;
×
1409
        }
1410

1411
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
36,727✔
1412
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1413
      } else {
1414
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
5,913✔
1415
        if (code != TSDB_CODE_SUCCESS) {
5,913✔
1416
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1417
          return code;
×
1418
        }
1419

1420
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
5,913✔
1421
        if (code != TSDB_CODE_SUCCESS) {
5,913✔
1422
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1423
          return code;
×
1424
        }
1425
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
5,913✔
1426
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1427
      }
1428

1429
      // restore the key value
1430
      pScanBaseInfo->cond.twindows.skey = oldSkey;
42,640✔
1431
    } else {
1432
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1433
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1434
    }
1435

1436
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1437
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
722,524✔
1438
      SStreamRawScanInfo* pInfo = pOperator->info;
709,209✔
1439
      SSnapContext*       sContext = pInfo->sContext;
709,209✔
1440
      SOperatorInfo*      p = NULL;
709,209✔
1441

1442
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
709,209✔
1443
      if (code != 0) {
709,209✔
1444
        return code;
×
1445
      }
1446

1447
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
709,209✔
1448

1449
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
709,209✔
1450
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1451
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1452
      }
1453

1454
      SMetaTableInfo mtInfo = {0};
709,209✔
1455
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
709,209✔
1456
      if (code != 0) {
708,647✔
1457
        destroyMetaTableInfo(&mtInfo);
1458
        return code;
×
1459
      }
1460
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
708,647✔
1461
      pInfo->dataReader = NULL;
708,065✔
1462

1463
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
708,065✔
1464
      tableListClear(pTableListInfo);
708,637✔
1465

1466
      if (mtInfo.uid == 0) {
708,918✔
1467
        destroyMetaTableInfo(&mtInfo);
1468
        goto end;  // no data
793✔
1469
      }
1470

1471
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
708,125✔
1472
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
707,573✔
1473
      if (code != TSDB_CODE_SUCCESS) {
708,135✔
1474
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
708,135✔
1479
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,234✔
1480
      } else {
1481
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
706,329✔
1482
      }
1483

1484
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
708,416✔
1485
      if (code != TSDB_CODE_SUCCESS) {
708,416✔
1486
        destroyMetaTableInfo(&mtInfo);
1487
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1488
        return code;
×
1489
      }
1490

1491
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
708,416✔
1492
      if (!pList) {
708,416✔
1493
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1494
        destroyMetaTableInfo(&mtInfo);
1495
        return code;
×
1496
      }
1497
      int32_t size = 0;
708,416✔
1498
      code = tableListGetSize(pTableListInfo, &size);
708,416✔
1499
      if (code != TSDB_CODE_SUCCESS) {
708,416✔
1500
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1501
        destroyMetaTableInfo(&mtInfo);
1502
        return code;
×
1503
      }
1504

1505
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,416,832✔
1506
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
708,416✔
1507
      if (code != TSDB_CODE_SUCCESS) {
706,991✔
1508
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1509
        destroyMetaTableInfo(&mtInfo);
1510
        return code;
×
1511
      }
1512

1513
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
706,991✔
1514
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
708,416✔
1515
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1516
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
707,563✔
1517
      pTaskInfo->streamInfo.schema = mtInfo.schema;
707,844✔
1518
      taosMemoryFreeClear(mtInfo.pExtSchemas);
707,844✔
1519

1520
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
707,844✔
1521
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
13,315✔
1522
      SStreamRawScanInfo* pInfo = pOperator->info;
4,491✔
1523
      SSnapContext*       sContext = pInfo->sContext;
4,491✔
1524
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,491✔
1525
      if (code != 0) {
4,491✔
1526
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1527
        return code;
×
1528
      }
1529
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,491✔
1530
             id);
1531
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
8,824✔
1532
      SStreamRawScanInfo* pInfo = pOperator->info;
8,824✔
1533
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
8,824✔
1534
      pInfo->dataReader = NULL;
8,824✔
1535
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
8,824✔
1536
    }
1537
  }
1538

1539
end:
1,608,195✔
1540
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,608,305✔
1541
  return 0;
1,608,060✔
1542
}
1543

1544
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
205,085,171✔
1545
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
205,085,171✔
1546
  if (pMsg->info.ahandle == NULL) {
205,110,811✔
1547
    rpcFreeCont(pMsg->pCont);
×
1548
    qError("pMsg->info.ahandle is NULL");
×
1549
    return;
×
1550
  }
1551

1552
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
205,069,626✔
1553
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1554

1555
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
205,073,994✔
1556

1557
  if (pMsg->contLen > 0) {
205,083,235✔
1558
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
205,062,265✔
1559
    if (buf.pData == NULL) {
204,999,362✔
1560
      pMsg->code = terrno;
×
1561
    } else {
1562
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
204,999,362✔
1563
    }
1564
  }
1565

1566
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
205,082,640✔
1567
  rpcFreeCont(pMsg->pCont);
205,122,351✔
1568
  destroySendMsgInfo(pSendInfo);
205,089,170✔
1569
}
1570

1571
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1572
  int32_t        code = TSDB_CODE_SUCCESS;
×
1573
  int32_t        lino = 0;
×
1574
  SExecTaskInfo* pTaskInfo = tinfo;
×
1575
  SArray*        plist = NULL;
×
1576

1577
  code = getTableListInfo(pTaskInfo, &plist);
×
1578
  if (code || plist == NULL) {
×
1579
    return NULL;
×
1580
  }
1581

1582
  // only extract table in the first elements
1583
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1584

1585
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1586
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1587

1588
  int32_t numOfTables = 0;
×
1589
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1590
  QUERY_CHECK_CODE(code, lino, _end);
×
1591

1592
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1593
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1594
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1595
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1596
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1597
  }
1598

1599
  taosArrayDestroy(plist);
×
1600

1601
_end:
×
1602
  if (code != TSDB_CODE_SUCCESS) {
×
1603
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1604
    T_LONG_JMP(pTaskInfo->env, code);
×
1605
  }
1606
  return pUidList;
×
1607
}
1608

1609
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,295,778✔
1610
  int32_t        code = TSDB_CODE_SUCCESS;
3,295,778✔
1611
  int32_t        lino = 0;
3,295,778✔
1612
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,295,778✔
1613

1614
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,295,778✔
1615
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1616
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1617

1618
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1619
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1620
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,295,778✔
1621
    STableScanInfo* pScanInfo = pOperator->info;
1,647,621✔
1622

1623
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,647,621✔
1624
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,648,157✔
1625
  } else {
1626
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,648,157✔
1627
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,648,157✔
1628
    }
1629
  }
1630

1631
_end:
×
1632
  if (code != TSDB_CODE_SUCCESS) {
3,296,314✔
1633
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1634
  }
1635
  return code;
3,296,314✔
1636
}
1637

1638
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,648,157✔
1639
  if (pList == NULL) {
1,648,157✔
1640
    return TSDB_CODE_INVALID_PARA;
×
1641
  }
1642

1643
  *pList = NULL;
1,648,157✔
1644
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,648,157✔
1645
  if (pArray == NULL) {
1,648,157✔
1646
    return terrno;
×
1647
  }
1648

1649
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,648,157✔
1650
  if (code == 0) {
1,648,157✔
1651
    *pList = pArray;
1,648,157✔
1652
  } else {
1653
    taosArrayDestroy(pArray);
×
1654
  }
1655
  return code;
1,648,157✔
1656
}
1657

1658
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1659
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1660
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1661
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1662
  }
1663
  return 0;
×
1664
}
1665

1666
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1667
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1668
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1669
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1670
  }
1671
  return 0;
×
1672
}
1673

1674
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1675
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1676

1677
  int32_t code = pTaskInfo->code;
×
1678
  pTaskInfo->code = 0;
×
1679
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1680
}
×
1681

1682
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1683
  int32_t code = 0;
×
1684
  return code;
×
1685
}
1686

1687
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1688
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1689
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1690
  return code;
×
1691
}
1692

1693
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
25,357,309✔
1694
  int32_t code = 0;
25,357,309✔
1695

1696
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
25,357,309✔
1697
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
25,355,993✔
1698

1699
  if (pOper->fpSet.resetStateFn) {
25,357,290✔
1700
    code = pOper->fpSet.resetStateFn(pOper);
25,358,745✔
1701
  }
1702
  pOper->status = OP_NOT_OPENED;
25,355,250✔
1703
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
43,149,969✔
1704
    code = clearStatesForOperator(pOper->pDownstream[i]);
17,789,902✔
1705
  }
1706
  return code;
25,360,732✔
1707
}
1708

1709
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,568,272✔
1710
  int32_t        code = 0;
7,568,272✔
1711
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,568,272✔
1712
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,568,272✔
1713
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,568,272✔
1714
  code = clearStatesForOperator(pOper);
7,568,272✔
1715
  return code;
7,568,053✔
1716
}
1717

1718
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,582,320✔
1719
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,582,320✔
1720
  int64_t        threadId = taosGetSelfPthreadId();
10,582,320✔
1721
  int64_t        curOwner = 0;
10,582,320✔
1722

1723
  *ppRes = NULL;
10,582,320✔
1724

1725
  // todo extract method
1726
  taosRLockLatch(&pTaskInfo->lock);
10,582,320✔
1727
  bool isKilled = isTaskKilled(pTaskInfo);
10,582,320✔
1728
  if (isKilled) {
10,582,320✔
1729
    // clearStreamBlock(pTaskInfo->pRoot);
1730
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1731

1732
    taosRUnLockLatch(&pTaskInfo->lock);
×
1733
    return pTaskInfo->code;
×
1734
  }
1735

1736
  if (pTaskInfo->owner != 0) {
10,582,320✔
1737
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1738
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1739

1740
    taosRUnLockLatch(&pTaskInfo->lock);
×
1741
    return pTaskInfo->code;
×
1742
  }
1743

1744
  pTaskInfo->owner = threadId;
10,582,320✔
1745
  taosRUnLockLatch(&pTaskInfo->lock);
10,582,320✔
1746

1747
  if (pTaskInfo->cost.start == 0) {
10,582,320✔
1748
    pTaskInfo->cost.start = taosGetTimestampUs();
196,292✔
1749
  }
1750

1751
  // error occurs, record the error code and return to client
1752
  int32_t ret = setjmp(pTaskInfo->env);
10,582,320✔
1753
  if (ret != TSDB_CODE_SUCCESS) {
12,213,478✔
1754
    pTaskInfo->code = ret;
1,631,761✔
1755
    (void)cleanUpUdfs();
1,631,560✔
1756
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,631,761✔
1757
    atomic_store_64(&pTaskInfo->owner, 0);
1,631,761✔
1758
    return pTaskInfo->code;
1,631,761✔
1759
  }
1760

1761
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,581,717✔
1762

1763
  int64_t st = taosGetTimestampUs();
10,582,320✔
1764

1765
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,582,320✔
1766
  if (code) {
8,950,559✔
1767
    pTaskInfo->code = code;
×
1768
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1769
  } else {
1770
    *finished = *ppRes == NULL;
8,950,559✔
1771
    code = blockDataCheck(*ppRes);
8,950,559✔
1772
  }
1773
  if (code) {
8,950,559✔
1774
    pTaskInfo->code = code;
×
1775
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1776
  }
1777

1778
  uint64_t el = (taosGetTimestampUs() - st);
8,949,876✔
1779

1780
  pTaskInfo->cost.elapsedTime += el;
8,949,876✔
1781
  if (NULL == *ppRes) {
8,949,876✔
1782
    *useconds = pTaskInfo->cost.elapsedTime;
5,614,717✔
1783
  }
1784

1785
  (void)cleanUpUdfs();
8,949,876✔
1786

1787
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,950,559✔
1788
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,950,559✔
1789

1790
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,950,559✔
1791
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1792

1793
  atomic_store_64(&pTaskInfo->owner, 0);
8,950,559✔
1794
  return pTaskInfo->code;
8,950,559✔
1795
}
1796

1797
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1798
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1799
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1800
// }
1801

1802
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
212,328✔
1803
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1804
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1805
  int32_t code = 0;                                        
212,328✔
1806
  if (*pTableListInfo != NULL) {
212,328✔
1807
    qDebug("table list already exists, no need to create again");
×
1808
    goto end;
×
1809
  }
1810
  STableListInfo* pList = tableListCreate();
212,506✔
1811
  if (pList == NULL) {
212,506✔
1812
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1813
    code = terrno;
×
1814
    goto end;
×
1815
  }
1816

1817
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
212,506✔
1818
  SReadHandle    pHandle = {.vnode = pVnode};
212,506✔
1819
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
212,506✔
1820

1821
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
212,506✔
1822
  if (code != 0) {
212,506✔
1823
    tableListDestroy(pList);
×
1824
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1825
    goto end;
×
1826
  }
1827
  *pTableListInfo = pList;
212,506✔
1828

1829
end:
212,506✔
1830
  return 0;
212,506✔
1831
}
1832

1833
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
29,586✔
1834
  bool   listAdded = false;
29,586✔
1835
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
29,586✔
1836
  if (code == 0 && !listAdded) {
29,586✔
1837
    int32_t numOfTables = taosArrayGetSize(pUidList);
24,159✔
1838
    for (int i = 0; i < numOfTables; i++) {
48,318✔
1839
      void* tmp = taosArrayGet(pUidList, i);
24,159✔
1840
      if (tmp == NULL) {
24,159✔
1841
        return terrno;
×
1842
      }
1843
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
24,159✔
1844

1845
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
24,159✔
1846
      if (p == NULL) {
24,159✔
1847
        return terrno;
×
1848
      }
1849
    }
1850
  }
1851
  return code;
29,586✔
1852
}
1853

1854
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
25,310✔
1855
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1856
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1857
  int32_t code = TSDB_CODE_SUCCESS;
25,310✔
1858
  STableListInfo* pList = tableListCreate();
25,310✔
1859
  if (pList == NULL) {
25,310✔
1860
    code = terrno;
×
1861
    goto end;
×
1862
  }
1863
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
25,310✔
1864
  if (uidListCopy == NULL) {
25,310✔
1865
    code = terrno;
×
1866
    goto end;
×
1867
  }
1868
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
25,310✔
1869
  SReadHandle    pHandle = {.vnode = pVnode};
25,310✔
1870

1871
  pList->idInfo.suid = suid;
25,310✔
1872
  pList->idInfo.tableType = TD_SUPER_TABLE;
25,310✔
1873
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
25,310✔
1874
  if (code != TSDB_CODE_SUCCESS) {
25,310✔
1875
    goto end;
×
1876
  }                                              
1877
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
25,310✔
1878
  if (code != TSDB_CODE_SUCCESS) {
25,310✔
1879
    goto end;
×
1880
  }
1881
  *tableList = pList->pTableList;
25,310✔
1882
  pList->pTableList = NULL;
25,310✔
1883

1884
  taosArrayClear(uidList);
25,310✔
1885
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
50,620✔
1886
    void* tmp = taosArrayGet(uidListCopy, i);
25,310✔
1887
    if (tmp == NULL) {
25,310✔
1888
      continue;
×
1889
    }
1890
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
25,310✔
1891
    if (slot == NULL) {
25,310✔
1892
      if (taosArrayPush(uidList, tmp) == NULL) {
3,110✔
1893
        code = terrno;
×
1894
        goto end;
×
1895
      }
1896
    }
1897
  }
1898
end:
25,310✔
1899
  taosArrayDestroy(uidListCopy);
25,310✔
1900
  tableListDestroy(pList);
25,310✔
1901
  return code;
25,310✔
1902
}
1903

1904
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1905
//   int32_t index = -1;
1906
//   (void)taosThreadRwlockRdlock(lock);
1907
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1908
//     index = 0;
1909
//     goto end;
1910
//   }
1911
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1912
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1913

1914
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1915
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1916
//       index = i;
1917
//       goto end;
1918
//     }
1919
//   }
1920
// end:
1921
//   (void)taosThreadRwlockUnlock(lock);
1922
//   return index;
1923
// }
1924

1925
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
212,506✔
1926
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
212,506✔
1927
  STableListInfo* pList = pTableListInfo;
212,506✔
1928
  return pList->pTableList;
212,506✔
1929
}
1930

1931
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,044,593✔
1932

1933
void streamDestroyExecTask(qTaskInfo_t tInfo) {
1,994,820✔
1934
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
1,994,820✔
1935
  qDestroyTask(tInfo);
1,994,820✔
1936
}
1,994,820✔
1937

1938
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
519,756✔
1939
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
519,756✔
1940
}
1941

1942
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
542,471✔
1943
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1944
  int32_t      code = 0;
542,471✔
1945
  SNode*       pNode = 0;
542,471✔
1946
  SNodeList*   pList = NULL;
542,471✔
1947
  SExprInfo*   pExprInfo = NULL;
542,471✔
1948
  int32_t      numOfExprs = 1;
542,471✔
1949
  int32_t*     offset = 0;
542,471✔
1950
  STargetNode* pTargetNode = NULL;
542,471✔
1951
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
542,471✔
1952
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
542,471✔
1953

1954
  if (code == 0) {
542,471✔
1955
    pTargetNode->dataBlockId = 0;
542,471✔
1956
    pTargetNode->pExpr = pNode;
542,471✔
1957
    pTargetNode->slotId = 0;
542,471✔
1958
  }
1959
  if (code == 0) {
542,471✔
1960
    code = nodesMakeList(&pList);
542,471✔
1961
  }
1962
  if (code == 0) {
542,471✔
1963
    code = nodesListAppend(pList, (SNode*)pTargetNode);
542,471✔
1964
  }
1965
  if (code == 0) {
542,471✔
1966
    pNode = NULL;
542,471✔
1967
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
542,471✔
1968
  }
1969

1970
  if (code == 0) {
542,471✔
1971
    const char* pVal = NULL;
542,471✔
1972
    int32_t     len = 0;
542,471✔
1973
    SNode*      pSclNode = NULL;
542,471✔
1974
    switch (pExprInfo->pExpr->nodeType) {
542,471✔
1975
      case QUERY_NODE_FUNCTION:
542,471✔
1976
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
542,471✔
1977
        break;
542,471✔
1978
      case QUERY_NODE_OPERATOR:
×
1979
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1980
        break;
×
1981
      default:
×
1982
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1983
        break;
×
1984
    }
1985
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
542,471✔
1986
    SSDataBlock block = {0};
542,471✔
1987
    block.info.rows = 1;
542,471✔
1988
    SSDataBlock* pBlock = &block;
542,471✔
1989
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
542,471✔
1990
    if (tmp == NULL) {
542,471✔
1991
      code = terrno;
×
1992
    }
1993
    if (code == 0) {
542,471✔
1994
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
542,471✔
1995
      gTaskScalarExtra.pStreamRange = NULL;
542,471✔
1996
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
542,471✔
1997
    }
1998
    taosArrayDestroy(pBlockList);
542,471✔
1999
  }
2000
  nodesDestroyList(pList);
542,471✔
2001
  destroyExprInfo(pExprInfo, numOfExprs);
542,471✔
2002
  taosMemoryFreeClear(pExprInfo);
542,471✔
2003
  return code;
542,471✔
2004
}
2005

2006
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,287,939✔
2007
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,287,939✔
2008
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,287,939✔
2009
  int32_t        code = 0;
2,287,939✔
2010
  SNode*         pNode = NULL;
2,287,939✔
2011
  if (!pForceOutputCols) return 0;
2,287,939✔
2012
  if (!*pRes) {
22,715✔
2013
    code = createDataBlock(pRes);
22,715✔
2014
  }
2015

2016
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
22,715✔
2017
    int32_t idx = 0;
22,715✔
2018
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
90,684✔
2019
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
67,969✔
2020
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
67,969✔
2021
      colInfo.info.precision = pCol->type.precision;
67,969✔
2022
      colInfo.info.scale = pCol->type.scale;
67,969✔
2023
      code = blockDataAppendColInfo(*pRes, &colInfo);
67,969✔
2024
      if (code != 0) break;
67,969✔
2025
    }
2026
  }
2027

2028
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
22,715✔
2029
  if (code != TSDB_CODE_SUCCESS) {
22,715✔
2030
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2031
    return code;
×
2032
  }
2033

2034
  // loop all exprs for force output, execute all exprs
2035
  int32_t idx = 0;
22,715✔
2036
  int32_t rowIdx = (*pRes)->info.rows;
22,715✔
2037
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
22,715✔
2038
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
22,715✔
2039
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
90,684✔
2040
    SScalarParam   dst = {0};
67,969✔
2041
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
67,969✔
2042
    code = nodesStringToNode(pCol->expr, &pNode);
67,969✔
2043
    if (code != 0) break;
67,969✔
2044
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
67,969✔
2045
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
67,969✔
2046
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
45,254✔
2047
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
45,254✔
2048
    } else {
2049
      dst.columnData = pInfo;
22,715✔
2050
      dst.numOfRows = rowIdx;
22,715✔
2051
      dst.colAlloced = false;
22,715✔
2052
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
22,715✔
2053
    }
2054
    ++idx;
67,969✔
2055
    // TODO sclFreeParam(&dst);
2056
    nodesDestroyNode(pNode);
67,969✔
2057
    if (code != 0) break;
67,969✔
2058
  }
2059
  if (code == TSDB_CODE_SUCCESS) {
22,715✔
2060
    (*pRes)->info.rows++;
22,715✔
2061
  }
2062
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
22,715✔
2063
  return code;
22,715✔
2064
}
2065

2066
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
201,625✔
2067
  int32_t      code = 0;
201,625✔
2068
  const char*  pVal = NULL;
201,625✔
2069
  SScalarParam dst = {0};
201,625✔
2070
  int32_t      len = 0;
201,625✔
2071
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
201,625✔
2072
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
201,625✔
2073
  // execute the expr
2074
  switch (pExpr->type) {
201,625✔
2075
    case QUERY_NODE_VALUE: {
×
2076
      SValueNode* pValue = (SValueNode*)pExpr;
×
2077
      int32_t     type = pValue->node.resType.type;
×
2078
      if (!IS_STR_DATA_TYPE(type)) {
×
2079
        qError("invalid sub tb expr with non-str type");
×
2080
        code = TSDB_CODE_INVALID_PARA;
×
2081
        break;
×
2082
      }
2083
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2084
      if (pTmp == NULL) {
×
2085
        qError("invalid sub tb expr with null value");
×
2086
        code = TSDB_CODE_INVALID_PARA;
×
2087
        break;
×
2088
      }
2089
      pVal = varDataVal(pTmp);
×
2090
      len = varDataLen(pTmp);
×
2091
    } break;
×
2092
    case QUERY_NODE_FUNCTION: {
201,625✔
2093
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
201,625✔
2094
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
201,625✔
2095
        qError("invalid sub tb expr with non-str type func");
×
2096
        code = TSDB_CODE_INVALID_PARA;
×
2097
        break;
×
2098
      }
2099
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
201,625✔
2100
      if (!pCol) {
201,625✔
2101
        code = terrno;
×
2102
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2103
        break;
×
2104
      }
2105

2106
      pCol->hasNull = true;
201,625✔
2107
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
201,625✔
2108
      pCol->info.colId = 0;
201,625✔
2109
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
201,625✔
2110
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
201,625✔
2111
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
201,625✔
2112
      code = colInfoDataEnsureCapacity(pCol, 1, true);
201,625✔
2113
      if (code != 0) {
201,625✔
2114
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2115
        taosMemoryFree(pCol);
×
2116
        break;
×
2117
      }
2118
      dst.columnData = pCol;
201,625✔
2119
      dst.numOfRows = 1;
201,625✔
2120
      dst.colAlloced = true;
201,625✔
2121
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
201,625✔
2122
      if (colDataIsNull_var(dst.columnData, 0)) {
201,625✔
2123
        qInfo("invalid sub tb expr with null value");
2,820✔
2124
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,820✔
2125
      }
2126
      if (code == 0) {
201,625✔
2127
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
198,805✔
2128
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
198,805✔
2129
      }
2130
    } break;
201,625✔
2131
    default:
×
2132
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2133
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2134
      break;
×
2135
  }
2136
  if (code == 0) {
201,625✔
2137
    if (!pVal || len == 0) {
198,805✔
2138
      qError("tbname generated with no characters which is not allowed");
×
2139
      code = TSDB_CODE_INVALID_PARA;
×
2140
    }
2141
    if(len > TSDB_TABLE_NAME_LEN - 1) {
198,805✔
2142
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
406✔
2143
      len = TSDB_TABLE_NAME_LEN - 1;
406✔
2144
    }
2145

2146
    memcpy(tbname, pVal, len);
198,805✔
2147
    tbname[len] = '\0';  // ensure null terminated
198,805✔
2148
    if (NULL != strchr(tbname, '.')) {
198,602✔
2149
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2150
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2151
    }
2152
  }
2153
  // TODO free dst
2154
  sclFreeParam(&dst);
201,422✔
2155
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
201,625✔
2156
  return code;
201,625✔
2157
}
2158

2159
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
191,988✔
2160
  if (pParam) {
191,988✔
2161
    if (pParam->tbname) {
191,988✔
2162
      taosMemFree(pParam->tbname);
191,988✔
2163
      pParam->tbname = NULL;
191,988✔
2164
    }
2165
    if (pParam->stbname) {
191,988✔
2166
      taosMemFree(pParam->stbname);
191,988✔
2167
      pParam->stbname = NULL;
191,988✔
2168
    }
2169
    if (pParam->dbFName) {
191,988✔
2170
      taosMemFree(pParam->dbFName);
191,988✔
2171
      pParam->dbFName = NULL;
191,988✔
2172
    }
2173
    if (pParam->pFields) {
191,988✔
2174
      taosArrayDestroy(pParam->pFields);
191,988✔
2175
      pParam->pFields = NULL;
191,988✔
2176
    }
2177
    if (pParam->pTagFields) {
191,988✔
2178
      taosArrayDestroy(pParam->pTagFields);
122,083✔
2179
      pParam->pTagFields = NULL;
122,083✔
2180
    }
2181
    taosMemFree(pParam);
191,988✔
2182
  }
2183
}
191,988✔
2184

2185
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
191,988✔
2186
  int32_t code = 0, lino = 0;
191,988✔
2187
  if (ppDst == NULL || pSrc == NULL) {
191,988✔
2188
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2189
  }
2190
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
191,988✔
2191
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
191,988✔
2192

2193
  (*ppDst)->suid = pSrc->suid;
191,988✔
2194
  (*ppDst)->sver = pSrc->sver;
191,988✔
2195
  (*ppDst)->tbType = pSrc->tbType;
191,988✔
2196
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
191,988✔
2197
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
191,988✔
2198

2199
  if (pSrc->stbname) {
191,988✔
2200
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
191,988✔
2201
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
191,988✔
2202
  }
2203

2204
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
191,988✔
2205
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
191,781✔
2206

2207
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
191,781✔
2208

2209
  if (pSrc->pFields && pSrc->pFields->size > 0) {
191,781✔
2210
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
191,988✔
2211
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
191,988✔
2212
  } else {
2213
    (*ppDst)->pFields = NULL;
×
2214
  }
2215
  
2216
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
191,988✔
2217
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
122,083✔
2218
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
122,083✔
2219
  } else {
2220
    (*ppDst)->pTagFields = NULL;
69,905✔
2221
  }
2222

2223
_exit:
191,988✔
2224

2225
  if (code != 0) {
191,988✔
2226
    if (*ppDst) {
×
2227
      destroyStreamInserterParam(*ppDst);
×
2228
      *ppDst = NULL;
×
2229
    }
2230
    
2231
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2232
  }
2233
  return code;
191,988✔
2234
}
2235

2236
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
190✔
2237
  return doDropStreamTable(pMsgCb, pOutput, pReq);
190✔
2238
}
2239

2240
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2241
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2242
}
2243

2244
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
4,276✔
2245
  int32_t code = TSDB_CODE_SUCCESS;
4,276✔
2246
  STableListInfo* pList = tableListCreate();
4,276✔
2247
  if (pList == NULL) {
4,276✔
2248
    code = terrno;
×
2249
    goto end;
×
2250
  }
2251
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2252
  // if (uidListCopy == NULL) {
2253
  //   code = terrno;
2254
  //   goto end;
2255
  // }
2256

2257
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,276✔
2258
  pList->idInfo.suid = suid;
4,276✔
2259
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,276✔
2260
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,276✔
2261
  if (code != TSDB_CODE_SUCCESS) {
4,276✔
2262
    goto end;
×
2263
  }                                              
2264
  // taosArrayClear(uidList);
2265
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2266
  //   void* tmp = taosArrayGet(uidListCopy, i);
2267
  //   if (tmp == NULL) {
2268
  //     continue;
2269
  //   }
2270
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2271
  //   if (slot == NULL) {
2272
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2273
  //       code = terrno;
2274
  //       goto end;
2275
  //     }
2276
  //   }
2277
  // }
2278
end:
4,276✔
2279
  // taosArrayDestroy(uidListCopy);
2280
  tableListDestroy(pList);
4,276✔
2281
  return code;
4,276✔
2282
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc