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

taosdata / TDengine / #4926

13 Jan 2026 05:43AM UTC coverage: 66.053% (-0.05%) from 66.107%
#4926

push

travis-ci

web-flow
feat: [6654385780] show snap progress (#34203)

48 of 59 new or added lines in 7 files covered. (81.36%)

582 existing lines in 124 files now uncovered.

200362 of 303334 relevant lines covered (66.05%)

132283104.31 hits per line

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

68.6
/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) {
951,407,610✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
951,407,610✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
951,407,610✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
951,583,638✔
47
}
951,202,974✔
48

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

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

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

70
static void initRefPool() {
541,259✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
541,259✔
72
  (void)atexit(cleanupRefPool);
541,259✔
73
}
541,259✔
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) {
43,847,790✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
43,847,790✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
43,849,975✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
20,028,104✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
20,028,104✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
20,026,528✔
162
      if (pScanInfo->base.dataReader != NULL) {
20,027,234✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
264,542✔
164
        if (code) {
264,232✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
23,820,616✔
172
  }
173

174
  return 0;
23,345,266✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
23,343,295✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
23,343,295✔
179
  pTaskInfo->id.queryId = queryId;
23,343,295✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
23,343,960✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
23,343,316✔
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) {
435,861✔
213
  if (msg == NULL) {  // create raw scan
435,861✔
214
    SExecTaskInfo* pTaskInfo = NULL;
116,452✔
215

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

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

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

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

248
  return pTaskInfo;
320,985✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
484,231✔
252
  if (streamInserterParam == NULL) {
484,231✔
253
    return TSDB_CODE_SUCCESS;
277,448✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
206,783✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
206,783✔
323
    if (code) {
206,380✔
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,
484,231✔
328
         tstrerror(code));
329

330
_error:
74,257✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,833,452✔
342
  if (pInfo == NULL) {
2,833,452✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,833,452✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,833,452✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,833,452✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,833,233✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,620,138✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
448,042✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,453,590✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,833,452✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
213,314✔
382
    setReadHandle(handle, pScanBaseInfo);
213,314✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,620,138✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,172,096✔
385
    setReadHandle(handle, pScanBaseInfo);
2,172,096✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
484,012✔
400

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

416
  return code;
484,231✔
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,
357,555✔
426
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
427
  int32_t code = TSDB_CODE_SUCCESS;
357,555✔
428
  int32_t lino = 0;
357,555✔
429
  int8_t  locked = 0;
357,555✔
430
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
357,555✔
431

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
357,555✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
357,555✔
444

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

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

454
  locked = 1;
357,555✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
731,751✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
374,196✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
374,196✔
458

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

465
    tDecoderClear(&mr.coder);
374,196✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
374,196✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
374,196✔
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) {
374,196✔
UNCOV
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};
374,196✔
485
    if (pScanInfo->pTagCond != NULL) {
374,196✔
486
      // tb_uid_t id = mr.me.uid;
487
      item.check = 1;
324,204✔
488
    }
489
    if (taosArrayPush(tUid, &item) == NULL) {
374,196✔
490
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
491
    }
492
  }
493

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
357,555✔
524

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

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

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

543
  if (isAdd) {
359,572✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
357,555✔
545
  }
546

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
359,572✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
359,572✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
359,572✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
359,572✔
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
359,572✔
566
    SArray* qa = NULL;
357,555✔
567
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
357,555✔
568
    if (code != TSDB_CODE_SUCCESS) {
357,555✔
569
      taosArrayDestroy(qa);
×
570
      return code;
×
571
    }
572
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
357,555✔
573
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
357,555✔
574
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
357,555✔
575

576
    bool   assignUid = false;
357,555✔
577
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
357,555✔
578
    char*  keyBuf = NULL;
357,555✔
579
    if (bufLen > 0) {
357,555✔
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;
357,555✔
589
    taosWLockLatch(&pTaskInfo->lock);
357,555✔
590

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

601
      if (bufLen > 0) {
212,094✔
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);
212,094✔
617
      if (code != TSDB_CODE_SUCCESS) {
212,094✔
618
        taosMemoryFree(keyBuf);
×
619
        taosArrayDestroy(qa);
×
620
        taosWUnLockLatch(&pTaskInfo->lock);
×
621
        return code;
×
622
      }
623
    }
624

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

630
    taosArrayDestroy(qa);
357,555✔
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);
2,017✔
633
    taosWLockLatch(&pTaskInfo->lock);
2,017✔
634
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
2,017✔
635
    taosWUnLockLatch(&pTaskInfo->lock);
2,017✔
636
  }
637

638
  return code;
359,572✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
742,361,726✔
647
    return TSDB_CODE_INVALID_PARA;
83,753✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
742,277,973✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
742,277,973✔
652
    return TSDB_CODE_SUCCESS;
424,398,505✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
318,114,391✔
662
  *tversion = pSchemaInfo->tversion;
318,206,862✔
663
  *rversion = pSchemaInfo->rversion;
318,311,725✔
664
  if (pSchemaInfo->dbname) {
317,977,465✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
317,818,720✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
318,219,973✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
318,103,432✔
671
  } else {
672
    tableName[0] = 0;
2✔
673
  }
674

675
  *tbGet = true;
318,223,408✔
676

677
  return TSDB_CODE_SUCCESS;
318,101,868✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
424,184,307✔
681

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

689
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
73,798,492✔
690
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
73,798,492✔
691
  ((SExecTaskInfo*)tinfo)->paramSet = false;
73,799,805✔
692
}
73,800,119✔
693

694
int32_t qExecutorInit(void) {
4,245,113✔
695
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,245,113✔
696
  return TSDB_CODE_SUCCESS;
4,245,113✔
697
}
698

