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

taosdata / TDengine / #4948

04 Feb 2026 09:05AM UTC coverage: 66.866% (-0.006%) from 66.872%
#4948

push

travis-ci

web-flow
enh(keeper): password information desensitization processing (#34458)

Close https://project.feishu.cn/taosdata_td/feature/detail/6600039687

Commits:

* feat: add String method to TDengineRestful for safe string representation

* feat: replace hardcoded credentials with test utility functions in tests

* feat: update test workflows to use environment variables for credentials

* test: fix failed test case

* chore: setup tmate

* feat: streamline test execution by setting environment variables for credentials

* feat: enhance test utility functions to return default values if environment variables are not set

205522 of 307364 relevant lines covered (66.87%)

124991068.44 hits per line

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

70.21
/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) {
577,704,475✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
577,704,475✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
577,704,475✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
577,768,042✔
47
}
577,691,674✔
48

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

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

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

70
static void initRefPool() {
542,242✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
542,242✔
72
  (void)atexit(cleanupRefPool);
542,242✔
73
}
542,242✔
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) {
46,823,421✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
46,823,421✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
46,824,133✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
21,435,150✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
21,435,464✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
21,434,772✔
162
      if (pScanInfo->base.dataReader != NULL) {
21,434,465✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
261,264✔
164
        if (code) {
260,979✔
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);
25,389,431✔
172
  }
173

174
  return 0;
24,853,812✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
24,854,010✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
24,854,010✔
179
  pTaskInfo->id.queryId = queryId;
24,854,010✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
24,853,970✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
24,853,600✔
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) {
439,035✔
213
  if (msg == NULL) {  // create raw scan
439,035✔
214
    SExecTaskInfo* pTaskInfo = NULL;
115,725✔
215

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

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

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

232
  SSubplan* pPlan = NULL;
323,310✔
233
  int32_t   code = qStringToSubplan(msg, &pPlan);
323,310✔
234
  if (code != TSDB_CODE_SUCCESS) {
323,659✔
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;
323,659✔
241
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
323,659✔
242
  if (code != TSDB_CODE_SUCCESS) {
323,659✔
243
    qDestroyTask(pTaskInfo);
×
244
    terrno = code;
×
245
    return NULL;
×
246
  }
247

248
  return pTaskInfo;
323,659✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
532,025✔
252
  if (streamInserterParam == NULL) {
532,025✔
253
    return TSDB_CODE_SUCCESS;
299,562✔
254
  }
255

256
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
232,463✔
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) {
232,463✔
262
    stError("insertParam: invalid db/table name");
×
263
    return TSDB_CODE_INVALID_PARA;
×
264
  }
265

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

272
  return TSDB_CODE_SUCCESS;
232,463✔
273
}
274

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

295
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, NULL);
532,025✔
296
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
531,860✔
297
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
50✔
298
    goto _error;
215✔
299
  }
300

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

310
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
232,463✔
311
    if (NULL == pInserterParam) {
232,463✔
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);
232,463✔
317
    TSDB_CHECK_CODE(code, lino, _error);
232,463✔
318
    
319
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
232,463✔
320
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
232,463✔
321

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
232,463✔
323
    if (code) {
232,463✔
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,
531,810✔
328
         tstrerror(code));
329

330
_error:
69,816✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,887,408✔
342
  if (pInfo == NULL) {
2,887,408✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,887,408✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,887,408✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,887,408✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,887,408✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,604,291✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
461,148✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,491,699✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

377
  void*           info = pOperator->info;
2,887,408✔
378
  STableScanBase* pScanBaseInfo = NULL;
2,887,408✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,887,408✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
283,117✔
382
    setReadHandle(handle, pScanBaseInfo);
283,117✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,604,291✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,143,143✔
385
    setReadHandle(handle, pScanBaseInfo);
2,143,143✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
532,025✔
400

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

416
  return code;
531,810✔
417
}
418

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
376,632✔
433

434
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
376,632✔
435
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
376,632✔
436

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
376,632✔
444

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

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

454
  locked = 1;
376,632✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
802,071✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
425,439✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
425,439✔
458

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

465
    tDecoderClear(&mr.coder);
425,439✔
466

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

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

494
  pAPI->metaReaderFn.clearReader(&mr);
376,632✔
495
  locked = 0;
376,632✔
496

497
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
802,071✔
498
    bool     qualified = false;
425,439✔
499
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
425,439✔
500
    if (t == NULL) {
425,439✔
501
      continue;
×
502
    }
503

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

511
      if (!qualified) {
337,775✔
512
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
169,030✔
513
        continue;
169,030✔
514
      }
515
    }
516

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

521
  // handle multiple partition
522

523
_end:
376,632✔
524

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

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

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

543
  if (isAdd) {
377,266✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
376,632✔
545
  }
546

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
377,266✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
377,266✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
377,266✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
377,266✔
558
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
×
559
      int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
×
560

561
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
562
    }
563
  }
564

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

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

588
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
376,632✔
589
    taosWLockLatch(&pTaskInfo->lock);
376,632✔
590

591
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
633,041✔
592
      uint64_t* uid = taosArrayGet(qa, i);
256,409✔
593
      if (!uid) {
256,409✔
594
        taosMemoryFree(keyBuf);
×
595
        taosArrayDestroy(qa);
×
596
        taosWUnLockLatch(&pTaskInfo->lock);
×
597
        return terrno;
×
598
      }
599
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
256,409✔
600

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

616
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
256,409✔
617
      if (code != TSDB_CODE_SUCCESS) {
256,409✔
618
        taosMemoryFree(keyBuf);
×
619
        taosArrayDestroy(qa);
×
620
        taosWUnLockLatch(&pTaskInfo->lock);
×
621
        return code;
×
622
      }
623
    }
624

625
    taosWUnLockLatch(&pTaskInfo->lock);
376,632✔
626
    if (keyBuf != NULL) {
376,632✔
627
      taosMemoryFree(keyBuf);
×
628
    }
629

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

638
  return code;
377,266✔
639
}
640

641
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
451,812,253✔
642
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
643
                                    int32_t idx, bool* tbGet) {
644
  *tbGet = false;
451,812,253✔
645

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
451,820,555✔
647
    return TSDB_CODE_INVALID_PARA;
×
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
451,852,302✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
451,852,302✔
652
    return TSDB_CODE_SUCCESS;
264,936,527✔
653
  }
654

655
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
186,863,163✔
656
  if (!pSchemaInfo) {
186,746,773✔
657
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
658
    return terrno;
×
659
  }
660

661
  *sversion = pSchemaInfo->sw->version;
186,746,773✔
662
  *tversion = pSchemaInfo->tversion;
186,871,507✔
663
  *rversion = pSchemaInfo->rversion;
186,871,845✔
664
  if (pSchemaInfo->dbname) {
186,963,080✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
186,959,923✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
187,046,218✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
186,976,752✔
671
  } else {
672
    tableName[0] = 0;
×
673
  }
674

675
  *tbGet = true;
187,030,407✔
676

677
  return TSDB_CODE_SUCCESS;
186,937,797✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
264,890,535✔
681

682
void qDestroyOperatorParam(SOperatorParam* pParam) {
121,600✔
683
  if (NULL == pParam) {
121,600✔
684
    return;
×
685
  }
686
  freeOperatorParam(pParam, OP_GET_PARAM);
121,600✔
687
}
688

689
/**
690
  @brief Update the operator param for the task.
691
  @note  Unlike setOperatorParam, this function will destroy the new param when
692
         operator type mismatch.
693
*/
694
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
31,801,450✔
695
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
31,801,450✔
696
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
31,801,450✔
697
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
31,801,450✔
698
    qError("%s, %s operator type mismatch, task operator type:%d, "
117,325✔
699
           "new param operator type:%d", GET_TASKID(pTask), __func__,
700
           pTask->pRoot->operatorType,
701
           pNewParam->opType);
702
    qDestroyOperatorParam((SOperatorParam*)pParam);
