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

taosdata / TDengine / #4910

30 Dec 2025 10:52AM UTC coverage: 65.864% (+0.3%) from 65.542%
#4910

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

999 existing lines in 108 files now uncovered.

194877 of 295877 relevant lines covered (65.86%)

121300574.4 hits per line

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

69.04
/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) {
735,637,155✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
735,637,155✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
735,637,155✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
735,866,279✔
47
}
735,428,352✔
48

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

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

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

70
static void initRefPool() {
523,043✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
523,043✔
72
  (void)atexit(cleanupRefPool);
523,043✔
73
}
523,043✔
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) {
42,369,566✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
42,369,566✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
42,370,966✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
18,968,285✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
18,968,285✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
18,968,241✔
162
      if (pScanInfo->base.dataReader != NULL) {
18,968,536✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
251,777✔
164
        if (code) {
251,504✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
23,401,911✔
172
  }
173

174
  return 0;
22,913,799✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
22,913,925✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
22,913,925✔
179
  pTaskInfo->id.queryId = queryId;
22,913,925✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
22,914,532✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
22,913,417✔
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) {
418,361✔
213
  if (msg == NULL) {  // create raw scan
418,361✔
214
    SExecTaskInfo* pTaskInfo = NULL;
111,513✔
215

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

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

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

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

248
  return pTaskInfo;
310,203✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
506,225✔
252
  if (streamInserterParam == NULL) {
506,225✔
253
    return TSDB_CODE_SUCCESS;
298,537✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
207,688✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
207,688✔
323
    if (code) {
207,688✔
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,
506,072✔
328
         tstrerror(code));
329

330
_error:
72,885✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,444,884✔
342
  if (pInfo == NULL) {
3,444,884✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,444,884✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,444,884✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,444,884✔
348
    return false;
4,292✔
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,440,592✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
3,138,764✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
6,579,356✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
354
}
355

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

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

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

376
  void*           info = pOperator->info;
3,440,592✔
377
  STableScanBase* pScanBaseInfo = NULL;
3,440,592✔
378

379
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,440,592✔
380
    pScanBaseInfo = &((STableScanInfo*)info)->base;
301,828✔
381
    setReadHandle(handle, pScanBaseInfo);
301,828✔
382
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
3,138,764✔
383
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,158,574✔
384
    setReadHandle(handle, pScanBaseInfo);
2,158,574✔
385
  }
386

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

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

398
  *pTaskInfo = NULL;
506,225✔
399

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

415
  return code;
506,225✔
416
}
417

418
typedef struct {
419
  tb_uid_t tableUid;
420
  tb_uid_t childUid;
421
  int8_t   check;
422
} STqPair;
423

424
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
341,265✔
425
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
426
  int32_t code = TSDB_CODE_SUCCESS;
341,265✔
427
  int32_t lino = 0;
341,265✔
428
  int8_t  locked = 0;
341,265✔
429
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
341,265✔
430

431
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
341,265✔
432

433
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
341,265✔
434
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
341,265✔
435

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

442
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
341,265✔
443

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

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

453
  locked = 1;
341,265✔
454
  for (int32_t i = 0; i < numOfUids; ++i) {
696,138✔
455
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
354,873✔
456
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
354,873✔
457

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

464
    tDecoderClear(&mr.coder);
354,873✔
465

466
    if (mr.me.type == TSDB_SUPER_TABLE) {
354,873✔
467
      continue;
×
468
    } else {
469
      if (type == TSDB_SUPER_TABLE) {
354,873✔
470
        // this new created child table does not belong to the scanned super table.
471
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
354,873✔
UNCOV
472
          continue;
×
473
        }
474
      } else {  // ordinary table
475
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
476
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
477
        if (mr.me.uid != uid) {
×
478
          continue;
×
479
        }
480
      }
481
    }
482

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

493
  pAPI->metaReaderFn.clearReader(&mr);
341,265✔
494
  locked = 0;
341,265✔
495

496
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
696,138✔
497
    bool     qualified = false;
354,873✔
498
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
354,873✔
499
    if (t == NULL) {
354,873✔
500
      continue;
×
501
    }
502

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

510
      if (!qualified) {
309,492✔
511
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
154,755✔
512
        continue;
154,755✔
513
      }
514
    }
515

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

520
  // handle multiple partition
521

522
_end:
341,265✔
523

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

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

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

542
  if (isAdd) {
341,875✔
543
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
341,265✔
544
  }
545

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

553
  SStreamScanInfo* pScanInfo = pInfo->info;
341,875✔
554
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
341,875✔
555
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
697,358✔
556
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
355,483✔
557
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
355,483✔
558
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
355,483✔
559
    }
560
  }
561

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

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

585
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
341,265✔
586
    taosWLockLatch(&pTaskInfo->lock);
341,265✔
587

588
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
541,383✔
589
      uint64_t* uid = taosArrayGet(qa, i);
200,118✔
590
      if (!uid) {
200,118✔
591
        taosMemoryFree(keyBuf);
×
592
        taosArrayDestroy(qa);
×
593
        taosWUnLockLatch(&pTaskInfo->lock);
×
594
        return terrno;
×
595
      }
596
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
200,118✔
597

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

613
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
200,118✔
614
      if (code != TSDB_CODE_SUCCESS) {
200,118✔
615
        taosMemoryFree(keyBuf);
×
616
        taosArrayDestroy(qa);
×
617
        taosWUnLockLatch(&pTaskInfo->lock);
×
618
        return code;
×
619
      }
620
    }
621

622
    taosWUnLockLatch(&pTaskInfo->lock);
341,265✔
623
    if (keyBuf != NULL) {
341,265✔
624
      taosMemoryFree(keyBuf);
×
625
    }
626

627
    taosArrayDestroy(qa);
341,265✔
628
  } else {  // remove the table id in current list
629
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
610✔
630
    taosWLockLatch(&pTaskInfo->lock);
610✔
631
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
610✔
632
    taosWUnLockLatch(&pTaskInfo->lock);
610✔
633
  }
634

635
  return code;
341,875✔
636
}
637

638
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
623,959,546✔
639
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
640
                                    int32_t idx, bool* tbGet) {
641
  *tbGet = false;
623,959,546✔
642

643
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
624,109,448✔
644
    return TSDB_CODE_INVALID_PARA;
×
645
  }
646
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
624,193,029✔
647

648
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
624,193,029✔
649
    return TSDB_CODE_SUCCESS;
353,275,912✔
650
  }
651

652
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
270,935,517✔
653
  if (!pSchemaInfo) {
270,656,201✔
654
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
655
    return terrno;
×
656
  }
657

658
  *sversion = pSchemaInfo->sw->version;
270,656,201✔
659
  *tversion = pSchemaInfo->tversion;
271,088,931✔
660
  *rversion = pSchemaInfo->rversion;
271,066,267✔
661
  if (pSchemaInfo->dbname) {
271,011,259✔
662
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
271,122,774✔
663
  } else {
664
    dbName[0] = 0;
×
665
  }
666
  if (pSchemaInfo->tablename) {
271,290,487✔
667
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
271,075,650✔
668
  } else {
669
    tableName[0] = 0;
52,092✔
670
  }
671

672
  *tbGet = true;
271,287,479✔
673

674
  return TSDB_CODE_SUCCESS;
271,166,986✔
675
}
676

677
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
353,044,050✔
678

679
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
680
  if (NULL == pParam) {
×
681
    return;
×
682
  }
683
  freeOperatorParam(pParam, OP_GET_PARAM);
×
684
}
685

686
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
6,854,986✔
687
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
6,854,986✔
688
  ((SExecTaskInfo*)tinfo)->paramSet = false;
6,857,830✔
689
}
6,857,356✔
690

691
int32_t qExecutorInit(void) {
4,064,217✔
692
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,064,217✔
693
  return TSDB_CODE_SUCCESS;
4,065,437✔
694
}
695

696
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
53,296,429✔
697
  int32_t        code = TSDB_CODE_SUCCESS;
53,296,429✔
698
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
53,296,429✔
699
  if (pTask->pWorkerCb) {
53,296,429✔
700
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
53,316,220✔
701
    if (code != TSDB_CODE_SUCCESS) {
53,327,091✔
702
      pTask->code = code;
×
703
      return pTask->code;
×
704
    }
705
  }
706

707
  code = tsem_wait(pSem);
53,336,489✔
708
  if (code != TSDB_CODE_SUCCESS) {
53,327,693✔
709
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
710
    pTask->code = code;
×
711
    return pTask->code;
×
712
  }
713

714
  if (pTask->pWorkerCb) {
53,327,693✔
715
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
53,331,013✔
716
    if (code != TSDB_CODE_SUCCESS) {
53,331,428✔
717
      pTask->code = code;
×
718
      return pTask->code;
×
719
    }
720
  }
721
  return TSDB_CODE_SUCCESS;
53,329,353✔
722
}
723

724
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
363,671,712✔
725
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
726
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
727
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
363,671,712✔
728
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
363,671,712✔
729

730
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
363,643,281✔
731
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
732

733
  readHandle->uid = 0;
363,685,012✔
734
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
363,749,724✔
735
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
363,517,924✔
736
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
8,722,807✔
737
    goto _error;
8,589,257✔
738
  }
739
    
740
  if (handle) {
354,889,859✔
741
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
354,635,512✔
742
    void*           pSinkManager = NULL;
354,765,709✔
743
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
354,571,624✔
744
    if (code != TSDB_CODE_SUCCESS) {
354,067,562✔
745
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
746
      goto _error;
×
747
    }
748

749
    void* pSinkParam = NULL;
354,067,562✔
750
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
354,398,644✔
751
    if (code != TSDB_CODE_SUCCESS) {
354,171,637✔
752
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
753
      taosMemoryFree(pSinkManager);
×
754
      goto _error;
×
755
    }
756

757
    SDataSinkNode* pSink = NULL;
354,171,637✔
758
    if (readHandle->localExec) {
354,435,598✔
759
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,684✔
760
      if (code != TSDB_CODE_SUCCESS) {
2,684✔
761
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
762
               (*pTask)->id.str);
763
        taosMemoryFree(pSinkManager);
×
764
        goto _error;
×
765
      }
766
    }
767

768
    // pSinkParam has been freed during create sinker.
769
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
354,336,439✔
770
                              (*pTask)->id.str, pSubplan->processOneBlock);
354,685,087✔
771
    if (code) {
354,219,101✔
772
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
564✔
773
    }
774
  }
775

776
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
354,848,220✔
777
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
778

779
_error:
153,290,979✔
780
  // if failed to add ref for all tables in this query, abort current query
781
  return code;
363,550,079✔
782
}
783

784
static void freeBlock(void* param) {
×
785
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
786
  blockDataDestroy(pBlock);
×
787
}
×
788

789
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
375,349,442✔
790
                     bool processOneBlock) {
791
  int32_t        code = TSDB_CODE_SUCCESS;
375,349,442✔
792
  int32_t        lino = 0;
375,349,442✔
793
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
375,349,442✔
794
  int64_t        threadId = taosGetSelfPthreadId();
375,349,442✔
795

796
  if (pLocal) {
375,349,600✔
797
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
371,688,957✔
798
  }
799

800
  taosArrayClear(pResList);
375,166,385✔
801

802
  int64_t curOwner = 0;
375,155,965✔
803
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
375,155,965✔
804
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
805
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
806
    return pTaskInfo->code;
×
807
  }
808