699
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
74,450,299✔
700
  int32_t        code = TSDB_CODE_SUCCESS;
74,450,299✔
701
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
74,450,299✔
702
  if (pTask->pWorkerCb) {
74,450,299✔
703
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
74,475,008✔
704
    if (code != TSDB_CODE_SUCCESS) {
74,494,107✔
705
      pTask->code = code;
×
706
      return pTask->code;
×
707
    }
708
  }
709

710
  code = tsem_wait(pSem);
74,512,590✔
711
  if (code != TSDB_CODE_SUCCESS) {
74,494,011✔
712
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
713
    pTask->code = code;
×
714
    return pTask->code;
×
715
  }
716

717
  if (pTask->pWorkerCb) {
74,494,011✔
718
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
74,498,160✔
719
    if (code != TSDB_CODE_SUCCESS) {
74,497,238✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
74,494,933✔
725
}
726

727
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
438,863,695✔
728
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
729
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
730
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
438,863,695✔
731
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
438,863,695✔
732

733
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
438,834,576✔
734
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
735

736
  readHandle->uid = 0;
438,865,483✔
737
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
438,923,477✔
738
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
438,667,509✔
739
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
13,135,186✔
740
    goto _error;
12,570,700✔
741
  }
742
    
743
  if (handle) {
425,652,453✔
744
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
425,716,063✔
745
    void*           pSinkManager = NULL;
425,864,058✔
746
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
424,918,002✔
747
    if (code != TSDB_CODE_SUCCESS) {
425,678,390✔
748
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
749
      goto _error;
×
750
    }
751

752
    void* pSinkParam = NULL;
425,678,390✔
753
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
425,670,603✔
754
    if (code != TSDB_CODE_SUCCESS) {
425,210,788✔
755
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
756
      taosMemoryFree(pSinkManager);
×
757
      goto _error;
×
758
    }
759

760
    SDataSinkNode* pSink = NULL;
425,210,788✔
761
    if (readHandle->localExec) {
425,632,788✔
762
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,992✔
763
      if (code != TSDB_CODE_SUCCESS) {
2,992✔
764
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
765
               (*pTask)->id.str);
766
        taosMemoryFree(pSinkManager);
×
767
        goto _error;
×
768
      }
769
    }
770

771
    // pSinkParam has been freed during create sinker.
772
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
425,595,935✔
773
                              (*pTask)->id.str, pSubplan->processOneBlock);
425,902,745✔
774
    if (code) {
425,427,669✔
775
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
584✔
776
    }
777
  }
778

779
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
425,928,054✔
780
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
781

782
_error:
213,675,309✔
783
  // if failed to add ref for all tables in this query, abort current query
784
  return code;
438,729,112✔
785
}
786

787
static void freeBlock(void* param) {
×
788
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
789
  blockDataDestroy(pBlock);
×
790
}
×
791

792
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
515,342,099✔
793
                     bool processOneBlock) {
794
  int32_t        code = TSDB_CODE_SUCCESS;
515,342,099✔
795
  int32_t        lino = 0;
515,342,099✔
796
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
515,342,099✔
797
  int64_t        threadId = taosGetSelfPthreadId();
515,342,099✔
798

799
  if (pLocal) {
515,327,827✔
800
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
512,298,502✔
801
  }
802

803
  taosArrayClear(pResList);
514,977,273✔
804

805
  int64_t curOwner = 0;
514,963,934✔
806
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
514,963,934✔
807
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
808
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
809
    return pTaskInfo->code;
×
810
  }
811

812
  if (pTaskInfo->cost.start == 0) {
515,117,781✔
813
    pTaskInfo->cost.start = taosGetTimestampUs();
420,521,279✔
814
  }
815

816
  if (isTaskKilled(pTaskInfo)) {
515,543,979✔
UNCOV
817
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
818
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
819
    return pTaskInfo->code;
×
820
  }
821

822
  // error occurs, record the error code and return to client
823
  int32_t ret = setjmp(pTaskInfo->env);
515,050,532✔
824
  if (ret != TSDB_CODE_SUCCESS) {
526,598,372✔
825
    pTaskInfo->code = ret;
12,103,899✔
826
    (void)cleanUpUdfs();
12,103,899✔
827

828
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
12,103,899✔
829
    atomic_store_64(&pTaskInfo->owner, 0);
12,103,899✔
830

831
    return pTaskInfo->code;
12,103,899✔
832
  }
833

834
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
514,494,473✔
835

836
  int32_t      current = 0;
514,496,121✔
837
  SSDataBlock* pRes = NULL;
514,496,121✔
838
  int64_t      st = taosGetTimestampUs();
515,483,559✔
839

840
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
515,483,559✔
841
    pTaskInfo->paramSet = true;
73,795,628✔
842
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
73,798,530✔
843
  } else {
844
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
441,629,051✔
845
  }
846

847
  QUERY_CHECK_CODE(code, lino, _end);
503,448,210✔
848
  code = blockDataCheck(pRes);
503,448,210✔
849
  QUERY_CHECK_CODE(code, lino, _end);
503,525,409✔
850

851
  if (pRes == NULL) {
503,525,409✔
852
    st = taosGetTimestampUs();
104,594,630✔
853
  }
854

855
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
503,537,657✔
856
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
503,464,604✔
857
    rowsThreshold = 4096;
502,959,269✔
858
  }
859

860
  int32_t blockIndex = 0;