117,325✔
703
    return;
117,325✔
704
  }
705
  TSWAP(pParam, pTask->pOpParam);
31,690,940✔
706
  ((SExecTaskInfo*)tinfo)->paramSet = false;
31,684,315✔
707
}
708

709
int32_t qExecutorInit(void) {
4,270,400✔
710
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,270,400✔
711
  return TSDB_CODE_SUCCESS;
4,270,400✔
712
}
713

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
2,871,666✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
2,871,666✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
2,871,666✔
717
  if (pTask->pWorkerCb) {
2,871,666✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
2,871,666✔
719
    if (code != TSDB_CODE_SUCCESS) {
2,873,226✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724

725
  code = tsem_wait(pSem);
2,873,226✔
726
  if (code != TSDB_CODE_SUCCESS) {
2,873,226✔
727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
728
    pTask->code = code;
×
729
    return pTask->code;
×
730
  }
731

732
  if (pTask->pWorkerCb) {
2,873,226✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,873,226✔
734
    if (code != TSDB_CODE_SUCCESS) {
2,873,226✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
2,873,226✔
740
}
741

742
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
267,315,943✔
743
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
744
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
745
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
267,315,943✔
746
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
267,315,943✔
747

748
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
267,311,831✔
749
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
750

751
  readHandle->uid = 0;
267,364,069✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
267,399,092✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
267,162,984✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
675,169✔
755
    goto _error;
442,786✔
756
  }
757
    
758
  if (handle) {
266,532,386✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
266,407,990✔
760
    void*           pSinkManager = NULL;
266,473,621✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
266,063,989✔
762
    if (code != TSDB_CODE_SUCCESS) {
266,391,507✔
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
266,391,507✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
266,406,309✔
769
    if (code != TSDB_CODE_SUCCESS) {
266,139,752✔
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
771
      taosMemoryFree(pSinkManager);
×
772
      goto _error;
×
773
    }
774

775
    SDataSinkNode* pSink = NULL;
266,139,752✔
776
    if (readHandle->localExec) {
266,336,016✔
777
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,816✔
778
      if (code != TSDB_CODE_SUCCESS) {
2,816✔
779
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
780
               (*pTask)->id.str);
781
        taosMemoryFree(pSinkManager);
×
782
        goto _error;
×
783
      }
784
    }
785

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
266,182,255✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
266,458,066✔
789
    if (code) {
266,150,208✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
580✔
791
    }
792
  }
793

794
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
266,601,345✔
795
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
796

797
_error:
51,001,521✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
267,334,383✔
800
}
801

802
static void freeBlock(void* param) {
×
803
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
804
  blockDataDestroy(pBlock);
×
805
}
×
806

807
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
313,199,668✔
808
                     bool processOneBlock) {
809
  int32_t        code = TSDB_CODE_SUCCESS;
313,199,668✔
810
  int32_t        lino = 0;
313,199,668✔
811
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
313,199,668✔
812
  int64_t        threadId = taosGetSelfPthreadId();
313,199,668✔
813

814
  if (pLocal) {
313,165,747✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
309,856,738✔
816
  }
817

818
  taosArrayClear(pResList);
313,179,042✔
819

820
  int64_t curOwner = 0;
313,100,333✔
821
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
313,100,333✔
822
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
823
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
824
    return pTaskInfo->code;
×
825
  }
826

827
  if (pTaskInfo->cost.start == 0) {
313,078,727✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
261,223,895✔
829
  }
830

831
  if (isTaskKilled(pTaskInfo)) {
313,247,170✔
832
    atomic_store_64(&pTaskInfo->owner, 0);
520✔
833
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
520✔
834
    return pTaskInfo->code;
520✔
835
  }
836

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
313,072,182✔
839
  if (ret != TSDB_CODE_SUCCESS) {
313,538,765✔
840
    pTaskInfo->code = ret;
581,021✔
841
    (void)cleanUpUdfs();
581,021✔
842

843
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
581,021✔
844
    atomic_store_64(&pTaskInfo->owner, 0);
581,021✔
845

846
    return pTaskInfo->code;
581,021✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
312,957,744✔
850

851
  int32_t      current = 0;
312,966,894✔
852
  SSDataBlock* pRes = NULL;
312,966,894✔
853
  int64_t      st = taosGetTimestampUs();
313,161,527✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
313,161,527✔
856
    pTaskInfo->paramSet = true;
31,683,050✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
31,680,275✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
281,525,393✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
312,647,497✔
863
  code = blockDataCheck(pRes);
312,647,497✔
864
  QUERY_CHECK_CODE(code, lino, _end);
312,664,383✔
865

866
  if (pRes == NULL) {
312,664,383✔
867
    st = taosGetTimestampUs();
60,158,404✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
312,651,635✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
312,676,061✔
872
    rowsThreshold = 4096;
312,113,714✔
873
  }
874

875
  int32_t blockIndex = 0;
312,663,743✔
876
  while (pRes != NULL) {
791,863,070✔
877
    SSDataBlock* p = NULL;
523,352,071✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
523,300,179✔
879
      SSDataBlock* p1 = NULL;
403,347,510✔
880
      code = createOneDataBlock(pRes, true, &p1);
403,351,070✔
881
      QUERY_CHECK_CODE(code, lino, _end);
403,334,301✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
403,334,301✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
403,343,703✔
885
      p = p1;
403,343,703✔
886
    } else if (processOneBlock) {
120,011,830✔
887
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
16,280,462✔
888
      if (tmp) {
16,280,462✔
889
        blockDataDestroy(*tmp);
16,280,462✔
890
        *tmp = NULL;
16,280,462✔
891
      }
892
      SSDataBlock* p1 = NULL;
16,280,462✔
893
      code = createOneDataBlock(pRes, true, &p1);
16,280,462✔
894
      QUERY_CHECK_CODE(code, lino, _end);
16,280,462✔
895

896
      *tmp = p1;
16,280,462✔
897
      p = p1;
16,280,462✔
898
    } else {
899
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
103,731,368✔
900
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
103,731,502✔
901

902
      p = *(SSDataBlock**)tmp;
103,731,502✔
903
      code = copyDataBlock(p, pRes);
103,730,615✔
904
      QUERY_CHECK_CODE(code, lino, _end);
103,732,003✔
905
    }
906

907
    blockIndex += 1;
523,342,225✔
908

909
    current += p->info.rows;
523,342,225✔
910
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
523,348,670✔
911
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
912
    void* tmp = taosArrayPush(pResList, &p);
523,358,972✔
913
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
523,358,972✔
914

915
    if (current >= rowsThreshold || processOneBlock) {
523,358,972✔
916
      break;
917
    }
918

919
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
479,203,640✔
920
    QUERY_CHECK_CODE(code, lino, _end);
479,198,159✔
921
    code = blockDataCheck(pRes);
479,198,159✔
922
    QUERY_CHECK_CODE(code, lino, _end);
479,209,545✔
923
  }
924

925
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
312,666,331✔
926
    pTaskInfo->pSubplan->rowsThreshold -= current;
547,861✔
927
  }
928

929
  *hasMore = (pRes != NULL);
312,675,619✔
930
  uint64_t el = (taosGetTimestampUs() - st);
312,655,229✔
931

932
  pTaskInfo->cost.elapsedTime += el;
312,655,229✔
933
  if (NULL == pRes) {
312,642,443✔
934
    *useconds = pTaskInfo->cost.elapsedTime;
268,486,543✔
935
  }
936

937
_end:
312,677,490✔
938
  (void)cleanUpUdfs();
312,667,752✔
939

940
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
312,698,662✔
941
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
312,699,036✔
942
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
943

944
  atomic_store_64(&pTaskInfo->owner, 0);
312,708,756✔
945
  if (code) {
312,699,318✔
946
    pTaskInfo->code = code;
×
947
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
948
  }
949

950
  return pTaskInfo->code;
312,699,318✔
951
}
952

953
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
954
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
955
  SArray*        pList = pTaskInfo->pResultBlockList;
