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

taosdata / TDengine / #4956

09 Feb 2026 01:16AM UTC coverage: 66.85% (-0.03%) from 66.884%
#4956

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

205696 of 307696 relevant lines covered (66.85%)

127754527.22 hits per line

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

68.46
/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) {
597,803,230✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
597,803,230✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
597,803,230✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
597,849,310✔
47
}
597,693,696✔
48

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

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

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

70
static void initRefPool() {
551,440✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
551,440✔
72
  (void)atexit(cleanupRefPool);
551,440✔
73
}
551,440✔
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) {
51,706,298✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
51,706,298✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
51,707,180✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
23,611,721✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
23,611,407✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
23,611,407✔
162
      if (pScanInfo->base.dataReader != NULL) {
23,610,779✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
268,838✔
164
        if (code) {
268,838✔
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);
28,094,947✔
172
  }
173

174
  return 0;
27,505,335✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
27,505,905✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
27,505,905✔
179
  pTaskInfo->id.queryId = queryId;
27,505,905✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
27,506,567✔
181

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

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

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

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

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

248
  return pTaskInfo;
327,165✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
605,596✔
252
  if (streamInserterParam == NULL) {
605,596✔
253
    return TSDB_CODE_SUCCESS;
357,963✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
247,633✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
247,633✔
323
    if (code) {
247,633✔
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,
605,596✔
328
         tstrerror(code));
329

330
_error:
72,681✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,289,381✔
342
  if (pInfo == NULL) {
3,289,381✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,289,381✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,289,381✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,289,381✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,289,381✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,955,402✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
498,598✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
6,244,783✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

377
  void*           info = pOperator->info;
3,289,381✔
378
  STableScanBase* pScanBaseInfo = NULL;
3,289,381✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,289,381✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
333,979✔
382
    setReadHandle(handle, pScanBaseInfo);
333,979✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,955,402✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,456,804✔
385
    setReadHandle(handle, pScanBaseInfo);
2,456,804✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
605,647✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
419,504✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
419,504✔
444

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

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

454
  locked = 1;
419,504✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
1,167,238✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
747,734✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
747,734✔
458

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

465
    tDecoderClear(&mr.coder);
747,734✔
466

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

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

497
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
1,167,238✔
498
    bool     qualified = false;
747,734✔
499
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
747,734✔
500
    if (t == NULL) {
747,734✔
501
      continue;
×
502
    }
503

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

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

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

521
  // handle multiple partition
522

523
_end:
419,504✔
524

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

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

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

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

550
  SStreamScanInfo* pScanInfo = pInfo->info;
1,404✔
551
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
1,404✔
552
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
1,404✔
553
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
1,404✔
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);
1,404✔
562
  taosWLockLatch(&pTaskInfo->lock);
1,404✔
563
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,404✔
564
  taosWUnLockLatch(&pTaskInfo->lock);
1,404✔
565

566
  return code;
1,404✔
567
}
568

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

580
  bool   assignUid = false;
419,504✔
581
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
419,504✔
582
  char*  keyBuf = NULL;
419,504✔
583
  if (bufLen > 0) {
419,504✔
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;
419,504✔
593
  taosWLockLatch(lock);
419,504✔
594

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

605
    if (bufLen > 0) {
572,534✔
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);
572,534✔
621
    if (code != TSDB_CODE_SUCCESS) {
572,534✔
622
      taosMemoryFree(keyBuf);
×
623
      taosArrayDestroy(qa);
×
624
      taosWUnLockLatch(lock);
×
625
      return code;
×
626
    }
627
  }
628

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

634
  taosArrayDestroy(qa);
419,504✔
635
  return 0;
419,504✔
636
}
637

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

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

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

652
  SStreamScanInfo* pScanInfo = pInfo->info;
419,504✔
653
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
419,504✔
654
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
419,504✔
655
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
419,504✔
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);
419,504✔
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,
468,128,376✔
698
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
699
                                    int32_t idx, bool* tbGet) {
700
  *tbGet = false;
468,128,376✔
701

702
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
468,163,865✔
703
    return TSDB_CODE_INVALID_PARA;
2,460✔
704
  }
705
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
468,162,489✔
706

707
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
468,162,489✔
708
    return TSDB_CODE_SUCCESS;
274,114,831✔
709
  }
710

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

717
  *sversion = pSchemaInfo->sw->version;
193,883,643✔
718
  *tversion = pSchemaInfo->tversion;
194,061,867✔
719
  *rversion = pSchemaInfo->rversion;
193,961,083✔
720
  if (pSchemaInfo->dbname) {
194,075,014✔
721
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
194,056,722✔
722
  } else {
723
    dbName[0] = 0;
×
724
  }
725
  if (pSchemaInfo->tablename) {
194,151,613✔
726
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
194,077,284✔
727
  } else {
728
    tableName[0] = 0;
17,032✔
729
  }
730

731
  *tbGet = true;
194,144,657✔
732

733
  return TSDB_CODE_SUCCESS;
194,104,283✔
734
}
735

736
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
274,078,587✔
737

738
void qDestroyOperatorParam(SOperatorParam* pParam) {
121,344✔
739
  if (NULL == pParam) {
121,344✔
740
    return;
×
741
  }
742
  freeOperatorParam(pParam, OP_GET_PARAM);
121,344✔
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) {
33,210,996✔
751
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
33,210,996✔
752
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
33,210,996✔
753
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
33,210,996✔
754
    qError("%s, %s operator type mismatch, task operator type:%d, "
116,604✔
755
           "new param operator type:%d", GET_TASKID(pTask), __func__,
756
           pTask->pRoot->operatorType,
757
           pNewParam->opType);
758
    qDestroyOperatorParam((SOperatorParam*)pParam);
116,604✔
759
    return;
116,604✔
760
  }
761
  TSWAP(pParam, pTask->pOpParam);
33,096,630✔
762
  ((SExecTaskInfo*)tinfo)->paramSet = false;
33,097,307✔
763
}
764

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

770
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
2,936,158✔
771
  int32_t        code = TSDB_CODE_SUCCESS;
2,936,158✔
772
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
2,936,158✔
773
  if (pTask->pWorkerCb) {
2,936,158✔
774
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
2,935,632✔
775
    if (code != TSDB_CODE_SUCCESS) {
2,936,684✔
776
      pTask->code = code;
×
777
      return pTask->code;
×
778
    }
779
  }
780

781
  code = tsem_wait(pSem);
2,937,210✔
782
  if (code != TSDB_CODE_SUCCESS) {
2,936,684✔
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,936,684✔
789
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,936,684✔
790
    if (code != TSDB_CODE_SUCCESS) {
2,936,684✔
791
      pTask->code = code;
×
792
      return pTask->code;
×
793
    }
794
  }
795
  return TSDB_CODE_SUCCESS;
2,936,684✔
796
}
797

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

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

807
  readHandle->uid = 0;
276,585,567✔
808
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
276,606,059✔
809
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
276,425,814✔
810
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
731,083✔
811
    goto _error;
446,854✔
812
  }
813
    
814
  if (handle) {
275,743,799✔
815
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
275,653,599✔
816
    void*           pSinkManager = NULL;
275,704,023✔
817
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
275,286,525✔
818
    if (code != TSDB_CODE_SUCCESS) {
275,591,128✔
819
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
820
      goto _error;
×
821
    }
822

823
    void* pSinkParam = NULL;
275,591,128✔
824
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
275,622,889✔
825
    if (code != TSDB_CODE_SUCCESS) {
275,315,465✔
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;
275,315,465✔
832
    if (readHandle->localExec) {
275,608,084✔
833
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,772✔
834
      if (code != TSDB_CODE_SUCCESS) {
2,772✔
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,
275,374,293✔
844
                              (*pTask)->id.str, pSubplan->processOneBlock);
275,682,585✔
845
    if (code) {
275,330,570✔
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", 
275,797,104✔
851
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
852

853
_error:
52,870,989✔
854
  // if failed to add ref for all tables in this query, abort current query
855
  return code;
276,564,075✔
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,
324,453,073✔
864
                     bool processOneBlock) {
865
  int32_t        code = TSDB_CODE_SUCCESS;
324,453,073✔
866
  int32_t        lino = 0;
324,453,073✔
867
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
324,453,073✔
868
  int64_t        threadId = taosGetSelfPthreadId();
324,453,073✔
869

870
  if (pLocal) {
324,404,768✔
871
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
320,642,708✔
872
  }
873

874
  taosArrayClear(pResList);
324,408,434✔
875

876
  int64_t curOwner = 0;
324,359,043✔
877
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
324,359,043✔
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) {
324,335,791✔
884
    pTaskInfo->cost.start = taosGetTimestampUs();
270,184,292✔
885
  }
886

887
  if (isTaskKilled(pTaskInfo)) {
324,484,120✔
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);
324,340,189✔
895
  if (ret != TSDB_CODE_SUCCESS) {
324,810,136✔
896
    pTaskInfo->code = ret;
588,953✔
897
    (void)cleanUpUdfs();
588,953✔
898

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

902
    return pTaskInfo->code;
588,953✔
903
  }
904

905
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
324,221,183✔
906

907
  int32_t      current = 0;
324,232,481✔
908
  SSDataBlock* pRes = NULL;
324,232,481✔
909
  int64_t      st = taosGetTimestampUs();
324,415,369✔
910

911
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
324,415,369✔
912
    pTaskInfo->paramSet = true;
33,093,316✔
913
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
33,088,878✔
914
  } else {
915
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
291,366,596✔
916
  }
917

918
  QUERY_CHECK_CODE(code, lino, _end);
323,882,233✔
919
  code = blockDataCheck(pRes);
323,882,233✔
920
  QUERY_CHECK_CODE(code, lino, _end);
323,888,867✔
921

922
  if (pRes == NULL) {
323,888,867✔
923
    st = taosGetTimestampUs();
63,104,058✔
924
  }
925

926
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
323,886,354✔
927
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
323,918,522✔
928
    rowsThreshold = 4096;
323,380,970✔
929
  }
930

931
  int32_t blockIndex = 0;
323,893,010✔
932
  while (pRes != NULL) {
808,985,328✔
933
    SSDataBlock* p = NULL;
530,780,291✔
934
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
530,763,995✔
935
      SSDataBlock* p1 = NULL;
407,508,760✔
936
      code = createOneDataBlock(pRes, true, &p1);
407,515,379✔
937
      QUERY_CHECK_CODE(code, lino, _end);
407,488,890✔
938

939
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
407,488,890✔
940
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
407,514,912✔
941
      p = p1;
407,514,912✔
942
    } else if (processOneBlock) {
123,277,023✔
943
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
16,900,311✔
944
      if (tmp) {
16,900,311✔
945
        blockDataDestroy(*tmp);
16,900,311✔
946
        *tmp = NULL;
16,900,311✔
947
      }
948
      SSDataBlock* p1 = NULL;
16,900,311✔
949
      code = createOneDataBlock(pRes, true, &p1);
16,900,311✔
950
      QUERY_CHECK_CODE(code, lino, _end);
16,900,311✔
951

952
      *tmp = p1;
16,900,311✔
953
      p = p1;
16,900,311✔
954
    } else {
955
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
106,376,712✔
956
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
106,375,014✔
957

958
      p = *(SSDataBlock**)tmp;
106,375,014✔
959
      code = copyDataBlock(p, pRes);
106,375,594✔
960
      QUERY_CHECK_CODE(code, lino, _end);
106,375,629✔
961
    }
962

963
    blockIndex += 1;
530,779,414✔
964

965
    current += p->info.rows;
530,779,414✔
966
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
530,785,695✔
967
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
968
    void* tmp = taosArrayPush(pResList, &p);
530,786,377✔
969
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
530,786,377✔
970

971
    if (current >= rowsThreshold || processOneBlock) {
530,786,377✔
972
      break;
973
    }
974

975
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
485,095,099✔
976
    QUERY_CHECK_CODE(code, lino, _end);
485,099,048✔
977
    code = blockDataCheck(pRes);
485,099,048✔
978
    QUERY_CHECK_CODE(code, lino, _end);
485,105,448✔
979
  }
980

981
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
323,896,315✔
982
    pTaskInfo->pSubplan->rowsThreshold -= current;
515,831✔
983
  }
984

985
  *hasMore = (pRes != NULL);
323,902,341✔
986
  uint64_t el = (taosGetTimestampUs() - st);
323,894,680✔
987

988
  pTaskInfo->cost.elapsedTime += el;
323,894,680✔
989
  if (NULL == pRes) {
323,873,739✔
990
    *useconds = pTaskInfo->cost.elapsedTime;
278,182,461✔
991
  }
992

993
_end:
323,923,619✔
994
  (void)cleanUpUdfs();
323,909,064✔
995

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

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

1006
  return pTaskInfo->code;
323,933,554✔
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) {
132,533,123✔
1024
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
132,533,123✔
1025
  int64_t        threadId = taosGetSelfPthreadId();
132,533,123✔
1026
  int64_t        curOwner = 0;
132,533,123✔
1027

1028
  *pRes = NULL;
132,533,123✔
1029

1030
  // todo extract method
1031
  taosRLockLatch(&pTaskInfo->lock);
132,533,422✔
1032
  bool isKilled = isTaskKilled(pTaskInfo);
132,534,849✔
1033
  if (isKilled) {
132,534,205✔
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) {
132,534,205✔
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;
132,534,550✔
1049
  taosRUnLockLatch(&pTaskInfo->lock);
132,534,550✔
1050

1051
  if (pTaskInfo->cost.start == 0) {
132,535,490✔
1052
    pTaskInfo->cost.start = taosGetTimestampUs();
236,015✔
1053
  }
1054

1055
  // error occurs, record the error code and return to client
1056
  int32_t ret = setjmp(pTaskInfo->env);
132,535,490✔
1057
  if (ret != TSDB_CODE_SUCCESS) {
132,533,126✔
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));
132,533,126✔
1066

1067
  int64_t st = taosGetTimestampUs();
132,531,005✔
1068
  int32_t code = TSDB_CODE_SUCCESS;
132,531,005✔
1069
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
132,531,005✔
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);
132,531,695✔
1074
  }
1075
  if (code) {
132,491,881✔
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);
132,491,881✔
1081
  if (code) {
132,531,334✔
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);
132,507,208✔
1087

1088
  pTaskInfo->cost.elapsedTime += el;
132,507,208✔
1089
  if (NULL == *pRes) {
132,513,774✔
1090
    *useconds = pTaskInfo->cost.elapsedTime;
23,141,240✔
1091
  }
1092

1093
  (void)cleanUpUdfs();
132,517,279✔
1094

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

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

1101
  atomic_store_64(&pTaskInfo->owner, 0);
132,534,455✔
1102
  return pTaskInfo->code;
132,534,455✔
1103
}
1104

1105
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
103,563,076✔
1106
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
103,563,076✔
1107
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
103,565,115✔
1108
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
103,565,113✔
1109

1110
  if (!tmp) {
103,562,564✔
1111
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1112
    return terrno;
×
1113
  }
1114
  return TSDB_CODE_SUCCESS;
103,562,564✔
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) {
71,027✔
1140
  if (pTaskInfo->subJobCtx.hasSubJobs) {
71,027✔
1141
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
21,040✔
1142
    if (pTaskInfo->subJobCtx.param) {
21,040✔
1143
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
13,150✔
1144
    }
1145
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
21,040✔
1146
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
21,040✔
1147
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
21,040✔
1148
  }
1149
  
1150
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
71,027✔
1151

1152
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
71,027✔
1153
  for (int32_t i = 0; i < num; ++i) {
73,629✔
1154
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
2,602✔
1155
    if (!pStop) {
2,602✔
1156
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1157
      continue;
×
1158
    }
1159
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
2,602✔
1160
    if (pExchangeInfo) {
2,602✔
1161
      int32_t code = tsem_post(&pExchangeInfo->ready);
2,602✔
1162
      if (code != TSDB_CODE_SUCCESS) {
2,602✔
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,602✔
1166
      }
1167
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
2,602✔
1168
      if (code != TSDB_CODE_SUCCESS) {
2,602✔
1169
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1170
      }
1171
    }
1172
  }
1173

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

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

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

1185
  setTaskKilled(pTaskInfo, rspCode);
71,027✔
1186
  qStopTaskOperators(pTaskInfo);
71,027✔
1187

1188
  return TSDB_CODE_SUCCESS;
71,027✔
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) {
276,893,691✔
1245
  STaskCostInfo* pSummary = &pTaskInfo->cost;
276,893,691✔
1246
  int64_t        idleTime = pSummary->start - pSummary->created;
276,898,923✔
1247

1248
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
276,897,702✔
1249
  if (pSummary->pRecoder != NULL) {
276,867,137✔
1250
    qDebug(
192,374,676✔
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,492,541✔
1259
           pSummary->elapsedTime / 1000.0);
1260
  }
1261
}
276,866,715✔
1262

1263

1264
void qDestroyTask(qTaskInfo_t qTaskHandle) {
283,481,177✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
283,481,177✔
1266
  if (pTaskInfo == NULL) {
283,481,177✔
1267
    return;
6,591,902✔
1268
  }
1269

1270
  if (pTaskInfo->pRoot != NULL) {
276,889,275✔
1271
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
276,902,329✔
1272
  } else {
1273
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1274
  }
1275

1276
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
276,904,209✔
1277
  doDestroyTask(pTaskInfo);
276,892,577✔
1278
}
1279

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

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

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

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

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

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

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

1320
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
24,261,832✔
1321
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
24,261,832✔
1322
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
24,261,832✔
1323
  return 0;
24,261,832✔
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) {
769,177✔
1332
  memset(pCond, 0, sizeof(SQueryTableDataCond));
769,177✔
1333
  pCond->order = TSDB_ORDER_ASC;
769,177✔
1334
  pCond->numOfCols = pMtInfo->schema->nCols;
769,768✔
1335
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
770,665✔
1336
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
769,469✔
1337
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
770,373✔
1338
    taosMemoryFreeClear(pCond->colList);
×
1339
    taosMemoryFreeClear(pCond->pSlotList);
×
1340
    return terrno;
×
1341
  }
1342

1343
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
770,373✔
1344
  pCond->suid = pMtInfo->suid;
770,665✔
1345
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
770,971✔
1346
  pCond->startVersion = -1;
770,366✔
1347
  pCond->endVersion = sContext->snapVersion;
769,469✔
1348

1349
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,894,226✔
1350
    SColumnInfo* pColInfo = &pCond->colList[i];
4,122,066✔
1351
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,121,767✔
1352
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,123,262✔
1353
    if (pMtInfo->pExtSchemas != NULL) {
4,122,372✔
1354
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
47,760✔
1355
    }
1356
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,124,764✔
1357
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,124,764✔
1358

1359
    pCond->pSlotList[i] = i;
4,124,159✔
1360
  }
1361

1362
  return TSDB_CODE_SUCCESS;
771,270✔
1363
}
1364

1365
void qStreamSetOpen(qTaskInfo_t tinfo) {
130,999,367✔
1366
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
130,999,367✔
1367
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
130,999,367✔
1368
  pOperator->status = OP_NOT_OPENED;
131,012,818✔
1369
}
131,017,130✔
1370

1371
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
23,434,573✔
1372
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,434,573✔
1373
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
23,434,573✔
1374
}
23,435,161✔
1375

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

1381
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
24,433,397✔
1382
  const char*    id = GET_TASKID(pTaskInfo);
24,433,437✔
1383

1384
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
24,433,434✔
1385
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
23,132,754✔
1386
    if (pOperator == NULL || code != 0) {
23,132,688✔
1387
      return code;
×
1388
    }
1389

1390
    SStreamScanInfo* pInfo = pOperator->info;
23,134,076✔
1391
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
23,134,076✔
1392
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
23,132,381✔
1393
    walReaderVerifyOffset(pWalReader, pOffset);
23,134,076✔
1394
  }
1395
  // if pOffset equal to current offset, means continue consume
1396
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
24,433,068✔
1397
    return 0;
22,510,893✔
1398
  }
1399

1400
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,920,859✔
1401
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,133,228✔
1402
    if (pOperator == NULL || code != 0) {
1,133,546✔
1403
      return code;
×
1404
    }
1405

1406
    SStreamScanInfo* pInfo = pOperator->info;
1,133,546✔
1407
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,133,228✔
1408
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,133,546✔
1409
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,133,546✔
1410

1411
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,133,228✔
1412
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
923,149✔
1413
      pScanBaseInfo->dataReader = NULL;
923,149✔
1414

1415
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
923,149✔
1416
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
923,149✔
1417
      walReaderVerifyOffset(pWalReader, pOffset);
922,844✔
1418
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
923,149✔
1419
      if (code < 0) {
923,149✔
1420
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,024✔
1421
        return code;
11,024✔
1422
      }
1423
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
210,079✔
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;
210,397✔
1427
      int64_t ts = pOffset->ts;
210,079✔
1428
      int32_t index = 0;
210,397✔
1429

1430
      // this value may be changed if new tables are created
1431
      taosRLockLatch(&pTaskInfo->lock);
210,397✔
1432
      int32_t numOfTables = 0;
210,397✔
1433
      code = tableListGetSize(pTableListInfo, &numOfTables);
210,397✔
1434
      if (code != TSDB_CODE_SUCCESS) {
210,079✔
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) {
210,079✔
1441
        if (numOfTables != 0) {
203,957✔
1442
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
39,458✔
1443
          if (!tmp) {
39,458✔
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,458✔
1449
          ts = INT64_MIN;
39,458✔
1450
          pScanInfo->currentTable = 0;
39,458✔
1451
        } else {
1452
          taosRUnLockLatch(&pTaskInfo->lock);
164,499✔
1453
          qError("no table in table list, %s", id);
164,499✔
1454
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
164,499✔
1455
        }
1456
      }
1457
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,580✔
1458

1459
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
45,588✔
1460
             pInfo->pTableScanOp->resultInfo.totalRows);
1461
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
45,898✔
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);
45,898✔
1467
      taosRUnLockLatch(&pTaskInfo->lock);
45,898✔
1468

1469
      if (index >= 0) {
45,898✔
1470
        pScanInfo->currentTable = index;
45,898✔
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};
45,898✔
1478
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
45,898✔
1479

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

1488
      if (pScanBaseInfo->dataReader == NULL) {
45,898✔
1489
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
78,654✔
1490
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1491
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
39,327✔
1492
        if (code != TSDB_CODE_SUCCESS) {
39,327✔
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",
39,327✔
1498
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1499
      } else {
1500
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,571✔
1501
        if (code != TSDB_CODE_SUCCESS) {
6,571✔
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);
6,571✔
1507
        if (code != TSDB_CODE_SUCCESS) {
6,571✔
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",
6,571✔
1512
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1513
      }
1514

1515
      // restore the key value
1516
      pScanBaseInfo->cond.twindows.skey = oldSkey;
45,898✔
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) {
787,631✔
1524
      SStreamRawScanInfo* pInfo = pOperator->info;
772,146✔
1525
      SSnapContext*       sContext = pInfo->sContext;
772,146✔
1526
      SOperatorInfo*      p = NULL;
772,146✔
1527

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

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

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

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

1549
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
771,249✔
1550
      tableListClear(pTableListInfo);
770,936✔
1551

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

1557
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
770,971✔
1558
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
770,074✔
1559
      if (code != TSDB_CODE_SUCCESS) {
770,665✔
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)) {
770,665✔
1565
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,322✔
1566
      } else {
1567
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
768,752✔
1568
      }
1569

1570
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
770,658✔
1571
      if (code != TSDB_CODE_SUCCESS) {
771,270✔
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);
771,270✔
1578
      if (!pList) {
770,971✔
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;
770,971✔
1584
      code = tableListGetSize(pTableListInfo, &size);
770,971✔
1585
      if (code != TSDB_CODE_SUCCESS) {
770,971✔
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,541,942✔
1592
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
770,971✔
1593
      if (code != TSDB_CODE_SUCCESS) {
770,373✔
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);
770,373✔
1600
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
770,672✔
1601
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1602
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
769,768✔
1603
      pTaskInfo->streamInfo.schema = mtInfo.schema;
770,672✔
1604
      taosMemoryFreeClear(mtInfo.pExtSchemas);
770,672✔
1605

1606
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
770,672✔
1607
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
15,485✔
1608
      SStreamRawScanInfo* pInfo = pOperator->info;
4,984✔
1609
      SSnapContext*       sContext = pInfo->sContext;
4,984✔
1610
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,984✔
1611
      if (code != 0) {
4,984✔
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,984✔
1616
             id);
1617
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
10,501✔
1618
      SStreamRawScanInfo* pInfo = pOperator->info;
10,501✔
1619
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
10,501✔
1620
      pInfo->dataReader = NULL;
10,501✔
1621
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
10,501✔
1622
    }
1623
  }
1624

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

1630
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
226,304,084✔
1631
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
226,304,084✔
1632
  if (pMsg->info.ahandle == NULL) {
226,333,871✔
1633
    rpcFreeCont(pMsg->pCont);
×
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));
×
1635
    return;
×
1636
  }
1637

1638
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
226,267,798✔
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};
226,278,677✔
1642

1643
  if (pMsg->contLen > 0) {
226,314,373✔
1644
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
226,298,401✔
1645
    if (buf.pData == NULL) {
226,263,657✔
1646
      pMsg->code = terrno;
×
1647
    } else {
1648
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
226,263,657✔
1649
    }
1650
  }
1651

1652
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
226,298,104✔
1653
  rpcFreeCont(pMsg->pCont);
226,348,804✔
1654
  destroySendMsgInfo(pSendInfo);
226,305,228✔
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,413,200✔
1696
  int32_t        code = TSDB_CODE_SUCCESS;
3,413,200✔
1697
  int32_t        lino = 0;
3,413,200✔
1698
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,413,200✔
1699

1700
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,412,036✔
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,413,200✔
1707
    STableScanInfo* pScanInfo = pOperator->info;
1,706,053✔
1708

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

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

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

1729
  *pList = NULL;
1,707,147✔
1730
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,707,147✔
1731
  if (pArray == NULL) {
1,706,054✔
1732
    return terrno;
×
1733
  }
1734

1735
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,706,054✔
1736
  if (code == 0) {
1,706,054✔
1737
    *pList = pArray;
1,706,054✔
1738
  } else {
1739
    taosArrayDestroy(pArray);
×
1740
  }
1741
  return code;
1,706,054✔
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,500,373✔
1780
  int32_t code = 0;
28,500,373✔
1781

1782
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
28,500,373✔
1783
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
28,499,084✔
1784

1785
  if (pOper->fpSet.resetStateFn) {
28,497,585✔
1786
    code = pOper->fpSet.resetStateFn(pOper);
28,498,573✔
1787
  }
1788
  pOper->status = OP_NOT_OPENED;
28,499,767✔
1789
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
48,869,907✔
1790
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,368,513✔
1791
  }
1792
  return code;
28,500,969✔
1793
}
1794

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

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

1809
  *ppRes = NULL;
11,183,774✔
1810

1811
  // todo extract method
1812
  taosRLockLatch(&pTaskInfo->lock);
11,183,554✔
1813
  bool isKilled = isTaskKilled(pTaskInfo);
