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

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

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

68.98
/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) {
736,190,014✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
736,190,014✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
736,190,014✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
736,377,771✔
47
}
735,897,656✔
48

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

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

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

70
static void initRefPool() {
524,611✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
524,611✔
72
  (void)atexit(cleanupRefPool);
524,611✔
73
}
524,611✔
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) {
45,268,968✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
45,268,968✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
45,270,753✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
20,623,004✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
20,623,332✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
20,623,329✔
162
      if (pScanInfo->base.dataReader != NULL) {
20,622,436✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
255,036✔
164
        if (code) {
255,036✔
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);
24,645,356✔
172
  }
173

174
  return 0;
24,150,214✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
24,148,102✔
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) {
420,820✔
213
  if (msg == NULL) {  // create raw scan
420,820✔
214
    SExecTaskInfo* pTaskInfo = NULL;
112,417✔
215

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

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

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

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

248
  return pTaskInfo;
308,978✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
532,549✔
252
  if (streamInserterParam == NULL) {
532,549✔
253
    return TSDB_CODE_SUCCESS;
326,045✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
206,705✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
206,705✔
323
    if (code) {
206,705✔
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,
532,750✔
328
         tstrerror(code));
329

330
_error:
71,872✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,998,663✔
342
  if (pInfo == NULL) {
2,998,663✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,998,663✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,998,663✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,998,663✔
348
    return false;
4,360✔
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,994,303✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,775,332✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
5,769,635✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
354
}
355

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

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

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

376
  void*           info = pOperator->info;
2,994,303✔
377
  STableScanBase* pScanBaseInfo = NULL;
2,994,303✔
378

379
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,994,303✔
380
    pScanBaseInfo = &((STableScanInfo*)info)->base;
218,971✔
381
    setReadHandle(handle, pScanBaseInfo);
218,971✔
382
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,775,332✔
383
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,317,342✔
384
    setReadHandle(handle, pScanBaseInfo);
2,317,342✔
385
  }
386

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

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

398
  *pTaskInfo = NULL;
532,542✔
399

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

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

431
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
352,344✔
432

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

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

442
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
352,344✔
443

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

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

453
  locked = 1;
352,344✔
454
  for (int32_t i = 0; i < numOfUids; ++i) {
713,877✔
455
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
361,533✔
456
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
361,533✔
457

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

464
    tDecoderClear(&mr.coder);
361,533✔
465

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

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

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

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

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

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

520
  // handle multiple partition
521

522
_end:
352,344✔
523

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

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

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

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

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

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

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

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

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

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

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

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

635
  return code;
354,288✔
636
}
637

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

643
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
630,108,967✔
644
    return TSDB_CODE_INVALID_PARA;
×
645
  }
646
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
630,192,472✔
647

648
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
630,192,472✔
649
    return TSDB_CODE_SUCCESS;
355,746,722✔
650
  }
651

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

658
  *sversion = pSchemaInfo->sw->version;
274,166,590✔
659
  *tversion = pSchemaInfo->tversion;
274,468,650✔
660
  *rversion = pSchemaInfo->rversion;
274,430,600✔
661
  if (pSchemaInfo->dbname) {
274,507,736✔
662
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
274,598,774✔
663
  } else {
664
    dbName[0] = 0;
×
665
  }
666
  if (pSchemaInfo->tablename) {
274,711,592✔
667
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
274,596,410✔
668
  } else {
669
    tableName[0] = 0;
4,261✔
670
  }
671

672
  *tbGet = true;
274,710,017✔
673

674
  return TSDB_CODE_SUCCESS;
274,607,158✔
675
}
676

677
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
355,584,699✔
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) {
595,221✔
687
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
595,221✔
688
  ((SExecTaskInfo*)tinfo)->paramSet = false;
595,221✔
689
}
595,221✔
690

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

696
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
55,179,984✔
697
  int32_t        code = TSDB_CODE_SUCCESS;
55,179,984✔
698
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
55,179,984✔
699
  if (pTask->pWorkerCb) {
55,179,984✔
700
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
55,188,140✔
701
    if (code != TSDB_CODE_SUCCESS) {
55,201,873✔
702
      pTask->code = code;
×
703
      return pTask->code;
×
704
    }
705
  }
706

707
  code = tsem_wait(pSem);
55,211,617✔
708
  if (code != TSDB_CODE_SUCCESS) {
55,198,703✔
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) {
55,198,703✔
715
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
55,201,873✔
716
    if (code != TSDB_CODE_SUCCESS) {
55,201,416✔
717
      pTask->code = code;
×
718
      return pTask->code;
×
719
    }
720
  }
721
  return TSDB_CODE_SUCCESS;
55,199,581✔
722
}
723

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

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

733
  readHandle->uid = 0;
366,539,411✔
734
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
366,609,455✔
735
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
366,417,242✔
736
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
9,064,726✔
737
    goto _error;
8,929,848✔
738
  }
739
    
740
  if (handle) {
357,447,896✔
741
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
357,192,839✔
742
    void*           pSinkManager = NULL;
357,299,998✔
743
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
357,174,177✔
744
    if (code != TSDB_CODE_SUCCESS) {
356,502,418✔
745
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
746
      goto _error;
×
747
    }
748

749
    void* pSinkParam = NULL;
356,502,418✔
750
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
357,003,492✔
751
    if (code != TSDB_CODE_SUCCESS) {
356,797,809✔
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;
356,797,809✔
758
    if (readHandle->localExec) {
357,027,779✔
759
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,904✔
760
      if (code != TSDB_CODE_SUCCESS) {
2,904✔
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,
356,753,335✔
770
                              (*pTask)->id.str, pSubplan->processOneBlock);
357,215,943✔
771
    if (code) {
356,914,690✔
772
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
566✔
773
    }
774
  }
775

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

779
_error:
154,803,460✔
780
  // if failed to add ref for all tables in this query, abort current query
781
  return code;
366,451,328✔
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,
372,548,915✔
790
                     bool processOneBlock) {
791
  int32_t        code = TSDB_CODE_SUCCESS;
372,548,915✔
792
  int32_t        lino = 0;
372,548,915✔
793
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
372,548,915✔
794
  int64_t        threadId = taosGetSelfPthreadId();
372,548,915✔
795

796
  if (pLocal) {
372,526,824✔
797
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
369,261,244✔
798
  }
799

800
  taosArrayClear(pResList);
372,484,583✔
801

802
  int64_t curOwner = 0;
372,388,283✔
803
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
372,388,283✔
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) {
372,365,617✔
810
    pTaskInfo->cost.start = taosGetTimestampUs();
353,242,323✔
811
  }
812

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

819
  // error occurs, record the error code and return to client
820
  int32_t ret = setjmp(pTaskInfo->env);
372,371,677✔
821
  if (ret != TSDB_CODE_SUCCESS) {
380,697,899✔
822
    pTaskInfo->code = ret;
8,739,073✔
823
    (void)cleanUpUdfs();
8,739,512✔
824

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

828
    return pTaskInfo->code;
8,739,512✔
829
  }
830

831
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
371,958,826✔
832

833
  int32_t      current = 0;
371,959,188✔
834
  SSDataBlock* pRes = NULL;
371,959,188✔
835
  int64_t      st = taosGetTimestampUs();
372,605,228✔
836

837
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
372,605,228✔
838
    pTaskInfo->paramSet = true;
595,221✔
839
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
595,221✔
840
  } else {
841
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
372,023,187✔
842
  }
843

844
  QUERY_CHECK_CODE(code, lino, _end);
363,968,919✔
845
  code = blockDataCheck(pRes);
363,968,919✔
846
  QUERY_CHECK_CODE(code, lino, _end);
363,964,768✔
847

848
  if (pRes == NULL) {
363,964,768✔
849
    st = taosGetTimestampUs();
81,930,760✔
850
  }
851

852
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
363,962,979✔
853
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
364,042,765✔
854
    rowsThreshold = 4096;
362,410,399✔
855
  }
856

857
  int32_t blockIndex = 0;
364,014,048✔
858
  while (pRes != NULL) {
926,378,650✔
859
    SSDataBlock* p = NULL;
578,694,924✔
860
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
578,631,697✔
861
      SSDataBlock* p1 = NULL;
491,608,965✔
862
      code = createOneDataBlock(pRes, true, &p1);
491,615,068✔
863
      QUERY_CHECK_CODE(code, lino, _end);
491,588,379✔
864

865
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
491,588,379✔
866
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
491,646,824✔
867
      p = p1;
491,646,824✔
868
    } else {
869
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
87,095,258✔
870
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
87,095,370✔
871

872
      p = *(SSDataBlock**)tmp;
87,095,370✔
873
      code = copyDataBlock(p, pRes);
87,095,759✔
874
      QUERY_CHECK_CODE(code, lino, _end);
87,096,058✔
875
    }
