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

taosdata / TDengine / #4946

02 Feb 2026 09:27AM UTC coverage: 66.932% (+0.06%) from 66.87%
#4946

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

413 existing lines in 119 files now uncovered.

205724 of 307364 relevant lines covered (66.93%)

126528878.24 hits per line

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

69.67
/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) {
578,296,860✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
578,296,860✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
578,296,860✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
578,386,112✔
47
}
578,286,136✔
48

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

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

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

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

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
48,403,785✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
48,403,785✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
48,405,111✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
22,324,116✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
22,323,862✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
22,323,180✔
162
      if (pScanInfo->base.dataReader != NULL) {
22,324,423✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
262,205✔
164
        if (code) {
261,896✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
26,083,297✔
172
  }
173

174
  return 0;
25,551,148✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
25,551,571✔
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) {
443,539✔
213
  if (msg == NULL) {  // create raw scan
443,539✔
214
    SExecTaskInfo* pTaskInfo = NULL;
118,860✔
215

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

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

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

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

248
  return pTaskInfo;
325,295✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
533,980✔
252
  if (streamInserterParam == NULL) {
533,980✔
253
    return TSDB_CODE_SUCCESS;
305,115✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
228,865✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
228,865✔
323
    if (code) {
228,865✔
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,
533,980✔
328
         tstrerror(code));
329

330
_error:
75,802✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,694,357✔
342
  if (pInfo == NULL) {
2,694,357✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,694,357✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,694,357✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,694,357✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,694,357✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,417,752✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
443,254✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,112,109✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,694,357✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
276,605✔
382
    setReadHandle(handle, pScanBaseInfo);
276,605✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,417,752✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
1,974,498✔
385
    setReadHandle(handle, pScanBaseInfo);
1,974,498✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
534,195✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
373,235✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
373,235✔
444

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

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

454
  locked = 1;
373,235✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
777,961✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
404,726✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
404,726✔
458

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

465
    tDecoderClear(&mr.coder);
404,726✔
466

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
373,235✔
524

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

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

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

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

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

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

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

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

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

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

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

638
  return code;
373,872✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
449,477,365✔
UNCOV
647
    return TSDB_CODE_INVALID_PARA;
×
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
449,521,978✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
449,521,978✔
652
    return TSDB_CODE_SUCCESS;
264,617,894✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
184,714,782✔
662
  *tversion = pSchemaInfo->tversion;
184,869,436✔
663
  *rversion = pSchemaInfo->rversion;
184,855,715✔
664
  if (pSchemaInfo->dbname) {
184,941,765✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
184,936,322✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
185,031,111✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
184,959,826✔
671
  } else {
UNCOV
672
    tableName[0] = 0;
×
673
  }
674

675
  *tbGet = true;
185,030,281✔
676

677
  return TSDB_CODE_SUCCESS;
184,960,013✔
678
}
679

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

682
void qDestroyOperatorParam(SOperatorParam* pParam) {
116,090✔
683
  if (NULL == pParam) {
116,090✔
684
    return;
×
685
  }
686
  freeOperatorParam(pParam, OP_GET_PARAM);
116,090✔
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) {
34,037,504✔
695
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
34,037,504✔
696
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
34,037,504✔
697
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
34,037,504✔
698
    qError("%s, %s operator type mismatch, task operator type:%d, "
114,680✔
699
           "new param operator type:%d", GET_TASKID(pTask), __func__,
700
           pTask->pRoot->operatorType,
701
           pNewParam->opType);
702
    qDestroyOperatorParam((SOperatorParam*)pParam);
114,680✔
703
    return;
114,680✔
704
  }
705
  TSWAP(pParam, pTask->pOpParam);
33,929,483✔
706
  ((SExecTaskInfo*)tinfo)->paramSet = false;
33,925,800✔
707
}
708

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

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

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

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

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

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

751
  readHandle->uid = 0;
267,020,160✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
267,062,818✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
266,796,828✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
668,394✔
755
    goto _error;
443,947✔
756
  }
757
    
758
  if (handle) {
266,181,757✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
266,049,054✔
760
    void*           pSinkManager = NULL;
266,131,219✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
265,705,294✔
762
    if (code != TSDB_CODE_SUCCESS) {
266,043,102✔
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
266,043,102✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
266,055,859✔
769
    if (code != TSDB_CODE_SUCCESS) {
265,700,235✔
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
771
      taosMemoryFree(pSinkManager);
×
772
      goto _error;
×
773
    }
774

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

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
265,842,969✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
266,130,631✔
789
    if (code) {
265,802,698✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
579✔
791
    }
792
  }
793

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

797
_error:
53,960,105✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
267,016,098✔
800
}
801

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

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

814
  if (pLocal) {
313,901,435✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
310,780,696✔
816
  }
817

818
  taosArrayClear(pResList);
313,922,732✔
819

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

827
  if (pTaskInfo->cost.start == 0) {
313,805,738✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
260,575,956✔
829
  }
830

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

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
313,812,010✔
839
  if (ret != TSDB_CODE_SUCCESS) {
314,312,538✔
840
    pTaskInfo->code = ret;
645,415✔
841
    (void)cleanUpUdfs();
645,415✔
842

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

846
    return pTaskInfo->code;
645,415✔
847
  }
848

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

851
  int32_t      current = 0;
313,681,689✔
852
  SSDataBlock* pRes = NULL;
313,681,689✔
853
  int64_t      st = taosGetTimestampUs();
313,917,189✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
313,917,189✔
856
    pTaskInfo->paramSet = true;
33,917,119✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
33,919,087✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
280,037,256✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
313,333,684✔
863
  code = blockDataCheck(pRes);
313,333,684✔
864
  QUERY_CHECK_CODE(code, lino, _end);
313,337,420✔
865

866
  if (pRes == NULL) {
313,337,420✔
867
    st = taosGetTimestampUs();
60,132,833✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
313,326,429✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
313,362,758✔
872
    rowsThreshold = 4096;
312,815,936✔
873
  }
874

875
  int32_t blockIndex = 0;
313,358,396✔
876
  while (pRes != NULL) {
777,070,341✔
877
    SSDataBlock* p = NULL;
508,820,481✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
508,781,841✔
879
      SSDataBlock* p1 = NULL;
387,767,471✔
880
      code = createOneDataBlock(pRes, true, &p1);
387,774,606✔
881
      QUERY_CHECK_CODE(code, lino, _end);
387,761,534✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
387,761,534✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
387,770,491✔
885
      p = p1;
387,770,491✔
886
    } else if (processOneBlock) {
121,068,254✔
887
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
17,057,840✔
888
      if (tmp) {
17,057,840✔
889
        blockDataDestroy(*tmp);
17,057,840✔
890
        *tmp = NULL;
17,057,840✔
891
      }
892
      SSDataBlock* p1 = NULL;
17,057,840✔
893
      code = createOneDataBlock(pRes, true, &p1);
17,057,840✔
894
      QUERY_CHECK_CODE(code, lino, _end);
17,057,840✔
895

896
      *tmp = p1;
17,057,840✔
897
      p = p1;
17,057,840✔
898
    } else {
899
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
104,010,414✔
900
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
104,009,915✔
901

902
      p = *(SSDataBlock**)tmp;
104,009,915✔
903
      code = copyDataBlock(p, pRes);
104,010,487✔
904
      QUERY_CHECK_CODE(code, lino, _end);
104,011,421✔
905
    }
906

907
    blockIndex += 1;
508,831,537✔
908

909
    current += p->info.rows;
508,831,537✔
910
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
508,833,165✔
911
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
912
    void* tmp = taosArrayPush(pResList, &p);
508,849,256✔
913
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
508,849,256✔
914

915
    if (current >= rowsThreshold || processOneBlock) {
508,849,256✔
916
      break;
917
    }
918

919
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
463,740,152✔
920
    QUERY_CHECK_CODE(code, lino, _end);
463,721,670✔
921
    code = blockDataCheck(pRes);
463,721,670✔
922
    QUERY_CHECK_CODE(code, lino, _end);
463,748,999✔
923
  }
924

925
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
313,359,179✔
926
    pTaskInfo->pSubplan->rowsThreshold -= current;
538,584✔
927
  }
928

929
  *hasMore = (pRes != NULL);
313,363,915✔
930
  uint64_t el = (taosGetTimestampUs() - st);
313,353,687✔
931

932
  pTaskInfo->cost.elapsedTime += el;
313,353,687✔
933
  if (NULL == pRes) {
313,324,277✔
934
    *useconds = pTaskInfo->cost.elapsedTime;
268,214,958✔
935
  }
936

937
_end:
313,398,510✔
938
  (void)cleanUpUdfs();
313,364,644✔
939

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

944
  atomic_store_64(&pTaskInfo->owner, 0);
313,394,439✔
945
  if (code) {
313,391,681✔
946
    pTaskInfo->code = code;
×
947
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
948
  }
949

950
  return pTaskInfo->code;
313,391,681✔
951
}
952

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

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

967
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
123,359,791✔
968
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
123,359,791✔
969
  int64_t        threadId = taosGetSelfPthreadId();
123,359,791✔
970
  int64_t        curOwner = 0;
123,361,827✔
971

972
  *pRes = NULL;
123,361,827✔
973

974
  // todo extract method
975
  taosRLockLatch(&pTaskInfo->lock);
123,362,168✔
976
  bool isKilled = isTaskKilled(pTaskInfo);
123,363,460✔
977
  if (isKilled) {
123,363,119✔
978
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
979

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

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

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

992
  pTaskInfo->owner = threadId;
123,361,412✔
993
  taosRUnLockLatch(&pTaskInfo->lock);
123,360,053✔
994

995
  if (pTaskInfo->cost.start == 0) {
123,363,119✔
996
    pTaskInfo->cost.start = taosGetTimestampUs();
229,635✔
997
  }
998

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

1009
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
123,357,992✔
1010

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

1024
  code = blockDataCheck(*pRes);
123,316,222✔
1025
  if (code) {
123,354,154✔
1026
    pTaskInfo->code = code;
×
1027
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1028
  }
1029

1030
  uint64_t el = (taosGetTimestampUs() - st);
123,331,110✔
1031

1032
  pTaskInfo->cost.elapsedTime += el;
123,331,110✔
1033
  if (NULL == *pRes) {
123,340,620✔
1034
    *useconds = pTaskInfo->cost.elapsedTime;
21,953,378✔
1035
  }
1036

1037
  (void)cleanUpUdfs();
123,343,934✔
1038

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

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

1045
  atomic_store_64(&pTaskInfo->owner, 0);
123,361,797✔
1046
  return pTaskInfo->code;
123,363,119✔
1047
}
1048

1049
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
100,152,187✔
1050
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
100,152,187✔
1051
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
100,153,905✔
1052
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
100,153,563✔
1053

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

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

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

1071
  return 0;
×
1072
}
1073

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

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

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

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

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

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

1129
  setTaskKilled(pTaskInfo, rspCode);
69,893✔
1130
  qStopTaskOperators(pTaskInfo);
69,893✔
1131

1132
  return TSDB_CODE_SUCCESS;
69,893✔
1133
}
1134

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

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

1148
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1149

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

1156
        taosMsleep(200);
×
1157

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

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

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

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

1188
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
267,286,974✔
1189
  STaskCostInfo* pSummary = &pTaskInfo->cost;
267,286,974✔
1190
  int64_t        idleTime = pSummary->start - pSummary->created;
267,296,048✔
1191

1192
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
267,289,638✔
1193
  if (pSummary->pRecoder != NULL) {
267,268,449✔
1194
    qDebug(
183,420,725✔
1195
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1196
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1197
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1198
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1199
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1200
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1201
  } else {
1202
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
83,835,004✔
1203
           pSummary->elapsedTime / 1000.0);
1204
  }
1205
}
267,256,094✔
1206

1207

1208
void qDestroyTask(qTaskInfo_t qTaskHandle) {
273,571,002✔
1209
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
273,571,002✔
1210
  if (pTaskInfo == NULL) {
273,571,002✔
1211
    return;
6,284,428✔
1212
  }
1213

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

1220
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
267,296,260✔
1221
  doDestroyTask(pTaskInfo);
267,290,039✔
1222
}
1223

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

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

1233
  while (1) {
319,099✔
1234
    uint16_t type = pOperator->operatorType;
644,394✔
1235
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
644,394✔
1236
      *scanner = pOperator->info;
325,295✔
1237
      break;
325,295✔
1238
    } else {
1239
      pOperator = pOperator->pDownstream[0];
319,099✔
1240
    }
1241
  }
1242
}
325,295✔
1243

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

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

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

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

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

1275
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
765,302✔
1276
  memset(pCond, 0, sizeof(SQueryTableDataCond));
765,302✔
1277
  pCond->order = TSDB_ORDER_ASC;
765,302✔
1278
  pCond->numOfCols = pMtInfo->schema->nCols;
765,289✔
1279
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
766,184✔
1280
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
764,982✔
1281
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
766,184✔
1282
    taosMemoryFreeClear(pCond->colList);
882✔
1283
    taosMemoryFreeClear(pCond->pSlotList);
×
1284
    return terrno;
×
1285
  }
1286

1287
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
765,302✔
1288
  pCond->suid = pMtInfo->suid;
765,596✔
1289
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
765,289✔
1290
  pCond->startVersion = -1;
765,596✔
1291
  pCond->endVersion = sContext->snapVersion;
765,302✔
1292

1293
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,875,550✔
1294
    SColumnInfo* pColInfo = &pCond->colList[i];
4,109,967✔
1295
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,109,660✔
1296
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,109,673✔
1297
    if (pMtInfo->pExtSchemas != NULL) {
4,109,980✔
1298
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,240✔
1299
    }
1300
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,110,274✔
1301
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,110,568✔
1302

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

1306
  return TSDB_CODE_SUCCESS;
766,184✔
1307
}
1308

1309
void qStreamSetOpen(qTaskInfo_t tinfo) {
121,839,040✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
121,839,040✔
1311
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
121,839,040✔
1312
  pOperator->status = OP_NOT_OPENED;
121,856,522✔
1313
}
121,860,681✔
1314

1315
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
22,148,406✔
1316
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,148,406✔
1317
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
22,148,406✔
1318
}
22,148,358✔
1319

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

1325
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
23,139,242✔
1326
  const char*    id = GET_TASKID(pTaskInfo);
23,138,673✔
1327

1328
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
23,138,001✔
1329
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
21,855,638✔
1330
    if (pOperator == NULL || code != 0) {
21,856,053✔
1331
      return code;
×
1332
    }
1333

1334
    SStreamScanInfo* pInfo = pOperator->info;
21,856,391✔
1335
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
21,855,343✔
1336
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
21,856,046✔
1337
    walReaderVerifyOffset(pWalReader, pOffset);
21,855,708✔
1338
  }
1339
  // if pOffset equal to current offset, means continue consume
1340
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
23,136,425✔
1341
    return 0;
21,265,967✔
1342
  }
1343

1344
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,871,781✔
1345
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,089,337✔
1346
    if (pOperator == NULL || code != 0) {
1,089,065✔
UNCOV
1347
      return code;
×
1348
    }
1349

1350
    SStreamScanInfo* pInfo = pOperator->info;
1,089,378✔
1351
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,089,065✔
1352
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,089,650✔
1353
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,089,650✔
1354

1355
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,089,337✔
1356
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
883,277✔
1357
      pScanBaseInfo->dataReader = NULL;
882,651✔
1358

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

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

1384
      if (uid == 0) {
205,478✔
1385
        if (numOfTables != 0) {
199,169✔
1386
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,875✔
1387
          if (!tmp) {
38,875✔
1388
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1389
            taosRUnLockLatch(&pTaskInfo->lock);
×
1390
            return terrno;
×
1391
          }
1392
          if (tmp) uid = tmp->uid;
38,875✔
1393
          ts = INT64_MIN;
38,875✔
1394
          pScanInfo->currentTable = 0;
38,875✔
1395
        } else {
1396
          taosRUnLockLatch(&pTaskInfo->lock);
160,294✔
1397
          qError("no table in table list, %s", id);
160,607✔
1398
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
160,625✔
1399
        }
1400
      }
1401
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,184✔
1402

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1493
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
766,430✔
1494
      tableListClear(pTableListInfo);
765,829✔
1495

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

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

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

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

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

1543
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
765,890✔
1544
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
765,890✔
1545
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1546
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
765,583✔
1547
      pTaskInfo->streamInfo.schema = mtInfo.schema;
765,583✔
1548
      taosMemoryFreeClear(mtInfo.pExtSchemas);
765,583✔
1549

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

1569
end:
1,698,892✔
1570
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,698,414✔
1571
  return 0;
1,698,998✔
1572
}
1573

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

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

1585
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
216,196,594✔
1586

1587
  if (pMsg->contLen > 0) {
216,249,682✔
1588
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
216,237,662✔
1589
    if (buf.pData == NULL) {
216,192,713✔
1590
      pMsg->code = terrno;
×
1591
    } else {
1592
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
216,192,713✔
1593
    }
1594
  }
1595

1596
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
216,253,604✔
1597
  rpcFreeCont(pMsg->pCont);
216,305,339✔
1598
  destroySendMsgInfo(pSendInfo);
216,235,838✔
1599
}
1600

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

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

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

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

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

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

1629
  taosArrayDestroy(plist);
×
1630

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

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

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

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

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

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

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

1673
  *pList = NULL;
1,669,899✔
1674
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,671,504✔
1675
  if (pArray == NULL) {
1,670,969✔
1676
    return terrno;
×
1677
  }
1678

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

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

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

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

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

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

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

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

1726
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
23,723,837✔
1727
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
23,721,494✔
1728

1729
  if (pOper->fpSet.resetStateFn) {
23,722,787✔
1730
    code = pOper->fpSet.resetStateFn(pOper);
23,723,211✔
1731
  }
1732
  pOper->status = OP_NOT_OPENED;
23,720,446✔
1733
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
40,434,708✔
1734
    code = clearStatesForOperator(pOper->pDownstream[i]);
16,709,851✔
1735
  }
1736
  return code;
23,725,923✔
1737
}
1738

1739
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,014,306✔
1740
  int32_t        code = 0;
7,014,306✔
1741
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,014,306✔
1742
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,014,306✔
1743
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,014,521✔
1744
  code = clearStatesForOperator(pOper);
7,014,521✔
1745
  return code;
7,014,301✔
1746
}
1747

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

1753
  *ppRes = NULL;
9,667,090✔
1754

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

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

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

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

1774
  pTaskInfo->owner = threadId;
9,666,870✔
1775
  taosRUnLockLatch(&pTaskInfo->lock);
9,667,090✔
1776

1777
  if (pTaskInfo->cost.start == 0) {
9,667,515✔
1778
    pTaskInfo->cost.start = taosGetTimestampUs();
235,343✔
1779
  }
1780

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

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

1793
  int64_t st = taosGetTimestampUs();
9,667,515✔
1794

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

1808
  uint64_t el = (taosGetTimestampUs() - st);
8,307,724✔
1809

1810
  pTaskInfo->cost.elapsedTime += el;
8,307,724✔
1811
  if (NULL == *ppRes) {
8,307,724✔
1812
    *useconds = pTaskInfo->cost.elapsedTime;
5,200,864✔
1813
  }
1814

1815
  (void)cleanUpUdfs();
8,307,724✔
1816

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

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

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

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

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

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

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

1859
end:
248,367✔
1860
  return 0;
248,367✔
1861
}
1862

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2231
  (*ppDst)->suid = pSrc->suid;
228,433✔
2232
  (*ppDst)->sver = pSrc->sver;
228,865✔
2233
  (*ppDst)->tbType = pSrc->tbType;
228,650✔
2234
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
228,865✔
2235
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
228,865✔
2236

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

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

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

2247
  if (pSrc->pFields && pSrc->pFields->size > 0) {
228,650✔
2248
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
228,865✔
2249
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
228,650✔
2250
  } else {
2251
    (*ppDst)->pFields = NULL;
×
2252
  }
2253
  
2254
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
228,865✔
2255
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
132,507✔
2256
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
132,507✔
2257
  } else {
2258
    (*ppDst)->pTagFields = NULL;
96,543✔
2259
  }
2260

2261
  if (pSrc->colCids && pSrc->colCids->size > 0) {
228,865✔
2262
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,734✔
2263
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,734✔
2264
  } else {
2265
    (*ppDst)->colCids = NULL;
227,131✔
2266
  }
2267

2268
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
228,865✔
2269
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,111✔
2270
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,111✔
2271
  } else {
2272
    (*ppDst)->tagCids = NULL;
227,754✔
2273
  }
2274

2275
_exit:
228,433✔
2276

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

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

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

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

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

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

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

© 2026 Coveralls, Inc