503,497,206✔
861
  while (pRes != NULL) {
1,167,470,106✔
862
    SSDataBlock* p = NULL;
709,190,482✔
863
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
709,048,567✔
864
      SSDataBlock* p1 = NULL;
555,928,423✔
865
      code = createOneDataBlock(pRes, true, &p1);
555,934,659✔
866
      QUERY_CHECK_CODE(code, lino, _end);
555,932,233✔
867

868
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
555,932,233✔
869
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
556,047,983✔
870
      p = p1;
556,047,983✔
871
    } else {
872
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
153,256,391✔
873
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
153,256,180✔
874

875
      p = *(SSDataBlock**)tmp;
153,256,180✔
876
      code = copyDataBlock(p, pRes);
153,256,180✔
877
      QUERY_CHECK_CODE(code, lino, _end);
153,255,069✔
878
    }
879

880
    blockIndex += 1;
709,304,637✔
881

882
    current += p->info.rows;
709,304,637✔
883
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
709,282,124✔
884
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
885
    void* tmp = taosArrayPush(pResList, &p);
709,308,967✔
886
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
709,308,967✔
887

888
    if (current >= rowsThreshold || processOneBlock) {
709,308,967✔
889
      break;
890
    }
891

892
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
664,054,252✔
893
    QUERY_CHECK_CODE(code, lino, _end);
663,950,577✔
894
    code = blockDataCheck(pRes);
663,950,577✔
895
    QUERY_CHECK_CODE(code, lino, _end);
664,004,534✔
896
  }
897

898
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
503,534,339✔
899
    pTaskInfo->pSubplan->rowsThreshold -= current;
541,829✔
900
  }
901

902
  *hasMore = (pRes != NULL);
503,574,875✔
903
  uint64_t el = (taosGetTimestampUs() - st);
503,482,698✔
904

905
  pTaskInfo->cost.elapsedTime += el;
503,482,698✔
906
  if (NULL == pRes) {
503,511,335✔
907
    *useconds = pTaskInfo->cost.elapsedTime;
458,256,620✔
908
  }
909

910
_end:
503,430,455✔
911
  (void)cleanUpUdfs();
503,451,733✔
912

913
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
503,622,391✔
914
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
503,622,391✔
915
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
916

917
  atomic_store_64(&pTaskInfo->owner, 0);
503,622,826✔
918
  if (code) {
503,621,701✔
919
    pTaskInfo->code = code;
×
920
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
921
  }
922

923
  return pTaskInfo->code;
503,621,701✔
924
}
925

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

937
  taosArrayClear(pTaskInfo->pResultBlockList);
×
938
}
×
939

940
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
123,634,965✔
941
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
123,634,965✔
942
  int64_t        threadId = taosGetSelfPthreadId();
123,634,965✔
943
  int64_t        curOwner = 0;
123,640,080✔
944

945
  *pRes = NULL;
123,640,080✔
946

947
  // todo extract method
948
  taosRLockLatch(&pTaskInfo->lock);
123,640,080✔
949
  bool isKilled = isTaskKilled(pTaskInfo);
123,642,467✔
950
  if (isKilled) {
123,641,870✔
951
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
952

953
    taosRUnLockLatch(&pTaskInfo->lock);
×
954
    return pTaskInfo->code;
×
955
  }
956

957
  if (pTaskInfo->owner != 0) {
123,641,870✔
958
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
959
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
960

961
    taosRUnLockLatch(&pTaskInfo->lock);
×
962
    return pTaskInfo->code;
×
963
  }
964

965
  pTaskInfo->owner = threadId;
123,641,870✔
966
  taosRUnLockLatch(&pTaskInfo->lock);
123,641,862✔
967

968
  if (pTaskInfo->cost.start == 0) {
123,641,119✔
969
    pTaskInfo->cost.start = taosGetTimestampUs();
226,629✔
970
  }
971

972
  // error occurs, record the error code and return to client
973
  int32_t ret = setjmp(pTaskInfo->env);
123,641,444✔
974
  if (ret != TSDB_CODE_SUCCESS) {
123,639,475✔
975
    pTaskInfo->code = ret;
×
976
    (void)cleanUpUdfs();
×
977
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
978
    atomic_store_64(&pTaskInfo->owner, 0);
×
979
    return pTaskInfo->code;
×
980
  }
981

982
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
123,639,475✔
983

984
  int64_t st = taosGetTimestampUs();
123,639,057✔
985
  int32_t code = TSDB_CODE_SUCCESS;
123,639,057✔
986
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
123,639,057✔
987
    pTaskInfo->paramSet = true;
×
988
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
989
  } else {
990
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
123,640,080✔
991
  }
992
  if (code) {
123,596,855✔
993
    pTaskInfo->code = code;
×
994
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
995
  }
996

997
  code = blockDataCheck(*pRes);
123,596,855✔
998
  if (code) {
123,637,821✔
999
    pTaskInfo->code = code;
×
1000
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1001
  }
1002

1003
  uint64_t el = (taosGetTimestampUs() - st);
123,600,865✔
1004

1005
  pTaskInfo->cost.elapsedTime += el;
123,600,865✔
1006
  if (NULL == *pRes) {
123,629,154✔
1007
    *useconds = pTaskInfo->cost.elapsedTime;
19,524,472✔
1008
  }
1009

1010
  (void)cleanUpUdfs();
123,630,178✔
1011

1012
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
123,639,197✔
1013
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
123,642,467✔
1014

1015
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
123,642,157✔
1016
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1017

1018
  atomic_store_64(&pTaskInfo->owner, 0);
123,640,801✔
1019
  return pTaskInfo->code;
123,641,125✔
1020
}
1021

1022
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
130,843,555✔
1023
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
130,843,555✔
1024
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
130,844,801✔
1025
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
130,845,863✔
1026

1027
  if (!tmp) {
130,844,477✔
1028
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1029
    return terrno;
×
1030
  }
1031
  return TSDB_CODE_SUCCESS;
130,844,477✔
1032
}
1033

1034
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1035
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1036
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1037

1038
  if (key->refId < pInfo->refId) {
×
1039
    return -1;
×
1040
  } else if (key->refId > pInfo->refId) {
×
1041
    return 1;
×
1042
  }
1043

1044
  return 0;
×
1045
}
1046

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

1056
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
433,125✔
1057
  if (pTaskInfo->subJobCtx.hasSubJobs) {
433,125✔
1058
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
385,022✔
1059
    if (pTaskInfo->subJobCtx.param) {
385,022✔
1060
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
201,482✔
1061
    }
1062
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
385,022✔
1063
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
385,022✔
1064
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
385,022✔
1065
  }
1066
  
1067
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
433,125✔
1068

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

1091
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
433,125✔
1092
}
433,125✔
1093

1094
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
433,125✔
1095
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
433,125✔
1096
  if (pTaskInfo == NULL) {
433,125✔
1097
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1098
  }
1099

1100
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
433,125✔
1101

1102
  setTaskKilled(pTaskInfo, rspCode);
433,125✔
1103
  qStopTaskOperators(pTaskInfo);
433,125✔
1104

1105
  return TSDB_CODE_SUCCESS;
433,125✔
1106
}
1107

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

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

1121
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1122

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

1129
        taosMsleep(200);
×
1130

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

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

1152
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1153
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1154
  if (NULL == pTaskInfo) {
×
1155
    return false;
×
1156
  }
1157

1158
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1159
}
1160

1161
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
427,031,055✔
1162
  STaskCostInfo* pSummary = &pTaskInfo->cost;
427,031,055✔
1163
  int64_t        idleTime = pSummary->start - pSummary->created;
427,101,234✔
1164

1165
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
427,018,449✔
1166
  if (pSummary->pRecoder != NULL) {
427,040,210✔
1167
    qDebug(
311,525,556✔
1168
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1169
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1170
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1171
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1172
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1173
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1174
  } else {
1175
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
115,537,466✔
1176
           pSummary->elapsedTime / 1000.0);
1177
  }
1178
}
427,063,022✔
1179

1180

1181
void qDestroyTask(qTaskInfo_t qTaskHandle) {
445,037,080✔
1182
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
445,037,080✔
1183
  if (pTaskInfo == NULL) {
445,037,080✔
1184
    return;
18,007,693✔
1185
  }
1186

1187
  if (pTaskInfo->pRoot != NULL) {
427,029,387✔
1188
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
427,068,583✔
1189
  } else {
1190
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1191
  }
1192

1193
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
427,069,176✔
1194
  doDestroyTask(pTaskInfo);
427,017,400✔
1195
}
1196

1197
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
20,011,765✔
1198
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,011,765✔
1199
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
20,011,765✔
1200
}
1201

1202
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
320,985✔
1203
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
320,985✔
1204
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
320,985✔
1205

1206
  while (1) {
315,010✔
1207
    uint16_t type = pOperator->operatorType;
635,995✔
1208
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
635,995✔
1209
      *scanner = pOperator->info;
320,985✔
1210
      break;
320,985✔
1211
    } else {
1212
      pOperator = pOperator->pDownstream[0];
315,010✔
1213
    }
1214
  }
1215
}
320,985✔
1216

1217
void* qExtractReaderFromTmqScanner(void* scanner) {
320,985✔
1218
  SStreamScanInfo* pInfo = scanner;
320,985✔
1219
  return (void*)pInfo->tqReader;
320,985✔
1220
}
1221

1222
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
733,950✔
1223
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
733,950✔
1224
  return pTaskInfo->streamInfo.schema;
733,950✔
1225
}
1226

1227
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
733,950✔
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
733,950✔
1229
  return pTaskInfo->streamInfo.tbName;
733,950✔
1230
}
1231

1232
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
782,782✔
1233
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
782,782✔
1234
  return &pTaskInfo->streamInfo.btMetaRsp;
782,782✔
1235
}
1236

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

1248
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
747,763✔
1249
  memset(pCond, 0, sizeof(SQueryTableDataCond));
747,763✔
1250
  pCond->order = TSDB_ORDER_ASC;
747,763✔
1251
  pCond->numOfCols = pMtInfo->schema->nCols;
747,425✔
1252
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
747,763✔
1253
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
747,763✔
1254
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
747,763✔
UNCOV
1255
    taosMemoryFreeClear(pCond->colList);
×
1256
    taosMemoryFreeClear(pCond->pSlotList);
×
1257
    return terrno;
×
1258
  }
1259

1260
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
747,763✔
1261
  pCond->suid = pMtInfo->suid;
747,763✔
1262
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
747,763✔
1263
  pCond->startVersion = -1;
747,763✔
1264
  pCond->endVersion = sContext->snapVersion;
747,763✔
1265

1266
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,720,962✔
1267
    SColumnInfo* pColInfo = &pCond->colList[i];
3,972,259✔
1268
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,972,259✔
1269
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,972,259✔
1270
    if (pMtInfo->pExtSchemas != NULL) {
3,971,954✔
1271
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,000✔
1272
    }
1273
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,972,251✔
1274
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,972,267✔
1275

1276
    pCond->pSlotList[i] = i;
3,972,564✔
1277
  }
1278

1279
  return TSDB_CODE_SUCCESS;
747,466✔
1280
}
1281

1282
void qStreamSetOpen(qTaskInfo_t tinfo) {
122,127,003✔
1283
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
122,127,003✔
1284
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
122,127,003✔
1285
  pOperator->status = OP_NOT_OPENED;
122,146,217✔
1286
}
122,141,505✔
1287

1288
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
19,833,169✔
1289
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,833,169✔
1290
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
19,833,169✔
1291
}
19,834,520✔
1292

1293
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
20,821,619✔
1294
  int32_t        code = TSDB_CODE_SUCCESS;
20,821,619✔
1295
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,821,619✔
1296
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
20,821,619✔
1297

1298
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
20,823,113✔
1299
  const char*    id = GET_TASKID(pTaskInfo);
20,822,845✔
1300

1301
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
20,821,553✔
1302
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
19,538,041✔
1303
    if (pOperator == NULL || code != 0) {
19,536,461✔
1304
      return code;
×
1305
    }
1306

1307
    SStreamScanInfo* pInfo = pOperator->info;
19,537,744✔
1308
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
19,535,443✔
1309
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
19,539,053✔
1310
    walReaderVerifyOffset(pWalReader, pOffset);
19,537,063✔
1311
  }
1312
  // if pOffset equal to current offset, means continue consume
1313
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
20,822,505✔
1314
    return 0;
18,990,077✔
1315
  }
1316

1317
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,828,590✔
1318
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,065,933✔
1319
    if (pOperator == NULL || code != 0) {
1,066,559✔
1320
      return code;
×
1321
    }
1322

1323
    SStreamScanInfo* pInfo = pOperator->info;
1,066,559✔
1324
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,065,933✔
1325
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,066,204✔
1326
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,066,204✔
1327

1328
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,065,933✔
1329
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
842,051✔
1330
      pScanBaseInfo->dataReader = NULL;
842,051✔
1331

1332
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
842,051✔
1333
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
842,051✔
1334
      walReaderVerifyOffset(pWalReader, pOffset);
842,051✔
1335
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
842,051✔
1336
      if (code < 0) {
842,051✔
1337
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,567✔
1338
        return code;
11,567✔
1339
      }
1340
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
224,237✔
1341
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1342
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1343
      int64_t uid = pOffset->uid;
224,822✔
1344
      int64_t ts = pOffset->ts;
224,508✔
1345
      int32_t index = 0;
224,508✔
1346

1347
      // this value may be changed if new tables are created
1348
      taosRLockLatch(&pTaskInfo->lock);
224,508✔
1349
      int32_t numOfTables = 0;
224,822✔
1350
      code = tableListGetSize(pTableListInfo, &numOfTables);
224,822✔
1351
      if (code != TSDB_CODE_SUCCESS) {
224,551✔
1352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1353
        taosRUnLockLatch(&pTaskInfo->lock);
×
1354
        return code;
×
1355
      }
1356

1357
      if (uid == 0) {
224,551✔
1358
        if (numOfTables != 0) {
216,981✔
1359
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
37,771✔
1360
          if (!tmp) {
37,771✔
1361
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1362
            taosRUnLockLatch(&pTaskInfo->lock);
×
1363
            return terrno;
×
1364
          }
1365
          if (tmp) uid = tmp->uid;
37,771✔
1366
          ts = INT64_MIN;
37,771✔
1367
          pScanInfo->currentTable = 0;
37,771✔
1368
        } else {
1369
          taosRUnLockLatch(&pTaskInfo->lock);
179,210✔
1370
          qError("no table in table list, %s", id);
179,481✔
1371
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
179,795✔
1372
        }
1373
      }
1374
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,341✔
1375

1376
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
44,091✔
1377
             pInfo->pTableScanOp->resultInfo.totalRows);
1378
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
44,091✔
1379

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

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

1394
      STableKeyInfo keyInfo = {.uid = uid};
45,027✔
1395
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
45,027✔
1396

1397
      // let's start from the next ts that returned to consumer.
1398
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
45,027✔
1399
        pScanBaseInfo->cond.twindows.skey = ts;
916✔
1400
      } else {
1401
        pScanBaseInfo->cond.twindows.skey = ts + 1;
44,111✔
1402
      }
1403
      pScanInfo->scanTimes = 0;
45,027✔
1404

1405
      if (pScanBaseInfo->dataReader == NULL) {
45,027✔
1406
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
77,126✔
1407
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1408
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
38,563✔
1409
        if (code != TSDB_CODE_SUCCESS) {
38,563✔
1410
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1411
          return code;
×
1412
        }
1413

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

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

1432
      // restore the key value
1433
      pScanBaseInfo->cond.twindows.skey = oldSkey;
45,027✔
1434
    } else {
1435
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1436
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1437
    }
1438

1439
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1440
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
762,657✔
1441
      SStreamRawScanInfo* pInfo = pOperator->info;
748,599✔
1442
      SSnapContext*       sContext = pInfo->sContext;
748,599✔
1443
      SOperatorInfo*      p = NULL;
748,599✔
1444

1445
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
748,599✔
1446
      if (code != 0) {
748,599✔
1447
        return code;
×
1448
      }
1449

1450
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
748,599✔
1451

1452
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
748,599✔
1453
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1454
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1455
      }
1456

1457
      SMetaTableInfo mtInfo = {0};
748,599✔
1458
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
748,599✔
1459
      if (code != 0) {
748,302✔
1460
        destroyMetaTableInfo(&mtInfo);
1461
        return code;
×
1462
      }
1463
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
748,302✔
1464
      pInfo->dataReader = NULL;
747,708✔
1465

1466
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
748,005✔
1467
      tableListClear(pTableListInfo);
748,005✔
1468

1469
      if (mtInfo.uid == 0) {
748,599✔
1470
        destroyMetaTableInfo(&mtInfo);
1471
        goto end;  // no data
836✔
1472
      }
1473

1474
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
747,763✔
1475
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
747,763✔
1476
      if (code != TSDB_CODE_SUCCESS) {
747,466✔
1477
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1478
        destroyMetaTableInfo(&mtInfo);
1479
        return code;
×
1480
      }