809
  if (pTaskInfo->cost.start == 0) {
375,200,493✔
810
    pTaskInfo->cost.start = taosGetTimestampUs();
350,643,990✔
811
  }
812

813
  if (isTaskKilled(pTaskInfo)) {
375,511,525✔
814
    atomic_store_64(&pTaskInfo->owner, 0);
1,383✔
815
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
1,383✔
816
    return pTaskInfo->code;
1,383✔
817
  }
818

819
  // error occurs, record the error code and return to client
820
  int32_t ret = setjmp(pTaskInfo->env);
375,174,001✔
821
  if (ret != TSDB_CODE_SUCCESS) {
382,963,511✔
822
    pTaskInfo->code = ret;
8,315,091✔
823
    (void)cleanUpUdfs();
8,315,091✔
824

825
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
8,316,012✔
826
    atomic_store_64(&pTaskInfo->owner, 0);
8,316,012✔
827

828
    return pTaskInfo->code;
8,316,012✔
829
  }
830

831
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
374,648,420✔
832

833
  int32_t      current = 0;
374,653,227✔
834
  SSDataBlock* pRes = NULL;
374,653,227✔
835
  int64_t      st = taosGetTimestampUs();
375,421,159✔
836

837
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
375,421,159✔
838
    pTaskInfo->paramSet = true;
6,852,142✔
839
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
6,853,090✔
840
  } else {
841
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
368,536,312✔
842
  }
843

844
  QUERY_CHECK_CODE(code, lino, _end);
367,214,330✔
845
  code = blockDataCheck(pRes);
367,214,330✔
846
  QUERY_CHECK_CODE(code, lino, _end);
367,205,357✔
847

848
  if (pRes == NULL) {
367,205,357✔
849
    st = taosGetTimestampUs();
83,343,022✔
850
  }
851

852
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
367,219,118✔
853
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
367,297,231✔
854
    rowsThreshold = 4096;
365,963,807✔
855
  }
856

857
  int32_t blockIndex = 0;
367,257,250✔
858
  while (pRes != NULL) {
942,298,072✔
859
    SSDataBlock* p = NULL;
594,616,619✔
860
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
594,516,593✔
861
      SSDataBlock* p1 = NULL;
503,098,713✔
862
      code = createOneDataBlock(pRes, true, &p1);
503,116,585✔
863
      QUERY_CHECK_CODE(code, lino, _end);
503,085,007✔
864

865
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
503,085,007✔
866
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
503,165,309✔
867
      p = p1;
503,165,309✔
868
    } else {
869
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
91,524,763✔
870
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
91,523,899✔
871

872
      p = *(SSDataBlock**)tmp;
91,523,899✔
873
      code = copyDataBlock(p, pRes);
91,524,331✔
874
      QUERY_CHECK_CODE(code, lino, _end);
91,525,627✔
875
    }
876

877
    blockIndex += 1;
594,669,885✔
878

879
    current += p->info.rows;
594,669,885✔
880
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
594,677,733✔
881
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
882
    void* tmp = taosArrayPush(pResList, &p);
594,698,119✔
883
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
594,698,119✔
884

885
    if (current >= rowsThreshold || processOneBlock) {
594,698,119✔
886
      break;
887
    }
888

889
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
575,093,343✔
890
    QUERY_CHECK_CODE(code, lino, _end);
575,075,143✔
891
    code = blockDataCheck(pRes);
575,075,143✔
892
    QUERY_CHECK_CODE(code, lino, _end);
575,095,401✔
893
  }
894

895
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
367,286,229✔
896
    pTaskInfo->pSubplan->rowsThreshold -= current;
1,282,708✔
897
  }
898

899
  *hasMore = (pRes != NULL);
367,279,691✔
900
  uint64_t el = (taosGetTimestampUs() - st);
367,238,205✔
901

902
  pTaskInfo->cost.elapsedTime += el;
367,238,205✔
903
  if (NULL == pRes) {
367,234,776✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
347,630,000✔
905
  }
906

907
_end:
367,172,551✔
908
  (void)cleanUpUdfs();
367,152,561✔
909

910
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
367,359,715✔
911
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
367,360,158✔
912
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
913

914
  atomic_store_64(&pTaskInfo->owner, 0);
367,360,158✔
915
  if (code) {
367,360,158✔
916
    pTaskInfo->code = code;
×
917
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
918
  }
919

920
  return pTaskInfo->code;
367,360,158✔
921
}
922

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

934
  taosArrayClear(pTaskInfo->pResultBlockList);
×
935
}
×
936

937
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
116,594,205✔
938
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
116,594,205✔
939
  int64_t        threadId = taosGetSelfPthreadId();
116,594,205✔
940
  int64_t        curOwner = 0;
116,594,867✔
941

942
  *pRes = NULL;
116,594,867✔
943

944
  // todo extract method
945
  taosRLockLatch(&pTaskInfo->lock);
116,594,542✔
946
  bool isKilled = isTaskKilled(pTaskInfo);
116,595,860✔
947
  if (isKilled) {
116,595,229✔
948
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
949

950
    taosRUnLockLatch(&pTaskInfo->lock);
×
951
    return pTaskInfo->code;
×
952
  }
953

954
  if (pTaskInfo->owner != 0) {
116,595,229✔
955
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
956
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
957

958
    taosRUnLockLatch(&pTaskInfo->lock);
×
959
    return pTaskInfo->code;
×
960
  }
961

962
  pTaskInfo->owner = threadId;
116,595,248✔
963
  taosRUnLockLatch(&pTaskInfo->lock);
116,595,529✔
964

965
  if (pTaskInfo->cost.start == 0) {
116,595,860✔
966
    pTaskInfo->cost.start = taosGetTimestampUs();
219,006✔
967
  }
968

969
  // error occurs, record the error code and return to client
970
  int32_t ret = setjmp(pTaskInfo->env);
116,595,860✔
971
  if (ret != TSDB_CODE_SUCCESS) {
116,592,059✔
972
    pTaskInfo->code = ret;
×
973
    (void)cleanUpUdfs();
×
974
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
975
    atomic_store_64(&pTaskInfo->owner, 0);
×
976
    return pTaskInfo->code;
×
977
  }
978

979
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
116,592,059✔
980

981
  int64_t st = taosGetTimestampUs();
116,595,529✔
982
  int32_t code = TSDB_CODE_SUCCESS;
116,595,529✔
983
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
116,595,529✔
984
    pTaskInfo->paramSet = true;
×
985
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
986
  } else {
987
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
116,594,879✔
988
  }
989
  if (code) {
116,561,061✔
990
    pTaskInfo->code = code;
×
991
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
992
  }
993

994
  code = blockDataCheck(*pRes);
116,561,061✔
995
  if (code) {
116,590,020✔
996
    pTaskInfo->code = code;
×
997
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
998
  }
999

1000
  uint64_t el = (taosGetTimestampUs() - st);
116,574,728✔
1001

1002
  pTaskInfo->cost.elapsedTime += el;
116,574,728✔
1003
  if (NULL == *pRes) {
116,584,094✔
1004
    *useconds = pTaskInfo->cost.elapsedTime;
18,554,845✔
1005
  }
1006

1007
  (void)cleanUpUdfs();
116,585,316✔
1008

1009
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
116,595,860✔
1010
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
116,595,860✔
1011

1012
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
116,595,860✔
1013
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1014

1015
  atomic_store_64(&pTaskInfo->owner, 0);
116,595,860✔
1016
  return pTaskInfo->code;
116,595,860✔
1017
}
1018

1019
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
111,210,654✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
111,210,654✔
1021
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
111,213,835✔
1022
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
111,214,968✔
1023

1024
  if (!tmp) {
111,211,542✔
1025
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1026
    return terrno;
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
111,211,542✔
1029
}
1030

1031
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1032
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1033
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1034

1035
  if (key->refId < pInfo->refId) {
×
1036
    return -1;
×
1037
  } else if (key->refId > pInfo->refId) {
×
1038
    return 1;
×
1039
  }
1040

1041
  return 0;
×
1042
}
1043

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

1053
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
308,850✔
1054
  if (pTaskInfo->subJobCtx.hasSubJobs) {
308,850✔
1055
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
272,998✔
1056
    if (pTaskInfo->subJobCtx.param) {
272,998✔
1057
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
134,398✔
1058
    }
1059
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
272,998✔
1060
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
272,998✔
1061
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
272,998✔
1062
  }
1063
  
1064
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
308,850✔
1065

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

1088
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
308,850✔
1089
}
308,850✔
1090

1091
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
308,850✔
1092
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
308,850✔
1093
  if (pTaskInfo == NULL) {
308,850✔
1094
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1095
  }
1096

1097
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
308,850✔
1098

1099
  setTaskKilled(pTaskInfo, rspCode);
308,850✔
1100
  qStopTaskOperators(pTaskInfo);
308,850✔
1101

1102
  return TSDB_CODE_SUCCESS;
308,850✔
1103
}
1104

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

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

1118
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1119

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

1126
        taosMsleep(200);
×
1127

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

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

1149
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1150
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1151
  if (NULL == pTaskInfo) {
×
1152
    return false;
×
1153
  }
1154

1155
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1156
}
1157

1158
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
355,813,151✔
1159
  STaskCostInfo* pSummary = &pTaskInfo->cost;
355,813,151✔
1160
  int64_t        idleTime = pSummary->start - pSummary->created;
355,870,253✔
1161

1162
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
355,902,489✔
1163
  if (pSummary->pRecoder != NULL) {
355,832,085✔
1164
    qDebug(
264,713,451✔
1165
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1166
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1167
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1168
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1169
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1170
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1171
  } else {
1172
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
91,042,817✔
1173
           pSummary->elapsedTime / 1000.0);
1174
  }
1175
}
355,755,797✔
1176

1177

1178
void qDestroyTask(qTaskInfo_t qTaskHandle) {
369,503,217✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
369,503,217✔
1180
  if (pTaskInfo == NULL) {
369,503,217✔
1181
    return;
13,676,748✔
1182
  }
1183

1184
  if (pTaskInfo->pRoot != NULL) {
355,826,469✔
1185
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
355,872,377✔
1186
  } else {
1187
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1188
  }
1189

1190
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
355,871,298✔
1191
  doDestroyTask(pTaskInfo);
355,824,726✔
1192
}
1193

1194
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
4,230,715✔
1195
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,230,715✔
1196
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
4,230,715✔
1197
}
1198

1199
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
310,203✔
1200
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
310,203✔
1201
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
310,203✔
1202

1203
  while (1) {
304,517✔
1204
    uint16_t type = pOperator->operatorType;
614,720✔
1205
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
614,720✔
1206
      *scanner = pOperator->info;
310,203✔
1207
      break;
310,203✔
1208
    } else {
1209
      pOperator = pOperator->pDownstream[0];
304,517✔
1210
    }
1211
  }
1212
}
310,203✔
1213

1214
void* qExtractReaderFromTmqScanner(void* scanner) {
310,203✔
1215
  SStreamScanInfo* pInfo = scanner;
310,203✔
1216
  return (void*)pInfo->tqReader;
310,203✔
1217
}
1218

1219
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
694,906✔
1220
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
694,906✔
1221
  return pTaskInfo->streamInfo.schema;
694,906✔
1222
}
1223

1224
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
694,906✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
694,906✔
1226
  return pTaskInfo->streamInfo.tbName;
694,906✔
1227
}
1228

1229
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
766,071✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
766,071✔
1231
  return &pTaskInfo->streamInfo.btMetaRsp;
766,071✔
1232
}
1233

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

1245
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
728,755✔
1246
  memset(pCond, 0, sizeof(SQueryTableDataCond));
728,755✔
1247
  pCond->order = TSDB_ORDER_ASC;
728,755✔
1248
  pCond->numOfCols = pMtInfo->schema->nCols;
729,853✔
1249
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
731,552✔
1250
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
729,317✔
1251
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
729,866✔
1252
    taosMemoryFreeClear(pCond->colList);
1,124✔
1253
    taosMemoryFreeClear(pCond->pSlotList);
×
1254
    return terrno;
×
1255
  }
1256

1257
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
728,755✔
1258
  pCond->suid = pMtInfo->suid;
729,892✔
1259
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
730,147✔
1260
  pCond->startVersion = -1;
729,866✔
1261
  pCond->endVersion = sContext->snapVersion;
729,036✔
1262

1263
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,660,353✔
1264
    SColumnInfo* pColInfo = &pCond->colList[i];
3,927,945✔
1265
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,927,076✔
1266
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,929,631✔
1267
    if (pMtInfo->pExtSchemas != NULL) {
3,929,069✔
1268
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
44,240✔
1269
    }
1270
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,930,180✔
1271
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,929,043✔
1272

1273
    pCond->pSlotList[i] = i;
3,928,762✔
1274
  }
1275

1276
  return TSDB_CODE_SUCCESS;
732,140✔
1277
}
1278

1279
void qStreamSetOpen(qTaskInfo_t tinfo) {
115,141,002✔
1280
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
115,141,002✔
1281
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
115,141,002✔
1282
  pOperator->status = OP_NOT_OPENED;
115,158,222✔
1283
}
115,151,742✔
1284

1285
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
18,778,628✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
18,778,628✔
1287
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
18,778,628✔
1288
}
18,780,102✔
1289

1290
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
19,747,065✔
1291
  int32_t        code = TSDB_CODE_SUCCESS;
19,747,065✔
1292
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,747,065✔
1293
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
19,747,065✔
1294

1295
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
19,748,123✔
1296
  const char*    id = GET_TASKID(pTaskInfo);
19,747,465✔
1297

1298
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
19,745,837✔
1299
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
18,497,472✔
1300
    if (pOperator == NULL || code != 0) {
18,495,245✔
1301
      return code;
×
1302
    }
1303

1304
    SStreamScanInfo* pInfo = pOperator->info;
18,496,530✔
1305
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
18,495,875✔
1306
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
18,495,983✔
1307
    walReaderVerifyOffset(pWalReader, pOffset);
18,496,802✔
1308
  }
1309
  // if pOffset equal to current offset, means continue consume
1310
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
19,744,849✔
1311
    return 0;
17,944,348✔
1312
  }
1313

1314
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,796,589✔
1315
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,049,005✔
1316
    if (pOperator == NULL || code != 0) {
1,048,694✔
1317
      return code;
×
1318
    }
1319

1320
    SStreamScanInfo* pInfo = pOperator->info;
1,049,307✔
1321
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,049,307✔
1322
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,048,694✔
1323
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,048,142✔
1324

1325
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,048,723✔
1326
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
831,193✔
1327
      pScanBaseInfo->dataReader = NULL;
830,551✔
1328

1329
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
830,882✔
1330
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
830,591✔
1331
      walReaderVerifyOffset(pWalReader, pOffset);
830,882✔
1332
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
831,193✔
1333
      if (code < 0) {
831,193✔
1334
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
12,159✔
1335
        return code;
12,159✔
1336
      }
1337
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
217,812✔
1338
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1339
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1340
      int64_t uid = pOffset->uid;
218,114✔
1341
      int64_t ts = pOffset->ts;
218,114✔
1342
      int32_t index = 0;
217,521✔
1343

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

1354
      if (uid == 0) {
217,521✔
1355
        if (numOfTables != 0) {
211,998✔
1356
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
36,980✔
1357
          if (!tmp) {
36,980✔
1358
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1359
            taosRUnLockLatch(&pTaskInfo->lock);
×
1360
            return terrno;
×
1361
          }
1362
          if (tmp) uid = tmp->uid;
36,980✔
1363
          ts = INT64_MIN;
36,980✔
1364
          pScanInfo->currentTable = 0;
36,980✔
1365
        } else {
1366
          taosRUnLockLatch(&pTaskInfo->lock);
175,018✔
1367
          qError("no table in table list, %s", id);
175,038✔
1368
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
175,018✔
1369
        }
1370
      }
1371
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
42,208✔
1372

1373
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
42,496✔
1374
             pInfo->pTableScanOp->resultInfo.totalRows);
1375
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
42,794✔
1376

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

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

1391
      STableKeyInfo keyInfo = {.uid = uid};
42,794✔
1392
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
42,794✔
1393

1394
      // let's start from the next ts that returned to consumer.
1395
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
42,794✔
1396
        pScanBaseInfo->cond.twindows.skey = ts;
859✔
1397
      } else {
1398
        pScanBaseInfo->cond.twindows.skey = ts + 1;
41,935✔
1399
      }
1400
      pScanInfo->scanTimes = 0;
42,794✔
1401

1402
      if (pScanBaseInfo->dataReader == NULL) {
42,794✔
1403
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
73,742✔
1404
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1405
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
36,871✔
1406
        if (code != TSDB_CODE_SUCCESS) {
36,573✔
1407
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1408
          return code;
×
1409
        }
1410

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

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

1429
      // restore the key value
1430
      pScanBaseInfo->cond.twindows.skey = oldSkey;
42,496✔
1431
    } else {
1432
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1433
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1434
    }
1435

1436
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1437
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
747,584✔
1438
      SStreamRawScanInfo* pInfo = pOperator->info;
732,944✔
1439
      SSnapContext*       sContext = pInfo->sContext;
732,944✔
1440
      SOperatorInfo*      p = NULL;
732,944✔
1441

1442
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
732,944✔
1443
      if (code != 0) {
732,944✔
1444
        return code;
×
1445
      }
1446

1447
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
732,944✔
1448

1449
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
732,944✔
1450
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1451
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1452
      }
1453

1454
      SMetaTableInfo mtInfo = {0};
732,944✔
1455
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
732,944✔
1456
      if (code != 0) {
732,101✔
1457
        destroyMetaTableInfo(&mtInfo);
1458
        return code;
×
1459
      }
1460
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
732,101✔
1461
      pInfo->dataReader = NULL;
729,827✔
1462

1463
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
730,683✔
1464
      tableListClear(pTableListInfo);
730,389✔
1465

1466
      if (mtInfo.uid == 0) {
732,650✔
1467
        destroyMetaTableInfo(&mtInfo);
1468
        goto end;  // no data
804✔
1469
      }
1470

1471
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
731,846✔
1472
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
729,304✔
1473
      if (code != TSDB_CODE_SUCCESS) {
729,317✔
1474
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
729,317✔
1479
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,236✔
1480
      } else {
1481
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
728,630✔
1482
      }
1483

1484
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
731,271✔
1485
      if (code != TSDB_CODE_SUCCESS) {
732,140✔
1486
        destroyMetaTableInfo(&mtInfo);
1487
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1488
        return code;
×
1489
      }
1490

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

1505
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,464,280✔
1506
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
732,140✔
1507
      if (code != TSDB_CODE_SUCCESS) {
731,297✔
1508
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1509
        destroyMetaTableInfo(&mtInfo);
1510
        return code;
×
1511
      }
1512

1513
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
731,297✔
1514
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
730,722✔
1515
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1516
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
730,160✔
1517
      pTaskInfo->streamInfo.schema = mtInfo.schema;
731,003✔
1518
      taosMemoryFreeClear(mtInfo.pExtSchemas);
731,297✔
1519

1520
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
731,297✔
1521
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
14,640✔
1522
      SStreamRawScanInfo* pInfo = pOperator->info;
4,562✔
1523
      SSnapContext*       sContext = pInfo->sContext;
4,562✔
1524
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,562✔
1525
      if (code != 0) {
4,562✔
1526
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1527
        return code;
×
1528
      }
1529
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,562✔
1530
             id);
1531
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
10,078✔
1532
      SStreamRawScanInfo* pInfo = pOperator->info;
10,078✔
1533
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
10,078✔
1534
      pInfo->dataReader = NULL;
10,078✔
1535
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
10,078✔
1536
    }
1537
  }
1538

1539
end:
1,609,302✔
1540
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,609,412✔
1541
  return 0;
1,609,412✔
1542
}
1543

1544
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
259,878,482✔
1545
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
259,878,482✔
1546
  if (pMsg->info.ahandle == NULL) {
259,969,056✔
1547
    rpcFreeCont(pMsg->pCont);
×
1548
    qError("pMsg->info.ahandle is NULL");
×
1549
    return;
×
1550
  }
1551

1552
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
259,858,981✔
1553
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1554

1555
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
259,869,209✔
1556

1557
  if (pMsg->contLen > 0) {
259,873,791✔
1558
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
259,859,520✔
1559
    if (buf.pData == NULL) {
259,642,203✔
1560
      pMsg->code = terrno;
×
1561
    } else {
1562
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
259,642,203✔
1563
    }
1564
  }
1565

1566
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
259,883,287✔
1567
  rpcFreeCont(pMsg->pCont);
260,031,517✔
1568
  destroySendMsgInfo(pSendInfo);
259,964,291✔
1569
}
1570

1571
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1572
  int32_t        code = TSDB_CODE_SUCCESS;
×
1573
  int32_t        lino = 0;
×
1574
  SExecTaskInfo* pTaskInfo = tinfo;
×
1575
  SArray*        plist = NULL;
×
1576

1577
  code = getTableListInfo(pTaskInfo, &plist);
×
1578
  if (code || plist == NULL) {
×
1579
    return NULL;
×
1580
  }
1581

1582
  // only extract table in the first elements
1583
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1584

1585
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1586
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1587

1588
  int32_t numOfTables = 0;
×
1589
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1590
  QUERY_CHECK_CODE(code, lino, _end);
×
1591

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

1599
  taosArrayDestroy(plist);
×
1600

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

1609
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,280,782✔
1610
  int32_t        code = TSDB_CODE_SUCCESS;
3,280,782✔
1611
  int32_t        lino = 0;
3,280,782✔
1612
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,280,782✔
1613

1614
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,281,308✔
1615
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1616
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1617

1618
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1619
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1620
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,279,204✔
1621
    STableScanInfo* pScanInfo = pOperator->info;
1,639,865✔
1622

1623
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,640,391✔
1624
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,640,917✔
1625
  } else {
1626
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,639,865✔
1627
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,639,865✔
1628
    }
1629
  }
1630

1631
_end:
×
1632
  if (code != TSDB_CODE_SUCCESS) {
3,280,782✔
1633
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1634
  }
1635
  return code;
3,280,782✔
1636
}
1637

1638
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,640,391✔
1639
  if (pList == NULL) {
1,640,391✔
1640
    return TSDB_CODE_INVALID_PARA;
×
1641
  }
1642

1643
  *pList = NULL;
1,640,391✔
1644
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,640,391✔
1645
  if (pArray == NULL) {
1,639,865✔
1646
    return terrno;
×
1647
  }
1648

1649
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,639,865✔
1650
  if (code == 0) {
1,640,391✔
1651
    *pList = pArray;
1,640,391✔
1652
  } else {
1653
    taosArrayDestroy(pArray);
×
1654
  }
1655
  return code;
1,639,865✔
1656
}
1657

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

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

1674
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1675
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1676

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

1682
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1683
  int32_t code = 0;
×
1684
  return code;
×
1685
}
1686

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

1693
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
23,622,408✔
1694
  int32_t code = 0;
23,622,408✔
1695

1696
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
23,622,408✔
1697
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
23,622,274✔
1698

1699
  if (pOper->fpSet.resetStateFn) {
23,622,248✔
1700
    code = pOper->fpSet.resetStateFn(pOper);
23,622,847✔
1701
  }
1702
  pOper->status = OP_NOT_OPENED;
23,617,352✔
1703
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
40,306,551✔
1704
    code = clearStatesForOperator(pOper->pDownstream[i]);
16,684,430✔
1705
  }
1706
  return code;
23,625,294✔
1707
}
1708

1709
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
6,939,869✔
1710
  int32_t        code = 0;
6,939,869✔
1711
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
6,939,869✔
1712
  SOperatorInfo* pOper = pTaskInfo->pRoot;
6,939,869✔
1713
  pTaskInfo->code = TSDB_CODE_SUCCESS;
6,939,869✔
1714
  code = clearStatesForOperator(pOper);
6,939,869✔
1715
  return code;
6,939,668✔
1716
}
1717

1718
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
9,687,241✔
1719
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
9,687,241✔
1720
  int64_t        threadId = taosGetSelfPthreadId();
9,687,241✔
1721
  int64_t        curOwner = 0;
9,687,675✔
1722

1723
  *ppRes = NULL;
9,687,675✔
1724

1725
  // todo extract method
1726
  taosRLockLatch(&pTaskInfo->lock);
9,687,675✔
1727
  bool isKilled = isTaskKilled(pTaskInfo);
9,687,878✔
1728
  if (isKilled) {
9,687,474✔
1729
    // clearStreamBlock(pTaskInfo->pRoot);
1730
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1731

1732
    taosRUnLockLatch(&pTaskInfo->lock);
×
1733
    return pTaskInfo->code;
×
1734
  }
1735

1736
  if (pTaskInfo->owner != 0) {
9,687,474✔
1737
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1738
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1739

1740
    taosRUnLockLatch(&pTaskInfo->lock);
×
1741
    return pTaskInfo->code;
×
1742
  }
1743

1744
  pTaskInfo->owner = threadId;
9,687,089✔
1745
  taosRUnLockLatch(&pTaskInfo->lock);
9,687,643✔
1746

1747
  if (pTaskInfo->cost.start == 0) {
9,688,079✔
1748
    pTaskInfo->cost.start = taosGetTimestampUs();
212,048✔
1749
  }
1750

1751
  // error occurs, record the error code and return to client
1752
  int32_t ret = setjmp(pTaskInfo->env);
9,688,079✔
1753
  if (ret != TSDB_CODE_SUCCESS) {
10,887,313✔
1754
    pTaskInfo->code = ret;
1,201,321✔
1755
    (void)cleanUpUdfs();
1,201,321✔
1756
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,201,321✔
1757
    atomic_store_64(&pTaskInfo->owner, 0);
1,201,321✔
1758
    return pTaskInfo->code;
1,201,321✔
1759
  }
1760

1761
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,685,992✔
1762

1763
  int64_t st = taosGetTimestampUs();
9,687,878✔
1764

1765
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
9,687,878✔
1766
  if (code) {
8,486,758✔
1767
    pTaskInfo->code = code;
×
1768
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1769
  } else {
1770
    *finished = *ppRes == NULL;
8,486,758✔
1771
    code = blockDataCheck(*ppRes);
8,486,758✔
1772
  }
1773
  if (code) {
8,486,758✔
1774
    pTaskInfo->code = code;
×
1775
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1776
  }
1777

1778
  uint64_t el = (taosGetTimestampUs() - st);
8,486,758✔
1779

1780
  pTaskInfo->cost.elapsedTime += el;
8,486,758✔
1781
  if (NULL == *ppRes) {
8,486,758✔
1782
    *useconds = pTaskInfo->cost.elapsedTime;
5,259,056✔
1783
  }
1784

1785
  (void)cleanUpUdfs();
8,486,758✔
1786

1787
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,486,758✔
1788
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,486,758✔
1789

1790
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,486,758✔
1791
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1792

1793
  atomic_store_64(&pTaskInfo->owner, 0);
8,486,758✔
1794
  return pTaskInfo->code;
8,486,557✔
1795
}
1796

1797
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1798
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1799
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1800
// }
1801

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

1817
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
217,652✔
1818
  SReadHandle    pHandle = {.vnode = pVnode};
217,652✔
1819
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
217,652✔
1820

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

1829
end:
217,652✔
1830
  return 0;
217,652✔
1831
}
1832

1833
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
31,394✔
1834
  bool   listAdded = false;
31,394✔
1835
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
31,394✔
1836
  if (code == 0 && !listAdded) {
31,394✔
1837
    int32_t numOfTables = taosArrayGetSize(pUidList);
25,711✔
1838
    for (int i = 0; i < numOfTables; i++) {
51,422✔
1839
      void* tmp = taosArrayGet(pUidList, i);
25,711✔
1840
      if (tmp == NULL) {
25,711✔
1841
        return terrno;
×
1842
      }
1843
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
25,711✔
1844

1845
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
25,711✔
1846
      if (p == NULL) {
25,711✔
1847
        return terrno;
×
1848
      }
1849
    }
1850
  }
1851
  return code;
31,394✔
1852
}
1853

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

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

1884
  taosArrayClear(uidList);
27,114✔
1885
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
54,075✔
1886
    void* tmp = taosArrayGet(uidListCopy, i);
27,114✔
1887
    if (tmp == NULL) {
27,114✔
1888
      continue;
×
1889
    }
1890
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
27,114✔
1891
    if (slot == NULL) {
26,961✔
1892
      if (taosArrayPush(uidList, tmp) == NULL) {
3,174✔
1893
        code = terrno;
×
1894
        goto end;
×
1895
      }
1896
    }
1897
  }
1898
end:
27,114✔
1899
  taosArrayDestroy(uidListCopy);
27,114✔
1900
  tableListDestroy(pList);
26,961✔
1901
  return code;
26,961✔
1902
}
1903

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

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

1925
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
217,652✔
1926
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
217,652✔
1927
  STableListInfo* pList = pTableListInfo;
217,652✔
1928
  return pList->pTableList;
217,652✔
1929
}
1930

1931
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,182,811✔
1932

1933
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,060,465✔
1934
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,060,465✔
1935
  qDestroyTask(tInfo);
2,060,465✔
1936
}
2,060,465✔
1937

1938
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
545,364✔
1939
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
545,364✔
1940
}
1941

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

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

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

2006
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,040,075✔
2007
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,040,075✔
2008
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,040,075✔
2009
  int32_t        code = 0;
2,040,075✔
2010
  SNode*         pNode = NULL;
2,040,075✔
2011
  if (!pForceOutputCols) return 0;
2,040,075✔
2012
  if (!*pRes) {
36,805✔
2013
    code = createDataBlock(pRes);
36,805✔
2014
  }
2015

2016
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
36,805✔
2017
    int32_t idx = 0;
36,805✔
2018
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
147,038✔
2019
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
110,233✔
2020
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
110,233✔
2021
      colInfo.info.precision = pCol->type.precision;
110,233✔
2022
      colInfo.info.scale = pCol->type.scale;
110,233✔
2023
      code = blockDataAppendColInfo(*pRes, &colInfo);
110,233✔
2024
      if (code != 0) break;
110,233✔
2025
    }
2026
  }
2027

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

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

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

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

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

2159
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
207,688✔
2160
  if (pParam) {
207,688✔
2161
    if (pParam->tbname) {
207,688✔
2162
      taosMemFree(pParam->tbname);
207,688✔
2163
      pParam->tbname = NULL;
207,688✔
2164
    }
2165
    if (pParam->stbname) {
207,688✔
2166
      taosMemFree(pParam->stbname);
207,688✔
2167
      pParam->stbname = NULL;
207,688✔
2168
    }
2169
    if (pParam->dbFName) {
207,688✔
2170
      taosMemFree(pParam->dbFName);
207,688✔
2171
      pParam->dbFName = NULL;
207,688✔
2172
    }
2173
    if (pParam->pFields) {
207,688✔
2174
      taosArrayDestroy(pParam->pFields);
207,688✔
2175
      pParam->pFields = NULL;
207,688✔
2176
    }
2177
    if (pParam->pTagFields) {
207,688✔
2178
      taosArrayDestroy(pParam->pTagFields);
131,281✔
2179
      pParam->pTagFields = NULL;
131,281✔
2180
    }
2181
    if (pParam->colCids) {
207,688✔
2182
      taosArrayDestroy(pParam->colCids);
1,633✔
2183
      pParam->colCids = NULL;
1,633✔
2184
    }
2185
    if (pParam->tagCids) {
207,688✔
2186
      taosArrayDestroy(pParam->tagCids);
1,047✔
2187
      pParam->tagCids = NULL;
1,047✔
2188
    }
2189
    taosMemFree(pParam);
207,688✔
2190
  }
2191
}
207,688✔
2192

2193
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
207,487✔
2194
  int32_t code = 0, lino = 0;
207,487✔
2195
  if (ppDst == NULL || pSrc == NULL) {
207,487✔
2196
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2197
  }
2198
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
207,487✔
2199
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
207,688✔
2200

2201
  (*ppDst)->suid = pSrc->suid;
207,487✔
2202
  (*ppDst)->sver = pSrc->sver;
207,688✔
2203
  (*ppDst)->tbType = pSrc->tbType;
207,688✔
2204
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
207,489✔
2205
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
207,688✔
2206

2207
  if (pSrc->stbname) {
207,288✔
2208
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
207,688✔
2209
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
207,688✔
2210
  }
2211

2212
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
207,688✔
2213
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
207,688✔
2214

2215
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
207,288✔
2216

2217
  if (pSrc->pFields && pSrc->pFields->size > 0) {
207,489✔
2218
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
207,688✔
2219
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
207,489✔
2220
  } else {
2221
    (*ppDst)->pFields = NULL;
2✔
2222
  }
2223
  
2224
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
207,688✔
2225
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
131,281✔
2226
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
131,281✔
2227
  } else {
2228
    (*ppDst)->pTagFields = NULL;
76,208✔
2229
  }
2230

2231
  if (pSrc->colCids && pSrc->colCids->size > 0) {
207,688✔
2232
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,633✔
2233
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,633✔
2234
  } else {
2235
    (*ppDst)->colCids = NULL;
206,055✔
2236
  }
2237

2238
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
207,489✔
2239
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,047✔
2240
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,047✔
2241
  } else {
2242
    (*ppDst)->tagCids = NULL;
206,442✔
2243
  }
2244

2245
_exit:
207,688✔
2246

2247
  if (code != 0) {
207,688✔
2248
    if (*ppDst) {
×
2249
      destroyStreamInserterParam(*ppDst);
×
2250
      *ppDst = NULL;
×
2251
    }
2252
    
2253
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2254
  }
2255
  return code;
207,489✔
2256
}
2257

2258
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
235✔
2259
  return doDropStreamTable(pMsgCb, pOutput, pReq);
235✔
2260
}
2261

2262
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2263
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2264
}
2265

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

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