876

877
    blockIndex += 1;
578,740,312✔
878

879
    current += p->info.rows;
578,740,312✔
880
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
578,735,514✔
881
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
882
    void* tmp = taosArrayPush(pResList, &p);
578,739,439✔
883
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
578,739,439✔
884

885
    if (current >= rowsThreshold || processOneBlock) {
578,739,439✔
886
      break;
887
    }
888

889
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
562,413,400✔
890
    QUERY_CHECK_CODE(code, lino, _end);
562,363,815✔
891
    code = blockDataCheck(pRes);
562,363,815✔
892
    QUERY_CHECK_CODE(code, lino, _end);
562,416,249✔
893
  }
894

895
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
364,009,765✔
896
    pTaskInfo->pSubplan->rowsThreshold -= current;
1,598,645✔
897
  }
898

899
  *hasMore = (pRes != NULL);
364,035,405✔
900
  uint64_t el = (taosGetTimestampUs() - st);
363,962,583✔
901

902
  pTaskInfo->cost.elapsedTime += el;
363,962,583✔
903
  if (NULL == pRes) {
363,919,253✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
347,593,214✔
905
  }
906

907
_end:
363,890,659✔
908
  (void)cleanUpUdfs();
363,880,591✔
909

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

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

920
  return pTaskInfo->code;
364,078,638✔
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) {
114,275,584✔
938
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
114,275,584✔
939
  int64_t        threadId = taosGetSelfPthreadId();
114,275,584✔
940
  int64_t        curOwner = 0;
114,276,219✔
941

942
  *pRes = NULL;
114,276,219✔
943

944
  // todo extract method
945
  taosRLockLatch(&pTaskInfo->lock);
114,276,219✔
946
  bool isKilled = isTaskKilled(pTaskInfo);
114,277,895✔
947
  if (isKilled) {
114,277,565✔
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) {
114,277,565✔
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;
114,276,882✔
963
  taosRUnLockLatch(&pTaskInfo->lock);
114,277,565✔
964

965
  if (pTaskInfo->cost.start == 0) {
114,277,895✔
966
    pTaskInfo->cost.start = taosGetTimestampUs();
217,862✔
967
  }
968

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

981
  int64_t st = taosGetTimestampUs();
114,276,905✔
982
  int32_t code = TSDB_CODE_SUCCESS;
114,276,905✔
983
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
114,276,905✔
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);
114,276,905✔
988
  }
989
  if (code) {
114,240,837✔
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);
114,240,837✔
995
  if (code) {
114,274,303✔
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);
114,254,560✔
1001

1002
  pTaskInfo->cost.elapsedTime += el;
114,254,560✔
1003
  if (NULL == *pRes) {
114,259,257✔
1004
    *useconds = pTaskInfo->cost.elapsedTime;
20,250,295✔
1005
  }
1006

1007
  (void)cleanUpUdfs();
114,262,477✔
1008

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

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

1015
  atomic_store_64(&pTaskInfo->owner, 0);
114,277,244✔
1016
  return pTaskInfo->code;
114,277,895✔
1017
}
1018

1019
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
112,227,791✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
112,227,791✔
1021
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
112,229,190✔
1022
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
112,227,808✔
1023

1024
  if (!tmp) {
112,226,432✔
1025
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1026
    return terrno;
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
112,226,432✔
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) {
358,800✔
1054
  if (pTaskInfo->subJobCtx.hasSubJobs) {
358,800✔
1055
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
329,784✔
1056
    if (pTaskInfo->subJobCtx.param) {
329,784✔
1057
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
153,177✔
1058
    }
1059
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
329,784✔
1060
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
329,784✔
1061
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
329,784✔
1062
  }
1063
  
1064
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
358,800✔
1065

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

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

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

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

1099
  setTaskKilled(pTaskInfo, rspCode);
358,800✔
1100
  qStopTaskOperators(pTaskInfo);
358,800✔
1101

1102
  return TSDB_CODE_SUCCESS;
358,800✔
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) {
358,356,977✔
1159
  STaskCostInfo* pSummary = &pTaskInfo->cost;
358,356,977✔
1160
  int64_t        idleTime = pSummary->start - pSummary->created;
358,401,439✔
1161

1162
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
358,401,068✔
1163
  if (pSummary->pRecoder != NULL) {
358,318,645✔
1164
    qDebug(
267,435,375✔
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,
90,903,188✔
1173
           pSummary->elapsedTime / 1000.0);
1174
  }
1175
}
358,339,118✔
1176

1177

1178
void qDestroyTask(qTaskInfo_t qTaskHandle) {
372,562,333✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
372,562,333✔
1180
  if (pTaskInfo == NULL) {
372,562,333✔
1181
    return;
14,208,491✔
1182
  }
1183

1184
  if (pTaskInfo->pRoot != NULL) {
358,353,842✔
1185
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
358,400,730✔
1186
  } else {
1187
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1188
  }
1189

1190
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
358,401,805✔
1191
  doDestroyTask(pTaskInfo);
358,352,821✔
1192
}
1193

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

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

1203
  while (1) {
303,229✔
1204
    uint16_t type = pOperator->operatorType;
612,207✔
1205
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
612,207✔
1206
      *scanner = pOperator->info;
308,978✔
1207
      break;
308,978✔
1208
    } else {
1209
      pOperator = pOperator->pDownstream[0];
303,229✔
1210
    }
1211
  }
1212
}
308,978✔
1213

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

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

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

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

1234
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
21,236,747✔
1235
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,236,747✔
1236
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
21,236,747✔
1237
  return 0;
21,236,747✔
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) {
730,386✔
1246
  memset(pCond, 0, sizeof(SQueryTableDataCond));
730,386✔
1247
  pCond->order = TSDB_ORDER_ASC;
730,386✔
1248
  pCond->numOfCols = pMtInfo->schema->nCols;
730,386✔
1249
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
730,948✔
1250
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
730,105✔
1251
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
731,229✔
1252
    taosMemoryFreeClear(pCond->colList);
1,405✔
1253
    taosMemoryFreeClear(pCond->pSlotList);
×
1254
    return terrno;
×
1255
  }
1256

1257
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
729,824✔
1258
  pCond->suid = pMtInfo->suid;
730,667✔
1259
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
730,386✔
1260
  pCond->startVersion = -1;
730,386✔
1261
  pCond->endVersion = sContext->snapVersion;
729,543✔
1262

1263
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,651,502✔
1264
    SColumnInfo* pColInfo = &pCond->colList[i];
3,919,149✔
1265
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,919,992✔
1266
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,920,835✔
1267
    if (pMtInfo->pExtSchemas != NULL) {
3,920,835✔
1268
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,280✔
1269
    }
1270
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,921,116✔
1271
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,920,835✔
1272

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

1276
  return TSDB_CODE_SUCCESS;
731,229✔
1277
}
1278

1279
void qStreamSetOpen(qTaskInfo_t tinfo) {
112,827,738✔
1280
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
112,827,738✔
1281
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
112,827,738✔
1282
  pOperator->status = OP_NOT_OPENED;
112,843,092✔
1283
}
112,843,567✔
1284

1285
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
20,452,838✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,452,838✔
1287
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
20,452,838✔
1288
}
20,452,521✔
1289

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

1295
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
21,401,369✔
1296
  const char*    id = GET_TASKID(pTaskInfo);
21,402,309✔
1297

1298
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
21,400,781✔
1299
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
20,167,731✔
1300
    if (pOperator == NULL || code != 0) {
20,165,378✔
UNCOV
1301
      return code;
×
1302
    }
1303

1304
    SStreamScanInfo* pInfo = pOperator->info;
20,168,228✔
1305
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
20,167,914✔
1306
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
20,165,746✔
1307
    walReaderVerifyOffset(pWalReader, pOffset);
20,167,262✔
1308
  }
1309
  // if pOffset equal to current offset, means continue consume
1310
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
21,401,041✔
1311
    return 0;
19,649,346✔
1312
  }
1313

1314
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,749,323✔
1315
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,002,364✔
1316
    if (pOperator == NULL || code != 0) {
1,001,765✔
1317
      return code;
×
1318
    }
1319

1320
    SStreamScanInfo* pInfo = pOperator->info;
1,002,626✔
1321
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,001,163✔
1322
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,001,799✔
1323
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,001,799✔
1324

1325
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,001,121✔
1326
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
801,440✔
1327
      pScanBaseInfo->dataReader = NULL;
801,440✔
1328

1329
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
801,440✔
1330
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
801,158✔
1331
      walReaderVerifyOffset(pWalReader, pOffset);
801,176✔
1332
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
801,440✔
1333
      if (code < 0) {
801,440✔
1334
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,243✔
1335
        return code;
11,243✔
1336
      }
1337
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
198,798✔
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;
201,203✔
1341
      int64_t ts = pOffset->ts;
201,183✔
1342
      int32_t index = 0;
200,280✔
1343

1344
      // this value may be changed if new tables are created
1345
      taosRLockLatch(&pTaskInfo->lock);
200,280✔
1346
      int32_t numOfTables = 0;
200,280✔
1347
      code = tableListGetSize(pTableListInfo, &numOfTables);
200,280✔
1348
      if (code != TSDB_CODE_SUCCESS) {
199,681✔
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) {
199,681✔
1355
        if (numOfTables != 0) {
194,496✔
1356
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
37,654✔
1357
          if (!tmp) {
37,654✔
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;
37,654✔
1363
          ts = INT64_MIN;
37,654✔
1364
          pScanInfo->currentTable = 0;
37,654✔
1365
        } else {
1366
          taosRUnLockLatch(&pTaskInfo->lock);
156,842✔
1367
          qError("no table in table list, %s", id);
158,043✔
1368
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
158,043✔
1369
        }
1370
      }
1371
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
42,839✔
1372

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

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

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

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

1429
      // restore the key value
1430
      pScanBaseInfo->cond.twindows.skey = oldSkey;
43,441✔
1431
    } else {
UNCOV
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) {
746,959✔
1438
      SStreamRawScanInfo* pInfo = pOperator->info;
732,057✔
1439
      SSnapContext*       sContext = pInfo->sContext;
732,057✔
1440
      SOperatorInfo*      p = NULL;
732,057✔
1441

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

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

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

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

1463
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
731,214✔
1464
      tableListClear(pTableListInfo);
731,776✔
1465

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

1471
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
731,229✔
1472
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
730,667✔
1473
      if (code != TSDB_CODE_SUCCESS) {
730,105✔
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)) {
730,105✔
1479
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,251✔
1480
      } else {
1481
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
729,416✔
1482
      }
1483

1484
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
730,667✔
1485
      if (code != TSDB_CODE_SUCCESS) {
731,229✔
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);
731,229✔
1492
      if (!pList) {
731,229✔
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;
731,229✔
1498
      code = tableListGetSize(pTableListInfo, &size);
731,229✔
1499
      if (code != TSDB_CODE_SUCCESS) {
731,229✔
1500
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1501
        destroyMetaTableInfo(&mtInfo);
1502
        return code;
×
1503
      }
1504

1505
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,462,458✔
1506
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
731,229✔
1507
      if (code != TSDB_CODE_SUCCESS) {
729,824✔
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);
729,824✔
1514
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
730,658✔
1515
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1516
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
729,824✔
1517
      pTaskInfo->streamInfo.schema = mtInfo.schema;
730,087✔
1518
      taosMemoryFreeClear(mtInfo.pExtSchemas);
730,334✔
1519

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

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

1544
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
254,133,346✔
1545
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
254,133,346✔
1546
  if (pMsg->info.ahandle == NULL) {
254,214,172✔
UNCOV
1547
    rpcFreeCont(pMsg->pCont);
×
UNCOV
1548
    qError("pMsg->info.ahandle is NULL");
×
UNCOV
1549
    return;
×
1550
  }
1551

1552
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
254,084,462✔
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};
254,091,529✔
1556

1557
  if (pMsg->contLen > 0) {
254,184,080✔
1558
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
254,113,601✔
1559
    if (buf.pData == NULL) {
253,923,615✔
1560
      pMsg->code = terrno;
×
1561
    } else {
1562
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
253,923,615✔
1563
    }
1564
  }
1565

1566
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
254,114,179✔
1567
  rpcFreeCont(pMsg->pCont);
254,260,584✔
1568
  destroySendMsgInfo(pSendInfo);
254,210,812✔
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,308,474✔
1610
  int32_t        code = TSDB_CODE_SUCCESS;
3,308,474✔
1611
  int32_t        lino = 0;
3,308,474✔
1612
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,308,474✔
1613

1614
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,309,014✔
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,306,780✔
1621
    STableScanInfo* pScanInfo = pOperator->info;
1,653,820✔
1622

1623
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,653,820✔
1624
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,655,096✔
1625
  } else {
1626
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,654,556✔
1627
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,653,918✔
1628
    }
1629
  }
1630

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

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

1643
  *pList = NULL;
1,654,556✔
1644
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,654,556✔
1645
  if (pArray == NULL) {
1,654,016✔
1646
    return terrno;
×
1647
  }
1648

1649
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,654,016✔
1650
  if (code == 0) {
1,653,488✔
1651
    *pList = pArray;
1,653,488✔
1652
  } else {
1653
    taosArrayDestroy(pArray);
×
1654
  }
1655
  return code;
1,653,488✔
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) {
29,377,849✔
1694
  int32_t code = 0;
29,377,849✔
1695

1696
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
29,377,849✔
1697
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
29,374,323✔
1698

1699
  if (pOper->fpSet.resetStateFn) {
29,375,816✔
1700
    code = pOper->fpSet.resetStateFn(pOper);
29,377,802✔
1701
  }
1702
  pOper->status = OP_NOT_OPENED;
29,373,167✔
1703
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
50,735,755✔
1704
    code = clearStatesForOperator(pOper->pDownstream[i]);
21,352,680✔
1705
  }
1706
  return code;
29,382,155✔
1707
}
1708

1709
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,025,120✔
1710
  int32_t        code = 0;
8,025,120✔
1711
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,025,120✔
1712
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,025,120✔
1713
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,025,120✔
1714
  code = clearStatesForOperator(pOper);
8,025,120✔
1715
  return code;
8,024,922✔
1716
}
1717

1718
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,691,310✔
1719
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,691,310✔
1720
  int64_t        threadId = taosGetSelfPthreadId();
10,691,310✔
1721
  int64_t        curOwner = 0;
10,691,104✔
1722

1723
  *ppRes = NULL;
10,691,104✔
1724

1725
  // todo extract method
1726
  taosRLockLatch(&pTaskInfo->lock);
10,691,104✔
1727
  bool isKilled = isTaskKilled(pTaskInfo);
10,691,709✔
1728
  if (isKilled) {
10,691,267✔
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) {
10,691,267✔
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;
10,691,473✔
1745
  taosRUnLockLatch(&pTaskInfo->lock);
10,691,473✔
1746

1747
  if (pTaskInfo->cost.start == 0) {
10,691,473✔
1748
    pTaskInfo->cost.start = taosGetTimestampUs();
211,141✔
1749
  }
1750

1751
  // error occurs, record the error code and return to client
1752
  int32_t ret = setjmp(pTaskInfo->env);
10,691,473✔
1753
  if (ret != TSDB_CODE_SUCCESS) {
13,001,017✔
1754
    pTaskInfo->code = ret;
2,311,161✔
1755
    (void)cleanUpUdfs();
2,311,161✔
1756
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,311,161✔
1757
    atomic_store_64(&pTaskInfo->owner, 0);
2,311,161✔
1758
    return pTaskInfo->code;
2,311,161✔
1759
  }
1760

1761
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,689,856✔
1762

1763
  int64_t st = taosGetTimestampUs();
10,691,300✔
1764

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

1778
  uint64_t el = (taosGetTimestampUs() - st);
8,380,347✔
1779

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

1785
  (void)cleanUpUdfs();
8,380,312✔
1786

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

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

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

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

1829
end:
224,078✔
1830
  return 0;
224,078✔
1831
}
1832

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2217
  if (pSrc->pFields && pSrc->pFields->size > 0) {
206,548✔
2218
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
206,705✔
2219
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
206,316✔
2220
  } else {
2221
    (*ppDst)->pFields = NULL;
×
2222
  }
2223
  
2224
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
206,705✔
2225
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
124,525✔
2226
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
124,525✔
2227
  } else {
2228
    (*ppDst)->pTagFields = NULL;
81,791✔
2229
  }
2230

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

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

2245
_exit:
206,705✔
2246

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

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

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

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

2279
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,272✔
2280
  pList->idInfo.suid = suid;
4,272✔
2281
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,272✔
2282
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,272✔
2283
  if (code != TSDB_CODE_SUCCESS) {
4,272✔
2284
    goto end;
×
2285
  }                                              
2286
  // taosArrayClear(uidList);
2287
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2288
  //   void* tmp = taosArrayGet(uidListCopy, i);
2289
  //   if (tmp == NULL) {
2290
  //     continue;
2291
  //   }
2292
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2293
  //   if (slot == NULL) {
2294
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2295
  //       code = terrno;
2296
  //       goto end;
2297
  //     }
2298
  //   }
2299
  // }
2300
end:
4,272✔
2301
  // taosArrayDestroy(uidListCopy);
2302
  tableListDestroy(pList);
4,272✔
2303
  return code;
4,272✔
2304
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc