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

taosdata / TDengine / #4959

09 Feb 2026 01:16AM UTC coverage: 66.863% (+0.02%) from 66.842%
#4959

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205736 of 307696 relevant lines covered (66.86%)

128357780.92 hits per line

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

67.99
/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) {
589,868,891✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
589,868,891✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
589,868,891✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
589,908,424✔
47
}
589,711,806✔
48

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

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

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

70
static void initRefPool() {
550,166✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
550,166✔
72
  (void)atexit(cleanupRefPool);
550,166✔
73
}
550,166✔
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) {
48,347,390✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
48,347,390✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
48,347,852✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
21,836,494✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
21,836,149✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
21,836,515✔
162
      if (pScanInfo->base.dataReader != NULL) {
21,836,494✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
267,816✔
164
        if (code) {
267,189✔
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);
26,510,579✔
172
  }
173

174
  return 0;
25,989,361✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
25,989,786✔
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) {
449,047✔
213
  if (msg == NULL) {  // create raw scan
449,047✔
214
    SExecTaskInfo* pTaskInfo = NULL;
121,977✔
215

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

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

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

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

248
  return pTaskInfo;
327,394✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
512,073✔
252
  if (streamInserterParam == NULL) {
512,073✔
253
    return TSDB_CODE_SUCCESS;
282,648✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
229,425✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
229,425✔
323
    if (code) {
229,425✔
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,
511,852✔
328
         tstrerror(code));
329

330
_error:
74,464✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,641,764✔
342
  if (pInfo == NULL) {
3,641,764✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,641,764✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,641,764✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,641,764✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,641,764✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
3,308,335✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
737,133✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
6,950,099✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

377
  void*           info = pOperator->info;
3,641,764✔
378
  STableScanBase* pScanBaseInfo = NULL;
3,641,543✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,641,543✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
333,429✔
382
    setReadHandle(handle, pScanBaseInfo);
333,429✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
3,308,335✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,571,202✔
385
    setReadHandle(handle, pScanBaseInfo);
2,571,202✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
512,073✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
385,233✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
385,233✔
444

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

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

454
  locked = 1;
385,233✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
847,074✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
461,841✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
461,841✔
458

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

465
    tDecoderClear(&mr.coder);
461,841✔
466

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
385,233✔
524

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

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

538
int32_t qDeleteTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
539
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
540
  const char*    id = GET_TASKID(pTaskInfo);
×
541
  int32_t        code = 0;
×
542

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

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

557
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
558
    }
559
  }
560
  
561
  qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
×
562
  taosWLockLatch(&pTaskInfo->lock);
×
563
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
564
  taosWUnLockLatch(&pTaskInfo->lock);
×
565

566
  return code;
×
567
}
568

569
static int32_t filterTableForTmqQuery(SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* id, SStorageAPI* pAPI, SRWLatch* lock) {
385,233✔
570
  SArray* qa = NULL;
385,233✔
571
  int32_t code = filterUnqualifiedTables(pScanInfo, tableIdList, id, pAPI, &qa);
385,233✔
572
  if (code != TSDB_CODE_SUCCESS) {
385,233✔
573
    taosArrayDestroy(qa);
×
574
    return code;
×
575
  }
576
  int32_t numOfQualifiedTables = taosArrayGetSize(qa);
385,233✔
577
  qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
385,233✔
578
  pAPI->tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
385,233✔
579

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

592
  STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
385,233✔
593
  taosWLockLatch(lock);
385,233✔
594

595
  for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
674,547✔
596
    uint64_t* uid = taosArrayGet(qa, i);
289,314✔
597
    if (!uid) {
289,314✔
598
      taosMemoryFree(keyBuf);
×
599
      taosArrayDestroy(qa);
×
600
      taosWUnLockLatch(lock);
×
601
      return terrno;
×
602
    }
603
    STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
289,314✔
604

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

620
    code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
289,314✔
621
    if (code != TSDB_CODE_SUCCESS) {
289,314✔
622
      taosMemoryFree(keyBuf);
×
623
      taosArrayDestroy(qa);
×
624
      taosWUnLockLatch(lock);
×
625
      return code;
×
626
    }
627
  }
628

629
  taosWUnLockLatch(lock);
385,233✔
630
  if (keyBuf != NULL) {
385,233✔
631
    taosMemoryFree(keyBuf);
×
632
  }
633

634
  taosArrayDestroy(qa);
385,233✔
635
  return 0;
385,233✔
636
}
637

638
int32_t qAddTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
385,233✔
639
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
385,233✔
640
  const char*    id = GET_TASKID(pTaskInfo);
385,233✔
641
  int32_t        code = 0;
385,233✔
642

643
  qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
385,233✔
644

645
  // traverse to the stream scanner node to add this table id
646
  SOperatorInfo* pInfo = NULL;
385,233✔
647
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
385,233✔
648
  if (code != 0 || pInfo == NULL) {
385,233✔
649
    return code;
×
650
  }
651

652
  SStreamScanInfo* pScanInfo = pInfo->info;
385,233✔
653
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
385,233✔
654
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
385,233✔
655
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
385,233✔
656
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
×
657
      int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
×
658

659
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
660
    }
661
  }
662

663
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
385,233✔
664
}
665

666
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
667
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
668
  const char*    id = GET_TASKID(pTaskInfo);
×
669
  int32_t        code = 0;
×
670

671
  // traverse to the stream scanner node to add this table id
672
  SOperatorInfo* pInfo = NULL;
×
673
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
×
674
  if (code != 0 || pInfo == NULL) {
×
675
    return code;
×
676
  }
677

678
  SStreamScanInfo* pScanInfo = pInfo->info;
×
679
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
680
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
×
681
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
×
682
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
×
683
      int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
×
684

685
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
686
    }
687
  }
688

689
  qDebug("%s %d remove child tables from the stream scanner, %s", __func__, (int32_t)taosArrayGetSize(tableIdList), id);
×
690
  taosWLockLatch(&pTaskInfo->lock);
×
691
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
692
  taosWUnLockLatch(&pTaskInfo->lock);
×
693
  
694
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
×
695
}
696

697
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
458,712,761✔
698
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
699
                                    int32_t idx, bool* tbGet) {
700
  *tbGet = false;
458,712,761✔
701

702
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
458,844,361✔
703
    return TSDB_CODE_INVALID_PARA;
6,823✔
704
  }
705
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
458,837,784✔
706

707
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
458,837,784✔
708
    return TSDB_CODE_SUCCESS;
270,151,278✔
709
  }
710

711
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
188,725,709✔
712
  if (!pSchemaInfo) {
188,590,923✔
713
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
714
    return terrno;
×
715
  }
716

717
  *sversion = pSchemaInfo->sw->version;
188,590,923✔
718
  *tversion = pSchemaInfo->tversion;
188,826,982✔
719
  *rversion = pSchemaInfo->rversion;
188,688,465✔
720
  if (pSchemaInfo->dbname) {
188,670,008✔
721
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
188,723,636✔
722
  } else {
723
    dbName[0] = 0;
×
724
  }
725
  if (pSchemaInfo->tablename) {
188,917,045✔
726
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
188,826,470✔
727
  } else {
728
    tableName[0] = 0;
29✔
729
  }
730

731
  *tbGet = true;
188,868,181✔
732

733
  return TSDB_CODE_SUCCESS;
188,777,475✔
734
}
735

736
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
270,052,270✔
737

738
void qDestroyOperatorParam(SOperatorParam* pParam) {
117,030✔
739
  if (NULL == pParam) {
117,030✔
740
    return;
×
741
  }
742
  freeOperatorParam(pParam, OP_GET_PARAM);
117,030✔
743
}
744

745
/**
746
  @brief Update the operator param for the task.
747
  @note  Unlike setOperatorParam, this function will destroy the new param when
748
         operator type mismatch.
749
*/
750
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
34,258,497✔
751
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
34,258,497✔
752
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
34,258,497✔
753
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
34,258,497✔
754
    qError("%s, %s operator type mismatch, task operator type:%d, "
115,620✔
755
           "new param operator type:%d", GET_TASKID(pTask), __func__,
756
           pTask->pRoot->operatorType,
757
           pNewParam->opType);
