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

taosdata / TDengine / #4931

16 Jan 2026 02:32AM UTC coverage: 66.749% (+0.03%) from 66.716%
#4931

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

1890 existing lines in 121 files now uncovered.

203303 of 304580 relevant lines covered (66.75%)

129941648.02 hits per line

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

68.92
/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) {
857,197,146✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
857,197,146✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
857,197,146✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
857,319,028✔
47
}
856,982,943✔
48

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

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

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

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

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
43,132,311✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
43,132,311✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
43,132,972✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
19,554,613✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
19,554,613✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
19,554,532✔
162
      if (pScanInfo->base.dataReader != NULL) {
19,555,192✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
264,175✔
164
        if (code) {
264,175✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
23,576,299✔
172
  }
173

174
  return 0;
23,033,190✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
23,033,881✔
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) {
442,921✔
213
  if (msg == NULL) {  // create raw scan
442,921✔
214
    SExecTaskInfo* pTaskInfo = NULL;
118,383✔
215

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

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

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

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

248
  return pTaskInfo;
325,486✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
546,757✔
252
  if (streamInserterParam == NULL) {
546,757✔
253
    return TSDB_CODE_SUCCESS;
311,626✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
235,348✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
235,348✔
323
    if (code) {
235,348✔
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,
546,974✔
328
         tstrerror(code));
329

330
_error:
84,557✔
331

332
  if (code != TSDB_CODE_SUCCESS) {
546,974✔
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;
546,974✔
339
}
340

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,931,365✔
342
  if (pInfo == NULL) {
2,931,365✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,931,365✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,931,365✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,931,365✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,931,365✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,651,606✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
403,087✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,582,971✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,931,365✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
279,759✔
382
    setReadHandle(handle, pScanBaseInfo);
279,759✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,651,606✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,248,519✔
385
    setReadHandle(handle, pScanBaseInfo);
2,248,519✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
546,974✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
401,137✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
401,137✔
444

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

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

454
  locked = 1;
401,137✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
1,090,202✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
689,065✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
689,065✔
458

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

465
    tDecoderClear(&mr.coder);
689,065✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
689,065✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
689,065✔
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) {
689,065✔
UNCOV
473
          continue;
×
474
        }
475
      } else {  // ordinary table
476
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
477
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
478
        if (mr.me.uid != uid) {
×
479
          continue;
×
480
        }
481
      }
482
    }
483

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
401,137✔
524

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

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

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

543
  if (isAdd) {
402,486✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
401,137✔
545
  }
546

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

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

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

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

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

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

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

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

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

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

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

638
  return code;
402,486✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
654,364,500✔
647
    return TSDB_CODE_INVALID_PARA;
43,132✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
654,321,467✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
654,321,467✔
652
    return TSDB_CODE_SUCCESS;
374,754,939✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
279,697,511✔
662
  *tversion = pSchemaInfo->tversion;
279,718,564✔
663
  *rversion = pSchemaInfo->rversion;
279,798,883✔
664
  if (pSchemaInfo->dbname) {
279,691,444✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
279,542,046✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
279,744,256✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
279,707,063✔
671
  } else {
UNCOV
672
    tableName[0] = 0;
×
673
  }
674

675
  *tbGet = true;
279,742,669✔
676

677
  return TSDB_CODE_SUCCESS;
279,687,566✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
374,641,338✔
681

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

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

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

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
53,286,490✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
53,286,490✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
53,286,490✔
717
  if (pTask->pWorkerCb) {
53,286,490✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
53,290,277✔
719
    if (code != TSDB_CODE_SUCCESS) {
53,294,613✔
UNCOV
720
      pTask->code = code;
×
UNCOV
721
      return pTask->code;
×
722
    }
723
  }
724

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

732
  if (pTask->pWorkerCb) {
53,294,400✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
53,293,914✔
734
    if (code != TSDB_CODE_SUCCESS) {
53,294,127✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
53,294,613✔
740
}
741

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

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

751
  readHandle->uid = 0;
383,221,155✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
383,278,542✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
382,949,669✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
6,573,980✔
755
    goto _error;
6,537,112✔
756
  }
757
    
758
  if (handle) {
376,455,892✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
376,169,335✔
760
    void*           pSinkManager = NULL;
376,272,260✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
376,156,975✔
762
    if (code != TSDB_CODE_SUCCESS) {
375,477,764✔
UNCOV
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
UNCOV
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
375,477,764✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
376,015,904✔
769
    if (code != TSDB_CODE_SUCCESS) {
375,730,248✔
UNCOV
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
UNCOV
771
      taosMemoryFree(pSinkManager);
×
UNCOV
772
      goto _error;
×
773
    }
774

775
    SDataSinkNode* pSink = NULL;
375,730,248✔
776
    if (readHandle->localExec) {
376,043,174✔
777
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
3,124✔
778
      if (code != TSDB_CODE_SUCCESS) {
3,124✔
779
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
780
               (*pTask)->id.str);
UNCOV
781
        taosMemoryFree(pSinkManager);
×
UNCOV
782
        goto _error;
×
783
      }
784
    }
785

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
375,745,403✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
376,235,620✔
789
    if (code) {
375,632,043✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
589✔
791
    }
792
  }
793

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

797
_error:
163,076,036✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
383,121,755✔
800
}
801

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

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

814
  if (pLocal) {
476,872,323✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
473,551,313✔
816
  }
817

818
  taosArrayClear(pResList);
476,822,106✔
819

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

827
  if (pTaskInfo->cost.start == 0) {
476,782,111✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
370,598,639✔
829
  }
830

831
  if (isTaskKilled(pTaskInfo)) {
476,997,323✔
UNCOV
832
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
833
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
834
    return pTaskInfo->code;
×
835
  }
836

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
476,720,780✔
839
  if (ret != TSDB_CODE_SUCCESS) {
482,086,620✔
840
    pTaskInfo->code = ret;
5,720,953✔
841
    (void)cleanUpUdfs();
5,720,953✔
842

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

846
    return pTaskInfo->code;
5,720,953✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
476,365,667✔
850

851
  int32_t      current = 0;
476,368,829✔
852
  SSDataBlock* pRes = NULL;
476,368,829✔
853
  int64_t      st = taosGetTimestampUs();
476,844,069✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
476,844,069✔
856
    pTaskInfo->paramSet = true;
85,648,457✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
85,660,030✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
391,298,748✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
471,190,358✔
863
  code = blockDataCheck(pRes);
471,190,358✔
864
  QUERY_CHECK_CODE(code, lino, _end);
471,217,050✔
865

866
  if (pRes == NULL) {
471,217,050✔
867
    st = taosGetTimestampUs();
107,552,996✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
471,209,751✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
471,293,652✔
872
    rowsThreshold = 4096;
470,689,127✔
873
  }
874

875
  int32_t blockIndex = 0;
471,257,316✔
876
  while (pRes != NULL) {
1,084,725,448✔
877
    SSDataBlock* p = NULL;
667,214,354✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
667,099,536✔
879
      SSDataBlock* p1 = NULL;
500,553,622✔
880
      code = createOneDataBlock(pRes, true, &p1);
500,559,550✔
881
      QUERY_CHECK_CODE(code, lino, _end);
500,557,060✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
500,557,060✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
500,624,061✔
885
      p = p1;
500,624,061✔
886
    } else {
887
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
166,663,764✔
888
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
166,662,218✔
889

890
      p = *(SSDataBlock**)tmp;
166,662,218✔
891
      code = copyDataBlock(p, pRes);
166,663,188✔
892
      QUERY_CHECK_CODE(code, lino, _end);
166,664,665✔
893
    }
894

895
    blockIndex += 1;
667,265,449✔
896

897
    current += p->info.rows;
667,265,449✔
898
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
667,233,321✔
899
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
900
    void* tmp = taosArrayPush(pResList, &p);
667,283,488✔
901
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
667,283,488✔
902

903
    if (current >= rowsThreshold || processOneBlock) {
667,283,488✔
904
      break;
905
    }
906

907
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
613,511,858✔
908
    QUERY_CHECK_CODE(code, lino, _end);
613,481,784✔
909
    code = blockDataCheck(pRes);
613,481,784✔
910
    QUERY_CHECK_CODE(code, lino, _end);
613,527,723✔
911
  }
912

913
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
471,283,209✔
914
    pTaskInfo->pSubplan->rowsThreshold -= current;
549,660✔
915
  }
916

917
  *hasMore = (pRes != NULL);
471,322,778✔
918
  uint64_t el = (taosGetTimestampUs() - st);
471,174,847✔
919

920
  pTaskInfo->cost.elapsedTime += el;
471,174,847✔
921
  if (NULL == pRes) {
471,151,709✔
922
    *useconds = pTaskInfo->cost.elapsedTime;
417,381,236✔
923
  }
924

925
_end:
471,304,182✔
926
  (void)cleanUpUdfs();
471,278,774✔
927

928
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
471,380,054✔
929
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
471,380,054✔
930
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
931

932
  atomic_store_64(&pTaskInfo->owner, 0);
471,380,054✔
933
  if (code) {
471,378,575✔
UNCOV
934
    pTaskInfo->code = code;
×
UNCOV
935
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
936
  }
937

938
  return pTaskInfo->code;
471,378,575✔
939
}
940

UNCOV
941
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
UNCOV
942
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
943
  SArray*        pList = pTaskInfo->pResultBlockList;
×
UNCOV
944
  size_t         num = taosArrayGetSize(pList);
×
UNCOV
945
  for (int32_t i = 0; i < num; ++i) {
×
UNCOV
946
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
UNCOV
947
    if (p) {
×
UNCOV
948
      blockDataDestroy(*p);
×
949
    }
950
  }
951

UNCOV
952
  taosArrayClear(pTaskInfo->pResultBlockList);
×
UNCOV
953
}
×
954

955
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
133,504,211✔
956
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
133,504,211✔
957
  int64_t        threadId = taosGetSelfPthreadId();
133,504,211✔
958
  int64_t        curOwner = 0;
133,504,211✔
959

960
  *pRes = NULL;
133,504,211✔
961

962
  // todo extract method
963
  taosRLockLatch(&pTaskInfo->lock);
133,504,211✔
964
  bool isKilled = isTaskKilled(pTaskInfo);
133,503,176✔
965
  if (isKilled) {
133,503,176✔
UNCOV
966
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
967

968
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
969
    return pTaskInfo->code;
×
970
  }
971

972
  if (pTaskInfo->owner != 0) {
133,503,176✔
UNCOV
973
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
974
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
975

UNCOV
976
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
977
    return pTaskInfo->code;
×
978
  }
979

980
  pTaskInfo->owner = threadId;
133,502,831✔
981
  taosRUnLockLatch(&pTaskInfo->lock);
133,502,486✔
982

983
  if (pTaskInfo->cost.start == 0) {
133,504,556✔
984
    pTaskInfo->cost.start = taosGetTimestampUs();
228,072✔
985
  }
986

987
  // error occurs, record the error code and return to client
988
  int32_t ret = setjmp(pTaskInfo->env);
133,504,211✔
989
  if (ret != TSDB_CODE_SUCCESS) {
133,496,276✔
UNCOV
990
    pTaskInfo->code = ret;
×
991
    (void)cleanUpUdfs();
×
992
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
UNCOV
993
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
994
    return pTaskInfo->code;
×
995
  }
996

997
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
133,496,276✔
998

999
  int64_t st = taosGetTimestampUs();
133,501,527✔
1000
  int32_t code = TSDB_CODE_SUCCESS;
133,501,527✔
1001
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
133,501,527✔
UNCOV
1002
    pTaskInfo->paramSet = true;
×
UNCOV
1003
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1004
  } else {
1005
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
133,497,732✔
1006
  }
1007
  if (code) {
133,457,284✔
1008
    pTaskInfo->code = code;
×
1009
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1010
  }
1011

1012
  code = blockDataCheck(*pRes);
133,457,284✔
1013
  if (code) {
133,495,929✔
UNCOV
1014
    pTaskInfo->code = code;
×
UNCOV
1015
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1016
  }
1017

1018
  uint64_t el = (taosGetTimestampUs() - st);
133,471,761✔
1019

1020
  pTaskInfo->cost.elapsedTime += el;
133,471,761✔
1021
  if (NULL == *pRes) {
133,486,901✔
1022
    *useconds = pTaskInfo->cost.elapsedTime;
19,008,297✔
1023
  }
1024

1025
  (void)cleanUpUdfs();
133,488,962✔
1026

1027
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
133,505,936✔
1028
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
133,505,936✔
1029

1030
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
133,505,586✔
1031
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1032

1033
  atomic_store_64(&pTaskInfo->owner, 0);
133,503,171✔
1034
  return pTaskInfo->code;
133,505,591✔
1035
}
1036

1037
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
109,277,366✔
1038
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
109,277,366✔
1039
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
109,280,210✔
1040
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
109,279,328✔
1041

1042
  if (!tmp) {
109,277,184✔
UNCOV
1043
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1044
    return terrno;
×
1045
  }
1046
  return TSDB_CODE_SUCCESS;
109,277,184✔
1047
}
1048

UNCOV
1049
int32_t stopInfoComp(void const* lp, void const* rp) {
×
UNCOV
1050
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
UNCOV
1051
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1052

UNCOV
1053
  if (key->refId < pInfo->refId) {
×
UNCOV
1054
    return -1;
×
UNCOV
1055
  } else if (key->refId > pInfo->refId) {
×
UNCOV
1056
    return 1;
×
1057
  }
1058

1059
  return 0;
×
1060
}
1061

UNCOV
1062
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
UNCOV
1063
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1064
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1065
  if (idx >= 0) {
×
1066
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1067
  }
1068
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1069
}
×
1070

1071
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
71,925✔
1072
  if (pTaskInfo->subJobCtx.hasSubJobs) {
71,925✔
1073
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
20,728✔
1074
    if (pTaskInfo->subJobCtx.param) {
20,728✔
1075
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
9,976✔
1076
    }
1077
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
20,728✔
1078
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
20,728✔
1079
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
20,728✔
1080
  }
1081
  
1082
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
71,925✔
1083

1084
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
71,925✔
1085
  for (int32_t i = 0; i < num; ++i) {
74,557✔
1086
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
2,632✔
1087
    if (!pStop) {
2,632✔
UNCOV
1088
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1089
      continue;
×
1090
    }
1091
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
2,632✔
1092
    if (pExchangeInfo) {
2,632✔
1093
      int32_t code = tsem_post(&pExchangeInfo->ready);
2,632✔
1094
      if (code != TSDB_CODE_SUCCESS) {
2,632✔
UNCOV
1095
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1096
      } else {
1097
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
2,632✔
1098
      }
1099
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
2,632✔
1100
      if (code != TSDB_CODE_SUCCESS) {
2,632✔
UNCOV
1101
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1102
      }
1103
    }
1104
  }
1105

1106
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
71,925✔
1107
}
71,925✔
1108

1109
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
71,925✔
1110
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
71,925✔
1111
  if (pTaskInfo == NULL) {
71,925✔
UNCOV
1112
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1113
  }
1114

1115
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
71,925✔
1116

1117
  setTaskKilled(pTaskInfo, rspCode);
71,925✔
1118
  qStopTaskOperators(pTaskInfo);
71,925✔
1119

1120
  return TSDB_CODE_SUCCESS;
71,925✔
1121
}
1122

UNCOV
1123
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
UNCOV
1124
  int64_t        st = taosGetTimestampMs();
×
UNCOV
1125
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
1126
  if (pTaskInfo == NULL) {
×
1127
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1128
  }
1129

UNCOV
1130
  if (waitDuration > 0) {
×
UNCOV
1131
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1132
  } else {
UNCOV
1133
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1134
  }
1135

UNCOV
1136
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1137

1138
  if (waitDuration > 0) {
×
1139
    while (1) {
1140
      taosWLockLatch(&pTaskInfo->lock);
×
1141
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1142
        taosWUnLockLatch(&pTaskInfo->lock);
×
1143

UNCOV
1144
        taosMsleep(200);
×
1145

1146
        int64_t d = taosGetTimestampMs() - st;
×
UNCOV
1147
        if (d >= waitDuration && waitDuration >= 0) {
×
1148
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
UNCOV
1149
          return TSDB_CODE_SUCCESS;
×
1150
        }
1151
      } else {  // not running now
UNCOV
1152
        pTaskInfo->code = rspCode;
×
1153
        taosWUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1154
        return TSDB_CODE_SUCCESS;
×
1155
      }
1156
    }
1157
  }
1158

1159
  int64_t et = taosGetTimestampMs() - st;
×
UNCOV
1160
  if (et < waitDuration) {
×
1161
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1162
    return TSDB_CODE_SUCCESS;
×
1163
  }
1164
  return TSDB_CODE_SUCCESS;
×
1165
}
1166

1167
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1168
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1169
  if (NULL == pTaskInfo) {
×
UNCOV
1170
    return false;
×
1171
  }
1172

UNCOV
1173
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1174
}
1175

1176
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
377,380,240✔
1177
  STaskCostInfo* pSummary = &pTaskInfo->cost;
377,380,240✔
1178
  int64_t        idleTime = pSummary->start - pSummary->created;
377,432,256✔
1179

1180
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
377,444,292✔
1181
  if (pSummary->pRecoder != NULL) {
377,329,476✔
1182
    qDebug(
276,243,944✔
1183
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1184
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1185
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1186
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1187
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1188
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1189
  } else {
1190
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
101,097,967✔
1191
           pSummary->elapsedTime / 1000.0);
1192
  }
1193
}
377,341,415✔
1194

1195

1196
void qDestroyTask(qTaskInfo_t qTaskHandle) {
390,037,248✔
1197
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
390,037,248✔
1198
  if (pTaskInfo == NULL) {
390,037,248✔
1199
    return;
12,660,386✔
1200
  }
1201

1202
  if (pTaskInfo->pRoot != NULL) {
377,376,862✔
1203
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
377,417,377✔
1204
  } else {
UNCOV
1205
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1206
  }
1207

1208
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
377,418,922✔
1209
  doDestroyTask(pTaskInfo);
377,375,130✔
1210
}
1211

1212
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
14,709,064✔
1213
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
14,709,064✔
1214
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
14,709,064✔
1215
}
1216

1217
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
325,175✔
1218
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
325,175✔
1219
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
325,175✔
1220

1221
  while (1) {
319,140✔
1222
    uint16_t type = pOperator->operatorType;
644,626✔
1223
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
644,626✔
1224
      *scanner = pOperator->info;
325,486✔
1225
      break;
325,486✔
1226
    } else {
1227
      pOperator = pOperator->pDownstream[0];
319,140✔
1228
    }
1229
  }
1230
}
325,486✔
1231

1232
void* qExtractReaderFromTmqScanner(void* scanner) {
325,486✔
1233
  SStreamScanInfo* pInfo = scanner;
325,486✔
1234
  return (void*)pInfo->tqReader;
325,486✔
1235
}
1236

1237
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
744,120✔
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
744,120✔
1239
  return pTaskInfo->streamInfo.schema;
744,120✔
1240
}
1241

1242
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
744,120✔
1243
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
744,120✔
1244
  return pTaskInfo->streamInfo.tbName;
744,120✔
1245
}
1246

1247
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
792,943✔
1248
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
792,943✔
1249
  return &pTaskInfo->streamInfo.btMetaRsp;
792,943✔
1250
}
1251

1252
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
20,201,665✔
1253
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,201,665✔
1254
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
20,201,665✔
1255
  return 0;
20,201,665✔
1256
  /*if (code != TSDB_CODE_SUCCESS) {
1257
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1258
    pTaskInfo->code = code;
1259
    T_LONG_JMP(pTaskInfo->env, code);
1260
  }*/
1261
}
1262

1263
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
752,532✔
1264
  memset(pCond, 0, sizeof(SQueryTableDataCond));
752,532✔
1265
  pCond->order = TSDB_ORDER_ASC;
752,532✔
1266
  pCond->numOfCols = pMtInfo->schema->nCols;
753,732✔
1267
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
755,532✔
1268
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
753,122✔
1269
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
755,252✔
1270
    taosMemoryFreeClear(pCond->colList);
2,410✔
UNCOV
1271
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1272
    return terrno;
×
1273
  }
1274

1275
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
753,442✔
1276
  pCond->suid = pMtInfo->suid;
754,322✔
1277
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
754,042✔
1278
  pCond->startVersion = -1;
754,642✔
1279
  pCond->endVersion = sContext->snapVersion;
752,542✔
1280

1281
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,771,560✔
1282
    SColumnInfo* pColInfo = &pCond->colList[i];
4,015,696✔
1283
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,015,076✔
1284
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,017,218✔
1285
    if (pMtInfo->pExtSchemas != NULL) {
4,015,086✔
1286
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,960✔
1287
    }
1288
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,018,106✔
1289
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,016,938✔
1290

1291
    pCond->pSlotList[i] = i;
4,016,928✔
1292
  }
1293

1294
  return TSDB_CODE_SUCCESS;
756,462✔
1295
}
1296

1297
void qStreamSetOpen(qTaskInfo_t tinfo) {
131,969,364✔
1298
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
131,969,364✔
1299
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
131,969,364✔
1300
  pOperator->status = OP_NOT_OPENED;
131,976,288✔
1301
}
131,994,220✔
1302

1303
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
19,387,292✔
1304
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,387,292✔
1305
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
19,387,292✔
1306
}
19,389,287✔
1307

1308
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
20,360,383✔
1309
  int32_t        code = TSDB_CODE_SUCCESS;
20,360,383✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,360,383✔
1311
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
20,360,383✔
1312

1313
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
20,361,653✔
1314
  const char*    id = GET_TASKID(pTaskInfo);
20,360,597✔
1315

1316
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
20,360,634✔
1317
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
19,091,870✔
1318
    if (pOperator == NULL || code != 0) {
19,090,559✔
1319
      return code;
298✔
1320
    }
1321

1322
    SStreamScanInfo* pInfo = pOperator->info;
19,090,261✔
1323
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
19,091,302✔
1324
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
19,092,542✔
1325
    walReaderVerifyOffset(pWalReader, pOffset);
19,092,868✔
1326
  }
1327
  // if pOffset equal to current offset, means continue consume
1328
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
20,358,316✔
1329
    return 0;
18,561,560✔
1330
  }
1331

1332
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,798,451✔
1333
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,026,806✔
1334
    if (pOperator == NULL || code != 0) {
1,026,806✔
UNCOV
1335
      return code;
×
1336
    }
1337

1338
    SStreamScanInfo* pInfo = pOperator->info;
1,027,117✔
1339
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,026,798✔
1340
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,026,193✔
1341
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,026,490✔
1342

1343
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,026,240✔
1344
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
828,723✔
1345
      pScanBaseInfo->dataReader = NULL;
828,723✔
1346

1347
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
828,723✔
1348
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
828,723✔
1349
      walReaderVerifyOffset(pWalReader, pOffset);
828,430✔
1350
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
828,723✔
1351
      if (code < 0) {
828,723✔
1352
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
9,590✔
1353
        return code;
9,590✔
1354
      }
1355
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
197,158✔
1356
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1357
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1358
      int64_t uid = pOffset->uid;
198,078✔
1359
      int64_t ts = pOffset->ts;
197,759✔
1360
      int32_t index = 0;
197,437✔
1361

1362
      // this value may be changed if new tables are created
1363
      taosRLockLatch(&pTaskInfo->lock);
197,437✔
1364
      int32_t numOfTables = 0;
198,394✔
1365
      code = tableListGetSize(pTableListInfo, &numOfTables);
198,394✔
1366
      if (code != TSDB_CODE_SUCCESS) {
197,440✔
UNCOV
1367
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1368
        taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1369
        return code;
×
1370
      }
1371

1372
      if (uid == 0) {
197,440✔
1373
        if (numOfTables != 0) {
192,225✔
1374
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,745✔
1375
          if (!tmp) {
38,455✔
UNCOV
1376
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1377
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1378
            return terrno;
×
1379
          }
1380
          if (tmp) uid = tmp->uid;
38,455✔
1381
          ts = INT64_MIN;
38,165✔
1382
          pScanInfo->currentTable = 0;
38,165✔
1383
        } else {
1384
          taosRUnLockLatch(&pTaskInfo->lock);
153,480✔
1385
          qError("no table in table list, %s", id);
154,110✔
1386
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
154,429✔
1387
        }
1388
      }
1389
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
43,641✔
1390

1391
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
43,651✔
1392
             pInfo->pTableScanOp->resultInfo.totalRows);
1393
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
43,965✔
1394

1395
      // start from current accessed position
1396
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1397
      // position, let's find it from the beginning.
1398
      index = tableListFind(pTableListInfo, uid, 0);
43,965✔
1399
      taosRUnLockLatch(&pTaskInfo->lock);
43,965✔
1400

1401
      if (index >= 0) {
43,965✔
1402
        pScanInfo->currentTable = index;
43,965✔
1403
      } else {
UNCOV
1404
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1405
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1406
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1407
      }
1408

1409
      STableKeyInfo keyInfo = {.uid = uid};
43,965✔
1410
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
43,965✔
1411

1412
      // let's start from the next ts that returned to consumer.
1413
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
43,965✔
1414
        pScanBaseInfo->cond.twindows.skey = ts;
924✔
1415
      } else {
1416
        pScanBaseInfo->cond.twindows.skey = ts + 1;
43,041✔
1417
      }
1418
      pScanInfo->scanTimes = 0;
43,965✔
1419

1420
      if (pScanBaseInfo->dataReader == NULL) {
43,965✔
1421
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
76,424✔
1422
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1423
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
38,349✔
1424
        if (code != TSDB_CODE_SUCCESS) {
38,349✔
UNCOV
1425
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
UNCOV
1426
          return code;
×
1427
        }
1428

1429
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
38,349✔
1430
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1431
      } else {
1432
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
5,616✔
1433
        if (code != TSDB_CODE_SUCCESS) {
5,616✔
UNCOV
1434
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1435
          return code;
×
1436
        }
1437

1438
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
5,616✔
1439
        if (code != TSDB_CODE_SUCCESS) {
5,616✔
1440
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1441
          return code;
×
1442
        }
1443
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
5,616✔
1444
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1445
      }
1446

1447
      // restore the key value
1448
      pScanBaseInfo->cond.twindows.skey = oldSkey;
43,965✔
1449
    } else {
1450
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
UNCOV
1451
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1452
    }
1453

1454
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1455
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
771,645✔
1456
      SStreamRawScanInfo* pInfo = pOperator->info;
757,317✔
1457
      SSnapContext*       sContext = pInfo->sContext;
757,317✔
1458
      SOperatorInfo*      p = NULL;
757,317✔
1459

1460
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
757,317✔
1461
      if (code != 0) {
757,317✔
UNCOV
1462
        return code;
×
1463
      }
1464

1465
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
757,317✔
1466

1467
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
757,317✔
UNCOV
1468
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
UNCOV
1469
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1470
      }
1471

1472
      SMetaTableInfo mtInfo = {0};
757,317✔
1473
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
757,317✔
1474
      if (code != 0) {
756,717✔
1475
        destroyMetaTableInfo(&mtInfo);
UNCOV
1476
        return code;
×
1477
      }
1478
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
756,717✔
1479
      pInfo->dataReader = NULL;
754,907✔
1480

1481
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
754,917✔
1482
      tableListClear(pTableListInfo);
755,197✔
1483

1484
      if (mtInfo.uid == 0) {
757,317✔
1485
        destroyMetaTableInfo(&mtInfo);
1486
        goto end;  // no data
855✔
1487
      }
1488

1489
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
756,462✔
1490
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
754,962✔
1491
      if (code != TSDB_CODE_SUCCESS) {
755,542✔
UNCOV
1492
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1493
        destroyMetaTableInfo(&mtInfo);
UNCOV
1494
        return code;
×
1495
      }
1496
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
755,542✔
1497
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,318✔
1498
      } else {
1499
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
754,234✔
1500
      }
1501

1502
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
754,354✔
1503
      if (code != TSDB_CODE_SUCCESS) {
756,462✔
1504
        destroyMetaTableInfo(&mtInfo);
UNCOV
1505
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1506
        return code;
×
1507
      }
1508

1509
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
756,462✔
1510
      if (!pList) {
756,462✔
UNCOV
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
UNCOV
1513
        return code;
×
1514
      }
1515
      int32_t size = 0;
756,462✔
1516
      code = tableListGetSize(pTableListInfo, &size);
756,462✔
1517
      if (code != TSDB_CODE_SUCCESS) {
756,462✔
UNCOV
1518
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1519
        destroyMetaTableInfo(&mtInfo);
1520
        return code;
×
1521
      }
1522

1523
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,512,924✔
1524
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
756,462✔
1525
      if (code != TSDB_CODE_SUCCESS) {
754,952✔
1526
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1527
        destroyMetaTableInfo(&mtInfo);
1528
        return code;
×
1529
      }
1530

1531
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
754,952✔
1532
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
755,562✔
1533
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1534
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
754,052✔
1535
      pTaskInfo->streamInfo.schema = mtInfo.schema;
754,952✔
1536
      taosMemoryFreeClear(mtInfo.pExtSchemas);
755,552✔
1537

1538
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
755,552✔
1539
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
14,328✔
1540
      SStreamRawScanInfo* pInfo = pOperator->info;
4,857✔
1541
      SSnapContext*       sContext = pInfo->sContext;
4,857✔
1542
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,857✔
1543
      if (code != 0) {
4,857✔
UNCOV
1544
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
UNCOV
1545
        return code;
×
1546
      }
1547
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,857✔
1548
             id);
1549
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,471✔
1550
      SStreamRawScanInfo* pInfo = pOperator->info;
9,471✔
1551
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,471✔
1552
      pInfo->dataReader = NULL;
9,471✔
1553
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,471✔
1554
    }
1555
  }
1556

1557
end:
1,634,637✔
1558
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,634,743✔
1559
  return 0;
1,634,743✔
1560
}
1561

1562
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
343,468,320✔
1563
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
343,468,320✔
1564
  if (pMsg->info.ahandle == NULL) {
343,515,948✔
UNCOV
1565
    rpcFreeCont(pMsg->pCont);
×
UNCOV
1566
    qError("pMsg->info.ahandle is NULL");
×
UNCOV
1567
    return;
×
1568
  }
1569

1570
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
343,471,968✔
1571
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1572

1573
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
343,479,961✔
1574

1575
  if (pMsg->contLen > 0) {
343,507,770✔
1576
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
343,456,689✔
1577
    if (buf.pData == NULL) {
343,382,084✔
UNCOV
1578
      pMsg->code = terrno;
×
1579
    } else {
1580
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
343,382,084✔
1581
    }
1582
  }
1583

1584
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
343,501,356✔
1585
  rpcFreeCont(pMsg->pCont);
343,546,589✔
1586
  destroySendMsgInfo(pSendInfo);
343,467,729✔
1587
}
1588

UNCOV
1589
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
UNCOV
1590
  int32_t        code = TSDB_CODE_SUCCESS;
×
UNCOV
1591
  int32_t        lino = 0;
×
UNCOV
1592
  SExecTaskInfo* pTaskInfo = tinfo;
×
1593
  SArray*        plist = NULL;
×
1594

UNCOV
1595
  code = getTableListInfo(pTaskInfo, &plist);
×
UNCOV
1596
  if (code || plist == NULL) {
×
UNCOV
1597
    return NULL;
×
1598
  }
1599

1600
  // only extract table in the first elements
UNCOV
1601
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1602

UNCOV
1603
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1604
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1605

1606
  int32_t numOfTables = 0;
×
1607
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1608
  QUERY_CHECK_CODE(code, lino, _end);
×
1609

1610
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1611
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1612
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
UNCOV
1613
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
UNCOV
1614
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1615
  }
1616

UNCOV
1617
  taosArrayDestroy(plist);
×
1618

1619
_end:
×
UNCOV
1620
  if (code != TSDB_CODE_SUCCESS) {
×
1621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1622
    T_LONG_JMP(pTaskInfo->env, code);
×
1623
  }
UNCOV
1624
  return pUidList;
×
1625
}
1626

1627
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,361,830✔
1628
  int32_t        code = TSDB_CODE_SUCCESS;
3,361,830✔
1629
  int32_t        lino = 0;
3,361,830✔
1630
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,361,830✔
1631

1632
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,363,998✔
UNCOV
1633
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1634
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1635

1636
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1637
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1638
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,361,288✔
1639
    STableScanInfo* pScanInfo = pOperator->info;
1,681,457✔
1640

1641
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,681,999✔
1642
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,681,457✔
1643
  } else {
1644
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,681,452✔
1645
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,681,452✔
1646
    }
1647
  }
1648

1649
_end:
×
1650
  if (code != TSDB_CODE_SUCCESS) {
3,361,283✔
1651
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1652
  }
1653
  return code;
3,361,283✔
1654
}
1655

1656
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,681,452✔
1657
  if (pList == NULL) {
1,681,452✔
UNCOV
1658
    return TSDB_CODE_INVALID_PARA;
×
1659
  }
1660

1661
  *pList = NULL;
1,681,452✔
1662
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,681,994✔
1663
  if (pArray == NULL) {
1,680,910✔
1664
    return terrno;
×
1665
  }
1666

1667
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,680,910✔
1668
  if (code == 0) {
1,680,910✔
1669
    *pList = pArray;
1,680,910✔
1670
  } else {
UNCOV
1671
    taosArrayDestroy(pArray);
×
1672
  }
1673
  return code;
1,679,826✔
1674
}
1675

UNCOV
1676
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
UNCOV
1677
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1678
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1679
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1680
  }
UNCOV
1681
  return 0;
×
1682
}
1683

UNCOV
1684
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
UNCOV
1685
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1686
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
UNCOV
1687
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1688
  }
UNCOV
1689
  return 0;
×
1690
}
1691

1692
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1693
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1694

UNCOV
1695
  int32_t code = pTaskInfo->code;
×
1696
  pTaskInfo->code = 0;
×
UNCOV
1697
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
UNCOV
1698
}
×
1699

1700
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1701
  int32_t code = 0;
×
1702
  return code;
×
1703
}
1704

UNCOV
1705
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
UNCOV
1706
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1707
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1708
  return code;
×
1709
}
1710

1711
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
28,705,858✔
1712
  int32_t code = 0;
28,705,858✔
1713

1714
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
28,705,858✔
1715
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
28,705,891✔
1716

1717
  if (pOper->fpSet.resetStateFn) {
28,705,947✔
1718
    code = pOper->fpSet.resetStateFn(pOper);
28,706,133✔
1719
  }
1720
  pOper->status = OP_NOT_OPENED;
28,700,856✔
1721
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
49,415,609✔
1722
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,707,152✔
1723
  }
1724
  return code;
28,710,681✔
1725
}
1726

1727
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,999,564✔
1728
  int32_t        code = 0;
7,999,564✔
1729
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,999,564✔
1730
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,999,564✔
1731
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,999,996✔
1732
  code = clearStatesForOperator(pOper);
7,999,997✔
1733
  return code;
7,999,726✔
1734
}
1735

1736
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,919,492✔
1737
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,919,492✔
1738
  int64_t        threadId = taosGetSelfPthreadId();
10,919,492✔
1739
  int64_t        curOwner = 0;
10,919,276✔
1740

1741
  *ppRes = NULL;
10,919,276✔
1742

1743
  // todo extract method
1744
  taosRLockLatch(&pTaskInfo->lock);
10,919,276✔
1745
  bool isKilled = isTaskKilled(pTaskInfo);
10,919,710✔
1746
  if (isKilled) {
10,919,710✔
1747
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1748
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1749

UNCOV
1750
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1751
    return pTaskInfo->code;
×
1752
  }
1753

1754
  if (pTaskInfo->owner != 0) {
10,919,710✔
UNCOV
1755
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1756
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1757

UNCOV
1758
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1759
    return pTaskInfo->code;
×
1760
  }
1761

1762
  pTaskInfo->owner = threadId;
10,919,216✔
1763
  taosRUnLockLatch(&pTaskInfo->lock);
10,919,710✔
1764

1765
  if (pTaskInfo->cost.start == 0) {
10,920,453✔
1766
    pTaskInfo->cost.start = taosGetTimestampUs();
241,966✔
1767
  }
1768

1769
  // error occurs, record the error code and return to client
1770
  int32_t ret = setjmp(pTaskInfo->env);
10,920,453✔
1771
  if (ret != TSDB_CODE_SUCCESS) {
13,030,538✔
1772
    pTaskInfo->code = ret;
2,112,891✔
1773
    (void)cleanUpUdfs();
2,112,891✔
1774
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,112,891✔
1775
    atomic_store_64(&pTaskInfo->owner, 0);
2,112,891✔
1776
    return pTaskInfo->code;
2,112,891✔
1777
  }
1778

1779
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,917,647✔
1780

1781
  int64_t st = taosGetTimestampUs();
10,920,014✔
1782

1783
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,920,014✔
1784
  if (code) {
8,807,562✔
UNCOV
1785
    pTaskInfo->code = code;
×
UNCOV
1786
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1787
  } else {
1788
    *finished = *ppRes == NULL;
8,807,562✔
1789
    code = blockDataCheck(*ppRes);
8,807,562✔
1790
  }
1791
  if (code) {
8,807,562✔
UNCOV
1792
    pTaskInfo->code = code;
×
UNCOV
1793
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1794
  }
1795

1796
  uint64_t el = (taosGetTimestampUs() - st);
8,807,344✔
1797

1798
  pTaskInfo->cost.elapsedTime += el;
8,807,344✔
1799
  if (NULL == *ppRes) {
8,807,344✔
1800
    *useconds = pTaskInfo->cost.elapsedTime;
5,481,949✔
1801
  }
1802

1803
  (void)cleanUpUdfs();
8,807,344✔
1804

1805
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,807,562✔
1806
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,807,562✔
1807

1808
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,807,562✔
1809
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1810

1811
  atomic_store_64(&pTaskInfo->owner, 0);
8,807,562✔
1812
  return pTaskInfo->code;
8,807,312✔
1813
}
1814

1815
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1816
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1817
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1818
// }
1819

1820
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
255,311✔
1821
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1822
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1823
  int32_t code = 0;                                        
255,311✔
1824
  if (*pTableListInfo != NULL) {
255,311✔
UNCOV
1825
    qDebug("table list already exists, no need to create again");
×
UNCOV
1826
    goto end;
×
1827
  }
1828
  STableListInfo* pList = tableListCreate();
255,311✔
1829
  if (pList == NULL) {
255,311✔
UNCOV
1830
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1831
    code = terrno;
×
UNCOV
1832
    goto end;
×
1833
  }
1834

1835
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
255,311✔
1836
  SReadHandle    pHandle = {.vnode = pVnode};
255,311✔
1837
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
255,311✔
1838

1839
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
255,311✔
1840
  if (code != 0) {
255,311✔
1841
    tableListDestroy(pList);
×
UNCOV
1842
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
UNCOV
1843
    goto end;
×
1844
  }
1845
  *pTableListInfo = pList;
255,311✔
1846

1847
end:
255,311✔
1848
  return 0;
255,311✔
1849
}
1850

1851
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
37,715✔
1852
  bool   listAdded = false;
37,715✔
1853
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
37,715✔
1854
  if (code == 0 && !listAdded) {
37,715✔
1855
    int32_t numOfTables = taosArrayGetSize(pUidList);
31,645✔
1856
    for (int i = 0; i < numOfTables; i++) {
93,368✔
1857
      void* tmp = taosArrayGet(pUidList, i);
61,723✔
1858
      if (tmp == NULL) {
61,723✔
UNCOV
1859
        return terrno;
×
1860
      }
1861
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
61,723✔
1862

1863
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
61,723✔
1864
      if (p == NULL) {
61,723✔
UNCOV
1865
        return terrno;
×
1866
      }
1867
    }
1868
  }
1869
  return code;
37,715✔
1870
}
1871

1872
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
29,867✔
1873
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1874
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1875
  int32_t code = TSDB_CODE_SUCCESS;
29,867✔
1876
  STableListInfo* pList = tableListCreate();
29,867✔
1877
  if (pList == NULL) {
29,867✔
UNCOV
1878
    code = terrno;
×
UNCOV
1879
    goto end;
×
1880
  }
1881
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
29,867✔
1882
  if (uidListCopy == NULL) {
29,867✔
UNCOV
1883
    code = terrno;
×
UNCOV
1884
    goto end;
×
1885
  }
1886
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
29,867✔
1887
  SReadHandle    pHandle = {.vnode = pVnode};
29,867✔
1888

1889
  pList->idInfo.suid = suid;
29,867✔
1890
  pList->idInfo.tableType = TD_SUPER_TABLE;
29,867✔
1891
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
29,867✔
1892
  if (code != TSDB_CODE_SUCCESS) {
29,867✔
1893
    goto end;
×
1894
  }                                              
1895
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
29,867✔
1896
  if (code != TSDB_CODE_SUCCESS) {
29,867✔
UNCOV
1897
    goto end;
×
1898
  }
1899
  *tableList = pList->pTableList;
29,867✔
1900
  pList->pTableList = NULL;
29,867✔
1901

1902
  taosArrayClear(uidList);
29,867✔
1903
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
59,734✔
1904
    void* tmp = taosArrayGet(uidListCopy, i);
29,867✔
1905
    if (tmp == NULL) {
29,867✔
UNCOV
1906
      continue;
×
1907
    }
1908
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
29,867✔
1909
    if (slot == NULL) {
29,867✔
1910
      if (taosArrayPush(uidList, tmp) == NULL) {
3,399✔
UNCOV
1911
        code = terrno;
×
1912
        goto end;
×
1913
      }
1914
    }
1915
  }
1916
end:
29,867✔
1917
  taosArrayDestroy(uidListCopy);
29,867✔
1918
  tableListDestroy(pList);
29,867✔
1919
  return code;
29,867✔
1920
}
1921

1922
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1923
//   int32_t index = -1;
1924
//   (void)taosThreadRwlockRdlock(lock);
1925
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1926
//     index = 0;
1927
//     goto end;
1928
//   }
1929
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1930
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1931

1932
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1933
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1934
//       index = i;
1935
//       goto end;
1936
//     }
1937
//   }
1938
// end:
1939
//   (void)taosThreadRwlockUnlock(lock);
1940
//   return index;
1941
// }
1942

1943
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
255,311✔
1944
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
255,311✔
1945
  STableListInfo* pList = pTableListInfo;
255,311✔
1946
  return pList->pTableList;
255,311✔
1947
}
1948

1949
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,361,294✔
1950

1951
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,560,310✔
1952
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,560,310✔
1953
  qDestroyTask(tInfo);
2,560,310✔
1954
}
2,560,310✔
1955

1956
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
618,783✔
1957
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
618,783✔
1958
}
1959

1960
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
656,863✔
1961
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1962
  int32_t      code = 0;
656,863✔
1963
  SNode*       pNode = 0;
656,863✔
1964
  SNodeList*   pList = NULL;
656,863✔
1965
  SExprInfo*   pExprInfo = NULL;
656,863✔
1966
  int32_t      numOfExprs = 1;
656,863✔
1967
  int32_t*     offset = 0;
656,863✔
1968
  STargetNode* pTargetNode = NULL;
656,863✔
1969
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
656,863✔
1970
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
656,613✔
1971

1972
  if (code == 0) {
656,863✔
1973
    pTargetNode->dataBlockId = 0;
656,863✔
1974
    pTargetNode->pExpr = pNode;
656,863✔
1975
    pTargetNode->slotId = 0;
656,863✔
1976
  }
1977
  if (code == 0) {
656,863✔
1978
    code = nodesMakeList(&pList);
656,863✔
1979
  }
1980
  if (code == 0) {
656,863✔
1981
    code = nodesListAppend(pList, (SNode*)pTargetNode);
656,863✔
1982
  }
1983
  if (code == 0) {
656,863✔
1984
    pNode = NULL;
656,640✔
1985
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
656,640✔
1986
  }
1987

1988
  if (code == 0) {
657,086✔
1989
    const char* pVal = NULL;
656,863✔
1990
    int32_t     len = 0;
656,863✔
1991
    SNode*      pSclNode = NULL;
656,863✔
1992
    switch (pExprInfo->pExpr->nodeType) {
656,863✔
1993
      case QUERY_NODE_FUNCTION:
656,613✔
1994
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
656,613✔
1995
        break;
656,613✔
UNCOV
1996
      case QUERY_NODE_OPERATOR:
×
UNCOV
1997
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
1998
        break;
×
UNCOV
1999
      default:
×
UNCOV
2000
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2001
        break;
×
2002
    }
2003
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
656,613✔
2004
    SSDataBlock block = {0};
656,863✔
2005
    block.info.rows = 1;
656,863✔
2006
    SSDataBlock* pBlock = &block;
656,863✔
2007
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
656,863✔
2008
    if (tmp == NULL) {
656,863✔
UNCOV
2009
      code = terrno;
×
2010
    }
2011
    if (code == 0) {
656,863✔
2012
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
656,863✔
2013
      gTaskScalarExtra.pStreamRange = NULL;
656,863✔
2014
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
656,863✔
2015
    }
2016
    taosArrayDestroy(pBlockList);
656,863✔
2017
  }
2018
  nodesDestroyList(pList);
657,086✔
2019
  destroyExprInfo(pExprInfo, numOfExprs);
656,863✔
2020
  taosMemoryFreeClear(pExprInfo);
656,863✔
2021
  return code;
656,863✔
2022
}
2023

2024
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,159,719✔
2025
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,159,719✔
2026
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,159,719✔
2027
  int32_t        code = 0;
2,159,719✔
2028
  SNode*         pNode = NULL;
2,159,719✔
2029
  if (!pForceOutputCols) return 0;
2,159,719✔
2030
  if (!*pRes) {
38,080✔
2031
    code = createDataBlock(pRes);
38,080✔
2032
  }
2033

2034
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
38,080✔
2035
    int32_t idx = 0;
38,080✔
2036
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
152,133✔
2037
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
114,053✔
2038
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
114,053✔
2039
      colInfo.info.precision = pCol->type.precision;
114,053✔
2040
      colInfo.info.scale = pCol->type.scale;
114,053✔
2041
      code = blockDataAppendColInfo(*pRes, &colInfo);
114,053✔
2042
      if (code != 0) break;
114,053✔
2043
    }
2044
  }
2045

2046
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
38,080✔
2047
  if (code != TSDB_CODE_SUCCESS) {
38,080✔
UNCOV
2048
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
2049
    return code;
×
2050
  }
2051

2052
  // loop all exprs for force output, execute all exprs
2053
  int32_t idx = 0;
38,080✔
2054
  int32_t rowIdx = (*pRes)->info.rows;
38,080✔
2055
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
38,080✔
2056
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
38,080✔
2057
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
152,133✔
2058
    SScalarParam   dst = {0};
114,053✔
2059
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
114,053✔
2060
    code = nodesStringToNode(pCol->expr, &pNode);
114,053✔
2061
    if (code != 0) break;
114,053✔
2062
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
114,053✔
2063
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
114,053✔
2064
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
75,973✔
2065
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
75,973✔
2066
    } else {
2067
      dst.columnData = pInfo;
38,080✔
2068
      dst.numOfRows = rowIdx;
38,080✔
2069
      dst.colAlloced = false;
38,080✔
2070
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
38,080✔
2071
    }
2072
    ++idx;
114,053✔
2073
    // TODO sclFreeParam(&dst);
2074
    nodesDestroyNode(pNode);
114,053✔
2075
    if (code != 0) break;
114,053✔
2076
  }
2077
  if (code == TSDB_CODE_SUCCESS) {
38,080✔
2078
    (*pRes)->info.rows++;
38,080✔
2079
  }
2080
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
38,080✔
2081
  return code;
38,080✔
2082
}
2083

2084
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
240,866✔
2085
  int32_t      code = 0;
240,866✔
2086
  const char*  pVal = NULL;
240,866✔
2087
  SScalarParam dst = {0};
240,866✔
2088
  int32_t      len = 0;
240,866✔
2089
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
240,866✔
2090
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
240,866✔
2091
  // execute the expr
2092
  switch (pExpr->type) {
240,866✔
UNCOV
2093
    case QUERY_NODE_VALUE: {
×
UNCOV
2094
      SValueNode* pValue = (SValueNode*)pExpr;
×
UNCOV
2095
      int32_t     type = pValue->node.resType.type;
×
UNCOV
2096
      if (!IS_STR_DATA_TYPE(type)) {
×
UNCOV
2097
        qError("invalid sub tb expr with non-str type");
×
UNCOV
2098
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2099
        break;
×
2100
      }
UNCOV
2101
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
UNCOV
2102
      if (pTmp == NULL) {
×
UNCOV
2103
        qError("invalid sub tb expr with null value");
×
UNCOV
2104
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2105
        break;
×
2106
      }
UNCOV
2107
      pVal = varDataVal(pTmp);
×
2108
      len = varDataLen(pTmp);
×
2109
    } break;
×
2110
    case QUERY_NODE_FUNCTION: {
240,866✔
2111
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
240,866✔
2112
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
240,866✔
2113
        qError("invalid sub tb expr with non-str type func");
×
2114
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2115
        break;
×
2116
      }
2117
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
240,866✔
2118
      if (!pCol) {
240,866✔
2119
        code = terrno;
×
2120
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2121
        break;
×
2122
      }
2123

2124
      pCol->hasNull = true;
240,866✔
2125
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
240,866✔
2126
      pCol->info.colId = 0;
240,616✔
2127
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
240,866✔
2128
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
240,866✔
2129
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
240,866✔
2130
      code = colInfoDataEnsureCapacity(pCol, 1, true);
240,616✔
2131
      if (code != 0) {
240,866✔
UNCOV
2132
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2133
        taosMemoryFree(pCol);
×
2134
        break;
×
2135
      }
2136
      dst.columnData = pCol;
240,866✔
2137
      dst.numOfRows = 1;
240,866✔
2138
      dst.colAlloced = true;
240,866✔
2139
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
240,866✔
2140
      if (colDataIsNull_var(dst.columnData, 0)) {
240,866✔
2141
        qInfo("invalid sub tb expr with null value");
2,158✔
2142
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,490✔
2143
      }
2144
      if (code == 0) {
240,866✔
2145
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
238,376✔
2146
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
238,126✔
2147
      }
2148
    } break;
240,616✔
2149
    default:
×
UNCOV
2150
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2151
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2152
      break;
×
2153
  }
2154
  if (code == 0) {
240,616✔
2155
    if (!pVal || len == 0) {
238,376✔
UNCOV
2156
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2157
      code = TSDB_CODE_INVALID_PARA;
×
2158
    }
2159
    if(len > TSDB_TABLE_NAME_LEN - 1) {
238,376✔
2160
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
434✔
2161
      len = TSDB_TABLE_NAME_LEN - 1;
434✔
2162
    }
2163

2164
    memcpy(tbname, pVal, len);
238,376✔
2165
    tbname[len] = '\0';  // ensure null terminated
238,376✔
2166
    if (NULL != strchr(tbname, '.')) {
238,376✔
2167
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
UNCOV
2168
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2169
    }
2170
  }
2171
  // TODO free dst
2172
  sclFreeParam(&dst);
240,616✔
2173
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
240,866✔
2174
  return code;
240,866✔
2175
}
2176

2177
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
235,348✔
2178
  if (pParam) {
235,348✔
2179
    if (pParam->tbname) {
235,348✔
2180
      taosMemFree(pParam->tbname);
235,348✔
2181
      pParam->tbname = NULL;
235,348✔
2182
    }
2183
    if (pParam->stbname) {
235,348✔
2184
      taosMemFree(pParam->stbname);
235,348✔
2185
      pParam->stbname = NULL;
235,348✔
2186
    }
2187
    if (pParam->dbFName) {
235,348✔
2188
      taosMemFree(pParam->dbFName);
235,348✔
2189
      pParam->dbFName = NULL;
235,348✔
2190
    }
2191
    if (pParam->pFields) {
235,348✔
2192
      taosArrayDestroy(pParam->pFields);
235,348✔
2193
      pParam->pFields = NULL;
235,348✔
2194
    }
2195
    if (pParam->pTagFields) {
235,348✔
2196
      taosArrayDestroy(pParam->pTagFields);
138,706✔
2197
      pParam->pTagFields = NULL;
138,706✔
2198
    }
2199
    if (pParam->colCids) {
235,348✔
2200
      taosArrayDestroy(pParam->colCids);
1,753✔
2201
      pParam->colCids = NULL;
1,753✔
2202
    }
2203
    if (pParam->tagCids) {
235,348✔
2204
      taosArrayDestroy(pParam->tagCids);
1,118✔
2205
      pParam->tagCids = NULL;
1,118✔
2206
    }
2207
    taosMemFree(pParam);
235,348✔
2208
  }
2209
}
235,348✔
2210

2211
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
235,132✔
2212
  int32_t code = 0, lino = 0;
235,132✔
2213
  if (ppDst == NULL || pSrc == NULL) {
235,132✔
UNCOV
2214
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2215
  }
2216
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
235,132✔
2217
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
235,132✔
2218

2219
  (*ppDst)->suid = pSrc->suid;
235,132✔
2220
  (*ppDst)->sver = pSrc->sver;
235,348✔
2221
  (*ppDst)->tbType = pSrc->tbType;
235,132✔
2222
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
235,132✔
2223
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
235,348✔
2224

2225
  if (pSrc->stbname) {
235,132✔
2226
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
235,348✔
2227
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
235,348✔
2228
  }
2229

2230
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
235,348✔
2231
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
235,348✔
2232

2233
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
234,916✔
2234

2235
  if (pSrc->pFields && pSrc->pFields->size > 0) {
234,916✔
2236
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
235,348✔
2237
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
235,098✔
2238
  } else {
UNCOV
2239
    (*ppDst)->pFields = NULL;
×
2240
  }
2241
  
2242
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
234,882✔
2243
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
138,490✔
2244
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
138,706✔
2245
  } else {
2246
    (*ppDst)->pTagFields = NULL;
96,426✔
2247
  }
2248

2249
  if (pSrc->colCids && pSrc->colCids->size > 0) {
234,916✔
2250
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,753✔
2251
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,753✔
2252
  } else {
2253
    (*ppDst)->colCids = NULL;
233,163✔
2254
  }
2255

2256
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
235,132✔
2257
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,118✔
2258
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,118✔
2259
  } else {
2260
    (*ppDst)->tagCids = NULL;
234,014✔
2261
  }
2262

2263
_exit:
235,132✔
2264

2265
  if (code != 0) {
235,132✔
UNCOV
2266
    if (*ppDst) {
×
UNCOV
2267
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2268
      *ppDst = NULL;
×
2269
    }
2270
    
UNCOV
2271
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2272
  }
2273
  return code;
235,348✔
2274
}
2275

2276
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
250✔
2277
  return doDropStreamTable(pMsgCb, pOutput, pReq);
250✔
2278
}
2279

UNCOV
2280
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2281
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2282
}
2283

2284
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
7,848✔
2285
  int32_t         code = TSDB_CODE_SUCCESS;
7,848✔
2286
  STableListInfo* pList = tableListCreate();
7,848✔
2287
  if (pList == NULL) {
7,848✔
UNCOV
2288
    code = terrno;
×
UNCOV
2289
    goto end;
×
2290
  }
2291
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2292
  // if (uidListCopy == NULL) {
2293
  //   code = terrno;
2294
  //   goto end;
2295
  // }
2296

2297
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
7,848✔
2298
  pList->idInfo.suid = suid;
7,848✔
2299
  pList->idInfo.tableType = TD_SUPER_TABLE;
7,848✔
2300
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
7,848✔
2301
  if (code != TSDB_CODE_SUCCESS) {
7,848✔
UNCOV
2302
    goto end;
×
2303
  }
2304
  // taosArrayClear(uidList);
2305
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2306
  //   void* tmp = taosArrayGet(uidListCopy, i);
2307
  //   if (tmp == NULL) {
2308
  //     continue;
2309
  //   }
2310
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2311
  //   if (slot == NULL) {
2312
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2313
  //       code = terrno;
2314
  //       goto end;
2315
  //     }
2316
  //   }
2317
  // }
2318
end:
7,848✔
2319
  // taosArrayDestroy(uidListCopy);
2320
  tableListDestroy(pList);
7,848✔
2321
  return code;
7,848✔
2322
}
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