×
956
  size_t         num = taosArrayGetSize(pList);
×
957
  for (int32_t i = 0; i < num; ++i) {
×
958
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
959
    if (p) {
×
960
      blockDataDestroy(*p);
×
961
    }
962
  }
963

964
  taosArrayClear(pTaskInfo->pResultBlockList);
×
965
}
×
966

967
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
120,323,843✔
968
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
120,323,843✔
969
  int64_t        threadId = taosGetSelfPthreadId();
120,323,843✔
970
  int64_t        curOwner = 0;
120,325,120✔
971

972
  *pRes = NULL;
120,325,120✔
973

974
  // todo extract method
975
  taosRLockLatch(&pTaskInfo->lock);
120,325,120✔
976
  bool isKilled = isTaskKilled(pTaskInfo);
120,326,149✔
977
  if (isKilled) {
120,325,512✔
978
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
979

980
    taosRUnLockLatch(&pTaskInfo->lock);
×
981
    return pTaskInfo->code;
×
982
  }
983

984
  if (pTaskInfo->owner != 0) {
120,325,512✔
985
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
986
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
987

988
    taosRUnLockLatch(&pTaskInfo->lock);
×
989
    return pTaskInfo->code;
×
990
  }
991

992
  pTaskInfo->owner = threadId;
120,324,823✔
993
  taosRUnLockLatch(&pTaskInfo->lock);
120,325,806✔
994

995
  if (pTaskInfo->cost.start == 0) {
120,326,149✔
996
    pTaskInfo->cost.start = taosGetTimestampUs();
231,014✔
997
  }
998

999
  // error occurs, record the error code and return to client
1000
  int32_t ret = setjmp(pTaskInfo->env);
120,325,806✔
1001
  if (ret != TSDB_CODE_SUCCESS) {
120,320,750✔
1002
    pTaskInfo->code = ret;
×
1003
    (void)cleanUpUdfs();
×
1004
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
1005
    atomic_store_64(&pTaskInfo->owner, 0);
×
1006
    return pTaskInfo->code;
×
1007
  }
1008

1009
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
120,320,750✔
1010

1011
  int64_t st = taosGetTimestampUs();
120,323,745✔
1012
  int32_t code = TSDB_CODE_SUCCESS;
120,323,745✔
1013
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
120,323,745✔
1014
    pTaskInfo->paramSet = true;
×
1015
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1016
  } else {
1017
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
120,323,748✔
1018
  }
1019
  if (code) {
120,274,457✔
1020
    pTaskInfo->code = code;
×
1021
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1022
  }
1023

1024
  code = blockDataCheck(*pRes);
120,274,457✔
1025
  if (code) {
120,319,850✔
1026
    pTaskInfo->code = code;
×
1027
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1028
  }
1029

1030
  uint64_t el = (taosGetTimestampUs() - st);
120,295,945✔
1031

1032
  pTaskInfo->cost.elapsedTime += el;
120,295,945✔
1033
  if (NULL == *pRes) {
120,308,416✔
1034
    *useconds = pTaskInfo->cost.elapsedTime;
21,019,286✔
1035
  }
1036

1037
  (void)cleanUpUdfs();
120,309,740✔
1038

1039
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
120,326,149✔
1040
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
120,326,149✔
1041

1042
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
120,326,149✔
1043
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1044

1045
  atomic_store_64(&pTaskInfo->owner, 0);
120,327,178✔
1046
  return pTaskInfo->code;
120,326,149✔
1047
}
1048

1049
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
100,190,477✔
1050
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
100,190,477✔
1051
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
100,188,707✔
1052
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
100,190,073✔
1053

1054
  if (!tmp) {
100,190,011✔
1055
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1056
    return terrno;
×
1057
  }
1058
  return TSDB_CODE_SUCCESS;
100,190,011✔
1059
}
1060

1061
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1062
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1063
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1064

1065
  if (key->refId < pInfo->refId) {
×
1066
    return -1;
×
1067
  } else if (key->refId > pInfo->refId) {
×
1068
    return 1;
×
1069
  }
1070

1071
  return 0;
×
1072
}
1073

1074
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1075
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1076
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1077
  if (idx >= 0) {
×
1078
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1079
  }
1080
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1081
}
×
1082

1083
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
57,751✔
1084
  if (pTaskInfo->subJobCtx.hasSubJobs) {
57,751✔
1085
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
18,200✔
1086
    if (pTaskInfo->subJobCtx.param) {
18,200✔
1087
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
5,200✔
1088
    }
1089
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
18,200✔
1090
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
18,200✔
1091
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
18,200✔
1092
  }
1093
  
1094
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,751✔
1095

1096
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
57,751✔
1097
  for (int32_t i = 0; i < num; ++i) {
60,134✔
1098
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
2,383✔
1099
    if (!pStop) {
2,383✔
1100
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1101
      continue;
×
1102
    }
1103
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
2,383✔
1104
    if (pExchangeInfo) {
2,383✔
1105
      int32_t code = tsem_post(&pExchangeInfo->ready);
2,383✔
1106
      if (code != TSDB_CODE_SUCCESS) {
2,383✔
1107
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1108
      } else {
1109
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
2,383✔
1110
      }
1111
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
2,383✔
1112
      if (code != TSDB_CODE_SUCCESS) {
2,383✔
1113
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1114
      }
1115
    }
1116
  }
1117

1118
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,751✔
1119
}
57,751✔
1120

1121
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
57,751✔
1122
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
57,751✔
1123
  if (pTaskInfo == NULL) {
57,751✔
1124
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1125
  }
1126

1127
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
57,751✔
1128

1129
  setTaskKilled(pTaskInfo, rspCode);
57,751✔
1130
  qStopTaskOperators(pTaskInfo);
57,751✔
1131

1132
  return TSDB_CODE_SUCCESS;
57,751✔
1133
}
1134

1135
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1136
  int64_t        st = taosGetTimestampMs();
×
1137
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1138
  if (pTaskInfo == NULL) {
×
1139
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1140
  }
1141

1142
  if (waitDuration > 0) {
×
1143
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1144
  } else {
1145
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1146
  }
1147

1148
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1149

1150
  if (waitDuration > 0) {
×
1151
    while (1) {
1152
      taosWLockLatch(&pTaskInfo->lock);
×
1153
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1154
        taosWUnLockLatch(&pTaskInfo->lock);
×
1155

1156
        taosMsleep(200);
×
1157

1158
        int64_t d = taosGetTimestampMs() - st;
×
1159
        if (d >= waitDuration && waitDuration >= 0) {
×
1160
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1161
          return TSDB_CODE_SUCCESS;
×
1162
        }
1163
      } else {  // not running now
1164
        pTaskInfo->code = rspCode;
×
1165
        taosWUnLockLatch(&pTaskInfo->lock);
×
1166
        return TSDB_CODE_SUCCESS;
×
1167
      }
1168
    }
1169
  }
1170

1171
  int64_t et = taosGetTimestampMs() - st;
×
1172
  if (et < waitDuration) {
×
1173
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1174
    return TSDB_CODE_SUCCESS;
×
1175
  }
1176
  return TSDB_CODE_SUCCESS;
×
1177
}
1178

1179
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1181
  if (NULL == pTaskInfo) {
×
1182
    return false;
×
1183
  }
1184

1185
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1186
}
1187

1188
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
267,620,133✔
1189
  STaskCostInfo* pSummary = &pTaskInfo->cost;
267,620,133✔
1190
  int64_t        idleTime = pSummary->start - pSummary->created;
267,622,037✔
1191

1192
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
267,617,810✔
1193
  if (pSummary->pRecoder != NULL) {
267,598,333✔
1194
    qDebug(
185,311,540✔
1195
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1196
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1197
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1198
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1199
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1200
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1201
  } else {
1202
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
82,274,304✔
1203
           pSummary->elapsedTime / 1000.0);
1204
  }
1205
}
267,586,337✔
1206

1207

1208
void qDestroyTask(qTaskInfo_t qTaskHandle) {
274,013,583✔
1209
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
274,013,583✔
1210
  if (pTaskInfo == NULL) {
274,013,583✔
1211
    return;
6,404,110✔
1212
  }
1213

1214
  if (pTaskInfo->pRoot != NULL) {
267,609,473✔
1215
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
267,612,593✔
1216
  } else {
1217
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1218
  }
1219

1220
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
267,617,150✔
1221
  doDestroyTask(pTaskInfo);
267,618,600✔
1222
}
1223

1224
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,635,792✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,635,792✔
1226
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,635,792✔
1227
}
1228

1229
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
323,659✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
323,659✔
1231
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
323,659✔
1232

1233
  while (1) {
317,737✔
1234
    uint16_t type = pOperator->operatorType;
641,396✔
1235
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
641,396✔
1236
      *scanner = pOperator->info;
323,659✔
1237
      break;
323,659✔
1238
    } else {
1239
      pOperator = pOperator->pDownstream[0];
317,737✔
1240
    }
1241
  }
1242
}
323,659✔
1243

1244
void* qExtractReaderFromTmqScanner(void* scanner) {
323,659✔
1245
  SStreamScanInfo* pInfo = scanner;
323,659✔
1246
  return (void*)pInfo->tqReader;
323,659✔
1247
}
1248

1249
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
723,791✔
1250
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
723,791✔
1251
  return pTaskInfo->streamInfo.schema;
723,791✔
1252
}
1253

1254
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
724,085✔
1255
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
724,085✔
1256
  return pTaskInfo->streamInfo.tbName;
724,085✔
1257
}
1258

1259
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
802,071✔
1260
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
802,071✔
1261
  return &pTaskInfo->streamInfo.btMetaRsp;
802,071✔
1262
}
1263

1264
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
22,072,862✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,072,862✔
1266
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
22,072,862✔
1267
  return 0;
22,072,862✔
1268
  /*if (code != TSDB_CODE_SUCCESS) {
1269
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1270
    pTaskInfo->code = code;
1271
    T_LONG_JMP(pTaskInfo->env, code);
1272
  }*/
1273
}
1274

1275
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
761,319✔
1276
  memset(pCond, 0, sizeof(SQueryTableDataCond));
761,319✔
1277
  pCond->order = TSDB_ORDER_ASC;
761,319✔
1278
  pCond->numOfCols = pMtInfo->schema->nCols;
765,729✔
1279
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
765,729✔
1280
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
763,377✔
1281
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
763,965✔
1282
    taosMemoryFreeClear(pCond->colList);
3,234✔
1283
    taosMemoryFreeClear(pCond->pSlotList);
×
1284
    return terrno;
×
1285
  }
1286

1287
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
761,907✔
1288
  pCond->suid = pMtInfo->suid;
763,377✔
1289
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
764,553✔
1290
  pCond->startVersion = -1;
764,553✔
1291
  pCond->endVersion = sContext->snapVersion;
763,377✔
1292

1293
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,877,264✔
1294
    SColumnInfo* pColInfo = &pCond->colList[i];
4,111,535✔
1295
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,110,947✔
1296
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,112,711✔
1297
    if (pMtInfo->pExtSchemas != NULL) {
4,110,653✔
1298
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,600✔
1299
    }
1300
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,112,417✔
1301
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,113,005✔
1302

1303
    pCond->pSlotList[i] = i;
4,110,653✔
1304
  }
1305

1306
  return TSDB_CODE_SUCCESS;
766,317✔
1307
}
1308

1309
void qStreamSetOpen(qTaskInfo_t tinfo) {
118,803,928✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
118,803,928✔
1311
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
118,803,928✔
1312
  pOperator->status = OP_NOT_OPENED;
118,822,667✔
1313
}
118,822,476✔
1314

1315
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
21,250,621✔
1316
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,250,621✔
1317
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
21,250,621✔
1318
}
21,252,584✔
1319

1320
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
22,250,998✔
1321
  int32_t        code = TSDB_CODE_SUCCESS;
22,250,998✔
1322
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,250,998✔
1323
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
22,250,998✔
1324

1325
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,251,994✔
1326
  const char*    id = GET_TASKID(pTaskInfo);
22,251,317✔
1327

1328
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,250,135✔
1329
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
20,955,436✔
1330
    if (pOperator == NULL || code != 0) {
20,954,179✔
1331
      return code;
×
1332
    }
1333

1334
    SStreamScanInfo* pInfo = pOperator->info;
20,955,165✔
1335
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
20,954,506✔
1336
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
20,954,607✔
1337
    walReaderVerifyOffset(pWalReader, pOffset);
20,955,770✔
1338
  }
1339
  // if pOffset equal to current offset, means continue consume
1340
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,248,917✔
1341
    return 0;
20,338,266✔
1342
  }
1343

1344
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,911,808✔
1345
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,129,356✔
1346
    if (pOperator == NULL || code != 0) {
1,129,672✔
1347
      return code;
×
1348
    }
1349

1350
    SStreamScanInfo* pInfo = pOperator->info;
1,129,672✔
1351
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,129,672✔
1352
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,129,361✔
1353
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,129,672✔
1354

1355
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,128,724✔
1356
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
912,708✔
1357
      pScanBaseInfo->dataReader = NULL;
912,443✔
1358

1359
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
912,708✔
1360
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
912,708✔
1361
      walReaderVerifyOffset(pWalReader, pOffset);
912,443✔
1362
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
912,443✔
1363
      if (code < 0) {
912,708✔
1364
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
12,190✔
1365
        return code;
12,190✔
1366
      }
1367
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
215,389✔
1368
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1369
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1370
      int64_t uid = pOffset->uid;
216,349✔
1371
      int64_t ts = pOffset->ts;
215,722✔
1372
      int32_t index = 0;
215,700✔
1373

1374
      // this value may be changed if new tables are created
1375
      taosRLockLatch(&pTaskInfo->lock);
215,700✔
1376
      int32_t numOfTables = 0;
216,653✔
1377
      code = tableListGetSize(pTableListInfo, &numOfTables);
216,653✔
1378
      if (code != TSDB_CODE_SUCCESS) {
215,073✔
1379
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1380
        taosRUnLockLatch(&pTaskInfo->lock);
×
1381
        return code;
×
1382
      }
1383

1384
      if (uid == 0) {
215,073✔
1385
        if (numOfTables != 0) {
209,028✔
1386
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,659✔
1387
          if (!tmp) {
38,970✔
1388
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1389
            taosRUnLockLatch(&pTaskInfo->lock);
×
1390
            return terrno;
×
1391
          }
1392
          if (tmp) uid = tmp->uid;
38,970✔
1393
          ts = INT64_MIN;
38,970✔
1394
          pScanInfo->currentTable = 0;
38,970✔
1395
        } else {
1396
          taosRUnLockLatch(&pTaskInfo->lock);
170,369✔
1397
          qError("no table in table list, %s", id);
170,690✔
1398
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
170,690✔
1399
        }
1400
      }
1401
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
44,388✔
1402

1403
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
46,274✔
1404
             pInfo->pTableScanOp->resultInfo.totalRows);
1405
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
46,274✔
1406

1407
      // start from current accessed position
1408
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1409
      // position, let's find it from the beginning.
1410
      index = tableListFind(pTableListInfo, uid, 0);
46,274✔
1411
      taosRUnLockLatch(&pTaskInfo->lock);
46,274✔
1412

1413
      if (index >= 0) {
46,274✔
1414
        pScanInfo->currentTable = index;
46,274✔
1415
      } else {
1416
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1417
               numOfTables, pScanInfo->currentTable, id);
1418
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1419
      }
1420

1421
      STableKeyInfo keyInfo = {.uid = uid};
46,274✔
1422
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
46,274✔
1423

1424
      // let's start from the next ts that returned to consumer.
1425
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
46,274✔
1426
        pScanBaseInfo->cond.twindows.skey = ts;
899✔
1427
      } else {
1428
        pScanBaseInfo->cond.twindows.skey = ts + 1;
45,375✔
1429
      }
1430
      pScanInfo->scanTimes = 0;
46,274✔
1431

1432
      if (pScanBaseInfo->dataReader == NULL) {
46,274✔
1433
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
82,008✔
1434
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1435
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
41,004✔
1436
        if (code != TSDB_CODE_SUCCESS) {
41,004✔
1437
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1438
          return code;
×
1439
        }
1440

1441
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
41,004✔
1442
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1443
      } else {
1444
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
5,270✔
1445
        if (code != TSDB_CODE_SUCCESS) {
5,270✔
1446
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1447
          return code;
×
1448
        }
1449

1450
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
5,270✔
1451
        if (code != TSDB_CODE_SUCCESS) {
5,270✔
1452
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1453
          return code;
×
1454
        }
1455
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
5,270✔
1456
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1457
      }
1458

1459
      // restore the key value
1460
      pScanBaseInfo->cond.twindows.skey = oldSkey;
46,274✔
1461
    } else {
1462
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1463
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1464
    }
1465

1466
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1467
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
782,452✔
1468
      SStreamRawScanInfo* pInfo = pOperator->info;
767,152✔
1469
      SSnapContext*       sContext = pInfo->sContext;
767,152✔
1470
      SOperatorInfo*      p = NULL;
767,152✔
1471

1472
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
767,152✔
1473
      if (code != 0) {
767,152✔
1474
        return code;
×
1475
      }
1476

1477
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
767,152✔
1478

1479
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
766,858✔
1480
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1481
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1482
      }
1483

1484
      SMetaTableInfo mtInfo = {0};
766,858✔
1485
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
766,858✔
1486
      if (code != 0) {
766,858✔
1487
        destroyMetaTableInfo(&mtInfo);
1488
        return code;
×
1489
      }
1490
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
766,858✔
1491
      pInfo->dataReader = NULL;
765,388✔
1492

1493
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
764,800✔
1494
      tableListClear(pTableListInfo);
764,492✔
1495

1496
      if (mtInfo.uid == 0) {
766,564✔
1497
        destroyMetaTableInfo(&mtInfo);
1498
        goto end;  // no data
835✔
1499
      }
1500

1501
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
765,729✔
1502
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
762,201✔
1503
      if (code != TSDB_CODE_SUCCESS) {
762,789✔
1504
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1505
        destroyMetaTableInfo(&mtInfo);
1506
        return code;
×
1507
      }
1508
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
762,789✔
1509
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,287✔
1510
      } else {
1511
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
762,090✔
1512
      }
1513

1514
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
765,435✔
1515
      if (code != TSDB_CODE_SUCCESS) {
766,317✔
1516
        destroyMetaTableInfo(&mtInfo);
1517
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1518
        return code;
×
1519
      }
1520

1521
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
766,317✔
1522
      if (!pList) {
766,317✔
1523
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1524
        destroyMetaTableInfo(&mtInfo);
1525
        return code;
×
1526
      }
1527
      int32_t size = 0;
766,317✔
1528
      code = tableListGetSize(pTableListInfo, &size);
766,317✔
1529
      if (code != TSDB_CODE_SUCCESS) {
766,317✔
1530
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1531
        destroyMetaTableInfo(&mtInfo);
1532
        return code;
×
1533
      }
1534

1535
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,532,634✔
1536
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
766,317✔
1537
      if (code != TSDB_CODE_SUCCESS) {
762,201✔
1538
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1539
        destroyMetaTableInfo(&mtInfo);
1540
        return code;
×
1541
      }
1542

1543
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
762,201✔
1544
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
763,671✔
1545
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1546
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
762,201✔
1547
      pTaskInfo->streamInfo.schema = mtInfo.schema;
763,671✔
1548
      taosMemoryFreeClear(mtInfo.pExtSchemas);
764,553✔
1549

1550
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
764,553✔
1551
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
15,300✔
1552
      SStreamRawScanInfo* pInfo = pOperator->info;
4,765✔
1553
      SSnapContext*       sContext = pInfo->sContext;
4,765✔
1554
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,765✔
1555
      if (code != 0) {
4,765✔
1556
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1557
        return code;
×
1558
      }
1559
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,765✔
1560
             id);
1561
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
10,535✔
1562
      SStreamRawScanInfo* pInfo = pOperator->info;
10,535✔
1563
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
10,535✔
1564
      pInfo->dataReader = NULL;
10,535✔
1565
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
10,535✔
1566
    }
1567
  }
1568

1569
end:
1,729,138✔
1570
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,729,244✔
1571
  return 0;
1,729,244✔
1572
}
1573

1574
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
215,775,427✔
1575
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
215,775,427✔
1576
  if (pMsg->info.ahandle == NULL) {
215,810,477✔
1577
    rpcFreeCont(pMsg->pCont);
475✔
1578
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
475✔
1579
    return;
475✔
1580
  }
1581

1582
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
215,718,844✔
1583
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1584

1585
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
215,736,352✔
1586

1587
  if (pMsg->contLen > 0) {
215,776,505✔
1588
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
215,769,848✔
1589
    if (buf.pData == NULL) {
215,719,881✔
1590
      pMsg->code = terrno;
×
1591
    } else {
1592
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
215,719,881✔
1593
    }
1594
  }
1595

1596
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
215,782,377✔
1597
  rpcFreeCont(pMsg->pCont);
215,839,846✔
1598
  destroySendMsgInfo(pSendInfo);
215,781,334✔
1599
}
1600

1601
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1602
  int32_t        code = TSDB_CODE_SUCCESS;
×
1603
  int32_t        lino = 0;
×
1604
  SExecTaskInfo* pTaskInfo = tinfo;
×
1605
  SArray*        plist = NULL;
×
1606

1607
  code = getTableListInfo(pTaskInfo, &plist);
×
1608
  if (code || plist == NULL) {
×
1609
    return NULL;
×
1610
  }
1611

1612
  // only extract table in the first elements
1613
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1614

1615
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1616
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1617

1618
  int32_t numOfTables = 0;
×
1619
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1620
  QUERY_CHECK_CODE(code, lino, _end);
×
1621

1622
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1623
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1624
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1625
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1626
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1627
  }
1628

1629
  taosArrayDestroy(plist);
×
1630

1631
_end:
×
1632
  if (code != TSDB_CODE_SUCCESS) {
×
1633
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1634
    T_LONG_JMP(pTaskInfo->env, code);
×
1635
  }
1636
  return pUidList;
×
1637
}
1638

1639
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,357,509✔
1640
  int32_t        code = TSDB_CODE_SUCCESS;
3,357,509✔
1641
  int32_t        lino = 0;
3,357,509✔
1642
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,357,509✔
1643

1644
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,358,046✔
1645
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1646
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1647

1648
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1649
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1650
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,357,504✔
1651
    STableScanInfo* pScanInfo = pOperator->info;
1,678,481✔
1652

1653
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,678,481✔
1654
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,677,944✔
1655
  } else {
1656
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,678,486✔
1657
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,677,939✔
1658
    }
1659
  }
1660

1661
_end:
×
1662
  if (code != TSDB_CODE_SUCCESS) {
3,355,883✔
1663
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1664
  }
1665
  return code;
3,355,883✔
1666
}
1667

1668
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,679,023✔
1669
  if (pList == NULL) {
1,679,023✔
1670
    return TSDB_CODE_INVALID_PARA;
×
1671
  }
1672

1673
  *pList = NULL;
1,679,023✔
1674
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,679,023✔
1675
  if (pArray == NULL) {
1,678,486✔
1676
    return terrno;
×
1677
  }
1678

1679
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,678,486✔
1680
  if (code == 0) {
1,677,939✔
1681
    *pList = pArray;
1,677,939✔
1682
  } else {
1683
    taosArrayDestroy(pArray);
×
1684
  }
1685
  return code;
1,677,939✔
1686
}
1687

1688
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1689
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1690
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1691
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1692
  }
1693
  return 0;
×
1694
}
1695

1696
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1697
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1698
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1699
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1700
  }
1701
  return 0;
×
1702
}
1703

1704
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1705
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1706

1707
  int32_t code = pTaskInfo->code;
×
1708
  pTaskInfo->code = 0;
×
1709
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1710
}
×
1711

1712
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1713
  int32_t code = 0;
×
1714
  return code;
×
1715
}
1716

1717
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1718
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1719
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1720
  return code;
×
1721
}
1722

1723
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
23,205,210✔
1724
  int32_t code = 0;
23,205,210✔
1725

1726
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
23,205,210✔
1727
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
23,204,788✔
1728

1729
  if (pOper->fpSet.resetStateFn) {
23,204,362✔
1730
    code = pOper->fpSet.resetStateFn(pOper);
23,204,151✔
1731
  }
1732
  pOper->status = OP_NOT_OPENED;
23,203,721✔
1733
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
39,466,212✔
1734
    code = clearStatesForOperator(pOper->pDownstream[i]);
16,259,317✔
1735
  }
1736
  return code;
23,206,652✔
1737
}
1738

1739
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
6,945,170✔
1740
  int32_t        code = 0;
6,945,170✔
1741
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
6,945,170✔
1742
  SOperatorInfo* pOper = pTaskInfo->pRoot;
6,945,170✔
1743
  pTaskInfo->code = TSDB_CODE_SUCCESS;
6,945,170✔
1744
  code = clearStatesForOperator(pOper);
6,944,959✔
1745
  return code;
6,944,717✔
1746
}
1747

1748
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
9,783,873✔
1749
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
9,783,873✔
1750
  int64_t        threadId = taosGetSelfPthreadId();
9,783,873✔
1751
  int64_t        curOwner = 0;
9,783,731✔
1752

1753
  *ppRes = NULL;
9,783,731✔
1754

1755
  // todo extract method
1756
  taosRLockLatch(&pTaskInfo->lock);
9,783,731✔
1757
  bool isKilled = isTaskKilled(pTaskInfo);
9,784,326✔
1758
  if (isKilled) {
9,784,326✔
1759
    // clearStreamBlock(pTaskInfo->pRoot);
1760
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1761

1762
    taosRUnLockLatch(&pTaskInfo->lock);
×
1763
    return pTaskInfo->code;
×
1764
  }
1765

1766
  if (pTaskInfo->owner != 0) {
9,784,326✔
1767
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1768
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1769

1770
    taosRUnLockLatch(&pTaskInfo->lock);
×
1771
    return pTaskInfo->code;
×
1772
  }
1773

1774
  pTaskInfo->owner = threadId;
9,783,516✔
1775
  taosRUnLockLatch(&pTaskInfo->lock);
9,784,111✔
1776

1777
  if (pTaskInfo->cost.start == 0) {
9,784,111✔
1778
    pTaskInfo->cost.start = taosGetTimestampUs();
238,958✔
1779
  }
1780

1781
  // error occurs, record the error code and return to client
1782
  int32_t ret = setjmp(pTaskInfo->env);
9,784,079✔
1783
  if (ret != TSDB_CODE_SUCCESS) {
11,059,578✔
1784
    pTaskInfo->code = ret;
1,276,562✔
1785
    (void)cleanUpUdfs();
1,276,562✔
1786
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,276,562✔
1787
    atomic_store_64(&pTaskInfo->owner, 0);
1,276,562✔
1788
    return pTaskInfo->code;
1,276,562✔
1789
  }
1790

1791
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,783,016✔
1792

1793
  int64_t st = taosGetTimestampUs();
9,783,896✔
1794

1795
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
9,783,896✔
1796
  if (code) {
8,507,764✔
1797
    pTaskInfo->code = code;
×
1798
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1799
  } else {
1800
    *finished = *ppRes == NULL;
8,507,764✔
1801
    code = blockDataCheck(*ppRes);
8,507,764✔
1802
  }
1803
  if (code) {
8,507,515✔
1804
    pTaskInfo->code = code;
×
1805
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1806
  }
1807

1808
  uint64_t el = (taosGetTimestampUs() - st);
8,507,764✔
1809

1810
  pTaskInfo->cost.elapsedTime += el;
8,507,764✔
1811
  if (NULL == *ppRes) {
8,507,764✔
1812
    *useconds = pTaskInfo->cost.elapsedTime;
5,267,767✔
1813
  }
1814

1815
  (void)cleanUpUdfs();
8,507,764✔
1816

1817
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,507,764✔
1818
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,507,764✔
1819

1820
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,507,764✔
1821
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1822

1823
  atomic_store_64(&pTaskInfo->owner, 0);
8,507,764✔
1824
  return pTaskInfo->code;
8,507,764✔
1825
}
1826

1827
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1828
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1829
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1830
// }
1831

1832
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
250,182✔
1833
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1834
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1835
  int32_t code = 0;                                        
250,182✔
1836
  if (*pTableListInfo != NULL) {
250,182✔
1837
    qDebug("table list already exists, no need to create again");
×
1838
    goto end;
×
1839
  }
1840
  STableListInfo* pList = tableListCreate();
250,586✔
1841
  if (pList == NULL) {
251,305✔
1842
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1843
    code = terrno;
×
1844
    goto end;
×
1845
  }
1846

1847
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
251,305✔
1848
  SReadHandle    pHandle = {.vnode = pVnode};
251,305✔
1849
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
251,305✔
1850

1851
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
251,305✔
1852
  if (code != 0) {
251,305✔
1853
    tableListDestroy(pList);
×
1854
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1855
    goto end;
×
1856
  }
1857
  *pTableListInfo = pList;
251,305✔
1858

1859
end:
251,305✔
1860
  return 0;
251,305✔
1861
}
1862

1863
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
38,243✔
1864
  bool   listAdded = false;
38,243✔
1865
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
38,243✔
1866
  if (code == 0 && !listAdded) {
38,243✔
1867
    int32_t numOfTables = taosArrayGetSize(pUidList);
31,917✔
1868
    for (int i = 0; i < numOfTables; i++) {
93,894✔
1869
      void* tmp = taosArrayGet(pUidList, i);
61,977✔
1870
      if (tmp == NULL) {
61,977✔
1871
        return terrno;
×
1872
      }
1873
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
61,977✔
1874

1875
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
61,977✔
1876
      if (p == NULL) {
61,977✔
1877
        return terrno;
×
1878
      }
1879
    }
1880
  }
1881
  return code;
38,243✔
1882
}
1883

1884
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
30,093✔
1885
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1886
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1887
  int32_t code = TSDB_CODE_SUCCESS;
30,093✔
1888
  STableListInfo* pList = tableListCreate();
30,093✔
1889
  if (pList == NULL) {
30,093✔
1890
    code = terrno;
×
1891
    goto end;
×
1892
  }
1893
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
30,093✔
1894
  if (uidListCopy == NULL) {
30,093✔
1895
    code = terrno;
×
1896
    goto end;
×
1897
  }
1898
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
30,093✔
1899
  SReadHandle    pHandle = {.vnode = pVnode};
30,093✔
1900

1901
  pList->idInfo.suid = suid;
29,874✔
1902
  pList->idInfo.tableType = TD_SUPER_TABLE;
30,093✔
1903
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
30,093✔
1904
  if (code != TSDB_CODE_SUCCESS) {
30,093✔
1905
    goto end;
×
1906
  }                                              
1907
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
30,093✔
1908
  if (code != TSDB_CODE_SUCCESS) {
30,093✔
1909
    goto end;
×
1910
  }
1911
  *tableList = pList->pTableList;
30,093✔
1912
  pList->pTableList = NULL;
30,093✔
1913

1914
  taosArrayClear(uidList);
30,093✔
1915
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
60,186✔
1916
    void* tmp = taosArrayGet(uidListCopy, i);
30,093✔
1917
    if (tmp == NULL) {
30,093✔
1918
      continue;
×
1919
    }
1920
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
30,093✔
1921
    if (slot == NULL) {
30,093✔
1922
      if (taosArrayPush(uidList, tmp) == NULL) {
3,345✔
1923
        code = terrno;
×
1924
        goto end;
×
1925
      }
1926
    }
1927
  }
1928
end:
30,093✔
1929
  taosArrayDestroy(uidListCopy);
30,093✔
1930
  tableListDestroy(pList);
30,093✔
1931
  return code;
30,093✔
1932
}
1933

1934
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1935
//   int32_t index = -1;
1936
//   (void)taosThreadRwlockRdlock(lock);
1937
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1938
//     index = 0;
1939
//     goto end;
1940
//   }
1941
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1942
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1943

1944
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1945
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1946
//       index = i;
1947
//       goto end;
1948
//     }
1949
//   }
1950
// end:
1951
//   (void)taosThreadRwlockUnlock(lock);
1952
//   return index;
1953
// }
1954

1955
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
251,305✔
1956
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
251,112✔
1957
  STableListInfo* pList = pTableListInfo;
251,112✔
1958
  return pList->pTableList;
251,112✔
1959
}
1960

1961
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,290,781✔
1962

1963
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,524,855✔
1964
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,524,855✔
1965
  qDestroyTask(tInfo);
2,524,855✔
1966
}
2,524,855✔
1967

1968
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
597,310✔
1969
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
597,310✔
1970
}
1971

1972
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
642,800✔
1973
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1974
  int32_t      code = 0;
642,800✔
1975
  SNode*       pNode = 0;
642,800✔
1976
  SNodeList*   pList = NULL;
642,800✔
1977
  SExprInfo*   pExprInfo = NULL;
642,800✔
1978
  int32_t      numOfExprs = 1;
642,800✔
1979
  int32_t*     offset = 0;
642,585✔
1980
  STargetNode* pTargetNode = NULL;
642,585✔
1981
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
642,585✔
1982
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
642,585✔
1983

1984
  if (code == 0) {
642,800✔
1985
    pTargetNode->dataBlockId = 0;
642,800✔
1986
    pTargetNode->pExpr = pNode;
642,800✔
1987
    pTargetNode->slotId = 0;
642,800✔
1988
  }
1989
  if (code == 0) {
642,800✔
1990
    code = nodesMakeList(&pList);
642,800✔
1991
  }
1992
  if (code == 0) {
642,800✔
1993
    code = nodesListAppend(pList, (SNode*)pTargetNode);
642,800✔
1994
  }
1995
  if (code == 0) {
642,800✔
1996
    pNode = NULL;
642,800✔
1997
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
642,800✔
1998
  }
1999

2000
  if (code == 0) {
642,617✔
2001
    const char* pVal = NULL;
642,800✔
2002
    int32_t     len = 0;
642,800✔
2003
    SNode*      pSclNode = NULL;
642,800✔
2004
    switch (pExprInfo->pExpr->nodeType) {
642,800✔
2005
      case QUERY_NODE_FUNCTION:
642,617✔
2006
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
642,617✔
2007
        break;
642,617✔
2008
      case QUERY_NODE_OPERATOR:
×
2009
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
2010
        break;
×
2011
      default:
×
2012
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2013
        break;
×
2014
    }
2015
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
642,617✔
2016
    SSDataBlock block = {0};
642,617✔
2017
    block.info.rows = 1;
642,617✔
2018
    SSDataBlock* pBlock = &block;
642,617✔
2019
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
642,800✔
2020
    if (tmp == NULL) {
642,800✔
2021
      code = terrno;
×
2022
    }
2023
    if (code == 0) {
642,800✔
2024
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
642,617✔
2025
      gTaskScalarExtra.pStreamRange = NULL;
642,617✔
2026
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
642,617✔
2027
    }
2028
    taosArrayDestroy(pBlockList);
642,818✔
2029
  }
2030
  nodesDestroyList(pList);
642,452✔
2031
  destroyExprInfo(pExprInfo, numOfExprs);
642,800✔
2032
  taosMemoryFreeClear(pExprInfo);
642,800✔
2033
  return code;
642,800✔
2034
}
2035

2036
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,030,997✔
2037
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,030,997✔
2038
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,030,997✔
2039
  int32_t        code = 0;
2,030,997✔
2040
  SNode*         pNode = NULL;
2,030,997✔
2041
  if (!pForceOutputCols) return 0;
2,030,997✔
2042
  if (!*pRes) {
45,490✔
2043
    code = createDataBlock(pRes);
45,490✔
2044
  }
2045

2046
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
45,490✔
2047
    int32_t idx = 0;
45,490✔
2048
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
181,767✔
2049
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
136,277✔
2050
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
136,277✔
2051
      colInfo.info.precision = pCol->type.precision;
136,277✔
2052
      colInfo.info.scale = pCol->type.scale;
136,277✔
2053
      code = blockDataAppendColInfo(*pRes, &colInfo);
136,277✔
2054
      if (code != 0) break;
136,277✔
2055
    }
2056
  }
2057

2058
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
45,490✔
2059
  if (code != TSDB_CODE_SUCCESS) {
45,490✔
2060
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2061
    return code;
×
2062
  }
2063

2064
  // loop all exprs for force output, execute all exprs
2065
  int32_t idx = 0;
45,490✔
2066
  int32_t rowIdx = (*pRes)->info.rows;
45,490✔
2067
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
45,490✔
2068
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
45,490✔
2069
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
181,767✔
2070
    SScalarParam   dst = {0};
136,277✔
2071
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
136,277✔
2072
    code = nodesStringToNode(pCol->expr, &pNode);
136,277✔
2073
    if (code != 0) break;
136,277✔
2074
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
136,277✔
2075
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
136,277✔
2076
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
90,787✔
2077
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
90,787✔
2078
    } else {
2079
      dst.columnData = pInfo;
45,490✔
2080
      dst.numOfRows = rowIdx;
45,490✔
2081
      dst.colAlloced = false;
45,490✔
2082
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
45,490✔
2083
    }
2084
    ++idx;
136,277✔
2085
    // TODO sclFreeParam(&dst);
2086
    nodesDestroyNode(pNode);
136,277✔
2087
    if (code != 0) break;
136,277✔
2088
  }
2089
  if (code == TSDB_CODE_SUCCESS) {
45,490✔
2090
    (*pRes)->info.rows++;
45,490✔
2091
  }
2092
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
45,490✔
2093
  return code;
45,490✔
2094
}
2095

2096
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
232,028✔
2097
  int32_t      code = 0;
232,028✔
2098
  const char*  pVal = NULL;
232,028✔
2099
  SScalarParam dst = {0};
232,028✔
2100
  int32_t      len = 0;
232,028✔
2101
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
232,028✔
2102
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
232,028✔
2103
  // execute the expr
