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

taosdata / TDengine / #4896

24 Dec 2025 07:36AM UTC coverage: 65.929% (+0.4%) from 65.513%
#4896

push

travis-ci

web-flow
enh: [TS-7591] Some code refactor and add more log. (#34022)

326 of 537 new or added lines in 4 files covered. (60.71%)

370 existing lines in 111 files now uncovered.

185828 of 281861 relevant lines covered (65.93%)

116309824.55 hits per line

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

68.95
/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) {
567,389,073✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
567,389,073✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
567,389,073✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
567,436,833✔
47
}
567,296,151✔
48

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

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

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

70
static void initRefPool() {
679,271✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
679,271✔
72
  (void)atexit(cleanupRefPool);
679,271✔
73
}
679,271✔
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) {
17,623,942✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
17,623,942✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
17,624,383✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
5,133,955✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
5,133,955✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
5,133,855✔
162
      if (pScanInfo->base.dataReader != NULL) {
5,133,754✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
58,154✔
164
        if (code) {
58,154✔
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);
12,489,500✔
172
  }
173

174
  return 0;
11,721,235✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
11,721,135✔
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) {
141,291✔
213
  if (msg == NULL) {  // create raw scan
141,291✔
214
    SExecTaskInfo* pTaskInfo = NULL;
23,993✔
215

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

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

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

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

248
  return pTaskInfo;
117,298✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
761,216✔
252
  if (streamInserterParam == NULL) {
761,216✔
253
    return TSDB_CODE_SUCCESS;
428,821✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
332,395✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
332,395✔
323
    if (code) {
331,990✔
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,
760,395✔
328
         tstrerror(code));
329

330
_error:
136,978✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
5,836,200✔
342
  if (pInfo == NULL) {
5,836,200✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,836,200✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,836,200✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,836,200✔
348
    return false;
8,874✔
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
5,827,326✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,534,144✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,361,470✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
354
}
355

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

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

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

376
  void*           info = pOperator->info;
5,827,326✔
377
  STableScanBase* pScanBaseInfo = NULL;
5,827,326✔
378

379
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,827,326✔
380
    pScanBaseInfo = &((STableScanInfo*)info)->base;
293,182✔
381
    setReadHandle(handle, pScanBaseInfo);
293,182✔
382
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,534,144✔
383
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,000,280✔
384
    setReadHandle(handle, pScanBaseInfo);
5,000,280✔
385
  }
386

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

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

398
  *pTaskInfo = NULL;
761,216✔
399

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

415
  return code;
760,395✔
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,
67,553✔
425
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
426
  int32_t code = TSDB_CODE_SUCCESS;
67,553✔
427
  int32_t lino = 0;
67,553✔
428
  int8_t  locked = 0;
67,553✔
429
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
67,553✔
430

431
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
67,553✔
432

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

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

442
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
67,553✔
443

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

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

453
  locked = 1;
67,553✔
454
  for (int32_t i = 0; i < numOfUids; ++i) {
135,673✔
455
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
68,120✔
456
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
68,120✔
457

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

464
    tDecoderClear(&mr.coder);
68,120✔
465

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

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

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

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

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

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

520
  // handle multiple partition
521

522
_end:
67,553✔
523

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

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

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

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

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

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

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

573
    bool   assignUid = false;
67,553✔
574
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
67,553✔
575
    char*  keyBuf = NULL;
67,553✔
576
    if (bufLen > 0) {
67,553✔
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;
67,553✔
586
    taosWLockLatch(&pTaskInfo->lock);
67,553✔
587

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

598
      if (bufLen > 0) {
38,240✔
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);
38,240✔
614
      if (code != TSDB_CODE_SUCCESS) {
38,240✔
615
        taosMemoryFree(keyBuf);
×
616
        taosArrayDestroy(qa);
×
617
        taosWUnLockLatch(&pTaskInfo->lock);
×
618
        return code;
×
619
      }
620
    }
621

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

627
    taosArrayDestroy(qa);
67,553✔
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);
146✔
630
    taosWLockLatch(&pTaskInfo->lock);
146✔
631
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
146✔
632
    taosWUnLockLatch(&pTaskInfo->lock);
146✔
633
  }
634

635
  return code;
67,699✔
636
}
637

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

643
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
480,136,566✔
644
    return TSDB_CODE_INVALID_PARA;
6✔
645
  }
646
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
480,146,335✔
647

648
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
480,146,335✔
649
    return TSDB_CODE_SUCCESS;
267,557,726✔
650
  }
651

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

658
  *sversion = pSchemaInfo->sw->version;
212,433,481✔
659
  *tversion = pSchemaInfo->tversion;
212,603,497✔
660
  *rversion = pSchemaInfo->rversion;
212,579,393✔
661
  if (pSchemaInfo->dbname) {
212,608,887✔
662
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
212,520,534✔
663
  } else {
664
    dbName[0] = 0;
×
665
  }
666
  if (pSchemaInfo->tablename) {
212,672,004✔
667
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
212,569,955✔
668
  } else {
669
    tableName[0] = 0;
62,969✔
670
  }
671

672
  *tbGet = true;
212,669,354✔
673

674
  return TSDB_CODE_SUCCESS;
212,632,751✔
675
}
676

677
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
267,521,441✔
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) {
13,959,147✔
687
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
13,959,147✔
688
  ((SExecTaskInfo*)tinfo)->paramSet = false;
13,964,867✔
689
}
13,963,723✔
690

691
int32_t qExecutorInit(void) {
4,832,558✔
692
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,832,558✔
693
  return TSDB_CODE_SUCCESS;
4,832,558✔
694
}
695

696
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
47,032,760✔
697
  int32_t        code = TSDB_CODE_SUCCESS;
47,032,760✔
698
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
47,032,760✔
699
  if (pTask->pWorkerCb) {
47,032,760✔
700
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
47,033,867✔
701
    if (code != TSDB_CODE_SUCCESS) {
47,035,598✔
702
      pTask->code = code;
×
703
      return pTask->code;
×
704
    }
705
  }
706

707
  code = tsem_wait(pSem);
47,037,872✔
708
  if (code != TSDB_CODE_SUCCESS) {
47,035,597✔
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) {
47,035,597✔
715
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
47,036,141✔
716
    if (code != TSDB_CODE_SUCCESS) {
47,036,141✔
717
      pTask->code = code;
×
718
      return pTask->code;
×
719
    }
720
  }
721
  return TSDB_CODE_SUCCESS;
47,036,141✔
722
}
723

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

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

733
  readHandle->uid = 0;
274,397,868✔
734
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
274,415,107✔
735
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
274,353,928✔
736
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
5,001,047✔
737
    goto _error;
4,972,408✔
738
  }
739
    
740
  if (handle) {
269,383,414✔
741
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
269,276,777✔
742
    void*           pSinkManager = NULL;
269,296,276✔
743
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
269,268,822✔
744
    if (code != TSDB_CODE_SUCCESS) {
269,106,838✔
745
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
746
      goto _error;
×
747
    }
748

749
    void* pSinkParam = NULL;
269,106,838✔
750
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
269,219,059✔
751
    if (code != TSDB_CODE_SUCCESS) {
269,093,669✔
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;
269,093,669✔
758
    if (readHandle->localExec) {
269,217,477✔
759
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
5,368✔
760
      if (code != TSDB_CODE_SUCCESS) {
5,368✔
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,
269,078,910✔
770
                              (*pTask)->id.str, pSubplan->processOneBlock);
269,238,432✔
771
    if (code) {
269,154,443✔
772
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
662✔
773
    }
774
  }
775

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

779
_error:
121,492,850✔
780
  // if failed to add ref for all tables in this query, abort current query
781
  return code;
274,384,932✔
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,
298,584,192✔
790
                     bool processOneBlock) {
791
  int32_t        code = TSDB_CODE_SUCCESS;
298,584,192✔
792
  int32_t        lino = 0;
298,584,192✔
793
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
298,584,192✔
794
  int64_t        threadId = taosGetSelfPthreadId();
298,584,192✔
795

796
  if (pLocal) {
298,543,092✔
797
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
292,258,425✔
798
  }
799

800
  taosArrayClear(pResList);
298,558,414✔
801

802
  int64_t curOwner = 0;
298,511,322✔
803
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
298,511,322✔
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) {
298,488,518✔
810
    pTaskInfo->cost.start = taosGetTimestampUs();
267,323,801✔
811
  }
812

813
  if (isTaskKilled(pTaskInfo)) {
298,617,174✔
UNCOV
814
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
815
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
816
    return pTaskInfo->code;
×
817
  }
818

819
  // error occurs, record the error code and return to client
820
  int32_t ret = setjmp(pTaskInfo->env);
298,475,311✔
821
  if (ret != TSDB_CODE_SUCCESS) {
302,755,318✔
822
    pTaskInfo->code = ret;
4,389,230✔
823
    (void)cleanUpUdfs();
4,389,230✔
824

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

828
    return pTaskInfo->code;
4,389,230✔
829
  }
830

831
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
298,366,088✔
832

833
  int32_t      current = 0;
298,367,790✔
834
  SSDataBlock* pRes = NULL;
298,367,790✔
835
  int64_t      st = taosGetTimestampUs();
298,500,600✔
836

837
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
298,500,600✔
838
    pTaskInfo->paramSet = true;
13,959,719✔
839
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
13,958,575✔
840
  } else {
841
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
284,627,515✔
842
  }
843

844
  QUERY_CHECK_CODE(code, lino, _end);
294,208,659✔
845
  code = blockDataCheck(pRes);
294,208,659✔
846
  QUERY_CHECK_CODE(code, lino, _end);
294,204,933✔
847

848
  if (pRes == NULL) {
294,204,933✔
849
    st = taosGetTimestampUs();
73,226,698✔
850
  }
851

852
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
294,202,896✔
853
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
294,246,008✔
854
    rowsThreshold = 4096;
293,406,268✔
855
  }
856

857
  int32_t blockIndex = 0;
294,218,626✔
858
  while (pRes != NULL) {
745,578,428✔
859
    SSDataBlock* p = NULL;
474,208,863✔
860
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
474,157,405✔
861
      SSDataBlock* p1 = NULL;
359,514,915✔
862
      code = createOneDataBlock(pRes, true, &p1);
359,516,926✔
863
      QUERY_CHECK_CODE(code, lino, _end);
359,480,257✔
864

865
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
359,480,257✔
866
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
359,540,842✔
867
      p = p1;
359,540,842✔
868
    } else {
869
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
114,699,623✔
870
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
114,710,690✔
871

872
      p = *(SSDataBlock**)tmp;
114,710,690✔
873
      code = copyDataBlock(p, pRes);
114,712,163✔
874
      QUERY_CHECK_CODE(code, lino, _end);
114,711,217✔
875
    }
876

877
    blockIndex += 1;
474,228,873✔
878

879
    current += p->info.rows;
474,228,873✔
880
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
474,234,213✔
881
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
882
    void* tmp = taosArrayPush(pResList, &p);
474,246,035✔
883
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
474,246,035✔
884

885
    if (current >= rowsThreshold || processOneBlock) {
474,246,035✔
886
      break;
887
    }
888

889
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
451,383,132✔
890
    QUERY_CHECK_CODE(code, lino, _end);
451,353,823✔
891
    code = blockDataCheck(pRes);
451,353,823✔
892
    QUERY_CHECK_CODE(code, lino, _end);
451,401,838✔
893
  }
894

895
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
294,232,468✔
896
    pTaskInfo->pSubplan->rowsThreshold -= current;
805,396✔
897
  }
898

899
  *hasMore = (pRes != NULL);
294,234,138✔
900
  uint64_t el = (taosGetTimestampUs() - st);
294,186,536✔
901

902
  pTaskInfo->cost.elapsedTime += el;
294,186,536✔
903
  if (NULL == pRes) {
294,169,223✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
271,306,320✔
905
  }
906

907
_end:
294,154,590✔
908
  (void)cleanUpUdfs();
294,138,682✔
909

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

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

920
  return pTaskInfo->code;
294,259,707✔
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) {
43,407,673✔
938
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
43,407,673✔
939
  int64_t        threadId = taosGetSelfPthreadId();
43,407,673✔
940
  int64_t        curOwner = 0;
43,407,782✔
941

942
  *pRes = NULL;
43,407,782✔
943

944
  // todo extract method
945
  taosRLockLatch(&pTaskInfo->lock);
43,407,891✔
946
  bool isKilled = isTaskKilled(pTaskInfo);
43,408,218✔
947
  if (isKilled) {
43,408,218✔
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) {
43,408,218✔
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;
43,408,218✔
963
  taosRUnLockLatch(&pTaskInfo->lock);
43,408,218✔
964

965
  if (pTaskInfo->cost.start == 0) {
43,408,327✔
966
    pTaskInfo->cost.start = taosGetTimestampUs();
94,554✔
967
  }
968

969
  // error occurs, record the error code and return to client
970
  int32_t ret = setjmp(pTaskInfo->env);
43,408,218✔
971
  if (ret != TSDB_CODE_SUCCESS) {
43,407,969✔
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));
43,407,969✔
980

981
  int64_t st = taosGetTimestampUs();
43,408,000✔
982
  int32_t code = TSDB_CODE_SUCCESS;
43,408,000✔
983
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
43,408,000✔
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);
43,408,000✔
988
  }
989
  if (code) {
43,394,046✔
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);
43,394,046✔
995
  if (code) {
43,407,232✔
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);
43,401,012✔
1001

1002
  pTaskInfo->cost.elapsedTime += el;
43,401,012✔
1003
  if (NULL == *pRes) {
43,401,760✔
1004
    *useconds = pTaskInfo->cost.elapsedTime;
4,798,824✔
1005
  }
1006

1007
  (void)cleanUpUdfs();
43,403,496✔
1008

1009
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
43,406,023✔
1010
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
43,408,327✔
1011

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

1015
  atomic_store_64(&pTaskInfo->owner, 0);
43,408,872✔
1016
  return pTaskInfo->code;
43,408,162✔
1017
}
1018

1019
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
68,567,236✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
68,567,236✔
1021
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
68,567,321✔
1022
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
68,567,440✔
1023

1024
  if (!tmp) {
68,567,440✔
1025
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1026
    return terrno;
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
68,567,440✔
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) {
43,061✔
1054
  if (pTaskInfo->subJobCtx.hasSubJobs) {
43,061✔
1055
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
16,859✔
1056
    if (pTaskInfo->subJobCtx.param) {
16,859✔
1057
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
3,621✔
1058
    }
1059
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
16,859✔
1060
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
16,859✔
1061
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
16,859✔
1062
  }
1063
  
1064
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
43,061✔
1065

1066
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
43,061✔
1067
  for (int32_t i = 0; i < num; ++i) {
44,328✔
1068
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
1,267✔
1069
    if (!pStop) {
1,267✔
1070
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1071
      continue;
×
1072
    }
1073
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
1,267✔
1074
    if (pExchangeInfo) {
1,267✔
1075
      int32_t code = tsem_post(&pExchangeInfo->ready);
1,267✔
1076
      if (code != TSDB_CODE_SUCCESS) {
1,267✔
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);
1,267✔
1080
      }
1081
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
1,267✔
1082
      if (code != TSDB_CODE_SUCCESS) {
1,267✔
1083
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1084
      }
1085
    }
1086
  }
1087

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

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

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

1099
  setTaskKilled(pTaskInfo, rspCode);
43,061✔
1100
  qStopTaskOperators(pTaskInfo);
43,061✔
1101

1102
  return TSDB_CODE_SUCCESS;
43,061✔
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) {
270,223,797✔
1159
  STaskCostInfo* pSummary = &pTaskInfo->cost;
270,223,797✔
1160
  int64_t        idleTime = pSummary->start - pSummary->created;
270,238,737✔
1161

1162
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
270,257,670✔
1163
  if (pSummary->pRecoder != NULL) {
270,207,430✔
1164
    qDebug(
209,105,706✔
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,
61,088,524✔
1173
           pSummary->elapsedTime / 1000.0);
1174
  }
1175
}
270,194,230✔
1176

1177

1178
void qDestroyTask(qTaskInfo_t qTaskHandle) {
282,902,701✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
282,902,701✔
1180
  if (pTaskInfo == NULL) {
282,902,701✔
1181
    return;
12,678,401✔
1182
  }
1183

1184
  if (pTaskInfo->pRoot != NULL) {
270,224,300✔
1185
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
270,238,266✔
1186
  } else {
1187
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1188
  }
1189

1190
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
270,238,128✔
1191
  doDestroyTask(pTaskInfo);
270,220,355✔
1192
}
1193

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

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

1203
  while (1) {
116,174✔
1204
    uint16_t type = pOperator->operatorType;
233,472✔
1205
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
233,472✔
1206
      *scanner = pOperator->info;
117,298✔
1207
      break;
117,298✔
1208
    } else {
1209
      pOperator = pOperator->pDownstream[0];
116,174✔
1210
    }
1211
  }
1212
}
117,298✔
1213

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

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

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

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

1234
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
5,249,247✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
5,249,247✔
1236
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
5,249,247✔
1237
  return 0;
5,249,247✔
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) {
149,664✔
1246
  memset(pCond, 0, sizeof(SQueryTableDataCond));
149,664✔
1247
  pCond->order = TSDB_ORDER_ASC;
149,664✔
1248
  pCond->numOfCols = pMtInfo->schema->nCols;
149,598✔
1249
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
149,776✔
1250
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
149,572✔
1251
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
149,664✔
1252
    taosMemoryFreeClear(pCond->colList);
56✔
1253
    taosMemoryFreeClear(pCond->pSlotList);
×
1254
    return terrno;
×
1255
  }
1256

1257
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
149,664✔
1258
  pCond->suid = pMtInfo->suid;
149,720✔
1259
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
149,628✔
1260
  pCond->startVersion = -1;
149,684✔
1261
  pCond->endVersion = sContext->snapVersion;
149,664✔
1262

1263
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
957,673✔
1264
    SColumnInfo* pColInfo = &pCond->colList[i];
807,989✔
1265
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
807,989✔
1266
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
808,045✔
1267
    if (pMtInfo->pExtSchemas != NULL) {
808,025✔
1268
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,892✔
1269
    }
1270
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
808,045✔
1271
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
808,025✔
1272

1273
    pCond->pSlotList[i] = i;
808,015✔
1274
  }
1275

1276
  return TSDB_CODE_SUCCESS;
149,776✔
1277
}
1278

1279
void qStreamSetOpen(qTaskInfo_t tinfo) {
43,109,669✔
1280
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
43,109,669✔
1281
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
43,109,669✔
1282
  pOperator->status = OP_NOT_OPENED;
43,113,893✔
1283
}
43,113,094✔
1284

1285
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
5,086,499✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
5,086,499✔
1287
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
5,086,499✔
1288
}
5,086,773✔
1289

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

1295
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
5,295,986✔
1296
  const char*    id = GET_TASKID(pTaskInfo);
5,295,767✔
1297

1298
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
5,296,049✔
1299
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
5,027,576✔
1300
    if (pOperator == NULL || code != 0) {
5,026,225✔
1301
      return code;
×
1302
    }
1303

1304
    SStreamScanInfo* pInfo = pOperator->info;
5,027,724✔
1305
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
5,027,640✔
1306
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
5,026,490✔
1307
    walReaderVerifyOffset(pWalReader, pOffset);
5,027,997✔
1308
  }
1309
  // if pOffset equal to current offset, means continue consume
1310
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
5,296,078✔
1311
    return 0;
4,876,073✔
1312
  }
1313

1314
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
418,544✔
1315
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
264,712✔
1316
    if (pOperator == NULL || code != 0) {
264,712✔
1317
      return code;
×
1318
    }
1319

1320
    SStreamScanInfo* pInfo = pOperator->info;
264,793✔
1321
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
264,793✔
1322
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
264,712✔
1323
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
264,712✔
1324

1325
    if (pOffset->type == TMQ_OFFSET__LOG) {
264,712✔
1326
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
216,294✔
1327
      pScanBaseInfo->dataReader = NULL;
216,385✔
1328

1329
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
216,385✔
1330
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
216,385✔
1331
      walReaderVerifyOffset(pWalReader, pOffset);
216,294✔
1332
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
216,375✔
1333
      if (code < 0) {
216,385✔
1334
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,844✔
1335
        return code;
7,844✔
1336
      }
1337
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
48,165✔
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;
48,327✔
1341
      int64_t ts = pOffset->ts;
48,246✔
1342
      int32_t index = 0;
48,165✔
1343

1344
      // this value may be changed if new tables are created
1345
      taosRLockLatch(&pTaskInfo->lock);
48,165✔
1346
      int32_t numOfTables = 0;
48,408✔
1347
      code = tableListGetSize(pTableListInfo, &numOfTables);
48,408✔
1348
      if (code != TSDB_CODE_SUCCESS) {
48,327✔
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) {
48,327✔
1355
        if (numOfTables != 0) {
46,985✔
1356
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
8,290✔
1357
          if (!tmp) {
8,217✔
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;
8,217✔
1363
          ts = INT64_MIN;
8,217✔
1364
          pScanInfo->currentTable = 0;
8,217✔
1365
        } else {
1366
          taosRUnLockLatch(&pTaskInfo->lock);
38,695✔
1367
          qError("no table in table list, %s", id);
38,695✔
1368
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
38,776✔
1369
        }
1370
      }
1371
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,632✔
1372

1373
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
9,632✔
1374
             pInfo->pTableScanOp->resultInfo.totalRows);
1375
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
9,632✔
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);
9,632✔
1381
      taosRUnLockLatch(&pTaskInfo->lock);
9,632✔
1382

1383
      if (index >= 0) {
9,632✔
1384
        pScanInfo->currentTable = index;
9,632✔
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};
9,632✔
1392
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
9,632✔
1393

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

1402
      if (pScanBaseInfo->dataReader == NULL) {
9,632✔
1403
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
16,506✔
1404
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1405
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
8,253✔
1406
        if (code != TSDB_CODE_SUCCESS) {
8,253✔
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",
8,253✔
1412
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1413
      } else {
1414
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,379✔
1415
        if (code != TSDB_CODE_SUCCESS) {
1,379✔
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);
1,379✔
1421
        if (code != TSDB_CODE_SUCCESS) {
1,379✔
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",
1,379✔
1426
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1427
      }
1428

1429
      // restore the key value
1430
      pScanBaseInfo->cond.twindows.skey = oldSkey;
9,705✔
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) {
153,832✔
1438
      SStreamRawScanInfo* pInfo = pOperator->info;
149,970✔
1439
      SSnapContext*       sContext = pInfo->sContext;
149,970✔
1440
      SOperatorInfo*      p = NULL;
149,970✔
1441

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

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

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

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

1463
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
149,914✔
1464
      tableListClear(pTableListInfo);
149,792✔
1465

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

1471
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
149,720✔
1472
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
149,664✔
1473
      if (code != TSDB_CODE_SUCCESS) {
149,776✔
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)) {
149,776✔
1479
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
393✔
1480
      } else {
1481
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
149,327✔
1482
      }
1483

1484
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
149,628✔
1485
      if (code != TSDB_CODE_SUCCESS) {
149,776✔
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);
149,776✔
1492
      if (!pList) {
149,776✔
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;
149,776✔
1498
      code = tableListGetSize(pTableListInfo, &size);
149,776✔
1499
      if (code != TSDB_CODE_SUCCESS) {
149,776✔
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,
299,552✔
1506
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
149,776✔
1507
      if (code != TSDB_CODE_SUCCESS) {
149,598✔
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);
149,598✔
1514
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
149,654✔
1515
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1516
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
149,598✔
1517
      pTaskInfo->streamInfo.schema = mtInfo.schema;
149,598✔
1518
      taosMemoryFreeClear(mtInfo.pExtSchemas);
149,506✔
1519

1520
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
149,506✔
1521
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,796✔
1522
      SStreamRawScanInfo* pInfo = pOperator->info;
1,315✔
1523
      SSnapContext*       sContext = pInfo->sContext;
1,315✔
1524
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,315✔
1525
      if (code != 0) {
1,315✔
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,
1,315✔
1530
             id);
1531
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
2,481✔
1532
      SStreamRawScanInfo* pInfo = pOperator->info;
2,547✔
1533
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,547✔
1534
      pInfo->dataReader = NULL;
2,547✔
1535
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
2,547✔
1536
    }
1537
  }
1538

1539
end:
371,997✔
1540
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
372,005✔
1541
  return 0;
372,005✔
1542
}
1543

1544
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
193,658,783✔
1545
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
193,658,783✔
1546
  if (pMsg->info.ahandle == NULL) {
193,685,024✔
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, 
193,659,165✔
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};
193,661,862✔
1556

1557
  if (pMsg->contLen > 0) {
193,670,277✔
1558
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
193,646,473✔
1559
    if (buf.pData == NULL) {
193,570,733✔
1560
      pMsg->code = terrno;
×
1561
    } else {
1562
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
193,570,733✔
1563
    }
1564
  }
1565

1566
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
193,648,058✔
1567
  rpcFreeCont(pMsg->pCont);
193,700,808✔
1568
  destroySendMsgInfo(pSendInfo);
193,670,064✔
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,546,527✔
1610
  int32_t        code = TSDB_CODE_SUCCESS;
3,546,527✔
1611
  int32_t        lino = 0;
3,546,527✔
1612
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,546,527✔
1613

1614
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,550,289✔
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,545,277✔
1621
    STableScanInfo* pScanInfo = pOperator->info;
1,773,580✔
1622

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

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

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

1643
  *pList = NULL;
1,772,955✔
1644
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,774,205✔
1645
  if (pArray == NULL) {
1,772,955✔
1646
    return terrno;
×
1647
  }
1648

1649
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,772,955✔
1650
  if (code == 0) {
1,772,955✔
1651
    *pList = pArray;
1,772,955✔
1652
  } else {
1653
    taosArrayDestroy(pArray);
×
1654
  }
1655
  return code;
1,771,076✔
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) {
42,279,420✔
1694
  int32_t code = 0;
42,279,420✔
1695

1696
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
42,279,420✔
1697
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
42,279,424✔
1698

1699
  if (pOper->fpSet.resetStateFn) {
42,278,983✔
1700
    code = pOper->fpSet.resetStateFn(pOper);
42,278,575✔
1701
  }
1702
  pOper->status = OP_NOT_OPENED;
42,277,759✔
1703
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
71,714,163✔
1704
    code = clearStatesForOperator(pOper->pDownstream[i]);
29,433,961✔
1705
  }
1706
  return code;
42,281,454✔
1707
}
1708

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

1718
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
17,444,640✔
1719
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
17,444,640✔
1720
  int64_t        threadId = taosGetSelfPthreadId();
17,444,640✔
1721
  int64_t        curOwner = 0;
17,445,494✔
1722

1723
  *ppRes = NULL;
17,445,494✔
1724

1725
  // todo extract method
1726
  taosRLockLatch(&pTaskInfo->lock);
17,445,494✔
1727
  bool isKilled = isTaskKilled(pTaskInfo);
17,445,494✔
1728
  if (isKilled) {
17,444,640✔
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) {
17,444,640✔
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;
17,444,640✔
1745
  taosRUnLockLatch(&pTaskInfo->lock);
17,444,640✔
1746

1747
  if (pTaskInfo->cost.start == 0) {
17,445,494✔
1748
    pTaskInfo->cost.start = taosGetTimestampUs();
341,269✔
1749
  }
1750

1751
  // error occurs, record the error code and return to client
1752
  int32_t ret = setjmp(pTaskInfo->env);
17,445,494✔
1753
  if (ret != TSDB_CODE_SUCCESS) {
20,124,405✔
1754
    pTaskInfo->code = ret;
2,680,990✔
1755
    (void)cleanUpUdfs();
2,680,990✔
1756
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,680,990✔
1757
    atomic_store_64(&pTaskInfo->owner, 0);
2,680,990✔
1758
    return pTaskInfo->code;
2,680,990✔
1759
  }
1760

1761
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,443,415✔
1762

1763
  int64_t st = taosGetTimestampUs();
17,445,494✔
1764

1765
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
17,445,494✔
1766
  if (code) {
14,764,504✔
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;
14,764,504✔
1771
    code = blockDataCheck(*ppRes);
14,764,504✔
1772
  }
1773
  if (code) {
14,764,072✔
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);
14,764,094✔
1779

1780
  pTaskInfo->cost.elapsedTime += el;
14,764,094✔
1781
  if (NULL == *ppRes) {
14,764,094✔
1782
    *useconds = pTaskInfo->cost.elapsedTime;
9,548,131✔
1783
  }
1784

1785
  (void)cleanUpUdfs();
14,764,094✔
1786

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

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

1793
  atomic_store_64(&pTaskInfo->owner, 0);
14,764,504✔
1794
  return pTaskInfo->code;
14,764,504✔
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,
338,758✔
1803
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1804
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1805
  int32_t code = 0;                                        
338,758✔
1806
  if (*pTableListInfo != NULL) {
338,758✔
1807
    qDebug("table list already exists, no need to create again");
×
1808
    goto end;
×
1809
  }
1810
  STableListInfo* pList = tableListCreate();
338,758✔
1811
  if (pList == NULL) {
338,758✔
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};
338,758✔
1818
  SReadHandle    pHandle = {.vnode = pVnode};
338,758✔
1819
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
338,758✔
1820

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

1829
end:
338,758✔
1830
  return 0;
338,758✔
1831
}
1832

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

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

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

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

1884
  taosArrayClear(uidList);
133,565✔
1885
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
267,130✔
1886
    void* tmp = taosArrayGet(uidListCopy, i);
133,565✔
1887
    if (tmp == NULL) {
133,565✔
1888
      continue;
×
1889
    }
1890
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
133,565✔
1891
    if (slot == NULL) {
133,565✔
1892
      if (taosArrayPush(uidList, tmp) == NULL) {
6,229✔
1893
        code = terrno;
×
1894
        goto end;
×
1895
      }
1896
    }
1897
  }
1898
end:
133,565✔
1899
  taosArrayDestroy(uidListCopy);
133,565✔
1900
  tableListDestroy(pList);
133,565✔
1901
  return code;
133,565✔
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); }
338,758✔
1926
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
338,758✔
1927
  STableListInfo* pList = pTableListInfo;
338,758✔
1928
  return pList->pTableList;
338,758✔
1929
}
1930

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

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

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

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

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

1970
  if (code == 0) {
992,315✔
1971
    const char* pVal = NULL;
992,315✔
1972
    int32_t     len = 0;
992,315✔
1973
    SNode*      pSclNode = NULL;
992,315✔
1974
    switch (pExprInfo->pExpr->nodeType) {
992,315✔
1975
      case QUERY_NODE_FUNCTION:
992,315✔
1976
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
992,315✔
1977
        break;
992,315✔
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);
992,315✔
1986
    SSDataBlock block = {0};
992,315✔
1987
    block.info.rows = 1;
992,315✔
1988
    SSDataBlock* pBlock = &block;
992,315✔
1989
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
992,315✔
1990
    if (tmp == NULL) {
992,315✔
1991
      code = terrno;
×
1992
    }
1993
    if (code == 0) {
992,315✔
1994
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
992,315✔
1995
      gTaskScalarExtra.pStreamRange = NULL;
992,315✔
1996
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
992,315✔
1997
    }
1998
    taosArrayDestroy(pBlockList);
992,315✔
1999
  }
2000
  nodesDestroyList(pList);
992,315✔
2001
  destroyExprInfo(pExprInfo, numOfExprs);
992,315✔
2002
  taosMemoryFreeClear(pExprInfo);
992,315✔
2003
  return code;
992,315✔
2004
}
2005

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

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

2028
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
47,841✔
2029
  if (code != TSDB_CODE_SUCCESS) {
47,841✔
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;
47,841✔
2036
  int32_t rowIdx = (*pRes)->info.rows;
47,841✔
2037
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
47,841✔
2038
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
47,841✔
2039
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
190,983✔
2040
    SScalarParam   dst = {0};
143,142✔
2041
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
143,142✔
2042
    code = nodesStringToNode(pCol->expr, &pNode);
143,142✔
2043
    if (code != 0) break;
143,142✔
2044
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
143,142✔
2045
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
143,142✔
2046
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
95,301✔
2047
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
95,301✔
2048
    } else {
2049
      dst.columnData = pInfo;
47,841✔
2050
      dst.numOfRows = rowIdx;
47,841✔
2051
      dst.colAlloced = false;
47,841✔
2052
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
47,841✔
2053
    }
2054
    ++idx;
143,142✔
2055
    // TODO sclFreeParam(&dst);
2056
    nodesDestroyNode(pNode);
143,142✔
2057
    if (code != 0) break;
143,142✔
2058
  }
2059
  if (code == TSDB_CODE_SUCCESS) {
47,841✔
2060
    (*pRes)->info.rows++;
47,841✔
2061
  }
2062
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
47,841✔
2063
  return code;
47,841✔
2064
}
2065

2066
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
358,661✔
2067
  int32_t      code = 0;
358,661✔
2068
  const char*  pVal = NULL;
358,661✔
2069
  SScalarParam dst = {0};
358,661✔
2070
  int32_t      len = 0;
358,661✔
2071
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
358,661✔
2072
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
358,661✔
2073
  // execute the expr
2074
  switch (pExpr->type) {
358,661✔
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: {
358,661✔
2093
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
358,661✔
2094
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
358,661✔
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));
358,661✔
2100
      if (!pCol) {
358,661✔
2101
        code = terrno;
×
2102
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2103
        break;
×
2104
      }
2105

2106
      pCol->hasNull = true;
358,661✔
2107
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
358,661✔
2108
      pCol->info.colId = 0;
358,661✔
2109
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
358,661✔
2110
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
358,661✔
2111
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
358,661✔
2112
      code = colInfoDataEnsureCapacity(pCol, 1, true);
358,661✔
2113
      if (code != 0) {
358,661✔
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;
358,661✔
2119
      dst.numOfRows = 1;
358,661✔
2120
      dst.colAlloced = true;
358,661✔
2121
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
358,661✔
2122
      if (colDataIsNull_var(dst.columnData, 0)) {
358,661✔
2123
        qInfo("invalid sub tb expr with null value");
5,415✔
2124
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
5,415✔
2125
      }
2126
      if (code == 0) {
358,661✔
2127
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
353,246✔
2128
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
353,246✔
2129
      }
2130
    } break;
358,661✔
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) {
358,661✔
2137
    if (!pVal || len == 0) {
353,246✔
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) {
353,246✔
2142
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
820✔
2143
      len = TSDB_TABLE_NAME_LEN - 1;
820✔
2144
    }
2145

2146
    memcpy(tbname, pVal, len);
353,246✔
2147
    tbname[len] = '\0';  // ensure null terminated
353,246✔
2148
    if (NULL != strchr(tbname, '.')) {
353,246✔
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);
358,661✔
2155
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
358,661✔
2156
  return code;
358,661✔
2157
}
2158

2159
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
332,395✔
2160
  if (pParam) {
332,395✔
2161
    if (pParam->tbname) {
332,395✔
2162
      taosMemFree(pParam->tbname);
332,395✔
2163
      pParam->tbname = NULL;
332,395✔
2164
    }
2165
    if (pParam->stbname) {
332,395✔
2166
      taosMemFree(pParam->stbname);
332,395✔
2167
      pParam->stbname = NULL;
332,395✔
2168
    }
2169
    if (pParam->dbFName) {
332,395✔
2170
      taosMemFree(pParam->dbFName);
332,395✔
2171
      pParam->dbFName = NULL;
332,395✔
2172
    }
2173
    if (pParam->pFields) {
332,395✔
2174
      taosArrayDestroy(pParam->pFields);
332,395✔
2175
      pParam->pFields = NULL;
332,395✔
2176
    }
2177
    if (pParam->pTagFields) {
332,395✔
2178
      taosArrayDestroy(pParam->pTagFields);
216,046✔
2179
      pParam->pTagFields = NULL;
216,046✔
2180
    }
2181
    taosMemFree(pParam);
332,395✔
2182
  }
2183
}
332,395✔
2184

2185
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
331,889✔
2186
  int32_t code = 0, lino = 0;
331,889✔
2187
  if (ppDst == NULL || pSrc == NULL) {
331,889✔
2188
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2189
  }
2190
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
331,889✔
2191
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
331,889✔
2192

2193
  (*ppDst)->suid = pSrc->suid;
331,889✔
2194
  (*ppDst)->sver = pSrc->sver;
332,395✔
2195
  (*ppDst)->tbType = pSrc->tbType;
331,889✔
2196
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
331,889✔
2197
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
331,889✔
2198

2199
  if (pSrc->stbname) {
331,889✔
2200
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
332,395✔
2201
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
331,889✔
2202
  }
2203

2204
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
331,383✔
2205
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
331,889✔
2206

2207
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
331,889✔
2208

2209
  if (pSrc->pFields && pSrc->pFields->size > 0) {
332,395✔
2210
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
332,395✔
2211
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
332,395✔
2212
  } else {
2213
    (*ppDst)->pFields = NULL;
×
2214
  }
2215
  
2216
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
331,889✔
2217
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
216,046✔
2218
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
216,046✔
2219
  } else {
2220
    (*ppDst)->pTagFields = NULL;
115,843✔
2221
  }
2222

2223
_exit:
332,395✔
2224

2225
  if (code != 0) {
332,395✔
2226
    if (*ppDst) {
×
2227
      destroyStreamInserterParam(*ppDst);
×
2228
      *ppDst = NULL;
×
2229
    }
2230
    
2231
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2232
  }
2233
  return code;
331,889✔
2234
}
2235

2236
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
443✔
2237
  return doDropStreamTable(pMsgCb, pOutput, pReq);
443✔
2238
}
2239

2240
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2241
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2242
}
2243

2244
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
1,122✔
2245
  int32_t code = TSDB_CODE_SUCCESS;
1,122✔
2246
  STableListInfo* pList = tableListCreate();
1,122✔
2247
  if (pList == NULL) {
1,122✔
2248
    code = terrno;
×
2249
    goto end;
×
2250
  }
2251
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2252
  // if (uidListCopy == NULL) {
2253
  //   code = terrno;
2254
  //   goto end;
2255
  // }
2256

2257
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
1,122✔
2258
  pList->idInfo.suid = suid;
1,122✔
2259
  pList->idInfo.tableType = TD_SUPER_TABLE;
1,122✔
2260
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
1,122✔
2261
  if (code != TSDB_CODE_SUCCESS) {
1,122✔
2262
    goto end;
×
2263
  }                                              
2264
  // taosArrayClear(uidList);
2265
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2266
  //   void* tmp = taosArrayGet(uidListCopy, i);
2267
  //   if (tmp == NULL) {
2268
  //     continue;
2269
  //   }
2270
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2271
  //   if (slot == NULL) {
2272
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2273
  //       code = terrno;
2274
  //       goto end;
2275
  //     }
2276
  //   }
2277
  // }
2278
end:
1,122✔
2279
  // taosArrayDestroy(uidListCopy);
2280
  tableListDestroy(pList);
1,122✔
2281
  return code;
1,122✔
2282
}
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