1481
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
747,466✔
1482
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,301✔
1483
      } else {
1484
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
746,165✔
1485
      }
1486

1487
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
747,128✔
1488
      if (code != TSDB_CODE_SUCCESS) {
747,763✔
1489
        destroyMetaTableInfo(&mtInfo);
1490
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1491
        return code;
×
1492
      }
1493

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

1508
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,495,526✔
1509
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
747,763✔
1510
      if (code != TSDB_CODE_SUCCESS) {
747,466✔
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
1513
        return code;
×
1514
      }
1515

1516
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
747,466✔
1517
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
747,169✔
1518
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1519
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
746,872✔
1520
      pTaskInfo->streamInfo.schema = mtInfo.schema;
746,872✔
1521
      taosMemoryFreeClear(mtInfo.pExtSchemas);
747,169✔
1522

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

1542
end:
1,638,060✔
1543
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,638,168✔
1544
  return 0;
1,638,168✔
1545
}
1546

1547
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
383,179,914✔
1548
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
383,179,914✔
1549
  if (pMsg->info.ahandle == NULL) {
383,297,251✔
1550
    rpcFreeCont(pMsg->pCont);
×
1551
    qError("pMsg->info.ahandle is NULL");
×
1552
    return;
×
1553
  }
1554

1555
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
383,128,420✔
1556
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1557

1558
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
383,133,120✔
1559

1560
  if (pMsg->contLen > 0) {
383,223,856✔
1561
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
383,214,295✔
1562
    if (buf.pData == NULL) {
382,995,668✔
1563
      pMsg->code = terrno;
×
1564
    } else {
1565
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
382,995,668✔
1566
    }
1567
  }
1568

1569
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
383,207,224✔
1570
  rpcFreeCont(pMsg->pCont);
383,354,426✔
1571
  destroySendMsgInfo(pSendInfo);
383,278,015✔
1572
}
1573

1574
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1575
  int32_t        code = TSDB_CODE_SUCCESS;
×
1576
  int32_t        lino = 0;
×
1577
  SExecTaskInfo* pTaskInfo = tinfo;
×
1578
  SArray*        plist = NULL;
×
1579

1580
  code = getTableListInfo(pTaskInfo, &plist);
×
1581
  if (code || plist == NULL) {
×
1582
    return NULL;
×
1583
  }
1584

1585
  // only extract table in the first elements
1586
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1587

1588
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1589
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1590

1591
  int32_t numOfTables = 0;
×
1592
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1593
  QUERY_CHECK_CODE(code, lino, _end);
×
1594

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

1602
  taosArrayDestroy(plist);
×
1603

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

1612
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,351,557✔
1613
  int32_t        code = TSDB_CODE_SUCCESS;
3,351,557✔
1614
  int32_t        lino = 0;
3,351,557✔
1615
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,351,557✔
1616

1617
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,352,094✔
1618
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1619
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1620

1621
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1622
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1623
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,351,557✔
1624
    STableScanInfo* pScanInfo = pOperator->info;
1,675,239✔
1625

1626
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,675,776✔
1627
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,676,313✔
1628
  } else {
1629
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,675,781✔
1630
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,674,702✔
1631
    }
1632
  }
1633

1634
_end:
×
1635
  if (code != TSDB_CODE_SUCCESS) {
3,352,631✔
1636
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1637
  }
1638
  return code;
3,351,015✔
1639
}
1640

1641
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,676,318✔
1642
  if (pList == NULL) {
1,676,318✔
1643
    return TSDB_CODE_INVALID_PARA;
×
1644
  }
1645

1646
  *pList = NULL;
1,676,318✔
1647
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,676,318✔
1648
  if (pArray == NULL) {
1,676,855✔
1649
    return terrno;
×
1650
  }
1651

1652
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,676,855✔
1653
  if (code == 0) {
1,676,318✔
1654
    *pList = pArray;
1,676,318✔
1655
  } else {
1656
    taosArrayDestroy(pArray);
×
1657
  }
1658
  return code;
1,676,855✔
1659
}
1660

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

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

1677
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1678
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1679

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

1685
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1686
  int32_t code = 0;
×
1687
  return code;
×
1688
}
1689

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

1696
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
24,588,437✔
1697
  int32_t code = 0;
24,588,437✔
1698

1699
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
24,588,437✔
1700
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
24,587,034✔
1701

1702
  if (pOper->fpSet.resetStateFn) {
24,586,961✔
1703
    code = pOper->fpSet.resetStateFn(pOper);
24,588,393✔
1704
  }
1705
  pOper->status = OP_NOT_OPENED;
24,582,958✔
1706
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
42,038,957✔
1707
    code = clearStatesForOperator(pOper->pDownstream[i]);
17,451,226✔
1708
  }
1709
  return code;
24,590,273✔
1710
}
1711

1712
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,138,212✔
1713
  int32_t        code = 0;
7,138,212✔
1714
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,138,212✔
1715
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,138,212✔
1716
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,138,212✔
1717
  code = clearStatesForOperator(pOper);
7,138,212✔
1718
  return code;
7,137,792✔
1719
}
1720

1721
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
9,790,772✔
1722
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
9,790,772✔
1723
  int64_t        threadId = taosGetSelfPthreadId();
9,790,772✔
1724
  int64_t        curOwner = 0;
9,790,772✔
1725

1726
  *ppRes = NULL;
9,790,772✔
1727

1728
  // todo extract method
1729
  taosRLockLatch(&pTaskInfo->lock);
9,790,772✔
1730
  bool isKilled = isTaskKilled(pTaskInfo);
9,790,772✔
1731
  if (isKilled) {
9,790,556✔
1732
    // clearStreamBlock(pTaskInfo->pRoot);
1733
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1734

1735
    taosRUnLockLatch(&pTaskInfo->lock);
×
1736
    return pTaskInfo->code;
×
1737
  }
1738

1739
  if (pTaskInfo->owner != 0) {
9,790,556✔
1740
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1741
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1742

1743
    taosRUnLockLatch(&pTaskInfo->lock);
×
1744
    return pTaskInfo->code;
×
1745
  }
1746

1747
  pTaskInfo->owner = threadId;
9,790,534✔
1748
  taosRUnLockLatch(&pTaskInfo->lock);
9,790,772✔
1749

1750
  if (pTaskInfo->cost.start == 0) {
9,790,534✔
1751
    pTaskInfo->cost.start = taosGetTimestampUs();
213,704✔
1752
  }
1753

1754
  // error occurs, record the error code and return to client
1755
  int32_t ret = setjmp(pTaskInfo->env);
9,790,772✔
1756
  if (ret != TSDB_CODE_SUCCESS) {
11,360,171✔
1757
    pTaskInfo->code = ret;
1,569,399✔
1758
    (void)cleanUpUdfs();
1,569,399✔
1759
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,569,399✔
1760
    atomic_store_64(&pTaskInfo->owner, 0);
1,569,399✔
1761
    return pTaskInfo->code;
1,569,399✔
1762
  }
1763

1764
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,790,772✔
1765

1766
  int64_t st = taosGetTimestampUs();
9,790,772✔
1767

1768
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
9,790,772✔
1769
  if (code) {
8,221,373✔
1770
    pTaskInfo->code = code;
×
1771
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1772
  } else {
1773
    *finished = *ppRes == NULL;
8,221,373✔
1774
    code = blockDataCheck(*ppRes);
8,221,373✔
1775
  }
1776
  if (code) {
8,221,373✔
1777
    pTaskInfo->code = code;
×
1778
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1779
  }
1780

1781
  uint64_t el = (taosGetTimestampUs() - st);
8,221,373✔
1782

1783
  pTaskInfo->cost.elapsedTime += el;
8,221,373✔
1784
  if (NULL == *ppRes) {
8,221,373✔
1785
    *useconds = pTaskInfo->cost.elapsedTime;
5,155,405✔
1786
  }
1787

1788
  (void)cleanUpUdfs();
8,221,373✔
1789

1790
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,221,373✔
1791
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,221,373✔
1792

1793
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,221,373✔
1794
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1795

1796
  atomic_store_64(&pTaskInfo->owner, 0);
8,221,373✔
1797
  return pTaskInfo->code;
8,221,373✔
1798
}
1799

1800
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1801
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1802
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1803
// }
1804

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

1820
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
231,422✔
1821
  SReadHandle    pHandle = {.vnode = pVnode};
231,422✔
1822
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
231,422✔
1823

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

1832
end:
231,422✔
1833
  return 0;
231,422✔
1834
}
1835

1836
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
33,160✔
1837
  bool   listAdded = false;
33,160✔
1838
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
33,160✔
1839
  if (code == 0 && !listAdded) {
33,160✔
1840
    int32_t numOfTables = taosArrayGetSize(pUidList);
27,202✔
1841
    for (int i = 0; i < numOfTables; i++) {
54,404✔
1842
      void* tmp = taosArrayGet(pUidList, i);
27,202✔
1843
      if (tmp == NULL) {
27,202✔
1844
        return terrno;
×
1845
      }
1846
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
27,202✔
1847

1848
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
27,202✔
1849
      if (p == NULL) {
27,202✔
1850
        return terrno;
×
1851
      }
1852
    }
1853
  }
1854
  return code;
33,160✔
1855
}
1856

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

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

1887
  taosArrayClear(uidList);
28,708✔
1888
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
57,416✔
1889
    void* tmp = taosArrayGet(uidListCopy, i);
28,708✔
1890
    if (tmp == NULL) {
28,708✔
1891
      continue;
×
1892
    }
1893
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
28,708✔
1894
    if (slot == NULL) {
28,708✔
1895
      if (taosArrayPush(uidList, tmp) == NULL) {
3,337✔
1896
        code = terrno;
×
1897
        goto end;
×
1898
      }
1899
    }
1900
  }
1901
end:
28,708✔
1902
  taosArrayDestroy(uidListCopy);
28,708✔
1903
  tableListDestroy(pList);
28,708✔
1904
  return code;
28,708✔
1905
}
1906

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

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

1928
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
231,422✔
1929
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
231,422✔
1930
  STableListInfo* pList = pTableListInfo;
231,422✔
1931
  return pList->pTableList;
231,422✔
1932
}
1933

1934
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,409,965✔
1935

1936
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,194,030✔
1937
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,194,030✔
1938
  qDestroyTask(tInfo);
2,194,030✔
1939
}
2,194,030✔
1940

1941
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
585,209✔
1942
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
585,209✔
1943
}
1944

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

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

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

2009
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,092,752✔
2010
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,092,752✔
2011
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,092,752✔
2012
  int32_t        code = 0;
2,092,752✔
2013
  SNode*         pNode = NULL;
2,092,752✔
2014
  if (!pForceOutputCols) return 0;
2,092,752✔
2015
  if (!*pRes) {
41,461✔
2016
    code = createDataBlock(pRes);
41,461✔
2017
  }
2018

2019
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
41,461✔
2020
    int32_t idx = 0;
41,461✔
2021
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
165,655✔
2022
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
124,194✔
2023
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
124,194✔
2024
      colInfo.info.precision = pCol->type.precision;
124,194✔
2025
      colInfo.info.scale = pCol->type.scale;
124,194✔
2026
      code = blockDataAppendColInfo(*pRes, &colInfo);
124,194✔
2027
      if (code != 0) break;
124,194✔
2028
    }
2029
  }
2030

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

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

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

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

2149
    memcpy(tbname, pVal, len);
227,459✔
2150
    tbname[len] = '\0';  // ensure null terminated
227,459✔
2151
    if (NULL != strchr(tbname, '.')) {
227,459✔
2152
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2153
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2154
    }
2155
  }
2156
  // TODO free dst
2157
  sclFreeParam(&dst);
229,889✔
2158
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
229,889✔
2159
  return code;
229,889✔
2160
}
2161

2162
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
206,783✔
2163
  if (pParam) {
206,783✔
2164
    if (pParam->tbname) {
206,783✔
2165
      taosMemFree(pParam->tbname);
206,783✔
2166
      pParam->tbname = NULL;
206,783✔
2167
    }
2168
    if (pParam->stbname) {
206,783✔
2169
      taosMemFree(pParam->stbname);
206,783✔
2170
      pParam->stbname = NULL;
206,783✔
2171
    }
2172
    if (pParam->dbFName) {
206,783✔
2173
      taosMemFree(pParam->dbFName);
206,783✔
2174
      pParam->dbFName = NULL;
206,783✔
2175
    }
2176
    if (pParam->pFields) {
206,783✔
2177
      taosArrayDestroy(pParam->pFields);
206,783✔
2178
      pParam->pFields = NULL;
206,783✔
2179
    }
2180
    if (pParam->pTagFields) {
206,783✔
2181
      taosArrayDestroy(pParam->pTagFields);
128,938✔
2182
      pParam->pTagFields = NULL;
128,938✔
2183
    }
2184
    if (pParam->colCids) {
206,783✔
2185
      taosArrayDestroy(pParam->colCids);
1,722✔
2186
      pParam->colCids = NULL;
1,722✔
2187
    }
2188
    if (pParam->tagCids) {
206,783✔
2189
      taosArrayDestroy(pParam->tagCids);
1,100✔
2190
      pParam->tagCids = NULL;
1,100✔
2191
    }
2192
    taosMemFree(pParam);
206,783✔
2193
  }
2194
}
206,783✔
2195

2196
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
206,783✔
2197
  int32_t code = 0, lino = 0;
206,783✔
2198
  if (ppDst == NULL || pSrc == NULL) {
206,783✔
2199
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2200
  }
2201
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
206,783✔
2202
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
206,783✔
2203

2204
  (*ppDst)->suid = pSrc->suid;
206,783✔
2205
  (*ppDst)->sver = pSrc->sver;
206,783✔
2206
  (*ppDst)->tbType = pSrc->tbType;
206,380✔
2207
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
206,783✔
2208
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
206,783✔
2209

2210
  if (pSrc->stbname) {
206,783✔
2211
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
206,783✔
2212
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
206,380✔
2213
  }
2214

2215
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
206,783✔
2216
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
206,783✔
2217

2218
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
206,783✔
2219

2220
  if (pSrc->pFields && pSrc->pFields->size > 0) {
206,783✔
2221
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
206,380✔
2222
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
206,380✔
2223
  } else {
2224
    (*ppDst)->pFields = NULL;
403✔
2225
  }
2226
  
2227
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
206,783✔
2228
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
128,938✔
2229
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
128,535✔
2230
  } else {
2231
    (*ppDst)->pTagFields = NULL;
77,442✔
2232
  }
2233

2234
  if (pSrc->colCids && pSrc->colCids->size > 0) {
206,380✔
2235
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,722✔
2236
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,722✔
2237
  } else {
2238
    (*ppDst)->colCids = NULL;
205,061✔
2239
  }
2240

2241
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
206,783✔
2242
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,100✔
2243
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,100✔
2244
  } else {
2245
    (*ppDst)->tagCids = NULL;
205,683✔
2246
  }
2247

2248
_exit:
206,783✔
2249

2250
  if (code != 0) {
206,783✔
2251
    if (*ppDst) {
×
2252
      destroyStreamInserterParam(*ppDst);
×
2253
      *ppDst = NULL;
×
2254
    }
2255
    
2256
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2257
  }
2258
  return code;
206,783✔
2259
}
2260

2261
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
245✔
2262
  return doDropStreamTable(pMsgCb, pOutput, pReq);
245✔
2263
}
2264

2265
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2266
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2267
}
2268

2269
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
4,452✔
2270
  int32_t         code = TSDB_CODE_SUCCESS;
4,452✔
2271
  STableListInfo* pList = tableListCreate();
4,452✔
2272
  if (pList == NULL) {
4,452✔
2273
    code = terrno;
×
2274
    goto end;
×
2275
  }
2276
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2277
  // if (uidListCopy == NULL) {
2278
  //   code = terrno;
2279
  //   goto end;
2280
  // }
2281

2282
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,452✔
2283
  pList->idInfo.suid = suid;
4,452✔
2284
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,452✔
2285
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,452✔
2286
  if (code != TSDB_CODE_SUCCESS) {
4,452✔
2287
    goto end;
×
2288
  }
2289
  // taosArrayClear(uidList);
2290
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2291
  //   void* tmp = taosArrayGet(uidListCopy, i);
2292
  //   if (tmp == NULL) {
2293
  //     continue;
2294
  //   }
2295
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2296
  //   if (slot == NULL) {
2297
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2298
  //       code = terrno;
2299
  //       goto end;
2300
  //     }
2301
  //   }
2302
  // }
2303
end:
4,452✔
2304
  // taosArrayDestroy(uidListCopy);
2305
  tableListDestroy(pList);
4,452✔
2306
  return code;
4,452✔
2307
}
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