2104
  switch (pExpr->type) {
232,028✔
2105
    case QUERY_NODE_VALUE: {
×
2106
      SValueNode* pValue = (SValueNode*)pExpr;
×
2107
      int32_t     type = pValue->node.resType.type;
×
2108
      if (!IS_STR_DATA_TYPE(type)) {
×
2109
        qError("invalid sub tb expr with non-str type");
×
2110
        code = TSDB_CODE_INVALID_PARA;
×
2111
        break;
×
2112
      }
2113
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2114
      if (pTmp == NULL) {
×
2115
        qError("invalid sub tb expr with null value");
×
2116
        code = TSDB_CODE_INVALID_PARA;
×
2117
        break;
×
2118
      }
2119
      pVal = varDataVal(pTmp);
×
2120
      len = varDataLen(pTmp);
×
2121
    } break;
×
2122
    case QUERY_NODE_FUNCTION: {
232,211✔
2123
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
232,211✔
2124
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
232,211✔
2125
        qError("invalid sub tb expr with non-str type func");
×
2126
        code = TSDB_CODE_INVALID_PARA;
×
2127
        break;
×
2128
      }
2129
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
232,211✔
2130
      if (!pCol) {
232,028✔
2131
        code = terrno;
×
2132
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2133
        break;
×
2134
      }
2135

2136
      pCol->hasNull = true;
232,028✔
2137
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
232,211✔
2138
      pCol->info.colId = 0;
232,211✔
2139
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
232,211✔
2140
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
232,211✔
2141
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
232,211✔
2142
      code = colInfoDataEnsureCapacity(pCol, 1, true);
232,211✔
2143
      if (code != 0) {
232,211✔
2144
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2145
        taosMemoryFree(pCol);
×
2146
        break;
×
2147
      }
2148
      dst.columnData = pCol;
232,211✔
2149
      dst.numOfRows = 1;
232,211✔
2150
      dst.colAlloced = true;
232,211✔
2151
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
232,211✔
2152
      if (colDataIsNull_var(dst.columnData, 0)) {
232,046✔
2153
        qInfo("invalid sub tb expr with null value");
2,805✔
2154
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,475✔
2155
      }
2156
      if (code == 0) {
231,881✔
2157
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
229,571✔
2158
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
229,736✔
2159
      }
2160
    } break;
232,046✔
2161
    default:
×
2162
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2163
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2164
      break;
×
2165
  }
2166
  if (code == 0) {
232,046✔
2167
    if (!pVal || len == 0) {
229,378✔
2168
      qError("tbname generated with no characters which is not allowed");
×
2169
      code = TSDB_CODE_INVALID_PARA;
×
2170
    }
2171
    if(len > TSDB_TABLE_NAME_LEN - 1) {
229,571✔
2172
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
430✔
2173
      len = TSDB_TABLE_NAME_LEN - 1;
430✔
2174
    }
2175

2176
    memcpy(tbname, pVal, len);
229,571✔
2177
    tbname[len] = '\0';  // ensure null terminated
229,571✔
2178
    if (NULL != strchr(tbname, '.')) {
229,736✔
2179
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2180
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2181
    }
2182
  }
2183
  // TODO free dst
2184
  sclFreeParam(&dst);
232,404✔
2185
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
232,046✔
2186
  return code;
232,046✔
2187
}
2188

2189
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
232,463✔
2190
  if (pParam) {
232,463✔
2191
    if (pParam->tbname) {
232,463✔
2192
      taosMemFree(pParam->tbname);
232,463✔
2193
      pParam->tbname = NULL;
232,463✔
2194
    }
2195
    if (pParam->stbname) {
232,463✔
2196
      taosMemFree(pParam->stbname);
232,463✔
2197
      pParam->stbname = NULL;
232,463✔
2198
    }
2199
    if (pParam->dbFName) {
232,463✔
2200
      taosMemFree(pParam->dbFName);
232,463✔
2201
      pParam->dbFName = NULL;
232,463✔
2202
    }
2203
    if (pParam->pFields) {
232,463✔
2204
      taosArrayDestroy(pParam->pFields);
232,463✔
2205
      pParam->pFields = NULL;
232,463✔
2206
    }
2207
    if (pParam->pTagFields) {
232,463✔
2208
      taosArrayDestroy(pParam->pTagFields);
135,096✔
2209
      pParam->pTagFields = NULL;
135,096✔
2210
    }
2211
    if (pParam->colCids) {
232,463✔
2212
      taosArrayDestroy(pParam->colCids);
1,733✔
2213
      pParam->colCids = NULL;
1,733✔
2214
    }
2215
    if (pParam->tagCids) {
232,463✔
2216
      taosArrayDestroy(pParam->tagCids);
1,110✔
2217
      pParam->tagCids = NULL;
1,110✔
2218
    }
2219
    taosMemFree(pParam);
232,463✔
2220
  }
2221
}
232,463✔
2222

2223
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
232,298✔
2224
  int32_t code = 0, lino = 0;
232,298✔
2225
  if (ppDst == NULL || pSrc == NULL) {
232,298✔
2226
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2227
  }
2228
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
232,298✔
2229
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
232,463✔
2230

2231
  (*ppDst)->suid = pSrc->suid;
232,463✔
2232
  (*ppDst)->sver = pSrc->sver;
232,248✔
2233
  (*ppDst)->tbType = pSrc->tbType;
232,248✔
2234
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
232,083✔
2235
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
232,248✔
2236

2237
  if (pSrc->stbname) {
232,248✔
2238
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
232,463✔
2239
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
232,463✔
2240
  }
2241

2242
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
232,463✔
2243
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
232,463✔
2244

2245
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
232,463✔
2246

2247
  if (pSrc->pFields && pSrc->pFields->size > 0) {
232,248✔
2248
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
232,463✔
2249
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
232,252✔
2250
  } else {
2251
    (*ppDst)->pFields = NULL;
×
2252
  }
2253
  
2254
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
232,463✔
2255
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
135,096✔
2256
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
135,096✔
2257
  } else {
2258
    (*ppDst)->pTagFields = NULL;
97,156✔
2259
  }
2260

2261
  if (pSrc->colCids && pSrc->colCids->size > 0) {
232,463✔
2262
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,733✔
2263
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,733✔
2264
  } else {
2265
    (*ppDst)->colCids = NULL;
230,519✔
2266
  }
2267

2268
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
232,463✔
2269
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,110✔
2270
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,110✔
2271
  } else {
2272
    (*ppDst)->tagCids = NULL;
231,142✔
2273
  }
2274

2275
_exit:
232,252✔
2276

2277
  if (code != 0) {
232,252✔
2278
    if (*ppDst) {
×
2279
      destroyStreamInserterParam(*ppDst);
×
2280
      *ppDst = NULL;
×
2281
    }
2282
    
2283
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2284
  }
2285
  return code;
232,463✔
2286
}
2287

2288
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
249✔
2289
  return doDropStreamTable(pMsgCb, pOutput, pReq);
249✔
2290
}
2291

2292
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2293
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2294
}
2295

2296
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
8,150✔
2297
  int32_t         code = TSDB_CODE_SUCCESS;
8,150✔
2298
  STableListInfo* pList = tableListCreate();
8,150✔
2299
  if (pList == NULL) {
8,150✔
2300
    code = terrno;
×
2301
    goto end;
×
2302
  }
2303
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2304
  // if (uidListCopy == NULL) {
2305
  //   code = terrno;
2306
  //   goto end;
2307
  // }
2308

2309
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
8,150✔
2310
  pList->idInfo.suid = suid;
8,150✔
2311
  pList->idInfo.tableType = TD_SUPER_TABLE;
8,150✔
2312
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
8,150✔
2313
  if (code != TSDB_CODE_SUCCESS) {
8,150✔
2314
    goto end;
×
2315
  }
2316
  // taosArrayClear(uidList);
2317
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2318
  //   void* tmp = taosArrayGet(uidListCopy, i);
2319
  //   if (tmp == NULL) {
2320
  //     continue;
2321
  //   }
2322
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2323
  //   if (slot == NULL) {
2324
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2325
  //       code = terrno;
2326
  //       goto end;
2327
  //     }
2328
  //   }
2329
  // }
2330
end:
8,150✔
2331
  // taosArrayDestroy(uidListCopy);
2332
  tableListDestroy(pList);
8,150✔
2333
  return code;
8,150✔
2334
}
2335

2336
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2337
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2338
    return true;
2,147,483,647✔
2339
  }
2340

2341
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2342
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2343
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2344
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2345
      return durationSatisfied;
2,147,483,647✔
2346
    case TRUE_FOR_COUNT_ONLY:
60,384✔
2347
      return countSatisfied;
60,384✔
2348
    case TRUE_FOR_AND:
59,052✔
2349
      return durationSatisfied && countSatisfied;
59,052✔
2350
    case TRUE_FOR_OR:
62,382✔
2351
      return durationSatisfied || countSatisfied;
62,382✔
2352
    default:
160✔
2353
      return true;
160✔
2354
  }
2355
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc