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

taosdata / TDengine / #4918

08 Jan 2026 11:50AM UTC coverage: 65.916% (+0.5%) from 65.42%
#4918

push

travis-ci

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

16 of 22 new or added lines in 3 files covered. (72.73%)

788 existing lines in 116 files now uncovered.

204221 of 309822 relevant lines covered (65.92%)

126504701.11 hits per line

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

68.93
/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) {
854,777,111✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
854,777,111✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
854,777,111✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
855,007,449✔
47
}
854,857,492✔
48

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

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

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

70
static void initRefPool() {
528,268✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
528,268✔
72
  (void)atexit(cleanupRefPool);
528,268✔
73
}
528,268✔
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) {
47,076,208✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
47,076,208✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
47,076,842✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
21,637,917✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
21,637,917✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
21,637,680✔
162
      if (pScanInfo->base.dataReader != NULL) {
21,637,680✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
256,070✔
164
        if (code) {
256,070✔
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);
25,439,255✔
172
  }
173

174
  return 0;
24,991,269✔
175
}
176

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

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

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

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

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

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

248
  return pTaskInfo;
313,632✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
459,710✔
252
  if (streamInserterParam == NULL) {
459,710✔
253
    return TSDB_CODE_SUCCESS;
265,924✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
193,786✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
193,786✔
323
    if (code) {
193,786✔
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,
459,710✔
328
         tstrerror(code));
329

330
_error:
71,585✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,894,473✔
342
  if (pInfo == NULL) {
2,894,473✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,894,473✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,894,473✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,894,473✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,894,473✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,696,406✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
433,088✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,590,879✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

362
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,461,385✔
363
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,461,385✔
364
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,461,147✔
365
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,461,147✔
366
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,460,910✔
367
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,460,910✔
368
}
369

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,894,473✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
198,067✔
382
    setReadHandle(handle, pScanBaseInfo);
198,067✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,696,406✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,263,318✔
385
    setReadHandle(handle, pScanBaseInfo);
2,263,318✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
459,710✔
400

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

416
  return code;
459,710✔
417
}
418

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
356,339✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
356,339✔
444

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

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

454
  locked = 1;
356,339✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
719,725✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
363,386✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
363,386✔
458

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

465
    tDecoderClear(&mr.coder);
363,386✔
466

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

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
356,339✔
524

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

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

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

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

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

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

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

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

586
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
356,339✔
587
    taosWLockLatch(&pTaskInfo->lock);
356,339✔
588

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

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

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

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

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

636
  return code;
358,293✔
637
}
638

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

644
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
724,185,417✔
645
    return TSDB_CODE_INVALID_PARA;
54,038✔
646
  }
647
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
724,131,471✔
648

649
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
724,131,471✔
650
    return TSDB_CODE_SUCCESS;
409,835,102✔
651
  }
652

653
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
314,358,601✔
654
  if (!pSchemaInfo) {
314,485,583✔
655
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
656
    return terrno;
×
657
  }
658

659
  *sversion = pSchemaInfo->sw->version;
314,485,583✔
660
  *tversion = pSchemaInfo->tversion;
314,533,718✔
661
  *rversion = pSchemaInfo->rversion;
314,681,380✔
662
  if (pSchemaInfo->dbname) {
314,263,341✔
663
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
314,271,467✔
664
  } else {
665
    dbName[0] = 0;
×
666
  }
667
  if (pSchemaInfo->tablename) {
314,629,809✔
668
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
314,478,065✔
669
  } else {
670
    tableName[0] = 0;
24✔
671
  }
672

673
  *tbGet = true;
314,583,137✔
674

675
  return TSDB_CODE_SUCCESS;
314,327,802✔
676
}
677

678
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
409,682,929✔
679

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

687
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
9,644,456✔
688
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
9,644,456✔
689
  ((SExecTaskInfo*)tinfo)->paramSet = false;
9,645,944✔
690
}
9,645,944✔
691

692
int32_t qExecutorInit(void) {
4,123,773✔
693
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,123,773✔
694
  return TSDB_CODE_SUCCESS;
4,124,477✔
695
}
696

697
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
83,908,898✔
698
  int32_t        code = TSDB_CODE_SUCCESS;
83,908,898✔
699
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
83,908,898✔
700
  if (pTask->pWorkerCb) {
83,908,898✔
701
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
83,921,965✔
702
    if (code != TSDB_CODE_SUCCESS) {
83,941,032✔
703
      pTask->code = code;
×
704
      return pTask->code;
×
705
    }
706
  }
707

708
  code = tsem_wait(pSem);
83,957,016✔
709
  if (code != TSDB_CODE_SUCCESS) {
83,939,861✔
710
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
711
    pTask->code = code;
×
712
    return pTask->code;
×
713
  }
714

715
  if (pTask->pWorkerCb) {
83,939,861✔
716
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
83,943,734✔
717
    if (code != TSDB_CODE_SUCCESS) {
83,945,959✔
718
      pTask->code = code;
×
719
      return pTask->code;
×
720
    }
721
  }
722
  return TSDB_CODE_SUCCESS;
83,945,069✔
723
}
724

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

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

734
  readHandle->uid = 0;
424,375,155✔
735
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
424,417,707✔
736
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
424,120,908✔
737
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
12,848,120✔
738
    goto _error;
12,631,067✔
739
  }
740
    
741
  if (handle) {
411,367,861✔
742
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
411,148,501✔
743
    void*           pSinkManager = NULL;
411,231,596✔
744
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
410,599,187✔
745
    if (code != TSDB_CODE_SUCCESS) {
411,124,693✔
746
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
747
      goto _error;
×
748
    }
749

750
    void* pSinkParam = NULL;
411,124,693✔
751
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
411,109,040✔
752
    if (code != TSDB_CODE_SUCCESS) {
410,834,433✔
753
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
754
      taosMemoryFree(pSinkManager);
×
755
      goto _error;
×
756
    }
757

758
    SDataSinkNode* pSink = NULL;
410,834,433✔
759
    if (readHandle->localExec) {
411,130,492✔
760
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,904✔
761
      if (code != TSDB_CODE_SUCCESS) {
2,904✔
762
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
763
               (*pTask)->id.str);
764
        taosMemoryFree(pSinkManager);
×
765
        goto _error;
×
766
      }
767
    }
768

769
    // pSinkParam has been freed during create sinker.
770
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
411,138,609✔
771
                              (*pTask)->id.str, pSubplan->processOneBlock);
411,344,292✔
772
    if (code) {
410,961,546✔
773
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
575✔
774
    }
775
  }
776

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

780
_error:
210,178,731✔
781
  // if failed to add ref for all tables in this query, abort current query
782
  return code;
424,269,677✔
783
}
784

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

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

797
  if (pLocal) {
433,194,339✔
798
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
430,098,348✔
799
  }
800

801
  taosArrayClear(pResList);
432,817,053✔
802

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

810
  if (pTaskInfo->cost.start == 0) {
432,812,624✔
811
    pTaskInfo->cost.start = taosGetTimestampUs();
406,877,506✔
812
  }
813

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

820
  // error occurs, record the error code and return to client
821
  int32_t ret = setjmp(pTaskInfo->env);
433,106,206✔
822
  if (ret != TSDB_CODE_SUCCESS) {
444,507,358✔
823
    pTaskInfo->code = ret;
12,067,319✔
824
    (void)cleanUpUdfs();
12,067,319✔
825

826
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
12,067,319✔
827
    atomic_store_64(&pTaskInfo->owner, 0);
12,067,319✔
828

829
    return pTaskInfo->code;
12,067,319✔
830
  }
831

832
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
432,440,039✔
833

834
  int32_t      current = 0;
432,441,528✔
835
  SSDataBlock* pRes = NULL;
432,441,528✔
836
  int64_t      st = taosGetTimestampUs();
433,398,896✔
837

838
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
433,398,896✔
839
    pTaskInfo->paramSet = true;
9,644,952✔
840
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
9,645,944✔
841
  } else {
842
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
423,669,264✔
843
  }
844

845
  QUERY_CHECK_CODE(code, lino, _end);
421,334,828✔
846
  code = blockDataCheck(pRes);
421,334,828✔
847
  QUERY_CHECK_CODE(code, lino, _end);
421,411,073✔
848

849
  if (pRes == NULL) {
421,411,073✔
850
    st = taosGetTimestampUs();
88,292,342✔
851
  }
852

853
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
421,411,428✔
854
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
421,349,015✔
855
    rowsThreshold = 4096;
420,746,867✔
856
  }
857

858
  int32_t blockIndex = 0;
421,295,900✔
859
  while (pRes != NULL) {
1,055,454,053✔
860
    SSDataBlock* p = NULL;
655,312,624✔
861
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
655,164,082✔
862
      SSDataBlock* p1 = NULL;
560,393,341✔
863
      code = createOneDataBlock(pRes, true, &p1);
560,414,391✔
864
      QUERY_CHECK_CODE(code, lino, _end);
560,478,346✔
865

866
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
560,478,346✔
867
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
560,560,834✔
868
      p = p1;
560,560,834✔
869
    } else {
870
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
94,832,131✔
871
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
94,831,673✔
872

873
      p = *(SSDataBlock**)tmp;
94,831,673✔
874
      code = copyDataBlock(p, pRes);
94,831,673✔
875
      QUERY_CHECK_CODE(code, lino, _end);
94,831,703✔
876
    }
877

878
    blockIndex += 1;
655,386,520✔
879

880
    current += p->info.rows;
655,386,520✔
881
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
655,392,305✔
882
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
883
    void* tmp = taosArrayPush(pResList, &p);
655,389,179✔
884
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
655,389,179✔
885

886
    if (current >= rowsThreshold || processOneBlock) {
655,389,179✔
887
      break;
888
    }
889

890
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
634,101,651✔
891
    QUERY_CHECK_CODE(code, lino, _end);
634,070,067✔
892
    code = blockDataCheck(pRes);
634,070,067✔
893
    QUERY_CHECK_CODE(code, lino, _end);
634,094,528✔
894
  }
895

896
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
421,428,957✔
897
    pTaskInfo->pSubplan->rowsThreshold -= current;
503,483✔
898
  }
899

900
  *hasMore = (pRes != NULL);
421,437,071✔
901
  uint64_t el = (taosGetTimestampUs() - st);
421,292,958✔
902

903
  pTaskInfo->cost.elapsedTime += el;
421,292,958✔
904
  if (NULL == pRes) {
421,344,347✔
905
    *useconds = pTaskInfo->cost.elapsedTime;
400,056,819✔
906
  }
907

908
_end:
421,073,978✔
909
  (void)cleanUpUdfs();
421,285,654✔
910

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

915
  atomic_store_64(&pTaskInfo->owner, 0);
421,507,180✔
916
  if (code) {
421,506,648✔
917
    pTaskInfo->code = code;
×
918
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
919
  }
920

921
  return pTaskInfo->code;
421,506,648✔
922
}
923

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

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

938
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
115,999,184✔
939
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
115,999,184✔
940
  int64_t        threadId = taosGetSelfPthreadId();
115,999,184✔
941
  int64_t        curOwner = 0;
115,999,518✔
942

943
  *pRes = NULL;
115,999,518✔
944

945
  // todo extract method
946
  taosRLockLatch(&pTaskInfo->lock);
115,999,518✔
947
  bool isKilled = isTaskKilled(pTaskInfo);
115,999,804✔
948
  if (isKilled) {
115,999,504✔
949
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
950

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

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

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

963
  pTaskInfo->owner = threadId;
115,999,781✔
964
  taosRUnLockLatch(&pTaskInfo->lock);
116,000,113✔
965

966
  if (pTaskInfo->cost.start == 0) {
116,000,166✔
967
    pTaskInfo->cost.start = taosGetTimestampUs();
219,583✔
968
  }
969

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

980
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
115,998,478✔
981

982
  int64_t st = taosGetTimestampUs();
115,997,789✔
983
  int32_t code = TSDB_CODE_SUCCESS;
115,997,789✔
984
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
115,997,789✔
985
    pTaskInfo->paramSet = true;
×
986
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
987
  } else {
988
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
115,998,121✔
989
  }
990
  if (code) {
115,940,745✔
991
    pTaskInfo->code = code;
×
992
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
993
  }
994

995
  code = blockDataCheck(*pRes);
115,940,745✔
996
  if (code) {
115,995,547✔
997
    pTaskInfo->code = code;
×
998
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
999
  }
1000

1001
  uint64_t el = (taosGetTimestampUs() - st);
115,964,264✔
1002

1003
  pTaskInfo->cost.elapsedTime += el;
115,964,264✔
1004
  if (NULL == *pRes) {
115,989,520✔
1005
    *useconds = pTaskInfo->cost.elapsedTime;
21,278,171✔
1006
  }
1007

1008
  (void)cleanUpUdfs();
115,991,086✔
1009

1010
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
115,997,457✔
1011
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
116,000,445✔
1012

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

1016
  atomic_store_64(&pTaskInfo->owner, 0);
116,000,113✔
1017
  return pTaskInfo->code;
115,999,783✔
1018
}
1019

1020
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
119,321,129✔
1021
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
119,321,129✔
1022
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
119,321,266✔
1023
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
119,322,601✔
1024

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

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

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

1042
  return 0;
×
1043
}
1044

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

1054
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
377,558✔
1055
  if (pTaskInfo->subJobCtx.hasSubJobs) {
377,558✔
1056
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
312,580✔
1057
    if (pTaskInfo->subJobCtx.param) {
312,580✔
1058
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
141,755✔
1059
    }
1060
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
312,580✔
1061
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
312,580✔
1062
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
312,580✔
1063
  }
1064
  
1065
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
377,558✔
1066

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

1089
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
377,558✔
1090
}
377,558✔
1091

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

1098
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
377,558✔
1099

1100
  setTaskKilled(pTaskInfo, rspCode);
377,558✔
1101
  qStopTaskOperators(pTaskInfo);
377,558✔
1102

1103
  return TSDB_CODE_SUCCESS;
377,558✔
1104
}
1105

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

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

1119
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1120

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

1127
        taosMsleep(200);
×
1128

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

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

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

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

1159
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
412,444,869✔
1160
  STaskCostInfo* pSummary = &pTaskInfo->cost;
412,444,869✔
1161
  int64_t        idleTime = pSummary->start - pSummary->created;
412,503,332✔
1162

1163
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
412,311,952✔
1164
  if (pSummary->pRecoder != NULL) {
412,353,326✔
1165
    qDebug(
306,863,618✔
1166
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1167
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1168
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1169
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1170
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1171
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1172
  } else {
1173
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
105,581,827✔
1174
           pSummary->elapsedTime / 1000.0);
1175
  }
1176
}
412,444,892✔
1177

1178

1179
void qDestroyTask(qTaskInfo_t qTaskHandle) {
430,076,583✔
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
430,076,583✔
1181
  if (pTaskInfo == NULL) {
430,076,583✔
1182
    return;
17,635,216✔
1183
  }
1184

1185
  if (pTaskInfo->pRoot != NULL) {
412,441,367✔
1186
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
412,508,556✔
1187
  } else {
1188
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1189
  }
1190

1191
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
412,508,560✔
1192
  doDestroyTask(pTaskInfo);
412,423,475✔
1193
}
1194

1195
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
38,546,933✔
1196
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,546,933✔
1197
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
38,546,933✔
1198
}
1199

1200
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
313,367✔
1201
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
313,367✔
1202
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
313,367✔
1203

1204
  while (1) {
307,801✔
1205
    uint16_t type = pOperator->operatorType;
621,433✔
1206
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
621,433✔
1207
      *scanner = pOperator->info;
313,632✔
1208
      break;
313,632✔
1209
    } else {
1210
      pOperator = pOperator->pDownstream[0];
307,801✔
1211
    }
1212
  }
1213
}
313,632✔
1214

1215
void* qExtractReaderFromTmqScanner(void* scanner) {
313,632✔
1216
  SStreamScanInfo* pInfo = scanner;
313,632✔
1217
  return (void*)pInfo->tqReader;
313,632✔
1218
}
1219

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

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

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

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

1246
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
739,832✔
1247
  memset(pCond, 0, sizeof(SQueryTableDataCond));
739,832✔
1248
  pCond->order = TSDB_ORDER_ASC;
739,832✔
1249
  pCond->numOfCols = pMtInfo->schema->nCols;
739,547✔
1250
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
739,832✔
1251
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
740,402✔
1252
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
740,117✔
1253
    taosMemoryFreeClear(pCond->colList);
292✔
1254
    taosMemoryFreeClear(pCond->pSlotList);
×
1255
    return terrno;
×
1256
  }
1257

1258
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
739,825✔
1259
  pCond->suid = pMtInfo->suid;
739,832✔
1260
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
739,540✔
1261
  pCond->startVersion = -1;
740,117✔
1262
  pCond->endVersion = sContext->snapVersion;
740,110✔
1263

1264
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,709,389✔
1265
    SColumnInfo* pColInfo = &pCond->colList[i];
3,969,272✔
1266
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,968,410✔
1267
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,969,272✔
1268
    if (pMtInfo->pExtSchemas != NULL) {
3,969,557✔
1269
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,680✔
1270
    }
1271
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,969,265✔
1272
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,968,987✔
1273

1274
    pCond->pSlotList[i] = i;
3,969,272✔
1275
  }
1276

1277
  return TSDB_CODE_SUCCESS;
740,117✔
1278
}
1279

1280
void qStreamSetOpen(qTaskInfo_t tinfo) {
114,533,400✔
1281
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
114,533,400✔
1282
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
114,533,400✔
1283
  pOperator->status = OP_NOT_OPENED;
114,543,773✔
1284
}
114,544,126✔
1285

1286
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
21,476,726✔
1287
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,476,726✔
1288
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
21,476,726✔
1289
}
21,476,982✔
1290

1291
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
22,426,191✔
1292
  int32_t        code = TSDB_CODE_SUCCESS;
22,426,191✔
1293
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,426,191✔
1294
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
22,426,191✔
1295

1296
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,425,265✔
1297
  const char*    id = GET_TASKID(pTaskInfo);
22,425,310✔
1298

1299
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,424,914✔
1300
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
21,189,243✔
1301
    if (pOperator == NULL || code != 0) {
21,188,195✔
UNCOV
1302
      return code;
×
1303
    }
1304

1305
    SStreamScanInfo* pInfo = pOperator->info;
21,188,195✔
1306
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
21,188,861✔
1307
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
21,188,861✔
1308
    walReaderVerifyOffset(pWalReader, pOffset);
21,188,294✔
1309
  }
1310
  // if pOffset equal to current offset, means continue consume
1311
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,424,498✔
1312
    return 0;
20,617,700✔
1313
  }
1314

1315
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,806,423✔
1316
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,050,153✔
1317
    if (pOperator == NULL || code != 0) {
1,050,112✔
1318
      return code;
×
1319
    }
1320

1321
    SStreamScanInfo* pInfo = pOperator->info;
1,050,112✔
1322
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,050,112✔
1323
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,049,479✔
1324
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,049,290✔
1325

1326
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,050,416✔
1327
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
856,691✔
1328
      pScanBaseInfo->dataReader = NULL;
856,954✔
1329

1330
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
856,954✔
1331
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
856,954✔
1332
      walReaderVerifyOffset(pWalReader, pOffset);
856,378✔
1333
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
856,954✔
1334
      if (code < 0) {
856,954✔
1335
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
9,922✔
1336
        return code;
9,922✔
1337
      }
1338
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
192,876✔
1339
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1340
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1341
      int64_t uid = pOffset->uid;
193,158✔
1342
      int64_t ts = pOffset->ts;
193,462✔
1343
      int32_t index = 0;
193,175✔
1344

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

1355
      if (uid == 0) {
193,165✔
1356
        if (numOfTables != 0) {
187,880✔
1357
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,228✔
1358
          if (!tmp) {
37,929✔
1359
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1360
            taosRUnLockLatch(&pTaskInfo->lock);
×
1361
            return terrno;
×
1362
          }
1363
          if (tmp) uid = tmp->uid;
37,929✔
1364
          ts = INT64_MIN;
37,929✔
1365
          pScanInfo->currentTable = 0;
37,929✔
1366
        } else {
1367
          taosRUnLockLatch(&pTaskInfo->lock);
149,652✔
1368
          qError("no table in table list, %s", id);
149,949✔
1369
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
149,949✔
1370
        }
1371
      }
1372
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
43,513✔
1373

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

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

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

1392
      STableKeyInfo keyInfo = {.uid = uid};
43,513✔
1393
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
43,513✔
1394

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

1403
      if (pScanBaseInfo->dataReader == NULL) {
43,513✔
1404
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
74,488✔
1405
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1406
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
37,244✔
1407
        if (code != TSDB_CODE_SUCCESS) {
37,244✔
1408
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1409
          return code;
×
1410
        }
1411

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

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

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

1437
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1438
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
756,270✔
1439
      SStreamRawScanInfo* pInfo = pOperator->info;
741,236✔
1440
      SSnapContext*       sContext = pInfo->sContext;
741,236✔
1441
      SOperatorInfo*      p = NULL;
741,236✔
1442

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

1448
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
741,236✔
1449

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

1455
      SMetaTableInfo mtInfo = {0};
741,236✔
1456
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
741,236✔
1457
      if (code != 0) {
740,096✔
1458
        destroyMetaTableInfo(&mtInfo);
1459
        return code;
×
1460
      }
1461
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
740,096✔
1462
      pInfo->dataReader = NULL;
738,949✔
1463

1464
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
739,526✔
1465
      tableListClear(pTableListInfo);
739,234✔
1466

1467
      if (mtInfo.uid == 0) {
741,236✔
1468
        destroyMetaTableInfo(&mtInfo);
1469
        goto end;  // no data
834✔
1470
      }
1471

1472
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
740,402✔
1473
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
739,547✔
1474
      if (code != TSDB_CODE_SUCCESS) {
739,832✔
1475
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1476
        destroyMetaTableInfo(&mtInfo);
1477
        return code;
×
1478
      }
1479
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
739,832✔
1480
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,272✔
1481
      } else {
1482
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
738,560✔
1483
      }
1484

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

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

1506
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,480,804✔
1507
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
740,402✔
1508
      if (code != TSDB_CODE_SUCCESS) {
739,547✔
1509
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1510
        destroyMetaTableInfo(&mtInfo);
1511
        return code;
×
1512
      }
1513

1514
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
739,547✔
1515
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
740,117✔
1516
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1517
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
739,547✔
1518
      pTaskInfo->streamInfo.schema = mtInfo.schema;
740,117✔
1519
      taosMemoryFreeClear(mtInfo.pExtSchemas);
740,402✔
1520

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

1540
end:
1,646,709✔
1541
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,646,815✔
1542
  return 0;
1,646,815✔
1543
}
1544

1545
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
303,078,291✔
1546
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
303,078,291✔
1547
  if (pMsg->info.ahandle == NULL) {
303,152,329✔
1548
    rpcFreeCont(pMsg->pCont);
×
1549
    qError("pMsg->info.ahandle is NULL");
×
1550
    return;
×
1551
  }
1552

1553
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
302,992,690✔
1554
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1555

1556
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
302,995,366✔
1557

1558
  if (pMsg->contLen > 0) {
303,103,500✔
1559
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
303,078,108✔
1560
    if (buf.pData == NULL) {
302,838,743✔
1561
      pMsg->code = terrno;
×
1562
    } else {
1563
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
302,838,743✔
1564
    }
1565
  }
1566

1567
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
302,961,012✔
1568
  rpcFreeCont(pMsg->pCont);
303,208,533✔
1569
  destroySendMsgInfo(pSendInfo);
303,130,935✔
1570
}
1571

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

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

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

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

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

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

1600
  taosArrayDestroy(plist);
×
1601

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

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

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

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

1624
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,668,311✔
1625
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,667,770✔
1626
  } else {
1627
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,666,695✔
1628
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,666,695✔
1629
    }
1630
  }
1631

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

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

1644
  *pList = NULL;
1,666,695✔
1645
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,667,236✔
1646
  if (pArray == NULL) {
1,667,777✔
1647
    return terrno;
×
1648
  }
1649

1650
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,667,777✔
1651
  if (code == 0) {
1,667,229✔
1652
    *pList = pArray;
1,667,229✔
1653
  } else {
1654
    taosArrayDestroy(pArray);
×
1655
  }
1656
  return code;
1,667,229✔
1657
}
1658

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

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

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

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

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

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

1694
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
25,537,687✔
1695
  int32_t code = 0;
25,537,687✔
1696

1697
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
25,537,687✔
1698
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
25,537,684✔
1699

1700
  if (pOper->fpSet.resetStateFn) {
25,537,284✔
1701
    code = pOper->fpSet.resetStateFn(pOper);
25,537,116✔
1702
  }
1703
  pOper->status = OP_NOT_OPENED;
25,535,323✔
1704
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
43,788,204✔
1705
    code = clearStatesForOperator(pOper->pDownstream[i]);
18,247,849✔
1706
  }
1707
  return code;
25,540,955✔
1708
}
1709

1710
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,291,109✔
1711
  int32_t        code = 0;
7,291,109✔
1712
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,291,109✔
1713
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,291,109✔
1714
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,291,109✔
1715
  code = clearStatesForOperator(pOper);
7,291,109✔
1716
  return code;
7,290,906✔
1717
}
1718

1719
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
9,814,740✔
1720
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
9,814,740✔
1721
  int64_t        threadId = taosGetSelfPthreadId();
9,814,740✔
1722
  int64_t        curOwner = 0;
9,814,506✔
1723

1724
  *ppRes = NULL;
9,814,506✔
1725

1726
  // todo extract method
1727
  taosRLockLatch(&pTaskInfo->lock);
9,814,740✔
1728
  bool isKilled = isTaskKilled(pTaskInfo);
9,815,148✔
1729
  if (isKilled) {
9,814,709✔
1730
    // clearStreamBlock(pTaskInfo->pRoot);
1731
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1732

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

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

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

1745
  pTaskInfo->owner = threadId;
9,814,306✔
1746
  taosRUnLockLatch(&pTaskInfo->lock);
9,814,745✔
1747

1748
  if (pTaskInfo->cost.start == 0) {
9,815,148✔
1749
    pTaskInfo->cost.start = taosGetTimestampUs();
198,286✔
1750
  }
1751

1752
  // error occurs, record the error code and return to client
1753
  int32_t ret = setjmp(pTaskInfo->env);
9,815,148✔
1754
  if (ret != TSDB_CODE_SUCCESS) {
11,583,759✔
1755
    pTaskInfo->code = ret;
1,769,253✔
1756
    (void)cleanUpUdfs();
1,769,253✔
1757
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,769,253✔
1758
    atomic_store_64(&pTaskInfo->owner, 0);
1,769,253✔
1759
    return pTaskInfo->code;
1,769,253✔
1760
  }
1761

1762
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,814,506✔
1763

1764
  int64_t st = taosGetTimestampUs();
9,814,740✔
1765

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

1779
  uint64_t el = (taosGetTimestampUs() - st);
8,045,895✔
1780

1781
  pTaskInfo->cost.elapsedTime += el;
8,045,895✔
1782
  if (NULL == *ppRes) {
8,045,895✔
1783
    *useconds = pTaskInfo->cost.elapsedTime;
5,083,350✔
1784
  }
1785

1786
  (void)cleanUpUdfs();
8,045,895✔
1787

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

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

1794
  atomic_store_64(&pTaskInfo->owner, 0);
8,045,895✔
1795
  return pTaskInfo->code;
8,045,895✔
1796
}
1797

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

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

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

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

1830
end:
213,438✔
1831
  return 0;
213,438✔
1832
}
1833

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

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

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

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

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

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

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

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

1932
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,308,581✔
1933

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

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

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

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

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

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

2017
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
38,716✔
2018
    int32_t idx = 0;
38,716✔
2019
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
154,675✔
2020
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
115,959✔
2021
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
115,959✔
2022
      colInfo.info.precision = pCol->type.precision;
115,959✔
2023
      colInfo.info.scale = pCol->type.scale;
115,959✔
2024
      code = blockDataAppendColInfo(*pRes, &colInfo);
115,959✔
2025
      if (code != 0) break;
115,959✔
2026
    }
2027
  }
2028

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

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

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

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

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

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

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

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

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

2213
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
193,786✔
2214
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
193,786✔
2215

2216
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
193,786✔
2217

2218
  if (pSrc->pFields && pSrc->pFields->size > 0) {
193,786✔
2219
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
193,786✔
2220
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
193,786✔
2221
  } else {
2222
    (*ppDst)->pFields = NULL;
×
2223
  }
2224
  
2225
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
193,786✔
2226
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
120,200✔
2227
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
120,200✔
2228
  } else {
2229
    (*ppDst)->pTagFields = NULL;
73,586✔
2230
  }
2231

2232
  if (pSrc->colCids && pSrc->colCids->size > 0) {
193,786✔
2233
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,627✔
2234
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,627✔
2235
  } else {
2236
    (*ppDst)->colCids = NULL;
192,159✔
2237
  }
2238

2239
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
193,786✔
2240
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,037✔
2241
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,037✔
2242
  } else {
2243
    (*ppDst)->tagCids = NULL;
192,749✔
2244
  }
2245

2246
_exit:
193,786✔
2247

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

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

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

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

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