11,183,774✔
1814
  if (isKilled) {
11,183,774✔
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,183,774✔
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,183,554✔
1831
  taosRUnLockLatch(&pTaskInfo->lock);
11,183,774✔
1832

1833
  if (pTaskInfo->cost.start == 0) {
11,183,774✔
1834
    pTaskInfo->cost.start = taosGetTimestampUs();
255,953✔
1835
  }
1836

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

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

1849
  int64_t st = taosGetTimestampUs();
11,183,774✔
1850

1851
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,183,774✔
1852
  if (code) {
9,251,978✔
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,251,978✔
1857
    code = blockDataCheck(*ppRes);
9,251,978✔
1858
  }
1859
  if (code) {
9,251,978✔
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,251,978✔
1865

1866
  pTaskInfo->cost.elapsedTime += el;
9,251,978✔
1867
  if (NULL == *ppRes) {
9,251,978✔
1868
    *useconds = pTaskInfo->cost.elapsedTime;
5,740,586✔
1869
  }
1870

1871
  (void)cleanUpUdfs();
9,251,978✔
1872

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

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

1879
  atomic_store_64(&pTaskInfo->owner, 0);
9,251,978✔
1880
  return pTaskInfo->code;
9,251,978✔
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,
261,352✔
1889
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1890
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1891
  int32_t code = 0;                                        
261,352✔
1892
  if (*pTableListInfo != NULL) {
261,352✔
1893
    qDebug("table list already exists, no need to create again");
×
1894
    goto end;
×
1895
  }
1896
  STableListInfo* pList = tableListCreate();
261,352✔
1897
  if (pList == NULL) {
261,352✔
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};
261,352✔
1904
  SReadHandle    pHandle = {.vnode = pVnode};
261,352✔
1905
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
261,352✔
1906

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

1915
end:
261,352✔
1916
  return 0;
261,352✔
1917
}
1918

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

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

1940
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
31,515✔
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,515✔
1944
  STableListInfo* pList = tableListCreate();
31,515✔
1945
  if (pList == NULL) {
31,515✔
1946
    code = terrno;
×
1947
    goto end;
×
1948
  }
1949
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
31,515✔
1950
  if (uidListCopy == NULL) {
31,515✔
1951
    code = terrno;
×
1952
    goto end;
×
1953
  }
1954
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
31,515✔
1955
  SReadHandle    pHandle = {.vnode = pVnode};
31,515✔
1956

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

1970
  taosArrayClear(uidList);
31,515✔
1971
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
62,692✔
1972
    void* tmp = taosArrayGet(uidListCopy, i);
31,346✔
1973
    if (tmp == NULL) {
31,515✔
1974
      continue;
×
1975
    }
1976
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
31,515✔
1977
    if (slot == NULL) {
31,346✔
1978
      if (taosArrayPush(uidList, tmp) == NULL) {
3,442✔
1979
        code = terrno;
×
1980
        goto end;
×
1981
      }
1982
    }
1983
  }
1984
end:
31,515✔
1985
  taosArrayDestroy(uidListCopy);
31,515✔
1986
  tableListDestroy(pList);
31,346✔
1987
  return code;
31,346✔
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); }
261,352✔
2012
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
261,352✔
2013
  STableListInfo* pList = pTableListInfo;
261,352✔
2014
  return pList->pTableList;
261,352✔
2015
}
2016

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

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

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

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

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

2056
  if (code == 0) {
669,649✔
2057
    const char* pVal = NULL;
669,649✔
2058
    int32_t     len = 0;
669,649✔
2059
    SNode*      pSclNode = NULL;
669,649✔
2060
    switch (pExprInfo->pExpr->nodeType) {
669,649✔
2061
      case QUERY_NODE_FUNCTION:
669,649✔
2062
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
669,649✔
2063
        break;
669,649✔
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);
669,649✔
2072
    SSDataBlock block = {0};
669,649✔
2073
    block.info.rows = 1;
669,649✔
2074
    SSDataBlock* pBlock = &block;
669,649✔
2075
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
669,649✔
2076
    if (tmp == NULL) {
669,649✔
2077
      code = terrno;
×
2078
    }
2079
    if (code == 0) {
669,649✔
2080
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
669,649✔
2081
      gTaskScalarExtra.pStreamRange = NULL;
669,649✔
2082
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
669,649✔
2083
    }
2084
    taosArrayDestroy(pBlockList);
669,649✔
2085
  }
2086
  nodesDestroyList(pList);
669,649✔
2087
  destroyExprInfo(pExprInfo, numOfExprs);
669,649✔
2088
  taosMemoryFreeClear(pExprInfo);
669,649✔
2089
  return code;
669,649✔
2090
}
2091

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

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

2114
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
50,423✔
2115
  if (code != TSDB_CODE_SUCCESS) {
50,423✔
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;
50,423✔
2122
  int32_t rowIdx = (*pRes)->info.rows;
50,423✔
2123
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
50,423✔
2124
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
50,423✔
2125
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
201,501✔
2126
    SScalarParam   dst = {0};
151,078✔
2127
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
151,078✔
2128
    code = nodesStringToNode(pCol->expr, &pNode);
151,078✔
2129
    if (code != 0) break;
151,078✔
2130
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
151,078✔
2131
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
151,078✔
2132
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
100,655✔
2133
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
100,655✔
2134
    } else {
2135
      dst.columnData = pInfo;
50,423✔
2136
      dst.numOfRows = rowIdx;
50,423✔
2137
      dst.colAlloced = false;
50,423✔
2138
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
50,423✔
2139
    }
2140
    ++idx;
151,078✔
2141
    // TODO sclFreeParam(&dst);
2142
    nodesDestroyNode(pNode);
151,078✔
2143
    if (code != 0) break;
151,078✔
2144
  }
2145
  if (code == TSDB_CODE_SUCCESS) {
50,423✔
2146
    (*pRes)->info.rows++;
50,423✔
2147
  }
2148
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
50,423✔
2149
  return code;
50,423✔
2150
}
2151

2152
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
244,125✔
2153
  int32_t      code = 0;
244,125✔
2154
  const char*  pVal = NULL;
244,125✔
2155
  SScalarParam dst = {0};
244,125✔
2156
  int32_t      len = 0;
244,125✔
2157
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
244,125✔
2158
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
244,125✔
2159
  // execute the expr
2160
  switch (pExpr->type) {
244,125✔
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: {
244,125✔
2179
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
244,125✔
2180
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
244,125✔
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));
244,125✔
2186
      if (!pCol) {
244,125✔
2187
        code = terrno;
×
2188
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2189
        break;
×
2190
      }
2191

2192
      pCol->hasNull = true;
244,125✔
2193
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
244,125✔
2194
      pCol->info.colId = 0;
244,125✔
2195
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
244,125✔
2196
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
244,125✔
2197
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
244,125✔
2198
      code = colInfoDataEnsureCapacity(pCol, 1, true);
244,125✔
2199
      if (code != 0) {
244,125✔
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;
244,125✔
2205
      dst.numOfRows = 1;
244,125✔
2206
      dst.colAlloced = true;
244,125✔
2207
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
244,125✔
2208
      if (colDataIsNull_var(dst.columnData, 0)) {
244,125✔
2209
        qInfo("invalid sub tb expr with null value");
3,380✔
2210
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,380✔
2211
      }
2212
      if (code == 0) {
244,125✔
2213
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
240,745✔
2214
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
240,745✔
2215
      }
2216
    } break;
244,125✔
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) {
244,125✔
2223
    if (!pVal || len == 0) {
240,745✔
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) {
240,745✔
2228
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
440✔
2229
      len = TSDB_TABLE_NAME_LEN - 1;
440✔
2230
    }
2231

2232
    memcpy(tbname, pVal, len);
240,745✔
2233
    tbname[len] = '\0';  // ensure null terminated
240,745✔
2234
    if (NULL != strchr(tbname, '.')) {
240,745✔
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);
244,125✔
2241
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
244,125✔
2242
  return code;
244,125✔
2243
}
2244

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

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

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

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

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

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

2303
  if (pSrc->pFields && pSrc->pFields->size > 0) {
247,633✔
2304
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
247,633✔
2305
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
247,633✔
2306
  } else {
2307
    (*ppDst)->pFields = NULL;
×
2308
  }
2309
  
2310
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
247,633✔
2311
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
136,871✔
2312
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
136,871✔
2313
  } else {
2314
    (*ppDst)->pTagFields = NULL;
110,762✔
2315
  }
2316

2317
  if (pSrc->colCids && pSrc->colCids->size > 0) {
247,633✔
2318
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,774✔
2319
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,774✔
2320
  } else {
2321
    (*ppDst)->colCids = NULL;
245,859✔
2322
  }
2323

2324
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
247,633✔
2325
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,132✔
2326
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,132✔
2327
  } else {
2328
    (*ppDst)->tagCids = NULL;
246,501✔
2329
  }
2330

2331
_exit:
247,633✔
2332

2333
  if (code != 0) {
247,633✔
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;
247,633✔
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) {
12,283✔
2353
  int32_t         code = TSDB_CODE_SUCCESS;
12,283✔
2354

2355
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
12,283✔
2356
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
12,283✔
2357
  if (code != TSDB_CODE_SUCCESS) {
12,283✔
2358
    goto end;
×
2359
  }
2360
end:
12,283✔
2361
  return code;
12,283✔
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:
60,416✔
2375
      return countSatisfied;
60,416✔
2376
    case TRUE_FOR_AND:
60,416✔
2377
      return durationSatisfied && countSatisfied;
60,416✔
2378
    case TRUE_FOR_OR:
60,416✔
2379
      return durationSatisfied || countSatisfied;
60,416✔
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