758
    qDestroyOperatorParam((SOperatorParam*)pParam);
115,620✔
759
    return;
115,620✔
760
  }
761
  TSWAP(pParam, pTask->pOpParam);
34,151,944✔
762
  ((SExecTaskInfo*)tinfo)->paramSet = false;
34,145,057✔
763
}
764

765
int32_t qExecutorInit(void) {
4,332,475✔
766
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,332,475✔
767
  return TSDB_CODE_SUCCESS;
4,332,577✔
768
}
769

770
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
2,927,143✔
771
  int32_t        code = TSDB_CODE_SUCCESS;
2,927,143✔
772
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
2,927,143✔
773
  if (pTask->pWorkerCb) {
2,927,143✔
774
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
2,925,568✔
775
    if (code != TSDB_CODE_SUCCESS) {
2,929,243✔
776
      pTask->code = code;
×
777
      return pTask->code;
×
778
    }
779
  }
780

781
  code = tsem_wait(pSem);
2,930,293✔
782
  if (code != TSDB_CODE_SUCCESS) {
2,929,243✔
783
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
784
    pTask->code = code;
×
785
    return pTask->code;
×
786
  }
787

788
  if (pTask->pWorkerCb) {
2,929,243✔
789
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,928,718✔
790
    if (code != TSDB_CODE_SUCCESS) {
2,928,718✔
791
      pTask->code = code;
×
792
      return pTask->code;
×
793
    }
794
  }
795
  return TSDB_CODE_SUCCESS;
2,928,718✔
796
}
797

798
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
272,568,768✔
799
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
800
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
801
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
272,568,768✔
802
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
272,568,768✔
803

804
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
272,578,879✔
805
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
806

807
  readHandle->uid = 0;
272,617,106✔
808
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
272,631,108✔
809
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
272,416,399✔
810
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
756,773✔
811
    goto _error;
445,536✔
812
  }
813
    
814
  if (handle) {
271,720,157✔
815
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
271,434,564✔
816
    void*           pSinkManager = NULL;
271,480,139✔
817
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
271,450,609✔
818
    if (code != TSDB_CODE_SUCCESS) {
271,379,216✔
819
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
820
      goto _error;
×
821
    }
822

823
    void* pSinkParam = NULL;
271,379,216✔
824
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
271,627,515✔
825
    if (code != TSDB_CODE_SUCCESS) {
271,368,360✔
826
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
827
      taosMemoryFree(pSinkManager);
×
828
      goto _error;
×
829
    }
830

831
    SDataSinkNode* pSink = NULL;
271,368,360✔
832
    if (readHandle->localExec) {
271,538,702✔
833
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,860✔
834
      if (code != TSDB_CODE_SUCCESS) {
2,860✔
835
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
836
               (*pTask)->id.str);
837
        taosMemoryFree(pSinkManager);
×
838
        goto _error;
×
839
      }
840
    }
841

842
    // pSinkParam has been freed during create sinker.
843
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
271,387,469✔
844
                              (*pTask)->id.str, pSubplan->processOneBlock);
271,652,439✔
845
    if (code) {
271,329,096✔
846
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
583✔
847
    }
848
  }
849

850
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
271,790,017✔
851
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
852

853
_error:
54,327,459✔
854
  // if failed to add ref for all tables in this query, abort current query
855
  return code;
272,422,020✔
856
}
857

858
static void freeBlock(void* param) {
×
859
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
860
  blockDataDestroy(pBlock);
×
861
}
×
862

863
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
320,872,659✔
864
                     bool processOneBlock) {
865
  int32_t        code = TSDB_CODE_SUCCESS;
320,872,659✔
866
  int32_t        lino = 0;
320,872,659✔
867
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
320,872,659✔
868
  int64_t        threadId = taosGetSelfPthreadId();
320,872,659✔
869

870
  if (pLocal) {
320,831,072✔
871
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
316,804,852✔
872
  }
873

874
  taosArrayClear(pResList);
320,779,029✔
875

876
  int64_t curOwner = 0;
320,729,472✔
877
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
320,729,472✔
878
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
879
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
880
    return pTaskInfo->code;
×
881
  }
882

883
  if (pTaskInfo->cost.start == 0) {
320,692,270✔
884
    pTaskInfo->cost.start = taosGetTimestampUs();
265,979,479✔
885
  }
886

887
  if (isTaskKilled(pTaskInfo)) {
320,784,728✔
888
    atomic_store_64(&pTaskInfo->owner, 0);
×
889
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
890
    return pTaskInfo->code;
×
891
  }
892

893
  // error occurs, record the error code and return to client
894
  int32_t ret = setjmp(pTaskInfo->env);
320,776,177✔
895
  if (ret != TSDB_CODE_SUCCESS) {
321,263,404✔
896
    pTaskInfo->code = ret;
666,607✔
897
    (void)cleanUpUdfs();
666,607✔
898

899
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
666,607✔
900
    atomic_store_64(&pTaskInfo->owner, 0);
666,607✔
901

902
    return pTaskInfo->code;
666,607✔
903
  }
904

905
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
320,596,797✔
906

907
  int32_t      current = 0;
320,612,861✔
908
  SSDataBlock* pRes = NULL;
320,612,861✔
909
  int64_t      st = taosGetTimestampUs();
320,799,262✔
910

911
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
320,799,262✔
912
    pTaskInfo->paramSet = true;
34,133,038✔
913
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
34,127,637✔
914
  } else {
915
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
286,597,023✔
916
  }
917

918
  QUERY_CHECK_CODE(code, lino, _end);
320,244,993✔
919
  code = blockDataCheck(pRes);
320,244,993✔
920
  QUERY_CHECK_CODE(code, lino, _end);
320,245,998✔
921

922
  if (pRes == NULL) {
320,245,998✔
923
    st = taosGetTimestampUs();
61,619,589✔
924
  }
925

926
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
320,243,608✔
927
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
320,272,855✔
928
    rowsThreshold = 4096;
319,729,743✔
929
  }
930

931
  int32_t blockIndex = 0;
320,238,925✔
932
  while (pRes != NULL) {
822,119,187✔
933
    SSDataBlock* p = NULL;
547,464,720✔
934
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
547,449,159✔
935
      SSDataBlock* p1 = NULL;
425,529,450✔
936
      code = createOneDataBlock(pRes, true, &p1);
425,538,751✔
937
      QUERY_CHECK_CODE(code, lino, _end);
425,516,549✔
938

939
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
425,516,549✔
940
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
425,544,110✔
941
      p = p1;
425,544,110✔
942
    } else if (processOneBlock) {
121,928,189✔
943
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
17,161,325✔
944
      if (tmp) {
17,161,325✔
945
        blockDataDestroy(*tmp);
17,161,325✔
946
        *tmp = NULL;
17,161,325✔
947
      }
948
      SSDataBlock* p1 = NULL;
17,161,325✔
949
      code = createOneDataBlock(pRes, true, &p1);
17,161,325✔
950
      QUERY_CHECK_CODE(code, lino, _end);
17,161,325✔
951

952
      *tmp = p1;
17,161,325✔
953
      p = p1;
17,161,325✔
954
    } else {
955
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
104,766,864✔
956
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
104,765,975✔
957

958
      p = *(SSDataBlock**)tmp;
104,765,975✔
959
      code = copyDataBlock(p, pRes);
104,766,865✔
960
      QUERY_CHECK_CODE(code, lino, _end);
104,766,372✔
961
    }
962

963
    blockIndex += 1;
547,433,495✔
964

965
    current += p->info.rows;
547,433,495✔
966
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
547,463,743✔
967
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
968
    void* tmp = taosArrayPush(pResList, &p);
547,466,143✔
969
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
547,466,143✔
970

971
    if (current >= rowsThreshold || processOneBlock) {
547,466,143✔
972
      break;
973
    }
974

975
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
501,868,175✔
976
    QUERY_CHECK_CODE(code, lino, _end);
501,865,797✔
977
    code = blockDataCheck(pRes);
501,865,797✔
978
    QUERY_CHECK_CODE(code, lino, _end);
501,889,730✔
979
  }
980

981
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
320,252,435✔
982
    pTaskInfo->pSubplan->rowsThreshold -= current;
516,438✔
983
  }
984

985
  *hasMore = (pRes != NULL);
320,273,492✔
986
  uint64_t el = (taosGetTimestampUs() - st);
320,252,752✔
987

988
  pTaskInfo->cost.elapsedTime += el;
320,252,752✔
989
  if (NULL == pRes) {
320,234,471✔
990
    *useconds = pTaskInfo->cost.elapsedTime;
274,635,929✔
991
  }
992

993
_end:
320,260,825✔
994
  (void)cleanUpUdfs();
320,258,848✔
995

996
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
320,303,992✔
997
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
320,303,992✔
998
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
999

1000
  atomic_store_64(&pTaskInfo->owner, 0);
320,304,113✔
1001
  if (code) {
320,303,497✔
1002
    pTaskInfo->code = code;
×
1003
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1004
  }
1005

1006
  return pTaskInfo->code;
320,303,497✔
1007
}
1008

1009
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
1010
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1011
  SArray*        pList = pTaskInfo->pResultBlockList;
×
1012
  size_t         num = taosArrayGetSize(pList);
×
1013
  for (int32_t i = 0; i < num; ++i) {
×
1014
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
1015
    if (p) {
×
1016
      blockDataDestroy(*p);
×
1017
    }
1018
  }
1019

1020
  taosArrayClear(pTaskInfo->pResultBlockList);
×
1021
}
×
1022

1023
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
121,752,123✔
1024
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
121,752,123✔
1025
  int64_t        threadId = taosGetSelfPthreadId();
121,752,123✔
1026
  int64_t        curOwner = 0;
121,752,123✔
1027

1028
  *pRes = NULL;
121,752,123✔
1029

1030
  // todo extract method
1031
  taosRLockLatch(&pTaskInfo->lock);
121,752,468✔
1032
  bool isKilled = isTaskKilled(pTaskInfo);
121,752,118✔
1033
  if (isKilled) {
121,751,794✔
1034
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1035

1036
    taosRUnLockLatch(&pTaskInfo->lock);
×
1037
    return pTaskInfo->code;
×
1038
  }
1039

1040
  if (pTaskInfo->owner != 0) {
121,751,794✔
1041
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1042
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1043

1044
    taosRUnLockLatch(&pTaskInfo->lock);
×
1045
    return pTaskInfo->code;
×
1046
  }
1047

1048
  pTaskInfo->owner = threadId;
121,752,139✔
1049
  taosRUnLockLatch(&pTaskInfo->lock);
121,752,139✔
1050

1051
  if (pTaskInfo->cost.start == 0) {
121,752,808✔
1052
    pTaskInfo->cost.start = taosGetTimestampUs();
231,489✔
1053
  }
1054

1055
  // error occurs, record the error code and return to client
1056
  int32_t ret = setjmp(pTaskInfo->env);
121,752,808✔
1057
  if (ret != TSDB_CODE_SUCCESS) {
121,748,357✔
1058
    pTaskInfo->code = ret;
×
1059
    (void)cleanUpUdfs();
×
1060
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
1061
    atomic_store_64(&pTaskInfo->owner, 0);
×
1062
    return pTaskInfo->code;
×
1063
  }
1064

1065
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
121,748,357✔
1066

1067
  int64_t st = taosGetTimestampUs();
121,748,664✔
1068
  int32_t code = TSDB_CODE_SUCCESS;
121,748,664✔
1069
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
121,748,664✔
1070
    pTaskInfo->paramSet = true;
×
1071
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1072
  } else {
1073
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
121,750,044✔
1074
  }
1075
  if (code) {
121,693,164✔
1076
    pTaskInfo->code = code;
×
1077
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1078
  }
1079

1080
  code = blockDataCheck(*pRes);
121,693,164✔
1081
  if (code) {
121,747,189✔
1082
    pTaskInfo->code = code;
×
1083
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1084
  }
1085

1086
  uint64_t el = (taosGetTimestampUs() - st);
121,719,061✔
1087

1088
  pTaskInfo->cost.elapsedTime += el;
121,719,061✔
1089
  if (NULL == *pRes) {
121,728,195✔
1090
    *useconds = pTaskInfo->cost.elapsedTime;
21,428,780✔
1091
  }
1092

1093
  (void)cleanUpUdfs();
121,732,876✔
1094

1095
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
121,753,158✔
1096
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
121,753,158✔
1097

1098
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
121,752,813✔
1099
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1100

1101
  atomic_store_64(&pTaskInfo->owner, 0);
121,753,129✔
1102
  return pTaskInfo->code;
121,752,464✔
1103
}
1104

1105
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
102,134,398✔
1106
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
102,134,398✔
1107
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
102,136,259✔
1108
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
102,136,633✔
1109

1110
  if (!tmp) {
102,134,398✔
1111
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1112
    return terrno;
×
1113
  }
1114
  return TSDB_CODE_SUCCESS;
102,134,398✔
1115
}
1116

1117
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1118
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1119
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1120

1121
  if (key->refId < pInfo->refId) {
×
1122
    return -1;
×
1123
  } else if (key->refId > pInfo->refId) {
×
1124
    return 1;
×
1125
  }
1126

1127
  return 0;
×
1128
}
1129

1130
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1131
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1132
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1133
  if (idx >= 0) {
×
1134
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1135
  }
1136
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1137
}
×
1138

1139
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
75,845✔
1140
  if (pTaskInfo->subJobCtx.hasSubJobs) {
75,845✔
1141
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
24,150✔
1142
    if (pTaskInfo->subJobCtx.param) {
24,150✔
1143
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
12,600✔
1144
    }
1145
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
24,150✔
1146
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
24,150✔
1147
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
24,150✔
1148
  }
1149
  
1150
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
75,845✔
1151

1152
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
75,845✔
1153
  for (int32_t i = 0; i < num; ++i) {
78,259✔
1154
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
2,414✔
1155
    if (!pStop) {
2,414✔
1156
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1157
      continue;
×
1158
    }
1159
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
2,414✔
1160
    if (pExchangeInfo) {
2,414✔
1161
      int32_t code = tsem_post(&pExchangeInfo->ready);
2,414✔
1162
      if (code != TSDB_CODE_SUCCESS) {
2,414✔
1163
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1164
      } else {
1165
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
2,414✔
1166
      }
1167
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
2,414✔
1168
      if (code != TSDB_CODE_SUCCESS) {
2,414✔
1169
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1170
      }
1171
    }
1172
  }
1173

1174
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
75,845✔
1175
}
75,845✔
1176

1177
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
75,845✔
1178
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
75,845✔
1179
  if (pTaskInfo == NULL) {
75,845✔
1180
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1181
  }
1182

1183
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
75,845✔
1184

1185
  setTaskKilled(pTaskInfo, rspCode);
75,845✔
1186
  qStopTaskOperators(pTaskInfo);
75,845✔
1187

1188
  return TSDB_CODE_SUCCESS;
75,845✔
1189
}
1190

1191
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1192
  int64_t        st = taosGetTimestampMs();
×
1193
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1194
  if (pTaskInfo == NULL) {
×
1195
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1196
  }
1197

1198
  if (waitDuration > 0) {
×
1199
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1200
  } else {
1201
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1202
  }
1203

1204
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1205

1206
  if (waitDuration > 0) {
×
1207
    while (1) {
1208
      taosWLockLatch(&pTaskInfo->lock);
×
1209
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1210
        taosWUnLockLatch(&pTaskInfo->lock);
×
1211

1212
        taosMsleep(200);
×
1213

1214
        int64_t d = taosGetTimestampMs() - st;
×
1215
        if (d >= waitDuration && waitDuration >= 0) {
×
1216
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1217
          return TSDB_CODE_SUCCESS;
×
1218
        }
1219
      } else {  // not running now
1220
        pTaskInfo->code = rspCode;
×
1221
        taosWUnLockLatch(&pTaskInfo->lock);
×
1222
        return TSDB_CODE_SUCCESS;
×
1223
      }
1224
    }
1225
  }
1226

1227
  int64_t et = taosGetTimestampMs() - st;
×
1228
  if (et < waitDuration) {
×
1229
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1230
    return TSDB_CODE_SUCCESS;
×
1231
  }
1232
  return TSDB_CODE_SUCCESS;
×
1233
}
1234

1235
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1236
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1237
  if (NULL == pTaskInfo) {
×
1238
    return false;
×
1239
  }
1240

1241
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1242
}
1243

1244
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
272,826,587✔
1245
  STaskCostInfo* pSummary = &pTaskInfo->cost;
272,826,587✔
1246
  int64_t        idleTime = pSummary->start - pSummary->created;
272,837,501✔
1247

1248
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
272,814,723✔
1249
  if (pSummary->pRecoder != NULL) {
272,789,019✔
1250
    qDebug(
187,921,883✔
1251
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1252
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1253
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1254
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1255
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1256
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1257
  } else {
1258
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
84,882,658✔
1259
           pSummary->elapsedTime / 1000.0);
1260
  }
1261
}
272,805,425✔
1262

1263

1264
void qDestroyTask(qTaskInfo_t qTaskHandle) {
279,408,162✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
279,408,162✔
1266
  if (pTaskInfo == NULL) {
279,408,162✔
1267
    return;
6,584,165✔
1268
  }
1269

1270
  if (pTaskInfo->pRoot != NULL) {
272,823,997✔
1271
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
272,829,701✔
1272
  } else {
1273
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1274
  }
1275

1276
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
272,830,393✔
1277
  doDestroyTask(pTaskInfo);
272,823,405✔
1278
}
1279

1280
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,651,684✔
1281
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,651,684✔
1282
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,651,684✔
1283
}
1284

1285
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
327,394✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
327,394✔
1287
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
327,394✔
1288

1289
  while (1) {
321,360✔
1290
    uint16_t type = pOperator->operatorType;
648,754✔
1291
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
648,754✔
1292
      *scanner = pOperator->info;
327,394✔
1293
      break;
327,394✔
1294
    } else {
1295
      pOperator = pOperator->pDownstream[0];
321,360✔
1296
    }
1297
  }
1298
}
327,394✔
1299

1300
void* qExtractReaderFromTmqScanner(void* scanner) {
327,394✔
1301
  SStreamScanInfo* pInfo = scanner;
327,394✔
1302
  return (void*)pInfo->tqReader;
327,394✔
1303
}
1304

1305
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
744,823✔
1306
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
744,823✔
1307
  return pTaskInfo->streamInfo.schema;
744,823✔
1308
}
1309

1310
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
744,823✔
1311
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
744,823✔
1312
  return pTaskInfo->streamInfo.tbName;
744,823✔
1313
}
1314

1315
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
787,808✔
1316
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
787,808✔
1317
  return &pTaskInfo->streamInfo.btMetaRsp;
787,808✔
1318
}
1319

1320
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
22,451,798✔
1321
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,451,798✔
1322
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
22,451,798✔
1323
  return 0;
22,451,798✔
1324
  /*if (code != TSDB_CODE_SUCCESS) {
1325
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1326
    pTaskInfo->code = code;
1327
    T_LONG_JMP(pTaskInfo->env, code);
1328
  }*/
1329
}
1330

1331
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
749,836✔
1332
  memset(pCond, 0, sizeof(SQueryTableDataCond));
749,836✔
1333
  pCond->order = TSDB_ORDER_ASC;
749,836✔
1334
  pCond->numOfCols = pMtInfo->schema->nCols;
750,119✔
1335
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
750,430✔
1336
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
750,430✔
1337
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
750,727✔
1338
    taosMemoryFreeClear(pCond->colList);
×
1339
    taosMemoryFreeClear(pCond->pSlotList);
×
1340
    return terrno;
×
1341
  }
1342

1343
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
750,430✔
1344
  pCond->suid = pMtInfo->suid;
750,727✔
1345
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
750,430✔
1346
  pCond->startVersion = -1;
750,727✔
1347
  pCond->endVersion = sContext->snapVersion;
750,119✔
1348

1349
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,743,026✔
1350
    SColumnInfo* pColInfo = &pCond->colList[i];
3,992,624✔
1351
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,992,610✔
1352
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,992,313✔
1353
    if (pMtInfo->pExtSchemas != NULL) {
3,991,733✔
1354
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,640✔
1355
    }
1356
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,992,610✔
1357
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,992,327✔
1358

1359
    pCond->pSlotList[i] = i;
3,992,313✔
1360
  }
1361

1362
  return TSDB_CODE_SUCCESS;
750,727✔
1363
}
1364

1365
void qStreamSetOpen(qTaskInfo_t tinfo) {
120,221,433✔
1366
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
120,221,433✔
1367
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
120,221,433✔
1368
  pOperator->status = OP_NOT_OPENED;
120,241,961✔
1369
}
120,241,040✔
1370

1371
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
21,643,809✔
1372
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,643,809✔
1373
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
21,643,809✔
1374
}
21,645,825✔
1375

1376
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
22,635,705✔
1377
  int32_t        code = TSDB_CODE_SUCCESS;
22,635,705✔
1378
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,635,705✔
1379
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
22,635,705✔
1380

1381
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,637,347✔
1382
  const char*    id = GET_TASKID(pTaskInfo);
22,636,722✔
1383

1384
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,635,087✔
1385
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
21,343,238✔
1386
    if (pOperator == NULL || code != 0) {
21,342,699✔
1387
      return code;
49✔
1388
    }
1389

1390
    SStreamScanInfo* pInfo = pOperator->info;
21,342,650✔
1391
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
21,343,643✔
1392
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
21,341,617✔
1393
    walReaderVerifyOffset(pWalReader, pOffset);
21,344,642✔
1394
  }
1395
  // if pOffset equal to current offset, means continue consume
1396
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,635,705✔
1397
    return 0;
20,766,739✔
1398
  }
1399

1400
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,867,312✔
1401
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,101,492✔
1402
    if (pOperator == NULL || code != 0) {
1,101,201✔
1403
      return code;
×
1404
    }
1405

1406
    SStreamScanInfo* pInfo = pOperator->info;
1,101,201✔
1407
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,100,848✔
1408
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,100,910✔
1409
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,100,290✔
1410

1411
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,100,290✔
1412
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
876,367✔
1413
      pScanBaseInfo->dataReader = NULL;
875,800✔
1414

1415
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
875,800✔
1416
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
875,800✔
1417
      walReaderVerifyOffset(pWalReader, pOffset);
875,688✔
1418
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
876,367✔
1419
      if (code < 0) {
876,367✔
1420
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
12,648✔
1421
        return code;
12,648✔
1422
      }
1423
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
224,168✔
1424
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1425
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1426
      int64_t uid = pOffset->uid;
224,806✔
1427
      int64_t ts = pOffset->ts;
225,125✔
1428
      int32_t index = 0;
223,895✔
1429

1430
      // this value may be changed if new tables are created
1431
      taosRLockLatch(&pTaskInfo->lock);
223,895✔
1432
      int32_t numOfTables = 0;
224,487✔
1433
      code = tableListGetSize(pTableListInfo, &numOfTables);
224,487✔
1434
      if (code != TSDB_CODE_SUCCESS) {
225,125✔
1435
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1436
        taosRUnLockLatch(&pTaskInfo->lock);
×
1437
        return code;
×
1438
      }
1439

1440
      if (uid == 0) {
225,125✔
1441
        if (numOfTables != 0) {
216,706✔
1442
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
39,658✔
1443
          if (!tmp) {
39,658✔
1444
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1445
            taosRUnLockLatch(&pTaskInfo->lock);
×
1446
            return terrno;
×
1447
          }
1448
          if (tmp) uid = tmp->uid;
39,658✔
1449
          ts = INT64_MIN;
39,658✔
1450
          pScanInfo->currentTable = 0;
39,658✔
1451
        } else {
1452
          taosRUnLockLatch(&pTaskInfo->lock);
177,048✔
1453
          qError("no table in table list, %s", id);
177,056✔
1454
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
177,367✔
1455
        }
1456
      }
1457
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
48,077✔
1458

1459
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
47,758✔
1460
             pInfo->pTableScanOp->resultInfo.totalRows);
1461
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
47,758✔
1462

1463
      // start from current accessed position
1464
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1465
      // position, let's find it from the beginning.
1466
      index = tableListFind(pTableListInfo, uid, 0);
47,758✔
1467
      taosRUnLockLatch(&pTaskInfo->lock);
47,758✔
1468

1469
      if (index >= 0) {
47,758✔
1470
        pScanInfo->currentTable = index;
47,758✔
1471
      } else {
1472
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1473
               numOfTables, pScanInfo->currentTable, id);
1474
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1475
      }
1476

1477
      STableKeyInfo keyInfo = {.uid = uid};
47,758✔
1478
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
47,758✔
1479

1480
      // let's start from the next ts that returned to consumer.
1481
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
47,758✔
1482
        pScanBaseInfo->cond.twindows.skey = ts;
911✔
1483
      } else {
1484
        pScanBaseInfo->cond.twindows.skey = ts + 1;
46,847✔
1485
      }
1486
      pScanInfo->scanTimes = 0;
47,758✔
1487

1488
      if (pScanBaseInfo->dataReader == NULL) {
47,758✔
1489
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
81,110✔
1490
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1491
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
40,555✔
1492
        if (code != TSDB_CODE_SUCCESS) {
40,555✔
1493
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1494
          return code;
×
1495
        }
1496

1497
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
40,555✔
1498
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1499
      } else {
1500
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
7,203✔
1501
        if (code != TSDB_CODE_SUCCESS) {
7,203✔
1502
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1503
          return code;
×
1504
        }
1505

1506
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
7,203✔
1507
        if (code != TSDB_CODE_SUCCESS) {
7,203✔
1508
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1509
          return code;
×
1510
        }
1511
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
7,203✔
1512
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1513
      }
1514

1515
      // restore the key value
1516
      pScanBaseInfo->cond.twindows.skey = oldSkey;
47,758✔
1517
    } else {
1518
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1519
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1520
    }
1521

1522
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1523
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
765,820✔
1524
      SStreamRawScanInfo* pInfo = pOperator->info;
751,577✔
1525
      SSnapContext*       sContext = pInfo->sContext;
751,577✔
1526
      SOperatorInfo*      p = NULL;
751,577✔
1527

1528
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
751,577✔
1529
      if (code != 0) {
751,577✔
1530
        return code;
×
1531
      }
1532

1533
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
751,577✔
1534

1535
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
751,577✔
1536
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1537
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1538
      }
1539

1540
      SMetaTableInfo mtInfo = {0};
751,577✔
1541
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
751,577✔
1542
      if (code != 0) {
751,577✔
1543
        destroyMetaTableInfo(&mtInfo);
1544
        return code;
×
1545
      }
1546
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
751,577✔
1547
      pInfo->dataReader = NULL;
751,266✔
1548

1549
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
751,577✔
1550
      tableListClear(pTableListInfo);
751,266✔
1551

1552
      if (mtInfo.uid == 0) {
751,577✔
1553
        destroyMetaTableInfo(&mtInfo);
1554
        goto end;  // no data
850✔
1555
      }
1556

1557
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
750,727✔
1558
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
750,727✔
1559
      if (code != TSDB_CODE_SUCCESS) {
750,727✔
1560
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1561
        destroyMetaTableInfo(&mtInfo);
1562
        return code;
×
1563
      }
1564
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
750,727✔
1565
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,301✔
1566
      } else {
1567
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
748,818✔
1568
      }
1569

1570
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
750,416✔
1571
      if (code != TSDB_CODE_SUCCESS) {
750,727✔
1572
        destroyMetaTableInfo(&mtInfo);
1573
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1574
        return code;
×
1575
      }
1576

1577
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
750,727✔
1578
      if (!pList) {
750,727✔
1579
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1580
        destroyMetaTableInfo(&mtInfo);
1581
        return code;
×
1582
      }
1583
      int32_t size = 0;
750,727✔
1584
      code = tableListGetSize(pTableListInfo, &size);
750,727✔
1585
      if (code != TSDB_CODE_SUCCESS) {
750,727✔
1586
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1587
        destroyMetaTableInfo(&mtInfo);
1588
        return code;
×
1589
      }
1590

1591
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,501,454✔
1592
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
750,727✔
1593
      if (code != TSDB_CODE_SUCCESS) {
750,727✔
1594
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1595
        destroyMetaTableInfo(&mtInfo);
1596
        return code;
×
1597
      }
1598

1599
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
750,727✔
1600
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
750,727✔
1601
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1602
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
750,119✔
1603
      pTaskInfo->streamInfo.schema = mtInfo.schema;
750,727✔
1604
      taosMemoryFreeClear(mtInfo.pExtSchemas);
750,416✔
1605

1606
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
750,416✔
1607
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
14,243✔
1608
      SStreamRawScanInfo* pInfo = pOperator->info;
4,828✔
1609
      SSnapContext*       sContext = pInfo->sContext;
4,828✔
1610
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,828✔
1611
      if (code != 0) {
4,828✔
1612
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1613
        return code;
×
1614
      }
1615
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,828✔
1616
             id);
1617
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,415✔
1618
      SStreamRawScanInfo* pInfo = pOperator->info;
9,415✔
1619
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,415✔
1620
      pInfo->dataReader = NULL;
9,415✔
1621
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,415✔
1622
    }
1623
  }
1624

1625
end:
1,677,193✔
1626
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,677,297✔
1627
  return 0;
1,677,297✔
1628
}
1629

1630
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
223,520,868✔
1631
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
223,520,868✔
1632
  if (pMsg->info.ahandle == NULL) {
223,566,520✔
1633
    rpcFreeCont(pMsg->pCont);
1,151✔
1634
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1,151✔
1635
    return;
1,151✔
1636
  }
1637

1638
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
223,457,235✔
1639
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1640

1641
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
223,472,343✔
1642

1643
  if (pMsg->contLen > 0) {
223,533,829✔
1644
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
223,462,977✔
1645
    if (buf.pData == NULL) {
223,458,323✔
1646
      pMsg->code = terrno;
×
1647
    } else {
1648
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
223,458,323✔
1649
    }
1650
  }
1651

1652
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
223,565,888✔
1653
  rpcFreeCont(pMsg->pCont);
223,595,322✔
1654
  destroySendMsgInfo(pSendInfo);
223,545,844✔
1655
}
1656

1657
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1658
  int32_t        code = TSDB_CODE_SUCCESS;
×
1659
  int32_t        lino = 0;
×
1660
  SExecTaskInfo* pTaskInfo = tinfo;
×
1661
  SArray*        plist = NULL;
×
1662

1663
  code = getTableListInfo(pTaskInfo, &plist);
×
1664
  if (code || plist == NULL) {
×
1665
    return NULL;
×
1666
  }
1667

1668
  // only extract table in the first elements
1669
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1670

1671
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1672
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1673

1674
  int32_t numOfTables = 0;
×
1675
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1676
  QUERY_CHECK_CODE(code, lino, _end);
×
1677

1678
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1679
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1680
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1681
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1682
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1683
  }
1684

1685
  taosArrayDestroy(plist);
×
1686

1687
_end:
×
1688
  if (code != TSDB_CODE_SUCCESS) {
×
1689
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1690
    T_LONG_JMP(pTaskInfo->env, code);
×
1691
  }
1692
  return pUidList;
×
1693
}
1694

1695
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,389,328✔
1696
  int32_t        code = TSDB_CODE_SUCCESS;
3,389,328✔
1697
  int32_t        lino = 0;
3,389,328✔
1698
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,389,328✔
1699

1700
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,390,420✔
1701
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1702
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1703

1704
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1705
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1706
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,389,874✔
1707
    STableScanInfo* pScanInfo = pOperator->info;
1,694,393✔
1708

1709
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,694,393✔
1710
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,695,481✔
1711
  } else {
1712
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,695,481✔
1713
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,694,393✔
1714
    }
1715
  }
1716

1717
_end:
×
1718
  if (code != TSDB_CODE_SUCCESS) {
3,389,874✔
1719
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1720
  }
1721
  return code;
3,388,782✔
1722
}
1723

1724
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,696,027✔
1725
  if (pList == NULL) {
1,696,027✔
1726
    return TSDB_CODE_INVALID_PARA;
×
1727
  }
1728

1729
  *pList = NULL;
1,696,027✔
1730
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,696,027✔
1731
  if (pArray == NULL) {
1,695,481✔
1732
    return terrno;
×
1733
  }
1734

1735
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,695,481✔
1736
  if (code == 0) {
1,694,391✔
1737
    *pList = pArray;
1,694,391✔
1738
  } else {
1739
    taosArrayDestroy(pArray);
×
1740
  }
1741
  return code;
1,694,937✔
1742
}
1743

1744
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1745
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1746
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1747
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1748
  }
1749
  return 0;
×
1750
}
1751

1752
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1753
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1754
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1755
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1756
  }
1757
  return 0;
×
1758
}
1759

1760
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1761
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1762

1763
  int32_t code = pTaskInfo->code;
×
1764
  pTaskInfo->code = 0;
×
1765
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1766
}
×
1767

1768
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1769
  int32_t code = 0;
×
1770
  return code;
×
1771
}
1772

1773
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1774
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1775
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1776
  return code;
×
1777
}
1778

1779
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
28,754,224✔
1780
  int32_t code = 0;
28,754,224✔
1781

1782
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
28,754,224✔
1783
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
28,752,462✔
1784

1785
  if (pOper->fpSet.resetStateFn) {
28,752,027✔
1786
    code = pOper->fpSet.resetStateFn(pOper);
28,752,483✔
1787
  }
1788
  pOper->status = OP_NOT_OPENED;
28,751,321✔
1789
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
49,220,123✔
1790
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,462,993✔
1791
  }
1792
  return code;
28,756,981✔
1793
}
1794

1795
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,290,414✔
1796
  int32_t        code = 0;
8,290,414✔
1797
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,290,414✔
1798
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,290,414✔
1799
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,290,414✔
1800
  code = clearStatesForOperator(pOper);
8,290,414✔
1801
  return code;
8,290,159✔
1802
}
1803

1804
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
11,468,827✔
1805
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,468,827✔
1806
  int64_t        threadId = taosGetSelfPthreadId();
11,468,827✔
1807
  int64_t        curOwner = 0;
11,468,827✔
1808

1809
  *ppRes = NULL;
11,468,827✔
1810

1811
  // todo extract method
1812
  taosRLockLatch(&pTaskInfo->lock);
11,468,572✔
1813
  bool isKilled = isTaskKilled(pTaskInfo);
11,468,827✔
1814
  if (isKilled) {
11,468,572✔
1815
    // clearStreamBlock(pTaskInfo->pRoot);
1816
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1817

1818
    taosRUnLockLatch(&pTaskInfo->lock);
×
1819
    return pTaskInfo->code;
×
1820
  }
1821

1822
  if (pTaskInfo->owner != 0) {
11,468,572✔
1823
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1824
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1825

1826
    taosRUnLockLatch(&pTaskInfo->lock);
×
1827
    return pTaskInfo->code;
×
1828
  }
1829

1830
  pTaskInfo->owner = threadId;
11,468,827✔
1831
  taosRUnLockLatch(&pTaskInfo->lock);
11,468,827✔
1832

1833
  if (pTaskInfo->cost.start == 0) {
11,468,827✔
1834
    pTaskInfo->cost.start = taosGetTimestampUs();
236,331✔
1835
  }
1836

1837
  // error occurs, record the error code and return to client
1838
  int32_t ret = setjmp(pTaskInfo->env);
11,468,827✔
1839
  if (ret != TSDB_CODE_SUCCESS) {
13,267,634✔
1840
    pTaskInfo->code = ret;
1,799,712✔
1841
    (void)cleanUpUdfs();
1,799,712✔
1842
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,799,712✔
1843
    atomic_store_64(&pTaskInfo->owner, 0);
1,799,712✔
1844
    return pTaskInfo->code;
1,799,712✔
1845
  }
1846

1847
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
11,467,922✔
1848

1849
  int64_t st = taosGetTimestampUs();
11,468,827✔
1850

1851
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,468,827✔
1852
  if (code) {
9,669,115✔
1853
    pTaskInfo->code = code;
×
1854
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1855
  } else {
1856
    *finished = *ppRes == NULL;
9,669,115✔
1857
    code = blockDataCheck(*ppRes);
9,669,115✔
1858
  }
1859
  if (code) {
9,668,895✔
1860
    pTaskInfo->code = code;
×
1861
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1862
  }
1863

1864
  uint64_t el = (taosGetTimestampUs() - st);
9,669,115✔
1865

1866
  pTaskInfo->cost.elapsedTime += el;
9,669,115✔
1867
  if (NULL == *ppRes) {
9,669,115✔
1868
    *useconds = pTaskInfo->cost.elapsedTime;
5,950,418✔
1869
  }
1870

1871
  (void)cleanUpUdfs();
9,669,115✔
1872

1873
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
9,669,115✔
1874
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,669,115✔
1875

1876
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,669,115✔
1877
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1878

1879
  atomic_store_64(&pTaskInfo->owner, 0);
9,669,115✔
1880
  return pTaskInfo->code;
9,669,115✔
1881
}
1882

1883
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1884
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1885
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1886
// }
1887

1888
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
257,528✔
1889
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1890
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1891
  int32_t code = 0;                                        
257,528✔
1892
  if (*pTableListInfo != NULL) {
257,528✔
1893
    qDebug("table list already exists, no need to create again");
×
1894
    goto end;
×
1895
  }
1896
  STableListInfo* pList = tableListCreate();
257,720✔
1897
  if (pList == NULL) {
258,124✔
1898
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1899
    code = terrno;
×
1900
    goto end;
×
1901
  }
1902

1903
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
258,124✔
1904
  SReadHandle    pHandle = {.vnode = pVnode};
258,124✔
1905
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
258,124✔
1906

1907
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
257,932✔
1908
  if (code != 0) {
258,124✔
1909
    tableListDestroy(pList);
×
1910
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1911
    goto end;
×
1912
  }
1913
  *pTableListInfo = pList;
258,124✔
1914

1915
end:
258,124✔
1916
  return 0;
258,124✔
1917
}
1918

1919
static int32_t doFilterTableByTagCond(void* pVnode, STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
31,612✔
1920
  int32_t code = doFilterByTagCond(pListInfo->idInfo.suid, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, NULL);
31,612✔
1921
  if (code != 0) {
31,612✔
1922
    return code;
×
1923
  }
1924
  int32_t numOfTables = taosArrayGetSize(pUidList);
31,612✔
1925
  for (int i = 0; i < numOfTables; i++) {
59,847✔
1926
    void* tmp = taosArrayGet(pUidList, i);
28,235✔
1927
    if (tmp == NULL) {
28,235✔
1928
      return terrno;
×
1929
    }
1930
    STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
28,235✔
1931

1932
    void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
28,235✔
1933
    if (p == NULL) {
28,235✔
1934
      return terrno;
×
1935
    }
1936
  }
1937
  return code;
31,612✔
1938
}
1939

1940
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
31,612✔
1941
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1942
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1943
  int32_t code = TSDB_CODE_SUCCESS;
31,612✔
1944
  STableListInfo* pList = tableListCreate();
31,612✔
1945
  if (pList == NULL) {
31,612✔
1946
    code = terrno;
×
1947
    goto end;
×
1948
  }
1949
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
31,612✔
1950
  if (uidListCopy == NULL) {
31,612✔
1951
    code = terrno;
×
1952
    goto end;
×
1953
  }
1954
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
31,612✔
1955
  SReadHandle    pHandle = {.vnode = pVnode};
31,612✔
1956

1957
  pList->idInfo.suid = suid;
31,612✔
1958
  pList->idInfo.tableType = TD_SUPER_TABLE;
31,612✔
1959
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
31,612✔
1960
  if (code != TSDB_CODE_SUCCESS) {
31,612✔
1961
    goto end;
×
1962
  }                                              
1963
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
31,612✔
1964
  if (code != TSDB_CODE_SUCCESS) {
31,612✔
1965
    goto end;
×
1966
  }
1967
  *tableList = pList->pTableList;
31,612✔
1968
  pList->pTableList = NULL;
31,612✔
1969

1970
  taosArrayClear(uidList);
31,612✔
1971
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
63,224✔
1972
    void* tmp = taosArrayGet(uidListCopy, i);
31,612✔
1973
    if (tmp == NULL) {
31,612✔
1974
      continue;
×
1975
    }
1976
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
31,612✔
1977
    if (slot == NULL) {
31,612✔
1978
      if (taosArrayPush(uidList, tmp) == NULL) {
3,377✔
1979
        code = terrno;
×
1980
        goto end;
×
1981
      }
1982
    }
1983
  }
1984
end:
31,612✔
1985
  taosArrayDestroy(uidListCopy);
31,612✔
1986
  tableListDestroy(pList);
31,612✔
1987
  return code;
31,612✔
1988
}
1989

1990
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1991
//   int32_t index = -1;
1992
//   (void)taosThreadRwlockRdlock(lock);
1993
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1994
//     index = 0;
1995
//     goto end;
1996
//   }
1997
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1998
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1999

2000
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
2001
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
2002
//       index = i;
2003
//       goto end;
2004
//     }
2005
//   }
2006
// end:
2007
//   (void)taosThreadRwlockUnlock(lock);
2008
//   return index;
2009
// }
2010

2011
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
258,124✔
2012
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
258,124✔
2013
  STableListInfo* pList = pTableListInfo;
258,124✔
2014
  return pList->pTableList;
258,124✔
2015
}
2016

2017
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,407,372✔
2018

2019
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,600,750✔
2020
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,600,750✔
2021
  qDestroyTask(tInfo);
2,600,750✔
2022
}
2,600,750✔
2023

2024
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
607,336✔
2025
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
607,336✔
2026
}
2027

2028
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
657,427✔
2029
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
2030
  int32_t      code = 0;
657,427✔
2031
  SNode*       pNode = 0;
657,427✔
2032
  SNodeList*   pList = NULL;
657,427✔
2033
  SExprInfo*   pExprInfo = NULL;
657,427✔
2034
  int32_t      numOfExprs = 1;
657,427✔
2035
  int32_t*     offset = 0;
657,649✔
2036
  STargetNode* pTargetNode = NULL;
657,649✔
2037
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
657,429✔
2038
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
657,649✔
2039

2040
  if (code == 0) {
657,649✔
2041
    pTargetNode->dataBlockId = 0;
657,649✔
2042
    pTargetNode->pExpr = pNode;
657,649✔
2043
    pTargetNode->slotId = 0;
657,649✔
2044
  }
2045
  if (code == 0) {
657,649✔
2046
    code = nodesMakeList(&pList);
657,649✔
2047
  }
2048
  if (code == 0) {
657,649✔
2049
    code = nodesListAppend(pList, (SNode*)pTargetNode);
657,649✔
2050
  }
2051
  if (code == 0) {
657,649✔
2052
    pNode = NULL;
657,649✔
2053
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
657,649✔
2054
  }
2055

2056
  if (code == 0) {
657,427✔
2057
    const char* pVal = NULL;
657,649✔
2058
    int32_t     len = 0;
657,649✔
2059
    SNode*      pSclNode = NULL;
657,649✔
2060
    switch (pExprInfo->pExpr->nodeType) {
657,649✔
2061
      case QUERY_NODE_FUNCTION:
657,649✔
2062
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
657,649✔
2063
        break;
657,427✔
2064
      case QUERY_NODE_OPERATOR:
×
2065
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
2066
        break;
×
2067
      default:
×
2068
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2069
        break;
×
2070
    }
2071
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
657,427✔
2072
    SSDataBlock block = {0};
657,649✔
2073
    block.info.rows = 1;
657,649✔
2074
    SSDataBlock* pBlock = &block;
657,649✔
2075
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
657,649✔
2076
    if (tmp == NULL) {
657,649✔
2077
      code = terrno;
×
2078
    }
2079
    if (code == 0) {
657,649✔
2080
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
657,649✔
2081
      gTaskScalarExtra.pStreamRange = NULL;
657,649✔
2082
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
657,649✔
2083
    }
2084
    taosArrayDestroy(pBlockList);
656,848✔
2085
  }
2086
  nodesDestroyList(pList);
656,846✔
2087
  destroyExprInfo(pExprInfo, numOfExprs);
656,985✔
2088
  taosMemoryFreeClear(pExprInfo);
657,205✔
2089
  return code;
656,793✔
2090
}
2091

2092
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,236,042✔
2093
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,236,042✔
2094
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,236,042✔
2095
  int32_t        code = 0;
2,236,042✔
2096
  SNode*         pNode = NULL;
2,236,042✔
2097
  if (!pForceOutputCols) return 0;
2,236,042✔
2098
  if (!*pRes) {
49,871✔
2099
    code = createDataBlock(pRes);
49,871✔
2100
  }
2101

2102
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
49,871✔
2103
    int32_t idx = 0;
49,871✔
2104
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
199,293✔
2105
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
149,422✔
2106
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
149,422✔
2107
      colInfo.info.precision = pCol->type.precision;
149,422✔
2108
      colInfo.info.scale = pCol->type.scale;
149,422✔
2109
      code = blockDataAppendColInfo(*pRes, &colInfo);
149,422✔
2110
      if (code != 0) break;
149,422✔
2111
    }
2112
  }
2113

2114
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
49,871✔
2115
  if (code != TSDB_CODE_SUCCESS) {
49,871✔
2116
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2117
    return code;
×
2118
  }
2119

2120
  // loop all exprs for force output, execute all exprs
2121
  int32_t idx = 0;
49,871✔
2122
  int32_t rowIdx = (*pRes)->info.rows;
49,871✔
2123
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
49,871✔
2124
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
49,871✔
2125
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
199,293✔
2126
    SScalarParam   dst = {0};
149,422✔
2127
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
149,422✔
2128
    code = nodesStringToNode(pCol->expr, &pNode);
149,422✔
2129
    if (code != 0) break;
149,422✔
2130
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
149,422✔
2131
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
149,422✔
2132
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
99,551✔
2133
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
99,551✔
2134
    } else {
2135
      dst.columnData = pInfo;
49,871✔
2136
      dst.numOfRows = rowIdx;
49,871✔
2137
      dst.colAlloced = false;
49,871✔
2138
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
49,871✔
2139
    }
2140
    ++idx;
149,422✔
2141
    // TODO sclFreeParam(&dst);
2142
    nodesDestroyNode(pNode);
149,422✔
2143
    if (code != 0) break;
149,422✔
2144
  }
2145
  if (code == TSDB_CODE_SUCCESS) {
49,871✔
2146
    (*pRes)->info.rows++;
49,871✔
2147
  }
2148
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
49,871✔
2149
  return code;
49,871✔
2150
}
2151

2152
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
235,945✔
2153
  int32_t      code = 0;
235,945✔
2154
  const char*  pVal = NULL;
235,945✔
2155
  SScalarParam dst = {0};
235,945✔
2156
  int32_t      len = 0;
235,945✔
2157
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
235,945✔
2158
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
235,945✔
2159
  // execute the expr
2160
  switch (pExpr->type) {
235,945✔
2161
    case QUERY_NODE_VALUE: {
×
2162
      SValueNode* pValue = (SValueNode*)pExpr;
×
2163
      int32_t     type = pValue->node.resType.type;
×
2164
      if (!IS_STR_DATA_TYPE(type)) {
×
2165
        qError("invalid sub tb expr with non-str type");
×
2166
        code = TSDB_CODE_INVALID_PARA;
×
2167
        break;
×
2168
      }
2169
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2170
      if (pTmp == NULL) {
×
2171
        qError("invalid sub tb expr with null value");
×
2172
        code = TSDB_CODE_INVALID_PARA;
×
2173
        break;
×
2174
      }
2175
      pVal = varDataVal(pTmp);
×
2176
      len = varDataLen(pTmp);
×
2177
    } break;
×
2178
    case QUERY_NODE_FUNCTION: {
235,945✔
2179
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
235,945✔
2180
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
235,945✔
2181
        qError("invalid sub tb expr with non-str type func");
×
2182
        code = TSDB_CODE_INVALID_PARA;
×
2183
        break;
×
2184
      }
2185
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
235,945✔
2186
      if (!pCol) {
235,774✔
2187
        code = terrno;
×
2188
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2189
        break;
×
2190
      }
2191

2192
      pCol->hasNull = true;
235,774✔
2193
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
235,774✔
2194
      pCol->info.colId = 0;
235,774✔
2195
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
235,774✔
2196
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
235,774✔
2197
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
235,554✔
2198
      code = colInfoDataEnsureCapacity(pCol, 1, true);
235,554✔
2199
      if (code != 0) {
235,554✔
2200
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2201
        taosMemoryFree(pCol);
×
2202
        break;
×
2203
      }
2204
      dst.columnData = pCol;
235,554✔
2205
      dst.numOfRows = 1;
235,554✔
2206
      dst.colAlloced = true;
235,554✔
2207
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
235,554✔
2208
      if (colDataIsNull_var(dst.columnData, 0)) {
235,757✔
2209
        qInfo("invalid sub tb expr with null value");
2,973✔
2210
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,565✔
2211
      }
2212
      if (code == 0) {
235,757✔
2213
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
233,192✔
2214
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
233,380✔
2215
      }
2216
    } break;
235,945✔
2217
    default:
×
2218
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2219
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2220
      break;
×
2221
  }
2222
  if (code == 0) {
235,945✔
2223
    if (!pVal || len == 0) {
233,192✔
2224
      qError("tbname generated with no characters which is not allowed");
×
2225
      code = TSDB_CODE_INVALID_PARA;
×
2226
    }
2227
    if(len > TSDB_TABLE_NAME_LEN - 1) {
233,192✔
2228
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
444✔
2229
      len = TSDB_TABLE_NAME_LEN - 1;
444✔
2230
    }
2231

2232
    memcpy(tbname, pVal, len);
233,192✔
2233
    tbname[len] = '\0';  // ensure null terminated
233,192✔
2234
    if (NULL != strchr(tbname, '.')) {
233,380✔
2235
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2236
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2237
    }
2238
  }
2239
  // TODO free dst
2240
  sclFreeParam(&dst);
236,133✔
2241
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
235,945✔
2242
  return code;
235,945✔
2243
}
2244

2245
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
229,425✔
2246
  if (pParam) {
229,425✔
2247
    if (pParam->tbname) {
229,425✔
2248
      taosMemFree(pParam->tbname);
229,425✔
2249
      pParam->tbname = NULL;
229,425✔
2250
    }
2251
    if (pParam->stbname) {
229,425✔
2252
      taosMemFree(pParam->stbname);
229,425✔
2253
      pParam->stbname = NULL;
229,425✔
2254
    }
2255
    if (pParam->dbFName) {
229,425✔
2256
      taosMemFree(pParam->dbFName);
229,425✔
2257
      pParam->dbFName = NULL;
229,425✔
2258
    }
2259
    if (pParam->pFields) {
229,425✔
2260
      taosArrayDestroy(pParam->pFields);
229,425✔
2261
      pParam->pFields = NULL;
229,425✔
2262
    }
2263
    if (pParam->pTagFields) {
229,425✔
2264
      taosArrayDestroy(pParam->pTagFields);
129,116✔
2265
      pParam->pTagFields = NULL;
129,116✔
2266
    }
2267
    if (pParam->colCids) {
229,425✔
2268
      taosArrayDestroy(pParam->colCids);
1,778✔
2269
      pParam->colCids = NULL;
1,778✔
2270
    }
2271
    if (pParam->tagCids) {
229,425✔
2272
      taosArrayDestroy(pParam->tagCids);
1,142✔
2273
      pParam->tagCids = NULL;
1,142✔
2274
    }
2275
    taosMemFree(pParam);
229,425✔
2276
  }
2277
}
229,425✔
2278

2279
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
229,425✔
2280
  int32_t code = 0, lino = 0;
229,425✔
2281
  if (ppDst == NULL || pSrc == NULL) {
229,425✔
2282
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2283
  }
2284
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
229,425✔
2285
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
229,425✔
2286

2287
  (*ppDst)->suid = pSrc->suid;
229,425✔
2288
  (*ppDst)->sver = pSrc->sver;
229,425✔
2289
  (*ppDst)->tbType = pSrc->tbType;
229,425✔
2290
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
229,425✔
2291
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
229,425✔
2292

2293
  if (pSrc->stbname) {
229,425✔
2294
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
229,425✔
2295
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
229,425✔
2296
  }
2297

2298
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
229,425✔
2299
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
229,425✔
2300

2301
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
229,425✔
2302

2303
  if (pSrc->pFields && pSrc->pFields->size > 0) {
229,425✔
2304
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
229,425✔
2305
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
229,425✔
2306
  } else {
2307
    (*ppDst)->pFields = NULL;
×
2308
  }
2309
  
2310
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
229,425✔
2311
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
129,116✔
2312
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
129,116✔
2313
  } else {
2314
    (*ppDst)->pTagFields = NULL;
100,309✔
2315
  }
2316

2317
  if (pSrc->colCids && pSrc->colCids->size > 0) {
229,425✔
2318
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,778✔
2319
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,778✔
2320
  } else {
2321
    (*ppDst)->colCids = NULL;
227,647✔
2322
  }
2323

2324
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
229,425✔
2325
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,142✔
2326
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,142✔
2327
  } else {
2328
    (*ppDst)->tagCids = NULL;
228,283✔
2329
  }
2330

2331
_exit:
229,425✔
2332

2333
  if (code != 0) {
229,425✔
2334
    if (*ppDst) {
×
2335
      destroyStreamInserterParam(*ppDst);
×
2336
      *ppDst = NULL;
×
2337
    }
2338
    
2339
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2340
  }
2341
  return code;
229,425✔
2342
}
2343

2344
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
252✔
2345
  return doDropStreamTable(pMsgCb, pOutput, pReq);
252✔
2346
}
2347

2348
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2349
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2350
}
2351

2352
int32_t qFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
5,564✔
2353
  int32_t         code = TSDB_CODE_SUCCESS;
5,564✔
2354

2355
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
5,564✔
2356
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
5,564✔
2357
  if (code != TSDB_CODE_SUCCESS) {
5,564✔
2358
    goto end;
×
2359
  }
2360
end:
5,564✔
2361
  return code;
5,564✔
2362
}
2363

2364
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2365
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2366
    return true;
2,147,483,647✔
2367
  }
2368

2369
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2370
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2371
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2372
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2373
      return durationSatisfied;
2,147,483,647✔
2374
    case TRUE_FOR_COUNT_ONLY:
58,624✔
2375
      return countSatisfied;
58,624✔
2376
    case TRUE_FOR_AND:
58,624✔
2377
      return durationSatisfied && countSatisfied;
58,624✔
2378
    case TRUE_FOR_OR:
58,624✔
2379
      return durationSatisfied || countSatisfied;
58,624✔
2380
    default:
×
2381
      return true;
×
2382
  }
2383
}
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